mathglpp::GLQuaternion< T > Class Template Reference

#include <GLQuaternion.h>

Collaboration diagram for mathglpp::GLQuaternion< T >:

Collaboration graph
[legend]
List of all members.

Detailed Description

template<class T>
class mathglpp::GLQuaternion< T >

A quaternion is a 3D plane in imaginary space, or some say a 4D vector.

It behaves mathematically much like a plane in 3D, where a rotation is a reflection about two planes, one with the normal at half the angle and another at the angle. It is useful for rotations in 3D space because multiplications of quaternions are commutative, ie. it doesn't matter in which order you do them.

Author:
Jacques Gasselin

Definition at line 34 of file GLQuaternion.h.

Public Member Functions

 GLQuaternion ()
 Create a unit quaternion.
 GLQuaternion (T f, const GLVector3< T > &gv)
 Create a scaled directed quaternion.
 GLQuaternion (const GLVector3< T > &gv)
 Create a directed quaternion.
 GLQuaternion (const T *gq)
 Create from array, must be at least 4 elements long.
 GLQuaternion (const GLQuaternion &gq)
 Copy a quaternion.
 GLQuaternion (GLVector3< T > gv, T degrees)
 Create a rotation quaternion, *MOST PEOPLE WILL WANT TO USE THIS CONSTRUCTOR ONLY* NB.
 ~GLQuaternion ()
 Default destructor.
getW ()
 Get real or plane constant.
const GLVector3< T > & getV ()
 Get imaginary or plane normal.
void set (T _w, const GLVector3< T > &gv)
 set from a scaled vector.
void set (T _w, T x, T y, T z)
 Set from immediates.
void set (const T *gq)
 Set from array, must be at least 4 elements long.
void set (GLVector3< T > vec, T degrees)
 Set from angle and normal, NB.
GLQuaternion operator+ (GLQuaternion gq)
 Quaternion addition.
GLQuaternion operator- (GLQuaternion gq)
 Quaternion subtraction.
GLQuaternionoperator+= (GLQuaternion gq)
 Quaternion self referenced addition.
GLQuaternionoperator-= (GLQuaternion gq)
 Quaternion self referenced subtraction.
GLQuaternion operator * (GLQuaternion gq)
 Quaternion multiplication.
GLQuaternion operator/ (T f)
 Quaternion division, NB. Do not divide by 0, there are no safety catches for that.
GLQuaternionoperator *= (GLQuaternion gq)
 Quaternion self referenced multiplication.
GLQuaternionoperator/= (T f)
 Quaternion self referenced division.
dot (GLQuaternion gq)
 Quaternion dot product.
GLQuaternion getCross (GLQuaternion gq)
 Quaternion cross product.
GLQuaternion getConjugate ()
 Quaternion complex conjugate.
GLQuaternionconjugate ()
 Quaternion self referenced complex conjugate.
GLQuaternion getInverse ()
 Quaternion inverse (more like a normalised conjugate).
GLQuaternioninverse ()
 Quaternion self referenced inverse (more like a normalised conjugate).
selection ()
 Real part.
norm ()
 Quaternion norm (similar to vector length).
GLQuaternion unit () const
 Quaternion unit.
GLQuaternionnormalise ()
 Quaternion self referenced unit.
GLQuaternionnormalize ()
GLVector3< T > rotateVector (const GLVector3< T > &gv)
 GLVector interface method for ease of use.
GLMatrix< T > matrix (void)
 GLMatrix interface method for obtaining an equivalent rotation matrix.


Constructor & Destructor Documentation

template<class T>
mathglpp::GLQuaternion< T >::GLQuaternion (  )  [inline]

Create a unit quaternion.

Definition at line 38 of file GLQuaternion.h.

Referenced by mathglpp::GLQuaternion< T >::getCross(), and mathglpp::GLQuaternion< T >::rotateVector().

00039     :w(1),v(0,0,0)
00040     {}

template<class T>
mathglpp::GLQuaternion< T >::GLQuaternion ( f,
const GLVector3< T > &  gv 
) [inline]

Create a scaled directed quaternion.

Definition at line 43 of file GLQuaternion.h.

00044     :w(f),v(gv)
00045     {}

