quat.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
  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. #include "quat.h"
  15. #include "vec3.h"
  16. #include "matrix3.h"
  17. #include "matrix4.h"
  18. #include "axisang.h"
  19. static inline void quat_vec3(struct vec3 *v, const struct quat *q)
  20. {
  21. v->m = q->m;
  22. v->w = 0.0f;
  23. }
  24. void quat_mul(struct quat *dst, const struct quat *q1, const struct quat *q2)
  25. {
  26. struct vec3 q1axis, q2axis;
  27. struct vec3 temp1, temp2;
  28. quat_vec3(&q1axis, q1);
  29. quat_vec3(&q2axis, q2);
  30. vec3_mulf(&temp1, &q2axis, q1->w);
  31. vec3_mulf(&temp2, &q1axis, q2->w);
  32. vec3_add(&temp1, &temp1, &temp2);
  33. vec3_cross(&temp2, &q1axis, &q2axis);
  34. vec3_add((struct vec3 *)dst, &temp1, &temp2);
  35. dst->w = (q1->w * q2->w) - vec3_dot(&q1axis, &q2axis);
  36. }
  37. void quat_from_axisang(struct quat *dst, const struct axisang *aa)
  38. {
  39. float halfa = aa->w * 0.5f;
  40. float sine = sinf(halfa);
  41. dst->x = aa->x * sine;
  42. dst->y = aa->y * sine;
  43. dst->z = aa->z * sine;
  44. dst->w = cosf(halfa);
  45. }
  46. struct f4x4 {
  47. float ptr[4][4];
  48. };
  49. void quat_from_matrix3(struct quat *dst, const struct matrix3 *m)
  50. {
  51. quat_from_matrix4(dst, (const struct matrix4 *)m);
  52. }
  53. void quat_from_matrix4(struct quat *dst, const struct matrix4 *m)
  54. {
  55. float tr = (m->x.x + m->y.y + m->z.z);
  56. float inv_half;
  57. float four_d;
  58. int i, j, k;
  59. if (tr > 0.0f) {
  60. four_d = sqrtf(tr + 1.0f);
  61. dst->w = four_d * 0.5f;
  62. inv_half = 0.5f / four_d;
  63. dst->x = (m->y.z - m->z.y) * inv_half;
  64. dst->y = (m->z.x - m->x.z) * inv_half;
  65. dst->z = (m->x.y - m->y.x) * inv_half;
  66. } else {
  67. struct f4x4 *val = (struct f4x4 *)m;
  68. i = (m->x.x > m->y.y) ? 0 : 1;
  69. if (m->z.z > val->ptr[i][i])
  70. i = 2;
  71. j = (i + 1) % 3;
  72. k = (i + 2) % 3;
  73. /* ---------------------------------- */
  74. four_d = sqrtf((val->ptr[i][i] - val->ptr[j][j] - val->ptr[k][k]) + 1.0f);
  75. dst->ptr[i] = four_d * 0.5f;
  76. inv_half = 0.5f / four_d;
  77. dst->ptr[j] = (val->ptr[i][j] + val->ptr[j][i]) * inv_half;
  78. dst->ptr[k] = (val->ptr[i][k] + val->ptr[k][i]) * inv_half;
  79. dst->w = (val->ptr[j][k] - val->ptr[k][j]) * inv_half;
  80. }
  81. }
  82. void quat_get_dir(struct vec3 *dst, const struct quat *q)
  83. {
  84. struct matrix3 m;
  85. matrix3_from_quat(&m, q);
  86. vec3_copy(dst, &m.z);
  87. }
  88. void quat_set_look_dir(struct quat *dst, const struct vec3 *dir)
  89. {
  90. struct vec3 new_dir;
  91. struct quat xz_rot, yz_rot;
  92. bool xz_valid;
  93. bool yz_valid;
  94. struct axisang aa;
  95. vec3_norm(&new_dir, dir);
  96. vec3_neg(&new_dir, &new_dir);
  97. quat_identity(&xz_rot);
  98. quat_identity(&yz_rot);
  99. xz_valid = close_float(new_dir.x, 0.0f, EPSILON) || close_float(new_dir.z, 0.0f, EPSILON);
  100. yz_valid = close_float(new_dir.y, 0.0f, EPSILON);
  101. if (xz_valid) {
  102. axisang_set(&aa, 0.0f, 1.0f, 0.0f, atan2f(new_dir.x, new_dir.z));
  103. quat_from_axisang(&xz_rot, &aa);
  104. }
  105. if (yz_valid) {
  106. axisang_set(&aa, -1.0f, 0.0f, 0.0f, asinf(new_dir.y));
  107. quat_from_axisang(&yz_rot, &aa);
  108. }
  109. if (!xz_valid)
  110. quat_copy(dst, &yz_rot);
  111. else if (!yz_valid)
  112. quat_copy(dst, &xz_rot);
  113. else
  114. quat_mul(dst, &xz_rot, &yz_rot);
  115. }
  116. void quat_log(struct quat *dst, const struct quat *q)
  117. {
  118. float angle = acosf(q->w);
  119. float sine = sinf(angle);
  120. float w = q->w;
  121. quat_copy(dst, q);
  122. dst->w = 0.0f;
  123. if ((fabsf(w) < 1.0f) && (fabsf(sine) >= EPSILON)) {
  124. sine = angle / sine;
  125. quat_mulf(dst, dst, sine);
  126. }
  127. }
  128. void quat_exp(struct quat *dst, const struct quat *q)
  129. {
  130. float length = sqrtf(q->x * q->x + q->y * q->y + q->z * q->z);
  131. float sine = sinf(length);
  132. quat_copy(dst, q);
  133. sine = (length > EPSILON) ? (sine / length) : 1.0f;
  134. quat_mulf(dst, dst, sine);
  135. dst->w = cosf(length);
  136. }
  137. void quat_interpolate(struct quat *dst, const struct quat *q1, const struct quat *q2, float t)
  138. {
  139. float dot = quat_dot(q1, q2);
  140. float anglef = acosf(dot);
  141. float sine, sinei, sinet, sineti;
  142. struct quat temp;
  143. if (anglef >= EPSILON) {
  144. sine = sinf(anglef);
  145. sinei = 1 / sine;
  146. sinet = sinf(anglef * t) * sinei;
  147. sineti = sinf(anglef * (1.0f - t)) * sinei;
  148. quat_mulf(&temp, q1, sineti);
  149. quat_mulf(dst, q2, sinet);
  150. quat_add(dst, &temp, dst);
  151. } else {
  152. quat_sub(&temp, q2, q1);
  153. quat_mulf(&temp, &temp, t);
  154. quat_add(dst, &temp, q1);
  155. }
  156. }
  157. void quat_get_tangent(struct quat *dst, const struct quat *prev, const struct quat *q, const struct quat *next)
  158. {
  159. struct quat temp;
  160. quat_sub(&temp, q, prev);
  161. quat_add(&temp, &temp, next);
  162. quat_sub(&temp, &temp, q);
  163. quat_mulf(dst, &temp, 0.5f);
  164. }
  165. void quat_interpolate_cubic(struct quat *dst, const struct quat *q1, const struct quat *q2, const struct quat *m1,
  166. const struct quat *m2, float t)
  167. {
  168. struct quat temp1, temp2;
  169. quat_interpolate(&temp1, q1, q2, t);
  170. quat_interpolate(&temp2, m1, m2, t);
  171. quat_interpolate(dst, &temp1, &temp2, 2.0f * (1.0f - t) * t);
  172. }