mQuat.h

Engine/source/math/mQuat.h

More...

Classes:

class

Public Functions

QuatIsEqual(F32 a, F32 b, F32 epsilon)
QuatIsZero(F32 a, F32 epsilon)

Detailed Description

Public Functions

QuatIsEqual(F32 a, F32 b, F32 epsilon)

QuatIsZero(F32 a, F32 epsilon)

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _MQUAT_H_
 25#define _MQUAT_H_
 26
 27#ifndef _MPOINT3_H_
 28#include "math/mPoint3.h"
 29#endif
 30
 31class MatrixF;
 32class AngAxisF;
 33
 34//----------------------------------------------------------------------------
 35// unit quaternion class:
 36
 37class QuatF
 38{
 39   //-------------------------------------- Public static constants
 40public:
 41   const static QuatF Identity;
 42
 43  public:
 44   F32  x,y,z,w;
 45
 46   QuatF() :x(0.0f), y(0.0f), z(0.0f), w(1.0f) {} //identity constructor
 47   QuatF( F32 _x, F32 _y, F32 _z, F32 w );
 48   QuatF( const Point3F &axis, F32 angle );
 49   QuatF( const MatrixF & m );
 50   QuatF( const AngAxisF & a );
 51   QuatF( const EulerF & e );
 52
 53   QuatF& set( F32 _x, F32 _y, F32 _z, F32 _w );
 54   QuatF& set( const Point3F &axis, F32 angle );
 55   QuatF& set( const MatrixF & m );
 56   QuatF& set( const AngAxisF & a );
 57   QuatF& set( const EulerF & e );
 58
 59   S32 operator==( const QuatF & c ) const;
 60   S32 operator!=( const QuatF & c ) const;
 61   QuatF& operator*=( const QuatF & c );
 62   QuatF& operator/=( const QuatF & c );
 63   QuatF& operator+=( const QuatF & c );
 64   QuatF& operator-=( const QuatF & c );
 65   QuatF& operator*=( F32 a );
 66   QuatF& operator/=( F32 a );
 67
 68   QuatF operator-( const QuatF &c ) const;
 69   QuatF operator*( F32 a ) const;
 70
 71   QuatF& square();
 72   QuatF& neg();
 73   F32  dot( const QuatF &q ) const;
 74
 75   MatrixF* setMatrix( MatrixF * mat ) const;
 76   QuatF& normalize();
 77   QuatF& inverse();
 78   QuatF& identity();
 79   S32    isIdentity() const;
 80   QuatF& slerp( const QuatF & q, F32 t );
 81   QuatF& extrapolate( const QuatF & q1, const QuatF & q2, F32 t );
 82   QuatF& interpolate( const QuatF & q1, const QuatF & q2, F32 t );
 83   F32  angleBetween( const QuatF & q );
 84
 85   Point3F& mulP(const Point3F& a, Point3F* r) const;   // r = p * this
 86   QuatF& mul(const QuatF& a, const QuatF& b);    // This = a * b
 87
 88   // Vectors passed in must be normalized
 89   QuatF& shortestArc( const VectorF &normalizedA, const VectorF &normalizedB );
 90};
 91
 92// a couple simple utility methods
 93inline F32 QuatIsEqual(F32 a,F32 b,F32 epsilon = 0.0001f) { return mFabs(a-b) < epsilon; }
 94inline F32 QuatIsZero(F32 a,F32 epsilon = 0.0001f) { return mFabs(a) < epsilon; }
 95
 96//----------------------------------------------------------------------------
 97// quaternion implementation:
 98
 99inline QuatF::QuatF( F32 _x, F32 _y, F32 _z, F32 _w )
100{
101   set( _x, _y, _z, _w );
102}
103
104inline QuatF::QuatF( const Point3F &axis, F32 angle )
105{
106   set( axis, angle );
107}
108
109inline QuatF::QuatF( const AngAxisF & a )
110{
111   set( a );
112}
113
114inline QuatF::QuatF( const EulerF & e )
115{
116   set( e );
117}
118
119inline QuatF::QuatF( const MatrixF & m )
120{
121   set( m );
122}
123
124inline QuatF& QuatF::set( F32 _x, F32 _y, F32 _z, F32 _w )
125{
126   x = _x;
127   y = _y;
128   z = _z;
129   w = _w;
130   return *this;
131}
132
133inline int QuatF::operator==( const QuatF & c ) const
134{
135   QuatF a = *this;
136   QuatF b = c;
137   a.normalize();
138   b.normalize();
139   b.inverse();
140   a *= b;
141   return a.isIdentity();
142}
143
144inline int QuatF::isIdentity() const
145{
146   return QuatIsZero( x ) && QuatIsZero( y ) && QuatIsZero( z );
147}
148
149inline QuatF& QuatF::identity()
150{
151   x = 0.0f;
152   y = 0.0f;
153   z = 0.0f;
154   w = 1.0f;
155   return *this;
156}
157
158inline int QuatF::operator!=( const QuatF & c ) const
159{
160   return !operator==( c );
161}
162
163inline QuatF& QuatF::operator+=( const QuatF & c )
164{
165   x += c.x;
166   y += c.y;
167   z += c.z;
168   w += c.w;
169   return *this;
170}
171
172inline QuatF& QuatF::operator-=( const QuatF & c )
173{
174   x -= c.x;
175   y -= c.y;
176   z -= c.z;
177   w -= c.w;
178   return *this;
179}
180
181inline QuatF& QuatF::operator*=( F32 a )
182{
183   x *= a;
184   y *= a;
185   z *= a;
186   w *= a;
187   return *this;
188}
189
190inline QuatF& QuatF::operator/=( F32 a )
191{
192   x /= a;
193   y /= a;
194   z /= a;
195   w /= a;
196   return *this;
197}
198
199inline QuatF QuatF::operator-( const QuatF &c ) const
200{
201   return QuatF( x - c.x,
202                 y - c.y,
203                 z - c.z,
204                 w - c.w );
205}
206
207inline QuatF QuatF::operator*( F32 a ) const
208{
209   return QuatF( x * a,
210                 y * a,
211                 z * a,
212                 w * a );
213}
214
215inline QuatF& QuatF::neg()
216{
217   x = -x;
218   y = -y;
219   z = -z;
220   w = -w;
221   return *this;
222}
223
224inline F32 QuatF::dot( const QuatF &q ) const
225{
226   return mClampF(w*q.w + x*q.x + y*q.y + z*q.z, -1.0f, 1.0f);
227}
228
229inline F32 QuatF::angleBetween( const QuatF & q )
230{
231   // angle between two normalized quaternions.
232   return mAcos(q.dot(*this)) * 2.0f;
233}
234
235#endif // _MQUAT_H_
236