-
Notifications
You must be signed in to change notification settings - Fork 1
/
AABB.h
178 lines (149 loc) · 4.16 KB
/
AABB.h
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// AABB.h
// Termin8or
//
// Created by Rasmus Anthin on 2024-10-24.
//
#pragma once
#include "Rectangle.h"
#include <Core/Vec2.h>
template<typename T>
class AABB
{
T rmin = math::get_max<T>();
T rmax = math::get_min<T>();
T cmin = math::get_max<T>();
T cmax = math::get_min<T>();
constexpr T offs() const { return static_cast<T>(std::is_integral<T>() ? 1 : 0); }
public:
AABB() = default;
AABB(T r, T c, T r_len, T c_len)
{
rmin = r;
cmin = c;
rmax = r + r_len - offs();
cmax = c + c_len - offs();
}
AABB(const ttl::Rectangle& rectangle)
{
rmin = static_cast<T>(rectangle.r);
cmin = static_cast<T>(rectangle.c);
rmax = rmin + static_cast<T>(rectangle.r_len - offs());
cmax = cmin + static_cast<T>(rectangle.c_len - offs());
}
AABB(const AABB& aabb)
{
rmin = aabb.rmin;
cmin = aabb.cmin;
rmax = aabb.rmax;
cmax = aabb.cmax;
}
std::pair<T, T> p0() const { return { rmin, cmin }; }
std::pair<T, T> p1() const { return { rmax, cmax }; }
T r_min() const { return rmin; }
T r_max() const { return rmax; }
T c_min() const { return cmin; }
T c_max() const { return cmax; }
T height() const { return rmax - rmin + offs(); }
T width() const { return cmax - cmin + offs(); }
void set_empty()
{
rmin = math::get_max<T>();
rmax = math::get_min<T>();
cmin = math::get_max<T>();
cmax = math::get_min<T>();
}
bool empty() const
{
return rmax < rmin || cmax < cmin;
}
void add_point(T r, T c)
{
math::minimize(rmin, r);
math::maximize(rmax, r);
math::minimize(cmin, c);
math::maximize(cmax, c);
}
void add_point(const RC& pt)
{
add_point(static_cast<T>(pt.r), static_cast<T>(pt.c));
}
void add_point(const Vec2& pt)
{
static_assert(std::is_floating_point<T>(), "ERROR in add_point_round() : Can only be used when T = float.");
add_point(static_cast<T>(pt.r), static_cast<T>(pt.c));
}
// Only rounds if T = int.
void add_point_round(const Vec2& pt)
{
static_assert(std::is_integral<T>(), "ERROR in add_point_round() : Can only be used when T = int.");
add_point(math::roundI(pt.r), math::roundI(pt.c));
}
// Only does floor if T = int.
void add_point_floor(const Vec2& pt)
{
static_assert(std::is_integral<T>(), "ERROR in add_point_floor() : Can only be used when T = int.");
add_point(static_cast<T>(pt.r), static_cast<T>(pt.c));
}
AABB set_union(const AABB& aabb) const
{
AABB ret = *this;
math::minimize(ret.rmin, aabb.rmin);
math::maximize(ret.rmax, aabb.rmax);
math::minimize(ret.cmin, aabb.cmin);
math::maximize(ret.cmax, aabb.cmax);
return ret;
}
AABB set_intersect(const AABB& aabb) const
{
AABB ret = *this;
math::maximize(ret.rmin, aabb.rmin);
math::minimize(ret.rmax, aabb.rmax);
math::maximize(ret.cmin, aabb.cmin);
math::minimize(ret.cmax, aabb.cmax);
return ret;
}
void grow(const AABB& aabb)
{
math::minimize(rmin, aabb.rmin);
math::maximize(rmax, aabb.rmax);
math::minimize(cmin, aabb.cmin);
math::maximize(cmax, aabb.cmax);
}
bool contains(T r, T c) const
{
return math::in_range<T>(r, rmin, rmax, Range::Closed)
&& math::in_range<T>(c, cmin, cmax, Range::Closed);
}
bool contains(const RC& pt) const
{
return contains(static_cast<T>(pt.r), static_cast<T>(pt.c));
}
bool overlaps(const AABB<T>& aabb) const
{
return !(aabb.rmax < this->rmin
|| this->rmax < aabb.rmin
|| aabb.cmax < this->cmin
|| this->cmax < aabb.cmin);
}
ttl::Rectangle to_rectangle() const
{
return
{
static_cast<int>(rmin), static_cast<int>(cmin),
static_cast<int>(rmax - rmin + offs()), static_cast<int>(cmax - cmin + offs())
};
}
template<typename Ret>
AABB<Ret> convert() const
{
AABB<Ret> aabb
{
static_cast<Ret>(math::roundI(static_cast<float>(rmin))),
static_cast<Ret>(math::roundI(static_cast<float>(cmin))),
static_cast<Ret>(math::roundI(static_cast<float>(rmax - rmin + offs()))),
static_cast<Ret>(math::roundI(static_cast<float>(cmax - cmin + offs()))),
};
return aabb;
}
};