Logo ROOT  
Reference Guide
RotationZ.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_RotationZ
18#define ROOT_Math_GenVector_RotationZ 1
19
20
26
28
29#include <cmath>
30
31namespace ROOT {
32namespace Math {
33
34
35//__________________________________________________________________________________________
36 /**
37 Rotation class representing a 3D rotation about the Z 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
43class RotationZ {
44
45public:
46
47 typedef double Scalar;
48
49
50 // ========== Constructors and Assignment =====================
51
52 /**
53 Default constructor (identity rotation)
54 */
55 RotationZ() : fAngle(0), fSin(0), fCos(1) { }
56
57 /**
58 Construct from an angle
59 */
60 explicit RotationZ( 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) );
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.y(), fCos*v.y()+fSin*v.x(), v.z() );
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.y(), fCos*v.y()+fSin*v.x(), v.z() );
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 RotationZ Inverse() const { RotationZ t(*this); t.Invert(); return t; }
188
189 // ========= Multi-Rotation Operations ===============
190
191 /**
192 Multiply (combine) two rotations
193 */
195 RotationZ 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 RotationZ & operator *= (const RotationZ & r) { return *this = (*this)*r; }
207
208 /**
209 Equality/inequality operators
210 */
211 bool operator == (const RotationZ & rhs) const {
212 if( fAngle != rhs.fAngle ) return false;
213 return true;
214 }
215 bool operator != (const RotationZ & rhs) const {
216 return ! operator==(rhs);
217 }
218
219private:
220
221 Scalar fAngle; // rotation angle
222 Scalar fSin; // sine of the rotation angle
223 Scalar fCos; // cosine of the rotaiton angle
224
225}; // RotationZ
226
227// ============ Class RotationZ ends here ============
228
229/**
230 Distance between two rotations
231 */
232template <class R>
233inline
234typename RotationZ::Scalar
235Distance ( const RotationZ& 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
242inline
243std::ostream & operator<< (std::ostream & os, const RotationZ & r) {
244 os << " RotationZ(" << r.Angle() << ") ";
245 return os;
246}
247
248
249} // namespace Math
250} // namespace ROOT
251
252#endif // ROOT_Math_GenVector_RotationZ
ROOT::R::TRInterface & r
Definition: Object.C:4
#define R(a, b, c, d, e, f, g, h, i)
Definition: RSha256.hxx:110
#define M_PI
Definition: Rotated.cxx:105
double atan2(double, double)
double cos(double)
double floor(double)
double sin(double)
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 position vector (point) in 3 dimensions.
Rotation class representing a 3D rotation about the Z axis by the angle of rotation.
Definition: RotationZ.h:43
Scalar CosAngle() const
Definition: RotationZ.h:109
bool operator==(const RotationZ &rhs) const
Equality/inequality operators.
Definition: RotationZ.h:211
RotationZ & operator*=(const RotationZ &r)
Post-Multiply (on right) by another rotation : T = T*R.
Definition: RotationZ.h:206
RotationZ(Scalar angle)
Construct from an angle.
Definition: RotationZ.h:60
RotationZ Inverse() const
Return inverse of a rotation.
Definition: RotationZ.h:187
bool operator!=(const RotationZ &rhs) const
Definition: RotationZ.h:215
void SetAngle(Scalar angle)
Set given the angle.
Definition: RotationZ.h:86
void Rectify()
Rectify makes sure the angle is in (-pi,pi].
Definition: RotationZ.h:72
DisplacementVector3D< CoordSystem, U > operator()(const DisplacementVector3D< CoordSystem, U > &v) const
Rotation operation on a displacement vector in any coordinate system.
Definition: RotationZ.h:127
RotationZ()
Default constructor (identity rotation)
Definition: RotationZ.h:55
AVector operator*(const AVector &v) const
Overload operator * for rotation on a vector.
Definition: RotationZ.h:174
Scalar Angle() const
Angle of rotation.
Definition: RotationZ.h:103
void Invert()
Invert a rotation in place.
Definition: RotationZ.h:182
void GetComponents(Scalar &angle) const
Definition: RotationZ.h:98
void GetAngle(Scalar &angle) const
Get the angle.
Definition: RotationZ.h:97
void SetComponents(Scalar angle)
Definition: RotationZ.h:92
Scalar SinAngle() const
Sine or Cosine of the rotation angle.
Definition: RotationZ.h:108
Double_t x[n]
Definition: legend1.C:17
Namespace for new Math classes and functions.
double dist(Rotation3D const &r1, Rotation3D const &r2)
Definition: 3DDistances.cxx:48
std::ostream & operator<<(std::ostream &os, const AxisAngle &a)
Stream Output and Input.
Definition: AxisAngle.cxx:91
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
AxisAngle::Scalar Distance(const AxisAngle &r1, const R &r2)
Distance between two rotations.
Definition: AxisAngle.h:320
VSD Structures.
Definition: StringConv.hxx:21