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