Logo ROOT   master
Reference Guide
RotationX.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 FNAL MathLib Team *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class RotationZ representing a rotation about the Z axis
12 //
13 // Created by: Mark Fischler Mon July 18 2005
14 //
15 // Last update: $Id$
16 //
17 #ifndef ROOT_Math_GenVector_RotationX
18 #define ROOT_Math_GenVector_RotationX 1
19 
20 
26 
28 
29 #include <cmath>
30 
31 namespace ROOT {
32 namespace Math {
33 
34 
35 //__________________________________________________________________________________________
36  /**
37  Rotation class representing a 3D rotation about the X axis by the angle of rotation.
38  For efficiency reason, in addition to the the angle, the sine and cosine of the angle are held
39 
40  @ingroup GenVector
41  */
42 
43 class RotationX {
44 
45 public:
46 
47  typedef double Scalar;
48 
49 
50  // ========== Constructors and Assignment =====================
51 
52  /**
53  Default constructor (identity rotation)
54  */
55  RotationX() : fAngle(0), fSin(0), fCos(1) { }
56 
57  /**
58  Construct from an angle
59  */
60  explicit RotationX( Scalar angle ) : fAngle(angle),
61  fSin(std::sin(angle)),
62  fCos(std::cos(angle))
63  {
64  Rectify();
65  }
66 
67  // The compiler-generated copy ctor, copy assignment, and dtor are OK.
68 
69  /**
70  Rectify makes sure the angle is in (-pi,pi]
71  */
72  void Rectify() {
73  if ( std::fabs(fAngle) >= M_PI ) {
74  double x = fAngle / (2.0 * M_PI);
75  fAngle = (2.0 * M_PI) * ( x + std::floor(.5-x) );
76  fSin = std::sin(fAngle);
77  fCos = std::cos(fAngle);
78  }
79  }
80 
81  // ======== Components ==============
82 
83  /**
84  Set given the angle.
85  */
86  void SetAngle (Scalar angle) {
87  fSin=std::sin(angle);
88  fCos=std::cos(angle);
89  fAngle= angle;
90  Rectify();
91  }
92  void SetComponents (Scalar angle) { SetAngle(angle); }
93 
94  /**
95  Get the angle
96  */
97  void GetAngle(Scalar &angle) const { angle = atan2(fSin, fCos); }
98  void GetComponents ( Scalar & angle ) const { GetAngle(angle); }
99 
100  /**
101  Angle of rotation
102  */
103  Scalar Angle() const { return atan2(fSin, fCos); }
104 
105  /**
106  Sine or Cosine of the rotation angle
107  */
108  Scalar SinAngle () const { return fSin; }
109  Scalar CosAngle () const { return fCos; }
110 
111  // =========== operations ==============
112 
113  /**
114  Rotation operation on a cartesian vector
115  */
116 // typedef DisplacementVector3D< Cartesian3D<double> > XYZVector;
117 // XYZVector operator() (const XYZVector & v) const {
118 // return XYZVector ( v.x(), fCos*v.y()-fSin*v.z(), fCos*v.z()+fSin*v.y() );
119 // }
120 
121 
122  /**
123  Rotation operation on a displacement vector in any coordinate system
124  */
125  template <class CoordSystem, class U>
129  xyz.SetXYZ( v.X(), fCos*v.Y()-fSin*v.Z(), fCos*v.Z()+fSin*v.Y() );
131  }
132 
133  /**
134  Rotation operation on a position vector in any coordinate system
135  */
136  template <class CoordSystem, class U>
141  return PositionVector3D<CoordSystem,U> ( rxyz );
142  }
143 
144  /**
145  Rotation operation on a Lorentz vector in any 4D coordinate system
146  */
147  template <class CoordSystem>
151  xyz = operator()(xyz);
152  LorentzVector< PxPyPzE4D<double> > xyzt (xyz.X(), xyz.Y(), xyz.Z(), v.E());
153  return LorentzVector<CoordSystem> ( xyzt );
154  }
155 
156  /**
157  Rotation operation on an arbitrary vector v.
158  Preconditions: v must implement methods x(), y(), and z()
159  and the arbitrary vector type must have a constructor taking (x,y,z)
160  */
161  template <class ForeignVector>
162  ForeignVector
163  operator() (const ForeignVector & v) const {
166  return ForeignVector ( rxyz.X(), rxyz.Y(), rxyz.Z() );
167  }
168 
169  /**
170  Overload operator * for rotation on a vector
171  */
172  template <class AVector>
173  inline
174  AVector operator* (const AVector & v) const
175  {
176  return operator()(v);
177  }
178 
179  /**
180  Invert a rotation in place
181  */
182  void Invert() { fAngle = -fAngle; fSin = -fSin; }
183 
184  /**
185  Return inverse of a rotation
186  */
187  RotationX Inverse() const { RotationX t(*this); t.Invert(); return t; }
188 
189  // ========= Multi-Rotation Operations ===============
190 
191  /**
192  Multiply (combine) two rotations
193  */
194  RotationX operator * (const RotationX & r) const {
195  RotationX ans;
196  double x = (fAngle + r.fAngle) / (2.0 * M_PI);
197  ans.fAngle = (2.0 * M_PI) * ( x + std::floor(.5-x) );
198  ans.fSin = fSin*r.fCos + fCos*r.fSin;
199  ans.fCos = fCos*r.fCos - fSin*r.fSin;
200  return ans;
201  }
202 
203  /**
204  Post-Multiply (on right) by another rotation : T = T*R
205  */
206  RotationX & operator *= (const RotationX & r) { return *this = (*this)*r; }
207 
208  /**
209  Equality/inequality operators
210  */
211  bool operator == (const RotationX & rhs) const {
212  if( fAngle != rhs.fAngle ) return false;
213  return true;
214  }
215  bool operator != (const RotationX & rhs) const {
216  return ! operator==(rhs);
217  }
218 
219 private:
220 
221  Scalar fAngle; // rotation angle
222  Scalar fSin; // sine of the rotation angle
223  Scalar fCos; // cosine of the rotaiton angle
224 
225 }; // RotationX
226 
227 // ============ Class RotationX ends here ============
228 
229 /**
230  Distance between two rotations
231  */
232 template <class R>
233 inline
234 typename RotationX::Scalar
235 Distance ( const RotationX& r1, const R & r2) {return gv_detail::dist(r1,r2);}
236 
237 /**
238  Stream Output and Input
239  */
240  // TODO - I/O should be put in the manipulator form
241 
242 inline
243 std::ostream & operator<< (std::ostream & os, const RotationX & r) {
244  os << " RotationX(" << r.Angle() << ") ";
245  return os;
246 }
247 
248 
249 } // namespace Math
250 } // namespace ROOT
251 
252 #endif // ROOT_Math_GenVector_RotationX
Class describing a generic LorentzVector in the 4D space-time, using the specified coordinate syste...
Definition: LorentzVector.h:59
double dist(Rotation3D const &r1, Rotation3D const &r2)
Definition: 3DDistances.cxx:48
RotationX & operator*=(const RotationX &r)
Post-Multiply (on right) by another rotation : T = T*R.
Definition: RotationX.h:206
RotationX Inverse() const
Return inverse of a rotation.
Definition: RotationX.h:187
DisplacementVector3D< CoordSystem, Tag > & SetXYZ(Scalar a, Scalar b, Scalar c)
set the values of the vector from the cartesian components (x,y,z) (if the vector is held in polar or...
Returns the available number of logical cores.
Definition: StringConv.hxx:21
RotationX(Scalar angle)
Construct from an angle.
Definition: RotationX.h:60
Class describing a generic position vector (point) in 3 dimensions.
void SetAngle(Scalar angle)
Set given the angle.
Definition: RotationX.h:86
bool operator==(const RotationX &rhs) const
Equality/inequality operators.
Definition: RotationX.h:211
STL namespace.
double cos(double)
#define R(a, b, c, d, e, f, g, h, i)
Definition: RSha256.hxx:110
std::ostream & operator<<(std::ostream &os, const AxisAngle &a)
Stream Output and Input.
Definition: AxisAngle.cxx:91
Scalar X() const
Cartesian X, converting if necessary from internal coordinate system.
Double_t x[n]
Definition: legend1.C:17
Scalar Z() const
Cartesian Z, converting if necessary from internal coordinate system.
Scalar Y() const
Cartesian Y, converting if necessary from internal coordinate system.
double sin(double)
void GetAngle(Scalar &angle) const
Get the angle.
Definition: RotationX.h:97
AVector operator*(const AVector &v) const
Overload operator * for rotation on a vector.
Definition: RotationX.h:174
void Invert()
Invert a rotation in place.
Definition: RotationX.h:182
Class describing a generic displacement vector in 3 dimensions.
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
Scalar Angle() const
Angle of rotation.
Definition: RotationX.h:103
ROOT::R::TRInterface & r
Definition: Object.C:4
#define M_PI
Definition: Rotated.cxx:105
void Rectify()
Rectify makes sure the angle is in (-pi,pi].
Definition: RotationX.h:72
Rotation class representing a 3D rotation about the X axis by the angle of rotation.
Definition: RotationX.h:43
double floor(double)
Scalar CosAngle() const
Definition: RotationX.h:109
double atan2(double, double)
void GetComponents(Scalar &angle) const
Definition: RotationX.h:98
Namespace for new Math classes and functions.
bool operator!=(const RotationX &rhs) const
Definition: RotationX.h:215
RotationX()
Default constructor (identity rotation)
Definition: RotationX.h:55
AxisAngle::Scalar Distance(const AxisAngle &r1, const R &r2)
Distance between two rotations.
Definition: AxisAngle.h:326
void SetComponents(Scalar angle)
Definition: RotationX.h:92
Scalar SinAngle() const
Sine or Cosine of the rotation angle.
Definition: RotationX.h:108
DisplacementVector3D< CoordSystem, U > operator()(const DisplacementVector3D< CoordSystem, U > &v) const
Rotation operation on a cartesian vector.
Definition: RotationX.h:127