aboutsummaryrefslogtreecommitdiff
path: root/include/math/aabb3.h
blob: 0b0005c1e85c1b121c9c7cd9a79f16c36538c97f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pragma once

#include "vec3.h"

#include <stddef.h>

typedef struct aabb3 {
  vec3 min;
  vec3 max;
} aabb3;

/// Construct an AABB from min and max bounds.
static inline aabb3 aabb3_make(vec3 min, vec3 max) {
  return (aabb3){.min = min, .max = max};
}

/// Construct an AABB from an array of points.
static inline aabb3 aabb3_from_array(const vec3 points[], size_t count) {
  aabb3 box = (aabb3){0};
  for (size_t i = 0; i < count; ++i) {
    box.min = vec3_min(box.min, points[i]);
    box.max = vec3_max(box.max, points[i]);
  }
  return box;
}

/// Add a point to the AABB. The AABB grows to contain the point.
static inline aabb3 aabb3_add(aabb3 box, vec3 p) {
  return (aabb3){.min = vec3_min(box.min, p), .max = vec3_max(box.max, p)};
}

/// Return the AABB of two AABBs.
static inline aabb3 aabb3_sum(aabb3 a, aabb3 b) {
  return (aabb3){.min = vec3_min(a.min, b.min), .max = vec3_max(a.max, b.max)};
}