Logo ROOT   6.10/09
Reference Guide
RotationY.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 RotationY representing a rotation about the Y axis
12 //
13 // Created by: Mark Fischler Mon July 18 2005
14 //
15 // Last update: $Id$
16 //
17 #ifndef ROOT_Math_GenVector_RotationY
18 #define ROOT_Math_GenVector_RotationY 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 Y 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 RotationY {
44 
45 public:
46 
47  typedef double Scalar;
48 
49 
50  // ========== Constructors and Assignment =====================
51 
52  /**
53  Default constructor (identity rotation)
54  */
55  RotationY() : fAngle(0), fSin(0), fCos(1) { }
56 
57  /**
58  Construct from an angle
59  */
60  explicit RotationY( 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
119 // ( fCos*v.x()+fSin*v.z(), v.y(), fCos*v.z()-fSin*v.x() );
120 // }
121 
122  /**
123  Rotation operation on a displacement vector in any coordinate system
124  */
125  template <class CoordSystem, class U>
129  xyz.SetXYZ( fCos*v.x()+fSin*v.z(), v.y(), fCos*v.z()-fSin*v.x() );
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  RotationY Inverse() const { RotationY t(*this); t.Invert(); return t; }
188 
189  // ========= Multi-Rotation Operations ===============
190 
191  /**
192  Multiply (combine) two rotations
193  */
194  RotationY operator * (const RotationY & r) const {
195  RotationY 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  RotationY & operator *= (const RotationY & r) { return *this = (*this)*r; }
207 
208  /**
209  Equality/inequality operators
210  */
211  bool operator == (const RotationY & rhs) const {
212  if( fAngle != rhs.fAngle ) return false;
213  return true;
214  }
215  bool operator != (const RotationY & 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 }; // RotationY
226 
227 // ============ Class RotationY ends here ============
228 
229 /**
230  Distance between two rotations
231  */
232 template <class R>
233 inline
234 typename RotationY::Scalar
235 Distance ( const RotationY& 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 RotationY & r) {
244  os << " RotationY(" << r.Angle() << ") ";
245  return os;
246 }
247 
248 
249 } // namespace Math
250 } // namespace ROOT
251 
252 #endif // ROOT_Math_GenVector_RotationY
Scalar Angle() const
Angle of rotation.
Definition: RotationY.h:103
XYZVector ans(TestRotation const &t, XYZVector const &v_in)
Class describing a generic LorentzVector in the 4D space-time, using the specified coordinate system ...
Definition: LorentzVector.h:48
double dist(Rotation3D const &r1, Rotation3D const &r2)
Definition: 3DDistances.cxx:48
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...
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
void Invert()
Invert a rotation in place.
Definition: RotationY.h:182
Scalar SinAngle() const
Sine or Cosine of the rotation angle.
Definition: RotationY.h:108
RotationY()
Default constructor (identity rotation)
Definition: RotationY.h:55
Class describing a generic position vector (point) in 3 dimensions.
void GetAngle(Scalar &angle) const
Get the angle.
Definition: RotationY.h:97
STL namespace.
AVector operator*(const AVector &v) const
Overload operator * for rotation on a vector.
Definition: RotationY.h:174
double cos(double)
std::ostream & operator<<(std::ostream &os, const AxisAngle &a)
Stream Output and Input.
Definition: AxisAngle.cxx:91
Scalar CosAngle() const
Definition: RotationY.h:109
Scalar X() const
Cartesian X, converting if necessary from internal coordinate system.
RotationY & operator*=(const RotationY &r)
Post-Multiply (on right) by another rotation : T = T*R.
Definition: RotationY.h:206
Double_t x[n]
Definition: legend1.C:17
Rotation class representing a 3D rotation about the Y axis by the angle of rotation.
Definition: RotationY.h:43
Scalar Z() const
Cartesian Z, converting if necessary from internal coordinate system.
Scalar Y() const
Cartesian Y, converting if necessary from internal coordinate system.
bool operator==(const RotationY &rhs) const
Equality/inequality operators.
Definition: RotationY.h:211
void Rectify()
Rectify makes sure the angle is in (-pi,pi].
Definition: RotationY.h:72
void SetAngle(Scalar angle)
Set given the angle.
Definition: RotationY.h:86
Scalar E() const
return 4-th component (time, or energy for a 4-momentum vector)
void GetComponents(Scalar &angle) const
Definition: RotationY.h:98
double sin(double)
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)
TRandom2 r(17)
#define M_PI
Definition: Rotated.cxx:105
SVector< double, 2 > v
Definition: Dict.h:5
unsigned int r1[N_CITIES]
Definition: simanTSP.cxx:321
double floor(double)
RotationY Inverse() const
Return inverse of a rotation.
Definition: RotationY.h:187
void SetComponents(Scalar angle)
Definition: RotationY.h:92
DisplacementVector3D< CoordSystem, U > operator()(const DisplacementVector3D< CoordSystem, U > &v) const
Rotation operation on a cartesian vector.
Definition: RotationY.h:127
double atan2(double, double)
Namespace for new Math classes and functions.
AxisAngle::Scalar Distance(const AxisAngle &r1, const R &r2)
Distance between two rotations.
Definition: AxisAngle.h:326
::ROOT::Math::DisplacementVector3D< Cartesian3D< Scalar > > Vect() const
get the spatial components of the Vector in a DisplacementVector based on Cartesian Coordinates ...
TRandom3 R
a TMatrixD.
Definition: testIO.cxx:28
RotationY(Scalar angle)
Construct from an angle.
Definition: RotationY.h:60
unsigned int r2[N_CITIES]
Definition: simanTSP.cxx:322
bool operator!=(const RotationY &rhs) const
Definition: RotationY.h:215