Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9656 in orxonox.OLD for trunk/src/lib/math


Ignore:
Timestamp:
Aug 4, 2006, 11:01:28 PM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the proxy bache back with no conflicts

Location:
trunk/src/lib/math
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/math/quaternion.cc

    r9110 r9656  
    336336  // check the diagonal
    337337  float tr = mat[0][0] + mat[1][1] + mat[2][2];
    338   if( tr > 0.0f) {
     338  if( tr > 0.0f)
     339  {
    339340    float s = (float)sqrtf(tr + 1.0f);
    340341    this->w = s * 0.5f;
     
    372373}
    373374
     375Quaternion Quaternion::lookAt(Vector from, Vector to, Vector up)
     376{
     377  Vector n = to - from;
     378  n.normalize();
     379  Vector v = n.cross(up);
     380  v.normalize();
     381  Vector u = v.cross(n);
     382
     383  float matrix[3][3];
     384  matrix[0][0] = v.x;
     385  matrix[0][1] = v.y;
     386  matrix[0][2] = v.z;
     387  matrix[1][0] = u.x;
     388  matrix[1][1] = u.y;
     389  matrix[1][2] = u.z;
     390  matrix[2][0] = -n.x;
     391  matrix[2][1] = -n.y;
     392  matrix[2][2] = -n.z;
     393
     394  Quaternion quat;
     395  quat.from3x3(matrix);
     396  return quat;
     397}
     398
     399
    374400/**
    375401 * @brief outputs some nice formated debug information about this quaternion
  • trunk/src/lib/math/quaternion.h

    r9110 r9656  
    3333class Quaternion
    3434{
    35  public:
    36   /** creates a Default quaternion (multiplicational identity Quaternion)*/
    37   inline Quaternion () { w = 1; v = Vector(0,0,0); }
    38   /** Copy constructor @param q the Quaternion to copy. */
    39   inline Quaternion (const Quaternion& q) { w = q.w; v = q.v; };
    40   /** creates a Quaternion looking into the direction v @param v: the direction @param f: the value */
    41   inline Quaternion (const Vector& v, float f) { this->w = f; this->v = v; }
    42   /** turns a rotation along an axis into a Quaternion @param angle: the amount of radians to rotate @param axis: the axis to rotate around */
    43   inline Quaternion (float angle, const Vector& axis) { w = cos(angle/2.0); v = axis * sin(angle/2.0); }
    44   Quaternion (const Vector& dir, const Vector& up);
    45   Quaternion (float roll, float pitch, float yaw);
     35  public:
     36    /** creates a Default quaternion (multiplicational identity Quaternion)*/
     37    inline Quaternion () { w = 1; v = Vector(0,0,0); }
     38    /** Copy constructor @param q the Quaternion to copy. */
     39    inline Quaternion (const Quaternion& q) { w = q.w; v = q.v; };
     40    /** creates a Quaternion looking into the direction v @param v: the direction @param f: the value */
     41    inline Quaternion (const Vector& v, float f) { this->w = f; this->v = v; }
     42    /** turns a rotation along an axis into a Quaternion @param angle: the amount of radians to rotate @param axis: the axis to rotate around */
     43    inline Quaternion (float angle, const Vector& axis) { w = cos(angle/2.0); v = axis * sin(angle/2.0); }
     44    Quaternion (const Vector& dir, const Vector& up);
     45    Quaternion (float roll, float pitch, float yaw);
    4646
    47   void from3x3(float m[3][3]);
    48   void from4x4(float m[4][4]);
     47    void from3x3(float m[3][3]);
     48    void from4x4(float m[4][4]);
    4949
    5050
    51   /** @param q: the Quaternion to compare with this one. @returns true if the Quaternions are the same, false otherwise */
     51    /** @param q: the Quaternion to compare with this one. @returns true if the Quaternions are the same, false otherwise */
    5252  inline bool operator== (const Quaternion& q) const { return (unlikely(this->v==q.v&&this->w==q.w))?true:false; };
    53   /** @param q: the Quaternion to compare with this one. @returns true if the Quaternions are the same, false otherwise */
     53    /** @param q: the Quaternion to compare with this one. @returns true if the Quaternions are the same, false otherwise */
    5454  inline bool operator!= (const Quaternion& q) const { return (unlikely(this->v!=q.v||this->w!=q.w))?true:false; };
    55   /** @param f: a real value @return a Quaternion containing the quotient */
    56   inline Quaternion operator/ (const float& f) const { return (unlikely(f==0.0)) ? Quaternion() : Quaternion(this->v/f, this->w/f); };
    57   /** @param f: the value to divide by @returns the quaternion devided by f (this /= f) */
    58   inline const Quaternion& operator/= (const float& f) {*this = *this / f; return *this;}
    59   /** @param f: a real value @return a Quaternion containing the product */
    60   inline Quaternion operator* (const float& f) const { return Quaternion(this->v*f, this->w*f); };
    61   /** @param f: the value to multiply by @returns the quaternion multiplied by f (this *= f) */
    62   inline const Quaternion& operator*= (const float& f) {*this = *this * f; return *this;}
    63   /** @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)) */
    64   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,
    65                                                                          this->w*q.v.y + this->v.y*q.w + this->v.z*q.v.x - this->v.x*q.v.z,
    66                                                                          this->w*q.v.z + this->v.z*q.w + this->v.x*q.v.y - this->v.y*q.v.x),
    67                                                                          this->w*q.w - this->v.x*q.v.x - this->v.y*q.v.y - this->v.z*q.v.z); };
    68   /** @param q: the Quaternion to multiply by @returns the quaternion multiplied by q (this *= q) */
    69   inline const Quaternion& operator*= (const Quaternion& q) {*this = *this * q; return *this; };
    70   /** @param q the Quaternion by which to devide @returns the division from this by q (this / q) */
    71   inline Quaternion operator/ (const Quaternion& q) const { return *this * q.inverse(); };
    72   /** @param q the Quaternion by which to devide @returns the division from this by q (this /= q) */
    73   inline const Quaternion& operator/= (const Quaternion& q) { *this = *this * q.inverse(); return *this; };
    74   /** @param q the Quaternion to add to this @returns the quaternion added with q (this + q) */
    75   inline Quaternion operator+ (const Quaternion& q) const { return Quaternion(q.v + v, q.w + w); };
    76   /** @param q the Quaternion to add to this @returns the quaternion added with q (this += q) */
    77   inline const Quaternion& operator+= (const Quaternion& q) { this->v += q.v; this->w += q.w; return *this; };
    78   /** @param q the Quaternion to substrace from this @returns the quaternion substracted by q (this - q) */
    79   inline Quaternion operator- (const Quaternion& q) const { return Quaternion(q.v - v, q.w - w); }
    80   /** @param q the Quaternion to substrace from this @returns the quaternion substracted by q (this -= q) */
    81   inline const Quaternion& operator-= (const Quaternion& q) { this->v -= q.v; this->w -= q.w; return *this; };
    82   /** copy constructor @param q: the Quaternion to set this to. @returns the Quaternion q (or this) */
    83   inline Quaternion operator= (const Quaternion& q) {this->v = q.v; this->w = q.w; return *this;}
    84   /** conjugates this Quaternion @returns the conjugate */
    85   inline Quaternion conjugate () const { return Quaternion(Vector(-v.x, -v.y, -v.z), this->w); };
    86   /** @returns the norm of The Quaternion */
    87   inline float norm () const { return sqrt(w*w + v.x*v.x + v.y*v.y + v.z*v.z); };
    88   /** @returns the inverted Quaterntion of this */
    89   inline Quaternion inverse () const { return conjugate() / (w*w + v.x*v.x + v.y*v.y + v.z*v.z); };
    90   /** @returns the dot Product of a Quaternion */
    91   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; };
    92   /** @retuns the Distance between two Quaternions */
    93   inline float distance(const Quaternion& q) const { return 2*acos(fabsf(this->dot(q))); };
    94   /** @param v: the Vector  @return a new Vector representing v rotated by the Quaternion */
    95   inline Vector apply (const Vector& v) const { return (*this * Quaternion(v, 0) * conjugate()).v; };
    96   void matrix (float m[4][4]) const;
    97   /** @returns the normalized Quaternion (|this|) */
    98   inline Quaternion getNormalized() const { float n = this->norm(); return Quaternion(this->v/n, this->w/n); };
    99   /** normalizes the current Quaternion */
    100   inline void normalize() { float n = this->norm(); this->v /= n; this->w/=n; };
     55    /** @param f: a real value @return a Quaternion containing the quotient */
     56    inline Quaternion operator/ (const float& f) const { return (unlikely(f==0.0)) ? Quaternion() : Quaternion(this->v/f, this->w/f); };
     57    /** @param f: the value to divide by @returns the quaternion devided by f (this /= f) */
     58    inline const Quaternion& operator/= (const float& f) {*this = *this / f; return *this;}
     59    /** @param f: a real value @return a Quaternion containing the product */
     60    inline Quaternion operator* (const float& f) const { return Quaternion(this->v*f, this->w*f); };
     61    /** @param f: the value to multiply by @returns the quaternion multiplied by f (this *= f) */
     62    inline const Quaternion& operator*= (const float& f) {*this = *this * f; return *this;}
     63    /** @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)) */
     64    Quaternion operator* (const Quaternion& q) const
     65    {
     66      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,
     67                               this->w*q.v.y + this->v.y*q.w + this->v.z*q.v.x - this->v.x*q.v.z,
     68                               this->w*q.v.z + this->v.z*q.w + this->v.x*q.v.y - this->v.y*q.v.x),
     69                        this->w*q.w - this->v.x*q.v.x - this->v.y*q.v.y - this->v.z*q.v.z);
     70    };
     71    /** @param q: the Quaternion to multiply by @returns the quaternion multiplied by q (this *= q) */
     72    inline const Quaternion& operator*= (const Quaternion& q) {*this = *this * q; return *this; };
     73    /** @param q the Quaternion by which to devide @returns the division from this by q (this / q) */
     74    inline Quaternion operator/ (const Quaternion& q) const { return *this * q.inverse(); };
     75    /** @param q the Quaternion by which to devide @returns the division from this by q (this /= q) */
     76    inline const Quaternion& operator/= (const Quaternion& q) { *this = *this * q.inverse(); return *this; };
     77    /** @param q the Quaternion to add to this @returns the quaternion added with q (this + q) */
     78    inline Quaternion operator+ (const Quaternion& q) const { return Quaternion(q.v + v, q.w + w); };
     79    /** @param q the Quaternion to add to this @returns the quaternion added with q (this += q) */
     80    inline const Quaternion& operator+= (const Quaternion& q) { this->v += q.v; this->w += q.w; return *this; };
     81    /** @param q the Quaternion to substrace from this @returns the quaternion substracted by q (this - q) */
     82    inline Quaternion operator- (const Quaternion& q) const { return Quaternion(q.v - v, q.w - w); }
     83    /** @param q the Quaternion to substrace from this @returns the quaternion substracted by q (this -= q) */
     84    inline const Quaternion& operator-= (const Quaternion& q) { this->v -= q.v; this->w -= q.w; return *this; };
     85    /** copy constructor @param q: the Quaternion to set this to. @returns the Quaternion q (or this) */
     86    inline Quaternion operator= (const Quaternion& q) {this->v = q.v; this->w = q.w; return *this;}
     87    /** conjugates this Quaternion @returns the conjugate */
     88    inline Quaternion conjugate () const { return Quaternion(Vector(-v.x, -v.y, -v.z), this->w); };
     89    /** @returns the norm of The Quaternion */
     90    inline float norm () const { return sqrt(w*w + v.x*v.x + v.y*v.y + v.z*v.z); };
     91    /** @returns the inverted Quaterntion of this */
     92    inline Quaternion inverse () const { return conjugate() / (w*w + v.x*v.x + v.y*v.y + v.z*v.z); };
     93    /** @returns the dot Product of a Quaternion */
     94    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; };
     95    /** @retuns the Distance between two Quaternions */
     96    inline float distance(const Quaternion& q) const { return 2*acos(fabsf(this->dot(q))); };
     97    /** @param v: the Vector  @return a new Vector representing v rotated by the Quaternion */
     98    inline Vector apply (const Vector& v) const { return (*this * Quaternion(v, 0) * conjugate()).v; };
     99    void matrix (float m[4][4]) const;
     100    /** @returns the normalized Quaternion (|this|) */
     101    inline Quaternion getNormalized() const { float n = this->norm(); return Quaternion(this->v/n, this->w/n); };
     102    /** normalizes the current Quaternion */
     103    inline void normalize() { float n = this->norm(); this->v /= n; this->w/=n; };
    101104
    102   float getHeading() const;
    103   Quaternion getHeadingQuat() const;
    104   float getAttitude() const;
    105   Quaternion getAttitudeQuat() const;
    106   float getBank() const;
    107   Quaternion getBankQuat() const;
    108   /** @returns the rotational axis of this Quaternion */
    109   inline Vector getSpacialAxis() const { return this->v / sin(acos(w));/*sqrt(v.x*v.x + v.y*v.y + v.z+v.z);*/ };
    110   /** @returns the rotational angle of this Quaternion around getSpacialAxis()  !! IN DEGREE !! */
    111   inline float getSpacialAxisAngle() const { return 360.0 / M_PI * acos( this->w ); };
     105    float getHeading() const;
     106    Quaternion getHeadingQuat() const;
     107    float getAttitude() const;
     108    Quaternion getAttitudeQuat() const;
     109    float getBank() const;
     110    Quaternion getBankQuat() const;
     111    /** @returns the rotational axis of this Quaternion */
     112    inline Vector getSpacialAxis() const { return this->v / sin(acos(w));/*sqrt(v.x*v.x + v.y*v.y + v.z+v.z);*/ };
     113    /** @returns the rotational angle of this Quaternion around getSpacialAxis()  !! IN DEGREE !! */
     114    inline float getSpacialAxisAngle() const { return 360.0 / M_PI * acos( this->w ); };
    112115
    113116
    114   inline void slerpTo(const Quaternion& toQuat, float t);
    115   static Quaternion quatSlerp(const Quaternion& from, const Quaternion& to, float t);
    116 
    117   void debug() const;
    118   void debug2() const;
     117    inline void slerpTo(const Quaternion& toQuat, float t);
     118    static Quaternion quatSlerp(const Quaternion& from, const Quaternion& to, float t);
    119119
    120120
    121  public:
    122   Vector    v;        //!< Imaginary Vector
    123   float     w;        //!< Real part of the number
     121    static Quaternion lookAt(Vector from, Vector to, Vector up);
     122
     123    void debug() const;
     124    void debug2() const;
     125
     126
     127  public:
     128    Vector    v;        //!< Imaginary Vector
     129    float     w;        //!< Real part of the number
    124130};
    125131
Note: See TracChangeset for help on using the changeset viewer.