quaternion.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// Restrictions:
  16. /// By making use of the Software for military purposes, you choose to make
  17. /// a Bunny unhappy.
  18. ///
  19. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. /// THE SOFTWARE.
  26. ///
  27. /// @ref gtc_quaternion
  28. /// @file glm/gtc/quaternion.hpp
  29. /// @date 2009-05-21 / 2012-12-20
  30. /// @author Christophe Riccio
  31. ///
  32. /// @see core (dependence)
  33. /// @see gtc_half_float (dependence)
  34. /// @see gtc_constants (dependence)
  35. ///
  36. /// @defgroup gtc_quaternion GLM_GTC_quaternion
  37. /// @ingroup gtc
  38. ///
  39. /// @brief Defines a templated quaternion type and several quaternion operations.
  40. ///
  41. /// <glm/gtc/quaternion.hpp> need to be included to use these functionalities.
  42. ///////////////////////////////////////////////////////////////////////////////////
  43. #pragma once
  44. // Dependency:
  45. #include "../mat3x3.hpp"
  46. #include "../mat4x4.hpp"
  47. #include "../vec3.hpp"
  48. #include "../vec4.hpp"
  49. #include "../gtc/constants.hpp"
  50. #if(defined(GLM_MESSAGES) && !defined(GLM_EXT_INCLUDED))
  51. # pragma message("GLM: GLM_GTC_quaternion extension included")
  52. #endif
  53. namespace glm
  54. {
  55. /// @addtogroup gtc_quaternion
  56. /// @{
  57. template <typename T, precision P>
  58. struct tquat
  59. {
  60. typedef T value_type;
  61. typedef tvec4<bool, P> bool_type;
  62. public:
  63. T x, y, z, w;
  64. #if GLM_FORCE_SIZE_FUNC
  65. /// Return the count of components of a quaternion
  66. GLM_FUNC_DECL GLM_CONSTEXPR size_t size() const;
  67. #else
  68. /// Return the count of components of a quaternion
  69. GLM_FUNC_DECL GLM_CONSTEXPR length_t length() const;
  70. #endif//GLM_FORCE_SIZE_FUNC
  71. //////////////////////////////////////
  72. // Accesses
  73. GLM_FUNC_DECL T & operator[](length_t i);
  74. GLM_FUNC_DECL T const & operator[](length_t i) const;
  75. //////////////////////////////////////
  76. // Implicit basic constructors
  77. GLM_FUNC_DECL tquat();
  78. template <precision Q>
  79. GLM_FUNC_DECL tquat(tquat<T, Q> const & q);
  80. //////////////////////////////////////
  81. // Explicit basic constructors
  82. GLM_FUNC_DECL explicit tquat(ctor);
  83. GLM_FUNC_DECL explicit tquat(T const & s, tvec3<T, P> const & v);
  84. GLM_FUNC_DECL tquat(T const & w, T const & x, T const & y, T const & z);
  85. //////////////////////////////////////
  86. // Convertions
  87. # ifdef GLM_FORCE_EXPLICIT_CTOR
  88. template <typename U, precision Q>
  89. GLM_FUNC_DECL explicit tquat(tquat<U, Q> const & q);
  90. # else
  91. template <typename U, precision Q>
  92. GLM_FUNC_DECL tquat(tquat<U, Q> const & q);
  93. # endif
  94. // explicit conversion operators
  95. GLM_FUNC_DECL explicit operator tmat3x3<T, P>();
  96. GLM_FUNC_DECL explicit operator tmat4x4<T, P>();
  97. /// Create a quaternion from two normalized axis
  98. ///
  99. /// @param u A first normalized axis
  100. /// @param v A second normalized axis
  101. /// @see gtc_quaternion
  102. /// @see http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors
  103. GLM_FUNC_DECL explicit tquat(tvec3<T, P> const & u, tvec3<T, P> const & v);
  104. /// Build a quaternion from euler angles (pitch, yaw, roll), in radians.
  105. GLM_FUNC_DECL explicit tquat(tvec3<T, P> const & eulerAngles);
  106. GLM_FUNC_DECL explicit tquat(tmat3x3<T, P> const & m);
  107. GLM_FUNC_DECL explicit tquat(tmat4x4<T, P> const & m);
  108. //////////////////////////////////////
  109. // Operators
  110. GLM_FUNC_DECL tquat<T, P> & operator+=(tquat<T, P> const & q);
  111. GLM_FUNC_DECL tquat<T, P> & operator*=(tquat<T, P> const & q);
  112. GLM_FUNC_DECL tquat<T, P> & operator*=(T const & s);
  113. GLM_FUNC_DECL tquat<T, P> & operator/=(T const & s);
  114. };
  115. template <typename T, precision P>
  116. GLM_FUNC_DECL tquat<T, P> operator-(tquat<T, P> const & q);
  117. template <typename T, precision P>
  118. GLM_FUNC_DECL tquat<T, P> operator+(tquat<T, P> const & q, tquat<T, P> const & p);
  119. template <typename T, precision P>
  120. GLM_FUNC_DECL tquat<T, P> operator*(tquat<T, P> const & q, tquat<T, P> const & p);
  121. template <typename T, precision P>
  122. GLM_FUNC_DECL tvec3<T, P> operator*(tquat<T, P> const & q, tvec3<T, P> const & v);
  123. template <typename T, precision P>
  124. GLM_FUNC_DECL tvec3<T, P> operator*(tvec3<T, P> const & v, tquat<T, P> const & q);
  125. template <typename T, precision P>
  126. GLM_FUNC_DECL tvec4<T, P> operator*(tquat<T, P> const & q, tvec4<T, P> const & v);
  127. template <typename T, precision P>
  128. GLM_FUNC_DECL tvec4<T, P> operator*(tvec4<T, P> const & v, tquat<T, P> const & q);
  129. template <typename T, precision P>
  130. GLM_FUNC_DECL tquat<T, P> operator*(tquat<T, P> const & q, T const & s);
  131. template <typename T, precision P>
  132. GLM_FUNC_DECL tquat<T, P> operator*(T const & s, tquat<T, P> const & q);
  133. template <typename T, precision P>
  134. GLM_FUNC_DECL tquat<T, P> operator/(tquat<T, P> const & q, T const & s);
  135. /// Returns the length of the quaternion.
  136. ///
  137. /// @see gtc_quaternion
  138. template <typename T, precision P>
  139. GLM_FUNC_DECL T length(tquat<T, P> const & q);
  140. /// Returns the normalized quaternion.
  141. ///
  142. /// @see gtc_quaternion
  143. template <typename T, precision P>
  144. GLM_FUNC_DECL tquat<T, P> normalize(tquat<T, P> const & q);
  145. /// Returns dot product of q1 and q2, i.e., q1[0] * q2[0] + q1[1] * q2[1] + ...
  146. ///
  147. /// @see gtc_quaternion
  148. template <typename T, precision P, template <typename, precision> class quatType>
  149. GLM_FUNC_DECL T dot(quatType<T, P> const & x, quatType<T, P> const & y);
  150. /// Spherical linear interpolation of two quaternions.
  151. /// The interpolation is oriented and the rotation is performed at constant speed.
  152. /// For short path spherical linear interpolation, use the slerp function.
  153. ///
  154. /// @param x A quaternion
  155. /// @param y A quaternion
  156. /// @param a Interpolation factor. The interpolation is defined beyond the range [0, 1].
  157. /// @tparam T Value type used to build the quaternion. Supported: half, float or double.
  158. /// @see gtc_quaternion
  159. /// @see - slerp(tquat<T, P> const & x, tquat<T, P> const & y, T const & a)
  160. template <typename T, precision P>
  161. GLM_FUNC_DECL tquat<T, P> mix(tquat<T, P> const & x, tquat<T, P> const & y, T const & a);
  162. /// Linear interpolation of two quaternions.
  163. /// The interpolation is oriented.
  164. ///
  165. /// @param x A quaternion
  166. /// @param y A quaternion
  167. /// @param a Interpolation factor. The interpolation is defined in the range [0, 1].
  168. /// @tparam T Value type used to build the quaternion. Supported: half, float or double.
  169. /// @see gtc_quaternion
  170. template <typename T, precision P>
  171. GLM_FUNC_DECL tquat<T, P> lerp(tquat<T, P> const & x, tquat<T, P> const & y, T const & a);
  172. /// Spherical linear interpolation of two quaternions.
  173. /// The interpolation always take the short path and the rotation is performed at constant speed.
  174. ///
  175. /// @param x A quaternion
  176. /// @param y A quaternion
  177. /// @param a Interpolation factor. The interpolation is defined beyond the range [0, 1].
  178. /// @tparam T Value type used to build the quaternion. Supported: half, float or double.
  179. /// @see gtc_quaternion
  180. template <typename T, precision P>
  181. GLM_FUNC_DECL tquat<T, P> slerp(tquat<T, P> const & x, tquat<T, P> const & y, T const & a);
  182. /// Returns the q conjugate.
  183. ///
  184. /// @see gtc_quaternion
  185. template <typename T, precision P>
  186. GLM_FUNC_DECL tquat<T, P> conjugate(tquat<T, P> const & q);
  187. /// Returns the q inverse.
  188. ///
  189. /// @see gtc_quaternion
  190. template <typename T, precision P>
  191. GLM_FUNC_DECL tquat<T, P> inverse(tquat<T, P> const & q);
  192. /// Rotates a quaternion from a vector of 3 components axis and an angle.
  193. ///
  194. /// @param q Source orientation
  195. /// @param angle Angle expressed in radians.
  196. /// @param axis Axis of the rotation
  197. ///
  198. /// @see gtc_quaternion
  199. template <typename T, precision P>
  200. GLM_FUNC_DECL tquat<T, P> rotate(tquat<T, P> const & q, T const & angle, tvec3<T, P> const & axis);
  201. /// Returns euler angles, yitch as x, yaw as y, roll as z.
  202. /// The result is expressed in radians if GLM_FORCE_RADIANS is defined or degrees otherwise.
  203. ///
  204. /// @see gtc_quaternion
  205. template <typename T, precision P>
  206. GLM_FUNC_DECL tvec3<T, P> eulerAngles(tquat<T, P> const & x);
  207. /// Returns roll value of euler angles expressed in radians.
  208. ///
  209. /// @see gtx_quaternion
  210. template <typename T, precision P>
  211. GLM_FUNC_DECL T roll(tquat<T, P> const & x);
  212. /// Returns pitch value of euler angles expressed in radians.
  213. ///
  214. /// @see gtx_quaternion
  215. template <typename T, precision P>
  216. GLM_FUNC_DECL T pitch(tquat<T, P> const & x);
  217. /// Returns yaw value of euler angles expressed in radians.
  218. ///
  219. /// @see gtx_quaternion
  220. template <typename T, precision P>
  221. GLM_FUNC_DECL T yaw(tquat<T, P> const & x);
  222. /// Converts a quaternion to a 3 * 3 matrix.
  223. ///
  224. /// @see gtc_quaternion
  225. template <typename T, precision P>
  226. GLM_FUNC_DECL tmat3x3<T, P> mat3_cast(tquat<T, P> const & x);
  227. /// Converts a quaternion to a 4 * 4 matrix.
  228. ///
  229. /// @see gtc_quaternion
  230. template <typename T, precision P>
  231. GLM_FUNC_DECL tmat4x4<T, P> mat4_cast(tquat<T, P> const & x);
  232. /// Converts a 3 * 3 matrix to a quaternion.
  233. ///
  234. /// @see gtc_quaternion
  235. template <typename T, precision P>
  236. GLM_FUNC_DECL tquat<T, P> quat_cast(tmat3x3<T, P> const & x);
  237. /// Converts a 4 * 4 matrix to a quaternion.
  238. ///
  239. /// @see gtc_quaternion
  240. template <typename T, precision P>
  241. GLM_FUNC_DECL tquat<T, P> quat_cast(tmat4x4<T, P> const & x);
  242. /// Returns the quaternion rotation angle.
  243. ///
  244. /// @see gtc_quaternion
  245. template <typename T, precision P>
  246. GLM_FUNC_DECL T angle(tquat<T, P> const & x);
  247. /// Returns the q rotation axis.
  248. ///
  249. /// @see gtc_quaternion
  250. template <typename T, precision P>
  251. GLM_FUNC_DECL tvec3<T, P> axis(tquat<T, P> const & x);
  252. /// Build a quaternion from an angle and a normalized axis.
  253. ///
  254. /// @param angle Angle expressed in radians.
  255. /// @param axis Axis of the quaternion, must be normalized.
  256. ///
  257. /// @see gtc_quaternion
  258. template <typename T, precision P>
  259. GLM_FUNC_DECL tquat<T, P> angleAxis(T const & angle, tvec3<T, P> const & axis);
  260. /// Returns the component-wise comparison result of x < y.
  261. ///
  262. /// @tparam quatType Floating-point quaternion types.
  263. ///
  264. /// @see gtc_quaternion
  265. template <typename T, precision P>
  266. GLM_FUNC_DECL tvec4<bool, P> lessThan(tquat<T, P> const & x, tquat<T, P> const & y);
  267. /// Returns the component-wise comparison of result x <= y.
  268. ///
  269. /// @tparam quatType Floating-point quaternion types.
  270. ///
  271. /// @see gtc_quaternion
  272. template <typename T, precision P>
  273. GLM_FUNC_DECL tvec4<bool, P> lessThanEqual(tquat<T, P> const & x, tquat<T, P> const & y);
  274. /// Returns the component-wise comparison of result x > y.
  275. ///
  276. /// @tparam quatType Floating-point quaternion types.
  277. ///
  278. /// @see gtc_quaternion
  279. template <typename T, precision P>
  280. GLM_FUNC_DECL tvec4<bool, P> greaterThan(tquat<T, P> const & x, tquat<T, P> const & y);
  281. /// Returns the component-wise comparison of result x >= y.
  282. ///
  283. /// @tparam quatType Floating-point quaternion types.
  284. ///
  285. /// @see gtc_quaternion
  286. template <typename T, precision P>
  287. GLM_FUNC_DECL tvec4<bool, P> greaterThanEqual(tquat<T, P> const & x, tquat<T, P> const & y);
  288. /// Returns the component-wise comparison of result x == y.
  289. ///
  290. /// @tparam quatType Floating-point quaternion types.
  291. ///
  292. /// @see gtc_quaternion
  293. template <typename T, precision P>
  294. GLM_FUNC_DECL tvec4<bool, P> equal(tquat<T, P> const & x, tquat<T, P> const & y);
  295. /// Returns the component-wise comparison of result x != y.
  296. ///
  297. /// @tparam quatType Floating-point quaternion types.
  298. ///
  299. /// @see gtc_quaternion
  300. template <typename T, precision P>
  301. GLM_FUNC_DECL tvec4<bool, P> notEqual(tquat<T, P> const & x, tquat<T, P> const & y);
  302. /// @}
  303. } //namespace glm
  304. #include "quaternion.inl"