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