quat.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include "../util/c99defs.h"
  16. #include "math-defs.h"
  17. #include "vec3.h"
  18. #include <xmmintrin.h>
  19. /*
  20. * Quaternion math
  21. *
  22. * Generally used to represent rotational data more than anything. Allows
  23. * for efficient and correct rotational interpolation without suffering from
  24. * things like gimbal lock.
  25. */
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. struct matrix3;
  30. struct matrix4;
  31. struct axisang;
  32. struct quat {
  33. union {
  34. struct {float x, y, z, w;};
  35. float ptr[4];
  36. __m128 m;
  37. };
  38. };
  39. static inline void quat_identity(struct quat *q)
  40. {
  41. q->m = _mm_setzero_ps();
  42. q->w = 1.0f;
  43. }
  44. static inline void quat_set(struct quat *dst, float x, float y, float z,
  45. float w)
  46. {
  47. dst->m = _mm_set_ps(x, y, z, w);
  48. }
  49. static inline void quat_copy(struct quat *dst, const struct quat *q)
  50. {
  51. dst->m = q->m;
  52. }
  53. static inline void quat_add(struct quat *dst, const struct quat *q1,
  54. const struct quat *q2)
  55. {
  56. dst->m = _mm_add_ps(q1->m, q2->m);
  57. }
  58. static inline void quat_sub(struct quat *dst, const struct quat *q1,
  59. const struct quat *q2)
  60. {
  61. dst->m = _mm_sub_ps(q1->m, q2->m);
  62. }
  63. EXPORT void quat_mul(struct quat *dst, const struct quat *q1,
  64. const struct quat *q2);
  65. static inline void quat_addf(struct quat *dst, const struct quat *q,
  66. float f)
  67. {
  68. dst->m = _mm_add_ps(q->m, _mm_set1_ps(f));
  69. }
  70. static inline void quat_subf(struct quat *dst, const struct quat *q,
  71. float f)
  72. {
  73. dst->m = _mm_sub_ps(q->m, _mm_set1_ps(f));
  74. }
  75. static inline void quat_mulf(struct quat *dst, const struct quat *q,
  76. float f)
  77. {
  78. dst->m = _mm_mul_ps(q->m, _mm_set1_ps(f));
  79. }
  80. static inline void quat_divf(struct quat *dst, const struct quat *q,
  81. float f)
  82. {
  83. dst->m = _mm_div_ps(q->m, _mm_set1_ps(f));
  84. }
  85. static inline float quat_dot(const struct quat *q1, const struct quat *q2)
  86. {
  87. struct vec3 add;
  88. __m128 mul = _mm_mul_ps(q1->m, q2->m);
  89. add.m = _mm_add_ps(_mm_movehl_ps(mul, mul), mul);
  90. add.m = _mm_add_ps(_mm_shuffle_ps(add.m, add.m, 0x55), add.m);
  91. return add.x;
  92. }
  93. static inline void quat_inv(struct quat *dst, const struct quat *q)
  94. {
  95. dst->x = -q->x;
  96. dst->y = -q->y;
  97. dst->z = -q->z;
  98. }
  99. static inline void quat_neg(struct quat *dst, const struct quat *q)
  100. {
  101. dst->x = -q->x;
  102. dst->y = -q->y;
  103. dst->z = -q->z;
  104. dst->w = -q->w;
  105. }
  106. static inline float quat_len(const struct quat *q)
  107. {
  108. float dot_val = quat_dot(q, q);
  109. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  110. }
  111. static inline float quat_dist(const struct quat *q1, const struct quat *q2)
  112. {
  113. struct quat temp;
  114. float dot_val;
  115. quat_sub(&temp, q1, q2);
  116. dot_val = quat_dot(&temp, &temp);
  117. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  118. }
  119. static inline void quat_norm(struct quat *dst, const struct quat *q)
  120. {
  121. float dot_val = quat_dot(q, q);
  122. dst->m = (dot_val > 0.0f) ?
  123. _mm_mul_ps(q->m, _mm_set1_ps(1.0f/sqrtf(dot_val))) :
  124. _mm_setzero_ps();
  125. }
  126. static inline bool quat_close(const struct quat *q1, const struct quat *q2,
  127. float epsilon)
  128. {
  129. struct quat test;
  130. quat_sub(&test, q1, q2);
  131. return test.x < epsilon &&
  132. test.y < epsilon &&
  133. test.z < epsilon &&
  134. test.w < epsilon;
  135. }
  136. EXPORT void quat_from_axisang(struct quat *dst, const struct axisang *aa);
  137. EXPORT void quat_from_matrix3(struct quat *dst, const struct matrix3 *m);
  138. EXPORT void quat_from_matrix4(struct quat *dst, const struct matrix4 *m);
  139. EXPORT void quat_get_dir(struct vec3 *dst, const struct quat *q);
  140. EXPORT void quat_set_look_dir(struct quat *dst, const struct vec3 *dir);
  141. EXPORT void quat_log(struct quat *dst, const struct quat *q);
  142. EXPORT void quat_exp(struct quat *dst, const struct quat *q);
  143. EXPORT void quat_interpolate(struct quat *dst, const struct quat *q1,
  144. const struct quat *q2, float t);
  145. EXPORT void quat_get_tangent(struct quat *dst, const struct quat *prev,
  146. const struct quat *q, const struct quat *next);
  147. EXPORT void quat_interpolate_cubic(struct quat *dst, const struct quat *q1,
  148. const struct quat *q2, const struct quat *m1,
  149. const struct quat *m2, float t);
  150. #ifdef __cplusplus
  151. }
  152. #endif