Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Quaternion.h 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*!
  2. * \file include/Quaternion.h
  3. * \brief Quaternion
  4. *
  5. * \author Mongoose
  6. */
  7. #ifndef _QUATERNION_H_
  8. #define _QUATERNION_H_
  9. #include "utils/math.h"
  10. /*!
  11. * \brief Quaternion
  12. */
  13. class Quaternion {
  14. public:
  15. /*!
  16. * \brief Constructs an object of Quaternion
  17. */
  18. Quaternion();
  19. /*!
  20. * \brief Constructs an object of Quaternion
  21. * \param w W part of new Quaternion
  22. * \param x X part of new Quaternion
  23. * \param y Y part of new Quaternion
  24. * \param z Z part of new Quaternion
  25. */
  26. Quaternion(vec_t w, vec_t x, vec_t y, vec_t z);
  27. /*!
  28. * \brief Constructs an object of Quaternion
  29. * \param v contents of new Quaternion
  30. */
  31. Quaternion(vec4_t v);
  32. /*!
  33. * \brief Deconstructs an object of Quaternion
  34. */
  35. ~Quaternion();
  36. /*!
  37. * \brief Get column order matrix equivalent of this quaternion
  38. * \param m where matrix will be stored
  39. */
  40. void getMatrix(matrix_t m);
  41. /*!
  42. * \brief Assign q to this quaternion
  43. * \param q what to assign this quaternion to
  44. * \returns this quaternion
  45. */
  46. Quaternion &operator =(const Quaternion &q);
  47. /*!
  48. * \brief Multiplies this quaternion.
  49. *
  50. * Use normalize() call for unit quaternion.
  51. *
  52. * \param q what to multiply this quaternion with
  53. * \returns resultant quaternion
  54. * \sa Quaternion::normalize()
  55. */
  56. Quaternion operator *(const Quaternion &q);
  57. /*!
  58. * \brief Divide from this quaternion
  59. * \param q what to divide from this quaternion
  60. * \returns resultant quaternion
  61. */
  62. Quaternion operator /(const Quaternion &q);
  63. /*!
  64. * \brief Add to this quaternion
  65. * \param q what to add to this quaternion
  66. * \returns resultant quaternion
  67. */
  68. Quaternion operator +(const Quaternion &q);
  69. /*!
  70. * \brief Subtract from this quaternion
  71. * \param q what to subtract from this quaternion
  72. * \returns resultant quaternion
  73. */
  74. Quaternion operator -(const Quaternion &q);
  75. /*!
  76. * \brief Compares q to this quaternion
  77. * \param q what to compare this quaternion to
  78. * \returns true if equal, false otherwise
  79. */
  80. bool operator ==(const Quaternion &q);
  81. /*!
  82. * \brief Conjugate this quaternion
  83. * \returns Conjugate of this quaternion
  84. */
  85. Quaternion conjugate();
  86. /*!
  87. * \brief Scale this quaternion
  88. * \param s scaling factor
  89. * \returns Scaled result of this quaternion
  90. */
  91. Quaternion scale(vec_t s);
  92. /*!
  93. * \brief Inverse this quaternion
  94. * \returns inverse of this quaternion
  95. */
  96. Quaternion inverse();
  97. /*!
  98. * \brief Dot Product of quaternions
  99. * \param a first argument to dot product
  100. * \param b second argument to dot product
  101. * \returns dot product between a and b quaternions
  102. */
  103. static vec_t dot(Quaternion a, Quaternion b);
  104. /*!
  105. * \brief Magnitude of this quaternion
  106. * \returns Magnitude of this quaternion
  107. */
  108. vec_t magnitude();
  109. /*!
  110. * \brief Interpolates between a and b rotations.
  111. *
  112. * Using spherical linear interpolation:
  113. * `I = (((B . A)^-1)^Time)A`
  114. *
  115. * \param a first argument for slerp
  116. * \param b second argument for slerp
  117. * \param time time argument for slerp
  118. * \returns resultant quaternion
  119. */
  120. static Quaternion slerp(Quaternion a, Quaternion b, vec_t time);
  121. /*!
  122. * \brief Sets this quaternion to identity
  123. */
  124. void setIdentity();
  125. /*!
  126. * \brief Sets this quaternion
  127. * \param angle new angle
  128. * \param x new X coordinate
  129. * \param y new Y coordinate
  130. * \param z new Z coordinate
  131. */
  132. void set(vec_t angle, vec_t x, vec_t y, vec_t z);
  133. /*!
  134. * \brief Normalize this quaternion
  135. */
  136. void normalize();
  137. /*!
  138. * \brief Set this quaternion
  139. * \param q will be copied into this quaternion
  140. */
  141. void copy(Quaternion q);
  142. /*!
  143. * \brief Sets matrix equivalent of this quaternion
  144. * \param m matrix in valid column order
  145. */
  146. void setByMatrix(matrix_t m);
  147. private:
  148. /*!
  149. * \brief Multiplies two quaternions
  150. * \param a first argument to multiplication
  151. * \param b second argument to multiplication
  152. * \returns resultant quaternion
  153. */
  154. static Quaternion multiply(Quaternion a, Quaternion b);
  155. /*!
  156. * \brief Divides B from A quaternion
  157. * \param a first argument to division
  158. * \param b second argument to division
  159. * \returns quotient quaternion
  160. */
  161. static Quaternion divide(Quaternion a, Quaternion b);
  162. /*!
  163. * \brief Adds A and B quaternions
  164. * \param a first argument to addition
  165. * \param b second argument to addition
  166. * \returns resultant quaternion
  167. */
  168. static Quaternion add(Quaternion a, Quaternion b);
  169. /*!
  170. * \brief Subtracts B from A quaternion
  171. * \param a first argument to subtraction
  172. * \param b second argument to subtraction
  173. * \returns resultant quaternion
  174. */
  175. static Quaternion subtract(Quaternion a, Quaternion b);
  176. vec_t mW; //!< Quaternion, W part
  177. vec_t mX; //!< Quaternion, X part
  178. vec_t mY; //!< Quaternion, Y part
  179. vec_t mZ; //!< Quaternion, Z part
  180. };
  181. #endif