UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
sphere.hpp
1
42#ifndef UFO_GEOMETRY_SPHERE_HPP
43#define UFO_GEOMETRY_SPHERE_HPP
44
45// UFO
46#include <ufo/geometry/aabb.hpp>
47#include <ufo/numeric/vec.hpp>
48
49// STL
50#include <concepts>
51#include <cstddef>
52#include <format>
53#include <iostream>
54#include <numbers>
55
56namespace ufo
57{
66template <std::size_t Dim = 3, std::floating_point T = float>
67struct Sphere {
68 using value_type = T;
69
74
79
83 constexpr Sphere() noexcept = default;
84
90 constexpr Sphere(Vec<Dim, T> const& center, T radius) noexcept
92 {
93 }
94
98 constexpr Sphere(Sphere const&) noexcept = default;
99
105 template <std::convertible_to<T> U>
106 constexpr explicit Sphere(Sphere<Dim, U> const& other) noexcept
107 : center_(Vec<Dim, T>(other.center())), radius(static_cast<T>(other.radius))
108 {
109 }
110
115 [[nodiscard]] static constexpr std::size_t dimension() noexcept { return Dim; }
116
121 [[nodiscard]] constexpr Vec<Dim, T>& center() noexcept { return center_; }
122
127 [[nodiscard]] constexpr Vec<Dim, T> const& center() const noexcept { return center_; }
128
133 [[nodiscard]] constexpr T diameter() const noexcept { return radius * T(2); }
134
139 [[nodiscard]] constexpr AABB<Dim, T> aabb() const noexcept
140 {
141 return AABB<Dim, T>(center_, radius);
142 }
143
148 [[nodiscard]] constexpr bool isDegenerate() const noexcept { return radius < T(0); }
149
157 [[nodiscard]] constexpr T volume() const noexcept
158 {
159 if constexpr (1 == Dim) {
160 return diameter();
161 } else if constexpr (2 == Dim) {
162 return std::numbers::pi_v<T> * radius * radius;
163 } else if constexpr (3 == Dim) {
164 return (T(4) / T(3)) * std::numbers::pi_v<T> * radius * radius * radius;
165 } else if constexpr (4 == Dim) {
166 return (std::numbers::pi_v<T> * std::numbers::pi_v<T> * radius * radius * radius *
167 radius) /
168 T(2);
169 } else {
170 return T(0);
171 }
172 }
173
177 [[nodiscard]] bool operator==(Sphere const&) const = default;
178};
179
180//
181// Deduction guides
182//
183
184template <std::size_t Dim, std::floating_point T>
186
190template <std::size_t Dim, std::floating_point T>
191std::ostream& operator<<(std::ostream& out, Sphere<Dim, T> const& sphere)
192{
193 return out << "Center: [" << sphere.center() << "], Radius: " << sphere.radius;
194}
195
196template <std::floating_point T>
197using Sphere1 = Sphere<1, T>;
198template <std::floating_point T>
199using Sphere2 = Sphere<2, T>;
200template <std::floating_point T>
201using Sphere3 = Sphere<3, T>;
202template <std::floating_point T>
203using Sphere4 = Sphere<4, T>;
204
205using Sphere1f = Sphere<1, float>;
206using Sphere2f = Sphere<2, float>;
207using Sphere3f = Sphere<3, float>;
208using Sphere4f = Sphere<4, float>;
209
210using Sphere1d = Sphere<1, double>;
211using Sphere2d = Sphere<2, double>;
212using Sphere3d = Sphere<3, double>;
213using Sphere4d = Sphere<4, double>;
214
215} // namespace ufo
216
217template <std::size_t Dim, std::floating_point T>
218 requires std::formattable<T, char>
219struct std::formatter<ufo::Sphere<Dim, T>> {
220 constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
221
222 auto format(ufo::Sphere<Dim, T> const& sphere, std::format_context& ctx) const
223 {
224 return std::format_to(ctx.out(), "Center: [{}], Radius: {}", sphere.center(),
225 sphere.radius);
226 }
227};
228
229#endif // UFO_GEOMETRY_SPHERE_HPP
All vision-related classes and functions.
Definition cloud.hpp:49
Axis-Aligned Bounding Box (AABB) in Dim-dimensional space.
Definition aabb.hpp:70
Sphere in Dim-dimensional space.
Definition sphere.hpp:67
constexpr bool isDegenerate() const noexcept
Returns whether the sphere is degenerate.
Definition sphere.hpp:148
constexpr Vec< Dim, T > & center() noexcept
Returns the center of the sphere.
Definition sphere.hpp:121
static constexpr std::size_t dimension() noexcept
Returns the dimensionality of the sphere.
Definition sphere.hpp:115
constexpr Sphere(Sphere< Dim, U > const &other) noexcept
Converting constructor from a sphere with a different scalar type.
Definition sphere.hpp:106
constexpr Vec< Dim, T > const & center() const noexcept
Returns the center of the sphere.
Definition sphere.hpp:127
constexpr T diameter() const noexcept
Returns the diameter of the sphere.
Definition sphere.hpp:133
constexpr Sphere() noexcept=default
Default constructor.
constexpr T volume() const noexcept
Returns the volume of the sphere.
Definition sphere.hpp:157
constexpr Sphere(Sphere const &) noexcept=default
Copy constructor.
T radius
The radius of the sphere.
Definition sphere.hpp:78
constexpr AABB< Dim, T > aabb() const noexcept
Returns the AABB of the sphere.
Definition sphere.hpp:139
Vec< Dim, T > center_
The center of the sphere.
Definition sphere.hpp:73
bool operator==(Sphere const &) const =default
Equality operator.
A fixed-size arithmetic vector of up to 4 dimensions.
Definition vec.hpp:76