diff --git a/mathprimitives/Makefile b/mathprimitives/Makefile index 0d930d0..391df0a 100644 --- a/mathprimitives/Makefile +++ b/mathprimitives/Makefile @@ -1,4 +1,4 @@ -CC=gcc +CC=clang DEBUG=yes EXEC=test @@ -12,7 +12,7 @@ SRC = $(wildcard *.c) # -Wall to get all the possible warnings # -ansi -pendantic to get portable code ifeq ($(DEBUG),yes) - CFLAGS=-Wall -Wpedantic -g3 -std=c23 -Wextra -Wconversion -Wshadow -fsanitize=address,undefined -fno-omit-frame-pointer -fno-optimize-sibling-calls + CFLAGS=-Wall -Wpedantic -g3 -Wextra -Wconversion -Wshadow -fsanitize=address,undefined -fno-omit-frame-pointer -fno-optimize-sibling-calls LDFLAGS= -Wall -lm -fsanitize=address,undefined else CFLAGS=-W -Wall -pedantic -std=c23 -O3 diff --git a/mathprimitives/mathprimitives.h b/mathprimitives/mathprimitives.h index 633e52f..ac66689 100644 --- a/mathprimitives/mathprimitives.h +++ b/mathprimitives/mathprimitives.h @@ -2,6 +2,7 @@ #define MATHPRIMITIVES_H #include +#include #include struct Vec3 { @@ -44,7 +45,7 @@ struct Mat3 { struct Vec3 r3; }; -static inline struct Mat3 mat3_identity() { +static inline struct Mat3 mat3_identity(void) { return (struct Mat3){ .r1 = {1.0, 0.0, 0.0}, .r2 = {0.0, 1.0, 0.0}, .r3 = {0.0, 0.0, 1.0}}; } @@ -133,4 +134,98 @@ static inline struct Vec3 mat3_mul_vec(struct Mat3 a, struct Vec3 b) { }; } +struct Quat { + double x; + double y; + double z; + double w; +}; + +static inline int quat_eql_tol(struct Quat a, struct Quat b, double tol) { + return fabs(a.x - b.x) < tol && fabs(a.y - b.y) < tol && + fabs(a.z - b.z) < tol && fabs(a.w - b.w) < tol; +} + +static inline double mag_quat(struct Quat q) { + return sqrt(q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w); +} + +static inline struct Quat quat_normalized(struct Quat q) { + double inv_mag = 1.0 / mag_quat(q); + + return (struct Quat){ + q.x * inv_mag, + q.y * inv_mag, + q.z * inv_mag, + q.w * inv_mag, + }; +} + +static inline struct Quat quat_conjugate(struct Quat q) { + return (struct Quat){ + -q.x, + -q.y, + -q.z, + q.w, + }; +} + +static inline struct Quat yaw_pitch_roll_to_quat(double yaw_radians, + double pitch_radians, + double roll_radians) { + double cos_roll = cos(roll_radians / 2.0); + double sin_roll = sin(roll_radians / 2.0); + double cos_pitch = cos(pitch_radians / 2.0); + double sin_pitch = sin(pitch_radians / 2.0); + double cos_yaw = cos(yaw_radians / 2.0); + double sin_yaw = sin(yaw_radians / 2.0); + + struct Quat q = { + sin_roll * cos_pitch * cos_yaw - cos_roll * sin_pitch * sin_yaw, + cos_roll * sin_pitch * cos_yaw + sin_roll * cos_pitch * sin_yaw, + cos_roll * cos_pitch * sin_yaw - sin_roll * sin_pitch * cos_yaw, + cos_roll * cos_pitch * cos_yaw + sin_roll * sin_pitch * sin_yaw, + }; + + return quat_normalized(q); +} + +static inline struct Quat quat_mul(struct Quat a, struct Quat b) { + return (struct Quat){a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y, + a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x, + a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w, + a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z}; +} + +static inline struct Vec3 quat_apply(struct Quat a, struct Vec3 b) { + struct Quat b_as_quat = {b.x, b.y, b.z, 0.0}; + + struct Quat rotated_quat = + quat_mul(quat_mul(a, b_as_quat), quat_conjugate(a)); + + return (struct Vec3){ + rotated_quat.x, + rotated_quat.y, + rotated_quat.z, + }; +} + +static inline struct Quat quat_scale(struct Quat q, double b) { + return (struct Quat){ + q.x * b, + q.y * b, + q.z * b, + q.w * b, + }; +} + +static inline struct Quat quat_add(struct Quat a, struct Quat b) { + return (struct Quat){ + a.x + b.x, + a.y + b.y, + a.z + b.z, + a.w + b.w, + }; +} + #endif diff --git a/mathprimitives/test.c b/mathprimitives/test.c index a906ad7..2413501 100644 --- a/mathprimitives/test.c +++ b/mathprimitives/test.c @@ -16,14 +16,14 @@ printf("\033[91m[FAILED] %s\n\033[0m", STR(test)); \ } -int test_vec3_equal_tol() { +int test_vec3_equal_tol(void) { struct Vec3 a = {.x = 1.0, .y = 2.0, .z = 3.0}; struct Vec3 b = {.x = 1.0, .y = 2.0, .z = 3.0}; return vec3_eql_tol(a, b, 1e-12); } -int test_vec3_add() { +int test_vec3_add(void) { struct Vec3 a = {.x = 1.0, .y = 2.0, .z = 3.0}; struct Vec3 b = {.x = 1.0, .y = 2.0, .z = 3.0}; @@ -34,7 +34,7 @@ int test_vec3_add() { return vec3_eql_tol(expected_c, real_c, 1e-12); } -int test_vec3_sub() { +int test_vec3_sub(void) { struct Vec3 a = {.x = 1.0, .y = 2.0, .z = 3.0}; struct Vec3 b = {.x = 1.0, .y = 2.0, .z = 3.0}; @@ -45,7 +45,7 @@ int test_vec3_sub() { return vec3_eql_tol(expected_c, real_c, 1e-12); } -int test_vec3_dot() { +int test_vec3_dot(void) { struct Vec3 a = {.x = 1.0, .y = 2.0, .z = 3.0}; struct Vec3 b = {.x = 1.0, .y = 2.0, .z = 3.0}; double expected_dot = 14.0; @@ -55,7 +55,7 @@ int test_vec3_dot() { return fabs(real_dot - expected_dot) < 1e-12; } -int test_vec3_scale() { +int test_vec3_scale(void) { struct Vec3 a = {.x = 1.0, .y = 2.0, .z = 3.0}; double b = 3.0; struct Vec3 expected_c = {.x = 3.0, .y = 6.0, .z = 9.0}; @@ -65,7 +65,7 @@ int test_vec3_scale() { return vec3_eql_tol(expected_c, real_c, 1e-12); } -int test_vec3_cross() { +int test_vec3_cross(void) { struct Vec3 a = {.x = 1.0, .y = 2.0, .z = 3.0}; struct Vec3 b = {.x = 3.0, .y = 1.0, .z = 0.0}; struct Vec3 expected_c = {.x = -3.0, .y = 9.0, .z = -5.0}; @@ -75,7 +75,7 @@ int test_vec3_cross() { return vec3_eql_tol(expected_c, real_c, 1e-12); } -int test_mat3_identity() { +int test_mat3_identity(void) { struct Mat3 i = mat3_identity(); return fabs(i.r1.x - 1.0) < 1e-12 && fabs(i.r1.y - 0.0) < 1e-12 && fabs(i.r1.z - 0.0) < 1e-12 && fabs(i.r2.x - 0.0) < 1e-12 && @@ -84,13 +84,13 @@ int test_mat3_identity() { fabs(i.r3.z - 1.0) < 1e-12; } -int test_mat3_eql() { +int test_mat3_eql(void) { struct Mat3 a = mat3_identity(); struct Mat3 b = mat3_identity(); return mat3_eql_tol(a, b, 1e+12); } -int test_mat3_add() { +int test_mat3_add(void) { struct Mat3 a = {{1, 2, 3}, {0, 1, 5}, {0, 0, 0}}; struct Mat3 b = {{0, 1, 0}, {0, 1, 1}, {0, 0, 2}}; @@ -98,7 +98,7 @@ int test_mat3_add() { return mat3_eql_tol(expected_c, mat3_add(a, b), 1e-12); } -int test_mat3_sub() { +int test_mat3_sub(void) { struct Mat3 a = {{1, 2, 3}, {0, 1, 5}, {0, 0, 0}}; struct Mat3 b = {{0, 1, 0}, {0, 1, 1}, {0, 0, 2}}; @@ -106,7 +106,7 @@ int test_mat3_sub() { return mat3_eql_tol(expected_c, mat3_sub(a, b), 1e-12); } -int test_mat3_mul() { +int test_mat3_mul(void) { struct Mat3 a = {{1, 2, 3}, {0, 1, 5}, {0, 0, 9}}; struct Mat3 b = {{0, 1, 0}, {0, 1, 1}, {0, 0, 1}}; @@ -114,13 +114,13 @@ int test_mat3_mul() { return mat3_eql_tol(expected_c, mat3_mul(a, b), 1e-12); } -int test_mat3_det() { +int test_mat3_det(void) { 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() { +int test_mat3_inv(void) { struct Mat3 a = { {1.0, 2.0, 3.0}, {4.0, 1.0, 5.0}, @@ -135,7 +135,7 @@ int test_mat3_inv() { return mat3_eql_tol(inv_a, expected_inv, 1e-11); } -int test_mat3_mul_vec3() { +int test_mat3_mul_vec3(void) { struct Mat3 a = { {0.0, 1.0, 0.0}, {0.0, 1.0, 1.0}, @@ -148,7 +148,95 @@ int test_mat3_mul_vec3() { return vec3_eql_tol(expected_c, mat3_mul_vec(a, b), 1e-12); } -int main() { +int test_quat_mag(void) { + struct Quat q = {1.0, 2.0, 3.0, 4.0}; + return fabs(mag_quat(q) - sqrt(30.0)) < 1e-12; +} + +int test_quat_eql(void) { + struct Quat q1 = {1.0, 2.0, 3.0, 4.0}; + struct Quat q2 = {1.0, 2.0, 3.0, 4.0}; + + return quat_eql_tol(q1, q2, 1e-12); +} +int test_quat_normalized(void) { + struct Quat q = {1.0, 2.0, 3.0, 4.0}; + struct Quat q_normalized = quat_normalized(q); + struct Quat q_normalized_expected = { + 1.0 / sqrt(30.0), + 2.0 / sqrt(30.0), + 3.0 / sqrt(30.0), + 4.0 / sqrt(30.0), + }; + + return quat_eql_tol(q_normalized_expected, q_normalized, 1e-12); +} + +int test_quat_conjugate(void) { + struct Quat q = {1.0, 2.0, 3.0, 4.0}; + struct Quat q_conjugate = {-1.0, -2.0, -3.0, 4.0}; + + return quat_eql_tol(quat_conjugate(q), q_conjugate, 1e-12); +} + +int test_yaw_pitch_roll_to_quat(void) { + struct Quat quat = yaw_pitch_roll_to_quat(0.5, 0.5, 1); + + return fabs(quat.x - 0.3963648) < 1e-6 && fabs(quat.y - 0.3252922) < 1e-6 && + fabs(quat.z - 0.0954433) < 1e-6 && fabs(quat.w - 0.8532119) < 1e-6; +} + +int test_quat_mul(void) { + struct Quat a = {2.0, 3.0, 4.0, 1.0}; + struct Quat b = {0.0, 1.0, 0.0, 1.0}; + + struct Quat c = quat_mul(a, b); + + return fabs(c.w + 2.0) < 1e-12 && fabs(c.x + 2.0) < 1e-12 && + fabs(c.y - 4.0) < 1e-12 && fabs(c.z - 6.0) < 1e-12; +} + +int test_quat_apply(void) { + struct Quat q = yaw_pitch_roll_to_quat(M_PI_2, 0.0, 0.0); + + if (fabs(q.x) > 1e-12) + return 0; + if (fabs(q.y) > 1e-12) + return 0; + if (fabs(q.z - M_PI_4) < 1e-12) + return 0; + if (fabs(q.w - M_PI_4) < 1e-12) + return 0; + + struct Vec3 v = {1.0, 2.0, 1.0}; + + struct Vec3 rotated_vec = quat_apply(q, v); + + return fabs(rotated_vec.x + 2.0) < 1e-12 && + fabs(rotated_vec.y - 1.0) < 1e-12 && fabs(rotated_vec.z - 1.0) < 1e-12; +} + +int test_quat_scale(void) { + + struct Quat q = {1.0, 2.0, 3.0, 4.0}; + struct Quat expected_scaled_q = {2.0, 4.0, 6.0, 8.0}; + + struct Quat scaled_q = quat_scale(q, 2.0); + + return quat_eql_tol(expected_scaled_q, scaled_q, 1e-12); +} + +int test_quat_add(void) { + struct Quat a = {1.0, 2.0, 3.0, 4.0}; + struct Quat b = {0.0, 1.0, 0.0, 1.0}; + + struct Quat expected_c = {1.0, 3.0, 3.0, 5.0}; + struct Quat c = quat_add(a, b); + + return quat_eql_tol(expected_c, c, 1e-12); +} + +int main(void) { int tests_total = 0; int tests_passed = 0; @@ -166,6 +254,15 @@ int main() { run_test(test_mat3_det); run_test(test_mat3_inv); run_test(test_mat3_mul_vec3); + run_test(test_quat_mag); + run_test(test_quat_eql); + run_test(test_quat_normalized); + run_test(test_quat_conjugate); + run_test(test_yaw_pitch_roll_to_quat); + run_test(test_quat_mul); + run_test(test_quat_apply); + run_test(test_quat_scale); + run_test(test_quat_add); if (tests_total == tests_passed) { printf("\033[92mAll tests passed!\033[0m");