UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
capsule.hpp
1
42#ifndef UFO_GEOMETRY_CAPSULE_HPP
43#define UFO_GEOMETRY_CAPSULE_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 <numbers>
54#include <ostream>
55
56namespace ufo
57{
66template <std::size_t Dim = 3, std::floating_point T = float>
67struct Capsule {
68 using value_type = T;
69
74
79
83 T radius{};
84
88 constexpr Capsule() noexcept = default;
89
96 constexpr Capsule(Vec<Dim, T> const& start, Vec<Dim, T> const& end, T radius) noexcept
98 {
99 }
100
104 constexpr Capsule(Capsule const&) noexcept = default;
105
111 template <std::convertible_to<T> U>
112 constexpr explicit Capsule(Capsule<Dim, U> const& other) noexcept
113 : start(Vec<Dim, T>(other.start))
114 , end(Vec<Dim, T>(other.end))
115 , radius(static_cast<T>(other.radius))
116 {
117 }
118
123 [[nodiscard]] static constexpr std::size_t dimension() noexcept { return Dim; }
124
129 [[nodiscard]] constexpr Vec<Dim, T> center() const noexcept
130 {
131 return (start + end) * T(0.5);
132 }
133
138 [[nodiscard]] constexpr T length() const noexcept { return ufo::length(end - start); }
139
144 [[nodiscard]] constexpr AABB<Dim, T> aabb() const noexcept
145 {
147 }
148
153 [[nodiscard]] constexpr bool isDegenerate() const noexcept { return radius < T(0); }
154
159 [[nodiscard]] constexpr T diameter() const noexcept { return radius * T(2); }
160
165 [[nodiscard]] constexpr T volume() const noexcept
166 {
167 T h = length();
168 if constexpr (1 == Dim) {
169 return h + diameter();
170 } else if constexpr (2 == Dim) {
171 return diameter() * h + std::numbers::pi_v<T> * radius * radius;
172 } else if constexpr (3 == Dim) {
173 T r2 = radius * radius;
174 return std::numbers::pi_v<T> * r2 * h +
175 (T(4) / T(3)) * std::numbers::pi_v<T> * r2 * radius;
176 } else {
177 // TODO: Add 4D if requested
178 return T(0);
179 }
180 }
181
185 [[nodiscard]] bool operator==(Capsule const&) const = default;
186};
187
188//
189// Deduction guides
190//
191
192template <std::size_t Dim, std::floating_point T>
194
198template <std::size_t Dim, std::floating_point T>
199std::ostream& operator<<(std::ostream& out, Capsule<Dim, T> const& capsule)
200{
201 return out << "Start: [" << capsule.start << "], End: [" << capsule.end
202 << "], Radius: " << capsule.radius;
203}
204
205template <std::floating_point T>
206using Capsule1 = Capsule<1, T>;
207template <std::floating_point T>
208using Capsule2 = Capsule<2, T>;
209template <std::floating_point T>
210using Capsule3 = Capsule<3, T>;
211template <std::floating_point T>
212using Capsule4 = Capsule<4, T>;
213
214using Capsule1f = Capsule<1, float>;
215using Capsule2f = Capsule<2, float>;
216using Capsule3f = Capsule<3, float>;
217using Capsule4f = Capsule<4, float>;
218
219using Capsule1d = Capsule<1, double>;
220using Capsule2d = Capsule<2, double>;
221using Capsule3d = Capsule<3, double>;
222using Capsule4d = Capsule<4, double>;
223
224} // namespace ufo
225
226template <std::size_t Dim, std::floating_point T>
227 requires std::formattable<T, char>
228struct std::formatter<ufo::Capsule<Dim, T>> {
229 constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
230
231 auto format(ufo::Capsule<Dim, T> const& capsule, std::format_context& ctx) const
232 {
233 return std::format_to(ctx.out(), "Start: [{}], End: [{}], Radius: {}", capsule.start,
234 capsule.end, capsule.radius);
235 }
236};
237
238#endif // UFO_GEOMETRY_CAPSULE_HPP
All vision-related classes and functions.
Definition cloud.hpp:49
constexpr T length(Vec< Dim, T > const &v) noexcept
Computes the Euclidean length (magnitude) of a vector.
Definition vec.hpp:1101
constexpr Vec< Geometry::dimension(), typename Geometry::value_type > max(Geometry const &g)
Returns the maximum coordinate of the minimum spanning axis-aligned bounding box of a geometry.
Definition fun.hpp:72
constexpr Vec< Geometry::dimension(), typename Geometry::value_type > min(Geometry const &g)
Returns the minimum coordinate of the minimum spanning axis-aligned bounding box of a geometry.
Definition fun.hpp:58
Axis-Aligned Bounding Box (AABB) in Dim-dimensional space.
Definition aabb.hpp:70
Capsule in Dim-dimensional space.
Definition capsule.hpp:67
static constexpr std::size_t dimension() noexcept
Returns the dimensionality of the capsule.
Definition capsule.hpp:123
constexpr T length() const noexcept
Returns the length of the segment between start and end.
Definition capsule.hpp:138
Vec< Dim, T > end
The end point of the capsule segment.
Definition capsule.hpp:78
Vec< Dim, T > start
The start point of the capsule segment.
Definition capsule.hpp:73
constexpr Capsule(Capsule< Dim, U > const &other) noexcept
Converting constructor from a capsule with a different scalar type.
Definition capsule.hpp:112
bool operator==(Capsule const &) const =default
Equality operator.
constexpr bool isDegenerate() const noexcept
Returns whether the capsule is degenerate.
Definition capsule.hpp:153
constexpr T diameter() const noexcept
Returns the diameter of the capsule.
Definition capsule.hpp:159
constexpr AABB< Dim, T > aabb() const noexcept
Returns the AABB of the capsule.
Definition capsule.hpp:144
T radius
The radius of the capsule.
Definition capsule.hpp:83
constexpr T volume() const noexcept
Returns the volume (or area/length) of the capsule.
Definition capsule.hpp:165
constexpr Capsule(Capsule const &) noexcept=default
Copy constructor.
constexpr Vec< Dim, T > center() const noexcept
Returns the center of the capsule.
Definition capsule.hpp:129
constexpr Capsule() noexcept=default
Default constructor.
A fixed-size arithmetic vector of up to 4 dimensions.
Definition vec.hpp:76