Skip to content
This repository was archived by the owner on Nov 17, 2021. It is now read-only.

Commit dbb0e9b

Browse files
committed
Quaternion/Vector: Small refactor for review: put more comments, switched type conversions, took out default destination vector because confusing
1 parent 8f959d0 commit dbb0e9b

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

matrix/Quaternion.hpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,19 @@ class Quaternion : public Vector<Type, 4>
189189
/**
190190
* Quaternion from two vectors
191191
* Generates shortest rotation from source to destination vector
192-
* Default destination if not specified is [0,0,1]
193192
*
194193
* @param dst destination vector (no need to normalize)
195194
* @param src source vector (no need to normalize)
196195
* @param eps epsilon threshold which decides if a value is considered zero
197196
*/
198-
Quaternion(const Vector3<Type> &src, const Vector<Type, 3> dst = Vector3<Type>(0, 0, 1), const Type eps = 1e-5f) :
197+
Quaternion(const Vector3<Type> &src, const Vector3<Type> &dst, const Type eps = Type(1e-5)) :
199198
Vector<Type, 4>()
200199
{
201200
Quaternion &q = *this;
202201
Vector3<Type> cr = src.cross(dst);
203202
float dt = src.dot(dst);
203+
/* If the two vectors are parallel, cross product is zero
204+
* If they point opposite, the dot product is negative */
204205
if (cr.norm() < eps && dt < 0) {
205206
cr = src.abs();
206207
if (cr(0) < cr(1)) {
@@ -219,6 +220,7 @@ class Quaternion : public Vector<Type, 4>
219220
q(0) = Type(0);
220221
cr = src.cross(cr);
221222
} else {
223+
/* Half-Way Quaternion Solution */
222224
q(0) = src.dot(dst) + sqrt(src.norm_squared() * dst.norm_squared());
223225
}
224226
q(1) = cr(0);

matrix/Vector.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class Vector : public Matrix<Type, M, 1>
8888
return (*this) / norm();
8989
}
9090

91-
Vector unit_or_zero(const Type eps = 1e-5f) {
91+
Vector unit_or_zero(const Type eps = Type(1e-5)) {
9292
const Type n = norm();
9393
if (n > eps) {
9494
return (*this) / n;

test/attitude.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ int main()
4040
TEST(fabs(q(3) - 4) < eps);
4141

4242
// quaternion ctor: vector to vector
43-
Vector3f v1(0.f, 0.f, 1.f);
44-
// identity & default destination vector test
45-
Quatf quat_v(v1);
46-
TEST(isEqual(quat_v.conjugate(v1), v1));
43+
// identity test
44+
Quatf quat_v(v,v);
45+
TEST(isEqual(quat_v.conjugate(v), v));
4746
// random test (vector norm can not be preserved with a pure rotation)
48-
v1 = Vector3f(-80.1f, 1.5f, -6.89f);
47+
Vector3f v1(-80.1f, 1.5f, -6.89f);
4948
quat_v = Quatf(v1, v);
5049
TEST(isEqual(quat_v.conjugate(v1).normalized() * v.norm(), v));
5150
// special 180 degree case 1

0 commit comments

Comments
 (0)