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

Quaternion: Shortest rotation between two vectors #55

Merged
merged 5 commits into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions matrix/Quaternion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,49 @@ class Quaternion : public Vector<Type, 4>
}
}

/**
* Quaternion from two vectors
* Generates shortest rotation from source to destination vector
*
* @param dst destination vector (no need to normalize)
* @param src source vector (no need to normalize)
* @param eps epsilon threshold which decides if a value is considered zero
*/
Quaternion(const Vector3<Type> &src, const Vector3<Type> &dst, const Type eps = Type(1e-5)) :
Vector<Type, 4>()
{
Quaternion &q = *this;
Vector3<Type> cr = src.cross(dst);
float dt = src.dot(dst);
/* If the two vectors are parallel, cross product is zero
* If they point opposite, the dot product is negative */
if (cr.norm() < eps && dt < 0) {
cr = src.abs();
if (cr(0) < cr(1)) {
if (cr(0) < cr(2)) {
cr = Vector3<Type>(1, 0, 0);
} else {
cr = Vector3<Type>(0, 0, 1);
}
} else {
if (cr(1) < cr(2)) {
cr = Vector3<Type>(0, 1, 0);
} else {
cr = Vector3<Type>(0, 0, 1);
}
}
q(0) = Type(0);
cr = src.cross(cr);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I get it what you do here with the cr manipulation, but I cannot really verify that it is correct or not?
Do you have a link to an example?

Copy link
Member Author

@MaEtUgR MaEtUgR Nov 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a bulk of (code) examples summarized here: https://stackoverflow.com/questions/1171849/finding-quaternion-representing-the-rotation-from-one-vector-to-another

Also if you look at the unit tests you see in the comments which one is going through which case. Plus I can share the MATLAB code I used for intial prototyping of the functionality.

} else {
/* Half-Way Quaternion Solution */
q(0) = src.dot(dst) + sqrt(src.norm_squared() * dst.norm_squared());
}
q(1) = cr(0);
q(2) = cr(1);
q(3) = cr(2);
q.normalize();
}


/**
* Constructor from quaternion values
Expand Down
7 changes: 6 additions & 1 deletion matrix/Vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ class Vector : public Matrix<Type, M, 1>
return Type(sqrt(a.dot(a)));
}

Type norm_squared() const {
const Vector &a(*this);
return a.dot(a);
}

inline Type length() const {
return norm();
}
Expand All @@ -83,7 +88,7 @@ class Vector : public Matrix<Type, M, 1>
return (*this) / norm();
}

Vector unit_or_zero(const Type eps = 1e-5f) {
Vector unit_or_zero(const Type eps = Type(1e-5)) {
const Type n = norm();
if (n > eps) {
return (*this) / n;
Expand Down
33 changes: 28 additions & 5 deletions test/attitude.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ int main()
TEST(isEqual(e, e));

// euler vector ctor
Vector<float, 3> v;
v(0) = 0.1f;
v(1) = 0.2f;
v(2) = 0.3f;
Vector3f v(0.1f, 0.2f, 0.3f);
Eulerf euler_copy(v);
TEST(isEqual(euler_copy, euler_check));

Expand All @@ -42,6 +39,32 @@ int main()
TEST(fabs(q(2) - 3) < eps);
TEST(fabs(q(3) - 4) < eps);

// quaternion ctor: vector to vector
// identity test
Quatf quat_v(v,v);
TEST(isEqual(quat_v.conjugate(v), v));
// random test (vector norm can not be preserved with a pure rotation)
Vector3f v1(-80.1f, 1.5f, -6.89f);
quat_v = Quatf(v1, v);
TEST(isEqual(quat_v.conjugate(v1).normalized() * v.norm(), v));
// special 180 degree case 1
v1 = Vector3f(0.f, 1.f, 1.f);
quat_v = Quatf(v1, -v1);
TEST(isEqual(quat_v.conjugate(v1), -v1));
// special 180 degree case 2
v1 = Vector3f(1.f, 2.f, 0.f);
quat_v = Quatf(v1, -v1);
TEST(isEqual(quat_v.conjugate(v1), -v1));
// special 180 degree case 3
v1 = Vector3f(0.f, 0.f, 1.f);
quat_v = Quatf(v1, -v1);
TEST(isEqual(quat_v.conjugate(v1), -v1));
// special 180 degree case 4
v1 = Vector3f(1.f, 1.f, 1.f);
quat_v = Quatf(v1, -v1);
TEST(isEqual(quat_v.conjugate(v1), -v1));


// quat normalization
q.normalize();
TEST(isEqual(q, Quatf(0.18257419f, 0.36514837f,
Expand Down Expand Up @@ -297,7 +320,7 @@ int main()
TEST(isEqual(Dcmf(q), q.to_dcm()));

// conjugate
Vector3f v1(1.5f, 2.2f, 3.2f);
v = Vector3f(1.5f, 2.2f, 3.2f);
TEST(isEqual(q.conjugate_inversed(v1), Dcmf(q).T()*v1));
TEST(isEqual(q.conjugate(v1), Dcmf(q)*v1));

Expand Down
1 change: 1 addition & 0 deletions test/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ int main()

// norm, dot product
TEST(isEqualF(v1.norm(), 7.416198487095663f));
TEST(isEqualF(v1.norm_squared(), v1.norm() * v1.norm()));
TEST(isEqualF(v1.norm(), v1.length()));
TEST(isEqualF(v1.dot(v2), 130.0f));
TEST(isEqualF(v1.dot(v2), v1 * v2));
Expand Down