c_projects/mathprimitives/mathprimitives.h

136 lines
3.7 KiB
C

#ifndef MATHPRIMITIVES_H
#define MATHPRIMITIVES_H
#include <math.h>
#include <xmmintrin.h>
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() {
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 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),
};
}
#endif