1
0

quat.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "../util/sse-intrin.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 {
  35. float x, y, z, w;
  36. };
  37. float ptr[4];
  38. __m128 m;
  39. };
  40. };
  41. static inline void quat_identity(struct quat *q)
  42. {
  43. q->m = _mm_setzero_ps();
  44. q->w = 1.0f;
  45. }
  46. static inline void quat_set(struct quat *dst, float x, float y, float z,
  47. float w)
  48. {
  49. dst->m = _mm_set_ps(x, y, z, w);
  50. }
  51. static inline void quat_copy(struct quat *dst, const struct quat *q)
  52. {
  53. dst->m = q->m;
  54. }
  55. static inline void quat_add(struct quat *dst, const struct quat *q1,
  56. const struct quat *q2)
  57. {
  58. dst->m = _mm_add_ps(q1->m, q2->m);
  59. }
  60. static inline void quat_sub(struct quat *dst, const struct quat *q1,
  61. const struct quat *q2)
  62. {
  63. dst->m = _mm_sub_ps(q1->m, q2->m);
  64. }
  65. EXPORT void quat_mul(struct quat *dst, const struct quat *q1,
  66. const struct quat *q2);
  67. static inline void quat_addf(struct quat *dst, const struct quat *q, float f)
  68. {
  69. dst->m = _mm_add_ps(q->m, _mm_set1_ps(f));
  70. }
  71. static inline void quat_subf(struct quat *dst, const struct quat *q, 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, float f)
  76. {
  77. dst->m = _mm_mul_ps(q->m, _mm_set1_ps(f));
  78. }
  79. static inline void quat_divf(struct quat *dst, const struct quat *q, float f)
  80. {
  81. dst->m = _mm_div_ps(q->m, _mm_set1_ps(f));
  82. }
  83. static inline float quat_dot(const struct quat *q1, const struct quat *q2)
  84. {
  85. struct vec3 add;
  86. __m128 mul = _mm_mul_ps(q1->m, q2->m);
  87. add.m = _mm_add_ps(_mm_movehl_ps(mul, mul), mul);
  88. add.m = _mm_add_ps(_mm_shuffle_ps(add.m, add.m, 0x55), add.m);
  89. return add.x;
  90. }
  91. static inline void quat_inv(struct quat *dst, const struct quat *q)
  92. {
  93. dst->x = -q->x;
  94. dst->y = -q->y;
  95. dst->z = -q->z;
  96. }
  97. static inline void quat_neg(struct quat *dst, const struct quat *q)
  98. {
  99. dst->x = -q->x;
  100. dst->y = -q->y;
  101. dst->z = -q->z;
  102. dst->w = -q->w;
  103. }
  104. static inline float quat_len(const struct quat *q)
  105. {
  106. float dot_val = quat_dot(q, q);
  107. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  108. }
  109. static inline float quat_dist(const struct quat *q1, const struct quat *q2)
  110. {
  111. struct quat temp;
  112. float dot_val;
  113. quat_sub(&temp, q1, q2);
  114. dot_val = quat_dot(&temp, &temp);
  115. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  116. }
  117. static inline void quat_norm(struct quat *dst, const struct quat *q)
  118. {
  119. float dot_val = quat_dot(q, q);
  120. dst->m = (dot_val > 0.0f)
  121. ? _mm_mul_ps(q->m, _mm_set1_ps(1.0f / sqrtf(dot_val)))
  122. : _mm_setzero_ps();
  123. }
  124. static inline bool quat_close(const struct quat *q1, const struct quat *q2,
  125. float epsilon)
  126. {
  127. struct quat test;
  128. quat_sub(&test, q1, q2);
  129. return test.x < epsilon && test.y < epsilon && test.z < epsilon &&
  130. test.w < epsilon;
  131. }
  132. EXPORT void quat_from_axisang(struct quat *dst, const struct axisang *aa);
  133. EXPORT void quat_from_matrix3(struct quat *dst, const struct matrix3 *m);
  134. EXPORT void quat_from_matrix4(struct quat *dst, const struct matrix4 *m);
  135. EXPORT void quat_get_dir(struct vec3 *dst, const struct quat *q);
  136. EXPORT void quat_set_look_dir(struct quat *dst, const struct vec3 *dir);
  137. EXPORT void quat_log(struct quat *dst, const struct quat *q);
  138. EXPORT void quat_exp(struct quat *dst, const struct quat *q);
  139. EXPORT void quat_interpolate(struct quat *dst, const struct quat *q1,
  140. const struct quat *q2, float t);
  141. EXPORT void quat_get_tangent(struct quat *dst, const struct quat *prev,
  142. const struct quat *q, const struct quat *next);
  143. EXPORT void quat_interpolate_cubic(struct quat *dst, const struct quat *q1,
  144. const struct quat *q2, const struct quat *m1,
  145. const struct quat *m2, float t);
  146. #ifdef __cplusplus
  147. }
  148. #endif