## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# csgeom/quaternion.h

Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2000 by Norman Kramer
    00003                   2006 by Marten Svanfeldt
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public
    00016     License along with this library; if not, write to the Free
    00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00018 */
    00019
    00020 #ifndef __CS_QUATERNION_H__
    00021 #define __CS_QUATERNION_H__
    00022
    00030 #include "csextern.h"
    00031 #include "csqsqrt.h"
    00032
    00033 #include "csgeom/vector3.h"
    00034
    00035 class csMatrix3;
    00036
    00041 class CS_CRYSTALSPACE_EXPORT csQuaternion
    00042 {
    00043 public:
    00044   // Constructors
    00045
    00047   csQuaternion ()
    00048     : v (0.0f), w (1.0f)
    00049   {}
    00050
    00052   csQuaternion (float x, float y, float z, float w)
    00053     : v (x, y, z), w (w)
    00054   {}
    00055
    00057   csQuaternion (const csVector3& v, float w)
    00058     : v (v), w (w)
    00059   {}
    00060
    00062   csQuaternion (const csQuaternion& q)
    00063     : v (q.v), w (q.w)
    00064   {}
    00065
    00067   inline void SetIdentity ()
    00068   {
    00069     v.Set (0.0f); w = 1.0f;
    00070   }
    00071
    00073   inline friend csQuaternion operator+ (const csQuaternion& q1,
    00074     const csQuaternion& q2)
    00075   {
    00076     return csQuaternion (q1.v+q2.v, q1.w+q2.w);
    00077   }
    00078
    00080   inline csQuaternion& operator+= (const csQuaternion& q)
    00081   {
    00082     v += q.v; w += q.w;
    00083     return *this;
    00084   }
    00085
    00087   inline friend csQuaternion operator- (const csQuaternion& q1,
    00088     const csQuaternion& q2)
    00089   {
    00090     return csQuaternion (q1.v-q2.v, q1.w-q2.w);
    00091   }
    00092
    00094   inline csQuaternion& operator-= (const csQuaternion& q)
    00095   {
    00096     v -= q.v; w -= q.w;
    00097     return *this;
    00098   }
    00099
    00101   inline friend csQuaternion operator- (const csQuaternion& q)
    00102   {
    00103     return csQuaternion (-q.v, -q.w);
    00104   }
    00105
    00107   inline friend csQuaternion operator* (const csQuaternion& q1,
    00108     const csQuaternion& q2)
    00109   {
    00110     return csQuaternion (q1.v*q2.w + q1.w*q2.v + q1.v%q2.v,
    00111       q1.w*q2.w - q1.v*q2.v);
    00112   }
    00113
    00115   inline csQuaternion& operator*= (const csQuaternion& q)
    00116   {
    00117     csVector3 newV = v*q.w + w*q.v + v%q.v;
    00118     w = w*q.w - v*q.v;
    00119     v = newV;
    00120     return *this;
    00121   }
    00122
    00124   inline friend csQuaternion operator* (const csQuaternion q, float f)
    00125   {
    00126     return csQuaternion (q.v*f, q.w*f);
    00127   }
    00128
    00130   inline friend csQuaternion operator* (float f, const csQuaternion q)
    00131   {
    00132     return csQuaternion (q.v*f, q.w*f);
    00133   }
    00134
    00136   inline friend csQuaternion operator/ (const csQuaternion q, float f)
    00137   {
    00138     float invF = 1.0f/f;
    00139     return csQuaternion (q.v*invF, q.w*invF);
    00140   }
    00141
    00143   inline friend csQuaternion operator/ (float f, const csQuaternion q)
    00144   {
    00145     float invF = 1.0f/f;
    00146     return csQuaternion (q.v*invF, q.w*invF);
    00147   }
    00148
    00150   inline csQuaternion GetConjugate () const
    00151   {
    00152     return csQuaternion (-v, w);
    00153   }
    00154
    00156   inline void Conjugate ()
    00157   {
    00158     v = -v;
    00159   }
    00160
    00162   inline float Dot (const csQuaternion& q) const
    00163   {
    00164     return v*q.v + w*q.w;
    00165   }
    00166
    00168   inline float SquaredNorm () const
    00169   {
    00170     return Dot (*this);
    00171   }
    00172
    00174   inline float Norm () const
    00175   {
    00176     return csQsqrt (SquaredNorm ());
    00177   }
    00178
    00184   inline csQuaternion Unit () const
    00185   {
    00186     return (*this) / Norm ();
    00187   }
    00188
    00192   inline csVector3 Rotate (const csVector3& src) const
    00193   {
    00194     csQuaternion p (src, 0);
    00195     csQuaternion q = *this * p;
    00196     q *= GetConjugate ();
    00197     return q.v;
    00198   }
    00199
    00207   inline void SetAxisAngle (const csVector3& axis, float angle)
    00208   {
    00209     v = axis * sinf (angle / 2.0f);
    00210     w = cosf (angle / 2.0f);
    00211   }
    00212
    00220   inline void GetAxisAngle (csVector3& axis, float& angle) const
    00221   {
    00222     angle = 2.0f * acosf (w);
    00223     if (v.SquaredNorm () != 0)
    00224       axis = v.Unit ();
    00225     else
    00226       axis.Set (1.0f, 0.0f, 0.0f);
    00227   }
    00228
    00232   void SetEulerAngles (const csVector3& angles);
    00233
    00237   csVector3 GetEulerAngles () const;
    00238
    00242   void SetMatrix (const csMatrix3& matrix);
    00243
    00247   csMatrix3 GetMatrix () const;
    00248
    00253   csQuaternion NLerp (const csQuaternion& q2, float t) const;
    00254
    00259   csQuaternion SLerp (const csQuaternion& q2, float t) const;
    00260
    00264   csQuaternion Log () const;
    00265
    00269   csQuaternion Exp () const;
    00270
    00276   csQuaternion Squad (const csQuaternion & t1, const csQuaternion & t2,
    00277     const csQuaternion & q, float t) const;
    00278
    00279   // Data
    00280   csVector3 v;
    00281   float w;
    00282 };
    00283
    00286 #endif // __CS_QUATERNION_H__
    00287