-
Notifications
You must be signed in to change notification settings - Fork 1
/
c3dlas.c
2993 lines (2281 loc) · 73.9 KB
/
c3dlas.c
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
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include <limits.h>
#include <float.h>
#include <x86intrin.h>
#include "c3dlas.h"
#ifdef C3DLAS_USE_BUILTINS
#define abs_double __builtin_fabs
#define abs_float __builtin_fabsf
#else
#define abs_double fabs
#define abs_float fabsf
#endif
#ifdef C3DLAS_NO_TGMATH
// requires GCC probably
/*
#define FD_CHOOSE_1(a, b, fn_f, fn_d)\
__builtin_choose_expr( \
__builtin_types_compatible_p(__typeof__(a), double), \
fn_d(a), \
fn_f(a))
#define FD_CHOOSE_2(a, b, fn_f, fn_d)\
__builtin_choose_expr( \
__builtin_types_compatible_p(__typeof__(a), double) || __builtin_types_compatible_p(__typeof__(b), double), \
fn_d(a, b), \
fn_f(a, b))
#define fmax(a,b) FD_CHOOSE_2(a, b, fmaxf, fmax)
#define fmin(a,b) FD_CHOOSE_2(a, b, fminf, fmin)
#define fabs(a) FD_CHOOSE_1(a, fabsf, fabs)
#define sqrt(a) FD_CHOOSE_1(a, sqrtf, sqrt)
*/
#else
#include <tgmath.h>
#endif
#ifndef _GNU_SOURCE
static inline void sincosf(float x, float* s, float* c) {
*s = sinf(x);
*c = cosf(x);
}
#endif
// utilities
// reverses the argument
uint32_t bitReverse32(uint32_t x) {
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return ((x >> 16) | (x << 16));
}
// reverses the least significant (len) bits, zeroing the top
uint32_t reverseBits(uint32_t n, int len) {
uint32_t rn = bitReverse32(n);
return rn >> (32 - len);
}
// random numbers
// returns a random number in (-1, 1) uninclusive
// Thanks to Kaslai (https://github.com/Aslai) for fixing a nasty bug in the previous version
float pcg_f(uint64_t* state, uint64_t stream) {
union {
uint32_t fu;
float ff;
} u;
uint64_t last = *state;
*state = (last * 6364136223846793005ULL) + (stream | 1);
uint32_t xs = ((last >> 18) ^ last) >> 27;
uint32_t rot = last >> 59;
uint32_t fin = (xs >> rot) | (xs << ((-rot) & 31));
uint32_t exp = (fin & 0x3F800000);
exp = (0x7F + 33 - __builtin_clzl(exp)) << 23;
u.fu = ((fin) & 0x807fffff) | exp;
return u.ff;
}
// returns a random number in [0, UINT32_MAX] inclusive
uint32_t pcg_u32(uint64_t* state, uint64_t stream) {
uint64_t last = *state;
*state = (last * 6364136223846793005ULL) + (stream | 1);
uint32_t xs = ((last >> 18) ^ last) >> 27;
uint32_t rot = last >> 59;
uint32_t fin = (xs >> rot) | (xs << ((-rot) & 31));
return fin;
}
// BUG: totally untested
// SIMD and C versions do not return the same values.
void pcg_f8(uint64_t* state, uint64_t stream, float* out) {
#if defined(C3DLAS_USE_SIMD)
__m256i s1, s2, xs1, xs2, xs, r, nra, q, f;
s1 = _mm256_add_epi64(_mm256_set1_epi64x(*state), _mm256_set_epi64x(1,2,3,4));
s2 = _mm256_add_epi64(_mm256_set1_epi64x(*state), _mm256_set_epi64x(5,6,7,8));
// cycle the state
*state = (*state * 6364136223846793005ULL) + (stream | 1);
xs1 = _mm256_srli_epi64(_mm256_xor_si256(_mm256_srli_epi64(s1, 18), s1), 27);
xs2 = _mm256_srli_epi64(_mm256_xor_si256(_mm256_srli_epi64(s2, 18), s2), 27);
xs = _mm256_unpacklo_epi32(xs1, xs2);
r = _mm256_srai_epi32(xs, 59);
nra = _mm256_and_si256(_mm256_sign_epi32(r, _mm256_set1_epi32(-1)), _mm256_set1_epi32(31));
q = _mm256_or_si256(_mm256_srav_epi32(xs, r), _mm256_sllv_epi32(xs, nra));
// q is full of random 32bit integers now
// convert to (-1, 1) floats by jamming in some exponent info
f = _mm256_or_si256(_mm256_and_si256(q, _mm256_set1_epi32(0x807fffff)), _mm256_set1_epi32(0x3f000000));
_mm256_storeu_si256((__m256i*)out, f);
#else
out[0] = pcg_f(state, stream);
out[1] = pcg_f(state, stream);
out[2] = pcg_f(state, stream);
out[3] = pcg_f(state, stream);
out[4] = pcg_f(state, stream);
out[5] = pcg_f(state, stream);
out[6] = pcg_f(state, stream);
out[7] = pcg_f(state, stream);
#endif
}
float frandPCG(float low, float high, PCG* pcg) {
return low + ((high - low) * (pcg_f(&pcg->state, pcg->stream) * 0.5 + 0.5));
}
uint32_t urandPCG(uint32_t low, uint32_t high, PCG* pcg) {
return pcg_u32(&pcg->state, pcg->stream) % (high - low) + low;
}
#define FN(sz, suf, ty, ft, sufft, pref, ...) \
\
int vEqExact##suf(const Vector##suf a, const Vector##suf b) { \
return vEqExact##suf##p(&a, &b); \
} \
int vEqExact##suf##p(const Vector##suf const * a, const Vector##suf const * b) { \
int tmp = 0; \
for(int i = 0; i < sz; i++) \
tmp += ((ty*)a)[i] == ((ty*)b)[i]; \
return tmp == sz; \
} \
\
int vEq##suf(const Vector##suf a, const Vector##suf b) { \
return vEqEp##suf(a, b, pref##_CMP_EPSILON); \
} \
int vEq##suf##p(const Vector##suf* a, const Vector##suf* b) { \
return vEqEp##suf(*a, *b, pref##_CMP_EPSILON); \
} \
\
int vEqEp##suf(const Vector##suf a, const Vector##suf b, ft epsilon) { \
return vEqEp##suf##p(&a, &b, epsilon); \
} \
int vEqEp##suf##p(const Vector##suf* a, const Vector##suf* b, ft epsilon) { \
return vDistSq##suf(*a, *b) <= epsilon * epsilon; \
} \
\
ft vDistSq##suf(const Vector##suf a, const Vector##suf b) { \
return vDistSq##suf##p(&a, &b); \
} \
ft vDistSq##suf##p(const Vector##suf* a, const Vector##suf* b) { \
ft tmp = 0; \
for(int i = 0; i < sz; i++) { \
ft q = ((ty*)a)[i] - ((ty*)b)[i]; \
tmp += q * q; \
} \
return tmp;\
} \
ft vDist##suf(const Vector##suf a, const Vector##suf b) { \
return sqrt(vDistSq##suf##p(&a, &b)); \
} \
ft vDist##suf##p(const Vector##suf* a, const Vector##suf* b) { \
return sqrt(vDistSq##suf##p(a, b)); \
} \
\
Vector##suf vAdd##suf(const Vector##suf a, const Vector##suf b) { \
Vector##suf out; \
vAdd##suf##p(&a, &b, &out); \
return out; \
} \
void vAdd##suf##p(const Vector##suf* a, const Vector##suf* b, Vector##suf* out) { \
for(int i = 0; i < sz; i++) { \
((ty*)out)[i] = ((ty*)a)[i] + ((ty*)b)[i]; \
} \
} \
\
Vector##suf vSub##suf(const Vector##suf a, const Vector##suf b) { \
Vector##suf out; \
vSub##suf##p(&a, &b, &out); \
return out; \
} \
void vSub##suf##p(const Vector##suf const * a, const Vector##suf const * b, Vector##suf* out) { \
for(int i = 0; i < sz; i++) { \
((ty*)out)[i] = ((ty*)a)[i] - ((ty*)b)[i]; \
} \
} \
\
Vector##suf vMul##suf(const Vector##suf a, const Vector##suf b) { \
Vector##suf out; \
vMul##suf##p(&a, &b, &out); \
return out; \
} \
void vMul##suf##p(const Vector##suf const * a, const Vector##suf const * b, Vector##suf* out) { \
for(int i = 0; i < sz; i++) { \
((ty*)out)[i] = ((ty*)a)[i] * ((ty*)b)[i]; \
} \
} \
\
Vector##suf vDiv##suf(const Vector##suf top, const Vector##suf bot) { \
Vector##suf out; \
vDiv##suf##p(&top, &bot, &out); \
return out; \
} \
void vDiv##suf##p(const Vector##suf const * top, const Vector##suf const * bot, Vector##suf* out) { \
for(int i = 0; i < sz; i++) { \
((ty*)out)[i] = ((ty*)top)[i] / ((ty*)bot)[i]; \
} \
} \
\
ft vDot##suf(const Vector##suf a, const Vector##suf b) { \
return vDot##suf##p(&a, &b); \
} \
ft vDot##suf##p(const Vector##suf* a, const Vector##suf* b) { \
ft tmp = 0; \
for(int i = 0; i < sz; i++) { \
tmp += ((ty*)a)[i] * ((ty*)b)[i]; \
} \
return tmp;\
} \
\
Vector##sufft vScale##suf(const Vector##suf v, ft scalar) { \
Vector##sufft out; \
vScale##suf##p(&v, scalar, &out); \
return out; \
} \
void vScale##suf##p(const Vector##suf* v, ft scalar, Vector##sufft* out) { \
for(int i = 0; i < sz; i++) \
((ft*)out)[i] = (ft)((ty*)v)[i] * scalar; \
} \
\
\
Vector##sufft vAvg##suf(const Vector##suf a, const Vector##suf b) { \
Vector##sufft out; \
vAvg##suf##p(&a, &b, &out); \
return out; \
} \
void vAvg##suf##p(const Vector##suf* a, const Vector##suf* b, Vector##sufft* out) { \
for(int i = 0; i < sz; i++) { \
((ty*)out)[i] = (((ty*)a)[i] + ((ty*)b)[i]) / (ft)2.0; \
} \
} \
\
Vector##suf vNeg##suf(const Vector##suf v) { \
Vector##suf out; \
vNeg##suf##p(&v, &out); \
return out; \
} \
void vNeg##suf##p(const Vector##suf* v, Vector##suf* out) { \
for(int i = 0; i < sz; i++) \
((ty*)out)[i] = -((ty*)v)[i]; \
} \
\
Vector##sufft vLerp##suf(const Vector##suf a, const Vector##suf b, ft t) { \
Vector##sufft out; \
vLerp##suf##p(&a, &b, t, &out); \
return out; \
} \
void vLerp##suf##p(const Vector##suf* a, const Vector##suf* b, ft t, Vector##sufft* out) { \
for(int i = 0; i < sz; i++) \
((ft*)out)[i] = (ft)((ty*)a)[i] + ((ft)(((ty*)b)[i] - ((ty*)a)[i]) * t) ; \
} \
\
Vector##sufft vInv##suf(const Vector##suf v) { \
Vector##sufft out; \
vInv##suf##p(&v, &out); \
return out; \
} \
void vInv##suf##p(const Vector##suf* v, Vector##sufft* out) { \
for(int i = 0; i < sz; i++) \
((ft*)out)[i] = (((ty*)v)[i] == 0) ? pref##_MAX : ((ft)1.0 / (ft)((ty*)v)[i]); \
} \
\
ft vLen##suf(const Vector##suf v) { \
return vLen##suf##p(&v); \
} \
ft vLen##suf##p(const Vector##suf* v) { \
ft tmp = 0.0; \
for(int i = 0; i < sz; i++) \
tmp += (ft)((ty*)v)[i] * (ft)((ty*)v)[i]; \
return sqrt(tmp); \
} \
\
ft vLenSq##suf(const Vector##suf v) { \
return vLenSq##suf##p(&v); \
} \
ft vLenSq##suf##p(const Vector##suf* v) { \
return vDot##suf##p(v, v); \
} \
\
ft vMag##suf(const Vector##suf v) { \
return vLen##suf##p(&v); \
} \
ft vMag##suf##p(const Vector##suf* v) { \
return vLen##suf##p(v); \
} \
\
ft vInvLen##suf(const Vector##suf v) { \
ft tmp = vLen##suf(v); \
return tmp == 0 ? pref##_MAX : (ft)1.0 / tmp; \
} \
ft vInvLen##suf##p(const Vector##suf* v) { \
return vInvLen##suf(*v); \
} \
\
Vector##sufft vNorm##suf(const Vector##suf v) { \
Vector##sufft out; \
vNorm##suf##p(&v, &out); \
return out; \
} \
void vNorm##suf##p(const Vector##suf* v, Vector##sufft* out) { \
ft n = vLenSq##suf(*v); \
\
if(n >= (ft)1.0f - pref##_CMP_EPSILON && n <= (ft)1.0 + pref##_CMP_EPSILON) { \
for(int i = 0; i < sz; i++) \
((ft*)out)[i] = (ft)((ty*)v)[i]; \
return; \
} \
else if(n == 0.0) { \
for(int i = 0; i < sz; i++) \
((ft*)out)[i] = 0; \
return; \
} \
\
n = (ft)1.0 / sqrt(n); \
for(int i = 0; i < sz; i++) \
((ft*)out)[i] = (ft)((ty*)v)[i] * n; \
} \
\
Vector##sufft vUnit##suf(const Vector##suf v) { \
return vNorm##suf(v); \
} \
void vUnit##suf##p(const Vector##suf* v, Vector##sufft* out) { \
return vNorm##suf##p(v, out); \
} \
C3DLAS_VECTOR_LIST(FN)
#undef FN
// swap two vectors
void vSwap2ip(Vector2i* a, Vector2i* b) {
Vector2i t;
t = *b;
*b = *a;
*a = t;
}
void vSwap2p(Vector2* a, Vector2* b) {
Vector2 t;
t = *b;
*b = *a;
*a = t;
}
void vSwap3p(Vector3* a, Vector3* b) {
Vector3 t;
t = *b;
*b = *a;
*a = t;
}
void vSwap4p(Vector4* a, Vector4* b) {
Vector4 t;
t = *b;
*b = *a;
*a = t;
}
// scalar muliplication
// Dot product (inner product)
// Cross product: out = a x b
// Cross products only exist in 3 and 7 dimensions
Vector3 vCross3(Vector3 a, Vector3 b) {
Vector3 out;
vCross3p(&a, &b, &out);
return out;
}
void vCross3p(Vector3* a, Vector3* b, Vector3* out) {
out->x = (a->y * b->z) - (a->z * b->y);
out->y = (a->z * b->x) - (a->x * b->z);
out->z = (a->x * b->y) - (a->y * b->x);
}
// Scalar triple product: a . (b x c)
float vScalarTriple3(Vector3 a, Vector3 b, Vector3 c) {
return vScalarTriple3p(&a, &b, &c);
}
float vScalarTriple3p(Vector3* a, Vector3* b, Vector3* c) {
return (float)((a->x * b->y * c->z) - (a->x * b->z * c->y) - (a->y * b->x * c->z)
+ (a->z * b->x * c->y) + (a->y * b->z * c->x) - (a->z * b->y * c->x));
}
// Vector Inverse. Returns FLT_MAX on div/0
// Vector magnitude (length)
// Squared distance from one point to another
// Distance from one point to another
// Vector normalize (scale to unit length)
// vMin(a, b) Returns the minimum values of each component
// vMin(a, b) Returns the maximum values of each component
#define FN(sz, suf, t, maxval) \
void vMin##sz##suf##p(const Vector##sz##suf* a, const Vector##sz##suf* b, Vector##sz##suf* out) { \
for(int i = 0; i < sz; i++) \
((t*)out)[i] = fmin(((t*)a)[i], ((t*)b)[i]); \
} \
void vMax##sz##suf##p(const Vector##sz##suf* a, const Vector##sz##suf* b, Vector##sz##suf* out) { \
for(int i = 0; i < sz; i++) \
((t*)out)[i] = fmax(((t*)a)[i], ((t*)b)[i]); \
} \
Vector##sz##suf vMin##sz##suf(Vector##sz##suf a, Vector##sz##suf b) { \
Vector##sz##suf out; \
vMin##sz##suf##p(&a, &b, &out); \
return out; \
} \
Vector##sz##suf vMax##sz##suf(Vector##sz##suf a, Vector##sz##suf b) { \
Vector##sz##suf out; \
vMax##sz##suf##p(&a, &b, &out); \
return out; \
} \
\
int vMinComp##sz##suf##p(const Vector##sz##suf* a) { \
t best = ((t*)a)[0]; \
int best_ind = 0; \
for(int i = 1; i < sz; i++) { \
if(((t*)a)[i] < best) { \
best = ((t*)a)[i]; \
best_ind = i; \
} \
} \
return best_ind; \
} \
\
int vMaxComp##sz##suf##p(const Vector##sz##suf* a) { \
t best = ((t*)a)[0]; \
int best_ind = 0; \
for(int i = 1; i < sz; i++) { \
if(((t*)a)[i] > best) { \
best = ((t*)a)[i]; \
best_ind = i; \
} \
} \
return best_ind; \
} \
\
int vMinComp##sz##suf(const Vector##sz##suf a) { \
return vMinComp##sz##suf##p(&a); \
} \
\
int vMaxComp##sz##suf(const Vector##sz##suf a) { \
return vMaxComp##sz##suf##p(&a); \
} \
\
Vector##sz##suf vClamp##sz##suf(Vector##sz##suf in, Vector##sz##suf min, Vector##sz##suf max) { \
Vector##sz##suf out; \
for(int i = 0; i < sz; i++) \
((t*)&out)[i] = fmax(((t*)&min)[i], fmin(((t*)&in)[i], ((t*)&max)[i])); \
return out; \
} \
Vector##sz##suf vAbs##sz##suf(const Vector##sz##suf v) { \
Vector##sz##suf out; \
vAbs##sz##suf##p(&v, &out); \
return out; \
} \
void vAbs##sz##suf##p(const Vector##sz##suf* v, Vector##sz##suf* out) { \
for(int i = 0; i < sz; i++) \
((t*)out)[i] = abs_##t( ((t*)v)[i] ); \
} \
Vector##sz##suf vSign##sz##suf(const Vector##sz##suf v) { \
Vector##sz##suf out; \
vSign##sz##suf##p(&v, &out); \
return out; \
} \
void vSign##sz##suf##p(const Vector##sz##suf* v, Vector##sz##suf* out) { \
for(int i = 0; i < sz; i++) \
((t*)out)[i] = copysign((t)1.0, ((t*)v)[i] ); \
} \
Vector##sz##suf vStep##sz##suf(const Vector##sz##suf edge, const Vector##sz##suf v) { \
Vector##sz##suf out; \
vStep##sz##suf##p(&edge, &v, &out); \
return out; \
} \
void vStep##sz##suf##p(const Vector##sz##suf* edge, const Vector##sz##suf* v, Vector##sz##suf* out) { \
for(int i = 0; i < sz; i++) \
((t*)out)[i] = ((t*)v)[i] < ((t*)edge)[i] ? 0.0 : 1.0; \
} \
FN(2, , float, FLT_MAX)
FN(3, , float, FLT_MAX)
FN(4, , float, FLT_MAX)
FN(2, d, double, DBL_MAX)
FN(3, d, double, DBL_MAX)
FN(4, d, double, DBL_MAX)
#undef FN
#define FN(sz, suf, t, maxval) \
void vMin##sz##suf##p(const Vector##sz##suf* a, const Vector##sz##suf* b, Vector##sz##suf* out) { \
for(int i = 0; i < sz; i++) \
((t*)out)[i] = MIN(((t*)a)[i], ((t*)b)[i]); \
} \
void vMax##sz##suf##p(const Vector##sz##suf* a, const Vector##sz##suf* b, Vector##sz##suf* out) { \
for(int i = 0; i < sz; i++) \
((t*)out)[i] = MAX(((t*)a)[i], ((t*)b)[i]); \
} \
Vector##sz##suf vMin##sz##suf(Vector##sz##suf a, Vector##sz##suf b) { \
Vector##sz##suf out; \
vMin##sz##suf##p(&a, &b, &out); \
return out; \
} \
Vector##sz##suf vMax##sz##suf(Vector##sz##suf a, Vector##sz##suf b) { \
Vector##sz##suf out; \
vMax##sz##suf##p(&a, &b, &out); \
return out; \
} \
Vector##sz##suf vClamp##sz##suf(Vector##sz##suf in, Vector##sz##suf min, Vector##sz##suf max) { \
Vector##sz##suf out; \
for(int i = 0; i < sz; i++) \
((t*)&out)[i] = MAX(((t*)&min)[i], MIN(((t*)&in)[i], ((t*)&max)[i])); \
return out; \
} \
Vector##sz##suf vAbs##sz##suf(const Vector##sz##suf v) { \
Vector##sz##suf out; \
vAbs##sz##suf##p(&v, &out); \
return out; \
} \
void vAbs##sz##suf##p(const Vector##sz##suf* v, Vector##sz##suf* out) { \
for(int i = 0; i < sz; i++) \
((t*)out)[i] = labs( ((t*)v)[i] ); \
} \
Vector##sz##suf vSign##sz##suf(const Vector##sz##suf v) { \
Vector##sz##suf out; \
vSign##sz##suf##p(&v, &out); \
return out; \
} \
void vSign##sz##suf##p(const Vector##sz##suf* v, Vector##sz##suf* out) { \
for(int i = 0; i < sz; i++) \
((t*)out)[i] = ((t*)v)[i] < 0 ? -1 : 1; \
} \
Vector##sz##suf vStep##sz##suf(const Vector##sz##suf edge, const Vector##sz##suf v) { \
Vector##sz##suf out; \
vStep##sz##suf##p(&edge, &v, &out); \
return out; \
} \
void vStep##sz##suf##p(const Vector##sz##suf* edge, const Vector##sz##suf* v, Vector##sz##suf* out) { \
for(int i = 0; i < sz; i++) \
((t*)out)[i] = ((t*)v)[i] < ((t*)edge)[i] ? 0.0 : 1.0; \
} \
FN(2, i, int, DBL_MAX)
FN(3, i, int, DBL_MAX)
FN(4, i, int, DBL_MAX)
FN(2, l, long, DBL_MAX)
FN(3, l, long, DBL_MAX)
FN(4, l, long, DBL_MAX)
#undef FN
// Returns an arbitrary unit vector perpendicular to the input
// The input vector does not need to be normalized
void vPerp3p(Vector3* n, Vector3* out) {
*out = vPerp3(*n);
}
Vector3 vPerp3(Vector3 n) {
float f, d;
float absx = fabs(n.x);
float absy = fabs(n.y);
if(absx < absy) {
if(n.x < n.z) goto MIN_X;
goto MIN_Z;
}
if(absy < fabs(n.z)) goto MIN_Y;
goto MIN_Z;
MIN_X:
d = 1.0f / sqrtf(n.z * n.z + n.y * n.y);
f = n.z;
n.z = n.y * d;
n.y = -f * d;
n.x = 0;
return n;
MIN_Y:
d = 1.0f / sqrtf(n.z * n.z + n.x * n.x);
f = n.x;
n.x = n.z * d;
n.z = -f * d;
n.y = 0;
return n;
MIN_Z:
d = 1.0f / sqrtf(n.x * n.x + n.y * n.y);
f = n.y;
n.y = n.x * d;
n.x = -f * d;
n.z = 0;
return n;
}
// Returns an arbitrary unit vector perpendicular to the input
// The input vector does not need to be normalized
void vPerp2p(Vector2* n, Vector2* out) {
*out = vPerp2(*n);
}
Vector2 vPerp2(Vector2 n) {
return vNorm2((Vector2){.x = -n.y, .y = n.x});
}
// Coordinate system conversions
// Does not check for degenerate vectors
// Cartesian to Spherical
Vector3 vC2S3(Vector3 cart) {
Vector3 sp;
sp.rho = vMag3(cart);
sp.theta = atan2f(cart.x, cart.y);
sp.phi = acosf(cart.z / sp.rho);
return sp;
}
// Spherical to Cartesian
Vector3 vS2C3(Vector3 s) {
float st, ct, sp, cp;
// as of July 2022, gcc trunk is smart enough to automatically optimize to sincos, but clang isn't.
sincosf(s.phi, &sp, &cp);
sincosf(s.theta, &st, &ct);
return (Vector3){
.x = s.rho * sp * ct,
.y = s.rho * sp * st,
.z = s.rho * cp
};
}
Vector3 closestPointToRay3(Vector3 p, Ray3 r) {
Vector3 po = vSub3(p, r.o); // vector from the starting point to p
float t = vDot3(po, r.d); // project the pa onto the ray direction
fclamp(t, 0.0, 1.0); // clamp t to between the endpoints of the line segment
return vSub3(po, vScale3(r.d, t));
}
// completely untested.
// can probably be optimized
// This function is poorly named. It is designed to check if a bounding sphere intersects a cone surrounding a viewing frustum.
int distanceSphereToCone(Vector3 spc, float spr, Vector3 c1, Vector3 c2, float cr1, float cr2) {
Vector3 cnorm = vNorm(vSub(c2, c1)); // normal pointing down the center of the cone
Vector3 sp_c1 = vSub(spc, c1); // vector pointing from c1 to the sphere center
Vector3 up = vCross3(spc, cnorm); // vector perpendicular to the plane containing the cone's centerline and the sphere center.
Vector3 perp_toward_sp = vNorm(vCross3(cnorm, up)); // vector perpendicular to the cone's centerline within the plane, towards the sphere
Vector3 outer_c1 = vAdd(c1, vScale(perp_toward_sp, cr1)); // point in the plane on the outer edge of the cone
Vector3 outer_c2 = vAdd(c2, vScale(perp_toward_sp, cr2)); // point in the plane on the outer edge of the cone
Vector3 closest = closestPointToRay3(spc, (Ray3){.o = outer_c1, .d = vNorm(vSub(outer_c2, outer_c1))}); // point on the cone closest to the sphere
// this part is probably wrong
if(vDot(perp_toward_sp, vSub(spc, closest)) < 0) return 1; // is the sphere center inside the cone?
return (vDist(closest, spc) - spr) <= 0;
}
// Muchas gracias, Inigo.
// https://iquilezles.org/articles/distfunctions2d/
float vDistPointLine2(Vector2 p, Line2 ls) {
Vector2 pa = vSub2(p, ls.start); // vector from the starting point to p
Vector2 ba = vSub2(ls.end, ls.start); // vector from the starting point to the ending point
float t = vDot2(pa, ba) / vDot2(ba, ba); // project the pa onto ba, then divide that distance by the length of ba to normalize it
fclamp(t, 0.0, 1.0); // clamp t to between the endpoints of the line segment
// Consider the starting point to be at the origin, for ease of visualization.
// ba is the vector from the origin to the endpoint og the line that now passes through the origin.
// Scaling ba by t gives the intercept point of the line through p that is perpendicular to the test line segment.
// pa is p if a was the origin. Therefore, pi is the vector from p to the intercept point on the test line segment.
Vector2 pi = vSub2(pa, vScale2(ba, t));
return vMag2(pi); // the answer is the length of pi
}
float vDistPointLine3(Vector3 p, Line3 ls) {
Vector3 pa = vSub3(p, ls.start);
Vector3 ba = vSub3(ls.end, ls.start);
float t = fclamp(vDot3(pa, ba) / vDot3(ba, ba), 0.0, 1.0);
return vMag3(vSub3(pa, vScale3(ba, t)));
}
// This version also returns the normalized distance along the line of the closest point
float vDistTPointLine2(Vector2 p, Line2 ls, float* T) {
Vector2 pa = vSub2(p, ls.start);
Vector2 ba = vSub2(ls.end, ls.start);
float t = fclamp(vDot2(pa, ba) / vDot2(ba, ba), 0.0, 1.0);
if(T) *T = t;
return vMag2(vSub2(pa, vScale2(ba, t)));
}
float vDistTPointLine3(Vector3 p, Line3 ls, float* T) {
Vector3 pa = vSub3(p, ls.start);
Vector3 ba = vSub3(ls.end, ls.start);
float t = fclamp(vDot3(pa, ba) / vDot3(ba, ba), 0.0, 1.0);
if(T) *T = t;
return vMag3(vSub3(pa, vScale3(ba, t)));
}
// ----
int intersectLine2Line2(Line2 a, Line2 b) {
float s1x = a.end.x - a.start.x;
float s1y = a.end.y - a.start.y;
float s2x = b.end.y - b.start.y;
float s2y = b.end.y - b.start.y;
float det = s1x * s2y - s2x * s1y;
if(fabsf(det) < 1e-5f) {
return C3DLAS_PARALLEL;
}
float invdet = 1.f / det;
float x02 = a.start.x - b.start.x;
float y02 = a.start.y - b.start.y;
float s = (s1x * y02 - s1y * x02) * invdet;
if(s >= 0.f && s <= 1.f) {
float t = (s2x * y02 - s2y * x02) * invdet;
if(t >= 0.f && t <= 1.f) {
return C3DLAS_INTERSECT;
}
}
return C3DLAS_DISJOINT;
}
float projPointLine2(Vector2 p, Line2 ls) {
Vector2 pa = vSub2(p, ls.start);
Vector2 ba = vSub2(ls.end, ls.start);
return vDot2(pa, ba) / vDot2(ba, ba);
}
float distTPointRay3(Vector3 p, Ray3 r, float* T) {
Vector3 pa = vSub3(p, r.o);
Vector3 ba = vNeg3(r.d);// vSub3(ls.end, ls.start);
float t = vDot3(pa, ba) / vDot3(ba, ba);
if(T) *T = t;
return vLen3(vSub3(pa, vScale3(ba, t)));
}
float dist2TPointRay3(Vector3 p, Ray3 r, float* T) {
Vector3 pa = vSub3(p, r.o);
Vector3 ba = vNeg3(r.d);// vSub3(ls.end, ls.start);
float t = vDot3(pa, ba) / vDot3(ba, ba);
if(T) *T = t;
return vLenSq3(vSub3(pa, vScale3(ba, t)));
}
int vInsidePolygon(Vector2 p, Polygon* poly) {
int inside = 0;
int cnt = poly->pointCount;
if(poly->maxRadiusSq < vDot2(poly->centroid, p)) return 0;
for(int i = 0; i < cnt; i++) {
Vector2 a = poly->points[i];
Vector2 b = poly->points[(i + 1) % cnt];
if(a.y == b.y) continue; // horizontal edges are ignored
// we're testing a ray going to the right
if(a.x < p.x && b.x < p.x) continue; // segment is entirely left of the point
if(a.y >= p.y && b.y >= p.y) continue; // segment entirely above the point
if(a.y < p.y && b.y < p.y) continue; // segment entirely below the point
// segment is in the same vertical band as the point
float sx = a.x + (b.x - a.x) * ((p.y - a.y) / (b.y - a.y));
if(p.x > sx) continue;
inside = !inside;
}
return inside;
}
// Muchas gracias, Inigo.
// https://iquilezles.org/articles/distfunctions2d/
float vDistPolygon(Vector2 p, Polygon* poly) {
float d = vDot2(vSub2(p, poly->points[0]), vSub2(p, poly->points[0]));
float s = 1.0;
for(int i = 0, j = poly->pointCount - 1; i < poly->pointCount; j = i, i++) {
Vector2 A = poly->points[i];
Vector2 B = poly->points[j];
Vector2 e = vSub2(B, A);
Vector2 w = vSub2(p, A);
Vector2 b = vSub2(w, vScale2(e, fclamp(vDot2(w, e) / vDot2(e, e), 0.0, 1.0)));
d = fminf(d, vDot2(b, b));
int c1 = p.y >= A.y;
int c2 = p.y < B.y;
int c3 = e.x * w.y > e.y * w.x;
if((c1 && c2 && c3) || (!c1 && !c2 && !c3)) s *= -1.0;
}
return s * sqrtf(d);
}
// ----
void polyCalcCentroid(Polygon* poly) {
int cnt = poly->pointCount;
Vector2 centroid = {0,0};
for(int i = 0; i < cnt; i++) {
Vector2 a = poly->points[i];
centroid = vAdd2(centroid, a);
}
poly->centroid = vScale2(centroid, 1.0 / poly->pointCount);
}
void polyCalcRadiusSq(Polygon* poly) {
int cnt = poly->pointCount;
float d = 0;
for(int i = 0; i < cnt; i++) {
Vector2 a = poly->points[i];
d = fmaxf(d, vDot2(poly->centroid, a));
}
poly->maxRadiusSq = d;
}
// feeding a zero vector into this will cause div/0 and you will deserve it
void vProject3p(Vector3* what, Vector3* onto, Vector3* out) { // slower; onto may not be normalized
float wdo = vDot3p(what, onto);
float odo = vDot3p(onto, onto);
vScale3p(onto, wdo / odo, out);
}
void vProjectNorm3p(Vector3* what, Vector3* onto, Vector3* out) { // faster; onto must be normalized
float wdo = vDot3p(what, onto);
vScale3p(onto, wdo, out);
}
void vRandomPCG2p(Vector2* end1, Vector2* end2, PCG* pcg, Vector2* out) {
out->x = frandPCG(fminf(end1->x, end2->x), fmaxf(end1->x, end2->x), pcg);
out->y = frandPCG(fminf(end1->y, end2->y), fmaxf(end1->y, end2->y), pcg);
}
Vector2 vRandomPCG2(Vector2 end1, Vector2 end2, PCG* pcg) {
Vector2 o;
vRandomPCG2p(&end1, &end2, pcg, &o);
return o;
}
void vRandomNormPCG2p(PCG* pcg, Vector2* out) {
float th = frandPCG(0, 2.0 * F_PI, pcg);
float sth, cth;
sincosf(th, &sth, &cth);
out->x = cth;
out->y = sth;
}
Vector2 vRandomNormPCG2(PCG* pcg) {
Vector2 o;
vRandomNormPCG2p(pcg, &o);
return o;
}
void vRandomPCG3p(Vector3* end1, Vector3* end2, PCG* pcg, Vector3* out) {
out->x = frandPCG(fminf(end1->x, end2->x), fmaxf(end1->x, end2->x), pcg);
out->y = frandPCG(fminf(end1->y, end2->y), fmaxf(end1->y, end2->y), pcg);