Move Vectormath main
C++20 game and graphics mathematics
Loading...
Searching...
No Matches
Triangle3.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 three-finite-point triangle; degeneracy and optional-normal
13 // policy are Move-specific.
14 template <typename T>
15 requires std::is_floating_point_v<T>
17 {
18 public:
19 using Component = T;
20
22
27 {
28 if (!IsFinitePoint(first) || !IsFinitePoint(second) ||
29 !IsFinitePoint(third))
30 {
31 return std::nullopt;
32 }
33 return Triangle3(first, second, third, UncheckedTag{});
34 }
35
37 {
38 return Vertices_[0];
39 }
40
42 {
43 return Vertices_[1];
44 }
45
47 {
48 return Vertices_[2];
49 }
50
52 {
53 return Second() - First();
54 }
55
57 {
58 return Third() - First();
59 }
60
62 {
63 return Third() - Second();
64 }
65
67 {
69 (First().Vector() + Second().Vector() + Third().Vector()) *
70 (T(1) / T(3)));
71 }
72
74 {
75 return std::sqrt(LengthSquared(Cross(Edge01(), Edge02())));
76 }
77
79 {
80 return DoubleArea() * T(0.5);
81 }
82
83 [[nodiscard]] std::optional<Normal3<T>> TryNormal() const noexcept
84 {
86 }
87
88 [[nodiscard]] friend bool operator==(const Triangle3& left,
89 const Triangle3& right) noexcept
90 {
91 return left.Vertices_[0] == right.Vertices_[0] &&
92 left.Vertices_[1] == right.Vertices_[1] &&
93 left.Vertices_[2] == right.Vertices_[2];
94 }
95
96 private:
97 struct UncheckedTag
98 {
99 };
100
101 Triangle3(const Point3<T>& first,
102 const Point3<T>& second,
103 const Point3<T>& third,
104 UncheckedTag) noexcept :
105 Vertices_{first, second, third}
106 {
107 }
108
109 [[nodiscard]] static bool IsFinitePoint(const Point3<T>& point) noexcept
110 {
111 return std::isfinite(point.X()) && std::isfinite(point.Y()) &&
112 std::isfinite(point.Z());
113 }
114
115 Point3<T> Vertices_[3];
116 };
117
120} // namespace mv::math
121
122static_assert(sizeof(mv::math::Triangle3f) == 48);
123static_assert(alignof(mv::math::Triangle3f) == 16);
124static_assert(std::is_trivially_copyable_v<mv::math::Triangle3f>);
125static_assert(std::is_standard_layout_v<mv::math::Triangle3f>);
126static_assert(sizeof(mv::math::Triangle3d) == 96);
127static_assert(alignof(mv::math::Triangle3d) == 32);
128static_assert(std::is_trivially_copyable_v<mv::math::Triangle3d>);
129static_assert(std::is_standard_layout_v<mv::math::Triangle3d>);
static std::optional< Normal3 > TryFrom(const Vec3< T > &value) noexcept
Definition Normal3.hpp:46
static Point3 FromVector(const Vec3< T > &value) noexcept
Definition Point3.hpp:28
friend bool operator==(const Triangle3 &left, const Triangle3 &right) noexcept
Definition Triangle3.hpp:88
const Point3< T > & First() const noexcept
Definition Triangle3.hpp:36
Point3< T > Centroid() const noexcept
Definition Triangle3.hpp:66
T DoubleArea() const noexcept
Definition Triangle3.hpp:73
T Area() const noexcept
Definition Triangle3.hpp:78
const Point3< T > & Second() const noexcept
Definition Triangle3.hpp:41
Vec3< T > Edge02() const noexcept
Definition Triangle3.hpp:56
Vec3< T > Edge01() const noexcept
Definition Triangle3.hpp:51
std::optional< Normal3< T > > TryNormal() const noexcept
Definition Triangle3.hpp:83
Vec3< T > Edge12() const noexcept
Definition Triangle3.hpp:61
const Point3< T > & Third() const noexcept
Definition Triangle3.hpp:46
Triangle3() noexcept=default
static std::optional< Triangle3 > TryFromPoints(const Point3< T > &first, const Point3< T > &second, const Point3< T > &third) noexcept
Definition Triangle3.hpp:23
Vec3< T > Cross(const Vec3< T > &left, const Vec3< T > &right) noexcept
Definition Vec3.hpp:367
Triangle3< double > Triangle3d
Triangle3< float > Triangle3f
constexpr T LengthSquared(const Quat< T > &value) noexcept
Definition Quat.hpp:110