UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
plane.hpp
1
42#ifndef UFO_GEOMETRY_PLANE_HPP
43#define UFO_GEOMETRY_PLANE_HPP
44
45// UFO
46#include <ufo/numeric/vec.hpp>
47
48// STL
49#include <concepts>
50#include <cstddef>
51#include <limits>
52#include <ostream>
53
54#include "ufo/geometry/aabb.hpp"
55
56namespace ufo
57{
68template <std::size_t Dim = 3, std::floating_point T = float>
69struct Plane {
70 using value_type = T;
71
76
81
85 constexpr Plane() noexcept = default;
86
90 constexpr Plane(Plane const&) noexcept = default;
91
96 constexpr explicit Plane(Vec<Dim, T> const& normal) noexcept : normal(normal) {}
97
103 constexpr Plane(Vec<Dim, T> const& normal, T distance) noexcept
105 {
106 }
107
113 constexpr Plane(Vec<Dim, T> const& v_1, Vec<Dim, T> const& v_2) noexcept
114 requires(2 == Dim)
115 {
116 auto aux = v_2 - v_1;
117 normal = normalize(Vec<2, T>(-aux.y(), aux.x()));
118 distance = dot(normal, v_1);
119 }
120
127 constexpr Plane(Vec<Dim, T> const& v_1, Vec<Dim, T> const& v_2,
128 Vec<Dim, T> const& v_3) noexcept
129 requires(3 == Dim)
130 {
131 auto aux_1 = v_1 - v_2;
132 auto aux_2 = v_3 - v_2;
133 normal = normalize(cross(aux_2, aux_1));
134 distance = dot(normal, v_2);
135 }
136
140 template <std::convertible_to<T> U>
141 constexpr explicit Plane(Plane<Dim, U> const& other) noexcept
142 : normal(Vec<Dim, T>(other.normal)), distance(static_cast<T>(other.distance))
143 {
144 }
145
150 [[nodiscard]] static constexpr std::size_t dimension() noexcept { return Dim; }
151
158 [[nodiscard]] bool operator==(Plane const&) const = default;
159
164 [[nodiscard]] constexpr bool isDegenerate() const noexcept
165 {
166 return dot(normal, normal) < std::numeric_limits<T>::epsilon();
167 }
168
173 [[nodiscard]] constexpr T volume() const noexcept { return T(0); }
174
179 [[nodiscard]] constexpr T diameter() const noexcept
180 {
181 return std::numeric_limits<T>::infinity();
182 }
183
188 [[nodiscard]] constexpr AABB<Dim, T> aabb() const noexcept
189 {
190 // A plane is infinite
191 return AABB<Dim, T>(Vec<Dim, T>(-std::numeric_limits<T>::infinity()),
192 Vec<Dim, T>(std::numeric_limits<T>::infinity()));
193 }
194};
195
199template <std::size_t Dim, std::floating_point T>
200std::ostream& operator<<(std::ostream& out, Plane<Dim, T> const& plane)
201{
202 return out << "Normal: [" << plane.normal << "], Distance: " << plane.distance;
203}
204
205using Plane3f = Plane<3, float>;
206using Plane3d = Plane<3, double>;
207
208} // namespace ufo
209
210template <std::size_t Dim, std::floating_point T>
211 requires std::formattable<T, char>
212struct std::formatter<ufo::Plane<Dim, T>> {
213 constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
214
215 auto format(ufo::Plane<Dim, T> const& plane, std::format_context& ctx) const
216 {
217 return std::format_to(ctx.out(), "Normal: [{}], Distance: {}", plane.normal,
218 plane.distance);
219 }
220};
221
222#endif // UFO_GEOMETRY_PLANE_HPP
constexpr T dot(Quat< T > const &a, Quat< T > const &b) noexcept
Computes the four-component dot product a.w*b.w + a.x*b.x + a.y*b.y + a.z*b.z.
Definition quat.hpp:643
All vision-related classes and functions.
Definition cloud.hpp:49
constexpr Quat< T > normalize(Quat< T > const &q) noexcept
Returns a unit quaternion in the same direction as q.
Definition quat.hpp:679
constexpr Quat< T > cross(Quat< T > const &q1, Quat< T > const &q2) noexcept
Computes the Hamilton cross product of two quaternions.
Definition quat.hpp:721
Axis-Aligned Bounding Box (AABB) in Dim-dimensional space.
Definition aabb.hpp:70
Plane in space.
Definition plane.hpp:69
bool operator==(Plane const &) const =default
Equality operator.
constexpr T diameter() const noexcept
Returns the diameter of the plane.
Definition plane.hpp:179
Vec< Dim, T > normal
The normal of the plane.
Definition plane.hpp:75
constexpr bool isDegenerate() const noexcept
Returns whether the plane is degenerate.
Definition plane.hpp:164
T distance
The distance from the origin.
Definition plane.hpp:80
constexpr Plane(Plane< Dim, U > const &other) noexcept
Conversion operator to a plane with a different scalar type.
Definition plane.hpp:141
constexpr Plane(Vec< Dim, T > const &v_1, Vec< Dim, T > const &v_2) noexcept
Constructs a plane from two points.
Definition plane.hpp:113
constexpr T volume() const noexcept
Returns the volume of the plane.
Definition plane.hpp:173
static constexpr std::size_t dimension() noexcept
Returns the dimensionality of the plane.
Definition plane.hpp:150
constexpr Plane(Vec< Dim, T > const &v_1, Vec< Dim, T > const &v_2, Vec< Dim, T > const &v_3) noexcept
Constructs a plane from three points.
Definition plane.hpp:127
constexpr AABB< Dim, T > aabb() const noexcept
Returns the AABB of the plane.
Definition plane.hpp:188
constexpr Plane() noexcept=default
Default constructor.
constexpr Plane(Vec< Dim, T > const &normal, T distance) noexcept
Constructs a plane from a normal and a distance.
Definition plane.hpp:103
A fixed-size arithmetic vector of up to 4 dimensions.
Definition vec.hpp:76