BsQuaternion.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisitesUtil.h"
  5. #include "BsMath.h"
  6. #include "BsVector3.h"
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup Math
  10. * @{
  11. */
  12. /** Represents a quaternion used for 3D rotations. */
  13. class BS_UTILITY_EXPORT Quaternion
  14. {
  15. private:
  16. struct EulerAngleOrderData
  17. {
  18. int a, b, c;
  19. };
  20. public:
  21. Quaternion(float w = 1.0f, float x = 0.0f, float y = 0.0f, float z = 0.0f)
  22. :x(x), y(y), z(z), w(w)
  23. { }
  24. /** Construct a quaternion from a rotation matrix. */
  25. explicit Quaternion(const Matrix3& rot)
  26. {
  27. fromRotationMatrix(rot);
  28. }
  29. /** Construct a quaternion from an angle/axis. */
  30. explicit Quaternion(const Vector3& axis, const Radian& angle)
  31. {
  32. fromAxisAngle(axis, angle);
  33. }
  34. /** Construct a quaternion from 3 orthonormal local axes. */
  35. explicit Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
  36. {
  37. fromAxes(xaxis, yaxis, zaxis);
  38. }
  39. /**
  40. * Construct a quaternion from euler angles, YXZ ordering.
  41. *
  42. * @see Quaternion::fromEulerAngles
  43. */
  44. explicit Quaternion(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle)
  45. {
  46. fromEulerAngles(xAngle, yAngle, zAngle);
  47. }
  48. /**
  49. * Construct a quaternion from euler angles, custom ordering.
  50. *
  51. * @see Quaternion::fromEulerAngles
  52. */
  53. explicit Quaternion(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle, EulerAngleOrder order)
  54. {
  55. fromEulerAngles(xAngle, yAngle, zAngle, order);
  56. }
  57. /** Exchange the contents of this quaternion with another. */
  58. void swap(Quaternion& other)
  59. {
  60. std::swap(w, other.w);
  61. std::swap(x, other.x);
  62. std::swap(y, other.y);
  63. std::swap(z, other.z);
  64. }
  65. float operator[] (const size_t i) const
  66. {
  67. assert(i < 4);
  68. return *(&w+i);
  69. }
  70. float& operator[] (const size_t i)
  71. {
  72. assert(i < 4);
  73. return *(&w+i);
  74. }
  75. /**
  76. * Initializes the quaternion from a 3x3 rotation matrix.
  77. *
  78. * @note It's up to the caller to ensure the matrix is orthonormal.
  79. */
  80. void fromRotationMatrix(const Matrix3& mat);
  81. /**
  82. * Initializes the quaternion from an angle axis pair. Quaternion will represent a rotation of "angle" radians
  83. * around "axis".
  84. */
  85. void fromAxisAngle(const Vector3& axis, const Radian& angle);
  86. /**
  87. * Initializes the quaternion from orthonormal set of axes. Quaternion will represent a rotation from base axes
  88. * to the specified set of axes.
  89. *
  90. * @note It's up to the caller to ensure the axes are orthonormal.
  91. */
  92. void fromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
  93. /**
  94. * Creates a quaternion from the provided Pitch/Yaw/Roll angles.
  95. *
  96. * @param[in] xAngle Rotation about x axis. (AKA Pitch)
  97. * @param[in] yAngle Rotation about y axis. (AKA Yaw)
  98. * @param[in] zAngle Rotation about z axis. (AKA Roll)
  99. *
  100. * @note
  101. * Since different values will be produced depending in which order are the rotations applied, this method assumes
  102. * they are applied in YXZ order. If you need a specific order, use the overloaded fromEulerAngles() method instead.
  103. */
  104. void fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle);
  105. /**
  106. * Creates a quaternion from the provided Pitch/Yaw/Roll angles.
  107. *
  108. * @param[in] xAngle Rotation about x axis. (AKA Pitch)
  109. * @param[in] yAngle Rotation about y axis. (AKA Yaw)
  110. * @param[in] zAngle Rotation about z axis. (AKA Roll)
  111. * @param[in] order The order in which rotations will be extracted. Different values can be retrieved depending
  112. * on the order.
  113. */
  114. void fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle, EulerAngleOrder order);
  115. /**
  116. * Converts a quaternion to a rotation matrix.
  117. */
  118. void toRotationMatrix(Matrix3& mat) const;
  119. /**
  120. * Converts a quaternion to an angle axis pair.
  121. *
  122. * @param[out] axis The axis around the which rotation takes place.
  123. * @param[out] angle The angle in radians determining amount of rotation around the axis.
  124. */
  125. void toAxisAngle(Vector3& axis, Radian& angle) const;
  126. /**
  127. * Converts a quaternion to an orthonormal set of axes.
  128. *
  129. * @param[out] xAxis The X axis.
  130. * @param[out] yAxis The Y axis.
  131. * @param[out] zAxis The Z axis.
  132. */
  133. void toAxes(Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const;
  134. /**
  135. * Extracts Pitch/Yaw/Roll rotations from this quaternion.
  136. *
  137. * @param[out] xAngle Rotation about x axis. (AKA Pitch)
  138. * @param[out] yAngle Rotation about y axis. (AKA Yaw)
  139. * @param[out] zAngle Rotation about z axis. (AKA Roll)
  140. *
  141. * @return True if unique solution was found, false otherwise.
  142. */
  143. bool toEulerAngles(Radian& xAngle, Radian& yAngle, Radian& zAngle) const;
  144. /** Gets the positive x-axis of the coordinate system transformed by this quaternion. */
  145. Vector3 xAxis() const;
  146. /** Gets the positive y-axis of the coordinate system transformed by this quaternion. */
  147. Vector3 yAxis() const;
  148. /** Gets the positive z-axis of the coordinate system transformed by this quaternion. */
  149. Vector3 zAxis() const;
  150. Quaternion& operator= (const Quaternion& rhs)
  151. {
  152. w = rhs.w;
  153. x = rhs.x;
  154. y = rhs.y;
  155. z = rhs.z;
  156. return *this;
  157. }
  158. Quaternion operator+ (const Quaternion& rhs) const;
  159. Quaternion operator- (const Quaternion& rhs) const;
  160. Quaternion operator* (const Quaternion& rhs) const;
  161. Quaternion operator* (float rhs) const;
  162. Quaternion operator- () const;
  163. bool operator== (const Quaternion& rhs) const
  164. {
  165. return (rhs.x == x) && (rhs.y == y) && (rhs.z == z) && (rhs.w == w);
  166. }
  167. bool operator!= (const Quaternion& rhs) const
  168. {
  169. return !operator==(rhs);
  170. }
  171. friend Quaternion operator* (float lhs, const Quaternion& rhs);
  172. /** Calculates the dot product of this quaternion and another. */
  173. float dot(const Quaternion& other) const;
  174. /** Normalizes this quaternion, and returns the previous length. */
  175. float normalize();
  176. /**
  177. * Gets the inverse.
  178. *
  179. * @note Quaternion must be non-zero.
  180. */
  181. Quaternion inverse() const;
  182. /** Rotates the provided vector. */
  183. Vector3 rotate(const Vector3& vec) const;
  184. /**
  185. * Orients the quaternion so its negative z axis points to the provided direction.
  186. *
  187. * @param[in] forwardDir Direction to orient towards.
  188. */
  189. void lookRotation(const Vector3& forwardDir);
  190. /**
  191. * Orients the quaternion so its negative z axis points to the provided direction.
  192. *
  193. * @param[in] forwardDir Direction to orient towards.
  194. * @param[in] upDir Constrains y axis orientation to a plane this vector lies on. This rule might be broken
  195. * if forward and up direction are nearly parallel.
  196. */
  197. void lookRotation(const Vector3& forwardDir, const Vector3& upDir);
  198. /** Query if any of the components of the quaternion are not a number. */
  199. bool isNaN() const
  200. {
  201. return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w);
  202. }
  203. /**
  204. * Performs spherical interpolation between two quaternions. Spherical interpolation neatly interpolates between
  205. * two rotations without modifying the size of the vector it is applied to (unlike linear interpolation).
  206. */
  207. static Quaternion slerp(float t, const Quaternion& p,
  208. const Quaternion& q, bool shortestPath = true);
  209. /** Gets the shortest arc quaternion to rotate this vector to the destination vector. */
  210. static Quaternion getRotationFromTo(const Vector3& from, const Vector3& dest, const Vector3& fallbackAxis = Vector3::ZERO);
  211. static const float EPSILON;
  212. static const Quaternion ZERO;
  213. static const Quaternion IDENTITY;
  214. float x, y, z, w;
  215. private:
  216. static const EulerAngleOrderData EA_LOOKUP[6];
  217. };
  218. /** @} */
  219. /** @cond SPECIALIZATIONS */
  220. BS_ALLOW_MEMCPY_SERIALIZATION(Quaternion);
  221. /** @endcond */
  222. }