EulerSystem.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2015 Tal Hadad <tal_hd@hotmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_EULERSYSTEM_H
11 #define EIGEN_EULERSYSTEM_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen
16 {
17  // Forward declarations
18  template <typename Scalar_, class _System>
19  class EulerAngles;
20 
21  namespace internal
22  {
23  // TODO: Add this trait to the Eigen internal API?
24  template <int Num, bool IsPositive = (Num > 0)>
25  struct Abs
26  {
27  enum { value = Num };
28  };
29 
30  template <int Num>
31  struct Abs<Num, false>
32  {
33  enum { value = -Num };
34  };
35 
36  template <int Axis>
37  struct IsValidAxis
38  {
39  enum { value = Axis != 0 && Abs<Axis>::value <= 3 };
40  };
41 
42  template<typename System,
43  typename Other,
44  int OtherRows=Other::RowsAtCompileTime,
45  int OtherCols=Other::ColsAtCompileTime>
46  struct eulerangles_assign_impl;
47  }
48 
49  #define EIGEN_EULER_ANGLES_CLASS_STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1]
50 
63  enum EulerAxis
64  {
65  EULER_X = 1,
66  EULER_Y = 2,
67  EULER_Z = 3
68  };
69 
127  template <int _AlphaAxis, int _BetaAxis, int _GammaAxis>
129  {
130  public:
131  // It's defined this way and not as enum, because I think
132  // that enum is not guerantee to support negative numbers
133 
135  static constexpr int AlphaAxis = _AlphaAxis;
136 
138  static constexpr int BetaAxis = _BetaAxis;
139 
141  static constexpr int GammaAxis = _GammaAxis;
142 
143  enum
144  {
145  AlphaAxisAbs = internal::Abs<AlphaAxis>::value,
146  BetaAxisAbs = internal::Abs<BetaAxis>::value,
147  GammaAxisAbs = internal::Abs<GammaAxis>::value,
149  IsAlphaOpposite = (AlphaAxis < 0) ? 1 : 0,
150  IsBetaOpposite = (BetaAxis < 0) ? 1 : 0,
151  IsGammaOpposite = (GammaAxis < 0) ? 1 : 0,
153  // Parity is even if alpha axis X is followed by beta axis Y, or Y is followed
154  // by Z, or Z is followed by X; otherwise it is odd.
155  IsOdd = ((AlphaAxisAbs)%3 == (BetaAxisAbs - 1)%3) ? 0 : 1,
156  IsEven = IsOdd ? 0 : 1,
158  IsTaitBryan = ((unsigned)AlphaAxisAbs != (unsigned)GammaAxisAbs) ? 1 : 0
159  };
160 
161  private:
162 
163  EIGEN_EULER_ANGLES_CLASS_STATIC_ASSERT(internal::IsValidAxis<AlphaAxis>::value,
164  ALPHA_AXIS_IS_INVALID);
165 
166  EIGEN_EULER_ANGLES_CLASS_STATIC_ASSERT(internal::IsValidAxis<BetaAxis>::value,
167  BETA_AXIS_IS_INVALID);
168 
169  EIGEN_EULER_ANGLES_CLASS_STATIC_ASSERT(internal::IsValidAxis<GammaAxis>::value,
170  GAMMA_AXIS_IS_INVALID);
171 
173  ALPHA_AXIS_CANT_BE_EQUAL_TO_BETA_AXIS);
174 
176  BETA_AXIS_CANT_BE_EQUAL_TO_GAMMA_AXIS);
177 
178  static const int
179  // I, J, K are the pivot indexes permutation for the rotation matrix, that match this Euler system.
180  // They are used in this class converters.
181  // They are always different from each other, and their possible values are: 0, 1, or 2.
183  J_ = (AlphaAxisAbs - 1 + 1 + IsOdd)%3,
184  K_ = (AlphaAxisAbs - 1 + 2 - IsOdd)%3
185  ;
186 
187  // TODO: Get @mat parameter in form that avoids double evaluation.
188  template <typename Derived>
189  static void CalcEulerAngles_imp(Matrix<typename MatrixBase<Derived>::Scalar, 3, 1>& res, const MatrixBase<Derived>& mat, internal::true_type /*isTaitBryan*/)
190  {
191  using std::atan2;
192  using std::sqrt;
193 
194  typedef typename Derived::Scalar Scalar;
195 
196  const Scalar plusMinus = IsEven? 1 : -1;
197  const Scalar minusPlus = IsOdd? 1 : -1;
198 
199  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);
200  res[1] = atan2(plusMinus * mat(I_,K_), Rsum);
201 
202  // There is a singularity when cos(beta) == 0
203  if(Rsum > 4 * NumTraits<Scalar>::epsilon()) {// cos(beta) != 0
204  res[0] = atan2(minusPlus * mat(J_, K_), mat(K_, K_));
205  res[2] = atan2(minusPlus * mat(I_, J_), mat(I_, I_));
206  }
207  else if(plusMinus * mat(I_, K_) > 0) {// cos(beta) == 0 and sin(beta) == 1
208  Scalar spos = mat(J_, I_) + plusMinus * mat(K_, J_); // 2*sin(alpha + plusMinus * gamma
209  Scalar cpos = mat(J_, J_) + minusPlus * mat(K_, I_); // 2*cos(alpha + plusMinus * gamma)
210  Scalar alphaPlusMinusGamma = atan2(spos, cpos);
211  res[0] = alphaPlusMinusGamma;
212  res[2] = 0;
213  }
214  else {// cos(beta) == 0 and sin(beta) == -1
215  Scalar sneg = plusMinus * (mat(K_, J_) + minusPlus * mat(J_, I_)); // 2*sin(alpha + minusPlus*gamma)
216  Scalar cneg = mat(J_, J_) + plusMinus * mat(K_, I_); // 2*cos(alpha + minusPlus*gamma)
217  Scalar alphaMinusPlusBeta = atan2(sneg, cneg);
218  res[0] = alphaMinusPlusBeta;
219  res[2] = 0;
220  }
221  }
222 
223  template <typename Derived>
225  const MatrixBase<Derived>& mat, internal::false_type /*isTaitBryan*/)
226  {
227  using std::atan2;
228  using std::sqrt;
229 
230  typedef typename Derived::Scalar Scalar;
231 
232  const Scalar plusMinus = IsEven? 1 : -1;
233  const Scalar minusPlus = IsOdd? 1 : -1;
234 
235  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);
236 
237  res[1] = atan2(Rsum, mat(I_, I_));
238 
239  // There is a singularity when sin(beta) == 0
240  if(Rsum > 4 * NumTraits<Scalar>::epsilon()) {// sin(beta) != 0
241  res[0] = atan2(mat(J_, I_), minusPlus * mat(K_, I_));
242  res[2] = atan2(mat(I_, J_), plusMinus * mat(I_, K_));
243  }
244  else if(mat(I_, I_) > 0) {// sin(beta) == 0 and cos(beta) == 1
245  Scalar spos = plusMinus * mat(K_, J_) + minusPlus * mat(J_, K_); // 2*sin(alpha + gamma)
246  Scalar cpos = mat(J_, J_) + mat(K_, K_); // 2*cos(alpha + gamma)
247  res[0] = atan2(spos, cpos);
248  res[2] = 0;
249  }
250  else {// sin(beta) == 0 and cos(beta) == -1
251  Scalar sneg = plusMinus * mat(K_, J_) + plusMinus * mat(J_, K_); // 2*sin(alpha - gamma)
252  Scalar cneg = mat(J_, J_) - mat(K_, K_); // 2*cos(alpha - gamma)
253  res[0] = atan2(sneg, cneg);
254  res[2] = 0;
255  }
256  }
257 
258  template<typename Scalar>
259  static void CalcEulerAngles(
261  const typename EulerAngles<Scalar, EulerSystem>::Matrix3& mat)
262  {
264  res.angles(), mat,
265  std::conditional_t<IsTaitBryan, internal::true_type, internal::false_type>());
266 
267  if (IsAlphaOpposite)
268  res.alpha() = -res.alpha();
269 
270  if (IsBetaOpposite)
271  res.beta() = -res.beta();
272 
273  if (IsGammaOpposite)
274  res.gamma() = -res.gamma();
275  }
276 
277  template <typename Scalar_, class _System>
278  friend class Eigen::EulerAngles;
279 
280  template<typename System,
281  typename Other,
282  int OtherRows,
283  int OtherCols>
284  friend struct internal::eulerangles_assign_impl;
285  };
286 
287 #define EIGEN_EULER_SYSTEM_TYPEDEF(A, B, C) \
288  \
289  typedef EulerSystem<EULER_##A, EULER_##B, EULER_##C> EulerSystem##A##B##C;
290 
295 
300 
305 }
306 
307 #endif // EIGEN_EULERSYSTEM_H
#define EIGEN_EULER_SYSTEM_TYPEDEF(A, B, C)
Definition: EulerSystem.h:287
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
MatrixXf mat
internal::traits< Derived >::Scalar Scalar
Represents a rotation in a 3 dimensional space as three Euler angles.
Definition: EulerAngles.h:103
Represents a fixed Euler rotation system.
Definition: EulerSystem.h:129
static constexpr int AlphaAxis
Definition: EulerSystem.h:135
static const int I_
Definition: EulerSystem.h:182
static void CalcEulerAngles(EulerAngles< Scalar, EulerSystem > &res, const typename EulerAngles< Scalar, EulerSystem >::Matrix3 &mat)
Definition: EulerSystem.h:259
static constexpr int GammaAxis
Definition: EulerSystem.h:141
EIGEN_EULER_ANGLES_CLASS_STATIC_ASSERT(internal::IsValidAxis< AlphaAxis >::value, ALPHA_AXIS_IS_INVALID)
EIGEN_EULER_ANGLES_CLASS_STATIC_ASSERT(internal::IsValidAxis< BetaAxis >::value, BETA_AXIS_IS_INVALID)
static const int J_
Definition: EulerSystem.h:183
EIGEN_EULER_ANGLES_CLASS_STATIC_ASSERT((unsigned) BetaAxisAbs !=(unsigned) GammaAxisAbs, BETA_AXIS_CANT_BE_EQUAL_TO_GAMMA_AXIS)
EIGEN_EULER_ANGLES_CLASS_STATIC_ASSERT((unsigned) AlphaAxisAbs !=(unsigned) BetaAxisAbs, ALPHA_AXIS_CANT_BE_EQUAL_TO_BETA_AXIS)
static void CalcEulerAngles_imp(Matrix< typename MatrixBase< Derived >::Scalar, 3, 1 > &res, const MatrixBase< Derived > &mat, internal::true_type)
Definition: EulerSystem.h:189
static void CalcEulerAngles_imp(Matrix< typename MatrixBase< Derived >::Scalar, 3, 1 > &res, const MatrixBase< Derived > &mat, internal::false_type)
Definition: EulerSystem.h:224
EIGEN_EULER_ANGLES_CLASS_STATIC_ASSERT(internal::IsValidAxis< GammaAxis >::value, GAMMA_AXIS_IS_INVALID)
static const int K_
Definition: EulerSystem.h:184
static constexpr int BetaAxis
Definition: EulerSystem.h:138
EulerAxis
Representation of a fixed signed rotation axis for EulerSystem.
Definition: EulerSystem.h:64
@ EULER_X
Definition: EulerSystem.h:65
@ EULER_Z
Definition: EulerSystem.h:67
@ EULER_Y
Definition: EulerSystem.h:66
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > sqrt(const Eigen::AutoDiffScalar< DerType > &x)
AutoDiffScalar< Matrix< typename internal::traits< internal::remove_all_t< DerTypeA > >::Scalar, Dynamic, 1 > > atan2(const AutoDiffScalar< DerTypeA > &a, const AutoDiffScalar< DerTypeB > &b)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)