Add some more mat3 functions
This commit is contained in:
parent
55ba9f6cde
commit
7e5de6f6eb
3 changed files with 108 additions and 0 deletions
28
mathprimitives/mathprimitives.c
Normal file
28
mathprimitives/mathprimitives.c
Normal 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};
|
||||
}
|
||||
|
|
@ -62,4 +62,42 @@ static inline struct Mat3 mat3_add(struct Mat3 a, struct Mat3 b) {
|
|||
return (struct Mat3){r1, r2, r3};
|
||||
}
|
||||
|
||||
static inline struct Mat3 mat3_sub(struct Mat3 a, struct Mat3 b) {
|
||||
struct Vec3 r1 = vec3_sub(a.r1, b.r1);
|
||||
struct Vec3 r2 = vec3_sub(a.r2, b.r2);
|
||||
struct Vec3 r3 = vec3_sub(a.r3, b.r3);
|
||||
|
||||
return (struct Mat3){r1, r2, r3};
|
||||
}
|
||||
|
||||
static inline struct Mat3 mat3_mul(struct Mat3 a, struct Mat3 b) {
|
||||
struct Vec3 r1 = {
|
||||
a.r1.x * b.r1.x + a.r1.y * b.r2.x + a.r1.z * b.r3.x,
|
||||
a.r1.x * b.r1.y + a.r1.y * b.r2.y + a.r1.z * b.r3.y,
|
||||
a.r1.x * b.r1.z + a.r1.y * b.r2.z + a.r1.z * b.r3.z,
|
||||
};
|
||||
|
||||
struct Vec3 r2 = {
|
||||
a.r2.x * b.r1.x + a.r2.y * b.r2.x + a.r2.z * b.r3.x,
|
||||
a.r2.x * b.r1.y + a.r2.y * b.r2.y + a.r2.z * b.r3.y,
|
||||
a.r2.x * b.r1.z + a.r2.y * b.r2.z + a.r2.z * b.r3.z,
|
||||
};
|
||||
|
||||
struct Vec3 r3 = {
|
||||
a.r3.x * b.r1.x + a.r3.y * b.r2.x + a.r3.z * b.r3.x,
|
||||
a.r3.x * b.r1.y + a.r3.y * b.r2.y + a.r3.z * b.r3.y,
|
||||
a.r3.x * b.r1.z + a.r3.y * b.r2.z + a.r3.z * b.r3.z,
|
||||
};
|
||||
|
||||
return (struct Mat3){r1, r2, r3};
|
||||
}
|
||||
|
||||
static inline double mat3_det(struct Mat3 a) {
|
||||
return a.r1.x * (a.r2.y * a.r3.z - a.r2.z * a.r3.y) -
|
||||
a.r1.y * (a.r2.x * a.r3.z - a.r2.z * a.r3.x) +
|
||||
a.r1.z * (a.r2.x * a.r3.y - a.r2.y * a.r3.x);
|
||||
}
|
||||
|
||||
struct Mat3 mat3_inv(struct Mat3 a);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -97,6 +97,44 @@ int test_mat3_add() {
|
|||
struct Mat3 expected_c = {{1, 3, 3}, {0, 2, 6}, {0, 0, 2}};
|
||||
return mat3_eql_tol(expected_c, mat3_add(a, b), 1e-12);
|
||||
}
|
||||
|
||||
int test_mat3_sub() {
|
||||
struct Mat3 a = {{1, 2, 3}, {0, 1, 5}, {0, 0, 0}};
|
||||
struct Mat3 b = {{0, 1, 0}, {0, 1, 1}, {0, 0, 2}};
|
||||
|
||||
struct Mat3 expected_c = {{1, 1, 3}, {0, 0, 4}, {0, 0, -2}};
|
||||
return mat3_eql_tol(expected_c, mat3_sub(a, b), 1e-12);
|
||||
}
|
||||
|
||||
int test_mat3_mul() {
|
||||
struct Mat3 a = {{1, 2, 3}, {0, 1, 5}, {0, 0, 9}};
|
||||
struct Mat3 b = {{0, 1, 0}, {0, 1, 1}, {0, 0, 1}};
|
||||
|
||||
struct Mat3 expected_c = {{0, 3, 5}, {0, 1, 6}, {0, 0, 9}};
|
||||
return mat3_eql_tol(expected_c, mat3_mul(a, b), 1e-12);
|
||||
}
|
||||
|
||||
int test_mat3_det() {
|
||||
struct Mat3 a = {{1, 2, 3}, {4, 1, 5}, {3, 1, 9}};
|
||||
double expected_det = -35.0;
|
||||
return fabs(mat3_det(a) - expected_det) < 1e-12;
|
||||
}
|
||||
|
||||
int test_mat3_inv() {
|
||||
struct Mat3 a = {
|
||||
{1.0, 2.0, 3.0},
|
||||
{4.0, 1.0, 5.0},
|
||||
{3.0, 1.0, 9.0},
|
||||
};
|
||||
|
||||
struct Mat3 expected_inv = {{-4.0 / 35.0, 3.0 / 7.0, -1.0 / 5.0},
|
||||
{3.0 / 5.0, 0.0, -1.0 / 5.0},
|
||||
{-1.0 / 35.0, -1.0 / 7.0, 1.0 / 5.0}};
|
||||
|
||||
struct Mat3 inv_a = mat3_inv(a);
|
||||
return mat3_eql_tol(inv_a, expected_inv, 1e-11);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int tests_total = 0;
|
||||
int tests_passed = 0;
|
||||
|
|
@ -110,6 +148,10 @@ int main() {
|
|||
run_test(test_mat3_identity);
|
||||
run_test(test_mat3_eql);
|
||||
run_test(test_mat3_add);
|
||||
run_test(test_mat3_sub);
|
||||
run_test(test_mat3_mul);
|
||||
run_test(test_mat3_det);
|
||||
run_test(test_mat3_inv);
|
||||
|
||||
if (tests_total == tests_passed) {
|
||||
printf("\033[92mAll tests passed!\033[0m");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue