diff options
Diffstat (limited to 'include/math/aabb3.h')
-rw-r--r-- | include/math/aabb3.h | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/include/math/aabb3.h b/include/math/aabb3.h new file mode 100644 index 0000000..0b0005c --- /dev/null +++ b/include/math/aabb3.h | |||
@@ -0,0 +1,35 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "vec3.h" | ||
4 | |||
5 | #include <stddef.h> | ||
6 | |||
7 | typedef struct aabb3 { | ||
8 | vec3 min; | ||
9 | vec3 max; | ||
10 | } aabb3; | ||
11 | |||
12 | /// Construct an AABB from min and max bounds. | ||
13 | static inline aabb3 aabb3_make(vec3 min, vec3 max) { | ||
14 | return (aabb3){.min = min, .max = max}; | ||
15 | } | ||
16 | |||
17 | /// Construct an AABB from an array of points. | ||
18 | static inline aabb3 aabb3_from_array(const vec3 points[], size_t count) { | ||
19 | aabb3 box = (aabb3){0}; | ||
20 | for (size_t i = 0; i < count; ++i) { | ||
21 | box.min = vec3_min(box.min, points[i]); | ||
22 | box.max = vec3_max(box.max, points[i]); | ||
23 | } | ||
24 | return box; | ||
25 | } | ||
26 | |||
27 | /// Add a point to the AABB. The AABB grows to contain the point. | ||
28 | static inline aabb3 aabb3_add(aabb3 box, vec3 p) { | ||
29 | return (aabb3){.min = vec3_min(box.min, p), .max = vec3_max(box.max, p)}; | ||
30 | } | ||
31 | |||
32 | /// Return the AABB of two AABBs. | ||
33 | static inline aabb3 aabb3_sum(aabb3 a, aabb3 b) { | ||
34 | return (aabb3){.min = vec3_min(a.min, b.min), .max = vec3_max(a.max, b.max)}; | ||
35 | } | ||