From c171a2ff7ae849fe8dc7c33ab97ab7445dfb5986 Mon Sep 17 00:00:00 2001 From: Alex Selimov Date: Tue, 20 Oct 2026 22:48:22 -0400 Subject: [PATCH] Initial commit with basic vec3 equals function --- .gitignore | 6 ++++ README.md | 3 ++ mathprimitives/Makefile | 49 +++++++++++++++++++++++++++++++++ mathprimitives/README.md | 10 +++++++ mathprimitives/mathprimitives.h | 26 +++++++++++++++++ mathprimitives/test.c | 33 ++++++++++++++++++++++ shell.nix | 9 ++++++ test.c | 3 ++ 8 files changed, 139 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 mathprimitives/Makefile create mode 100644 mathprimitives/README.md create mode 100644 mathprimitives/mathprimitives.h create mode 100644 mathprimitives/test.c create mode 100644 shell.nix create mode 100644 test.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1993ca9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +test +*.d +*.o +.direnv +.envrc +.clangd diff --git a/README.md b/README.md new file mode 100644 index 0000000..c048247 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Collection of my projects using C + +I use a monorepo to facilitate sharing libraries between my different projects. diff --git a/mathprimitives/Makefile b/mathprimitives/Makefile new file mode 100644 index 0000000..17a88a2 --- /dev/null +++ b/mathprimitives/Makefile @@ -0,0 +1,49 @@ +CC=gcc +DEBUG=yes +EXEC=test + +# all c files in this directory +SRC = $(wildcard *.c) +# otherwise +#SRC = $(wildcard src_dir1/*.c) \ +# $(wildcard src_dir2/*.c) + +# -g for debug +# -Wall to get all the possible warnings +# -ansi -pendantic to get portable code +ifeq ($(DEBUG),yes) + CFLAGS=-W -Wall -ansi -pedantic -g -std=c23 + LDFLAGS= -Wall -lm +else + CFLAGS=-W -Wall -ansi -pedantic -std=c23 -O3 + LDFLAGS= -Wall -lm +endif + +############################################### +# end of the configuration +############################################### + +all: $(EXEC) + +obj = $(SRC:.c=.o) +dep = $(obj:.o=.d) + + +$(EXEC): $(obj) + $(CC) -o $@ $^ $(LDFLAGS) + +.PHONY: tests +tests: $(EXEC) + ./test + +-include $(dep) + +%.d: %.c + @$(CC) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ + +%.o: %.c + $(CC) $(CFLAGS) -o $@ -c $< + +.PHONY: clean +clean: + rm -f $(obj) $(EXEC) $(dep) diff --git a/mathprimitives/README.md b/mathprimitives/README.md new file mode 100644 index 0000000..65fdce9 --- /dev/null +++ b/mathprimitives/README.md @@ -0,0 +1,10 @@ +# mathprimitives + +Collection of math primitives and functions used for various projects. + +Includes implementations of the following using double precision floats + +- Vector3 +- Matrix3 +- Quat + diff --git a/mathprimitives/mathprimitives.h b/mathprimitives/mathprimitives.h new file mode 100644 index 0000000..a3e3d91 --- /dev/null +++ b/mathprimitives/mathprimitives.h @@ -0,0 +1,26 @@ +#ifndef MATHPRIMITIVES_H +#define MATHPRIMITIVES_H + +#include +#include + +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 bool 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; +} + +#endif diff --git a/mathprimitives/test.c b/mathprimitives/test.c new file mode 100644 index 0000000..56e1b92 --- /dev/null +++ b/mathprimitives/test.c @@ -0,0 +1,33 @@ +#include "./mathprimitives.h" +#include + +#ifdef assert +#undef assert +#endif + +int test_vec3_equal_tol() { + struct Vec3 a = {.x = 1.0, .y = 2.0, .z = 3.0}; + struct Vec3 b = {.x = 1.0, .y = 2.0, .z = 3.0}; + + return vec3_eql_tol(a, b, 1e-12); +} + +int main() { + int tests_total = 0; + int tests_passed = 0; + + tests_total += 1; + if (test_vec3_equal_tol()) { + tests_passed += 1; + } else { + printf("\033[91mtest_vec3_equal_tol failed\033[0m"); + }; + + if (tests_total == tests_passed) { + printf("\033[92mAll tests passed!\033[0m"); + } else { + + printf("\033[91m%d of %d tests failed\033[0m", tests_total - tests_passed, + tests_total); + } +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..b1709b5 --- /dev/null +++ b/shell.nix @@ -0,0 +1,9 @@ +{ pkgs ? import {} }: + +pkgs.mkShell { + nativeBuildInputs = [ + pkgs.clang + pkgs.clang-tools + pkgs.glibc.dev + ]; +} diff --git a/test.c b/test.c new file mode 100644 index 0000000..0b248e4 --- /dev/null +++ b/test.c @@ -0,0 +1,3 @@ +#include + +int main(void) { return EXIT_SUCCESS; }