Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/TQuaternion.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // @(#)root/physics:$Id$
0002 // Author: Eric Anciant 28/06/2005
0003 
0004 #ifndef ROOT_TQuaternion
0005 #define ROOT_TQuaternion
0006 
0007 #include "TVector3.h"
0008 #include "TMath.h"
0009 
0010 
0011 class TQuaternion : public TObject {
0012 
0013 public:
0014 
0015    TQuaternion(Double_t real = 0, Double_t X = 0, Double_t Y = 0, Double_t Z = 0);
0016    TQuaternion(const TVector3 & vector, Double_t real = 0);
0017    TQuaternion(const Double_t *);
0018    TQuaternion(const Float_t *);
0019    // Constructors from an array : 0 to 2 = vector part, 3 = real part
0020 
0021    TQuaternion(const TQuaternion &);
0022    // The copy constructor.
0023 
0024    ~TQuaternion() override;
0025    // Destructor
0026 
0027    Double_t operator () (int) const;
0028    inline Double_t operator [] (int) const;
0029    // Get components by index. 0 to 2 = vector part, 3 = real part
0030 
0031    Double_t & operator () (int);
0032    inline Double_t & operator [] (int);
0033    // Set components by index. 0 to 2 = vector part, 3 = real part
0034 
0035    inline TQuaternion& SetRXYZ(Double_t r,Double_t x,Double_t y,Double_t z);
0036    inline TQuaternion& SetRV(Double_t r, TVector3& vect);
0037    // Sets components
0038    TQuaternion& SetAxisQAngle(TVector3& v,Double_t QAngle);
0039    // Set from vector direction and quaternion angle
0040    Double_t GetQAngle() const;
0041    TQuaternion& SetQAngle(Double_t angle);
0042    // set and get quaternion angle
0043 
0044    inline void GetRXYZ(Double_t *carray) const;
0045    inline void GetRXYZ(Float_t *carray) const;
0046    // Get the components into an array : 0 to 2 vector part, 3 real part
0047    // not checked!
0048 
0049    // ---------------  real to quaternion algebra
0050    inline TQuaternion& operator=(Double_t r);
0051    inline Bool_t operator == (Double_t r) const;
0052    inline Bool_t operator != (Double_t r) const;
0053    inline TQuaternion& operator+=(Double_t real);
0054    inline TQuaternion& operator-=(Double_t real);
0055    inline TQuaternion& operator*=(Double_t real);
0056    inline TQuaternion& operator/=(Double_t real);
0057    TQuaternion operator*(Double_t real) const;
0058    TQuaternion operator+(Double_t real) const;
0059    TQuaternion operator-(Double_t real) const;
0060    TQuaternion operator/(Double_t real) const;
0061 
0062    // ---------------- vector to quaternion algebra
0063    inline TQuaternion& operator=(const TVector3& );
0064    inline Bool_t operator == (const TVector3&) const;
0065    inline Bool_t operator != (const TVector3&) const;
0066    inline TQuaternion& operator+=(const TVector3 &vector);
0067    inline TQuaternion& operator-=(const TVector3 &vector);
0068    TQuaternion& MultiplyLeft(const TVector3 &vector);
0069    TQuaternion& operator*=(const TVector3 &vector);
0070    TQuaternion& DivideLeft(const TVector3 &vector);
0071    TQuaternion& operator/=(const TVector3 &vector);
0072    TQuaternion operator+(const TVector3 &vector) const;
0073    TQuaternion operator-(const TVector3 &vector) const;
0074    TQuaternion LeftProduct(const TVector3 &vector) const;
0075    TQuaternion operator*(const TVector3 &vector) const;
0076    TQuaternion LeftQuotient(const TVector3 &vector) const;
0077    TQuaternion operator/(const TVector3 &vector) const;
0078 
0079    // ----------------- quaternion algebra
0080    inline TQuaternion& operator=(const TQuaternion& );
0081    inline Bool_t operator == (const TQuaternion&) const;
0082    inline Bool_t operator != (const TQuaternion&) const;
0083    inline TQuaternion& operator+=(const TQuaternion &quaternion);
0084    inline TQuaternion& operator-=(const TQuaternion &quaternion);
0085    TQuaternion& MultiplyLeft(const TQuaternion &quaternion);
0086    TQuaternion& operator*=(const TQuaternion &quaternion);
0087    TQuaternion& DivideLeft(const TQuaternion &quaternion);
0088    TQuaternion& operator/=(const TQuaternion &quaternion);
0089    TQuaternion operator+(const TQuaternion &quaternion) const;
0090    TQuaternion operator-(const TQuaternion &quaternion) const;
0091    TQuaternion LeftProduct(const TQuaternion &quaternion) const;
0092    TQuaternion operator*(const TQuaternion &quaternion) const;
0093    TQuaternion LeftQuotient(const TQuaternion &quaternion) const;
0094    TQuaternion operator/(const TQuaternion &quaternion) const;
0095 
0096    // ------------------ general algebra
0097    inline Double_t Norm() const; // quaternion magnitude
0098    inline Double_t Norm2() const; // quaternion squared magnitude
0099    Double_t QMag() const { return Norm(); } // quaternion magnitude
0100    Double_t QMag2() const { return Norm2(); } // quaternion squared magnitude
0101    inline TQuaternion& Normalize();  // normalize quaternion
0102    inline TQuaternion operator - () const; // Unary minus.
0103    inline TQuaternion Conjugate() const;
0104    TQuaternion Invert() const;
0105    void Rotate(TVector3& vect) const;
0106    TVector3 Rotation(const TVector3& vect) const;
0107 
0108    void Print(Option_t* option="") const override;
0109 
0110    Double_t fRealPart;          // Real part
0111    TVector3 fVectorPart; // vector part
0112 
0113    ClassDefOverride(TQuaternion,1) // a quaternion class
0114 };
0115 
0116 
0117 // getters / setters
0118 
0119 inline TQuaternion& TQuaternion::SetRXYZ(Double_t r,Double_t x,Double_t y,Double_t z) {
0120    fRealPart = r;
0121    fVectorPart.SetXYZ(x,y,z);
0122    return (*this);
0123 }
0124 
0125 inline TQuaternion& TQuaternion::SetRV(Double_t r, TVector3& vect) {
0126    fRealPart = r;
0127    fVectorPart= vect;
0128    return (*this);
0129 }
0130 
0131 inline void TQuaternion::GetRXYZ(Double_t *carray) const {
0132    fVectorPart.GetXYZ(carray+1);
0133    carray[0] = fRealPart;
0134 }
0135 
0136 inline void TQuaternion::GetRXYZ(Float_t *carray) const {
0137    fVectorPart.GetXYZ(carray+1);
0138    carray[0] = (Float_t) fRealPart;
0139 }
0140 
0141 inline Double_t & TQuaternion::operator[] (int i)       { return operator()(i); }
0142 inline Double_t   TQuaternion::operator[] (int i) const { return operator()(i); }
0143 
0144 // ------------------ real to quaternion algebra
0145 
0146 inline Bool_t TQuaternion::operator == (Double_t r) const {
0147    return (fVectorPart.Mag2()==0 && fRealPart == r) ? kTRUE : kFALSE;
0148 }
0149 
0150 inline Bool_t TQuaternion::operator != (Double_t r) const {
0151    return (fVectorPart.Mag2()!=0 || fRealPart != r) ? kTRUE : kFALSE;
0152 }
0153 
0154 inline TQuaternion& TQuaternion::operator=(Double_t r) {
0155    fRealPart = r;
0156    fVectorPart.SetXYZ(0,0,0);
0157    return (*this);
0158 }
0159 
0160 inline TQuaternion& TQuaternion::operator+=(Double_t real) {
0161    fRealPart += real;
0162    return (*this);
0163 }
0164 
0165 inline TQuaternion& TQuaternion::operator-=(Double_t real) {
0166    fRealPart -= real;
0167    return (*this);
0168 }
0169 
0170 inline TQuaternion& TQuaternion::operator*=(Double_t real) {
0171    fRealPart *= real;
0172    fVectorPart *= real;
0173    return (*this);
0174 }
0175 
0176 inline TQuaternion& TQuaternion::operator/=(Double_t real) {
0177    if (real!=0) {
0178       fRealPart /= real;
0179       fVectorPart.SetX(fVectorPart.x()/real); // keep numericaly compliant with operator/(Double_t)
0180       fVectorPart.SetY(fVectorPart.y()/real);
0181       fVectorPart.SetZ(fVectorPart.z()/real);
0182    } else {
0183       Error("operator/=()(Double_t)", "bad value (%f) ignored",real);
0184    }
0185    return (*this);
0186 }
0187 
0188 TQuaternion operator + (Double_t r, const TQuaternion & q);
0189 TQuaternion operator - (Double_t r, const TQuaternion & q);
0190 TQuaternion operator * (Double_t r, const TQuaternion & q);
0191 TQuaternion operator / (Double_t r, const TQuaternion & q);
0192 
0193 // ------------------- vector to quaternion algebra
0194 
0195 inline Bool_t TQuaternion::operator == (const TVector3& V) const {
0196    return (fVectorPart == V && fRealPart == 0) ? kTRUE : kFALSE;
0197 }
0198 
0199 inline Bool_t TQuaternion::operator != (const TVector3& V) const {
0200    return (fVectorPart != V || fRealPart != 0) ? kTRUE : kFALSE;
0201 }
0202 
0203 inline TQuaternion& TQuaternion::operator=(const TVector3& vect) {
0204    fRealPart = 0;
0205    fVectorPart.SetXYZ(vect.X(),vect.Y(),vect.Z());
0206    return *this;
0207 }
0208 
0209 inline TQuaternion& TQuaternion::operator+=(const TVector3 &vect) {
0210    fVectorPart += vect;
0211    return (*this);
0212 }
0213 
0214 inline TQuaternion& TQuaternion::operator-=(const TVector3 &vect) {
0215    fVectorPart -= vect;
0216    return (*this);
0217 }
0218 
0219 TQuaternion operator + (const TVector3 &V, const TQuaternion &Q);
0220 TQuaternion operator - (const TVector3 &V, const TQuaternion &Q);
0221 TQuaternion operator * (const TVector3 &V, const TQuaternion &Q);
0222 TQuaternion operator / (const TVector3 &V, const TQuaternion &Q);
0223 
0224 // --------------- quaternion algebra
0225 
0226 inline Bool_t TQuaternion::operator == (const TQuaternion& Q) const {
0227    return (fVectorPart == Q.fVectorPart && fRealPart == Q.fRealPart) ? kTRUE : kFALSE;
0228 }
0229 
0230 inline Bool_t TQuaternion::operator != (const TQuaternion& Q) const {
0231    return (fVectorPart != Q.fVectorPart || fRealPart != Q.fRealPart) ? kTRUE : kFALSE;
0232 }
0233 
0234 inline TQuaternion& TQuaternion::operator=(const TQuaternion& quat) {
0235    if (&quat != this) {
0236       fRealPart = quat.fRealPart;
0237       fVectorPart.SetXYZ(quat.fVectorPart.X(),quat.fVectorPart.Y(),quat.fVectorPart.Z());
0238    }
0239    return (*this);
0240 }
0241 
0242 inline TQuaternion& TQuaternion::operator+=(const TQuaternion &quaternion) {
0243    fVectorPart += quaternion.fVectorPart;
0244    fRealPart += quaternion.fRealPart;
0245    return (*this);
0246 }
0247 
0248 inline TQuaternion& TQuaternion::operator-=(const TQuaternion &quaternion) {
0249    fVectorPart -= quaternion.fVectorPart;
0250    fRealPart   -= quaternion.fRealPart;
0251    return (*this);
0252 }
0253 
0254 inline TQuaternion TQuaternion::operator+(const TQuaternion &quaternion) const {
0255 
0256    return TQuaternion(fVectorPart+quaternion.fVectorPart, fRealPart+quaternion.fRealPart);
0257 }
0258 
0259 inline TQuaternion TQuaternion::operator-(const TQuaternion &quaternion) const {
0260 
0261    return TQuaternion(fVectorPart-quaternion.fVectorPart, fRealPart-quaternion.fRealPart);
0262 }
0263 
0264 // ---------------- general
0265 inline Double_t TQuaternion::Norm() const {
0266    return TMath::Sqrt(Norm2());
0267 }
0268 
0269 inline Double_t TQuaternion::Norm2() const {
0270    return fRealPart*fRealPart + fVectorPart.Mag2();
0271 }
0272 
0273 inline TQuaternion& TQuaternion::Normalize() {
0274 
0275    (*this) /= Norm();
0276    return (*this);
0277 }
0278 
0279 inline TQuaternion TQuaternion::operator - () const {
0280    return TQuaternion(-fVectorPart,-fRealPart);
0281 }
0282 
0283 inline TQuaternion TQuaternion::Conjugate() const {
0284    return TQuaternion(-fVectorPart,fRealPart);
0285 }
0286 
0287 #endif
0288