Logo ROOT   6.14/05
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 #include "TVector3.h"
8 #include "TMath.h"
9 
10 
11 class TQuaternion : public TObject {
12 
13 public:
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 
31  Double_t & operator () (int);
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
50  inline TQuaternion& operator=(Double_t r);
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;
121  fVectorPart.SetXYZ(x,y,z);
122  return (*this);
123 }
124 
126  fRealPart = r;
127  fVectorPart= vect;
128  return (*this);
129 }
130 
131 inline void TQuaternion::GetRXYZ(Double_t *carray) const {
132  fVectorPart.GetXYZ(carray+1);
133  carray[0] = fRealPart;
134 }
135 
136 inline void TQuaternion::GetRXYZ(Float_t *carray) const {
137  fVectorPart.GetXYZ(carray+1);
138  carray[0] = (Float_t) fRealPart;
139 }
140 
141 inline Double_t & TQuaternion::operator[] (int i) { return operator()(i); }
142 inline 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)
180  fVectorPart.SetY(fVectorPart.y()/real);
181  fVectorPart.SetZ(fVectorPart.z()/real);
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 
195 inline Bool_t TQuaternion::operator == (const TVector3& V) const {
196  return (fVectorPart == V && fRealPart == 0) ? kTRUE : kFALSE;
197 }
198 
199 inline Bool_t TQuaternion::operator != (const TVector3& V) const {
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 
219 TQuaternion operator + (const TVector3 &V, const TQuaternion &Q);
220 TQuaternion operator - (const TVector3 &V, const TQuaternion &Q);
221 TQuaternion operator * (const TVector3 &V, const TQuaternion &Q);
222 TQuaternion 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;
237  fVectorPart.SetXYZ(quat.fVectorPart.X(),quat.fVectorPart.Y(),quat.fVectorPart.Z());
238  }
239  return (*this);
240 }
241 
242 inline TQuaternion& TQuaternion::operator+=(const TQuaternion &quaternion) {
243  fVectorPart += quaternion.fVectorPart;
244  fRealPart += quaternion.fRealPart;
245  return (*this);
246 }
247 
248 inline TQuaternion& TQuaternion::operator-=(const TQuaternion &quaternion) {
249  fVectorPart -= quaternion.fVectorPart;
250  fRealPart -= quaternion.fRealPart;
251  return (*this);
252 }
253 
254 inline TQuaternion TQuaternion::operator+(const TQuaternion &quaternion) const {
255 
256  return TQuaternion(fVectorPart+quaternion.fVectorPart, fRealPart+quaternion.fRealPart);
257 }
258 
259 inline TQuaternion TQuaternion::operator-(const TQuaternion &quaternion) const {
260 
261  return TQuaternion(fVectorPart-quaternion.fVectorPart, fRealPart-quaternion.fRealPart);
262 }
263 
264 // ---------------- general
265 inline Double_t TQuaternion::Norm() const {
266  return TMath::Sqrt(Norm2());
267 }
268 
269 inline Double_t TQuaternion::Norm2() const {
270  return fRealPart*fRealPart + fVectorPart.Mag2();
271 }
272 
274 
275  (*this) /= Norm();
276  return (*this);
277 }
278 
281 }
282 
285 }
286 
287 #endif
288 
Double_t X() const
Definition: TVector3.h:216
void SetY(Double_t)
Definition: TVector3.h:224
TQuaternion operator-() const
Definition: TQuaternion.h:279
float Float_t
Definition: RtypesCore.h:53
const char Option_t
Definition: RtypesCore.h:62
TVector3 fVectorPart
Definition: TQuaternion.h:111
Quaternion is a 4-component mathematic object quite convenient when dealing with space rotation (or r...
Definition: TQuaternion.h:11
Double_t Mag2() const
Definition: TVector3.h:339
bool Bool_t
Definition: RtypesCore.h:59
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 y() const
Definition: TVector3.h:214
void Rotate(TVector3 &vect) const
rotate vect by current quaternion
TQuaternion & operator/=(Double_t real)
Definition: TQuaternion.h:176
TQuaternion & operator-=(Double_t real)
Definition: TQuaternion.h:165
Double_t x[n]
Definition: legend1.C:17
virtual ~TQuaternion()
#define ClassDef(name, id)
Definition: Rtypes.h:320
void GetRXYZ(Double_t *carray) const
Definition: TQuaternion.h:131
Double_t Y() const
Definition: TVector3.h:217
TQuaternion & SetRXYZ(Double_t r, Double_t x, Double_t y, Double_t z)
Definition: TQuaternion.h:119
void SetXYZ(Double_t x, Double_t y, Double_t z)
Definition: TVector3.h:227
void GetXYZ(Double_t *carray) const
Definition: TVector3.h:233
TQuaternion & DivideLeft(const TVector3 &vector)
left division
TQuaternion & operator=(Double_t r)
Definition: TQuaternion.h:154
TVector3 is a general three vector class, which can be used for the description of different vectors ...
Definition: TVector3.h:22
Bool_t operator==(Double_t r) const
Definition: TQuaternion.h:146
Double_t Z() const
Definition: TVector3.h:218
TQuaternion & operator*=(Double_t real)
Definition: TQuaternion.h:170
ROOT::R::TRInterface & r
Definition: Object.C:4
SVector< double, 2 > v
Definition: Dict.h:5
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...
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
void Print(Option_t *option="") const
Print Quaternion parameters.
TQuaternion operator*(Double_t real) const
product of quaternion with a real
Double_t Norm() const
Definition: TQuaternion.h:265
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:215
const Bool_t kFALSE
Definition: RtypesCore.h:88
TQuaternion & MultiplyLeft(const TVector3 &vector)
left multiplication
TQuaternion Conjugate() const
Definition: TQuaternion.h:283
TQuaternion operator/(Double_t real) const
division by a real
double Double_t
Definition: RtypesCore.h:55
void SetZ(Double_t)
Definition: TVector3.h:225
Double_t QMag() const
Definition: TQuaternion.h:99
TVector3 Rotation(const TVector3 &vect) const
rotation of vect by current quaternion
TQuaternion Invert() const
invert
TQuaternion LeftQuotient(const TVector3 &vector) const
left quotient
Double_t y[n]
Definition: legend1.C:17
Bool_t operator!=(Double_t r) const
Definition: TQuaternion.h:150
TQuaternion operator+(Double_t real) const
sum of quaternion with a real
Mother of all ROOT objects.
Definition: TObject.h:37
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
TQuaternion & SetRV(Double_t r, TVector3 &vect)
Definition: TQuaternion.h:125
TQuaternion & Normalize()
Definition: TQuaternion.h:273
Double_t x() const
Definition: TVector3.h:213
Double_t fRealPart
Definition: TQuaternion.h:110
Double_t operator[](int) const
Definition: TQuaternion.h:142
void SetX(Double_t)
Definition: TVector3.h:223
Double_t Sqrt(Double_t x)
Definition: TMath.h:690
Double_t operator()(int) const
dereferencing operator const
float * q
Definition: THbookFile.cxx:87
const Bool_t kTRUE
Definition: RtypesCore.h:87
Double_t Norm2() const
Definition: TQuaternion.h:269
Double_t QMag2() const
Definition: TQuaternion.h:100
TQuaternion LeftProduct(const TVector3 &vector) const
left product
static double Q[]
TQuaternion & operator+=(Double_t real)
Definition: TQuaternion.h:160