matrix4.c 7.2 KB

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