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