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_MathX_GenVectorX_Quaternion
17#define ROOT_MathX_GenVectorX_Quaternion 1
18
25
26#include <algorithm>
27#include <cassert>
28
30
32
33using namespace ROOT::ROOT_MATH_ARCH;
34
35namespace ROOT {
36namespace ROOT_MATH_ARCH {
37
38//__________________________________________________________________________________________
39/**
40 Rotation class with the (3D) rotation represented by
41 a unit quaternion (u, i, j, k).
42 This is the optimal representation for multiplication of multiple
43 rotations, and for computation of group-manifold-invariant distance
44 between two rotations.
45 See also ROOT::Math::AxisAngle, ROOT::Math::EulerAngles, and ROOT::Math::Rotation3D.
46
47 @ingroup GenVectorX
48
49 @see GenVectorX
50*/
51
53
54public:
55 typedef double Scalar;
56
57 // ========== Constructors and Assignment =====================
58
59 /**
60 Default constructor (identity rotation)
61 */
62 Quaternion() : fU(1.0), fI(0.0), fJ(0.0), fK(0.0) {}
63
64 /**
65 Construct given a pair of pointers or iterators defining the
66 beginning and end of an array of four Scalars
67 */
68 template <class IT>
69 Quaternion(IT begin, IT end)
70 {
71 SetComponents(begin, end);
72 }
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)
81 {
82 gv_detail::convert(r, *this);
83 }
84
85 /**
86 Construct from four Scalars representing the coefficients of u, i, j, k
87 */
88 Quaternion(Scalar u, Scalar i, Scalar j, Scalar k) : fU(u), fI(i), fJ(j), fK(k) {}
89
90 // The compiler-generated copy ctor, copy assignment, and dtor are OK.
91
92 /**
93 Re-adjust components to eliminate small deviations from |Q| = 1
94 orthonormality.
95 */
96 void Rectify();
97
98 /**
99 Assign from another supported rotation type (see gv_detail::convert )
100 */
101 template <class OtherRotation>
102 Quaternion &operator=(OtherRotation const &r)
103 {
104 gv_detail::convert(r, *this);
105 return *this;
106 }
107
108 // ======== Components ==============
109
110 /**
111 Set the four components given an iterator to the start of
112 the desired data, and another to the end (4 past start).
113 */
114 template <class IT>
115 void SetComponents(IT begin, IT end)
116 {
117 fU = *begin++;
118 fI = *begin++;
119 fJ = *begin++;
120 fK = *begin++;
121 (void)end;
122 assert(end == begin);
123 }
124
125 /**
126 Get the components into data specified by an iterator begin
127 and another to the end of the desired data (4 past start).
128 */
129 template <class IT>
130 void GetComponents(IT begin, IT end) const
131 {
132 *begin++ = fU;
133 *begin++ = fI;
134 *begin++ = fJ;
135 *begin++ = fK;
136 (void)end;
137 assert(end == begin);
138 }
139
140 /**
141 Get the components into data specified by an iterator begin
142 */
143 template <class IT>
144 void GetComponents(IT begin) const
145 {
146 *begin++ = fU;
147 *begin++ = fI;
148 *begin++ = fJ;
149 *begin = fK;
150 }
151
152 /**
153 Set the components based on four Scalars. The sum of the squares of
154 these Scalars should be 1; no checking is done.
155 */
157 {
158 fU = u;
159 fI = i;
160 fJ = j;
161 fK = k;
162 }
163
164 /**
165 Get the components into four Scalars.
166 */
167 void GetComponents(Scalar &u, Scalar &i, Scalar &j, Scalar &k) const
168 {
169 u = fU;
170 i = fI;
171 j = fJ;
172 k = fK;
173 }
174
175 /**
176 Access to the four quaternion components:
177 U() is the coefficient of the identity Pauli matrix,
178 I(), J() and K() are the coefficients of sigma_x, sigma_y, sigma_z
179 */
180 Scalar U() const { return fU; }
181 Scalar I() const { return fI; }
182 Scalar J() const { return fJ; }
183 Scalar K() const { return fK; }
184
185 // =========== operations ==============
186
187 /**
188 Rotation operation on a cartesian vector
189 */
192 {
193
194 const Scalar alpha = fU * fU - fI * fI - fJ * fJ - fK * fK;
195 const Scalar twoQv = 2 * (fI * v.X() + fJ * v.Y() + fK * v.Z());
196 const Scalar twoU = 2 * fU;
197 return XYZVector(alpha * v.X() + twoU * (fJ * v.Z() - fK * v.Y()) + twoQv * fI,
198 alpha * v.Y() + twoU * (fK * v.X() - fI * v.Z()) + twoQv * fJ,
199 alpha * v.Z() + twoU * (fI * v.Y() - fJ * v.X()) + twoQv * fK);
200 }
201
202 /**
203 Rotation operation on a displacement vector in any coordinate system
204 */
205 template <class CoordSystem, class Tag>
214
215 /**
216 Rotation operation on a position vector in any coordinate system
217 */
218 template <class CoordSystem, class Tag>
225
226 /**
227 Rotation operation on a Lorentz vector in any 4D coordinate system
228 */
229 template <class CoordSystem>
231 {
233 xyz = operator()(xyz);
234 LorentzVector<PxPyPzE4D<double>> xyzt(xyz.X(), xyz.Y(), xyz.Z(), v.E());
235 return LorentzVector<CoordSystem>(xyzt);
236 }
237
238 /**
239 Rotation operation on an arbitrary vector v.
240 Preconditions: v must implement methods x(), y(), and z()
241 and the arbitrary vector type must have a constructor taking (x,y,z)
242 */
243 template <class ForeignVector>
244 ForeignVector operator()(const ForeignVector &v) const
245 {
248 return ForeignVector(rxyz.X(), rxyz.Y(), rxyz.Z());
249 }
250
251 /**
252 Overload operator * for rotation on a vector
253 */
254 template <class AVector>
255 inline AVector operator*(const AVector &v) const
256 {
257 return operator()(v);
258 }
259
260 /**
261 Invert a rotation in place
262 */
263 void Invert()
264 {
265 fI = -fI;
266 fJ = -fJ;
267 fK = -fK;
268 }
269
270 /**
271 Return inverse of a rotation
272 */
273 Quaternion Inverse() const { return Quaternion(fU, -fI, -fJ, -fK); }
274
275 // ========= Multi-Rotation Operations ===============
276
277 /**
278 Multiply (combine) two rotations
279 */
280 /**
281 Multiply (combine) two rotations
282 */
284 {
285 return Quaternion(fU * q.fU - fI * q.fI - fJ * q.fJ - fK * q.fK, fU * q.fI + fI * q.fU + fJ * q.fK - fK * q.fJ,
286 fU * q.fJ - fI * q.fK + fJ * q.fU + fK * q.fI, fU * q.fK + fI * q.fJ - fJ * q.fI + fK * q.fU);
287 }
288
289 Quaternion operator*(const Rotation3D &r) const;
290 Quaternion operator*(const AxisAngle &a) const;
291 Quaternion operator*(const EulerAngles &e) const;
292 Quaternion operator*(const RotationZYX &r) const;
293 Quaternion operator*(const RotationX &rx) const;
294 Quaternion operator*(const RotationY &ry) const;
295 Quaternion operator*(const RotationZ &rz) const;
296
297 /**
298 Post-Multiply (on right) by another rotation : T = T*R
299 */
300 template <class R>
302 {
303 return *this = (*this) * r;
304 }
305
306 /**
307 Distance between two rotations in Quaternion form
308 Note: The rotation group is isomorphic to a 3-sphere
309 with diametrically opposite points identified.
310 The (rotation group-invariant) is the smaller
311 of the two possible angles between the images of
312 the two totations on that sphere. Thus the distance
313 is never greater than pi/2.
314 */
315
316 Scalar Distance(const Quaternion &q) const;
317
318 /**
319 Equality/inequality operators
320 */
321 bool operator==(const Quaternion &rhs) const
322 {
323 if (fU != rhs.fU)
324 return false;
325 if (fI != rhs.fI)
326 return false;
327 if (fJ != rhs.fJ)
328 return false;
329 if (fK != rhs.fK)
330 return false;
331 return true;
332 }
333 bool operator!=(const Quaternion &rhs) const { return !operator==(rhs); }
334
335private:
340
341}; // Quaternion
342
343// ============ Class Quaternion ends here ============
344
345/**
346 Distance between two rotations
347 */
348template <class R>
349inline typename Quaternion::Scalar Distance(const Quaternion &r1, const R &r2)
350{
351 return gv_detail::dist(r1, r2);
352}
353
354/**
355 Multiplication of an axial rotation by an AxisAngle
356 */
357Quaternion operator*(RotationX const &r1, Quaternion const &r2);
358Quaternion operator*(RotationY const &r1, Quaternion const &r2);
359Quaternion operator*(RotationZ const &r1, Quaternion const &r2);
360
361#if !defined(ROOT_MATH_SYCL) && !defined(ROOT_MATH_CUDA)
362/**
363 Stream Output and Input
364 */
365// TODO - I/O should be put in the manipulator form
366
367std::ostream &operator<<(std::ostream &os, const Quaternion &q);
368#endif
369
370} // namespace ROOT_MATH_ARCH
371} // namespace ROOT
372
373#endif // ROOT_MathX_GenVectorX_Quaternion
ROOT::R::TRInterface & r
Definition Object.C:4
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
float * q
AxisAngle class describing rotation represented with direction axis (3D Vector) and an angle of rotat...
Definition AxisAngle.h:46
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.
Scalar Z() const
Cartesian Z, 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...
EulerAngles class describing rotation as three angles (Euler Angles).
Definition EulerAngles.h:50
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:52
LorentzVector< CoordSystem > operator()(const LorentzVector< CoordSystem > &v) const
Rotation operation on a Lorentz vector in any 4D coordinate system.
Definition Quaternion.h:230
void Invert()
Invert a rotation in place.
Definition Quaternion.h:263
Quaternion Inverse() const
Return inverse of a rotation.
Definition Quaternion.h:273
Scalar U() const
Access to the four quaternion components: U() is the coefficient of the identity Pauli matrix,...
Definition Quaternion.h:180
bool operator==(const Quaternion &rhs) const
Equality/inequality operators.
Definition Quaternion.h:321
AVector operator*(const AVector &v) const
Overload operator * for rotation on a vector.
Definition Quaternion.h:255
void SetComponents(Scalar u, Scalar i, Scalar j, Scalar k)
Set the components based on four Scalars.
Definition Quaternion.h:156
PositionVector3D< CoordSystem, Tag > operator()(const PositionVector3D< CoordSystem, Tag > &p) const
Rotation operation on a position vector in any coordinate system.
Definition Quaternion.h:219
Quaternion & operator*=(const R &r)
Post-Multiply (on right) by another rotation : T = T*R.
Definition Quaternion.h:301
Quaternion(Scalar u, Scalar i, Scalar j, Scalar k)
Construct from four Scalars representing the coefficients of u, i, j, k.
Definition Quaternion.h:88
void Rectify()
Re-adjust components to eliminate small deviations from |Q| = 1 orthonormality.
bool operator!=(const Quaternion &rhs) const
Definition Quaternion.h:333
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:69
Quaternion(const OtherRotation &r)
Construct from another supported rotation type (see gv_detail::convert ).
Definition Quaternion.h:80
DisplacementVector3D< CoordSystem, Tag > operator()(const DisplacementVector3D< CoordSystem, Tag > &v) const
Rotation operation on a displacement vector in any coordinate system.
Definition Quaternion.h:206
Quaternion()
Default constructor (identity rotation).
Definition Quaternion.h:62
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:130
ForeignVector operator()(const ForeignVector &v) const
Rotation operation on an arbitrary vector v.
Definition Quaternion.h:244
Scalar Distance(const Quaternion &q) const
Distance between two rotations in Quaternion form Note: The rotation group is isomorphic to a 3-spher...
void GetComponents(IT begin) const
Get the components into data specified by an iterator begin.
Definition Quaternion.h:144
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:115
XYZVector operator()(const XYZVector &v) const
Definition Quaternion.h:191
void GetComponents(Scalar &u, Scalar &i, Scalar &j, Scalar &k) const
Get the components into four Scalars.
Definition Quaternion.h:167
Quaternion & operator=(OtherRotation const &r)
Assign from another supported rotation type (see gv_detail::convert ).
Definition Quaternion.h:102
Quaternion operator*(const Quaternion &q) const
Multiply (combine) two rotations.
Definition Quaternion.h:283
DisplacementVector3D< Cartesian3D< double >, DefaultCoordinateSystemTag > XYZVector
Rotation operation on a cartesian vector.
Definition Quaternion.h:190
Rotation class with the (3D) rotation represented by a 3x3 orthogonal matrix.
Definition Rotation3D.h:71
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:64
Rotation class representing a 3D rotation about the Z axis by the angle of rotation.
Definition RotationZ.h:45
double dist(Rotation3D const &r1, Rotation3D const &r2)
void convert(R1 const &, R2 const)
Rotation3D::Scalar Scalar
AxisAngle::Scalar Distance(const AxisAngle &r1, const R &r2)
Distance between two rotations.
Definition AxisAngle.h:346
std::ostream & operator<<(std::ostream &os, const AxisAngle &a)
Stream Output and Input.
Definition AxisAngle.cxx:84
AxisAngle operator*(RotationX const &r1, AxisAngle const &r2)
Multiplication of an axial rotation by an AxisAngle.
namespace associated R package for ROOT.
Definition RExports.h:72