2
0

quat.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. .. default-domain:: C
  2. quaternions
  3. ===========
  4. Header: cglm/quat.h
  5. **Important:** *cglm* stores quaternion as **[x, y, z, w]** in memory
  6. since **v0.4.0** it was **[w, x, y, z]**
  7. before v0.4.0 ( **v0.3.5 and earlier** ). w is real part.
  8. What you can do with quaternions with existing functions is (Some of them):
  9. - You can rotate transform matrix using quaternion
  10. - You can rotate vector using quaternion
  11. - You can create view matrix using quaternion
  12. - You can create a lookrotation (from source point to dest)
  13. Table of contents (click to go):
  14. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  15. Macros:
  16. 1. GLM_QUAT_IDENTITY_INIT
  17. #. GLM_QUAT_IDENTITY
  18. Functions:
  19. 1. :c:func:`glm_quat_identity`
  20. #. :c:func:`glm_quat_identity_array`
  21. #. :c:func:`glm_quat_init`
  22. #. :c:func:`glm_quat`
  23. #. :c:func:`glm_quatv`
  24. #. :c:func:`glm_quat_copy`
  25. #. :c:func:`glm_quat_from_vecs`
  26. #. :c:func:`glm_quat_norm`
  27. #. :c:func:`glm_quat_normalize`
  28. #. :c:func:`glm_quat_normalize_to`
  29. #. :c:func:`glm_quat_dot`
  30. #. :c:func:`glm_quat_conjugate`
  31. #. :c:func:`glm_quat_inv`
  32. #. :c:func:`glm_quat_add`
  33. #. :c:func:`glm_quat_sub`
  34. #. :c:func:`glm_quat_real`
  35. #. :c:func:`glm_quat_imag`
  36. #. :c:func:`glm_quat_imagn`
  37. #. :c:func:`glm_quat_imaglen`
  38. #. :c:func:`glm_quat_angle`
  39. #. :c:func:`glm_quat_axis`
  40. #. :c:func:`glm_quat_mul`
  41. #. :c:func:`glm_quat_mat4`
  42. #. :c:func:`glm_quat_mat4t`
  43. #. :c:func:`glm_quat_mat3`
  44. #. :c:func:`glm_quat_mat3t`
  45. #. :c:func:`glm_quat_lerp`
  46. #. :c:func:`glm_quat_nlerp`
  47. #. :c:func:`glm_quat_slerp`
  48. #. :c:func:`glm_quat_slerp_longest`
  49. #. :c:func:`glm_quat_look`
  50. #. :c:func:`glm_quat_for`
  51. #. :c:func:`glm_quat_forp`
  52. #. :c:func:`glm_quat_rotatev`
  53. #. :c:func:`glm_quat_rotate`
  54. #. :c:func:`glm_quat_rotate_at`
  55. #. :c:func:`glm_quat_rotate_atm`
  56. #. :c:func:`glm_quat_make`
  57. Functions documentation
  58. ~~~~~~~~~~~~~~~~~~~~~~~
  59. .. c:function:: void glm_quat_identity(versor q)
  60. | makes given quat to identity
  61. Parameters:
  62. | *[in, out]* **q** quaternion
  63. .. c:function:: void glm_quat_identity_array(versor * __restrict q, size_t count)
  64. | make given quaternion array's each element identity quaternion
  65. Parameters:
  66. | *[in, out]* **q** quat array (must be aligned (16) if alignment is not disabled)
  67. | *[in]* **count** count of quaternions
  68. .. c:function:: void glm_quat_init(versor q, float x, float y, float z, float w)
  69. | inits quaternion with given values
  70. Parameters:
  71. | *[out]* **q** quaternion
  72. | *[in]* **x** imag.x
  73. | *[in]* **y** imag.y
  74. | *[in]* **z** imag.z
  75. | *[in]* **w** w (real part)
  76. .. c:function:: void glm_quat(versor q, float angle, float x, float y, float z)
  77. | creates NEW quaternion with individual axis components
  78. | given axis will be normalized
  79. Parameters:
  80. | *[out]* **q** quaternion
  81. | *[in]* **angle** angle (radians)
  82. | *[in]* **x** axis.x
  83. | *[in]* **y** axis.y
  84. | *[in]* **z** axis.z
  85. .. c:function:: void glm_quatv(versor q, float angle, vec3 axis)
  86. | creates NEW quaternion with axis vector
  87. | given axis will be normalized
  88. Parameters:
  89. | *[out]* **q** quaternion
  90. | *[in]* **angle** angle (radians)
  91. | *[in]* **axis** axis (will be normalized)
  92. .. c:function:: void glm_quat_copy(versor q, versor dest)
  93. | copy quaternion to another one
  94. Parameters:
  95. | *[in]* **q** source quaternion
  96. | *[out]* **dest** destination quaternion
  97. .. c:function:: void glm_quat_from_vecs(vec3 a, vec3 b, versor dest)
  98. | compute unit quaternion needed to rotate a into b
  99. References:
  100. * `Finding quaternion representing the rotation from one vector to another <https://stackoverflow.com/a/11741520/183120>`_
  101. * `Quaternion from two vectors <http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final>`_
  102. * `Angle between vectors <http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/minorlogic.htm>`_
  103. Parameters:
  104. | *[in]* **a** unit vector
  105. | *[in]* **b** unit vector
  106. | *[in]* **dest** unit quaternion
  107. .. c:function:: float glm_quat_norm(versor q)
  108. | returns norm (magnitude) of quaternion
  109. Parameters:
  110. | *[in]* **a** quaternion
  111. Returns:
  112. norm (magnitude)
  113. .. c:function:: void glm_quat_normalize_to(versor q, versor dest)
  114. | normalize quaternion and store result in dest, original one will not be normalized
  115. Parameters:
  116. | *[in]* **q** quaternion to normalize into
  117. | *[out]* **dest** destination quaternion
  118. .. c:function:: void glm_quat_normalize(versor q)
  119. | normalize quaternion
  120. Parameters:
  121. | *[in, out]* **q** quaternion
  122. .. c:function:: float glm_quat_dot(versor p, versor q)
  123. dot product of two quaternion
  124. Parameters:
  125. | *[in]* **p** quaternion 1
  126. | *[in]* **q** quaternion 2
  127. Returns:
  128. dot product
  129. .. c:function:: void glm_quat_conjugate(versor q, versor dest)
  130. conjugate of quaternion
  131. Parameters:
  132. | *[in]* **q** quaternion
  133. | *[in]* **dest** conjugate
  134. .. c:function:: void glm_quat_inv(versor q, versor dest)
  135. inverse of non-zero quaternion
  136. Parameters:
  137. | *[in]* **q** quaternion
  138. | *[in]* **dest** inverse quaternion
  139. .. c:function:: void glm_quat_add(versor p, versor q, versor dest)
  140. add (componentwise) two quaternions and store result in dest
  141. Parameters:
  142. | *[in]* **p** quaternion 1
  143. | *[in]* **q** quaternion 2
  144. | *[in]* **dest** result quaternion
  145. .. c:function:: void glm_quat_sub(versor p, versor q, versor dest)
  146. subtract (componentwise) two quaternions and store result in dest
  147. Parameters:
  148. | *[in]* **p** quaternion 1
  149. | *[in]* **q** quaternion 2
  150. | *[in]* **dest** result quaternion
  151. .. c:function:: float glm_quat_real(versor q)
  152. returns real part of quaternion
  153. Parameters:
  154. | *[in]* **q** quaternion
  155. Returns:
  156. real part (quat.w)
  157. .. c:function:: void glm_quat_imag(versor q, vec3 dest)
  158. returns imaginary part of quaternion
  159. Parameters:
  160. | *[in]* **q** quaternion
  161. | *[out]* **dest** imag
  162. .. c:function:: void glm_quat_imagn(versor q, vec3 dest)
  163. returns normalized imaginary part of quaternion
  164. Parameters:
  165. | *[in]* **q** quaternion
  166. | *[out]* **dest** imag
  167. .. c:function:: float glm_quat_imaglen(versor q)
  168. returns length of imaginary part of quaternion
  169. Parameters:
  170. | *[in]* **q** quaternion
  171. Returns:
  172. norm of imaginary part
  173. .. c:function:: float glm_quat_angle(versor q)
  174. returns angle of quaternion
  175. Parameters:
  176. | *[in]* **q** quaternion
  177. Returns:
  178. angles of quat (radians)
  179. .. c:function:: void glm_quat_axis(versor q, versor dest)
  180. axis of quaternion
  181. Parameters:
  182. | *[in]* **p** quaternion
  183. | *[out]* **dest** axis of quaternion
  184. .. c:function:: void glm_quat_mul(versor p, versor q, versor dest)
  185. | multiplies two quaternion and stores result in dest
  186. | this is also called Hamilton Product
  187. | According to WikiPedia:
  188. | The product of two rotation quaternions [clarification needed] will be
  189. equivalent to the rotation q followed by the rotation p
  190. Parameters:
  191. | *[in]* **p** quaternion 1 (first rotation)
  192. | *[in]* **q** quaternion 2 (second rotation)
  193. | *[out]* **dest** result quaternion
  194. .. c:function:: void glm_quat_mat4(versor q, mat4 dest)
  195. | convert quaternion to mat4
  196. Parameters:
  197. | *[in]* **q** quaternion
  198. | *[out]* **dest** result matrix
  199. .. c:function:: void glm_quat_mat4t(versor q, mat4 dest)
  200. | convert quaternion to mat4 (transposed). This is transposed version of glm_quat_mat4
  201. Parameters:
  202. | *[in]* **q** quaternion
  203. | *[out]* **dest** result matrix
  204. .. c:function:: void glm_quat_mat3(versor q, mat3 dest)
  205. | convert quaternion to mat3
  206. Parameters:
  207. | *[in]* **q** quaternion
  208. | *[out]* **dest** result matrix
  209. .. c:function:: void glm_quat_mat3t(versor q, mat3 dest)
  210. | convert quaternion to mat3 (transposed). This is transposed version of glm_quat_mat3
  211. Parameters:
  212. | *[in]* **q** quaternion
  213. | *[out]* **dest** result matrix
  214. .. c:function:: void glm_quat_lerp(versor from, versor to, float t, versor dest)
  215. | interpolates between two quaternions
  216. | using spherical linear interpolation (LERP)
  217. Parameters:
  218. | *[in]* **from** from
  219. | *[in]* **to** to
  220. | *[in]* **t** interpolant (amount) clamped between 0 and 1
  221. | *[out]* **dest** result quaternion
  222. .. c:function:: void glm_quat_nlerp(versor q, versor r, float t, versor dest)
  223. | interpolates between two quaternions
  224. | taking the shortest rotation path using
  225. | normalized linear interpolation (NLERP)
  226. | This is a cheaper alternative to slerp; most games use nlerp
  227. | for animations as it visually makes little difference.
  228. References:
  229. * `Understanding Slerp, Then Not Using it <http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It>`_
  230. * `Lerp, Slerp and Nlerp <https://keithmaggio.wordpress.com/2011/02/15/math-magician-lerp-slerp-and-nlerp/>`_
  231. Parameters:
  232. | *[in]* **from** from
  233. | *[in]* **to** to
  234. | *[in]* **t** interpolant (amount) clamped between 0 and 1
  235. | *[out]* **dest** result quaternion
  236. .. c:function:: void glm_quat_slerp(versor q, versor r, float t, versor dest)
  237. | interpolates between two quaternions
  238. | using spherical linear interpolation (SLERP)
  239. Parameters:
  240. | *[in]* **from** from
  241. | *[in]* **to** to
  242. | *[in]* **t** interpolant (amount) clamped between 0 and 1
  243. | *[out]* **dest** result quaternion
  244. .. c:function:: void glm_quat_slerp_longest(versor q, versor r, float t, versor dest)
  245. | interpolates between two quaternions
  246. | using spherical linear interpolation (SLERP) and always takes the longest path
  247. Parameters:
  248. | *[in]* **from** from
  249. | *[in]* **to** to
  250. | *[in]* **t** interpolant (amount) clamped between 0 and 1
  251. | *[out]* **dest** result quaternion
  252. .. c:function:: void glm_quat_look(vec3 eye, versor ori, mat4 dest)
  253. | creates view matrix using quaternion as camera orientation
  254. Parameters:
  255. | *[in]* **eye** eye
  256. | *[in]* **ori** orientation in world space as quaternion
  257. | *[out]* **dest** result matrix
  258. .. c:function:: void glm_quat_for(vec3 dir, vec3 up, versor dest)
  259. | creates look rotation quaternion
  260. Parameters:
  261. | *[in]* **dir** direction to look
  262. | *[in]* **up** up vector
  263. | *[out]* **dest** result matrix
  264. .. c:function:: void glm_quat_forp(vec3 from, vec3 to, vec3 up, versor dest)
  265. | creates look rotation quaternion using source and destination positions p suffix stands for position
  266. | this is similar to glm_quat_for except this computes direction for glm_quat_for for you.
  267. Parameters:
  268. | *[in]* **from** source point
  269. | *[in]* **to** destination point
  270. | *[in]* **up** up vector
  271. | *[out]* **dest** result matrix
  272. .. c:function:: void glm_quat_rotatev(versor q, vec3 v, vec3 dest)
  273. | crotate vector using using quaternion
  274. Parameters:
  275. | *[in]* **q** quaternion
  276. | *[in]* **v** vector to rotate
  277. | *[out]* **dest** rotated vector
  278. .. c:function:: void glm_quat_rotate(mat4 m, versor q, mat4 dest)
  279. | rotate existing transform matrix using quaternion
  280. instead of passing identity matrix, consider to use quat_mat4 functions
  281. Parameters:
  282. | *[in]* **m** existing transform matrix to rotate
  283. | *[in]* **q** quaternion
  284. | *[out]* **dest** rotated matrix/transform
  285. .. c:function:: void glm_quat_rotate_at(mat4 m, versor q, vec3 pivot)
  286. | rotate existing transform matrix using quaternion at pivot point
  287. Parameters:
  288. | *[in, out]* **m** existing transform matrix to rotate
  289. | *[in]* **q** quaternion
  290. | *[in]* **pivot** pivot
  291. .. c:function:: void glm_quat_rotate_atm(mat4 m, versor q, vec3 pivot)
  292. | rotate NEW transform matrix using quaternion at pivot point
  293. | this creates rotation matrix, it assumes you don't have a matrix
  294. | this should work faster than glm_quat_rotate_at because it reduces one glm_translate.
  295. Parameters:
  296. | *[in, out]* **m** existing transform matrix to rotate
  297. | *[in]* **q** quaternion
  298. | *[in]* **pivot** pivot
  299. .. c:function:: void glm_quat_make(const float * __restrict src, versor dest)
  300. Create quaternion from pointer
  301. .. note:: **@src** must contain at least 4 elements. cglm store quaternions as [x, y, z, w].
  302. Parameters:
  303. | *[in]* **src** pointer to an array of floats
  304. | *[out]* **dest** destination quaternion