Lodestone.Level
Loading...
Searching...
No Matches
Vec3.h
Go to the documentation of this file.
1//
2// Created by DexrnZacAttack on 10/14/25 using zPc-i2.
3//
4#ifndef LODESTONE_VEC3I_H
5#define LODESTONE_VEC3I_H
7
9 template <typename T>
10 struct LODESTONE_API Vec3 final {
11 static_assert(std::is_arithmetic_v<T>, "T must be a numeric type");
12
13 T x, y, z;
14
15 constexpr Vec3(const T x, const T y, const T z) : x(x), y(y), z(z) {}
16
17 template <typename O>
18 explicit constexpr Vec3(const Vec3<O>& rhs) : x(static_cast<T>(rhs.x)), y(static_cast<T>(rhs.y)), z(static_cast<T>(rhs.z)) {
19 }
20
21 std::string toString() const {
22 return std::format("Vec3[x={}, y={}, z={}]", x, y, z);
23 };
24
25 constexpr operator T *() { return &x; }
26 constexpr operator const T *() const { return &x; }
27
28 constexpr bool operator==(const Vec3 &rhs) const {
29 return x == rhs.x && y == rhs.y && z == rhs.z;
30 }
31
32 constexpr Vec3 operator+(const T v) const {
33 return {x + v, y + v, z + v};
34 }
35
36 constexpr Vec3 operator-(const T v) const {
37 return {x - v, y - v, z - v};
38 }
39
40 constexpr Vec3 operator*(const T v) const {
41 return {x * v, y * v, z * v};
42 }
43
44 constexpr Vec3 operator/(const T v) const {
45 return {x / v, y / v, z / v};
46 }
47
48 constexpr Vec3& operator+=(const T v) {
49 this->x += v;
50 this->y += v;
51 this->z += v;
52
53 return *this;
54 }
55
56 constexpr Vec3& operator-=(const T v) {
57 this->x -= v;
58 this->y -= v;
59 this->z -= v;
60
61 return *this;
62 }
63
64 constexpr Vec3& operator*=(const T v) {
65 this->x *= v;
66 this->y *= v;
67 this->z *= v;
68
69 return *this;
70 }
71
72 constexpr Vec3& operator/=(const T v) {
73 this->x /= v;
74 this->y /= v;
75 this->z /= v;
76
77 return *this;
78 }
79
80 constexpr Vec3 operator+(const Vec3 &rhs) const {
81 return {x + rhs.x, y + rhs.y, z + rhs.z};
82 }
83
84 constexpr Vec3 operator-(const Vec3 &rhs) const {
85 return {x - rhs.x, x - rhs.y, z - rhs.z};
86 }
87
88 constexpr Vec3 operator*(const Vec3 &rhs) const {
89 return {x * rhs.x, y * rhs.y, z * rhs.z};
90 }
91
92 constexpr Vec3 operator/(const Vec3 &rhs) const {
93 return {x / rhs.x, y / rhs.y, z / rhs.z};
94 }
95
96 constexpr Vec3& operator+=(const Vec3 &rhs) {
97 this->x += rhs.x;
98 this->y += rhs.y;
99 this->z += rhs.z;
100
101 return *this;
102 }
103
104 constexpr Vec3& operator-=(const Vec3 &rhs) {
105 this->x -= rhs.x;
106 this->y -= rhs.y;
107 this->z -= rhs.z;
108
109 return *this;
110 }
111
112 constexpr Vec3& operator*=(const Vec3 &rhs) {
113 this->x *= rhs.x;
114 this->y *= rhs.y;
115 this->z *= rhs.z;
116
117 return *this;
118 }
119
120 constexpr Vec3& operator/=(const Vec3 &rhs) {
121 this->x /= rhs.x;
122 this->y /= rhs.y;
123 this->z /= rhs.z;
124
125 return *this;
126 }
127
128 constexpr Vec3& normalize() {
129 float m = std::sqrt(this->x * this->x + this->y * this-> y + this->z * this->z);
130
131 this->x /= m;
132 this->y /= m;
133 this->z /= m;
134
135 return *this;
136 }
137
138 constexpr Vec3 normalized() const {
139 float m = std::sqrt(this->x * this->x + this->y * this-> y + this->z * this->z);
140
141 return this / m;
142 }
143
144 constexpr Vec3& cross(const Vec3 &rhs) {
145 T ox = this->x;
146 T oy = this->y;
147 T oz = this->z;
148
149 this->x = oy * rhs.z - oz * rhs.y;
150 this->y = oz * rhs.x - ox * rhs.z;
151 this->z = ox * rhs.y - oy * rhs.x;
152
153 return *this;
154 }
155
156 constexpr Vec3 crossed(const Vec3 &rhs) const {
157 T ox = this->x;
158 T oy = this->y;
159 T oz = this->z;
160
161 return Vec3(
162 oy * rhs.z - oz * rhs.y,
163 oz * rhs.x - ox * rhs.z,
164 ox * rhs.y - oy * rhs.x
165 );
166 }
167 };
168
169 template <typename T>
170 constexpr Vec3<T> VEC3_ZERO{0,0,0};
171
172 template <typename T>
173 constexpr Vec3<T> VEC3_ONE{1,1,1};
174
178} // namespace lodestone::level::types
179
180template <typename T> struct std::hash<lodestone::level::types::Vec3<T>> {
181 size_t
183 return std::hash<T>()(v.x) ^ (std::hash<T>()(v.y) << 1) ^
184 (std::hash<T>()(v.z) << 2);
185 }
186};
187
188template <typename T>
189struct std::formatter<lodestone::level::types::Vec3<T>> {
190 template <typename FormatParseContext> constexpr auto parse(FormatParseContext &pc) { return pc.begin(); }
191 template <typename FormatContext> auto format(lodestone::level::types::Vec3<T> vec, FormatContext &fc) const { return std::format_to(fc.out(), "x={}, y={}, z={}", vec.x, vec.y, vec.z); }
192};
193
194#endif
#define LODESTONE_API
Definition Bounds2.h:12
constexpr Vec3< T > VEC3_ONE
Definition Vec3.h:173
Vec3< double > Vec3d
Definition Vec3.h:177
Vec3< int > Vec3i
Definition Vec3.h:175
Vec3< float > Vec3f
Definition Vec3.h:176
constexpr Vec3< T > VEC3_ZERO
Definition Vec3.h:170
Definition Vec3.h:10
constexpr Vec3 & operator/=(const T v)
Definition Vec3.h:72
std::string toString() const
Definition Vec3.h:21
constexpr Vec3 crossed(const Vec3 &rhs) const
Definition Vec3.h:156
constexpr bool operator==(const Vec3 &rhs) const
Definition Vec3.h:28
constexpr Vec3 operator+(const T v) const
Definition Vec3.h:32
constexpr Vec3 operator/(const Vec3 &rhs) const
Definition Vec3.h:92
constexpr Vec3 & operator-=(const Vec3 &rhs)
Definition Vec3.h:104
constexpr Vec3 operator*(const Vec3 &rhs) const
Definition Vec3.h:88
constexpr Vec3 operator*(const T v) const
Definition Vec3.h:40
constexpr Vec3 & operator+=(const Vec3 &rhs)
Definition Vec3.h:96
constexpr Vec3(const T x, const T y, const T z)
Definition Vec3.h:15
constexpr Vec3 operator-(const Vec3 &rhs) const
Definition Vec3.h:84
constexpr Vec3 & operator*=(const Vec3 &rhs)
Definition Vec3.h:112
constexpr Vec3 operator/(const T v) const
Definition Vec3.h:44
constexpr Vec3 operator-(const T v) const
Definition Vec3.h:36
constexpr Vec3 operator+(const Vec3 &rhs) const
Definition Vec3.h:80
constexpr Vec3 & normalize()
Definition Vec3.h:128
constexpr Vec3 & operator+=(const T v)
Definition Vec3.h:48
constexpr Vec3 & operator*=(const T v)
Definition Vec3.h:64
constexpr Vec3 normalized() const
Definition Vec3.h:138
constexpr Vec3 & operator-=(const T v)
Definition Vec3.h:56
constexpr Vec3 & cross(const Vec3 &rhs)
Definition Vec3.h:144
constexpr Vec3(const Vec3< O > &rhs)
Definition Vec3.h:18
constexpr Vec3 & operator/=(const Vec3 &rhs)
Definition Vec3.h:120
auto format(lodestone::level::types::Vec3< T > vec, FormatContext &fc) const
Definition Vec3.h:191
constexpr auto parse(FormatParseContext &pc)
Definition Vec3.h:190
size_t operator()(const lodestone::level::types::Vec3< T > &v) const noexcept
Definition Vec3.h:182