From 4da7be39827ea5888ef9c97b1aadf61b0d76347c Mon Sep 17 00:00:00 2001 From: Silas Bartha Date: Fri, 7 Feb 2025 11:27:18 -0500 Subject: initial commit (lol) --- mons_math/src/mat2.c | 123 ++++++++++++++++++++ mons_math/src/mat3.c | 237 +++++++++++++++++++++++++++++++++++++ mons_math/src/mat4.c | 321 +++++++++++++++++++++++++++++++++++++++++++++++++++ mons_math/src/quat.c | 141 ++++++++++++++++++++++ mons_math/src/util.c | 7 ++ mons_math/src/vec2.c | 120 +++++++++++++++++++ mons_math/src/vec3.c | 175 ++++++++++++++++++++++++++++ mons_math/src/vec4.c | 149 ++++++++++++++++++++++++ 8 files changed, 1273 insertions(+) create mode 100644 mons_math/src/mat2.c create mode 100644 mons_math/src/mat3.c create mode 100644 mons_math/src/mat4.c create mode 100644 mons_math/src/quat.c create mode 100644 mons_math/src/util.c create mode 100644 mons_math/src/vec2.c create mode 100644 mons_math/src/vec3.c create mode 100644 mons_math/src/vec4.c (limited to 'mons_math/src') diff --git a/mons_math/src/mat2.c b/mons_math/src/mat2.c new file mode 100644 index 0000000..026b165 --- /dev/null +++ b/mons_math/src/mat2.c @@ -0,0 +1,123 @@ +#include "mons_math/mat2.h" + +const mons_mat2 MONS_MAT2_ZERO = { + {0, 0}, + {0, 0}, +}; + +const mons_mat2 MONS_MAT2_IDENTITY = { + {1, 0}, + {0, 1}, +}; + +mons_mat2 mons_mat2_add(mons_mat2 a, mons_mat2 b) { + mons_mat2 result = { + mons_vec2_add(a.m1, b.m1), + mons_vec2_add(a.m2, b.m2), + }; + return result; +} + +void mons_mat2_add_inplace(mons_mat2 *a, mons_mat2 b) { + mons_vec2_add_inplace(&a->m1, b.m1); + mons_vec2_add_inplace(&a->m2, b.m2); +} + +mons_mat2 mons_mat2_mul_f(mons_mat2 a, float b) { + mons_mat2 result = { + mons_vec2_mul_f(a.m1, b), + mons_vec2_mul_f(a.m2, b), + }; + return result; +} + +mons_mat2 mons_mat2_mul_i(mons_mat2 a, int b) { + mons_mat2 result = { + mons_vec2_mul_i(a.m1, b), + mons_vec2_mul_i(a.m2, b), + }; + return result; +} + +mons_mat2 mons_mat2_mul(mons_mat2 a, mons_mat2 b) { + mons_vec2 b_n1 = mons_mat2_n1(b); + mons_vec2 b_n2 = mons_mat2_n2(b); + + mons_mat2 result = { + { + mons_vec2_dot(a.m1, b_n1), + mons_vec2_dot(a.m1, b_n2), + }, + { + mons_vec2_dot(a.m2, b_n1), + mons_vec2_dot(a.m2, b_n2), + }, + }; + return result; +} + +void mons_mat2_mul_f_inplace(mons_mat2 *a, float b) { + mons_vec2_mul_f_inplace(&a->m1, b); + mons_vec2_mul_f_inplace(&a->m2, b); +} + +void mons_mat2_mul_i_inplace(mons_mat2 *a, int b) { + mons_vec2_mul_i_inplace(&a->m1, b); + mons_vec2_mul_i_inplace(&a->m2, b); +} + +mons_mat2 mons_mat2_transpose(mons_mat2 a) { + mons_mat2 result = { + mons_mat2_n1(a), + mons_mat2_n2(a), + }; + return result; +} + +void mons_mat2_transpose_inplace(mons_mat2 *a) { *a = mons_mat2_transpose(*a); } + +mons_vec2 mons_mat2_n1(mons_mat2 a) { + mons_vec2 result = { + a.m1.x, + a.m2.x, + }; + return result; +} + +mons_vec2 mons_mat2_n2(mons_mat2 a) { + mons_vec2 result = { + a.m1.y, + a.m2.y, + }; + return result; +} + +float mons_mat2_determinant(mons_mat2 a) { + // ad - bc + return (a.m1.x * a.m2.y) - (a.m1.y * a.m2.x); +} + +mons_mat2 mons_mat2_adjoint(mons_mat2 a) { + /* + * d -b + * -c a + */ + mons_mat2 result = {{ + a.m2.y, + -a.m1.y, + }, + { + -a.m2.x, + a.m1.x, + }}; + return result; +} + +mons_mat2 mons_mat2_inverse(mons_mat2 a) { + return mons_mat2_mul_f(mons_mat2_adjoint(a), + 1.0 / mons_mat2_determinant(a)); +} + +int mons_mat2_equal(mons_mat2 a, mons_mat2 b) { + return mons_vec2_equal(a.m1, b.m1) && mons_vec2_equal(a.m2, b.m2); +} diff --git a/mons_math/src/mat3.c b/mons_math/src/mat3.c new file mode 100644 index 0000000..f806375 --- /dev/null +++ b/mons_math/src/mat3.c @@ -0,0 +1,237 @@ +#include "mons_math/mat3.h" +#include "mons_math/mat2.h" +#include "mons_math/vec3.h" + +const mons_mat3 MONS_MAT3_ZERO = { + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, +}; + +const mons_mat3 MONS_MAT3_IDENTITY = { + {1, 0, 0}, + {0, 1, 0}, + {0, 0, 1}, +}; + +mons_mat3 mons_mat3_add(mons_mat3 a, mons_mat3 b) { + mons_mat3 result = { + mons_vec3_add(a.m1, b.m1), + mons_vec3_add(a.m2, b.m2), + mons_vec3_add(a.m3, b.m3), + }; + return result; +} + +void mons_mat3_add_inplace(mons_mat3 *a, mons_mat3 b) { + mons_vec3_add_inplace(&a->m1, b.m1); + mons_vec3_add_inplace(&a->m2, b.m2); + mons_vec3_add_inplace(&a->m3, b.m3); +} + +mons_mat3 mons_mat3_mul_f(mons_mat3 a, float b) { + mons_mat3 result = { + mons_vec3_mul_f(a.m1, b), + mons_vec3_mul_f(a.m2, b), + mons_vec3_mul_f(a.m3, b), + }; + return result; +} + +mons_mat3 mons_mat3_mul_i(mons_mat3 a, int b) { + mons_mat3 result = { + mons_vec3_mul_i(a.m1, b), + mons_vec3_mul_i(a.m2, b), + mons_vec3_mul_i(a.m3, b), + }; + return result; +} + +mons_mat3 mons_mat3_mul(mons_mat3 a, mons_mat3 b) { + mons_vec3 b_n1 = mons_mat3_n1(b); + mons_vec3 b_n2 = mons_mat3_n2(b); + mons_vec3 b_n3 = mons_mat3_n3(b); + + mons_mat3 result = { + { + mons_vec3_dot(a.m1, b_n1), + mons_vec3_dot(a.m1, b_n2), + mons_vec3_dot(a.m1, b_n3), + }, + { + mons_vec3_dot(a.m2, b_n1), + mons_vec3_dot(a.m2, b_n2), + mons_vec3_dot(a.m2, b_n3), + }, + { + mons_vec3_dot(a.m3, b_n1), + mons_vec3_dot(a.m3, b_n2), + mons_vec3_dot(a.m3, b_n3), + }, + }; + return result; +} + +void mons_mat3_mul_f_inplace(mons_mat3 *a, float b) { + mons_vec3_mul_f_inplace(&a->m1, b); + mons_vec3_mul_f_inplace(&a->m2, b); + mons_vec3_mul_f_inplace(&a->m3, b); +} + +void mons_mat3_mul_i_inplace(mons_mat3 *a, int b) { + mons_vec3_mul_i_inplace(&a->m1, b); + mons_vec3_mul_i_inplace(&a->m2, b); + mons_vec3_mul_i_inplace(&a->m3, b); +} + +mons_mat3 mons_mat3_transpose(mons_mat3 a) { + mons_mat3 result = { + mons_mat3_n1(a), + mons_mat3_n2(a), + mons_mat3_n3(a), + }; +} + +void mons_mat3_transpose_inplace(mons_mat3 *a) { *a = mons_mat3_transpose(*a); } + +mons_vec3 mons_mat3_n1(mons_mat3 a) { + mons_vec3 result = { + a.m1.x, + a.m2.x, + a.m3.x, + }; + return result; +} + +mons_vec3 mons_mat3_n2(mons_mat3 a) { + mons_vec3 result = { + a.m1.y, + a.m2.y, + a.m3.y, + }; + return result; +} + +mons_vec3 mons_mat3_n3(mons_mat3 a) { + mons_vec3 result = { + a.m1.z, + a.m2.z, + a.m3.z, + }; + return result; +} + +float mons_mat3_determinant(mons_mat3 a) { + /* + * a * det( + * e f + * h i + * ) + * - b * det( + * d f + * g i + * ) + * + c * det( + * d e + * g h + * ) + */ + mons_mat2 efhi = { + {a.m2.y, a.m2.z}, + {a.m3.y, a.m3.z}, + }; + mons_mat2 dfgi = { + {a.m2.x, a.m2.z}, + {a.m3.x, a.m3.z}, + }; + mons_mat2 degh = { + {a.m2.x, a.m2.y}, + {a.m3.x, a.m3.y}, + }; + + return (a.m1.x * mons_mat2_determinant(efhi)) - + (a.m1.y * mons_mat2_determinant(dfgi)) + + (a.m1.z * mons_mat2_determinant(degh)); +} + +mons_mat3 mons_mat3_minor(mons_mat3 a) { + // Get determinants of minors + // First row elements + mons_mat2 minor_1_1 = { + {a.m2.y, a.m2.z}, + {a.m3.y, a.m3.z}, + }; + float det_1_1 = mons_mat2_determinant(minor_1_1); + mons_mat2 minor_1_2 = { + {a.m2.x, a.m2.z}, + {a.m3.x, a.m3.z}, + }; + float det_1_2 = mons_mat2_determinant(minor_1_2); + mons_mat2 minor_1_3 = { + {a.m2.x, a.m2.y}, + {a.m3.x, a.m3.y}, + }; + float det_1_3 = mons_mat2_determinant(minor_1_3); + // Second row elements + mons_mat2 minor_2_1 = { + {a.m1.y, a.m1.z}, + {a.m3.y, a.m3.z}, + }; + float det_2_1 = mons_mat2_determinant(minor_2_1); + mons_mat2 minor_2_2 = { + {a.m1.x, a.m1.z}, + {a.m3.x, a.m3.z}, + }; + float det_2_2 = mons_mat2_determinant(minor_2_2); + mons_mat2 minor_2_3 = { + {a.m1.x, a.m1.y}, + {a.m3.x, a.m3.y}, + }; + float det_2_3 = mons_mat2_determinant(minor_2_3); + // Third row elements + mons_mat2 minor_3_1 = { + {a.m1.y, a.m1.z}, + {a.m2.y, a.m2.z}, + }; + float det_3_1 = mons_mat2_determinant(minor_3_1); + mons_mat2 minor_3_2 = { + {a.m1.x, a.m1.z}, + {a.m2.x, a.m2.z}, + }; + float det_3_2 = mons_mat2_determinant(minor_3_2); + mons_mat2 minor_3_3 = { + {a.m1.x, a.m1.y}, + {a.m2.x, a.m2.y}, + }; + float det_3_3 = mons_mat2_determinant(minor_3_3); + + mons_mat3 result = { + {det_1_1, det_1_2, det_1_3}, + {det_2_1, det_2_2, det_2_3}, + {det_3_1, det_3_2, det_3_3}, + }; + return result; +} + +mons_mat3 mons_mat3_cofactor(mons_mat3 a) { + mons_mat3 result = mons_mat3_minor(a); + result.m1.y *= -1; + result.m2.x *= -1; + result.m2.z *= -1; + result.m3.y *= -1; + return result; +} + +mons_mat3 mons_mat3_adjoint(mons_mat3 a) { + return mons_mat3_transpose(mons_mat3_cofactor(a)); +} + +mons_mat3 mons_mat3_inverse(mons_mat3 a) { + return mons_mat3_mul_f(mons_mat3_adjoint(a), + 1.0 / mons_mat3_determinant(a)); +} + +int mons_mat3_equal(mons_mat3 a, mons_mat3 b) { + return mons_vec3_equal(a.m1, b.m1) && mons_vec3_equal(a.m2, b.m2) && + mons_vec3_equal(a.m3, b.m3); +} diff --git a/mons_math/src/mat4.c b/mons_math/src/mat4.c new file mode 100644 index 0000000..bb92ca8 --- /dev/null +++ b/mons_math/src/mat4.c @@ -0,0 +1,321 @@ +#include "mons_math/mat4.h" +#include "mons_math/mat3.h" +#include "mons_math/vec4.h" +#include + +mons_mat4 mons_mat4_add(mons_mat4 a, mons_mat4 b) { + mons_mat4 result = { + mons_vec4_add(a.m1, b.m1), + mons_vec4_add(a.m2, b.m2), + mons_vec4_add(a.m3, b.m3), + mons_vec4_add(a.m4, b.m4), + }; + return result; +} + +void mons_mat4_add_inplace(mons_mat4 *a, mons_mat4 b) { + mons_vec4_add_inplace(&a->m1, b.m1); + mons_vec4_add_inplace(&a->m2, b.m2); + mons_vec4_add_inplace(&a->m3, b.m3); + mons_vec4_add_inplace(&a->m4, b.m4); +} + +mons_mat4 mons_mat4_mul_f(mons_mat4 a, float b) { + mons_mat4 result = { + mons_vec4_mul_f(a.m1, b), + mons_vec4_mul_f(a.m2, b), + mons_vec4_mul_f(a.m3, b), + mons_vec4_mul_f(a.m4, b), + }; + return result; +} + +mons_mat4 mons_mat4_mul_i(mons_mat4 a, int b) { + mons_mat4 result = { + mons_vec4_mul_i(a.m1, b), + mons_vec4_mul_i(a.m2, b), + mons_vec4_mul_i(a.m3, b), + mons_vec4_mul_i(a.m4, b), + }; + return result; +} + +void mons_mat4_mul_f_inplace(mons_mat4 *a, float b) { + mons_vec4_mul_f_inplace(&a->m1, b); + mons_vec4_mul_f_inplace(&a->m2, b); + mons_vec4_mul_f_inplace(&a->m3, b); + mons_vec4_mul_f_inplace(&a->m4, b); +} + +void mons_mat4_mul_i_inplace(mons_mat4 *a, int b) { + mons_vec4_mul_i_inplace(&a->m1, b); + mons_vec4_mul_i_inplace(&a->m2, b); + mons_vec4_mul_i_inplace(&a->m3, b); + mons_vec4_mul_i_inplace(&a->m4, b); +} + +mons_mat4 mons_mat4_mul(mons_mat4 a, mons_mat4 b) { + mons_vec4 b_n1 = mons_mat4_n1(b); + mons_vec4 b_n2 = mons_mat4_n2(b); + mons_vec4 b_n3 = mons_mat4_n3(b); + mons_vec4 b_n4 = mons_mat4_n4(b); + + mons_mat4 result = { + { + mons_vec4_dot(a.m1, b_n1), + mons_vec4_dot(a.m1, b_n2), + mons_vec4_dot(a.m1, b_n3), + mons_vec4_dot(a.m1, b_n4), + }, + { + mons_vec4_dot(a.m2, b_n1), + mons_vec4_dot(a.m2, b_n2), + mons_vec4_dot(a.m2, b_n3), + mons_vec4_dot(a.m2, b_n4), + }, + { + mons_vec4_dot(a.m3, b_n1), + mons_vec4_dot(a.m3, b_n2), + mons_vec4_dot(a.m3, b_n3), + mons_vec4_dot(a.m3, b_n4), + }, + { + mons_vec4_dot(a.m4, b_n1), + mons_vec4_dot(a.m4, b_n2), + mons_vec4_dot(a.m4, b_n3), + mons_vec4_dot(a.m4, b_n4), + }, + }; + return result; +} + +mons_mat4 mons_mat4_transpose(mons_mat4 a) { + mons_mat4 result = { + mons_mat4_n1(a), + mons_mat4_n2(a), + mons_mat4_n3(a), + mons_mat4_n4(a), + }; + return result; +} + +void mons_mat4_transpose_inplace(mons_mat4 *a) { *a = mons_mat4_transpose(*a); } + +mons_vec4 mons_mat4_n1(mons_mat4 a) { + mons_vec4 result = { + a.m1.x, + a.m2.x, + a.m3.x, + a.m4.x, + }; + return result; +} + +mons_vec4 mons_mat4_n2(mons_mat4 a) { + mons_vec4 result = { + a.m1.y, + a.m2.y, + a.m3.y, + a.m4.y, + }; + return result; +} + +mons_vec4 mons_mat4_n3(mons_mat4 a) { + mons_vec4 result = { + a.m1.z, + a.m2.z, + a.m3.z, + a.m4.z, + }; + return result; +} + +mons_vec4 mons_mat4_n4(mons_mat4 a) { + mons_vec4 result = { + a.m1.w, + a.m2.w, + a.m3.w, + a.m4.w, + }; + return result; +} + +float mons_mat4_determinant(mons_mat4 a) { + mons_mat3 minor_1_1 = { + {a.m2.y, a.m2.z, a.m2.w}, + {a.m3.y, a.m3.z, a.m3.w}, + {a.m4.y, a.m4.z, a.m4.w}, + }; + mons_mat3 minor_1_2 = { + {a.m2.x, a.m2.z, a.m2.w}, + {a.m3.x, a.m3.z, a.m3.w}, + {a.m4.x, a.m4.z, a.m4.w}, + }; + mons_mat3 minor_1_3 = { + {a.m2.x, a.m2.y, a.m2.w}, + {a.m3.x, a.m3.y, a.m3.w}, + {a.m4.x, a.m4.y, a.m4.w}, + }; + mons_mat3 minor_1_4 = { + {a.m2.x, a.m2.y, a.m2.z}, + {a.m3.x, a.m3.y, a.m3.z}, + {a.m4.x, a.m4.y, a.m4.z}, + }; + + return (a.m1.x * mons_mat3_determinant(minor_1_1)) - + (a.m1.y * mons_mat3_determinant(minor_1_2)) + + (a.m1.z * mons_mat3_determinant(minor_1_3)) - + (a.m1.w * mons_mat3_determinant(minor_1_4)); +} + +mons_mat4 mons_mat4_minor(mons_mat4 a) { + // First Row + mons_mat3 minor_1_1 = { + {a.m2.y, a.m2.z, a.m2.w}, + {a.m3.y, a.m3.z, a.m3.w}, + {a.m4.y, a.m4.z, a.m4.w}, + }; + float det_1_1 = mons_mat3_determinant(minor_1_1); + mons_mat3 minor_1_2 = { + {a.m2.x, a.m2.z, a.m2.w}, + {a.m3.x, a.m3.z, a.m3.w}, + {a.m4.x, a.m4.z, a.m4.w}, + }; + float det_1_2 = mons_mat3_determinant(minor_1_2); + mons_mat3 minor_1_3 = { + {a.m2.x, a.m2.y, a.m2.w}, + {a.m3.x, a.m3.y, a.m3.w}, + {a.m4.x, a.m4.y, a.m4.w}, + }; + float det_1_3 = mons_mat3_determinant(minor_1_3); + mons_mat3 minor_1_4 = { + {a.m2.x, a.m2.y, a.m2.z}, + {a.m3.x, a.m3.y, a.m3.z}, + {a.m4.x, a.m4.y, a.m4.z}, + }; + float det_1_4 = mons_mat3_determinant(minor_1_4); + // Second Row + mons_mat3 minor_2_1 = { + {a.m1.y, a.m1.z, a.m1.w}, + {a.m3.y, a.m3.z, a.m3.w}, + {a.m4.y, a.m4.z, a.m4.w}, + }; + float det_2_1 = mons_mat3_determinant(minor_2_1); + mons_mat3 minor_2_2 = { + {a.m1.x, a.m1.z, a.m1.w}, + {a.m3.x, a.m3.z, a.m3.w}, + {a.m4.x, a.m4.z, a.m4.w}, + }; + float det_2_2 = mons_mat3_determinant(minor_2_2); + mons_mat3 minor_2_3 = { + {a.m1.x, a.m1.y, a.m1.w}, + {a.m3.x, a.m3.y, a.m3.w}, + {a.m4.x, a.m4.y, a.m4.w}, + }; + float det_2_3 = mons_mat3_determinant(minor_2_3); + mons_mat3 minor_2_4 = { + {a.m1.x, a.m1.y, a.m1.z}, + {a.m3.x, a.m3.y, a.m3.z}, + {a.m4.x, a.m4.y, a.m4.z}, + }; + float det_2_4 = mons_mat3_determinant(minor_2_4); + // Third Row + mons_mat3 minor_3_1 = { + {a.m1.y, a.m1.z, a.m1.w}, + {a.m2.y, a.m2.z, a.m2.w}, + {a.m4.y, a.m4.z, a.m4.w}, + }; + float det_3_1 = mons_mat3_determinant(minor_3_1); + mons_mat3 minor_3_2 = { + {a.m1.x, a.m1.z, a.m1.w}, + {a.m2.x, a.m2.z, a.m2.w}, + {a.m4.x, a.m4.z, a.m4.w}, + }; + float det_3_2 = mons_mat3_determinant(minor_3_2); + mons_mat3 minor_3_3 = { + {a.m1.x, a.m1.y, a.m1.w}, + {a.m2.x, a.m2.y, a.m2.w}, + {a.m4.x, a.m4.y, a.m4.w}, + }; + float det_3_3 = mons_mat3_determinant(minor_3_3); + mons_mat3 minor_3_4 = { + {a.m1.x, a.m1.y, a.m1.z}, + {a.m2.x, a.m2.y, a.m2.z}, + {a.m4.x, a.m4.y, a.m4.z}, + }; + float det_3_4 = mons_mat3_determinant(minor_3_4); + // Fourth Row + mons_mat3 minor_4_1 = { + {a.m1.y, a.m1.z, a.m1.w}, + {a.m2.y, a.m2.z, a.m2.w}, + {a.m3.y, a.m3.z, a.m3.w}, + }; + float det_4_1 = mons_mat3_determinant(minor_4_1); + mons_mat3 minor_4_2 = { + {a.m1.x, a.m1.z, a.m1.w}, + {a.m2.x, a.m2.z, a.m2.w}, + {a.m3.x, a.m3.z, a.m3.w}, + }; + float det_4_2 = mons_mat3_determinant(minor_4_2); + mons_mat3 minor_4_3 = { + {a.m1.x, a.m1.y, a.m1.w}, + {a.m2.x, a.m2.y, a.m2.w}, + {a.m3.x, a.m3.y, a.m3.w}, + }; + float det_4_3 = mons_mat3_determinant(minor_4_3); + mons_mat3 minor_4_4 = { + {a.m1.x, a.m1.y, a.m1.z}, + {a.m2.x, a.m2.y, a.m2.z}, + {a.m3.x, a.m3.y, a.m3.z}, + }; + float det_4_4 = mons_mat3_determinant(minor_4_4); + + mons_mat4 result = { + {det_1_1, det_1_2, det_1_3, det_1_4}, + {det_2_1, det_2_2, det_2_3, det_2_4}, + {det_3_1, det_3_2, det_3_3, det_3_4}, + {det_4_1, det_4_2, det_4_3, det_4_4}, + }; + return result; +} + +mons_mat4 mons_mat4_cofactor(mons_mat4 a) { + mons_mat4 result = mons_mat4_minor(a); + result.m1.y *= -1; + result.m1.w *= -1; + result.m2.x *= -1; + result.m2.z *= -1; + result.m3.y *= -1; + result.m3.w *= -1; + result.m4.x *= -1; + result.m4.z *= -1; + return result; +} + +mons_mat4 mons_mat4_adjoint(mons_mat4 a) { + return mons_mat4_transpose(mons_mat4_cofactor(a)); +} + +mons_mat4 mons_mat4_inverse(mons_mat4 a) { + return mons_mat4_mul_f(mons_mat4_adjoint(a), + 1.0 / mons_mat4_determinant(a)); +} + +int mons_mat4_equal(mons_mat4 a, mons_mat4 b) { + return mons_vec4_equal(a.m1, b.m1) + && mons_vec4_equal(a.m2, b.m2) + && mons_vec4_equal(a.m3, b.m3) + && mons_vec4_equal(a.m4, b.m4); +} + +void mons_mat4_mul_inplace(mons_mat4 *a, mons_mat4 b) { + *a = mons_mat4_mul(*a, b); +} + +void mons_mat4_print(mons_mat4 mat) { + printf("[%.3f %.3f %.3f %.3f]\n", mat.m1.x, mat.m2.x, mat.m3.x, mat.m4.x); + printf("[%.3f %.3f %.3f %.3f]\n", mat.m1.y, mat.m2.y, mat.m3.y, mat.m4.y); + printf("[%.3f %.3f %.3f %.3f]\n", mat.m1.z, mat.m2.z, mat.m3.z, mat.m4.z); + printf("[%.3f %.3f %.3f %.3f]\n", mat.m1.w, mat.m2.w, mat.m3.w, mat.m4.w); +} diff --git a/mons_math/src/quat.c b/mons_math/src/quat.c new file mode 100644 index 0000000..7c6a1cd --- /dev/null +++ b/mons_math/src/quat.c @@ -0,0 +1,141 @@ +#include "mons_math/quat.h" +#include "mons_math/mat4.h" +#include "mons_math/vec3.h" +#include "mons_math/vec4.h" +#include +#include +#include + +mons_quat mons_quat_from_axis_angle(mons_vec3 axis, float angle) { + float half_angle = 0.5f * angle; + float s = sinf(half_angle); + return (mons_quat){ + axis.x * s, + axis.y * s, + axis.z * s, + cosf(half_angle), + }; +} + +mons_quat mons_quat_mul(mons_quat a, mons_quat b) { + return (mons_quat){ + (a.w * b.x) + (a.x * b.w) + (a.y * b.z) - (a.z * b.y), + (a.w * b.y) + (a.y * b.w) + (a.z * b.x) - (a.x * b.z), + (a.w * b.z) + (a.z * b.w) + (a.x * b.y) - (a.y * b.x), + (a.w * b.w) - (a.x * b.x) - (a.y * b.y) - (a.z * b.z), + }; +} + +void mons_quat_mul_inplace(mons_quat *a, mons_quat b) { + *a = mons_quat_mul(*a, b); +} + +mons_quat mons_quat_mul_f(mons_quat a, float b) { + return (mons_quat){ + a.x * b, + a.y * b, + a.z * b, + a.w * b, + }; +} + +void mons_quat_mul_f_inplace(mons_quat *a, float b) { + a->x *= b; + a->y *= b; + a->z *= b; + a->w *= b; +} + +mons_quat mons_quat_mul_i(mons_quat a, int b) { + return (mons_quat){ + a.x * b, + a.y * b, + a.z * b, + a.w * b, + }; +} + +void mons_quat_mul_i_inplace(mons_quat *a, int b) { + a->x *= b; + a->y *= b; + a->z *= b; + a->w *= b; +} + +mons_mat4 mons_quat_to_mat4(mons_quat a) { + return (mons_mat4){ + { + 1.0 - (2.0 * a.y * a.y) - (2.0 * a.z * a.z), + (2.0 * a.x * a.y) - (2.0 * a.w * a.z), + (2.0 * a.x * a.z) + (2.0 * a.w * a.y), + 0.0, + }, + { + (2.0 * a.x * a.y) + (2.0 * a.w * a.z), + 1.0 - (2.0 * a.x * a.x) - (2.0 * a.z * a.z), + (2.0 * a.y * a.z) - (2.0 * a.w * a.x), + 0.0, + }, + { + (2.0 * a.x * a.z) - (2.0 * a.w * a.y), + (2.0 * a.y * a.z) + (2.0 * a.w * a.x), + 1.0 - (2.0 * a.x * a.x) - (2.0 * a.y * a.y), + 0.0, + }, + { + 0.0, + 0.0, + 0.0, + 1.0, + }, + }; +} + +mons_quat mons_quat_normalize(mons_quat a) { + mons_vec4 normalized = mons_vec4_normalize(*((mons_vec4 *)&a)); + return *((mons_quat *)&normalized); +} + +void mons_quat_normalize_inplace(mons_quat *a) { + mons_vec4_normalize_inplace((mons_vec4 *)a); +} + +mons_vec3 mons_quat_transform_vec3(mons_quat quat, mons_vec3 vec) { + mons_vec3 quat_vec = (mons_vec3){quat.x, quat.y, quat.z}; + float s = quat.w; + + return mons_vec3_add( + mons_vec3_add( + mons_vec3_mul_f(quat_vec, mons_vec3_dot(quat_vec, vec) * 2.0f), + mons_vec3_mul_f(vec, s * s - mons_vec3_dot(quat_vec, quat_vec))), + mons_vec3_mul_f(mons_vec3_cross(quat_vec, vec), 2.0f * s)); +} + +mons_quat mons_quat_conjugate(mons_quat quat) { + return (mons_quat){ + .x = -quat.x, + .y = -quat.y, + .z = -quat.z, + .w = quat.w, + }; +} + +mons_quat mons_quat_looking_at(mons_vec3 eye, mons_vec3 target, mons_vec3 up) { + mons_vec3 f = mons_vec3_normalize(mons_vec3_sub(target, eye)); + mons_vec3 side = mons_vec3_normalize(mons_vec3_cross(up, f)); + mons_vec3 b = mons_vec3_normalize(mons_vec3_add(f, MONS_VEC3_NEG_Z)); + mons_quat p = mons_quat_from_axis_angle(mons_vec3_cross(b, f), mons_vec3_dot(b,f)); + mons_vec3 r = (mons_vec3) { + p.w*p.w + p.x*p.x - p.y*p.y - p.z*p.z, + (2.0f * p.x * p.y) - (2.0f * p.w * p.z), + (2.0f * p.x * p.z) + (2.0f * p.w * p.y), + }; + b = mons_vec3_normalize(mons_vec3_add(side, r)); + mons_quat q = mons_quat_from_axis_angle(mons_vec3_cross(b,side), mons_vec3_dot(b, side)); + + return mons_quat_mul(p, q); +} + +void mons_quat_print(mons_quat quat) { + mons_vec4_print(*(mons_vec4*)&quat); +} diff --git a/mons_math/src/util.c b/mons_math/src/util.c new file mode 100644 index 0000000..f0f3ec0 --- /dev/null +++ b/mons_math/src/util.c @@ -0,0 +1,7 @@ +#include "mons_math/util.h" +#include + +int mons_float_approx_equal(float a, float b) { + return fabs(a - b) < MONS_FLOAT_EQUAL_EPSILON; +} + diff --git a/mons_math/src/vec2.c b/mons_math/src/vec2.c new file mode 100644 index 0000000..590397d --- /dev/null +++ b/mons_math/src/vec2.c @@ -0,0 +1,120 @@ +#include "mons_math/vec2.h" +#include "mons_math/util.h" +#include "mons_math/vec3.h" +#include + +mons_vec2 mons_vec2_add(mons_vec2 a, mons_vec2 b) { + mons_vec2 result = { + a.x + b.x, + a.y + b.y, + }; + return result; +} + +void mons_vec2_add_inplace(mons_vec2 *a, mons_vec2 b) { + a->x += b.x; + a->y += b.y; +} + +mons_vec2 mons_vec2_sub(mons_vec2 a, mons_vec2 b) { + mons_vec2 result = { + a.x - b.x, + a.y - b.y, + }; + return result; +} + +void mons_vec2_sub_inplace(mons_vec2 *a, mons_vec2 b) { + a->x -= b.x; + a->y -= b.y; +} + +mons_vec2 mons_vec2_mul_f(mons_vec2 a, float b) { + mons_vec2 result = { + a.x * b, + a.y * b, + }; + return result; +} + +mons_vec2 mons_vec2_mul_i(mons_vec2 a, int b) { + mons_vec2 result = { + a.x * b, + a.y * b, + }; + return result; +} + +float mons_vec2_dot(mons_vec2 a, mons_vec2 b) { + return (a.x * b.x) + (a.y * b.y); +} + +void mons_vec2_mul_f_inplace(mons_vec2 *a, float b) { + a->x *= b; + a->y *= b; +} + +void mons_vec2_mul_i_inplace(mons_vec2 *a, int b) { + a->x *= b; + a->y *= b; +} + +float mons_vec2_len(mons_vec2 a) { return sqrtf(mons_vec2_len_squared(a)); } + +float mons_vec2_len_squared(mons_vec2 a) { return (a.x * a.x) + (a.y * a.y); } + +mons_vec3 mons_vec2_extend(mons_vec2 a) { + mons_vec3 result = { + a.x, + a.y, + 0.0, + }; + return result; +} + +int mons_vec2_equal(mons_vec2 a, mons_vec2 b) { + return mons_float_approx_equal(a.x, b.x) && + mons_float_approx_equal(a.y, b.y); +} + +mons_vec2 mons_vec2_div_f(mons_vec2 a, float b) { + return (mons_vec2) { + a.x / b, + a.y / b, + }; +} + +mons_vec2 mons_vec2_div_i(mons_vec2 a, int b) { + return (mons_vec2) { + a.x / b, + a.y / b, + }; +} + +void mons_vec2_div_f_inplace(mons_vec2 *a, float b) { + a->x /= b; + a->y /= b; +} + +void mons_vec2_div_i_inplace(mons_vec2 *a, int b) { + a->x /= b; + a->y /= b; +} + +mons_vec2 mons_vec2_negate(mons_vec2 a) { + return mons_vec2_mul_i(a, -1); +} + +void mons_vec2_negate_inplace(mons_vec2 *a) { + mons_vec2_mul_i_inplace(a, -1); +} + +mons_vec2 mons_vec2_normalize(mons_vec2 a) { + float len = mons_vec2_len(a); + return mons_vec2_div_f(a, len); +} + +void mons_vec2_normalize_inplace(mons_vec2 *a) { + float len = mons_vec2_len(*a); + mons_vec2_div_f_inplace(a, len); +} diff --git a/mons_math/src/vec3.c b/mons_math/src/vec3.c new file mode 100644 index 0000000..725f5b3 --- /dev/null +++ b/mons_math/src/vec3.c @@ -0,0 +1,175 @@ +#include "mons_math/vec3.h" +#include "mons_math/vec2.h" +#include "mons_math/vec4.h" +#include "mons_math/util.h" +#include +#include +#include +#include + +mons_vec3 mons_vec3_add(mons_vec3 a, mons_vec3 b) { + mons_vec3 result = { + a.x + b.x, + a.y + b.y, + a.z + b.z, + }; + return result; +} + +void mons_vec3_add_inplace(mons_vec3 *a, mons_vec3 b) { + a->x += b.x; + a->y += b.y; + a->z += b.z; +} + +mons_vec3 mons_vec3_sub(mons_vec3 a, mons_vec3 b) { + mons_vec3 result = { + a.x - b.x, + a.y - b.y, + a.z - b.z, + }; + return result; +} + +void mons_vec3_sub_inplace(mons_vec3 *a, mons_vec3 b) { + a->x -= b.x; + a->y -= b.y; + a->z -= b.z; +} + +mons_vec3 mons_vec3_mul_f(mons_vec3 a, float b) { + mons_vec3 result = { + a.x * b, + a.y * b, + a.z * b, + }; + return result; +} + +mons_vec3 mons_vec3_mul_i(mons_vec3 a, int b) { + mons_vec3 result = { + a.x * b, + a.y * b, + a.z * b, + }; + return result; +} + +float mons_vec3_dot(mons_vec3 a, mons_vec3 b) { + return (a.x * b.x) + (a.y * b.y) + (a.z * b.z); +} + +void mons_vec3_mul_f_inplace(mons_vec3 *a, float b) { + a->x *= b; + a->y *= b; + a->z *= b; +} + +void mons_vec3_mul_i_inplace(mons_vec3 *a, int b) { + a->x *= b; + a->y *= b; + a->z *= b; +} + +float mons_vec3_len(mons_vec3 a) { return sqrtf(mons_vec3_len_squared(a)); } + +float mons_vec3_len_squared(mons_vec3 a) { + return (a.x * a.x) + (a.y * a.y) + (a.z * a.z); +} + +mons_vec4 mons_vec3_extend(mons_vec3 a) { + mons_vec4 result = { + a.x, + a.y, + a.z, + 0.0, + }; + return result; +} + +mons_vec2 mons_vec3_truncate(mons_vec3 a) { + mons_vec2 result = { + a.x, + a.y, + }; + return result; +} + +int mons_vec3_equal(mons_vec3 a, mons_vec3 b) { + return mons_float_approx_equal(a.x, b.x) + && mons_float_approx_equal(a.y, b.y) + && mons_float_approx_equal(a.z, b.z); +} + +mons_vec3 mons_vec3_div_f(mons_vec3 a, float b) { + return (mons_vec3) { + a.x / b, + a.y / b, + a.z / b, + }; +} + +void mons_vec3_div_f_inplace(mons_vec3 *a, float b) { + a->x /= b; + a->y /= b; + a->z /= b; +} + +mons_vec3 mons_vec3_div_i(mons_vec3 a, int b) { + return (mons_vec3) { + a.x / b, + a.y / b, + a.z / b, + }; +} + +void mons_vec3_div_i_inplace(mons_vec3 *a, int b) { + a->x /= b; + a->y /= b; + a->z /= b; +} + +mons_vec3 mons_vec3_negate(mons_vec3 a) { + return mons_vec3_mul_i(a, -1); +} + +void mons_vec3_negate_inplace(mons_vec3 *a) { + return mons_vec3_mul_i_inplace(a, -1); +} + +mons_vec3 mons_vec3_normalize(mons_vec3 a) { + float length = mons_vec3_len(a); + return mons_vec3_div_f(a, length); +} + +void mons_vec3_normalize_inplace(mons_vec3 *a) { + float length = mons_vec3_len(*a); + mons_vec3_div_f_inplace(a, length); +} + +mons_vec3 mons_vec3_cross(mons_vec3 a, mons_vec3 b) { + return (mons_vec3) { + (a.y * b.z) - (a.z * b.y), + (a.z * b.x) - (a.x * b.z), + (a.x * b.y) - (a.y * b.x), + }; +} + +mons_vec3 mons_vec3_mul_memberwise(mons_vec3 a, mons_vec3 b) { + return (mons_vec3) { + a.x * b.x, + a.y * b.y, + a.z * b.z, + }; +} + +void mons_vec3_mul_memberwise_inplace(mons_vec3 *a, mons_vec3 b) { + a->x *= b.x; + a->y *= b.y; + a->z *= b.z; +} + +void mons_vec3_print(mons_vec3 vec) { + printf("[%f, %f, %f]", vec.x, vec.y, vec.z); +} + diff --git a/mons_math/src/vec4.c b/mons_math/src/vec4.c new file mode 100644 index 0000000..d4474ee --- /dev/null +++ b/mons_math/src/vec4.c @@ -0,0 +1,149 @@ +#include "mons_math/vec4.h" +#include "mons_math/vec3.h" +#include "mons_math/util.h" +#include +#include + +mons_vec4 mons_vec4_add(mons_vec4 a, mons_vec4 b) { + mons_vec4 result = { + a.x + b.x, + a.y + b.y, + a.z + b.z, + a.w + b.w, + }; + return result; +} + +void mons_vec4_add_inplace(mons_vec4 *a, mons_vec4 b) { + a->x += b.x; + a->y += b.y; + a->z += b.z; + a->w += b.w; +} + +mons_vec4 mons_vec4_sub(mons_vec4 a, mons_vec4 b) { + mons_vec4 result = { + a.x - b.x, + a.y - b.y, + a.z - b.z, + a.w - b.w, + }; + return result; +} + +void mons_vec4_sub_inplace(mons_vec4 *a, mons_vec4 b) { + a->x -= b.x; + a->y -= b.y; + a->z -= b.z; + a->w -= b.w; +} + +mons_vec4 mons_vec4_mul_f(mons_vec4 a, float b) { + mons_vec4 result = { + a.x * b, + a.y * b, + a.z * b, + a.w * b, + }; + return result; +} + +mons_vec4 mons_vec4_mul_i(mons_vec4 a, int b) { + mons_vec4 result = { + a.x * b, + a.y * b, + a.z * b, + a.w * b, + }; + return result; +} + +void mons_vec4_mul_f_inplace(mons_vec4 *a, float b) { + a->x *= b; + a->y *= b; + a->z *= b; + a->w *= b; +} + +void mons_vec4_mul_i_inplace(mons_vec4 *a, int b) { + a->x *= b; + a->y *= b; + a->z *= b; + a->w *= b; +} + +mons_vec4 mons_vec4_div_f(mons_vec4 a, float b) { + return (mons_vec4) { + a.x / b, + a.y / b, + a.z / b, + a.w / b, + }; +} + +mons_vec4 mons_vec4_div_i(mons_vec4 a, int b) { + return (mons_vec4) { + a.x / b, + a.y / b, + a.z / b, + a.w / b, + }; +} + +void mons_vec4_div_f_inplace(mons_vec4 *a, float b) { + a->x /= b; + a->y /= b; + a->z /= b; + a->w /= b; +} + +void mons_vec4_div_i_inplace(mons_vec4 *a, int b) { + a->x /= b; + a->y /= b; + a->z /= b; + a->w /= b; +} + +float mons_vec4_dot(mons_vec4 a, mons_vec4 b) { + return (a.x * b.x) + (a.y * b.y) + (a.z * b.z) + (a.w * b.w); +} + +float mons_vec4_len(mons_vec4 a) { return sqrt(mons_vec4_len_squared(a)); } + +float mons_vec4_len_squared(mons_vec4 a) { + return (a.x * a.x) + (a.y * a.y) + (a.z * a.z) + (a.w * a.w); +} + +mons_vec3 mons_vec4_truncate(mons_vec4 a) { + mons_vec3 result = {a.x, a.y, a.z}; + return result; +} + +int mons_vec4_equal(mons_vec4 a, mons_vec4 b) { + return mons_float_approx_equal(a.x, b.x) + && mons_float_approx_equal(a.y, b.y) + && mons_float_approx_equal(a.z, b.z) + && mons_float_approx_equal(a.w, b.w); +} + +mons_vec4 mons_vec4_negate(mons_vec4 a) { + return mons_vec4_mul_i(a, -1); +} + +void mons_vec4_negate_inplace(mons_vec4 *a) { + mons_vec4_mul_i_inplace(a, -1); +} + +mons_vec4 mons_vec4_normalize(mons_vec4 a) { + float len = mons_vec4_len(a); + return mons_vec4_div_f(a, len); +} + +void mons_vec4_normalize_inplace(mons_vec4 *a) { + float len = mons_vec4_len(*a); + mons_vec4_div_f_inplace(a, len); +} + +void mons_vec4_print(mons_vec4 vec) { + printf("[%f, %f, %f, %f]", vec.x, vec.y, vec.z, vec.w); +} -- cgit v1.2.3