Botcraft 26.3
Loading...
Searching...
No Matches
Vector3.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4#include <cmath>
5#include <ostream>
6#include <type_traits>
7
9#if PROTOCOL_VERSION > 772 /* > 1.21.8 */
11#endif
12
13namespace Botcraft
14{
15 template <typename T>
16 struct Vector3
17 {
18 Vector3(const T& v = 0)
19 {
20 x = v;
21 y = v;
22 z = v;
23 }
24
25 Vector3(const T& x_, const T& y_, const T& z_)
26 {
27 x = x_;
28 y = y_;
29 z = z_;
30 }
31
33 {
34 x = position.GetX();
35 y = position.GetY();
36 z = position.GetZ();
37 }
38
39#if PROTOCOL_VERSION > 772 /* > 1.21.8 */
41 {
42 x = v.GetX();
43 y = v.GetY();
44 z = v.GetZ();
45 }
46#endif
47
48 // Not float to int, cast is ok
49 template <typename U, std::enable_if_t<!std::is_floating_point_v<U> || !std::is_integral_v<T>, int> = 0>
50 Vector3(const Vector3<U>& position)
51 {
52 x = static_cast<T>(position.x);
53 y = static_cast<T>(position.y);
54 z = static_cast<T>(position.z);
55 }
56
57 // Float to int, explicit because usually you want to call ToBlockPos() instead
58 template <typename U, std::enable_if_t<std::is_floating_point_v<U> && std::is_integral_v<T>, int> = 0>
59 explicit Vector3(const Vector3<U>& position)
60 {
61 x = static_cast<T>(position.x);
62 y = static_cast<T>(position.y);
63 z = static_cast<T>(position.z);
64 }
65
66 Vector3(const std::vector<T>& v)
67 {
68 assert(v.size() == 3);
69 x = v[0];
70 y = v[1];
71 z = v[2];
72 }
73
74 Vector3(const std::array<T, 3>& v)
75 {
76 x = v[0];
77 y = v[1];
78 z = v[2];
79 }
80
81 T x;
82 T y;
83 T z;
84
85 Vector3 operator+(const Vector3& a) const
86 {
87 return Vector3(a.x + x, a.y + y, a.z + z);
88 }
89
90 Vector3 operator-(const Vector3& a) const
91 {
92 return Vector3(x - a.x, y - a.y, z - a.z);
93 }
94
95 Vector3 operator*(const Vector3& v) const
96 {
97 return Vector3(x * v.x, y * v.y, z * v.z);
98 }
99
100 Vector3 operator/(const Vector3& v) const
101 {
102 return Vector3(x / v.x, y / v.y, z / v.z);
103 }
104
105 bool operator==(const Vector3& a) const
106 {
107 return (x == a.x) && (y == a.y) && (z == a.z);
108 }
109
110 bool operator!=(const Vector3& a) const
111 {
112 return (x != a.x) || (y != a.y) || (z != a.z);
113 }
114
115 bool operator<(const Vector3& a) const
116 {
117 return (x < a.x) ||
118 (x == a.x && y < a.y) ||
119 (x == a.x && y == a.y && z < a.z);
120 }
121
122 Vector3 operator*(const T d) const
123 {
124 return Vector3(x * d, y * d, z * d);
125 }
126
127 Vector3 operator-(const T d) const
128 {
129 return Vector3(x - d, y - d, z - d);
130 }
131
132 Vector3 operator+(const T d) const
133 {
134 return Vector3(x + d, y + d, z + d);
135 }
136
137 Vector3 operator/(const T d) const
138 {
139 return Vector3(x / d, y / d, z / d);
140 }
141
143 {
144 x += v.x;
145 y += v.y;
146 z += v.z;
147 return *this;
148 }
149
151 {
152 x += v;
153 y += v;
154 z += v;
155 return *this;
156 }
157
159 {
160 x -= v.x;
161 y -= v.y;
162 z -= v.z;
163 return *this;
164 }
165
167 {
168 x -= v;
169 y -= v;
170 z -= v;
171 return *this;
172 }
173
175 {
176 x *= v.x;
177 y *= v.y;
178 z *= v.z;
179 return *this;
180 }
181
183 {
184 x *= v;
185 y *= v;
186 z *= v;
187 return *this;
188 }
189
191 {
192 x /= v.x;
193 y /= v.y;
194 z /= v.z;
195 return *this;
196 }
197
199 {
200 x /= v;
201 y /= v;
202 z /= v;
203 return *this;
204 }
205
206 double dot(const Vector3& v) const
207 {
208 double output = 0.0;
209 output += x * v.x;
210 output += y * v.y;
211 output += z * v.z;
212 return output;
213 }
214
215 double SqrDist(const Vector3& v) const
216 {
217 return (x - v.x) * (x - v.x)
218 + (y - v.y) * (y - v.y)
219 + (z - v.z) * (z - v.z);
220 }
221
222 Vector3 Abs() const
223 {
224 return Vector3(
225 std::abs(x),
226 std::abs(y),
227 std::abs(z)
228 );
229 }
230
231 T& operator[] (const int i)
232 {
233 assert(i > -1);
234 assert(i < 3);
235 switch (i)
236 {
237 case 0:
238 return x;
239 break;
240 case 1:
241 return y;
242 break;
243 case 2:
244 return z;
245 break;
246 }
247 // This doesn't happen
248 return x;
249 }
250
251 const T& operator[] (const int i) const
252 {
253 assert(i > -1);
254 assert(i < 3);
255 switch (i)
256 {
257 case 0:
258 return x;
259 break;
260 case 1:
261 return y;
262 break;
263 case 2:
264 return z;
265 break;
266 }
267 // This doesn't happen
268 return x;
269 }
270
272 {
273 const double norm = std::sqrt(SqrNorm());
274 x /= norm;
275 y /= norm;
276 z /= norm;
277 }
278
279 double SqrNorm() const
280 {
281 return x * x + y * y + z * z;
282 }
283
284 friend std::ostream& operator << (std::ostream& o, const Vector3& vec)
285 {
286 o << "(" << vec.x << "," << vec.y << "," << vec.z << ")";
287 return o;
288 }
289
291 {
292 return ProtocolCraft::Json::Value({ x, y, z });
293 }
294
296 {
298 output.SetX(x);
299 output.SetY(y);
300 output.SetZ(z);
301
302 return output;
303 }
304
306 {
307 return Vector3<int>(
308 static_cast<int>(std::floor(x)),
309 static_cast<int>(std::floor(y)),
310 static_cast<int>(std::floor(z))
311 );
312 }
313 };
315} // Botcraft
316
317namespace std
318{
319 template <typename T>
320 struct hash<Botcraft::Vector3<T> >
321 {
322 inline size_t operator()(const Botcraft::Vector3<T>& v) const
323 {
324 std::hash<T> hasher;
325 size_t value = hasher(v.x);
326 value ^= hasher(v.y) + 0x9e3779b9 + (value << 6) + (value >> 2);
327 value ^= hasher(v.z) + 0x9e3779b9 + (value << 6) + (value >> 2);
328 return value;
329 }
330 };
331}
Main class, basically a JsonVariant with extra utility functions it doesn't inherit JsonVariant direc...
Definition Json.hpp:45
Vector3< int > Position
Definition Vector3.hpp:314
STL namespace.
bool operator!=(const Vector3 &a) const
Definition Vector3.hpp:110
Vector3 operator/(const Vector3 &v) const
Definition Vector3.hpp:100
Vector3(const Vector3< U > &position)
Definition Vector3.hpp:50
Vector3(const ProtocolCraft::LpVec3 &v)
Definition Vector3.hpp:40
Vector3 operator+(const T d) const
Definition Vector3.hpp:132
Vector3 operator+(const Vector3 &a) const
Definition Vector3.hpp:85
Vector3(const ProtocolCraft::NetworkPosition &position)
Definition Vector3.hpp:32
Vector3(const T &v=0)
Definition Vector3.hpp:18
Vector3 & operator+=(const Vector3 &v)
Definition Vector3.hpp:142
Vector3 & operator*=(const Vector3 &v)
Definition Vector3.hpp:174
double SqrDist(const Vector3 &v) const
Definition Vector3.hpp:215
double dot(const Vector3 &v) const
Definition Vector3.hpp:206
ProtocolCraft::NetworkPosition ToNetworkPosition() const
Definition Vector3.hpp:295
double SqrNorm() const
Definition Vector3.hpp:279
ProtocolCraft::Json::Value Serialize() const
Definition Vector3.hpp:290
bool operator==(const Vector3 &a) const
Definition Vector3.hpp:105
Vector3 operator-(const T d) const
Definition Vector3.hpp:127
Vector3(const std::array< T, 3 > &v)
Definition Vector3.hpp:74
Vector3 Abs() const
Definition Vector3.hpp:222
Vector3< int > ToBlockPos() const
Definition Vector3.hpp:305
Vector3 & operator/=(const Vector3 &v)
Definition Vector3.hpp:190
bool operator<(const Vector3 &a) const
Definition Vector3.hpp:115
T & operator[](const int i)
Definition Vector3.hpp:231
Vector3 operator/(const T d) const
Definition Vector3.hpp:137
Vector3(const std::vector< T > &v)
Definition Vector3.hpp:66
Vector3 operator-(const Vector3 &a) const
Definition Vector3.hpp:90
friend std::ostream & operator<<(std::ostream &o, const Vector3 &vec)
Definition Vector3.hpp:284
Vector3 & operator-=(const Vector3 &v)
Definition Vector3.hpp:158
Vector3 operator*(const Vector3 &v) const
Definition Vector3.hpp:95
Vector3 operator*(const T d) const
Definition Vector3.hpp:122
Vector3(const T &x_, const T &y_, const T &z_)
Definition Vector3.hpp:25
size_t operator()(const Botcraft::Vector3< T > &v) const
Definition Vector3.hpp:322