Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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.
42 See also ROOT::Math::AxisAngle, ROOT::Math::EulerAngles, and ROOT::Math::Rotation3D.
43
44 @ingroup GenVector
45
46 @see GenVector
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 constexpr 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 /**
163 Access to the four quaternion components:
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>
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). GenVector coordinate systems whose
228 constructors take other coordinates use SetXYZ() instead.
229 */
230 template <class ForeignVector>
231 ForeignVector
232 operator() (const ForeignVector & v) const {
235 return Impl::MakeFromXYZ<ForeignVector>(rxyz.X(), rxyz.Y(), rxyz.Z());
236 }
237
238 /**
239 Overload operator * for rotation on a vector
240 */
241 template <class AVector>
242 inline
243 AVector operator* (const AVector & v) const
244 {
245 return operator()(v);
246 }
247
248 /**
249 Invert a rotation in place
250 */
251 void Invert() { fI = -fI; fJ = -fJ; fK = -fK; }
252
253 /**
254 Return inverse of a rotation
255 */
256 Quaternion Inverse() const { return Quaternion(fU, -fI, -fJ, -fK); }
257
258 // ========= Multi-Rotation Operations ===============
259
260 /**
261 Multiply (combine) two rotations
262 */
263 /**
264 Multiply (combine) two rotations
265 */
267 return Quaternion ( fU*q.fU - fI*q.fI - fJ*q.fJ - fK*q.fK ,
268 fU*q.fI + fI*q.fU + fJ*q.fK - fK*q.fJ ,
269 fU*q.fJ - fI*q.fK + fJ*q.fU + fK*q.fI ,
270 fU*q.fK + fI*q.fJ - fJ*q.fI + fK*q.fU );
271 }
272
273 Quaternion operator * (const Rotation3D & r) const;
274 Quaternion operator * (const AxisAngle & a) const;
275 Quaternion operator * (const EulerAngles & e) const;
276 Quaternion operator * (const RotationZYX & r) const;
277 Quaternion operator * (const RotationX & rx) const;
278 Quaternion operator * (const RotationY & ry) const;
279 Quaternion operator * (const RotationZ & rz) const;
280
281 /**
282 Post-Multiply (on right) by another rotation : T = T*R
283 */
284 template <class R>
285 Quaternion & operator *= (const R & r) { return *this = (*this)*r; }
286
287
288 /**
289 Distance between two rotations in Quaternion form
290 Note: The rotation group is isomorphic to a 3-sphere
291 with diametrically opposite points identified.
292 The (rotation group-invariant) is the smaller
293 of the two possible angles between the images of
294 the two totations on that sphere. Thus the distance
295 is never greater than pi/2.
296 */
297
298 Scalar Distance(const Quaternion & q) const ;
299
300 /**
301 Equality/inequality operators
302 */
303 bool operator == (const Quaternion & rhs) const {
304 if( fU != rhs.fU ) return false;
305 if( fI != rhs.fI ) return false;
306 if( fJ != rhs.fJ ) return false;
307 if( fK != rhs.fK ) return false;
308 return true;
309 }
310 bool operator != (const Quaternion & rhs) const {
311 return ! operator==(rhs);
312 }
313
314private:
315
320
321}; // Quaternion
322
323// ============ Class Quaternion ends here ============
324
325/**
326 Distance between two rotations
327 */
328template <class R>
329inline
330typename Quaternion::Scalar
331Distance ( const Quaternion& r1, const R & r2) {return gv_detail::dist(r1,r2);}
332
333/**
334 Multiplication of an axial rotation by an AxisAngle
335 */
336Quaternion operator* (RotationX const & r1, Quaternion const & r2);
337Quaternion operator* (RotationY const & r1, Quaternion const & r2);
338Quaternion operator* (RotationZ const & r1, Quaternion const & r2);
339
340/**
341 Stream Output and Input
342 */
343 // TODO - I/O should be put in the manipulator form
344
345std::ostream & operator<< (std::ostream & os, const Quaternion & q);
346
347
348} // namespace Math
349} // namespace ROOT
350
351#endif // ROOT_Math_GenVector_Quaternion
#define a(i)
Definition RSha256.hxx:99
#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
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:46
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 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...
bool operator!=(const Quaternion &rhs) const
Definition Quaternion.h:310
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.
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:285
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:303
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:243
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:256
constexpr Quaternion(const OtherRotation &r)
Construct from another supported rotation type (see gv_detail::convert )
Definition Quaternion.h:80
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
void Invert()
Invert a rotation in place.
Definition Quaternion.h:251
Rotation class with the (3D) rotation represented by a 3x3 orthogonal matrix.
Definition Rotation3D.h:68
Rotation class representing a 3D rotation about the X axis by the angle of rotation.
Definition RotationX.h:46
Rotation class representing a 3D rotation about the Y axis by the angle of rotation.
Definition RotationY.h:46
Rotation class with the (3D) rotation represented by angles describing first a rotation of an angle p...
Definition RotationZYX.h:64
Rotation class representing a 3D rotation about the Z axis by the angle of rotation.
Definition RotationZ.h:46
Namespace for new Math classes and functions.
double dist(Rotation3D const &r1, Rotation3D const &r2)
void convert(R1 const &, R2 const)
std::ostream & operator<<(std::ostream &os, const AxisAngle &a)
Stream Output and Input.
Definition AxisAngle.cxx:81
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