quat.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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, float w)
  47. {
  48. dst->m = _mm_set_ps(x, y, z, w);
  49. }
  50. static inline void quat_copy(struct quat *dst, const struct quat *q)
  51. {
  52. dst->m = q->m;
  53. }
  54. static inline void quat_add(struct quat *dst, const struct quat *q1, 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, const struct quat *q2)
  59. {
  60. dst->m = _mm_sub_ps(q1->m, q2->m);
  61. }
  62. EXPORT void quat_mul(struct quat *dst, const struct quat *q1, const struct quat *q2);
  63. static inline void quat_addf(struct quat *dst, const struct quat *q, float f)
  64. {
  65. dst->m = _mm_add_ps(q->m, _mm_set1_ps(f));
  66. }
  67. static inline void quat_subf(struct quat *dst, const struct quat *q, float f)
  68. {
  69. dst->m = _mm_sub_ps(q->m, _mm_set1_ps(f));
  70. }
  71. static inline void quat_mulf(struct quat *dst, const struct quat *q, float f)
  72. {
  73. dst->m = _mm_mul_ps(q->m, _mm_set1_ps(f));
  74. }
  75. static inline void quat_divf(struct quat *dst, const struct quat *q, float f)
  76. {
  77. dst->m = _mm_div_ps(q->m, _mm_set1_ps(f));
  78. }
  79. static inline float quat_dot(const struct quat *q1, const struct quat *q2)
  80. {
  81. struct vec3 add;
  82. __m128 mul = _mm_mul_ps(q1->m, q2->m);
  83. add.m = _mm_add_ps(_mm_movehl_ps(mul, mul), mul);
  84. add.m = _mm_add_ps(_mm_shuffle_ps(add.m, add.m, 0x55), add.m);
  85. return add.x;
  86. }
  87. static inline void quat_inv(struct quat *dst, const struct quat *q)
  88. {
  89. dst->x = -q->x;
  90. dst->y = -q->y;
  91. dst->z = -q->z;
  92. }
  93. static inline void quat_neg(struct quat *dst, const struct quat *q)
  94. {
  95. dst->x = -q->x;
  96. dst->y = -q->y;
  97. dst->z = -q->z;
  98. dst->w = -q->w;
  99. }
  100. static inline float quat_len(const struct quat *q)
  101. {
  102. float dot_val = quat_dot(q, q);
  103. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  104. }
  105. static inline float quat_dist(const struct quat *q1, const struct quat *q2)
  106. {
  107. struct quat temp;
  108. float dot_val;
  109. quat_sub(&temp, q1, q2);
  110. dot_val = quat_dot(&temp, &temp);
  111. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  112. }
  113. static inline void quat_norm(struct quat *dst, const struct quat *q)
  114. {
  115. float dot_val = quat_dot(q, q);
  116. dst->m = (dot_val > 0.0f) ? _mm_mul_ps(q->m, _mm_set1_ps(1.0f / sqrtf(dot_val))) : _mm_setzero_ps();
  117. }
  118. static inline bool quat_close(const struct quat *q1, const struct quat *q2, float epsilon)
  119. {
  120. struct quat test;
  121. quat_sub(&test, q1, q2);
  122. return test.x < epsilon && test.y < epsilon && test.z < epsilon && test.w < epsilon;
  123. }
  124. EXPORT void quat_from_axisang(struct quat *dst, const struct axisang *aa);
  125. EXPORT void quat_from_matrix3(struct quat *dst, const struct matrix3 *m);
  126. EXPORT void quat_from_matrix4(struct quat *dst, const struct matrix4 *m);
  127. EXPORT void quat_get_dir(struct vec3 *dst, const struct quat *q);
  128. EXPORT void quat_set_look_dir(struct quat *dst, const struct vec3 *dir);
  129. EXPORT void quat_log(struct quat *dst, const struct quat *q);
  130. EXPORT void quat_exp(struct quat *dst, const struct quat *q);
  131. EXPORT void quat_interpolate(struct quat *dst, const struct quat *q1, const struct quat *q2, float t);
  132. EXPORT void quat_get_tangent(struct quat *dst, const struct quat *prev, const struct quat *q, const struct quat *next);
  133. EXPORT void quat_interpolate_cubic(struct quat *dst, const struct quat *q1, const struct quat *q2,
  134. const struct quat *m1, const struct quat *m2, float t);
  135. #ifdef __cplusplus
  136. }
  137. #endif