Initial commit with basic vec3 equals function
This commit is contained in:
commit
c171a2ff7a
8 changed files with 139 additions and 0 deletions
49
mathprimitives/Makefile
Normal file
49
mathprimitives/Makefile
Normal file
|
|
@ -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)
|
||||
10
mathprimitives/README.md
Normal file
10
mathprimitives/README.md
Normal file
|
|
@ -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
|
||||
|
||||
26
mathprimitives/mathprimitives.h
Normal file
26
mathprimitives/mathprimitives.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#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 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
|
||||
33
mathprimitives/test.c
Normal file
33
mathprimitives/test.c
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#include "./mathprimitives.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue