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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
#pragma once
#include <array>
#include <vector>
#include "Vector.hpp"
#include "../Common/Lambda.hpp"
struct AABB {
AABB() = default;
AABB(Vector<3> min, Vector<3> max) : min(min), max(max) {}
explicit AABB(Vector<3> max) : max(max) {}
static AABB from_center(Vector<3> center, Vector<3> size) {
auto min = center - size / 2.0;
auto max = min + size;
return {min, max};
}
Vector<3> size() const { return max - min; }
Vector<3> center() const { return (min + max) / 2; }
Bool contains(Vector<3> point) const {
return point.x() >= min.x() && point.x() <= max.x()
&& point.y() >= min.y() && point.y() <= max.y()
&& point.z() >= min.z() && point.z() <= max.z();
}
Bool intersects_on_x(AABB other) const {
return min.x() <= other.max.x() && max.x() >= other.min.x();
}
Bool intersects_on_y(AABB other) const {
return min.y() <= other.max.y() && max.y() >= other.min.y();
}
Bool intersects_on_z(AABB other) const {
return min.z() <= other.max.z() && max.z() >= other.min.z();
}
Bool collides(AABB other) const {
return intersects_on_x(other) && intersects_on_y(other) && intersects_on_z(other);
}
Bool collides(std::vector<AABB> others) {
for (auto& other : others) {
if (collides(other)) return true;
}
return false;
}
std::array<Vector<3>, 8> corners() const {
return {{
{min.x(), min.y(), min.z()},
{min.x(), min.y(), max.z()},
{min.x(), max.y(), min.z()},
{min.x(), max.y(), max.z()},
{max.x(), min.y(), min.z()},
{max.x(), min.y(), max.z()},
{max.x(), max.y(), min.z()},
{max.x(), max.y(), max.z()},
}};
}
AABB offset(Vector<3> by) const { return {min + by, max + by}; }
AABB sum(AABB with) const {
auto new_size = size() + with.size();
return {center() - new_size / 2, center() + new_size / 2};
}
AABB unite(AABB with) const {
return {
min.zip(with.min, LAMBDA(std::min, 2)),
max.zip(with.max, LAMBDA(std::max, 2))
};
}
Vec3 pushout(Vector<3> v, AABB against) const;
Vector<3> min, max;
};
|