Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6616 in orxonox.OLD for trunk/src/lib/math/vector.h


Ignore:
Timestamp:
Jan 19, 2006, 12:27:45 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: taken the quaternion outside of Vector.cc to quaternion.cc/h

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/math/vector.h

    r5982 r6616  
    125125
    126126
    127 //! Quaternion
    128 /**
    129    Class to handle 3-dimensional rotation efficiently
    130 */
    131 class Quaternion
    132 {
    133  public:
    134   /** creates a Default quaternion (multiplicational identity Quaternion)*/
    135   inline Quaternion () { w = 1; v = Vector(0,0,0); }
    136   /** creates a Quaternion looking into the direction v @param v: the direction @param f: the value */
    137   inline Quaternion (const Vector& v, float f) { this->w = f; this->v = v; }
    138   Quaternion (float m[4][4]);
    139   /** turns a rotation along an axis into a Quaternion @param angle: the amount of radians to rotate @param axis: the axis to rotate around */
    140   inline Quaternion (float angle, const Vector& axis) { w = cos(angle/2); v = axis * sin(angle/2); }
    141   Quaternion (const Vector& dir, const Vector& up);
    142   Quaternion (float roll, float pitch, float yaw);
    143 
    144   /** @param q: the Quaternion to compare with this one. @returns true if the Quaternions are the same, false otherwise */
    145   inline bool operator== (const Quaternion& q) const { return (unlikely(this->v==q.v&&this->w==q.w))?true:false; };
    146   /** @param f: a real value @return a Quaternion containing the quotient */
    147   inline Quaternion operator/ (const float& f) const { return (unlikely(f==0.0)) ? Quaternion() : Quaternion(this->v/f, this->w/f); };
    148   /** @param f: the value to divide by @returns the quaternion devided by f (this /= f) */
    149   inline const Quaternion& operator/= (const float& f) {*this = *this / f; return *this;}
    150   /** @param f: a real value @return a Quaternion containing the product */
    151   inline Quaternion operator* (const float& f) const { return Quaternion(this->v*f, this->w*f); };
    152   /** @param f: the value to multiply by @returns the quaternion multiplied by f (this *= f) */
    153   inline const Quaternion& operator*= (const float& f) {*this = *this * f; return *this;}
    154   /** @param q: another Quaternion to rotate this by @return a quaternion that represents the first one rotated by the second one (WARUNING: this operation is not commutative! e.g. (A*B) != (B*A)) */
    155   Quaternion operator* (const Quaternion& q) const { return Quaternion(Vector(this->w*q.v.x + this->v.x*q.w + this->v.y*q.v.z - this->v.z*q.v.y,
    156                                                                          this->w*q.v.y + this->v.y*q.w + this->v.z*q.v.x - this->v.x*q.v.z,
    157                                                                          this->w*q.v.z + this->v.z*q.w + this->v.x*q.v.y - this->v.y*q.v.x),
    158                                                                          this->w*q.w - this->v.x*q.v.x - this->v.y*q.v.y - this->v.z*q.v.z); };
    159   /** @param q: the Quaternion to multiply by @returns the quaternion multiplied by q (this *= q) */
    160   inline const Quaternion& operator*= (const Quaternion& q) {*this = *this * q; return *this; };
    161   /** @param q the Quaternion by which to devide @returns the division from this by q (this / q) */
    162   inline Quaternion operator/ (const Quaternion& q) const { return *this * q.inverse(); };
    163   /** @param q the Quaternion by which to devide @returns the division from this by q (this /= q) */
    164   inline const Quaternion& operator/= (const Quaternion& q) { *this = *this * q.inverse(); return *this; };
    165   /** @param q the Quaternion to add to this @returns the quaternion added with q (this + q) */
    166   inline Quaternion operator+ (const Quaternion& q) const { return Quaternion(q.v + v, q.w + w); };
    167   /** @param q the Quaternion to add to this @returns the quaternion added with q (this += q) */
    168   inline const Quaternion& operator+= (const Quaternion& q) { this->v += q.v; this->w += q.w; return *this; };
    169   /** @param q the Quaternion to substrace from this @returns the quaternion substracted by q (this - q) */
    170   inline Quaternion operator- (const Quaternion& q) const { return Quaternion(q.v - v, q.w - w); }
    171   /** @param q the Quaternion to substrace from this @returns the quaternion substracted by q (this -= q) */
    172   inline const Quaternion& operator-= (const Quaternion& q) { this->v -= q.v; this->w -= q.w; return *this; };
    173   /** copy constructor @param q: the Quaternion to set this to. @returns the Quaternion q (or this) */
    174   inline Quaternion operator= (const Quaternion& q) {this->v = q.v; this->w = q.w; return *this;}
    175   /** conjugates this Quaternion @returns the conjugate */
    176   inline Quaternion conjugate () const { return Quaternion(Vector(-v.x, -v.y, -v.z), this->w); };
    177   /** @returns the norm of The Quaternion */
    178   inline float norm () const { return sqrt(w*w + v.x*v.x + v.y*v.y + v.z*v.z); };
    179   /** @returns the inverted Quaterntion of this */
    180   inline Quaternion inverse () const { return conjugate() / (w*w + v.x*v.x + v.y*v.y + v.z*v.z); };
    181   /** @returns the dot Product of a Quaternion */
    182   inline float dot (const Quaternion& q) const { return v.x*q.v.x + v.y*q.v.y + v.z*q.v.z + w*q.w; };
    183   /** @retuns the Distance between two Quaternions */
    184   inline float distance(const Quaternion& q) const { return 2*acos(fabsf(this->dot(q))); };
    185   /** @param v: the Vector  @return a new Vector representing v rotated by the Quaternion */
    186   inline Vector apply (const Vector& v) const { return (*this * Quaternion(v, 0) * conjugate()).v; };
    187   void matrix (float m[4][4]) const;
    188   /** @returns the normalized Quaternion (|this|) */
    189   inline Quaternion getNormalized() const { float n = this->norm(); return Quaternion(this->v/n, this->w/n); };
    190   /** normalizes the current Quaternion */
    191   inline void normalize() { float n = this->norm(); this->v /= n; this->w/=n; };
    192 
    193   /** @returns the rotational axis of this Quaternion */
    194   inline Vector getSpacialAxis() const { return this->v / sin(acos(w));/*sqrt(v.x*v.x + v.y*v.y + v.z+v.z);*/ };
    195   /** @returns the rotational angle of this Quaternion around getSpacialAxis()  !! IN DEGREE !! */
    196   inline float getSpacialAxisAngle() const { return 360.0 / M_PI * acos( this->w ); };
    197 
    198   static Quaternion quatSlerp(const Quaternion& from, const Quaternion& to, float t);
    199 
    200   void debug();
    201   void debug2();
    202 
    203 
    204  public:
    205   Vector    v;        //!< Imaginary Vector
    206   float     w;        //!< Real part of the number
    207 
    208 };
    209 
    210 
    211 
    212 
    213127//! 3D rotation (OBSOLETE)
    214128/**
Note: See TracChangeset for help on using the changeset viewer.