csgeom/quaternion.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

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

- 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
    00042 class CS_CRYSTALSPACE_EXPORT csQuaternion
    00043 {
    00044 public:
    00045   // Constructors
    00046
    00048   csQuaternion ()
    00049     : v (0.0f), w (1.0f)
    00050   {}
    00051
    00053   csQuaternion (float x, float y, float z, float w)
    00054     : v (x, y, z), w (w)
    00055   {}
    00056
    00058   csQuaternion (const csVector3& v, float w)
    00059     : v (v), w (w)
    00060   {}
    00061
    00063   csQuaternion (const csQuaternion& q)
    00064     : v (q.v), w (q.w)
    00065   {}
    00066
    00070   inline void Set (float x, float y, float z, float w)
    00071   {
    00072     v.x = x;
    00073     v.y = y;
    00074     v.z = z;
    00075     this->w = w;
    00076   }
    00077
    00079   inline void SetIdentity ()
    00080   {
    00081     v.Set (0.0f); w = 1.0f;
    00082   }
    00083
    00085   inline friend csQuaternion operator+ (const csQuaternion& q1,
    00086     const csQuaternion& q2)
    00087   {
    00088     return csQuaternion (q1.v+q2.v, q1.w+q2.w);
    00089   }
    00090
    00092   inline csQuaternion& operator+= (const csQuaternion& q)
    00093   {
    00094     v += q.v; w += q.w;
    00095     return *this;
    00096   }
    00097
    00099   inline friend csQuaternion operator- (const csQuaternion& q1,
    00100     const csQuaternion& q2)
    00101   {
    00102     return csQuaternion (q1.v-q2.v, q1.w-q2.w);
    00103   }
    00104
    00106   inline csQuaternion& operator-= (const csQuaternion& q)
    00107   {
    00108     v -= q.v; w -= q.w;
    00109     return *this;
    00110   }
    00111
    00113   inline friend csQuaternion operator- (const csQuaternion& q)
    00114   {
    00115     return csQuaternion (-q.v, -q.w);
    00116   }
    00117
    00119   inline friend csQuaternion operator* (const csQuaternion& q1,
    00120     const csQuaternion& q2)
    00121   {
    00122     return csQuaternion (q1.v*q2.w + q1.w*q2.v + q1.v%q2.v,
    00123       q1.w*q2.w - q1.v*q2.v);
    00124   }
    00125
    00127   inline csQuaternion& operator*= (const csQuaternion& q)
    00128   {
    00129     csVector3 newV = v*q.w + w*q.v + v%q.v;
    00130     w = w*q.w - v*q.v;
    00131     v = newV;
    00132     return *this;
    00133   }
    00134
    00136   inline friend csQuaternion operator* (const csQuaternion& q, float f)
    00137   {
    00138     return csQuaternion (q.v*f, q.w*f);
    00139   }
    00140
    00142   inline friend csQuaternion operator* (float f, const csQuaternion& q)
    00143   {
    00144     return csQuaternion (q.v*f, q.w*f);
    00145   }
    00146
    00148   inline csQuaternion& operator*= (float f)
    00149   {
    00150     v *= f;
    00151     w *= f;
    00152
    00153     return *this;
    00154   }
    00155
    00157   inline friend csQuaternion operator/ (const csQuaternion& q, float f)
    00158   {
    00159     float invF = 1.0f/f;
    00160     return csQuaternion (q.v*invF, q.w*invF);
    00161   }
    00162
    00164   inline friend csQuaternion operator/ (float f, const csQuaternion& q)
    00165   {
    00166     float invF = 1.0f/f;
    00167     return csQuaternion (q.v*invF, q.w*invF);
    00168   }
    00169
    00171   inline csQuaternion& operator/= (float f)
    00172   {
    00173     float invF = 1.0f/f;
    00174     v *= invF;
    00175     w *= invF;
    00176
    00177     return *this;
    00178   }
    00179
    00181   inline csQuaternion GetConjugate () const
    00182   {
    00183     return csQuaternion (-v, w);
    00184   }
    00185
    00187   inline void Conjugate ()
    00188   {
    00189     v = -v;
    00190   }
    00191
    00193   inline float Dot (const csQuaternion& q) const
    00194   {
    00195     return v*q.v + w*q.w;
    00196   }
    00197
    00199   inline float SquaredNorm () const
    00200   {
    00201     return Dot (*this);
    00202   }
    00203
    00205   inline float Norm () const
    00206   {
    00207     return csQsqrt (SquaredNorm ());
    00208   }
    00209
    00215   inline csQuaternion Unit () const
    00216   {
    00217     return (*this) / Norm ();
    00218   }
    00219
    00223   inline csVector3 Rotate (const csVector3& src) const
    00224   {
    00225     csQuaternion p (src, 0);
    00226     csQuaternion q = *this * p;
    00227     q *= GetConjugate ();
    00228     return q.v;
    00229   }
    00230
    00238   inline void SetAxisAngle (const csVector3& axis, float angle)
    00239   {
    00240     v = axis * sinf (angle / 2.0f);
    00241     w = cosf (angle / 2.0f);
    00242   }
    00243
    00251   inline void GetAxisAngle (csVector3& axis, float& angle) const
    00252   {
    00253     angle = 2.0f * acosf (w);
    00254     if (v.SquaredNorm () != 0)
    00255       axis = v.Unit ();
    00256     else
    00257       axis.Set (1.0f, 0.0f, 0.0f);
    00258   }
    00259
    00263   void SetEulerAngles (const csVector3& angles);
    00264
    00268   csVector3 GetEulerAngles () const;
    00269
    00273   void SetMatrix (const csMatrix3& matrix);
    00274
    00278   csMatrix3 GetMatrix () const;
    00279
    00284   csQuaternion NLerp (const csQuaternion& q2, float t) const;
    00285
    00290   csQuaternion SLerp (const csQuaternion& q2, float t) const;
    00291
    00295   csQuaternion Log () const;
    00296
    00300   csQuaternion Exp () const;
    00301
    00307   csQuaternion Squad (const csQuaternion & t1, const csQuaternion & t2,
    00308     const csQuaternion & q, float t) const;
    00309
    00311   csVector3 v;
    00312
    00314   float w;
    00315 };
    00316
    00319 #endif // __CS_QUATERNION_H__
    00320