Add some more mat3 functions

This commit is contained in:
Alex Selimov 2026-10-26 23:28:32 -04:00
parent 55ba9f6cde
commit 7e5de6f6eb
3 changed files with 108 additions and 0 deletions

View file

@ -0,0 +1,28 @@
#include "mathprimitives.h"
struct Mat3 mat3_inv(struct Mat3 a) {
double inv_det = 1.0 / mat3_det(a);
struct Vec3 r1 = vec3_scale(
(struct Vec3){
a.r2.y * a.r3.z - a.r2.z * a.r3.y,
a.r1.z * a.r3.y - a.r1.y * a.r3.z,
a.r1.y * a.r2.z - a.r1.z * a.r2.y,
},
inv_det);
struct Vec3 r2 = vec3_scale(
(struct Vec3){
a.r2.z * a.r3.x - a.r2.x * a.r3.z,
a.r1.x * a.r3.z - a.r1.z * a.r3.x,
a.r1.z * a.r2.x - a.r1.x * a.r2.z,
},
inv_det);
struct Vec3 r3 = vec3_scale(
(struct Vec3){
a.r2.x * a.r3.y - a.r2.y * a.r3.x,
a.r1.y * a.r3.x - a.r1.x * a.r3.y,
a.r1.x * a.r2.y - a.r1.y * a.r2.x,
},
inv_det);
return (struct Mat3){r1, r2, r3};
}