-
Notifications
You must be signed in to change notification settings - Fork 10
/
Vector.h
850 lines (678 loc) · 32.5 KB
/
Vector.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
// Copyright 2014-2015 Isis Innovation Limited and the authors of InfiniTAM
#pragma once
#include <math.h>
#include <ostream>
namespace ORUtils {
//////////////////////////////////////////////////////////////////////////
// Basic Vector Structure
//////////////////////////////////////////////////////////////////////////
template <class T> struct Vector2_{
union {
struct { T x, y; }; // standard names for components
struct { T s, t; }; // standard names for components
struct { T width, height; };
T v[2]; // array access
};
};
template <class T> struct Vector3_{
union {
struct{ T x, y, z; }; // standard names for components
struct{ T r, g, b; }; // standard names for components
struct{ T s, t, p; }; // standard names for components
T v[3];
};
};
template <class T> struct Vector4_ {
union {
struct { T x, y, z, w; }; // standard names for components
struct { T r, g, b, a; }; // standard names for components
struct { T s, t, p, q; }; // standard names for components
T v[4];
};
};
template <class T> struct Vector6_ {
//union {
T v[6];
//};
};
template<class T, int s> struct VectorX_
{
int vsize;
T v[s];
};
//////////////////////////////////////////////////////////////////////////
// Vector class with math operators: +, -, *, /, +=, -=, /=, [], ==, !=, T*(), etc.
//////////////////////////////////////////////////////////////////////////
template <class T> class Vector2 : public Vector2_ < T >
{
public:
typedef T value_type;
_CPU_AND_GPU_CODE_ inline int size() const { return 2; }
////////////////////////////////////////////////////////
// Constructors
////////////////////////////////////////////////////////
_CPU_AND_GPU_CODE_ Vector2(){} // Default constructor
_CPU_AND_GPU_CODE_ Vector2(const T &t) { this->x = t; this->y = t; } // Scalar constructor
_CPU_AND_GPU_CODE_ Vector2(const T *tp) { this->x = tp[0]; this->y = tp[1]; } // Construct from array
_CPU_AND_GPU_CODE_ Vector2(const T v0, const T v1) { this->x = v0; this->y = v1; } // Construct from explicit values
_CPU_AND_GPU_CODE_ Vector2(const Vector2_<T> &v) { this->x = v.x; this->y = v.y; }// copy constructor
_CPU_AND_GPU_CODE_ explicit Vector2(const Vector3_<T> &u) { this->x = u.x; this->y = u.y; }
_CPU_AND_GPU_CODE_ explicit Vector2(const Vector4_<T> &u) { this->x = u.x; this->y = u.y; }
_CPU_AND_GPU_CODE_ inline Vector2<int> toInt() const {
return Vector2<int>((int)ROUND(this->x), (int)ROUND(this->y));
}
_CPU_AND_GPU_CODE_ inline Vector2<int> toIntFloor() const {
return Vector2<int>((int)floor(this->x), (int)floor(this->y));
}
_CPU_AND_GPU_CODE_ inline Vector2<unsigned char> toUChar() const {
Vector2<int> vi = toInt(); return Vector2<unsigned char>((unsigned char)CLAMP(vi.x, 0, 255), (unsigned char)CLAMP(vi.y, 0, 255));
}
_CPU_AND_GPU_CODE_ inline Vector2<float> toFloat() const {
return Vector2<float>((float)this->x, (float)this->y);
}
_CPU_AND_GPU_CODE_ const T *getValues() const { return this->v; }
_CPU_AND_GPU_CODE_ Vector2<T> &setValues(const T *rhs) { this->x = rhs[0]; this->y = rhs[1]; return *this; }
// indexing operators
_CPU_AND_GPU_CODE_ T &operator [](int i) { return this->v[i]; }
_CPU_AND_GPU_CODE_ const T &operator [](int i) const { return this->v[i]; }
// type-cast operators
_CPU_AND_GPU_CODE_ operator T *() { return this->v; }
_CPU_AND_GPU_CODE_ operator const T *() const { return this->v; }
////////////////////////////////////////////////////////
// Math operators
////////////////////////////////////////////////////////
// scalar multiply assign
_CPU_AND_GPU_CODE_ friend Vector2<T> &operator *= (const Vector2<T> &lhs, T d) {
lhs.x *= d; lhs.y *= d; return lhs;
}
// component-wise vector multiply assign
_CPU_AND_GPU_CODE_ friend Vector2<T> &operator *= (Vector2<T> &lhs, const Vector2<T> &rhs) {
lhs.x *= rhs.x; lhs.y *= rhs.y; return lhs;
}
// scalar divide assign
_CPU_AND_GPU_CODE_ friend Vector2<T> &operator /= (Vector2<T> &lhs, T d) {
if (d == 0) return lhs; lhs.x /= d; lhs.y /= d; return lhs;
}
// component-wise vector divide assign
_CPU_AND_GPU_CODE_ friend Vector2<T> &operator /= (Vector2<T> &lhs, const Vector2<T> &rhs) {
lhs.x /= rhs.x; lhs.y /= rhs.y; return lhs;
}
// component-wise vector add assign
_CPU_AND_GPU_CODE_ friend Vector2<T> &operator += (Vector2<T> &lhs, const Vector2<T> &rhs) {
lhs.x += rhs.x; lhs.y += rhs.y; return lhs;
}
// component-wise vector subtract assign
_CPU_AND_GPU_CODE_ friend Vector2<T> &operator -= (Vector2<T> &lhs, const Vector2<T> &rhs) {
lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs;
}
// unary negate
_CPU_AND_GPU_CODE_ friend Vector2<T> operator - (const Vector2<T> &rhs) {
Vector2<T> rv; rv.x = -rhs.x; rv.y = -rhs.y; return rv;
}
// vector add
_CPU_AND_GPU_CODE_ friend Vector2<T> operator + (const Vector2<T> &lhs, const Vector2<T> &rhs) {
Vector2<T> rv(lhs); return rv += rhs;
}
// vector subtract
_CPU_AND_GPU_CODE_ friend Vector2<T> operator - (const Vector2<T> &lhs, const Vector2<T> &rhs) {
Vector2<T> rv(lhs); return rv -= rhs;
}
// scalar multiply
_CPU_AND_GPU_CODE_ friend Vector2<T> operator * (const Vector2<T> &lhs, T rhs) {
Vector2<T> rv(lhs); return rv *= rhs;
}
// scalar multiply
_CPU_AND_GPU_CODE_ friend Vector2<T> operator * (T lhs, const Vector2<T> &rhs) {
Vector2<T> rv(lhs); return rv *= rhs;
}
// vector component-wise multiply
_CPU_AND_GPU_CODE_ friend Vector2<T> operator * (const Vector2<T> &lhs, const Vector2<T> &rhs) {
Vector2<T> rv(lhs); return rv *= rhs;
}
// scalar multiply
_CPU_AND_GPU_CODE_ friend Vector2<T> operator / (const Vector2<T> &lhs, T rhs) {
Vector2<T> rv(lhs); return rv /= rhs;
}
// vector component-wise multiply
_CPU_AND_GPU_CODE_ friend Vector2<T> operator / (const Vector2<T> &lhs, const Vector2<T> &rhs) {
Vector2<T> rv(lhs); return rv /= rhs;
}
////////////////////////////////////////////////////////
// Comparison operators
////////////////////////////////////////////////////////
// equality
_CPU_AND_GPU_CODE_ friend bool operator == (const Vector2<T> &lhs, const Vector2<T> &rhs) {
return (lhs.x == rhs.x) && (lhs.y == rhs.y);
}
// inequality
_CPU_AND_GPU_CODE_ friend bool operator != (const Vector2<T> &lhs, const Vector2<T> &rhs) {
return (lhs.x != rhs.x) || (lhs.y != rhs.y);
}
friend std::ostream& operator<<(std::ostream& os, const Vector2<T>& dt){
os << dt.x << ", " << dt.y;
return os;
}
};
template <class T> class Vector3 : public Vector3_ < T >
{
public:
typedef T value_type;
_CPU_AND_GPU_CODE_ inline int size() const { return 3; }
////////////////////////////////////////////////////////
// Constructors
////////////////////////////////////////////////////////
_CPU_AND_GPU_CODE_ Vector3(){} // Default constructor
_CPU_AND_GPU_CODE_ Vector3(const T &t) { this->x = t; this->y = t; this->z = t; } // Scalar constructor
_CPU_AND_GPU_CODE_ Vector3(const T *tp) { this->x = tp[0]; this->y = tp[1]; this->z = tp[2]; } // Construct from array
_CPU_AND_GPU_CODE_ Vector3(const T v0, const T v1, const T v2) { this->x = v0; this->y = v1; this->z = v2; } // Construct from explicit values
_CPU_AND_GPU_CODE_ explicit Vector3(const Vector4_<T> &u) { this->x = u.x; this->y = u.y; this->z = u.z; }
_CPU_AND_GPU_CODE_ explicit Vector3(const Vector2_<T> &u, T v0) { this->x = u.x; this->y = u.y; this->z = v0; }
_CPU_AND_GPU_CODE_ inline Vector3<int> toIntRound() const {
return Vector3<int>((int)ROUND(this->x), (int)ROUND(this->y), (int)ROUND(this->z));
}
_CPU_AND_GPU_CODE_ inline Vector3<int> toInt() const {
return Vector3<int>((int)(this->x), (int)(this->y), (int)(this->z));
}
_CPU_AND_GPU_CODE_ inline Vector3<int> toInt(Vector3<float> &residual) const {
Vector3<int> intRound = toInt();
residual = Vector3<float>(this->x - intRound.x, this->y - intRound.y, this->z - intRound.z);
return intRound;
}
_CPU_AND_GPU_CODE_ inline Vector3<short> toShortRound() const {
return Vector3<short>((short)ROUND(this->x), (short)ROUND(this->y), (short)ROUND(this->z));
}
_CPU_AND_GPU_CODE_ inline Vector3<short> toShortFloor() const {
return Vector3<short>((short)floor(this->x), (short)floor(this->y), (short)floor(this->z));
}
_CPU_AND_GPU_CODE_ inline Vector3<int> toIntFloor() const {
return Vector3<int>((int)floor(this->x), (int)floor(this->y), (int)floor(this->z));
}
_CPU_AND_GPU_CODE_ inline Vector3<int> toIntFloor(Vector3<float> &residual) const {
Vector3<float> intFloor(floor(this->x), floor(this->y), floor(this->z));
residual = *this - intFloor;
return Vector3<int>((int)intFloor.x, (int)intFloor.y, (int)intFloor.z);
}
_CPU_AND_GPU_CODE_ inline Vector3<unsigned char> toUChar() const {
Vector3<int> vi = toIntRound(); return Vector3<unsigned char>((unsigned char)CLAMP(vi.x, 0, 255), (unsigned char)CLAMP(vi.y, 0, 255), (unsigned char)CLAMP(vi.z, 0, 255));
}
_CPU_AND_GPU_CODE_ inline Vector3<float> toFloat() const {
return Vector3<float>((float)this->x, (float)this->y, (float)this->z);
}
_CPU_AND_GPU_CODE_ inline Vector3<float> normalised() const {
float norm = 1.0f / sqrt((float)(this->x * this->x + this->y * this->y + this->z * this->z));
return Vector3<float>((float)this->x * norm, (float)this->y * norm, (float)this->z * norm);
}
_CPU_AND_GPU_CODE_ const T *getValues() const { return this->v; }
_CPU_AND_GPU_CODE_ Vector3<T> &setValues(const T *rhs) { this->x = rhs[0]; this->y = rhs[1]; this->z = rhs[2]; return *this; }
// indexing operators
_CPU_AND_GPU_CODE_ T &operator [](int i) { return this->v[i]; }
_CPU_AND_GPU_CODE_ const T &operator [](int i) const { return this->v[i]; }
// type-cast operators
_CPU_AND_GPU_CODE_ operator T *() { return this->v; }
_CPU_AND_GPU_CODE_ operator const T *() const { return this->v; }
////////////////////////////////////////////////////////
// Math operators
////////////////////////////////////////////////////////
// scalar multiply assign
_CPU_AND_GPU_CODE_ friend Vector3<T> &operator *= (Vector3<T> &lhs, T d) {
lhs.x *= d; lhs.y *= d; lhs.z *= d; return lhs;
}
// component-wise vector multiply assign
_CPU_AND_GPU_CODE_ friend Vector3<T> &operator *= (Vector3<T> &lhs, const Vector3<T> &rhs) {
lhs.x *= rhs.x; lhs.y *= rhs.y; lhs.z *= rhs.z; return lhs;
}
// scalar divide assign
_CPU_AND_GPU_CODE_ friend Vector3<T> &operator /= (Vector3<T> &lhs, T d) {
lhs.x /= d; lhs.y /= d; lhs.z /= d; return lhs;
}
// component-wise vector divide assign
_CPU_AND_GPU_CODE_ friend Vector3<T> &operator /= (Vector3<T> &lhs, const Vector3<T> &rhs) {
lhs.x /= rhs.x; lhs.y /= rhs.y; lhs.z /= rhs.z; return lhs;
}
// component-wise vector add assign
_CPU_AND_GPU_CODE_ friend Vector3<T> &operator += (Vector3<T> &lhs, const Vector3<T> &rhs) {
lhs.x += rhs.x; lhs.y += rhs.y; lhs.z += rhs.z; return lhs;
}
// component-wise vector subtract assign
_CPU_AND_GPU_CODE_ friend Vector3<T> &operator -= (Vector3<T> &lhs, const Vector3<T> &rhs) {
lhs.x -= rhs.x; lhs.y -= rhs.y; lhs.z -= rhs.z; return lhs;
}
// unary negate
_CPU_AND_GPU_CODE_ friend Vector3<T> operator - (const Vector3<T> &rhs) {
Vector3<T> rv; rv.x = -rhs.x; rv.y = -rhs.y; rv.z = -rhs.z; return rv;
}
// vector add
_CPU_AND_GPU_CODE_ friend Vector3<T> operator + (const Vector3<T> &lhs, const Vector3<T> &rhs){
Vector3<T> rv(lhs); return rv += rhs;
}
// vector subtract
_CPU_AND_GPU_CODE_ friend Vector3<T> operator - (const Vector3<T> &lhs, const Vector3<T> &rhs){
Vector3<T> rv(lhs); return rv -= rhs;
}
// scalar multiply
_CPU_AND_GPU_CODE_ friend Vector3<T> operator * (const Vector3<T> &lhs, T rhs) {
Vector3<T> rv(lhs); return rv *= rhs;
}
// scalar multiply
_CPU_AND_GPU_CODE_ friend Vector3<T> operator * (T lhs, const Vector3<T> &rhs) {
Vector3<T> rv(lhs); return rv *= rhs;
}
// vector component-wise multiply
_CPU_AND_GPU_CODE_ friend Vector3<T> operator * (const Vector3<T> &lhs, const Vector3<T> &rhs) {
Vector3<T> rv(lhs); return rv *= rhs;
}
// scalar multiply
_CPU_AND_GPU_CODE_ friend Vector3<T> operator / (const Vector3<T> &lhs, T rhs) {
Vector3<T> rv(lhs); return rv /= rhs;
}
// vector component-wise multiply
_CPU_AND_GPU_CODE_ friend Vector3<T> operator / (const Vector3<T> &lhs, const Vector3<T> &rhs) {
Vector3<T> rv(lhs); return rv /= rhs;
}
////////////////////////////////////////////////////////
// Comparison operators
////////////////////////////////////////////////////////
// inequality
_CPU_AND_GPU_CODE_ friend bool operator != (const Vector3<T> &lhs, const Vector3<T> &rhs) {
return (lhs.x != rhs.x) || (lhs.y != rhs.y) || (lhs.z != rhs.z);
}
////////////////////////////////////////////////////////////////////////////////
// dimension specific operations
////////////////////////////////////////////////////////////////////////////////
// cross product
_CPU_AND_GPU_CODE_ friend Vector3<T> cross(const Vector3<T> &lhs, const Vector3<T> &rhs) {
Vector3<T> r;
r.x = lhs.y * rhs.z - lhs.z * rhs.y;
r.y = lhs.z * rhs.x - lhs.x * rhs.z;
r.z = lhs.x * rhs.y - lhs.y * rhs.x;
return r;
}
friend std::ostream& operator<<(std::ostream& os, const Vector3<T>& dt){
os << dt.x << ", " << dt.y << ", " << dt.z;
return os;
}
};
////////////////////////////////////////////////////////
// Non-member comparison operators
////////////////////////////////////////////////////////
// equality
template <typename T1, typename T2> _CPU_AND_GPU_CODE_ inline bool operator == (const Vector3<T1> &lhs, const Vector3<T2> &rhs){
return (lhs.x == rhs.x) && (lhs.y == rhs.y) && (lhs.z == rhs.z);
}
template <class T> class Vector4 : public Vector4_ < T >
{
public:
typedef T value_type;
_CPU_AND_GPU_CODE_ inline int size() const { return 4; }
////////////////////////////////////////////////////////
// Constructors
////////////////////////////////////////////////////////
_CPU_AND_GPU_CODE_ Vector4() {} // Default constructor
_CPU_AND_GPU_CODE_ Vector4(const T &t) { this->x = t; this->y = t; this->z = t; this->w = t; } //Scalar constructor
_CPU_AND_GPU_CODE_ Vector4(const T *tp) { this->x = tp[0]; this->y = tp[1]; this->z = tp[2]; this->w = tp[3]; } // Construct from array
_CPU_AND_GPU_CODE_ Vector4(const T v0, const T v1, const T v2, const T v3) { this->x = v0; this->y = v1; this->z = v2; this->w = v3; } // Construct from explicit values
_CPU_AND_GPU_CODE_ explicit Vector4(const Vector3_<T> &u, T v0) { this->x = u.x; this->y = u.y; this->z = u.z; this->w = v0; }
_CPU_AND_GPU_CODE_ explicit Vector4(const Vector2_<T> &u, T v0, T v1) { this->x = u.x; this->y = u.y; this->z = v0; this->w = v1; }
_CPU_AND_GPU_CODE_ inline Vector4<int> toIntRound() const {
return Vector4<int>((int)ROUND(this->x), (int)ROUND(this->y), (int)ROUND(this->z), (int)ROUND(this->w));
}
_CPU_AND_GPU_CODE_ inline Vector4<unsigned char> toUChar() const {
Vector4<int> vi = toIntRound(); return Vector4<unsigned char>((unsigned char)CLAMP(vi.x, 0, 255), (unsigned char)CLAMP(vi.y, 0, 255), (unsigned char)CLAMP(vi.z, 0, 255), (unsigned char)CLAMP(vi.w, 0, 255));
}
_CPU_AND_GPU_CODE_ inline Vector4<float> toFloat() const {
return Vector4<float>((float)this->x, (float)this->y, (float)this->z, (float)this->w);
}
_CPU_AND_GPU_CODE_ inline Vector4<T> homogeneousCoordinatesNormalize() const {
return (this->w <= 0) ? *this : Vector4<T>(this->x / this->w, this->y / this->w, this->z / this->w, 1);
}
_CPU_AND_GPU_CODE_ inline Vector3<T> toVector3() const {
return Vector3<T>(this->x, this->y, this->z);
}
_CPU_AND_GPU_CODE_ const T *getValues() const { return this->v; }
_CPU_AND_GPU_CODE_ Vector4<T> &setValues(const T *rhs) { this->x = rhs[0]; this->y = rhs[1]; this->z = rhs[2]; this->w = rhs[3]; return *this; }
// indexing operators
_CPU_AND_GPU_CODE_ T &operator [](int i) { return this->v[i]; }
_CPU_AND_GPU_CODE_ const T &operator [](int i) const { return this->v[i]; }
// type-cast operators
_CPU_AND_GPU_CODE_ operator T *() { return this->v; }
_CPU_AND_GPU_CODE_ operator const T *() const { return this->v; }
////////////////////////////////////////////////////////
// Math operators
////////////////////////////////////////////////////////
// scalar multiply assign
_CPU_AND_GPU_CODE_ friend Vector4<T> &operator *= (Vector4<T> &lhs, T d) {
lhs.x *= d; lhs.y *= d; lhs.z *= d; lhs.w *= d; return lhs;
}
// component-wise vector multiply assign
_CPU_AND_GPU_CODE_ friend Vector4<T> &operator *= (Vector4<T> &lhs, const Vector4<T> &rhs) {
lhs.x *= rhs.x; lhs.y *= rhs.y; lhs.z *= rhs.z; lhs.w *= rhs.w; return lhs;
}
// scalar divide assign
_CPU_AND_GPU_CODE_ friend Vector4<T> &operator /= (Vector4<T> &lhs, T d){
lhs.x /= d; lhs.y /= d; lhs.z /= d; lhs.w /= d; return lhs;
}
// component-wise vector divide assign
_CPU_AND_GPU_CODE_ friend Vector4<T> &operator /= (Vector4<T> &lhs, const Vector4<T> &rhs) {
lhs.x /= rhs.x; lhs.y /= rhs.y; lhs.z /= rhs.z; lhs.w /= rhs.w; return lhs;
}
// component-wise vector add assign
_CPU_AND_GPU_CODE_ friend Vector4<T> &operator += (Vector4<T> &lhs, const Vector4<T> &rhs) {
lhs.x += rhs.x; lhs.y += rhs.y; lhs.z += rhs.z; lhs.w += rhs.w; return lhs;
}
// component-wise vector subtract assign
_CPU_AND_GPU_CODE_ friend Vector4<T> &operator -= (Vector4<T> &lhs, const Vector4<T> &rhs) {
lhs.x -= rhs.x; lhs.y -= rhs.y; lhs.z -= rhs.z; lhs.w -= rhs.w; return lhs;
}
// unary negate
_CPU_AND_GPU_CODE_ friend Vector4<T> operator - (const Vector4<T> &rhs) {
Vector4<T> rv; rv.x = -rhs.x; rv.y = -rhs.y; rv.z = -rhs.z; rv.w = -rhs.w; return rv;
}
// vector add
_CPU_AND_GPU_CODE_ friend Vector4<T> operator + (const Vector4<T> &lhs, const Vector4<T> &rhs) {
Vector4<T> rv(lhs); return rv += rhs;
}
// vector subtract
_CPU_AND_GPU_CODE_ friend Vector4<T> operator - (const Vector4<T> &lhs, const Vector4<T> &rhs) {
Vector4<T> rv(lhs); return rv -= rhs;
}
// scalar multiply
_CPU_AND_GPU_CODE_ friend Vector4<T> operator * (const Vector4<T> &lhs, T rhs) {
Vector4<T> rv(lhs); return rv *= rhs;
}
// scalar multiply
_CPU_AND_GPU_CODE_ friend Vector4<T> operator * (T lhs, const Vector4<T> &rhs) {
Vector4<T> rv(lhs); return rv *= rhs;
}
// vector component-wise multiply
_CPU_AND_GPU_CODE_ friend Vector4<T> operator * (const Vector4<T> &lhs, const Vector4<T> &rhs) {
Vector4<T> rv(lhs); return rv *= rhs;
}
// scalar divide
_CPU_AND_GPU_CODE_ friend Vector4<T> operator / (const Vector4<T> &lhs, T rhs) {
Vector4<T> rv(lhs); return rv /= rhs;
}
// vector component-wise divide
_CPU_AND_GPU_CODE_ friend Vector4<T> operator / (const Vector4<T> &lhs, const Vector4<T> &rhs) {
Vector4<T> rv(lhs); return rv /= rhs;
}
////////////////////////////////////////////////////////
// Comparison operators
////////////////////////////////////////////////////////
// equality
_CPU_AND_GPU_CODE_ friend bool operator == (const Vector4<T> &lhs, const Vector4<T> &rhs) {
return (lhs.x == rhs.x) && (lhs.y == rhs.y) && (lhs.z == rhs.z) && (lhs.w == rhs.w);
}
// inequality
_CPU_AND_GPU_CODE_ friend bool operator != (const Vector4<T> &lhs, const Vector4<T> &rhs) {
return (lhs.x != rhs.x) || (lhs.y != rhs.y) || (lhs.z != rhs.z) || (lhs.w != rhs.w);
}
friend std::ostream& operator<<(std::ostream& os, const Vector4<T>& dt){
os << dt.x << ", " << dt.y << ", " << dt.z << ", " << dt.w;
return os;
}
};
template <class T> class Vector6 : public Vector6_ < T >
{
public:
typedef T value_type;
_CPU_AND_GPU_CODE_ inline int size() const { return 6; }
////////////////////////////////////////////////////////
// Constructors
////////////////////////////////////////////////////////
_CPU_AND_GPU_CODE_ Vector6() {} // Default constructor
_CPU_AND_GPU_CODE_ Vector6(const T &t) { this->v[0] = t; this->v[1] = t; this->v[2] = t; this->v[3] = t; this->v[4] = t; this->v[5] = t; } //Scalar constructor
_CPU_AND_GPU_CODE_ Vector6(const T *tp) { this->v[0] = tp[0]; this->v[1] = tp[1]; this->v[2] = tp[2]; this->v[3] = tp[3]; this->v[4] = tp[4]; this->v[5] = tp[5]; } // Construct from array
_CPU_AND_GPU_CODE_ Vector6(const T v0, const T v1, const T v2, const T v3, const T v4, const T v5) { this->v[0] = v0; this->v[1] = v1; this->v[2] = v2; this->v[3] = v3; this->v[4] = v4; this->v[5] = v5; } // Construct from explicit values
_CPU_AND_GPU_CODE_ explicit Vector6(const Vector4_<T> &u, T v0, T v1) { this->v[0] = u.x; this->v[1] = u.y; this->v[2] = u.z; this->v[3] = u.w; this->v[4] = v0; this->v[5] = v1; }
_CPU_AND_GPU_CODE_ explicit Vector6(const Vector3_<T> &u, T v0, T v1, T v2) { this->v[0] = u.x; this->v[1] = u.y; this->v[2] = u.z; this->v[3] = v0; this->v[4] = v1; this->v[5] = v2; }
_CPU_AND_GPU_CODE_ explicit Vector6(const Vector2_<T> &u, T v0, T v1, T v2, T v3) { this->v[0] = u.x; this->v[1] = u.y; this->v[2] = v0; this->v[3] = v1; this->v[4] = v2, this->v[5] = v3; }
_CPU_AND_GPU_CODE_ inline Vector6<int> toIntRound() const {
return Vector6<int>((int)ROUND(this[0]), (int)ROUND(this[1]), (int)ROUND(this[2]), (int)ROUND(this[3]), (int)ROUND(this[4]), (int)ROUND(this[5]));
}
_CPU_AND_GPU_CODE_ inline Vector6<unsigned char> toUChar() const {
Vector6<int> vi = toIntRound(); return Vector6<unsigned char>((unsigned char)CLAMP(vi[0], 0, 255), (unsigned char)CLAMP(vi[1], 0, 255), (unsigned char)CLAMP(vi[2], 0, 255), (unsigned char)CLAMP(vi[3], 0, 255), (unsigned char)CLAMP(vi[4], 0, 255), (unsigned char)CLAMP(vi[5], 0, 255));
}
_CPU_AND_GPU_CODE_ inline Vector6<float> toFloat() const {
return Vector6<float>((float)this[0], (float)this[1], (float)this[2], (float)this[3], (float)this[4], (float)this[5]);
}
_CPU_AND_GPU_CODE_ const T *getValues() const { return this->v; }
_CPU_AND_GPU_CODE_ Vector6<T> &setValues(const T *rhs) { this[0] = rhs[0]; this[1] = rhs[1]; this[2] = rhs[2]; this[3] = rhs[3]; this[4] = rhs[4]; this[5] = rhs[5]; return *this; }
// indexing operators
_CPU_AND_GPU_CODE_ T &operator [](int i) { return this->v[i]; }
_CPU_AND_GPU_CODE_ const T &operator [](int i) const { return this->v[i]; }
// type-cast operators
_CPU_AND_GPU_CODE_ operator T *() { return this->v; }
_CPU_AND_GPU_CODE_ operator const T *() const { return this->v; }
////////////////////////////////////////////////////////
// Math operators
////////////////////////////////////////////////////////
// scalar multiply assign
_CPU_AND_GPU_CODE_ friend Vector6<T> &operator *= (Vector6<T> &lhs, T d) {
lhs[0] *= d; lhs[1] *= d; lhs[2] *= d; lhs[3] *= d; lhs[4] *= d; lhs[5] *= d; return lhs;
}
// component-wise vector multiply assign
_CPU_AND_GPU_CODE_ friend Vector6<T> &operator *= (Vector6<T> &lhs, const Vector6<T> &rhs) {
lhs[0] *= rhs[0]; lhs[1] *= rhs[1]; lhs[2] *= rhs[2]; lhs[3] *= rhs[3]; lhs[4] *= rhs[4]; lhs[5] *= rhs[5]; return lhs;
}
// scalar divide assign
_CPU_AND_GPU_CODE_ friend Vector6<T> &operator /= (Vector6<T> &lhs, T d){
lhs[0] /= d; lhs[1] /= d; lhs[2] /= d; lhs[3] /= d; lhs[4] /= d; lhs[5] /= d; return lhs;
}
// component-wise vector divide assign
_CPU_AND_GPU_CODE_ friend Vector6<T> &operator /= (Vector6<T> &lhs, const Vector6<T> &rhs) {
lhs[0] /= rhs[0]; lhs[1] /= rhs[1]; lhs[2] /= rhs[2]; lhs[3] /= rhs[3]; lhs[4] /= rhs[4]; lhs[5] /= rhs[5]; return lhs;
}
// component-wise vector add assign
_CPU_AND_GPU_CODE_ friend Vector6<T> &operator += (Vector6<T> &lhs, const Vector6<T> &rhs) {
lhs[0] += rhs[0]; lhs[1] += rhs[1]; lhs[2] += rhs[2]; lhs[3] += rhs[3]; lhs[4] += rhs[4]; lhs[5] += rhs[5]; return lhs;
}
// component-wise vector subtract assign
_CPU_AND_GPU_CODE_ friend Vector6<T> &operator -= (Vector6<T> &lhs, const Vector6<T> &rhs) {
lhs[0] -= rhs[0]; lhs[1] -= rhs[1]; lhs[2] -= rhs[2]; lhs[3] -= rhs[3]; lhs[4] -= rhs[4]; lhs[5] -= rhs[5]; return lhs;
}
// unary negate
_CPU_AND_GPU_CODE_ friend Vector6<T> operator - (const Vector6<T> &rhs) {
Vector6<T> rv; rv[0] = -rhs[0]; rv[1] = -rhs[1]; rv[2] = -rhs[2]; rv[3] = -rhs[3]; rv[4] = -rhs[4]; rv[5] = -rhs[5]; return rv;
}
// vector add
_CPU_AND_GPU_CODE_ friend Vector6<T> operator + (const Vector6<T> &lhs, const Vector6<T> &rhs) {
Vector6<T> rv(lhs); return rv += rhs;
}
// vector subtract
_CPU_AND_GPU_CODE_ friend Vector6<T> operator - (const Vector6<T> &lhs, const Vector6<T> &rhs) {
Vector6<T> rv(lhs); return rv -= rhs;
}
// scalar multiply
_CPU_AND_GPU_CODE_ friend Vector6<T> operator * (const Vector6<T> &lhs, T rhs) {
Vector6<T> rv(lhs); return rv *= rhs;
}
// scalar multiply
_CPU_AND_GPU_CODE_ friend Vector6<T> operator * (T lhs, const Vector6<T> &rhs) {
Vector6<T> rv(lhs); return rv *= rhs;
}
// vector component-wise multiply
_CPU_AND_GPU_CODE_ friend Vector6<T> operator * (const Vector6<T> &lhs, const Vector6<T> &rhs) {
Vector6<T> rv(lhs); return rv *= rhs;
}
// scalar divide
_CPU_AND_GPU_CODE_ friend Vector6<T> operator / (const Vector6<T> &lhs, T rhs) {
Vector6<T> rv(lhs); return rv /= rhs;
}
// vector component-wise divide
_CPU_AND_GPU_CODE_ friend Vector6<T> operator / (const Vector6<T> &lhs, const Vector6<T> &rhs) {
Vector6<T> rv(lhs); return rv /= rhs;
}
////////////////////////////////////////////////////////
// Comparison operators
////////////////////////////////////////////////////////
// equality
_CPU_AND_GPU_CODE_ friend bool operator == (const Vector6<T> &lhs, const Vector6<T> &rhs) {
return (lhs[0] == rhs[0]) && (lhs[1] == rhs[1]) && (lhs[2] == rhs[2]) && (lhs[3] == rhs[3]) && (lhs[4] == rhs[4]) && (lhs[5] == rhs[5]);
}
// inequality
_CPU_AND_GPU_CODE_ friend bool operator != (const Vector6<T> &lhs, const Vector6<T> &rhs) {
return (lhs[0] != rhs[0]) || (lhs[1] != rhs[1]) || (lhs[2] != rhs[2]) || (lhs[3] != rhs[3]) || (lhs[4] != rhs[4]) || (lhs[5] != rhs[5]);
}
friend std::ostream& operator<<(std::ostream& os, const Vector6<T>& dt){
os << dt[0] << ", " << dt[1] << ", " << dt[2] << ", " << dt[3] << ", " << dt[4] << ", " << dt[5];
return os;
}
};
template <class T, int s> class VectorX : public VectorX_ < T, s >
{
public:
typedef T value_type;
_CPU_AND_GPU_CODE_ inline int size() const { return this->vsize; }
////////////////////////////////////////////////////////
// Constructors
////////////////////////////////////////////////////////
_CPU_AND_GPU_CODE_ VectorX() { this->vsize = s; } // Default constructor
_CPU_AND_GPU_CODE_ VectorX(const T &t) { for (int i = 0; i < s; i++) this->v[i] = t; } //Scalar constructor
_CPU_AND_GPU_CODE_ VectorX(const T *tp) { for (int i = 0; i < s; i++) this->v[i] = tp[i]; } // Construct from array
// indexing operators
_CPU_AND_GPU_CODE_ T &operator [](int i) { return this->v[i]; }
_CPU_AND_GPU_CODE_ const T &operator [](int i) const { return this->v[i]; }
_CPU_AND_GPU_CODE_ inline VectorX<int, s> toIntRound() const {
VectorX<int, s> retv;
for (int i = 0; i < s; i++) retv[i] = (int)ROUND(this->v[i]);
return retv;
}
_CPU_AND_GPU_CODE_ inline VectorX<unsigned char, s> toUChar() const {
VectorX<int, s> vi = toIntRound();
VectorX<unsigned char, s> retv;
for (int i = 0; i < s; i++) retv[i] = (unsigned char)CLAMP(vi[0], 0, 255);
return retv;
}
_CPU_AND_GPU_CODE_ inline VectorX<float, s> toFloat() const {
VectorX<float, s> retv;
for (int i = 0; i < s; i++) retv[i] = (float) this->v[i];
return retv;
}
_CPU_AND_GPU_CODE_ const T *getValues() const { return this->v; }
_CPU_AND_GPU_CODE_ VectorX<T, s> &setValues(const T *rhs) { for (int i = 0; i < s; i++) this->v[i] = rhs[i]; return *this; }
_CPU_AND_GPU_CODE_ void Clear(T v){
for (int i = 0; i < s; i++)
this->v[i] = v;
}
// type-cast operators
_CPU_AND_GPU_CODE_ operator T *() { return this->v; }
_CPU_AND_GPU_CODE_ operator const T *() const { return this->v; }
////////////////////////////////////////////////////////
// Math operators
////////////////////////////////////////////////////////
// scalar multiply assign
_CPU_AND_GPU_CODE_ friend VectorX<T, s> &operator *= (VectorX<T, s> &lhs, T d) {
for (int i = 0; i < s; i++) lhs[i] *= d; return lhs;
}
// component-wise vector multiply assign
_CPU_AND_GPU_CODE_ friend VectorX<T, s> &operator *= (VectorX<T, s> &lhs, const VectorX<T, s> &rhs) {
for (int i = 0; i < s; i++) lhs[i] *= rhs[i]; return lhs;
}
// scalar divide assign
_CPU_AND_GPU_CODE_ friend VectorX<T, s> &operator /= (VectorX<T, s> &lhs, T d){
for (int i = 0; i < s; i++) lhs[i] /= d; return lhs;
}
// component-wise vector divide assign
_CPU_AND_GPU_CODE_ friend VectorX<T, s> &operator /= (VectorX<T, s> &lhs, const VectorX<T, s> &rhs) {
for (int i = 0; i < s; i++) lhs[i] /= rhs[i]; return lhs;
}
// component-wise vector add assign
_CPU_AND_GPU_CODE_ friend VectorX<T, s> &operator += (VectorX<T, s> &lhs, const VectorX<T, s> &rhs) {
for (int i = 0; i < s; i++) lhs[i] += rhs[i]; return lhs;
}
// component-wise vector subtract assign
_CPU_AND_GPU_CODE_ friend VectorX<T, s> &operator -= (VectorX<T, s> &lhs, const VectorX<T, s> &rhs) {
for (int i = 0; i < s; i++) lhs[i] -= rhs[i]; return lhs;
}
// unary negate
_CPU_AND_GPU_CODE_ friend VectorX<T, s> operator - (const VectorX<T, s> &rhs) {
VectorX<T, s> rv; for (int i = 0; i < s; i++) rv[i] = -rhs[i]; return rv;
}
// vector add
_CPU_AND_GPU_CODE_ friend VectorX<T, s> operator + (const VectorX<T, s> &lhs, const VectorX<T, s> &rhs) {
VectorX<T, s> rv(lhs); return rv += rhs;
}
// vector subtract
_CPU_AND_GPU_CODE_ friend VectorX<T, s> operator - (const VectorX<T, s> &lhs, const VectorX<T, s> &rhs) {
VectorX<T, s> rv(lhs); return rv -= rhs;
}
// scalar multiply
_CPU_AND_GPU_CODE_ friend VectorX<T, s> operator * (const VectorX<T, s> &lhs, T rhs) {
VectorX<T, s> rv(lhs); return rv *= rhs;
}
// scalar multiply
_CPU_AND_GPU_CODE_ friend VectorX<T, s> operator * (T lhs, const VectorX<T, s> &rhs) {
VectorX<T, s> rv(lhs); return rv *= rhs;
}
// vector component-wise multiply
_CPU_AND_GPU_CODE_ friend VectorX<T, s> operator * (const VectorX<T, s> &lhs, const VectorX<T, s> &rhs) {
VectorX<T, s> rv(lhs); return rv *= rhs;
}
// scalar divide
_CPU_AND_GPU_CODE_ friend VectorX<T, s> operator / (const VectorX<T, s> &lhs, T rhs) {
VectorX<T, s> rv(lhs); return rv /= rhs;
}
// vector component-wise divide
_CPU_AND_GPU_CODE_ friend VectorX<T, s> operator / (const VectorX<T, s> &lhs, const VectorX<T, s> &rhs) {
VectorX<T, s> rv(lhs); return rv /= rhs;
}
////////////////////////////////////////////////////////
// Comparison operators
////////////////////////////////////////////////////////
// equality
_CPU_AND_GPU_CODE_ friend bool operator == (const VectorX<T, s> &lhs, const VectorX<T, s> &rhs) {
for (int i = 0; i < s; i++) if (lhs[i] != rhs[i]) return false;
return true;
}
// inequality
_CPU_AND_GPU_CODE_ friend bool operator != (const VectorX<T, s> &lhs, const Vector6<T> &rhs) {
for (int i = 0; i < s; i++) if (lhs[i] != rhs[i]) return true;
return false;
}
friend std::ostream& operator<<(std::ostream& os, const VectorX<T, s>& dt){
for (int i = 0; i < s; i++) os << dt[i] << "\n";
return os;
}
};
////////////////////////////////////////////////////////////////////////////////
// Generic vector operations
////////////////////////////////////////////////////////////////////////////////
template< class T> _CPU_AND_GPU_CODE_ inline T sqr(const T &v) { return v*v; }
// compute the dot product of two vectors
template<class T> _CPU_AND_GPU_CODE_ inline typename T::value_type dot(const T &lhs, const T &rhs) {
typename T::value_type r = 0;
for (int i = 0; i < lhs.size(); i++)
r += lhs[i] * rhs[i];
return r;
}
// return the length of the provided vector
template< class T> _CPU_AND_GPU_CODE_ inline typename T::value_type length(const T &vec) {
return sqrt(dot(vec, vec));
}
// return the normalized version of the vector
template< class T> _CPU_AND_GPU_CODE_ inline T normalize(const T &vec) {
typename T::value_type sum = length(vec);
return sum == 0 ? T(typename T::value_type(0)) : vec / sum;
}
//template< class T> _CPU_AND_GPU_CODE_ inline T min(const T &lhs, const T &rhs) {
// return lhs <= rhs ? lhs : rhs;
//}
//template< class T> _CPU_AND_GPU_CODE_ inline T max(const T &lhs, const T &rhs) {
// return lhs >= rhs ? lhs : rhs;
//}
//component wise min
template< class T> _CPU_AND_GPU_CODE_ inline T minV(const T &lhs, const T &rhs) {
T rv;
for (int i = 0; i < lhs.size(); i++)
rv[i] = min(lhs[i], rhs[i]);
return rv;
}
// component wise max
template< class T>
_CPU_AND_GPU_CODE_ inline T maxV(const T &lhs, const T &rhs) {
T rv;
for (int i = 0; i < lhs.size(); i++)
rv[i] = max(lhs[i], rhs[i]);
return rv;
}
};