1 //------------------------------------------------------------------------------
3 //------------------------------------------------------------------------------
4 // GLVU : Copyright 1997 - 2002
5 // The University of North Carolina at Chapel Hill
6 //------------------------------------------------------------------------------
7 // Permission to use, copy, modify, distribute and sell this software and its
8 // documentation for any purpose is hereby granted without fee, provided that
9 // the above copyright notice appear in all copies and that both that copyright
10 // notice and this permission notice appear in supporting documentation.
11 // Binaries may be compiled with this software without any royalties or
14 // The University of North Carolina at Chapel Hill makes no representations
15 // about the suitability of this software for any purpose. It is provided
16 // "as is" without express or implied warranty.
18 /**************************************************************************
22 A quaternion template class
24 ---------------------------------------------------------------------
26 Feb 1998, Paul Rademacher (rademach@cs.unc.edu)
29 - Oct 2000, Bill Baxter. Adapted to GLVU coding conventions and
30 templetized. Also implemented many methods that were declared but
31 not defined. Added some methods from quatlib and other sources.
33 **************************************************************************/
42 /****************************************************************
44 ****************************************************************/
50 typedef Vec3<Type> qvec;
52 Type s; /* scalar component */
53 qvec v; /* quat vector component */
58 Quat(Type s, Type x, Type y, Type z);
59 Quat(Type x, Type y, Type z); // s=0
60 Quat(Type s, const qvec& v);
61 Quat(const qvec& v, Type s = 0.0);
62 Quat(const Type *d); /* copy from four-element Type array s,x,y,z */
63 Quat(const Quat &q); /* copy from other Quat */
67 void Set( Type x, Type y, Type z );
68 void Set( Type s, Type x, Type y, Type z );
69 void Set( Type s, const qvec& v );
70 void Set( const qvec& v, Type s=0 );
74 Quat &operator = ( const Quat &v ); /* assignment of a Quat */
75 Quat &operator += ( const Quat &v ); /* incrementation by a Quat */
76 Quat &operator -= ( const Quat &v ); /* decrementation by a Quat */
77 Quat &operator *= ( const Type d ); /* multiplication by a scalar */
78 Quat &operator *= ( const Quat &v ); /* quat product (this*v) */
79 Quat &operator /= ( const Type d ); /* division by a scalar */
80 Type &operator [] ( int i); /* indexing s=0,x=1,y=2,z=3 */
82 /* special functions */
84 Type Length(void) const; /* length of a Quat */
85 Type LengthSqr(void) const; /* squared length of a Quat */
86 Type Norm(void) const; /* also squared length of a Quat */
87 Quat &Normalize(void); /* normalize a Quat */
88 Quat &Invert(void); /* q = q^-1 */
89 Quat &Conjugate(void); /* q = q* */
90 qvec Xform( const qvec &v ) const; /* q*v*q-1 */
91 Quat &Log(void); /* log(q) */
92 Quat &Exp(void); /* exp(q) */
93 qvec GetAxis( void ) const; /* Get rot axis */
94 Type GetAngle( void ) const; /* Get rot angle (radians) */
95 void SetAngle( Type rad_ang ); /* set rot angle (radians) */
96 void ScaleAngle( Type f ); /* scale rot angle */
97 void Print( ) const; /* print Quat */
100 Mat44<Type>& ToMat( Mat44<Type> &dest ) const; /* to 4x4 matrix */
101 Mat33<Type>& ToMat( Mat33<Type> &dest ) const; /* to 3x3 matrix */
102 Quat& FromMat( const Mat44<Type>& src ); /* from 4x4 rot matrix */
103 Quat& FromMat( const Mat33<Type>& src ); /* from 3x3 rot matrix */
105 void ToAngleAxis( Type &ang, qvec &ax ) const; /* to rot angle AND axis */
106 Quat& FromAngleAxis( Type ang, const qvec &ax );/*from rot angle AND axis */
107 Quat& FromTwoVecs(const qvec &a, const qvec& b); /* quat from a to b */
108 // to/from Euler Angles (XYZ-Fixed/ZYX-Relative, angles in radians)
109 // See quatimpl.hpp for more detailed comments.
110 Quat& FromEuler( Type yaw_Z, Type pitch_Y, Type roll_X);
111 void ToEuler(Type &yaw_Z, Type &pitch_Y, Type &roll_X) const;
115 static Type DEG2RAD(Type d);
116 static Type RAD2DEG(Type d);
117 static Type Sin(double d);
118 static Type Cos(double d);
119 static Type ACos(double d);
120 static Type ASin(double d);
121 static Type ATan(double d);
122 static Type ATan2(double n, double d);
127 static Quat IDENTITY();
130 /* Utility functions */
131 template <class Type>
132 Quat<Type>& QuatSlerp(
133 Quat<Type> &dest, const Quat<Type>& from, const Quat<Type>& to, Type t );
134 template <class Type>
135 Quat<Type> QuatSlerp(const Quat<Type>& from, const Quat<Type>& to, Type t );
138 template <class Type>
139 Quat<Type> operator -(const Quat<Type> &v); // -q1
140 template <class Type>
141 Quat<Type> operator +(const Quat<Type> &a, const Quat<Type> &b); // q1 + q2
142 template <class Type>
143 Quat<Type> operator -(const Quat<Type> &a, const Quat<Type> &b); // q1 - q2
144 template <class Type>
145 Quat<Type> operator *(const Quat<Type> &a, const Type d); // q1 * 3.0
146 template <class Type>
147 Quat<Type> operator *(const Type d, const Quat<Type> &a); // 3.0 * q1
148 template <class Type>
149 Quat<Type> operator *(const Quat<Type> &a, const Quat<Type> &b); // q1 * q2
150 template <class Type>
151 Quat<Type> operator /(const Quat<Type> &a, const Type d); // q1 / 3.0
152 template <class Type>
153 bool operator ==(const Quat<Type> &a, const Quat<Type> &b); // q1 == q2 ?
154 template <class Type>
155 bool operator !=(const Quat<Type> &a, const Quat<Type> &b); // q1 != q2 ?
159 #include "quatimpl.hpp"
163 typedef Quat<float> Quatf;
164 typedef Quat<double> Quatd;