diff --git a/unsupported/Eigen/src/EulerAngles/EulerAngles.h b/unsupported/Eigen/src/EulerAngles/EulerAngles.h index b3bd66441..ccde28eb6 100644 --- a/unsupported/Eigen/src/EulerAngles/EulerAngles.h +++ b/unsupported/Eigen/src/EulerAngles/EulerAngles.h @@ -37,16 +37,26 @@ namespace Eigen typedef Matrix Vector3; typedef Quaternion QuaternionType; typedef AngleAxis AngleAxisType; + + static Vector3 HeadingAxisVector() { + return internal::NegativeIf::run(Vector3::Unit(System::HeadingAxisAbs - 1)); + } + + static Vector3 PitchAxisVector() { + return internal::NegativeIf::run(Vector3::Unit(System::PitchAxisAbs - 1)); + } + + static Vector3 RollAxisVector() { + return internal::NegativeIf::run(Vector3::Unit(System::RollAxisAbs - 1)); + } - protected: - + private: Vector3 m_angles; public: EulerAngles() {} inline EulerAngles(Scalar a0, Scalar a1, Scalar a2) : m_angles(a0, a1, a2) {} - inline EulerAngles(Vector3 angles) : m_angles(angles) {} inline EulerAngles(const QuaternionType& q) { *this = q; } inline EulerAngles(const AngleAxisType& aa) { *this = aa; } template @@ -116,7 +126,7 @@ namespace Eigen EulerAngles& operator=(const QuaternionType& q){ // TODO: Implement it in a better way // According to http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/ - // we can compute only the needed matrix cells and then convert to euler angles. + // we can compute only the needed matrix cells and then convert to euler angles. (see ZYX example below) // Currently we compute all matrix cells from quaternion. fromRotationMatrix(q.toRotationMatrix()); @@ -131,6 +141,8 @@ namespace Eigen return *this; } + // TODO: Support isApprox function + /** Set \c *this from AngleAxis \a ea. */ EulerAngles& operator=(const AngleAxisType& ea) diff --git a/unsupported/Eigen/src/EulerAngles/EulerSystem.h b/unsupported/Eigen/src/EulerAngles/EulerSystem.h index fc782e914..6ee3f51df 100644 --- a/unsupported/Eigen/src/EulerAngles/EulerSystem.h +++ b/unsupported/Eigen/src/EulerAngles/EulerSystem.h @@ -31,6 +31,26 @@ namespace Eigen enum { value = -Num }; }; + template + struct NegativeIf + { + template + static T run(const T& t) + { + return -t; + } + }; + + template <> + struct NegativeIf + { + template + static T run(const T& t) + { + return t; + } + }; + template struct NegateIf { @@ -45,7 +65,7 @@ namespace Eigen struct NegateIf { template - static void run(T& t) + static void run(T&) { // no op } @@ -113,7 +133,7 @@ namespace Eigen }; template - static void eulerAngles_imp(Matrix::Scalar, 3, 1>& res, const MatrixBase& mat, internal::true_type isTaitBryan) + static void eulerAngles_imp(Matrix::Scalar, 3, 1>& res, const MatrixBase& mat, internal::true_type /*isTaitBryan*/) { using std::atan2; using std::sin; @@ -136,7 +156,7 @@ namespace Eigen } template - static void eulerAngles_imp(Matrix::Scalar,3,1>& res, const MatrixBase& mat, internal::false_type isTaitBryan) + static void eulerAngles_imp(Matrix::Scalar,3,1>& res, const MatrixBase& mat, internal::false_type /*isTaitBryan*/) { using std::atan2; using std::sin; diff --git a/unsupported/test/EulerAngles.cpp b/unsupported/test/EulerAngles.cpp index d03db1ac3..57a34776a 100644 --- a/unsupported/test/EulerAngles.cpp +++ b/unsupported/test/EulerAngles.cpp @@ -11,8 +11,125 @@ #include +using namespace Eigen; + +template +void verify_euler(const Matrix& ea) +{ + typedef EulerAngles EulerAnglesType; + typedef Matrix Matrix3; + typedef Matrix Vector3; + typedef AngleAxis AngleAxisx; + using std::abs; + + const int i = EulerSystem::HeadingAxisAbs - 1; + const int j = EulerSystem::PitchAxisAbs - 1; + const int k = EulerSystem::RollAxisAbs - 1; + + const int iFactor = EulerSystem::IsHeadingOpposite ? -1 : 1; + const int jFactor = EulerSystem::IsPitchOpposite ? -1 : 1; + const int kFactor = EulerSystem::IsRollOpposite ? -1 : 1; + + const Vector3 I = EulerAnglesType::HeadingAxisVector(); + const Vector3 J = EulerAnglesType::PitchAxisVector(); + const Vector3 K = EulerAnglesType::RollAxisVector(); + + EulerAnglesType e(ea[0], ea[1], ea[2]); + + Matrix3 m(e); + Vector3 eabis = EulerAnglesType(m).coeffs(); + Vector3 eabis2 = m.eulerAngles(i, j, k); + eabis2[0] *= iFactor; + eabis2[1] *= jFactor; + eabis2[2] *= kFactor; + + VERIFY_IS_APPROX(eabis, eabis2);// Verify that our estimation is the same as m.eulerAngles() is + + Matrix3 mbis(AngleAxisx(eabis[0], I) * AngleAxisx(eabis[1], J) * AngleAxisx(eabis[2], K)); + VERIFY_IS_APPROX(m, mbis); + /* If I==K, and ea[1]==0, then there no unique solution. */ + /* The remark apply in the case where I!=K, and |ea[1]| is close to pi/2. */ + if( (i!=k || ea[1]!=0) && (i==k || !internal::isApprox(abs(ea[1]),Scalar(EIGEN_PI/2),test_precision())) ) + VERIFY((ea-eabis).norm() <= test_precision()); + + // approx_or_less_than does not work for 0 + VERIFY(0 < eabis[0] || test_isMuchSmallerThan(eabis[0], Scalar(1))); + VERIFY_IS_APPROX_OR_LESS_THAN(eabis[0], Scalar(EIGEN_PI)); + VERIFY_IS_APPROX_OR_LESS_THAN(-Scalar(EIGEN_PI), eabis[1]); + VERIFY_IS_APPROX_OR_LESS_THAN(eabis[1], Scalar(EIGEN_PI)); + VERIFY_IS_APPROX_OR_LESS_THAN(-Scalar(EIGEN_PI), eabis[2]); + VERIFY_IS_APPROX_OR_LESS_THAN(eabis[2], Scalar(EIGEN_PI)); +} + +template void check_all_var(const Matrix& ea) +{ + verify_euler(ea); + verify_euler(ea); + verify_euler(ea); + verify_euler(ea); + + verify_euler(ea); + verify_euler(ea); + verify_euler(ea); + verify_euler(ea); + + verify_euler(ea); + verify_euler(ea); + verify_euler(ea); + verify_euler(ea); +} + +template void eulerangles() +{ + typedef Matrix Matrix3; + typedef Matrix Vector3; + typedef Array Array3; + typedef Quaternion Quaternionx; + typedef AngleAxis AngleAxisx; + + Scalar a = internal::random(-Scalar(EIGEN_PI), Scalar(EIGEN_PI)); + Quaternionx q1; + q1 = AngleAxisx(a, Vector3::Random().normalized()); + Matrix3 m; + m = q1; + + Vector3 ea = m.eulerAngles(0,1,2); + check_all_var(ea); + ea = m.eulerAngles(0,1,0); + check_all_var(ea); + + // Check with purely random Quaternion: + q1.coeffs() = Quaternionx::Coefficients::Random().normalized(); + m = q1; + ea = m.eulerAngles(0,1,2); + check_all_var(ea); + ea = m.eulerAngles(0,1,0); + check_all_var(ea); + + // Check with random angles in range [0:pi]x[-pi:pi]x[-pi:pi]. + ea = (Array3::Random() + Array3(1,0,0))*Scalar(EIGEN_PI)*Array3(0.5,1,1); + check_all_var(ea); + + ea[2] = ea[0] = internal::random(0,Scalar(EIGEN_PI)); + check_all_var(ea); + + ea[0] = ea[1] = internal::random(0,Scalar(EIGEN_PI)); + check_all_var(ea); + + ea[1] = 0; + check_all_var(ea); + + ea.head(2).setZero(); + check_all_var(ea); + + ea.setZero(); + check_all_var(ea); +} + void test_EulerAngles() { - //CALL_SUBTEST( test_return_by_value(32) ); - + for(int i = 0; i < g_repeat; i++) { + CALL_SUBTEST_1( eulerangles() ); + CALL_SUBTEST_2( eulerangles() ); + } }