quat.c 5.2 KB

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