-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector3.hpp
More file actions
151 lines (121 loc) · 4.19 KB
/
vector3.hpp
File metadata and controls
151 lines (121 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef PSTACK_GEO_VECTOR3_HPP
#define PSTACK_GEO_VECTOR3_HPP
#include "pstack/geo/functions.hpp"
#include <algorithm>
#include <type_traits>
namespace pstack::geo {
enum class Axis {
X = 0,
Y = 1,
Z = 2
};
template <class T>
struct vector3 {
T x;
T y;
T z;
constexpr T& operator[](const Axis axis) {
auto index = static_cast<std::underlying_type_t<Axis>>(axis);
return *(&x + index);
}
constexpr const T& operator[](const Axis axis) const {
auto index = static_cast<std::underlying_type_t<Axis>>(axis);
return *(&x + index);
}
};
template <class T>
inline constexpr vector3<T> unit_x = { 1, 0, 0 };
template <class T>
inline constexpr vector3<T> unit_y = { 0, 1, 0 };
template <class T>
inline constexpr vector3<T> unit_z = { 0, 0, 1 };
template <class T>
constexpr auto operator<=>(const vector3<T>& lhs, const vector3<T>& rhs) {
if (auto c = lhs.x <=> rhs.x; c != 0) {
return c;
}
if (auto c = lhs.y <=> rhs.y; c != 0) {
return c;
}
return lhs.z <=> rhs.z;
}
template<class T>
constexpr vector3<T> component_min(const vector3<T>& lhs, const vector3<T>& rhs) {
return { std::min(lhs.x, rhs.x), std::min(lhs.y, rhs.y), std::min(lhs.z, rhs.z) };
}
template<class T>
constexpr vector3<T> component_max(const vector3<T>& lhs, const vector3<T>& rhs) {
return { std::max(lhs.x, rhs.x), std::max(lhs.y, rhs.y), std::max(lhs.z, rhs.z) };
}
template <class T>
constexpr vector3<T> sort(const vector3<T>& v) {
vector3<T> result = v;
if (result.x > result.z) std::swap(result.x, result.z);
if (result.x > result.y) std::swap(result.x, result.y);
if (result.y > result.z) std::swap(result.y, result.z);
return result;
}
template <class T>
constexpr bool fits(const vector3<T>& a, const vector3<T>& b) {
return a.x >= b.x && a.y >= b.y && a.z >= b.z;
}
template <class T>
constexpr vector3<T> operator+(const vector3<T>& lhs, const vector3<T>& rhs) {
return { lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z };
}
template <class T>
constexpr vector3<T> operator-(const vector3<T>& lhs, const vector3<T>& rhs) {
return { lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z };
}
template <class T>
constexpr vector3<T> operator-(const vector3<T>& lhs) {
return { -lhs.x, -lhs.y, -lhs.z };
}
template <class T>
constexpr vector3<T>& operator+=(vector3<T>& lhs, const vector3<T>& rhs) {
lhs = { lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z };
return lhs;
}
template <class T>
constexpr vector3<T> operator+(const vector3<T>& lhs, const std::type_identity_t<T>& rhs) {
return { lhs.x + rhs, lhs.y + rhs, lhs.z + rhs };
}
template <class T>
constexpr vector3<T> operator+(const std::type_identity_t<T>& lhs, const vector3<T>& rhs) {
return { lhs + rhs.x, lhs + rhs.y, lhs + rhs.z };
}
template <class T>
constexpr vector3<T> operator-(const vector3<T>& lhs, const std::type_identity_t<T>& rhs) {
return { lhs.x - rhs, lhs.y - rhs, lhs.z - rhs };
}
template <class T>
constexpr vector3<T> operator-(const std::type_identity_t<T>& lhs, const vector3<T>& rhs) {
return { lhs - rhs.x, lhs - rhs.y, lhs - rhs.z };
}
template <class T>
constexpr vector3<T> operator*(const std::type_identity_t<T>& lhs, const vector3<T>& rhs) {
return { rhs.x * lhs, rhs.y * lhs, rhs.z * lhs };
}
template <class T>
constexpr vector3<T> operator*(const vector3<T>& lhs, const std::type_identity_t<T>& rhs) {
return { lhs.x * rhs, lhs.y * rhs, lhs.z * rhs };
}
template <class T>
constexpr vector3<T> operator/(const vector3<T>& lhs, const std::type_identity_t<T>& rhs) {
return { lhs.x / rhs, lhs.y / rhs, lhs.z / rhs };
}
template <class T>
constexpr T dot(const vector3<T>& lhs, const vector3<T>& rhs) {
return (lhs.x * rhs.x) + (lhs.y * rhs.y) + (lhs.z * rhs.z);
}
template <class T>
constexpr vector3<T> cross(const vector3<T>& lhs, const vector3<T>& rhs) {
return { lhs.y * rhs.z - lhs.z * rhs.y, lhs.z * rhs.x - lhs.x * rhs.z, lhs.x * rhs.y - lhs.y * rhs.x };
}
template <class T>
constexpr vector3<T> normalize(const vector3<T>& v) {
const auto length_squared = (v.x * v.x) + (v.y * v.y) + (v.z * v.z);
return v * inverse_sqrt(length_squared);
}
} // namespace pstack::geo
#endif // PSTACK_GEO_VECTOR3_HPP