forked from Force67/DawnHook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.h
More file actions
608 lines (467 loc) · 16.6 KB
/
Vector.h
File metadata and controls
608 lines (467 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#pragma once
#include <algorithm>
#include <stdexcept>
#ifndef _WIN32
#include <cmath>
#endif
namespace math
{
// Move this to some math utils file
template <typename T>
constexpr T clamp(const T x, const T min, const T max)
{
#undef min
#undef max
return std::min(max, std::max(min, x));
}
// Lerps between two values depending on the weight
template <class T>
T lerp(const T& from, float fAlpha, const T& to)
{
return (T)((to - from) * fAlpha + from);
}
// Find the relative position of Pos between From and To
inline const float unlerp(const double dFrom, const double dPos, const double dTo)
{
// Avoid dividing by 0 (results in INF values)
if (dFrom == dTo)
return 1.0f;
return static_cast<float>((dPos - dFrom) / (dTo - dFrom));
}
// Unlerp avoiding extrapolation
inline const float unlerpClamped(const double dFrom, const double dPos, const double dTo)
{
return clamp(0.0f, unlerp(dFrom, dPos, dTo), 1.0f);
}
template <typename T>
class basic_vector2
{
private:
T _x = 0, _y = 0;
public:
basic_vector2(T x_, T y_) : _x(x_), _y(y_) {}
basic_vector2() : basic_vector2(T(0), T(0)) {}
T x() const { return _x; }
T y() const { return _y; }
void SetX(T value) { _x = value; }
void SetY(T value) { _y = value; }
T& operator[](size_t i)
{
if (i == 0)
return this->_x;
else if (i == 1)
return this->_y;
throw std::out_of_range("Maximum is 2");
}
basic_vector2 operator-(const basic_vector2& right) const
{
return basic_vector2{this->_x - right._x, this->_y - right._y};
}
basic_vector2 operator-(const T right) const { return basic_vector2{this->_x - right, this->_y - right}; }
basic_vector2 operator+(const basic_vector2& right) const
{
return basic_vector2{this->_x + right._x, this->_y + right._y};
}
basic_vector2 operator+(const T right) const { return basic_vector2{this->_x + right, this->_y + right}; }
basic_vector2 operator*(const basic_vector2& right) const
{
return basic_vector2{this->_x * right._x, this->_y * right._y};
}
basic_vector2 operator*(const T right) const { return basic_vector2{this->_x * right, this->_y * right}; }
basic_vector2 operator/(const basic_vector2& right) const
{
return basic_vector2{this->_x / right._x, this->_y / right._y};
}
basic_vector2 operator/(const T right) const { return basic_vector2{this->_x / right, this->_y / right}; }
basic_vector2& operator/=(const T rigth) const
{
this->_x /= right;
this->_y /= right;
return *this;
}
bool operator>(const basic_vector2& other) const { return this->_x > other._x && this->_y > other._y; }
bool operator>=(const basic_vector2& other) const { return this->_x >= other._x && this->_y >= other._y; }
bool operator<(const basic_vector2& other) const { return this->_x < other._x && this->_y < other._y; }
bool operator<=(const basic_vector2& other) const { return this->_x <= other._x && this->_y <= other._y; }
bool operator==(const basic_vector2& other) const { return this->_x == other._x && this->_y == other._y; }
bool operator!=(const basic_vector2& other) const { return this->_x != other._x || this->_y != other._y; }
basic_vector2& operator+=(const basic_vector2& other)
{
this->_x += other._x;
this->_y += other._y;
return *this;
}
basic_vector2& operator+=(const T other)
{
this->_x += other;
this->_y += other;
return *this;
}
basic_vector2& operator-=(const basic_vector2& other)
{
this->_x -= other._x;
this->_y -= other._y;
return *this;
}
basic_vector2& operator-=(const T other)
{
this->_x -= other;
this->_y -= other;
return *this;
}
basic_vector2& operator*=(const basic_vector2& other)
{
this->_x *= other._x;
this->_y *= other._y;
return *this;
}
basic_vector2& operator*=(const T other)
{
this->_x *= other;
this->_y *= other;
return *this;
}
basic_vector2& operator/=(const basic_vector2& other)
{
this->_x /= other._x;
this->_y /= other._y;
return *this;
}
basic_vector2& operator/=(const T other)
{
this->_x /= other;
this->_y /= other;
return *this;
}
basic_vector2& operator=(const basic_vector2& other)
{
this->_x = other._x;
this->_y = other._y;
return *this;
}
double length() const { return std::sqrt(this->_x * this->_x + this->_y * this->_y); }
/* Returns the length of the vector */
T magnitude() const { return length(); }
T sqrMagnitude() const { return this->_x * this->_x + this->_y * this->_y; }
void normalize() { this->operator/=(this->magnitude()); }
basic_vector2 normalized() const { return this->operator/(this->magnitude()); }
T dot(const basic_vector2& otr) { return this->_x * otr._x + this->_y * otr._y; }
T angle(const basic_vector2& to) { return angle(*this, to); }
static T angle(const basic_vector2& from, const basic_vector2& to) { return 0.0f; }
basic_vector2 lerp(const basic_vector2& to, float t) { return lerp(*this, to, t); }
static basic_vector2 lerp(basic_vector2 from, basic_vector2 to, float t) { return (from + t * (to - from)); }
basic_vector2 slerp(const basic_vector2& to, float t) { return slerp(*this, to, t); }
static basic_vector2 slerp(const basic_vector2& from, const basic_vector2& to, float t)
{
// Dot product - the cosine of the angle between 2 vectors.
float dot = from.dot(to);
// Clamp it to be in the range of Acos()
// This may be unnecessary, but floating point
// precision can be a fickle mistress.
dot = clamp(dot, -1.0f, 1.0f);
// Acos(dot) returns the angle between start and end,
// And multiplying that by percent returns the angle between
// start and the final result.
float theta = std::acos(dot) * t;
basic_vector2 RelativeVec = to - from * dot;
RelativeVec.normalize(); // Orthonormal basis
// The final result.
return ((from * std::cos(theta)) + (RelativeVec * std::sin(theta)));
}
basic_vector2 nlerp(const basic_vector2& to, float t) { return nlerp(*this, to, t); }
static basic_vector2 nlerp(basic_vector2 start, basic_vector2 end, float percent)
{
return lerp(start, end, percent).normalized();
}
const static basic_vector2 one;
const static basic_vector2 right;
const static basic_vector2 up;
const static basic_vector2 zero;
};
template <typename T>
const basic_vector2<T> basic_vector2<T>::one = basic_vector2<T>{1, 1};
template <typename T>
const basic_vector2<T> basic_vector2<T>::right = basic_vector2<T>{0, 1};
template <typename T>
const basic_vector2<T> basic_vector2<T>::up = basic_vector2<T>{1, 0};
template <typename T>
const basic_vector2<T> basic_vector2<T>::zero = basic_vector2<T>{0, 0};
template <typename T>
class basic_vector3
{
public:
T _x = 0, _y = 0, _z = 0;
basic_vector3() : basic_vector3(T(0), T(0), T(0)) {}
basic_vector3(T x_, T y_, T z_) : _x(x_), _y(y_), _z(z_) {}
T x() const { return _x; }
T y() const { return _y; }
T z() const { return _z; }
T& operator[](size_t i)
{
if (i == 0)
return this->_x;
else if (i == 1)
return this->_y;
else if (i == 2)
return this->_z;
throw std::out_of_range("Maximum is 3");
}
void SetX(T value) { _x = value; }
void SetY(T value) { _y = value; }
void SetZ(T value) { _z = value; }
basic_vector3 operator-(const basic_vector3& right) const
{
return basic_vector3{this->_x - right._x, this->_y - right._y, this->_z - right._z};
}
basic_vector3 operator-(const T right) const
{
return basic_vector3{this->_x - right, this->_y - right, this->_z - right};
}
basic_vector3 operator+(const basic_vector3& right) const
{
return basic_vector3{this->_x + right._x, this->_y + right._y, this->_z + right._z};
}
basic_vector3 operator+(const T right) const
{
return basic_vector3{this->_x + right, this->_y + right, this->_z + right};
}
basic_vector3 operator*(const basic_vector3& right) const
{
return basic_vector3{this->_x * right._x, this->_y * right._y, this->_z * right._z};
}
basic_vector3 operator*(const T right) const
{
return basic_vector3{this->_x * right, this->_y * right, this->_z * right};
}
basic_vector3 operator/(const basic_vector3& right) const
{
return basic_vector3{this->_x / right._x, this->_y / right._y, this->_z / right._z};
}
basic_vector3 operator/(const T right) const
{
return basic_vector3{this->_x / right, this->_y / right, this->_z / right};
}
basic_vector3 operator%(T right) const
{
return basic_vector3{fmodf(this->_x, right), fmodf(this->_y, right), fmodf(this->_z, right)};
}
bool operator>(const basic_vector3& other) const
{
return this->_x > other._x && this->_y > other._y && this->_z > other._z;
}
bool operator>=(const basic_vector3& other) const
{
return this->_x >= other._x && this->_y >= other._y && this->_z >= other._z;
}
bool operator<(const basic_vector3& other) const
{
return this->_x < other._x && this->_y < other._y && this->_z < other._z;
}
bool operator<=(const basic_vector3& other) const
{
return this->_x <= other._x && this->_y <= other._y && this->_z <= other._z;
}
bool operator==(const basic_vector3& other) const
{
return this->_x == other._x && this->_y == other._y && this->_z == other._z;
}
bool operator!=(const basic_vector3& other) const
{
return this->_x != other._x || this->_y != other._y || this->_z != other._z;
}
basic_vector3& operator+=(const basic_vector3& other)
{
this->_x += other._x;
this->_y += other._y;
this->_z += other._z;
return *this;
}
basic_vector3& operator+=(const T other)
{
this->_x += other;
this->_y += other;
this->_z += other;
return *this;
}
basic_vector3& operator-=(const basic_vector3& other)
{
this->_x -= other._x;
this->_y -= other._y;
this->_z -= other._z;
return *this;
}
basic_vector3& operator-=(const T other)
{
this->_x -= other;
this->_y -= other;
this->_z -= other;
return *this;
}
basic_vector3& operator*=(const basic_vector3& other)
{
this->_x /= other._x;
this->_y /= other._y;
this->_z /= other._z;
return *this;
}
basic_vector3& operator*=(const T other)
{
this->_x *= other;
this->_y *= other;
this->_z *= other;
return *this;
}
basic_vector3& operator/=(const basic_vector3& other)
{
this->_x /= other._x;
this->_y /= other._y;
this->_z /= other._z;
return *this;
}
basic_vector3& operator/=(const T other)
{
this->_x /= other;
this->_y /= other;
this->_z /= other;
return *this;
}
basic_vector3& operator%=(const T other)
{
this->_x %= other;
this->_y %= other;
this->_z %= other;
return *this;
}
basic_vector3& operator=(const basic_vector3& other)
{
this->_x = other._x;
this->_y = other._y;
this->_z = other._z;
return *this;
}
double length() const { return std::sqrt(this->_x * this->_x + this->_y * this->_y + this->_z * this->_z); }
/* Returns the length of the vector */
T magnitude() const { return length(); }
T sqrMagnitude() const { return this->_x * this->_x + this->_y * this->_y + this->_z * this->_z; }
void normalize() { this->operator/=(this->magnitude()); }
basic_vector3 normalized() const { return this->operator/(this->magnitude()); }
T dot(const basic_vector3& otr) { return this->_x * otr._x + this->_y * otr._y + this->_z * otr._z; }
T angle(const basic_vector3& to) { return angle(*this, to); }
static T angle(const basic_vector3& from, const basic_vector3& to) { return 0.0f; }
// Or should we assign it to self?
basic_vector3 lerp(const basic_vector3& to, float t) { return lerp(*this, to, t); }
static basic_vector3 lerp(const basic_vector3& from, const basic_vector3& to, float t)
{
return (from + t * (to - from));
;
}
basic_vector3 slerp(const basic_vector3& to, float t) { return slerp(*this, to, t); }
static basic_vector3 slerp(const basic_vector3& from, const basic_vector3& to, float t)
{
// Dot product - the cosine of the angle between 2 vectors.
float dot = from.dot(to);
// Clamp it to be in the range of Acos()
// This may be unnecessary, but floating point
// precision can be a fickle mistress.
dot = clamp(dot, -1.0f, 1.0f);
// Acos(dot) returns the angle between start and end,
// And multiplying that by percent returns the angle between
// start and the final result.
float theta = std::acos(dot) * t;
basic_vector3 RelativeVec = to - from * dot;
RelativeVec.normalize(); // Orthonormal basis
// The final result.
return ((from * std::cos(theta)) + (RelativeVec * std::sin(theta)));
}
basic_vector3 nlerp(const basic_vector3& to, float t) { return nlerp(*this, to, t); }
static basic_vector3 nlerp(const basic_vector3& start, const basic_vector3& end, float percent)
{
return lerp(start, end, percent).normalized();
}
const static basic_vector3 back;
const static basic_vector3 down;
const static basic_vector3 forward;
const static basic_vector3 left;
const static basic_vector3 one;
const static basic_vector3 right;
const static basic_vector3 up;
const static basic_vector3 zero;
};
template <typename T>
class basic_vector4
{
public:
T x = 0;
T y = 0;
T z = 0;
T a = 0;
basic_vector4() = default;
basic_vector4(T _x, T _y, T _z, T _a)
{
x = _x;
y = _y;
z = _z;
a = _a;
}
T& operator[](size_t i)
{
if (i == 0)
return this->x;
else if (i == 1)
return this->y;
else if (i == 2)
return this->z;
else if (i == 3)
return this->a;
throw std::out_of_range("Maximum is 4");
}
double length() { return std::sqrt(this->x * this->x + this->y * this->y + this->z * this->z + this->a * this->a); }
};
template <typename T>
const basic_vector3<T> basic_vector3<T>::back = basic_vector3<T>{0, 0, -1};
template <typename T>
const basic_vector3<T> basic_vector3<T>::down = basic_vector3<T>{0, -1, 0};
template <typename T>
const basic_vector3<T> basic_vector3<T>::forward = basic_vector3<T>{0, 0, 1};
template <typename T>
const basic_vector3<T> basic_vector3<T>::left = basic_vector3<T>{-1, 0, 0};
template <typename T>
const basic_vector3<T> basic_vector3<T>::one = basic_vector3<T>{1, 1, 1};
template <typename T>
const basic_vector3<T> basic_vector3<T>::right = basic_vector3<T>{1, 0, 0};
template <typename T>
const basic_vector3<T> basic_vector3<T>::up = basic_vector3<T>{0, 1, 0};
template <typename T>
const basic_vector3<T> basic_vector3<T>::zero = basic_vector3<T>{0, 0, 0};
using Vector2 = basic_vector2<int32_t>;
using Vector3 = basic_vector3<int32_t>;
using Vector4 = basic_vector4<int32_t>;
using Vector2f = basic_vector2<float>;
using Vector3f = basic_vector3<float>;
using Vector4f = basic_vector4<float>;
template <typename T>
basic_vector3<T> operator*(const T left, basic_vector3<T> right)
{
return right * left;
}
using Vector2by = basic_vector2<uint8_t>;
using Vector3by = basic_vector3<uint8_t>;
using Vector4by = basic_vector4<uint8_t>;
using Vector2bo = basic_vector2<bool>;
using Vector3bo = basic_vector3<bool>;
using Vector4bo = basic_vector4<bool>;
inline Vector3f RadianToRotation(math::Vector3f r)
{
float x = (r.x() * (180.0f / 3.14159265358979323846f));
float y = (r.y() * (180.0f / 3.14159265358979323846f));
float z = (r.z() * (180.0f / 3.14159265358979323846f));
return Vector3f{x, y, z};
}
inline Vector3f RotationToRadian(math::Vector3f r)
{
float x = (r.x() * (3.14159265358979323846f / 180.0f));
float y = (r.y() * (3.14159265358979323846f / 180.0f));
float z = (r.z() * (3.14159265358979323846f / 180.0f));
return Vector3f{x, y, z};
}
} // namespace math