template<class T>
mathglpp::GLQuaternion< T >::GLQuaternion ( const GLVector3< T > &  gv  )  [inline]

Create a directed quaternion.

Definition at line 48 of file GLQuaternion.h.

00049     :w(0),v(gv)
00050     {}

template<class T>
mathglpp::GLQuaternion< T >::GLQuaternion ( const T *  gq  )  [inline]

Create from array, must be at least 4 elements long.

Definition at line 53 of file GLQuaternion.h.

00054     :w(gq[0]),v(gq+1)
00055     {}

template<class T>
mathglpp::GLQuaternion< T >::GLQuaternion ( const GLQuaternion< T > &  gq  )  [inline]

Copy a quaternion.

Definition at line 58 of file GLQuaternion.h.

00059     :w(gq.w),v(gq.v)
00060     {}

template<class T>
mathglpp::GLQuaternion< T >::GLQuaternion ( GLVector3< T >  gv,
degrees 
) [inline]

Create a rotation quaternion, *MOST PEOPLE WILL WANT TO USE THIS CONSTRUCTOR ONLY* NB.

Do not halve angle, this is done within the constructor. *TO CONFORM WITH THE OpenGL INTERFACE THE ANGLES MUST BE GIVEN IN DEGREES

Definition at line 66 of file GLQuaternion.h.

References mathglpp::GLVector3< T >::unit().

00067     {   set(gv.unit(), degrees); }

Here is the call graph for this function:

template<class T>
mathglpp::GLQuaternion< T >::~GLQuaternion (  )  [inline]

Default destructor.

Definition at line 70 of file GLQuaternion.h.

00070 {}


Member Function Documentation

template<class T>
T mathglpp::GLQuaternion< T >::getW (  )  [inline]

Get real or plane constant.

Definition at line 73 of file GLQuaternion.h.

00074     {   return w; }

template<class T>
const GLVector3<T>& mathglpp::GLQuaternion< T >::getV (  )  [inline]

Get imaginary or plane normal.

Definition at line 77 of file GLQuaternion.h.

00078     {   return v; }

template<class T>
void mathglpp::GLQuaternion< T >::set ( _w,
const GLVector3< T > &  gv 
) [inline]

set from a scaled vector.

Definition at line 81 of file GLQuaternion.h.

00082     {   w = _w; v = gv; }

template<class T>
void mathglpp::GLQuaternion< T >::set ( _w,
x,
y,
z 
) [inline]

Set from immediates.

Definition at line 85 of file GLQuaternion.h.

00086     {   w = _w; v.set(x,y,z); }

template<class T>
void mathglpp::GLQuaternion< T >::set ( const T *  gq  )  [inline]

Set from array, must be at least 4 elements long.

Definition at line 89 of file GLQuaternion.h.

00090     {   set(gq[0],gq[1],gq[2],gq[3]); }

template<class T>
void mathglpp::GLQuaternion< T >::set ( GLVector3< T >  vec,
degrees 
) [inline]

Set from angle and normal, NB.

Do not halve angle it is done internally. TO CONFORM WITH THE OpenGL INTERFACE THE ANGLES MUST BE GIVEN IN DEGREES

Definition at line 94 of file GLQuaternion.h.

00095     {   set(cos(degrees*M_PI/360.0), vec * sin(degrees*M_PI/360.0) ); }

template<class T>
GLQuaternion mathglpp::GLQuaternion< T >::operator+ ( GLQuaternion< T >  gq  )  [inline]

Quaternion addition.

Definition at line 98 of file GLQuaternion.h.

00099     {   GLQuaternion temp(*this); temp += gq; return temp; }

template<class T>
GLQuaternion mathglpp::GLQuaternion< T >::operator- ( GLQuaternion< T >  gq  )  [inline]

Quaternion subtraction.

Definition at line 102 of file GLQuaternion.h.

00103     {   GLQuaternion temp(*this); temp -= gq; return temp; }

template<class T>
GLQuaternion& mathglpp::GLQuaternion< T >::operator+= ( GLQuaternion< T >  gq  )  [inline]

Quaternion self referenced addition.

Definition at line 106 of file GLQuaternion.h.

References mathglpp::GLQuaternion< T >::v, and mathglpp::GLQuaternion< T >::w.

