BsQuaternion.h 8.4 KB

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