Logo ROOT  
Reference Guide
EulerAngles.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: W. Brown, M. Fischler, L. Moneta 2005
3 
4  /**********************************************************************
5  * *
6  * Copyright (c) 2005 , LCG ROOT MathLib Team *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class EulerAngles
12 //
13 // Created by: Lorenzo Moneta at Tue May 10 17:55:10 2005
14 //
15 // Last update: Tue May 10 17:55:10 2005
16 //
17 #ifndef ROOT_Math_GenVector_EulerAngles
18 #define ROOT_Math_GenVector_EulerAngles 1
19 
25 #include <algorithm>
26 #include <cassert>
27 
28 namespace ROOT {
29 namespace Math {
30 
31 
32 //__________________________________________________________________________________________
33  /**
34  EulerAngles class describing rotation as three angles (Euler Angles).
35  The Euler angles definition matches that of Classical Mechanics (Goldstein).
36  It is also the same convention defined in
37  <A HREF="http://mathworld.wolfram.com/EulerAngles.html">mathworld</A>
38  and used in Mathematica and CLHEP. Note that the ROOT class TRotation defines
39  a slightly different convention.
40 
41  @ingroup GenVector
42  */
43 class EulerAngles {
44 
45 public:
46 
47  typedef double Scalar;
48 
49  /**
50  Default constructor
51  */
52  EulerAngles() : fPhi(0.0), fTheta(0.0), fPsi(0.0) { }
53 
54  /**
55  Constructor from phi, theta and psi
56  */
57  EulerAngles( Scalar phi, Scalar theta, Scalar psi ) :
58  fPhi(phi), fTheta(theta), fPsi(psi)
59  {Rectify();} // Added 27 Jan. 06 JMM
60 
61  /**
62  Construct given a pair of pointers or iterators defining the
63  beginning and end of an array of three Scalars, to be treated as
64  the angles phi, theta and psi.
65  */
66  template<class IT>
67  EulerAngles(IT begin, IT end) { SetComponents(begin,end); }
68 
69  // The compiler-generated copy ctor, copy assignment, and dtor are OK.
70 
71  /**
72  Re-adjust components place angles in canonical ranges
73  */
74  void Rectify();
75 
76 
77  // ======== Construction and assignement from any other rotation ==================
78 
79  /**
80  Create from any other supported rotation (see gv_detail::convert )
81  */
82  template <class OtherRotation>
83  explicit EulerAngles(const OtherRotation & r) {gv_detail::convert(r,*this);}
84 
85  /**
86  Assign from any other rotation (see gv_detail::convert )
87  */
88  template <class OtherRotation>
89  EulerAngles & operator=( OtherRotation const & r ) {
90  gv_detail::convert(r,*this);
91  return *this;
92  }
93 
94 #ifdef OLD
95  explicit EulerAngles(const Rotation3D & r) {gv_detail::convert(r,*this);}
96 
97  /**
98  Construct from a rotation matrix
99  */
100  explicit EulerAngles(const Rotation3D & r) {gv_detail::convert(r,*this);}
101 
102  /**
103  Construct from a rotation represented by a Quaternion
104  */
105  explicit EulerAngles(const Quaternion & q) {gv_detail::convert(q,*this);}
106 
107  /**
108  Construct from an AxisAngle
109  */
110  explicit EulerAngles(const AxisAngle & a ) { gv_detail::convert(a, *this); }
111 
112  /**
113  Construct from an axial rotation
114  */
115  explicit EulerAngles( RotationZ const & r ) { gv_detail::convert(r, *this); }
116  explicit EulerAngles( RotationY const & r ) { gv_detail::convert(r, *this); }
117  explicit EulerAngles( RotationX const & r ) { gv_detail::convert(r, *this); }
118 
119 
120  /**
121  Assign from an AxisAngle
122  */
123  EulerAngles &
124  operator=( AxisAngle const & a ) { return operator=(EulerAngles(a)); }
125 
126  /**
127  Assign from a Quaternion
128  */
129  EulerAngles &
130  operator=( Quaternion const & q ) {return operator=(EulerAngles(q)); }
131 
132  /**
133  Assign from an axial rotation
134  */
135  EulerAngles &
136  operator=( RotationZ const & r ) { return operator=(EulerAngles(r)); }
137  EulerAngles &
138  operator=( RotationY const & r ) { return operator=(EulerAngles(r)); }
139  EulerAngles &
140  operator=( RotationX const & r ) { return operator=(EulerAngles(r)); }
141 
142 #endif
143 
144  // ======== Components ==============
145 
146  /**
147  Set the three Euler angles given a pair of pointers or iterators
148  defining the beginning and end of an array of three Scalars.
149  */
150  template<class IT>
151  void SetComponents(IT begin, IT end) {
152  fPhi = *begin++;
153  fTheta = *begin++;
154  fPsi = *begin++;
155  (void)end;
156  assert(begin == end);
157  Rectify(); // Added 27 Jan. 06 JMM
158  }
159 
160  /**
161  Get the axis and then the angle into data specified by an iterator begin
162  and another to the end of the desired data (4 past start).
163  */
164  template<class IT>
165  void GetComponents(IT begin, IT end) const {
166  *begin++ = fPhi;
167  *begin++ = fTheta;
168  *begin++ = fPsi;
169  (void)end;
170  assert(begin == end);
171  }
172 
173  /**
174  Get the axis and then the angle into data specified by an iterator begin
175  */
176  template<class IT>
177  void GetComponents(IT begin) const {
178  *begin++ = fPhi;
179  *begin++ = fTheta;
180  *begin = fPsi;
181  }
182 
183  /**
184  Set the components phi, theta, psi based on three Scalars.
185  */
186  void SetComponents(Scalar phi, Scalar theta, Scalar psi) {
187  fPhi=phi; fTheta=theta; fPsi=psi;
188  Rectify(); // Added 27 Jan. 06 JMM
189  }
190 
191  /**
192  Get the components phi, theta, psi into three Scalars.
193  */
194  void GetComponents(Scalar & phi, Scalar & theta, Scalar & psi) const {
195  phi=fPhi; theta=fTheta; psi=fPsi;
196  }
197 
198  /**
199  Set Phi Euler angle // JMM 30 Jan. 2006
200  */
201  void SetPhi(Scalar phi) { fPhi=phi; Rectify(); }
202 
203  /**
204  Return Phi Euler angle
205  */
206  Scalar Phi() const { return fPhi; }
207 
208  /**
209  Set Theta Euler angle // JMM 30 Jan. 2006
210  */
211  void SetTheta(Scalar theta) { fTheta=theta; Rectify(); }
212 
213  /**
214  Return Theta Euler angle
215  */
216  Scalar Theta() const { return fTheta; }
217 
218  /**
219  Set Psi Euler angle // JMM 30 Jan. 2006
220  */
221  void SetPsi(Scalar psi) { fPsi=psi; Rectify(); }
222 
223  /**
224  Return Psi Euler angle
225  */
226  Scalar Psi() const { return fPsi; }
227 
228  // =========== operations ==============
229 
230 
231  /**
232  Rotation operation on a displacement vector in any coordinate system and tag
233  */
234  template <class CoordSystem, class U>
237  return Rotation3D(*this) ( v );
238  }
239 
240  /**
241  Rotation operation on a position vector in any coordinate system
242  */
243  template <class CoordSystem, class U>
248  return PositionVector3D<CoordSystem,U> ( rxyz );
249  }
250 
251  /**
252  Rotation operation on a Lorentz vector in any 4D coordinate system
253  */
254  template <class CoordSystem>
256  operator() (const LorentzVector<CoordSystem> & v) const {
258  xyz = operator()(xyz);
259  LorentzVector< PxPyPzE4D<double> > xyzt (xyz.X(), xyz.Y(), xyz.Z(), v.E());
260  return LorentzVector<CoordSystem> ( xyzt );
261  }
262 
263  /**
264  Rotation operation on an arbitrary vector v.
265  Preconditions: v must implement methods x(), y(), and z()
266  and the arbitrary vector type must have a constructor taking (x,y,z)
267  */
268  template <class ForeignVector>
269  ForeignVector
270  operator() (const ForeignVector & v) const {
273  return ForeignVector ( rxyz.X(), rxyz.Y(), rxyz.Z() );
274  }
275 
276  /**
277  Overload operator * for rotation on a vector
278  */
279  template <class AVector>
280  inline
281  AVector operator* (const AVector & v) const
282  {
283  return operator()(v);
284  }
285 
286  /**
287  Invert a rotation in place
288  */
289  // theta stays the same and negative rotation in Theta is done via a rotation
290  // of + PI in phi and Psi
291  void Invert() {
292  Scalar tmp = -fPhi;
293  fPhi = -fPsi + Pi();
294  fPsi=tmp + Pi();
295  }
296 
297  /**
298  Return inverse of a rotation
299  */
300  EulerAngles Inverse() const { return EulerAngles(-fPsi + Pi(), fTheta, -fPhi + Pi()); }
301 
302  // ========= Multi-Rotation Operations ===============
303 
304  /**
305  Multiply (combine) two rotations
306  */
307  EulerAngles operator * (const Rotation3D & r) const;
308  EulerAngles operator * (const AxisAngle & a) const;
309  EulerAngles operator * (const EulerAngles & e) const;
310  EulerAngles operator * (const Quaternion & q) const;
311  EulerAngles operator * (const RotationX & rx) const;
312  EulerAngles operator * (const RotationY & ry) const;
313  EulerAngles operator * (const RotationZ & rz) const;
314 
315  /**
316  Post-Multiply (on right) by another rotation : T = T*R
317  */
318  template <class R>
319  EulerAngles & operator *= (const R & r) { return *this = (*this)*r; }
320 
321  /**
322  Distance between two rotations
323  */
324  template <class R>
325  Scalar Distance ( const R & r ) const {return gv_detail::dist(*this,r);}
326 
327  /**
328  Equality/inequality operators
329  */
330  bool operator == (const EulerAngles & rhs) const {
331  if( fPhi != rhs.fPhi ) return false;
332  if( fTheta != rhs.fTheta ) return false;
333  if( fPsi != rhs.fPsi ) return false;
334  return true;
335  }
336  bool operator != (const EulerAngles & rhs) const {
337  return ! operator==(rhs);
338  }
339 
340 private:
341 
342  double fPhi; // Z rotation angle (first) defined in [-PI,PI]
343  double fTheta; // X rotation angle (second) defined only [0,PI]
344  double fPsi; // Z rotation angle (third) defined in [-PI,PI]
345 
346  static double Pi() { return M_PI; }
347 
348 }; // EulerAngles
349 
350 /**
351  Distance between two rotations
352  */
353 template <class R>
354 inline
355 typename EulerAngles::Scalar
356 Distance ( const EulerAngles& r1, const R & r2) {return gv_detail::dist(r1,r2);}
357 
358 /**
359  Multiplication of an axial rotation by an AxisAngle
360  */
361 EulerAngles operator* (RotationX const & r1, EulerAngles const & r2);
362 EulerAngles operator* (RotationY const & r1, EulerAngles const & r2);
363 EulerAngles operator* (RotationZ const & r1, EulerAngles const & r2);
364 
365 /**
366  Stream Output and Input
367  */
368  // TODO - I/O should be put in the manipulator form
369 
370 std::ostream & operator<< (std::ostream & os, const EulerAngles & e);
371 
372 } // namespace Math
373 } // namespace ROOT
374 
375 
376 #endif /* ROOT_Math_GenVector_EulerAngles */
ROOT::Math::EulerAngles::fPhi
double fPhi
Definition: EulerAngles.h:357
ROOT::Math::EulerAngles::Theta
Scalar Theta() const
Return Theta Euler angle.
Definition: EulerAngles.h:231
ROOT::Math::EulerAngles::Invert
void Invert()
Invert a rotation in place.
Definition: EulerAngles.h:306
ROOT::Math::EulerAngles::operator()
DisplacementVector3D< CoordSystem, U > operator()(const DisplacementVector3D< CoordSystem, U > &v) const
Rotation operation on a displacement vector in any coordinate system and tag.
Definition: EulerAngles.h:251
ROOT::Math::AxisAngle
AxisAngle class describing rotation represented with direction axis (3D Vector) and an angle of rotat...
Definition: AxisAngle.h:51
e
#define e(i)
Definition: RSha256.hxx:121
ROOT::Math::EulerAngles::SetPsi
void SetPsi(Scalar psi)
Set Psi Euler angle // JMM 30 Jan.
Definition: EulerAngles.h:236
ROOT::Math::EulerAngles::operator==
bool operator==(const EulerAngles &rhs) const
Equality/inequality operators.
Definition: EulerAngles.h:345
ROOT::Math::EulerAngles::GetComponents
void GetComponents(IT begin, IT end) const
Get the axis and then the angle into data specified by an iterator begin and another to the end of th...
Definition: EulerAngles.h:180
ROOT::Math::PositionVector3D
Class describing a generic position vector (point) in 3 dimensions.
Definition: PositionVector3D.h:63
r
ROOT::R::TRInterface & r
Definition: Object.C:4
ROOT::Math::RotationZ
Rotation class representing a 3D rotation about the Z axis by the angle of rotation.
Definition: RotationZ.h:53
ROOT::Math::Rotation3D
Rotation class with the (3D) rotation represented by a 3x3 orthogonal matrix.
Definition: Rotation3D.h:75
ROOT::Math::EulerAngles::Phi
Scalar Phi() const
Return Phi Euler angle.
Definition: EulerAngles.h:221
ROOT::Math::operator<<
std::ostream & operator<<(std::ostream &os, const AxisAngle &a)
Stream Output and Input.
Definition: AxisAngle.cxx:101
ROOT::Math::DisplacementVector3D::Y
Scalar Y() const
Cartesian Y, converting if necessary from internal coordinate system.
Definition: DisplacementVector3D.h:287
ROOT::Math::EulerAngles::Pi
static double Pi()
Definition: EulerAngles.h:361
v
@ v
Definition: rootcling_impl.cxx:3635
q
float * q
Definition: THbookFile.cxx:89
ROOT::Math::EulerAngles::Inverse
EulerAngles Inverse() const
Return inverse of a rotation.
Definition: EulerAngles.h:315
R
#define R(a, b, c, d, e, f, g, h, i)
Definition: RSha256.hxx:128
ROOT::Math::gv_detail::dist
double dist(Rotation3D const &r1, Rotation3D const &r2)
Definition: 3DDistances.cxx:63
ROOT::Math::EulerAngles::SetComponents
void SetComponents(IT begin, IT end)
Set the three Euler angles given a pair of pointers or iterators defining the beginning and end of an...
Definition: EulerAngles.h:166
ROOT::Math::EulerAngles::Rectify
void Rectify()
Re-adjust components place angles in canonical ranges.
Definition: EulerAngles.cxx:47
ROOT::Math::EulerAngles::Scalar
double Scalar
Definition: EulerAngles.h:62
a
auto * a
Definition: textangle.C:12
ROOT::Math::EulerAngles::operator!=
bool operator!=(const EulerAngles &rhs) const
Definition: EulerAngles.h:351
ROOT::Math::EulerAngles
EulerAngles class describing rotation as three angles (Euler Angles).
Definition: EulerAngles.h:53
ROOT::Math::DisplacementVector3D::X
Scalar X() const
Cartesian X, converting if necessary from internal coordinate system.
Definition: DisplacementVector3D.h:282
ROOT::Math::RotationX
Rotation class representing a 3D rotation about the X axis by the angle of rotation.
Definition: RotationX.h:53
ROOT::Math::EulerAngles::fTheta
double fTheta
Definition: EulerAngles.h:358
PositionVector3D.h
void
typedef void((*Func_t)())
ROOT::Math::DisplacementVector3D::Z
Scalar Z() const
Cartesian Z, converting if necessary from internal coordinate system.
Definition: DisplacementVector3D.h:292
ROOT::Math::EulerAngles::Psi
Scalar Psi() const
Return Psi Euler angle.
Definition: EulerAngles.h:241
3DConversions.h
ROOT::Math::operator*
AxisAngle operator*(RotationX const &r1, AxisAngle const &r2)
Multiplication of an axial rotation by an AxisAngle.
Definition: AxisAngleXother.cxx:191
Rotation3D.h
DisplacementVector3D.h
ROOT::Math::EulerAngles::fPsi
double fPsi
Definition: EulerAngles.h:359
ROOT::Math::Scalar
Rotation3D::Scalar Scalar
Definition: Rotation3DxAxial.cxx:69
ROOT::Math::EulerAngles::operator*=
EulerAngles & operator*=(const R &r)
Post-Multiply (on right) by another rotation : T = T*R.
Definition: EulerAngles.h:334
M_PI
#define M_PI
Definition: Rotated.cxx:105
ROOT::Math::gv_detail::convert
void convert(R1 const &, R2 const)
Definition: 3DConversions.h:56
ROOT::Math::DisplacementVector3D
Class describing a generic displacement vector in 3 dimensions.
Definition: DisplacementVector3D.h:67
LorentzVector.h
ROOT::Math::EulerAngles::Distance
Scalar Distance(const R &r) const
Distance between two rotations.
Definition: EulerAngles.h:340
ROOT::Math::EulerAngles::EulerAngles
EulerAngles()
Default constructor.
Definition: EulerAngles.h:67
ROOT::Math::EulerAngles::operator*
AVector operator*(const AVector &v) const
Overload operator * for rotation on a vector.
Definition: EulerAngles.h:296
ROOT::Math::Quaternion
Rotation class with the (3D) rotation represented by a unit quaternion (u, i, j, k).
Definition: Quaternion.h:57
ROOT::Math::Distance
AxisAngle::Scalar Distance(const AxisAngle &r1, const R &r2)
Distance between two rotations.
Definition: AxisAngle.h:330
ROOT
VSD Structures.
Definition: StringConv.hxx:21
ROOT::Math::EulerAngles::SetTheta
void SetTheta(Scalar theta)
Set Theta Euler angle // JMM 30 Jan.
Definition: EulerAngles.h:226
Math
ROOT::Math::EulerAngles::operator=
EulerAngles & operator=(OtherRotation const &r)
Assign from any other rotation (see gv_detail::convert )
Definition: EulerAngles.h:104
ROOT::Math::LorentzVector
Definition: LorentzVector.h:70
ROOT::Math::EulerAngles::SetPhi
void SetPhi(Scalar phi)
Set Phi Euler angle // JMM 30 Jan.
Definition: EulerAngles.h:216
ROOT::Math::RotationY
Rotation class representing a 3D rotation about the Y axis by the angle of rotation.
Definition: RotationY.h:53