diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2018-07-03 07:37:12 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2018-07-03 08:13:09 +0200 |
commit | 88534ba623421c956d8ffcda2d27f41d704d15ef (patch) | |
tree | fccc55245aec3f7381cd525a1355568e10ea37f4 /eigen/unsupported/Eigen/src/EulerAngles/EulerSystem.h | |
parent | 3ee09beb3f0458fbeb0b0e816f854b9d5b406e6b (diff) |
update eigen
Diffstat (limited to 'eigen/unsupported/Eigen/src/EulerAngles/EulerSystem.h')
-rw-r--r-- | eigen/unsupported/Eigen/src/EulerAngles/EulerSystem.h | 184 |
1 files changed, 102 insertions, 82 deletions
diff --git a/eigen/unsupported/Eigen/src/EulerAngles/EulerSystem.h b/eigen/unsupported/Eigen/src/EulerAngles/EulerSystem.h index 28f52da..98f9f64 100644 --- a/eigen/unsupported/Eigen/src/EulerAngles/EulerSystem.h +++ b/eigen/unsupported/Eigen/src/EulerAngles/EulerSystem.h @@ -18,7 +18,7 @@ namespace Eigen namespace internal { - // TODO: Add this trait to the Eigen internal API? + // TODO: Check if already exists on the rest API template <int Num, bool IsPositive = (Num > 0)> struct Abs { @@ -36,12 +36,6 @@ namespace Eigen { enum { value = Axis != 0 && Abs<Axis>::value <= 3 }; }; - - template<typename System, - typename Other, - int OtherRows=Other::RowsAtCompileTime, - int OtherCols=Other::ColsAtCompileTime> - struct eulerangles_assign_impl; } #define EIGEN_EULER_ANGLES_CLASS_STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1] @@ -75,7 +69,7 @@ namespace Eigen * * You can use this class to get two things: * - Build an Euler system, and then pass it as a template parameter to EulerAngles. - * - Query some compile time data about an Euler system. (e.g. Whether it's Tait-Bryan) + * - Query some compile time data about an Euler system. (e.g. Whether it's tait bryan) * * Euler rotation is a set of three rotation on fixed axes. (see \ref EulerAngles) * This meta-class store constantly those signed axes. (see \ref EulerAxis) @@ -86,7 +80,7 @@ namespace Eigen * signed axes{+X,+Y,+Z,-X,-Y,-Z} are supported: * - all axes X, Y, Z in each valid order (see below what order is valid) * - rotation over the axis is supported both over the positive and negative directions. - * - both Tait-Bryan and proper/classic Euler angles (i.e. the opposite). + * - both tait bryan and proper/classic Euler angles (i.e. the opposite). * * Since EulerSystem support both positive and negative directions, * you may call this rotation distinction in other names: @@ -96,7 +90,7 @@ namespace Eigen * Notice all axed combination are valid, and would trigger a static assertion. * Same unsigned axes can't be neighbors, e.g. {X,X,Y} is invalid. * This yield two and only two classes: - * - _Tait-Bryan_ - all unsigned axes are distinct, e.g. {X,Y,Z} + * - _tait bryan_ - all unsigned axes are distinct, e.g. {X,Y,Z} * - _proper/classic Euler angles_ - The first and the third unsigned axes is equal, * and the second is different, e.g. {X,Y,X} * @@ -118,9 +112,9 @@ namespace Eigen * * \tparam _AlphaAxis the first fixed EulerAxis * - * \tparam _BetaAxis the second fixed EulerAxis + * \tparam _AlphaAxis the second fixed EulerAxis * - * \tparam _GammaAxis the third fixed EulerAxis + * \tparam _AlphaAxis the third fixed EulerAxis */ template <int _AlphaAxis, int _BetaAxis, int _GammaAxis> class EulerSystem @@ -144,16 +138,14 @@ namespace Eigen BetaAxisAbs = internal::Abs<BetaAxis>::value, /*!< the second rotation axis unsigned */ GammaAxisAbs = internal::Abs<GammaAxis>::value, /*!< the third rotation axis unsigned */ - IsAlphaOpposite = (AlphaAxis < 0) ? 1 : 0, /*!< whether alpha axis is negative */ - IsBetaOpposite = (BetaAxis < 0) ? 1 : 0, /*!< whether beta axis is negative */ - IsGammaOpposite = (GammaAxis < 0) ? 1 : 0, /*!< whether gamma axis is negative */ - - // Parity is even if alpha axis X is followed by beta axis Y, or Y is followed - // by Z, or Z is followed by X; otherwise it is odd. - IsOdd = ((AlphaAxisAbs)%3 == (BetaAxisAbs - 1)%3) ? 0 : 1, /*!< whether the Euler system is odd */ - IsEven = IsOdd ? 0 : 1, /*!< whether the Euler system is even */ + IsAlphaOpposite = (AlphaAxis < 0) ? 1 : 0, /*!< weather alpha axis is negative */ + IsBetaOpposite = (BetaAxis < 0) ? 1 : 0, /*!< weather beta axis is negative */ + IsGammaOpposite = (GammaAxis < 0) ? 1 : 0, /*!< weather gamma axis is negative */ + + IsOdd = ((AlphaAxisAbs)%3 == (BetaAxisAbs - 1)%3) ? 0 : 1, /*!< weather the Euler system is odd */ + IsEven = IsOdd ? 0 : 1, /*!< weather the Euler system is even */ - IsTaitBryan = ((unsigned)AlphaAxisAbs != (unsigned)GammaAxisAbs) ? 1 : 0 /*!< whether the Euler system is Tait-Bryan */ + IsTaitBryan = ((unsigned)AlphaAxisAbs != (unsigned)GammaAxisAbs) ? 1 : 0 /*!< weather the Euler system is tait bryan */ }; private: @@ -188,70 +180,71 @@ namespace Eigen static void CalcEulerAngles_imp(Matrix<typename MatrixBase<Derived>::Scalar, 3, 1>& res, const MatrixBase<Derived>& mat, internal::true_type /*isTaitBryan*/) { using std::atan2; - using std::sqrt; + using std::sin; + using std::cos; typedef typename Derived::Scalar Scalar; - - const Scalar plusMinus = IsEven? 1 : -1; - const Scalar minusPlus = IsOdd? 1 : -1; - - const Scalar Rsum = sqrt((mat(I,I) * mat(I,I) + mat(I,J) * mat(I,J) + mat(J,K) * mat(J,K) + mat(K,K) * mat(K,K))/2); - res[1] = atan2(plusMinus * mat(I,K), Rsum); - - // There is a singularity when cos(beta) == 0 - if(Rsum > 4 * NumTraits<Scalar>::epsilon()) {// cos(beta) != 0 - res[0] = atan2(minusPlus * mat(J, K), mat(K, K)); - res[2] = atan2(minusPlus * mat(I, J), mat(I, I)); - } - else if(plusMinus * mat(I, K) > 0) {// cos(beta) == 0 and sin(beta) == 1 - Scalar spos = mat(J, I) + plusMinus * mat(K, J); // 2*sin(alpha + plusMinus * gamma - Scalar cpos = mat(J, J) + minusPlus * mat(K, I); // 2*cos(alpha + plusMinus * gamma) - Scalar alphaPlusMinusGamma = atan2(spos, cpos); - res[0] = alphaPlusMinusGamma; - res[2] = 0; - } - else {// cos(beta) == 0 and sin(beta) == -1 - Scalar sneg = plusMinus * (mat(K, J) + minusPlus * mat(J, I)); // 2*sin(alpha + minusPlus*gamma) - Scalar cneg = mat(J, J) + plusMinus * mat(K, I); // 2*cos(alpha + minusPlus*gamma) - Scalar alphaMinusPlusBeta = atan2(sneg, cneg); - res[0] = alphaMinusPlusBeta; - res[2] = 0; + typedef Matrix<Scalar,2,1> Vector2; + + res[0] = atan2(mat(J,K), mat(K,K)); + Scalar c2 = Vector2(mat(I,I), mat(I,J)).norm(); + if((IsOdd && res[0]<Scalar(0)) || ((!IsOdd) && res[0]>Scalar(0))) { + if(res[0] > Scalar(0)) { + res[0] -= Scalar(EIGEN_PI); + } + else { + res[0] += Scalar(EIGEN_PI); + } + res[1] = atan2(-mat(I,K), -c2); } + else + res[1] = atan2(-mat(I,K), c2); + Scalar s1 = sin(res[0]); + Scalar c1 = cos(res[0]); + res[2] = atan2(s1*mat(K,I)-c1*mat(J,I), c1*mat(J,J) - s1 * mat(K,J)); } template <typename Derived> - static void CalcEulerAngles_imp(Matrix<typename MatrixBase<Derived>::Scalar,3,1>& res, - const MatrixBase<Derived>& mat, internal::false_type /*isTaitBryan*/) + static void CalcEulerAngles_imp(Matrix<typename MatrixBase<Derived>::Scalar,3,1>& res, const MatrixBase<Derived>& mat, internal::false_type /*isTaitBryan*/) { using std::atan2; - using std::sqrt; + using std::sin; + using std::cos; typedef typename Derived::Scalar Scalar; - - const Scalar plusMinus = IsEven? 1 : -1; - const Scalar minusPlus = IsOdd? 1 : -1; - - const Scalar Rsum = sqrt((mat(I, J) * mat(I, J) + mat(I, K) * mat(I, K) + mat(J, I) * mat(J, I) + mat(K, I) * mat(K, I)) / 2); - - res[1] = atan2(Rsum, mat(I, I)); - - // There is a singularity when sin(beta) == 0 - if(Rsum > 4 * NumTraits<Scalar>::epsilon()) {// sin(beta) != 0 - res[0] = atan2(mat(J, I), minusPlus * mat(K, I)); - res[2] = atan2(mat(I, J), plusMinus * mat(I, K)); - } - else if(mat(I, I) > 0) {// sin(beta) == 0 and cos(beta) == 1 - Scalar spos = plusMinus * mat(K, J) + minusPlus * mat(J, K); // 2*sin(alpha + gamma) - Scalar cpos = mat(J, J) + mat(K, K); // 2*cos(alpha + gamma) - res[0] = atan2(spos, cpos); - res[2] = 0; + typedef Matrix<Scalar,2,1> Vector2; + + res[0] = atan2(mat(J,I), mat(K,I)); + if((IsOdd && res[0]<Scalar(0)) || ((!IsOdd) && res[0]>Scalar(0))) + { + if(res[0] > Scalar(0)) { + res[0] -= Scalar(EIGEN_PI); + } + else { + res[0] += Scalar(EIGEN_PI); + } + Scalar s2 = Vector2(mat(J,I), mat(K,I)).norm(); + res[1] = -atan2(s2, mat(I,I)); } - else {// sin(beta) == 0 and cos(beta) == -1 - Scalar sneg = plusMinus * mat(K, J) + plusMinus * mat(J, K); // 2*sin(alpha - gamma) - Scalar cneg = mat(J, J) - mat(K, K); // 2*cos(alpha - gamma) - res[0] = atan2(sneg, cneg); - res[2] = 0; + else + { + Scalar s2 = Vector2(mat(J,I), mat(K,I)).norm(); + res[1] = atan2(s2, mat(I,I)); } + + // With a=(0,1,0), we have i=0; j=1; k=2, and after computing the first two angles, + // we can compute their respective rotation, and apply its inverse to M. Since the result must + // be a rotation around x, we have: + // + // c2 s1.s2 c1.s2 1 0 0 + // 0 c1 -s1 * M = 0 c3 s3 + // -s2 s1.c2 c1.c2 0 -s3 c3 + // + // Thus: m11.c1 - m21.s1 = c3 & m12.c1 - m22.s1 = s3 + + Scalar s1 = sin(res[0]); + Scalar c1 = cos(res[0]); + res[2] = atan2(c1*mat(J,K)-s1*mat(K,K), c1*mat(J,J) - s1 * mat(K,J)); } template<typename Scalar> @@ -259,28 +252,55 @@ namespace Eigen EulerAngles<Scalar, EulerSystem>& res, const typename EulerAngles<Scalar, EulerSystem>::Matrix3& mat) { + CalcEulerAngles(res, mat, false, false, false); + } + + template< + bool PositiveRangeAlpha, + bool PositiveRangeBeta, + bool PositiveRangeGamma, + typename Scalar> + static void CalcEulerAngles( + EulerAngles<Scalar, EulerSystem>& res, + const typename EulerAngles<Scalar, EulerSystem>::Matrix3& mat) + { + CalcEulerAngles(res, mat, PositiveRangeAlpha, PositiveRangeBeta, PositiveRangeGamma); + } + + template<typename Scalar> + static void CalcEulerAngles( + EulerAngles<Scalar, EulerSystem>& res, + const typename EulerAngles<Scalar, EulerSystem>::Matrix3& mat, + bool PositiveRangeAlpha, + bool PositiveRangeBeta, + bool PositiveRangeGamma) + { CalcEulerAngles_imp( res.angles(), mat, typename internal::conditional<IsTaitBryan, internal::true_type, internal::false_type>::type()); - if (IsAlphaOpposite) + if (IsAlphaOpposite == IsOdd) res.alpha() = -res.alpha(); - if (IsBetaOpposite) + if (IsBetaOpposite == IsOdd) res.beta() = -res.beta(); - if (IsGammaOpposite) + if (IsGammaOpposite == IsOdd) res.gamma() = -res.gamma(); + + // Saturate results to the requested range + if (PositiveRangeAlpha && (res.alpha() < 0)) + res.alpha() += Scalar(2 * EIGEN_PI); + + if (PositiveRangeBeta && (res.beta() < 0)) + res.beta() += Scalar(2 * EIGEN_PI); + + if (PositiveRangeGamma && (res.gamma() < 0)) + res.gamma() += Scalar(2 * EIGEN_PI); } template <typename _Scalar, class _System> friend class Eigen::EulerAngles; - - template<typename System, - typename Other, - int OtherRows, - int OtherCols> - friend struct internal::eulerangles_assign_impl; }; #define EIGEN_EULER_SYSTEM_TYPEDEF(A, B, C) \ |