UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
triangle.hpp
1
42#ifndef UFO_GEOMETRY_TRIANGLE_HPP
43#define UFO_GEOMETRY_TRIANGLE_HPP
44
45// UFO
46#include <ufo/geometry/aabb.hpp>
47#include <ufo/numeric/vec.hpp>
48
49// STL
50#include <algorithm>
51#include <array>
52#include <cmath>
53#include <concepts>
54#include <cstddef>
55#include <format>
56#include <limits>
57#include <ostream>
58
59namespace ufo
60{
69template <std::size_t Dim = 3, std::floating_point T = float>
70struct Triangle {
71 using value_type = T;
72
76 std::array<Vec<Dim, T>, 3> points;
77
81 constexpr Triangle() noexcept = default;
82
86 constexpr Triangle(Triangle const&) noexcept = default;
87
94 constexpr Triangle(Vec<Dim, T> point_1, Vec<Dim, T> point_2,
95 Vec<Dim, T> point_3) noexcept
96 : points{point_1, point_2, point_3}
97 {
98 }
99
105 template <std::convertible_to<T> U>
106 constexpr explicit Triangle(Triangle<Dim, U> const& other) noexcept
107 : points{Vec<Dim, T>(other[0]), Vec<Dim, T>(other[1]), Vec<Dim, T>(other[2])}
108 {
109 }
110
115 [[nodiscard]] static constexpr std::size_t dimension() noexcept { return Dim; }
116
122 [[nodiscard]] constexpr auto& operator[](this auto& self, std::size_t pos) noexcept
123 {
124 return self.points[pos];
125 }
126
131 [[nodiscard]] constexpr Vec<Dim, T> center() const noexcept
132 {
133 return (points[0] + points[1] + points[2]) / T(3);
134 }
135
142 [[nodiscard]] constexpr T area() const noexcept
143 {
144 Vec<Dim, T> v1 = points[1] - points[0];
145 Vec<Dim, T> v2 = points[2] - points[0];
146 T d1 = dot(v1, v1);
147 T d2 = dot(v2, v2);
148 T d12 = dot(v1, v2);
149 return T(0.5) * std::sqrt(std::max(T(0), d1 * d2 - d12 * d12));
150 }
151
156 [[nodiscard]] constexpr AABB<Dim, T> aabb() const noexcept
157 {
158 return AABB<Dim, T>(ufo::min(points[0], ufo::min(points[1], points[2])),
159 ufo::max(points[0], ufo::max(points[1], points[2])));
160 }
161
168 [[nodiscard]] constexpr Vec<3, T> normal() const noexcept
169 requires(3 == Dim)
170 {
171 return normalize(cross(points[1] - points[0], points[2] - points[0]));
172 }
173
181 [[nodiscard]] constexpr bool isDegenerate(
182 T eps = std::numeric_limits<T>::epsilon()) const noexcept
183 {
184 return area() <= eps;
185 }
186
191 [[nodiscard]] constexpr T volume() const noexcept { return T(0); }
192
197 [[nodiscard]] constexpr T diameter() const noexcept
198 {
199 return std::sqrt(
200 std::max({normSquared(points[1] - points[0]), normSquared(points[2] - points[1]),
201 normSquared(points[0] - points[2])}));
202 }
203
207 [[nodiscard]] bool operator==(Triangle const&) const = default;
208};
209
210//
211// Deduction guides
212//
213
214template <std::size_t Dim, std::floating_point T>
216
220template <std::size_t Dim, std::floating_point T>
221std::ostream& operator<<(std::ostream& out, Triangle<Dim, T> const& triangle)
222{
223 return out << "Point 1: [" << triangle[0] << "], Point 2: [" << triangle[1]
224 << "], Point 3: [" << triangle[2] << "]";
225}
226
227template <std::floating_point T>
228using Triangle1 = Triangle<1, T>;
229template <std::floating_point T>
230using Triangle2 = Triangle<2, T>;
231template <std::floating_point T>
232using Triangle3 = Triangle<3, T>;
233template <std::floating_point T>
234using Triangle4 = Triangle<4, T>;
235
236using Triangle1f = Triangle<1, float>;
237using Triangle2f = Triangle<2, float>;
238using Triangle3f = Triangle<3, float>;
239using Triangle4f = Triangle<4, float>;
240
241using Triangle1d = Triangle<1, double>;
242using Triangle2d = Triangle<2, double>;
243using Triangle3d = Triangle<3, double>;
244using Triangle4d = Triangle<4, double>;
245
246} // namespace ufo
247
248template <std::size_t Dim, std::floating_point T>
249 requires std::formattable<T, char>
250struct std::formatter<ufo::Triangle<Dim, T>> {
251 constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
252
253 auto format(ufo::Triangle<Dim, T> const& triangle, std::format_context& ctx) const
254 {
255 return std::format_to(ctx.out(), "Points: [{}], [{}], [{}]", triangle[0], triangle[1],
256 triangle[2]);
257 }
258};
259
260#endif // UFO_GEOMETRY_TRIANGLE_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 T normSquared(Quat< T > const &q) noexcept
Returns the squared norm (dot product with itself).
Definition quat.hpp:655
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 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
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
Triangle in Dim-dimensional space.
Definition triangle.hpp:70
std::array< Vec< Dim, T >, 3 > points
The vertices of the triangle.
Definition triangle.hpp:76
bool operator==(Triangle const &) const =default
Equality operator.
constexpr T diameter() const noexcept
Returns the diameter of the triangle.
Definition triangle.hpp:197
constexpr Triangle(Triangle< Dim, U > const &other) noexcept
Converting constructor from a triangle with a different scalar type.
Definition triangle.hpp:106
constexpr Triangle() noexcept=default
Default constructor.
constexpr T volume() const noexcept
Returns the volume of the triangle.
Definition triangle.hpp:191
constexpr bool isDegenerate(T eps=std::numeric_limits< T >::epsilon()) const noexcept
Returns true if the triangle is degenerate.
Definition triangle.hpp:181
constexpr T area() const noexcept
Returns the area of the triangle.
Definition triangle.hpp:142
constexpr AABB< Dim, T > aabb() const noexcept
Returns the AABB of the triangle.
Definition triangle.hpp:156
constexpr Vec< Dim, T > center() const noexcept
Returns the centroid (arithmetic mean) of the triangle.
Definition triangle.hpp:131
static constexpr std::size_t dimension() noexcept
Returns the dimensionality of the triangle.
Definition triangle.hpp:115
constexpr Vec< 3, T > normal() const noexcept
Returns the surface unit normal of the triangle.
Definition triangle.hpp:168
constexpr auto & operator[](this auto &self, std::size_t pos) noexcept
Returns the vertex at position pos.
Definition triangle.hpp:122
A fixed-size arithmetic vector of up to 4 dimensions.
Definition vec.hpp:76