From a9aa97be63e2b2e3541d685a314b1f9abb2f201d Mon Sep 17 00:00:00 2001 From: Gregory Wells Date: Sun, 3 Aug 2025 14:39:56 -0400 Subject: [PATCH] move some stuff to C files --- CMakeLists.txt | 5 +++ utils/math/gryphn_vec3.c | 36 ++++++++++++++++++++ utils/math/gryphn_vec3.h | 72 ++++------------------------------------ 3 files changed, 48 insertions(+), 65 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 utils/math/gryphn_vec3.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..dc74a8e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,5 @@ +set(CMAKE_EXPORT_COMPILE_COMMANDS on) +project(GryphnUtils) + +file(GLOB_RECURSE SOURCE_FILES CONFIGURE_DEPENDS "utils/*.c") +add_library(GryphnUtils STATIC ${SOURCE_FILES}) diff --git a/utils/math/gryphn_vec3.c b/utils/math/gryphn_vec3.c new file mode 100644 index 0000000..585e7f1 --- /dev/null +++ b/utils/math/gryphn_vec3.c @@ -0,0 +1,36 @@ +#include "gryphn_vec3.h" +#include "math.h" + +gnVec3 gnVec3Subtract(gnVec3 a, gnVec3 b) { return (gnVec3){ + .x = a.x - b.x, + .y = a.y - b.y, + .z = a.z - b.z }; +} +gnVec3 gnVec3Add(gnVec3 a, gnVec3 b) { return (gnVec3){ + .x = a.x + b.x, + .b = a.y + b.y, + .c = a.z + b.z }; +} +gnVec3 gnVec3Normalize(gnVec3 in) { + float lengthOfVector = sqrt((in.x * in.x) + (in.y * in.y) + (in.z * in.z)); + return (gnVec3){ + .a = in.x / lengthOfVector, + .b = in.y / lengthOfVector, + .c = in.z / lengthOfVector}; +} +gnVec3 gnVec3Cross(gnVec3 a, gnVec3 b) { return (gnVec3){ + .x = a.y * b.z - a.z * b.y, + .y = a.z * b.x - a.x * b.z, + .z = a.x * b.y - a.y * b.x}; +} +float gnVec3Dot(gnVec3 a, gnVec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; } +gnVec3 gnVec3Multiply(gnVec3 a, gnVec3 b) { return (gnVec3){ + .x = a.x * b.x, + .y = a.y * b.y, + .z = a.z * b.z }; +} +gnVec3 gnVec3MultiplyBy(gnVec3 a, float b) { return (gnVec3){ + .x = a.x * b, + .y= a.y * b, + .z = a.z * b }; +} diff --git a/utils/math/gryphn_vec3.h b/utils/math/gryphn_vec3.h index 38443d7..32f8d28 100644 --- a/utils/math/gryphn_vec3.h +++ b/utils/math/gryphn_vec3.h @@ -1,42 +1,20 @@ #pragma once #include "stdint.h" -#include "math.h" typedef struct gnVec3 { union { struct { float a, b, c; }; struct { float x, y, z; }; }; - -#ifdef GN_UTILS_CPP - gnVec3(float x, float y, float z) { this->x = x; this->y = y; this->z = z; } - gnVec3(float s) { this->x = s; this->y = s; this->z = s; } - gnVec3() {}; - - gnVec3 operator-(const gnVec3& other) { - gnVec3 returnGnVec3; - returnGnVec3.x = this->x - other.x; - returnGnVec3.y = this->y - other.y; - returnGnVec3.z = this->z - other.z; - return returnGnVec3; - } - - bool operator==(const gnVec3& other) const { - return this->a == other.a && this->b == other.b && this->c == other.c; - } -#endif } gnVec3; -static const inline gnVec3 gnVec3Subtract(gnVec3 a, gnVec3 b) { return (gnVec3){ a.x - b.x, a.y - b.y, a.z - b.z }; } -static const inline gnVec3 gnVec3Add(gnVec3 a, gnVec3 b) { return (gnVec3){ a.x + b.x, a.y + b.y, a.z + b.z }; } -static const inline gnVec3 gnVec3Normalize(gnVec3 in) { - float lengthOfVector = sqrt((in.x * in.x) + (in.y * in.y) + (in.z * in.z)); - return (gnVec3){in.x / lengthOfVector, in.y / lengthOfVector, in.z / lengthOfVector}; -} -static const inline gnVec3 gnVec3Cross(gnVec3 a, gnVec3 b) { return (gnVec3){a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; } -static const inline float gnVec3Dot(gnVec3 a, gnVec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; } -static const inline gnVec3 gnVec3Multiply(gnVec3 a, gnVec3 b) { return (gnVec3){ a.x * b.x, a.y * b.y, a.z * b.z }; } -static const inline gnVec3 gnVec3MultiplyBy(gnVec3 a, float b) { return (gnVec3){ a.x * b, a.y * b, a.z * b }; } +gnVec3 gnVec3Subtract(gnVec3 a, gnVec3 b); +gnVec3 gnVec3Add(gnVec3 a, gnVec3 b); +gnVec3 gnVec3Normalize(gnVec3 in); +gnVec3 gnVec3Cross(gnVec3 a, gnVec3 b); +float gnVec3Dot(gnVec3 a, gnVec3 b); +gnVec3 gnVec3Multiply(gnVec3 a, gnVec3 b); +gnVec3 gnVec3MultiplyBy(gnVec3 a, float b); typedef gnVec3 gnFVec3; typedef gnVec3 gnFloat3; @@ -47,24 +25,6 @@ typedef struct gnUInt3 { struct { uint32_t x, y, z; }; struct { uint32_t width, height, depth; }; }; - -#ifdef GN_UTILS_CPP - gnUInt3(uint32_t x, uint32_t y, uint32_t z) { this->x = x; this->y = y; this->z = z; } - gnUInt3(uint32_t s) { this->x = s; this->y = s; this->z = s; } - gnUInt3() {}; - - gnUInt3 operator-(const gnUInt3& other) { - gnUInt3 returnGnVec3; - returnGnVec3.x = this->x - other.x; - returnGnVec3.y = this->y - other.y; - returnGnVec3.z = this->z - other.z; - return returnGnVec3; - } - - bool operator==(const gnUInt3& other) const { - return this->a == other.a && this->b == other.b && this->c == other.c; - } -#endif } gnUInt3; typedef gnUInt3 gnUVec3; @@ -75,22 +35,4 @@ typedef struct gnInt3 { struct { int a, b, c; }; struct { int x, y, z; }; }; - -#ifdef GN_UTILS_CPP - gnInt3(int x, int y, int z) { this->x = x; this->y = y; this->z = z; } - gnInt3(int s) { this->x = s; this->y = s; this->z = s; } - gnInt3() {}; - - gnInt3 operator-(const gnInt3& other) { - gnInt3 returnGnVec3; - returnGnVec3.x = this->x - other.x; - returnGnVec3.y = this->y - other.y; - returnGnVec3.z = this->z - other.z; - return returnGnVec3; - } - - bool operator==(const gnInt3& other) const { - return this->a == other.a && this->b == other.b && this->c == other.c; - } -#endif } gnInt3;