CMSC23740 Common Code Library
Support code for CS23740 programming projects
Toggle main menu visibility
Loading...
Searching...
No Matches
aabb.hpp
Go to the documentation of this file.
1
9
10
/*
11
* COPYRIGHT (c) 2026 John Reppy (https://cs.uchicago.edu/~jhr)
12
* All rights reserved.
13
*/
14
15
#ifndef _CS237_AABB_HPP_
16
#define _CS237_AABB_HPP_
17
18
#ifndef _CS237_HPP_
19
# error "cs237/aabb.hpp should not be included directly"
20
#endif
21
22
namespace
cs237
{
23
24
namespace
__detail
{
25
27
template
<
typename
T>
28
struct
AABB
{
29
using
vec3
= glm::vec<3, T, glm::defaultp>;
30
31
vec3
_min
,
_max
;
32
bool
_empty
;
33
34
AABB
() :
_min
(),
_max
(),
_empty
(true) {}
35
AABB
(
AABB
const
& bbox) :
_min
(bbox.
_min
),
_max
(bbox.
_max
),
_empty
(bbox.
_empty
) {}
36
AABB
(
vec3
const
&pt) :
_min
(pt),
_max
(pt),
_empty
(false) {}
37
AABB
(
vec3
const
&
min
,
vec3
const
&
max
) :
_min
(
min
),
_max
(
max
),
_empty
(false) {}
38
40
bool
isEmpty
()
const
{
return
this->_empty; }
41
43
void
clear
() { this->_empty =
true
; }
44
46
bool
includesPt
(
vec3
const
&pt)
const
47
{
48
return
(! this->_empty)
49
&& (this->_min.x <= pt.x) && (pt.x <= this->_max.x)
50
&& (this->_min.y <= pt.y) && (pt.y <= this->_max.y)
51
&& (this->_min.z <= pt.z) && (pt.z <= this->_max.z);
52
}
53
55
T
distanceToPt
(
vec3
const
&pt)
const
56
{
57
#define DELTA(A) \
58
(pt.A < this->_min.A) \
59
? (this->_min.A - pt.A) \
60
: ((this->_max.A < pt.A) ? (pt.A - this->_max.A) : T(0))
61
62
vec3
dv(
DELTA
(x),
DELTA
(y),
DELTA
(z));
63
T lenSq = dot(dv, dv);
64
if
(lenSq == 0.0)
65
return
lenSq;
66
else
67
return
sqrt(lenSq);
68
#undef DELTA
69
}
70
72
void
addPt
(
vec3
const
&pt)
73
{
74
if
(this->_empty) {
75
this->_empty =
false
;
76
this->_min = pt;
77
this->_max = pt;
78
}
79
else
if
(pt.x < this->_min.x) this->_min.x = pt.x;
80
else
if
(this->_max.x < pt.x) this->_max.x = pt.x;
81
if
(pt.y < this->_min.y) this->_min.y = pt.y;
82
else
if
(this->_max.y < pt.y) this->_max.y = pt.y;
83
if
(pt.z < this->_min.z) this->_min.z = pt.z;
84
else
if
(this->_max.z < pt.z) this->_max.z = pt.z;
85
}
86
89
AABB
&
operator+=
(
AABB
const
&bb)
90
{
91
if
((! this->_empty) && (! bb.
_empty
)) {
92
this->_min = glm::min(this->_min, bb.
_min
);
93
this->_max = glm::min(this->_max, bb.
_max
);
94
}
95
else
if
(this->_empty) {
96
this->_empty =
false
;
97
this->_min = bb.
_min
;
98
this->_max = bb.
_max
;
99
}
100
101
return
*
this
;
102
}
103
106
AABB
&
operator+=
(
vec3
const
&pt)
107
{
108
this->
addPt
(pt);
109
return
*
this
;
110
}
111
112
/***** Warning: the following functions are undefined on empty boxes! *****/
113
115
vec3
const
&
min
()
const
116
{
117
assert (! this->_empty);
118
return
this->_min;
119
}
120
122
vec3
const
&
max
()
const
123
{
124
assert (! this->_empty);
125
return
this->_max;
126
}
127
129
vec3
center
()
const
130
{
131
assert (! this->_empty);
132
return
T(0.5) * (this->_min + this->_max);
133
}
134
136
T
minX
()
const
137
{
138
assert (! this->_empty);
139
return
this->_min.x;
140
}
141
142
T
minY
()
const
143
{
144
assert (! this->_empty);
145
return
this->_min.y;
146
}
147
148
T
minZ
()
const
149
{
150
assert (! this->_empty);
151
return
this->_min.z;
152
}
153
154
T
maxX
()
const
155
{
156
assert (! this->_empty);
157
return
this->_max.x;
158
}
159
160
T
maxY
()
const
161
{
162
assert (! this->_empty);
163
return
this->_max.y;
164
}
165
166
T
maxZ
()
const
167
{
168
assert (! this->_empty);
169
return
this->_max.z;
170
}
171
174
vec3
corner
(
int
i)
const
175
{
176
assert (! this->_empty);
177
assert ((0 <= i) && (i < 8));
178
return
vec3
(
179
(i & 4) ? this->_max.x : this->_min.x,
180
(i & 2) ? this->_max.y : this->_min.y,
181
(i & 1) ? this->_max.z : this->_min.z);
182
}
183
185
std::array<vec3,8>
corners
()
const
186
{
187
return
std::array<vec3,8>{
188
vec3
(this->_min.x, this->_min.y, this->_min.z),
189
vec3
(this->_min.x, this->_min.y, this->_max.z),
190
vec3
(this->_min.x, this->_max.y, this->_min.z),
191
vec3
(this->_min.x, this->_max.y, this->_max.z),
192
vec3
(this->_max.x, this->_min.y, this->_min.z),
193
vec3
(this->_max.x, this->_min.y, this->_max.z),
194
vec3
(this->_max.x, this->_max.y, this->_min.z),
195
vec3
(this->_max.x, this->_max.y, this->_max.z)
196
};
197
}
198
};
199
201
template
<
typename
T>
202
std::ostream&
operator<<
(std::ostream& s,
__detail::AABB<T>
const
&bb);
203
208
template
<
typename
T>
209
inline
AABB<T>
operator+
(
AABB<T>
const
&bb1,
AABB<T>
&bb2)
210
{
211
if
((! bb1.
_empty
) && (! bb2.empty)) {
212
return
AABB<T>
(glm::min(bb1.
_min
, bb2.
_min
), glm::max(bb1.
_min
, bb2.
_min
));
213
}
214
else
if
(bb1.empty) {
215
return
bb2;
216
}
else
{
217
return
bb1;
218
}
219
}
220
221
}
// namespace __detail
222
224
using
AABBf_t
=
__detail::AABB<float>
;
226
using
AABBd_t
=
__detail::AABB<double>
;
227
228
}
//namespace cs237
229
230
#endif
/* !_CS237_AABB_HPP_ */
DELTA
#define DELTA(A)
cs237::__detail
Definition
aabb.hpp:24
cs237::__detail::operator<<
std::ostream & operator<<(std::ostream &s, __detail::AABB< T > const &bb)
print the axis-aligned bounding box to the output stream
cs237::__detail::operator+
AABB< T > operator+(AABB< T > const &bb1, AABB< T > &bb2)
Definition
aabb.hpp:209
cs237
Definition
aabb.hpp:22
cs237::AABBf_t
__detail::AABB< float > AABBf_t
Single-precision axis-aligned bounding boxes.
Definition
aabb.hpp:224
cs237::AABBd_t
__detail::AABB< double > AABBd_t
Double-precision axis-aligned bounding boxes.
Definition
aabb.hpp:226
cs237::__detail::AABB
Axis-Aligned Bounding Box parameterized over the scalar type.
Definition
aabb.hpp:28
cs237::__detail::AABB< float >::_max
vec3 _max
Definition
aabb.hpp:31
cs237::__detail::AABB::AABB
AABB(vec3 const &min, vec3 const &max)
Definition
aabb.hpp:37
cs237::__detail::AABB::isEmpty
bool isEmpty() const
is the box empty
Definition
aabb.hpp:40
cs237::__detail::AABB::maxY
T maxY() const
maximum Y-coordinate of the box
Definition
aabb.hpp:160
cs237::__detail::AABB::AABB
AABB(AABB const &bbox)
Definition
aabb.hpp:35
cs237::__detail::AABB::minX
T minX() const
minimum X-coordinate of the box
Definition
aabb.hpp:136
cs237::__detail::AABB::maxX
T maxX() const
maximum X-coordinate of the box
Definition
aabb.hpp:154
cs237::__detail::AABB::clear
void clear()
clear the box (i.e., make it empty)
Definition
aabb.hpp:43
cs237::__detail::AABB< float >::max
vec3 const & max() const
Definition
aabb.hpp:122
cs237::__detail::AABB::AABB
AABB()
Definition
aabb.hpp:34
cs237::__detail::AABB::corners
std::array< vec3, 8 > corners() const
return an array of the corners of the box
Definition
aabb.hpp:185
cs237::__detail::AABB::minZ
T minZ() const
minimum Z-coordinate of the box
Definition
aabb.hpp:148
cs237::__detail::AABB::minY
T minY() const
minimum Y-coordinate of the box
Definition
aabb.hpp:142
cs237::__detail::AABB::center
vec3 center() const
center of the box
Definition
aabb.hpp:129
cs237::__detail::AABB::operator+=
AABB & operator+=(AABB const &bb)
Definition
aabb.hpp:89
cs237::__detail::AABB< float >::min
vec3 const & min() const
Definition
aabb.hpp:115
cs237::__detail::AABB::distanceToPt
T distanceToPt(vec3 const &pt) const
distance to a point; will be 0.0 if the point is inside the box
Definition
aabb.hpp:55
cs237::__detail::AABB< float >::_min
vec3 _min
Definition
aabb.hpp:31
cs237::__detail::AABB::AABB
AABB(vec3 const &pt)
Definition
aabb.hpp:36
cs237::__detail::AABB::addPt
void addPt(vec3 const &pt)
extend this box as necessary to include the point
Definition
aabb.hpp:72
cs237::__detail::AABB::includesPt
bool includesPt(vec3 const &pt) const
is a point inside the box?
Definition
aabb.hpp:46
cs237::__detail::AABB::maxZ
T maxZ() const
maximum Z-coordinate of the box
Definition
aabb.hpp:166
cs237::__detail::AABB::corner
vec3 corner(int i) const
Definition
aabb.hpp:174
cs237::__detail::AABB< float >::_empty
bool _empty
Definition
aabb.hpp:32
cs237::__detail::AABB::vec3
glm::vec< 3, T, glm::defaultp > vec3
Definition
aabb.hpp:29
cs237-library
include
cs237
aabb.hpp
Generated by
1.18.0