linmath.h - lightweight vector and matrix math


SUBMITTED BY: Laka

DATE: May 19, 2016, 7:13 a.m.

FORMAT: C

SIZE: 13.5 kB

HITS: 1104

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

comments powered by Disqus