Logo ROOT  
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
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 
TVector3
Definition: TVector3.h:22
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TQuaternion::operator()
Double_t operator()(int) const
dereferencing operator const
Definition: TQuaternion.cxx:121
TVector3::SetXYZ
void SetXYZ(Double_t x, Double_t y, Double_t z)
Definition: TVector3.h:227
TVector3::Y
Double_t Y() const
Definition: TVector3.h:217
TQuaternion::QMag2
Double_t QMag2() const
Definition: TQuaternion.h:100
TQuaternion::SetQAngle
TQuaternion & SetQAngle(Double_t angle)
Set angle of quaternion (rad) - keep quaternion norm N.B : this angle is half of the corresponding ro...
Definition: TQuaternion.cxx:165
TQuaternion::LeftProduct
TQuaternion LeftProduct(const TVector3 &vector) const
left product
Definition: TQuaternion.cxx:274
r
ROOT::R::TRInterface & r
Definition: Object.C:4
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TMath::Sqrt
Double_t Sqrt(Double_t x)
Definition: TMath.h:691
TQuaternion::Rotate
void Rotate(TVector3 &vect) const
rotate vect by current quaternion
Definition: TQuaternion.cxx:493
TQuaternion::GetRXYZ
void GetRXYZ(Double_t *carray) const
Definition: TQuaternion.h:131
Float_t
float Float_t
Definition: RtypesCore.h:57
TQuaternion::SetRXYZ
TQuaternion & SetRXYZ(Double_t r, Double_t x, Double_t y, Double_t z)
Definition: TQuaternion.h:119
TVector3::y
Double_t y() const
Definition: TVector3.h:214
x
Double_t x[n]
Definition: legend1.C:17
TVector3::SetZ
void SetZ(Double_t)
Definition: TVector3.h:225
TVector3::Mag2
Double_t Mag2() const
Definition: TVector3.h:339
v
@ v
Definition: rootcling_impl.cxx:3635
operator-
TQuaternion operator-(Double_t r, const TQuaternion &q)
Definition: TQuaternion.cxx:227
TQuaternion::operator/=
TQuaternion & operator/=(Double_t real)
Definition: TQuaternion.h:176
TQuaternion::operator-=
TQuaternion & operator-=(Double_t real)
Definition: TQuaternion.h:165
bool
TVector3.h
q
float * q
Definition: THbookFile.cxx:89
TQuaternion::SetAxisQAngle
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...
Definition: TQuaternion.cxx:179
TVector3::z
Double_t z() const
Definition: TVector3.h:215
TQuaternion::Print
void Print(Option_t *option="") const
Print Quaternion parameters.
Definition: TQuaternion.cxx:527
Option_t
const typedef char Option_t
Definition: RtypesCore.h:66
TQuaternion::~TQuaternion
virtual ~TQuaternion()
Definition: TQuaternion.cxx:116
operator/
TQuaternion operator/(Double_t r, const TQuaternion &q)
Definition: TQuaternion.cxx:229
TQuaternion::operator=
TQuaternion & operator=(Double_t r)
Definition: TQuaternion.h:154
TQuaternion::operator*=
TQuaternion & operator*=(Double_t real)
Definition: TQuaternion.h:170
TQuaternion::TQuaternion
TQuaternion(Double_t real=0, Double_t X=0, Double_t Y=0, Double_t Z=0)
Definition: TQuaternion.cxx:113
TQuaternion::MultiplyLeft
TQuaternion & MultiplyLeft(const TVector3 &vector)
left multiplication
Definition: TQuaternion.cxx:250
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TQuaternion::Invert
TQuaternion Invert() const
invert
Definition: TQuaternion.cxx:479
TVector3::x
Double_t x() const
Definition: TVector3.h:213
TQuaternion::DivideLeft
TQuaternion & DivideLeft(const TVector3 &vector)
left division
Definition: TQuaternion.cxx:288
TVector3::SetX
void SetX(Double_t)
Definition: TVector3.h:223
TQuaternion::LeftQuotient
TQuaternion LeftQuotient(const TVector3 &vector) const
left quotient
Definition: TQuaternion.cxx:318
TQuaternion::operator*
TQuaternion operator*(Double_t real) const
product of quaternion with a real
Definition: TQuaternion.cxx:208
TQuaternion::operator+
TQuaternion operator+(Double_t real) const
sum of quaternion with a real
Definition: TQuaternion.cxx:194
y
Double_t y[n]
Definition: legend1.C:17
TQuaternion::operator/
TQuaternion operator/(Double_t real) const
division by a real
Definition: TQuaternion.cxx:216
TQuaternion::Conjugate
TQuaternion Conjugate() const
Definition: TQuaternion.h:283
TQuaternion::operator==
Bool_t operator==(Double_t r) const
Definition: TQuaternion.h:146
TQuaternion::QMag
Double_t QMag() const
Definition: TQuaternion.h:99
TQuaternion::Norm
Double_t Norm() const
Definition: TQuaternion.h:265
TVector3::GetXYZ
void GetXYZ(Double_t *carray) const
Definition: TVector3.h:233
TQuaternion::GetQAngle
Double_t GetQAngle() const
Get angle of quaternion (rad) N.B : this angle is half of the corresponding rotation angle.
Definition: TQuaternion.cxx:155
TQuaternion::operator[]
Double_t operator[](int) const
Definition: TQuaternion.h:142
Double_t
double Double_t
Definition: RtypesCore.h:59
TQuaternion::Rotation
TVector3 Rotation(const TVector3 &vect) const
rotation of vect by current quaternion
Definition: TQuaternion.cxx:500
TObject
Definition: TObject.h:37
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TQuaternion::operator!=
Bool_t operator!=(Double_t r) const
Definition: TQuaternion.h:150
TVector3::Z
Double_t Z() const
Definition: TVector3.h:218
TQuaternion::SetRV
TQuaternion & SetRV(Double_t r, TVector3 &vect)
Definition: TQuaternion.h:125
TQuaternion::operator+=
TQuaternion & operator+=(Double_t real)
Definition: TQuaternion.h:160
TQuaternion::fRealPart
Double_t fRealPart
Definition: TQuaternion.h:110
TQuaternion::Normalize
TQuaternion & Normalize()
Definition: TQuaternion.h:273
TQuaternion
Definition: TQuaternion.h:11
TVector3::X
Double_t X() const
Definition: TVector3.h:216
operator*
TQuaternion operator*(Double_t r, const TQuaternion &q)
Definition: TQuaternion.cxx:228
TQuaternion::operator-
TQuaternion operator-() const
Definition: TQuaternion.h:279
TVector3::SetY
void SetY(Double_t)
Definition: TVector3.h:224
TMath.h
TQuaternion::Norm2
Double_t Norm2() const
Definition: TQuaternion.h:269
TQuaternion::fVectorPart
TVector3 fVectorPart
Definition: TQuaternion.h:111
operator+
TQuaternion operator+(Double_t r, const TQuaternion &q)
Definition: TQuaternion.cxx:226
ROOT::Math::Cephes::Q
static double Q[]
Definition: SpecFuncCephes.cxx:294