-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathntcd.h
1725 lines (1484 loc) · 59.4 KB
/
ntcd.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
// LICENSE
//
// MIT License
//
// Copyright (c) 2017 Nikos Tasios
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//
// USAGE
//
// Include this file in whatever places neeed to refer to it. In ONE C/C++
// file, write:
// #define STB_TRUETYPE_IMPLEMENTATION
// before the #include of this file. This expands out the actual
// implementation into that C/C++ file.
//
// To make the implementation private to the file that generates the implementation,
// #define STBTT_STATIC
//
// DOCUMENTATION
//
// Please read the comments in the 'Interface' section for documentation.
// Below you can find a usage example. The example can be copied to a separate
// file and compiled.
//
#if 0
#define NTCD_IMPLEMENTATION
#include "ntcd.h"
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif
#include <assert.h>
int main(int argc, char* argv[]){
ntcd_cylinder cyl;
ntcd_cylinder_initialize(&cyl, 1.0, 1.0);
//Two cylinders of height 1, rotate by 45 degrees around the z-axis,
//should overlap at x = 1 / sin(45).
double d = 1.0 / sin(M_PI / 4.0);
ntcd_transform ta = {
{0.0, 0.0, 0.0},
{0.0, 0.0, sin(M_PI / 8.0), cos(M_PI / 8.0)},
1.0
};
ntcd_transform tb = {
{d - 0.01, 0.0, 0.0},
{0.0, 0.0, sin(M_PI / 8.0), cos(M_PI / 8.0)},
1.0
};
ntcd_transform tc = {
{d + 0.01, 0.0, 0.0},
{0.0, 0.0, sin(M_PI / 8.0), cos(M_PI / 8.0)},
1.0
};
int overlap_a = ntcd_gjk_boolean(&ta, &cyl, &tb, &cyl);
int overlap_b = ntcd_gjk_boolean(&ta, &cyl, &tc, &cyl);
assert(overlap_a != overlap_b);
double dist[3];
ntcd_gjk_distance(dist, &ta, &cyl, &tc, &cyl);
double length = sqrt(dist[0] * dist[0] + dist[1] * dist[1] + dist[2] * dist[2]);
assert(length < 0.01);
assert(length > 0.0);
return 0;
}
#endif
#ifdef NTCD_IMPLEMENTATION
#ifndef NTCD_GJK_ERROR_TOLERANCE
#include <float.h>
#define NTCD_GJK_ERROR_TOLERANCE 10.0 * DBL_EPSILON
#endif
#ifndef NTCD_GJK_RELATIVE_ERROR
#define NTCD_GJK_RELATIVE_ERROR 1.0e-4
#define NTCD_GJK_RELATIVE_ERROR2 NTCD_GJK_RELATIVE_ERROR * NTCD_GJK_RELATIVE_ERROR
#endif
#endif
//Interface (Read this part for documentation)
#ifndef __NTCD_H__
#define __NTCD_H__
#ifdef NTCD_STATIC
#define NTCD_DEF static
#else
#define NTCD_DEF extern
#endif
#ifdef __cplusplus
extern "C"{
#endif
//Shape transform, consisting of the 3d position, the rotation in the form
//of a quaternion, and the size, which scales the shape size. The struct
//should be easy to replace with your own.
typedef struct{
double pos[3];
double rot[4]; //Quaternion
double size;
}ntcd_transform;
//Type of the support function which GJK uses.
typedef void (*ntcd_support)(double*, const void*, const double*);
//Check if two shapes, ca and cb with transforms ta and tb respectively
//overlap. Returns 1 if they overlap and 0 otherwise.
NTCD_DEF int ntcd_gjk_boolean(
const ntcd_transform* ta, const void* ca,
const ntcd_transform* tb, const void* cb
);
//Calculate the closest distance dist_vec between two shapes, ca and cb with
//transforms ta and tb respectively.
NTCD_DEF void ntcd_gjk_distance(
double* dist_vec,
const ntcd_transform* ta, const void* ca,
const ntcd_transform* tb, const void* cb
);
//Calculate the closest points, point_on_a and point_on_b on shape, ca and cb
//with transforms ta and tb respectively. The two outputs are zeroed in case
//the two shapes overlap.
NTCD_DEF void ntcd_gjk_closest_points(
double* point_on_a, double* point_on_b,
const ntcd_transform* ta, const void* ca,
const ntcd_transform* tb, const void* cb
);
//Calculate the distance shape ca with transform ta can be moved on a straight
//line in the direction ray_dir before it meets shape cb with transform tb.
//Returns 0 when shape ca does not meet cb, othewise it returns 1 if it succeeds.
NTCD_DEF int ntcd_gjk_raycast(
double* distance, double* normal,
const ntcd_transform* ta, const void* ca,
const ntcd_transform* tb, const void* cb,
const double* ray_dir
);
//Shapes
//NOTE: Be sure you know how each shape is originally oriented.
//Current supported shapes:
//
// Sphere, Point, Line, Mesh, Cylinder, Box, Cone, Bicone,
// Leaf Cylinder, Sphere-swept, Hull, Minkowski
//Sphere
//The sphere is of radius 1.
typedef struct{
ntcd_support support;
}ntcd_sphere;
NTCD_DEF void ntcd_sphere_initialize(ntcd_sphere* sph);
//Point
typedef struct{
ntcd_support support;
}ntcd_point;
NTCD_DEF void ntcd_point_initialize(ntcd_point* point);
//Line
//The line is of length 1 and is parallel to the y-axis.
typedef struct{
ntcd_support support;
}ntcd_line;
NTCD_DEF void ntcd_line_initialize(ntcd_line* line);
//Disk
//The disk is of radius 1 and its axis is parallel to the y-axis.
typedef struct{
ntcd_support support;
}ntcd_disk;
NTCD_DEF void ntcd_disk_initialize(ntcd_disk* disk);
//Mesh
typedef struct{
ntcd_support support;
unsigned int n_vertices_;
double* vertices_;
unsigned int* n_vert_neighbours_;
unsigned int** vert_neighbours_;
}ntcd_mesh;
//Initializing a mesh is slightly involved. The mesh vertices and the vertex
//indices of each face need to be passed. The vertices are passed as a flat array
//of size 3 * n_vertices. Vertex i with i < n_vertices is given by (vertices + 3 * i).
//The vertex indices of each face are also passed as a flat array. The array face_start
//stores the array position inside faces where each face starts.
//
//As an example, suppose we would like to load a tetrahedron with edge length 2. The
//4 vertices are located at,
//
// { 1, 0, -1 / sqrt(2)},
// {-1, 0, -1 / sqrt(2)},
// { 0, 1, 1 / sqrt(2)},
// { 0, -1, 1 / sqrt(2)}
//
//The four faces are defined as (note that the order of face indices does not play
//any role in our mesh implementation),
//
// {0, 1, 2},
// {0, 1, 3},
// {0, 2, 3},
// {1, 2, 3}
//
//The vertices array passed would then look like,
//
// vertices[] = {
// 1, 0, -1 / sqrt(2), -1, 0, -1 / sqrt(2),
// 0, 1, 1 / sqrt(2), 0, -1, 1 / sqrt(2)
// }
//
//while faces would look like,
//
// faces[] = {0, 1, 2, 0, 1, 3, 0, 2, 3, 1, 2, 3}
//
//and face_start,
//
// face_start[] = {0, 3, 6, 9}
//
//Also note that faces are not required to have 3 vertices, and that different faces
//of the shame shape need not have the same number of vertices either.
NTCD_DEF void ntcd_mesh_initialize(
ntcd_mesh* mesh,
const unsigned int n_vertices, const double* vertices,
const unsigned int n_faces, const unsigned int* face_start, const unsigned int* faces
);
NTCD_DEF void ntcd_mesh_terminate(ntcd_mesh* mesh);
//Cylinder
//The cylinder is of radius base_radius and height height, and its axis of symmetry is
//parallel to the y-axis.
typedef struct{
ntcd_support support;
double base_radius_, half_height_;
}ntcd_cylinder;
NTCD_DEF void ntcd_cylinder_initialize(ntcd_cylinder* cyl, double base_radius, double height);
//Box
//The length of each side of the box is give by the extent array.
typedef struct{
ntcd_support support;
double extent_[3];
}ntcd_box;
NTCD_DEF void ntcd_box_initialize(ntcd_box* box, const double* extent);
//Cone
//The cone has a base radius base_radius and height height, and its axis of symmetry is
//parallel to the y-axis.
typedef struct{
ntcd_support support;
double base_radius_, half_height_;
double sintheta_;
}ntcd_cone;
NTCD_DEF void ntcd_cone_initialize(ntcd_cone* cone, double base_radius, double height);
//Bicone
//The bicone has a mid radius base_radius and height height, and its axis of symmetry is
//parallel to the y-axis.
typedef struct{
ntcd_support support;
double base_radius_, half_height_;
double sintheta_;
}ntcd_bicone;
NTCD_DEF void ntcd_bicone_initialize(ntcd_bicone* bicone, double base_radius, double height);
//Leaf Cylinder
typedef struct{
ntcd_support support;
double half_width_, half_length_, half_height_;
double circle_radius_, circle_distance_;
}ntcd_leaf_cylinder;
NTCD_DEF void ntcd_leaf_cylinder_initialize(ntcd_leaf_cylinder* leaf, double width, double length, double height);
//Sphere-swept
//The sphere swept shape is produced from a shape, by sweeping a sphere around the
//shape's surface.
typedef struct{
ntcd_support support;
const void* shape_;
double radius_;
}ntcd_sphere_swept;
NTCD_DEF void ntcd_sphere_swept_initialize(ntcd_sphere_swept* msum, double radius, const void* shape);
//Hull
//The convex hull of two shapes.
typedef struct{
ntcd_support support;
const void* ca_;
const void* cb_;
ntcd_transform ta_;
ntcd_transform tb_;
//Cache inverse rotations.
double inv_rot_a_[4];
double inv_rot_b_[4];
}ntcd_hull;
NTCD_DEF void ntcd_hull_initialize(ntcd_hull* hull, const ntcd_transform* ta, const void* ca, const ntcd_transform* tb, const void* cb);
//Minkowski
//The minkowski sum of two shapes. The transform t is applied to the second shape, cb.
typedef struct{
ntcd_support support;
const void* ca_;
const void* cb_;
ntcd_transform t_;
//Cache inverse rotation.
double inv_rot_[4];
}ntcd_minkowski_sum;
NTCD_DEF void ntcd_minkowski_sum_initialize(ntcd_minkowski_sum* msum, const ntcd_transform* t, const void* ca, const void* cb);
#ifdef __cplusplus
}
#endif
#endif //__NTCD_H__
//Implementation
#ifdef NTCD_IMPLEMENTATION
//TODO: Investigate raycast false positives on y-axis for cylinders.
//TODO: Impelement EPA for penetration depth.
//TODO: Implement Johnson's dstance algorithm as an alternative.
//TODO: Add MPR?
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#define BARY_GEPP
//Vectors
static double ntcd__vec3_dot(const double* a, const double* b){
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
static void ntcd__vec3_cross(double* c, const double* a, const double* b){
c[0] = a[1] * b[2] - a[2] * b[1];
c[1] = a[2] * b[0] - a[0] * b[2];
c[2] = a[0] * b[1] - a[1] * b[0];
}
//Calculates d = (a x b) x c
static void ntcd__vec3_triple_product(double* d, const double* a, const double* b, const double* c){
d[0] = b[0] * ntcd__vec3_dot(a, c) - a[0] * ntcd__vec3_dot(b, c);
d[1] = b[1] * ntcd__vec3_dot(a, c) - a[1] * ntcd__vec3_dot(b, c);
d[2] = b[2] * ntcd__vec3_dot(a, c) - a[2] * ntcd__vec3_dot(b, c);
}
static void ntcd__vec3_add(double*c, const double* a, const double* b){
c[0] = a[0] + b[0];
c[1] = a[1] + b[1];
c[2] = a[2] + b[2];
}
static void ntcd__vec3_sub(double* c, const double* a, const double* b){
c[0] = a[0] - b[0];
c[1] = a[1] - b[1];
c[2] = a[2] - b[2];
}
static void ntcd__vec3_smul(double* c, double f, const double* a){
c[0] = f * a[0];
c[1] = f * a[1];
c[2] = f * a[2];
}
static void ntcd__vec3_fmadd(double* c, double a, const double* x, const double* y){
c[0] = a * x[0] + y[0];
c[1] = a * x[1] + y[1];
c[2] = a * x[2] + y[2];
}
static double ntcd__vec3_length2(const double* vec){
return vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2];
}
static double ntcd__vec3_length(const double* vec){
return sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
}
//TODO: Try memcmp performance
static int ntcd__vec3_equal(const double* a, const double* b){
return (a[0] == b[0]) && (a[1] == b[1]) && (a[2] == b[2]);
}
// Quaternions
//TODO: Do we really need to calculate the length? Aren't all quaternions assumed of unit length?
static void ntcd__quat_inverse(double* r, const double* q){
double ilength2 = 1.0 / (q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
r[0] = -q[0] * ilength2;
r[1] = -q[1] * ilength2;
r[2] = -q[2] * ilength2;
r[3] = q[3] * ilength2;
}
static void ntcd__quat_vec3_rotate(double* r, const double* q, const double* v){
double u[3];
{
double a[3], b[3];
ntcd__vec3_cross(a, q, v);
ntcd__vec3_fmadd(b, q[3], v, a);
ntcd__vec3_cross(u, q, b);
}
ntcd__vec3_fmadd(r, 2.0, u, v);
//TODO: Check if this is faster
//r[0] = v[0] + 2.0 * (q[1] * (q[0] * v[1] - q[1] * v[0] + q[3] * v[2]) - q[2] * (q[2] * v[0] - q[0] * v[2] + q[3] * v[1]));
//r[1] = v[1] + 2.0 * (q[2] * (q[1] * v[2] - q[2] * v[1] + q[3] * v[0]) - q[0] * (q[0] * v[1] - q[1] * v[0] + q[3] * v[2]));
//r[2] = v[2] + 2.0 * (q[0] * (q[2] * v[0] - q[0] * v[2] + q[3] * v[1]) - q[1] * (q[1] * v[2] - q[2] * v[1] + q[3] * v[0]));
}
//Lookup table for single enabled bit position -- Calculates the log2 of a 4-bit integer.
static const unsigned char s_pos[] = {0, 0, 1, 0, 2, 0, 0, 0, 3};
//_______________________________________^__^_____^___________^
//Lookup table which tells us at which positions in the simplex array
//to get our points a, b, c, d. I.e. if our bits are 0111 -> 7 -> {0, 1, 2}
static const unsigned char p_pos[16][3] =
{
{0, 0, 0}, {0, 0, 0}, {1, 0, 0}, {0, 1, 0},
{2, 0, 0}, {0, 2, 0}, {1, 2, 0}, {0, 1, 2},
{3, 0, 0}, {0, 3, 0}, {1, 3, 0}, {0, 1, 3},
{2, 3, 0}, {0, 2, 3}, {1, 2, 3}, {0, 0, 0}
};
#if defined(BARY_ERICSON)
static double triangle_area_2D(double x1, double y1, double x2, double y2, double x3, double y3){
return (x1 - x2) * (y2 - y3) - (x2 - x3) * (y1 - y2);
}
//Algorithm for calculating barycentric coordinates from
//'Real-time collision detection' by Christer Ericson.
static void barycentric_coordinates(double* R, const double* P, const double* A, const double* B, const double* C){
double u, v, w;
double m[3];
{
double AB[3], AC[3];
ntcd__vec3_sub(AB, B, A);
ntcd__vec3_sub(AC, C, A);
ntcd__vec3_cross(m, AB, AC);
}
double nu, nv, ood;
double x = fabs(m[0]), y = fabs(m[1]), z = fabs(m[2]);
if(x >= y && x >= z){
nu = triangle_area_2D(P[1], P[2], B[1], B[2], C[1], C[2]);
nv = triangle_area_2D(P[1], P[2], C[1], C[2], A[1], A[2]);
ood = 1.0 / m[0];
}
else if(y >= x && y >= z){
nu = triangle_area_2D(P[0], P[2], B[0], B[2], C[0], C[2]);
nv = triangle_area_2D(P[0], P[2], C[0], C[2], A[0], A[2]);
ood = 1.0 / -m[1];
}
else{
nu = triangle_area_2D(P[0], P[1], B[0], B[1], C[0], C[1]);
nv = triangle_area_2D(P[0], P[1], C[0], C[1], A[0], A[1]);
ood = 1.0 / m[2];
}
R[0] = nu * ood;
R[1] = nv * ood;
R[2] = 1.0 - R[0] - R[1];
}
#elif defined(BARY_CRAMER)
static void barycentric_coordinates(double* R, const double* P, const double* A, const double* B, const double* C){
double v0[3], v1[3], v2[3];
ntcd__vec3_sub(v0, B, A);
ntcd__vec3_sub(v1, C, A);
ntcd__vec3_sub(v2, P, A);
double d00 = ntcd__vec3_dot(v0, v0);
double d01 = ntcd__vec3_dot(v0, v1);
double d02 = ntcd__vec3_dot(v0, v2);
double d11 = ntcd__vec3_dot(v1, v1);
double d12 = ntcd__vec3_dot(v1, v2);
double denom = d00 * d11 - d01 * d01;
R[0] = (d11 * d02 - d01 * d12) / denom;
R[1] = (d00 * d12 - d01 * d02) / denom;
R[2] = 1.0 - R[0] - R[1];
}
#elif defined(BARY_GEPP)
static void barycentric_coordinates(double* R, const double* P, const double* A, const double* B, const double* C){
double v0[3], v1[3], v2[3];
ntcd__vec3_sub(v0, B, A);
ntcd__vec3_sub(v1, C, A);
ntcd__vec3_sub(v2, P, A);
double d00 = ntcd__vec3_dot(v0, v0);
double d01 = ntcd__vec3_dot(v0, v1);
double d02 = ntcd__vec3_dot(v0, v2);
double d11 = ntcd__vec3_dot(v1, v1);
double d12 = ntcd__vec3_dot(v1, v2);
R[0] = (d00 * d12 - d01 * d02) / (d00 * d11 - d01 * d01);
R[1] = (d00 >= d01)? (d02 - d01 * R[0]) / d00: (d12 - d11 * R[0]) / d01;
//R[1] = (d00 >= d01)? d02 / d00 - (d01 / d00) * w: d12 / d01 - (d11 / d01) * w;
R[2] = 1.0 - R[0] - R[1];
}
#endif
typedef struct{
unsigned char bits_;
unsigned char last_sb_;
unsigned char size_;
double p_[3 * 4];
double a_[3 * 4];
double b_[3 * 4];
double max_vert2_;
}ntcd__simplex;
static void ntcd__simplex_initialize(ntcd__simplex* simplex){
simplex->bits_ = 0;
simplex->last_sb_ = 0;
simplex->size_ = 0;
simplex->max_vert2_ = 0.0;
}
//void ntcd__simplex_print(const ntcd__simplex* simplex){
// printf("yo\n");
// unsigned char bits = simplex->bits_;
// for(int i = 0; i < 4; ++i, bits >>= 1){
// if(bits & 1) printf("%d: %f, %f, %f\n", i, simplex->p_[3 * i + 0], simplex->p_[3 * i + 1], simplex->p_[3 * i + 2]);
// }
//}
static void ntcd__simplex_add_point(ntcd__simplex* simplex, const double* point){
unsigned char b = ~simplex->bits_; //Flip bits
b &= -b; //Last set (available) bit
unsigned char pos = s_pos[b]; //Get the bit position from the lookup table
simplex->last_sb_ = pos;
simplex->bits_ |= b; //Insert the new bit
++simplex->size_;
memcpy(simplex->p_ + 3 * pos, point, 3 * sizeof(*point));
double l2 = ntcd__vec3_length2(point);
if(l2 > simplex->max_vert2_) simplex->max_vert2_ = l2;
}
static void ntcd__simplex_add_point_with_info(ntcd__simplex* simplex, const double* point, const double* pa, const double* pb){
ntcd__simplex_add_point(simplex, point);
memcpy(simplex->a_ + 3 * simplex->last_sb_, pa, 3 * sizeof(*pa));
memcpy(simplex->b_ + 3 * simplex->last_sb_, pb, 3 * sizeof(*pb));
}
static void ntcd__simplex_remove_point(ntcd__simplex* simplex, int p){
simplex->bits_ ^= (1 << p); //Erase the bit at position p
--simplex->size_;
}
static int ntcd__simplex_contains(const ntcd__simplex* simplex, const double* point){
unsigned char bits = simplex->bits_;
for(int i = 0; i < 4; ++i, bits >>= 1){
if((bits & 1) && ntcd__vec3_equal(simplex->p_ + 3 * i, point)) return 1;
}
return 0;
}
static void ntcd__simplex_translate(ntcd__simplex* simplex, const double* dr){
//for(int k = 0; k < 4; ++k) p_[k] += dr;
simplex->max_vert2_ = 0.0;
unsigned char bits = simplex->bits_;
for(int i = 0; i < 4; ++i, bits >>= 1){
if(bits & 1){
ntcd__vec3_add(simplex->p_ + 3 * i, dr, simplex->p_ + 3 * i);
if(ntcd__vec3_length2(simplex->p_ + 3 * i) > simplex->max_vert2_) simplex->max_vert2_ = ntcd__vec3_length2(simplex->p_ + 3 * i);
}
}
}
static void ntcd__simplex_compute_closest_points(ntcd__simplex* simplex, double* pa, double* pb, const double* P){
switch(simplex->size_){
//IMPORTANT: We are having accuracy problems with this projection.
case 3:{
const unsigned char* pos = p_pos[(simplex->bits_ ^ (1 << simplex->last_sb_))];
const double* aA = simplex->a_ + 3 * simplex->last_sb_;
const double* aB = simplex->a_ + 3 * pos[0];
const double* aC = simplex->a_ + 3 * pos[1];
const double* bA = simplex->b_ + 3 * simplex->last_sb_;
const double* bB = simplex->b_ + 3 * pos[0];
const double* bC = simplex->b_ + 3 * pos[1];
const double* A = simplex->p_ + 3 * simplex->last_sb_;
const double* B = simplex->p_ + 3 * pos[0];
const double* C = simplex->p_ + 3 * pos[1];
double bary[3];
barycentric_coordinates(bary, P, A, B, C);
for(int i = 0; i < 3; ++i){
pa[i] = aA[i] * bary[0] + aB[i] * bary[1] + aC[i] * bary[2];
pb[i] = bA[i] * bary[0] + bB[i] * bary[1] + bC[i] * bary[2];
}
break;
}
case 2:{
const unsigned char* pos = p_pos[(simplex->bits_ ^ (1 << simplex->last_sb_))];
const double* aA = simplex->a_ + 3 * simplex->last_sb_;
const double* aB = simplex->a_ + 3 * pos[0];
const double* bA = simplex->b_ + 3 * simplex->last_sb_;
const double* bB = simplex->b_ + 3 * pos[0];
double u, v;
{
const double* A = simplex->p_ + 3 * simplex->last_sb_;
const double* B = simplex->p_ + 3 * pos[0];
{
double AB[3], AP[3];
ntcd__vec3_sub(AB, B, A);
ntcd__vec3_sub(AP, P, A);
v = ntcd__vec3_dot(AB, AP) / ntcd__vec3_length2(AB);
u = 1.0 - v;
}
}
for(int i = 0; i < 3; ++i){
pa[i] = aA[i] * u + aB[i] * v;
pb[i] = bA[i] * u + bB[i] * v;
}
break;
}
case 1:{
memcpy(pa, simplex->a_ + 3 * simplex->last_sb_, 3 * sizeof(*pa));
memcpy(pb, simplex->b_ + 3 * simplex->last_sb_, 3 * sizeof(*pb));
break;
}
default:
break;
}
}
//Reduce the simple to its feature closest to the origin and update the search direction, dir.
static void ntcd__simplex_closest(ntcd__simplex* simplex, double* dir){
switch(simplex->size_){
case 4:
{
const unsigned char* pos = p_pos[(simplex->bits_ ^ (1 << simplex->last_sb_))];
const double* a = simplex->p_ + 3 * simplex->last_sb_;
const double* b = simplex->p_ + 3 * pos[0];
const double* c = simplex->p_ + 3 * pos[1];
const double* d = simplex->p_ + 3 * pos[2];
double ab[3], ac[3], ad[3];
ntcd__vec3_sub(ab, b, a);
ntcd__vec3_sub(ac, c, a);
ntcd__vec3_sub(ad, d, a);
////////////////////* Vertex Case *///////////////////
double dot_aba = ntcd__vec3_dot(ab, a);
double dot_aca = ntcd__vec3_dot(ac, a);
double dot_ada = ntcd__vec3_dot(ad, a);
if(dot_aba >= 0.0 && dot_aca >= 0.0 && dot_ada >= 0.0){
memcpy(dir, a, 3 * sizeof(*dir)); //Take direction passing through origin
ntcd__simplex_remove_point(simplex, pos[0]);
ntcd__simplex_remove_point(simplex, pos[1]);
ntcd__simplex_remove_point(simplex, pos[2]);
break;
}
////////////////////* Edge Cases *///////////////////
/* ab Edge case */
double dot_abb = ntcd__vec3_dot(ab, b);
double dot_abPerp1 = dot_aba * ntcd__vec3_dot(ac, b) - dot_abb * dot_aca;
double dot_abPerp2 = dot_aba * ntcd__vec3_dot(ad, b) - dot_abb * dot_ada;
// The origin must be inside the space defined by the intersection
// of two half-space normal to the adjacent faces abc, abd
if(dot_abPerp1 <= 0.0 && dot_abPerp2 <= 0.0 && dot_aba <= 0.0){
double f = dot_aba / (dot_aba - dot_abb);
ntcd__vec3_fmadd(dir, f, ab, a);
ntcd__simplex_remove_point(simplex, pos[1]);
ntcd__simplex_remove_point(simplex, pos[2]);
break;
}
/* ac Edge case */
double dot_acc = ntcd__vec3_dot(ac, c);
double dot_acPerp1 = dot_aca * ntcd__vec3_dot(ad, c) - dot_acc * dot_ada;
double dot_acPerp2 = dot_aca * ntcd__vec3_dot(ab, c) - dot_acc * dot_aba;
// The origin must be inside the space defined by the intersection
// of two half-space normal to the adjacent faces abc, acd
if(dot_acPerp1 <= 0.0 && dot_acPerp2 <= 0.0 && dot_aca <= 0.0){
double f = dot_aca / (dot_aca - dot_acc);
ntcd__vec3_fmadd(dir, f, ac, a);
ntcd__simplex_remove_point(simplex, pos[0]);
ntcd__simplex_remove_point(simplex, pos[2]);
break;
}
/* ad Edge case */
double dot_add = ntcd__vec3_dot(ad, d);
double dot_adPerp1 = dot_ada * ntcd__vec3_dot(ab, d) - dot_add * dot_aba;
double dot_adPerp2 = dot_ada * ntcd__vec3_dot(ac, d) - dot_add * dot_aca;
// The origin must be inside the space defined by the intersection
// of two half-space normal to the adjacent faces acd, abd
if(dot_adPerp1 <= 0.0 && dot_adPerp2 <= 0.0 && dot_ada <= 0.0){
double f = dot_ada / (dot_ada - dot_add);
ntcd__vec3_fmadd(dir, f, ad, a);
ntcd__simplex_remove_point(simplex, pos[0]);
ntcd__simplex_remove_point(simplex, pos[1]);
break;
}
////////////////////* Face Cases *///////////////////
/* On abc side */
// The origin should be on abc's side and between the half-spaces defined by ac and ab (normal to abc)
{
double abxac[3];
ntcd__vec3_cross(abxac, ab, ac);
if(ntcd__vec3_dot(ad, abxac) * ntcd__vec3_dot(a, abxac) > 0.0 && dot_abPerp1 >= 0.0 && dot_acPerp2 >= 0.0){
/* Remove point d */
ntcd__simplex_remove_point(simplex, pos[2]);
ntcd__vec3_smul(dir, ntcd__vec3_dot(abxac, a) / ntcd__vec3_length2(abxac), abxac);
break;
}
}
/* On abd side */
// The origin should be on abd's side and between the half-spaces defined by ab and ad (normal to abd)
{
double abxad[3];
ntcd__vec3_cross(abxad, ab, ad);
if(ntcd__vec3_dot(ac, abxad) * ntcd__vec3_dot(a, abxad) > 0.0 && dot_abPerp2 >= 0.0 && dot_adPerp1 >= 0.0){
/* Remove point c */
ntcd__simplex_remove_point(simplex, pos[1]);
ntcd__vec3_smul(dir, ntcd__vec3_dot(abxad, a) / ntcd__vec3_length2(abxad), abxad);
break;
}
}
///* On acd side */
// The origin should be on acd's side and between the half-spaces defined by ac and ad (normal to acd)
{
double acxad[3];
ntcd__vec3_cross(acxad, ac, ad);
if(ntcd__vec3_dot(ab, acxad) * ntcd__vec3_dot(a, acxad) > 0.0 && dot_acPerp1 >= 0.0 && dot_adPerp2 >= 0.0){
/* Remove point b */
ntcd__simplex_remove_point(simplex, pos[0]);
ntcd__vec3_smul(dir, ntcd__vec3_dot(acxad, a) / ntcd__vec3_length2(acxad), acxad);
break;
}
}
///* On bcd side */
//// The origin should be on bcd's side
{
double bc[3], bd[3], bcxbd[3];
ntcd__vec3_sub(bc, c, b);
ntcd__vec3_sub(bd, d, b);
ntcd__vec3_cross(bcxbd, bc, bd);
if(ntcd__vec3_dot(bcxbd, ab) * ntcd__vec3_dot(bcxbd, b) < 0.0){
/* Remove point a */
ntcd__simplex_remove_point(simplex, simplex->last_sb_);
simplex->last_sb_ = pos[0];
ntcd__vec3_smul(dir, ntcd__vec3_dot(bcxbd, b) / ntcd__vec3_length2(bcxbd), bcxbd);
break;
}
}
/* 'else' should only be when the origin is inside the tetrahedron */
break;
}
case 3:
{
const unsigned char* pos = p_pos[(simplex->bits_ ^ (1 << simplex->last_sb_))];
const double* a = simplex->p_ + 3 * simplex->last_sb_;
const double* b = simplex->p_ + 3 * pos[0];
const double* c = simplex->p_ + 3 * pos[1];
double ab[3], ac[3];
ntcd__vec3_sub(ab, b, a);
ntcd__vec3_sub(ac, c, a);
// Check if O in vertex region A
double dot_aba = -ntcd__vec3_dot(ab, a);
double dot_aca = -ntcd__vec3_dot(ac, a);
if(dot_aba <= 0.0 && dot_aca <= 0.0){
memcpy(dir, a, 3 * sizeof(*dir)); //Take direction passing through origin
ntcd__simplex_remove_point(simplex, pos[0]);
ntcd__simplex_remove_point(simplex, pos[1]);
break;
}
// Check if O in edge region AB
double dot_abb = -ntcd__vec3_dot(ab, b);
double dot_acb = -ntcd__vec3_dot(ac, b);
double vc = dot_aba * dot_acb - dot_abb * dot_aca;
if(vc <= 0.0 && dot_aba >= 0.0 && dot_abb <= 0.0){
double f = dot_aba / (dot_aba - dot_abb);
ntcd__vec3_fmadd(dir, f, ab, a);
/* Remove Point c */
ntcd__simplex_remove_point(simplex, pos[1]);
break;
}
// Check if O in edge region AC
double dot_abc = -ntcd__vec3_dot(ab, c);
double dot_acc = -ntcd__vec3_dot(ac, c);
double vb = dot_abc * dot_aca - dot_aba * dot_acc;
if(vb <= 0.0 && dot_aca >= 0.0 && dot_acc <= 0.0){
double f = dot_aca / (dot_aca - dot_acc);
ntcd__vec3_fmadd(dir, f, ac, a);
/* Remove Point b */
ntcd__simplex_remove_point(simplex, pos[0]);
break;
}
double va = dot_abb * dot_acc - dot_abc * dot_acb;
double w = 1.0 / (va + vb + vc);
for(int i = 0; i < 3; ++i) dir[i] = a[i] + (ab[i] * vb + ac[i] * vc) * w;
break;
}
case 2:
{
const unsigned char* pos = p_pos[(simplex->bits_ ^ (1 << simplex->last_sb_))];
const double* a = simplex->p_ + 3 * simplex->last_sb_;
const double* b = simplex->p_ + 3 * pos[0];
double ab[3];
ntcd__vec3_sub(ab, b, a);
double t = -ntcd__vec3_dot(ab, a);
if(t <= 0.0){
memcpy(dir, a, 3 * sizeof(*dir)); //Take direction passing through origin
ntcd__simplex_remove_point(simplex, pos[0]);
break;
}
double denom = ntcd__vec3_length2(ab);
if(t >= denom){
ntcd__simplex_remove_point(simplex, simplex->last_sb_);
simplex->last_sb_ = pos[0];
memcpy(dir, b, 3 * sizeof(*dir));
break;
}
ntcd__vec3_fmadd(dir, t / denom, ab, a);
break;
}
case 1:
{
memcpy(dir, simplex->p_ + 3 * simplex->last_sb_, 3 * sizeof(*dir));
break;
}
default: break;
}
}
//TODO: dir -> -dir
//Check if the origin is contained in the simplex and update the search direction, dir.
static int ntcd__simplex_contains_origin(ntcd__simplex* simplex, double* dir){
///////////////////////////////////////////////
switch(simplex->size_){
case 4:
{
const unsigned char* pos = p_pos[(simplex->bits_ ^ (1 << simplex->last_sb_))];
const double* a = simplex->p_ + 3 * simplex->last_sb_;
const double* b = simplex->p_ + 3 * pos[0];
const double* c = simplex->p_ + 3 * pos[1];
const double* d = simplex->p_ + 3 * pos[2];
double ab[3], ac[3], ad[3];
ntcd__vec3_sub(ab, b, a);
ntcd__vec3_sub(ac, c, a);
ntcd__vec3_sub(ad, d, a);
////////////////////* Face Cases *///////////////////
/* On abc side */
double abxac[3];
ntcd__vec3_cross(abxac, ab, ac);
int abPerp1Pos, acPerp2Pos;
{
double cross_temp_a[3], cross_temp_b[3];
ntcd__vec3_cross(cross_temp_a, abxac, ab);
ntcd__vec3_cross(cross_temp_b, ac, abxac);
abPerp1Pos = (ntcd__vec3_dot(cross_temp_a, a) > 0.0);
acPerp2Pos = (ntcd__vec3_dot(cross_temp_b, a) > 0.0);
}
// The origin should be on abc's side and between the half-spaces defined by ac and ab (normal to abc)
{
double abcPerp[3];
if(ntcd__vec3_dot(abxac, ad) > 0.0) ntcd__vec3_smul(abcPerp, -1.0, abxac);
else memcpy(abcPerp, abxac, 3 * sizeof(*abcPerp));
if((ntcd__vec3_dot(abcPerp, a) < 0.0) && !abPerp1Pos && !acPerp2Pos){
/* Remove point d */
ntcd__simplex_remove_point(simplex, pos[2]);
memcpy(dir, abcPerp, 3 * sizeof(*dir));
break;
}
}
/* On abd side */
double abxad[3];
ntcd__vec3_cross(abxad, ab, ad);
int abPerp2Pos, adPerp1Pos;
{
double cross_temp_a[3], cross_temp_b[3];
ntcd__vec3_cross(cross_temp_a, abxad, ab);
ntcd__vec3_cross(cross_temp_b, ad, abxad);
abPerp2Pos = (ntcd__vec3_dot(cross_temp_a, a) > 0.0);
adPerp1Pos = (ntcd__vec3_dot(cross_temp_b, a) > 0.0);
}
// The origin should be on abd's side and between the half-spaces defined by ab and ad (normal to abd)
{
double abdPerp[3];
if(ntcd__vec3_dot(abxad, ac) > 0.0) ntcd__vec3_smul(abdPerp, -1.0, abxad);
else memcpy(abdPerp, abxad, 3 * sizeof(*abdPerp));
if((ntcd__vec3_dot(abdPerp, a) < 0.0) && !abPerp2Pos && !adPerp1Pos){
/* Remove point c */
ntcd__simplex_remove_point(simplex, pos[1]);
memcpy(dir, abdPerp, 3 * sizeof(*dir));
break;
}
}
/* On acd side */
double acxad[3];
ntcd__vec3_cross(acxad, ac, ad);
int acPerp1Pos, adPerp2Pos;
{
double cross_temp_a[3], cross_temp_b[3];
ntcd__vec3_cross(cross_temp_a, acxad, ac);
ntcd__vec3_cross(cross_temp_b, ad, acxad);
acPerp1Pos = (ntcd__vec3_dot(cross_temp_a, a) > 0.0);
adPerp2Pos = (ntcd__vec3_dot(cross_temp_b, a) > 0.0);
}
// The origin should be on acd's side and between the half-spaces defined by ac and ad (normal to acd)
{
double acdPerp[3];
if(ntcd__vec3_dot(acxad, ab) > 0.0) ntcd__vec3_smul(acdPerp, -1.0, acxad);
else memcpy(acdPerp, acxad, 3 * sizeof(*acdPerp));
if((ntcd__vec3_dot(acdPerp, a) < 0.0) && !acPerp1Pos && !adPerp2Pos){
/* Remove point b */
ntcd__simplex_remove_point(simplex, pos[0]);
memcpy(dir, acdPerp, 3 * sizeof(*dir));
break;
}
}
////////////////////* Edge Cases *///////////////////
/* ab Edge case */
// The origin must be inside the space defined by the intersection
// of two half-space normal to the adjacent faces abc, abd
if(abPerp1Pos && abPerp2Pos){
ntcd__vec3_triple_product(dir, a, ab, ab);
ntcd__simplex_remove_point(simplex, pos[1]);
ntcd__simplex_remove_point(simplex, pos[2]);
break;
}
/* ac Edge case */
// The origin must be inside the space defined by the intersection