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