quat.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #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(
  75. (val->ptr[i][i] - val->ptr[j][j] - val->ptr[k][k]) +
  76. 1.0f);
  77. dst->ptr[i] = four_d * 0.5f;
  78. inv_half = 0.5f / four_d;
  79. dst->ptr[j] = (val->ptr[i][j] + val->ptr[j][i]) * inv_half;
  80. dst->ptr[k] = (val->ptr[i][k] + val->ptr[k][i]) * inv_half;
  81. dst->w = (val->ptr[j][k] - val->ptr[k][j]) * inv_half;
  82. }
  83. }
  84. void quat_get_dir(struct vec3 *dst, const struct quat *q)
  85. {
  86. struct matrix3 m;
  87. matrix3_from_quat(&m, q);
  88. vec3_copy(dst, &m.z);
  89. }
  90. void quat_set_look_dir(struct quat *dst, const struct vec3 *dir)
  91. {
  92. struct vec3 new_dir;
  93. struct quat xz_rot, yz_rot;
  94. bool xz_valid;
  95. bool yz_valid;
  96. struct axisang aa;
  97. vec3_norm(&new_dir, dir);
  98. vec3_neg(&new_dir, &new_dir);
  99. quat_identity(&xz_rot);
  100. quat_identity(&yz_rot);
  101. xz_valid = close_float(new_dir.x, 0.0f, EPSILON) ||
  102. close_float(new_dir.z, 0.0f, EPSILON);
  103. yz_valid = close_float(new_dir.y, 0.0f, EPSILON);
  104. if (xz_valid) {
  105. axisang_set(&aa, 0.0f, 1.0f, 0.0f,
  106. atan2f(new_dir.x, new_dir.z));
  107. quat_from_axisang(&xz_rot, &aa);
  108. }
  109. if (yz_valid) {
  110. axisang_set(&aa, -1.0f, 0.0f, 0.0f, asinf(new_dir.y));
  111. quat_from_axisang(&yz_rot, &aa);
  112. }
  113. if (!xz_valid)
  114. quat_copy(dst, &yz_rot);
  115. else if (!yz_valid)
  116. quat_copy(dst, &xz_rot);
  117. else
  118. quat_mul(dst, &xz_rot, &yz_rot);
  119. }
  120. void quat_log(struct quat *dst, const struct quat *q)
  121. {
  122. float angle = acosf(q->w);
  123. float sine = sinf(angle);
  124. float w = q->w;
  125. quat_copy(dst, q);
  126. dst->w = 0.0f;
  127. if ((fabsf(w) < 1.0f) && (fabsf(sine) >= EPSILON)) {
  128. sine = angle / sine;
  129. quat_mulf(dst, dst, sine);
  130. }
  131. }
  132. void quat_exp(struct quat *dst, const struct quat *q)
  133. {
  134. float length = sqrtf(q->x * q->x + q->y * q->y + q->z * q->z);
  135. float sine = sinf(length);
  136. quat_copy(dst, q);
  137. sine = (length > EPSILON) ? (sine / length) : 1.0f;
  138. quat_mulf(dst, dst, sine);
  139. dst->w = cosf(length);
  140. }
  141. void quat_interpolate(struct quat *dst, const struct quat *q1,
  142. const struct quat *q2, float t)
  143. {
  144. float dot = quat_dot(q1, q2);
  145. float anglef = acosf(dot);
  146. float sine, sinei, sinet, sineti;
  147. struct quat temp;
  148. if (anglef >= EPSILON) {
  149. sine = sinf(anglef);
  150. sinei = 1 / sine;
  151. sinet = sinf(anglef * t) * sinei;
  152. sineti = sinf(anglef * (1.0f - t)) * sinei;
  153. quat_mulf(&temp, q1, sineti);
  154. quat_mulf(dst, q2, sinet);
  155. quat_add(dst, &temp, dst);
  156. } else {
  157. quat_sub(&temp, q2, q1);
  158. quat_mulf(&temp, &temp, t);
  159. quat_add(dst, &temp, q1);
  160. }
  161. }
  162. void quat_get_tangent(struct quat *dst, const struct quat *prev,
  163. const struct quat *q, const struct quat *next)
  164. {
  165. struct quat temp;
  166. quat_sub(&temp, q, prev);
  167. quat_add(&temp, &temp, next);
  168. quat_sub(&temp, &temp, q);
  169. quat_mulf(dst, &temp, 0.5f);
  170. }
  171. void quat_interpolate_cubic(struct quat *dst, const struct quat *q1,
  172. const struct quat *q2, const struct quat *m1,
  173. const struct quat *m2, float t)
  174. {
  175. struct quat temp1, temp2;
  176. quat_interpolate(&temp1, q1, q2, t);
  177. quat_interpolate(&temp2, m1, m2, t);
  178. quat_interpolate(dst, &temp1, &temp2, 2.0f * (1.0f - t) * t);
  179. }