matrix4.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "math-defs.h"
  15. #include "matrix4.h"
  16. #include "matrix3.h"
  17. #include "quat.h"
  18. void matrix4_from_matrix3(struct matrix4 *dst, const struct matrix3 *m)
  19. {
  20. dst->x.m = m->x.m;
  21. dst->y.m = m->y.m;
  22. dst->z.m = m->z.m;
  23. dst->t.m = m->t.m;
  24. dst->t.w = 1.0f;
  25. }
  26. void matrix4_from_quat(struct matrix4 *dst, const struct quat *q)
  27. {
  28. float norm = quat_dot(q, q);
  29. float s = (norm > 0.0f) ? (2.0f / norm) : 0.0f;
  30. float xx = q->x * q->x * s;
  31. float yy = q->y * q->y * s;
  32. float zz = q->z * q->z * s;
  33. float xy = q->x * q->y * s;
  34. float xz = q->x * q->z * s;
  35. float yz = q->y * q->z * s;
  36. float wx = q->w * q->x * s;
  37. float wy = q->w * q->y * s;
  38. float wz = q->w * q->z * s;
  39. vec4_set(&dst->x, 1.0f - (yy + zz), xy + wz, xz - wy, 0.0f);
  40. vec4_set(&dst->y, xy - wz, 1.0f - (xx + zz), yz + wx, 0.0f);
  41. vec4_set(&dst->z, xz + wy, yz - wx, 1.0f - (xx + yy), 0.0f);
  42. vec4_set(&dst->t, 0.0f, 0.0f, 0.0f, 1.0f);
  43. }
  44. void matrix4_from_axisang(struct matrix4 *dst, const struct axisang *aa)
  45. {
  46. struct quat q;
  47. quat_from_axisang(&q, aa);
  48. matrix4_from_quat(dst, &q);
  49. }
  50. void matrix4_mul(struct matrix4 *dst, const struct matrix4 *m1, const struct matrix4 *m2)
  51. {
  52. const struct vec4 *m1v = (const struct vec4 *)m1;
  53. const float *m2f = (const float *)m2;
  54. struct vec4 out[4];
  55. int i, j;
  56. for (i = 0; i < 4; i++) {
  57. for (j = 0; j < 4; j++) {
  58. struct vec4 temp;
  59. vec4_set(&temp, m2f[j], m2f[j + 4], m2f[j + 8], m2f[j + 12]);
  60. out[i].ptr[j] = vec4_dot(&m1v[i], &temp);
  61. }
  62. }
  63. matrix4_copy(dst, (struct matrix4 *)out);
  64. }
  65. static inline void get_3x3_submatrix(float *dst, const struct matrix4 *m, int i, int j)
  66. {
  67. const float *mf = (const float *)m;
  68. int ti, tj, idst, jdst;
  69. for (ti = 0; ti < 4; ti++) {
  70. if (ti < i)
  71. idst = ti;
  72. else if (ti > i)
  73. idst = ti - 1;
  74. else
  75. continue;
  76. for (tj = 0; tj < 4; tj++) {
  77. if (tj < j)
  78. jdst = tj;
  79. else if (tj > j)
  80. jdst = tj - 1;
  81. else
  82. continue;
  83. dst[(idst * 3) + jdst] = mf[(ti * 4) + tj];
  84. }
  85. }
  86. }
  87. static inline float get_3x3_determinant(const float *m)
  88. {
  89. return (m[0] * ((m[4] * m[8]) - (m[7] * m[5]))) - (m[1] * ((m[3] * m[8]) - (m[6] * m[5]))) +
  90. (m[2] * ((m[3] * m[7]) - (m[6] * m[4])));
  91. }
  92. float matrix4_determinant(const struct matrix4 *m)
  93. {
  94. const float *mf = (const float *)m;
  95. float det, result = 0.0f, i = 1.0f;
  96. float m3x3[9];
  97. int n;
  98. for (n = 0; n < 4; n++, i = -i) { // NOLINT(clang-tidy-cert-flp30-c)
  99. get_3x3_submatrix(m3x3, m, 0, n);
  100. det = get_3x3_determinant(m3x3);
  101. result += mf[n] * det * i;
  102. }
  103. return result;
  104. }
  105. void matrix4_translate3v(struct matrix4 *dst, const struct matrix4 *m, const struct vec3 *v)
  106. {
  107. struct matrix4 temp;
  108. vec4_set(&temp.x, 1.0f, 0.0f, 0.0f, 0.0f);
  109. vec4_set(&temp.y, 0.0f, 1.0f, 0.0f, 0.0f);
  110. vec4_set(&temp.z, 0.0f, 0.0f, 1.0f, 0.0f);
  111. vec4_from_vec3(&temp.t, v);
  112. matrix4_mul(dst, m, &temp);
  113. }
  114. void matrix4_translate4v(struct matrix4 *dst, const struct matrix4 *m, const struct vec4 *v)
  115. {
  116. struct matrix4 temp;
  117. vec4_set(&temp.x, 1.0f, 0.0f, 0.0f, 0.0f);
  118. vec4_set(&temp.y, 0.0f, 1.0f, 0.0f, 0.0f);
  119. vec4_set(&temp.z, 0.0f, 0.0f, 1.0f, 0.0f);
  120. vec4_copy(&temp.t, v);
  121. matrix4_mul(dst, m, &temp);
  122. }
  123. void matrix4_rotate(struct matrix4 *dst, const struct matrix4 *m, const struct quat *q)
  124. {
  125. struct matrix4 temp;
  126. matrix4_from_quat(&temp, q);
  127. matrix4_mul(dst, m, &temp);
  128. }
  129. void matrix4_rotate_aa(struct matrix4 *dst, const struct matrix4 *m, const struct axisang *aa)
  130. {
  131. struct matrix4 temp;
  132. matrix4_from_axisang(&temp, aa);
  133. matrix4_mul(dst, m, &temp);
  134. }
  135. void matrix4_scale(struct matrix4 *dst, const struct matrix4 *m, const struct vec3 *v)
  136. {
  137. struct matrix4 temp;
  138. vec4_set(&temp.x, v->x, 0.0f, 0.0f, 0.0f);
  139. vec4_set(&temp.y, 0.0f, v->y, 0.0f, 0.0f);
  140. vec4_set(&temp.z, 0.0f, 0.0f, v->z, 0.0f);
  141. vec4_set(&temp.t, 0.0f, 0.0f, 0.0f, 1.0f);
  142. matrix4_mul(dst, m, &temp);
  143. }
  144. void matrix4_translate3v_i(struct matrix4 *dst, const struct vec3 *v, const struct matrix4 *m)
  145. {
  146. struct matrix4 temp;
  147. vec4_set(&temp.x, 1.0f, 0.0f, 0.0f, 0.0f);
  148. vec4_set(&temp.y, 0.0f, 1.0f, 0.0f, 0.0f);
  149. vec4_set(&temp.z, 0.0f, 0.0f, 1.0f, 0.0f);
  150. vec4_from_vec3(&temp.t, v);
  151. matrix4_mul(dst, &temp, m);
  152. }
  153. void matrix4_translate4v_i(struct matrix4 *dst, const struct vec4 *v, const struct matrix4 *m)
  154. {
  155. struct matrix4 temp;
  156. vec4_set(&temp.x, 1.0f, 0.0f, 0.0f, 0.0f);
  157. vec4_set(&temp.y, 0.0f, 1.0f, 0.0f, 0.0f);
  158. vec4_set(&temp.z, 0.0f, 0.0f, 1.0f, 0.0f);
  159. vec4_copy(&temp.t, v);
  160. matrix4_mul(dst, &temp, m);
  161. }
  162. void matrix4_rotate_i(struct matrix4 *dst, const struct quat *q, const struct matrix4 *m)
  163. {
  164. struct matrix4 temp;
  165. matrix4_from_quat(&temp, q);
  166. matrix4_mul(dst, &temp, m);
  167. }
  168. void matrix4_rotate_aa_i(struct matrix4 *dst, const struct axisang *aa, const struct matrix4 *m)
  169. {
  170. struct matrix4 temp;
  171. matrix4_from_axisang(&temp, aa);
  172. matrix4_mul(dst, &temp, m);
  173. }
  174. void matrix4_scale_i(struct matrix4 *dst, const struct vec3 *v, const struct matrix4 *m)
  175. {
  176. struct matrix4 temp;
  177. vec4_set(&temp.x, v->x, 0.0f, 0.0f, 0.0f);
  178. vec4_set(&temp.y, 0.0f, v->y, 0.0f, 0.0f);
  179. vec4_set(&temp.z, 0.0f, 0.0f, v->z, 0.0f);
  180. vec4_set(&temp.t, 0.0f, 0.0f, 0.0f, 1.0f);
  181. matrix4_mul(dst, &temp, m);
  182. }
  183. bool matrix4_inv(struct matrix4 *dst, const struct matrix4 *m)
  184. {
  185. struct vec4 *dstv;
  186. float det;
  187. float m3x3[9];
  188. int i, j, sign;
  189. if (dst == m) {
  190. struct matrix4 temp = *m;
  191. return matrix4_inv(dst, &temp);
  192. }
  193. dstv = (struct vec4 *)dst;
  194. det = matrix4_determinant(m);
  195. if (fabs(det) < 0.0005f)
  196. return false;
  197. for (i = 0; i < 4; i++) {
  198. for (j = 0; j < 4; j++) {
  199. sign = 1 - ((i + j) % 2) * 2;
  200. get_3x3_submatrix(m3x3, m, i, j);
  201. dstv[j].ptr[i] = get_3x3_determinant(m3x3) * (float)sign / det;
  202. }
  203. }
  204. return true;
  205. }
  206. void matrix4_transpose(struct matrix4 *dst, const struct matrix4 *m)
  207. {
  208. if (dst == m) {
  209. struct matrix4 temp = *m;
  210. matrix4_transpose(dst, &temp);
  211. return;
  212. }
  213. #ifdef NO_INTRINSICS
  214. dst->x.x = m->x.x;
  215. dst->x.y = m->y.x;
  216. dst->x.z = m->z.x;
  217. dst->x.w = m->t.x;
  218. dst->y.x = m->x.y;
  219. dst->y.y = m->y.y;
  220. dst->y.z = m->z.y;
  221. dst->y.w = m->t.y;
  222. dst->z.x = m->x.z;
  223. dst->z.y = m->y.z;
  224. dst->z.z = m->z.z;
  225. dst->z.w = m->t.z;
  226. dst->t.x = m->x.w;
  227. dst->t.y = m->y.w;
  228. dst->t.z = m->z.w;
  229. dst->t.w = m->t.w;
  230. #else
  231. __m128 a0 = _mm_unpacklo_ps(m->x.m, m->z.m);
  232. __m128 a1 = _mm_unpacklo_ps(m->y.m, m->t.m);
  233. __m128 a2 = _mm_unpackhi_ps(m->x.m, m->z.m);
  234. __m128 a3 = _mm_unpackhi_ps(m->y.m, m->t.m);
  235. dst->x.m = _mm_unpacklo_ps(a0, a1);
  236. dst->y.m = _mm_unpackhi_ps(a0, a1);
  237. dst->z.m = _mm_unpacklo_ps(a2, a3);
  238. dst->t.m = _mm_unpackhi_ps(a2, a3);
  239. #endif
  240. }