forked from MattiaMontanari/openGJK
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenGJK.c
More file actions
1283 lines (1135 loc) · 46.8 KB
/
Copy pathopenGJK.c
File metadata and controls
1283 lines (1135 loc) · 46.8 KB
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
// _____ _ _ __ //
// / ____| | | |/ / //
// ___ _ __ ___ _ __ | | __ | | ' / //
// / _ \| '_ \ / _ \ '_ \| | |_ |_ | | < //
// | (_) | |_) | __/ | | | |__| | |__| | . \ //
// \___/| .__/ \___|_| |_|\_____|\____/|_|\_\ //
// | | //
// |_| //
// //
// Copyright 2022 Mattia Montanari, University of Oxford //
// //
// This program is free software: you can redistribute it and/or modify it under //
// the terms of the GNU General Public License as published by the Free Software //
// Foundation, either version 3 of the License. You should have received a copy //
// of the GNU General Public License along with this program. If not, visit //
// //
// https://www.gnu.org/licenses/ //
// //
// This program is distributed in the hope that it will be useful, but WITHOUT //
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS //
// FOR A PARTICULAR PURPOSE. See GNU General Public License for details. //
/**
* @file openGJK.c
* @author Mattia Montanari
* @date 1 Jan 2022
* @brief Source of OpenGJK and its fast sub-algorithm.
*
* @see https://www.mattiamontanari.com/opengjk/
*/
#include "openGJK/openGJK.h"
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
/** If instricuted, compile a mex function for Matlab. */
#ifdef MATLAB_MEX_BUILD
#include "mex.h"
#else
#define mexPrintf printf
#endif
#define eps_rel22 (gkFloat) gkEpsilon * 1e4f
#define eps_tot22 (gkFloat) gkEpsilon * 1e2f
#define norm2(a) (a[0] * a[0] + a[1] * a[1] + a[2] * a[2])
#define dotProduct(a, b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])
#define S3Dregion1234() \
v[0] = 0; \
v[1] = 0; \
v[2] = 0; \
s->nvrtx = 4;
#define select_1ik() \
s->nvrtx = 3; \
for (t = 0; t < 3; t++) \
s->vrtx[2][t] = s->vrtx[3][t]; \
for (t = 0; t < 2; t++) \
s->vrtx_idx[2][t] = s->vrtx_idx[3][t]; \
for (t = 0; t < 3; t++) \
s->vrtx[1][t] = si[t]; \
for (t = 0; t < 2; t++) \
s->vrtx_idx[1][t] = si_idx[t]; \
for (t = 0; t < 3; t++) \
s->vrtx[0][t] = sk[t]; \
for (t = 0; t < 2; t++) \
s->vrtx_idx[0][t] = sk_idx[t];
#define select_1ij() \
s->nvrtx = 3; \
for (t = 0; t < 3; t++) \
s->vrtx[2][t] = s->vrtx[3][t]; \
for (t = 0; t < 2; t++) \
s->vrtx_idx[2][t] = s->vrtx_idx[3][t]; \
for (t = 0; t < 3; t++) \
s->vrtx[1][t] = si[t]; \
for (t = 0; t < 2; t++) \
s->vrtx_idx[1][t] = si_idx[t]; \
for (t = 0; t < 3; t++) \
s->vrtx[0][t] = sj[t]; \
for (t = 0; t < 2; t++) \
s->vrtx_idx[0][t] = sj_idx[t];
#define select_1jk() \
s->nvrtx = 3; \
for (t = 0; t < 3; t++) \
s->vrtx[2][t] = s->vrtx[3][t]; \
for (t = 0; t < 2; t++) \
s->vrtx_idx[2][t] = s->vrtx_idx[3][t]; \
for (t = 0; t < 3; t++) \
s->vrtx[1][t] = sj[t]; \
for (t = 0; t < 2; t++) \
s->vrtx_idx[1][t] = sj_idx[t]; \
for (t = 0; t < 3; t++) \
s->vrtx[0][t] = sk[t]; \
for (t = 0; t < 2; t++) \
s->vrtx_idx[0][t] = sk_idx[t];
#define select_1i() \
s->nvrtx = 2; \
for (t = 0; t < 3; t++) \
s->vrtx[1][t] = s->vrtx[3][t]; \
for (t = 0; t < 2; t++) \
s->vrtx_idx[1][t] = s->vrtx_idx[3][t]; \
for (t = 0; t < 3; t++) \
s->vrtx[0][t] = si[t]; \
for (t = 0; t < 2; t++) \
s->vrtx_idx[0][t] = si_idx[t];
#define select_1j() \
s->nvrtx = 2; \
for (t = 0; t < 3; t++) \
s->vrtx[1][t] = s->vrtx[3][t]; \
for (t = 0; t < 2; t++) \
s->vrtx_idx[1][t] = s->vrtx_idx[3][t]; \
for (t = 0; t < 3; t++) \
s->vrtx[0][t] = sj[t]; \
for (t = 0; t < 2; t++) \
s->vrtx_idx[0][t] = sj_idx[t];
#define select_1k() \
s->nvrtx = 2; \
for (t = 0; t < 3; t++) \
s->vrtx[1][t] = s->vrtx[3][t]; \
for (t = 0; t < 2; t++) \
s->vrtx_idx[1][t] = s->vrtx_idx[3][t]; \
for (t = 0; t < 3; t++) \
s->vrtx[0][t] = sk[t]; \
for (t = 0; t < 2; t++) \
s->vrtx_idx[0][t] = sk_idx[t];
#define getvrtx(point, location) \
point[0] = s->vrtx[location][0]; \
point[1] = s->vrtx[location][1]; \
point[2] = s->vrtx[location][2];
#define getvrtxidx(point, index, location) \
point[0] = s->vrtx[location][0]; \
point[1] = s->vrtx[location][1]; \
point[2] = s->vrtx[location][2]; \
index[0] = s->vrtx_idx[location][0]; \
index[1] = s->vrtx_idx[location][1];
#define calculateEdgeVector(p1p2, p2) \
p1p2[0] = p2[0] - s->vrtx[3][0]; \
p1p2[1] = p2[1] - s->vrtx[3][1]; \
p1p2[2] = p2[2] - s->vrtx[3][2];
#define S1Dregion1() \
v[0] = s->vrtx[1][0]; \
v[1] = s->vrtx[1][1]; \
v[2] = s->vrtx[1][2]; \
s->nvrtx = 1; \
s->vrtx[0][0] = s->vrtx[1][0]; \
s->vrtx[0][1] = s->vrtx[1][1]; \
s->vrtx[0][2] = s->vrtx[1][2]; \
s->vrtx_idx[0][0] = s->vrtx_idx[1][0]; \
s->vrtx_idx[0][1] = s->vrtx_idx[1][1];
#define S2Dregion1() \
v[0] = s->vrtx[2][0]; \
v[1] = s->vrtx[2][1]; \
v[2] = s->vrtx[2][2]; \
s->nvrtx = 1; \
s->vrtx[0][0] = s->vrtx[2][0]; \
s->vrtx[0][1] = s->vrtx[2][1]; \
s->vrtx[0][2] = s->vrtx[2][2]; \
s->vrtx_idx[0][0] = s->vrtx_idx[2][0]; \
s->vrtx_idx[0][1] = s->vrtx_idx[2][1];
#define S2Dregion12() \
s->nvrtx = 2; \
s->vrtx[0][0] = s->vrtx[2][0]; \
s->vrtx[0][1] = s->vrtx[2][1]; \
s->vrtx[0][2] = s->vrtx[2][2]; \
s->vrtx_idx[0][0] = s->vrtx_idx[2][0]; \
s->vrtx_idx[0][1] = s->vrtx_idx[2][1];
#define S2Dregion13() \
s->nvrtx = 2; \
s->vrtx[1][0] = s->vrtx[2][0]; \
s->vrtx[1][1] = s->vrtx[2][1]; \
s->vrtx[1][2] = s->vrtx[2][2]; \
s->vrtx_idx[1][0] = s->vrtx_idx[2][0]; \
s->vrtx_idx[1][1] = s->vrtx_idx[2][1];
#define S3Dregion1() \
v[0] = s1[0]; \
v[1] = s1[1]; \
v[2] = s1[2]; \
s->nvrtx = 1; \
s->vrtx[0][0] = s1[0]; \
s->vrtx[0][1] = s1[1]; \
s->vrtx[0][2] = s1[2]; \
s->vrtx_idx[0][0] = s1_idx[0]; \
s->vrtx_idx[0][1] = s1_idx[1];
inline static gkFloat
determinant(const gkFloat* restrict p, const gkFloat* restrict q, const gkFloat* restrict r) {
return p[0] * ((q[1] * r[2]) - (r[1] * q[2])) - p[1] * (q[0] * r[2] - r[0] * q[2])
+ p[2] * (q[0] * r[1] - r[0] * q[1]);
}
inline static void
crossProduct(const gkFloat* restrict a, const gkFloat* restrict b, gkFloat* restrict c) {
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];
}
inline static void
projectOnLine(const gkFloat* restrict p, const gkFloat* restrict q, gkFloat* restrict v) {
gkFloat pq[3];
pq[0] = p[0] - q[0];
pq[1] = p[1] - q[1];
pq[2] = p[2] - q[2];
const gkFloat tmp = dotProduct(p, pq) / dotProduct(pq, pq);
for (int i = 0; i < 3; i++) {
v[i] = p[i] - pq[i] * tmp;
}
}
inline static void
projectOnPlane(const gkFloat* restrict p, const gkFloat* restrict q, const gkFloat* restrict r, gkFloat* restrict v) {
gkFloat n[3], pq[3], pr[3];
for (int i = 0; i < 3; i++) {
pq[i] = p[i] - q[i];
}
for (int i = 0; i < 3; i++) {
pr[i] = p[i] - r[i];
}
crossProduct(pq, pr, n);
const gkFloat tmp = dotProduct(n, p) / dotProduct(n, n);
for (int i = 0; i < 3; i++) {
v[i] = n[i] * tmp;
}
}
inline static int
hff1(const gkFloat* restrict p, const gkFloat* restrict q) {
gkFloat tmp = 0;
for (int i = 0; i < 3; i++) {
tmp += (p[i] * p[i] - p[i] * q[i]);
}
if (tmp > 0) {
return 1; // keep q
}
return 0;
}
inline static int
hff2(const gkFloat* restrict p, const gkFloat* restrict q, const gkFloat* restrict r) {
gkFloat ntmp[3];
gkFloat n[3], pq[3], pr[3];
for (int i = 0; i < 3; i++) {
pq[i] = q[i] - p[i];
}
for (int i = 0; i < 3; i++) {
pr[i] = r[i] - p[i];
}
crossProduct(pq, pr, ntmp);
crossProduct(pq, ntmp, n);
return dotProduct(p, n) < 0; // Discard r if true
}
inline static int
hff3(const gkFloat* restrict p, const gkFloat* restrict q, const gkFloat* restrict r) {
gkFloat n[3], pq[3], pr[3];
for (int i = 0; i < 3; i++) {
pq[i] = q[i] - p[i];
}
for (int i = 0; i < 3; i++) {
pr[i] = r[i] - p[i];
}
crossProduct(pq, pr, n);
return dotProduct(p, n) <= 0; // discard s if true
}
inline static void
S1D(gkSimplex* s, gkFloat* v) {
const gkFloat* restrict s1p = s->vrtx[1];
const gkFloat* restrict s2p = s->vrtx[0];
if (hff1(s1p, s2p)) {
projectOnLine(s1p, s2p, v); // Update v, no need to update s
return; // Return V{1,2}
} else {
S1Dregion1(); // Update v and s
return; // Return V{1}
}
}
inline static void
S2D(gkSimplex* s, gkFloat* v) {
const gkFloat* s1p = s->vrtx[2];
const gkFloat* s2p = s->vrtx[1];
const gkFloat* s3p = s->vrtx[0];
const int hff1f_s12 = hff1(s1p, s2p);
const int hff1f_s13 = hff1(s1p, s3p);
if (hff1f_s12) {
const int hff2f_23 = !hff2(s1p, s2p, s3p);
if (hff2f_23) {
if (hff1f_s13) {
const int hff2f_32 = !hff2(s1p, s3p, s2p);
if (hff2f_32) {
projectOnPlane(s1p, s2p, s3p, v); // Update s, no need to update c
return; // Return V{1,2,3}
} else {
projectOnLine(s1p, s3p, v); // Update v
S2Dregion13(); // Update s
return; // Return V{1,3}
}
} else {
projectOnPlane(s1p, s2p, s3p, v); // Update s, no need to update c
return; // Return V{1,2,3}
}
} else {
projectOnLine(s1p, s2p, v); // Update v
S2Dregion12(); // Update s
return; // Return V{1,2}
}
} else if (hff1f_s13) {
const int hff2f_32 = !hff2(s1p, s3p, s2p);
if (hff2f_32) {
projectOnPlane(s1p, s2p, s3p, v); // Update s, no need to update v
return; // Return V{1,2,3}
} else {
projectOnLine(s1p, s3p, v); // Update v
S2Dregion13(); // Update s
return; // Return V{1,3}
}
} else {
S2Dregion1(); // Update s and v
return; // Return V{1}
}
}
inline static void
S3D(gkSimplex* s, gkFloat* v) {
gkFloat s1[3], s2[3], s3[3], s4[3], s1s2[3], s1s3[3], s1s4[3];
gkFloat si[3], sj[3], sk[3];
int s1_idx[2], s2_idx[2], s3_idx[2];
int si_idx[2], sj_idx[2], sk_idx[2];
int testLineThree, testLineFour, testPlaneTwo, testPlaneThree, testPlaneFour, dotTotal;
int i, j, k, t;
getvrtxidx(s1, s1_idx, 3);
getvrtxidx(s2, s2_idx, 2);
getvrtxidx(s3, s3_idx, 1);
getvrtx(s4, 0);
calculateEdgeVector(s1s2, s2);
calculateEdgeVector(s1s3, s3);
calculateEdgeVector(s1s4, s4);
int hff1_tests[3];
hff1_tests[2] = hff1(s1, s2);
hff1_tests[1] = hff1(s1, s3);
hff1_tests[0] = hff1(s1, s4);
testLineThree = hff1(s1, s3);
testLineFour = hff1(s1, s4);
dotTotal = hff1(s1, s2) + testLineThree + testLineFour;
if (dotTotal == 0) { /* case 0.0 -------------------------------------- */
S3Dregion1();
return;
}
const gkFloat det134 = determinant(s1s3, s1s4, s1s2);
const int sss = (det134 <= 0);
testPlaneTwo = hff3(s1, s3, s4) - sss;
testPlaneTwo = testPlaneTwo * testPlaneTwo;
testPlaneThree = hff3(s1, s4, s2) - sss;
testPlaneThree = testPlaneThree * testPlaneThree;
testPlaneFour = hff3(s1, s2, s3) - sss;
testPlaneFour = testPlaneFour * testPlaneFour;
switch (testPlaneTwo + testPlaneThree + testPlaneFour) {
case 3:
S3Dregion1234();
break;
case 2:
// Only one facing the oring
// 1,i,j, are the indices of the points on the triangle and remove k from
// simplex
s->nvrtx = 3;
if (!testPlaneTwo)
{ // k = 2; removes s2
for (i = 0; i < 3; i++)
{
s->vrtx[2][i] = s->vrtx[3][i];
}
for (i = 0; i < 2; i++)
{
s->vrtx_idx[2][i] = s->vrtx_idx[3][i];
}
}
else if (!testPlaneThree)
{ // k = 1; // removes s3
for (i = 0; i < 3; i++)
{
s->vrtx[1][i] = s2[i];
s->vrtx[2][i] = s->vrtx[3][i];
}
for (i = 0; i < 2; i++)
{
s->vrtx_idx[1][i] = s2_idx[i];
s->vrtx_idx[2][i] = s->vrtx_idx[3][i];
}
}
else if (!testPlaneFour)
{ // k = 0; // removes s4 and no need to reorder
for (i = 0; i < 3; i++)
{
s->vrtx[0][i] = s3[i];
s->vrtx[1][i] = s2[i];
s->vrtx[2][i] = s->vrtx[3][i];
}
for (i = 0; i < 2; i++)
{
s->vrtx_idx[0][i] = s3_idx[i];
s->vrtx_idx[1][i] = s2_idx[i];
s->vrtx_idx[2][i] = s->vrtx_idx[3][i];
}
}
// Call S2D
S2D(s, v);
break;
case 1:
// Two triangles face the origins:
// The only positive hff3 is for triangle 1,i,j, therefore k must be in
// the solution as it supports the the point of minimum norm.
// 1,i,j, are the indices of the points on the triangle and remove k from
// simplex
s->nvrtx = 3;
if (testPlaneTwo) {
k = 2; // s2
i = 1;
j = 0;
} else if (testPlaneThree) {
k = 1; // s3
i = 0;
j = 2;
} else {
k = 0; // s4
i = 2;
j = 1;
}
getvrtxidx(si, si_idx, i);
getvrtxidx(sj, sj_idx, j);
getvrtxidx(sk, sk_idx, k);
if (dotTotal == 1) {
if (hff1_tests[k]) {
if (!hff2(s1, sk, si)) {
select_1ik();
projectOnPlane(s1, si, sk, v);
} else if (!hff2(s1, sk, sj)) {
select_1jk();
projectOnPlane(s1, sj, sk, v);
} else {
select_1k(); // select region 1i
projectOnLine(s1, sk, v);
}
} else if (hff1_tests[i]) {
if (!hff2(s1, si, sk)) {
select_1ik();
projectOnPlane(s1, si, sk, v);
} else {
select_1i(); // select region 1i
projectOnLine(s1, si, v);
}
} else {
if (!hff2(s1, sj, sk)) {
select_1jk();
projectOnPlane(s1, sj, sk, v);
} else {
select_1j(); // select region 1i
projectOnLine(s1, sj, v);
}
}
} else if (dotTotal == 2) {
// Two edges have positive hff1, meaning that for two edges the origin's
// project fall on the segement.
// Certainly the edge 1,k supports the the point of minimum norm, and so
// hff1_1k is positive
if (hff1_tests[i]) {
if (!hff2(s1, sk, si)) {
if (!hff2(s1, si, sk)) {
select_1ik(); // select region 1ik
projectOnPlane(s1, si, sk, v);
} else {
select_1k(); // select region 1k
projectOnLine(s1, sk, v);
}
} else {
if (!hff2(s1, sk, sj)) {
select_1jk(); // select region 1jk
projectOnPlane(s1, sj, sk, v);
} else {
select_1k(); // select region 1k
projectOnLine(s1, sk, v);
}
}
} else if (hff1_tests[j]) { // there is no other choice
if (!hff2(s1, sk, sj)) {
if (!hff2(s1, sj, sk)) {
select_1jk(); // select region 1jk
projectOnPlane(s1, sj, sk, v);
} else {
select_1j(); // select region 1j
projectOnLine(s1, sj, v);
}
} else {
if (!hff2(s1, sk, si)) {
select_1ik(); // select region 1ik
projectOnPlane(s1, si, sk, v);
} else {
select_1k(); // select region 1k
projectOnLine(s1, sk, v);
}
}
} else {
// ERROR;
}
} else if (dotTotal == 3) {
// MM : ALL THIS HYPHOTESIS IS FALSE
// sk is s.t. hff3 for sk < 0. So, sk must support the origin because
// there are 2 triangles facing the origin.
int hff2_ik = hff2(s1, si, sk);
int hff2_jk = hff2(s1, sj, sk);
int hff2_ki = hff2(s1, sk, si);
int hff2_kj = hff2(s1, sk, sj);
if (hff2_ki == 0 && hff2_kj == 0) {
mexPrintf("\n\n UNEXPECTED VALUES!!! \n\n");
}
if (hff2_ki == 1 && hff2_kj == 1) {
select_1k();
projectOnLine(s1, sk, v);
} else if (hff2_ki) {
// discard i
if (hff2_jk) {
// discard k
select_1j();
projectOnLine(s1, sj, v);
} else {
select_1jk();
projectOnPlane(s1, sk, sj, v);
}
} else {
// discard j
if (hff2_ik) {
// discard k
select_1i();
projectOnLine(s1, si, v);
} else {
select_1ik();
projectOnPlane(s1, sk, si, v);
}
}
}
break;
case 0:
// The origin is outside all 3 triangles
if (dotTotal == 1) {
// Here si is set such that hff(s1,si) > 0
if (testLineThree) {
k = 2;
i = 1; // s3
j = 0;
} else if (testLineFour) {
k = 1; // s3
i = 0;
j = 2;
} else {
k = 0;
i = 2; // s2
j = 1;
}
getvrtxidx(si, si_idx, i);
getvrtxidx(sj, sj_idx, j);
getvrtxidx(sk, sk_idx, k);
if (!hff2(s1, si, sj)) {
select_1ij();
projectOnPlane(s1, si, sj, v);
} else if (!hff2(s1, si, sk)) {
select_1ik();
projectOnPlane(s1, si, sk, v);
} else {
select_1i();
projectOnLine(s1, si, v);
}
} else if (dotTotal == 2) {
// Here si is set such that hff(s1,si) < 0
s->nvrtx = 3;
if (!testLineThree) {
k = 2;
i = 1; // s3
j = 0;
} else if (!testLineFour) {
k = 1;
i = 0; // s4
j = 2;
} else {
k = 0;
i = 2; // s2
j = 1;
}
getvrtxidx(si, si_idx, i);
getvrtxidx(sj, sj_idx, j);
getvrtxidx(sk, sk_idx, k);
if (!hff2(s1, sj, sk)) {
if (!hff2(s1, sk, sj)) {
select_1jk(); // select region 1jk
projectOnPlane(s1, sj, sk, v);
} else if (!hff2(s1, sk, si)) {
select_1ik();
projectOnPlane(s1, sk, si, v);
} else {
select_1k();
projectOnLine(s1, sk, v);
}
} else if (!hff2(s1, sj, si)) {
select_1ij();
projectOnPlane(s1, si, sj, v);
} else {
select_1j();
projectOnLine(s1, sj, v);
}
}
break;
default:
mexPrintf("\nERROR:\tunhandled");
}
}
inline static void
support(gkPolytope* restrict body, const gkFloat* restrict v) {
gkFloat s, maxs;
gkFloat* vrt;
int better = -1;
maxs = dotProduct(body->s, v);
for (int i = 0; i < body->numpoints; ++i) {
vrt = body->coord[i];
s = dotProduct(vrt, v);
if (s > maxs) {
maxs = s;
better = i;
}
}
if (better != -1) {
body->s[0] = body->coord[better][0];
body->s[1] = body->coord[better][1];
body->s[2] = body->coord[better][2];
body->s_idx = better;
}
}
inline static void
subalgorithm(gkSimplex* s, gkFloat* v) {
switch (s->nvrtx) {
case 4:
S3D(s, v);
break;
case 3:
S2D(s, v);
break;
case 2:
S1D(s, v);
break;
default:
mexPrintf("\nERROR:\t invalid simplex\n");
}
}
inline static void W0D(const gkPolytope *bd1, const gkPolytope *bd2, gkSimplex *smp)
{
const gkFloat *w00 = bd1->coord[smp->vrtx_idx[0][0]];
const gkFloat *w01 = bd2->coord[smp->vrtx_idx[0][1]];
for (int t = 0; t < 3; t++)
{
smp->witnesses[0][t] = w00[t];
smp->witnesses[1][t] = w01[t];
}
}
inline static void W1D(const gkPolytope *bd1, const gkPolytope *bd2, gkSimplex *smp)
{
gkFloat pq[3], po[3];
const gkFloat *p = smp->vrtx[0];
const gkFloat *q = smp->vrtx[1];
for (int t = 0; t < 3; t++)
{
pq[t] = q[t] - p[t];
po[t] = -p[t];
}
// Compute barycentric coordinates via matrix inversion
// (in the linear case the matrix is 1x1 thus simplified)
const gkFloat det = dotProduct(pq, pq);
if(det == 0.0){
// Degenerate case
W0D(bd1, bd2, smp);
}
const gkFloat a1 = dotProduct(pq, po) / det;
const gkFloat a0 = 1.0 - a1;
// Compute witness points
const gkFloat *w00 = bd1->coord[smp->vrtx_idx[0][0]];
const gkFloat *w01 = bd2->coord[smp->vrtx_idx[0][1]];
const gkFloat *w10 = bd1->coord[smp->vrtx_idx[1][0]];
const gkFloat *w11 = bd2->coord[smp->vrtx_idx[1][1]];
for (int t = 0; t < 3; t++)
{
smp->witnesses[0][t] = w00[t] * a0 + w10[t] * a1;
smp->witnesses[1][t] = w01[t] * a0 + w11[t] * a1;
}
}
inline static void W2D(const gkPolytope* bd1, const gkPolytope* bd2, gkSimplex* smp)
{
gkFloat pq[3], pr[3], po[3];
const gkFloat* p = smp->vrtx[0];
const gkFloat* q = smp->vrtx[1];
const gkFloat* r = smp->vrtx[2];
for(int t=0; t < 3; t++){
pq[t] = q[t] - p[t];
pr[t] = r[t] - p[t];
po[t] = -p[t];
}
/**
* Compute barycentric coordinates via matrix inversion
* Given the points $P$, $Q$, and $R$ forming a triangle
* we want to find the barycentric coordinates of the origin
* projected onto the triangle. We can do this
* by inverting $\mathbf{T}$ in the linear equation below:
*
* \begin{align*}
* \mathbf{T}
* \begin{bmatrix}
* \lambda_q \\
* \lambda_r
* \end{bmatrix} &= \begin{bmatrix}
* \overrightarrow{PQ}\cdot\overrightarrow{PO} \\
* \overrightarrow{PR}\cdot\overrightarrow{PO}
* \end{bmatrix} \\
* \lambda_p &= 1 - \lambda_q - \lambda_r \\
* \mathbf{T} &= \begin{bmatrix}
* \overrightarrow{PQ}\cdot\overrightarrow{PQ} & \overrightarrow{PR}\cdot\overrightarrow{PQ} \\
* \overrightarrow{PR}\cdot\overrightarrow{PQ} & \overrightarrow{PR}\cdot\overrightarrow{PR}
* \end{bmatrix}
* \end{align*}
*/
const gkFloat T00 = dotProduct(pq, pq);
const gkFloat T01 = dotProduct(pq, pr);
const gkFloat T11 = dotProduct(pr, pr);
const gkFloat det = T00 * T11 - T01 * T01;
if(det == 0.0){
// Degenerate case
W1D(bd1, bd2, smp);
}
const gkFloat b0 = dotProduct(pq, po);
const gkFloat b1 = dotProduct(pr, po);
const gkFloat I00 = T11 / det;
const gkFloat I01 = -T01 / det;
const gkFloat I11 = T00 / det;
const gkFloat a1 = I00 * b0 + I01 * b1;
const gkFloat a2 = I01 * b0 + I11 * b1;
const gkFloat a0 = 1.0 - a1 - a2;
// check if the origin is very close to one of the edges of the
// simplex. In this case, a 1D projection will be more accurate.
if (a0 < gkEpsilon)
{
smp->nvrtx = 2;
smp->vrtx[0][0] = smp->vrtx[2][0];
smp->vrtx[0][1] = smp->vrtx[2][1];
smp->vrtx[0][2] = smp->vrtx[2][2];
smp->vrtx_idx[0][0] = smp->vrtx_idx[2][0];
smp->vrtx_idx[0][1] = smp->vrtx_idx[2][1];
W1D(bd1, bd2, smp);
}
else if (a1 < gkEpsilon)
{
smp->nvrtx = 2;
smp->vrtx[1][0] = smp->vrtx[2][0];
smp->vrtx[1][1] = smp->vrtx[2][1];
smp->vrtx[1][2] = smp->vrtx[2][2];
smp->vrtx_idx[1][0] = smp->vrtx_idx[2][0];
smp->vrtx_idx[1][1] = smp->vrtx_idx[2][1];
W1D(bd1, bd2, smp);
}
else if (a2 < gkEpsilon)
{
smp->nvrtx = 2;
W1D(bd1, bd2, smp);
}
// Compute witness points
// This is done by blending the source points using
// the barycentric coordinates
const gkFloat* w00 = bd1->coord[smp->vrtx_idx[0][0]];
const gkFloat* w01 = bd2->coord[smp->vrtx_idx[0][1]];
const gkFloat* w10 = bd1->coord[smp->vrtx_idx[1][0]];
const gkFloat* w11 = bd2->coord[smp->vrtx_idx[1][1]];
const gkFloat* w20 = bd1->coord[smp->vrtx_idx[2][0]];
const gkFloat* w21 = bd2->coord[smp->vrtx_idx[2][1]];
for(int t=0; t < 3; t++){
smp->witnesses[0][t] = w00[t] * a0 + w10[t] * a1 + w20[t] * a2;
smp->witnesses[1][t] = w01[t] * a0 + w11[t] * a1 + w21[t] * a2;
}
}
inline static void W3D(const gkPolytope *bd1, const gkPolytope *bd2, gkSimplex *smp)
{
gkFloat pq[3], pr[3], ps[3], po[3];
const gkFloat *p = smp->vrtx[0];
const gkFloat *q = smp->vrtx[1];
const gkFloat *r = smp->vrtx[2];
const gkFloat *s = smp->vrtx[3];
for (int t = 0; t < 3; t++)
{
pq[t] = q[t] - p[t];
pr[t] = r[t] - p[t];
ps[t] = s[t] - p[t];
po[t] = -p[t];
}
/**
* Compute barycentric coordinates via matrix inversion
* Given the points $P$, $Q$, and $R$, and $S$ forming a
* tetrahedron we want to find the barycentric coordinates of
* the origin. We can do this by inverting $\mathbf{T}$ in the
* linear equation below:
*
* \begin{align*}
* \mathbf{T}
* \begin{bmatrix}
* \lambda_q \\
* \lambda_r \\
* \lambda_s
* \end{bmatrix} &= \begin{bmatrix}
* \overrightarrow{PQ}\cdot\overrightarrow{PO} \\
* \overrightarrow{PR}\cdot\overrightarrow{PO} \\
* \overrightarrow{PS}\cdot\overrightarrow{PO}
* \end{bmatrix} \\
* \lambda_p &= 1 - \lambda_q - \lambda_r - \lambda_s \\
* \mathbf{T} &= \begin{bmatrix}
* \overrightarrow{PQ}\cdot\overrightarrow{PQ} & \overrightarrow{PQ}\cdot\overrightarrow{PR} & \overrightarrow{PQ}\cdot\overrightarrow{PS}\\
* \overrightarrow{PR}\cdot\overrightarrow{PQ} & \overrightarrow{PR} \cdot \overrightarrow{PR} & \overrightarrow{PR}\cdot\overrightarrow{PS} \\
* \overrightarrow{PS}\cdot\overrightarrow{PQ} & \overrightarrow{PS}\cdot\overrightarrow{PR} & \overrightarrow{PS}\cdot\overrightarrow{PS}
* \end{bmatrix}
* \end{align*}
*/
const gkFloat T00 = dotProduct(pq, pq);
const gkFloat T01 = dotProduct(pq, pr);
const gkFloat T02 = dotProduct(pq, ps);
const gkFloat T11 = dotProduct(pr, pr);
const gkFloat T12 = dotProduct(pr, ps);
const gkFloat T22 = dotProduct(ps, ps);
const gkFloat det00 = T11 * T22 - T12 * T12;
const gkFloat det01 = T01 * T22 - T02 * T12;
const gkFloat det02 = T01 * T12 - T02 * T11;
const gkFloat det = T00 * det00 - T01 * det01 + T02 * det02;
if (det == 0.0)
{
// Degenerate case
W2D(bd1, bd2, smp);
}
const gkFloat b0 = dotProduct(pq, po);
const gkFloat b1 = dotProduct(pr, po);
const gkFloat b2 = dotProduct(ps, po);
// inverse matrix
// (the matrix is symmetric, so we can use the cofactor matrix)
const gkFloat det11 = T00 * T22 - T02 * T02;
const gkFloat det12 = T00 * T12 - T01 * T02;
const gkFloat det22 = T00 * T11 - T01 * T01;
const gkFloat I00 = det00 / det;
const gkFloat I01 = -det01 / det;
const gkFloat I02 = det02 / det;
const gkFloat I11 = det11 / det;
const gkFloat I12 = -det12 / det;
const gkFloat I22 = det22 / det;
const gkFloat a1 = I00 * b0 + I01 * b1 + I02 * b2;
const gkFloat a2 = I01 * b0 + I11 * b1 + I12 * b2;
const gkFloat a3 = I02 * b0 + I12 * b1 + I22 * b2;
const gkFloat a0 = 1.0 - a1 - a2 - a3;
// check if the origin is very close to one of the faces of the
// simplex. In this case, a 2D projection will be more accurate.
if (a0 < gkEpsilon)
{
smp->nvrtx = 3;
smp->vrtx[0][0] = smp->vrtx[3][0];
smp->vrtx[0][1] = smp->vrtx[3][1];
smp->vrtx[0][2] = smp->vrtx[3][2];
smp->vrtx_idx[0][0] = smp->vrtx_idx[3][0];
smp->vrtx_idx[0][1] = smp->vrtx_idx[3][1];
W2D(bd1, bd2, smp);
}
else if (a1 < gkEpsilon)
{
smp->nvrtx = 3;
smp->vrtx[1][0] = smp->vrtx[3][0];
smp->vrtx[1][1] = smp->vrtx[3][1];
smp->vrtx[1][2] = smp->vrtx[3][2];
smp->vrtx_idx[1][0] = smp->vrtx_idx[3][0];
smp->vrtx_idx[1][1] = smp->vrtx_idx[3][1];
W2D(bd1, bd2, smp);
}
else if (a2 < gkEpsilon)
{
smp->nvrtx = 3;
smp->vrtx[2][0] = smp->vrtx[3][0];
smp->vrtx[2][1] = smp->vrtx[3][1];
smp->vrtx[2][2] = smp->vrtx[3][2];
smp->vrtx_idx[2][0] = smp->vrtx_idx[3][0];
smp->vrtx_idx[2][1] = smp->vrtx_idx[3][1];
W2D(bd1, bd2, smp);
}
else if(a3 < gkEpsilon)
{
smp->nvrtx = 3;
W2D(bd1, bd2, smp);
}
// Compute witness points
// This is done by blending the original points using
// the barycentric coordinates
const gkFloat *w00 = bd1->coord[smp->vrtx_idx[0][0]];
const gkFloat *w01 = bd2->coord[smp->vrtx_idx[0][1]];
const gkFloat *w10 = bd1->coord[smp->vrtx_idx[1][0]];
const gkFloat *w11 = bd2->coord[smp->vrtx_idx[1][1]];
const gkFloat *w20 = bd1->coord[smp->vrtx_idx[2][0]];
const gkFloat *w21 = bd2->coord[smp->vrtx_idx[2][1]];
const gkFloat *w30 = bd1->coord[smp->vrtx_idx[3][0]];
const gkFloat *w31 = bd2->coord[smp->vrtx_idx[3][1]];
for (int t = 0; t < 3; t++)
{
smp->witnesses[0][t] = w00[t] * a0 + w10[t] * a1 + w20[t] * a2 + w30[t] * a3;
smp->witnesses[1][t] = w01[t] * a0 + w11[t] * a1 + w21[t] * a2 + w31[t] * a3;
}
}
inline static void
compute_witnesses(const gkPolytope* bd1, const gkPolytope* bd2, gkSimplex* smp)
{
switch(smp->nvrtx){
case 4:
W3D(bd1, bd2, smp);
break;
case 3:
W2D(bd1, bd2, smp);
break;
case 2:
W1D(bd1, bd2, smp);
break;