UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
ray.hpp
1
42#ifndef UFO_GEOMETRY_RAY_HPP
43#define UFO_GEOMETRY_RAY_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{
65template <std::size_t Dim = 3, std::floating_point T = float>
66struct Ray {
67 using value_type = T;
68
73
80
84 constexpr Ray() noexcept = default;
85
89 constexpr Ray(Ray const&) noexcept = default;
90
98 constexpr Ray(Vec<Dim, T> origin, Vec<Dim, T> direction) noexcept
100 {
101 }
102
108 template <std::convertible_to<T> U>
109 constexpr explicit Ray(Ray<Dim, U> const& other) noexcept
110 : origin(other.origin), direction(other.direction)
111 {
112 }
113
118 [[nodiscard]] static constexpr std::size_t dimension() noexcept { return Dim; }
119
125 [[nodiscard]] constexpr Vec<Dim, T> at(T t) const { return origin + t * direction; }
126
130 [[nodiscard]] bool operator==(Ray const&) const = default;
131
136 [[nodiscard]] constexpr bool isDegenerate() const noexcept
137 {
138 return dot(direction, direction) < std::numeric_limits<T>::epsilon();
139 }
140
145 [[nodiscard]] constexpr T volume() const noexcept { return T(0); }
146
151 [[nodiscard]] constexpr T diameter() const noexcept
152 {
153 return std::numeric_limits<T>::infinity();
154 }
155
160 [[nodiscard]] constexpr AABB<Dim, T> aabb() const noexcept
161 {
164 for (std::size_t i = 0; i < Dim; ++i) {
165 if (direction[i] > T(0)) {
166 max[i] = std::numeric_limits<T>::infinity();
167 } else if (direction[i] < T(0)) {
168 min[i] = -std::numeric_limits<T>::infinity();
169 }
170 }
171 return AABB<Dim, T>(min, max);
172 }
173};
174
175//
176// Deduction guides
177//
178
179template <std::size_t Dim, std::floating_point T>
180Ray(Vec<Dim, T>, Vec<Dim, T>) -> Ray<Dim, T>;
181
185template <std::size_t Dim, std::floating_point T>
186std::ostream& operator<<(std::ostream& out, Ray<Dim, T> const& ray)
187{
188 return out << "Origin: [" << ray.origin << "], Direction: [" << ray.direction << "]";
189}
190
191template <std::floating_point T>
192using Ray1 = Ray<1, T>;
193template <std::floating_point T>
194using Ray2 = Ray<2, T>;
195template <std::floating_point T>
196using Ray3 = Ray<3, T>;
197template <std::floating_point T>
198using Ray4 = Ray<4, T>;
199
200using Ray1f = Ray<1, float>;
201using Ray2f = Ray<2, float>;
202using Ray3f = Ray<3, float>;
203using Ray4f = Ray<4, float>;
204
205using Ray1d = Ray<1, double>;
206using Ray2d = Ray<2, double>;
207using Ray3d = Ray<3, double>;
208using Ray4d = Ray<4, double>;
209
210} // namespace ufo
211
212template <std::size_t Dim, std::floating_point T>
213 requires std::formattable<T, char>
214struct std::formatter<ufo::Ray<Dim, T>> {
215 constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
216
217 auto format(ufo::Ray<Dim, T> const& ray, std::format_context& ctx) const
218 {
219 return std::format_to(ctx.out(), "Origin: [{}], Direction: [{}]", ray.origin,
220 ray.direction);
221 }
222};
223
224#endif // UFO_GEOMETRY_RAY_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 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
Ray in Dim-dimensional space.
Definition ray.hpp:66
static constexpr std::size_t dimension() noexcept
Returns the dimensionality of the ray.
Definition ray.hpp:118
constexpr AABB< Dim, T > aabb() const noexcept
Returns the AABB of the ray.
Definition ray.hpp:160
constexpr Ray() noexcept=default
Default constructor.
constexpr T volume() const noexcept
Returns the volume of the ray.
Definition ray.hpp:145
constexpr Ray(Ray< Dim, U > const &other) noexcept
Converting constructor from a ray with a different scalar type.
Definition ray.hpp:109
bool operator==(Ray const &) const =default
Equality operator.
Vec< Dim, T > direction
The direction of the ray.
Definition ray.hpp:79
constexpr T diameter() const noexcept
Returns the diameter of the ray.
Definition ray.hpp:151
constexpr bool isDegenerate() const noexcept
Returns whether the ray is degenerate.
Definition ray.hpp:136
constexpr Vec< Dim, T > at(T t) const
Returns the point at distance t along the ray.
Definition ray.hpp:125
Vec< Dim, T > origin
The origin of the ray.
Definition ray.hpp:72
A fixed-size arithmetic vector of up to 4 dimensions.
Definition vec.hpp:76