Move Vectormath main
C++20 game and graphics mathematics
Loading...
Searching...
No Matches
Segment3.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <optional>
5#include <type_traits>
6
9
10namespace mv::math
11{
12 // Standard endpoint segment; the [0,1] fraction convention matches
13 // Geometric Tools Segment.h and DistPointSegment.h (BSL-1.0).
14 template <typename T>
15 requires std::is_floating_point_v<T>
17 {
18 public:
19 using Component = T;
20
22
24 Start_(start), End_(end)
25 {
26 }
27
29 {
30 return Start_;
31 }
32
34 {
35 return End_;
36 }
37
39 {
40 return End_ - Start_;
41 }
42
47
49 {
50 return std::sqrt(LengthSquared());
51 }
52
54 {
55 return Start_ == End_;
56 }
57
58 [[nodiscard]] std::optional<Direction3<T>> TryDirection() const noexcept
59 {
61 }
62
63 // The caller supplies a fraction in [0,1]. No implicit clamping is
64 // performed so extrapolation is visible rather than silently repaired.
66 {
67 return Start_ + Displacement() * fraction;
68 }
69
71 {
72 return IsFinitePoint(Start_) && IsFinitePoint(End_);
73 }
74
75 [[nodiscard]] friend bool operator==(const Segment3& left,
76 const Segment3& right) noexcept
77 {
78 return left.Start_ == right.Start_ && left.End_ == right.End_;
79 }
80
81 private:
82 [[nodiscard]] static bool IsFinitePoint(const Point3<T>& point) noexcept
83 {
84 return std::isfinite(point.X()) && std::isfinite(point.Y()) &&
85 std::isfinite(point.Z());
86 }
87
88 Point3<T> Start_;
89 Point3<T> End_;
90 };
91
94} // namespace mv::math
95
96static_assert(sizeof(mv::math::Segment3f) == 32);
97static_assert(alignof(mv::math::Segment3f) == 16);
98static_assert(std::is_trivially_copyable_v<mv::math::Segment3f>);
99static_assert(std::is_standard_layout_v<mv::math::Segment3f>);
100static_assert(sizeof(mv::math::Segment3d) == 64);
101static_assert(alignof(mv::math::Segment3d) == 32);
102static_assert(std::is_trivially_copyable_v<mv::math::Segment3d>);
103static_assert(std::is_standard_layout_v<mv::math::Segment3d>);
static std::optional< Direction3 > TryFrom(const Vec3< T > &value) noexcept
Vec3< T > Displacement() const noexcept
Definition Segment3.hpp:38
const Point3< T > & End() const noexcept
Definition Segment3.hpp:33
friend bool operator==(const Segment3 &left, const Segment3 &right) noexcept
Definition Segment3.hpp:75
Segment3() noexcept=default
const Point3< T > & Start() const noexcept
Definition Segment3.hpp:28
bool IsFinite() const noexcept
Definition Segment3.hpp:70
T LengthSquared() const noexcept
Definition Segment3.hpp:43
T Length() const noexcept
Definition Segment3.hpp:48
std::optional< Direction3< T > > TryDirection() const noexcept
Definition Segment3.hpp:58
bool IsDegenerate() const noexcept
Definition Segment3.hpp:53
Point3< T > PointAtFraction(T fraction) const noexcept
Definition Segment3.hpp:65
Segment3< double > Segment3d
Definition Segment3.hpp:93
constexpr T LengthSquared(const Quat< T > &value) noexcept
Definition Quat.hpp:110
Segment3< float > Segment3f
Definition Segment3.hpp:92