-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathV2.h
1191 lines (1144 loc) · 81.1 KB
/
V2.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
#pragma once
#pragma warning(disable:26495)
#include <CppCore/Root.h>
#include <CppCore/Math/Util.h>
#if defined(CPPCORE_HAVE_OGRE3D)
#include <Ogre/OgreVector2.h>
#endif
namespace CppCore
{
//------------------------------------------------------------------------------------------------------------------------//
// ROOT TEMPLATE V2 [L1, ABSTRACT] //
//------------------------------------------------------------------------------------------------------------------------//
/// <summary>
/// Abstract 2D Vector Template for Floating Point (32/64) AND Integer (32/64). [L1]
/// </summary>
template <typename V, typename F>
class V2
{
protected:
INLINE V* thiss() const { return (V*)this; }
public:
union
{
struct { F x, y; };
F vals[2];
};
//------------------------------------------------------------------------------------------------------------------------//
INLINE V2() { }
INLINE V2(const F x, const F y) : x(x), y(y) { }
INLINE V2(const F scalar) : x(scalar), y(scalar) { }
INLINE V2(const F values[2]) : x(values[0]), y(values[1]) { }
//------------------------------------------------------------------------------------------------------------------------//
static INLINE V ZERO() { return V((F)0.0, (F)0.0); }
static INLINE V UNITX() { return V((F)1.0, (F)0.0); }
static INLINE V UNITY() { return V((F)0.0, (F)1.0); }
//------------------------------------------------------------------------------------------------------------------------//
INLINE void* operator new (size_t size) { return ::malloc(size); }
INLINE void* operator new (size_t size, V* v) { return v; }
INLINE void* operator new[](size_t size) { return ::malloc(size); }
INLINE void operator delete (void* ptr) { return ::free(ptr); }
INLINE void operator delete (void* ptr, V* v) { }
INLINE void operator delete[] (void* ptr) { return ::free(ptr); }
INLINE F operator [] (const size_t i) const { return vals[i]; }
INLINE F operator [] (const size_t i) { return vals[i]; }
//------------------------------------------------------------------------------------------------------------------------//
INLINE bool operator == (const V& v) const { return ((x == v.x) & (y == v.y)); }
INLINE bool operator != (const V& v) const { return ((x != v.x) | (y != v.y)); }
INLINE bool operator < (const V& v) const { return ((x < v.x) & (y < v.y)); }
INLINE bool operator <= (const V& v) const { return ((x <= v.x) & (y <= v.y)); }
INLINE bool operator > (const V& v) const { return ((x > v.x) & (y > v.y)); }
INLINE bool operator >= (const V& v) const { return ((x >= v.x) & (y >= v.y)); }
INLINE V operator + (const V& v) const { return V(x + v.x, y + v.y); }
INLINE V operator - (const V& v) const { return V(x - v.x, y - v.y); }
INLINE V operator * (const V& v) const { return V(x * v.x, y * v.y); }
INLINE V operator / (const V& v) const { return V(x / v.x, y / v.y); }
INLINE V operator + (const F s) const { return V(x + s, y + s); }
INLINE V operator - (const F s) const { return V(x - s, y - s); }
INLINE V operator * (const F s) const { return V(x * s, y * s); }
INLINE V operator / (const F s) const { return V(x / s, y / s); }
INLINE V& operator = (const V& v) { x = v.x; y = v.y; return *thiss(); }
INLINE V& operator += (const V& v) { x += v.x; y += v.y; return *thiss(); }
INLINE V& operator -= (const V& v) { x -= v.x; y -= v.y; return *thiss(); }
INLINE V& operator *= (const V& v) { x *= v.x; y *= v.y; return *thiss(); }
INLINE V& operator /= (const V& v) { x /= v.x; y /= v.y; return *thiss(); }
INLINE V& operator = (const F s) { x = s; y = s; return *thiss(); }
INLINE V& operator += (const F s) { x += s; y += s; return *thiss(); }
INLINE V& operator -= (const F s) { x -= s; y -= s; return *thiss(); }
INLINE V& operator *= (const F s) { x *= s; y *= s; return *thiss(); }
INLINE V& operator /= (const F s) { x /= s; y /= s; return *thiss(); }
//------------------------------------------------------------------------------------------------------------------------//
INLINE V operator - () const { return V(-x, -y); }
INLINE const V& operator + () const { return *this; }
//------------------------------------------------------------------------------------------------------------------------//
template<typename T> INLINE T& ref() const { return *((T*)this); }
//------------------------------------------------------------------------------------------------------------------------//
INLINE V& point() const { return *thiss(); }
INLINE void madd(const V& m, const V& a) { *thiss() = (*thiss() * m) + a; }
INLINE void msub(const V& m, const V& s) { *thiss() = (*thiss() * m) - s; }
INLINE bool isZero() const { return x == (F)0.0 && y == (F)0.0; }
INLINE bool isZero(const F e2) const { return thiss()->length2() <= e2; }
INLINE bool equals(const V& v, const F e2) const { return (*thiss() - v).length2() <= e2; }
INLINE void swap(V& v) { V t(v); v=*thiss(); *thiss()=t; }
INLINE F cross(const V& v) const { return x * v.y - y * v.x; }
INLINE F dot(const V& v) const { return x * v.x + y * v.y; }
INLINE F length2() const { return thiss()->dot(*thiss()); }
INLINE F length() const { return V::_sqrt(thiss()->length2()); }
INLINE F distance2(const V& v) const { return (*thiss() - v).length2(); }
INLINE F distance(const V& v) const { return V::_sqrt(thiss()->distance2(v)); }
INLINE V yx() const { return V(y, x); }
INLINE void yx() { std::swap(x, y); }
INLINE V maxC(const V& v) const { return V(v.x > x ? v.x : x, v.y > y ? v.y : y); }
INLINE V minC(const V& v) const { return V(v.x < x ? v.x : x, v.y < y ? v.y : y); }
INLINE V boundC(const V& mi, const V& ma) const { V t(thiss()->minC(ma)); t.max(mi); return t; }
INLINE V absC() const { return V(V::_abs(x), V::_abs(y)); }
INLINE void max(const V& v) { if (v.x > x) x = v.x; if (v.y > y) y = v.y; }
INLINE void min(const V& v) { if (v.x < x) x = v.x; if (v.y < y) y = v.y; }
INLINE void bound(const V& mi, const V& ma) { thiss()->min(ma); thiss()->max(mi); }
INLINE void abs() { x = V::_abs(x); y = V::_abs(y); }
INLINE V perp1() const { return V(y, -x); }
INLINE V perp2() const { return V(-y, x); }
INLINE V signs() const { return V((F)SIGN(x), (F)SIGN(y)); }
//------------------------------------------------------------------------------------------------------------------------//
INLINE F side(const V& s, const V& e) const { return (e - s).cross(*thiss() - s); }
INLINE bool inside(const V& min, const V& max) const { return *thiss() >= min && *thiss() <= max; } // Box
INLINE bool inside(const V& min, const V& max, const F e) const { return *thiss() >= (min - e) && *thiss() <= (max + e); } // Box
INLINE bool inside(const V& m, const F r2) const { return thiss()->distance2(m) <= r2; } // Circle
INLINE bool inside(const V& m, const F r2, const F e) const { return thiss()->distance2(m) <= (r2 + e); } // Circle
INLINE F area(const V& p) const { V t(p-*thiss()); t.abs(); return t.x*t.y; } // Box
INLINE F area(const V& p, const V& q) const { return (p-*thiss()).cross(q-*thiss()) / (F)2; } // Triangle
//------------------------------------------------------------------------------------------------------------------------//
INLINE::std::string toString() { return '(' + ::std::to_string(x) + '/' + ::std::to_string(y) + ')';}
//------------------------------------------------------------------------------------------------------------------------//
template<typename PRNG>
INLINE void randomize(PRNG& prng, const F min, const F max)
{
x = prng.next(min, max);
y = prng.next(min, max);
}
template<typename PRNG>
static INLINE V random(PRNG& prng, const F min, const F max)
{
return V(prng.next(min, max), prng.next(min, max));
}
template<typename PRNG>
static INLINE void random(PRNG& prng, const F min, const F max, V* v, const size_t size)
{
for (size_t i = 0; i < size; i++)
v[i].randomize(prng, min, max);
}
};
//------------------------------------------------------------------------------------------------------------------------//
// FLOATING POINT & INTEGER TEMPLATES [L2, ABSTRACT] //
//------------------------------------------------------------------------------------------------------------------------//
/// <summary>
/// Abstract 2D Vector Template for Floating Point (32/64) [L2]
/// </summary>
template <typename V, typename F>
class V2fdt : public V2<V, F>
{
protected:
using V2<V, F>::thiss;
public:
using V2<V, F>::x;
using V2<V, F>::y;
using V2<V, F>::vals;
public:
INLINE V2fdt() { }
INLINE V2fdt(const F x, const F y) : V2<V, F>(x, y) { }
INLINE V2fdt(const F scalar) : V2<V, F>(scalar) { }
INLINE V2fdt(const F values[2]) : V2<V, F>(values) { }
//------------------------------------------------------------------------------------------------------------------------//
INLINE V& operator /= (const V& v) { x /= v.x; y /= v.y; return (V&)*this; }
INLINE V operator / (const V& v) const { return V(x / v.x, y / v.y); }
INLINE V& operator /= (const F s) { F t = (F)1.0 / s; x *= t; y *= t; return (V&)*this; }
INLINE V operator / (const F s) const { F t = (F)1.0 / s; return V(x * t, y * t); }
//------------------------------------------------------------------------------------------------------------------------//
INLINE void normalise() { *thiss() /= thiss()->length(); }
INLINE V normaliseC() const { V t(*thiss()); t.normalise(); return t; }
INLINE void scaleTo(const F l) { *thiss() *= (l / thiss()->length()); }
INLINE V scaleToC(const F l) const { V t(*thiss()); t.scaleTo(l); return t; }
//------------------------------------------------------------------------------------------------------------------------//
INLINE V roundC() const { return V(V::_round(x), V::_round(y)); }
INLINE V floorC() const { return V(V::_floor(x), V::_floor(y)); }
INLINE V ceilC() const { return V(V::_ceil(x), V::_ceil(y)); }
INLINE V roundC(const F n) const { V c(*thiss()); c.round(n); return c; }
INLINE V ceilPow2C() const { V c(*thiss()); c.ceilPow2(); return c; }
INLINE void round() { x = V::_round(x); y = V::_round(y); }
INLINE void floor() { x = V::_floor(x); y = V::_floor(y); }
INLINE void ceil() { x = V::_ceil(x); y = V::_ceil(y); }
INLINE void round(const F n) { *thiss() /= n; thiss()->round(); *thiss() *= n; }
//------------------------------------------------------------------------------------------------------------------------//
INLINE void ceilPow2() { thiss()->ceil(); x = (F)ngptwo32((uint32_t)x); y = (F)ngptwo32((uint32_t)y); }
INLINE void floorPow2() { thiss()->floor(); x = (F)nlptwo32((uint32_t)x); y = (F)nlptwo32((uint32_t)y); }
//------------------------------------------------------------------------------------------------------------------------//
INLINE F angle() const { return V::_acos(x / thiss()->length()); }
INLINE F angleNoN() const { return V::_acos(x); }
INLINE F angleOri() const { F t = thiss()->angle(); if (y < (F)0.0) t = (F)TWOPI - t; return t; }
INLINE F angleOriNoN() const { F t = thiss()->angleNoN(); if (y < (F)0.0) t = (F)TWOPI - t; return t; }
INLINE F angle(const V& v) const { F lp = thiss()->length() * v.length(); return V::_acos(thiss()->dot(v) / lp); }
INLINE F angleOri(const V& v) const { F t = thiss()->angle(v); if (thiss()->cross(v) < (F)0.0) t = (F)TWOPI - t; return t; }
//------------------------------------------------------------------------------------------------------------------------//
//INLINE F area(const V& p, const V& q) const { return (F)0.5 * (p - *thiss()).cross(q - *thiss()); } // Triangle
//------------------------------------------------------------------------------------------------------------------------//
INLINE void rotate(F r)
{
F cs = V::_cos(r);
F sn = V::_sin(r);
F p = x;
x = p * cs - y * sn;
y = p * sn + y * cs;
}
INLINE V project(const V& v) const
{
const F denom = v.length2();
if (denom > 0.0f)
{
const F num = thiss()->dot(v);
return v * (num / denom);
}
else
return V::ZERO();
}
INLINE bool intersectCircleLineInternal(const V& s, const V& e, const F r, const F eps, V& d, F& t1, F& t2) const
{
d = e - s;
V f = s - *thiss();
const F a = d.length2();
const F b = (F)2.0 * f.dot(d);
const F c = f.length2() - (r*r);
const F div = (F)2.0 * a;
const F discriminant = b * b - (F)4.0 * a * c;
if (discriminant < (F)0.0 || ISZERO(div, eps))
return false;
const F sqrt = V::_sqrt(discriminant);
t1 = (-b - sqrt) / div;
t2 = (-b + sqrt) / div;
return true;
}
INLINE bool intersectCircleLine(const V& s, const V& e, const F r, const F eps, const bool withinside) const
{
V d;
F t1, t2;
if (!intersectCircleLineInternal(s, e, r, eps, d, t1, t2))
return false;
return
(t1 >= (F)0.0 && t1 <= (F)1.0) || // intersect
(t2 >= (F)0.0 && t2 <= (F)1.0) || // intersect
(withinside && t1 <= (F)0.0 && t2 >= (F)1.0); // inside
}
INLINE bool intersectCircleLine(const V& s, const V& e, const F r, const F eps, const bool withinside, V& p) const
{
V d;
F t1, t2;
if (!intersectCircleLineInternal(s, e, r, eps, d, t1, t2))
return false;
if (t1 >= (F)0.0 && t1 <= (F)1.0) // intersect
{
p = s + (d * t1);
return true;
}
else if (t2 >= (F)0.0 && t2 <= (F)1.0) // intersect
{
p = s + (d * t2);
return true;
}
else if (withinside && t1 <= (F)0.0 && t2 >= (F)1.0) // inside
{
p = (F)0.0;
return true;
}
else // outside
{
p = (F)0.0;
return false;
}
}
INLINE F minSquaredDistanceToFiniteLine(const V& p1, const V& p2, size_t& closest) const
{
// from p1 to p2
V diff = p2 - p1;
// squared length
const F l2 = diff.length2();
// p1=p2, both closest
if (l2 == (F)0.0)
{
closest = 1;
return thiss()->distance2(p1);
}
const F t = ((*thiss() - p1).dot(diff)) / l2;
// p1 is closest
if (t < (F)0.0)
{
closest = 1;
return thiss()->distance2(p1);
}
// p2 is closest
else if (t > (F)1.0)
{
closest = 2;
return thiss()->distance2(p2);
}
// point on line is closest
else
{
closest = 3;
diff *= t;
return thiss()->distance2(p1 + diff);
}
}
static INLINE bool insideFrustum(const V& p, const V& vp, const F vangle, const F width)
{
// from vp to p
const V se = p - vp;
// center
V c(V::_cos(vangle), V::_sin(vangle));
// behind
if (c.dot(se) < (F)0.0)
return false;
// offset
const V o = c * width;
// first left than right
V v;
// too left
v.x = -c.y;
v.y = c.x;
v += o;
if (v.dot(se) < (F)0.0)
return false;
// too right
v.x = c.y;
v.y = -c.x;
v += o;
if (v.dot(se) < (F)0.0)
return false;
// ok
return true;
}
//------------------------------------------------------------------------------------------------------------------------//
static INLINE V randomN() { V t(V::random()); t.normalise(); return t; }
static INLINE void randomN(V* v, const size_t size) { for (size_t i = 0; i < size; i++) v[i] = V::randomN(); }
static INLINE V fromAngle(const F rad) { V t(V::UNITX()); t.rotate(rad); return t; }
//------------------------------------------------------------------------------------------------------------------------//
};
/// <summary>
/// Abstract 2D Vector for Integer (32/64) [L2]
/// </summary>
template <typename V, typename F>
class V2ilt : public V2<V, F>
{
protected:
using V2<V, F>::thiss;
public:
using V2<V, F>::x;
using V2<V, F>::y;
using V2<V, F>::vals;
public:
static INLINE F _abs(const F s) { return std::abs(s); }
static INLINE F _sqrt(const F s) { return (F)std::sqrt(s); }
//------------------------------------------------------------------------------------------------------------------------//
INLINE V2ilt() { }
INLINE V2ilt(const F x, const F y) : V2<V, F>(x, y) { }
INLINE V2ilt(const F s) : V2<V, F>(s) { }
INLINE V2ilt(const F v[2]) : V2<V, F>(v) { }
};
//------------------------------------------------------------------------------------------------------------------------//
// 32-BIT & 64-BIT TEMPLATES [L3, ABSTRACT] //
//------------------------------------------------------------------------------------------------------------------------//
/// <summary>
/// Abstract 2D Vector Template for Single Precision FP [L3]
/// </summary>
template <typename V>
class V2ft : public V2fdt<V, float>
{
public:
static INLINE float _abs(const float s) { return ::fabsf(s); }
static INLINE float _round(const float s) { return ::roundf(s); }
static INLINE float _floor(const float s) { return ::floorf(s); }
static INLINE float _ceil(const float s) { return ::ceilf(s); }
static INLINE float _sqrt(const float s) { return ::sqrtf(s); }
static INLINE float _cos(const float s) { return ::cosf(s); }
static INLINE float _sin(const float s) { return ::sinf(s); }
static INLINE float _acos(const float s) { return ::acosf(s); }
//------------------------------------------------------------------------------------------------------------------------//
INLINE V2ft() { }
INLINE V2ft(const float x, const float y) : V2fdt<V, float>(x, y) { }
INLINE V2ft(const float s) : V2fdt<V, float>(s) { }
INLINE V2ft(const float v[2]) : V2fdt<V, float>(v) { }
INLINE V2ft(const double x, const double y) : V2fdt<V, float>((float)x, (float)y) { }
INLINE V2ft(const double s) : V2fdt<V, float>((float)s) { }
INLINE V2ft(const double v[2]) : V2fdt<V, float>((float)v[0], (float)v[1]) { }
INLINE V2ft(const int32_t x, const int32_t y) : V2fdt<V, float>((float)x, (float)y) { }
INLINE V2ft(const int32_t s) : V2fdt<V, float>((float)s) { }
INLINE V2ft(const int32_t v[2]) : V2fdt<V, float>((float)v[0], (float)v[1]) { }
INLINE V2ft(const int64_t x, const int64_t y) : V2fdt<V, float>((float)x, (float)y) { }
INLINE V2ft(const int64_t s) : V2fdt<V, float>((float)s) { }
INLINE V2ft(const int64_t v[2]) : V2fdt<V, float>((float)v[0], (float)v[1]) { }
#if defined(CPPCORE_HAVE_OGRE3D) && (OGRE_DOUBLE_PRECISION == 0)
INLINE operator Ogre::Vector2& () { return *(Ogre::Vector2*)this; }
#endif
};
/// <summary>
/// Abstract 2D Vector Template for Double Precision FP [L3]
/// </summary>
template <typename V>
class V2dt : public V2fdt<V, double>
{
public:
static INLINE double _abs(const double s) { return std::abs(s); }
static INLINE double _round(const double s) { return std::round(s); }
static INLINE double _floor(const double s) { return std::floor(s); }
static INLINE double _ceil(const double s) { return std::ceil(s); }
static INLINE double _sqrt(const double s) { return std::sqrt(s); }
static INLINE double _cos(const double s) { return std::cos(s); }
static INLINE double _sin(const double s) { return std::sin(s); }
static INLINE double _acos(const double s) { return std::acos(s); }
//------------------------------------------------------------------------------------------------------------------------//
INLINE V2dt() { }
INLINE V2dt(const double x, const double y) : V2fdt<V, double>(x, y) { }
INLINE V2dt(const double s) : V2fdt<V, double>(s) { }
INLINE V2dt(const double v[2]) : V2fdt<V, double>(v) { }
INLINE V2dt(const float x, const float y) : V2fdt<V, double>((double)x, (double)y) { }
INLINE V2dt(const float s) : V2fdt<V, double>((double)s) { }
INLINE V2dt(const float v[2]) : V2fdt<V, double>((double)v[0], (double)v[1]) { }
INLINE V2dt(const int32_t x, const int32_t y) : V2fdt<V, double>((double)x, (double)y) { }
INLINE V2dt(const int32_t s) : V2fdt<V, double>((double)s) { }
INLINE V2dt(const int32_t v[2]) : V2fdt<V, double>((double)v[0], (double)v[1]) { }
INLINE V2dt(const int64_t x, const int64_t y) : V2fdt<V, double>((double)x, (double)y) { }
INLINE V2dt(const int64_t s) : V2fdt<V, double>((double)s) { }
INLINE V2dt(const int64_t v[2]) : V2fdt<V, double>((double)v[0], (double)v[1]) { }
#if defined(CPPCORE_HAVE_OGRE3D) && (OGRE_DOUBLE_PRECISION == 1)
INLINE operator Ogre::Vector2& () { return *(Ogre::Vector2*)this; }
#endif
};
/// <summary>
/// Abstract 2D Vector Template for Integer (32) [L3]
/// </summary>
template <typename V>
class V2it : public V2ilt<V, int32_t>
{
public:
INLINE V2it() { }
INLINE V2it(const int32_t x, const int32_t y) : V2ilt<V, int32_t>(x, y) { }
INLINE V2it(const int32_t s) : V2ilt<V, int32_t>(s) { }
INLINE V2it(const int32_t v[2]) : V2ilt<V, int32_t>(v) { }
INLINE V2it(const float x, const float y) : V2ilt<V, int32_t>((int32_t)x,(int32_t)y) { }
INLINE V2it(const float s) : V2ilt<V, int32_t>((int32_t)s) { }
INLINE V2it(const float v[2]) : V2ilt<V, int32_t>((int32_t)v[0],(int32_t)v[1]) { }
INLINE V2it(const double x, const double y) : V2ilt<V, int32_t>((int32_t)x,(int32_t)y) { }
INLINE V2it(const double s) : V2ilt<V, int32_t>((int32_t)s) { }
INLINE V2it(const double v[2]) : V2ilt<V, int32_t>((int32_t)v[0], (int32_t)v[1]){ }
INLINE V2it(const int64_t x, const int64_t y) : V2ilt<V, int32_t>((int32_t)x,(int32_t)y) { }
INLINE V2it(const int64_t s) : V2ilt<V, int32_t>((int32_t)s) { }
INLINE V2it(const int64_t v[2]) : V2ilt<V, int32_t>((int32_t)v[0],(int32_t)v[1]) { }
};
/// <summary>
/// Abstract 2D Vector Template for Integer (64) [L3]
/// </summary>
template <typename V>
class V2lt : public V2ilt<V, int64_t>
{
public:
INLINE V2lt() { }
INLINE V2lt(const int64_t x, const int64_t y) : V2ilt<V, int64_t>(x, y) { }
INLINE V2lt(const int64_t s) : V2ilt<V, int64_t>(s) { }
INLINE V2lt(const int64_t v[2]) : V2ilt<V, int64_t>(v) { }
INLINE V2lt(const float x, const float y) : V2ilt<V, int64_t>((int64_t)x, (int64_t)y) { }
INLINE V2lt(const float s) : V2ilt<V, int64_t>((int64_t)s) { }
INLINE V2lt(const float v[2]) : V2ilt<V, int64_t>((int64_t)v[0],(int64_t)v[1]) { }
INLINE V2lt(const double x, const double y) : V2ilt<V, int64_t>((int64_t)x,(int64_t)y) { }
INLINE V2lt(const double s) : V2ilt<V, int64_t>((int64_t)s) { }
INLINE V2lt(const double v[2]) : V2ilt<V, int64_t>((int64_t)v[0],(int64_t)v[1]) { }
INLINE V2lt(const int32_t x, const int32_t y) : V2ilt<V, int64_t>((int64_t)x,(int64_t)y) { }
INLINE V2lt(const int32_t s) : V2ilt<V, int64_t>((int64_t)s) { }
INLINE V2lt(const int32_t v[2]) : V2ilt<V, int64_t>((int64_t)v[0],(int64_t)v[1]) { }
};
//------------------------------------------------------------------------------------------------------------------------//
// GENERIC/NON-SIMD CLASSES [L4] //
//------------------------------------------------------------------------------------------------------------------------//
class V2fg;
class V2dg;
class V2ig;
class V2lg;
/// <summary>
/// Single Precision 2D Vector (Generic, no SIMD) [L4]
/// </summary>
class V2fg : public V2ft<V2fg>
{
public:
INLINE V2fg() { }
INLINE V2fg(const float x, const float y) : V2ft<V2fg>(x, y) { }
INLINE V2fg(const float s) : V2ft<V2fg>(s) { }
INLINE V2fg(const float v[2]) : V2ft<V2fg>(v) { }
INLINE V2fg(const double x, const double y) : V2ft<V2fg>(x, y) { }
INLINE V2fg(const double s) : V2ft<V2fg>(s) { }
INLINE V2fg(const double v[2]) : V2ft<V2fg>(v) { }
INLINE V2fg(const int32_t x, const int32_t y) : V2ft<V2fg>(x, y) { }
INLINE V2fg(const int32_t s) : V2ft<V2fg>(s) { }
INLINE V2fg(const int32_t v[2]) : V2ft<V2fg>(v) { }
INLINE V2fg(const int64_t x, const int64_t y) : V2ft<V2fg>(x, y) { }
INLINE V2fg(const int64_t s) : V2ft<V2fg>(s) { }
INLINE V2fg(const int64_t v[2]) : V2ft<V2fg>(v) { }
INLINE V2fg(const V2dt<V2dg>& v) : V2ft<V2fg>((float)v.x, (float)v.y) { }
INLINE V2fg(const V2it<V2ig>& v) : V2ft<V2fg>((float)v.x, (float)v.y) { }
INLINE V2fg(const V2lt<V2lg>& v) : V2ft<V2fg>((float)v.x, (float)v.y) { }
};
/// <summary>
/// Double Precision 2D Vector (Generic, no SIMD) [L4]
/// </summary>
class V2dg : public V2dt<V2dg>
{
public:
INLINE V2dg() { }
INLINE V2dg(const double x, const double y) : V2dt<V2dg>(x, y) { }
INLINE V2dg(const double s) : V2dt<V2dg>(s, s) { }
INLINE V2dg(const double v[2]) : V2dt<V2dg>(v) { }
INLINE V2dg(const float x, const float y) : V2dt<V2dg>(x, y) { }
INLINE V2dg(const float s) : V2dt<V2dg>(s) { }
INLINE V2dg(const float v[2]) : V2dt<V2dg>(v) { }
INLINE V2dg(const int32_t x, const int32_t y) : V2dt<V2dg>(x, y) { }
INLINE V2dg(const int32_t s) : V2dt<V2dg>(s) { }
INLINE V2dg(const int32_t v[2]) : V2dt<V2dg>(v) { }
INLINE V2dg(const int64_t x, const int64_t y) : V2dt<V2dg>(x, y) { }
INLINE V2dg(const int64_t s) : V2dt<V2dg>(s) { }
INLINE V2dg(const int64_t v[2]) : V2dt<V2dg>(v) { }
INLINE V2dg(const V2ft<V2fg>& v) : V2dt<V2dg>((double)v.x, (double)v.y) { }
INLINE V2dg(const V2it<V2ig>& v) : V2dt<V2dg>((double)v.x, (double)v.y) { }
INLINE V2dg(const V2lt<V2lg>& v) : V2dt<V2dg>((double)v.x, (double)v.y) { }
};
/// <summary>
/// 32-Bit Integer 2D Vector (Generic, no SIMD) [L4]
/// </summary>
class V2ig : public V2it<V2ig>
{
public:
INLINE V2ig() { }
INLINE V2ig(const int32_t x, const int32_t y) : V2it<V2ig>(x, y) { }
INLINE V2ig(const int32_t s) : V2it<V2ig>(s, s) { }
INLINE V2ig(const int32_t v[2]) : V2it<V2ig>(v) { }
INLINE V2ig(const float x, const float y) : V2it<V2ig>(x, y) { }
INLINE V2ig(const float s) : V2it<V2ig>(s) { }
INLINE V2ig(const float v[2]) : V2it<V2ig>(v) { }
INLINE V2ig(const double x, const double y) : V2it<V2ig>(x, y) { }
INLINE V2ig(const double s) : V2it<V2ig>(s) { }
INLINE V2ig(const double v[2]) : V2it<V2ig>(v) { }
INLINE V2ig(const int64_t x, const int64_t y) : V2it<V2ig>(x, y) { }
INLINE V2ig(const int64_t s) : V2it<V2ig>(s) { }
INLINE V2ig(const int64_t v[2]) : V2it<V2ig>(v) { }
INLINE V2ig(const V2ft<V2fg>& v) : V2it<V2ig>((int32_t)v.x, (int32_t)v.y) { }
INLINE V2ig(const V2dt<V2dg>& v) : V2it<V2ig>((int32_t)v.x, (int32_t)v.y) { }
INLINE V2ig(const V2lt<V2lg>& v) : V2it<V2ig>((int32_t)v.x, (int32_t)v.y) { }
};
/// <summary>
/// 64-Bit Integer 2D Vector (Generic, no SIMD) [L4]
/// </summary>
class V2lg : public V2lt<V2lg>
{
public:
INLINE V2lg() { }
INLINE V2lg(const int64_t x, const int64_t y) : V2lt<V2lg>(x, y) { }
INLINE V2lg(const int64_t s) : V2lt<V2lg>(s) { }
INLINE V2lg(const int64_t v[2]) : V2lt<V2lg>(v) { }
INLINE V2lg(const float x, const float y) : V2lt<V2lg>(x, y) { }
INLINE V2lg(const float s) : V2lt<V2lg>(s) { }
INLINE V2lg(const float v[2]) : V2lt<V2lg>(v) { }
INLINE V2lg(const double x, const double y) : V2lt<V2lg>(x, y) { }
INLINE V2lg(const double s) : V2lt<V2lg>(s) { }
INLINE V2lg(const double v[2]) : V2lt<V2lg>(v) { }
INLINE V2lg(const int32_t x, const int32_t y) : V2lt<V2lg>(x, y) { }
INLINE V2lg(const int32_t s) : V2lt<V2lg>(s) { }
INLINE V2lg(const int32_t v[2]) : V2lt<V2lg>(v) { }
INLINE V2lg(const V2ft<V2fg>& v) : V2lt<V2lg>((int64_t)v.x, (int64_t)v.y) { }
INLINE V2lg(const V2dt<V2dg>& v) : V2lt<V2lg>((int64_t)v.x, (int64_t)v.y) { }
INLINE V2lg(const V2it<V2ig>& v) : V2lt<V2lg>((int64_t)v.x, (int64_t)v.y) { }
};
//------------------------------------------------------------------------------------------------------------------------//
// SIMD CLASSES [L4] //
//------------------------------------------------------------------------------------------------------------------------//
#if defined(CPPCORE_CPUFEAT_SSE2)
class V2fs;
class V2ds;
class V2is;
class V2ls;
/// <summary>
/// Single Precision 2D Vector (SSE/SIMD)
/// </summary>
class CPPCORE_ALIGN8_INTRIN V2fs : public V2ft<V2fs>
{
public:
INLINE __m128 load() const { return _mm_castpd_ps(_mm_load_sd((const double*)vals)); }
INLINE void store(const __m128& v) { _mm_store_sd((double*)vals, _mm_castps_pd(v)); }
//------------------------------------------------------------------------------------------------------------------------//
INLINE V2fs() { }
INLINE V2fs(const __m128& v) { store(v); }
INLINE V2fs(const float x, const float y) { store(_mm_set_ps(0.0f, 0.0f, y, x)); }
INLINE V2fs(const float s) { store(_mm_set_ps(0.0f, 0.0f, s, s)); }
INLINE V2fs(const float v[2]) { store(_mm_castpd_ps(_mm_load_sd((const double*)v))); }
INLINE V2fs(const double x, const double y) { store(_mm_cvtpd_ps(_mm_set_pd(y, x))); }
INLINE V2fs(const double s) { store(_mm_cvtpd_ps(_mm_set1_pd(s))); }
INLINE V2fs(const double v[2]) { store(_mm_cvtpd_ps(_mm_loadu_pd(v))); }
INLINE V2fs(const int32_t x, const int32_t y) { store(_mm_cvtepi32_ps(_mm_set_epi32(0, 0, y, x))); }
INLINE V2fs(const int32_t s) { store(_mm_cvtepi32_ps(_mm_set_epi32(0, 0, s, s))); }
INLINE V2fs(const int32_t v[2]) { store(_mm_cvtepi32_ps(_mm_castpd_si128(_mm_load_sd((const double*)v)))); }
#if defined(CPPCORE_CPUFEAT_AVX512DQ) && defined(CPPCORE_CPUFEAT_AVX512VL)
INLINE V2fs(const int64_t x, const int32_t y) { store(_mm_cvtepi64_ps(_mm_set_epi64x(y, x))); }
INLINE V2fs(const int64_t s) { store(_mm_cvtepi64_ps(_mm_set1_epi64x(s))); }
INLINE V2fs(const int64_t v[2]) { store(_mm_cvtepi64_ps(_mm_loadu_epi64(v))); }
#else
INLINE V2fs(const int64_t x, const int32_t y) { store(_mm_set_ps(0.0f, 0.0f, (float)y, (float)x)); }
INLINE V2fs(const int64_t s) { store(_mm_set_ps(0.0f, 0.0f, (float)s, (float)s)); }
INLINE V2fs(const int64_t v[2]) { store(_mm_set_ps(0.0f, 0.0f, (float)v[1], (float)v[0])); }
#endif
//------------------------------------------------------------------------------------------------------------------------//
template<typename V> INLINE V2fs(const V2dt<V>& v) { store(_mm_cvtpd_ps(_mm_loadu_pd(v.vals))); }
template<typename V> INLINE V2fs(const V2it<V>& v) { store(_mm_cvtepi32_ps(_mm_castpd_si128(_mm_load_sd((const double*)v.vals)))); }
#if defined(CPPCORE_CPUFEAT_AVX512DQ) && defined(CPPCORE_CPUFEAT_AVX512VL)
template<typename V> INLINE V2fs(const V2lt<V>& v) { store(_mm_cvtepi64_ps(_mm_loadu_epi64(v.vals))); }
#else
template<typename V> INLINE V2fs(const V2lt<V>& v) { store(_mm_set_ps(0.0f, 0.0f, (float)v.y, (float)v.x)); }
#endif
//------------------------------------------------------------------------------------------------------------------------//
INLINE void* operator new (size_t size) { return CPPCORE_ALIGNED_ALLOC(size, 8U); }
INLINE void* operator new (size_t size,V2fs* v){ return v; }
INLINE void* operator new[](size_t size) { return CPPCORE_ALIGNED_ALLOC(size, 8U); }
INLINE void operator delete (void* ptr) { return CPPCORE_ALIGNED_FREE(ptr); }
INLINE void operator delete (void* ptr,V2fs* v){ }
INLINE void operator delete[] (void* ptr) { return CPPCORE_ALIGNED_FREE(ptr); }
INLINE bool operator == (const V2fs& v) const { return _mm_movemask_ps(_mm_cmpeq_ps(load(), v.load())) == 0x0F; }
INLINE bool operator != (const V2fs& v) const { return _mm_movemask_ps(_mm_cmpeq_ps(load(), v.load())) != 0x0F; }
INLINE bool operator < (const V2fs& v) const { return _mm_movemask_ps(_mm_cmplt_ps(load(), v.load())) == 0x03; }
INLINE bool operator <= (const V2fs& v) const { return _mm_movemask_ps(_mm_cmple_ps(load(), v.load())) == 0x0F; }
INLINE bool operator > (const V2fs& v) const { return _mm_movemask_ps(_mm_cmpgt_ps(load(), v.load())) == 0x03; }
INLINE bool operator >= (const V2fs& v) const { return _mm_movemask_ps(_mm_cmpge_ps(load(), v.load())) == 0x0F; }
INLINE V2fs operator + (const V2fs& v) const { return V2fs(_mm_add_ps(load(), v.load())); }
INLINE V2fs operator - (const V2fs& v) const { return V2fs(_mm_sub_ps(load(), v.load())); }
INLINE V2fs operator * (const V2fs& v) const { return V2fs(_mm_mul_ps(load(), v.load())); }
INLINE V2fs operator / (const V2fs& v) const { return V2fs(_mm_div_ps(load(), v.load())); }
INLINE V2fs operator + (const float s) const { return V2fs(_mm_add_ps(load(), _mm_set1_ps(s))); }
INLINE V2fs operator - (const float s) const { return V2fs(_mm_sub_ps(load(), _mm_set1_ps(s))); }
INLINE V2fs operator * (const float s) const { return V2fs(_mm_mul_ps(load(), _mm_set1_ps(s))); }
INLINE V2fs operator / (const float s) const { return V2fs(_mm_div_ps(load(), _mm_set1_ps(s))); }
INLINE V2fs operator - () const { return V2fs(_mm_sub_ps(_mm_setzero_ps(), load())); }
INLINE const V2fs& operator + () const { return *this; }
INLINE V2fs& operator = (const V2fs& v) { store(v.load()); return *this; }
INLINE V2fs& operator += (const V2fs& v) { store(_mm_add_ps(load(), v.load())); return *this; }
INLINE V2fs& operator -= (const V2fs& v) { store(_mm_sub_ps(load(), v.load())); return *this; }
INLINE V2fs& operator *= (const V2fs& v) { store(_mm_mul_ps(load(), v.load())); return *this; }
INLINE V2fs& operator /= (const V2fs& v) { store(_mm_div_ps(load(), v.load())); return *this; }
INLINE V2fs& operator = (const float s) { store(_mm_set1_ps(s)); return *this; }
INLINE V2fs& operator += (const float s) { store(_mm_add_ps(load(), _mm_set1_ps(s))); return *this; }
INLINE V2fs& operator -= (const float s) { store(_mm_sub_ps(load(), _mm_set1_ps(s))); return *this; }
INLINE V2fs& operator *= (const float s) { store(_mm_mul_ps(load(), _mm_set1_ps(s))); return *this; }
INLINE V2fs& operator /= (const float s) { store(_mm_div_ps(load(), _mm_set1_ps(s))); return *this; }
//------------------------------------------------------------------------------------------------------------------------//
INLINE void swap(V2fs& v) { __m128 t(load()); store(v.load()); v.store(t); }
INLINE V2fs absC() const { return V2fs(_mm_andnot_ps(_mm_set1_ps(-0.f), load())); }
INLINE V2fs maxC(const V2fs& v) const { return V2fs(_mm_max_ps(load(), v.load())); }
INLINE V2fs minC(const V2fs& v) const { return V2fs(_mm_min_ps(load(), v.load())); }
INLINE V2fs boundC(const V2fs& mi, const V2fs& ma) const { return V2fs(_mm_max_ps(_mm_min_ps(load(), ma.load()), mi.load())); }
INLINE void abs() { store(_mm_andnot_ps(_mm_set1_ps(-0.), load())); }
INLINE void max(const V2fs& v) { store(_mm_max_ps(load(), v.load())); }
INLINE void min(const V2fs& v) { store(_mm_min_ps(load(), v.load())); }
INLINE void bound(const V2fs& mi, const V2fs& ma) { store(_mm_max_ps(_mm_min_ps(load(), ma.load()), mi.load())); }
//------------------------------------------------------------------------------------------------------------------------//
INLINE void madd(const V2fs& m, const V2fs& a) { store(madd128f(load(), m.load(), a.load())); }
INLINE void msub(const V2fs& m, const V2fs& s) { store(msub128f(load(), m.load(), s.load())); }
//------------------------------------------------------------------------------------------------------------------------//
INLINE float dot(const V2fs& v) const
{
__m128 a(_mm_mul_ps(load(), v.load()));
__m128 b(permute128f<_MM_SHUFFLE(2, 3, 0, 1)>(a));
__m128 c(_mm_add_ss(a, b));
return _mm_cvtss_f32(c);
}
INLINE float length() const
{
__m128 t(load());
__m128 a(_mm_mul_ps(t, t));
__m128 b(permute128f<_MM_SHUFFLE(2, 3, 0, 1)>(a));
__m128 c(_mm_add_ss(a, b));
__m128 d(_mm_sqrt_ss(c));
return _mm_cvtss_f32(d);
}
INLINE float side(const V2fs& s, const V2fs& e) const
{
__m128 t(s.load());
__m128 a(_mm_sub_ps(e.load(), t));
__m128 b(_mm_sub_ps(load(), t));
__m128 c(permute128f<_MM_SHUFFLE(2, 3, 0, 1)>(b));
__m128 d(_mm_mul_ps(a, c));
__m128 f(permute128f<_MM_SHUFFLE(2, 3, 0, 1)>(d));
__m128 g(_mm_sub_ss(d, f));
return _mm_cvtss_f32(g);
}
INLINE bool inside(const V2fs& min, const V2fs& max) const
{
const __m128 t = load();
return _mm_movemask_ps(_mm_and_ps(
_mm_cmpge_ps(t, min.load()),
_mm_cmple_ps(t, max.load()))) == 0x0F;
}
INLINE bool inside(const V2fs& min, const V2fs& max, const float e) const
{
const __m128 t = load();
const __m128 eps(broadcast128f(&e));
const __m128 a(_mm_and_ps(
_mm_cmpge_ps(t, _mm_sub_ps(min.load(), eps)),
_mm_cmple_ps(t, _mm_add_ps(max.load(), eps))));
return _mm_movemask_ps(a) == 0x0F;
}
INLINE bool inside(const V2fs& m, const float r2) const
{
__m128 t(_mm_sub_ps(load(), m.load()));
__m128 a(_mm_mul_ps(t, t));
__m128 b(permute128f<_MM_SHUFFLE(2, 3, 0, 1)>(a));
__m128 c(_mm_add_ss(a, b));
return _mm_comile_ss(c, _mm_load_ss(&r2));
}
INLINE bool inside(const V2fs& m, const float r2, const float e) const
{
__m128 t(_mm_sub_ps(load(), m.load()));
__m128 a(_mm_mul_ps(t, t));
__m128 b(permute128f<_MM_SHUFFLE(2, 3, 0, 1)>(a));
__m128 c(_mm_add_ss(a, b));
return _mm_comile_ss(c, _mm_add_ss(_mm_load_ss(&r2), _mm_load_ss(&e)));
}
INLINE float area(const V2fs& p) const
{
return V2ft<V2fs>::area(p);
}
INLINE float area(const V2fs& p, const V2fs& q) const
{
static constexpr const float HALF = 0.5f;
__m128 t(load());
__m128 a(_mm_sub_ps(p.load(), t));
__m128 b(_mm_sub_ps(q.load(), t));
__m128 c(permute128f<_MM_SHUFFLE(2, 3, 0, 1)>(b));
__m128 d(_mm_mul_ps(a, c));
__m128 e(permute128f<_MM_SHUFFLE(2, 3, 0, 1)>(d));
__m128 f(_mm_sub_ss(d, e));
return _mm_cvtss_f32(_mm_mul_ss(f, _mm_load_ss(&HALF)));
}
#if defined(CPPCORE_CPUFEAT_SSE41)
INLINE void round() { store(_mm_round_ps(load(), _MM_FROUND_NINT)); }
INLINE void floor() { store(_mm_round_ps(load(), _MM_FROUND_FLOOR)); }
INLINE void ceil() { store(_mm_round_ps(load(), _MM_FROUND_CEIL)); }
INLINE void round(const float n)
{
const __m128 t1(broadcast128f(&n));
store(_mm_mul_ps(_mm_round_ps(_mm_div_ps(load(), t1), _MM_FROUND_NINT), t1));
}
#endif
};
/// <summary>
/// Double Precision 2D Vector (SSE/SIMD)
/// </summary>
class CPPCORE_ALIGN16_INTRIN V2ds : public V2dt<V2ds>
{
public:
INLINE __m128d load() const { return _mm_load_pd(vals); }
INLINE void store(const __m128d& v) { _mm_store_pd(vals, v); }
//------------------------------------------------------------------------------------------------------------------------//
INLINE V2ds() { }
INLINE V2ds(const __m128d& v) { store(v); }
INLINE V2ds(const double x, const double y) { store(_mm_set_pd(y, x)); }
INLINE V2ds(const double s) { store(_mm_set1_pd(s)); }
INLINE V2ds(const double v[2]) { store(_mm_loadu_pd(v)); }
INLINE V2ds(const float x, const float y) { store(_mm_cvtps_pd(_mm_set_ps(0.0f, 0.0f, y, x))); }
INLINE V2ds(const float s) { store(_mm_cvtps_pd(_mm_set1_ps(s))); }
INLINE V2ds(const float v[2]) { store(_mm_cvtps_pd(_mm_castsi128_ps(_mm_loadl_epi64((__m128i*)v)))); }
INLINE V2ds(const int32_t x, const int32_t y) { store(_mm_cvtepi32_pd(_mm_set_epi32(0, 0, y, x))); }
INLINE V2ds(const int32_t s) { store(_mm_cvtepi32_pd(_mm_set1_epi32(s))); }
INLINE V2ds(const int32_t v[2]) { store(_mm_cvtepi32_pd(_mm_loadl_epi64((__m128i*)v))); }
#if defined(CPPCORE_CPUFEAT_AVX512DQ) && defined(CPPCORE_CPUFEAT_AVX512VL)
INLINE V2ds(const int64_t x, const int64_t y) { store(_mm_cvtepi64_pd(_mm_set_epi64x(y, x))); }
INLINE V2ds(const int64_t s) { store(_mm_cvtepi64_pd(_mm_set1_epi64x(s))); }
INLINE V2ds(const int64_t v[2]) { store(_mm_cvtepi64_pd(_mm_loadu_si128((__m128i*)v))); }
#else
INLINE V2ds(const int64_t x, const int64_t y) { store(_mm_set_pd((double)y, (double)x)); }
INLINE V2ds(const int64_t s) { store(_mm_set1_pd((double)s)); }
INLINE V2ds(const int64_t v[2]) { store(_mm_set_pd((double)v[1], (double)v[0])); }
#endif
//------------------------------------------------------------------------------------------------------------------------//
template<typename V> INLINE V2ds(const V2ft<V>& v) { store(_mm_cvtps_pd(_mm_castsi128_ps(_mm_loadl_epi64((__m128i*)v.vals))));}
template<typename V> INLINE V2ds(const V2it<V>& v) { store(_mm_cvtepi32_pd(_mm_loadl_epi64((__m128i*)v.vals))); }
#if defined(CPPCORE_CPUFEAT_AVX512DQ) && defined(CPPCORE_CPUFEAT_AVX512VL)
template<typename V> INLINE V2ds(const V2lt<V>& v) { store(_mm_cvtepi64_pd(_mm_loadu_si128((__m128i*)v.vals))); }
#else
template<typename V> INLINE V2ds(const V2lt<V>& v) { store(_mm_set_pd((double)v.y, (double)v.x)); }
#endif
//------------------------------------------------------------------------------------------------------------------------//
INLINE void* operator new (size_t size) { return CPPCORE_ALIGNED_ALLOC(size, 16U); }
INLINE void* operator new (size_t size,V2ds* v){ return v; }
INLINE void* operator new[](size_t size) { return CPPCORE_ALIGNED_ALLOC(size, 16U); }
INLINE void operator delete (void* ptr) { return CPPCORE_ALIGNED_FREE(ptr); }
INLINE void operator delete (void* ptr,V2ds* v){ }
INLINE void operator delete[] (void* ptr) { return CPPCORE_ALIGNED_FREE(ptr); }
INLINE bool operator == (const V2ds& v) const { return _mm_movemask_pd(_mm_cmpeq_pd(load(), v.load())) == 0x03; }
INLINE bool operator != (const V2ds& v) const { return _mm_movemask_pd(_mm_cmpeq_pd(load(), v.load())) != 0x03; }
INLINE bool operator < (const V2ds& v) const { return _mm_movemask_pd(_mm_cmplt_pd(load(), v.load())) == 0x03; }
INLINE bool operator <= (const V2ds& v) const { return _mm_movemask_pd(_mm_cmple_pd(load(), v.load())) == 0x03; }
INLINE bool operator > (const V2ds& v) const { return _mm_movemask_pd(_mm_cmpgt_pd(load(), v.load())) == 0x03; }
INLINE bool operator >= (const V2ds& v) const { return _mm_movemask_pd(_mm_cmpge_pd(load(), v.load())) == 0x03; }
INLINE V2ds operator + (const V2ds& v) const { return V2ds(_mm_add_pd(load(), v.load())); }
INLINE V2ds operator - (const V2ds& v) const { return V2ds(_mm_sub_pd(load(), v.load())); }
INLINE V2ds operator * (const V2ds& v) const { return V2ds(_mm_mul_pd(load(), v.load())); }
INLINE V2ds operator / (const V2ds& v) const { return V2ds(_mm_div_pd(load(), v.load())); }
INLINE V2ds operator + (const double s) const { return V2ds(_mm_add_pd(load(), _mm_set1_pd(s))); }
INLINE V2ds operator - (const double s) const { return V2ds(_mm_sub_pd(load(), _mm_set1_pd(s))); }
INLINE V2ds operator * (const double s) const { return V2ds(_mm_mul_pd(load(), _mm_set1_pd(s))); }
INLINE V2ds operator / (const double s) const { return V2ds(_mm_div_pd(load(), _mm_set1_pd(s))); }
INLINE V2ds operator - () const { return V2ds(_mm_sub_pd(_mm_setzero_pd(), load())); }
INLINE const V2ds& operator + () const { return *this; }
INLINE V2ds& operator = (const V2ds& v) { store(v.load()); return *this; }
INLINE V2ds& operator += (const V2ds& v) { store(_mm_add_pd(load(), v.load())); return *this; }
INLINE V2ds& operator -= (const V2ds& v) { store(_mm_sub_pd(load(), v.load())); return *this; }
INLINE V2ds& operator *= (const V2ds& v) { store(_mm_mul_pd(load(), v.load())); return *this; }
INLINE V2ds& operator /= (const V2ds& v) { store(_mm_div_pd(load(), v.load())); return *this; }
INLINE V2ds& operator = (const double s) { store(_mm_set1_pd(s)); return *this; }
INLINE V2ds& operator += (const double s) { store(_mm_add_pd(load(), _mm_set1_pd(s))); return *this; }
INLINE V2ds& operator -= (const double s) { store(_mm_sub_pd(load(), _mm_set1_pd(s))); return *this; }
INLINE V2ds& operator *= (const double s) { store(_mm_mul_pd(load(), _mm_set1_pd(s))); return *this; }
INLINE V2ds& operator /= (const double s) { store(_mm_div_pd(load(), _mm_set1_pd(s))); return *this; }
//------------------------------------------------------------------------------------------------------------------------//
INLINE void swap(V2ds& v) { __m128d t(load()); store(v.load()); v.store(t); }
INLINE V2ds absC() const { return V2ds(_mm_andnot_pd(_mm_set1_pd(-0.), load())); }
INLINE V2ds maxC(const V2ds& v) const { return V2ds(_mm_max_pd(load(), v.load())); }
INLINE V2ds minC(const V2ds& v) const { return V2ds(_mm_min_pd(load(), v.load())); }
INLINE V2ds boundC(const V2ds& mi, const V2ds& ma) const { return V2ds(_mm_max_pd(_mm_min_pd(load(), ma.load()), mi.load())); }
INLINE void abs() { store(_mm_andnot_pd(_mm_set1_pd(-0.), load())); }
INLINE void max(const V2ds& v) { store(_mm_max_pd(load(), v.load())); }
INLINE void min(const V2ds& v) { store(_mm_min_pd(load(), v.load())); }
INLINE void bound(const V2ds& mi, const V2ds& ma) { store(_mm_max_pd(_mm_min_pd(load(), ma.load()), mi.load())); }
//------------------------------------------------------------------------------------------------------------------------//
INLINE void rotate(double r)
{
__m128d cs(_mm_set1_pd(_cos(r)));
__m128d sn(_mm_set1_pd(_sin(r)));
__m128d p(_mm_set_pd(x, -y));
store(_mm_add_pd(_mm_mul_pd(load(), cs), _mm_mul_pd(p, sn)));
}
INLINE double cross(const V2ds& v) const
{
__m128d a(_mm_shuffle_pd(v.load(), v.load(), _MM_SHUFFLE2(0, 1)));
__m128d b(_mm_mul_pd(load(), a));
__m128d c(_mm_shuffle_pd(b, b, _MM_SHUFFLE2(0, 1)));
__m128d d(_mm_sub_sd(b, c));
return _mm_cvtsd_f64(d);
}
INLINE bool inside(const V2ds& min, const V2ds& max) const
{
__m128d a(_mm_cmpge_pd(load(), min.load()));
__m128d b(_mm_cmple_pd(load(), max.load()));
__m128d c(_mm_and_pd(a, b));
return _mm_movemask_pd(c) == 0x03;
}
INLINE bool inside(const V2ds& min, const V2ds& max, const double e) const
{
__m128d eps(_mm_set1_pd(e));
__m128d a(_mm_cmpge_pd(load(), _mm_sub_pd(min.load(), eps)));
__m128d b(_mm_cmple_pd(load(), _mm_add_pd(max.load(), eps)));
__m128d c(_mm_and_pd(a, b));
return _mm_movemask_pd(c) == 0x03;
}
#if defined(CPPCORE_CPUFEAT_SSE41)
INLINE double dot(const V2ds& v) const { return _mm_cvtsd_f64(_mm_dp_pd(load(), v.load(), 0x31)); }
INLINE double length() const { __m128d t(load()); return _mm_cvtsd_f64(_mm_sqrt_pd(_mm_dp_pd(t, t, 0x31))); }
INLINE void round() { store(_mm_round_pd(load(), _MM_FROUND_NINT)); }
INLINE void floor() { store(_mm_round_pd(load(), _MM_FROUND_FLOOR)); }
INLINE void ceil() { store(_mm_round_pd(load(), _MM_FROUND_CEIL)); }
INLINE void normalise() { __m128d t(load()); store(_mm_div_pd(t, _mm_sqrt_pd(_mm_dp_pd(t, t, 0x33)))); }
INLINE void round(const double n)
{
const __m128d t1(_mm_set1_pd(n));
store(_mm_mul_pd(_mm_round_pd(_mm_div_pd(load(), t1), _MM_FROUND_NINT), t1));
}
#else
INLINE double dot(const V2ds& v) const
{
__m128d a(_mm_mul_pd(load(), v.load()));
__m128d b(_mm_castps_pd(_mm_movehl_ps(_mm_undefined_ps(), _mm_castpd_ps(a))));
__m128d c(_mm_add_sd(a, b));
return _mm_cvtsd_f64(c);
}
INLINE double length() const
{
__m128d t(load());
__m128d a(_mm_mul_pd(t, t));
__m128d b(_mm_castps_pd(_mm_movehl_ps(_mm_undefined_ps(), _mm_castpd_ps(a))));
__m128d c(_mm_add_sd(a, b));
__m128d d(_mm_sqrt_sd(c, c));
return _mm_cvtsd_f64(d);
}
INLINE void normalise()
{
__m128d t(load());
__m128d a(_mm_mul_pd(t, t));
__m128d b(_mm_shuffle_pd(a, a, _MM_SHUFFLE2(0,1)));
__m128d c(_mm_add_pd(a, b));
__m128d d(_mm_sqrt_pd(c));
store(_mm_div_pd(t, d));
}
#endif
#if defined(CPPCORE_CPUFEAT_FMA3)
INLINE void madd(const V2ds& m, const V2ds& a) { store(_mm_fmadd_pd(load(), m.load(), a.load())); }
INLINE void msub(const V2ds& m, const V2ds& s) { store(_mm_fmsub_pd(load(), m.load(), s.load())); }
#else
INLINE void madd(const V2ds& m, const V2ds& a) { store(_mm_add_pd(_mm_mul_pd(load(), m.load()), a.load())); }
INLINE void msub(const V2ds& m, const V2ds& s) { store(_mm_sub_pd(_mm_mul_pd(load(), m.load()), s.load())); }
#endif
//------------------------------------------------------------------------------------------------------------------------//
};
/// <summary>
/// 32-Bit Integer 2D Vector (SSE/SIMD)
/// </summary>
class CPPCORE_ALIGN8_INTRIN V2is : public V2it<V2is>
{
private:
static const int64_t HIGHONES = 0x0000000100000001;
public:
INLINE __m128i load() const { return _mm_loadl_epi64((__m128i*)vals); }
INLINE void store(const __m128i& v) { _mm_storel_epi64((__m128i*)vals, v); }
//------------------------------------------------------------------------------------------------------------------------//
INLINE V2is() { }
INLINE V2is(const __m128i& v) { store(v); }
INLINE V2is(const int32_t x, const int32_t y) { store(_mm_set_epi32(0, 0, y, x)); }
INLINE V2is(const int32_t s) { store(_mm_set_epi32(0, 0, s, s)); }
INLINE V2is(const int32_t values[2]) { store(_mm_loadl_epi64((__m128i*)values)); }
INLINE V2is(const float x, const float y) { store(_mm_cvttps_epi32(_mm_set_ps(0.0f, 0.0f, y, x))); }
INLINE V2is(const float s) { store(_mm_cvttps_epi32(_mm_set_ps(0.0f, 0.0f, s, s))); }
INLINE V2is(const float v[2]) { store(_mm_cvttps_epi32(_mm_castsi128_ps(_mm_loadl_epi64((__m128i*)v)))); }
INLINE V2is(const double x, const double y) { store(_mm_cvttpd_epi32(_mm_set_pd(y, x))); }
INLINE V2is(const double s) { store(_mm_cvttpd_epi32(_mm_set1_pd(s))); }
INLINE V2is(const double v[2]) { store(_mm_cvttpd_epi32(_mm_loadu_pd(v))); }
#if defined(CPPCORE_CPUFEAT_AVX512F) && defined(CPPCORE_CPUFEAT_AVX512VL)
INLINE V2is(const int64_t x, const int64_t y) { store(_mm_cvtepi64_epi32(_mm_set_epi64x(y, x))); }
INLINE V2is(const int64_t s) { store(_mm_cvtepi64_epi32(_mm_set1_epi64x(s))); }
INLINE V2is(const int64_t v[2]) { store(_mm_cvtepi64_epi32(_mm_loadu_si128((__m128i*)v))); }
#else
INLINE V2is(const int64_t x, const int64_t y) { store(_mm_set_epi32(0, 0, (int32_t)y, (int32_t)x)); }
INLINE V2is(const int64_t s) { store(_mm_set_epi32(0, 0, (int32_t)s, (int32_t)s)); }
INLINE V2is(const int64_t v[2]) { store(_mm_set_epi32(0, 0, (int32_t)v[1], (int32_t)v[0])); }
#endif
//------------------------------------------------------------------------------------------------------------------------//
template<typename V> INLINE V2is(const V2ft<V>& v) { store(_mm_cvttps_epi32(_mm_castsi128_ps(_mm_loadl_epi64((__m128i*)v.vals))));}
template<typename V> INLINE V2is(const V2dt<V>& v) { store(_mm_cvttpd_epi32(_mm_loadu_pd(v.vals))); }
#if defined(CPPCORE_CPUFEAT_AVX512F) && defined(CPPCORE_CPUFEAT_AVX512VL)
template<typename V> INLINE V2is(const V2lt<V>& v) { store(_mm_cvtepi64_epi32(_mm_loadu_si128((__m128i*)v.vals))); }
#else
template<typename V> INLINE V2is(const V2lt<V>& v) { store(_mm_set_epi32(0, 0, (int32_t)v.y, (int32_t)v.x)); }
#endif
//------------------------------------------------------------------------------------------------------------------------//
INLINE void* operator new (size_t size) { return CPPCORE_ALIGNED_ALLOC(size, 8U); }
INLINE void* operator new (size_t size, V2is* v){ return v; }
INLINE void* operator new[](size_t size) { return CPPCORE_ALIGNED_ALLOC(size, 8U); }
INLINE void operator delete (void* ptr) { return CPPCORE_ALIGNED_FREE(ptr); }
INLINE void operator delete (void* ptr,V2is* v){ }
INLINE void operator delete[] (void* ptr) { return CPPCORE_ALIGNED_FREE(ptr); }
#if defined(CPPCORE_CPUFEAT_AVX512F) && defined(CPPCORE_CPUFEAT_AVX512VL)
INLINE bool operator == (const V2is& v) const { return _mm_cmpeq_epi32_mask(load(), v.load()) == 0x0F; }
INLINE bool operator != (const V2is& v) const { return _mm_cmpeq_epi32_mask(load(), v.load()) != 0x0F; }
INLINE bool operator < (const V2is& v) const { return _mm_cmplt_epi32_mask(load(), v.load()) == 0x03; }
INLINE bool operator <= (const V2is& v) const { return _mm_cmple_epi32_mask(load(), v.load()) == 0x0F; }
INLINE bool operator > (const V2is& v) const { return _mm_cmpgt_epi32_mask(load(), v.load()) == 0x03; }
INLINE bool operator >= (const V2is& v) const { return _mm_cmpge_epi32_mask(load(), v.load()) == 0x0F; }
#else
INLINE bool operator == (const V2is& v) const { return _mm_movemask_epi8(_mm_cmpeq_epi32(load(),v.load()))==0xFFFF;}
INLINE bool operator != (const V2is& v) const { return _mm_movemask_epi8(_mm_cmpeq_epi32(load(),v.load()))!=0xFFFF;}
INLINE bool operator < (const V2is& v) const { return _mm_movemask_epi8(_mm_cmplt_epi32(load(),v.load()))==0xFF; }
INLINE bool operator > (const V2is& v) const { return _mm_movemask_epi8(_mm_cmpgt_epi32(load(),v.load()))==0xFF; }
#endif
INLINE V2is operator + (const V2is& v) const { return V2is(_mm_add_epi32(load(), v.load())); }
INLINE V2is operator + (const int32_t s) const { return V2is(_mm_add_epi32(load(), _mm_set1_epi32(s))); }
INLINE V2is operator - (const V2is& v) const { return V2is(_mm_sub_epi32(load(), v.load())); }
INLINE V2is operator - (const int32_t s) const { return V2is(_mm_sub_epi32(load(), _mm_set1_epi32(s))); }
INLINE V2is operator - () const { return V2is(_mm_sub_epi32(_mm_setzero_si128(), load())); }
INLINE const V2is& operator + () const { return *this; }
INLINE V2is& operator = (const V2is& v) { store(v.load()); return *this; }