aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2024-02-08 07:53:43 -0800
committer3gg <3gg@shellblade.net>2024-02-08 07:53:43 -0800
commit9306828192644cf367aeec30c00cc6bc178edb62 (patch)
tree2ccee95aac97d0d211fd6301f4ed9697e73b875e /include
parentc0d7fcbc6ca51fbe75e3597ae179bd056df9916d (diff)
Add AABB.
Diffstat (limited to 'include')
-rw-r--r--include/math/aabb2.h35
-rw-r--r--include/math/aabb3.h35
-rw-r--r--include/math/fwd.h2
3 files changed, 72 insertions, 0 deletions
diff --git a/include/math/aabb2.h b/include/math/aabb2.h
new file mode 100644
index 0000000..edb6cb6
--- /dev/null
+++ b/include/math/aabb2.h
@@ -0,0 +1,35 @@
1#pragma once
2
3#include "vec2.h"
4
5#include <stddef.h>
6
7typedef struct aabb2 {
8 vec2 min;
9 vec2 max;
10} aabb2;
11
12/// Construct an AABB from min and max bounds.
13static inline aabb2 aabb2_make(vec2 min, vec2 max) {
14 return (aabb2){.min = min, .max = max};
15}
16
17/// Construct an AABB from an array of points.
18static inline aabb2 aabb2_from_array(const vec2 points[], size_t count) {
19 aabb2 box = (aabb2){0};
20 for (size_t i = 0; i < count; ++i) {
21 box.min = vec2_min(box.min, points[i]);
22 box.max = vec2_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.
28static inline aabb2 aabb2_add(aabb2 box, vec2 p) {
29 return (aabb2){.min = vec2_min(box.min, p), .max = vec2_max(box.max, p)};
30}
31
32/// Return the AABB of two AABBs.
33static inline aabb2 aabb2_sum(aabb2 a, aabb2 b) {
34 return (aabb2){.min = vec2_min(a.min, b.min), .max = vec2_max(a.max, b.max)};
35}
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
7typedef struct aabb3 {
8 vec3 min;
9 vec3 max;
10} aabb3;
11
12/// Construct an AABB from min and max bounds.
13static 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.
18static 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.
28static 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.
33static 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}
diff --git a/include/math/fwd.h b/include/math/fwd.h
index d70817a..283f821 100644
--- a/include/math/fwd.h
+++ b/include/math/fwd.h
@@ -1,6 +1,8 @@
1/// Forward declarations for all math objects. 1/// Forward declarations for all math objects.
2#pragma once 2#pragma once
3 3
4typedef struct aabb2 aabb2;
5typedef struct aabb3 aabb3;
4typedef struct Camera Camera; 6typedef struct Camera Camera;
5typedef struct mat4 mat4; 7typedef struct mat4 mat4;
6typedef struct quat quat; 8typedef struct quat quat;