forked from project-imprimis/libprimis-headers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeom.h
2215 lines (1927 loc) · 70.7 KB
/
geom.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
/**
* @file geom.h
* @brief Physical geometry objects
*
* This file describes objects which represent concrete geometric constructions,
* such as vectors that represent 2D, 3D, and 4D Cartesian coordinates, as well
* as more complex geometric structures such as quaternions and matrices.
*/
#ifndef GEOM_H_
#define GEOM_H_
struct vec;
template<typename T>
struct vec4;
struct plane;
struct quat;
struct dualquat;
class matrix3;
struct matrix4x3;
struct matrix4;
/**
* @brief 2 dimensional vector object
*
* with relevant operators
*/
struct vec2
{
union
{
struct
{
float x, y;
};
float v[2];
};
vec2() {}
vec2(float x, float y) : x(x), y(y) {}
explicit vec2(const vec &v);
explicit vec2(const vec4<float> &v);
float &operator[](int i) { return v[i]; }
float operator[](int i) const { return v[i]; }
bool operator==(const vec2 &o) const { return x == o.x && y == o.y; }
bool operator!=(const vec2 &o) const { return x != o.x || y != o.y; }
bool iszero() const { return x==0 && y==0; }
float dot(const vec2 &o) const { return x*o.x + y*o.y; }
float squaredlen() const { return dot(*this); }
float magnitude() const { return sqrtf(squaredlen()); }
vec2 &normalize() { mul(1/magnitude()); return *this; }
vec2 &safenormalize() { float m = magnitude(); if(m) mul(1/m); return *this; }
float cross(const vec2 &o) const { return x*o.y - y*o.x; }
float squaredist(const vec2 &e) const { return vec2(*this).sub(e).squaredlen(); }
float dist(const vec2 &e) const { return sqrtf(squaredist(e)); }
vec2 &mul(float f) { x *= f; y *= f; return *this; }
vec2 &mul(const vec2 &o) { x *= o.x; y *= o.y; return *this; }
vec2 &square() { mul(*this); return *this; }
vec2 &div(float f) { x /= f; y /= f; return *this; }
vec2 &div(const vec2 &o) { x /= o.x; y /= o.y; return *this; }
vec2 &recip() { x = 1/x; y = 1/y; return *this; }
vec2 &add(float f) { x += f; y += f; return *this; }
vec2 &add(const vec2 &o) { x += o.x; y += o.y; return *this; }
vec2 &sub(float f) { x -= f; y -= f; return *this; }
vec2 &sub(const vec2 &o) { x -= o.x; y -= o.y; return *this; }
vec2 &neg() { x = -x; y = -y; return *this; }
vec2 &min(const vec2 &o) { x = ::min(x, o.x); y = ::min(y, o.y); return *this; }
vec2 &max(const vec2 &o) { x = ::max(x, o.x); y = ::max(y, o.y); return *this; }
vec2 &min(float f) { x = ::min(x, f); y = ::min(y, f); return *this; }
vec2 &max(float f) { x = ::max(x, f); y = ::max(y, f); return *this; }
vec2 &abs() { x = fabs(x); y = fabs(y); return *this; }
vec2 &clamp(float l, float h) { x = ::std::clamp(x, l, h); y = ::std::clamp(y, l, h); return *this; }
vec2 &reflect(const vec2 &n) { float k = 2*dot(n); x -= k*n.x; y -= k*n.y; return *this; }
vec2 &lerp(const vec2 &b, float t) { x += (b.x-x)*t; y += (b.y-y)*t; return *this; }
vec2 &lerp(const vec2 &a, const vec2 &b, float t) { x = a.x + (b.x-a.x)*t; y = a.y + (b.y-a.y)*t; return *this; }
vec2 &avg(const vec2 &b) { add(b); mul(0.5f); return *this; }
vec2 operator+(const vec2 &v2) const
{
return vec2(x+v2.x, y+v2.y);
}
vec2 operator-(const vec2 &v2) const
{
return vec2(x-v2.x, y-v2.y);
}
vec2 operator-() const
{
return vec2(-x, -y);
}
template<typename T>
vec2 operator*(const T &n)
{
return vec2(n*x, n*y);
}
vec2 operator*(const vec2 &v2)
{
return vec2(x*v2.x, y*v2.y);
}
template<typename T>
vec2 operator/(const T &n)
{
return vec2(x/n, y/n);
}
vec2 operator/(const vec2 &v2)
{
return vec2(x/v2.x, y/v2.y);
}
template<class B>
vec2 &madd(const vec2 &a, const B &b) { return add(vec2(a).mul(b)); }
template<class B>
vec2 &msub(const vec2 &a, const B &b) { return sub(vec2(a).mul(b)); }
vec2 &rotate_around_z(float c, float s) { float rx = x, ry = y; x = c*rx-s*ry; y = c*ry+s*rx; return *this; }
vec2 &rotate_around_z(float angle) { return rotate_around_z(cosf(angle), std::sin(angle)); }
vec2 &rotate_around_z(const vec2 &sc) { return rotate_around_z(sc.x, sc.y); }
};
struct ivec;
struct svec;
/**
* @brief three dimensional vector object
*
* this object finds uses in nearly every part of the engine,
* including world geometry, mapmodels, particles, projectiles, colors, etc
*/
struct vec
{
union
{
struct
{
float x, y, z;
};
struct
{
float r, g, b;
};
float v[3];
};
vec() {}
explicit vec(int a) : x(a), y(a), z(a) {}
explicit vec(float a) : x(a), y(a), z(a) {}
vec(float a, float b, float c) : x(a), y(b), z(c) {}
explicit vec(int v[3]) : x(v[0]), y(v[1]), z(v[2]) {}
explicit vec(const float *v) : x(v[0]), y(v[1]), z(v[2]) {}
explicit vec(const vec2 &v, float z = 0) : x(v.x), y(v.y), z(z) {}
explicit vec(const vec4<float> &v);
explicit vec(const ivec &v);
explicit vec(const svec &v);
vec(float yaw, float pitch) : x(-std::sin(yaw)*cosf(pitch)), y(cosf(yaw)*cosf(pitch)), z(std::sin(pitch)) {}
vec &set(int i, float f) { v[i] = f; return *this; }
//operator overloads
float &operator[](int i) { return v[i]; }
float operator[](int i) const { return v[i]; }
bool operator==(const vec &o) const { return x == o.x && y == o.y && z == o.z; }
bool operator!=(const vec &o) const { return x != o.x || y != o.y || z != o.z; }
vec operator+(const vec &v2)
{
return vec(x+v2.x, y+v2.y, z+v2.z);
}
vec operator-(const vec &v2)
{
return vec(x-v2.x, y-v2.y, z-v2.z);
}
vec operator-()
{
return vec(-x, -y, -z);
}
template<typename T>
vec operator*(const T &n)
{
return vec(n*x, n*y, n*z);
}
vec operator*(const vec &v2)
{
return vec(x*v2.x, y*v2.y, z*v2.z);
}
template<typename T>
vec operator/(const T &n)
{
return vec(x/n, y/n, z/n);
}
vec operator/(const vec &v2)
{
return vec(x/v2.x, y/v2.y, z/v2.z);
}
//unary operators
bool iszero() const { return x==0 && y==0 && z==0; }
float squaredlen() const { return x*x + y*y + z*z; }
vec &square() { mul(*this); return *this; }
vec &neg2() { x = -x; y = -y; return *this; } //unused
vec &neg() { x = -x; y = -y; z = -z; return *this; } //overloaded by operator-()
vec &abs() { x = fabs(x); y = fabs(y); z = fabs(z); return *this; }
vec &recip() { x = 1/x; y = 1/y; z = 1/z; return *this; } //used twice
float magnitude2() const { return sqrtf(dot2(*this)); }
float magnitude() const { return sqrtf(squaredlen()); }
vec &normalize() { div(magnitude()); return *this; }
vec &safenormalize() { float m = magnitude(); if(m) div(m); return *this; }
bool isnormalized() const { float m = squaredlen(); return (m>0.99f && m<1.01f); }
//elementwise float operators
vec &mul(float f) { x *= f; y *= f; z *= f; return *this; }
vec &mul2(float f) { x *= f; y *= f; return *this; } //unused
vec &div(float f) { x /= f; y /= f; z /= f; return *this; }
vec &div2(float f) { x /= f; y /= f; return *this; } //unused
vec &add(float f) { x += f; y += f; z += f; return *this; }
vec &add2(float f) { x += f; y += f; return *this; } //used once
vec &addz(float f) { z += f; return *this; } //unused
vec &sub(float f) { x -= f; y -= f; z -= f; return *this; }
vec &sub2(float f) { x -= f; y -= f; return *this; } //unused
vec &subz(float f) { z -= f; return *this; } //unused
vec &min(float f) { x = ::min(x, f); y = ::min(y, f); z = ::min(z, f); return *this; }
vec &max(float f) { x = ::max(x, f); y = ::max(y, f); z = ::max(z, f); return *this; }
vec &clamp(float l, float h) { x = ::std::clamp(x, l, h); y = ::std::clamp(y, l, h); z = ::std::clamp(z, l, h); return *this; }
//elementwise vector operators
vec &mul(const vec &o) { x *= o.x; y *= o.y; z *= o.z; return *this; }
vec &div(const vec &o) { x /= o.x; y /= o.y; z /= o.z; return *this; }
vec &add(const vec &o) { x += o.x; y += o.y; z += o.z; return *this; }
vec &sub(const vec &o) { x -= o.x; y -= o.y; z -= o.z; return *this; }
vec &min(const vec &o) { x = ::min(x, o.x); y = ::min(y, o.y); z = ::min(z, o.z); return *this; }
vec &max(const vec &o) { x = ::max(x, o.x); y = ::max(y, o.y); z = ::max(z, o.z); return *this; }
//dot products
float dot2(const vec2 &o) const { return x*o.x + y*o.y; }
float dot2(const vec &o) const { return x*o.x + y*o.y; }
float dot(const vec &o) const { return x*o.x + y*o.y + z*o.z; }
float squaredot(const vec &o) const { float k = dot(o); return k*k; } //unused
float absdot(const vec &o) const { return fabs(x*o.x) + fabs(y*o.y) + fabs(z*o.z); } //used once
float zdot(const vec &o) const { return z*o.z; } //unused
//distances
float squaredist(const vec &e) const { return vec(*this).sub(e).squaredlen(); }
float dist(const vec &e) const { return sqrtf(squaredist(e)); }
float dist(const vec &e, vec &t) const { t = *this; t.sub(e); return t.magnitude(); }
float dist2(const vec &o) const { float dx = x-o.x, dy = y-o.y; return sqrtf(dx*dx + dy*dy); }
//cross products
template<class T>
bool reject(const T &o, float r) const { return x>o.x+r || x<o.x-r || y>o.y+r || y<o.y-r; }
template<class A, class B>
vec &cross(const A &a, const B &b) { x = a.y*b.z-a.z*b.y; y = a.z*b.x-a.x*b.z; z = a.x*b.y-a.y*b.x; return *this; }
vec &cross(const vec &o, const vec &a, const vec &b) { return cross(vec(a).sub(o), vec(b).sub(o)); }
/**
* @brief scalar triple product A*(BxC)
*/
float scalartriple(const vec &a, const vec &b) const { return x*(a.y*b.z-a.z*b.y) + y*(a.z*b.x-a.x*b.z) + z*(a.x*b.y-a.y*b.x); }
float zscalartriple(const vec &a, const vec &b) const { return z*(a.x*b.y-a.y*b.x); } //unused
//transformations
vec &reflectz(float rz) { z = 2*rz - z; return *this; }
vec &reflect(const vec &n) { float k = 2*dot(n); x -= k*n.x; y -= k*n.y; z -= k*n.z; return *this; }
vec &project(const vec &n) { float k = dot(n); x -= k*n.x; y -= k*n.y; z -= k*n.z; return *this; }
vec &projectxydir(const vec &n) { if(n.z) z = -(x*n.x/n.z + y*n.y/n.z); return *this; } //used once
vec &projectxy(const vec &n)
{
float m = squaredlen(), k = dot(n);
projectxydir(n);
rescale(sqrtf(::max(m - k*k, 0.0f)));
return *this;
} //used once
vec &projectxy(const vec &n, float threshold)
{
float m = squaredlen(), k = ::min(dot(n), threshold);
projectxydir(n);
rescale(sqrtf(::max(m - k*k, 0.0f)));
return *this;
} //used once
vec &lerp(const vec &b, float t) { x += (b.x-x)*t; y += (b.y-y)*t; z += (b.z-z)*t; return *this; }
vec &lerp(const vec &a, const vec &b, float t) { x = a.x + (b.x-a.x)*t; y = a.y + (b.y-a.y)*t; z = a.z + (b.z-a.z)*t; return *this; }
vec &avg(const vec &b) { add(b); mul(0.5f); return *this; }
template<class B>
vec &madd(const vec &a, const B &b) { return add(vec(a).mul(b)); }
template<class B>
vec &msub(const vec &a, const B &b) { return sub(vec(a).mul(b)); }
vec &rescale(float k)
{
float mag = magnitude();
if(mag > 1e-6f) mul(k / mag);
return *this;
}
vec &rotate_around_z(float c, float s) { float rx = x, ry = y; x = c*rx-s*ry; y = c*ry+s*rx; return *this; }
vec &rotate_around_x(float c, float s) { float ry = y, rz = z; y = c*ry-s*rz; z = c*rz+s*ry; return *this; }
vec &rotate_around_y(float c, float s) { float rx = x, rz = z; x = c*rx+s*rz; z = c*rz-s*rx; return *this; }
vec &rotate_around_z(float angle) { return rotate_around_z(cosf(angle), std::sin(angle)); }
vec &rotate_around_x(float angle) { return rotate_around_x(cosf(angle), std::sin(angle)); }
vec &rotate_around_y(float angle) { return rotate_around_y(cosf(angle), std::sin(angle)); }
vec &rotate_around_z(const vec2 &sc) { return rotate_around_z(sc.x, sc.y); }
vec &rotate_around_x(const vec2 &sc) { return rotate_around_x(sc.x, sc.y); }
vec &rotate_around_y(const vec2 &sc) { return rotate_around_y(sc.x, sc.y); }
vec &rotate(float c, float s, const vec &d)
{
*this = vec(x*(d.x*d.x*(1-c)+c) + y*(d.x*d.y*(1-c)-d.z*s) + z*(d.x*d.z*(1-c)+d.y*s),
x*(d.y*d.x*(1-c)+d.z*s) + y*(d.y*d.y*(1-c)+c) + z*(d.y*d.z*(1-c)-d.x*s),
x*(d.x*d.z*(1-c)-d.y*s) + y*(d.y*d.z*(1-c)+d.x*s) + z*(d.z*d.z*(1-c)+c));
return *this;
}
vec &rotate(float angle, const vec &d) { return rotate(cosf(angle), std::sin(angle), d); }
vec &rotate(const vec2 &sc, const vec &d) { return rotate(sc.x, sc.y, d); }
void orthogonal(const vec &d)
{
*this = fabs(d.x) > fabs(d.z) ? vec(-d.y, d.x, 0) : vec(0, -d.z, d.y);
}
void orthonormalize(vec &s, vec &t) const
{
s.project(*this);
t.project(*this).project(s);
} //unused
template<class T>
bool insidebb(const T &bbmin, const T &bbmax) const
{
return x >= bbmin.x && x <= bbmax.x && y >= bbmin.y && y <= bbmax.y && z >= bbmin.z && z <= bbmax.z;
}
template<class T, class U>
bool insidebb(const T &bbmin, const T &bbmax, U margin) const
{
return x >= bbmin.x-margin && x <= bbmax.x+margin && y >= bbmin.y-margin && y <= bbmax.y+margin && z >= bbmin.z-margin && z <= bbmax.z+margin;
}
template<class T, class U>
bool insidebb(const T &o, U size) const
{
return x >= o.x && x <= o.x + size && y >= o.y && y <= o.y + size && z >= o.z && z <= o.z + size;
}
template<class T, class U>
bool insidebb(const T &o, U size, U margin) const
{
size += margin;
return x >= o.x-margin && x <= o.x + size && y >= o.y-margin && y <= o.y + size && z >= o.z-margin && z <= o.z + size;
}
template<class T>
float dist_to_bb(const T &min, const T &max) const
{
float sqrdist = 0;
for(int i = 0; i < 3; ++i)
{
if (v[i] < min[i])
{
float delta = v[i]-min[i];
sqrdist += delta*delta;
}
else if(v[i] > max[i])
{
float delta = max[i]-v[i];
sqrdist += delta*delta;
}
}
return sqrtf(sqrdist);
}
template<class T, class S>
float dist_to_bb(const T &o, S size) const
{
return dist_to_bb(o, T(o).add(size));
}
template<class T>
float project_bb(const T &min, const T &max) const
{
return x*(x < 0 ? max.x : min.x) + y*(y < 0 ? max.y : min.y) + z*(z < 0 ? max.z : min.z);
}
static vec hexcolor(int color)
{
return vec(((color>>16)&0xFF)*(1.0f/255.0f), ((color>>8)&0xFF)*(1.0f/255.0f), (color&0xFF)*(1.0f/255.0f));
}
int tohexcolor() const
{
return (static_cast<int>(::std::clamp(r, 0.0f, 1.0f)*255)<<16) |
(static_cast<int>(::std::clamp(g, 0.0f, 1.0f)*255)<<8) |
static_cast<int>(::std::clamp(b, 0.0f, 1.0f)*255);
}
};
inline vec2::vec2(const vec &v) : x(v.x), y(v.y) {}
template<>
struct std::hash<vec>
{
size_t operator()(const vec& k) const
{
union { uint i; float f; } x, y, z;
x.f = k.x;
y.f = k.y;
z.f = k.z;
uint v = x.i^y.i^z.i;
return v + (v>>12);
}
};
/**
* @brief color vector3 (r,g,b)
*/
struct bvec
{
union
{
struct
{
uchar x, y, z;
};
struct
{
uchar r, g, b;
};
uchar v[3];
};
bvec() {}
bvec(uchar x, uchar y, uchar z) : x(x), y(y), z(z) {}
explicit bvec(const vec &v) : x(static_cast<uchar>((v.x+1)*(255.0f/2.0f))), y(static_cast<uchar>((v.y+1)*(255.0f/2.0f))), z(static_cast<uchar>((v.z+1)*(255.0f/2.0f))) {}
explicit bvec(const vec4<uchar> &v);
uchar &operator[](int i) { return v[i]; }
uchar operator[](int i) const { return v[i]; }
bool operator==(const bvec &v) const { return x==v.x && y==v.y && z==v.z; }
bool operator!=(const bvec &v) const { return x!=v.x || y!=v.y || z!=v.z; }
bool iszero() const { return x==0 && y==0 && z==0; }
vec tonormal() const { return vec(x*(2.0f/255.0f)-1.0f, y*(2.0f/255.0f)-1.0f, z*(2.0f/255.0f)-1.0f); }
bvec &normalize()
{
vec n(x-127.5f, y-127.5f, z-127.5f);
float mag = 127.5f/n.magnitude();
x = static_cast<uchar>(n.x*mag+127.5f);
y = static_cast<uchar>(n.y*mag+127.5f);
z = static_cast<uchar>(n.z*mag+127.5f);
return *this;
}
void lerp(const bvec &a, const bvec &b, float t)
{
x = static_cast<uchar>(a.x + (b.x-a.x)*t);
y = static_cast<uchar>(a.y + (b.y-a.y)*t);
z = static_cast<uchar>(a.z + (b.z-a.z)*t);
}
void lerp(const bvec &a, const bvec &b, int ka, int kb, int d)
{
x = static_cast<uchar>((a.x*ka + b.x*kb)/d);
y = static_cast<uchar>((a.y*ka + b.y*kb)/d);
z = static_cast<uchar>((a.z*ka + b.z*kb)/d);
}
void flip()
{
x ^= 0x80;
y ^= 0x80;
z ^= 0x80;
}
void scale(int k, int d)
{
x = static_cast<uchar>((x*k)/d);
y = static_cast<uchar>((y*k)/d);
z = static_cast<uchar>((z*k)/d);
}
bvec &shl(int n)
{
x <<= n;
y <<= n;
z <<= n;
return *this;
}
bvec &shr(int n)
{
x >>= n;
y >>= n;
z >>= n;
return *this;
}
static bvec fromcolor(const vec &v)
{
return bvec(static_cast<uchar>(v.x*255.0f), static_cast<uchar>(v.y*255.0f), static_cast<uchar>(v.z*255.0f));
}
vec tocolor() const
{
return vec(x*(1.0f/255.0f), y*(1.0f/255.0f), z*(1.0f/255.0f));
}
static bvec from565(ushort c)
{
return bvec((((c>>11)&0x1F)*527 + 15) >> 6, (((c>>5)&0x3F)*259 + 35) >> 6, ((c&0x1F)*527 + 15) >> 6);
}
static bvec hexcolor(int color)
{
return bvec((color>>16)&0xFF, (color>>8)&0xFF, color&0xFF);
}
int tohexcolor() const
{
return (static_cast<int>(r)<<16)|(static_cast<int>(g)<<8)|static_cast<int>(b);
}
};
/**
* @brief A four dimensional Cartesian-space vector template.
*
* This object defines a location in four dimensional Cartesian space, in whatever
* arithmetic type it is specialized to be. All four values are of the type T
* specialized, and all operators (unless explicitly specified, require types
* trivially convertable to T (or preferably of type T).
*/
template<typename T>
struct vec4
{
union
{
struct { T x, y, z, w; }; /** geometric space representation */
struct { T r, g, b, a; }; /** color space representation (red, green, blue, alpha)*/
T v[4]; /** four-entry array representation*/
uint mask; /** used for uchar (color) specific operations*/
};
vec4() {}
explicit vec4(const vec &p, T w = 0) : x(p.x), y(p.y), z(p.z), w(w) {}
explicit vec4(const vec2 &p, T z = 0, T w = 0) : x(p.x), y(p.y), z(z), w(w) {}
vec4(T x, T y, T z, T w) : x(x), y(y), z(z), w(w) {}
vec4(bvec v, uchar c)
{
x = v.x;
y = v.y;
z = v.z;
w = c;
}
vec4(bvec v)
{
x = v.x;
y = v.y;
z = v.z;
w = 0;
}
explicit vec4(const T *v) : x(v[0]), y(v[1]), z(v[2]), w(v[3]) {}
template<class U>
operator vec4<U>()
{
return vec4<U>(static_cast<U>(this->x),
static_cast<U>(this->y),
static_cast<U>(this->z),
static_cast<U>(this->w));
}
T &operator[](int i) { return v[i]; }
T operator[](int i) const { return v[i]; }
bool operator==(const vec4 &o) const { return x == o.x && y == o.y && z == o.z && w == o.w; }
bool operator!=(const vec4 &o) const { return x != o.x || y != o.y || z != o.z || w != o.w; }
/**
* @brief Returns the 3 dimensional dot product between two 4-vecs.
*
* Returns the scalar (dot) product of two 4D vecs, `this` and the passed
* one, as if they are 3D vecs, by ignoring the fourth term in each vector.
*
* @param o the vec4 to multiply by
*
* @return the dot product of the first three elements of each vector
*/
T dot3(const vec4 &o) const { return x*o.x + y*o.y + z*o.z; }
/**
* @brief Returns the 3 dimensional dot product between `this` and a 3D vec.
*
* Returns the scalar (dot) product of a 3D and 4D vec, `this` and the passed
* 3D vec, by ignoring the fourth term in the 4D vec.
*
* @param o the vec3 to multiply by
*
* @return the dot product of the first three elements of each vector
*/
T dot3(const vec &o) const { return x*o.x + y*o.y + z*o.z; }
/**
* @brief Returns the scalar product with another vec4.
*
* Returns the scalar (dot) product of two 4D vecs, `this` and the passed
* vec4 `o`.
*
* @param o the vector to multiply with
*
* @return the dot product of the two vectors
*/
T dot(const vec4 &o) const { return dot3(o) + w*o.w; }
/**
* @brief Returns the dot product of `this` and a vec3, assuming o.w = 1.
*
* Calculates the dot product with a vec3, with `o.w` implied to equal 1.
*
* @param o the vec3 to multiply by
*
* @return the dot product of the two vectors
*/
T dot(const vec &o) const { return x*o.x + y*o.y + z*o.z + w; }
/**
* @brief Returns the square of the magnitude of the vector.
*
* Calculates the dot product of the vector with itself, yielding the
* square of the magnitude of the vec4.
*
* @return the magnitude of `this` squared
*/
T squaredlen() const { return dot(*this); }
/**
* @brief Returns the magnitude of the vector.
*
* Calculates the magnitude (length) of the vector, by taking the square
* root of the dot product of the vector with itself.
*
* @return the maginitude of `this`
*/
T magnitude() const { return sqrtf(squaredlen()); }
/**
* @brief Returns the magnitude of the vector, ignoring the w dimension.
*
* Calculates the #D magnitude (length) of the vector, by taking the square
* root of the 3D dot product of the vector with itself.
*
* @return the maginitude of `this`
*/
T magnitude3() const { return sqrtf(dot3(*this)); }
/**
* @brief Scales the vector to have a magnitude of 1
*
* Will cause a divide-by-zero if the vector is (0,0,0,0).
*
* @return a reference to `this` vector
*/
vec4 &normalize() { mul(1/magnitude()); return *this; }
/**
* @brief Scales the vector to have a magnitude of 1
*
* Will return (0,0,0,0) if the vector passed (0,0,0,0).
*
* @return a reference to `this` vector
*/
vec4 &safenormalize() { T m = magnitude(); if(m) mul(1/m); return *this; }
void lerp(const vec4<uchar> &a, const vec4<uchar> &b, float t)
{
x = static_cast<uchar>(a.x + (b.x-a.x)*t);
y = static_cast<uchar>(a.y + (b.y-a.y)*t);
z = static_cast<uchar>(a.z + (b.z-a.z)*t);
w = a.w;
}
void lerp(const vec4<uchar> &a, const vec4<uchar> &b, int ka, int kb, int d)
{
x = static_cast<uchar>((a.x*ka + b.x*kb)/d);
y = static_cast<uchar>((a.y*ka + b.y*kb)/d);
z = static_cast<uchar>((a.z*ka + b.z*kb)/d);
w = a.w;
}
void lerp(const vec4<uchar> &a, const vec4<uchar> &b, const vec4<uchar> &c, float ta, float tb, float tc)
{
x = static_cast<uchar>(a.x*ta + b.x*tb + c.x*tc);
y = static_cast<uchar>(a.y*ta + b.y*tb + c.y*tc);
z = static_cast<uchar>(a.z*ta + b.z*tb + c.z*tc);
w = static_cast<uchar>(a.w*ta + b.w*tb + c.w*tc);
}
/**
* @brief Flips a vec4<uchar> by using the mask type pun.
*
* Not for use with non-char vec4<> objects.
*/
void flip() { mask ^= 0x80808080; }
vec4 &lerp(const vec4 &b, T t)
{
x += (b.x-x)*t;
y += (b.y-y)*t;
z += (b.z-z)*t;
w += (b.w-w)*t;
return *this;
}
vec4 &lerp(const vec4 &a, const vec4 &b, T t)
{
x = a.x+(b.x-a.x)*t;
y = a.y+(b.y-a.y)*t;
z = a.z+(b.z-a.z)*t;
w = a.w+(b.w-a.w)*t;
return *this;
}
/**
* @brief Calculates the elementwise arithmetic mean.
*
* Sets `this` to the elementwise arithmetic mean of `this` object and the
* passed object. The original vector is not preserved, but the passed one
* is.
*
* @param b the vec4 to average with
*
* @return a reference to `this` object following the operation
*/
vec4 &avg(const vec4 &b) { add(b); mul(0.5f); return *this; }
template<class B>
vec4 &madd(const vec4 &a, const B &b) { return add(vec4(a).mul(b)); }
template<class B>
vec4 &msub(const vec4 &a, const B &b) { return sub(vec4(a).mul(b)); }
/**
* @brief Calculates the elementwise product.
*
* Calculates the elementwise product of `f` with the first three entries
* in `this`. This means that the original vector is not preserved.
*
* @param f the value to multiply by
*
* @return a reference to `this` object following the operation
*/
vec4 &mul3(T f) { x *= f; y *= f; z *= f; return *this; }
/**
* @brief Calculates the elementwise product.
*
* Calculates the elementwise product of `f` with all four entries
* in `this`. This means that the original vector is not preserved.
*
* @param f the value to multiply by
*
* @return a reference to `this` object following the operation
*/
vec4 &mul(T f) { mul3(f); w *= f; return *this; }
/**
* @brief Calculates the elementwise product.
*
* Calculates the elementwise product of the four parameters in `this` with
* the four parameters in `o`. This measn that the original vector is not
* preserved.
*
* @param o the vec4 to multiply by
*
* @return a reference to `this` object following the operation
*/
vec4 &mul(const vec4 &o) { x *= o.x; y *= o.y; z *= o.z; w *= o.w; return *this; }
/**
* @brief Calculates the elementwise product.
*
* Calculates the elementwise product of the first three parameters in `this`
* with the three parameters in `o`. This means that the original vector is
* not preserved.
*
* @param o the vec3 to multiply by
*
* @return a reference to `this` object following the operation
*/
vec4 &mul(const vec &o) { x *= o.x; y *= o.y; z *= o.z; return *this; }
/**
* @brief Calculates the elementwise square.
*
* Calculates the elementwise product of `this` with itself. This means that
* the original vector is not preserved.
*
* @return a reference to `this` object following the operation
*/
vec4 &square() { mul(*this); return *this; }
/**
* @brief Calculates the elementwise quotient.
*
* Calculates the quotient of the first three values in `this` with the value
* passed to `f`. This means that the original vector is not preserved.
*
* @param f the value to divide by
*
* @return a reference to `this` object following the operation
*
*/
vec4 &div3(T f) { x /= f; y /= f; z /= f; return *this; }
/**
* @brief Calculates the elementwise quotient.
*
* Calculates the quotient of the four values in `this` with the value passed
* to `f`. This means that the original vector is not preserved.
*
* @param f the value to divide by
*
* @return a reference to `this` object following the operation
*/
vec4 &div(T f) { div3(f); w /= f; return *this; }
/**
* @brief Calculates the elementwise quotient.
*
* Calculates the quotient of the four values in `this` with the four values in
* `o`, element-by-element. This means that the original vector is not preserved.
*
* @param o the vector to divide by
*
* @return a reference to `this` object following the operation
*/
vec4 &div(const vec4 &o) { x /= o.x; y /= o.y; z /= o.z; w /= o.w; return *this; }
/**
* @brief Calculates the elementwise quotient.
*
* Calculates the quotient of the three values in `this` with the four values in
* `o`, element-by-element. This means that the original vector is not preserved.
*
* @param o the vector to divide by
*
* @return a reference to `this` object following the operation
*/
vec4 &div(const vec &o) { x /= o.x; y /= o.y; z /= o.z; return *this; }
/**
* @brief Calculates the elementwise reciprocal.
*
* Calculates the value 1/x for each of the four values in the vector, and assigns
* them to `this`. This means that the original vector is not preserved
*
* @return a reference to `this` object following the operation
*/
vec4 &recip() { x = 1/x; y = 1/y; z = 1/z; w = 1/w; return *this; }
/**
* @brief Calculates the elementwise sum.
*
* Calculates the sum of the four elements in `this` with the four values in
* `o`, element-by-element. This means that the original vector is not preserved.
*
* @param o the vector to add with
*
* @return a reference to `this` object following the operation
*/
vec4 &add(const vec4 &o) { x += o.x; y += o.y; z += o.z; w += o.w; return *this; }
/**
* @brief Calculates the elementwise sum.
*
* Calculates the sum of the first three elements in `this` with the first
* three values in `o`, element-by-element. This means that the original
* vector is not preserved.
*
* @param o the vector to add with
*
* @return a reference to `this` object following the operation
*/
vec4 &add(const vec &o) { x += o.x; y += o.y; z += o.z; return *this; }
/**
* @brief Calculates the elementwise sum.
*
* Calculates the sum of the first three elements in `this` with the value
* passed to `f`. This means that the original vector is not preserved.
*
* @param f the value to add with
*
* @return a reference to `this` object following the operation
*/
vec4 &add3(T f) { x += f; y += f; z += f; return *this; }
/**
* @brief Calculates the elementwise sum.
*
* Calculates the sum of the four elements in `this` with the four elements
* comprising `f`. This means that the original vector is not preserved.
*
* @param f the value to add with
*
* @return a reference to `this` object following the operation
*/
vec4 &add(T f) { add3(f); w += f; return *this; }
/**
* @brief Adds to the fourth value of the vector (w/a).
*
* Calculates the sum of the passed value and the fourth element of the vec4,
* modifying the original vector to equal this sum.
*
* @param f the value to add with
*
* @return a reference to `this` object following the operation
*/
vec4 &addw(T f) { w += f; return *this; }
/**
* @brief Subtracts from `this` the vec4 passed.
*
* Calculates the difference between the four elements in `this` and the
* four elements of `o`, modifying the original vector to equal this difference.
*
* @param o the vec4 to subtract
*
* @return a reference to `this` object following the operation
*/
vec4 &sub(const vec4 &o) { x -= o.x; y -= o.y; z -= o.z; w -= o.w; return *this; }
/**
* @brief Subtracts from `this` the vec passed.
*
* Calculates the difference between the four elements in `this` and the three
* elements of `o`, modifying the original vector to equal this difference.
*
* @param o the vec to subtract
*
* @return a reference to `this` object following the operation
*/
vec4 &sub(const vec &o) { x -= o.x; y -= o.y; z -= o.z; return *this; }
/**
* @brief Subtracts from the first three entries `this` the svalue passed.
*
* Calculates the difference between the first three elements in `this` and
* the value passed, modifying the original vector to equal this difference.
*
* @param f the value to subtract
*
* @return a return to `this` object following the operation
*/
vec4 &sub3(T f) { x -= f; y -= f; z -= f; return *this; }
vec4 &sub(T f) { sub3(f); w -= f; return *this; }
vec4 &subw(T f) { w -= f; return *this; }
vec4 &neg3() { x = -x; y = -y; z = -z; return *this; }
vec4 &neg() { neg3(); w = -w; return *this; }
vec4 &clamp(T l, T h) { x = ::std::clamp(x, l, h); y = ::std::clamp(y, l, h); z = ::std::clamp(z, l, h); w = ::std::clamp(w, l, h); return *this; }
vec4 operator+(const vec4 &v2) const
{
return vec4(x+v2.x, y+v2.y, z+v2.z, w+v2.w);
}
vec4 operator-(const vec4 &v2) const
{
return vec4(x-v2.x, y-v2.y, z-v2.z, w+v2.w);
}
vec4 operator-() const
{
return vec4(-x, -y, -z, -w);
}