Move Vectormath main
C++20 game and graphics mathematics
Loading...
Searching...
No Matches
Plane3.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 Hessian-normal plane form; finite construction and semantic
13 // Normal3f storage are Move-specific.
14 enum class PlaneSide
15 {
16 Back,
18 Front
19 };
20
21 template <typename T>
22 requires std::is_floating_point_v<T>
23 class alignas(sizeof(T) * 4U) Plane3
24 {
25 public:
26 using Component = T;
27
29
32 {
33 if (!std::isfinite(offset))
34 {
35 return std::nullopt;
36 }
37 return Plane3(normal, offset, UncheckedTag{});
38 }
39
40 [[nodiscard]] static std::optional<Plane3> TryFromPointNormal(
41 const Point3<T>& point, const Normal3<T>& normal) noexcept
42 {
43 const T offset = -Dot(normal.Vector(), point.Vector());
45 }
46
47 [[nodiscard]] static std::optional<Plane3> TryFromPoints(
48 const Point3<T>& first,
49 const Point3<T>& second,
50 const Point3<T>& third) noexcept
51 {
52 const auto normal =
54 if (!normal)
55 {
56 return std::nullopt;
57 }
59 }
60
62 {
63 return Normal3<T>(
64 Vec3<T>(Coefficients_[0], Coefficients_[1], Coefficients_[2]),
65 typename Normal3<T>::UncheckedTag{});
66 }
67
69 {
70 return Coefficients_[3];
71 }
72
73 [[nodiscard]] T SignedDistance(const Point3<T>& point) const noexcept
74 {
75 return Dot(Normal().Vector(), point.Vector()) + Offset();
76 }
77
80 {
82 const T tolerance = std::abs(boundaryTolerance);
83 if (distance > tolerance)
84 {
85 return PlaneSide::Front;
86 }
87 if (distance < -tolerance)
88 {
89 return PlaneSide::Back;
90 }
92 }
93
95 const Point3<T>& point) const noexcept
96 {
97 return point - Normal().Vector() * SignedDistance(point);
98 }
99
101 {
102 return Plane3(-Normal(), -Offset(), UncheckedTag{});
103 }
104
105 [[nodiscard]] friend bool operator==(const Plane3& left,
106 const Plane3& right) noexcept
107 {
108 return left.Coefficients_[0] == right.Coefficients_[0] &&
109 left.Coefficients_[1] == right.Coefficients_[1] &&
110 left.Coefficients_[2] == right.Coefficients_[2] &&
111 left.Coefficients_[3] == right.Coefficients_[3];
112 }
113
114 private:
115 struct UncheckedTag
116 {
117 };
118
119 Plane3(const Normal3<T>& normal, T offset, UncheckedTag) noexcept :
120 Coefficients_{normal.Vector().X(), normal.Vector().Y(),
121 normal.Vector().Z(), offset}
122 {
123 }
124
125 T Coefficients_[4]{T(1), T(0), T(0), T(0)};
126 };
127
130} // namespace mv::math
131
132static_assert(sizeof(mv::math::Plane3f) == 16);
133static_assert(alignof(mv::math::Plane3f) == 16);
134static_assert(std::is_trivially_copyable_v<mv::math::Plane3f>);
135static_assert(std::is_standard_layout_v<mv::math::Plane3f>);
136static_assert(sizeof(mv::math::Plane3d) == 32);
137static_assert(alignof(mv::math::Plane3d) == 32);
138static_assert(std::is_trivially_copyable_v<mv::math::Plane3d>);
139static_assert(std::is_standard_layout_v<mv::math::Plane3d>);
static std::optional< Normal3 > TryFrom(const Vec3< T > &value) noexcept
Definition Normal3.hpp:46
Plane3() noexcept=default
static std::optional< Plane3 > TryFromPoints(const Point3< T > &first, const Point3< T > &second, const Point3< T > &third) noexcept
Definition Plane3.hpp:47
T Offset() const noexcept
Definition Plane3.hpp:68
static std::optional< Plane3 > TryFromNormalAndOffset(const Normal3< T > &normal, T offset) noexcept
Definition Plane3.hpp:30
Plane3 Flipped() const noexcept
Definition Plane3.hpp:100
static std::optional< Plane3 > TryFromPointNormal(const Point3< T > &point, const Normal3< T > &normal) noexcept
Definition Plane3.hpp:40
friend bool operator==(const Plane3 &left, const Plane3 &right) noexcept
Definition Plane3.hpp:105
T SignedDistance(const Point3< T > &point) const noexcept
Definition Plane3.hpp:73
Normal3< T > Normal() const noexcept
Definition Plane3.hpp:61
Point3< T > ClosestPoint(const Point3< T > &point) const noexcept
Definition Plane3.hpp:94
PlaneSide Classify(const Point3< T > &point, T boundaryTolerance=T(0)) const noexcept
Definition Plane3.hpp:78
constexpr T Dot(const Quat< T > &left, const Quat< T > &right) noexcept
Definition Quat.hpp:102
Vec3< T > Cross(const Vec3< T > &left, const Vec3< T > &right) noexcept
Definition Vec3.hpp:367
Plane3< double > Plane3d
Definition Plane3.hpp:129
Plane3< float > Plane3f
Definition Plane3.hpp:128