aboutsummaryrefslogtreecommitdiffhomepage
path: root/unsupported/Eigen/src/EulerAngles/EulerSystem.h
blob: ba33d54007b2899f3cb8ae656c8d023d1f9c793f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2015 Tal Hadad <tal_hd@hotmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef EIGEN_EULERSYSTEM_H
#define EIGEN_EULERSYSTEM_H

namespace Eigen
{
  // Forward declerations
  template <typename _Scalar, class _System>
  class EulerAngles;
  
  namespace internal
  {
    // TODO: Check if already exists on the rest API
    template <int Num, bool IsPossitive = (Num > 0)>
    struct Abs
    {
      enum { value = Num };
    };
  
    template <int Num>
    struct Abs<Num, false>
    {
      enum { value = -Num };
    };
  
    template <bool Cond>
    struct NegativeIf
    {
      template <typename T>
      static T run(const T& t)
      {
        return -t;
      }
    };
  
    template <>
    struct NegativeIf<false>
    {
      template <typename T>
      static T run(const T& t)
      {
        return t;
      }
    };
  
    template <bool Cond>
    struct NegateIf
    {
      template <typename T>
      static void run(T& t)
      {
        t = -t;
      }
    };
  
    template <>
    struct NegateIf<false>
    {
      template <typename T>
      static void run(T&)
      {
        // no op
      }
    };
  
    template <bool Cond1, bool Cond2>
    struct NegateIfXor : NegateIf<Cond1 != Cond2> {};

    template <int Axis>
    struct IsValidAxis
    {
      enum { value = Axis != 0 && Abs<Axis>::value <= 3 };
    };
  }
  
  enum EulerAxis
  {
    EULER_X = 1,
    EULER_Y = 2,
    EULER_Z = 3
  };

  template <int _HeadingAxis, int _PitchAxis, int _RollAxis>
  class EulerSystem
  {
    public:
    // It's defined this way and not as enum, because I think
    //  that enum is not guerantee to support negative numbers
    static const int HeadingAxis = _HeadingAxis;
    static const int PitchAxis = _PitchAxis;
    static const int RollAxis = _RollAxis;

    enum
    {
      HeadingAxisAbs = internal::Abs<HeadingAxis>::value,
      PitchAxisAbs = internal::Abs<PitchAxis>::value,
      RollAxisAbs = internal::Abs<RollAxis>::value,
      
      IsHeadingOpposite = (HeadingAxis < 0) ? 1 : 0,
      IsPitchOpposite = (PitchAxis < 0) ? 1 : 0,
      IsRollOpposite = (RollAxis < 0) ? 1 : 0,
      
      IsOdd = ((HeadingAxisAbs)%3 == (PitchAxisAbs - 1)%3) ? 0 : 1,
      IsEven = IsOdd ? 0 : 1,
      
      // TODO: Assert this, and sort it in a better way
      IsValid = ((unsigned)HeadingAxisAbs != (unsigned)PitchAxisAbs &&
        (unsigned)PitchAxisAbs != (unsigned)RollAxisAbs &&
        internal::IsValidAxis<HeadingAxis>::value && internal::IsValidAxis<PitchAxis>::value && internal::IsValidAxis<RollAxis>::value) ? 1 : 0,

      // TODO: After a proper assertation, remove the "IsValid" from this expression
      IsTaitBryan = (IsValid && (unsigned)HeadingAxisAbs != (unsigned)RollAxisAbs) ? 1 : 0
    };

    private:

    enum
    {
      // I, J, K are the pivot indexes permutation for the rotation matrix, that match this euler system. 
      // They are used in this class converters.
      // They are always different from each other, and their possible values are: 0, 1, or 2.
      I = HeadingAxisAbs - 1,
      J = (HeadingAxisAbs - 1 + 1 + IsOdd)%3,
      K = (HeadingAxisAbs - 1 + 2 - IsOdd)%3
    };
    
    template <typename Derived>
    static void eulerAngles_imp(Matrix<typename MatrixBase<Derived>::Scalar, 3, 1>& res, const MatrixBase<Derived>& mat, internal::true_type /*isTaitBryan*/)
    {
      using std::atan2;
      using std::sin;
      using std::cos;
      
      typedef typename Derived::Scalar Scalar;
      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))) {
        res[0] = (res[0] > Scalar(0)) ? res[0] - Scalar(M_PI) : res[0] + Scalar(M_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 eulerAngles_imp(Matrix<typename MatrixBase<Derived>::Scalar,3,1>& res, const MatrixBase<Derived>& mat, internal::false_type /*isTaitBryan*/)
    {
      using std::atan2;
      using std::sin;
      using std::cos;

      typedef typename Derived::Scalar Scalar;
      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)))
      {
        res[0] = (res[0] > Scalar(0)) ? res[0] - Scalar(M_PI) : res[0] + Scalar(M_PI);
        Scalar s2 = Vector2(mat(J,I), mat(K,I)).norm();
        res[1] = -atan2(s2, mat(I,I));
      }
      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));
    }

    public:
    
    template<typename Scalar>
    static void eulerAngles(EulerAngles<Scalar, EulerSystem>& res, const typename EulerAngles<Scalar, EulerSystem>::Matrix3& mat)
    {
      eulerAngles_imp(
        res.coeffs(), mat,
        typename internal::conditional<IsTaitBryan, internal::true_type, internal::false_type>::type());

      internal::NegateIfXor<IsHeadingOpposite, IsEven>::run(res.h());

      internal::NegateIfXor<IsPitchOpposite, IsEven>::run(res.p());

      internal::NegateIfXor<IsRollOpposite, IsEven>::run(res.r());
    }
  };

  typedef EulerSystem<EULER_X, EULER_Y, EULER_Z> EulerSystemXYZ;
  typedef EulerSystem<EULER_X, EULER_Y, EULER_X> EulerSystemXYX;
  typedef EulerSystem<EULER_X, EULER_Z, EULER_Y> EulerSystemXZY;
  typedef EulerSystem<EULER_X, EULER_Z, EULER_X> EulerSystemXZX;

  typedef EulerSystem<EULER_Y, EULER_Z, EULER_X> EulerSystemYZX;
  typedef EulerSystem<EULER_Y, EULER_Z, EULER_Y> EulerSystemYZY;
  typedef EulerSystem<EULER_Y, EULER_X, EULER_Z> EulerSystemYXZ;
  typedef EulerSystem<EULER_Y, EULER_X, EULER_Y> EulerSystemYXY;

  typedef EulerSystem<EULER_Z, EULER_X, EULER_Y> EulerSystemZXY;
  typedef EulerSystem<EULER_Z, EULER_X, EULER_Z> EulerSystemZXZ;
  typedef EulerSystem<EULER_Z, EULER_Y, EULER_X> EulerSystemZYX;
  typedef EulerSystem<EULER_Z, EULER_Y, EULER_Z> EulerSystemZYZ;
}

#endif // EIGEN_EULERSYSTEM_H