Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TQuaternion.h
Go to the documentation of this file.
1// @(#)root/physics:$Id$
2// Author: Eric Anciant 28/06/2005
3
4#ifndef ROOT_TQuaternion
5#define ROOT_TQuaternion
6
7#include "TVector3.h"
8#include "TMath.h"
9
10
11class TQuaternion : public TObject {
12
13public:
14
15 TQuaternion(Double_t real = 0, Double_t X = 0, Double_t Y = 0, Double_t Z = 0);
16 TQuaternion(const TVector3 & vector, Double_t real = 0);
17 TQuaternion(const Double_t *);
18 TQuaternion(const Float_t *);
19 // Constructors from an array : 0 to 2 = vector part, 3 = real part
20
21 TQuaternion(const TQuaternion &);
22 // The copy constructor.
23
24 virtual ~TQuaternion();
25 // Destructor
26
27 Double_t operator () (int) const;
28 inline Double_t operator [] (int) const;
29 // Get components by index. 0 to 2 = vector part, 3 = real part
30
32 inline Double_t & operator [] (int);
33 // Set components by index. 0 to 2 = vector part, 3 = real part
34
36 inline TQuaternion& SetRV(Double_t r, TVector3& vect);
37 // Sets components
39 // Set from vector direction and quaternion angle
40 Double_t GetQAngle() const;
42 // set and get quaternion angle
43
44 inline void GetRXYZ(Double_t *carray) const;
45 inline void GetRXYZ(Float_t *carray) const;
46 // Get the components into an array : 0 to 2 vector part, 3 real part
47 // not checked!
48
49 // --------------- real to quaternion algebra
51 inline Bool_t operator == (Double_t r) const;
52 inline Bool_t operator != (Double_t r) const;
53 inline TQuaternion& operator+=(Double_t real);
54 inline TQuaternion& operator-=(Double_t real);
55 inline TQuaternion& operator*=(Double_t real);
56 inline TQuaternion& operator/=(Double_t real);
57 TQuaternion operator*(Double_t real) const;
58 TQuaternion operator+(Double_t real) const;
59 TQuaternion operator-(Double_t real) const;
60 TQuaternion operator/(Double_t real) const;
61
62 // ---------------- vector to quaternion algebra
63 inline TQuaternion& operator=(const TVector3& );
64 inline Bool_t operator == (const TVector3&) const;
65 inline Bool_t operator != (const TVector3&) const;
66 inline TQuaternion& operator+=(const TVector3 &vector);
67 inline TQuaternion& operator-=(const TVector3 &vector);
68 TQuaternion& MultiplyLeft(const TVector3 &vector);
69 TQuaternion& operator*=(const TVector3 &vector);
70 TQuaternion& DivideLeft(const TVector3 &vector);
71 TQuaternion& operator/=(const TVector3 &vector);
72 TQuaternion operator+(const TVector3 &vector) const;
73 TQuaternion operator-(const TVector3 &vector) const;
74 TQuaternion LeftProduct(const TVector3 &vector) const;
75 TQuaternion operator*(const TVector3 &vector) const;
76 TQuaternion LeftQuotient(const TVector3 &vector) const;
77 TQuaternion operator/(const TVector3 &vector) const;
78
79 // ----------------- quaternion algebra
80 inline TQuaternion& operator=(const TQuaternion& );
81 inline Bool_t operator == (const TQuaternion&) const;
82 inline Bool_t operator != (const TQuaternion&) const;
83 inline TQuaternion& operator+=(const TQuaternion &quaternion);
84 inline TQuaternion& operator-=(const TQuaternion &quaternion);
85 TQuaternion& MultiplyLeft(const TQuaternion &quaternion);
86 TQuaternion& operator*=(const TQuaternion &quaternion);
87 TQuaternion& DivideLeft(const TQuaternion &quaternion);
88 TQuaternion& operator/=(const TQuaternion &quaternion);
89 TQuaternion operator+(const TQuaternion &quaternion) const;
90 TQuaternion operator-(const TQuaternion &quaternion) const;
91 TQuaternion LeftProduct(const TQuaternion &quaternion) const;
92 TQuaternion operator*(const TQuaternion &quaternion) const;
93 TQuaternion LeftQuotient(const TQuaternion &quaternion) const;
94 TQuaternion operator/(const TQuaternion &quaternion) const;
95
96 // ------------------ general algebra
97 inline Double_t Norm() const; // quaternion magnitude
98 inline Double_t Norm2() const; // quaternion squared magnitude
99 Double_t QMag() const { return Norm(); } // quaternion magnitude
100 Double_t QMag2() const { return Norm2(); } // quaternion squared magnitude
101 inline TQuaternion& Normalize(); // normalize quaternion
102 inline TQuaternion operator - () const; // Unary minus.
103 inline TQuaternion Conjugate() const;
104 TQuaternion Invert() const;
105 void Rotate(TVector3& vect) const;
106 TVector3 Rotation(const TVector3& vect) const;
107
108 void Print(Option_t* option="") const;
109
110 Double_t fRealPart; // Real part
111 TVector3 fVectorPart; // vector part
112
113 ClassDef(TQuaternion,1) // a quaternion class
114};
115
116
117// getters / setters
118
120 fRealPart = r;
122 return (*this);
123}
124
126 fRealPart = r;
127 fVectorPart= vect;
128 return (*this);
129}
130
131inline void TQuaternion::GetRXYZ(Double_t *carray) const {
132 fVectorPart.GetXYZ(carray+1);
133 carray[0] = fRealPart;
134}
135
136inline void TQuaternion::GetRXYZ(Float_t *carray) const {
137 fVectorPart.GetXYZ(carray+1);
138 carray[0] = (Float_t) fRealPart;
139}
140
141inline Double_t & TQuaternion::operator[] (int i) { return operator()(i); }
142inline Double_t TQuaternion::operator[] (int i) const { return operator()(i); }
143
144// ------------------ real to quaternion algebra
145
147 return (fVectorPart.Mag2()==0 && fRealPart == r) ? kTRUE : kFALSE;
148}
149
151 return (fVectorPart.Mag2()!=0 || fRealPart != r) ? kTRUE : kFALSE;
152}
153
155 fRealPart = r;
156 fVectorPart.SetXYZ(0,0,0);
157 return (*this);
158}
159
161 fRealPart += real;
162 return (*this);
163}
164
166 fRealPart -= real;
167 return (*this);
168}
169
171 fRealPart *= real;
172 fVectorPart *= real;
173 return (*this);
174}
175
177 if (real!=0) {
178 fRealPart /= real;
179 fVectorPart.SetX(fVectorPart.x()/real); // keep numericaly compliant with operator/(Double_t)
182 } else {
183 Error("operator/=()(Double_t)", "bad value (%f) ignored",real);
184 }
185 return (*this);
186}
187
192
193// ------------------- vector to quaternion algebra
194
196 return (fVectorPart == V && fRealPart == 0) ? kTRUE : kFALSE;
197}
198
200 return (fVectorPart != V || fRealPart != 0) ? kTRUE : kFALSE;
201}
202
204 fRealPart = 0;
205 fVectorPart.SetXYZ(vect.X(),vect.Y(),vect.Z());
206 return *this;
207}
208
210 fVectorPart += vect;
211 return (*this);
212}
213
215 fVectorPart -= vect;
216 return (*this);
217}
218
219TQuaternion operator + (const TVector3 &V, const TQuaternion &Q);
220TQuaternion operator - (const TVector3 &V, const TQuaternion &Q);
221TQuaternion operator * (const TVector3 &V, const TQuaternion &Q);
222TQuaternion operator / (const TVector3 &V, const TQuaternion &Q);
223
224// --------------- quaternion algebra
225
227 return (fVectorPart == Q.fVectorPart && fRealPart == Q.fRealPart) ? kTRUE : kFALSE;
228}
229
231 return (fVectorPart != Q.fVectorPart || fRealPart != Q.fRealPart) ? kTRUE : kFALSE;
232}
233
235 if (&quat != this) {
236 fRealPart = quat.fRealPart;
238 }
239 return (*this);
240}
241
243 fVectorPart += quaternion.fVectorPart;
244 fRealPart += quaternion.fRealPart;
245 return (*this);
246}
247
249 fVectorPart -= quaternion.fVectorPart;
250 fRealPart -= quaternion.fRealPart;
251 return (*this);
252}
253
254inline TQuaternion TQuaternion::operator+(const TQuaternion &quaternion) const {
255
256 return TQuaternion(fVectorPart+quaternion.fVectorPart, fRealPart+quaternion.fRealPart);
257}
258
259inline TQuaternion TQuaternion::operator-(const TQuaternion &quaternion) const {
260
261 return TQuaternion(fVectorPart-quaternion.fVectorPart, fRealPart-quaternion.fRealPart);
262}
263
264// ---------------- general
266 return TMath::Sqrt(Norm2());
267}
268
271}
272
274
275 (*this) /= Norm();
276 return (*this);
277}
278
281}
282
285}
286
287#endif
288
ROOT::R::TRInterface & r
Definition Object.C:4
const Bool_t kFALSE
Definition RtypesCore.h:101
double Double_t
Definition RtypesCore.h:59
float Float_t
Definition RtypesCore.h:57
const Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassDef(name, id)
Definition Rtypes.h:325
float * q
TQuaternion operator*(Double_t r, const TQuaternion &q)
TQuaternion operator+(Double_t r, const TQuaternion &q)
TQuaternion operator-(Double_t r, const TQuaternion &q)
TQuaternion operator/(Double_t r, const TQuaternion &q)
Mother of all ROOT objects.
Definition TObject.h:41
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:963
Quaternion is a 4-component mathematic object quite convenient when dealing with space rotation (or r...
Definition TQuaternion.h:11
Bool_t operator==(Double_t r) const
void GetRXYZ(Double_t *carray) const
TQuaternion & MultiplyLeft(const TVector3 &vector)
left multiplication
TQuaternion LeftQuotient(const TVector3 &vector) const
left quotient
TQuaternion operator*(Double_t real) const
product of quaternion with a real
TQuaternion & operator=(Double_t r)
Double_t QMag2() const
TQuaternion & operator-=(Double_t real)
TQuaternion & SetRV(Double_t r, TVector3 &vect)
virtual ~TQuaternion()
void Rotate(TVector3 &vect) const
rotate vect by current quaternion
Double_t operator()(int) const
dereferencing operator const
TQuaternion & DivideLeft(const TVector3 &vector)
left division
Bool_t operator!=(Double_t r) const
void Print(Option_t *option="") const
Print Quaternion parameters.
Double_t Norm() const
TQuaternion operator+(Double_t real) const
sum of quaternion with a real
TQuaternion LeftProduct(const TVector3 &vector) const
left product
TQuaternion & operator/=(Double_t real)
Double_t Norm2() const
TQuaternion & operator*=(Double_t real)
Double_t operator[](int) const
TQuaternion & operator+=(Double_t real)
TVector3 fVectorPart
TQuaternion & SetQAngle(Double_t angle)
Set angle of quaternion (rad) - keep quaternion norm N.B : this angle is half of the corresponding ro...
TQuaternion operator/(Double_t real) const
division by a real
Double_t fRealPart
TQuaternion Invert() const
invert
TQuaternion & Normalize()
TVector3 Rotation(const TVector3 &vect) const
rotation of vect by current quaternion
Double_t QMag() const
Definition TQuaternion.h:99
TQuaternion operator-() const
TQuaternion & SetAxisQAngle(TVector3 &v, Double_t QAngle)
set quaternion from vector and angle (rad) quaternion is set as unitary N.B : this angle is half of t...
TQuaternion & SetRXYZ(Double_t r, Double_t x, Double_t y, Double_t z)
TQuaternion Conjugate() const
Double_t GetQAngle() const
Get angle of quaternion (rad) N.B : this angle is half of the corresponding rotation angle.
Double_t Z() const
Definition TVector3.h:218
void SetY(Double_t)
Definition TVector3.h:224
void SetXYZ(Double_t x, Double_t y, Double_t z)
Definition TVector3.h:227
Double_t Y() const
Definition TVector3.h:217
Double_t Mag2() const
Definition TVector3.h:339
Double_t X() const
Definition TVector3.h:216
void GetXYZ(Double_t *carray) const
Definition TVector3.h:233
Double_t z() const
Definition TVector3.h:215
Double_t x() const
Definition TVector3.h:213
Double_t y() const
Definition TVector3.h:214
void SetX(Double_t)
Definition TVector3.h:223
void SetZ(Double_t)
Definition TVector3.h:225
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Double_t Sqrt(Double_t x)
Definition TMath.h:641