CrystalSpace

Public API Reference

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 
00069   inline void Set (float x, float y, float z, float w)
00070   {
00071     v.x = x;
00072     v.y = y; 
00073     v.z = z;
00074     this->w = w;
00075   }
00076   
00078   inline void SetIdentity () 
00079   {
00080     v.Set (0.0f); w = 1.0f;
00081   }
00082 
00084   inline friend csQuaternion operator+ (const csQuaternion& q1, 
00085     const csQuaternion& q2)
00086   {
00087     return csQuaternion (q1.v+q2.v, q1.w+q2.w);
00088   }
00089 
00091   inline csQuaternion& operator+= (const csQuaternion& q)
00092   {
00093     v += q.v; w += q.w;
00094     return *this;
00095   }
00096 
00098   inline friend csQuaternion operator- (const csQuaternion& q1, 
00099     const csQuaternion& q2)
00100   {
00101     return csQuaternion (q1.v-q2.v, q1.w-q2.w);
00102   }
00103 
00105   inline csQuaternion& operator-= (const csQuaternion& q)
00106   {
00107     v -= q.v; w -= q.w;
00108     return *this;
00109   }
00110   
00112   inline friend csQuaternion operator- (const csQuaternion& q)
00113   {
00114     return csQuaternion (-q.v, -q.w);
00115   }
00116 
00118   inline friend csQuaternion operator* (const csQuaternion& q1,
00119     const csQuaternion& q2)
00120   {
00121     return csQuaternion (q1.v*q2.w + q1.w*q2.v + q1.v%q2.v, 
00122       q1.w*q2.w - q1.v*q2.v);
00123   }
00124 
00126   inline csQuaternion& operator*= (const csQuaternion& q)
00127   {
00128     csVector3 newV = v*q.w + w*q.v + v%q.v;
00129     w = w*q.w - v*q.v;
00130     v = newV;
00131     return *this;
00132   }
00133 
00135   inline friend csQuaternion operator* (const csQuaternion q, float f)
00136   {
00137     return csQuaternion (q.v*f, q.w*f);
00138   }
00139 
00141   inline friend csQuaternion operator* (float f, const csQuaternion q)
00142   {
00143     return csQuaternion (q.v*f, q.w*f);
00144   }
00145 
00147   inline friend csQuaternion operator/ (const csQuaternion q, float f)
00148   {
00149     float invF = 1.0f/f;
00150     return csQuaternion (q.v*invF, q.w*invF);
00151   }
00152 
00154   inline friend csQuaternion operator/ (float f, const csQuaternion q)
00155   {
00156     float invF = 1.0f/f;
00157     return csQuaternion (q.v*invF, q.w*invF);
00158   }
00159 
00161   inline csQuaternion GetConjugate () const
00162   {
00163     return csQuaternion (-v, w);
00164   }
00165 
00167   inline void Conjugate () 
00168   {
00169     v = -v;
00170   }
00171 
00173   inline float Dot (const csQuaternion& q) const
00174   {
00175     return v*q.v + w*q.w;
00176   }
00177 
00179   inline float SquaredNorm () const
00180   {
00181     return Dot (*this);
00182   }
00183 
00185   inline float Norm () const
00186   {
00187     return csQsqrt (SquaredNorm ());
00188   }
00189 
00195   inline csQuaternion Unit () const
00196   {
00197     return (*this) / Norm ();
00198   }
00199 
00203   inline csVector3 Rotate (const csVector3& src) const
00204   {
00205     csQuaternion p (src, 0);
00206     csQuaternion q = *this * p;
00207     q *= GetConjugate ();
00208     return q.v;
00209   }
00210 
00218   inline void SetAxisAngle (const csVector3& axis, float angle)
00219   {
00220     v = axis * sinf (angle / 2.0f);
00221     w = cosf (angle / 2.0f);
00222   }
00223 
00231   inline void GetAxisAngle (csVector3& axis, float& angle) const
00232   {
00233     angle = 2.0f * acosf (w);
00234     if (v.SquaredNorm () != 0)
00235       axis = v.Unit ();
00236     else
00237       axis.Set (1.0f, 0.0f, 0.0f);
00238   }
00239 
00243   void SetEulerAngles (const csVector3& angles);
00244 
00248   csVector3 GetEulerAngles () const; 
00249 
00253   void SetMatrix (const csMatrix3& matrix);
00254 
00258   csMatrix3 GetMatrix () const;
00259 
00264   csQuaternion NLerp (const csQuaternion& q2, float t) const;
00265 
00270   csQuaternion SLerp (const csQuaternion& q2, float t) const;
00271 
00275   csQuaternion Log () const;
00276 
00280   csQuaternion Exp () const;
00281 
00287   csQuaternion Squad (const csQuaternion & t1, const csQuaternion & t2,
00288     const csQuaternion & q, float t) const;
00289 
00290   // Data
00291   csVector3 v;
00292   float w;
00293 };
00294 
00297 #endif // __CS_QUATERNION_H__
00298 

Generated for Crystal Space 1.2.1 by doxygen 1.5.3