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.

381 lines
11 KiB

4 years ago
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2013 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. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. /// THE SOFTWARE.
  22. ///
  23. /// @ref gtc_quaternion
  24. /// @file glm/gtc/quaternion.hpp
  25. /// @date 2009-05-21 / 2012-12-20
  26. /// @author Christophe Riccio
  27. ///
  28. /// @see core (dependence)
  29. /// @see gtc_half_float (dependence)
  30. /// @see gtc_constants (dependence)
  31. ///
  32. /// @defgroup gtc_quaternion GLM_GTC_quaternion
  33. /// @ingroup gtc
  34. ///
  35. /// @brief Defines a templated quaternion type and several quaternion operations.
  36. ///
  37. /// <glm/gtc/quaternion.hpp> need to be included to use these functionalities.
  38. ///////////////////////////////////////////////////////////////////////////////////
  39. #ifndef GLM_GTC_quaternion
  40. #define GLM_GTC_quaternion GLM_VERSION
  41. // Dependency:
  42. #include "../glm.hpp"
  43. #include "../gtc/half_float.hpp"
  44. #include "../gtc/constants.hpp"
  45. #if(defined(GLM_MESSAGES) && !defined(glm_ext))
  46. # pragma message("GLM: GLM_GTC_quaternion extension included")
  47. #endif
  48. namespace glm{
  49. namespace detail
  50. {
  51. template <typename T>
  52. struct tquat// : public genType<T, tquat>
  53. {
  54. enum ctor{null};
  55. typedef T value_type;
  56. typedef std::size_t size_type;
  57. public:
  58. value_type x, y, z, w;
  59. GLM_FUNC_DECL size_type length() const;
  60. // Constructors
  61. tquat();
  62. explicit tquat(
  63. value_type const & s,
  64. glm::detail::tvec3<T> const & v);
  65. explicit tquat(
  66. value_type const & w,
  67. value_type const & x,
  68. value_type const & y,
  69. value_type const & z);
  70. // Convertions
  71. /// Build a quaternion from euler angles (pitch, yaw, roll), in radians.
  72. explicit tquat(
  73. tvec3<T> const & eulerAngles);
  74. explicit tquat(
  75. tmat3x3<T> const & m);
  76. explicit tquat(
  77. tmat4x4<T> const & m);
  78. // Accesses
  79. value_type & operator[](int i);
  80. value_type const & operator[](int i) const;
  81. // Operators
  82. tquat<T> & operator*=(value_type const & s);
  83. tquat<T> & operator/=(value_type const & s);
  84. };
  85. template <typename T>
  86. detail::tquat<T> operator- (
  87. detail::tquat<T> const & q);
  88. template <typename T>
  89. detail::tquat<T> operator+ (
  90. detail::tquat<T> const & q,
  91. detail::tquat<T> const & p);
  92. template <typename T>
  93. detail::tquat<T> operator* (
  94. detail::tquat<T> const & q,
  95. detail::tquat<T> const & p);
  96. template <typename T>
  97. detail::tvec3<T> operator* (
  98. detail::tquat<T> const & q,
  99. detail::tvec3<T> const & v);
  100. template <typename T>
  101. detail::tvec3<T> operator* (
  102. detail::tvec3<T> const & v,
  103. detail::tquat<T> const & q);
  104. template <typename T>
  105. detail::tvec4<T> operator* (
  106. detail::tquat<T> const & q,
  107. detail::tvec4<T> const & v);
  108. template <typename T>
  109. detail::tvec4<T> operator* (
  110. detail::tvec4<T> const & v,
  111. detail::tquat<T> const & q);
  112. template <typename T>
  113. detail::tquat<T> operator* (
  114. detail::tquat<T> const & q,
  115. typename detail::tquat<T>::value_type const & s);
  116. template <typename T>
  117. detail::tquat<T> operator* (
  118. typename detail::tquat<T>::value_type const & s,
  119. detail::tquat<T> const & q);
  120. template <typename T>
  121. detail::tquat<T> operator/ (
  122. detail::tquat<T> const & q,
  123. typename detail::tquat<T>::value_type const & s);
  124. } //namespace detail
  125. /// @addtogroup gtc_quaternion
  126. /// @{
  127. /// Returns the length of the quaternion.
  128. ///
  129. /// @see gtc_quaternion
  130. template <typename T>
  131. T length(
  132. detail::tquat<T> const & q);
  133. /// Returns the normalized quaternion.
  134. ///
  135. /// @see gtc_quaternion
  136. template <typename T>
  137. detail::tquat<T> normalize(
  138. detail::tquat<T> const & q);
  139. /// Returns dot product of q1 and q2, i.e., q1[0] * q2[0] + q1[1] * q2[1] + ...
  140. ///
  141. /// @see gtc_quaternion
  142. template <typename T>
  143. T dot(
  144. detail::tquat<T> const & q1,
  145. detail::tquat<T> const & q2);
  146. /// Spherical linear interpolation of two quaternions.
  147. /// The interpolation is oriented and the rotation is performed at constant speed.
  148. /// For short path spherical linear interpolation, use the slerp function.
  149. ///
  150. /// @param x A quaternion
  151. /// @param y A quaternion
  152. /// @param a Interpolation factor. The interpolation is defined beyond the range [0, 1].
  153. /// @tparam T Value type used to build the quaternion. Supported: half, float or double.
  154. /// @see gtc_quaternion
  155. /// @see - slerp(detail::tquat<T> const & x, detail::tquat<T> const & y, T const & a)
  156. template <typename T>
  157. detail::tquat<T> mix(
  158. detail::tquat<T> const & x,
  159. detail::tquat<T> const & y,
  160. T const & a);
  161. /// Linear interpolation of two quaternions.
  162. /// The interpolation is oriented.
  163. ///
  164. /// @param x A quaternion
  165. /// @param y A quaternion
  166. /// @param a Interpolation factor. The interpolation is defined in the range [0, 1].
  167. /// @tparam T Value type used to build the quaternion. Supported: half, float or double.
  168. /// @see gtc_quaternion
  169. template <typename T>
  170. detail::tquat<T> lerp(
  171. detail::tquat<T> const & x,
  172. detail::tquat<T> const & y,
  173. T const & a);
  174. /// Spherical linear interpolation of two quaternions.
  175. /// The interpolation always take the short path and the rotation is performed at constant speed.
  176. ///
  177. /// @param x A quaternion
  178. /// @param y A quaternion
  179. /// @param a Interpolation factor. The interpolation is defined beyond the range [0, 1].
  180. /// @tparam T Value type used to build the quaternion. Supported: half, float or double.
  181. /// @see gtc_quaternion
  182. template <typename T>
  183. detail::tquat<T> slerp(
  184. detail::tquat<T> const & x,
  185. detail::tquat<T> const & y,
  186. T const & a);
  187. /// Returns the q conjugate.
  188. ///
  189. /// @see gtc_quaternion
  190. template <typename T>
  191. detail::tquat<T> conjugate(
  192. detail::tquat<T> const & q);
  193. /// Returns the q inverse.
  194. ///
  195. /// @see gtc_quaternion
  196. template <typename T>
  197. detail::tquat<T> inverse(
  198. detail::tquat<T> const & q);
  199. /// Rotates a quaternion from an vector of 3 components axis and an angle.
  200. ///
  201. /// @param q Source orientation
  202. /// @param angle Angle expressed in radians if GLM_FORCE_RADIANS is define or degrees otherwise.
  203. /// @param axis Axis of the rotation, must be normalized.
  204. ///
  205. /// @see gtc_quaternion
  206. template <typename T>
  207. detail::tquat<T> rotate(
  208. detail::tquat<T> const & q,
  209. typename detail::tquat<T>::value_type const & angle,
  210. detail::tvec3<T> const & axis);
  211. /// Returns euler angles, yitch as x, yaw as y, roll as z.
  212. ///
  213. /// @see gtc_quaternion
  214. template <typename T>
  215. detail::tvec3<T> eulerAngles(
  216. detail::tquat<T> const & x);
  217. /// Returns roll value of euler angles expressed in radians if GLM_FORCE_RADIANS is define or degrees otherwise.
  218. ///
  219. /// @see gtx_quaternion
  220. template <typename valType>
  221. valType roll(
  222. detail::tquat<valType> const & x);
  223. /// Returns pitch value of euler angles expressed in radians if GLM_FORCE_RADIANS is define or degrees otherwise.
  224. ///
  225. /// @see gtx_quaternion
  226. template <typename valType>
  227. valType pitch(
  228. detail::tquat<valType> const & x);
  229. /// Returns yaw value of euler angles expressed in radians if GLM_FORCE_RADIANS is define or degrees otherwise.
  230. ///
  231. /// @see gtx_quaternion
  232. template <typename valType>
  233. valType yaw(
  234. detail::tquat<valType> const & x);
  235. /// Converts a quaternion to a 3 * 3 matrix.
  236. ///
  237. /// @see gtc_quaternion
  238. template <typename T>
  239. detail::tmat3x3<T> mat3_cast(
  240. detail::tquat<T> const & x);
  241. /// Converts a quaternion to a 4 * 4 matrix.
  242. ///
  243. /// @see gtc_quaternion
  244. template <typename T>
  245. detail::tmat4x4<T> mat4_cast(
  246. detail::tquat<T> const & x);
  247. /// Converts a 3 * 3 matrix to a quaternion.
  248. ///
  249. /// @see gtc_quaternion
  250. template <typename T>
  251. detail::tquat<T> quat_cast(
  252. detail::tmat3x3<T> const & x);
  253. /// Converts a 4 * 4 matrix to a quaternion.
  254. ///
  255. /// @see gtc_quaternion
  256. template <typename T>
  257. detail::tquat<T> quat_cast(
  258. detail::tmat4x4<T> const & x);
  259. /// Returns the quaternion rotation angle.
  260. ///
  261. /// @see gtc_quaternion
  262. template <typename valType>
  263. valType angle(
  264. detail::tquat<valType> const & x);
  265. /// Returns the q rotation axis.
  266. ///
  267. /// @see gtc_quaternion
  268. template <typename valType>
  269. detail::tvec3<valType> axis(
  270. detail::tquat<valType> const & x);
  271. /// Build a quaternion from an angle and a normalized axis.
  272. ///
  273. /// @param angle Angle expressed in radians if GLM_FORCE_RADIANS is define or degrees otherwise.
  274. /// @param x x component of the x-axis, x, y, z must be a normalized axis
  275. /// @param y y component of the y-axis, x, y, z must be a normalized axis
  276. /// @param z z component of the z-axis, x, y, z must be a normalized axis
  277. ///
  278. /// @see gtc_quaternion
  279. template <typename valType>
  280. detail::tquat<valType> angleAxis(
  281. valType const & angle,
  282. valType const & x,
  283. valType const & y,
  284. valType const & z);
  285. /// Build a quaternion from an angle and a normalized axis.
  286. ///
  287. /// @param angle Angle expressed in radians if GLM_FORCE_RADIANS is define or degrees otherwise.
  288. /// @param axis Axis of the quaternion, must be normalized.
  289. ///
  290. /// @see gtc_quaternion
  291. template <typename valType>
  292. detail::tquat<valType> angleAxis(
  293. valType const & angle,
  294. detail::tvec3<valType> const & axis);
  295. /// Quaternion of floating-point numbers.
  296. ///
  297. /// @see gtc_quaternion
  298. typedef detail::tquat<float> quat;
  299. /// Quaternion of half-precision floating-point numbers.
  300. ///
  301. /// @see gtc_quaternion
  302. typedef detail::tquat<detail::half> hquat;
  303. /// Quaternion of single-precision floating-point numbers.
  304. ///
  305. /// @see gtc_quaternion
  306. typedef detail::tquat<float> fquat;
  307. /// Quaternion of double-precision floating-point numbers.
  308. ///
  309. /// @see gtc_quaternion
  310. typedef detail::tquat<double> dquat;
  311. /// Quaternion of low precision floating-point numbers.
  312. ///
  313. /// @see gtc_quaternion
  314. typedef detail::tquat<lowp_float> lowp_quat;
  315. /// Quaternion of medium precision floating-point numbers.
  316. ///
  317. /// @see gtc_quaternion
  318. typedef detail::tquat<mediump_float> mediump_quat;
  319. /// Quaternion of high precision floating-point numbers.
  320. ///
  321. /// @see gtc_quaternion
  322. typedef detail::tquat<highp_float> highp_quat;
  323. /// @}
  324. } //namespace glm
  325. #include "quaternion.inl"
  326. #endif//GLM_GTC_quaternion