UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
cylinder.hpp
1
42#ifndef UFO_GEOMETRY_CYLINDER_HPP
43#define UFO_GEOMETRY_CYLINDER_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 Cylinder {
68 using value_type = T;
69
74
79
83 T radius{};
84
88 constexpr Cylinder() noexcept = default;
89
96 constexpr Cylinder(Vec<Dim, T> start, Vec<Dim, T> end, T radius) noexcept
98 {
99 }
100
104 constexpr Cylinder(Cylinder const&) noexcept = default;
105
111 template <std::convertible_to<T> U>
112 constexpr explicit Cylinder(Cylinder<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 diameter() const noexcept
139 {
140 return std::max(radius * T(2), length());
141 }
142
148 [[nodiscard]] constexpr Vec<Dim, T> at(T t) const noexcept
149 {
150 return start + t * (end - start);
151 }
152
157 [[nodiscard]] constexpr T length() const noexcept { return ufo::length(end - start); }
158
163 [[nodiscard]] constexpr AABB<Dim, T> aabb() const noexcept
164 {
167 if (length < std::numeric_limits<T>::epsilon()) {
168 return AABB<Dim, T>(start, radius);
169 }
170 axis /= length;
171 Vec<Dim, T> min_ext;
172 Vec<Dim, T> max_ext;
173 for (std::size_t i = 0; i < Dim; ++i) {
174 T s = std::sqrt(std::max(T(0), T(1) - axis[i] * axis[i]));
175 min_ext[i] = std::min(start[i], end[i]) - radius * s;
176 max_ext[i] = std::max(start[i], end[i]) + radius * s;
177 }
178 return AABB<Dim, T>(min_ext, max_ext);
179 }
180
185 [[nodiscard]] constexpr bool isDegenerate() const noexcept { return radius < T(0); }
186
191 [[nodiscard]] constexpr T volume() const noexcept
192 {
193 if constexpr (1 == Dim) {
194 return length();
195 } else if constexpr (2 == Dim) {
196 return radius * T(2) * length();
197 } else if constexpr (3 == Dim) {
198 return std::numbers::pi_v<T> * radius * radius * length();
199 } else {
200 // Higher dimensions would need the general n-cylinder volume formula
201 return T(0);
202 }
203 }
204
211 [[nodiscard]] bool operator==(Cylinder const&) const = default;
212};
213
214//
215// Deduction guides
216//
217
218template <std::size_t Dim, std::floating_point T>
220
227template <std::size_t Dim, std::floating_point T>
228std::ostream& operator<<(std::ostream& out, Cylinder<Dim, T> const& cylinder)
229{
230 return out << "Start: [" << cylinder.start << "], End: [" << cylinder.end
231 << "], Radius: " << cylinder.radius;
232}
233
234template <std::floating_point T>
235using Cylinder1 = Cylinder<1, T>;
236template <std::floating_point T>
237using Cylinder2 = Cylinder<2, T>;
238template <std::floating_point T>
239using Cylinder3 = Cylinder<3, T>;
240template <std::floating_point T>
241using Cylinder4 = Cylinder<4, T>;
242
243using Cylinder1f = Cylinder<1, float>;
244using Cylinder2f = Cylinder<2, float>;
245using Cylinder3f = Cylinder<3, float>;
246using Cylinder4f = Cylinder<4, float>;
247
248using Cylinder1d = Cylinder<1, double>;
249using Cylinder2d = Cylinder<2, double>;
250using Cylinder3d = Cylinder<3, double>;
251using Cylinder4d = Cylinder<4, double>;
252
253} // namespace ufo
254
255template <std::size_t Dim, std::floating_point T>
256 requires std::formattable<T, char>
257struct std::formatter<ufo::Cylinder<Dim, T>> {
258 constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
259
260 auto format(ufo::Cylinder<Dim, T> const& cylinder, std::format_context& ctx) const
261 {
262 return std::format_to(ctx.out(), "Start: [{}], End: [{}], Radius: {}", cylinder.start,
263 cylinder.end, cylinder.radius);
264 }
265};
266
267#endif // UFO_GEOMETRY_CYLINDER_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
Vec< 3, T > axis(Quat< T > const &q) noexcept
Extracts the unit rotation axis from a unit quaternion.
Definition quat.hpp:869
Axis-Aligned Bounding Box (AABB) in Dim-dimensional space.
Definition aabb.hpp:70
Cylinder in Dim-dimensional space.
Definition cylinder.hpp:67
constexpr Cylinder() noexcept=default
Default constructor.
constexpr AABB< Dim, T > aabb() const noexcept
Returns the AABB of the cylinder.
Definition cylinder.hpp:163
bool operator==(Cylinder const &) const =default
Equality operator.
constexpr T diameter() const noexcept
Returns the diameter of the cylinder.
Definition cylinder.hpp:138
constexpr bool isDegenerate() const noexcept
Returns whether the cylinder is degenerate.
Definition cylinder.hpp:185
T radius
The radius of the cylinder.
Definition cylinder.hpp:83
constexpr Cylinder(Cylinder const &) noexcept=default
Copy constructor.
constexpr T length() const noexcept
Returns the height (length) of the cylinder.
Definition cylinder.hpp:157
constexpr Cylinder(Cylinder< Dim, U > const &other) noexcept
Converting constructor from a cylinder with a different scalar type.
Definition cylinder.hpp:112
constexpr T volume() const noexcept
Returns the volume (or area/length) of the cylinder.
Definition cylinder.hpp:191
Vec< Dim, T > end
The end point of the second cap of the cylinder.
Definition cylinder.hpp:78
constexpr Vec< Dim, T > at(T t) const noexcept
Returns the point at parameter t along the cylinder axis.
Definition cylinder.hpp:148
constexpr Vec< Dim, T > center() const noexcept
Returns the center of the cylinder.
Definition cylinder.hpp:129
Vec< Dim, T > start
The center of the first cap of the cylinder.
Definition cylinder.hpp:73
static constexpr std::size_t dimension() noexcept
Returns the dimensionality of the cylinder.
Definition cylinder.hpp:123
A fixed-size arithmetic vector of up to 4 dimensions.
Definition vec.hpp:76