CrystalSpace

Public API Reference

Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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 #include "csqsqrt.h"
00030 
00031 #include "csgeom/vector3.h"
00032 
00033 class csMatrix3;
00034 
00038 class CS_CRYSTALSPACE_EXPORT csQuaternion
00039 {
00040 public:
00042   inline void Init (float theR, float theX, float theY, float theZ)
00043   { r = theR; x = theX; y = theY; z = theZ; }
00044 
00046   csQuaternion () { Init(0, 0, 0, 0 ); }
00048   csQuaternion (float theR, float theX=0.0, float theY=0.0, float theZ=0.0)
00049   { Init (theR, theX, theY, theZ ); }
00051   csQuaternion (const csQuaternion& q) { Init (q.r, q.x, q.y, q.z); }
00053   csQuaternion (const csVector3& q) { Init (0, q.x, q.y, q.z); }
00054 
00056   csQuaternion (const csMatrix3& smat);
00057 
00059   inline friend csQuaternion operator+ (const csQuaternion& q1,
00060         const csQuaternion& q2)
00061   {
00062     return csQuaternion (q1.r + q2.r, q1.x + q2.x, q1.y + q2.y, q1.z + q2.z );
00063   }
00064 
00066   inline friend csQuaternion operator- (const csQuaternion& q1,
00067         const csQuaternion& q2)
00068   {
00069     return csQuaternion (q1.r - q2.r, q1.x - q2.x, q1.y - q2.y, q1.z - q2.z );
00070   }
00071 
00073   inline friend csQuaternion operator* (const csQuaternion& q1,
00074         const csQuaternion& q2)
00075   {
00076     return csQuaternion (q1.r*q2.r -  q1.x*q2.x - q1.y*q2.y - q1.z*q2.z,
00077              q1.y*q2.z -  q1.z*q2.y + q1.r*q2.x + q1.x*q2.r,
00078              q1.z*q2.x -  q1.x*q2.z + q1.r*q2.y + q1.y*q2.r,
00079              q1.x*q2.y -  q1.y*q2.x + q1.r*q2.z + q1.z*q2.r);
00080   }
00081 
00083   csQuaternion& operator*= (const csQuaternion& q2)
00084   {
00085     Init (r*q2.r -  x*q2.x - y*q2.y - z*q2.z,
00086       y*q2.z -  z*q2.y + r*q2.x + x*q2.r,
00087       z*q2.x -  x*q2.z + r*q2.y + y*q2.r,
00088       x*q2.y -  y*q2.x + r*q2.z + z*q2.r);
00089     return *this;
00090   }
00091 
00093   void Conjugate () { Init (r, -x, -y, -z); }
00094 
00096   void Negate () { Init(-r, -x, -y, -z); }
00097 
00099   void Invert();
00100 
00106   void GetAxisAngle(csVector3& axis, float& phi) const;
00107 
00113   void SetWithAxisAngle(csVector3 axis, float phi);
00114 
00120   void PrepRotation (float angle, csVector3 vec)
00121   {
00122     double theSin = sin (angle / 2.0f);
00123     Init ((float) cos (angle / 2.0f), vec.x * theSin, vec.y * theSin,
00124         vec.z * theSin);
00125   }
00126 
00128   csVector3 Rotate (csVector3 vec)
00129   {
00130     csQuaternion p (vec);
00131     csQuaternion qConj (r, -x, -y, -z);
00132 
00133     p = *this * p;
00134     p *= qConj;
00135     return csVector3 (p.x, p.y, p.z);
00136   }
00137 
00139   void Normalize ()
00140   {
00141     float dist, square;
00142     square = x * x + y * y + z * z + r * r;
00143 
00144     if (square > 0.0) dist = (float)csQisqrt(square);
00145     else dist = 1;
00146 
00147     x *= dist;
00148     y *= dist;
00149     z *= dist;
00150     r *= dist;
00151 
00152     /*if(x*x + y*y + z*z > .999)
00153     {
00154       // Severe problems...
00155       float inverselen = 1.0f / (x*x + y*y + z*z);
00156       x *= inverselen;
00157       y *= inverselen;
00158       z *= inverselen;
00159       if(r > 0) r = -1 + r;
00160       else r = 1 + r;
00161     }
00162     else
00163     {
00164       r = csQsqrt(1.0f - x*x - y*y - z*z);
00165       }*/
00166   }
00167 
00173   void SetWithEuler (const csVector3 &rot);
00174 
00179   void GetEulerAngles (csVector3& angles);
00180 
00184   csQuaternion ToAxisAngle () const;
00185 
00191   csQuaternion Slerp (const csQuaternion &quat2, float slerp) const;
00192 
00193   //csQuaternion Lerp(const csQuaternion& quat2, float ratio) const;
00194 
00195   float r,x,y,z;
00196 };
00197 
00200 #endif // __CS_QUATERNION_H__
00201 

Generated for Crystal Space by doxygen 1.4.4