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

quat.hpp

00001 /**************************************************************************
00002   
00003   quat.hpp
00004 
00005   A quaternion template class
00006 
00007   ---------------------------------------------------------------------
00008 
00009   Feb 1998, Paul Rademacher (rademach@cs.unc.edu)  
00010 
00011   Modification History:
00012   - Oct 2000, Bill Baxter.  Adapted to GLVU coding conventions and
00013       templetized.  Also implemented many methods that were declared but
00014       not defined.  Added some methods from quatlib and other sources.
00015                 
00016 **************************************************************************/
00017 
00018 #ifndef _QUAT_H_
00019 #define _QUAT_H_
00020 
00021 #include "vec3f.hpp"
00022 #include "mat44.hpp"
00023 #include "mat33.hpp"
00024 
00025 /****************************************************************
00026 *                    Quaternion                                 *
00027 ****************************************************************/
00028 
00029 template <class Type>
00030 class Quat
00031 {
00032 public:
00033   typedef Vec3<Type> qvec;
00034 
00035   Type s;  /* scalar component */
00036   qvec v;  /* quat vector component */
00037 
00038   /* Constructors */
00039 
00040   Quat(void);
00041   Quat(Type s, Type x, Type y, Type z);
00042   Quat(Type x, Type y, Type z); // s=0
00043   Quat(Type s, const qvec& v);
00044   Quat(const qvec& v, Type s = 0.0); 
00045   Quat(const Type *d);        /* copy from four-element Type array s,x,y,z */
00046   Quat(const Quat &q);        /* copy from other Quat */
00047 
00048   /* Setters */
00049 
00050   void  Set( Type x, Type y, Type z );
00051   void  Set( Type s, Type x, Type y, Type z );
00052   void  Set( Type s, const qvec& v );
00053   void  Set( const qvec& v, Type s=0 );
00054 
00055   /* Operators */
00056 
00057   Quat &operator  = ( const Quat &v );      /* assignment of a Quat */
00058   Quat &operator += ( const Quat &v );      /* incrementation by a Quat */
00059   Quat &operator -= ( const Quat &v );      /* decrementation by a Quat */
00060   Quat &operator *= ( const Type d );       /* multiplication by a scalar */
00061   Quat &operator *= ( const Quat &v );      /* quat product (this*v) */
00062   Quat &operator /= ( const Type d );       /* division by a scalar */
00063   Type &operator [] ( int i);               /* indexing s=0,x=1,y=2,z=3 */
00064   
00065   /* special functions */
00066   
00067   Type Length(void) const;                  /* length of a Quat */
00068   Type LengthSqr(void) const;               /* squared length of a Quat */
00069   Type Norm(void) const;                    /* also squared length of a Quat */
00070   Quat &Normalize(void);                    /* normalize a Quat */
00071   Quat &Invert(void);                       /* q = q^-1 */
00072   Quat &Conjugate(void);                    /* q = q* */
00073   qvec Xform( const qvec &v ) const;        /* q*v*q-1 */
00074   Quat &Log(void);                          /* log(q) */
00075   Quat &Exp(void);                          /* exp(q) */
00076   qvec GetAxis( void ) const;               /* Get rot axis */
00077   Type GetAngle( void ) const;              /* Get rot angle (radians) */
00078   void SetAngle( Type rad_ang );            /* set rot angle (radians) */
00079   void ScaleAngle( Type f );                /* scale rot angle */
00080   void Print( ) const;                      /* print Quat */
00081 
00082   /* Conversions */
00083   Mat44<Type>& ToMat( Mat44<Type> &dest ) const;   /* to 4x4 matrix */
00084   Mat33<Type>& ToMat( Mat33<Type> &dest ) const;   /* to 3x3 matrix */
00085   Quat& FromMat( const Mat44<Type>& src );         /* from 4x4 rot matrix */
00086   Quat& FromMat( const Mat33<Type>& src );         /* from 3x3 rot matrix */
00087 
00088   void ToAngleAxis( Type &ang, qvec &ax ) const;  /* to rot angle AND axis */
00089   Quat& FromAngleAxis( Type ang, const qvec &ax );/*from rot angle AND axis */
00090   Quat& FromTwoVecs(const qvec &a, const qvec& b); /* quat from a to b */
00091   /* to/from Euler Angles (XYZ-Fixed/ZYX-Relative, angles in radians) */
00092   Quat& FromEuler( Type yaw, Type pitch, Type roll);
00093   void ToEuler(Type &yaw, Type &pitch, Type &roll) const;
00094   
00095 
00096   // HELPERS
00097   static Type DEG2RAD(Type d);
00098   static Type RAD2DEG(Type d);
00099   static Type Sin(double d);
00100   static Type Cos(double d);
00101   static Type ACos(double d);
00102   static Type ASin(double d);
00103   static Type ATan(double d);
00104   static Type ATan2(double n, double d);
00105 
00106   // CONSTANTS
00107   static const Type FUDGE();
00108   static const Quat ZERO();
00109   static const Quat IDENTITY();
00110 }; 
00111 
00112 /* Utility functions */
00113 template <class Type>
00114 Quat<Type>& QuatSlerp(
00115   Quat<Type> &dest, const Quat<Type>& from, const Quat<Type>& to, Type t );
00116 template <class Type>
00117 Quat<Type> QuatSlerp(const Quat<Type>& from, const Quat<Type>& to, Type t );
00118 
00119 /* "Friends" */
00120 template <class Type>
00121 Quat<Type> operator -(const Quat<Type> &v);                      // -q1
00122 template <class Type>
00123 Quat<Type> operator +(const Quat<Type> &a, const Quat<Type> &b); // q1 + q2
00124 template <class Type>
00125 Quat<Type> operator -(const Quat<Type> &a, const Quat<Type> &b); // q1 - q2
00126 template <class Type>
00127 Quat<Type> operator *(const Quat<Type> &a, const Type d);        // q1 * 3.0
00128 template <class Type>
00129 Quat<Type> operator *(const Type d, const Quat<Type> &a);        // 3.0 * q1
00130 template <class Type>
00131 Quat<Type> operator *(const Quat<Type> &a, const Quat<Type> &b); // q1 * q2
00132 template <class Type>
00133 Quat<Type> operator /(const Quat<Type> &a, const Type d);        // q1 / 3.0
00134 template <class Type>
00135 bool operator ==(const Quat<Type> &a, const Quat<Type> &b);      // q1 == q2 ?
00136 template <class Type>
00137 bool operator !=(const Quat<Type> &a, const Quat<Type> &b);      // q1 != q2 ?
00138 
00139 
00140 
00141 #include "quatimpl.hpp"
00142 
00143 
00144 
00145 typedef Quat<float> Quatf;
00146 typedef Quat<double> Quatd;
00147 
00148 #endif

Generated at Fri Oct 12 15:12:21 2001 for GLVU by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001