CrystalSpace

Public API Reference

Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

csgeom/quaterni.h

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2000 by Norman Kramer
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public
00015     License along with this library; if not, write to the Free
00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 #ifndef __CS_QUATERNION_H__
00020 #define __CS_QUATERNION_H__
00021 
00028 #include "csextern.h"
00029 
00030 #include "csgeom/math3d.h"
00031 #include "csgeom/matrix3.h"
00032 #include "qsqrt.h"
00033 
00037 class CS_CSGEOM_EXPORT csQuaternion
00038 {
00039 public:
00041   inline void Init (float theR, float theX, float theY, float theZ)
00042   { r = theR; x = theX; y = theY; z = theZ; }
00043 
00045   csQuaternion () { Init(0, 0, 0, 0 ); }
00047   csQuaternion (float theR, float theX=0.0, float theY=0.0, float theZ=0.0)
00048   { Init (theR, theX, theY, theZ ); }
00050   csQuaternion (const csQuaternion& q) { Init (q.r, q.x, q.y, q.z); }
00052   csQuaternion (const csVector3& q) { Init (0, q.x, q.y, q.z); }
00053 
00055   csQuaternion (const csMatrix3& smat);
00056 
00058   inline friend csQuaternion operator+ (const csQuaternion& q1,
00059         const csQuaternion& q2)
00060   {
00061     return csQuaternion (q1.r + q2.r, q1.x + q2.x, q1.y + q2.y, q1.z + q2.z );
00062   }
00063 
00065   inline friend csQuaternion operator- (const csQuaternion& q1,
00066         const csQuaternion& q2)
00067   {
00068     return csQuaternion (q1.r - q2.r, q1.x - q2.x, q1.y - q2.y, q1.z - q2.z );
00069   }
00070 
00072   inline friend csQuaternion operator* (const csQuaternion& q1,
00073         const csQuaternion& q2)
00074   {
00075     return csQuaternion (q1.r*q2.r -  q1.x*q2.x - q1.y*q2.y - q1.z*q2.z,
00076              q1.y*q2.z -  q1.z*q2.y + q1.r*q2.x + q1.x*q2.r,
00077              q1.z*q2.x -  q1.x*q2.z + q1.r*q2.y + q1.y*q2.r,
00078              q1.x*q2.y -  q1.y*q2.x + q1.r*q2.z + q1.z*q2.r);
00079   }
00080 
00082   csQuaternion& operator*= (const csQuaternion& q2)
00083   {
00084     Init (r*q2.r -  x*q2.x - y*q2.y - z*q2.z,
00085       y*q2.z -  z*q2.y + r*q2.x + x*q2.r,
00086       z*q2.x -  x*q2.z + r*q2.y + y*q2.r,
00087       x*q2.y -  y*q2.x + r*q2.z + z*q2.r);
00088     return *this;
00089   }
00090 
00092   void Conjugate () { Init (r, -x, -y, -z); }
00093 
00095   void Negate () { Init(-r, -x, -y, -z); }
00096 
00098   void Invert();
00099 
00105   void GetAxisAngle(csVector3& axis, float& phi) const;
00106 
00112   void SetWithAxisAngle(csVector3 axis, float phi);
00113 
00119   void PrepRotation (float angle, csVector3 vec)
00120   {
00121     double theSin = sin (angle / 2.0f);
00122     Init ((float) cos (angle / 2.0f), vec.x * theSin, vec.y * theSin,
00123         vec.z * theSin);
00124   }
00125 
00127   csVector3 Rotate (csVector3 vec)
00128   {
00129     csQuaternion p (vec);
00130     csQuaternion qConj (r, -x, -y, -z);
00131 
00132     p = *this * p;
00133     p *= qConj;
00134     return csVector3 (p.x, p.y, p.z);
00135   }
00136 
00138   void Normalize ()
00139   {
00140     float dist, square;
00141     square = x * x + y * y + z * z + r * r;
00142 
00143     if (square > 0.0) dist = (float)qisqrt(square);
00144     else dist = 1;
00145 
00146     x *= dist;
00147     y *= dist;
00148     z *= dist;
00149     r *= dist;
00150 
00151     /*if(x*x + y*y + z*z > .999)
00152     {
00153       // Severe problems...
00154       float inverselen = 1.0f / (x*x + y*y + z*z);
00155       x *= inverselen;
00156       y *= inverselen;
00157       z *= inverselen;
00158       if(r > 0) r = -1 + r;
00159       else r = 1 + r;
00160     }
00161     else
00162     {
00163       r = qsqrt(1.0f - x*x - y*y - z*z);
00164       }*/
00165   }
00166 
00172   void SetWithEuler (const csVector3 &rot);
00173 
00178   void GetEulerAngles (csVector3& angles);
00179 
00183   csQuaternion ToAxisAngle () const;
00184 
00190   csQuaternion Slerp (const csQuaternion &quat2, float slerp) const;
00191 
00192   //csQuaternion Lerp(const csQuaternion& quat2, float ratio) const;
00193 
00194   float r,x,y,z;
00195 };
00196 
00199 #endif // __CS_QUATERNION_H__
00200 

Generated for Crystal Space by doxygen 1.2.18