linmath.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. #ifndef LINMATH_H
  2. #define LINMATH_H
  3. #include <math.h>
  4. #ifdef _MSC_VER
  5. #define inline __inline
  6. #endif
  7. #define LINMATH_H_DEFINE_VEC(n) \
  8. typedef float vec##n[n]; \
  9. static inline void vec##n##_add(vec##n r, vec##n const a, vec##n const b) \
  10. { \
  11. int i; \
  12. for(i=0; i<n; ++i) \
  13. r[i] = a[i] + b[i]; \
  14. } \
  15. static inline void vec##n##_sub(vec##n r, vec##n const a, vec##n const b) \
  16. { \
  17. int i; \
  18. for(i=0; i<n; ++i) \
  19. r[i] = a[i] - b[i]; \
  20. } \
  21. static inline void vec##n##_scale(vec##n r, vec##n const v, float const s) \
  22. { \
  23. int i; \
  24. for(i=0; i<n; ++i) \
  25. r[i] = v[i] * s; \
  26. } \
  27. static inline float vec##n##_mul_inner(vec##n const a, vec##n const b) \
  28. { \
  29. float p = 0.; \
  30. int i; \
  31. for(i=0; i<n; ++i) \
  32. p += b[i]*a[i]; \
  33. return p; \
  34. } \
  35. static inline float vec##n##_len(vec##n const v) \
  36. { \
  37. return (float) sqrt(vec##n##_mul_inner(v,v)); \
  38. } \
  39. static inline void vec##n##_norm(vec##n r, vec##n const v) \
  40. { \
  41. float k = 1.f / vec##n##_len(v); \
  42. vec##n##_scale(r, v, k); \
  43. }
  44. LINMATH_H_DEFINE_VEC(2)
  45. LINMATH_H_DEFINE_VEC(3)
  46. LINMATH_H_DEFINE_VEC(4)
  47. static inline void vec3_mul_cross(vec3 r, vec3 const a, vec3 const b)
  48. {
  49. r[0] = a[1]*b[2] - a[2]*b[1];
  50. r[1] = a[2]*b[0] - a[0]*b[2];
  51. r[2] = a[0]*b[1] - a[1]*b[0];
  52. }
  53. static inline void vec3_reflect(vec3 r, vec3 const v, vec3 const n)
  54. {
  55. float p = 2.f*vec3_mul_inner(v, n);
  56. int i;
  57. for(i=0;i<3;++i)
  58. r[i] = v[i] - p*n[i];
  59. }
  60. static inline void vec4_mul_cross(vec4 r, vec4 a, vec4 b)
  61. {
  62. r[0] = a[1]*b[2] - a[2]*b[1];
  63. r[1] = a[2]*b[0] - a[0]*b[2];
  64. r[2] = a[0]*b[1] - a[1]*b[0];
  65. r[3] = 1.f;
  66. }
  67. static inline void vec4_reflect(vec4 r, vec4 v, vec4 n)
  68. {
  69. float p = 2.f*vec4_mul_inner(v, n);
  70. int i;
  71. for(i=0;i<4;++i)
  72. r[i] = v[i] - p*n[i];
  73. }
  74. typedef vec4 mat4x4[4];
  75. static inline void mat4x4_identity(mat4x4 M)
  76. {
  77. int i, j;
  78. for(i=0; i<4; ++i)
  79. for(j=0; j<4; ++j)
  80. M[i][j] = i==j ? 1.f : 0.f;
  81. }
  82. static inline void mat4x4_dup(mat4x4 M, mat4x4 N)
  83. {
  84. int i, j;
  85. for(i=0; i<4; ++i)
  86. for(j=0; j<4; ++j)
  87. M[i][j] = N[i][j];
  88. }
  89. static inline void mat4x4_row(vec4 r, mat4x4 M, int i)
  90. {
  91. int k;
  92. for(k=0; k<4; ++k)
  93. r[k] = M[k][i];
  94. }
  95. static inline void mat4x4_col(vec4 r, mat4x4 M, int i)
  96. {
  97. int k;
  98. for(k=0; k<4; ++k)
  99. r[k] = M[i][k];
  100. }
  101. static inline void mat4x4_transpose(mat4x4 M, mat4x4 N)
  102. {
  103. int i, j;
  104. for(j=0; j<4; ++j)
  105. for(i=0; i<4; ++i)
  106. M[i][j] = N[j][i];
  107. }
  108. static inline void mat4x4_add(mat4x4 M, mat4x4 a, mat4x4 b)
  109. {
  110. int i;
  111. for(i=0; i<4; ++i)
  112. vec4_add(M[i], a[i], b[i]);
  113. }
  114. static inline void mat4x4_sub(mat4x4 M, mat4x4 a, mat4x4 b)
  115. {
  116. int i;
  117. for(i=0; i<4; ++i)
  118. vec4_sub(M[i], a[i], b[i]);
  119. }
  120. static inline void mat4x4_scale(mat4x4 M, mat4x4 a, float k)
  121. {
  122. int i;
  123. for(i=0; i<4; ++i)
  124. vec4_scale(M[i], a[i], k);
  125. }
  126. static inline void mat4x4_scale_aniso(mat4x4 M, mat4x4 a, float x, float y, float z)
  127. {
  128. int i;
  129. vec4_scale(M[0], a[0], x);
  130. vec4_scale(M[1], a[1], y);
  131. vec4_scale(M[2], a[2], z);
  132. for(i = 0; i < 4; ++i) {
  133. M[3][i] = a[3][i];
  134. }
  135. }
  136. static inline void mat4x4_mul(mat4x4 M, mat4x4 a, mat4x4 b)
  137. {
  138. mat4x4 temp;
  139. int k, r, c;
  140. for(c=0; c<4; ++c) for(r=0; r<4; ++r) {
  141. temp[c][r] = 0.f;
  142. for(k=0; k<4; ++k)
  143. temp[c][r] += a[k][r] * b[c][k];
  144. }
  145. mat4x4_dup(M, temp);
  146. }
  147. static inline void mat4x4_mul_vec4(vec4 r, mat4x4 M, vec4 v)
  148. {
  149. int i, j;
  150. for(j=0; j<4; ++j) {
  151. r[j] = 0.f;
  152. for(i=0; i<4; ++i)
  153. r[j] += M[i][j] * v[i];
  154. }
  155. }
  156. static inline void mat4x4_translate(mat4x4 T, float x, float y, float z)
  157. {
  158. mat4x4_identity(T);
  159. T[3][0] = x;
  160. T[3][1] = y;
  161. T[3][2] = z;
  162. }
  163. static inline void mat4x4_translate_in_place(mat4x4 M, float x, float y, float z)
  164. {
  165. vec4 t = {x, y, z, 0};
  166. vec4 r;
  167. int i;
  168. for (i = 0; i < 4; ++i) {
  169. mat4x4_row(r, M, i);
  170. M[3][i] += vec4_mul_inner(r, t);
  171. }
  172. }
  173. static inline void mat4x4_from_vec3_mul_outer(mat4x4 M, vec3 a, vec3 b)
  174. {
  175. int i, j;
  176. for(i=0; i<4; ++i) for(j=0; j<4; ++j)
  177. M[i][j] = i<3 && j<3 ? a[i] * b[j] : 0.f;
  178. }
  179. static inline void mat4x4_rotate(mat4x4 R, mat4x4 M, float x, float y, float z, float angle)
  180. {
  181. float s = sinf(angle);
  182. float c = cosf(angle);
  183. vec3 u = {x, y, z};
  184. if(vec3_len(u) > 1e-4) {
  185. mat4x4 T, C, S = {{0}};
  186. vec3_norm(u, u);
  187. mat4x4_from_vec3_mul_outer(T, u, u);
  188. S[1][2] = u[0];
  189. S[2][1] = -u[0];
  190. S[2][0] = u[1];
  191. S[0][2] = -u[1];
  192. S[0][1] = u[2];
  193. S[1][0] = -u[2];
  194. mat4x4_scale(S, S, s);
  195. mat4x4_identity(C);
  196. mat4x4_sub(C, C, T);
  197. mat4x4_scale(C, C, c);
  198. mat4x4_add(T, T, C);
  199. mat4x4_add(T, T, S);
  200. T[3][3] = 1.;
  201. mat4x4_mul(R, M, T);
  202. } else {
  203. mat4x4_dup(R, M);
  204. }
  205. }
  206. static inline void mat4x4_rotate_X(mat4x4 Q, mat4x4 M, float angle)
  207. {
  208. float s = sinf(angle);
  209. float c = cosf(angle);
  210. mat4x4 R = {
  211. {1.f, 0.f, 0.f, 0.f},
  212. {0.f, c, s, 0.f},
  213. {0.f, -s, c, 0.f},
  214. {0.f, 0.f, 0.f, 1.f}
  215. };
  216. mat4x4_mul(Q, M, R);
  217. }
  218. static inline void mat4x4_rotate_Y(mat4x4 Q, mat4x4 M, float angle)
  219. {
  220. float s = sinf(angle);
  221. float c = cosf(angle);
  222. mat4x4 R = {
  223. { c, 0.f, -s, 0.f},
  224. { 0.f, 1.f, 0.f, 0.f},
  225. { s, 0.f, c, 0.f},
  226. { 0.f, 0.f, 0.f, 1.f}
  227. };
  228. mat4x4_mul(Q, M, R);
  229. }
  230. static inline void mat4x4_rotate_Z(mat4x4 Q, mat4x4 M, float angle)
  231. {
  232. float s = sinf(angle);
  233. float c = cosf(angle);
  234. mat4x4 R = {
  235. { c, s, 0.f, 0.f},
  236. { -s, c, 0.f, 0.f},
  237. { 0.f, 0.f, 1.f, 0.f},
  238. { 0.f, 0.f, 0.f, 1.f}
  239. };
  240. mat4x4_mul(Q, M, R);
  241. }
  242. static inline void mat4x4_invert(mat4x4 T, mat4x4 M)
  243. {
  244. float idet;
  245. float s[6];
  246. float c[6];
  247. s[0] = M[0][0]*M[1][1] - M[1][0]*M[0][1];
  248. s[1] = M[0][0]*M[1][2] - M[1][0]*M[0][2];
  249. s[2] = M[0][0]*M[1][3] - M[1][0]*M[0][3];
  250. s[3] = M[0][1]*M[1][2] - M[1][1]*M[0][2];
  251. s[4] = M[0][1]*M[1][3] - M[1][1]*M[0][3];
  252. s[5] = M[0][2]*M[1][3] - M[1][2]*M[0][3];
  253. c[0] = M[2][0]*M[3][1] - M[3][0]*M[2][1];
  254. c[1] = M[2][0]*M[3][2] - M[3][0]*M[2][2];
  255. c[2] = M[2][0]*M[3][3] - M[3][0]*M[2][3];
  256. c[3] = M[2][1]*M[3][2] - M[3][1]*M[2][2];
  257. c[4] = M[2][1]*M[3][3] - M[3][1]*M[2][3];
  258. c[5] = M[2][2]*M[3][3] - M[3][2]*M[2][3];
  259. /* Assumes it is invertible */
  260. idet = 1.0f/( s[0]*c[5]-s[1]*c[4]+s[2]*c[3]+s[3]*c[2]-s[4]*c[1]+s[5]*c[0] );
  261. T[0][0] = ( M[1][1] * c[5] - M[1][2] * c[4] + M[1][3] * c[3]) * idet;
  262. T[0][1] = (-M[0][1] * c[5] + M[0][2] * c[4] - M[0][3] * c[3]) * idet;
  263. T[0][2] = ( M[3][1] * s[5] - M[3][2] * s[4] + M[3][3] * s[3]) * idet;
  264. T[0][3] = (-M[2][1] * s[5] + M[2][2] * s[4] - M[2][3] * s[3]) * idet;
  265. T[1][0] = (-M[1][0] * c[5] + M[1][2] * c[2] - M[1][3] * c[1]) * idet;
  266. T[1][1] = ( M[0][0] * c[5] - M[0][2] * c[2] + M[0][3] * c[1]) * idet;
  267. T[1][2] = (-M[3][0] * s[5] + M[3][2] * s[2] - M[3][3] * s[1]) * idet;
  268. T[1][3] = ( M[2][0] * s[5] - M[2][2] * s[2] + M[2][3] * s[1]) * idet;
  269. T[2][0] = ( M[1][0] * c[4] - M[1][1] * c[2] + M[1][3] * c[0]) * idet;
  270. T[2][1] = (-M[0][0] * c[4] + M[0][1] * c[2] - M[0][3] * c[0]) * idet;
  271. T[2][2] = ( M[3][0] * s[4] - M[3][1] * s[2] + M[3][3] * s[0]) * idet;
  272. T[2][3] = (-M[2][0] * s[4] + M[2][1] * s[2] - M[2][3] * s[0]) * idet;
  273. T[3][0] = (-M[1][0] * c[3] + M[1][1] * c[1] - M[1][2] * c[0]) * idet;
  274. T[3][1] = ( M[0][0] * c[3] - M[0][1] * c[1] + M[0][2] * c[0]) * idet;
  275. T[3][2] = (-M[3][0] * s[3] + M[3][1] * s[1] - M[3][2] * s[0]) * idet;
  276. T[3][3] = ( M[2][0] * s[3] - M[2][1] * s[1] + M[2][2] * s[0]) * idet;
  277. }
  278. static inline void mat4x4_orthonormalize(mat4x4 R, mat4x4 M)
  279. {
  280. float s = 1.;
  281. vec3 h;
  282. mat4x4_dup(R, M);
  283. vec3_norm(R[2], R[2]);
  284. s = vec3_mul_inner(R[1], R[2]);
  285. vec3_scale(h, R[2], s);
  286. vec3_sub(R[1], R[1], h);
  287. vec3_norm(R[2], R[2]);
  288. s = vec3_mul_inner(R[1], R[2]);
  289. vec3_scale(h, R[2], s);
  290. vec3_sub(R[1], R[1], h);
  291. vec3_norm(R[1], R[1]);
  292. s = vec3_mul_inner(R[0], R[1]);
  293. vec3_scale(h, R[1], s);
  294. vec3_sub(R[0], R[0], h);
  295. vec3_norm(R[0], R[0]);
  296. }
  297. static inline void mat4x4_frustum(mat4x4 M, float l, float r, float b, float t, float n, float f)
  298. {
  299. M[0][0] = 2.f*n/(r-l);
  300. M[0][1] = M[0][2] = M[0][3] = 0.f;
  301. M[1][1] = 2.f*n/(t-b);
  302. M[1][0] = M[1][2] = M[1][3] = 0.f;
  303. M[2][0] = (r+l)/(r-l);
  304. M[2][1] = (t+b)/(t-b);
  305. M[2][2] = -(f+n)/(f-n);
  306. M[2][3] = -1.f;
  307. M[3][2] = -2.f*(f*n)/(f-n);
  308. M[3][0] = M[3][1] = M[3][3] = 0.f;
  309. }
  310. static inline void mat4x4_ortho(mat4x4 M, float l, float r, float b, float t, float n, float f)
  311. {
  312. M[0][0] = 2.f/(r-l);
  313. M[0][1] = M[0][2] = M[0][3] = 0.f;
  314. M[1][1] = 2.f/(t-b);
  315. M[1][0] = M[1][2] = M[1][3] = 0.f;
  316. M[2][2] = -2.f/(f-n);
  317. M[2][0] = M[2][1] = M[2][3] = 0.f;
  318. M[3][0] = -(r+l)/(r-l);
  319. M[3][1] = -(t+b)/(t-b);
  320. M[3][2] = -(f+n)/(f-n);
  321. M[3][3] = 1.f;
  322. }
  323. static inline void mat4x4_perspective(mat4x4 m, float y_fov, float aspect, float n, float f)
  324. {
  325. /* NOTE: Degrees are an unhandy unit to work with.
  326. * linmath.h uses radians for everything! */
  327. float const a = 1.f / (float) tan(y_fov / 2.f);
  328. m[0][0] = a / aspect;
  329. m[0][1] = 0.f;
  330. m[0][2] = 0.f;
  331. m[0][3] = 0.f;
  332. m[1][0] = 0.f;
  333. m[1][1] = a;
  334. m[1][2] = 0.f;
  335. m[1][3] = 0.f;
  336. m[2][0] = 0.f;
  337. m[2][1] = 0.f;
  338. m[2][2] = -((f + n) / (f - n));
  339. m[2][3] = -1.f;
  340. m[3][0] = 0.f;
  341. m[3][1] = 0.f;
  342. m[3][2] = -((2.f * f * n) / (f - n));
  343. m[3][3] = 0.f;
  344. }
  345. static inline void mat4x4_look_at(mat4x4 m, vec3 eye, vec3 center, vec3 up)
  346. {
  347. /* Adapted from Android's OpenGL Matrix.java. */
  348. /* See the OpenGL GLUT documentation for gluLookAt for a description */
  349. /* of the algorithm. We implement it in a straightforward way: */
  350. /* TODO: The negation of of can be spared by swapping the order of
  351. * operands in the following cross products in the right way. */
  352. vec3 f;
  353. vec3 s;
  354. vec3 t;
  355. vec3_sub(f, center, eye);
  356. vec3_norm(f, f);
  357. vec3_mul_cross(s, f, up);
  358. vec3_norm(s, s);
  359. vec3_mul_cross(t, s, f);
  360. m[0][0] = s[0];
  361. m[0][1] = t[0];
  362. m[0][2] = -f[0];
  363. m[0][3] = 0.f;
  364. m[1][0] = s[1];
  365. m[1][1] = t[1];
  366. m[1][2] = -f[1];
  367. m[1][3] = 0.f;
  368. m[2][0] = s[2];
  369. m[2][1] = t[2];
  370. m[2][2] = -f[2];
  371. m[2][3] = 0.f;
  372. m[3][0] = 0.f;
  373. m[3][1] = 0.f;
  374. m[3][2] = 0.f;
  375. m[3][3] = 1.f;
  376. mat4x4_translate_in_place(m, -eye[0], -eye[1], -eye[2]);
  377. }
  378. typedef float quat[4];
  379. static inline void quat_identity(quat q)
  380. {
  381. q[0] = q[1] = q[2] = 0.f;
  382. q[3] = 1.f;
  383. }
  384. static inline void quat_add(quat r, quat a, quat b)
  385. {
  386. int i;
  387. for(i=0; i<4; ++i)
  388. r[i] = a[i] + b[i];
  389. }
  390. static inline void quat_sub(quat r, quat a, quat b)
  391. {
  392. int i;
  393. for(i=0; i<4; ++i)
  394. r[i] = a[i] - b[i];
  395. }
  396. static inline void quat_mul(quat r, quat p, quat q)
  397. {
  398. vec3 w;
  399. vec3_mul_cross(r, p, q);
  400. vec3_scale(w, p, q[3]);
  401. vec3_add(r, r, w);
  402. vec3_scale(w, q, p[3]);
  403. vec3_add(r, r, w);
  404. r[3] = p[3]*q[3] - vec3_mul_inner(p, q);
  405. }
  406. static inline void quat_scale(quat r, quat v, float s)
  407. {
  408. int i;
  409. for(i=0; i<4; ++i)
  410. r[i] = v[i] * s;
  411. }
  412. static inline float quat_inner_product(quat a, quat b)
  413. {
  414. float p = 0.f;
  415. int i;
  416. for(i=0; i<4; ++i)
  417. p += b[i]*a[i];
  418. return p;
  419. }
  420. static inline void quat_conj(quat r, quat q)
  421. {
  422. int i;
  423. for(i=0; i<3; ++i)
  424. r[i] = -q[i];
  425. r[3] = q[3];
  426. }
  427. static inline void quat_rotate(quat r, float angle, vec3 axis) {
  428. int i;
  429. vec3 v;
  430. vec3_scale(v, axis, sinf(angle / 2));
  431. for(i=0; i<3; ++i)
  432. r[i] = v[i];
  433. r[3] = cosf(angle / 2);
  434. }
  435. #define quat_norm vec4_norm
  436. static inline void quat_mul_vec3(vec3 r, quat q, vec3 v)
  437. {
  438. /*
  439. * Method by Fabian 'ryg' Giessen (of Farbrausch)
  440. t = 2 * cross(q.xyz, v)
  441. v' = v + q.w * t + cross(q.xyz, t)
  442. */
  443. vec3 t = {q[0], q[1], q[2]};
  444. vec3 u = {q[0], q[1], q[2]};
  445. vec3_mul_cross(t, t, v);
  446. vec3_scale(t, t, 2);
  447. vec3_mul_cross(u, u, t);
  448. vec3_scale(t, t, q[3]);
  449. vec3_add(r, v, t);
  450. vec3_add(r, r, u);
  451. }
  452. static inline void mat4x4_from_quat(mat4x4 M, quat q)
  453. {
  454. float a = q[3];
  455. float b = q[0];
  456. float c = q[1];
  457. float d = q[2];
  458. float a2 = a*a;
  459. float b2 = b*b;
  460. float c2 = c*c;
  461. float d2 = d*d;
  462. M[0][0] = a2 + b2 - c2 - d2;
  463. M[0][1] = 2.f*(b*c + a*d);
  464. M[0][2] = 2.f*(b*d - a*c);
  465. M[0][3] = 0.f;
  466. M[1][0] = 2*(b*c - a*d);
  467. M[1][1] = a2 - b2 + c2 - d2;
  468. M[1][2] = 2.f*(c*d + a*b);
  469. M[1][3] = 0.f;
  470. M[2][0] = 2.f*(b*d + a*c);
  471. M[2][1] = 2.f*(c*d - a*b);
  472. M[2][2] = a2 - b2 - c2 + d2;
  473. M[2][3] = 0.f;
  474. M[3][0] = M[3][1] = M[3][2] = 0.f;
  475. M[3][3] = 1.f;
  476. }
  477. static inline void mat4x4o_mul_quat(mat4x4 R, mat4x4 M, quat q)
  478. {
  479. /* XXX: The way this is written only works for othogonal matrices. */
  480. /* TODO: Take care of non-orthogonal case. */
  481. quat_mul_vec3(R[0], q, M[0]);
  482. quat_mul_vec3(R[1], q, M[1]);
  483. quat_mul_vec3(R[2], q, M[2]);
  484. R[3][0] = R[3][1] = R[3][2] = 0.f;
  485. R[3][3] = 1.f;
  486. }
  487. static inline void quat_from_mat4x4(quat q, mat4x4 M)
  488. {
  489. float r=0.f;
  490. int i;
  491. int perm[] = { 0, 1, 2, 0, 1 };
  492. int *p = perm;
  493. for(i = 0; i<3; i++) {
  494. float m = M[i][i];
  495. if( m < r )
  496. continue;
  497. m = r;
  498. p = &perm[i];
  499. }
  500. r = (float) sqrt(1.f + M[p[0]][p[0]] - M[p[1]][p[1]] - M[p[2]][p[2]] );
  501. if(r < 1e-6) {
  502. q[0] = 1.f;
  503. q[1] = q[2] = q[3] = 0.f;
  504. return;
  505. }
  506. q[0] = r/2.f;
  507. q[1] = (M[p[0]][p[1]] - M[p[1]][p[0]])/(2.f*r);
  508. q[2] = (M[p[2]][p[0]] - M[p[0]][p[2]])/(2.f*r);
  509. q[3] = (M[p[2]][p[1]] - M[p[1]][p[2]])/(2.f*r);
  510. }
  511. #endif