00107     {   w += gq.w; v += gq.v; return *this; }

template<class T>
GLQuaternion& mathglpp::GLQuaternion< T >::operator-= ( GLQuaternion< T >  gq  )  [inline]

Quaternion self referenced subtraction.

Definition at line 110 of file GLQuaternion.h.

References mathglpp::GLQuaternion< T >::v, and mathglpp::GLQuaternion< T >::w.

00111     {   w -= gq.w; v -= gq.v; return *this; }

template<class T>
GLQuaternion mathglpp::GLQuaternion< T >::operator * ( GLQuaternion< T >  gq  )  [inline]

Quaternion multiplication.

Definition at line 114 of file GLQuaternion.h.

00115     {   GLQuaternion temp(*this); temp *=  gq; return temp; }

template<class T>
GLQuaternion mathglpp::GLQuaternion< T >::operator/ ( f  )  [inline]

Quaternion division, NB. Do not divide by 0, there are no safety catches for that.

Definition at line 118 of file GLQuaternion.h.

Referenced by mathglpp::GLQuaternion< T >::unit().

00119     {   GLQuaternion temp(*this); temp /= f; return temp; }

template<class T>
GLQuaternion& mathglpp::GLQuaternion< T >::operator *= ( GLQuaternion< T >  gq  )  [inline]

Quaternion self referenced multiplication.

Definition at line 122 of file GLQuaternion.h.

References mathglpp::GLQuaternion< T >::v, and mathglpp::GLQuaternion< T >::w.

00123     {   set( w * gq.w - v.dot(gq.v) , (gq.v * w) + (v * gq.w) + v.getCross(gq.v) ); return *this; }

template<class T>
GLQuaternion& mathglpp::GLQuaternion< T >::operator/= ( f  )  [inline]

Quaternion self referenced division.

Definition at line 126 of file GLQuaternion.h.

Referenced by mathglpp::GLQuaternion< T >::normalize().

00127     {   set( w / f , v / f ); return *this; }

template<class T>
T mathglpp::GLQuaternion< T >::dot ( GLQuaternion< T >  gq  )  [inline]

Quaternion dot product.

Definition at line 130 of file GLQuaternion.h.

References mathglpp::GLQuaternion< T >::v, and mathglpp::GLQuaternion< T >::w.

00131     {   return ( w*gq.w + v.dot(gq.v) ); }

template<class T>
GLQuaternion mathglpp::GLQuaternion< T >::getCross ( GLQuaternion< T >  gq  )  [inline]

Quaternion cross product.

Definition at line 134 of file GLQuaternion.h.

References mathglpp::GLQuaternion< T >::GLQuaternion(), mathglpp::GLQuaternion< T >::v, and mathglpp::GLQuaternion< T >::w.

Referenced by mathglpp::GLQuaternion< T >::rotateVector().

00135     {   return GLQuaternion( w * gq.w - v.dot(gq.v) , (gq.v * w) + (v * gq.w) + v.getCross(gq.v) ); }

Here is the call graph for this function:

template<class T>
GLQuaternion mathglpp::GLQuaternion< T >::getConjugate (  )  [inline]

Quaternion complex conjugate.

Definition at line 138 of file GLQuaternion.h.

References mathglpp::GLQuaternion< T >::conjugate().

Referenced by mathglpp::GLQuaternion< T >::rotateVector().

00139     {   GLQuaternion temp(*this); temp.conjugate(); return temp; }

Here is the call graph for this function:

template<class T>
GLQuaternion& mathglpp::GLQuaternion< T >::conjugate (  )  [inline]

Quaternion self referenced complex conjugate.

Definition at line 142 of file GLQuaternion.h.

Referenced by mathglpp::GLQuaternion< T >::getConjugate(), and mathglpp::GLQuaternion< T >::inverse().

00143     {   v = -v; return *this; }

template<class T>
GLQuaternion mathglpp::GLQuaternion< T >::getInverse (  )  [inline]

Quaternion inverse (more like a normalised conjugate).

Definition at line 146 of file GLQuaternion.h.

References mathglpp::GLQuaternion< T >::inverse().

00147     {   GLQuaternion temp(*this); temp.inverse(); return temp; }

Here is the call graph for this function:

