utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
aabb.hpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#pragma once
24
25#include <algorithm>
26#include <concepts>
27#include <cstddef>
28#include <iterator>
29#include <limits>
30#include <ostream>
31#include <span>
32#include <type_traits>
33
34#include <glm/geometric.hpp>
35#include <glm/vec3.hpp>
36
37#include <utility/math/vector.hpp>
38
39namespace utility::math
40{
52 template<CanBeVectorComponent T>
53 class Aabb
54 {
55 static_assert(
56 std::is_floating_point_v<T>,
57 "Aabb requires a floating-point component type (float or double)");
58
59 public:
66 Aabb(void)
67 : _min(std::numeric_limits<T>::max())
68 , _max(std::numeric_limits<T>::lowest())
69 {
70 }
71
77 Aabb(const Vector<T, 3> &min, const Vector<T, 3> &max)
78 : _min(min)
79 , _max(max)
80 {
81 }
82
93 template<std::input_iterator Iterator,
94 std::sentinel_for<Iterator> Sentinel>
95 static Aabb fromPoints(Iterator first, Sentinel last)
96 {
97 Aabb box;
98 for (; first != last; ++first) {
99 box.include(*first);
100 }
101 return box;
102 }
103
114 static Aabb fromPoints(std::span<const Vector<T, 3>> points)
115 {
116 return fromPoints(points.begin(), points.end());
117 }
118
123 void include(const Vector<T, 3> &point) noexcept
124 {
125 for (std::size_t i = 0; i < 3; ++i) {
126 _min[i] = std::min(_min[i], point[i]);
127 _max[i] = std::max(_max[i], point[i]);
128 }
129 }
130
137 void include(const Aabb &other) noexcept
138 {
139 if (other.isEmpty()) {
140 return;
141 }
142 include(other._min);
143 include(other._max);
144 }
145
153 [[nodiscard]] Vector<T, 3> center(void) const noexcept
154 {
155 return _min * T { 0.5 } + _max * T { 0.5 };
156 }
157
162 [[nodiscard]] Vector<T, 3> extents(void) const noexcept
163 {
164 return isEmpty() ? Vector<T, 3>(T { 0 }) : (_max - _min);
165 }
166
171 [[nodiscard]] Vector<T, 3> halfSize(void) const noexcept
172 {
173 return extents() * T { 0.5 };
174 }
175
182 [[nodiscard]] T radius(void) const noexcept
183 {
184 return static_cast<T>(glm::length(
185 static_cast<const glm::vec<3, T> &>(halfSize())));
186 }
187
197 [[nodiscard]] Vector<T, 3>
198 positiveVertex(const Vector<T, 3> &normal) const noexcept
199 {
200 Vector<T, 3> result;
201 for (std::size_t i = 0; i < 3; ++i) {
202 result[i] = (normal[i] >= T { 0 }) ? _max[i] : _min[i];
203 }
204 return result;
205 }
206
214 [[nodiscard]] bool contains(const Vector<T, 3> &point) const noexcept
215 {
216 if (isEmpty()) {
217 return false;
218 }
219 for (std::size_t i = 0; i < 3; ++i) {
220 if (point[i] < _min[i] || point[i] > _max[i]) {
221 return false;
222 }
223 }
224 return true;
225 }
226
233 [[nodiscard]] bool intersects(const Aabb &other) const noexcept
234 {
235 if (isEmpty() || other.isEmpty()) {
236 return false;
237 }
238 for (std::size_t i = 0; i < 3; ++i) {
239 if (_min[i] > other._max[i] || other._min[i] > _max[i]) {
240 return false;
241 }
242 }
243 return true;
244 }
245
250 [[nodiscard]] bool isEmpty(void) const noexcept
251 {
252 for (std::size_t i = 0; i < 3; ++i) {
253 if (_min[i] > _max[i]) {
254 return true;
255 }
256 }
257 return false;
258 }
259
264 [[nodiscard]] Vector<T, 3> getMin(void) const noexcept
265 {
266 return _min;
267 }
268
273 [[nodiscard]] Vector<T, 3> getMax(void) const noexcept
274 {
275 return _max;
276 }
277
278 private:
279 Vector<T, 3> _min;
280 Vector<T, 3> _max;
281 };
282
286 using AabbF = Aabb<float>;
287
291 using AabbD = Aabb<double>;
292
299 std::ostream &operator<<(std::ostream &stream, const AabbF &box);
300
307 std::ostream &operator<<(std::ostream &stream, const AabbD &box);
308
309} // namespace utility::math
Axis-aligned bounding box (AABB) in 3D space.
Definition aabb.hpp:54
void include(const Aabb &other) noexcept
Grow the box to contain another box (union).
Definition aabb.hpp:137
bool isEmpty(void) const noexcept
Test whether the box is empty (inverted).
Definition aabb.hpp:250
T radius(void) const noexcept
Get the radius of the bounding sphere around the box.
Definition aabb.hpp:182
Vector< T, 3 > positiveVertex(const Vector< T, 3 > &normal) const noexcept
Get the corner farthest along a direction (the "p-vertex").
Definition aabb.hpp:198
Vector< T, 3 > getMax(void) const noexcept
Get the maximum corner.
Definition aabb.hpp:273
static Aabb fromPoints(Iterator first, Sentinel last)
Build the tightest box containing a range of 3D points.
Definition aabb.hpp:95
void include(const Vector< T, 3 > &point) noexcept
Grow the box to contain a point.
Definition aabb.hpp:123
Vector< T, 3 > halfSize(void) const noexcept
Get half the size of the box along each axis.
Definition aabb.hpp:171
Aabb(const Vector< T, 3 > &min, const Vector< T, 3 > &max)
Construct from explicit minimum and maximum corners.
Definition aabb.hpp:77
Vector< T, 3 > center(void) const noexcept
Get the center of the box.
Definition aabb.hpp:153
Vector< T, 3 > extents(void) const noexcept
Get the full size of the box along each axis.
Definition aabb.hpp:162
Aabb(void)
Default constructor creating an empty (inverted) box.
Definition aabb.hpp:66
bool contains(const Vector< T, 3 > &point) const noexcept
Test whether a point lies inside the box (boundaries included).
Definition aabb.hpp:214
bool intersects(const Aabb &other) const noexcept
Test whether two boxes overlap (touching counts).
Definition aabb.hpp:233
static Aabb fromPoints(std::span< const Vector< T, 3 > > points)
Build the tightest box containing a contiguous range of points.
Definition aabb.hpp:114
Vector< T, 3 > getMin(void) const noexcept
Get the minimum corner.
Definition aabb.hpp:264
3D vector class inheriting from glm::vec3.
Definition vector.hpp:83