EHS
Loading...
Searching...
No Matches
Vec3.h
Go to the documentation of this file.
1#pragma once
2
3#include "Types.h"
4#include "UTF.h"
5#include "Str.h"
6#include "Math.h"
7#include "Vec2.h"
8#include "Log.h"
9
10namespace ehs
11{
12 template<typename T>
13 class Vec3
14 {
15 public:
16 T x;
17 T y;
18 T z;
19
20 Vec3(const T scalar = 0)
21 : x(scalar), y(scalar), z(scalar)
22 {
23 }
24
25 Vec3(const T x, const T y, const T z)
26 : x(x), y(y), z(z)
27 {
28 }
29
30 template<typename C>
31 Vec3(const Vec2<C>& vec, const T z = 0)
32 : x((T)vec.x), y((T)vec.y), z(z)
33 {
34 }
35
36 template<typename C>
37 Vec3(const Vec3<C>& vec)
38 : x((T)vec.x), y((T)vec.y), z((T)vec.z)
39 {
40 }
41
42 template<typename C>
44 {
45 x = (T)vec.x;
46 y = (T)vec.y;
47 z = 0;
48
49 return *this;
50 }
51
52 template<typename C>
54 {
55 x = (T)vec.x;
56 y = (T)vec.y;
57 z = (T)vec.z;
58
59 return *this;
60 }
61
62 bool operator==(const Vec3<T>& vec) const
63 {
64 return Math::ComCmp(x, vec.x) && Math::ComCmp(y, vec.y) && Math::ComCmp(z, vec.z);
65 }
66
67
68 bool operator!=(const Vec3<T>& vec) const
69 {
70 return !Math::ComCmp(z, vec.z) || !Math::ComCmp(y, vec.y) || !Math::ComCmp(z, vec.z);
71 }
72
73 Vec3<T> operator+(const Vec3<T>& vec) const
74 {
75 Vec3<T> tmp;
76
77 tmp.x = x + vec.x;
78 tmp.y = y + vec.y;
79 tmp.z = z + vec.z;
80
81 return tmp;
82 }
83
85 {
86 x += vec.x;
87 y += vec.y;
88 z += vec.z;
89
90 return *this;
91 }
92
93 Vec3<T> operator+(const T scalar) const
94 {
95 Vec3<T> tmp;
96
97 tmp.x = x + scalar;
98 tmp.y = y + scalar;
99 tmp.z = z + scalar;
100
101 return tmp;
102 }
103
104 Vec3<T>& operator+=(const T scalar)
105 {
106 x += scalar;
107 y += scalar;
108 z += scalar;
109
110 return *this;
111 }
112
113 Vec3<T> operator-(const Vec3<T>& vec) const
114 {
115 Vec3<T> tmp;
116
117 tmp.x = x - vec.x;
118 tmp.y = y - vec.y;
119 tmp.z = z - vec.z;
120
121 return tmp;
122 }
123
125 {
126 x -= vec.x;
127 y -= vec.y;
128 z -= vec.z;
129
130 return *this;
131 }
132
133 Vec3<T> operator-(const T scalar) const
134 {
135 Vec3<T> tmp;
136
137 tmp.x = x - scalar;
138 tmp.y = y - scalar;
139 tmp.z = z - scalar;
140
141 return tmp;
142 }
143
144 Vec3<T>& operator-=(const T scalar)
145 {
146 x -= scalar;
147 y -= scalar;
148 z -= scalar;
149
150 return *this;
151 }
152
153 Vec3<T> operator*(const Vec3<T>& vec) const
154 {
155 Vec3<T> tmp;
156
157 tmp.x = x * vec.x;
158 tmp.y = y * vec.y;
159 tmp.z = z * vec.z;
160
161 return tmp;
162 }
163
165 {
166 x *= vec.x;
167 y *= vec.y;
168 z *= vec.z;
169
170 return *this;
171 }
172
173 Vec3<T> operator*(const T scalar) const
174 {
175 Vec3<T> tmp;
176
177 tmp.x = x * scalar;
178 tmp.y = y * scalar;
179 tmp.z = z * scalar;
180
181 return tmp;
182 }
183
184 Vec3<T>& operator*=(const T scalar)
185 {
186 x *= scalar;
187 y *= scalar;
188 z *= scalar;
189
190 return *this;
191 }
192
193 Vec3<T> operator/(const Vec3<T>& vec) const
194 {
195 Vec3<T> tmp;
196
197 tmp.x = x / vec.x;
198 tmp.y = y / vec.y;
199 tmp.z = z / vec.z;
200
201 return tmp;
202 }
203
205 {
206 x /= vec.x;
207 y /= vec.y;
208 z /= vec.z;
209
210 return *this;
211 }
212
213 Vec3<T> operator/(const T scalar) const
214 {
215 Vec3<T> tmp;
216
217 tmp.x = x / scalar;
218 tmp.y = y / scalar;
219 tmp.z = z / scalar;
220
221 return tmp;
222 }
223
224 Vec3<T>& operator/=(const T scalar)
225 {
226 x /= scalar;
227 y /= scalar;
228 z /= scalar;
229
230 return *this;
231 }
232
233 bool operator<=(const Vec3<T>& other) const
234 {
235 return x <= other.x && y <= other.y && z <= other.z;
236 }
237
238 bool operator<(const Vec3<T>& other) const
239 {
240 return x < other.x && y < other.y && z < other.z;
241 }
242
243 bool operator>=(const Vec3<T>& other) const
244 {
245 return x >= other.x && y >= other.y && z >= other.z;
246 }
247
248 bool operator>(const Vec3<T>& other) const
249 {
250 return x > other.x && y > other.y && z > other.z;
251 }
252
254 {
255 return {-x, -y, -z};
256 }
257
258 T operator[](const UInt_64 index) const
259 {
260 switch (index)
261 {
262 case 0:
263 return x;
264 case 1:
265 return y;
266 case 2:
267 return z;
268 default:
269 EHS_LOG_INT(LogType::ERR, 0, "Index of, \"" + Str_8::FromNum(index) + "\" is out of range for a Vector 3.");
270 return x;
271 }
272 }
273
274 T& operator[](const UInt_64 index)
275 {
276 switch (index)
277 {
278 case 0:
279 return x;
280 case 1:
281 return y;
282 case 2:
283 return z;
284 default:
285 EHS_LOG_INT(LogType::ERR, 0, "Index of, \"" + Str_8::FromNum(index) + "\" is out of range for a Vector 3.");
286 return x;
287 }
288 }
289
290 operator Vec2<T>()
291 {
292 return Vec2<T>(x, y);
293 }
294
296 {
297 Vec3<T> absolute;
298
299 absolute.x = Math::Abs(x);
300 absolute.y = Math::Abs(y);
301 absolute.z = Math::Abs(z);
302
303 return absolute;
304 }
305
306 void Abs()
307 {
308 x = Math::Abs(x);
309 y = Math::Abs(y);
310 z = Math::Abs(z);
311 }
312
314 {
315 Vec3<T> norm;
316
317 T dis = GetMagnitude();
318
319 norm.x = x / dis;
320 norm.y = y / dis;
321 norm.z = z / dis;
322
323 return norm;
324 }
325
326 void Norm()
327 {
328 T dis = GetMagnitude();
329
330 x /= dis;
331 y /= dis;
332 z /= dis;
333 }
334
335 Vec3<T> GetCross(const Vec3<T>& vec) const
336 {
337 return Vec3<T>(
338 y * vec.z - z * vec.y,
339 z * vec.x - x * vec.z,
340 x * vec.y - y * vec.x
341 );
342 }
343
344 T GetDot(const Vec3<T>& vec) const
345 {
346 return x * vec.x + y * vec.y + z * vec.z;
347 }
348
349 T GetAngle(const Vec2<T>& vec) const
350 {
351 return Math::ACos(GetDot(vec) / Math::Sqrt(GetMagnitude2() * vec.GetMagnitude2()));
352 }
353
354 Vec2<T> GetProjection(const Vec2<T>& length) const
355 {
356 return operator*(length.GetDot(*this) / GetMagnitude2());
357 }
358
359 Vec2<T> GetPerpendicular(const Vec2<T>& length) const
360 {
361 return length - GetProjection(length);
362 }
363
364 Vec2<T> GetReflection(const Vec2<T>& normal) const
365 {
366 return operator-(normal * (GetDot(normal) * 2));
367 }
368
369 T GetMagnitude() const
370 {
371 return Math::Sqrt(x * x + y * y + z * z);
372 }
373
375 {
376 return x * x + y * y + z * z;
377 }
378
379 T GetDistance(const Vec3<T>& vec) const
380 {
381 return (T)Math::Sqrt(Math::Pow<T>(vec.x - x, 2) + Math::Pow<T>(vec.y - y, 2) + Math::Pow<T>(vec.z - z, 2));
382 }
383
384 T GetDistance2(const Vec3<T>& vec) const
385 {
386 return static_cast<T>(Math::Pow<T>(vec.x - x, 2) + Math::Pow<T>(vec.y - y, 2) + Math::Pow<T>(vec.z - z, 2));
387 }
388
390 {
391 Vec3<T> tmp;
392
393 tmp.x = Math::Rads(x);
394 tmp.y = Math::Rads(y);
395 tmp.z = Math::Rads(z);
396
397 return tmp;
398 }
399
400 void ToRads()
401 {
402 x = Math::Rads(x);
403 y = Math::Rads(y);
404 z = Math::Rads(z);
405 }
406
408 {
409 Vec3<T> tmp;
410
411 tmp.x = Math::Degr(x);
412 tmp.y = Math::Degr(y);
413 tmp.z = Math::Degr(z);
414
415 return tmp;
416 }
417
418 void ToDegr()
419 {
420 x = Math::Degr(x);
421 y = Math::Degr(y);
422 z = Math::Degr(z);
423 }
424
425 static Vec3 Lerp(const Vec3& start, const Vec3& finish, const T t)
426 {
427 return start + (finish - start) * t;
428 }
429 };
430
445}
#define EHS_LOG_INT(type, code, msg)
Definition Log.h:137
static T Pow(const T base, const I exponent)
Definition Math.h:173
static T Degr(const T from)
Definition Math.h:99
static T Rads(const T from)
Definition Math.h:89
static R ACos(const R xPos, const T precision=10)
Definition Math.h:328
static bool ComCmp(float a, float b)
Combined absolute and relative tolerance comparison for single precision floats.
Definition Math.cpp:26
static T Abs(const T from)
Definition Math.h:70
static T Sqrt(const T from)
Definition Math.h:238
static Str< Char_8, UInt_64 > FromNum(const SInt_64 num)
Definition Str.h:1399
Definition Vec2.h:13
T y
Definition Vec2.h:16
T x
Definition Vec2.h:15
T GetMagnitude2() const
Definition Vec2.h:299
T GetDot(const Vec2< T > &vec) const
Definition Vec2.h:269
Definition Vec3.h:14
void ToDegr()
Definition Vec3.h:418
Vec3< T > & operator-=(const T scalar)
Definition Vec3.h:144
Vec3< T > operator/(const T scalar) const
Definition Vec3.h:213
Vec3< T > operator-(const Vec3< T > &vec) const
Definition Vec3.h:113
bool operator<(const Vec3< T > &other) const
Definition Vec3.h:238
bool operator>=(const Vec3< T > &other) const
Definition Vec3.h:243
bool operator>(const Vec3< T > &other) const
Definition Vec3.h:248
Vec3< T > & operator-=(const Vec3< T > &vec)
Definition Vec3.h:124
Vec3< T > & operator/=(const T scalar)
Definition Vec3.h:224
Vec3< T > & operator*=(const Vec3< T > &vec)
Definition Vec3.h:164
Vec3< T > & operator+=(const T scalar)
Definition Vec3.h:104
Vec3< T > & operator+=(const Vec3< T > &vec)
Definition Vec3.h:84
Vec3(const T x, const T y, const T z)
Definition Vec3.h:25
Vec3< T > & operator/=(const Vec3< T > &vec)
Definition Vec3.h:204
UInt_64 x
Definition Vec3.h:16
T GetDistance2(const Vec3< T > &vec) const
Definition Vec3.h:384
Vec3< T > & operator=(const Vec2< C > &vec)
Definition Vec3.h:43
Vec3(const Vec3< C > &vec)
Definition Vec3.h:37
T GetDot(const Vec3< T > &vec) const
Definition Vec3.h:344
T GetMagnitude() const
Definition Vec3.h:369
Vec3< T > GetNorm() const
Definition Vec3.h:313
T & operator[](const UInt_64 index)
Definition Vec3.h:274
bool operator<=(const Vec3< T > &other) const
Definition Vec3.h:233
bool operator!=(const Vec3< T > &vec) const
Definition Vec3.h:68
Vec3(const Vec2< C > &vec, const T z=0)
Definition Vec3.h:31
UInt_64 z
Definition Vec3.h:18
UInt_64 y
Definition Vec3.h:17
Vec3< T > operator+(const Vec3< T > &vec) const
Definition Vec3.h:73
void Norm()
Definition Vec3.h:326
Vec3< T > operator/(const Vec3< T > &vec) const
Definition Vec3.h:193
void ToRads()
Definition Vec3.h:400
Vec2< T > GetReflection(const Vec2< T > &normal) const
Definition Vec3.h:364
bool operator==(const Vec3< T > &vec) const
Definition Vec3.h:62
T GetMagnitude2() const
Definition Vec3.h:374
T operator[](const UInt_64 index) const
Definition Vec3.h:258
Vec3< T > operator*(const Vec3< T > &vec) const
Definition Vec3.h:153
Vec2< T > GetPerpendicular(const Vec2< T > &length) const
Definition Vec3.h:359
Vec2< T > GetProjection(const Vec2< T > &length) const
Definition Vec3.h:354
Vec3< T > GetCross(const Vec3< T > &vec) const
Definition Vec3.h:335
void Abs()
Definition Vec3.h:306
Vec3< T > operator-(const T scalar) const
Definition Vec3.h:133
Vec3< T > GetDegr() const
Definition Vec3.h:407
Vec3< T > & operator*=(const T scalar)
Definition Vec3.h:184
T GetDistance(const Vec3< T > &vec) const
Definition Vec3.h:379
T GetAngle(const Vec2< T > &vec) const
Definition Vec3.h:349
Vec3< T > & operator=(const Vec3< C > &vec)
Definition Vec3.h:53
static Vec3 Lerp(const Vec3 &start, const Vec3 &finish, const T t)
Definition Vec3.h:425
Vec3< T > operator+(const T scalar) const
Definition Vec3.h:93
Vec3(const T scalar=0)
Definition Vec3.h:20
Vec3< T > GetAbs() const
Definition Vec3.h:295
Vec3< T > operator*(const T scalar) const
Definition Vec3.h:173
Vec3 operator-()
Definition Vec3.h:253
Vec3< T > GetRads() const
Definition Vec3.h:389
Definition Anchor.h:6
Vec3< Int_32 > Vec3_32
Definition Vec3.h:436
Vec3< UInt_64 > Vec3_u64
Definition Vec3.h:431
Vec3< float > Vec3_f
Definition Vec3.h:443
Vec3< UInt_8 > Vec3_u8
Definition Vec3.h:440
Vec3< Int_8 > Vec3_8
Definition Vec3.h:442
Vec3< Int_16 > Vec3_16
Definition Vec3.h:439
Vec3< double > Vec3_d
Definition Vec3.h:444
Vec3< SInt_8 > Vec3_s8
Definition Vec3.h:441
Vec3< UInt_16 > Vec3_u16
Definition Vec3.h:437
Vec3< SInt_32 > Vec3_s32
Definition Vec3.h:435
Vec3< SInt_64 > Vec3_s64
Definition Vec3.h:432
Vec3< UInt_32 > Vec3_u32
Definition Vec3.h:434
Vec3< Int_64 > Vec3_64
Definition Vec3.h:433
Vec3< SInt_16 > Vec3_s16
Definition Vec3.h:438
@ ERR
Definition Log.h:20