#ifndef MATHPRIMITIVES_H #define MATHPRIMITIVES_H #include #include #include struct Vec3 { double x; double y; double z; }; static inline struct Vec3 vec3_add(struct Vec3 a, struct Vec3 b) { return (struct Vec3){a.x + b.x, a.y + b.y, a.z + b.z}; } static inline struct Vec3 vec3_sub(struct Vec3 a, struct Vec3 b) { return (struct Vec3){a.x - b.x, a.y - b.y, a.z - b.z}; } static inline double vec3_dot(struct Vec3 a, struct Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; } static inline struct Vec3 vec3_scale(struct Vec3 a, double b) { return (struct Vec3){.x = a.x * b, .y = a.y * b, .z = a.z * b}; } static inline struct Vec3 vec3_cross(struct Vec3 a, struct Vec3 b) { double x = a.y * b.z - a.z * b.y; double y = a.z * b.x - a.x * b.z; double z = a.x * b.y - a.y * b.x; return (struct Vec3){x, y, z}; } static inline int vec3_eql_tol(struct Vec3 a, struct Vec3 b, double tol) { return fabs(a.x - b.x) < tol && fabs(a.y - b.y) < tol && fabs(a.z - b.z) < tol; } struct Mat3 { struct Vec3 r1; struct Vec3 r2; struct Vec3 r3; }; 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}}; } static inline int mat3_eql_tol(struct Mat3 a, struct Mat3 b, double tol) { return vec3_eql_tol(a.r1, b.r1, tol) && vec3_eql_tol(a.r2, b.r2, tol) && vec3_eql_tol(a.r3, b.r3, tol); } static inline struct Mat3 mat3_add(struct Mat3 a, struct Mat3 b) { struct Vec3 r1 = vec3_add(a.r1, b.r1); struct Vec3 r2 = vec3_add(a.r2, b.r2); struct Vec3 r3 = vec3_add(a.r3, b.r3); 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); } static inline 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}; } static inline struct Vec3 mat3_mul_vec(struct Mat3 a, struct Vec3 b) { return (struct Vec3){ vec3_dot(a.r1, b), vec3_dot(a.r2, b), vec3_dot(a.r3, 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