ROOT  6.06/09
Reference Guide
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 #ifndef ROOT_TVector3
8 #include "TVector3.h"
9 #endif
10 #ifndef ROOT_TMath
11 #include "TMath.h"
12 #endif
13 
14 
15 class TQuaternion : public TObject {
16 
17 public:
18 
19  TQuaternion(Double_t real = 0, Double_t X = 0, Double_t Y = 0, Double_t Z = 0);
20  TQuaternion(const TVector3 & vector, Double_t real = 0);
21  TQuaternion(const Double_t *);
22  TQuaternion(const Float_t *);
23  // Constructors from an array : 0 to 2 = vector part, 3 = real part
24 
25  TQuaternion(const TQuaternion &);
26  // The copy constructor.
27 
28  virtual ~TQuaternion();
29  // Destructor
30 
31  Double_t operator () (int) const;
32  inline Double_t operator [] (int) const;
33  // Get components by index. 0 to 2 = vector part, 3 = real part
34 
35  Double_t & operator () (int);
36  inline Double_t & operator [] (int);
37  // Set components by index. 0 to 2 = vector part, 3 = real part
38 
40  inline TQuaternion& SetRV(Double_t r, TVector3& vect);
41  // Sets components
43  // Set from vector direction and quaternion angle
44  Double_t GetQAngle() const;
46  // set and get quaternion angle
47 
48  inline void GetRXYZ(Double_t *carray) const;
49  inline void GetRXYZ(Float_t *carray) const;
50  // Get the components into an array : 0 to 2 vector part, 3 real part
51  // not checked!
52 
53  // --------------- real to quaternion algebra
54  inline TQuaternion& operator=(Double_t r);
55  inline Bool_t operator == (Double_t r) const;
56  inline Bool_t operator != (Double_t r) const;
57  inline TQuaternion& operator+=(Double_t real);
58  inline TQuaternion& operator-=(Double_t real);
59  inline TQuaternion& operator*=(Double_t real);
60  inline TQuaternion& operator/=(Double_t real);
61  TQuaternion operator*(Double_t real) const;
62  TQuaternion operator+(Double_t real) const;
63  TQuaternion operator-(Double_t real) const;
64  TQuaternion operator/(Double_t real) const;
65 
66  // ---------------- vector to quaternion algebra
67  inline TQuaternion& operator=(const TVector3& );
68  inline Bool_t operator == (const TVector3&) const;
69  inline Bool_t operator != (const TVector3&) const;
70  inline TQuaternion& operator+=(const TVector3 &vector);
71  inline TQuaternion& operator-=(const TVector3 &vector);
72  TQuaternion& MultiplyLeft(const TVector3 &vector);
73  TQuaternion& operator*=(const TVector3 &vector);
74  TQuaternion& DivideLeft(const TVector3 &vector);
75  TQuaternion& operator/=(const TVector3 &vector);
76  TQuaternion operator+(const TVector3 &vector) const;
77  TQuaternion operator-(const TVector3 &vector) const;
78  TQuaternion LeftProduct(const TVector3 &vector) const;
79  TQuaternion operator*(const TVector3 &vector) const;
80  TQuaternion LeftQuotient(const TVector3 &vector) const;
81  TQuaternion operator/(const TVector3 &vector) const;
82 
83  // ----------------- quaternion algebra
84  inline TQuaternion& operator=(const TQuaternion& );
85  inline Bool_t operator == (const TQuaternion&) const;
86  inline Bool_t operator != (const TQuaternion&) const;
87  inline TQuaternion& operator+=(const TQuaternion &quaternion);
88  inline TQuaternion& operator-=(const TQuaternion &quaternion);
89  TQuaternion& MultiplyLeft(const TQuaternion &quaternion);
90  TQuaternion& operator*=(const TQuaternion &quaternion);
91  TQuaternion& DivideLeft(const TQuaternion &quaternion);
92  TQuaternion& operator/=(const TQuaternion &quaternion);
93  TQuaternion operator+(const TQuaternion &quaternion) const;
94  TQuaternion operator-(const TQuaternion &quaternion) const;
95  TQuaternion LeftProduct(const TQuaternion &quaternion) const;
96  TQuaternion operator*(const TQuaternion &quaternion) const;
97  TQuaternion LeftQuotient(const TQuaternion &quaternion) const;
98  TQuaternion operator/(const TQuaternion &quaternion) const;
99 
100  // ------------------ general algebra
101  inline Double_t Norm() const; // quaternion magnitude
102  inline Double_t Norm2() const; // quaternion squared magnitude
103  Double_t QMag() const { return Norm(); } // quaternion magnitude
104  Double_t QMag2() const { return Norm2(); } // quaternion squared magnitude
105  inline TQuaternion& Normalize(); // normalize quaternion
106  inline TQuaternion operator - () const; // Unary minus.
107  inline TQuaternion Conjugate() const;
108  TQuaternion Invert() const;
109  void Rotate(TVector3& vect) const;
110  TVector3 Rotation(const TVector3& vect) const;
111 
112  void Print(Option_t* option="") const;
113 
114  Double_t fRealPart; // Real part
115  TVector3 fVectorPart; // vector part
116 
117  ClassDef(TQuaternion,1) // a quaternion class
118 };
119 
120 
121 // getters / setters
122 
124  fRealPart = r;
125  fVectorPart.SetXYZ(x,y,z);
126  return (*this);
127 }
128 
130  fRealPart = r;
131  fVectorPart= vect;
132  return (*this);
133 }
134 
135 inline void TQuaternion::GetRXYZ(Double_t *carray) const {
136  fVectorPart.GetXYZ(carray+1);
137  carray[0] = fRealPart;
138 }
139 
140 inline void TQuaternion::GetRXYZ(Float_t *carray) const {
141  fVectorPart.GetXYZ(carray+1);
142  carray[0] = (Float_t) fRealPart;
143 }
144 
145 inline Double_t & TQuaternion::operator[] (int i) { return operator()(i); }
146 inline Double_t TQuaternion::operator[] (int i) const { return operator()(i); }
147 
148 // ------------------ real to quaternion algebra
149 
151  return (fVectorPart.Mag2()==0 && fRealPart == r) ? kTRUE : kFALSE;
152 }
153 
155  return (fVectorPart.Mag2()!=0 || fRealPart != r) ? kTRUE : kFALSE;
156 }
157 
159  fRealPart = r;
160  fVectorPart.SetXYZ(0,0,0);
161  return (*this);
162 }
163 
165  fRealPart += real;
166  return (*this);
167 }
168 
170  fRealPart -= real;
171  return (*this);
172 }
173 
175  fRealPart *= real;
176  fVectorPart *= real;
177  return (*this);
178 }
179 
181  if (real!=0) {
182  fRealPart /= real;
183  fVectorPart.SetX(fVectorPart.x()/real); // keep numericaly compliant with operator/(Double_t)
184  fVectorPart.SetY(fVectorPart.y()/real);
185  fVectorPart.SetZ(fVectorPart.z()/real);
186  } else {
187  Error("operator/=()(Double_t)", "bad value (%f) ignored",real);
188  }
189  return (*this);
190 }
191 
196 
197 // ------------------- vector to quaternion algebra
198 
199 inline Bool_t TQuaternion::operator == (const TVector3& V) const {
200  return (fVectorPart == V && fRealPart == 0) ? kTRUE : kFALSE;
201 }
202 
203 inline Bool_t TQuaternion::operator != (const TVector3& V) const {
204  return (fVectorPart != V || fRealPart != 0) ? kTRUE : kFALSE;
205 }
206 
208  fRealPart = 0;
209  fVectorPart.SetXYZ(vect.X(),vect.Y(),vect.Z());
210  return *this;
211 }
212 
214  fVectorPart += vect;
215  return (*this);
216 }
217 
219  fVectorPart -= vect;
220  return (*this);
221 }
222 
223 TQuaternion operator + (const TVector3 &V, const TQuaternion &Q);
224 TQuaternion operator - (const TVector3 &V, const TQuaternion &Q);
225 TQuaternion operator * (const TVector3 &V, const TQuaternion &Q);
226 TQuaternion operator / (const TVector3 &V, const TQuaternion &Q);
227 
228 // --------------- quaternion algebra
229 
231  return (fVectorPart == Q.fVectorPart && fRealPart == Q.fRealPart) ? kTRUE : kFALSE;
232 }
233 
235  return (fVectorPart != Q.fVectorPart || fRealPart != Q.fRealPart) ? kTRUE : kFALSE;
236 }
237 
239  if (&quat != this) {
240  fRealPart = quat.fRealPart;
241  fVectorPart.SetXYZ(quat.fVectorPart.X(),quat.fVectorPart.Y(),quat.fVectorPart.Z());
242  }
243  return (*this);
244 }
245 
246 inline TQuaternion& TQuaternion::operator+=(const TQuaternion &quaternion) {
247  fVectorPart += quaternion.fVectorPart;
248  fRealPart += quaternion.fRealPart;
249  return (*this);
250 }
251 
252 inline TQuaternion& TQuaternion::operator-=(const TQuaternion &quaternion) {
253  fVectorPart -= quaternion.fVectorPart;
254  fRealPart -= quaternion.fRealPart;
255  return (*this);
256 }
257 
258 inline TQuaternion TQuaternion::operator+(const TQuaternion &quaternion) const {
259 
260  return TQuaternion(fVectorPart+quaternion.fVectorPart, fRealPart+quaternion.fRealPart);
261 }
262 
263 inline TQuaternion TQuaternion::operator-(const TQuaternion &quaternion) const {
264 
265  return TQuaternion(fVectorPart-quaternion.fVectorPart, fRealPart-quaternion.fRealPart);
266 }
267 
268 // ---------------- general
269 inline Double_t TQuaternion::Norm() const {
270  return TMath::Sqrt(Norm2());
271 }
272 
273 inline Double_t TQuaternion::Norm2() const {
274  return fRealPart*fRealPart + fVectorPart.Mag2();
275 }
276 
278 
279  (*this) /= Norm();
280  return (*this);
281 }
282 
285 }
286 
289 }
290 
291 #endif
292 
Double_t y() const
Definition: TVector3.h:218
TQuaternion operator/(Double_t r, const TQuaternion &q)
TQuaternion LeftQuotient(const TVector3 &vector) const
left quotient
Double_t Z() const
Definition: TVector3.h:222
Double_t z() const
Definition: TVector3.h:219
void SetY(Double_t)
Definition: TVector3.h:228
float Float_t
Definition: RtypesCore.h:53
const char Option_t
Definition: RtypesCore.h:62
TVector3 fVectorPart
Definition: TQuaternion.h:115
TQuaternion operator-() const
Definition: TQuaternion.h:283
TQuaternion operator*(Double_t real) const
product of quaternion with a real
TQuaternion operator+(Double_t real) const
sum of quaternion with a real
Double_t operator[](int) const
Definition: TQuaternion.h:146
void Rotate(TVector3 &vect) const
rotate vect by current quaternion
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
TQuaternion & SetQAngle(Double_t angle)
Set angle of quaternion (rad) - keep quaternion norm N.B : this angle is half of the corresponding ro...
Double_t Norm() const
Definition: TQuaternion.h:269
TQuaternion & operator/=(Double_t real)
Definition: TQuaternion.h:180
TQuaternion & operator-=(Double_t real)
Definition: TQuaternion.h:169
Double_t x[n]
Definition: legend1.C:17
Double_t QMag2() const
Definition: TQuaternion.h:104
virtual ~TQuaternion()
#define ClassDef(name, id)
Definition: Rtypes.h:254
TQuaternion operator*(Double_t r, const TQuaternion &q)
TQuaternion & SetRXYZ(Double_t r, Double_t x, Double_t y, Double_t z)
Definition: TQuaternion.h:123
TQuaternion operator/(Double_t real) const
division by a real
void SetXYZ(Double_t x, Double_t y, Double_t z)
Definition: TVector3.h:231
TQuaternion LeftProduct(const TVector3 &vector) const
left product
TQuaternion & DivideLeft(const TVector3 &vector)
left division
TQuaternion & operator=(Double_t r)
Definition: TQuaternion.h:158
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
Bool_t operator!=(Double_t r) const
Definition: TQuaternion.h:154
void Print(Option_t *option="") const
Print Quaternion parameters.
TQuaternion operator-(Double_t r, const TQuaternion &q)
TQuaternion & operator*=(Double_t real)
Definition: TQuaternion.h:174
TQuaternion Conjugate() const
Definition: TQuaternion.h:287
void GetRXYZ(Double_t *carray) const
Definition: TQuaternion.h:135
ROOT::R::TRInterface & r
Definition: Object.C:4
SVector< double, 2 > v
Definition: Dict.h:5
TQuaternion operator+(Double_t r, const TQuaternion &q)
TQuaternion(Double_t real=0, Double_t X=0, Double_t Y=0, Double_t Z=0)
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...
Double_t X() const
Definition: TVector3.h:220
Double_t x() const
Definition: TVector3.h:217
Double_t Mag2() const
Definition: TVector3.h:298
Double_t operator()(int) const
dereferencing operator const
TQuaternion & MultiplyLeft(const TVector3 &vector)
left multitplication
Double_t QMag() const
Definition: TQuaternion.h:103
TVector3 Rotation(const TVector3 &vect) const
rotation of vect by current quaternion
double Double_t
Definition: RtypesCore.h:55
void SetZ(Double_t)
Definition: TVector3.h:229
Double_t y[n]
Definition: legend1.C:17
Bool_t operator==(Double_t r) const
Definition: TQuaternion.h:150
Double_t GetQAngle() const
Get angle of quaternion (rad) N.B : this angle is half of the corresponding rotation angle...
void GetXYZ(Double_t *carray) const
Definition: TVector3.h:237
Mother of all ROOT objects.
Definition: TObject.h:58
TQuaternion & SetRV(Double_t r, TVector3 &vect)
Definition: TQuaternion.h:129
TQuaternion & Normalize()
Definition: TQuaternion.h:277
TQuaternion Invert() const
invert
Double_t fRealPart
Definition: TQuaternion.h:114
Double_t Y() const
Definition: TVector3.h:221
void SetX(Double_t)
Definition: TVector3.h:227
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
std::complex< float_v > Z
Definition: main.cpp:120
const Bool_t kTRUE
Definition: Rtypes.h:91
float * q
Definition: THbookFile.cxx:87
Double_t Norm2() const
Definition: TQuaternion.h:273
static double Q[]
TQuaternion & operator+=(Double_t real)
Definition: TQuaternion.h:164