Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
mat.h
Go to the documentation of this file.
1
45#ifndef BJ_MAT_H
46#define BJ_MAT_H
47
48#include <banjo/api.h>
49#include <banjo/math.h>
50#include <banjo/vec.h>
51
52
59struct bj_mat3x3 { bj_real m[9]; };
61#define BJ_M3(c,r) ((c)*3 + (r))
62
70struct bj_mat3x2 { bj_real m[6]; };
72#define BJ_M32(c,r) ((c)*2 + (r))
73
79struct bj_mat4x4 { bj_real m[16]; };
81#define BJ_M4(c,r) ((c)*4 + (r))
82
89struct bj_mat4x3 { bj_real m[12]; };
91#define BJ_M43(c,r) ((c)*3 + (r))
92
98 bj_real* m = M->m;
99 m[BJ_M3(0, 0)] = BJ_F(1.0); m[BJ_M3(0, 1)] = BJ_FZERO; m[BJ_M3(0, 2)] = BJ_FZERO;
100 m[BJ_M3(1, 0)] = BJ_FZERO; m[BJ_M3(1, 1)] = BJ_F(1.0); m[BJ_M3(1, 2)] = BJ_FZERO;
101 m[BJ_M3(2, 0)] = BJ_FZERO; m[BJ_M3(2, 1)] = BJ_FZERO; m[BJ_M3(2, 2)] = BJ_F(1.0);
102}
103
110 struct bj_mat3x3* BJ_RESTRICT dst,
111 const struct bj_mat3x3* BJ_RESTRICT src
112) {
113 for (int i = 0 ; i < 9 ; ++i) {
114 dst->m[i] = src->m[i];
115 }
116}
117
124static BJ_INLINE struct bj_vec3 bj_mat3_row(const struct bj_mat3x3* BJ_RESTRICT M, int r) {
125 const bj_real* m = M->m;
126 return (struct bj_vec3) {
127 m[BJ_M3(0, r)], m[BJ_M3(1, r)], m[BJ_M3(2, r)]
128 };
129}
130
137static BJ_INLINE struct bj_vec3 bj_mat3_col(const struct bj_mat3x3* BJ_RESTRICT M, int c) {
138 const bj_real* m = M->m;
139 return (struct bj_vec3) { m[BJ_M3(c, 0)], m[BJ_M3(c, 1)], m[BJ_M3(c, 2)] };
140}
141
147static BJ_INLINE void bj_mat3_transpose(struct bj_mat3x3* BJ_RESTRICT out, const struct bj_mat3x3* BJ_RESTRICT A) {
148 const bj_real* a = A->m;
149 bj_real r[9];
150 r[BJ_M3(0,0)] = a[BJ_M3(0,0)];
151 r[BJ_M3(0,1)] = a[BJ_M3(1,0)];
152 r[BJ_M3(0,2)] = a[BJ_M3(2,0)];
153 r[BJ_M3(1,0)] = a[BJ_M3(0,1)];
154 r[BJ_M3(1,1)] = a[BJ_M3(1,1)];
155 r[BJ_M3(1,2)] = a[BJ_M3(2,1)];
156 r[BJ_M3(2,0)] = a[BJ_M3(0,2)];
157 r[BJ_M3(2,1)] = a[BJ_M3(1,2)];
158 r[BJ_M3(2,2)] = a[BJ_M3(2,2)];
159 for (int i = 0 ; i<9 ; ++i) {
160 out->m[i] = r[i];
161 }
162}
163
171 struct bj_mat3x3* BJ_RESTRICT out,
172 const struct bj_mat3x3* BJ_RESTRICT A,
173 const struct bj_mat3x3* BJ_RESTRICT B
174) {
175 for (int i = 0 ; i < 9 ; ++i) {
176 out->m[i] = A->m[i] + B->m[i];
177 }
178}
179
187 struct bj_mat3x3* BJ_RESTRICT out,
188 const struct bj_mat3x3* BJ_RESTRICT A,
189 const struct bj_mat3x3* BJ_RESTRICT B
190) {
191 for (int i = 0 ; i < 9 ; ++i) {
192 out->m[i] = A->m[i] - B->m[i];
193 }
194}
195
203 struct bj_mat3x3* BJ_RESTRICT out,
204 const struct bj_mat3x3* BJ_RESTRICT A,
205 bj_real k
206) {
207 for (int i = 0 ; i < 9 ; ++i) {
208 out->m[i] = A->m[i] * k;
209 }
210}
211
220 struct bj_mat3x3* BJ_RESTRICT out,
221 const struct bj_mat3x3* BJ_RESTRICT A,
222 const struct bj_mat3x3* BJ_RESTRICT B
223) {
224 const bj_real* a = A->m;
225 const bj_real* b = B->m;
226 bj_real* o = out->m;
227
228 for (int c = 0; c < 3; ++c) {
229 const bj_real b0 = b[BJ_M3(c, 0)];
230 const bj_real b1 = b[BJ_M3(c, 1)];
231 const bj_real b2 = b[BJ_M3(c, 2)];
232 o[BJ_M3(c,0)] = a[BJ_M3(0,0)] * b0 + a[BJ_M3(1,0)] * b1 + a[BJ_M3(2,0)] * b2;
233 o[BJ_M3(c,1)] = a[BJ_M3(0,1)] * b0 + a[BJ_M3(1,1)] * b1 + a[BJ_M3(2,1)] * b2;
234 o[BJ_M3(c,2)] = a[BJ_M3(0,2)] * b0 + a[BJ_M3(1,2)] * b1 + a[BJ_M3(2,2)] * b2;
235 }
236}
237
246 const struct bj_mat3x3* BJ_RESTRICT M,
247 struct bj_vec3 v
248) {
249 const bj_real* m = M->m;
250 return (struct bj_vec3) {
251 m[BJ_M3(0,0)] * v.x + m[BJ_M3(1,0)] * v.y + m[BJ_M3(2,0)] * v.z,
252 m[BJ_M3(0,1)] * v.x + m[BJ_M3(1,1)] * v.y + m[BJ_M3(2,1)] * v.z,
253 m[BJ_M3(0,2)] * v.x + m[BJ_M3(1,2)] * v.y + m[BJ_M3(2,2)] * v.z
254 };
255}
256
265 const struct bj_mat3x3* BJ_RESTRICT M,
266 struct bj_vec2 p
267) {
268 const bj_real* m = M->m;
269 const bj_real x = p.x;
270 const bj_real y = p.y;
271 const bj_real w = m[BJ_M3(0,2)] * x + m[BJ_M3(1,2)] * y + m[BJ_M3(2,2)];
272 const bj_real rx = m[BJ_M3(0,0)] * x + m[BJ_M3(1,0)] * y + m[BJ_M3(2,0)];
273 const bj_real ry = m[BJ_M3(0,1)] * x + m[BJ_M3(1,1)] * y + m[BJ_M3(2,1)];
274 if (w != BJ_FZERO) {
275 return (struct bj_vec2) { rx / w, ry / w };
276 }
277 return (struct bj_vec2) { rx, ry };
278}
279
287 struct bj_mat3x3* BJ_RESTRICT M,
288 bj_real tx,
289 bj_real ty
290) {
292 M->m[BJ_M3(2,0)] = tx;
293 M->m[BJ_M3(2,1)] = ty;
294}
295
304 struct bj_mat3x3* BJ_RESTRICT M,
305 bj_real tx,
306 bj_real ty
307) {
308 const struct bj_vec3 t = { tx, ty, BJ_FZERO };
309 for (int r = 0 ; r<3 ; ++r) {
310 const struct bj_vec3 row = bj_mat3_row(M, r);
311 M->m[BJ_M3(2,r)] += row.x * t.x + row.y * t.y + row.z * t.z;
312 }
313}
314
327 struct bj_mat3x3* BJ_RESTRICT M,
328 bj_real sx,
329 bj_real sy
330) {
332 M->m[BJ_M3(0,0)] = sx;
333 M->m[BJ_M3(1,1)] = sy;
334}
335
346 struct bj_mat3x3* BJ_RESTRICT M,
347 bj_real shx,
348 bj_real shy
349) {
351 M->m[BJ_M3(1,0)] = shy;
352 M->m[BJ_M3(0,1)] = shx;
353}
354
361 struct bj_mat3x3* BJ_RESTRICT M,
362 bj_real angle
363) {
364 const bj_real s = bj_sin(angle);
365 const bj_real c = bj_cos(angle);
366 bj_real* m = M->m;
367 m[BJ_M3(0,0)] = c;
368 m[BJ_M3(0,1)] = s;
369 m[BJ_M3(0,2)] = BJ_FZERO;
370 m[BJ_M3(1,0)] = -s;
371 m[BJ_M3(1,1)] = c;
372 m[BJ_M3(1,2)] = BJ_FZERO;
373 m[BJ_M3(2,0)] = BJ_FZERO;
374 m[BJ_M3(2,1)] = BJ_FZERO;
375 m[BJ_M3(2,2)] = BJ_F(1.0);
376}
377
384 const struct bj_mat3x3* BJ_RESTRICT A
385) {
386 const bj_real* m = A->m;
387 return
388 m[BJ_M3(0,0)] * (m[BJ_M3(1,1)] * m[BJ_M3(2,2)]-m[BJ_M3(2,1)] * m[BJ_M3(1,2)])
389 - m[BJ_M3(1,0)] * (m[BJ_M3(0,1)] * m[BJ_M3(2,2)]-m[BJ_M3(2,1)] * m[BJ_M3(0,2)])
390 + m[BJ_M3(2,0)] * (m[BJ_M3(0,1)] * m[BJ_M3(1,2)]-m[BJ_M3(1,1)] * m[BJ_M3(0,2)]);
391}
392
408 struct bj_mat3x3* BJ_RESTRICT out,
409 const struct bj_mat3x3* BJ_RESTRICT A
410) {
411 const bj_real* m = A->m;
412 const bj_real c00 = (m[BJ_M3(1,1)] * m[BJ_M3(2,2)] - m[BJ_M3(2,1)] * m[BJ_M3(1,2)]);
413 const bj_real c01 = -(m[BJ_M3(0,1)] * m[BJ_M3(2,2)] - m[BJ_M3(2,1)] * m[BJ_M3(0,2)]);
414 const bj_real c02 = (m[BJ_M3(0,1)] * m[BJ_M3(1,2)] - m[BJ_M3(1,1)] * m[BJ_M3(0,2)]);
415 const bj_real c10 = -(m[BJ_M3(1,0)] * m[BJ_M3(2,2)] - m[BJ_M3(2,0)] * m[BJ_M3(1,2)]);
416 const bj_real c11 = (m[BJ_M3(0,0)] * m[BJ_M3(2,2)] - m[BJ_M3(2,0)] * m[BJ_M3(0,2)]);
417 const bj_real c12 = -(m[BJ_M3(0,0)] * m[BJ_M3(1,2)] - m[BJ_M3(1,0)] * m[BJ_M3(0,2)]);
418 const bj_real c20 = (m[BJ_M3(1,0)] * m[BJ_M3(2,1)] - m[BJ_M3(2,0)] * m[BJ_M3(1,1)]);
419 const bj_real c21 = -(m[BJ_M3(0,0)] * m[BJ_M3(2,1)] - m[BJ_M3(2,0)] * m[BJ_M3(0,1)]);
420 const bj_real c22 = (m[BJ_M3(0,0)] * m[BJ_M3(1,1)] - m[BJ_M3(1,0)] * m[BJ_M3(0,1)]);
421 const bj_real det = m[BJ_M3(0,0)] * c00 + m[BJ_M3(1,0)] * c01 + m[BJ_M3(2,0)] * c02;
422
423 if (bj_real_is_zero(det)) {
424 return BJ_FALSE;
425 }
426 const bj_real id = BJ_F(1.0) / det;
427 out->m[BJ_M3(0,0)] = c00 * id; out->m[BJ_M3(1,0)] = c01 * id; out->m[BJ_M3(2,0)] = c02 * id;
428 out->m[BJ_M3(0,1)] = c10 * id; out->m[BJ_M3(1,1)] = c11 * id; out->m[BJ_M3(2,1)] = c12 * id;
429 out->m[BJ_M3(0,2)] = c20 * id; out->m[BJ_M3(1,2)] = c21 * id; out->m[BJ_M3(2,2)] = c22 * id;
430 return BJ_TRUE;
431}
432
445 struct bj_mat3x3* BJ_RESTRICT out,
446 const struct bj_mat3x3* BJ_RESTRICT A
447) {
448 const bj_real* m = A->m;
449 const bj_real c00 = (m[BJ_M3(1,1)] * m[BJ_M3(2,2)] - m[BJ_M3(2,1)] * m[BJ_M3(1,2)]);
450 const bj_real c01 = -(m[BJ_M3(0,1)] * m[BJ_M3(2,2)] - m[BJ_M3(2,1)] * m[BJ_M3(0,2)]);
451 const bj_real c02 = (m[BJ_M3(0,1)] * m[BJ_M3(1,2)] - m[BJ_M3(1,1)] * m[BJ_M3(0,2)]);
452 const bj_real c10 = -(m[BJ_M3(1,0)] * m[BJ_M3(2,2)] - m[BJ_M3(2,0)] * m[BJ_M3(1,2)]);
453 const bj_real c11 = (m[BJ_M3(0,0)] * m[BJ_M3(2,2)] - m[BJ_M3(2,0)] * m[BJ_M3(0,2)]);
454 const bj_real c12 = -(m[BJ_M3(0,0)] * m[BJ_M3(1,2)] - m[BJ_M3(1,0)] * m[BJ_M3(0,2)]);
455 const bj_real c20 = (m[BJ_M3(1,0)] * m[BJ_M3(2,1)] - m[BJ_M3(2,0)] * m[BJ_M3(1,1)]);
456 const bj_real c21 = -(m[BJ_M3(0,0)] * m[BJ_M3(2,1)] - m[BJ_M3(2,0)] * m[BJ_M3(0,1)]);
457 const bj_real c22 = (m[BJ_M3(0,0)] * m[BJ_M3(1,1)] - m[BJ_M3(1,0)] * m[BJ_M3(0,1)]);
458 const bj_real id = BJ_F(1.0) / (m[BJ_M3(0,0)] * c00 + m[BJ_M3(1,0)] * c01 + m[BJ_M3(2,0)] * c02);
459
460 out->m[BJ_M3(0,0)] = c00 * id; out->m[BJ_M3(1,0)] = c01 * id; out->m[BJ_M3(2,0)] = c02 * id;
461 out->m[BJ_M3(0,1)] = c10 * id; out->m[BJ_M3(1,1)] = c11 * id; out->m[BJ_M3(2,1)] = c12 * id;
462 out->m[BJ_M3(0,2)] = c20 * id; out->m[BJ_M3(1,2)] = c21 * id; out->m[BJ_M3(2,2)] = c22 * id;
463}
464
465
480 struct bj_mat3x3* BJ_RESTRICT M,
481 bj_real l, bj_real r,
482 bj_real b, bj_real t
483) {
484 bj_real* m = M->m;
485 m[BJ_M3(0,0)] = BJ_F(2.0)/(r-l);
486 m[BJ_M3(0,1)] = BJ_FZERO;
487 m[BJ_M3(0,2)] = BJ_FZERO;
488
489 m[BJ_M3(1,0)] = BJ_FZERO;
490 m[BJ_M3(1,1)] = BJ_F(-2.0)/(t-b);
491 m[BJ_M3(1,2)] = BJ_FZERO;
492
493 m[BJ_M3(2,0)] = -(r+l)/(r-l);
494 m[BJ_M3(2,1)] = (t+b)/(t-b);
495 m[BJ_M3(2,2)] = BJ_F(1.0);
496}
497
507 struct bj_mat3x3* BJ_RESTRICT M,
508 bj_real x,
509 bj_real y,
510 bj_real w,
511 bj_real h
512) {
513 bj_real* m = M->m;
514 m[BJ_M3(0,0)] = BJ_F(0.5) * w;
515 m[BJ_M3(0,1)] = BJ_FZERO;
516 m[BJ_M3(0,2)] = BJ_FZERO;
517 m[BJ_M3(1,0)] = BJ_FZERO;
518 m[BJ_M3(1,1)] = BJ_F(0.5) * h;
519 m[BJ_M3(1,2)] = BJ_FZERO;
520 m[BJ_M3(2,0)] = x + BJ_F(0.5) * w;
521 m[BJ_M3(2,1)] = y + BJ_F(0.5) * h;
522 m[BJ_M3(2,2)] = BJ_F(1.0);
523}
524
530 struct bj_mat3x2* BJ_RESTRICT M
531) {
532 bj_real* m = M->m;
533 m[BJ_M32(0,0)] = BJ_F(1.0);
534 m[BJ_M32(0,1)] = BJ_FZERO;
535 m[BJ_M32(1,0)] = BJ_FZERO;
536 m[BJ_M32(1,1)] = BJ_F(1.0);
537 m[BJ_M32(2,0)] = BJ_FZERO;
538 m[BJ_M32(2,1)] = BJ_FZERO;
539}
540
551 struct bj_mat3x2* BJ_RESTRICT M,
552 bj_real tx,
553 bj_real ty
554) {
556 M->m[BJ_M32(2,0)] = tx;
557 M->m[BJ_M32(2,1)] = ty;
558}
559
571 struct bj_mat3x2* BJ_RESTRICT M,
572 bj_real sx,
573 bj_real sy
574) {
576 M->m[BJ_M32(0,0)] = sx;
577 M->m[BJ_M32(1,1)] = sy;
578}
579
586 struct bj_mat3x2* BJ_RESTRICT M,
587 bj_real angle
588) {
589 const bj_real c = bj_cos(angle);
590 const bj_real s = bj_sin(angle);
591 bj_real* m = M->m;
592 m[BJ_M32(0,0)] = c;
593 m[BJ_M32(0,1)] = s;
594 m[BJ_M32(1,0)] = -s;
595 m[BJ_M32(1,1)] = c;
596 m[BJ_M32(2,0)] = BJ_FZERO;
597 m[BJ_M32(2,1)] = BJ_FZERO;
598}
599
608 struct bj_mat3x2* BJ_RESTRICT out,
609 const struct bj_mat3x2* BJ_RESTRICT A,
610 const struct bj_mat3x2* BJ_RESTRICT B
611) {
612 const bj_real a00 = A->m[BJ_M32(0,0)];
613 const bj_real a10 = A->m[BJ_M32(0,1)];
614 const bj_real a01 = A->m[BJ_M32(1,0)];
615 const bj_real a11 = A->m[BJ_M32(1,1)];
616 const bj_real a02 = A->m[BJ_M32(2,0)];
617 const bj_real a12 = A->m[BJ_M32(2,1)];
618
619 const bj_real b00 = B->m[BJ_M32(0,0)];
620 const bj_real b10 = B->m[BJ_M32(0,1)];
621 const bj_real b01 = B->m[BJ_M32(1,0)];
622 const bj_real b11 = B->m[BJ_M32(1,1)];
623 const bj_real b02 = B->m[BJ_M32(2,0)];
624 const bj_real b12 = B->m[BJ_M32(2,1)];
625
626 out->m[BJ_M32(0,0)] = a00*b00 + a01*b10;
627 out->m[BJ_M32(0,1)] = a10*b00 + a11*b10;
628 out->m[BJ_M32(1,0)] = a00*b01 + a01*b11;
629 out->m[BJ_M32(1,1)] = a10*b01 + a11*b11;
630 out->m[BJ_M32(2,0)] = a00*b02 + a01*b12 + a02;
631 out->m[BJ_M32(2,1)] = a10*b02 + a11*b12 + a12;
632}
633
634
647 const struct bj_mat3x2* BJ_RESTRICT M,
648 struct bj_vec2 p
649) {
650 const bj_real* m = M->m;
651 const bj_real x = p.x;
652 const bj_real y = p.y;
653 return (struct bj_vec2) {
654 m[BJ_M32(0,0)] * x + m[BJ_M32(1,0)] * y + m[BJ_M32(2,0)],
655 m[BJ_M32(0,1)] * x + m[BJ_M32(1,1)] * y + m[BJ_M32(2,1)]
656 };
657}
658
672 const struct bj_mat3x2* BJ_RESTRICT M,
673 struct bj_vec2 v
674) {
675 const bj_real* m = M->m;
676 const bj_real x = v.x;
677 const bj_real y = v.y;
678 return (struct bj_vec2) {
679 m[BJ_M32(0,0)] * x + m[BJ_M32(1,0)] * y,
680 m[BJ_M32(0,1)] * x + m[BJ_M32(1,1)] * y
681 };
682}
683
690 struct bj_mat3x3* BJ_RESTRICT M,
691 const struct bj_mat3x2* BJ_RESTRICT A
692) {
693 bj_real* o = M->m;
694 const bj_real* a = A->m;
695 o[BJ_M3(0,0)] = a[BJ_M32(0,0)];
696 o[BJ_M3(0,1)] = a[BJ_M32(0,1)];
697 o[BJ_M3(0,2)] = BJ_FZERO;
698 o[BJ_M3(1,0)] = a[BJ_M32(1,0)];
699 o[BJ_M3(1,1)] = a[BJ_M32(1,1)];
700 o[BJ_M3(1,2)] = BJ_FZERO;
701 o[BJ_M3(2,0)] = a[BJ_M32(2,0)];
702 o[BJ_M3(2,1)] = a[BJ_M32(2,1)];
703 o[BJ_M3(2,2)] = BJ_F(1.0);
704}
705
712 struct bj_mat3x2* BJ_RESTRICT M,
713 const struct bj_mat3x3* BJ_RESTRICT A
714) {
715 const bj_real* a = A->m;
716 bj_real* o = M->m;
717 o[BJ_M32(0,0)] = a[BJ_M3(0,0)];
718 o[BJ_M32(0,1)] = a[BJ_M3(0,1)];
719 o[BJ_M32(1,0)] = a[BJ_M3(1,0)];
720 o[BJ_M32(1,1)] = a[BJ_M3(1,1)];
721 o[BJ_M32(2,0)] = a[BJ_M3(2,0)];
722 o[BJ_M32(2,1)] = a[BJ_M3(2,1)];
723}
724
730 struct bj_mat4x4* BJ_RESTRICT M
731) {
732 bj_real* m = M->m;
733 m[BJ_M4(0,0)] = BJ_F(1.0);
734 m[BJ_M4(0,1)] = BJ_FZERO;
735 m[BJ_M4(0,2)] = BJ_FZERO;
736 m[BJ_M4(0,3)] = BJ_FZERO;
737 m[BJ_M4(1,0)] = BJ_FZERO;
738 m[BJ_M4(1,1)] = BJ_F(1.0);
739 m[BJ_M4(1,2)] = BJ_FZERO;
740 m[BJ_M4(1,3)] = BJ_FZERO;
741 m[BJ_M4(2,0)] = BJ_FZERO;
742 m[BJ_M4(2,1)] = BJ_FZERO;
743 m[BJ_M4(2,2)] = BJ_F(1.0);
744 m[BJ_M4(2,3)] = BJ_FZERO;
745 m[BJ_M4(3,0)] = BJ_FZERO;
746 m[BJ_M4(3,1)] = BJ_FZERO;
747 m[BJ_M4(3,2)] = BJ_FZERO;
748 m[BJ_M4(3,3)] = BJ_F(1.0);
749}
750
757 struct bj_mat4x4* BJ_RESTRICT dst,
758 const struct bj_mat4x4* BJ_RESTRICT src
759) {
760 for(int i = 0 ; i < 16 ; ++i) {
761 dst->m[i] = src->m[i];
762 }
763}
764
772 const struct bj_mat4x4* BJ_RESTRICT M,
773 int r
774) {
775 const bj_real* m = M->m;
776 return (struct bj_vec4) {
777 m[BJ_M4(0,r)],
778 m[BJ_M4(1,r)],
779 m[BJ_M4(2,r)],
780 m[BJ_M4(3,r)]
781 };
782}
783
791 const struct bj_mat4x4* BJ_RESTRICT M,
792 int c
793) {
794 const bj_real* m = M->m;
795 return (struct bj_vec4) {
796 m[BJ_M4(c,0)],
797 m[BJ_M4(c,1)],
798 m[BJ_M4(c,2)],
799 m[BJ_M4(c,3)]
800 };
801}
802
809 struct bj_mat4x4* BJ_RESTRICT out,
810 const struct bj_mat4x4* BJ_RESTRICT A
811) {
812 const bj_real* a = A->m;
813 bj_real r[16];
814 for(int c = 0 ; c < 4; ++c) {
815 for(int r0 = 0 ; r0 < 4; ++r0) {
816 r[BJ_M4(r0,c)] = a[BJ_M4(c,r0)];
817 }
818 }
819 for(int i = 0 ; i < 16 ; ++i) {
820 out->m[i] = r[i];
821 }
822}
823
831 struct bj_mat4x4* BJ_RESTRICT out,
832 const struct bj_mat4x4* BJ_RESTRICT A,
833 const struct bj_mat4x4* BJ_RESTRICT B
834) {
835
836 for(int i = 0 ; i < 16 ; ++i) {
837 out->m[i] = A->m[i] + B->m[i];
838 }
839}
840
848 struct bj_mat4x4* BJ_RESTRICT out,
849 const struct bj_mat4x4* BJ_RESTRICT A,
850 const struct bj_mat4x4* BJ_RESTRICT B
851) {
852
853 for(int i = 0 ; i < 16 ; ++i) {
854 out->m[i] = A->m[i] - B->m[i];
855 }
856}
857
865 struct bj_mat4x4* BJ_RESTRICT out,
866 const struct bj_mat4x4* BJ_RESTRICT A,
867 bj_real k
868) {
869
870 for(int i = 0;i<16;++i) {
871 out->m[i] = A->m[i] * k;
872 }
873}
874
884 struct bj_mat4x4* BJ_RESTRICT out,
885 const struct bj_mat4x4* BJ_RESTRICT A,
886 bj_real sx,
887 bj_real sy,
888 bj_real sz
889) {
890
891 const bj_real* a = A->m;
892 bj_real* m = out->m;
893 m[BJ_M4(0,0)] = a[BJ_M4(0,0)] * sx;
894 m[BJ_M4(0,1)] = a[BJ_M4(0,1)] * sx;
895 m[BJ_M4(0,2)] = a[BJ_M4(0,2)] * sx;
896 m[BJ_M4(0,3)] = a[BJ_M4(0,3)] * sx;
897 m[BJ_M4(1,0)] = a[BJ_M4(1,0)] * sy;
898 m[BJ_M4(1,1)] = a[BJ_M4(1,1)] * sy;
899 m[BJ_M4(1,2)] = a[BJ_M4(1,2)] * sy;
900 m[BJ_M4(1,3)] = a[BJ_M4(1,3)] * sy;
901 m[BJ_M4(2,0)] = a[BJ_M4(2,0)] * sz;
902 m[BJ_M4(2,1)] = a[BJ_M4(2,1)] * sz;
903 m[BJ_M4(2,2)] = a[BJ_M4(2,2)] * sz;
904 m[BJ_M4(2,3)] = a[BJ_M4(2,3)] * sz;
905 m[BJ_M4(3,0)] = a[BJ_M4(3,0)];
906 m[BJ_M4(3,1)] = a[BJ_M4(3,1)];
907 m[BJ_M4(3,2)] = a[BJ_M4(3,2)];
908 m[BJ_M4(3,3)] = a[BJ_M4(3,3)];
909}
910
919 struct bj_mat4x4* BJ_RESTRICT out,
920 const struct bj_mat4x4* BJ_RESTRICT A,
921 const struct bj_mat4x4* BJ_RESTRICT B
922)
923{
924 const bj_real* a = A->m;
925 const bj_real* b = B->m;
926 bj_real* o = out->m;
927
928 for (int c = 0; c < 4; ++c) {
929 const bj_real b0 = b[BJ_M4(c,0)];
930 const bj_real b1 = b[BJ_M4(c,1)];
931 const bj_real b2 = b[BJ_M4(c,2)];
932 const bj_real b3 = b[BJ_M4(c,3)];
933
934 o[BJ_M4(c,0)] = a[BJ_M4(0,0)] * b0
935 + a[BJ_M4(1,0)] * b1
936 + a[BJ_M4(2,0)] * b2
937 + a[BJ_M4(3,0)] * b3;
938 o[BJ_M4(c,1)] = a[BJ_M4(0,1)] * b0
939 + a[BJ_M4(1,1)] * b1
940 + a[BJ_M4(2,1)] * b2
941 + a[BJ_M4(3,1)] * b3;
942 o[BJ_M4(c,2)] = a[BJ_M4(0,2)] * b0
943 + a[BJ_M4(1,2)] * b1
944 + a[BJ_M4(2,2)] * b2
945 + a[BJ_M4(3,2)] * b3;
946 o[BJ_M4(c,3)] = a[BJ_M4(0,3)] * b0
947 + a[BJ_M4(1,3)] * b1
948 + a[BJ_M4(2,3)] * b2
949 + a[BJ_M4(3,3)] * b3;
950 }
951}
952
961 const struct bj_mat4x4* BJ_RESTRICT M,
962 struct bj_vec4 v
963) {
964 const bj_real* m = M->m;
965 return (struct bj_vec4) {
966 m[BJ_M4(0,0)] * v.x + m[BJ_M4(1,0)] * v.y +
967 m[BJ_M4(2,0)] * v.z + m[BJ_M4(3,0)] * v.w,
968 m[BJ_M4(0,1)] * v.x + m[BJ_M4(1,1)] * v.y +
969 m[BJ_M4(2,1)] * v.z + m[BJ_M4(3,1)] * v.w,
970 m[BJ_M4(0,2)] * v.x + m[BJ_M4(1,2)] * v.y +
971 m[BJ_M4(2,2)] * v.z + m[BJ_M4(3,2)] * v.w,
972 m[BJ_M4(0,3)] * v.x + m[BJ_M4(1,3)] * v.y +
973 m[BJ_M4(2,3)] * v.z + m[BJ_M4(3,3)] * v.w
974 };
975}
976
988 struct bj_mat4x4* BJ_RESTRICT M,
989 bj_real x,
990 bj_real y,
991 bj_real z
992) {
994 M->m[BJ_M4(3,0)] = x;
995 M->m[BJ_M4(3,1)] = y;
996 M->m[BJ_M4(3,2)] = z;
997}
998
1010 struct bj_mat4x4* BJ_RESTRICT M,
1011 bj_real x,
1012 bj_real y,
1013 bj_real z
1014) {
1015 const struct bj_vec4 t = { x, y, z, BJ_FZERO };
1016 for (int r = 0 ; r<4 ; ++r) {
1017 const struct bj_vec4 row = bj_mat4_row(M, r);
1018 M->m[BJ_M4(3,r)] += bj_vec4_dot(row, t);
1019 }
1020}
1021
1034 struct bj_mat4x4* BJ_RESTRICT out,
1035 struct bj_vec3 a,
1036 struct bj_vec3 b
1037) {
1038 bj_real* m = out->m;
1039 m[BJ_M4(0,0)] = a.x*b.x;
1040 m[BJ_M4(1,0)] = a.y*b.x;
1041 m[BJ_M4(2,0)] = a.z*b.x;
1042 m[BJ_M4(3,0)] = BJ_FZERO;
1043 m[BJ_M4(0,1)] = a.x*b.y;
1044 m[BJ_M4(1,1)] = a.y*b.y;
1045 m[BJ_M4(2,1)] = a.z*b.y;
1046 m[BJ_M4(3,1)] = BJ_FZERO;
1047 m[BJ_M4(0,2)] = a.x*b.z;
1048 m[BJ_M4(1,2)] = a.y*b.z;
1049 m[BJ_M4(2,2)] = a.z*b.z;
1050 m[BJ_M4(3,2)] = BJ_FZERO;
1051 m[BJ_M4(0,3)] = BJ_FZERO;
1052 m[BJ_M4(1,3)] = BJ_FZERO;
1053 m[BJ_M4(2,3)] = BJ_FZERO;
1054 m[BJ_M4(3,3)] = BJ_FZERO;
1055}
1056
1071 struct bj_mat4x4* restrict out,
1072 const struct bj_mat4x4* restrict M,
1073 struct bj_vec3 axis,
1074 bj_real angle
1075) {
1076 const bj_real s = bj_sin(angle);
1077 const bj_real c = bj_cos(angle);
1078 const bj_real len = bj_vec3_len(axis);
1079 if (len <= BJ_F(1e-4)) {
1080 bj_mat4_copy(out, M);
1081 return;
1082 }
1083
1084 const bj_real inv = BJ_F(1.0) / len;
1085 const bj_real ux = axis.x * inv, uy = axis.y * inv, uz = axis.z * inv;
1086 const bj_real t = BJ_F(1.0) - c;
1087
1088 /* 3x3 rotation block */
1089 const bj_real r00 = c + t*ux*ux;
1090 const bj_real r01 = t*ux*uy + s*uz;
1091 const bj_real r02 = t*ux*uz - s*uy;
1092
1093 const bj_real r10 = t*uy*ux - s*uz;
1094 const bj_real r11 = c + t*uy*uy;
1095 const bj_real r12 = t*uy*uz + s*ux;
1096
1097 const bj_real r20 = t*uz*ux + s*uy;
1098 const bj_real r21 = t*uz*uy - s*ux;
1099 const bj_real r22 = c + t*uz*uz;
1100
1101 const bj_real* m = M->m;
1102 bj_real* o = out->m;
1103
1104 /* Columns of M (column-major) */
1105 const bj_real* m0 = &m[BJ_M4(0,0)];
1106 const bj_real* m1 = &m[BJ_M4(1,0)];
1107 const bj_real* m2 = &m[BJ_M4(2,0)];
1108 const bj_real* m3 = &m[BJ_M4(3,0)];
1109
1110 bj_real* o0 = &o[BJ_M4(0,0)];
1111 bj_real* o1 = &o[BJ_M4(1,0)];
1112 bj_real* o2 = &o[BJ_M4(2,0)];
1113 bj_real* o3 = &o[BJ_M4(3,0)];
1114
1115 for (int r = 0; r < 4; ++r) {
1116 const bj_real M0 = m0[r], M1 = m1[r], M2 = m2[r];
1117 o0[r] = M0*r00 + M1*r10 + M2*r20;
1118 o1[r] = M0*r01 + M1*r11 + M2*r21;
1119 o2[r] = M0*r02 + M1*r12 + M2*r22;
1120 o3[r] = m3[r];
1121 }
1122}
1123
1132 struct bj_mat4x4* restrict out,
1133 const struct bj_mat4x4* restrict M,
1134 bj_real a
1135) {
1136
1137 const bj_real s = bj_sin(a), c = bj_cos(a);
1138 const bj_real* m = M->m; bj_real* o = out->m;
1139
1140 const bj_real* m0 = &m[BJ_M4(0,0)];
1141 const bj_real* m1 = &m[BJ_M4(1,0)];
1142 const bj_real* m2 = &m[BJ_M4(2,0)];
1143 const bj_real* m3 = &m[BJ_M4(3,0)];
1144
1145 bj_real* o0 = &o[BJ_M4(0,0)];
1146 bj_real* o1 = &o[BJ_M4(1,0)];
1147 bj_real* o2 = &o[BJ_M4(2,0)];
1148 bj_real* o3 = &o[BJ_M4(3,0)];
1149
1150 for (int r = 0; r < 4; ++r) {
1151 const bj_real M1 = m1[r], M2 = m2[r];
1152 o0[r] = m0[r];
1153 o1[r] = c*M1 - s*M2;
1154 o2[r] = s*M1 + c*M2;
1155 o3[r] = m3[r];
1156 }
1157}
1158
1167 struct bj_mat4x4* restrict out,
1168 const struct bj_mat4x4* restrict M,
1169 bj_real a
1170) {
1171 const bj_real s = bj_sin(a), c = bj_cos(a);
1172 const bj_real* m = M->m; bj_real* o = out->m;
1173
1174 const bj_real* m0 = &m[BJ_M4(0,0)];
1175 const bj_real* m1 = &m[BJ_M4(1,0)];
1176 const bj_real* m2 = &m[BJ_M4(2,0)];
1177 const bj_real* m3 = &m[BJ_M4(3,0)];
1178
1179 bj_real* o0 = &o[BJ_M4(0,0)];
1180 bj_real* o1 = &o[BJ_M4(1,0)];
1181 bj_real* o2 = &o[BJ_M4(2,0)];
1182 bj_real* o3 = &o[BJ_M4(3,0)];
1183
1184 for (int r = 0; r < 4; ++r) {
1185 const bj_real M0 = m0[r], M2 = m2[r];
1186 o0[r] = c*M0 + s*M2;
1187 o1[r] = m1[r];
1188 o2[r] = -s*M0 + c*M2;
1189 o3[r] = m3[r];
1190 }
1191}
1192
1201 struct bj_mat4x4* restrict out,
1202 const struct bj_mat4x4* restrict M,
1203 bj_real a
1204) {
1205 const bj_real s = bj_sin(a), c = bj_cos(a);
1206 const bj_real* m = M->m; bj_real* o = out->m;
1207
1208 const bj_real* m0 = &m[BJ_M4(0,0)];
1209 const bj_real* m1 = &m[BJ_M4(1,0)];
1210 const bj_real* m2 = &m[BJ_M4(2,0)];
1211 const bj_real* m3 = &m[BJ_M4(3,0)];
1212
1213 bj_real* o0 = &o[BJ_M4(0,0)];
1214 bj_real* o1 = &o[BJ_M4(1,0)];
1215 bj_real* o2 = &o[BJ_M4(2,0)];
1216 bj_real* o3 = &o[BJ_M4(3,0)];
1217
1218 for (int r = 0; r < 4; ++r) {
1219 const bj_real M0 = m0[r], M1 = m1[r];
1220 o0[r] = c*M0 - s*M1;
1221 o1[r] = s*M0 + c*M1;
1222 o2[r] = m2[r];
1223 o3[r] = m3[r];
1224 }
1225}
1226
1243 struct bj_mat4x4* BJ_RESTRICT R,
1244 const struct bj_mat4x4* BJ_RESTRICT M,
1245 struct bj_vec2 a,
1246 struct bj_vec2 b,
1247 bj_real s
1248) {
1249 bj_real z_a = BJ_FZERO;
1250 bj_real z_b = BJ_FZERO;
1251 const bj_real la = bj_vec2_len(a);
1252 const bj_real lb = bj_vec2_len(b);
1253 if (la < BJ_F(1.0)) {
1254 z_a = bj_sqrt(BJ_F(1.0) - bj_vec2_dot(a,a));
1255 } else {
1256 a = bj_vec2_normalize(a);
1257 }
1258 if (lb < BJ_F(1.0)) {
1259 z_b = bj_sqrt(BJ_F(1.0) - bj_vec2_dot(b,b));
1260 } else {
1261 b = bj_vec2_normalize(b);
1262 }
1263 const struct bj_vec3 A = { a.x, a.y, z_a };
1264 const struct bj_vec3 B = { b.x, b.y, z_b };
1265 const struct bj_vec3 C = bj_vec3_cross(A,B);
1266 const bj_real ang = bj_acos(bj_vec3_dot(A,B)) * s;
1267 bj_mat4_rotate_axis_angle(R, M, C, ang);
1268}
1269
1286 struct bj_mat4x4* BJ_RESTRICT out,
1287 const struct bj_mat4x4* BJ_RESTRICT M
1288) {
1289 const bj_real* m = M->m;
1290 bj_real s[6], c[6];
1291
1292 s[0] = m[BJ_M4(0,0)] * m[BJ_M4(1,1)] - m[BJ_M4(1,0)] * m[BJ_M4(0,1)];
1293 s[1] = m[BJ_M4(0,0)] * m[BJ_M4(1,2)] - m[BJ_M4(1,0)] * m[BJ_M4(0,2)];
1294 s[2] = m[BJ_M4(0,0)] * m[BJ_M4(1,3)] - m[BJ_M4(1,0)] * m[BJ_M4(0,3)];
1295 s[3] = m[BJ_M4(0,1)] * m[BJ_M4(1,2)] - m[BJ_M4(1,1)] * m[BJ_M4(0,2)];
1296 s[4] = m[BJ_M4(0,1)] * m[BJ_M4(1,3)] - m[BJ_M4(1,1)] * m[BJ_M4(0,3)];
1297 s[5] = m[BJ_M4(0,2)] * m[BJ_M4(1,3)] - m[BJ_M4(1,2)] * m[BJ_M4(0,3)];
1298
1299 c[0] = m[BJ_M4(2,0)] * m[BJ_M4(3,1)] - m[BJ_M4(3,0)] * m[BJ_M4(2,1)];
1300 c[1] = m[BJ_M4(2,0)] * m[BJ_M4(3,2)] - m[BJ_M4(3,0)] * m[BJ_M4(2,2)];
1301 c[2] = m[BJ_M4(2,0)] * m[BJ_M4(3,3)] - m[BJ_M4(3,0)] * m[BJ_M4(2,3)];
1302 c[3] = m[BJ_M4(2,1)] * m[BJ_M4(3,2)] - m[BJ_M4(3,1)] * m[BJ_M4(2,2)];
1303 c[4] = m[BJ_M4(2,1)] * m[BJ_M4(3,3)] - m[BJ_M4(3,1)] * m[BJ_M4(2,3)];
1304 c[5] = m[BJ_M4(2,2)] * m[BJ_M4(3,3)] - m[BJ_M4(3,2)] * m[BJ_M4(2,3)];
1305
1306 const bj_real det = s[0]*c[5] - s[1]*c[4] + s[2]*c[3]
1307 + s[3]*c[2] - s[4]*c[1] + s[5]*c[0];
1308
1309 if (bj_real_is_zero(det)) {
1310 return BJ_FALSE;
1311 }
1312 const bj_real id = BJ_F(1.0) / det;
1313 bj_real* o = out->m;
1314
1315 o[BJ_M4(0,0)] = ( m[BJ_M4(1,1)] * c[5] - m[BJ_M4(1,2)] * c[4] + m[BJ_M4(1,3)] * c[3]) * id;
1316 o[BJ_M4(0,1)] = (-m[BJ_M4(0,1)] * c[5] + m[BJ_M4(0,2)] * c[4] - m[BJ_M4(0,3)] * c[3]) * id;
1317 o[BJ_M4(0,2)] = ( m[BJ_M4(3,1)] * s[5] - m[BJ_M4(3,2)] * s[4] + m[BJ_M4(3,3)] * s[3]) * id;
1318 o[BJ_M4(0,3)] = (-m[BJ_M4(2,1)] * s[5] + m[BJ_M4(2,2)] * s[4] - m[BJ_M4(2,3)] * s[3]) * id;
1319
1320 o[BJ_M4(1,0)] = (-m[BJ_M4(1,0)] * c[5] + m[BJ_M4(1,2)] * c[2] - m[BJ_M4(1,3)] * c[1]) * id;
1321 o[BJ_M4(1,1)] = ( m[BJ_M4(0,0)] * c[5] - m[BJ_M4(0,2)] * c[2] + m[BJ_M4(0,3)] * c[1]) * id;
1322 o[BJ_M4(1,2)] = (-m[BJ_M4(3,0)] * s[5] + m[BJ_M4(3,2)] * s[2] - m[BJ_M4(3,3)] * s[1]) * id;
1323 o[BJ_M4(1,3)] = ( m[BJ_M4(2,0)] * s[5] - m[BJ_M4(2,2)] * s[2] + m[BJ_M4(2,3)] * s[1]) * id;
1324
1325 o[BJ_M4(2,0)] = ( m[BJ_M4(1,0)] * c[4] - m[BJ_M4(1,1)] * c[2] + m[BJ_M4(1,3)] * c[0]) * id;
1326 o[BJ_M4(2,1)] = (-m[BJ_M4(0,0)] * c[4] + m[BJ_M4(0,1)] * c[2] - m[BJ_M4(0,3)] * c[0]) * id;
1327 o[BJ_M4(2,2)] = ( m[BJ_M4(3,0)] * s[4] - m[BJ_M4(3,1)] * s[2] + m[BJ_M4(3,3)] * s[0]) * id;
1328 o[BJ_M4(2,3)] = (-m[BJ_M4(2,0)] * s[4] + m[BJ_M4(2,1)] * s[2] - m[BJ_M4(2,3)] * s[0]) * id;
1329
1330 o[BJ_M4(3,0)] = (-m[BJ_M4(1,0)] * c[3] + m[BJ_M4(1,1)] * c[1] - m[BJ_M4(1,2)] * c[0]) * id;
1331 o[BJ_M4(3,1)] = ( m[BJ_M4(0,0)] * c[3] - m[BJ_M4(0,1)] * c[1] + m[BJ_M4(0,2)] * c[0]) * id;
1332 o[BJ_M4(3,2)] = (-m[BJ_M4(3,0)] * s[3] + m[BJ_M4(3,1)] * s[1] - m[BJ_M4(3,2)] * s[0]) * id;
1333 o[BJ_M4(3,3)] = ( m[BJ_M4(2,0)] * s[3] - m[BJ_M4(2,1)] * s[1] + m[BJ_M4(2,2)] * s[0]) * id;
1334
1335 return BJ_TRUE;
1336}
1337
1350 struct bj_mat4x4* BJ_RESTRICT out,
1351 const struct bj_mat4x4* BJ_RESTRICT M
1352) {
1353 const bj_real* m = M->m;
1354 bj_real s[6], c[6];
1355
1356 s[0] = m[BJ_M4(0,0)] * m[BJ_M4(1,1)] - m[BJ_M4(1,0)] * m[BJ_M4(0,1)];
1357 s[1] = m[BJ_M4(0,0)] * m[BJ_M4(1,2)] - m[BJ_M4(1,0)] * m[BJ_M4(0,2)];
1358 s[2] = m[BJ_M4(0,0)] * m[BJ_M4(1,3)] - m[BJ_M4(1,0)] * m[BJ_M4(0,3)];
1359 s[3] = m[BJ_M4(0,1)] * m[BJ_M4(1,2)] - m[BJ_M4(1,1)] * m[BJ_M4(0,2)];
1360 s[4] = m[BJ_M4(0,1)] * m[BJ_M4(1,3)] - m[BJ_M4(1,1)] * m[BJ_M4(0,3)];
1361 s[5] = m[BJ_M4(0,2)] * m[BJ_M4(1,3)] - m[BJ_M4(1,2)] * m[BJ_M4(0,3)];
1362
1363 c[0] = m[BJ_M4(2,0)] * m[BJ_M4(3,1)] - m[BJ_M4(3,0)] * m[BJ_M4(2,1)];
1364 c[1] = m[BJ_M4(2,0)] * m[BJ_M4(3,2)] - m[BJ_M4(3,0)] * m[BJ_M4(2,2)];
1365 c[2] = m[BJ_M4(2,0)] * m[BJ_M4(3,3)] - m[BJ_M4(3,0)] * m[BJ_M4(2,3)];
1366 c[3] = m[BJ_M4(2,1)] * m[BJ_M4(3,2)] - m[BJ_M4(3,1)] * m[BJ_M4(2,2)];
1367 c[4] = m[BJ_M4(2,1)] * m[BJ_M4(3,3)] - m[BJ_M4(3,1)] * m[BJ_M4(2,3)];
1368 c[5] = m[BJ_M4(2,2)] * m[BJ_M4(3,3)] - m[BJ_M4(3,2)] * m[BJ_M4(2,3)];
1369
1370 const bj_real id = BJ_F(1.0) /
1371 (s[0]*c[5] - s[1]*c[4] + s[2]*c[3] + s[3]*c[2] - s[4]*c[1] + s[5]*c[0]);
1372
1373 bj_real* o = out->m;
1374
1375 o[BJ_M4(0,0)] = ( m[BJ_M4(1,1)] * c[5] - m[BJ_M4(1,2)] * c[4] + m[BJ_M4(1,3)] * c[3]) * id;
1376 o[BJ_M4(0,1)] = (-m[BJ_M4(0,1)] * c[5] + m[BJ_M4(0,2)] * c[4] - m[BJ_M4(0,3)] * c[3]) * id;
1377 o[BJ_M4(0,2)] = ( m[BJ_M4(3,1)] * s[5] - m[BJ_M4(3,2)] * s[4] + m[BJ_M4(3,3)] * s[3]) * id;
1378 o[BJ_M4(0,3)] = (-m[BJ_M4(2,1)] * s[5] + m[BJ_M4(2,2)] * s[4] - m[BJ_M4(2,3)] * s[3]) * id;
1379
1380 o[BJ_M4(1,0)] = (-m[BJ_M4(1,0)] * c[5] + m[BJ_M4(1,2)] * c[2] - m[BJ_M4(1,3)] * c[1]) * id;
1381 o[BJ_M4(1,1)] = ( m[BJ_M4(0,0)] * c[5] - m[BJ_M4(0,2)] * c[2] + m[BJ_M4(0,3)] * c[1]) * id;
1382 o[BJ_M4(1,2)] = (-m[BJ_M4(3,0)] * s[5] + m[BJ_M4(3,2)] * s[2] - m[BJ_M4(3,3)] * s[1]) * id;
1383 o[BJ_M4(1,3)] = ( m[BJ_M4(2,0)] * s[5] - m[BJ_M4(2,2)] * s[2] + m[BJ_M4(2,3)] * s[1]) * id;
1384
1385 o[BJ_M4(2,0)] = ( m[BJ_M4(1,0)] * c[4] - m[BJ_M4(1,1)] * c[2] + m[BJ_M4(1,3)] * c[0]) * id;
1386 o[BJ_M4(2,1)] = (-m[BJ_M4(0,0)] * c[4] + m[BJ_M4(0,1)] * c[2] - m[BJ_M4(0,3)] * c[0]) * id;
1387 o[BJ_M4(2,2)] = ( m[BJ_M4(3,0)] * s[4] - m[BJ_M4(3,1)] * s[2] + m[BJ_M4(3,3)] * s[0]) * id;
1388 o[BJ_M4(2,3)] = (-m[BJ_M4(2,0)] * s[4] + m[BJ_M4(2,1)] * s[2] - m[BJ_M4(2,3)] * s[0]) * id;
1389
1390 o[BJ_M4(3,0)] = (-m[BJ_M4(1,0)] * c[3] + m[BJ_M4(1,1)] * c[1] - m[BJ_M4(1,2)] * c[0]) * id;
1391 o[BJ_M4(3,1)] = ( m[BJ_M4(0,0)] * c[3] - m[BJ_M4(0,1)] * c[1] + m[BJ_M4(0,2)] * c[0]) * id;
1392 o[BJ_M4(3,2)] = (-m[BJ_M4(3,0)] * s[3] + m[BJ_M4(3,1)] * s[1] - m[BJ_M4(3,2)] * s[0]) * id;
1393 o[BJ_M4(3,3)] = ( m[BJ_M4(2,0)] * s[3] - m[BJ_M4(2,1)] * s[1] + m[BJ_M4(2,2)] * s[0]) * id;
1394}
1395
1403 struct bj_mat4x4* BJ_RESTRICT out,
1404 const struct bj_mat4x4* BJ_RESTRICT A
1405) {
1406 bj_mat4_copy(out, A);
1407 struct bj_vec3 z = {
1408 out->m[BJ_M4(2,0)],
1409 out->m[BJ_M4(2,1)],
1410 out->m[BJ_M4(2,2)]
1411 };
1413
1414 struct bj_vec3 y = {
1415 out->m[BJ_M4(1,0)],
1416 out->m[BJ_M4(1,1)],
1417 out->m[BJ_M4(1,2)]
1418 };
1421
1422 struct bj_vec3 x = {
1423 out->m[BJ_M4(0,0)],
1424 out->m[BJ_M4(0,1)],
1425 out->m[BJ_M4(0,2)]
1426 };
1430
1431 out->m[BJ_M4(0,0)] = x.x;
1432 out->m[BJ_M4(0,1)] = x.y;
1433 out->m[BJ_M4(0,2)] = x.z;
1434 out->m[BJ_M4(1,0)] = y.x;
1435 out->m[BJ_M4(1,1)] = y.y;
1436 out->m[BJ_M4(1,2)] = y.z;
1437 out->m[BJ_M4(2,0)] = z.x;
1438 out->m[BJ_M4(2,1)] = z.y;
1439 out->m[BJ_M4(2,2)] = z.z;
1440}
1441
1459 struct bj_mat4x4* BJ_RESTRICT M,
1460 bj_real l,
1461 bj_real r,
1462 bj_real b,
1463 bj_real t,
1464 bj_real n,
1465 bj_real f
1466) {
1467
1468 bj_real* m = M->m;
1469 m[BJ_M4(0,0)] = BJ_F(2.0)*n/(r-l);
1470 m[BJ_M4(0,1)] = m[BJ_M4(0,2)] = m[BJ_M4(0,3)] = BJ_FZERO;
1471
1472 m[BJ_M4(1,0)] = BJ_FZERO;
1473 m[BJ_M4(1,1)] = BJ_F(-2.0)*n/(t-b);
1474 m[BJ_M4(1,2)] = m[BJ_M4(1,3)] = BJ_FZERO;
1475
1476 m[BJ_M4(2,0)] = (r+l)/(r-l);
1477 m[BJ_M4(2,1)] = (t+b)/(t-b);
1478 m[BJ_M4(2,2)] = f/(f-n);
1479 m[BJ_M4(2,3)] = BJ_F(1.0);
1480
1481 m[BJ_M4(3,0)] = BJ_FZERO; m[BJ_M4(3,1)] = BJ_FZERO;
1482 m[BJ_M4(3,2)] = -(f*n)/(f-n);
1483 m[BJ_M4(3,3)] = BJ_FZERO;
1484}
1485
1502 struct bj_mat4x4* BJ_RESTRICT M,
1503 bj_real l,
1504 bj_real r,
1505 bj_real b,
1506 bj_real t,
1507 bj_real n,
1508 bj_real f
1509) {
1510
1511 bj_real* m = M->m;
1512 m[BJ_M4(0,0)] = BJ_F(2.0)/(r-l);
1513 m[BJ_M4(0,1)] = m[BJ_M4(0,2)] = m[BJ_M4(0,3)] = BJ_FZERO;
1514
1515 m[BJ_M4(1,0)] = m[BJ_M4(1,2)] = m[BJ_M4(1,3)] = BJ_FZERO;
1516 m[BJ_M4(1,1)] = BJ_F(-2.0)/(t-b);
1517
1518 m[BJ_M4(2,0)] = m[BJ_M4(2,1)] = m[BJ_M4(2,3)] = BJ_FZERO;
1519 m[BJ_M4(2,2)] = BJ_F(1.0)/(f-n);
1520
1521 m[BJ_M4(3,0)] = -(r+l)/(r-l);
1522 m[BJ_M4(3,1)] = (t+b)/(t-b);
1523 m[BJ_M4(3,2)] = -n/(f-n);
1524 m[BJ_M4(3,3)] = BJ_F(1.0);
1525}
1526
1527
1538 struct bj_mat4x4* BJ_RESTRICT M,
1539 bj_real y_fov,
1540 bj_real aspect,
1541 bj_real n,
1542 bj_real f
1543) {
1544
1545 const bj_real a = BJ_F(1.0)/bj_tan(y_fov/BJ_F(2.0));
1546 bj_real* m = M->m;
1547 m[BJ_M4(0,0)] = a/aspect;
1548 m[BJ_M4(0,1)] = m[BJ_M4(0,2)] = m[BJ_M4(0,3)] = BJ_FZERO;
1549
1550 m[BJ_M4(1,0)] = BJ_FZERO;
1551 m[BJ_M4(1,1)] = -a;
1552 m[BJ_M4(1,2)] = m[BJ_M4(1,3)] = BJ_FZERO;
1553
1554 m[BJ_M4(2,0)] = BJ_FZERO; m[BJ_M4(2,1)] = BJ_FZERO;
1555 m[BJ_M4(2,2)] = f/(f-n);
1556 m[BJ_M4(2,3)] = BJ_F(1.0);
1557
1558 m[BJ_M4(3,0)] = BJ_FZERO; m[BJ_M4(3,1)] = BJ_FZERO;
1559 m[BJ_M4(3,2)] = -(f*n)/(f-n);
1560 m[BJ_M4(3,3)] = BJ_FZERO;
1561}
1562
1573 struct bj_mat4x4* BJ_RESTRICT M,
1574 bj_real x,
1575 bj_real y,
1576 bj_real w,
1577 bj_real h
1578) {
1579 const bj_real zmin = BJ_FZERO;
1580 const bj_real zmax = BJ_F(1.0);
1581 bj_real* m = M->m;
1582 m[BJ_M4(0,0)] = BJ_F(0.5) * w;
1583 m[BJ_M4(0,1)] = BJ_FZERO;
1584 m[BJ_M4(0,2)] = BJ_FZERO;
1585 m[BJ_M4(0,3)] = BJ_FZERO;
1586 m[BJ_M4(1,0)] = BJ_FZERO;
1587 m[BJ_M4(1,1)] = BJ_F(0.5) * h;
1588 m[BJ_M4(1,2)] = BJ_FZERO;
1589 m[BJ_M4(1,3)] = BJ_FZERO;
1590 m[BJ_M4(2,0)] = BJ_FZERO;
1591 m[BJ_M4(2,1)] = BJ_FZERO;
1592 m[BJ_M4(2,2)] = (zmax - zmin);
1593 m[BJ_M4(2,3)] = BJ_FZERO;
1594 m[BJ_M4(3,0)] = x + BJ_F(0.5) * w;
1595 m[BJ_M4(3,1)] = y + BJ_F(0.5) * h;
1596 m[BJ_M4(3,2)] = zmin;
1597 m[BJ_M4(3,3)] = BJ_F(1.0);
1598}
1599
1609 struct bj_mat4x4* BJ_RESTRICT M,
1610 struct bj_vec3 eye,
1611 struct bj_vec3 center,
1612 struct bj_vec3 up
1613) {
1614 struct bj_vec3 f = bj_vec3_normalize(bj_vec3_sub(center, eye));
1615 struct bj_vec3 s = bj_vec3_normalize(bj_vec3_cross(up, f));
1616 struct bj_vec3 t = bj_vec3_cross(f, s);
1617
1619 M->m[BJ_M4(0,0)] = s.x;
1620 M->m[BJ_M4(0,1)] = t.x;
1621 M->m[BJ_M4(0,2)] = f.x;
1622 M->m[BJ_M4(1,0)] = s.y;
1623 M->m[BJ_M4(1,1)] = t.y;
1624 M->m[BJ_M4(1,2)] = f.y;
1625 M->m[BJ_M4(2,0)] = s.z;
1626 M->m[BJ_M4(2,1)] = t.z;
1627 M->m[BJ_M4(2,2)] = f.z;
1628 bj_mat4_translate(M, -eye.x, -eye.y, -eye.z);
1629}
1630
1636 struct bj_mat4x3* BJ_RESTRICT M
1637) {
1638 bj_real* m = M->m;
1639 m[BJ_M43(0,0)] = BJ_F(1.0);
1640 m[BJ_M43(0,1)] = BJ_FZERO;
1641 m[BJ_M43(0,2)] = BJ_FZERO;
1642 m[BJ_M43(1,0)] = BJ_FZERO;
1643 m[BJ_M43(1,1)] = BJ_F(1.0);
1644 m[BJ_M43(1,2)] = BJ_FZERO;
1645 m[BJ_M43(2,0)] = BJ_FZERO;
1646 m[BJ_M43(2,1)] = BJ_FZERO;
1647 m[BJ_M43(2,2)] = BJ_F(1.0);
1648 m[BJ_M43(3,0)] = BJ_FZERO;
1649 m[BJ_M43(3,1)] = BJ_FZERO;
1650 m[BJ_M43(3,2)] = BJ_FZERO;
1651}
1652
1664 struct bj_mat4x3* BJ_RESTRICT M,
1665 bj_real tx,
1666 bj_real ty,
1667 bj_real tz
1668) {
1670 M->m[BJ_M43(3,0)] = tx;
1671 M->m[BJ_M43(3,1)] = ty;
1672 M->m[BJ_M43(3,2)] = tz;
1673}
1674
1687 struct bj_mat4x3* BJ_RESTRICT M,
1688 bj_real sx,
1689 bj_real sy,
1690 bj_real sz
1691) {
1693 M->m[BJ_M43(0,0)] = sx;
1694 M->m[BJ_M43(1,1)] = sy;
1695 M->m[BJ_M43(2,2)] = sz;
1696}
1697
1708 struct bj_mat4x3* BJ_RESTRICT M,
1709 bj_real a
1710) {
1711 const bj_real c = bj_cos(a), s = bj_sin(a);
1713 M->m[BJ_M43(1,1)] = c; M->m[BJ_M43(1,2)] = s;
1714 M->m[BJ_M43(2,1)] = -s; M->m[BJ_M43(2,2)] = c;
1715}
1716
1727 struct bj_mat4x3* BJ_RESTRICT M,
1728 bj_real a
1729) {
1730 const bj_real c = bj_cos(a), s = bj_sin(a);
1732 M->m[BJ_M43(0,0)] = c; M->m[BJ_M43(0,2)] = -s;
1733 M->m[BJ_M43(2,0)] = s; M->m[BJ_M43(2,2)] = c;
1734}
1735
1746 struct bj_mat4x3* BJ_RESTRICT M,
1747 bj_real a
1748) {
1749 const bj_real c = bj_cos(a), s = bj_sin(a);
1751 M->m[BJ_M43(0,0)] = c; M->m[BJ_M43(0,1)] = s;
1752 M->m[BJ_M43(1,0)] = -s; M->m[BJ_M43(1,1)] = c;
1753}
1754
1763 struct bj_mat4x3* BJ_RESTRICT out,
1764 const struct bj_mat4x3* BJ_RESTRICT A,
1765 const struct bj_mat4x3* BJ_RESTRICT B
1766){
1767
1768 const bj_real a00 = A->m[BJ_M43(0,0)];
1769 const bj_real a10 = A->m[BJ_M43(0,1)];
1770 const bj_real a20 = A->m[BJ_M43(0,2)];
1771 const bj_real a30 = A->m[BJ_M43(3,0)];
1772 const bj_real a01 = A->m[BJ_M43(1,0)];
1773 const bj_real a11 = A->m[BJ_M43(1,1)];
1774 const bj_real a21 = A->m[BJ_M43(1,2)];
1775 const bj_real a31 = A->m[BJ_M43(3,1)];
1776 const bj_real a02 = A->m[BJ_M43(2,0)];
1777 const bj_real a12 = A->m[BJ_M43(2,1)];
1778 const bj_real a22 = A->m[BJ_M43(2,2)];
1779 const bj_real a32 = A->m[BJ_M43(3,2)];
1780
1781 const bj_real b00 = B->m[BJ_M43(0,0)];
1782 const bj_real b01 = B->m[BJ_M43(1,0)];
1783 const bj_real bt0 = B->m[BJ_M43(3,0)];
1784 const bj_real b10 = B->m[BJ_M43(0,1)];
1785 const bj_real b11 = B->m[BJ_M43(1,1)];
1786 const bj_real bt1 = B->m[BJ_M43(3,1)];
1787 const bj_real b20 = B->m[BJ_M43(0,2)];
1788 const bj_real b21 = B->m[BJ_M43(1,2)];
1789 const bj_real b22 = B->m[BJ_M43(2,2)];
1790 const bj_real b02 = B->m[BJ_M43(2,0)];
1791 const bj_real b12 = B->m[BJ_M43(2,1)];
1792 const bj_real bt2 = B->m[BJ_M43(3,2)];
1793
1794 out->m[BJ_M43(0,0)] = a00 * b00 + a01 * b10 + a02 * b20;
1795 out->m[BJ_M43(0,1)] = a10 * b00 + a11 * b10 + a12 * b20;
1796 out->m[BJ_M43(0,2)] = a20 * b00 + a21 * b10 + a22 * b20;
1797
1798 out->m[BJ_M43(1,0)] = a00 * b01 + a01 * b11 + a02 * b21;
1799 out->m[BJ_M43(1,1)] = a10 * b01 + a11 * b11 + a12 * b21;
1800 out->m[BJ_M43(1,2)] = a20 * b01 + a21 * b11 + a22 * b21;
1801
1802 out->m[BJ_M43(2,0)] = a00 * b02 + a01 * b12 + a02 * b22;
1803 out->m[BJ_M43(2,1)] = a10 * b02 + a11 * b12 + a12 * b22;
1804 out->m[BJ_M43(2,2)] = a20 * b02 + a21 * b12 + a22 * b22;
1805
1806 out->m[BJ_M43(3,0)] = a00 * bt0 + a01 * bt1 + a02 * bt2 + a30;
1807 out->m[BJ_M43(3,1)] = a10 * bt0 + a11 * bt1 + a12 * bt2 + a31;
1808 out->m[BJ_M43(3,2)] = a20 * bt0 + a21 * bt1 + a22 * bt2 + a32;
1809}
1810
1819 const struct bj_mat4x3* BJ_RESTRICT M,
1820 struct bj_vec3 p
1821){
1822 const bj_real* m = M->m; const bj_real x = p.x,y = p.y,z = p.z;
1823 return (struct bj_vec3){
1824 m[BJ_M43(0,0)]*x + m[BJ_M43(1,0)]*y + m[BJ_M43(2,0)]*z + m[BJ_M43(3,0)],
1825 m[BJ_M43(0,1)]*x + m[BJ_M43(1,1)]*y + m[BJ_M43(2,1)]*z + m[BJ_M43(3,1)],
1826 m[BJ_M43(0,2)]*x + m[BJ_M43(1,2)]*y + m[BJ_M43(2,2)]*z + m[BJ_M43(3,2)]
1827 };
1828}
1829
1838 const struct bj_mat4x3* BJ_RESTRICT M,
1839 struct bj_vec3 v
1840){
1841 const bj_real* m = M->m; const bj_real x = v.x,y = v.y,z = v.z;
1842 return (struct bj_vec3){
1843 m[BJ_M43(0,0)]*x + m[BJ_M43(1,0)]*y + m[BJ_M43(2,0)]*z,
1844 m[BJ_M43(0,1)]*x + m[BJ_M43(1,1)]*y + m[BJ_M43(2,1)]*z,
1845 m[BJ_M43(0,2)]*x + m[BJ_M43(1,2)]*y + m[BJ_M43(2,2)]*z
1846 };
1847}
1848
1855 struct bj_mat4x4* BJ_RESTRICT M,
1856 const struct bj_mat4x3* BJ_RESTRICT A
1857){
1858 const bj_real* a = A->m; bj_real* o = M->m;
1859 o[BJ_M4(0,0)] = a[BJ_M43(0,0)];
1860 o[BJ_M4(0,1)] = a[BJ_M43(0,1)];
1861 o[BJ_M4(0,2)] = a[BJ_M43(0,2)];
1862 o[BJ_M4(0,3)] = BJ_FZERO;
1863 o[BJ_M4(1,0)] = a[BJ_M43(1,0)];
1864 o[BJ_M4(1,1)] = a[BJ_M43(1,1)];
1865 o[BJ_M4(1,2)] = a[BJ_M43(1,2)];
1866 o[BJ_M4(1,3)] = BJ_FZERO;
1867 o[BJ_M4(2,0)] = a[BJ_M43(2,0)];
1868 o[BJ_M4(2,1)] = a[BJ_M43(2,1)];
1869 o[BJ_M4(2,2)] = a[BJ_M43(2,2)];
1870 o[BJ_M4(2,3)] = BJ_FZERO;
1871 o[BJ_M4(3,0)] = a[BJ_M43(3,0)];
1872 o[BJ_M4(3,1)] = a[BJ_M43(3,1)];
1873 o[BJ_M4(3,2)] = a[BJ_M43(3,2)];
1874 o[BJ_M4(3,3)] = BJ_F(1.0);
1875}
1876
1883 struct bj_mat4x3* BJ_RESTRICT M,
1884 const struct bj_mat4x4* BJ_RESTRICT A
1885){
1886 const bj_real* a = A->m; bj_real* o = M->m;
1887 o[BJ_M43(0,0)] = a[BJ_M4(0,0)];
1888 o[BJ_M43(0,1)] = a[BJ_M4(0,1)];
1889 o[BJ_M43(0,2)] = a[BJ_M4(0,2)];
1890 o[BJ_M43(1,0)] = a[BJ_M4(1,0)];
1891 o[BJ_M43(1,1)] = a[BJ_M4(1,1)];
1892 o[BJ_M43(1,2)] = a[BJ_M4(1,2)];
1893 o[BJ_M43(2,0)] = a[BJ_M4(2,0)];
1894 o[BJ_M43(2,1)] = a[BJ_M4(2,1)];
1895 o[BJ_M43(2,2)] = a[BJ_M4(2,2)];
1896 o[BJ_M43(3,0)] = a[BJ_M4(3,0)];
1897 o[BJ_M43(3,1)] = a[BJ_M4(3,1)];
1898 o[BJ_M43(3,2)] = a[BJ_M4(3,2)];
1899}
1900
1901#endif
General-purpose definitions for Banjo API.
uint32_t bj_bool
Boolean type used throughout the Banjo API.
Definition api.h:257
#define BJ_INLINE
BJ_INLINE expands to an inline specifier appropriate for the toolchain.
Definition api.h:241
#define BJ_RESTRICT
BJ_RESTRICT expands to the appropriate restrict qualifier per toolchain.
Definition api.h:207
#define BJ_FALSE
Boolean false value (0).
Definition api.h:266
#define BJ_TRUE
Boolean true value (1).
Definition api.h:275
bj_real z
Z component.
Definition vec.h:82
bj_real x
X component.
Definition vec.h:52
bj_real x
X component.
Definition vec.h:65
bj_real y
Y component.
Definition vec.h:53
bj_real m[6]
Definition mat.h:70
bj_real z
Z component.
Definition vec.h:67
bj_real x
X component.
Definition vec.h:80
bj_real m[16]
Definition mat.h:79
bj_real m[9]
Definition mat.h:59
bj_real m[12]
Definition mat.h:89
bj_real y
Y component.
Definition vec.h:66
bj_real y
Y component.
Definition vec.h:81
static void bj_mat3x2_set_scaling_xy(struct bj_mat3x2 *restrict M, bj_real sx, bj_real sy)
Build a 3×2 non-uniform XY scale.
Definition mat.h:570
static bj_real bj_vec2_len(struct bj_vec2 v)
Euclidean length: sqrt(x^2 + y^2).
Definition vec.h:219
static void bj_mat4_scale_axes(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, bj_real sx, bj_real sy, bj_real sz)
Scale basis vectors of a 4×4 matrix by per-axis factors.
Definition mat.h:883
#define BJ_M43(c, r)
Index a 4×3 matrix element at column c, row r. 0 <= c < 4, 0 <= r < 3.
Definition mat.h:91
static bj_bool bj_mat3_invert(struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A)
Invert a 3×3 matrix (safe, adjugate).
Definition mat.h:407
static struct bj_vec3 bj_vec3_scale(struct bj_vec3 v, bj_real s)
Uniform scaling by a scalar: v * s.
Definition vec.h:411
static struct bj_vec3 bj_vec3_sub(struct bj_vec3 lhs, struct bj_vec3 rhs)
Component-wise subtraction: lhs - rhs.
Definition vec.h:393
static bj_real bj_mat3_determinant(const struct bj_mat3x3 *restrict A)
Determinant of a 3×3 matrix.
Definition mat.h:383
static struct bj_vec2 bj_mat3x2_transform_point(const struct bj_mat3x2 *restrict M, struct bj_vec2 p)
Transform a 2D point by a 3×2 affine matrix.
Definition mat.h:646
static void bj_mat3_set_identity(struct bj_mat3x3 *restrict M)
Set a 3×3 matrix to identity.
Definition mat.h:97
static void bj_mat4x3_set_rotation_y(struct bj_mat4x3 *restrict M, bj_real a)
Build a 4×3 Y-axis rotation.
Definition mat.h:1726
static bj_real bj_vec2_dot(struct bj_vec2 a, struct bj_vec2 b)
Dot product: a.x*b.x + a.y*b.y.
Definition vec.h:189
static void bj_mat4x3_from_mat4(struct bj_mat4x3 *restrict M, const struct bj_mat4x4 *restrict A)
Demote a 4×4 matrix to 4×3 (drop projective terms).
Definition mat.h:1882
#define BJ_M32(c, r)
Index a 3×2 matrix element at column c, row r. 0 <= c < 3, 0 <= r < 2.
Definition mat.h:72
static void bj_mat4_orthonormalize(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A)
Orthonormalize the 3×3 linear part of a 4×4 matrix.
Definition mat.h:1402
static struct bj_vec4 bj_mat4_row(const struct bj_mat4x4 *restrict M, int r)
Extract a matrix row as a vector.
Definition mat.h:771
static void bj_mat3x2_from_mat3(struct bj_mat3x2 *restrict M, const struct bj_mat3x3 *restrict A)
Demote a 3×3 matrix to 3×2 (drop projective terms).
Definition mat.h:711
static void bj_mat3_set_shear_xy(struct bj_mat3x3 *restrict M, bj_real shx, bj_real shy)
Build an XY shear into a 3×3 matrix.
Definition mat.h:345
static struct bj_vec3 bj_mat3_row(const struct bj_mat3x3 *restrict M, int r)
Extract a matrix row as a vector.
Definition mat.h:124
static void bj_mat4_rotate_z(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M, bj_real a)
Right-multiply a 4×4 by a Z-axis rotation: out = M * Rz(a).
Definition mat.h:1200
static void bj_mat4_set_identity(struct bj_mat4x4 *restrict M)
Set a 4×4 matrix to identity.
Definition mat.h:729
static bj_real bj_vec4_dot(struct bj_vec4 a, struct bj_vec4 b)
Dot product over all four components.
Definition vec.h:691
static void bj_mat4_copy(struct bj_mat4x4 *restrict dst, const struct bj_mat4x4 *restrict src)
Copy a matrix.
Definition mat.h:756
#define bj_tan
Tangent.
Definition math.h:248
static struct bj_vec3 bj_mat4x3_transform_dir(const struct bj_mat4x3 *restrict M, struct bj_vec3 v)
Transform a direction vector (ignoring translation).
Definition mat.h:1837
static struct bj_vec3 bj_mat3_transform_vec3(const struct bj_mat3x3 *restrict M, struct bj_vec3 v)
Multiply a 3×3 matrix by a 3D vector: r = M * v.
Definition mat.h:245
static void bj_mat4_set_outer_product(struct bj_mat4x4 *restrict out, struct bj_vec3 a, struct bj_vec3 b)
Build the outer product a * b^T as a 4×4 matrix.
Definition mat.h:1033
#define BJ_M3(c, r)
Index a 3×3 matrix element at column c, row r. 0 <= c,r < 3.
Definition mat.h:61
static void bj_mat4_rotate_x(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M, bj_real a)
Right-multiply a 4×4 by an X-axis rotation: out = M * Rx(a).
Definition mat.h:1131
static bj_real bj_vec3_len(struct bj_vec3 v)
Euclidean length: sqrt(x^2 + y^2 + z^2).
Definition vec.h:436
static void bj_mat4_set_translation(struct bj_mat4x4 *restrict M, bj_real x, bj_real y, bj_real z)
Build a 4×4 translation matrix.
Definition mat.h:987
static void bj_mat4_mul_scalar(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, bj_real k)
Scalar multiply: out = A * k.
Definition mat.h:864
static void bj_mat4_set_viewport(struct bj_mat4x4 *restrict M, bj_real x, bj_real y, bj_real w, bj_real h)
Build a 3D viewport transform into a 4×4 matrix.
Definition mat.h:1572
static void bj_mat4x3_set_rotation_x(struct bj_mat4x3 *restrict M, bj_real a)
Build a 4×3 X-axis rotation.
Definition mat.h:1707
#define bj_acos
Arc cosine.
Definition math.h:234
static void bj_mat3x2_mul(struct bj_mat3x2 *restrict out, const struct bj_mat3x2 *restrict A, const struct bj_mat3x2 *restrict B)
Matrix product: out = A * B.
Definition mat.h:607
static void bj_mat4_rotate_y(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M, bj_real a)
Right-multiply a 4×4 by a Y-axis rotation: out = M * Ry(a).
Definition mat.h:1166
static void bj_mat4_translate(struct bj_mat4x4 *restrict M, bj_real x, bj_real y, bj_real z)
Right-multiply by a translation: M = M * T(x, y, z).
Definition mat.h:1009
static void bj_mat4_set_ortho(struct bj_mat4x4 *restrict M, bj_real l, bj_real r, bj_real b, bj_real t, bj_real n, bj_real f)
Build a 3D orthographic projection into a 4×4 matrix.
Definition mat.h:1501
static void bj_mat4x3_set_translation(struct bj_mat4x3 *restrict M, bj_real tx, bj_real ty, bj_real tz)
Build a 4×3 translation matrix.
Definition mat.h:1663
static void bj_mat4x3_mul(struct bj_mat4x3 *restrict out, const struct bj_mat4x3 *restrict A, const struct bj_mat4x3 *restrict B)
Matrix product: out = A * B.
Definition mat.h:1762
static void bj_mat3_set_ortho(struct bj_mat3x3 *restrict M, bj_real l, bj_real r, bj_real b, bj_real t)
Build a 2D orthographic projection into a 3×3 matrix.
Definition mat.h:479
static void bj_mat3_from_mat3x2(struct bj_mat3x3 *restrict M, const struct bj_mat3x2 *restrict A)
Promote a 3×2 affine matrix to 3×3.
Definition mat.h:689
#define bj_sin
Sine.
Definition math.h:246
static void bj_mat4_invert_unsafe(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M)
Invert a 4×4 matrix (unsafe).
Definition mat.h:1349
static struct bj_vec3 bj_mat4x3_transform_point(const struct bj_mat4x3 *restrict M, struct bj_vec3 p)
Transform a 3D point by a 4×3 affine matrix.
Definition mat.h:1818
static void bj_mat4x3_set_scaling_xyz(struct bj_mat4x3 *restrict M, bj_real sx, bj_real sy, bj_real sz)
Build a 4×3 non-uniform XYZ scale.
Definition mat.h:1686
static void bj_mat4x3_set_rotation_z(struct bj_mat4x3 *restrict M, bj_real a)
Build a 4×3 Z-axis rotation.
Definition mat.h:1745
static void bj_mat3x2_set_identity(struct bj_mat3x2 *restrict M)
Set a 3×2 affine matrix to identity.
Definition mat.h:529
#define BJ_FZERO
Zero constant in bj_real.
Definition math.h:89
static void bj_mat3x2_set_rotation_z(struct bj_mat3x2 *restrict M, bj_real angle)
Build a 3×2 Z-axis (2D) rotation.
Definition mat.h:585
static bj_real bj_vec3_dot(struct bj_vec3 a, struct bj_vec3 b)
Dot product: a.x*b.x + a.y*b.y + a.z*b.z.
Definition vec.h:426
static void bj_mat3x2_set_translation(struct bj_mat3x2 *restrict M, bj_real tx, bj_real ty)
Build a 3×2 translation matrix.
Definition mat.h:550
static void bj_mat3_set_scaling_xy(struct bj_mat3x3 *restrict M, bj_real sx, bj_real sy)
Build a non-uniform XY scale into a 3×3 matrix.
Definition mat.h:326
static struct bj_vec2 bj_vec2_normalize(struct bj_vec2 v)
Normalise to unit length (safe).
Definition vec.h:286
static void bj_mat4_from_mat4x3(struct bj_mat4x4 *restrict M, const struct bj_mat4x3 *restrict A)
Promote a 4×3 affine matrix to 4×4.
Definition mat.h:1854
static struct bj_vec2 bj_mat3x2_transform_dir(const struct bj_mat3x2 *restrict M, struct bj_vec2 v)
Transform a direction vector by a 3×2 affine matrix (ignoring translation).
Definition mat.h:671
static int bj_real_is_zero(bj_real x)
Absolute-zero test.
Definition math.h:479
static void bj_mat4_add(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, const struct bj_mat4x4 *restrict B)
Component-wise addition: out = A + B.
Definition mat.h:830
static struct bj_vec4 bj_mat4_transform_vec4(const struct bj_mat4x4 *restrict M, struct bj_vec4 v)
Multiply a 4×4 matrix by a 4D vector: r = M * v.
Definition mat.h:960
static void bj_mat4_transpose(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A)
Transpose a matrix.
Definition mat.h:808
static struct bj_vec3 bj_vec3_cross(struct bj_vec3 l, struct bj_vec3 r)
3D cross product: l × r (right-hand rule).
Definition vec.h:567
static struct bj_vec2 bj_mat3_transform_point(const struct bj_mat3x3 *restrict M, struct bj_vec2 p)
Transform a 2D point by a 3×3 homogeneous transform.
Definition mat.h:264
static void bj_mat3_invert_unsafe(struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A)
Invert a 3×3 matrix (unsafe, adjugate).
Definition mat.h:444
static void bj_mat4_rotate_arcball(struct bj_mat4x4 *restrict R, const struct bj_mat4x4 *restrict M, struct bj_vec2 a, struct bj_vec2 b, bj_real s)
Right-multiply by an arcball rotation between two screen points.
Definition mat.h:1242
#define bj_cos
Cosine.
Definition math.h:237
static void bj_mat3_mul_scalar(struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, bj_real k)
Scalar multiply: out = A * k.
Definition mat.h:202
static struct bj_vec4 bj_mat4_col(const struct bj_mat4x4 *restrict M, int c)
Extract a matrix column as a vector.
Definition mat.h:790
static void bj_mat4_set_perspective(struct bj_mat4x4 *restrict M, bj_real y_fov, bj_real aspect, bj_real n, bj_real f)
Build a 4×4 perspective projection from vertical FOV.
Definition mat.h:1537
static void bj_mat3_set_translation(struct bj_mat3x3 *restrict M, bj_real tx, bj_real ty)
Build a 3×3 translation matrix.
Definition mat.h:286
static void bj_mat4_set_lookat(struct bj_mat4x4 *restrict M, struct bj_vec3 eye, struct bj_vec3 center, struct bj_vec3 up)
Build a right-handed look-at view matrix.
Definition mat.h:1608
static void bj_mat3_translate(struct bj_mat3x3 *restrict M, bj_real tx, bj_real ty)
Right-multiply by a translation: M = M * T(tx,ty).
Definition mat.h:303
static void bj_mat3_mul(struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, const struct bj_mat3x3 *restrict B)
Matrix product: out = A * B.
Definition mat.h:219
static struct bj_vec3 bj_mat3_col(const struct bj_mat3x3 *restrict M, int c)
Extract a matrix column as a vector.
Definition mat.h:137
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
Definition math.h:78
static struct bj_vec3 bj_vec3_normalize(struct bj_vec3 v)
Normalise to unit length (safe).
Definition vec.h:502
static void bj_mat3_set_rotation_z(struct bj_mat3x3 *restrict M, bj_real angle)
Build a Z-axis (i.e.
Definition mat.h:360
static void bj_mat4_sub(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, const struct bj_mat4x4 *restrict B)
Component-wise subtraction: out = A - B.
Definition mat.h:847
static void bj_mat3_copy(struct bj_mat3x3 *restrict dst, const struct bj_mat3x3 *restrict src)
Copy a matrix.
Definition mat.h:109
static void bj_mat4_set_frustum(struct bj_mat4x4 *restrict M, bj_real l, bj_real r, bj_real b, bj_real t, bj_real n, bj_real f)
Build a perspective frustum projection into a 4×4 matrix.
Definition mat.h:1458
static void bj_mat3_sub(struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, const struct bj_mat3x3 *restrict B)
Component-wise subtraction: out = A - B.
Definition mat.h:186
static void bj_mat3_transpose(struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A)
Transpose a matrix.
Definition mat.h:147
#define bj_sqrt
Square root.
Definition math.h:247
float bj_real
Selected real type for float configuration.
Definition math.h:76
static bj_bool bj_mat4_invert(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M)
Invert a 4×4 matrix (safe).
Definition mat.h:1285
#define BJ_M4(c, r)
Index a 4×4 matrix element at column c, row r. 0 <= c,r < 4.
Definition mat.h:81
static void bj_mat3_set_viewport(struct bj_mat3x3 *restrict M, bj_real x, bj_real y, bj_real w, bj_real h)
Build a 2D viewport transform into a 3×3 matrix.
Definition mat.h:506
static void bj_mat4x3_set_identity(struct bj_mat4x3 *restrict M)
Set a 4×3 affine matrix to identity.
Definition mat.h:1635
static void bj_mat4_rotate_axis_angle(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M, struct bj_vec3 axis, bj_real angle)
Right-multiply a 4×4 by a rotation around an arbitrary axis.
Definition mat.h:1070
static void bj_mat4_mul(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, const struct bj_mat4x4 *restrict B)
Matrix product: out = A * B.
Definition mat.h:918
static void bj_mat3_add(struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, const struct bj_mat3x3 *restrict B)
Component-wise addition: out = A + B.
Definition mat.h:170
3×2 column-major matrix (2D affine).
Definition mat.h:70
3×3 column-major matrix.
Definition mat.h:59
4×3 column-major matrix (3D affine).
Definition mat.h:89
4×4 column-major matrix.
Definition mat.h:79
2D vector of bj_real components.
Definition vec.h:51
3D vector of bj_real components.
Definition vec.h:64
4D vector of bj_real components.
Definition vec.h:79
C99 math shim with bj_real precision type and scalar utilities.
Fixed-size vector types (2D, 3D, 4D) and inline operations.