 ROOT   Reference Guide Quaternion.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 rotation in 3 dimensions, represented by a quaternion
12// Created by: Mark Fischler Thurs June 9 2005
13//
14// Last update: $Id$
15//
16#ifndef ROOT_Math_GenVector_Quaternion
17#define ROOT_Math_GenVector_Quaternion 1
18
19
26
27#include <algorithm>
28#include <cassert>
29
30
31namespace ROOT {
32namespace Math {
33
34
35//__________________________________________________________________________________________
36 /**
37 Rotation class with the (3D) rotation represented by
38 a unit quaternion (u, i, j, k).
39 This is the optimal representation for multiplication of multiple
40 rotations, and for computation of group-manifold-invariant distance
41 between two rotations.
43
44 @ingroup GenVector
45
46 @sa Overview of the @ref GenVector "physics vector library"
47 */
48
50
51public:
52
53 typedef double Scalar;
54
55 // ========== Constructors and Assignment =====================
56
57 /**
58 Default constructor (identity rotation)
59 */
61 : fU(1.0)
62 , fI(0.0)
63 , fJ(0.0)
64 , fK(0.0)
65 { }
66
67 /**
68 Construct given a pair of pointers or iterators defining the
69 beginning and end of an array of four Scalars
70 */
71 template<class IT>
72 Quaternion(IT begin, IT end) { SetComponents(begin,end); }
73
74 // ======== Construction From other Rotation Forms ==================
75
76 /**
77 Construct from another supported rotation type (see gv_detail::convert )
78 */
79 template <class OtherRotation>
80 explicit Quaternion(const OtherRotation & r) {gv_detail::convert(r,*this);}
81
82
83 /**
84 Construct from four Scalars representing the coefficients of u, i, j, k
85 */
87 fU(u), fI(i), fJ(j), fK(k) { }
88
89 // The compiler-generated copy ctor, copy assignment, and dtor are OK.
90
91 /**
92 Re-adjust components to eliminate small deviations from |Q| = 1
93 orthonormality.
94 */
95 void Rectify();
96
97 /**
98 Assign from another supported rotation type (see gv_detail::convert )
99 */
100 template <class OtherRotation>
101 Quaternion & operator=( OtherRotation const & r ) {
102 gv_detail::convert(r,*this);
103 return *this;
104 }
105
106 // ======== Components ==============
107
108 /**
109 Set the four components given an iterator to the start of
110 the desired data, and another to the end (4 past start).
111 */
112 template<class IT>
113 void SetComponents(IT begin, IT end) {
114 fU = *begin++;
115 fI = *begin++;
116 fJ = *begin++;
117 fK = *begin++;
118 (void)end;
119 assert (end==begin);
120 }
121
122 /**
123 Get the components into data specified by an iterator begin
124 and another to the end of the desired data (4 past start).
125 */
126 template<class IT>
127 void GetComponents(IT begin, IT end) const {
128 *begin++ = fU;
129 *begin++ = fI;
130 *begin++ = fJ;
131 *begin++ = fK;
132 (void)end;
133 assert (end==begin);
134 }
135
136 /**
137 Get the components into data specified by an iterator begin
138 */
139 template<class IT>
140 void GetComponents(IT begin ) const {
141 *begin++ = fU;
142 *begin++ = fI;
143 *begin++ = fJ;
144 *begin = fK;
145 }
146
147 /**
148 Set the components based on four Scalars. The sum of the squares of
149 these Scalars should be 1; no checking is done.
150 */
152 fU=u; fI=i; fJ=j; fK=k;
153 }
154
155 /**
156 Get the components into four Scalars.
157 */
158 void GetComponents(Scalar & u, Scalar & i, Scalar & j, Scalar & k) const {
159 u=fU; i=fI; j=fJ; k=fK;
160 }
161
162 /**
164 U() is the coefficient of the identity Pauli matrix,
165 I(), J() and K() are the coefficients of sigma_x, sigma_y, sigma_z
166 */
167 Scalar U() const { return fU; }
168 Scalar I() const { return fI; }
169 Scalar J() const { return fJ; }
170 Scalar K() const { return fK; }
171
172 // =========== operations ==============
173
174 /**
175 Rotation operation on a cartesian vector
176 */
179
180 const Scalar alpha = fU*fU - fI*fI - fJ*fJ - fK*fK;
181 const Scalar twoQv = 2*(fI*v.X() + fJ*v.Y() + fK*v.Z());
182 const Scalar twoU = 2 * fU;
183 return XYZVector ( alpha * v.X() + twoU * (fJ*v.Z() - fK*v.Y()) + twoQv * fI ,
184 alpha * v.Y() + twoU * (fK*v.X() - fI*v.Z()) + twoQv * fJ ,
185 alpha * v.Z() + twoU * (fI*v.Y() - fJ*v.X()) + twoQv * fK );
186 }
187
188 /**
189 Rotation operation on a displacement vector in any coordinate system
190 */
191 template <class CoordSystem,class Tag>
194 DisplacementVector3D< Cartesian3D<double> > xyz(v.X(), v.Y(), v.Z());
197 vNew.SetXYZ( rxyz.X(), rxyz.Y(), rxyz.Z() );
198 return vNew;
199 }
200
201 /**
202 Rotation operation on a position vector in any coordinate system
203 */
204 template <class CoordSystem, class Tag>
209 return PositionVector3D<CoordSystem,Tag> ( rxyz );
210 }
211
212 /**
213 Rotation operation on a Lorentz vector in any 4D coordinate system
214 */
215 template <class CoordSystem>
219 xyz = operator()(xyz);
220 LorentzVector< PxPyPzE4D<double> > xyzt (xyz.X(), xyz.Y(), xyz.Z(), v.E());
221 return LorentzVector<CoordSystem> ( xyzt );
222 }
223
224 /**
225 Rotation operation on an arbitrary vector v.
226 Preconditions: v must implement methods x(), y(), and z()
227 and the arbitrary vector type must have a constructor taking (x,y,z)
228 */
229 template <class ForeignVector>
230 ForeignVector
231 operator() (const ForeignVector & v) const {
234 return ForeignVector ( rxyz.X(), rxyz.Y(), rxyz.Z() );
235 }
236
237 /**
238 Overload operator * for rotation on a vector
239 */
240 template <class AVector>
241 inline
242 AVector operator* (const AVector & v) const
243 {
244 return operator()(v);
245 }
246
247 /**
248 Invert a rotation in place
249 */
250 void Invert() { fI = -fI; fJ = -fJ; fK = -fK; }
251
252 /**
253 Return inverse of a rotation
254 */
255 Quaternion Inverse() const { return Quaternion(fU, -fI, -fJ, -fK); }
256
257 // ========= Multi-Rotation Operations ===============
258
259 /**
260 Multiply (combine) two rotations
261 */
262 /**
263 Multiply (combine) two rotations
264 */
266 return Quaternion ( fU*q.fU - fI*q.fI - fJ*q.fJ - fK*q.fK ,
267 fU*q.fI + fI*q.fU + fJ*q.fK - fK*q.fJ ,
268 fU*q.fJ - fI*q.fK + fJ*q.fU + fK*q.fI ,
269 fU*q.fK + fI*q.fJ - fJ*q.fI + fK*q.fU );
270 }
271
272 Quaternion operator * (const Rotation3D & r) const;
273 Quaternion operator * (const AxisAngle & a) const;
274 Quaternion operator * (const EulerAngles & e) const;
275 Quaternion operator * (const RotationZYX & r) const;
276 Quaternion operator * (const RotationX & rx) const;
277 Quaternion operator * (const RotationY & ry) const;
278 Quaternion operator * (const RotationZ & rz) const;
279
280 /**
281 Post-Multiply (on right) by another rotation : T = T*R
282 */
283 template <class R>
284 Quaternion & operator *= (const R & r) { return *this = (*this)*r; }
285
286
287 /**
288 Distance between two rotations in Quaternion form
289 Note: The rotation group is isomorphic to a 3-sphere
290 with diametrically opposite points identified.
291 The (rotation group-invariant) is the smaller
292 of the two possible angles between the images of
293 the two totations on that sphere. Thus the distance
294 is never greater than pi/2.
295 */
296
297 Scalar Distance(const Quaternion & q) const ;
298
299 /**
300 Equality/inequality operators
301 */
302 bool operator == (const Quaternion & rhs) const {
303 if( fU != rhs.fU ) return false;
304 if( fI != rhs.fI ) return false;
305 if( fJ != rhs.fJ ) return false;
306 if( fK != rhs.fK ) return false;
307 return true;
308 }
309 bool operator != (const Quaternion & rhs) const {
310 return ! operator==(rhs);
311 }
312
313private:
314
319
320}; // Quaternion
321
322// ============ Class Quaternion ends here ============
323
324/**
325 Distance between two rotations
326 */
327template <class R>
328inline
329typename Quaternion::Scalar
330Distance ( const Quaternion& r1, const R & r2) {return gv_detail::dist(r1,r2);}
331
332/**
333 Multiplication of an axial rotation by an AxisAngle
334 */
335Quaternion operator* (RotationX const & r1, Quaternion const & r2);
336Quaternion operator* (RotationY const & r1, Quaternion const & r2);
337Quaternion operator* (RotationZ const & r1, Quaternion const & r2);
338
339/**
340 Stream Output and Input
341 */
342 // TODO - I/O should be put in the manipulator form
343
344std::ostream & operator<< (std::ostream & os, const Quaternion & q);
345
346
347} // namespace Math
348} // namespace ROOT
349
350#endif // ROOT_Math_GenVector_Quaternion
#define e(i)
Definition: RSha256.hxx:103
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
float * q
Definition: THbookFile.cxx:89
AxisAngle class describing rotation represented with direction axis (3D Vector) and an angle of rotat...
Definition: AxisAngle.h:42
DefaultCoordinateSystemTag Default tag for identifying any coordinate system.
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.
EulerAngles class describing rotation as three angles (Euler Angles).
Definition: EulerAngles.h:45
Class describing a generic LorentzVector in the 4D space-time, using the specified coordinate system ...
Definition: LorentzVector.h:59
Class describing a generic position vector (point) in 3 dimensions.
Rotation class with the (3D) rotation represented by a unit quaternion (u, i, j, k).
Definition: Quaternion.h:49
Scalar Distance(const Quaternion &q) const
Distance between two rotations in Quaternion form Note: The rotation group is isomorphic to a 3-spher...
Definition: Quaternion.cxx:91
Scalar I() const
Definition: Quaternion.h:168
Quaternion(const OtherRotation &r)
Construct from another supported rotation type (see gv_detail::convert )
Definition: Quaternion.h:80
bool operator!=(const Quaternion &rhs) const
Definition: Quaternion.h:309
void SetComponents(Scalar u, Scalar i, Scalar j, Scalar k)
Set the components based on four Scalars.
Definition: Quaternion.h:151
Quaternion()
Default constructor (identity rotation)
Definition: Quaternion.h:60
Quaternion(Scalar u, Scalar i, Scalar j, Scalar k)
Construct from four Scalars representing the coefficients of u, i, j, k.
Definition: Quaternion.h:86
DisplacementVector3D< Cartesian3D< double >, DefaultCoordinateSystemTag > XYZVector
Rotation operation on a cartesian vector.
Definition: Quaternion.h:177
void Rectify()
Re-adjust components to eliminate small deviations from |Q| = 1 orthonormality.
Definition: Quaternion.cxx:34
Quaternion(IT begin, IT end)
Construct given a pair of pointers or iterators defining the beginning and end of an array of four Sc...
Definition: Quaternion.h:72
Scalar U() const
Access to the four quaternion components: U() is the coefficient of the identity Pauli matrix,...
Definition: Quaternion.h:167
void GetComponents(IT begin, IT end) const
Get the components into data specified by an iterator begin and another to the end of the desired dat...
Definition: Quaternion.h:127
Quaternion & operator*=(const R &r)
Post-Multiply (on right) by another rotation : T = T*R.
Definition: Quaternion.h:284
Quaternion & operator=(OtherRotation const &r)
Assign from another supported rotation type (see gv_detail::convert )
Definition: Quaternion.h:101
bool operator==(const Quaternion &rhs) const
Equality/inequality operators.
Definition: Quaternion.h:302
XYZVector operator()(const XYZVector &v) const
Definition: Quaternion.h:178
AVector operator*(const AVector &v) const
Overload operator * for rotation on a vector.
Definition: Quaternion.h:242
Scalar K() const
Definition: Quaternion.h:170
void GetComponents(IT begin) const
Get the components into data specified by an iterator begin.
Definition: Quaternion.h:140
Quaternion Inverse() const
Return inverse of a rotation.
Definition: Quaternion.h:255
void GetComponents(Scalar &u, Scalar &i, Scalar &j, Scalar &k) const
Get the components into four Scalars.
Definition: Quaternion.h:158
void SetComponents(IT begin, IT end)
Set the four components given an iterator to the start of the desired data, and another to the end (4...
Definition: Quaternion.h:113
Scalar J() const
Definition: Quaternion.h:169
void Invert()
Invert a rotation in place.
Definition: Quaternion.h:250
Rotation class with the (3D) rotation represented by a 3x3 orthogonal matrix.
Definition: Rotation3D.h:67
Rotation class representing a 3D rotation about the X axis by the angle of rotation.
Definition: RotationX.h:45
Rotation class representing a 3D rotation about the Y axis by the angle of rotation.
Definition: RotationY.h:45
Rotation class with the (3D) rotation represented by angles describing first a rotation of an angle p...
Definition: RotationZYX.h:63
Rotation class representing a 3D rotation about the Z axis by the angle of rotation.
Definition: RotationZ.h:45
Namespace for new Math classes and functions.
void(off) SmallVectorTemplateBase< T
double dist(Rotation3D const &r1, Rotation3D const &r2)
Definition: 3DDistances.cxx:48
void convert(R1 const &, R2 const)
Definition: 3DConversions.h:41
std::ostream & operator<<(std::ostream &os, const AxisAngle &a)
Stream Output and Input.
Definition: AxisAngle.cxx:91
AxisAngle operator*(RotationX const &r1, AxisAngle const &r2)
Multiplication of an axial rotation by an AxisAngle.
AxisAngle::Scalar Distance(const AxisAngle &r1, const R &r2)
Distance between two rotations.
Definition: AxisAngle.h:321
Rotation3D::Scalar Scalar
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
TArc a
Definition: textangle.C:12