matrix4.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #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. struct matrix4 transposed;
  53. struct matrix4 out;
  54. matrix4_transpose(&transposed, m2);
  55. out.x.x = vec4_dot(&m1->x, &transposed.x);
  56. out.x.y = vec4_dot(&m1->x, &transposed.y);
  57. out.x.z = vec4_dot(&m1->x, &transposed.z);
  58. out.x.w = vec4_dot(&m1->x, &transposed.t);
  59. out.y.x = vec4_dot(&m1->y, &transposed.x);
  60. out.y.y = vec4_dot(&m1->y, &transposed.y);
  61. out.y.z = vec4_dot(&m1->y, &transposed.z);
  62. out.y.w = vec4_dot(&m1->y, &transposed.t);
  63. out.z.x = vec4_dot(&m1->z, &transposed.x);
  64. out.z.y = vec4_dot(&m1->z, &transposed.y);
  65. out.z.z = vec4_dot(&m1->z, &transposed.z);
  66. out.z.w = vec4_dot(&m1->z, &transposed.t);
  67. out.t.x = vec4_dot(&m1->t, &transposed.x);
  68. out.t.y = vec4_dot(&m1->t, &transposed.y);
  69. out.t.z = vec4_dot(&m1->t, &transposed.z);
  70. out.t.w = vec4_dot(&m1->t, &transposed.t);
  71. matrix4_copy(dst, &out);
  72. }
  73. void matrix4_mul_4x3_only(struct matrix4 *dst, const struct matrix4 *m1, const struct matrix4 *m2)
  74. {
  75. struct matrix4 transposed;
  76. struct vec4 x;
  77. struct vec4 y;
  78. struct vec4 z;
  79. matrix4_transpose(&transposed, m2);
  80. x.x = vec4_dot(&m1->x, &transposed.x);
  81. x.y = vec4_dot(&m1->x, &transposed.y);
  82. x.z = vec4_dot(&m1->x, &transposed.z);
  83. x.w = vec4_dot(&m1->x, &transposed.t);
  84. y.x = vec4_dot(&m1->y, &transposed.x);
  85. y.y = vec4_dot(&m1->y, &transposed.y);
  86. y.z = vec4_dot(&m1->y, &transposed.z);
  87. y.w = vec4_dot(&m1->y, &transposed.t);
  88. z.x = vec4_dot(&m1->z, &transposed.x);
  89. z.y = vec4_dot(&m1->z, &transposed.y);
  90. z.z = vec4_dot(&m1->z, &transposed.z);
  91. z.w = vec4_dot(&m1->z, &transposed.t);
  92. vec4_copy(&dst->x, &x);
  93. vec4_copy(&dst->y, &y);
  94. vec4_copy(&dst->z, &z);
  95. vec4_copy(&dst->t, &m2->t);
  96. }
  97. static inline void get_3x3_submatrix(float *dst, const struct matrix4 *m, int i, int j)
  98. {
  99. const float *mf = (const float *)m;
  100. int ti, tj, idst, jdst;
  101. for (ti = 0; ti < 4; ti++) {
  102. if (ti < i)
  103. idst = ti;
  104. else if (ti > i)
  105. idst = ti - 1;
  106. else
  107. continue;
  108. for (tj = 0; tj < 4; tj++) {
  109. if (tj < j)
  110. jdst = tj;
  111. else if (tj > j)
  112. jdst = tj - 1;
  113. else
  114. continue;
  115. dst[(idst * 3) + jdst] = mf[(ti * 4) + tj];
  116. }
  117. }
  118. }
  119. static inline float get_3x3_determinant(const float *m)
  120. {
  121. return (m[0] * ((m[4] * m[8]) - (m[7] * m[5]))) - (m[1] * ((m[3] * m[8]) - (m[6] * m[5]))) +
  122. (m[2] * ((m[3] * m[7]) - (m[6] * m[4])));
  123. }
  124. float matrix4_determinant(const struct matrix4 *m)
  125. {
  126. const float *mf = (const float *)m;
  127. float det, result = 0.0f, i = 1.0f;
  128. float m3x3[9];
  129. int n;
  130. for (n = 0; n < 4; n++, i = -i) { // NOLINT(clang-tidy-cert-flp30-c)
  131. get_3x3_submatrix(m3x3, m, 0, n);
  132. det = get_3x3_determinant(m3x3);
  133. result += mf[n] * det * i;
  134. }
  135. return result;
  136. }
  137. void matrix4_translate3v(struct matrix4 *dst, const struct matrix4 *m, const struct vec3 *v)
  138. {
  139. struct matrix4 temp;
  140. vec4_set(&temp.x, 1.0f, 0.0f, 0.0f, 0.0f);
  141. vec4_set(&temp.y, 0.0f, 1.0f, 0.0f, 0.0f);
  142. vec4_set(&temp.z, 0.0f, 0.0f, 1.0f, 0.0f);
  143. vec4_from_vec3(&temp.t, v);
  144. matrix4_mul(dst, m, &temp);
  145. }
  146. void matrix4_translate4v(struct matrix4 *dst, const struct matrix4 *m, const struct vec4 *v)
  147. {
  148. struct matrix4 temp;
  149. vec4_set(&temp.x, 1.0f, 0.0f, 0.0f, 0.0f);
  150. vec4_set(&temp.y, 0.0f, 1.0f, 0.0f, 0.0f);
  151. vec4_set(&temp.z, 0.0f, 0.0f, 1.0f, 0.0f);
  152. vec4_copy(&temp.t, v);
  153. matrix4_mul(dst, m, &temp);
  154. }
  155. void matrix4_rotate(struct matrix4 *dst, const struct matrix4 *m, const struct quat *q)
  156. {
  157. struct matrix4 temp;
  158. matrix4_from_quat(&temp, q);
  159. matrix4_mul(dst, m, &temp);
  160. }
  161. void matrix4_rotate_aa(struct matrix4 *dst, const struct matrix4 *m, const struct axisang *aa)
  162. {
  163. struct matrix4 temp;
  164. matrix4_from_axisang(&temp, aa);
  165. matrix4_mul(dst, m, &temp);
  166. }
  167. void matrix4_scale(struct matrix4 *dst, const struct matrix4 *m, const struct vec3 *v)
  168. {
  169. struct matrix4 temp;
  170. vec4_set(&temp.x, v->x, 0.0f, 0.0f, 0.0f);
  171. vec4_set(&temp.y, 0.0f, v->y, 0.0f, 0.0f);
  172. vec4_set(&temp.z, 0.0f, 0.0f, v->z, 0.0f);
  173. vec4_set(&temp.t, 0.0f, 0.0f, 0.0f, 1.0f);
  174. matrix4_mul(dst, m, &temp);
  175. }
  176. void matrix4_translate3v_i(struct matrix4 *dst, const struct vec3 *v, const struct matrix4 *m)
  177. {
  178. struct matrix4 transposed;
  179. struct vec4 v4;
  180. struct vec4 t;
  181. vec4_from_vec3(&v4, v);
  182. matrix4_transpose(&transposed, m);
  183. t.x = vec4_dot(&v4, &transposed.x);
  184. t.y = vec4_dot(&v4, &transposed.y);
  185. t.z = vec4_dot(&v4, &transposed.z);
  186. t.w = vec4_dot(&v4, &transposed.t);
  187. vec4_copy(&dst->x, &m->x);
  188. vec4_copy(&dst->y, &m->y);
  189. vec4_copy(&dst->z, &m->z);
  190. vec4_copy(&dst->t, &t);
  191. }
  192. void matrix4_translate4v_i(struct matrix4 *dst, const struct vec4 *v, const struct matrix4 *m)
  193. {
  194. struct matrix4 transposed;
  195. struct vec4 t;
  196. matrix4_transpose(&transposed, m);
  197. t.x = vec4_dot(v, &transposed.x);
  198. t.y = vec4_dot(v, &transposed.y);
  199. t.z = vec4_dot(v, &transposed.z);
  200. t.w = vec4_dot(v, &transposed.t);
  201. vec4_copy(&dst->x, &m->x);
  202. vec4_copy(&dst->y, &m->y);
  203. vec4_copy(&dst->z, &m->z);
  204. vec4_copy(&dst->t, &t);
  205. }
  206. void matrix4_rotate_i(struct matrix4 *dst, const struct quat *q, const struct matrix4 *m)
  207. {
  208. struct matrix4 temp;
  209. matrix4_from_quat(&temp, q);
  210. matrix4_mul_4x3_only(dst, &temp, m);
  211. }
  212. void matrix4_rotate_aa_i(struct matrix4 *dst, const struct axisang *aa, const struct matrix4 *m)
  213. {
  214. struct matrix4 temp;
  215. matrix4_from_axisang(&temp, aa);
  216. matrix4_mul_4x3_only(dst, &temp, m);
  217. }
  218. void matrix4_scale_i(struct matrix4 *dst, const struct vec3 *v, const struct matrix4 *m)
  219. {
  220. struct matrix4 temp;
  221. vec4_set(&temp.x, v->x, 0.0f, 0.0f, 0.0f);
  222. vec4_set(&temp.y, 0.0f, v->y, 0.0f, 0.0f);
  223. vec4_set(&temp.z, 0.0f, 0.0f, v->z, 0.0f);
  224. vec4_set(&temp.t, 0.0f, 0.0f, 0.0f, 1.0f);
  225. matrix4_mul_4x3_only(dst, &temp, m);
  226. }
  227. bool matrix4_inv(struct matrix4 *dst, const struct matrix4 *m)
  228. {
  229. struct vec4 *dstv;
  230. float det;
  231. float m3x3[9];
  232. int i, j, sign;
  233. if (dst == m) {
  234. struct matrix4 temp = *m;
  235. return matrix4_inv(dst, &temp);
  236. }
  237. dstv = (struct vec4 *)dst;
  238. det = matrix4_determinant(m);
  239. if (fabs(det) < 0.0005f)
  240. return false;
  241. for (i = 0; i < 4; i++) {
  242. for (j = 0; j < 4; j++) {
  243. sign = 1 - ((i + j) % 2) * 2;
  244. get_3x3_submatrix(m3x3, m, i, j);
  245. dstv[j].ptr[i] = get_3x3_determinant(m3x3) * (float)sign / det;
  246. }
  247. }
  248. return true;
  249. }
  250. void matrix4_transpose(struct matrix4 *dst, const struct matrix4 *m)
  251. {
  252. if (dst == m) {
  253. struct matrix4 temp = *m;
  254. matrix4_transpose(dst, &temp);
  255. return;
  256. }
  257. #ifdef NO_INTRINSICS
  258. dst->x.x = m->x.x;
  259. dst->x.y = m->y.x;
  260. dst->x.z = m->z.x;
  261. dst->x.w = m->t.x;
  262. dst->y.x = m->x.y;
  263. dst->y.y = m->y.y;
  264. dst->y.z = m->z.y;
  265. dst->y.w = m->t.y;
  266. dst->z.x = m->x.z;
  267. dst->z.y = m->y.z;
  268. dst->z.z = m->z.z;
  269. dst->z.w = m->t.z;
  270. dst->t.x = m->x.w;
  271. dst->t.y = m->y.w;
  272. dst->t.z = m->z.w;
  273. dst->t.w = m->t.w;
  274. #else
  275. __m128 a0 = _mm_unpacklo_ps(m->x.m, m->z.m);
  276. __m128 a1 = _mm_unpacklo_ps(m->y.m, m->t.m);
  277. __m128 a2 = _mm_unpackhi_ps(m->x.m, m->z.m);
  278. __m128 a3 = _mm_unpackhi_ps(m->y.m, m->t.m);
  279. dst->x.m = _mm_unpacklo_ps(a0, a1);
  280. dst->y.m = _mm_unpackhi_ps(a0, a1);
  281. dst->z.m = _mm_unpacklo_ps(a2, a3);
  282. dst->t.m = _mm_unpackhi_ps(a2, a3);
  283. #endif
  284. }