c_projects/mathprimitives/mathprimitives.h

64 lines
1.3 KiB
C
Raw Normal View History

#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) {
struct Vec3 c;
c.x = a.x + b.x;
c.y = a.y + b.y;
c.z = a.z + b.z;
return c;
}
static inline struct Vec3 vec3_sub(struct Vec3 a, struct Vec3 b) {
struct Vec3 c;
c.x = a.x - b.x;
c.y = a.y - b.y;
c.z = a.z - b.z;
return c;
}
static inline double vec3_dot(struct Vec3 a, struct Vec3 b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
2026-10-23 23:01:53 -04:00
static inline struct Vec3 vec3_scale(struct Vec3 a, double b) {
struct Vec3 c = {.x = a.x * b, .y = a.y * b, .z = a.z * b};
return c;
}
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;
struct Vec3 c = {x, y, z};
return c;
}
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;
}
2026-10-23 23:01:53 -04:00
struct Mat3 {
struct Vec3 r1;
struct Vec3 r2;
struct Vec3 r3;
};
static inline struct Mat3 mat3_identity() {
struct Mat3 i = {
.r1 = {1.0, 0.0, 0.0}, .r2 = {0.0, 1.0, 0.0}, .r3 = {0.0, 0.0, 1.0}};
return i;
}
#endif