UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
aabb.hpp
1
42#ifndef UFO_GEOMETRY_AABB_HPP
43#define UFO_GEOMETRY_AABB_HPP
44
45// UFO
46#include <ufo/numeric/vec.hpp>
47
48// STL
49#include <concepts>
50#include <cstddef>
51#include <format>
52#include <ostream>
53
59namespace ufo
60{
69template <std::size_t Dim = 3, std::floating_point T = float>
70struct AABB {
71 using value_type = T;
72
77
82
86 constexpr AABB() noexcept = default;
87
94 constexpr AABB(Vec<Dim, T> const& min, Vec<Dim, T> const& max) noexcept
95 : min(min), max(max)
96 {
97 }
98
105 constexpr AABB(Vec<Dim, T> const& center, T half_length) noexcept
106 : min(center - half_length), max(center + half_length)
107 {
108 }
109
113 constexpr AABB(AABB const&) noexcept = default;
114
121 template <std::convertible_to<T> U>
122 constexpr explicit AABB(AABB<Dim, U> const& other) noexcept
123 : min(other.min), max(other.max)
124 {
125 }
126
131 [[nodiscard]] static constexpr std::size_t dimension() noexcept { return Dim; }
132
137 [[nodiscard]] constexpr Vec<Dim, T> center() const noexcept
138 {
139 return min + halfLength();
140 }
141
146 [[nodiscard]] constexpr Vec<Dim, T> length() const noexcept { return max - min; }
147
152 [[nodiscard]] constexpr Vec<Dim, T> halfLength() const noexcept
153 {
154 return length() * T(0.5);
155 }
156
161 [[nodiscard]] constexpr T diagonal() const noexcept { return ufo::length(length()); }
162
167 [[nodiscard]] constexpr T volume() const noexcept
168 {
169 auto l = length();
170 T v = l[0];
171 for (std::size_t i = 1; i < Dim; ++i) {
172 v *= l[i];
173 }
174 return v;
175 }
176
181 [[nodiscard]] constexpr AABB aabb() const noexcept { return *this; }
182
187 [[nodiscard]] constexpr bool isDegenerate() const noexcept
188 {
189 return any(greaterThan(min, max));
190 }
191
198 [[nodiscard]] bool operator==(AABB const&) const = default;
199};
200
201//
202// Deduction guides
203//
204
205template <std::size_t Dim, std::floating_point T>
207
208template <std::size_t Dim, std::floating_point T>
210
217template <std::size_t Dim, std::floating_point T>
218std::ostream& operator<<(std::ostream& out, AABB<Dim, T> const& aabb)
219{
220 return out << "Min: [" << aabb.min << "], Max: [" << aabb.max << "]";
221}
222
223template <std::floating_point T>
224using AABB1 = AABB<1, T>;
225template <std::floating_point T>
226using AABB2 = AABB<2, T>;
227template <std::floating_point T>
228using AABB3 = AABB<3, T>;
229template <std::floating_point T>
230using AABB4 = AABB<4, T>;
231
232using AABB1f = AABB<1, float>;
233using AABB2f = AABB<2, float>;
234using AABB3f = AABB<3, float>;
235using AABB4f = AABB<4, float>;
236
237using AABB1d = AABB<1, double>;
238using AABB2d = AABB<2, double>;
239using AABB3d = AABB<3, double>;
240using AABB4d = AABB<4, double>;
241
242} // namespace ufo
243
248template <std::size_t Dim, std::floating_point T>
249 requires std::formattable<T, char>
250struct std::formatter<ufo::AABB<Dim, T>> {
251 constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
252
253 auto format(ufo::AABB<Dim, T> const& aabb, std::format_context& ctx) const
254 {
255 return std::format_to(ctx.out(), "Min: [{}], Max: [{}]", aabb.min, aabb.max);
256 }
257};
258
259#endif // UFO_GEOMETRY_AABB_HPP
All vision-related classes and functions.
Definition cloud.hpp:49
constexpr bool any(Vec< Dim, bool > const &v) noexcept
Returns true if at least one component of a bool vector is true.
Definition vec.hpp:1560
constexpr T length(Vec< Dim, T > const &v) noexcept
Computes the Euclidean length (magnitude) of a vector.
Definition vec.hpp:1101
Axis-Aligned Bounding Box (AABB) in Dim-dimensional space.
Definition aabb.hpp:70
constexpr AABB() noexcept=default
Default constructor.
constexpr Vec< Dim, T > center() const noexcept
Returns the center of the AABB.
Definition aabb.hpp:137
Vec< Dim, T > min
The minimum point.
Definition aabb.hpp:76
constexpr AABB(Vec< Dim, T > const &center, T half_length) noexcept
Constructs an AABB from a center point and a half-length.
Definition aabb.hpp:105
constexpr T volume() const noexcept
Returns the volume of the AABB.
Definition aabb.hpp:167
constexpr AABB aabb() const noexcept
Returns the AABB of the AABB.
Definition aabb.hpp:181
constexpr bool isDegenerate() const noexcept
Returns whether the AABB is degenerate.
Definition aabb.hpp:187
constexpr T diagonal() const noexcept
Returns the diagonal length of the AABB.
Definition aabb.hpp:161
constexpr Vec< Dim, T > length() const noexcept
Returns the length (extent) of the AABB in each dimension.
Definition aabb.hpp:146
constexpr Vec< Dim, T > halfLength() const noexcept
Returns the half-length (extent) of the AABB in each dimension.
Definition aabb.hpp:152
constexpr AABB(AABB const &) noexcept=default
Copy constructor.
Vec< Dim, T > max
The maximum point.
Definition aabb.hpp:81
static constexpr std::size_t dimension() noexcept
Returns the dimensionality of the AABB.
Definition aabb.hpp:131
bool operator==(AABB const &) const =default
Equality operator.
constexpr AABB(AABB< Dim, U > const &other) noexcept
Converting constructor from an AABB with a different scalar type.
Definition aabb.hpp:122
A fixed-size arithmetic vector of up to 4 dimensions.
Definition vec.hpp:76