template<class T>
GLQuaternion& mathglpp::GLQuaternion< T >::inverse (  )  [inline]

Quaternion self referenced inverse (more like a normalised conjugate).

Definition at line 150 of file GLQuaternion.h.

References mathglpp::GLQuaternion< T >::conjugate(), and mathglpp::GLQuaternion< T >::norm().

Referenced by mathglpp::GLQuaternion< T >::getInverse().

00151     {   conjugate(); *this /= norm(); return *this; }

Here is the call graph for this function:

template<class T>
T mathglpp::GLQuaternion< T >::selection (  )  [inline]

Real part.

Definition at line 154 of file GLQuaternion.h.

00155     {   return w; }

template<class T>
T mathglpp::GLQuaternion< T >::norm (  )  [inline]

Quaternion norm (similar to vector length).

Definition at line 158 of file GLQuaternion.h.

Referenced by mathglpp::GLQuaternion< T >::inverse(), mathglpp::GLQuaternion< T >::normalize(), and mathglpp::GLQuaternion< T >::unit().

00159     {   return sqrt( w*w + v.lengthSqr() ); }

template<class T>
GLQuaternion mathglpp::GLQuaternion< T >::unit (  )  const [inline]

Quaternion unit.

Definition at line 162 of file GLQuaternion.h.

References mathglpp::GLQuaternion< T >::norm(), and mathglpp::GLQuaternion< T >::operator/().

00163     {   return this->operator /(norm()); }

Here is the call graph for this function:

template<class T>
GLQuaternion& mathglpp::GLQuaternion< T >::normalise (  )  [inline]

Quaternion self referenced unit.

Definition at line 166 of file GLQuaternion.h.

References mathglpp::GLQuaternion< T >::normalize().

00166 { return normalize(); }

Here is the call graph for this function:

template<class T>
GLVector3<T> mathglpp::GLQuaternion< T >::rotateVector ( const GLVector3< T > &  gv  )  [inline]

GLVector interface method for ease of use.

THIS IS PROBABLY THE ONLY METHOD YOU WOULD NORMALLY USE. NB. Don't forget to normalise the quaternion unless you want axial translation as well as rotation.

Definition at line 173 of file GLQuaternion.h.

References mathglpp::GLQuaternion< T >::getConjugate(), mathglpp::GLQuaternion< T >::getCross(), mathglpp::GLQuaternion< T >::GLQuaternion(), and mathglpp::GLQuaternion< T >::v.

00174     {   GLQuaternion temp = getCross(GLQuaternion(gv)); temp *= getConjugate(); return temp.v; }

Here is the call graph for this function:

template<class T>
GLMatrix<T> mathglpp::GLQuaternion< T >::matrix ( void   )  [inline]

GLMatrix interface method for obtaining an equivalent rotation matrix.

You would se this for interfacing with the OpenGL matrix stacks.

Definition at line 178 of file GLQuaternion.h.

00179     {
00180         GLMatrix<T> temp;
00181 
00182         T xx=v.x*v.x;  T xy=v.x*v.y;  T xz=v.x*v.z;  T xw=v.x*w;
00183         T yy=v.y*v.y;  T yz=v.y*v.z;  T yw=v.y*w;
00184         T zz=v.z*v.z;  T zw=v.z*w;
00185 
00186         temp[0] = 1 - 2*(zz + yy);
00187         temp[1] = 2*(xy + zw);
00188         temp[2] = 2*(xz - yw);
00189         temp[3] = 0;
00190 
00191         temp[4] = 2*(xy - zw);
00192         temp[5] = 1 - 2*(zz + xx);
00193         temp[6] = 2*(yz + xw);
00194         temp[7] = 0;
00195 
00196         temp[8] = 2*(yw + xz);
00197         temp[9] = 2*(yz - xw);
00198         temp[10] = 1 - 2*(yy + xx);
00199         temp[11] = 0;
00200 
00201         temp[12] = 0;
00202         temp[13] = 0;
00203         temp[14] = 0;
00204         temp[15] = 1;
00205 
00206         return temp;
00207     }


The documentation for this class was generated from the following file:
Generated on Wed Oct 3 12:50:53 2007 for MathGL++ by  doxygen 1.5.2