-
Notifications
You must be signed in to change notification settings - Fork 0
/
svml.h
2978 lines (2810 loc) · 216 KB
/
svml.h
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
#ifndef SVML_H
#define SVML_H
/*
SVML - Simple Vector Math Library
Author: Dwight House ( http://dwighthouse.com )
Assistance by: Dr. Dmitri Volper, Mike Fried, Jeff Hubbard, Chris Peters
Instructions and Information: https://github.com/dwighthouse/svml
Colossians 3:23-24
Copyright (c) 2010-2015 Dwight House
*/
#include <iostream> // cout, endl
#include <sstream> // ostream, ostringstream, string
#include <math.h> // sqrt, fabs, min, max, ceil, floor, sin, cos
namespace SVML
{
//----------------------------------------------------------------------
//
// Sec. 01 - Startup, globals, predefinitions, types
//
//----------------------------------------------------------------------
using std::cout;
using std::endl;
using std::string;
using std::ostream;
using std::ostringstream;
using std::max;
using std::min;
// Typedefs
typedef float SCALAR_TYPE;
// Global constants
const SCALAR_TYPE COMPARISON_EPSILON = (SCALAR_TYPE)0.000001; // Used by AlmostEqual()
// Predefines
template <typename TYPE> union VECTOR2;
template <typename TYPE> union VECTOR3;
template <typename TYPE> union VECTOR4;
// Default types (BUILT-IN TYPE CUSTOMIZATION HERE!)
typedef VECTOR2<float> vec2;
typedef VECTOR3<float> vec3;
typedef VECTOR4<float> vec4;
// etc.
//----------------------------------------------------------------------
//
// Sec. 02 - Math utilities not specific to vectors
//
//----------------------------------------------------------------------
inline SCALAR_TYPE DegToRad(const SCALAR_TYPE& degrees)
{
return degrees * (SCALAR_TYPE)0.017453292519943; // (pi / 180)
}
inline SCALAR_TYPE RadToDeg(const SCALAR_TYPE& radians)
{
return radians * (SCALAR_TYPE)57.295779513082325; // (180 / pi)
}
inline SCALAR_TYPE Lerp(const SCALAR_TYPE& start, const SCALAR_TYPE& end, const SCALAR_TYPE& delta)
{
return start + delta * (end - start);
}
//----------------------------------------------------------------------
//
// Sec. 03 - SFINAE utilities
//
//----------------------------------------------------------------------
// EnableIf
template <typename TYPE, typename R, bool b = TYPE::value> struct EnableIf {};
template <typename TYPE, typename R> struct EnableIf<TYPE, R, true> { typedef R type; };
// Generic types
template <typename TYPE> struct Is2D { enum { value = false }; };
template <typename TYPE> struct Is3D { enum { value = false }; };
template <typename TYPE> struct Is4D { enum { value = false }; };
// Specific type
template <typename TYPE> struct Is2D< VECTOR2<TYPE> > { enum { value = true }; };
template <typename TYPE> struct Is3D< VECTOR3<TYPE> > { enum { value = true }; };
template <typename TYPE> struct Is4D< VECTOR4<TYPE> > { enum { value = true }; };
//----------------------------------------------------------------------
//
// Sec. 04 - 2D vector type and associated functions
//
//----------------------------------------------------------------------
template <typename TYPE>
union VECTOR2
{
private:
struct { TYPE x, y; } v;
struct s1 { protected: TYPE x, y; };
struct s2 { protected: TYPE x, y; public: typedef VECTOR2<TYPE> PARENT; };
struct s3 { protected: TYPE x, y; public: typedef VECTOR3<TYPE> PARENT; };
struct s4 { protected: TYPE x, y; public: typedef VECTOR4<TYPE> PARENT; };
struct X : s1
{
operator TYPE() const { return s1::x; }
const X& operator=(const TYPE& rhs) { s1::x = rhs; return *this; }
const X& operator+=(const TYPE& rhs) { s1::x += rhs; return *this; }
const X& operator-=(const TYPE& rhs) { s1::x -= rhs; return *this; }
const X& operator*=(const TYPE& rhs) { s1::x *= rhs; return *this; }
const X& operator/=(const TYPE& rhs) { s1::x /= rhs; return *this; }
};
struct Y : s1
{
operator TYPE() const { return s1::y; }
const Y& operator=(const TYPE& rhs) { s1::y = rhs; return *this; }
const Y& operator+=(const TYPE& rhs) { s1::y += rhs; return *this; }
const Y& operator-=(const TYPE& rhs) { s1::y -= rhs; return *this; }
const Y& operator*=(const TYPE& rhs) { s1::y *= rhs; return *this; }
const Y& operator/=(const TYPE& rhs) { s1::y /= rhs; return *this; }
};
struct XX : s2 { operator VECTOR2<TYPE>() const { return VECTOR2<TYPE>(s2::x, s2::x); } };
struct XY : s2
{
operator VECTOR2<TYPE>() const { return VECTOR2<TYPE>(s2::x, s2::y); }
const XY& operator=(const VECTOR2<TYPE>& rhs) { s2::x = rhs.v.x; s2::y = rhs.v.y; return *this; }
const XY& operator+=(const VECTOR2<TYPE>& rhs) { s2::x += rhs.v.x; s2::y += rhs.v.y; return *this; }
const XY& operator-=(const VECTOR2<TYPE>& rhs) { s2::x -= rhs.v.x; s2::y -= rhs.v.y; return *this; }
const XY& operator*=(const VECTOR2<TYPE>& rhs) { s2::x *= rhs.v.x; s2::y *= rhs.v.y; return *this; }
const XY& operator/=(const VECTOR2<TYPE>& rhs) { s2::x /= rhs.v.x; s2::y /= rhs.v.y; return *this; }
const XY& operator*=(const TYPE& rhs) { s2::x *= rhs; s2::y *= rhs; return *this; }
const XY& operator/=(const TYPE& rhs) { s2::x /= rhs; s2::y /= rhs; return *this; }
};
struct YX : s2
{
operator VECTOR2<TYPE>() const { return VECTOR2<TYPE>(s2::y, s2::x); }
const YX& operator=(const VECTOR2<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::y = rhs.v.x; s2::x = t0; return *this; }
const YX& operator+=(const VECTOR2<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::y += rhs.v.x; s2::x += t0; return *this; }
const YX& operator-=(const VECTOR2<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::y -= rhs.v.x; s2::x -= t0; return *this; }
const YX& operator*=(const VECTOR2<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::y *= rhs.v.x; s2::x *= t0; return *this; }
const YX& operator/=(const VECTOR2<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::y /= rhs.v.x; s2::x /= t0; return *this; }
const YX& operator*=(const TYPE& rhs) { s2::y *= rhs; s2::x *= rhs; return *this; }
const YX& operator/=(const TYPE& rhs) { s2::y /= rhs; s2::x /= rhs; return *this; }
};
struct YY : s2 { operator VECTOR2<TYPE>() const { return VECTOR2<TYPE>(s2::y, s2::y); } };
struct XXX : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::x, s3::x, s3::x); } };
struct XXY : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::x, s3::x, s3::y); } };
struct XYX : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::x, s3::y, s3::x); } };
struct XYY : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::x, s3::y, s3::y); } };
struct YXX : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::y, s3::x, s3::x); } };
struct YXY : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::y, s3::x, s3::y); } };
struct YYX : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::y, s3::y, s3::x); } };
struct YYY : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::y, s3::y, s3::y); } };
struct XXXX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::x, s4::x, s4::x); } };
struct XXXY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::x, s4::x, s4::y); } };
struct XXYX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::x, s4::y, s4::x); } };
struct XXYY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::x, s4::y, s4::y); } };
struct XYXX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::y, s4::x, s4::x); } };
struct XYXY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::y, s4::x, s4::y); } };
struct XYYX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::y, s4::y, s4::x); } };
struct XYYY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::y, s4::y, s4::y); } };
struct YXXX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::x, s4::x, s4::x); } };
struct YXXY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::x, s4::x, s4::y); } };
struct YXYX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::x, s4::y, s4::x); } };
struct YXYY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::x, s4::y, s4::y); } };
struct YYXX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::y, s4::x, s4::x); } };
struct YYXY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::y, s4::x, s4::y); } };
struct YYYX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::y, s4::y, s4::x); } };
struct YYYY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::y, s4::y, s4::y); } };
// Length property
struct LENGTH
{
private:
TYPE x, y;
TYPE ApplyLength(const TYPE& curMag, const TYPE& newMag)
{
TYPE inverseScaledMagnitude = newMag / curMag;
x *= inverseScaledMagnitude;
y *= inverseScaledMagnitude;
return newMag;
}
public:
operator TYPE() const { return sqrt(x * x + y * y); }
TYPE operator=(const TYPE& rhs) { return ApplyLength((TYPE)(*this), rhs); }
TYPE operator+=(const TYPE& rhs) { TYPE curMag = (TYPE)(*this); return ApplyLength(curMag, curMag + rhs); }
TYPE operator-=(const TYPE& rhs) { TYPE curMag = (TYPE)(*this); return ApplyLength(curMag, curMag - rhs); }
TYPE operator*=(const TYPE& rhs) { TYPE curMag = (TYPE)(*this); return ApplyLength(curMag, curMag * rhs); }
TYPE operator/=(const TYPE& rhs) { TYPE curMag = (TYPE)(*this); return ApplyLength(curMag, curMag / rhs); }
};
public:
VECTOR2() {}
VECTOR2(const TYPE& x, const TYPE& y) { v.x = x; v.y = y; }
// Swizzle variables
X x, r, s;
Y y, g, t;
XX xx, rr, ss;
XY xy, rg, st;
YX yx, gr, ts;
YY yy, gg, tt;
XXX xxx, rrr, sss;
XXY xxy, rrg, sst;
XYX xyx, rgr, sts;
XYY xyy, rgg, stt;
YXX yxx, grr, tss;
YXY yxy, grg, tst;
YYX yyx, ggr, tts;
YYY yyy, ggg, ttt;
XXXX xxxx, rrrr, ssss;
XXXY xxxy, rrrg, ssst;
XXYX xxyx, rrgr, ssts;
XXYY xxyy, rrgg, sstt;
XYXX xyxx, rgrr, stss;
XYXY xyxy, rgrg, stst;
XYYX xyyx, rggr, stts;
XYYY xyyy, rggg, sttt;
YXXX yxxx, grrr, tsss;
YXXY yxxy, grrg, tsst;
YXYX yxyx, grgr, tsts;
YXYY yxyy, grgg, tstt;
YYXX yyxx, ggrr, ttss;
YYXY yyxy, ggrg, ttst;
YYYX yyyx, gggr, ttts;
YYYY yyyy, gggg, tttt;
// Length property
LENGTH Length;
// Assignment operators (default for = is fine)
const VECTOR2& operator+=(const VECTOR2& rhs) { v.x += rhs.v.x; v.y += rhs.v.y; return *this; }
const VECTOR2& operator-=(const VECTOR2& rhs) { v.x -= rhs.v.x; v.y -= rhs.v.y; return *this; }
const VECTOR2& operator*=(const VECTOR2& rhs) { v.x *= rhs.v.x; v.y *= rhs.v.y; return *this; }
const VECTOR2& operator/=(const VECTOR2& rhs) { v.x /= rhs.v.x; v.y /= rhs.v.y; return *this; }
const VECTOR2& operator*=(const TYPE& rhs) { v.x *= rhs; v.y *= rhs; return *this; }
const VECTOR2& operator/=(const TYPE& rhs) { v.x /= rhs; v.y /= rhs; return *this; }
// Array notation access
const TYPE& operator[](const unsigned& index)
{
if (index == 0) { return v.x; }
else if (index == 1) { return v.y; }
else { cout << "Fatal Error: Attempted out of bounds bracket access of 2D vector." << endl << " - Vector: " << *this << endl << " - Index: " << index << endl; exit(-1); }
}
// Overload for cout
friend ostream& operator<<(ostream& os, const VECTOR2<TYPE>& printVector)
{
os << "(" << printVector.v.x << ", " << printVector.v.y << ")";
return os;
}
// Negation
VECTOR2 operator-() const { return VECTOR2(-v.x, -v.y); }
void Normalize() { *this /= this->Length; }
};
// 2D ToString()
template <typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, string >::type ToString(const SWIZZLE& printSwizzle) { return ToString(typename SWIZZLE::PARENT(printSwizzle)); }
template <typename TYPE> string ToString(const VECTOR2<TYPE>& printVector)
{
std::ostringstream buffer;
buffer << "(" << printVector.x << ", " << printVector.y << ")";
return buffer.str();
}
// 2D Negate [-]
template <typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type operator-(const SWIZZLE& toNegate)
{
return -(typename SWIZZLE::PARENT(toNegate));
}
// 2D Addition [+]
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, typename SWIZZLE0::PARENT >::type >::type operator+(const SWIZZLE0& lhs, const SWIZZLE1& rhs) { return typename SWIZZLE0::PARENT(lhs) + typename SWIZZLE1::PARENT(rhs); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type operator+(const SWIZZLE& lhs, const VECTOR2<TYPE>& rhs) { return typename SWIZZLE::PARENT(lhs) + rhs; }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type operator+(const VECTOR2<TYPE>& lhs, const SWIZZLE& rhs) { return lhs + typename SWIZZLE::PARENT(rhs); }
template <typename TYPE> VECTOR2<TYPE> operator+(const VECTOR2<TYPE>& lhs, const VECTOR2<TYPE>& rhs)
{
return VECTOR2<TYPE>(lhs.x + rhs.x, lhs.y + rhs.y);
}
// 2D Subtraction [-]
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, typename SWIZZLE0::PARENT >::type >::type operator-(const SWIZZLE0& lhs, const SWIZZLE1& rhs) { return typename SWIZZLE0::PARENT(lhs) - typename SWIZZLE1::PARENT(rhs); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type operator-(const SWIZZLE& lhs, const VECTOR2<TYPE>& rhs) { return typename SWIZZLE::PARENT(lhs) - rhs; }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type operator-(const VECTOR2<TYPE>& lhs, const SWIZZLE& rhs) { return lhs - typename SWIZZLE::PARENT(rhs); }
template <typename TYPE> VECTOR2<TYPE> operator-(const VECTOR2<TYPE>& lhs, const VECTOR2<TYPE>& rhs)
{
return VECTOR2<TYPE>(lhs.x - rhs.x, lhs.y - rhs.y);
}
// 2D Multiplication [*]: Component-wise
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, typename SWIZZLE0::PARENT >::type >::type operator*(const SWIZZLE0& lhs, const SWIZZLE1& rhs) { return typename SWIZZLE0::PARENT(lhs) * typename SWIZZLE1::PARENT(rhs); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type operator*(const SWIZZLE& lhs, const VECTOR2<TYPE>& rhs) { return typename SWIZZLE::PARENT(lhs) * rhs; }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type operator*(const VECTOR2<TYPE>& lhs, const SWIZZLE& rhs) { return lhs * typename SWIZZLE::PARENT(rhs); }
template <typename TYPE> VECTOR2<TYPE> operator*(const VECTOR2<TYPE>& lhs, const VECTOR2<TYPE>& rhs)
{
return VECTOR2<TYPE>(lhs.x * rhs.x, lhs.y * rhs.y);
}
// 2D Division [/]: Component-wise
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, typename SWIZZLE0::PARENT >::type >::type operator/(const SWIZZLE0& lhs, const SWIZZLE1& rhs) { return typename SWIZZLE0::PARENT(lhs) / typename SWIZZLE1::PARENT(rhs); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type operator/(const SWIZZLE& lhs, const VECTOR2<TYPE>& rhs) { return typename SWIZZLE::PARENT(lhs) / rhs; }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type operator/(const VECTOR2<TYPE>& lhs, const SWIZZLE& rhs) { return lhs / typename SWIZZLE::PARENT(rhs); }
template <typename TYPE> VECTOR2<TYPE> operator/(const VECTOR2<TYPE>& lhs, const VECTOR2<TYPE>& rhs)
{
return VECTOR2<TYPE>(lhs.x / rhs.x, lhs.y / rhs.y);
}
// 2D Multiplication [*]: Scalar
template <typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type operator*(const SWIZZLE& lhs, const SCALAR_TYPE& rhs) { return typename SWIZZLE::PARENT(lhs) * rhs; }
template <typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type operator*(const SCALAR_TYPE& lhs, const SWIZZLE& rhs) { return lhs * typename SWIZZLE::PARENT(rhs); }
template <typename TYPE> VECTOR2<TYPE> operator*(const VECTOR2<TYPE>& lhs, const SCALAR_TYPE& rhs)
{
return VECTOR2<TYPE>(lhs.x * rhs, lhs.y * rhs);
}
template <typename TYPE> VECTOR2<TYPE> operator*(const SCALAR_TYPE& lhs, const VECTOR2<TYPE>& rhs)
{
return VECTOR2<TYPE>(lhs * rhs.x, lhs * rhs.y);
}
// 2D Division [/]: Scalar
template <typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type operator/(const SWIZZLE& lhs, const SCALAR_TYPE& rhs) { return typename SWIZZLE::PARENT(lhs) / rhs; }
template <typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type operator/(const SCALAR_TYPE& lhs, const SWIZZLE& rhs) { return lhs / typename SWIZZLE::PARENT(rhs); }
template <typename TYPE> VECTOR2<TYPE> operator/(const VECTOR2<TYPE>& lhs, const SCALAR_TYPE& rhs)
{
return VECTOR2<TYPE>(lhs.x / rhs, lhs.y / rhs);
}
template <typename TYPE> VECTOR2<TYPE> operator/(const SCALAR_TYPE& lhs, const VECTOR2<TYPE>& rhs)
{
return VECTOR2<TYPE>(lhs / rhs.x, lhs / rhs.y);
}
// 2D Equal To [==]
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, bool >::type >::type operator==(const SWIZZLE0& lhs, const SWIZZLE1& rhs) { return typename SWIZZLE0::PARENT(lhs) == typename SWIZZLE1::PARENT(rhs); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, bool >::type operator==(const SWIZZLE& lhs, const VECTOR2<TYPE>& rhs) { return typename SWIZZLE::PARENT(lhs) == rhs; }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, bool >::type operator==(const VECTOR2<TYPE>& lhs, const SWIZZLE& rhs) { return lhs == typename SWIZZLE::PARENT(rhs); }
template <typename TYPE> bool operator==(const VECTOR2<TYPE>& lhs, const VECTOR2<TYPE>& rhs)
{
return !(lhs.x != rhs.x || lhs.y != rhs.y);
}
// 2D Not Equal To [!=]
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, bool >::type >::type operator!=(const SWIZZLE0& lhs, const SWIZZLE1& rhs) { return typename SWIZZLE0::PARENT(lhs) != typename SWIZZLE1::PARENT(rhs); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, bool >::type operator!=(const SWIZZLE& lhs, const VECTOR2<TYPE>& rhs) { return typename SWIZZLE::PARENT(lhs) != rhs; }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, bool >::type operator!=(const VECTOR2<TYPE>& lhs, const SWIZZLE& rhs) { return lhs != typename SWIZZLE::PARENT(rhs); }
template <typename TYPE> bool operator!=(const VECTOR2<TYPE>& lhs, const VECTOR2<TYPE>& rhs)
{
return lhs.x != rhs.x || lhs.y != rhs.y;
}
// 2D AlmostEqual()
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, bool >::type >::type AlmostEqual(const SWIZZLE0& lhs, const SWIZZLE1& rhs) { return AlmostEqual((typename SWIZZLE0::PARENT(lhs)), (typename SWIZZLE1::PARENT(rhs))); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, bool >::type AlmostEqual(const SWIZZLE& lhs, const VECTOR2<TYPE>& rhs) { return AlmostEqual((typename SWIZZLE::PARENT(lhs)), rhs); }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, bool >::type AlmostEqual(const VECTOR2<TYPE>& lhs, const SWIZZLE& rhs) { return AlmostEqual(lhs, (typename SWIZZLE::PARENT(rhs))); }
template <typename TYPE> bool AlmostEqual(const VECTOR2<TYPE>& lhs, const VECTOR2<TYPE>& rhs)
{
return fabs(lhs.x - rhs.x) < COMPARISON_EPSILON && fabs(lhs.y - rhs.y) < COMPARISON_EPSILON;
}
// 2D Less Than [<]
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, bool >::type >::type operator<(const SWIZZLE0& lhs, const SWIZZLE1& rhs) { return typename SWIZZLE0::PARENT(lhs) < typename SWIZZLE1::PARENT(rhs); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, bool >::type operator<(const SWIZZLE& lhs, const VECTOR2<TYPE>& rhs) { return typename SWIZZLE::PARENT(lhs) < rhs; }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, bool >::type operator<(const VECTOR2<TYPE>& lhs, const SWIZZLE& rhs) { return lhs < typename SWIZZLE::PARENT(rhs); }
template <typename TYPE> bool operator<(const VECTOR2<TYPE>& lhs, const VECTOR2<TYPE>& rhs)
{
return lhs.x < rhs.x || (lhs.x == rhs.x && lhs.y < rhs.y);
}
// 2D Greater Than [>]
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, bool >::type >::type operator>(const SWIZZLE0& lhs, const SWIZZLE1& rhs) { return typename SWIZZLE0::PARENT(lhs) > typename SWIZZLE1::PARENT(rhs); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, bool >::type operator>(const SWIZZLE& lhs, const VECTOR2<TYPE>& rhs) { return typename SWIZZLE::PARENT(lhs) > rhs; }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, bool >::type operator>(const VECTOR2<TYPE>& lhs, const SWIZZLE& rhs) { return lhs > typename SWIZZLE::PARENT(rhs); }
template <typename TYPE> bool operator>(const VECTOR2<TYPE>& lhs, const VECTOR2<TYPE>& rhs)
{
return lhs.x > rhs.x || (lhs.x == rhs.x && lhs.y > rhs.y);
}
// 2D Less Than Or Equal To [<=]
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, bool >::type >::type operator<=(const SWIZZLE0& lhs, const SWIZZLE1& rhs) { return typename SWIZZLE0::PARENT(lhs) <= typename SWIZZLE1::PARENT(rhs); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, bool >::type operator<=(const SWIZZLE& lhs, const VECTOR2<TYPE>& rhs) { return typename SWIZZLE::PARENT(lhs) <= rhs; }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, bool >::type operator<=(const VECTOR2<TYPE>& lhs, const SWIZZLE& rhs) { return lhs <= typename SWIZZLE::PARENT(rhs); }
template <typename TYPE> bool operator<=(const VECTOR2<TYPE>& lhs, const VECTOR2<TYPE>& rhs)
{
return lhs.x < rhs.x || (lhs.x == rhs.x && lhs.y <= rhs.y);
}
// 2D Greater Than Or Equal To [<=]
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, bool >::type >::type operator>=(const SWIZZLE0& lhs, const SWIZZLE1& rhs) { return typename SWIZZLE0::PARENT(lhs) >= typename SWIZZLE1::PARENT(rhs); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, bool >::type operator>=(const SWIZZLE& lhs, const VECTOR2<TYPE>& rhs) { return typename SWIZZLE::PARENT(lhs) >= rhs; }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, bool >::type operator>=(const VECTOR2<TYPE>& lhs, const SWIZZLE& rhs) { return lhs >= typename SWIZZLE::PARENT(rhs); }
template <typename TYPE> bool operator>=(const VECTOR2<TYPE>& lhs, const VECTOR2<TYPE>& rhs)
{
return lhs.x > rhs.x || (lhs.x == rhs.x && lhs.y >= rhs.y);
}
// 2D Normalize()
template <typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Normalize(const SWIZZLE& toNormalize) { return Normalize(typename SWIZZLE::PARENT(toNormalize)); }
template <typename TYPE> VECTOR2<TYPE> Normalize(const VECTOR2<TYPE>& toNormalize)
{
return toNormalize / toNormalize.Length;
}
// 2D Dot()
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, SCALAR_TYPE >::type >::type Dot(const SWIZZLE0& a, const SWIZZLE1& b) { return Dot(typename SWIZZLE0::PARENT(a), typename SWIZZLE1::PARENT(b)); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, SCALAR_TYPE >::type Dot(const SWIZZLE& a, const VECTOR2<TYPE>& b) { return Dot(typename SWIZZLE::PARENT(a), b); }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, SCALAR_TYPE >::type Dot(const VECTOR2<TYPE>& a, const SWIZZLE& b) { return Dot(a, typename SWIZZLE::PARENT(b)); }
template <typename TYPE> SCALAR_TYPE Dot(const VECTOR2<TYPE>& a, const VECTOR2<TYPE>& b)
{
return a.x * b.x + a.y * b.y;
}
// 2D Perpendicular()
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Perpendicular(const SWIZZLE& toPerpendicular) { return Perpendicular(typename SWIZZLE::PARENT(toPerpendicular)); }
template <typename TYPE> VECTOR2<TYPE> Perpendicular(const VECTOR2<TYPE>& toPerpendicular)
{
return vec2(-toPerpendicular.y, toPerpendicular.x);
}
// 2D Project()
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, typename SWIZZLE0::PARENT >::type >::type Project(const SWIZZLE0& projectThis, const SWIZZLE1& ontoThis) { return Project(typename SWIZZLE0::PARENT(projectThis), typename SWIZZLE1::PARENT(ontoThis)); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Project(const SWIZZLE& projectThis, const VECTOR2<TYPE>& ontoThis) { return Project(typename SWIZZLE::PARENT(projectThis), ontoThis); }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Project(const VECTOR2<TYPE>& projectThis, const SWIZZLE& ontoThis) { return Project(projectThis, typename SWIZZLE::PARENT(ontoThis)); }
template <typename TYPE> VECTOR2<TYPE> Project(const VECTOR2<TYPE>& projectThis, const VECTOR2<TYPE>& ontoThis)
{
return (Dot(projectThis, ontoThis) / Dot(ontoThis, ontoThis)) * ontoThis;
}
// 2D Rotate()
template <typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Rotate(const SWIZZLE& vector, const SCALAR_TYPE& angle) { return Rotate(typename SWIZZLE::PARENT(vector), angle); }
template <typename TYPE> VECTOR2<TYPE> Rotate(const VECTOR2<TYPE>& vector, const SCALAR_TYPE& angle)
{
SCALAR_TYPE sa = sin(angle);
SCALAR_TYPE ca = cos(angle);
return VECTOR2<TYPE>(vector.x * ca - vector.y * sa, vector.x * sa + vector.y * ca);
}
// 2D Lerp()
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, typename SWIZZLE0::PARENT >::type >::type Lerp(const SWIZZLE0& start, const SWIZZLE1& end, const SCALAR_TYPE& delta) { return Lerp(typename SWIZZLE0::PARENT(start), typename SWIZZLE1::PARENT(end), delta); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Lerp(const SWIZZLE& start, const VECTOR2<TYPE>& end, const SCALAR_TYPE& delta) { return Lerp(typename SWIZZLE::PARENT(start), end, delta); }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Lerp(const VECTOR2<TYPE>& start, const SWIZZLE& end, const SCALAR_TYPE& delta) { return Lerp(start, typename SWIZZLE::PARENT(end), delta); }
template <typename TYPE> VECTOR2<TYPE> Lerp(const VECTOR2<TYPE>& start, const VECTOR2<TYPE>& end, const SCALAR_TYPE& delta)
{
return VECTOR2<TYPE>(start.x + delta * (end.x - start.x),
start.y + delta * (end.y - start.y));
}
// 2D Max(): Component-wise
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, typename SWIZZLE0::PARENT >::type >::type Max(const SWIZZLE0& a, const SWIZZLE1& b) { return Max(typename SWIZZLE0::PARENT(a), typename SWIZZLE1::PARENT(b)); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Max(const SWIZZLE& a, const VECTOR2<TYPE>& b) { return Max(typename SWIZZLE::PARENT(a), b); }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Max(const VECTOR2<TYPE>& a, const SWIZZLE& b) { return Max(a, typename SWIZZLE::PARENT(b)); }
template <typename TYPE> VECTOR2<TYPE> Max(const VECTOR2<TYPE>& a, const VECTOR2<TYPE>& b)
{
return VECTOR2<TYPE>(max(a.x, b.x), max(a.y, b.y));
}
// 2D Min(): Component-wise
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, typename SWIZZLE0::PARENT >::type >::type Min(const SWIZZLE0& a, const SWIZZLE1& b) { return Min(typename SWIZZLE0::PARENT(a), typename SWIZZLE1::PARENT(b)); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Min(const SWIZZLE& a, const VECTOR2<TYPE>& b) { return Min(typename SWIZZLE::PARENT(a), b); }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Min(const VECTOR2<TYPE>& a, const SWIZZLE& b) { return Min(a, typename SWIZZLE::PARENT(b)); }
template <typename TYPE> VECTOR2<TYPE> Min(const VECTOR2<TYPE>& a, const VECTOR2<TYPE>& b)
{
return VECTOR2<TYPE>(min(a.x, b.x), min(a.y, b.y));
}
// 2D Max(): Scalar
template <typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Max(const SWIZZLE& a, const SCALAR_TYPE& maximum) { return Max(typename SWIZZLE::PARENT(a), maximum); }
template <typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Max(const SCALAR_TYPE& maximum, const SWIZZLE& b) { return Max(maximum, typename SWIZZLE::PARENT(b)); }
template <typename TYPE> VECTOR2<TYPE> Max(const VECTOR2<TYPE>& a, const SCALAR_TYPE& maximum)
{
return VECTOR2<TYPE>(max((SCALAR_TYPE)a.x, maximum), max((SCALAR_TYPE)a.y, maximum));
}
template <typename TYPE> VECTOR2<TYPE> Max(const SCALAR_TYPE& maximum, const VECTOR2<TYPE>& b)
{
return VECTOR2<TYPE>(max(maximum, (SCALAR_TYPE)b.x), max(maximum, (SCALAR_TYPE)b.y));
}
// 2D Min(): Scalar
template <typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Min(const SWIZZLE& a, const SCALAR_TYPE& minimum) { return Min(typename SWIZZLE::PARENT(a), minimum); }
template <typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Min(const SCALAR_TYPE& minimum, const SWIZZLE& b) { return Min(minimum, typename SWIZZLE::PARENT(b)); }
template <typename TYPE> VECTOR2<TYPE> Min(const VECTOR2<TYPE>& a, const SCALAR_TYPE& minimum)
{
return VECTOR2<TYPE>(min((SCALAR_TYPE)a.x, minimum), min((SCALAR_TYPE)a.y, minimum));
}
template <typename TYPE> VECTOR2<TYPE> Min(const SCALAR_TYPE& minimum, const VECTOR2<TYPE>& b)
{
return VECTOR2<TYPE>(min(minimum, (SCALAR_TYPE)b.x), min(minimum, (SCALAR_TYPE)b.y));
}
// 2D Ceil()
template <typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Ceil(const SWIZZLE& toCeil) { return Ceil(typename SWIZZLE::PARENT(toCeil)); }
template <typename TYPE> VECTOR2<TYPE> Ceil(const VECTOR2<TYPE>& toCeil)
{
return VECTOR2<TYPE>(ceil(toCeil.x), ceil(toCeil.y));
}
// 2D Floor()
template <typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, typename SWIZZLE::PARENT >::type Floor(const SWIZZLE& toFloor) { return Floor(typename SWIZZLE::PARENT(toFloor)); }
template <typename TYPE> VECTOR2<TYPE> Floor(const VECTOR2<TYPE>& toFloor)
{
return VECTOR2<TYPE>(floor(toFloor.x), floor(toFloor.y));
}
// 2D Distance()
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, SCALAR_TYPE >::type >::type Distance(const SWIZZLE0& a, const SWIZZLE1& b) { return Distance(typename SWIZZLE0::PARENT(a), typename SWIZZLE1::PARENT(b)); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, SCALAR_TYPE >::type Distance(const SWIZZLE& a, const VECTOR2<TYPE>& b) { return Distance(typename SWIZZLE::PARENT(a), b); }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, SCALAR_TYPE >::type Distance(const VECTOR2<TYPE>& a, const SWIZZLE& b) { return Distance(a, typename SWIZZLE::PARENT(b)); }
template <typename TYPE> SCALAR_TYPE Distance(const VECTOR2<TYPE>& a, const VECTOR2<TYPE>& b)
{
return sqrt(DistanceSquared(a, b));
}
// 2D DistanceSquared()
template <typename SWIZZLE0, typename SWIZZLE1> inline typename EnableIf< Is2D< typename SWIZZLE0::PARENT >, typename EnableIf< Is2D< typename SWIZZLE1::PARENT >, SCALAR_TYPE >::type >::type DistanceSquared(const SWIZZLE0& a, const SWIZZLE1& b) { return DistanceSquared(typename SWIZZLE0::PARENT(a), typename SWIZZLE1::PARENT(b)); }
template <typename SWIZZLE, typename TYPE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, SCALAR_TYPE >::type DistanceSquared(const SWIZZLE& a, const VECTOR2<TYPE>& b) { return DistanceSquared(typename SWIZZLE::PARENT(a), b); }
template <typename TYPE, typename SWIZZLE> inline typename EnableIf< Is2D< typename SWIZZLE::PARENT >, SCALAR_TYPE >::type DistanceSquared(const VECTOR2<TYPE>& a, const SWIZZLE& b) { return DistanceSquared(a, typename SWIZZLE::PARENT(b)); }
template <typename TYPE> SCALAR_TYPE DistanceSquared(const VECTOR2<TYPE>& a, const VECTOR2<TYPE>& b)
{
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
//----------------------------------------------------------------------
//
// Sec. 05 - 3D vector type and associated functions
//
//----------------------------------------------------------------------
template <typename TYPE>
union VECTOR3
{
private:
struct { TYPE x, y, z; } v;
struct s1 { protected: TYPE x, y, z; };
struct s2 { protected: TYPE x, y, z; public: typedef VECTOR2<TYPE> PARENT; };
struct s3 { protected: TYPE x, y, z; public: typedef VECTOR3<TYPE> PARENT; };
struct s4 { protected: TYPE x, y, z; public: typedef VECTOR4<TYPE> PARENT; };
struct X : s1
{
operator TYPE() const { return s1::x; }
const X& operator=(const TYPE& rhs) { s1::x = rhs; return *this; }
const X& operator+=(const TYPE& rhs) { s1::x += rhs; return *this; }
const X& operator-=(const TYPE& rhs) { s1::x -= rhs; return *this; }
const X& operator*=(const TYPE& rhs) { s1::x *= rhs; return *this; }
const X& operator/=(const TYPE& rhs) { s1::x /= rhs; return *this; }
};
struct Y : s1
{
operator TYPE() const { return s1::y; }
const Y& operator=(const TYPE& rhs) { s1::y = rhs; return *this; }
const Y& operator+=(const TYPE& rhs) { s1::y += rhs; return *this; }
const Y& operator-=(const TYPE& rhs) { s1::y -= rhs; return *this; }
const Y& operator*=(const TYPE& rhs) { s1::y *= rhs; return *this; }
const Y& operator/=(const TYPE& rhs) { s1::y /= rhs; return *this; }
};
struct Z : s1
{
operator TYPE() const { return s1::z; }
const Z& operator=(const TYPE& rhs) { s1::z = rhs; return *this; }
const Z& operator+=(const TYPE& rhs) { s1::z += rhs; return *this; }
const Z& operator-=(const TYPE& rhs) { s1::z -= rhs; return *this; }
const Z& operator*=(const TYPE& rhs) { s1::z *= rhs; return *this; }
const Z& operator/=(const TYPE& rhs) { s1::z /= rhs; return *this; }
};
struct XX : s2 { operator VECTOR2<TYPE>() const { return VECTOR2<TYPE>(s2::x, s2::x); } };
struct XY : s2
{
operator VECTOR2<TYPE>() const { return VECTOR2<TYPE>(s2::x, s2::y); }
const XY& operator=(const VECTOR3<TYPE>& rhs) { s2::x = rhs.v.x; s2::y = rhs.v.y; return *this; }
const XY& operator+=(const VECTOR3<TYPE>& rhs) { s2::x += rhs.v.x; s2::y += rhs.v.y; return *this; }
const XY& operator-=(const VECTOR3<TYPE>& rhs) { s2::x -= rhs.v.x; s2::y -= rhs.v.y; return *this; }
const XY& operator*=(const VECTOR3<TYPE>& rhs) { s2::x *= rhs.v.x; s2::y *= rhs.v.y; return *this; }
const XY& operator/=(const VECTOR3<TYPE>& rhs) { s2::x /= rhs.v.x; s2::y /= rhs.v.y; return *this; }
const XY& operator*=(const TYPE& rhs) { s2::x *= rhs; s2::y *= rhs; return *this; }
const XY& operator/=(const TYPE& rhs) { s2::x /= rhs; s2::y /= rhs; return *this; }
};
struct XZ : s2
{
operator VECTOR2<TYPE>() const { return VECTOR2<TYPE>(s2::x, s2::z); }
const XZ& operator=(const VECTOR3<TYPE>& rhs) { s2::x = rhs.v.x; s2::z = rhs.v.y; return *this; }
const XZ& operator+=(const VECTOR3<TYPE>& rhs) { s2::x += rhs.v.x; s2::z += rhs.v.y; return *this; }
const XZ& operator-=(const VECTOR3<TYPE>& rhs) { s2::x -= rhs.v.x; s2::z -= rhs.v.y; return *this; }
const XZ& operator*=(const VECTOR3<TYPE>& rhs) { s2::x *= rhs.v.x; s2::z *= rhs.v.y; return *this; }
const XZ& operator/=(const VECTOR3<TYPE>& rhs) { s2::x /= rhs.v.x; s2::z /= rhs.v.y; return *this; }
const XZ& operator*=(const TYPE& rhs) { s2::x *= rhs; s2::z *= rhs; return *this; }
const XZ& operator/=(const TYPE& rhs) { s2::x /= rhs; s2::z /= rhs; return *this; }
};
struct YX : s2
{
operator VECTOR2<TYPE>() const { return VECTOR2<TYPE>(s2::y, s2::x); }
const YX& operator=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::y = rhs.v.x; s2::x = t0; return *this; }
const YX& operator+=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::y += rhs.v.x; s2::x += t0; return *this; }
const YX& operator-=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::y -= rhs.v.x; s2::x -= t0; return *this; }
const YX& operator*=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::y *= rhs.v.x; s2::x *= t0; return *this; }
const YX& operator/=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::y /= rhs.v.x; s2::x /= t0; return *this; }
const YX& operator*=(const TYPE& rhs) { s2::y *= rhs; s2::x *= rhs; return *this; }
const YX& operator/=(const TYPE& rhs) { s2::y /= rhs; s2::x /= rhs; return *this; }
};
struct YY : s2 { operator VECTOR2<TYPE>() const { return VECTOR2<TYPE>(s2::y, s2::y); } };
struct YZ : s2
{
operator VECTOR2<TYPE>() const { return VECTOR2<TYPE>(s2::y, s2::z); }
const YZ& operator=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::y = rhs.v.x; s2::z = t0; return *this; }
const YZ& operator+=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::y += rhs.v.x; s2::z += t0; return *this; }
const YZ& operator-=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::y -= rhs.v.x; s2::z -= t0; return *this; }
const YZ& operator*=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::y *= rhs.v.x; s2::z *= t0; return *this; }
const YZ& operator/=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::y /= rhs.v.x; s2::z /= t0; return *this; }
const YZ& operator*=(const TYPE& rhs) { s2::y *= rhs; s2::z *= rhs; return *this; }
const YZ& operator/=(const TYPE& rhs) { s2::y /= rhs; s2::z /= rhs; return *this; }
};
struct ZX : s2
{
operator VECTOR2<TYPE>() const { return VECTOR2<TYPE>(s2::z, s2::x); }
const ZX& operator=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::z = rhs.v.x; s2::x = t0; return *this; }
const ZX& operator+=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::z += rhs.v.x; s2::x += t0; return *this; }
const ZX& operator-=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::z -= rhs.v.x; s2::x -= t0; return *this; }
const ZX& operator*=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::z *= rhs.v.x; s2::x *= t0; return *this; }
const ZX& operator/=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s2::z /= rhs.v.x; s2::x /= t0; return *this; }
const ZX& operator*=(const TYPE& rhs) { s2::z *= rhs; s2::x *= rhs; return *this; }
const ZX& operator/=(const TYPE& rhs) { s2::z /= rhs; s2::x /= rhs; return *this; }
};
struct ZY : s2
{
operator VECTOR2<TYPE>() const { return VECTOR2<TYPE>(s2::z, s2::y); }
const ZY& operator=(const VECTOR3<TYPE>& rhs) { s2::z = rhs.v.x; s2::y = rhs.v.y; return *this; }
const ZY& operator+=(const VECTOR3<TYPE>& rhs) { s2::z += rhs.v.x; s2::y += rhs.v.y; return *this; }
const ZY& operator-=(const VECTOR3<TYPE>& rhs) { s2::z -= rhs.v.x; s2::y -= rhs.v.y; return *this; }
const ZY& operator*=(const VECTOR3<TYPE>& rhs) { s2::z *= rhs.v.x; s2::y *= rhs.v.y; return *this; }
const ZY& operator/=(const VECTOR3<TYPE>& rhs) { s2::z /= rhs.v.x; s2::y /= rhs.v.y; return *this; }
const ZY& operator*=(const TYPE& rhs) { s2::z *= rhs; s2::y *= rhs; return *this; }
const ZY& operator/=(const TYPE& rhs) { s2::z /= rhs; s2::y /= rhs; return *this; }
};
struct ZZ : s2 { operator VECTOR2<TYPE>() const { return VECTOR2<TYPE>(s2::z, s2::z); } };
struct XXX : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::x, s3::x, s3::x); } };
struct XXY : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::x, s3::x, s3::y); } };
struct XXZ : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::x, s3::x, s3::z); } };
struct XYX : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::x, s3::y, s3::x); } };
struct XYY : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::x, s3::y, s3::y); } };
struct XYZ : s3
{
operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::x, s3::y, s3::z); }
const XYZ& operator=(const VECTOR3<TYPE>& rhs) { s3::x = rhs.v.x; s3::y = rhs.v.y; s3::z = rhs.v.z; return *this; }
const XYZ& operator+=(const VECTOR3<TYPE>& rhs) { s3::x += rhs.v.x; s3::y += rhs.v.y; s3::z += rhs.v.z; return *this; }
const XYZ& operator-=(const VECTOR3<TYPE>& rhs) { s3::x -= rhs.v.x; s3::y -= rhs.v.y; s3::z -= rhs.v.z; return *this; }
const XYZ& operator*=(const VECTOR3<TYPE>& rhs) { s3::x *= rhs.v.x; s3::y *= rhs.v.y; s3::z *= rhs.v.z; return *this; }
const XYZ& operator/=(const VECTOR3<TYPE>& rhs) { s3::x /= rhs.v.x; s3::y /= rhs.v.y; s3::z /= rhs.v.z; return *this; }
const XYZ& operator*=(const TYPE& rhs) { s3::x *= rhs; s3::y *= rhs; s3::z *= rhs; return *this; }
const XYZ& operator/=(const TYPE& rhs) { s3::x /= rhs; s3::y /= rhs; s3::z /= rhs; return *this; }
};
struct XZX : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::x, s3::z, s3::x); } };
struct XZY : s3
{
operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::x, s3::z, s3::y); }
const XZY& operator=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.z; s3::x = rhs.v.x; s3::z = rhs.v.y; s3::y = t0; return *this; }
const XZY& operator+=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.z; s3::x += rhs.v.x; s3::z += rhs.v.y; s3::y += t0; return *this; }
const XZY& operator-=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.z; s3::x -= rhs.v.x; s3::z -= rhs.v.y; s3::y -= t0; return *this; }
const XZY& operator*=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.z; s3::x *= rhs.v.x; s3::z *= rhs.v.y; s3::y *= t0; return *this; }
const XZY& operator/=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.z; s3::x /= rhs.v.x; s3::z /= rhs.v.y; s3::y /= t0; return *this; }
const XZY& operator*=(const TYPE& rhs) { s3::x *= rhs; s3::z *= rhs; s3::y *= rhs; return *this; }
const XZY& operator/=(const TYPE& rhs) { s3::x /= rhs; s3::z /= rhs; s3::y /= rhs; return *this; }
};
struct XZZ : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::x, s3::z, s3::z); } };
struct YXX : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::y, s3::x, s3::x); } };
struct YXY : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::y, s3::x, s3::y); } };
struct YXZ : s3
{
operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::y, s3::x, s3::z); }
const YXZ& operator=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s3::y = rhs.v.x; s3::x = t0; s3::z = rhs.v.z; return *this; }
const YXZ& operator+=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s3::y += rhs.v.x; s3::x += t0; s3::z += rhs.v.z; return *this; }
const YXZ& operator-=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s3::y -= rhs.v.x; s3::x -= t0; s3::z -= rhs.v.z; return *this; }
const YXZ& operator*=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s3::y *= rhs.v.x; s3::x *= t0; s3::z *= rhs.v.z; return *this; }
const YXZ& operator/=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; s3::y /= rhs.v.x; s3::x /= t0; s3::z /= rhs.v.z; return *this; }
const YXZ& operator*=(const TYPE& rhs) { s3::y *= rhs; s3::x *= rhs; s3::z *= rhs; return *this; }
const YXZ& operator/=(const TYPE& rhs) { s3::y /= rhs; s3::x /= rhs; s3::z /= rhs; return *this; }
};
struct YYX : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::y, s3::y, s3::x); } };
struct YYY : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::y, s3::y, s3::y); } };
struct YYZ : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::y, s3::y, s3::z); } };
struct YZX : s3
{
operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::y, s3::z, s3::x); }
const YZX& operator=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; TYPE t1 = rhs.v.z; s3::y = rhs.v.x; s3::z = t0; s3::x = t1; return *this; }
const YZX& operator+=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; TYPE t1 = rhs.v.z; s3::y += rhs.v.x; s3::z += t0; s3::x += t1; return *this; }
const YZX& operator-=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; TYPE t1 = rhs.v.z; s3::y -= rhs.v.x; s3::z -= t0; s3::x -= t1; return *this; }
const YZX& operator*=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; TYPE t1 = rhs.v.z; s3::y *= rhs.v.x; s3::z *= t0; s3::x *= t1; return *this; }
const YZX& operator/=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; TYPE t1 = rhs.v.z; s3::y /= rhs.v.x; s3::z /= t0; s3::x /= t1; return *this; }
const YZX& operator*=(const TYPE& rhs) { s3::y *= rhs; s3::z *= rhs; s3::x *= rhs; return *this; }
const YZX& operator/=(const TYPE& rhs) { s3::y /= rhs; s3::z /= rhs; s3::x /= rhs; return *this; }
};
struct YZY : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::y, s3::z, s3::y); } };
struct YZZ : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::y, s3::z, s3::z); } };
struct ZXX : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::z, s3::x, s3::x); } };
struct ZXY : s3
{
operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::z, s3::x, s3::y); }
const ZXY& operator=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; TYPE t1 = rhs.v.z; s3::z = rhs.v.x; s3::x = t0; s3::y = t1; return *this; }
const ZXY& operator+=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; TYPE t1 = rhs.v.z; s3::z += rhs.v.x; s3::x += t0; s3::y += t1; return *this; }
const ZXY& operator-=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; TYPE t1 = rhs.v.z; s3::z -= rhs.v.x; s3::x -= t0; s3::y -= t1; return *this; }
const ZXY& operator*=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; TYPE t1 = rhs.v.z; s3::z *= rhs.v.x; s3::x *= t0; s3::y *= t1; return *this; }
const ZXY& operator/=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.y; TYPE t1 = rhs.v.z; s3::z /= rhs.v.x; s3::x /= t0; s3::y /= t1; return *this; }
const ZXY& operator*=(const TYPE& rhs) { s3::z *= rhs; s3::x *= rhs; s3::y *= rhs; return *this; }
const ZXY& operator/=(const TYPE& rhs) { s3::z /= rhs; s3::x /= rhs; s3::y /= rhs; return *this; }
};
struct ZXZ : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::z, s3::x, s3::z); } };
struct ZYX : s3
{
operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::z, s3::y, s3::x); }
const ZYX& operator=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.z; s3::z = rhs.v.x; s3::y = rhs.v.y; s3::x = t0; return *this; }
const ZYX& operator+=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.z; s3::z += rhs.v.x; s3::y += rhs.v.y; s3::x += t0; return *this; }
const ZYX& operator-=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.z; s3::z -= rhs.v.x; s3::y -= rhs.v.y; s3::x -= t0; return *this; }
const ZYX& operator*=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.z; s3::z *= rhs.v.x; s3::y *= rhs.v.y; s3::x *= t0; return *this; }
const ZYX& operator/=(const VECTOR3<TYPE>& rhs) { TYPE t0 = rhs.v.z; s3::z /= rhs.v.x; s3::y /= rhs.v.y; s3::x /= t0; return *this; }
const ZYX& operator*=(const TYPE& rhs) { s3::z *= rhs; s3::y *= rhs; s3::x *= rhs; return *this; }
const ZYX& operator/=(const TYPE& rhs) { s3::z /= rhs; s3::y /= rhs; s3::x /= rhs; return *this; }
};
struct ZYY : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::z, s3::y, s3::y); } };
struct ZYZ : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::z, s3::y, s3::z); } };
struct ZZX : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::z, s3::z, s3::x); } };
struct ZZY : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::z, s3::z, s3::y); } };
struct ZZZ : s3 { operator VECTOR3<TYPE>() const { return VECTOR3<TYPE>(s3::z, s3::z, s3::z); } };
struct XXXX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::x, s4::x, s4::x); } };
struct XXXY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::x, s4::x, s4::y); } };
struct XXXZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::x, s4::x, s4::z); } };
struct XXYX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::x, s4::y, s4::x); } };
struct XXYY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::x, s4::y, s4::y); } };
struct XXYZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::x, s4::y, s4::z); } };
struct XXZX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::x, s4::z, s4::x); } };
struct XXZY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::x, s4::z, s4::y); } };
struct XXZZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::x, s4::z, s4::z); } };
struct XYXX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::y, s4::x, s4::x); } };
struct XYXY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::y, s4::x, s4::y); } };
struct XYXZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::y, s4::x, s4::z); } };
struct XYYX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::y, s4::y, s4::x); } };
struct XYYY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::y, s4::y, s4::y); } };
struct XYYZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::y, s4::y, s4::z); } };
struct XYZX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::y, s4::z, s4::x); } };
struct XYZY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::y, s4::z, s4::y); } };
struct XYZZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::y, s4::z, s4::z); } };
struct XZXX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::z, s4::x, s4::x); } };
struct XZXY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::z, s4::x, s4::y); } };
struct XZXZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::z, s4::x, s4::z); } };
struct XZYX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::z, s4::y, s4::x); } };
struct XZYY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::z, s4::y, s4::y); } };
struct XZYZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::z, s4::y, s4::z); } };
struct XZZX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::z, s4::z, s4::x); } };
struct XZZY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::z, s4::z, s4::y); } };
struct XZZZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::x, s4::z, s4::z, s4::z); } };
struct YXXX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::x, s4::x, s4::x); } };
struct YXXY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::x, s4::x, s4::y); } };
struct YXXZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::x, s4::x, s4::z); } };
struct YXYX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::x, s4::y, s4::x); } };
struct YXYY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::x, s4::y, s4::y); } };
struct YXYZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::x, s4::y, s4::z); } };
struct YXZX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::x, s4::z, s4::x); } };
struct YXZY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::x, s4::z, s4::y); } };
struct YXZZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::x, s4::z, s4::z); } };
struct YYXX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::y, s4::x, s4::x); } };
struct YYXY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::y, s4::x, s4::y); } };
struct YYXZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::y, s4::x, s4::z); } };
struct YYYX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::y, s4::y, s4::x); } };
struct YYYY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::y, s4::y, s4::y); } };
struct YYYZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::y, s4::y, s4::z); } };
struct YYZX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::y, s4::z, s4::x); } };
struct YYZY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::y, s4::z, s4::y); } };
struct YYZZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::y, s4::z, s4::z); } };
struct YZXX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::z, s4::x, s4::x); } };
struct YZXY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::z, s4::x, s4::y); } };
struct YZXZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::z, s4::x, s4::z); } };
struct YZYX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::z, s4::y, s4::x); } };
struct YZYY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::z, s4::y, s4::y); } };
struct YZYZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::z, s4::y, s4::z); } };
struct YZZX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::z, s4::z, s4::x); } };
struct YZZY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::z, s4::z, s4::y); } };
struct YZZZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::y, s4::z, s4::z, s4::z); } };
struct ZXXX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::x, s4::x, s4::x); } };
struct ZXXY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::x, s4::x, s4::y); } };
struct ZXXZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::x, s4::x, s4::z); } };
struct ZXYX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::x, s4::y, s4::x); } };
struct ZXYY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::x, s4::y, s4::y); } };
struct ZXYZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::x, s4::y, s4::z); } };
struct ZXZX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::x, s4::z, s4::x); } };
struct ZXZY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::x, s4::z, s4::y); } };
struct ZXZZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::x, s4::z, s4::z); } };
struct ZYXX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::y, s4::x, s4::x); } };
struct ZYXY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::y, s4::x, s4::y); } };
struct ZYXZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::y, s4::x, s4::z); } };
struct ZYYX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::y, s4::y, s4::x); } };
struct ZYYY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::y, s4::y, s4::y); } };
struct ZYYZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::y, s4::y, s4::z); } };
struct ZYZX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::y, s4::z, s4::x); } };
struct ZYZY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::y, s4::z, s4::y); } };
struct ZYZZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::y, s4::z, s4::z); } };
struct ZZXX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::z, s4::x, s4::x); } };
struct ZZXY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::z, s4::x, s4::y); } };
struct ZZXZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::z, s4::x, s4::z); } };
struct ZZYX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::z, s4::y, s4::x); } };
struct ZZYY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::z, s4::y, s4::y); } };
struct ZZYZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::z, s4::y, s4::z); } };
struct ZZZX : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::z, s4::z, s4::x); } };
struct ZZZY : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::z, s4::z, s4::y); } };
struct ZZZZ : s4 { operator VECTOR4<TYPE>() const { return VECTOR4<TYPE>(s4::z, s4::z, s4::z, s4::z); } };
// Length property
struct LENGTH
{
private:
TYPE x, y, z;
TYPE ApplyLength(const TYPE& curMag, const TYPE& newMag)
{
TYPE inverseScaledMagnitude = newMag / curMag;
x *= inverseScaledMagnitude;
y *= inverseScaledMagnitude;
z *= inverseScaledMagnitude;
return newMag;
}
public:
operator TYPE() const { return sqrt(x * x + y * y + z * z); }
TYPE operator=(const TYPE& rhs) { return ApplyLength((TYPE)(*this), rhs); }
TYPE operator+=(const TYPE& rhs) { TYPE curMag = (TYPE)(*this); return ApplyLength(curMag, curMag + rhs); }
TYPE operator-=(const TYPE& rhs) { TYPE curMag = (TYPE)(*this); return ApplyLength(curMag, curMag - rhs); }
TYPE operator*=(const TYPE& rhs) { TYPE curMag = (TYPE)(*this); return ApplyLength(curMag, curMag * rhs); }
TYPE operator/=(const TYPE& rhs) { TYPE curMag = (TYPE)(*this); return ApplyLength(curMag, curMag / rhs); }
};
public:
VECTOR3() {}
VECTOR3(const TYPE& x, const TYPE& y, const TYPE& z) { v.x = x; v.y = y; v.z = z; }
VECTOR3(const VECTOR2<TYPE>& xy, const TYPE& z) { v.x = xy.x; v.y = xy.y; v.z = z; }
VECTOR3(const TYPE& x, const VECTOR2<TYPE>& yz) { v.x = x; v.y = yz.x; v.z = yz.y; }
// Swizzle variables
X x, r, s;
Y y, g, t;
Z z, b, p;
XX xx, rr, ss;
XY xy, rg, st;
XZ xz, rb, sp;
YX yx, gr, ts;
YY yy, gg, tt;
YZ yz, gb, tp;
ZX zx, br, ps;
ZY zy, bg, pt;
ZZ zz, bb, pp;
XXX xxx, rrr, sss;
XXY xxy, rrg, sst;
XXZ xxz, rrb, ssp;
XYX xyx, rgr, sts;
XYY xyy, rgg, stt;
XYZ xyz, rgb, stp;
XZX xzx, rbr, sps;
XZY xzy, rbg, spt;
XZZ xzz, rbb, spp;
YXX yxx, grr, tss;
YXY yxy, grg, tst;
YXZ yxz, grb, tsp;
YYX yyx, ggr, tts;
YYY yyy, ggg, ttt;
YYZ yyz, ggb, ttp;
YZX yzx, gbr, tps;
YZY yzy, gbg, tpt;
YZZ yzz, gbb, tpp;
ZXX zxx, brr, pss;
ZXY zxy, brg, pst;
ZXZ zxz, brb, psp;
ZYX zyx, bgr, pts;
ZYY zyy, bgg, ptt;
ZYZ zyz, bgb, ptp;
ZZX zzx, bbr, pps;
ZZY zzy, bbg, ppt;
ZZZ zzz, bbb, ppp;
XXXX xxxx, rrrr, ssss;
XXXY xxxy, rrrg, ssst;
XXXZ xxxz, rrrb, sssp;
XXYX xxyx, rrgr, ssts;
XXYY xxyy, rrgg, sstt;
XXYZ xxyz, rrgb, sstp;
XXZX xxzx, rrbr, ssps;
XXZY xxzy, rrbg, sspt;
XXZZ xxzz, rrbb, sspp;
XYXX xyxx, rgrr, stss;
XYXY xyxy, rgrg, stst;
XYXZ xyxz, rgrb, stsp;
XYYX xyyx, rggr, stts;
XYYY xyyy, rggg, sttt;
XYYZ xyyz, rggb, sttp;
XYZX xyzx, rgbr, stps;
XYZY xyzy, rgbg, stpt;
XYZZ xyzz, rgbb, stpp;
XZXX xzxx, rbrr, spss;
XZXY xzxy, rbrg, spst;
XZXZ xzxz, rbrb, spsp;
XZYX xzyx, rbgr, spts;
XZYY xzyy, rbgg, sptt;
XZYZ xzyz, rbgb, sptp;
XZZX xzzx, rbbr, spps;
XZZY xzzy, rbbg, sppt;
XZZZ xzzz, rbbb, sppp;
YXXX yxxx, grrr, tsss;
YXXY yxxy, grrg, tsst;
YXXZ yxxz, grrb, tssp;
YXYX yxyx, grgr, tsts;
YXYY yxyy, grgg, tstt;
YXYZ yxyz, grgb, tstp;
YXZX yxzx, grbr, tsps;
YXZY yxzy, grbg, tspt;
YXZZ yxzz, grbb, tspp;
YYXX yyxx, ggrr, ttss;
YYXY yyxy, ggrg, ttst;
YYXZ yyxz, ggrb, ttsp;
YYYX yyyx, gggr, ttts;
YYYY yyyy, gggg, tttt;
YYYZ yyyz, gggb, tttp;
YYZX yyzx, ggbr, ttps;
YYZY yyzy, ggbg, ttpt;
YYZZ yyzz, ggbb, ttpp;
YZXX yzxx, gbrr, tpss;
YZXY yzxy, gbrg, tpst;
YZXZ yzxz, gbrb, tpsp;
YZYX yzyx, gbgr, tpts;
YZYY yzyy, gbgg, tptt;
YZYZ yzyz, gbgb, tptp;
YZZX yzzx, gbbr, tpps;
YZZY yzzy, gbbg, tppt;
YZZZ yzzz, gbbb, tppp;
ZXXX zxxx, brrr, psss;
ZXXY zxxy, brrg, psst;
ZXXZ zxxz, brrb, pssp;
ZXYX zxyx, brgr, psts;
ZXYY zxyy, brgg, pstt;
ZXYZ zxyz, brgb, pstp;
ZXZX zxzx, brbr, psps;
ZXZY zxzy, brbg, pspt;
ZXZZ zxzz, brbb, pspp;
ZYXX zyxx, bgrr, ptss;
ZYXY zyxy, bgrg, ptst;
ZYXZ zyxz, bgrb, ptsp;
ZYYX zyyx, bggr, ptts;
ZYYY zyyy, bggg, pttt;
ZYYZ zyyz, bggb, pttp;
ZYZX zyzx, bgbr, ptps;
ZYZY zyzy, bgbg, ptpt;
ZYZZ zyzz, bgbb, ptpp;
ZZXX zzxx, bbrr, ppss;
ZZXY zzxy, bbrg, ppst;
ZZXZ zzxz, bbrb, ppsp;
ZZYX zzyx, bbgr, ppts;
ZZYY zzyy, bbgg, pptt;
ZZYZ zzyz, bbgb, pptp;
ZZZX zzzx, bbbr, ppps;
ZZZY zzzy, bbbg, pppt;
ZZZZ zzzz, bbbb, pppp;
// Length property
LENGTH Length;
// Assignment operators (default for = is fine)
const VECTOR3& operator+=(const VECTOR3& rhs) { v.x += rhs.v.x; v.y += rhs.v.y; v.z += rhs.v.z; return *this; }
const VECTOR3& operator-=(const VECTOR3& rhs) { v.x -= rhs.v.x; v.y -= rhs.v.y; v.z -= rhs.v.z; return *this; }
const VECTOR3& operator*=(const VECTOR3& rhs) { v.x *= rhs.v.x; v.y *= rhs.v.y; v.z *= rhs.v.z; return *this; }
const VECTOR3& operator/=(const VECTOR3& rhs) { v.x /= rhs.v.x; v.y /= rhs.v.y; v.z /= rhs.v.z; return *this; }
const VECTOR3& operator*=(const TYPE& rhs) { v.x *= rhs; v.y *= rhs; v.z *= rhs; return *this; }
const VECTOR3& operator/=(const TYPE& rhs) { v.x /= rhs; v.y /= rhs; v.z /= rhs; return *this; }
// Array notation access
const TYPE& operator[](const unsigned& index)
{
if (index == 0) { return v.x; }
else if (index == 1) { return v.y; }
else if (index == 2) { return v.z; }
else { cout << "Fatal Error: Attempted out of bounds bracket access of 3D vector." << endl << " - Vector: " << *this << endl << " - Index: " << index << endl; exit(-1); }
}
// Overload for cout
friend ostream& operator<<(ostream& os, const VECTOR3<TYPE>& printVector)
{
os << "(" << printVector.v.x << ", " << printVector.v.y << ", " << printVector.v.z << ")";
return os;
}
// Negation