Collection of DPF-based plugins for packaging
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.

438 lines
13KB

  1. /// @ref gtc_quaternion
  2. /// @file glm/gtc/quaternion.hpp
  3. ///
  4. /// @see core (dependence)
  5. /// @see gtc_constants (dependence)
  6. ///
  7. /// @defgroup gtc_quaternion GLM_GTC_quaternion
  8. /// @ingroup gtc
  9. ///
  10. /// Include <glm/gtc/quaternion.hpp> to use the features of this extension.
  11. ///
  12. /// Defines a templated quaternion type and several quaternion operations.
  13. #pragma once
  14. // Dependency:
  15. #include "../mat3x3.hpp"
  16. #include "../mat4x4.hpp"
  17. #include "../vec3.hpp"
  18. #include "../vec4.hpp"
  19. #include "../gtc/constants.hpp"
  20. #if GLM_MESSAGES == GLM_MESSAGES_ENABLED && !defined(GLM_EXT_INCLUDED)
  21. # pragma message("GLM: GLM_GTC_quaternion extension included")
  22. #endif
  23. namespace glm
  24. {
  25. /// @addtogroup gtc_quaternion
  26. /// @{
  27. template<typename T, qualifier Q = defaultp>
  28. struct tquat
  29. {
  30. // -- Implementation detail --
  31. typedef tquat<T, Q> type;
  32. typedef T value_type;
  33. // -- Data --
  34. # if GLM_HAS_ALIGNED_TYPE
  35. # if GLM_COMPILER & GLM_COMPILER_GCC
  36. # pragma GCC diagnostic push
  37. # pragma GCC diagnostic ignored "-Wpedantic"
  38. # endif
  39. # if GLM_COMPILER & GLM_COMPILER_CLANG
  40. # pragma clang diagnostic push
  41. # pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
  42. # pragma clang diagnostic ignored "-Wnested-anon-types"
  43. # endif
  44. union
  45. {
  46. struct { T x, y, z, w;};
  47. typename detail::storage<T, sizeof(T) * 4, detail::is_aligned<Q>::value>::type data;
  48. };
  49. # if GLM_COMPILER & GLM_COMPILER_CLANG
  50. # pragma clang diagnostic pop
  51. # endif
  52. # if GLM_COMPILER & GLM_COMPILER_GCC
  53. # pragma GCC diagnostic pop
  54. # endif
  55. # else
  56. T x, y, z, w;
  57. # endif
  58. // -- Component accesses --
  59. typedef length_t length_type;
  60. /// Return the count of components of a quaternion
  61. GLM_FUNC_DECL static GLM_CONSTEXPR length_type length(){return 4;}
  62. GLM_FUNC_DECL T & operator[](length_type i);
  63. GLM_FUNC_DECL T const& operator[](length_type i) const;
  64. // -- Implicit basic constructors --
  65. GLM_FUNC_DECL GLM_CONSTEXPR tquat() GLM_DEFAULT_CTOR;
  66. GLM_FUNC_DECL GLM_CONSTEXPR tquat(tquat<T, Q> const& q) GLM_DEFAULT;
  67. template<qualifier P>
  68. GLM_FUNC_DECL GLM_CONSTEXPR tquat(tquat<T, P> const& q);
  69. // -- Explicit basic constructors --
  70. GLM_FUNC_DECL GLM_CONSTEXPR tquat(T s, vec<3, T, Q> const& v);
  71. GLM_FUNC_DECL GLM_CONSTEXPR tquat(T w, T x, T y, T z);
  72. // -- Conversion constructors --
  73. template<typename U, qualifier P>
  74. GLM_FUNC_DECL GLM_CONSTEXPR GLM_EXPLICIT tquat(tquat<U, P> const& q);
  75. /// Explicit conversion operators
  76. # if GLM_HAS_EXPLICIT_CONVERSION_OPERATORS
  77. GLM_FUNC_DECL explicit operator mat<3, 3, T, Q>();
  78. GLM_FUNC_DECL explicit operator mat<4, 4, T, Q>();
  79. # endif
  80. /// Create a quaternion from two normalized axis
  81. ///
  82. /// @param u A first normalized axis
  83. /// @param v A second normalized axis
  84. /// @see gtc_quaternion
  85. /// @see http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors
  86. GLM_FUNC_DECL tquat(vec<3, T, Q> const& u, vec<3, T, Q> const& v);
  87. /// Build a quaternion from euler angles (pitch, yaw, roll), in radians.
  88. GLM_FUNC_DECL GLM_EXPLICIT tquat(vec<3, T, Q> const& eulerAngles);
  89. GLM_FUNC_DECL GLM_EXPLICIT tquat(mat<3, 3, T, Q> const& q);
  90. GLM_FUNC_DECL GLM_EXPLICIT tquat(mat<4, 4, T, Q> const& q);
  91. // -- Unary arithmetic operators --
  92. GLM_FUNC_DECL tquat<T, Q> & operator=(tquat<T, Q> const& q) GLM_DEFAULT;
  93. template<typename U>
  94. GLM_FUNC_DECL tquat<T, Q> & operator=(tquat<U, Q> const& q);
  95. template<typename U>
  96. GLM_FUNC_DECL tquat<T, Q> & operator+=(tquat<U, Q> const& q);
  97. template<typename U>
  98. GLM_FUNC_DECL tquat<T, Q> & operator-=(tquat<U, Q> const& q);
  99. template<typename U>
  100. GLM_FUNC_DECL tquat<T, Q> & operator*=(tquat<U, Q> const& q);
  101. template<typename U>
  102. GLM_FUNC_DECL tquat<T, Q> & operator*=(U s);
  103. template<typename U>
  104. GLM_FUNC_DECL tquat<T, Q> & operator/=(U s);
  105. };
  106. // -- Unary bit operators --
  107. template<typename T, qualifier Q>
  108. GLM_FUNC_DECL tquat<T, Q> operator+(tquat<T, Q> const& q);
  109. template<typename T, qualifier Q>
  110. GLM_FUNC_DECL tquat<T, Q> operator-(tquat<T, Q> const& q);
  111. // -- Binary operators --
  112. template<typename T, qualifier Q>
  113. GLM_FUNC_DECL tquat<T, Q> operator+(tquat<T, Q> const& q, tquat<T, Q> const& p);
  114. template<typename T, qualifier Q>
  115. GLM_FUNC_DECL tquat<T, Q> operator-(tquat<T, Q> const& q, tquat<T, Q> const& p);
  116. template<typename T, qualifier Q>
  117. GLM_FUNC_DECL tquat<T, Q> operator*(tquat<T, Q> const& q, tquat<T, Q> const& p);
  118. template<typename T, qualifier Q>
  119. GLM_FUNC_DECL vec<3, T, Q> operator*(tquat<T, Q> const& q, vec<3, T, Q> const& v);
  120. template<typename T, qualifier Q>
  121. GLM_FUNC_DECL vec<3, T, Q> operator*(vec<3, T, Q> const& v, tquat<T, Q> const& q);
  122. template<typename T, qualifier Q>
  123. GLM_FUNC_DECL vec<4, T, Q> operator*(tquat<T, Q> const& q, vec<4, T, Q> const& v);
  124. template<typename T, qualifier Q>
  125. GLM_FUNC_DECL vec<4, T, Q> operator*(vec<4, T, Q> const& v, tquat<T, Q> const& q);
  126. template<typename T, qualifier Q>
  127. GLM_FUNC_DECL tquat<T, Q> operator*(tquat<T, Q> const& q, T const& s);
  128. template<typename T, qualifier Q>
  129. GLM_FUNC_DECL tquat<T, Q> operator*(T const& s, tquat<T, Q> const& q);
  130. template<typename T, qualifier Q>
  131. GLM_FUNC_DECL tquat<T, Q> operator/(tquat<T, Q> const& q, T const& s);
  132. // -- Boolean operators --
  133. template<typename T, qualifier Q>
  134. GLM_FUNC_DECL bool operator==(tquat<T, Q> const& q1, tquat<T, Q> const& q2);
  135. template<typename T, qualifier Q>
  136. GLM_FUNC_DECL bool operator!=(tquat<T, Q> const& q1, tquat<T, Q> const& q2);
  137. /// Returns the length of the quaternion.
  138. ///
  139. /// @tparam T Floating-point scalar types.
  140. ///
  141. /// @see gtc_quaternion
  142. template<typename T, qualifier Q>
  143. GLM_FUNC_DECL T length(tquat<T, Q> const& q);
  144. /// Returns the normalized quaternion.
  145. ///
  146. /// @tparam T Floating-point scalar types.
  147. ///
  148. /// @see gtc_quaternion
  149. template<typename T, qualifier Q>
  150. GLM_FUNC_DECL tquat<T, Q> normalize(tquat<T, Q> const& q);
  151. /// Returns dot product of q1 and q2, i.e., q1[0] * q2[0] + q1[1] * q2[1] + ...
  152. ///
  153. /// @tparam T Floating-point scalar types.
  154. ///
  155. /// @see gtc_quaternion
  156. template<typename T, qualifier Q>
  157. GLM_FUNC_DECL T dot(tquat<T, Q> const& x, tquat<T, Q> const& y);
  158. /// Spherical linear interpolation of two quaternions.
  159. /// The interpolation is oriented and the rotation is performed at constant speed.
  160. /// For short path spherical linear interpolation, use the slerp function.
  161. ///
  162. /// @param x A quaternion
  163. /// @param y A quaternion
  164. /// @param a Interpolation factor. The interpolation is defined beyond the range [0, 1].
  165. /// @tparam T Floating-point scalar types.
  166. ///
  167. /// @see - slerp(tquat<T, Q> const& x, tquat<T, Q> const& y, T const& a)
  168. /// @see gtc_quaternion
  169. template<typename T, qualifier Q>
  170. GLM_FUNC_DECL tquat<T, Q> mix(tquat<T, Q> const& x, tquat<T, Q> const& y, T a);
  171. /// Linear interpolation of two quaternions.
  172. /// The interpolation is oriented.
  173. ///
  174. /// @param x A quaternion
  175. /// @param y A quaternion
  176. /// @param a Interpolation factor. The interpolation is defined in the range [0, 1].
  177. /// @tparam T Floating-point scalar types.
  178. ///
  179. /// @see gtc_quaternion
  180. template<typename T, qualifier Q>
  181. GLM_FUNC_DECL tquat<T, Q> lerp(tquat<T, Q> const& x, tquat<T, Q> const& y, T a);
  182. /// Spherical linear interpolation of two quaternions.
  183. /// The interpolation always take the short path and the rotation is performed at constant speed.
  184. ///
  185. /// @param x A quaternion
  186. /// @param y A quaternion
  187. /// @param a Interpolation factor. The interpolation is defined beyond the range [0, 1].
  188. /// @tparam T Floating-point scalar types.
  189. ///
  190. /// @see gtc_quaternion
  191. template<typename T, qualifier Q>
  192. GLM_FUNC_DECL tquat<T, Q> slerp(tquat<T, Q> const& x, tquat<T, Q> const& y, T a);
  193. /// Returns the q conjugate.
  194. ///
  195. /// @tparam T Floating-point scalar types.
  196. ///
  197. /// @see gtc_quaternion
  198. template<typename T, qualifier Q>
  199. GLM_FUNC_DECL tquat<T, Q> conjugate(tquat<T, Q> const& q);
  200. /// Returns the q inverse.
  201. ///
  202. /// @tparam T Floating-point scalar types.
  203. ///
  204. /// @see gtc_quaternion
  205. template<typename T, qualifier Q>
  206. GLM_FUNC_DECL tquat<T, Q> inverse(tquat<T, Q> const& q);
  207. /// Rotates a quaternion from a vector of 3 components axis and an angle.
  208. ///
  209. /// @param q Source orientation
  210. /// @param angle Angle expressed in radians.
  211. /// @param axis Axis of the rotation
  212. /// @tparam T Floating-point scalar types.
  213. ///
  214. /// @see gtc_quaternion
  215. template<typename T, qualifier Q>
  216. GLM_FUNC_DECL tquat<T, Q> rotate(tquat<T, Q> const& q, T const& angle, vec<3, T, Q> const& axis);
  217. /// Returns euler angles, pitch as x, yaw as y, roll as z.
  218. /// The result is expressed in radians.
  219. ///
  220. /// @tparam T Floating-point scalar types.
  221. ///
  222. /// @see gtc_quaternion
  223. template<typename T, qualifier Q>
  224. GLM_FUNC_DECL vec<3, T, Q> eulerAngles(tquat<T, Q> const& x);
  225. /// Returns roll value of euler angles expressed in radians.
  226. ///
  227. /// @tparam T Floating-point scalar types.
  228. ///
  229. /// @see gtc_quaternion
  230. template<typename T, qualifier Q>
  231. GLM_FUNC_DECL T roll(tquat<T, Q> const& x);
  232. /// Returns pitch value of euler angles expressed in radians.
  233. ///
  234. /// @tparam T Floating-point scalar types.
  235. ///
  236. /// @see gtc_quaternion
  237. template<typename T, qualifier Q>
  238. GLM_FUNC_DECL T pitch(tquat<T, Q> const& x);
  239. /// Returns yaw value of euler angles expressed in radians.
  240. ///
  241. /// @tparam T Floating-point scalar types.
  242. ///
  243. /// @see gtc_quaternion
  244. template<typename T, qualifier Q>
  245. GLM_FUNC_DECL T yaw(tquat<T, Q> const& x);
  246. /// Converts a quaternion to a 3 * 3 matrix.
  247. ///
  248. /// @tparam T Floating-point scalar types.
  249. ///
  250. /// @see gtc_quaternion
  251. template<typename T, qualifier Q>
  252. GLM_FUNC_DECL mat<3, 3, T, Q> mat3_cast(tquat<T, Q> const& x);
  253. /// Converts a quaternion to a 4 * 4 matrix.
  254. ///
  255. /// @tparam T Floating-point scalar types.
  256. ///
  257. /// @see gtc_quaternion
  258. template<typename T, qualifier Q>
  259. GLM_FUNC_DECL mat<4, 4, T, Q> mat4_cast(tquat<T, Q> const& x);
  260. /// Converts a pure rotation 3 * 3 matrix to a quaternion.
  261. ///
  262. /// @tparam T Floating-point scalar types.
  263. ///
  264. /// @see gtc_quaternion
  265. template<typename T, qualifier Q>
  266. GLM_FUNC_DECL tquat<T, Q> quat_cast(mat<3, 3, T, Q> const& x);
  267. /// Converts a pure rotation 4 * 4 matrix to a quaternion.
  268. ///
  269. /// @tparam T Floating-point scalar types.
  270. ///
  271. /// @see gtc_quaternion
  272. template<typename T, qualifier Q>
  273. GLM_FUNC_DECL tquat<T, Q> quat_cast(mat<4, 4, T, Q> const& x);
  274. /// Returns the quaternion rotation angle.
  275. ///
  276. /// @tparam T Floating-point scalar types.
  277. ///
  278. /// @see gtc_quaternion
  279. template<typename T, qualifier Q>
  280. GLM_FUNC_DECL T angle(tquat<T, Q> const& x);
  281. /// Returns the q rotation axis.
  282. ///
  283. /// @tparam T Floating-point scalar types.
  284. ///
  285. /// @see gtc_quaternion
  286. template<typename T, qualifier Q>
  287. GLM_FUNC_DECL vec<3, T, Q> axis(tquat<T, Q> const& x);
  288. /// Build a quaternion from an angle and a normalized axis.
  289. ///
  290. /// @param angle Angle expressed in radians.
  291. /// @param axis Axis of the quaternion, must be normalized.
  292. /// @tparam T Floating-point scalar types.
  293. ///
  294. /// @see gtc_quaternion
  295. template<typename T, qualifier Q>
  296. GLM_FUNC_DECL tquat<T, Q> angleAxis(T const& angle, vec<3, T, Q> const& axis);
  297. /// Returns the component-wise comparison result of x < y.
  298. ///
  299. /// @tparam T Floating-point scalar types.
  300. ///
  301. /// @see gtc_quaternion
  302. template<typename T, qualifier Q>
  303. GLM_FUNC_DECL vec<4, bool, Q> lessThan(tquat<T, Q> const& x, tquat<T, Q> const& y);
  304. /// Returns the component-wise comparison of result x <= y.
  305. ///
  306. /// @tparam T Floating-point scalar types.
  307. ///
  308. /// @see gtc_quaternion
  309. template<typename T, qualifier Q>
  310. GLM_FUNC_DECL vec<4, bool, Q> lessThanEqual(tquat<T, Q> const& x, tquat<T, Q> const& y);
  311. /// Returns the component-wise comparison of result x > y.
  312. ///
  313. /// @tparam T Floating-point scalar types.
  314. ///
  315. /// @see gtc_quaternion
  316. template<typename T, qualifier Q>
  317. GLM_FUNC_DECL vec<4, bool, Q> greaterThan(tquat<T, Q> const& x, tquat<T, Q> const& y);
  318. /// Returns the component-wise comparison of result x >= y.
  319. ///
  320. /// @tparam T Floating-point scalar types.
  321. ///
  322. /// @see gtc_quaternion
  323. template<typename T, qualifier Q>
  324. GLM_FUNC_DECL vec<4, bool, Q> greaterThanEqual(tquat<T, Q> const& x, tquat<T, Q> const& y);
  325. /// Returns the component-wise comparison of result x == y.
  326. ///
  327. /// @tparam T Floating-point scalar types.
  328. ///
  329. /// @see gtc_quaternion
  330. template<typename T, qualifier Q>
  331. GLM_FUNC_DECL vec<4, bool, Q> equal(tquat<T, Q> const& x, tquat<T, Q> const& y);
  332. /// Returns the component-wise comparison of result x != y.
  333. ///
  334. /// @tparam T Floating-point scalar types.
  335. ///
  336. /// @see gtc_quaternion
  337. template<typename T, qualifier Q>
  338. GLM_FUNC_DECL vec<4, bool, Q> notEqual(tquat<T, Q> const& x, tquat<T, Q> const& y);
  339. /// Returns true if x holds a NaN (not a number)
  340. /// representation in the underlying implementation's set of
  341. /// floating point representations. Returns false otherwise,
  342. /// including for implementations with no NaN
  343. /// representations.
  344. ///
  345. /// /!\ When using compiler fast math, this function may fail.
  346. ///
  347. /// @tparam T Floating-point scalar types.
  348. ///
  349. /// @see gtc_quaternion
  350. template<typename T, qualifier Q>
  351. GLM_FUNC_DECL vec<4, bool, Q> isnan(tquat<T, Q> const& x);
  352. /// Returns true if x holds a positive infinity or negative
  353. /// infinity representation in the underlying implementation's
  354. /// set of floating point representations. Returns false
  355. /// otherwise, including for implementations with no infinity
  356. /// representations.
  357. ///
  358. /// @tparam T Floating-point scalar types.
  359. ///
  360. /// @see gtc_quaternion
  361. template<typename T, qualifier Q>
  362. GLM_FUNC_DECL vec<4, bool, Q> isinf(tquat<T, Q> const& x);
  363. /// @}
  364. } //namespace glm
  365. #include "quaternion.inl"