CmQuaternion.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  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 FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. // This file is based on material originally from:
  25. // Geometric Tools, LLC
  26. // Copyright (c) 1998-2010
  27. // Distributed under the Boost Software License, Version 1.0.
  28. // http://www.boost.org/LICENSE_1_0.txt
  29. // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  30. #ifndef __Quaternion_H__
  31. #define __Quaternion_H__
  32. #include "CmPrerequisitesUtil.h"
  33. #include "CmMath.h"
  34. namespace CamelotEngine {
  35. /** \addtogroup Core
  36. * @{
  37. */
  38. /** \addtogroup Math
  39. * @{
  40. */
  41. /** Implementation of a Quaternion, i.e. a rotation around an axis.
  42. */
  43. class CM_UTILITY_EXPORT Quaternion
  44. {
  45. public:
  46. inline Quaternion (
  47. float fW = 1.0,
  48. float fX = 0.0, float fY = 0.0, float fZ = 0.0)
  49. {
  50. w = fW;
  51. x = fX;
  52. y = fY;
  53. z = fZ;
  54. }
  55. /// Construct a quaternion from a rotation matrix
  56. inline Quaternion(const Matrix3& rot)
  57. {
  58. this->FromRotationMatrix(rot);
  59. }
  60. /// Construct a quaternion from an angle/axis
  61. inline Quaternion(const Radian& rfAngle, const Vector3& rkAxis)
  62. {
  63. this->FromAngleAxis(rfAngle, rkAxis);
  64. }
  65. /// Construct a quaternion from 3 orthonormal local axes
  66. inline Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
  67. {
  68. this->FromAxes(xaxis, yaxis, zaxis);
  69. }
  70. /// Construct a quaternion from 3 orthonormal local axes
  71. inline Quaternion(const Vector3* akAxis)
  72. {
  73. this->FromAxes(akAxis);
  74. }
  75. /// Construct a quaternion from 4 manual w/x/y/z values
  76. inline Quaternion(float* valptr)
  77. {
  78. memcpy(&w, valptr, sizeof(float)*4);
  79. }
  80. /** Exchange the contents of this quaternion with another.
  81. */
  82. inline void swap(Quaternion& other)
  83. {
  84. std::swap(w, other.w);
  85. std::swap(x, other.x);
  86. std::swap(y, other.y);
  87. std::swap(z, other.z);
  88. }
  89. /// Array accessor operator
  90. inline float operator [] ( const size_t i ) const
  91. {
  92. assert( i < 4 );
  93. return *(&w+i);
  94. }
  95. /// Array accessor operator
  96. inline float& operator [] ( const size_t i )
  97. {
  98. assert( i < 4 );
  99. return *(&w+i);
  100. }
  101. /// Pointer accessor for direct copying
  102. inline float* ptr()
  103. {
  104. return &w;
  105. }
  106. /// Pointer accessor for direct copying
  107. inline const float* ptr() const
  108. {
  109. return &w;
  110. }
  111. void FromRotationMatrix (const Matrix3& kRot);
  112. void ToRotationMatrix (Matrix3& kRot) const;
  113. void FromAngleAxis (const Radian& rfAngle, const Vector3& rkAxis);
  114. void ToAngleAxis (Radian& rfAngle, Vector3& rkAxis) const;
  115. inline void ToAngleAxis (Degree& dAngle, Vector3& rkAxis) const {
  116. Radian rAngle;
  117. ToAngleAxis ( rAngle, rkAxis );
  118. dAngle = rAngle;
  119. }
  120. void FromAxes (const Vector3* akAxis);
  121. void FromAxes (const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
  122. void ToAxes (Vector3* akAxis) const;
  123. void ToAxes (Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const;
  124. /// Get the local x-axis
  125. Vector3 xAxis(void) const;
  126. /// Get the local y-axis
  127. Vector3 yAxis(void) const;
  128. /// Get the local z-axis
  129. Vector3 zAxis(void) const;
  130. inline Quaternion& operator= (const Quaternion& rkQ)
  131. {
  132. w = rkQ.w;
  133. x = rkQ.x;
  134. y = rkQ.y;
  135. z = rkQ.z;
  136. return *this;
  137. }
  138. Quaternion operator+ (const Quaternion& rkQ) const;
  139. Quaternion operator- (const Quaternion& rkQ) const;
  140. Quaternion operator* (const Quaternion& rkQ) const;
  141. Quaternion operator* (float fScalar) const;
  142. CM_UTILITY_EXPORT friend Quaternion operator* (float fScalar,
  143. const Quaternion& rkQ);
  144. Quaternion operator- () const;
  145. inline bool operator== (const Quaternion& rhs) const
  146. {
  147. return (rhs.x == x) && (rhs.y == y) &&
  148. (rhs.z == z) && (rhs.w == w);
  149. }
  150. inline bool operator!= (const Quaternion& rhs) const
  151. {
  152. return !operator==(rhs);
  153. }
  154. // functions of a quaternion
  155. float Dot (const Quaternion& rkQ) const; // dot product
  156. float Norm () const; // squared-length
  157. /// Normalises this quaternion, and returns the previous length
  158. float normalise(void);
  159. Quaternion Inverse () const; // apply to non-zero quaternion
  160. Quaternion UnitInverse () const; // apply to unit-length quaternion
  161. Quaternion Exp () const;
  162. Quaternion Log () const;
  163. // rotation of a vector by a quaternion
  164. Vector3 operator* (const Vector3& rkVector) const;
  165. /** Calculate the local roll element of this quaternion.
  166. @param reprojectAxis By default the method returns the 'intuitive' result
  167. that is, if you projected the local Y of the quaternion onto the X and
  168. Y axes, the angle between them is returned. If set to false though, the
  169. result is the actual yaw that will be used to implement the quaternion,
  170. which is the shortest possible path to get to the same orientation and
  171. may involve less axial rotation.
  172. */
  173. Radian getRoll(bool reprojectAxis = true) const;
  174. /** Calculate the local pitch element of this quaternion
  175. @param reprojectAxis By default the method returns the 'intuitive' result
  176. that is, if you projected the local Z of the quaternion onto the X and
  177. Y axes, the angle between them is returned. If set to true though, the
  178. result is the actual yaw that will be used to implement the quaternion,
  179. which is the shortest possible path to get to the same orientation and
  180. may involve less axial rotation.
  181. */
  182. Radian getPitch(bool reprojectAxis = true) const;
  183. /** Calculate the local yaw element of this quaternion
  184. @param reprojectAxis By default the method returns the 'intuitive' result
  185. that is, if you projected the local Z of the quaternion onto the X and
  186. Z axes, the angle between them is returned. If set to true though, the
  187. result is the actual yaw that will be used to implement the quaternion,
  188. which is the shortest possible path to get to the same orientation and
  189. may involve less axial rotation.
  190. */
  191. Radian getYaw(bool reprojectAxis = true) const;
  192. /// Equality with tolerance (tolerance is max angle difference)
  193. bool equals(const Quaternion& rhs, const Radian& tolerance) const;
  194. // spherical linear interpolation
  195. static Quaternion Slerp (float fT, const Quaternion& rkP,
  196. const Quaternion& rkQ, bool shortestPath = false);
  197. static Quaternion SlerpExtraSpins (float fT,
  198. const Quaternion& rkP, const Quaternion& rkQ,
  199. int iExtraSpins);
  200. // setup for spherical quadratic interpolation
  201. static void Intermediate (const Quaternion& rkQ0,
  202. const Quaternion& rkQ1, const Quaternion& rkQ2,
  203. Quaternion& rka, Quaternion& rkB);
  204. // spherical quadratic interpolation
  205. static Quaternion Squad (float fT, const Quaternion& rkP,
  206. const Quaternion& rkA, const Quaternion& rkB,
  207. const Quaternion& rkQ, bool shortestPath = false);
  208. // normalised linear interpolation - faster but less accurate (non-constant rotation velocity)
  209. static Quaternion nlerp(float fT, const Quaternion& rkP,
  210. const Quaternion& rkQ, bool shortestPath = false);
  211. // cutoff for sine near zero
  212. static const float ms_fEpsilon;
  213. // special values
  214. static const Quaternion ZERO;
  215. static const Quaternion IDENTITY;
  216. float w, x, y, z;
  217. /// Check whether this quaternion contains valid values
  218. inline bool isNaN() const
  219. {
  220. return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w);
  221. }
  222. /** Function for writing to a stream. Outputs "Quaternion(w, x, y, z)" with w,x,y,z
  223. being the member values of the quaternion.
  224. */
  225. inline CM_UTILITY_EXPORT friend std::ostream& operator <<
  226. ( std::ostream& o, const Quaternion& q )
  227. {
  228. o << "Quaternion(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")";
  229. return o;
  230. }
  231. };
  232. /** @} */
  233. /** @} */
  234. }
  235. #endif