Small refactor and add some more basic functions

This commit is contained in:
Alex Selimov 2026-10-25 22:56:57 -04:00
parent 519fbd131a
commit 55ba9f6cde
2 changed files with 33 additions and 16 deletions

View file

@ -11,19 +11,11 @@ struct Vec3 {
};
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;
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) {
struct Vec3 c;
c.x = a.x - b.x;
c.y = a.y - b.y;
c.z = a.z - b.z;
return c;
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) {
@ -31,16 +23,14 @@ static inline double vec3_dot(struct Vec3 a, struct Vec3 b) {
}
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;
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;
struct Vec3 c = {x, y, z};
return c;
return (struct Vec3){x, y, z};
}
static inline int vec3_eql_tol(struct Vec3 a, struct Vec3 b, double tol) {
@ -55,9 +45,21 @@ struct Mat3 {
};
static inline struct Mat3 mat3_identity() {
struct Mat3 i = {
return (struct Mat3){
.r1 = {1.0, 0.0, 0.0}, .r2 = {0.0, 1.0, 0.0}, .r3 = {0.0, 0.0, 1.0}};
return i;
}
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};
}
#endif