Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
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 <TMath.h>
30#include <cmath>
31
32namespace ROOT {
33namespace Math {
34
35
36//__________________________________________________________________________________________
37 /**
38 Rotation class representing a 3D rotation about the X axis by the angle of rotation.
39 For efficiency reason, in addition to the angle, the sine and cosine of the angle are held
40
41 @ingroup GenVector
42
43 @see GenVector
44 */
45
46class RotationX {
47
48public:
49
50 typedef double Scalar;
51
52
53 // ========== Constructors and Assignment =====================
54
55 /**
56 Default constructor (identity rotation)
57 */
58 RotationX() : fAngle(0), fSin(0), fCos(1) { }
59
60 /**
61 Construct from an angle
62 */
63 explicit RotationX( Scalar angle ) : fAngle(angle),
64 fSin(std::sin(angle)),
65 fCos(std::cos(angle))
66 {
67 Rectify();
68 }
69
70 // The compiler-generated copy ctor, copy assignment, and destructor are OK.
71
72 /**
73 Rectify makes sure the angle is in (-pi,pi]
74 */
75 void Rectify() {
76 if (std::fabs(fAngle) >= TMath::Pi()) {
77 double x = fAngle / TMath::TwoPi();
78 fAngle = TMath::TwoPi() * (x + std::floor(.5 - x));
79 fSin = std::sin(fAngle);
80 fCos = std::cos(fAngle);
81 }
82 }
83
84 // ======== Components ==============
85
86 /**
87 Set given the angle.
88 */
89 void SetAngle (Scalar angle) {
90 fSin=std::sin(angle);
91 fCos=std::cos(angle);
92 fAngle= angle;
93 Rectify();
94 }
95 void SetComponents (Scalar angle) { SetAngle(angle); }
96
97 /**
98 Get the angle
99 */
100 void GetAngle(Scalar &angle) const { using std::atan2; angle = atan2(fSin, fCos); }
101 void GetComponents ( Scalar & angle ) const { GetAngle(angle); }
102
103 /**
104 Angle of rotation
105 */
106 Scalar Angle() const { using std::atan2; return atan2(fSin, fCos); }
107
108 /**
109 Sine or Cosine of the rotation angle
110 */
111 Scalar SinAngle () const { return fSin; }
112 Scalar CosAngle () const { return fCos; }
113
114 // =========== operations ==============
115
116 /**
117 Rotation operation on a cartesian vector
118 */
119// typedef DisplacementVector3D< Cartesian3D<double> > XYZVector;
120// XYZVector operator() (const XYZVector & v) const {
121// return XYZVector ( v.x(), fCos*v.y()-fSin*v.z(), fCos*v.z()+fSin*v.y() );
122// }
123
124
125 /**
126 Rotation operation on a displacement vector in any coordinate system
127 */
128 template <class CoordSystem, class U>
132 xyz.SetXYZ( v.X(), fCos*v.Y()-fSin*v.Z(), fCos*v.Z()+fSin*v.Y() );
134 }
135
136 /**
137 Rotation operation on a position vector in any coordinate system
138 */
139 template <class CoordSystem, class U>
146
147 /**
148 Rotation operation on a Lorentz vector in any 4D coordinate system
149 */
150 template <class CoordSystem>
154 xyz = operator()(xyz);
155 LorentzVector< PxPyPzE4D<double> > xyzt (xyz.X(), xyz.Y(), xyz.Z(), v.E());
156 return LorentzVector<CoordSystem> ( xyzt );
157 }
158
159 /**
160 Rotation operation on an arbitrary vector v.
161 Preconditions: v must implement methods x(), y(), and z()
162 and the arbitrary vector type must have a constructor taking (x,y,z)
163 */
164 template <class ForeignVector>
165 ForeignVector
166 operator() (const ForeignVector & v) const {
169 return ForeignVector ( rxyz.X(), rxyz.Y(), rxyz.Z() );
170 }
171
172 /**
173 Overload operator * for rotation on a vector
174 */
175 template <class AVector>
176 inline
177 AVector operator* (const AVector & v) const
178 {
179 return operator()(v);
180 }
181
182 /**
183 Invert a rotation in place
184 */
185 void Invert() { fAngle = -fAngle; fSin = -fSin; }
186
187 /**
188 Return inverse of a rotation
189 */
190 RotationX Inverse() const { RotationX t(*this); t.Invert(); return t; }
191
192 // ========= Multi-Rotation Operations ===============
193
194 /**
195 Multiply (combine) two rotations
196 */
198 RotationX ans;
199 double x = (fAngle + r.fAngle) / TMath::TwoPi();
200 ans.fAngle = TMath::TwoPi() * (x + std::floor(.5 - x));
201 ans.fSin = fSin*r.fCos + fCos*r.fSin;
202 ans.fCos = fCos*r.fCos - fSin*r.fSin;
203 return ans;
204 }
205
206 /**
207 Post-Multiply (on right) by another rotation : T = T*R
208 */
209 RotationX & operator *= (const RotationX & r) { return *this = (*this)*r; }
210
211 /**
212 Equality/inequality operators
213 */
214 bool operator == (const RotationX & rhs) const {
215 if( fAngle != rhs.fAngle ) return false;
216 return true;
217 }
218 bool operator != (const RotationX & rhs) const {
219 return ! operator==(rhs);
220 }
221
222private:
223
224 Scalar fAngle; // rotation angle
225 Scalar fSin; // sine of the rotation angle
226 Scalar fCos; // cosine of the rotation angle
227
228}; // RotationX
229
230// ============ Class RotationX ends here ============
231
232/**
233 Distance between two rotations
234 */
235template <class R>
236inline
237typename RotationX::Scalar
238Distance ( const RotationX& r1, const R & r2) {return gv_detail::dist(r1,r2);}
239
240/**
241 Stream Output and Input
242 */
243 // TODO - I/O should be put in the manipulator form
244
245inline
246std::ostream & operator<< (std::ostream & os, const RotationX & r) {
247 os << " RotationX(" << r.Angle() << ") ";
248 return os;
249}
250
251
252} // namespace Math
253} // namespace ROOT
254
255#endif // ROOT_Math_GenVector_RotationX
ROOT::R::TRInterface & r
Definition Object.C:4
Class describing a generic displacement vector in 3 dimensions.
Scalar X() const
Cartesian X, converting if necessary from internal coordinate system.
Scalar Y() const
Cartesian Y, converting if necessary from internal coordinate system.
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...
Scalar Z() const
Cartesian Z, converting if necessary from internal coordinate system.
Class describing a generic LorentzVector in the 4D space-time, using the specified coordinate system ...
Class describing a generic position vector (point) in 3 dimensions.
Rotation class representing a 3D rotation about the X axis by the angle of rotation.
Definition RotationX.h:46
Scalar CosAngle() const
Definition RotationX.h:112
AVector operator*(const AVector &v) const
Overload operator * for rotation on a vector.
Definition RotationX.h:177
RotationX & operator*=(const RotationX &r)
Post-Multiply (on right) by another rotation : T = T*R.
Definition RotationX.h:209
void GetAngle(Scalar &angle) const
Get the angle.
Definition RotationX.h:100
void SetAngle(Scalar angle)
Set given the angle.
Definition RotationX.h:89
void GetComponents(Scalar &angle) const
Definition RotationX.h:101
void Invert()
Invert a rotation in place.
Definition RotationX.h:185
RotationX Inverse() const
Return inverse of a rotation.
Definition RotationX.h:190
RotationX(Scalar angle)
Construct from an angle.
Definition RotationX.h:63
DisplacementVector3D< CoordSystem, U > operator()(const DisplacementVector3D< CoordSystem, U > &v) const
Rotation operation on a cartesian vector.
Definition RotationX.h:130
bool operator!=(const RotationX &rhs) const
Definition RotationX.h:218
Scalar SinAngle() const
Sine or Cosine of the rotation angle.
Definition RotationX.h:111
bool operator==(const RotationX &rhs) const
Equality/inequality operators.
Definition RotationX.h:214
Scalar Angle() const
Angle of rotation.
Definition RotationX.h:106
void SetComponents(Scalar angle)
Definition RotationX.h:95
RotationX()
Default constructor (identity rotation).
Definition RotationX.h:58
void Rectify()
Rectify makes sure the angle is in (-pi,pi].
Definition RotationX.h:75
Double_t x[n]
Definition legend1.C:17
double dist(Rotation3D const &r1, Rotation3D const &r2)
std::ostream & operator<<(std::ostream &os, const AxisAngle &a)
Stream Output and Input.
Definition AxisAngle.cxx:81
AxisAngle::Scalar Distance(const AxisAngle &r1, const R &r2)
Distance between two rotations.
Definition AxisAngle.h:321
namespace associated R package for ROOT.
Definition RExports.h:72
constexpr Double_t Pi()
Definition TMath.h:40
constexpr Double_t TwoPi()
Definition TMath.h:47