TQuaternion.h

Go to the documentation of this file.
00001 // @(#)root/physics:$Id: TQuaternion.h 30815 2009-10-20 13:49:22Z rdm $
00002 // Author: Eric Anciant 28/06/2005
00003 
00004 #ifndef ROOT_TQuaternion
00005 #define ROOT_TQuaternion
00006 
00007 #ifndef ROOT_TVector3
00008 #include "TVector3.h"
00009 #endif
00010 #ifndef ROOT_TMath
00011 #include "TMath.h"
00012 #endif
00013 
00014 
00015 class TQuaternion : public TObject {
00016 
00017 public:
00018 
00019    TQuaternion(Double_t real = 0, Double_t X = 0, Double_t Y = 0, Double_t Z = 0);
00020    TQuaternion(const TVector3 & vector, Double_t real = 0);
00021    TQuaternion(const Double_t *);
00022    TQuaternion(const Float_t *);
00023    // Constructors from an array : 0 to 2 = vector part, 3 = real part
00024 
00025    TQuaternion(const TQuaternion &);
00026    // The copy constructor.
00027 
00028    virtual ~TQuaternion();
00029    // Destructor
00030 
00031    Double_t operator () (int) const;
00032    inline Double_t operator [] (int) const;
00033    // Get components by index. 0 to 2 = vector part, 3 = real part
00034 
00035    Double_t & operator () (int);
00036    inline Double_t & operator [] (int);
00037    // Set components by index. 0 to 2 = vector part, 3 = real part
00038 
00039    inline TQuaternion& SetRXYZ(Double_t r,Double_t x,Double_t y,Double_t z);
00040    inline TQuaternion& SetRV(Double_t r, TVector3& vect);
00041    // Sets components
00042    TQuaternion& SetAxisQAngle(TVector3& v,Double_t QAngle);
00043    // Set from vector direction and quaternion angle
00044    Double_t GetQAngle() const;
00045    TQuaternion& SetQAngle(Double_t angle);
00046    // set and get quaternion angle
00047 
00048    inline void GetRXYZ(Double_t *carray) const;
00049    inline void GetRXYZ(Float_t *carray) const;
00050    // Get the components into an array : 0 to 2 vector part, 3 real part
00051    // not checked!
00052 
00053    // ---------------  real to quaternion algebra
00054    inline TQuaternion& operator=(Double_t r);
00055    inline Bool_t operator == (Double_t r) const;
00056    inline Bool_t operator != (Double_t r) const;
00057    inline TQuaternion& operator+=(Double_t real);
00058    inline TQuaternion& operator-=(Double_t real);
00059    inline TQuaternion& operator*=(Double_t real);
00060    inline TQuaternion& operator/=(Double_t real);
00061    TQuaternion operator*(Double_t real) const;
00062    TQuaternion operator+(Double_t real) const;
00063    TQuaternion operator-(Double_t real) const;
00064    TQuaternion operator/(Double_t real) const;
00065 
00066    // ---------------- vector to quaternion algebra
00067    inline TQuaternion& operator=(const TVector3& );
00068    inline Bool_t operator == (const TVector3&) const;
00069    inline Bool_t operator != (const TVector3&) const;
00070    inline TQuaternion& operator+=(const TVector3 &vector);
00071    inline TQuaternion& operator-=(const TVector3 &vector);
00072    TQuaternion& MultiplyLeft(const TVector3 &vector);
00073    TQuaternion& operator*=(const TVector3 &vector);
00074    TQuaternion& DivideLeft(const TVector3 &vector);
00075    TQuaternion& operator/=(const TVector3 &vector);
00076    TQuaternion operator+(const TVector3 &vector) const;
00077    TQuaternion operator-(const TVector3 &vector) const;
00078    TQuaternion LeftProduct(const TVector3 &vector) const;
00079    TQuaternion operator*(const TVector3 &vector) const;
00080    TQuaternion LeftQuotient(const TVector3 &vector) const;
00081    TQuaternion operator/(const TVector3 &vector) const;
00082 
00083    // ----------------- quaternion algebra
00084    inline TQuaternion& operator=(const TQuaternion& );
00085    inline Bool_t operator == (const TQuaternion&) const;
00086    inline Bool_t operator != (const TQuaternion&) const;
00087    inline TQuaternion& operator+=(const TQuaternion &quaternion);
00088    inline TQuaternion& operator-=(const TQuaternion &quaternion);
00089    TQuaternion& MultiplyLeft(const TQuaternion &quaternion);
00090    TQuaternion& operator*=(const TQuaternion &quaternion);
00091    TQuaternion& DivideLeft(const TQuaternion &quaternion);
00092    TQuaternion& operator/=(const TQuaternion &quaternion);
00093    TQuaternion operator+(const TQuaternion &quaternion) const;
00094    TQuaternion operator-(const TQuaternion &quaternion) const;
00095    TQuaternion LeftProduct(const TQuaternion &quaternion) const;
00096    TQuaternion operator*(const TQuaternion &quaternion) const;
00097    TQuaternion LeftQuotient(const TQuaternion &quaternion) const;
00098    TQuaternion operator/(const TQuaternion &quaternion) const;
00099 
00100    // ------------------ general algebra
00101    inline Double_t Norm() const; // quaternion magnitude
00102    inline Double_t Norm2() const; // quaternion squared magnitude
00103    Double_t QMag() const { return Norm(); } // quaternion magnitude
00104    Double_t QMag2() const { return Norm2(); } // quaternion squared magnitude
00105    inline TQuaternion& Normalize();  // normalize quaternion
00106    inline TQuaternion operator - () const; // Unary minus.
00107    inline TQuaternion Conjugate() const;
00108    TQuaternion Invert() const;
00109    void Rotate(TVector3& vect) const;
00110    TVector3 Rotation(const TVector3& vect) const;
00111 
00112    void Print(Option_t* option="") const;
00113 
00114    Double_t fRealPart;          // Real part
00115    TVector3 fVectorPart; // vector part
00116 
00117    ClassDef(TQuaternion,1) // a quaternion class
00118 };
00119 
00120 
00121 // getters / setters
00122 
00123 inline TQuaternion& TQuaternion::SetRXYZ(Double_t r,Double_t x,Double_t y,Double_t z) {
00124    fRealPart = r;
00125    fVectorPart.SetXYZ(x,y,z);
00126    return (*this);
00127 }
00128 
00129 inline TQuaternion& TQuaternion::SetRV(Double_t r, TVector3& vect) {
00130    fRealPart = r;
00131    fVectorPart= vect;
00132    return (*this);
00133 }
00134 
00135 inline void TQuaternion::GetRXYZ(Double_t *carray) const {
00136    fVectorPart.GetXYZ(carray+1);
00137    carray[0] = fRealPart;
00138 }
00139 
00140 inline void TQuaternion::GetRXYZ(Float_t *carray) const {
00141    fVectorPart.GetXYZ(carray+1);
00142    carray[0] = (Float_t) fRealPart;
00143 }
00144 
00145 inline Double_t & TQuaternion::operator[] (int i)       { return operator()(i); }
00146 inline Double_t   TQuaternion::operator[] (int i) const { return operator()(i); }
00147 
00148 // ------------------ real to quaternion algebra
00149 
00150 inline Bool_t TQuaternion::operator == (Double_t r) const {
00151    return (fVectorPart.Mag2()==0 && fRealPart == r) ? kTRUE : kFALSE;
00152 }
00153 
00154 inline Bool_t TQuaternion::operator != (Double_t r) const {
00155    return (fVectorPart.Mag2()!=0 || fRealPart != r) ? kTRUE : kFALSE;
00156 }
00157 
00158 inline TQuaternion& TQuaternion::operator=(Double_t r) {
00159    fRealPart = r;
00160    fVectorPart.SetXYZ(0,0,0);
00161    return (*this);
00162 }
00163 
00164 inline TQuaternion& TQuaternion::operator+=(Double_t real) {
00165    fRealPart += real;
00166    return (*this);
00167 }
00168 
00169 inline TQuaternion& TQuaternion::operator-=(Double_t real) {
00170    fRealPart -= real;
00171    return (*this);
00172 }
00173 
00174 inline TQuaternion& TQuaternion::operator*=(Double_t real) {
00175    fRealPart *= real;
00176    fVectorPart *= real;
00177    return (*this);
00178 }
00179 
00180 inline TQuaternion& TQuaternion::operator/=(Double_t real) {
00181    if (real!=0) {
00182       fRealPart /= real;
00183       fVectorPart.SetX(fVectorPart.x()/real); // keep numericaly compliant with operator/(Double_t)
00184       fVectorPart.SetY(fVectorPart.y()/real);
00185       fVectorPart.SetZ(fVectorPart.z()/real);
00186    } else {
00187       Error("operator/=()(Double_t)", "bad value (%f) ignored",real);
00188    }
00189    return (*this);
00190 }
00191 
00192 TQuaternion operator + (Double_t r, const TQuaternion & q);
00193 TQuaternion operator - (Double_t r, const TQuaternion & q);
00194 TQuaternion operator * (Double_t r, const TQuaternion & q);
00195 TQuaternion operator / (Double_t r, const TQuaternion & q);
00196 
00197 // ------------------- vector to quaternion algebra
00198 
00199 inline Bool_t TQuaternion::operator == (const TVector3& V) const {
00200    return (fVectorPart == V && fRealPart == 0) ? kTRUE : kFALSE;
00201 }
00202 
00203 inline Bool_t TQuaternion::operator != (const TVector3& V) const {
00204    return (fVectorPart != V || fRealPart != 0) ? kTRUE : kFALSE;
00205 }
00206 
00207 inline TQuaternion& TQuaternion::operator=(const TVector3& vect) {
00208    fRealPart = 0;
00209    fVectorPart.SetXYZ(vect.X(),vect.Y(),vect.Z());
00210    return *this;
00211 }
00212 
00213 inline TQuaternion& TQuaternion::operator+=(const TVector3 &vect) {
00214    fVectorPart += vect;
00215    return (*this);
00216 }
00217 
00218 inline TQuaternion& TQuaternion::operator-=(const TVector3 &vect) {
00219    fVectorPart -= vect;
00220    return (*this);
00221 }
00222 
00223 TQuaternion operator + (const TVector3 &V, const TQuaternion &Q);
00224 TQuaternion operator - (const TVector3 &V, const TQuaternion &Q);
00225 TQuaternion operator * (const TVector3 &V, const TQuaternion &Q);
00226 TQuaternion operator / (const TVector3 &V, const TQuaternion &Q);
00227 
00228 // --------------- quaternion algebra
00229 
00230 inline Bool_t TQuaternion::operator == (const TQuaternion& Q) const {
00231    return (fVectorPart == Q.fVectorPart && fRealPart == Q.fRealPart) ? kTRUE : kFALSE;
00232 }
00233 
00234 inline Bool_t TQuaternion::operator != (const TQuaternion& Q) const {
00235    return (fVectorPart != Q.fVectorPart || fRealPart != Q.fRealPart) ? kTRUE : kFALSE;
00236 }
00237 
00238 inline TQuaternion& TQuaternion::operator=(const TQuaternion& quat) {
00239    fRealPart = quat.fRealPart;
00240    fVectorPart.SetXYZ(quat.fVectorPart.X(),quat.fVectorPart.Y(),quat.fVectorPart.Z());
00241    return (*this);
00242 }
00243 
00244 inline TQuaternion& TQuaternion::operator+=(const TQuaternion &quaternion) {
00245    fVectorPart += quaternion.fVectorPart;
00246    fRealPart += quaternion.fRealPart;
00247    return (*this);
00248 }
00249 
00250 inline TQuaternion& TQuaternion::operator-=(const TQuaternion &quaternion) {
00251    fVectorPart -= quaternion.fVectorPart;
00252    fRealPart   -= quaternion.fRealPart;
00253    return (*this);
00254 }
00255 
00256 inline TQuaternion TQuaternion::operator+(const TQuaternion &quaternion) const {
00257 
00258    return TQuaternion(fVectorPart+quaternion.fVectorPart, fRealPart+quaternion.fRealPart);
00259 }
00260 
00261 inline TQuaternion TQuaternion::operator-(const TQuaternion &quaternion) const {
00262 
00263    return TQuaternion(fVectorPart-quaternion.fVectorPart, fRealPart-quaternion.fRealPart);
00264 }
00265 
00266 // ---------------- general
00267 inline Double_t TQuaternion::Norm() const {
00268    return TMath::Sqrt(Norm2());
00269 }
00270 
00271 inline Double_t TQuaternion::Norm2() const {
00272    return fRealPart*fRealPart + fVectorPart.Mag2();
00273 }
00274 
00275 inline TQuaternion& TQuaternion::Normalize() {
00276 
00277    (*this) /= Norm();
00278    return (*this);
00279 }
00280 
00281 inline TQuaternion TQuaternion::operator - () const {
00282    return TQuaternion(-fVectorPart,-fRealPart);
00283 }
00284 
00285 inline TQuaternion TQuaternion::Conjugate() const {
00286    return TQuaternion(-fVectorPart,fRealPart);
00287 }
00288 
00289 #endif
00290 

Generated on Tue Jul 5 14:28:07 2011 for ROOT_528-00b_version by  doxygen 1.5.1