UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
line_segment.hpp
1
42#ifndef UFO_GEOMETRY_LINE_SEGMENT_HPP
43#define UFO_GEOMETRY_LINE_SEGMENT_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 <ostream>
54
55namespace ufo
56{
67template <std::size_t Dim = 3, std::floating_point T = float>
69 using value_type = T;
70
75
80
84 constexpr LineSegment() noexcept = default;
85
92 constexpr LineSegment(Vec<Dim, T> const& start, Vec<Dim, T> const& end) noexcept
93 : start(start), end(end)
94 {
95 }
96
100 constexpr LineSegment(LineSegment const&) noexcept = default;
101
108 template <std::convertible_to<T> U>
109 constexpr explicit LineSegment(LineSegment<Dim, U> const& other) noexcept
110 : start(Vec<Dim, T>(other.start)), end(Vec<Dim, T>(other.end))
111 {
112 }
113
118 [[nodiscard]] static constexpr std::size_t dimension() noexcept { return Dim; }
119
125 [[nodiscard]] constexpr T length() const noexcept { return norm(end - start); }
126
133 [[nodiscard]] constexpr Vec<Dim, T> at(T t) const noexcept
134 {
135 return start + t * (end - start);
136 }
137
142 [[nodiscard]] constexpr Vec<Dim, T> center() const noexcept
143 {
144 return (start + end) * T(0.5);
145 }
146
151 [[nodiscard]] constexpr T diameter() const noexcept { return length(); }
152
158 [[nodiscard]] constexpr bool isDegenerate(
159 T eps = std::numeric_limits<T>::epsilon()) const noexcept
160 {
161 return length() <= eps;
162 }
163
168 [[nodiscard]] constexpr AABB<Dim, T> aabb() const noexcept
169 {
171 }
172
177 [[nodiscard]] constexpr T volume() const noexcept { return T(0); }
178
182 [[nodiscard]] bool operator==(LineSegment const&) const = default;
183};
184
185//
186// Deduction guides
187//
188
189template <std::size_t Dim, std::floating_point T>
191
195template <std::size_t Dim, std::floating_point T>
196std::ostream& operator<<(std::ostream& out, LineSegment<Dim, T> const& ls)
197{
198 return out << "Start: [" << ls.start << "], End: [" << ls.end << "]";
199}
200
201template <std::floating_point T>
202using LineSegment1 = LineSegment<1, T>;
203template <std::floating_point T>
204using LineSegment2 = LineSegment<2, T>;
205template <std::floating_point T>
206using LineSegment3 = LineSegment<3, T>;
207template <std::floating_point T>
208using LineSegment4 = LineSegment<4, T>;
209
210using LineSegment1f = LineSegment<1, float>;
211using LineSegment2f = LineSegment<2, float>;
212using LineSegment3f = LineSegment<3, float>;
213using LineSegment4f = LineSegment<4, float>;
214
215using LineSegment1d = LineSegment<1, double>;
216using LineSegment2d = LineSegment<2, double>;
217using LineSegment3d = LineSegment<3, double>;
218using LineSegment4d = LineSegment<4, double>;
219
220} // namespace ufo
221
222template <std::size_t Dim, std::floating_point T>
223 requires std::formattable<T, char>
224struct std::formatter<ufo::LineSegment<Dim, T>> {
225 constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
226
227 auto format(ufo::LineSegment<Dim, T> const& ls, std::format_context& ctx) const
228 {
229 return std::format_to(ctx.out(), "Start: [{}], End: [{}]", ls.start, ls.end);
230 }
231};
232
233#endif // UFO_GEOMETRY_LINE_SEGMENT_HPP
All vision-related classes and functions.
Definition cloud.hpp:49
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 T norm(Quat< T > const &q) noexcept
Returns the Euclidean norm sqrt(w² + x² + y² + z²).
Definition quat.hpp:667
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
Line segment in Dim-dimensional space.
constexpr LineSegment(LineSegment const &) noexcept=default
Copy constructor.
Vec< Dim, T > start
The start point of the line segment.
constexpr T diameter() const noexcept
Returns the diameter of the line segment.
constexpr T volume() const noexcept
Returns the volume of the line segment.
constexpr LineSegment() noexcept=default
Default constructor.
constexpr AABB< Dim, T > aabb() const noexcept
Returns the AABB of the line segment.
bool operator==(LineSegment const &) const =default
Equality operator.
Vec< Dim, T > end
The end point of the line segment.
constexpr T length() const noexcept
Returns the length of the line segment.
constexpr LineSegment(LineSegment< Dim, U > const &other) noexcept
Converting constructor from a line segment with a different scalar type.
static constexpr std::size_t dimension() noexcept
Returns the dimensionality of the line segment.
constexpr Vec< Dim, T > center() const noexcept
Returns the center of the line segment.
constexpr Vec< Dim, T > at(T t) const noexcept
Returns the point at parameter t along the line segment.
constexpr bool isDegenerate(T eps=std::numeric_limits< T >::epsilon()) const noexcept
Returns whether the line segment is degenerate.
A fixed-size arithmetic vector of up to 4 dimensions.
Definition vec.hpp:76