-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinalg.hpp
4872 lines (3477 loc) · 120 KB
/
linalg.hpp
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Author: Christian Vallentin <vallentin.source@gmail.com>
// Website: https://vallentin.dev
// Repository: https://github.com/vallentin/LinearAlgebra
//
// Date Created: October 01, 2013
// Last Modified: July 16, 2016
// Copyright (c) 2013-2016 Christian Vallentin <vallentin.source@gmail.com>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
// Refrain from using any exposed macros, functions
// or structs prefixed with an underscore. As these
// are only intended for internal purposes. Which
// additionally means they can be removed, renamed
// or changed between minor updates without notice.
#ifndef LINEAR_ALGEBRA_HPP
#define LINEAR_ALGEBRA_HPP
#define _LINALG_STRINGIFY(str) #str
#define _LINALG_STRINGIFY_TOKEN(str) _LINALG_STRINGIFY(str)
#define LINALG_STRINGIFY_VERSION(major, minor, patch) _LINALG_STRINGIFY(major) "." _LINALG_STRINGIFY(minor) "." _LINALG_STRINGIFY(patch)
#define LINALG_NAME "LinearAlgebra"
#define LINALG_VERSION_MAJOR 1
#define LINALG_VERSION_MINOR 1
#define LINALG_VERSION_PATCH 17
#define LINALG_VERSION LINALG_STRINGIFY_VERSION(LINALG_VERSION_MAJOR, LINALG_VERSION_MINOR, LINALG_VERSION_PATCH)
#define LINALG_NAME_VERSION LINALG_NAME " " LINALG_VERSION
#include <math.h>
#ifdef _IOSTREAM_
#define _LINALG_IN_FIND_BEGIN() \
do { \
stream >> c; \
if (stream.eof()) \
return stream; \
} while (c != '(');
#define _LINALG_IN_FIND_NEXT() \
do { \
stream >> c; \
if (stream.eof()) \
return stream; \
else if (c == ')') \
return stream; \
} while (c != ',');
#define _LINALG_IN_FIND_END() \
do { \
stream >> c; \
if (stream.eof()) \
return stream; \
} while (c != ')');
#endif
#ifndef LINALG_DEFAULT_SCALAR
# define LINALG_DEFAULT_SCALAR float
#endif
// Disable structure padding
#pragma pack(push, 1)
template<typename T> class vec2_t;
template<typename T> class vec3_t;
template<typename T> class vec4_t;
template<typename T> class mat2_t;
template<typename T> class mat3_t;
template<typename T> class mat4_t;
template<typename T> class quat_t;
typedef vec2_t<LINALG_DEFAULT_SCALAR> vec2;
typedef vec2_t<float> fvec2;
typedef vec2_t<double> dvec2;
typedef vec2_t<signed int> ivec2;
typedef vec2_t<unsigned int> uvec2;
typedef vec2_t<bool> bvec2;
typedef vec2_t<signed long> lvec2;
typedef vec2_t<unsigned long> ulvec2;
typedef vec2_t<signed long long> llvec2;
typedef vec2_t<unsigned long long> ullvec2;
typedef vec3_t<LINALG_DEFAULT_SCALAR> vec3;
typedef vec3_t<float> fvec3;
typedef vec3_t<double> dvec3;
typedef vec3_t<signed int> ivec3;
typedef vec3_t<unsigned int> uvec3;
typedef vec3_t<bool> bvec3;
typedef vec3_t<signed long> lvec3;
typedef vec3_t<unsigned long> ulvec3;
typedef vec3_t<signed long long> llvec3;
typedef vec3_t<unsigned long long> ullvec3;
typedef vec4_t<LINALG_DEFAULT_SCALAR> vec4;
typedef vec4_t<float> fvec4;
typedef vec4_t<double> dvec4;
typedef vec4_t<signed int> ivec4;
typedef vec4_t<unsigned int> uvec4;
typedef vec4_t<bool> bvec4;
typedef vec4_t<signed long> lvec4;
typedef vec4_t<unsigned long> ulvec4;
typedef vec4_t<signed long long> llvec4;
typedef vec4_t<unsigned long long> ullvec4;
typedef mat2_t<LINALG_DEFAULT_SCALAR> mat2;
typedef mat2_t<float> fmat2;
typedef mat2_t<double> dmat2;
template<typename T> using mat2x2_t = mat2_t<T>;
typedef mat2x2_t<LINALG_DEFAULT_SCALAR> mat2x2;
typedef mat2x2_t<float> fmat2x2;
typedef mat2x2_t<double> dmat2x2;
typedef mat3_t<LINALG_DEFAULT_SCALAR> mat3;
typedef mat3_t<float> fmat3;
typedef mat3_t<double> dmat3;
template<typename T> using mat3x3_t = mat3_t<T>;
typedef mat3x3_t<LINALG_DEFAULT_SCALAR> mat3x3;
typedef mat3x3_t<float> fmat3x3;
typedef mat3x3_t<double> dmat3x3;
typedef mat4_t<LINALG_DEFAULT_SCALAR> mat4;
typedef mat4_t<float> fmat4;
typedef mat4_t<double> dmat4;
template<typename T> using mat4x4_t = mat4_t<T>;
typedef mat4x4_t<LINALG_DEFAULT_SCALAR> mat4x4;
typedef mat4x4_t<float> fmat4x4;
typedef mat4x4_t<double> dmat4x4;
typedef quat_t<LINALG_DEFAULT_SCALAR> quat;
typedef quat_t<float> fquat;
typedef quat_t<double> dquat;
#if defined(_DEBUG) && !defined(DEBUG)
# define DEBUG 1
#endif
// This was changed from 1E-6 to 1E-4, as asserting rotate(90deg) didn't match.
#define LINALG_EPSILON 1E-4f
#define LINALG_FEQUAL(x, y) ((((y) - LINALG_EPSILON) < (x)) && ((x) < ((y) + LINALG_EPSILON)))
#define LINALG_DEQUAL(x, y) ((((y) - LINALG_EPSILON) < (x)) && ((x) < ((y) + LINALG_EPSILON)))
#define LINALG_PI 3.1415926535897932
#define LINALG_DEG2RAD (LINALG_PI / 180.0)
#define LINALG_RAD2DEG (180.0 / LINALG_PI)
// These annoyingly named Windows macros are interfering with the related vector methods!
#ifdef min
# undef min
#endif
#ifdef max
# undef max
#endif
template<typename T>
class vec2_t
{
private:
typedef vec2_t<T> vec2;
typedef vec3_t<T> vec3;
typedef vec4_t<T> vec4;
public:
static const vec2_t<T> zero;
static const vec2_t<T> one;
static const vec2_t<T> up, down;
static const vec2_t<T> left, right;
public:
// Performs Gram-Schmidt Orthogonalization on 2 basis vectors to turn them into orthonormal basis vectors
static vec2 orthogonalize(const vec2 &a, vec2 &b)
{
b = b - b.project(a);
b = b.normalize();
}
public:
T x, y;
public:
vec2_t() : x(T(0)), y(T(0)) {}
vec2_t(const vec2_t<T> &v) : x(T(v.x)), y(T(v.y)) {}
template<typename T2> vec2_t(const vec2_t<T2> &v) : x(T(v.x)), y(T(v.y)) {}
template<typename T2> vec2_t(const T2 &xy) : x(T(xy)), y(T(xy)) {}
template<typename T2> vec2_t(const T2 &x, const T2 &y) : x(T(x)), y(T(y)) {}
template<typename T2> vec2_t(const T2 *xy) : x(T(xy[0])), y(T(xy[1])) {}
template<typename T2> vec2_t(const vec3_t<T2> &v);
template<typename T2> vec2_t(const vec4_t<T2> &v);
~vec2_t() {}
#pragma region Operator Overloading
#pragma region Member Access Operators
inline T& operator[](const int index) { return (reinterpret_cast<T*>(this))[index]; }
inline T operator[](const int index) const { return ((T*) this)[index]; }
#pragma endregion
#pragma region Arithmetic Operators
vec2 operator+() const { return vec2(+this->x, +this->y); }
vec2 operator-() const { return vec2(-this->x, -this->y); }
friend vec2 operator+(const vec2 &lhs, const vec2 &rhs) { return vec2((lhs.x + rhs.x), (lhs.y + rhs.y)); }
friend vec2 operator-(const vec2 &lhs, const vec2 &rhs) { return vec2((lhs.x - rhs.x), (lhs.y - rhs.y)); }
friend vec2 operator*(const vec2 &lhs, const vec2 &rhs) { return vec2((lhs.x * rhs.x), (lhs.y * rhs.y)); }
friend vec2 operator/(const vec2 &lhs, const vec2 &rhs) { return vec2((lhs.x / rhs.x), (lhs.y / rhs.y)); }
friend vec2 operator%(const vec2 &lhs, const vec2 &rhs) { return vec2((lhs.x % rhs.x), (lhs.y % rhs.y)); }
friend inline vec2 operator+(const vec2 &lhs, const T &rhs) { return (lhs + vec2(rhs)); }
friend inline vec2 operator+(const T &lhs, const vec2 &rhs) { return (vec2(lhs) + rhs); }
friend inline vec2 operator-(const vec2 &lhs, const T &rhs) { return (lhs - vec2(rhs)); }
friend inline vec2 operator-(const T &lhs, const vec2 &rhs) { return (vec2(lhs) - rhs); }
friend inline vec2 operator*(const vec2 &lhs, const T &rhs) { return (lhs * vec2(rhs)); }
friend inline vec2 operator*(const T &lhs, const vec2 &rhs) { return (vec2(lhs) * rhs); }
friend inline vec2 operator/(const vec2 &lhs, const T &rhs) { return (lhs / vec2(rhs)); }
friend inline vec2 operator/(const T &lhs, const vec2 &rhs) { return (vec2(lhs) / rhs); }
friend inline vec2 operator%(const vec2 &lhs, const T &rhs) { return (lhs % vec2(rhs)); }
friend inline vec2 operator%(const T &lhs, const vec2 &rhs) { return (vec2(lhs) % rhs); }
#pragma endregion
#pragma region Increment & Decrement Operators
vec2& operator++() // Prefix
{
++this->x;
++this->y;
return (*this);
}
inline vec2 operator++(int) { return ++(*this); } // Postfix
vec2& operator--() // Prefix
{
--this->x;
--this->y;
return (*this);
}
inline vec2 operator--(int) { return --(*this); } // Postfix
#pragma endregion
#pragma region Assignment Operators
inline vec2& operator+=(const vec2 &rhs) { return ((*this) = ((*this) + rhs)); }
inline vec2& operator+=(const T &rhs) { return ((*this) = ((*this) + rhs)); }
inline vec2& operator-=(const vec2 &rhs) { return ((*this) = ((*this) - rhs)); }
inline vec2& operator-=(const T &rhs) { return ((*this) = ((*this) - rhs)); }
inline vec2& operator*=(const vec2 &rhs) { return ((*this) = ((*this) * rhs)); }
inline vec2& operator*=(const T &rhs) { return ((*this) = ((*this) * rhs)); }
inline vec2& operator/=(const vec2 &rhs) { return ((*this) = ((*this) / rhs)); }
inline vec2& operator/=(const T &rhs) { return ((*this) = ((*this) / rhs)); }
inline vec2& operator%=(const vec2 &rhs) { return ((*this) = ((*this) % rhs)); }
inline vec2& operator%=(const T &rhs) { return ((*this) = ((*this) % rhs)); }
vec2& operator=(const T &rhs)
{
(*this) = vec2(rhs);
return (*this);
}
template<typename T2>
vec2& operator=(const T2 &rhs)
{
(*this) = vec2(rhs);
return (*this);
}
vec2& operator=(const vec2 &rhs)
{
this->x = rhs.x;
this->y = rhs.y;
return (*this);
}
template<typename T2>
vec2& operator=(const vec2_t<T2> &rhs)
{
this->x = T(rhs.x);
this->y = T(rhs.y);
return (*this);
}
#pragma endregion
#pragma region Logical Operators
#pragma endregion
#pragma region Comparison Operators
bool operator==(const vec2 &rhs) const;
friend inline bool operator==(const vec2 &lhs, const T &rhs) { return (lhs == vec2(rhs)); }
friend inline bool operator==(const T &lhs, const vec2 &rhs) { return (vec2(lhs) == rhs); }
friend inline bool operator!=(const vec2 &lhs, const vec2 &rhs) { return !(lhs == rhs); }
friend inline bool operator!=(const vec2 &lhs, const T &rhs) { return (lhs != vec2(rhs)); }
friend inline bool operator!=(const T &lhs, const vec2 &rhs) { return (vec2(lhs) != rhs); }
friend inline bool operator>(const vec2 &lhs, const vec2 &rhs) { return ((lhs.x > rhs.x) && (lhs.y > rhs.y)); }
friend inline bool operator>=(const vec2 &lhs, const vec2 &rhs) { return ((lhs.x >= rhs.x) && (lhs.y >= rhs.y)); }
friend inline bool operator<(const vec2 &lhs, const vec2 &rhs) { return ((lhs.x < rhs.x) && (lhs.y < rhs.y)); }
friend inline bool operator<=(const vec2 &lhs, const vec2 &rhs) { return ((lhs.x <= rhs.x) && (lhs.y <= rhs.y)); }
friend inline bool operator>(const vec2 &lhs, const T &rhs) { return (lhs > vec2(rhs)); }
friend inline bool operator>(const T &lhs, const vec2 &rhs) { return (vec2(lhs) > rhs); }
friend inline bool operator>=(const vec2 &lhs, const T &rhs) { return (lhs >= vec2(rhs)); }
friend inline bool operator>=(const T &lhs, const vec2 &rhs) { return (vec2(lhs) >= rhs); }
friend inline bool operator<(const vec2 &lhs, const T &rhs) { return (lhs < vec2(rhs)); }
friend inline bool operator<(const T &lhs, const vec2 &rhs) { return (vec2(lhs) < rhs); }
friend inline bool operator<=(const vec2 &lhs, const T &rhs) { return (lhs <= vec2(rhs)); }
friend inline bool operator<=(const T &lhs, const vec2 &rhs) { return (vec2(lhs) <= rhs); }
#pragma endregion
#pragma region Cast Operators
inline explicit operator T*() const
{
return reinterpret_cast<T*>(this);
}
template<typename T2>
inline operator vec2_t<T2>() const
{
return vec2_t<T2>(
static_cast<T2>(this->x),
static_cast<T2>(this->y)
);
}
#pragma endregion
#pragma region Stream Operators
#ifdef _IOSTREAM_
friend inline std::ostream& operator<<(std::ostream &stream, const vec2 &rhs)
{
return (stream << "vec2(" << rhs.x << ", " << rhs.y << ")");
}
friend inline std::wostream& operator<<(std::wostream &stream, const vec2 &rhs)
{
return (stream << L"vec2(" << rhs.x << L", " << rhs.y << L")");
}
friend inline std::istream& operator>>(std::istream &stream, vec2 &rhs)
{
rhs = vec2::zero;
char c;
_LINALG_IN_FIND_BEGIN();
stream >> rhs.x;
_LINALG_IN_FIND_NEXT();
stream >> rhs.y;
_LINALG_IN_FIND_END();
return stream;
}
friend inline std::wistream& operator>>(std::wistream &stream, vec2 &rhs)
{
rhs = vec2::zero;
wchar_t c;
_LINALG_IN_FIND_BEGIN();
stream >> rhs.x;
_LINALG_IN_FIND_NEXT();
stream >> rhs.y;
_LINALG_IN_FIND_END();
return stream;
}
#endif
#pragma endregion
#pragma endregion
#pragma region
inline T dot(const vec2 &rhs) const
{
return (this->x * rhs.x + this->y * rhs.y);
}
friend inline T dot(const vec2 &lhs, const vec2 &rhs) { return lhs.dot(rhs); }
vec2 cross(const vec2 &rhs) const
{
return vec2(
((this->y * rhs.z) - (this->z * rhs.y)),
((this->z * rhs.x) - (this->x * rhs.z))
);
}
friend inline vec2 cross(const vec2 &lhs, const vec2 &rhs) { return lhs.cross(rhs); }
inline T lengthSquared() const { return (this->x * this->x + this->y * this->y); }
friend inline T lengthSquared(const vec2 &lhs) { return lhs.lengthSquared(); }
inline T length2() const { return (this->x * this->x + this->y * this->y); }
friend inline T length2(const vec2 &lhs) { return lhs.lengthSquared(); }
inline T length() const { return sqrt(lengthSquared()); }
friend inline T length(const vec2 &lhs) { return lhs.length(); }
inline T distanceSquared(const vec2 &rhs) { return ((*this) - rhs).lengthSquared(); }
friend inline T distanceSquared(const vec2 &lhs, const vec2 &rhs) { return lhs.distanceSquared(rhs); }
inline T distance(const vec2 &rhs) { return ((*this) - rhs).length(); }
friend inline T distance(const vec2 &lhs, const vec2 &rhs) { return lhs.distance(rhs); }
vec2 normalize(const T &to = T(1.0)) const;
friend inline vec2 normalize(const vec2 &lhs, const T &to = T(1.0)) { return lhs.normalize(to); }
T angle(const vec2 &rhs) const
{
return (dot(rhs) / (length() * rhs.length()));
}
friend inline T angle(const vec2 &lhs, const vec2 &rhs) { return lhs.angle(rhs); }
// Calculates the projection of a onto b
//
// Reference: http://en.wikipedia.org/wiki/Vector_projection#Vector_projection_2
inline vec2 project(const vec2 &b) const
{
const float length = b.length();
return ((dot(b) / (length * length)) * b);
}
friend inline vec2 project(const vec2 &a, const vec2 &b) { return a.project(b); }
// Calculates the components of a perpendicular to b
inline vec2 perpendicular(const vec2 &b) const
{
const float length = b.length();
return ((*this) - ((dot(b) / (length * length)) * b));
}
friend inline vec2 perpendicular(const vec2 &a, const vec2 &b) { return a.perpendicular(b); }
// Calculates the reflection vector from entering ray direction a and surface normal b
inline vec2 reflect(const vec2 &b) const
{
return (T(2) * project(b) - (*this));
}
friend inline vec2 reflect(const vec2 &a, const vec2 &b) { return a.reflect(b); }
inline T cosine(const vec2 &b) const
{
return normalize().dot(b.normalize());
}
friend inline T cosine(const vec2 &a, const vec2 &b) { return a.cosine(b); }
inline vec2 rotate(const T theta) const
{
const T c = cos(theta), s = sin(theta);
return vec2(
(c * this->x - s * this->y),
(s * this->x + c * this->y)
);
}
friend inline vec2 rotate(const vec2 &a, const T theta) { return a.rotate(theta); }
#pragma endregion
#pragma region
inline bool isNullVector() const;
friend inline bool isNullVector(const vec2 &v) { return v.isNullVector(); }
inline bool isUnitVector() const;
friend inline bool isUnitVector(const vec2 &v) { return v.isUnitVector(); }
inline bool isNormalized(const T &to = T(1.0)) const;
friend inline bool isNormalized(const vec2 &v) { return v.isNormalized(); }
inline bool isOrthogonalTo(const vec2 &rhs) const;
friend inline bool isOrthogonalTo(const vec2 &lhs, const vec2 &rhs) { return (lhs.isOrthogonalTo(rhs)); }
inline bool isPerpendicularTo(const vec2 &rhs) const;
friend inline bool isPerpendicularTo(const vec2 &lhs, const vec2 &rhs) { return (lhs.isPerpendicularTo(rhs)); }
inline bool isParallelTo(const vec2 &rhs) const;
friend inline bool isParallelTo(const vec2 &lhs, const vec2 &rhs) { return (lhs.isParallelTo(rhs)); }
#pragma endregion
#pragma region
inline vec2 abs() const { return vec2(abs(this->x), abs(this->y), abs(this->z)); }
friend inline vec2 abs(const vec2 &v) { return v.abs(); }
inline vec2 max(const vec2 &rhs) const
{
return vec2(
((this->x > rhs.x) ? this->x : rhs.x),
((this->y > rhs.y) ? this->y : rhs.y)
);
}
friend inline vec2 max(const vec2 &lhs, const vec2 &rhs) { return lhs.max(rhs); }
inline vec2 min(const vec2 &rhs) const
{
return vec2(
((this->x < rhs.x) ? this->x : rhs.x),
((this->y < rhs.y) ? this->y : rhs.y)
);
}
friend inline vec2 min(const vec2 &lhs, const vec2 &rhs) { return lhs.min(rhs); }
inline vec2 clamp(const vec2 &min, const vec2 &max) const
{
return min(max).max(min);
}
friend inline vec2 clamp(const vec2 &v, const vec2 &min, const vec2 &max) { return v.clamp(min, max); }
inline vec2 lerp(const vec2 &to, const T &t) const { return ((*this) + t * (to - (*this))); }
friend inline vec2 lerp(const vec2 &from, const vec2 &to, const T &t) { return from.lerp(to, t); }
inline vec2 lerp(const vec2 &to, const vec2 &t) const { return ((*this) + t * (to - (*this))); }
friend inline vec2 lerp(const vec2 &from, const vec2 &to, const vec2 &t) { return from.lerp(to, t); }
// Reference: https://en.wikipedia.org/wiki/Slerp
// Referemce: https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
inline vec2 slerp(const vec2 &to, const T &t) const
{
const T dot = normalize().dot(to.normalize());
const T theta = acos(dot);
const T s = sin(theta);
return (sin((T(1) - t) * theta) / s * (*this) + sin(t * theta) / s * to);
}
friend inline vec2 slerp(const vec2 &from, const vec2 &to, const T &t) { return from.slerp(to, t); }
// Reference: https://en.wikipedia.org/wiki/Slerp
// Referemce: https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
inline vec2 slerp(const vec2 &to, const vec2 &t) const
{
const T dot = normalize().dot(to.normalize());
const T theta = acos(dot);
const T s = sin(theta);
return (sin((T(1) - t) * theta) / s * (*this) + sin(t * theta) / s * to);
}
friend inline vec2 slerp(const vec2 &from, const vec2 &to, const vec2 &t) { return from.slerp(to, t); }
// Reference: https://en.wikipedia.org/wiki/Sign_function
inline const vec2 signum() const
{
return vec2(
((this->x < T(0)) ? T(-1) : ((this->x > T(0)) ? T(1) : T(0))),
((this->y < T(0)) ? T(-1) : ((this->y > T(0)) ? T(1) : T(0)))
);
}
friend inline vec2 signum(const vec2 &v) { return v.signum(); }
#pragma endregion
#pragma region Swizzling
#define LINALG_SWIZZLE_INDEX(index, c) \
if ((c == 'X') || (c == 'X') || (c == 'r') || (c == 'R') || (c == 's') || (c == 'S')) index = 0; \
else if ((c == 'y') || (c == 'Y') || (c == 'g') || (c == 'G') || (c == 't') || (c == 'T')) index = 1; \
else index = 0;
vec2 swizzle(const char x, const char y) const
{
int x_index = 0, y_index = 1;
LINALG_SWIZZLE_INDEX(x_index, x);
LINALG_SWIZZLE_INDEX(y_index, y);
return vec2((*this)[x_index], (*this)[y_index]);
}
#undef LINALG_SWIZZLE_INDEX
#pragma endregion
inline void swap(vec2 &other)
{
const vec2 tmp(*this);
(*this) = other;
other = tmp;
}
friend inline void swap(vec2 &a, vec2 &b) { a.swap(b); }
};
template<typename T>
class vec3_t
{
private:
typedef vec2_t<T> vec2;
typedef vec3_t<T> vec3;
typedef vec4_t<T> vec4;
public:
static const vec3_t<T> zero;
static const vec3_t<T> one;
static const vec3_t<T> up, down;
static const vec3_t<T> left, right;
static const vec3_t<T> forward, backward;
public:
// Performs Gram-Schmidt Orthogonalization on 2 basis vectors to turn them into orthonormal basis vectors
static inline vec3 orthogonalize(const vec3 &a, vec3 &b)
{
b = b - b.project(a);
b = b.normalize();
}
// Performs Gram-Schmidt Orthogonalization on 3 basis vectors to turn them into orthonormal basis vectors
static inline void orthogonalize(const vec3 &a, vec3 &b, vec3 &c)
{
b = b - b.project(a);
b = b.normalize();
c = c - c.project(a) - c.project(b);
c = c.normalize();
}
// The order of the vertices given will affect the direction of the resulting normal.
// The front face of the triangle is considered to be the counter-clockwise order of
// the three vertices given.
static inline vec3 surfaceNormal(const vec3 &point1, const vec3 &point2, const vec3 &point3)
{
const vec3 u = point3 - point1;
const vec3 v = point2 - point1;
const vec3 normal = u.cross(v);
return normal.normalize();
}
public:
T x, y, z;
public:
vec3_t() : x(T(0)), y(T(0)), z(T(0)) {}
vec3_t(const vec3_t<T> &v) : x(v.x), y(v.y), z(v.z) {}
template<typename T2> vec3_t(const vec3_t<T2> &v) : x(T(v.x)), y(T(v.y)), z(T(v.z)) {}
template<typename T2> vec3_t(const T2 &xyz) : x(T(xyz)), y(T(xyz)), z(T(xyz)) {}
template<typename T2> vec3_t(const T2 &x, const T2 &y, const T2 &z = T(0)) : x(T(x)), y(T(y)), z(T(z)) {}
template<typename T2> vec3_t(const T2 *xyz) : x(T(xyz[0])), y(T(xyz[1])), z(T(xyz[2])) {}
template<typename T2, typename T3> vec3_t(const vec2_t<T2> &xy, const T3 &z = T3(0)) : x(T(xy.x)), y(T(xy.y)), z(T(z)) {}
template<typename T2, typename T3> vec3_t(const T2 &x, const vec2_t<T3> &yz) : x(T(x)), y(T(yz.x)), z(T(yz.y)) {}
template<typename T2> vec3_t(const vec4_t<T2> &v);
~vec3_t() {}
#pragma region Operator Overloading
#pragma region Member Access Operators
inline T& operator[](const int index) { return (reinterpret_cast<T*>(this))[index]; }
inline T operator[](const int index) const { return ((T*) this)[index]; }
#pragma endregion
#pragma region Arithmetic Operators
vec3 operator+() const { return vec3(+this->x, +this->y, +this->z); }
vec3 operator-() const { return vec3(-this->x, -this->y, -this->z); }
friend vec3 operator+(const vec3 &lhs, const vec3 &rhs) { return vec3((lhs.x + rhs.x), (lhs.y + rhs.y), (lhs.z + rhs.z)); }
friend vec3 operator-(const vec3 &lhs, const vec3 &rhs) { return vec3((lhs.x - rhs.x), (lhs.y - rhs.y), (lhs.z - rhs.z)); }
friend vec3 operator*(const vec3 &lhs, const vec3 &rhs) { return vec3((lhs.x * rhs.x), (lhs.y * rhs.y), (lhs.z * rhs.z)); }
friend vec3 operator/(const vec3 &lhs, const vec3 &rhs) { return vec3((lhs.x / rhs.x), (lhs.y / rhs.y), (lhs.z / rhs.z)); }
friend vec3 operator%(const vec3 &lhs, const vec3 &rhs) { return vec3((lhs.x % rhs.x), (lhs.y % rhs.y), (lhs.z % rhs.z)); }
friend inline vec3 operator+(const vec3 &lhs, const T &rhs) { return (lhs + vec3(rhs)); }
friend inline vec3 operator+(const T &lhs, const vec3 &rhs) { return (vec3(lhs) + rhs); }
friend inline vec3 operator-(const vec3 &lhs, const T &rhs) { return (lhs - vec3(rhs)); }
friend inline vec3 operator-(const T &lhs, const vec3 &rhs) { return (vec3(lhs) - rhs); }
friend inline vec3 operator*(const vec3 &lhs, const T &rhs) { return (lhs * vec3(rhs)); }
friend inline vec3 operator*(const T &lhs, const vec3 &rhs) { return (vec3(lhs) * rhs); }
friend inline vec3 operator/(const vec3 &lhs, const T &rhs) { return (lhs / vec3(rhs)); }
friend inline vec3 operator/(const T &lhs, const vec3 &rhs) { return (vec3(lhs) / rhs); }
friend inline vec3 operator%(const vec3 &lhs, const T &rhs) { return (lhs % vec3(rhs)); }
friend inline vec3 operator%(const T &lhs, const vec3 &rhs) { return (vec3(lhs) % rhs); }
#pragma endregion
#pragma region Increment & Decrement Operators
vec3& operator++() // Prefix
{
++this->x;
++this->y;
++this->z;
return (*this);
}
inline vec3 operator++(int) { return ++(*this); } // Postfix
vec3& operator--() // Prefix
{
--this->x;
--this->y;
--this->z;
return (*this);
}
inline vec3 operator--(int) { return --(*this); } // Postfix
#pragma endregion
#pragma region Assignment Operators
inline vec3& operator+=(const vec3 &rhs) { return ((*this) = ((*this) + rhs)); }
inline vec3& operator+=(const T &rhs) { return ((*this) = ((*this) + rhs)); }
inline vec3& operator-=(const vec3 &rhs) { return ((*this) = ((*this) - rhs)); }
inline vec3& operator-=(const T &rhs) { return ((*this) = ((*this) - rhs)); }
inline vec3& operator*=(const vec3 &rhs) { return ((*this) = ((*this) * rhs)); }
inline vec3& operator*=(const T &rhs) { return ((*this) = ((*this) * rhs)); }
inline vec3& operator/=(const vec3 &rhs) { return ((*this) = ((*this) / rhs)); }
inline vec3& operator/=(const T &rhs) { return ((*this) = ((*this) / rhs)); }
inline vec3& operator%=(const vec3 &rhs) { return ((*this) = ((*this) % rhs)); }
inline vec3& operator%=(const T &rhs) { return ((*this) = ((*this) % rhs)); }
vec3& operator=(const T &rhs)
{
(*this) = vec3(rhs);
return (*this);
}
template<typename T2>
vec3& operator=(const T2 &rhs)
{
(*this) = vec3(rhs);
return (*this);
}
vec3& operator=(const vec3 &rhs)
{
this->x = rhs.x;
this->y = rhs.y;
this->z = rhs.z;
return (*this);
}
template<typename T2>
vec3& operator=(const vec3_t<T2> &rhs)
{
this->x = T(rhs.x);
this->y = T(rhs.y);
this->z = T(rhs.z);
return (*this);
}
#pragma endregion
#pragma region Logical Operators
#pragma endregion
#pragma region Comparison Operators
bool operator==(const vec3 &rhs) const;
friend inline bool operator==(const vec3 &lhs, const T &rhs) { return (lhs == vec3(rhs)); }
friend inline bool operator==(const T &lhs, const vec3 &rhs) { return (vec3(lhs) == rhs); }
friend inline bool operator!=(const vec3 &lhs, const vec3 &rhs) { return !(lhs == rhs); }
friend inline bool operator!=(const vec3 &lhs, const T &rhs) { return (lhs != vec3(rhs)); }
friend inline bool operator!=(const T &lhs, const vec3 &rhs) { return (vec3(lhs) != rhs); }
friend inline bool operator>(const vec3 &lhs, const vec3 &rhs) { return ((lhs.x > rhs.x) && (lhs.y > rhs.y) && (lhs.z > rhs.z)); }
friend inline bool operator>=(const vec3 &lhs, const vec3 &rhs) { return ((lhs.x >= rhs.x) && (lhs.y >= rhs.y) && (lhs.z >= rhs.z)); }
friend inline bool operator<(const vec3 &lhs, const vec3 &rhs) { return ((lhs.x < rhs.x) && (lhs.y < rhs.y) && (lhs.z < rhs.z)); }
friend inline bool operator<=(const vec3 &lhs, const vec3 &rhs) { return ((lhs.x <= rhs.x) && (lhs.y <= rhs.y) && (lhs.z <= rhs.z)); }
friend inline bool operator>(const vec3 &lhs, const T &rhs) { return (lhs > vec3(rhs)); }
friend inline bool operator>(const T &lhs, const vec3 &rhs) { return (vec3(lhs) > rhs); }
friend inline bool operator>=(const vec3 &lhs, const T &rhs) { return (lhs >= vec3(rhs)); }
friend inline bool operator>=(const T &lhs, const vec3 &rhs) { return (vec3(lhs) >= rhs); }
friend inline bool operator<(const vec3 &lhs, const T &rhs) { return (lhs < vec3(rhs)); }
friend inline bool operator<(const T &lhs, const vec3 &rhs) { return (vec3(lhs) < rhs); }
friend inline bool operator<=(const vec3 &lhs, const T &rhs) { return (lhs <= vec3(rhs)); }
friend inline bool operator<=(const T &lhs, const vec3 &rhs) { return (vec3(lhs) <= rhs); }
#pragma endregion
#pragma region Cast Operators
inline explicit operator T*() const
{
return reinterpret_cast<T*>(this);
}
template<typename T2>
inline operator vec3_t<T2>() const
{
return vec3_t<T2>(
static_cast<T2>(this->x),
static_cast<T2>(this->y),
static_cast<T2>(this->z)
);
}
#pragma endregion
#pragma region Stream Operators
#ifdef _IOSTREAM_
friend inline std::ostream& operator<<(std::ostream &stream, const vec3 &rhs)
{
return (stream << "vec3(" << rhs.x << ", " << rhs.y << ", " << rhs.z << ")");
}
friend inline std::wostream& operator<<(std::wostream &stream, const vec3 &rhs)
{
return (stream << L"vec3(" << rhs.x << L", " << rhs.y << L", " << rhs.z << L")");
}
friend inline std::istream& operator>>(std::istream &stream, vec3 &rhs)
{
rhs = vec3::zero;
char c;
_LINALG_IN_FIND_BEGIN();
stream >> rhs.x;
_LINALG_IN_FIND_NEXT();
stream >> rhs.y;
_LINALG_IN_FIND_NEXT();
stream >> rhs.z;
_LINALG_IN_FIND_END();
return stream;
}
friend inline std::wistream& operator>>(std::wistream &stream, vec3 &rhs)
{