CMSC23700 Common Code Library
Support code for CS23700 programming projects
Loading...
Searching...
No Matches
cs237-aabb.hpp
Go to the documentation of this file.
1
10/*
11 * COPYRIGHT (c) 2023 John Reppy (http://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
22namespace cs237 {
23
24namespace __detail {
25
27template <typename T>
28struct AABB {
29 using vec3 = glm::vec<3, T, glm::defaultp>;
30
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 }
142 T minY () const
143 {
144 assert (! this->_empty);
145 return this->_min.y;
146 }
148 T minZ () const
149 {
150 assert (! this->_empty);
151 return this->_min.z;
152 }
154 T maxX () const
155 {
156 assert (! this->_empty);
157 return this->_max.x;
158 }
160 T maxY () const
161 {
162 assert (! this->_empty);
163 return this->_max.y;
164 }
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
201template <typename T>
202std::ostream& operator<< (std::ostream& s, __detail::AABB<T> const &bb);
203
208template <typename T>
209inline 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
227
228} //namespace cs237
229
230#endif /* !_CS237_AABB_HPP_ */
#define DELTA(A)
std::ostream & operator<<(std::ostream &s, __detail::AABB< T > const &bb)
print the axis-aligned bounding box to the output stream
AABB< T > operator+(AABB< T > const &bb1, AABB< T > &bb2)
Definition cs237-aabb.hpp:209
Definition cs237-aabb.hpp:22
Axis-Aligned Bounding Box parameterized over the scalar type.
Definition cs237-aabb.hpp:28
vec3 _max
Definition cs237-aabb.hpp:31
AABB(vec3 const &min, vec3 const &max)
Definition cs237-aabb.hpp:37
bool isEmpty() const
is the box empty
Definition cs237-aabb.hpp:40
T maxY() const
maximum Y-coordinate of the box
Definition cs237-aabb.hpp:160
AABB(AABB const &bbox)
Definition cs237-aabb.hpp:35
T minX() const
minimum X-coordinate of the box
Definition cs237-aabb.hpp:136
T maxX() const
maximum X-coordinate of the box
Definition cs237-aabb.hpp:154
void clear()
clear the box (i.e., make it empty)
Definition cs237-aabb.hpp:43
vec3 const & max() const
maximum extents of the box
Definition cs237-aabb.hpp:122
AABB()
Definition cs237-aabb.hpp:34
std::array< vec3, 8 > corners() const
return an array of the corners of the box
Definition cs237-aabb.hpp:185
T minZ() const
minimum Z-coordinate of the box
Definition cs237-aabb.hpp:148
T minY() const
minimum Y-coordinate of the box
Definition cs237-aabb.hpp:142
vec3 center() const
center of the box
Definition cs237-aabb.hpp:129
AABB & operator+=(AABB const &bb)
Definition cs237-aabb.hpp:89
vec3 const & min() const
minimum extents of the box
Definition cs237-aabb.hpp:115
T distanceToPt(vec3 const &pt) const
distance to a point; will be 0.0 if the point is inside the box
Definition cs237-aabb.hpp:55
vec3 _min
Definition cs237-aabb.hpp:31
AABB(vec3 const &pt)
Definition cs237-aabb.hpp:36
void addPt(vec3 const &pt)
extend this box as necessary to include the point
Definition cs237-aabb.hpp:72
bool includesPt(vec3 const &pt) const
is a point inside the box?
Definition cs237-aabb.hpp:46
T maxZ() const
maximum Z-coordinate of the box
Definition cs237-aabb.hpp:166
vec3 corner(int i) const
Definition cs237-aabb.hpp:174
bool _empty
Definition cs237-aabb.hpp:32
glm::vec< 3, T, glm::defaultp > vec3
Definition cs237-aabb.hpp:29