quaternion.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2011 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 / 2011-06-05
  26. /// @author Christophe Riccio
  27. ///
  28. /// @see core (dependence)
  29. /// @see gtc_half_float (dependence)
  30. ///
  31. /// @defgroup gtc_quaternion GLM_GTC_quaternion: Quaternion types and functions
  32. /// @ingroup gtc
  33. ///
  34. /// @brief Defines a templated quaternion type and several quaternion operations.
  35. ///
  36. /// <glm/gtc/quaternion.hpp> need to be included to use these functionalities.
  37. ///////////////////////////////////////////////////////////////////////////////////
  38. #ifndef GLM_GTC_quaternion
  39. #define GLM_GTC_quaternion 90
  40. // Dependency:
  41. #include "../glm.hpp"
  42. #include "../gtc/half_float.hpp"
  43. #if(defined(GLM_MESSAGES) && !defined(glm_ext))
  44. # pragma message("GLM: GLM_GTC_quaternion extension included")
  45. #endif
  46. namespace glm{
  47. namespace detail
  48. {
  49. /// @brief Template for quaternion.
  50. /// From GLM_GTC_quaternion extension.
  51. /// @ingroup gtc_quaternion
  52. template <typename T>
  53. struct tquat// : public genType<T, tquat>
  54. {
  55. enum ctor{null};
  56. typedef T value_type;
  57. typedef std::size_t size_type;
  58. public:
  59. value_type x, y, z, w;
  60. GLM_FUNC_DECL size_type length() const;
  61. // Constructors
  62. tquat();
  63. explicit tquat(
  64. value_type const & s,
  65. glm::detail::tvec3<T> const & v);
  66. explicit tquat(
  67. value_type const & w,
  68. value_type const & x,
  69. value_type const & y,
  70. value_type const & z);
  71. // Convertions
  72. //explicit tquat(valType const & pitch, valType const & yaw, valType const & roll);
  73. //! pitch, yaw, roll
  74. explicit tquat(
  75. tvec3<T> const & eulerAngles);
  76. explicit tquat(
  77. tmat3x3<T> const & m);
  78. explicit tquat(
  79. tmat4x4<T> const & m);
  80. // Accesses
  81. value_type & operator[](int i);
  82. value_type const & operator[](int i) const;
  83. // Operators
  84. tquat<T> & operator*=(value_type const & s);
  85. tquat<T> & operator/=(value_type const & s);
  86. };
  87. template <typename T>
  88. detail::tquat<T> operator- (
  89. detail::tquat<T> const & q);
  90. template <typename T>
  91. detail::tquat<T> operator+ (
  92. detail::tquat<T> const & q,
  93. detail::tquat<T> const & p);
  94. template <typename T>
  95. detail::tquat<T> operator* (
  96. detail::tquat<T> const & q,
  97. detail::tquat<T> const & p);
  98. template <typename T>
  99. detail::tvec3<T> operator* (
  100. detail::tquat<T> const & q,
  101. detail::tvec3<T> const & v);
  102. template <typename T>
  103. detail::tvec3<T> operator* (
  104. detail::tvec3<T> const & v,
  105. detail::tquat<T> const & q);
  106. template <typename T>
  107. detail::tvec4<T> operator* (
  108. detail::tquat<T> const & q,
  109. detail::tvec4<T> const & v);
  110. template <typename T>
  111. detail::tvec4<T> operator* (
  112. detail::tvec4<T> const & v,
  113. detail::tquat<T> const & q);
  114. template <typename T>
  115. detail::tquat<T> operator* (
  116. detail::tquat<T> const & q,
  117. typename detail::tquat<T>::value_type const & s);
  118. template <typename T>
  119. detail::tquat<T> operator* (
  120. typename detail::tquat<T>::value_type const & s,
  121. detail::tquat<T> const & q);
  122. template <typename T>
  123. detail::tquat<T> operator/ (
  124. detail::tquat<T> const & q,
  125. typename detail::tquat<T>::value_type const & s);
  126. } //namespace detail
  127. /// @addtogroup gtc_quaternion
  128. /// @{
  129. //! Returns the length of the quaternion.
  130. //! From GLM_GTC_quaternion extension.
  131. template <typename T>
  132. T length(
  133. detail::tquat<T> const & q);
  134. //! Returns the normalized quaternion.
  135. //! From GLM_GTC_quaternion extension.
  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. //! From GLM_GTC_quaternion extension.
  141. template <typename T>
  142. T dot(
  143. detail::tquat<T> const & q1,
  144. detail::tquat<T> const & q2);
  145. //! Returns a SLERP interpolated quaternion of x and y according a.
  146. //! From GLM_GTC_quaternion extension.
  147. template <typename T>
  148. detail::tquat<T> mix(
  149. detail::tquat<T> const & x,
  150. detail::tquat<T> const & y,
  151. T const & a);
  152. //! Returns the q conjugate.
  153. //! From GLM_GTC_quaternion extension.
  154. template <typename T>
  155. detail::tquat<T> conjugate(
  156. detail::tquat<T> const & q);
  157. //! Returns the q inverse.
  158. //! From GLM_GTC_quaternion extension.
  159. template <typename T>
  160. detail::tquat<T> inverse(
  161. detail::tquat<T> const & q);
  162. //! Rotates a quaternion from an vector of 3 components axis and an angle expressed in degrees.
  163. //! From GLM_GTC_quaternion extension.
  164. template <typename T>
  165. detail::tquat<T> rotate(
  166. detail::tquat<T> const & q,
  167. typename detail::tquat<T>::value_type const & angle,
  168. detail::tvec3<T> const & v);
  169. //! Converts a quaternion to a 3 * 3 matrix.
  170. //! From GLM_GTC_quaternion extension.
  171. template <typename T>
  172. detail::tmat3x3<T> mat3_cast(
  173. detail::tquat<T> const & x);
  174. //! Converts a quaternion to a 4 * 4 matrix.
  175. //! From GLM_GTC_quaternion extension.
  176. template <typename T>
  177. detail::tmat4x4<T> mat4_cast(
  178. detail::tquat<T> const & x);
  179. //! Converts a 3 * 3 matrix to a quaternion.
  180. //! From GLM_GTC_quaternion extension.
  181. template <typename T>
  182. detail::tquat<T> quat_cast(
  183. detail::tmat3x3<T> const & x);
  184. //! Converts a 4 * 4 matrix to a quaternion.
  185. //! From GLM_GTC_quaternion extension.
  186. template <typename T>
  187. detail::tquat<T> quat_cast(
  188. detail::tmat4x4<T> const & x);
  189. //! Quaternion of floating-point numbers.
  190. //! From GLM_GTC_quaternion extension.
  191. typedef detail::tquat<float> quat;
  192. //! Quaternion of half-precision floating-point numbers.
  193. //! From GLM_GTC_quaternion extension.
  194. typedef detail::tquat<detail::thalf> hquat;
  195. //! Quaternion of single-precision floating-point numbers.
  196. //! From GLM_GTC_quaternion extension.
  197. typedef detail::tquat<float> fquat;
  198. //! Quaternion of double-precision floating-point numbers.
  199. //! From GLM_GTC_quaternion extension.
  200. typedef detail::tquat<double> dquat;
  201. //! Quaternion of low precision floating-point numbers.
  202. //! From GLM_GTC_quaternion extension.
  203. typedef detail::tquat<lowp_float> lowp_quat;
  204. //! Quaternion of medium precision floating-point numbers.
  205. //! From GLM_GTC_quaternion extension.
  206. typedef detail::tquat<mediump_float> mediump_quat;
  207. //! Quaternion of high precision floating-point numbers.
  208. //! From GLM_GTC_quaternion extension.
  209. typedef detail::tquat<highp_float> highp_quat;
  210. /// @}
  211. } //namespace glm
  212. #include "quaternion.inl"
  213. #endif//GLM_GTC_quaternion