42#ifndef UFO_GEOMETRY_LINE_HPP
43#define UFO_GEOMETRY_LINE_HPP
46#include <ufo/geometry/aabb.hpp>
47#include <ufo/numeric/vec.hpp>
58template <std::
size_t Dim = 3, std::
floating_po
int T =
float>
75 constexpr Line() noexcept = default;
80 constexpr
Line(
Line const&) noexcept = default;
103 return Line(v_1, v_2 - v_1);
112 template <std::convertible_to<T> U>
122 [[nodiscard]]
static constexpr std::size_t
dimension() noexcept {
return Dim; }
158 [[nodiscard]]
constexpr T
diameter() const noexcept
160 return std::numeric_limits<T>::infinity();
167 [[nodiscard]]
constexpr T
volume() const noexcept {
return T(0); }
190template <std::
size_t Dim, std::
floating_po
int T>
194 if constexpr (2 == Dim) {
195 T det =
a.direction.
x() *
b.direction.y() -
a.direction.y() *
b.direction.x();
196 if (std::abs(det) < std::numeric_limits<T>::epsilon()) {
197 return Vec<Dim, T>(std::numeric_limits<T>::infinity());
199 T t = ((
b.origin.x() -
a.origin.x()) *
b.direction.y() -
200 (
b.origin.y() -
a.origin.y()) *
b.direction.x()) /
202 return a.origin + t *
a.direction;
203 }
else if constexpr (3 == Dim) {
207 T ab_dot =
dot(
a.direction,
b.direction);
208 T aw_dot =
dot(
a.direction, w0);
209 T bw_dot =
dot(
b.direction, w0);
210 T denom = a_dot * b_dot - ab_dot * ab_dot;
211 if (std::abs(denom) < std::numeric_limits<T>::epsilon()) {
212 return Vec<Dim, T>(std::numeric_limits<T>::infinity());
214 T sc = (ab_dot * bw_dot - b_dot * aw_dot) / denom;
215 return a.origin + sc *
a.direction;
218 return Vec<Dim, T>(std::numeric_limits<T>::quiet_NaN());
225template <std::
size_t Dim, std::
floating_po
int T>
230 T d =
dot(lhs.direction, rhs.direction);
231 if (std::abs(std::abs(d) - T(1)) > std::numeric_limits<T>::epsilon()) {
235 if (
normSquared(diff) < std::numeric_limits<T>::epsilon()) {
239 return std::abs(std::abs(
dot(diff, lhs.direction)) - T(1)) <
240 std::numeric_limits<T>::epsilon();
246template <std::
size_t Dim, std::
floating_po
int T>
249 return out <<
"Origin: [" << line.
origin <<
"], Direction: [" << line.
direction <<
"]";
252template <std::
floating_po
int T>
253using Line1 = Line<1, T>;
254template <std::
floating_po
int T>
255using Line2 = Line<2, T>;
256template <std::
floating_po
int T>
257using Line3 = Line<3, T>;
258template <std::
floating_po
int T>
259using Line4 = Line<4, T>;
261using Line1f = Line<1, float>;
262using Line2f = Line<2, float>;
263using Line3f = Line<3, float>;
264using Line4f = Line<4, float>;
266using Line1d = Line<1, double>;
267using Line2d = Line<2, double>;
268using Line3d = Line<3, double>;
269using Line4d = Line<4, double>;
273template <std::
size_t Dim, std::
floating_po
int T>
274 requires std::formattable<T, char>
275struct std::formatter<
ufo::Line<Dim, T>> {
276 constexpr auto parse(std::format_parse_context& ctx) {
return ctx.begin(); }
280 return std::format_to(ctx.out(),
"Origin: [{}], Direction: [{}]", line.
origin,
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.
All vision-related classes and functions.
constexpr T normSquared(Quat< T > const &q) noexcept
Returns the squared norm (dot product with itself).
constexpr T b(Lab< T, Flags > color) noexcept
Returns the un-weighted blue–yellow axis value.
constexpr T a(Lab< T, Flags > color) noexcept
Returns the un-weighted green–red axis value.
constexpr Vec< Dim, T > intersectionPoint(Line< Dim, T > const &a, Line< Dim, T > const &b)
Computes the intersection point of two lines.
constexpr Quat< T > normalize(Quat< T > const &q) noexcept
Returns a unit quaternion in the same direction as q.
Axis-Aligned Bounding Box (AABB) in Dim-dimensional space.
constexpr Vec< Dim, T > center() const noexcept
Returns the center of the line.
constexpr Line() noexcept=default
Default constructor.
static constexpr Line fromPoints(Vec< Dim, T > const &v_1, Vec< Dim, T > const &v_2) noexcept
Constructs a line from two points.
static constexpr std::size_t dimension() noexcept
Returns the dimensionality of the line.
constexpr AABB< Dim, T > aabb() const noexcept
Returns the AABB of the line.
constexpr bool isDegenerate() const noexcept
Returns whether the line is degenerate.
constexpr Vec< Dim, T > at(T t) const noexcept
Returns the point at distance t along the line.
Vec< Dim, T > origin
The origin point of the line.
Vec< Dim, T > direction
The direction of the line.
constexpr Line & operator=(Line const &) noexcept=default
Copy assignment operator.
constexpr T volume() const noexcept
Returns the volume of the line.
constexpr T diameter() const noexcept
Returns the diameter of the line.
constexpr Line(Line< Dim, U > const &other) noexcept
Converting constructor from a line with a different scalar type.
A fixed-size arithmetic vector of up to 4 dimensions.
constexpr auto & x(this auto &self) noexcept
Accesses the first component (x).