-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgeom.h
3148 lines (2801 loc) · 93.3 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 two dimensional Cartesian vector object
*
* This object represents a two-dimensional location or vector in Cartesian space,
* consisting of the `x` and `y` axes. Many operators and utility functions
* corresponding to operations on the 2D plane are defined.
*/
struct vec2
{
float x, y;
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)
{
switch(i)
{
case 1:
{
return y;
}
default:
{
return x;
}
}
}
float operator[](int i) const
{
switch(i)
{
case 1:
{
return y;
}
default:
{
return x;
}
}
}
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; }
const float *data() const { return &x; }
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; }
/**
* @brief Linearly interpolates between another vec2 according to scale t
*
* If 0 < t < 1 then the matrix will be t% of the way between `this` and `to`.
* Values outside 0..1 imply a linear extrapolation.
*
* @param b the other vec2 to interpolate between
* @param t the interpolation factor
*/
vec2 &lerp(const vec2 &b, float t)
{
x += (b.x-x)*t;
y += (b.y-y)*t;
return *this;
}
/**
* @brief Linearly interpolates between two other vec2s according to scale t
*
* If 0 < t < 1 then the matrix will be t% of the way between `this` and `to`.
* Values outside 0..1 imply a linear extrapolation.
*
* @param a the first vec2 to interpolate from
* @param b the other vec2 to interpolate between
* @param t the interpolation factor
*/
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 Cartesian vector object
*
* This object represents a three-dimensional positiom or vector in Cartesian space,
* represented by the axes `x`,`y`,`z` (`r`,`g`,`b`). Many operators and utility
* functions corresponding to operations on the 2D plane are defined.
*
* The axis aliases `r``g``b` are provided for manipulation as a color,
* in which case the values for each dimension should be no greater than `1.f` for
* easy conversion to `bvec` byte representation.
*
* This object finds uses in nearly every part of the engine,
* including world geometry, mapmodels, particles, projectiles, colors, etc.
*/
struct vec
{
float x, y, z;
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)
{
(*this)[i] = f; return *this;
}
float &r() {return x;}
float &g() {return y;}
float &b() {return z;}
float r() const {return x;}
float g() const {return y;}
float b() const {return z;}
const float *data() const {return &x;}
//operator overloads
float &operator[](int i)
{
switch(i)
{
case 1:
{
return y;
}
case 2:
{
return z;
}
default:
{
return x;
}
}
}
float operator[](int i) const
{
switch(i)
{
case 1:
{
return y;
}
case 2:
{
return z;
}
default:
{
return x;
}
}
}
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
/**
* @brief Returns whether this `vec` is exactly zero in all axes.
*
* The values are compared to zero under the IEEE 754 equality condition, meaning
* that negative zero is treated as equal to positive zero.
*
* @return true if the vec is equal to (0,0,0)
* @return false if the vec has any axis value that is not +/-0
*/
bool iszero() const
{
return x==0 && y==0 && z==0;
}
/**
* @brief Returns the square of this `vec`'s magnitude.
*
* This is equivalent to the dot product of this `vec` with itself.
*
* @return the square of the magnitude of the `vec`
*/
float squaredlen() const { return x*x + y*y + z*z; }
/**
* @brief Sets this `vec` to its elementwise square.
*
* Each axis is independently multiplied by itself and assigned to `this`.
*
* @return a reference to this vec
*/
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
/**
* @brief Checks whether the x,y dimensions are outside a distance from a vec
*
* If this vector's `x` or `y` coordinates are within `r` of the passed vec `o`,
* the function returns false; otherwise return true. If the value is exactly
* `r` away from the vec `o` in either direction, the function returns false
*
* @param o the vec to check nearness
* @param r the distance at which to check
*
* @return true if `this` vec is close to `o`
* @return false if `this` vec is far away from `o`
*/
bool reject(const vec &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); }
/**
* @brief z component only of scalar triple product (A*(BxC))
*/
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
/**
* @brief Linearly interpolates between another vec2 according to scale t
*
* If 0 < t < 1 then the matrix will be t% of the way between `this` and `to`.
* Values outside 0..1 imply a linear extrapolation.
*
* @param b the other vec2 to interpolate between
* @param t the interpolation factor
*/
vec &lerp(const vec &b, float t)
{
x += (b.x-x)*t;
y += (b.y-y)*t;
z += (b.z-z)*t;
return *this;
}
/**
* @brief Linearly interpolates between two other vecs according to scale t
*
* If 0 < t < 1 then the matrix will be t% of the way between `this` and `to`.
* Values outside 0..1 imply a linear extrapolation.
*
* @param a the first vec to interpolate from
* @param b the other vec to interpolate between
* @param t the interpolation factor
*/
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;
}
/**
* @brief Sets this `vec` to the arithmetic mean of itself and another `vec`.
*
* Each axis has its arithmetic mean (average) set independently.
*
* @param b the other `vec` to average with
*/
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;
}
//all rotate_around functions use RH coordinates (positive rotation in CCW direction)
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;
}
/**
* @brief Determines whether this vec is within the boundaries {o, o+size}.
*
* The conditions cannot be met if `size` is negative (will always return
* false). If `this` is on the boundary of the bounding box, returns true.
*
* @param o the position of the bounding box start
* @param size the projection in +x,+y,+z for the bounding box to occupy
*
* @return true if `this` is inside the bounding box
* @return false if size is negative, or `this` is not inside it
*/
bool insidebb(const ivec &o, int size) const;
/**
* @brief Determines whether this vec is within the boundaries {o-margin, o+size+margin}.
*
* The conditions cannot be met if `size` is negative (will always return
* false). If `this` is on the boundary of the bounding box (including margin),
* returns true.
*
* @param o the position of the bounding box start
* @param size the projection in +x,+y,+z for the bounding box to occupy
* @param margin distance in every direction around bounding box
*
* @return true if `this` is inside the bounding box
* @return false if size is negative, or `this` is not inside it
*/
bool insidebb(const ivec &o, int size, int margin) const;
float dist_to_bb(const ivec &min, const ivec &max) const;
/**
* @brief Returns the dot product of this and min/max, depending on sign.
*
* Returns the dot product between `this` and an ivec generated from `min`
* and `max`. The generated vector to dot product is determined by `this`'s
* sign for each dimension; if the dimension is negative then `max` is used,
* and if it is zero or positive then `min` is used.
*
* @param min the `ivec` to use for positive/zero values
* @param max the `ivec` to use for negative values
*
* @return the dot product between `this` and `min`/`max`
*/
float project_bb(const ivec &min, const ivec &max) const;
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 three dimensional Cartesian byte vector
*
* A three dimensional position in the Cartesian space consisting of values between
* 0 and 255 in the `x`,`y`,`z` (`r`,`g`,`b`) axes. Generally the vector space represented by this object is the HTML
* color space (0x000000 to 0xFFFFFF). This object contains several utility methods
* and operations corresponding to useful color transformations in this space.
*
* For general purpose integral-valued positions in Cartesian space, `ivec` is
* generally a more useful choice.
*/
struct bvec
{
uchar x, y, z;
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 &r() {return x;}
uchar &g() {return y;}
uchar &b() {return z;}
uchar r() const {return x;}
uchar g() const {return y;}
uchar b() const {return z;}
uchar &operator[](int i)
{
switch(i)
{
case 1:
{
return y;
}
case 2:
{
return z;
}
default:
{
return x;
}
}
}
uchar operator[](int i) const
{
switch(i)
{
case 1:
{
return y;
}
case 2:
{
return z;
}
default:
{
return x;
}
}
}
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>(x)<<16)|(static_cast<int>(y)<<8)|static_cast<int>(z);
}
};
/**
* @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
{
T x, y, z, w; /** geometric space representation */
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 = 0, T z = 0, T w = 0) : 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>
vec4(const vec4<U> &p) : x(p.x), y(p.y), z(p.z), w(p.w) {}
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));
}
/**
* @brief Returns the i-th dimension of this vec4.
*
* 0 = x
* 1 = y
* 2 = z
* 3 = w
*
* All values other than these will return a reference to x.
*
* @param i the index to access
*
* @return reference to the value along the specified dimension
*/
T &operator[](int i)
{
switch(i)
{
case 1:
{
return y;
}
case 2:
{
return z;
}
case 3:
{
return w;
}
default:
{
return x;
}
}
}
/**
* @brief Returns the i-th dimension of this vec4.
*
* 0 = x
* 1 = y
* 2 = z
* 3 = w
*
* All values other than these will return the value of x.
*
* @param i the index to access
*
* @return the value along the specified dimension
*/
T operator[](int i) const
{
switch(i)
{
case 1:
{
return y;
}
case 2:
{
return z;
}
case 3:
{
return w;
}
default:
{
return x;
}
}
}
/**
* @brief References the x coordinate of this vec4
*
* @return a reference to x
*/
T &r() { return x; }
/**
* @brief References the y coordinate of this vec4
*
* @return a reference to y
*/
T &g() { return y; }
/**
* @brief References the z coordinate of this vec4
*
* @return a reference to z
*/
T &b() { return z; }
/**
* @brief References the w coordinate of this vec4
*
* @return a reference to w
*/
T &a() { return w; }
/**
* @brief Returns the x coordinate of this vec4
*
* @return the value of x
*/
T r() const { return x; }
/**
* @brief Returns the y coordinate of this vec4
*
* @return the value of y
*/
T g() const { return y; }
/**
* @brief Returns the z coordinate of this vec4
*