mQuat.h 5.5 KB

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