-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtettest.cpp
More file actions
1434 lines (1316 loc) · 46.4 KB
/
tettest.cpp
File metadata and controls
1434 lines (1316 loc) · 46.4 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
/* Author: Samuel Hornus <samuel.hornus@inria.fr>
* Copyright © Inria, 2017
* Licence: Creative Commons CC BY-ND 3.0 license available online at
* http://creativecommons.org/licenses/by-nd/3.0/
*/
#include "Statistics.h"
#include "chronograph.h"
#include "spherical.h"
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <tclap/CmdLine.h>
#include <sqlite3.h>
//#define WITH_CGAL
#ifdef WITH_CGAL
#include "CGALStuff.h"
#endif
#include "convexes.h"
#include "obb.h"
#include "gjk.h"
#define INTER_MAX_ITER 100
using namespace std;
typedef set<int> IntSet;
typedef Statistics<unsigned long> IntStat;
static TCLAP::CmdLine gCmdLine("Hi! I am Tettest and I like to intersect convexes! <3", ' ', "0.0");
static TCLAP::ValueArg<string> benchArg("b", "bench", "path to SQLite3 database file for dumping statistics", false, "", "path");
static TCLAP::SwitchArg tetArg("t", "tet", "Intersect pairs of tetrahedra", false);
static TCLAP::SwitchArg generalArg("g", "general", "Intersect pairs of convex polytopes", false);
static TCLAP::SwitchArg frustumSphereArg("", "frustum-sphere", "frustum/sphere intersections", false);
static TCLAP::SwitchArg frustumAABBArg("", "frustum-aabb", "frustum/aabb intersections", false);
static TCLAP::SwitchArg obbArg("o", "obb", "Intersect pairs of OBB", false);
static vector<string> tetMethods{"sat", "naive", "sphere", "sphered", "gjk", "gjkd", "hybrid", "greene", "cmpd"};
static TCLAP::ValuesConstraint<string> allowedAlgos(tetMethods);
static TCLAP::ValueArg<string> algoArg("a", "algorithm", "Intersection algorithm to use", false, "sphere", &allowedAlgos);
static TCLAP::SwitchArg sortedArg("s", "sorted-vertices", "Organize vertices into hierarchy", false);
static TCLAP::ValueArg<int> nboArg("n", "number-of-convexes", "Number of convexes", false, 1000, "positive integer");
static TCLAP::ValueArg<int> nbvArg("v", "number-of-vertices", "Number of vertices per convex", false, 10, "positive integer");
static TCLAP::ValueArg<float> spreadArg("p", "spread", "Maximum shift", false, 0.0, "positive real");
static int gNbV(0);
static long gNumHits(0);
static IntStat planeStat;
static float gSpread(0.0f);
// =========================================================
sqlite3 *db;
void openDatabase() { // ------------------ Database creation
bool dumpStats = benchArg.getValue().length() > 0;
if( ! dumpStats ) return;
int dbret;
dbret = sqlite3_open(benchArg.getValue().c_str(), &db);
if( dbret ) {
cerr << "\nCan't open database: %s" << sqlite3_errmsg(db);
sqlite3_close(db); benchArg.reset(); return;
}
const char * SQLTableCreation = "CREATE TABLE IF NOT EXISTS STATISTICS(" \
"DATE TEXT NOT NULL," \
"N_OBJECT INT NOT NULL," \
"N_PAIR INT NOT NULL," \
"N_HITS INT NOT NULL," \
"N_VERTEX INT NOT NULL," \
"ALGORITHM TEXT NOT NULL," \
"SORTED INT NOT NULL," \
"TEST_NAME TEXT NOT NULL," \
"TIME REAL NOT NULL," \
"SPREAD INT NOT NULL," \
"N_FAIL INT NOT NULL," \
"N_PLANE_MAX INT NOT NULL," \
"N_PLANE INT NOT NULL," \
"N_SQ_PLANE INT NOT NULL);";
sqlite3_stmt * statement;
const char * pzTail;
dbret = sqlite3_prepare_v2(db, SQLTableCreation, strlen(SQLTableCreation), &statement, &pzTail);
if( NULL == statement ) {
cerr << "\nCan't prepare statement for table creation: " << sqlite3_errmsg(db)
<< ". retcode = " << dbret;
sqlite3_close(db); benchArg.reset(); return;
}
dbret = sqlite3_step(statement);
if( dbret != SQLITE_DONE ) {
cerr << "\nCan't execute table creation: %s" << sqlite3_errmsg(db);
sqlite3_close(db); benchArg.reset(); return;
}
sqlite3_finalize(statement);
statement = NULL;
return;
}
Chronograph myChrono;
unsigned long planeStatPerPair;
unsigned long nbFails;
void closeDatabase() {
bool dumpStats = benchArg.getValue().length() > 0;
if( ! dumpStats ) return;
stringstream s;
s.precision(12);
s << "insert into statistics values (datetime('now','localtime'),"
<< nboArg.getValue() << ','
<< planeStat.nbSamples() << ','
<< gNumHits << ','
<< gNbV << ','
<< '\'' << (algoArg.getValue()) << "',"
<< (sortedArg.getValue() ? 1 : 0) << ','
<< ( tetArg.getValue() ? "'TETS'" :
( obbArg.getValue() ? "'OBB'" :
( generalArg.getValue() ? "'GENERAL'" :
( frustumSphereArg.getValue() ? "'FRUSTUM-SPHERE'" :
( frustumAABBArg.getValue() ? "'FRUSTUM-AABB'" : "'UNKNOWN'"
)
)
)
)
) << ','
<< myChrono.elapsed_time() << ','
<< static_cast<int>(gSpread*100.0f) << ','
<< nbFails << ','
<< (planeStat.max() == INTER_MAX_ITER ? planeStat.second_max() : planeStat.max()) << ','
<< planeStat.total() << ','
<< planeStat.sqTotal()
<< ");";
char * err(NULL);
sqlite3_exec(db, s.str().c_str(), NULL, NULL, &err);
if( NULL != err )
cerr << endl << err;
sqlite3_close(db);
}
// =========================================================
bool readCommandLine(int argc, char **argv) {
try
{
vector<TCLAP::Arg*> v{ & tetArg, & generalArg, & frustumSphereArg, & frustumAABBArg, & obbArg };
gCmdLine.xorAdd(v);
gCmdLine.add(benchArg);
gCmdLine.add(algoArg);
gCmdLine.add(sortedArg);
gCmdLine.add(nbvArg);
gCmdLine.add(nboArg);
gCmdLine.add(spreadArg);
gCmdLine.parse(argc, argv);
if( spreadArg.getValue() < 0.0f )
throw TCLAP::ArgException("bad value", "-p --spread");
gSpread = spreadArg.getValue();
if( nboArg.getValue() < 2 || nboArg.getValue() > 10000 )
throw TCLAP::ArgException("bad value", "-n --number-of-convexes");
if( nbvArg.getValue() < 3 || nbvArg.getValue() > 10000 )
throw TCLAP::ArgException("bad value", "-v --number-of-vertices");
gNbV = nbvArg.getValue();
if( generalArg.getValue() && algoArg.getValue() == "naive" && nbvArg.getValue() > 1000 )
throw TCLAP::ArgException("bad value", "too many vertices. Will segfault by filling the stack.");
return false;
}
catch (const TCLAP::ArgException & e)
{
cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
}
catch (...) // catch any exceptions
{
cerr << "Error: unknown exception caught" << endl;
}
return true;
}
// =========================================================
static const int edgesIdx[6][2] = {{0,1},{0,2},{0,3},{1,2},{1,3},{2,3}};
static const int normalsIdx[6][2] = {{2,3},{1,3},{1,2},{0,3},{0,2},{0,1}};
/*
* 00 11 22 33 44 55
* 01 12 23 34 45 50
* 02 13 24 35 40 51
* 03 14 25 30 41 52
* 04 15 20 31 42 53
* 05 10 21 32 43 54
* */
bool faceSeparation(const Tet & t1, const Tet & t2) {
return t1.faceSeparate(t2) || t2.faceSeparate(t1);
}
// ----------------------------------------------------------- AABB / Frustum
bool gjkDisjointAAB(const OBB & a, const VerticesOnly & b) {
#define TT_GJK_MACRO \
a.AAmaximizeInDirection(dir, maxiA, vA); \
b.minimizeInDirection(dir, miniB, vB); \
++planeStatPerPair; \
if( maxiA <= miniB ) return true
static GJK simplex;
int vB; Vec3f vA;
float maxiA, miniB;
Vec3f dir(b.vertex(0) - a.center());
TT_GJK_MACRO;
simplex.set(dir);
do {
if( simplex.add_and_nearest(b.vertex(vB) - vA, dir) ) return false;
TT_GJK_MACRO;
if(planeStatPerPair >= INTER_MAX_ITER) {
//cerr << "MAX NUMBER OF ITERATIONS EXCEEDED\n";
++nbFails;
return false;
}
} while( true );
#undef TT_GJK_MACRO
}
bool hybridDisjointAAB(const OBB & a, const VerticesOnly & b) {
#define TT_MACRO \
a.AAmaximizeInDirection(dir, maxiA, vA); \
b.minimizeInDirection(dir, miniB, vB); \
++planeStatPerPair; \
if( maxiA <= miniB ) return true
static GJK simplex;
static SphericalPolygon positiveBound, tempPoly;
int vB; Vec3f vA;
float maxiA, miniB;
Vec3f dir(b.vertex(0) - a.center());
TT_MACRO;
simplex.set(dir);
for( int n = 1; n <= 3; ++n ) {
if( simplex.add_and_nearest(b.vertex(vB) - vA, dir) ) return false;
TT_MACRO;
}
positiveBound.clear();
positiveBound.emplace_back(simplex.pts_[0]);
positiveBound.clip(simplex.pts_[1], tempPoly); positiveBound.swap(tempPoly);
if( simplex.n_ > 2 ) {
positiveBound.clip(simplex.pts_[2], tempPoly); positiveBound.swap(tempPoly);
}
positiveBound.clip(b.vertex(vB) - vA, tempPoly); positiveBound.swap(tempPoly);
/**/
if( positiveBound.empty() ) return false;
do {
dir = positiveBound.averageDirection();
TT_MACRO;
if(planeStatPerPair >= INTER_MAX_ITER) {
++nbFails;
return false;
}
positiveBound.clip(b.vertex(vB) - vA, tempPoly); positiveBound.swap(tempPoly);
if( positiveBound.empty() ) return false;
} while( true );
#undef TT_MACRO
}
bool sphericalDisjointAAB(const OBB & a, const VerticesOnly & b) {
#define TT_SPH_MACRO \
a.AAmaximizeInDirection(dir, maxiA, vA); \
b.minimizeInDirection(dir, miniB, vB); \
++planeStatPerPair; \
if( maxiA <= miniB ) return true
static SphericalPolygon positiveBound, tempPoly;
Vec3f vA; int vB;
float maxiA, miniB;
Vec3f dir(b.vertex(0) - a.center());
TT_SPH_MACRO;
positiveBound.clear();
positiveBound.emplace_back(dir);
positiveBound.clip((b.vertex(vB) - vA), tempPoly); positiveBound.swap(tempPoly);
if( positiveBound.empty() ) return false;
do {
dir = positiveBound.averageDirection();
TT_SPH_MACRO;
if(planeStatPerPair >= INTER_MAX_ITER) {
++nbFails;
return false;
}
positiveBound.clip((b.vertex(vB) - vA), tempPoly); positiveBound.swap(tempPoly);
if( positiveBound.empty() ) return false;
} while( true );
#undef TT_SPH_MACRO
}
// -------------------------------------------------------- Centered AABB / OBB
bool gjkDisjointOBB(const OBB & a, const OBB & b) {
#define TT_GJK_MACRO(testdir) \
a.centeredAAmaximizeInDirection(testdir, maxiA, vA); \
b.minimizeInDirection(testdir, miniB, vB); \
++planeStatPerPair; \
if( maxiA <= miniB ) return true
static GJK simplex;
Vec3f vA, vB;
float maxiA, miniB;
Vec3f dir(b.center());// - a.center()); because a.center() == (0,0,0)
simplex.clear();
TT_GJK_MACRO(dir);
simplex.set(dir);
do {
if( simplex.add_and_nearest(vB - vA, dir) ) return false;
TT_GJK_MACRO(dir);
if(planeStatPerPair >= INTER_MAX_ITER) {
++nbFails;
return false;
}
} while( true );
#undef TT_GJK_MACRO
}
bool sphericalDisjointOBB(const OBB & a, const OBB & b) {
#define TT_SPH_MACRO(testdir) \
a.centeredAAmaximizeInDirection(testdir, maxiA, vA); \
b.minimizeInDirection(testdir, miniB, vB); \
++planeStatPerPair; \
if( maxiA <= miniB ) return true
static SphericalPolygon positiveBound, tempPoly;
Vec3f vA; float maxiA;
Vec3f vB; float miniB;
Vec3f dir;
TT_SPH_MACRO(b.center());
positiveBound.clear();
positiveBound.emplace_back(b.center());
positiveBound.clip(vB - vA, tempPoly); positiveBound.swap(tempPoly);
if( positiveBound.empty() ) return false;
do {
dir = positiveBound.averageDirection();
TT_SPH_MACRO(dir);
if(planeStatPerPair >= INTER_MAX_ITER) {
++nbFails;
return false;
}
positiveBound.clip(vB - vA, tempPoly); positiveBound.swap(tempPoly);
if( positiveBound.empty() ) return false;
} while( true );
#undef TT_SPH_MACRO
}
// ------------------------------------------------------------ Convex / Sphere
template< typename Convex >
bool gjkDisjointWithSphere(const Convex & a, const Sphere & s) {
#define TT_GJK_MACRO(testdir) \
a.maximizeInDirection(testdir, maxiA, vA); \
s.minimizeInDirection(testdir, miniS, vS); \
++planeStatPerPair; \
if( maxiA <= miniS ) return true
static GJK simplex;
int vA; Vec3f vS;
float maxiA, miniS;
Vec3f dir(s.vertex(0) - a.vertex(0));
simplex.clear();
TT_GJK_MACRO(dir);
simplex.set(dir);
do {
if( simplex.add_and_nearest(vS - a.vertex(vA), dir) ) return false;
TT_GJK_MACRO(dir);
if(planeStatPerPair >= INTER_MAX_ITER) {
++nbFails;
return false;
}
} while( true );
#undef TT_GJK_MACRO
}
template< typename Convex >
bool sphericalDisjointWithSphere(const Convex & a, const Sphere & s) {
#define TT_SPH_MACRO(testdir) \
a.maximizeInDirection(testdir, maxiA, vA); \
s.minimizeInDirection(testdir, miniS, vS); \
++planeStatPerPair; \
if( maxiA <= miniS ) return true
static SphericalPolygon positiveBound, tempPoly;
int vA; Vec3f vS;
float maxiA, miniS;
Vec3f dir(s.vertex(0) - a.vertex(0));
TT_SPH_MACRO(dir);
positiveBound.clear();
positiveBound.emplace_back(dir);
positiveBound.clip(vS - a.vertex(vA), tempPoly); positiveBound.swap(tempPoly);
if( positiveBound.empty() ) return false;
do {
dir = positiveBound.averageDirection();
TT_SPH_MACRO(dir);
if(planeStatPerPair >= INTER_MAX_ITER) {
++nbFails;
return false;
}
positiveBound.clip(vS - a.vertex(vA), tempPoly); positiveBound.swap(tempPoly);
if( positiveBound.empty() ) return false;
} while( true );
#undef TT_SPH_MACRO
}
bool hybridDisjointWithSphere(const VerticesOnly & f, const Sphere & s) {
#define TT_MACRO \
f.maximizeInDirection(dir, maxiF, vF); \
s.minimizeInDirection(dir, miniS, vS); \
++planeStatPerPair; \
if( maxiF <= miniS ) return true
static GJK simplex;
static SphericalPolygon positiveBound, tempPoly;
int vF; Vec3f vS;
float maxiF, miniS;
Vec3f dir(s.center() - f.vertex(0));
TT_MACRO;
simplex.set(dir);
for( int n = 1; n <= 3; ++n ) {
if( simplex.add_and_nearest(vS - f.vertex(vF), dir) ) return false;
TT_MACRO;
}
positiveBound.clear();
positiveBound.emplace_back(simplex.pts_[0]);
positiveBound.clip(simplex.pts_[1], tempPoly); positiveBound.swap(tempPoly);
if( simplex.n_ > 2 ) {
positiveBound.clip(simplex.pts_[2], tempPoly); positiveBound.swap(tempPoly);
}
positiveBound.clip(vS - f.vertex(vF), tempPoly); positiveBound.swap(tempPoly);
/**/
if( positiveBound.empty() ) return false;
do {
dir = positiveBound.averageDirection();
TT_MACRO;
if(planeStatPerPair >= INTER_MAX_ITER) {
++nbFails;
return false;
}
positiveBound.clip(vS - f.vertex(vF), tempPoly); positiveBound.swap(tempPoly);
if( positiveBound.empty() ) return false;
} while( true );
#undef TT_MACRO
}
// -------------------------------------------------------------------- Convex / Convex
template< class Convex >
bool gjkDisjoint(const Convex & a, const Convex & b) {
static GJK simplex;
int vA, vB;
Vec3f dir = b.vertex(0) - a.vertex(0);
simplex.clear();
if( ! a.differenceCoversZeroInDir(b, vA, vB, dir) ) return true;
simplex.set(dir);
do {
if( simplex.add_and_nearest(b.vertex(vB) - a.vertex(vA), dir) ) return false;
if( ! a.differenceCoversZeroInDir(b, vA, vB, dir) ) return true;
if(planeStatPerPair >= INTER_MAX_ITER) {
++nbFails;
return false;
}
} while( true );
}
template< class Convex >
float gjkDistance(const Convex & a, const Convex & b) {
static GJK simplex;
int vA(0), vB(0);
Vec3f dir;
simplex.clear();
float upperBound(std::numeric_limits<float>::max());
float lowerBound(0.0f);
do {
float maxOverA, minOverB;
if( simplex.add_and_nearest_for_distance(b.vertex(vB) - a.vertex(vA), dir) ) return 0.0f;
float d = dir.length();
upperBound = min(upperBound, d);
dir = dir / d;
a.differenceInDir(b, vA, vB, maxOverA, minOverB, dir);
lowerBound = max(lowerBound, (minOverB - maxOverA));
if( upperBound - lowerBound < - 1e-5f ) {
++nbFails;
cerr << "BUG: ";
}
if( upperBound - lowerBound < 1e-4f ) {
return 0.5f * (upperBound + lowerBound);
}
if(planeStatPerPair >= INTER_MAX_ITER) {
++nbFails;
return 0.5f * (upperBound + lowerBound);
}
} while( true );
}
// - - - - - - - - - - - - - - - - - - - - - - - -
static SphericalPolygon tupTemp_;
struct ThreeUniquePoints {
vector<Vec3f> pts_;
int pos_ = 0;
ThreeUniquePoints() : pts_() {}
void add(const Vec3f & v) {
bool same_found(false);
for( const auto & p : pts_ ) {
if( v == p ) {
same_found = true;
break;
}
}
if( same_found ) return;
#define TUPSIZE 3
if( pts_.size() < TUPSIZE ) {
pts_.push_back(v);
return;
} else {
pts_[pos_] = v;
pos_ = (pos_ + 1) % TUPSIZE;
}
}
void clip(const SphericalPolygon & src, SphericalPolygon & dest, const Vec3f & delta) {
int i(1);
src.clip(pts_[0] - delta, dest);
for( ; i < pts_.size(); ) {
dest.clip(pts_[i++] - delta, tupTemp_);
dest.swap(tupTemp_);
}
}
};
// - - - - - - - - - - - - - - - - - - - - - - - -
template< typename Convex >
bool sphericalDisjoint(const Convex & a, const Convex & b) {
static SphericalPolygon positiveBound, tempPoly;
int vA, vB;
Vec3f dir(b.vertex(0) - a.vertex(0));
if( ! a.differenceCoversZeroInDir(b, vA, vB, dir) ) return true;
positiveBound.clear();
positiveBound.emplace_back(dir);
positiveBound.clip(b.vertex(vB) - a.vertex(vA), tempPoly); positiveBound.swap(tempPoly);
if( positiveBound.empty() ) return false;
do {
if( ! a.differenceCoversZeroInDir(b, vA, vB, positiveBound.averageDirection()) ) return true;
if(planeStatPerPair >= INTER_MAX_ITER) {
++nbFails;
return false;
}
positiveBound.clip(b.vertex(vB) - a.vertex(vA), tempPoly); positiveBound.swap(tempPoly);
if( positiveBound.empty() ) return false;
} while( true );
}
// - - - - - - - - - - - - - - - - - - - - - - - -
template< typename Convex >
bool sphericalDisjointWithBounds(const Convex & a, const Convex & b,
SphericalPolygon & positiveBound, GJK & simplex,
float & lowerBound, float & upperBound) {
//Vec3f & P1, Vec3f & P2) {
static SphericalPolygon tempPoly;
static vector<Vec3f> vertices;
int vA, vB;
Vec3f P(b.vertex(0) - a.vertex(0));
simplex.clear();
upperBound = simplex.add_and_distance(P);
positiveBound.clear();
positiveBound.emplace_back(P);
do {
float maxOverA, minOverB;
Vec3f n = positiveBound.averageDirection().normalized();
a.differenceInDir(b, vA, vB, maxOverA, minOverB, n);
if( maxOverA < minOverB ) {
P = b.vertex(vB) - a.vertex(vA);
upperBound = std::min(upperBound, simplex.add_and_distance(P));
//upperBound = std::min(upperBound, P.norm2());
lowerBound = minOverB - maxOverA; // > 0
Vec3f delta(lowerBound * n);
cout << endl << "NO INTERSECTION. low = " << lowerBound << ", hi = " << upperBound << endl;
positiveBound.clip(P, P - delta, tempPoly); positiveBound.swap(tempPoly);
/*vertices.clear();
for( const auto & v : positiveBound )
vertices.push_back(v.silVertex_);
for( const auto & v : vertices ) {
positiveBound.clip(v, v - delta, tempPoly, false); positiveBound.swap(tempPoly);
if( positiveBound.empty() ) break;
}*/
return true;
}
if(planeStatPerPair >= INTER_MAX_ITER) {
++nbFails;
return false;
}
P = b.vertex(vB) - a.vertex(vA);
positiveBound.clip(P, tempPoly); positiveBound.swap(tempPoly);
if( positiveBound.empty() ) return false;
upperBound = std::min(upperBound, simplex.add_and_distance(P));
//upperBound = std::min(upperBound, P.norm2());
} while( true );
}
// - - - - - - - - - - - - - - - - - - - - - - - -
//
// TODO : reput ThreeUniquePoints (lasts) but use it to compute distance to
// the triangle formed by the last 3 points, in order to have an upper bound
template< typename ConvexA, typename ConvexB >
float sphericalDistance(const ConvexA & a, const ConvexB & b) {
static SphericalPolygon S_low, S_cur, tempPoly;
static GJK simplex;
float low, low_diff;
Vec3f low_n, low_p;
float cur, cur_diff;
Vec3f cur_n, cur_p, dir;
float hi(std::numeric_limits<float>::max());
float maxOverA, minOverB;
int vA, vB;
if( ! sphericalDisjointWithBounds(a, b, S_cur, simplex, low, hi) )
return 0.0f;
//hi = ::sqrtf(hi);
#define TRACE
if( S_cur.empty() ) {
#ifdef TRACE
cout << endl << "EMPTY. " << (hi-low) << ", low:"<<low<<", cur:"<<cur<<", hi:"<<hi << ", |S|:" << S_cur.size() << endl;
#endif
return low;
}
if( hi - low < 1e-4f ) {
#ifdef TRACE
cout << endl << "GOOD BOUNDS. " << (hi-low) << ", low:"<<low<<", cur:"<<cur<<", hi:"<<hi << ", |S|:" << S_cur.size() << endl;
#endif
return 0.5f*(low+hi);
}
S_low = S_cur;
float k = 2.0f;
float kk = 2.0f*k-1.0f;
low_n = S_cur.averageDirection().normalized();
a.differenceInDir(b, vA, vB, maxOverA, minOverB, low_n);
low_diff = minOverB - maxOverA;
low_p = b.vertex(vB) - a.vertex(vA);
hi = std::min(hi, simplex.add_and_distance(low_p));
cur_n = low_n;
cur_diff = low_diff;
cur_p = low_p;
cur = (kk * low < hi) ? k * low : 0.5f * (low + hi);
bool cur_is_low(true);
do {
#ifdef TRACE
cout << endl << (hi-low) << ", low:"<<low<<", cur:"<<cur<<", hi:"<<hi << ", |S|:" << S_cur.size();
#endif
if( hi - low < 1e-4f ) {
#ifdef TRACE
cout << " FOUND! " << 0.5f*(low+hi)<<". ";
#endif
return 0.5f*(low+hi);
}
if( planeStatPerPair >= INTER_MAX_ITER ) {
++nbFails;
#ifdef TRACE
cout << " FAIL " << 0.5f*(low+hi)<<". ";
#endif
return 0.5f*(low+hi);
}
if( cur_diff <= cur ) { // maxOverA + cur >= minOverB
S_cur.clip(cur_p, cur_p - cur * cur_n, tempPoly);// S_cur.swap(tempPoly);
if( tempPoly.empty() ) { // cur is too large, restart on S_low
hi = cur;
cur = (kk * low < hi) ? k * low : 0.5f * (low + hi);
if( ! cur_is_low ) {
S_cur = S_low;
cur_is_low = true;
cur_diff = low_diff;
cur_n = low_n;
cur_p = low_p;
}
#ifdef TRACE
cout<<" --> too large, restart on S_low. ";
#endif
} else { // continue clipping
S_cur.swap(tempPoly);
cur_is_low = false;
cur_n = S_cur.averageDirection().normalized();
a.differenceInDir(b, vA, vB, maxOverA, minOverB, cur_n);
cur_diff = minOverB - maxOverA;
cur_p = b.vertex(vB) - a.vertex(vA);
hi = std::min(hi, simplex.add_and_distance(cur_p));
if( hi <= cur ) {
cur = (kk * low < hi) ? k * low : 0.5f * (low + hi);
S_cur = S_low;
cur_is_low = true;
cur_diff = low_diff;
cur_n = low_n;
cur_p = low_p;
}
#ifdef TRACE
cout << " --> ...";
#endif
}
} else { // maxOverA + cur < minOverB => new lower bound: cur_diff > cur
Vec3f delta(cur_diff * cur_n);
S_cur.clip(cur_p, cur_p - delta, S_low);
/*for( int i = 0; i < S_cur.size(); ++i ) {
tempPoly.swap(S_low);
tempPoly.clip(tempPoly[i].silVertex_, tempPoly[i].silVertex_ - delta, S_low, false);
if( S_low.empty() ) break;
}*/
if( S_low.empty() ) {
#ifdef TRACE
cout << " not enough precision: "<<cur_diff <<". ";
#endif
return cur_diff;
}
//S_cur.swap(tempPoly);
//S_low = S_cur;
S_cur = S_low;
cur_is_low = true;
low = cur_diff;
low_n = S_cur.averageDirection().normalized();
a.differenceInDir(b, vA, vB, maxOverA, minOverB, low_n);
low_diff = minOverB - maxOverA;
low_p = b.vertex(vB) - a.vertex(vA);
hi = std::min(hi, simplex.add_and_distance(low_p));
cur_diff = low_diff;
cur_n = low_n;
cur_p = low_p;
//hi = std::min(hi, low_p.length());
cur = (kk * low < hi) ? k * low : 0.5f * (low + hi);
#ifdef TRACE
cout << " --> too low, new lower bound";
#endif
}
} while( true );
}
// =============================================================
Vec3f genConvex(int nbVertices, Polyhedron_3 & poly) {
if( nbVertices < 4 )
nbVertices = 4;
static vector<Point_3> v;
v.clear();
RandomPointOnSphere rps;
Vec3f avg;
float shift = drand48() * gSpread;
Vec3f s(shift, 0.0f, 0.0f);
for( int i = 0; i < nbVertices; ++i ) {
Vec3f p(rps.vec3f() + s);
v.emplace_back(p.x(), p.y(), p.z());
avg = avg + p;
}
avg = (1.0f/nbVertices) * avg;
convex_hull_3(v.begin(), v.end(), poly);
return avg;
}
// =============================================================
template< typename Convex >
bool testDisjoint(const Convex & a, const Convex & b) {
// test B against facets of A
const int nva = a.nbVertices();
const int nvb = b.nbVertices();
const int nfa = a.nbFacets();
const int nfb = b.nbFacets();
for( int af = 0; af < nfa; ++af ) {
const Vec3f & n = a.facets_[af].normal_;
float minOverB(n | b.vertices_[0]);
for( int bv = 1; bv < nvb; ++bv ) {
float d = b.vertices_[bv] | n;
if( minOverB > d ) minOverB = d;
if( a.facets_[af].maxAlong_ > minOverB ) break;
}
++planeStatPerPair;
if( a.facets_[af].maxAlong_ <= minOverB ) return true;
}
float AvDotBn[nva][nfb];
// test A against facets of B
for( int bf = 0; bf < nfb; ++bf ) {
const Vec3f & n = b.facets_[bf].normal_;
float minOverA(n | a.vertices_[0]);
AvDotBn[0][bf] = minOverA;
for( int av = 1; av < nva; ++av ) {
float d;
AvDotBn[av][bf] = d = a.vertices_[av] | n;
if( minOverA > d ) minOverA = d;
}
++planeStatPerPair;
if( b.facets_[bf].maxAlong_ <= minOverA ) return true;
}
// test A-B-edge pairs
float bpos[nfb];
const int nea = a.nbEdges();
const int neb = b.nbEdges();
for( int ia = 0; ia < nea; ++ia ) {
int ani0 = a.edges_[ia][2];
int ani1 = a.edges_[ia][3];
for( int i = 0; i < nfb; ++i ) {// precompute the dot-product of normals of |b| with |a|'s current edge
bpos[i] = AvDotBn[a.edges_[ia][0]][i] - AvDotBn[a.edges_[ia][1]][i];
}
for( int ib = 0; ib < neb; ++ib ) {
int bni0 = b.edges_[ib][2];
int bni1 = b.edges_[ib][3];
if( bpos[bni0] * bpos[bni1] >= 0.0f ) continue;
float bpos0 = ::fabsf(bpos[bni0]);
float bpos1 = ::fabsf(bpos[bni1]);
float maxOverB = bpos0 * b.facets_[bni1].maxAlong_ + bpos1 * b.facets_[bni0].maxAlong_;
float xbDotAw0 = bpos0 * AvDotBn[ani0][bni1] + bpos1 * AvDotBn[ani0][bni0];
float xbDotAw1 = bpos0 * AvDotBn[ani1][bni1] + bpos1 * AvDotBn[ani1][bni0];
int edId = a.edges_[ia][0];
if( xbDotAw0 > xbDotAw1 ) xbDotAw0 = xbDotAw1;
float xbDotAve = bpos0 * AvDotBn[edId][bni1] + bpos1 * AvDotBn[edId][bni0];
++planeStatPerPair;
if( maxOverB <= xbDotAve && xbDotAve <= xbDotAw0 ) return true;
}
}
return false;
}
template< >
bool testDisjoint<VerticesOnly>(const VerticesOnly & a, const VerticesOnly & b) {
return true;
}
template< >
bool testDisjoint<SortedVertices>(const SortedVertices & a, const SortedVertices & b) {
return true;
}
// =============================================================
template< typename Convex >
void test_general(const int N, const int nbv) {
vector<Convex> convexes;
convexes.reserve(N);
int algo = 0;
if( algoArg.getValue() == "naive" ) {
algo = 0;
} else if( algoArg.getValue() == "sphere" ) {
algo = 1;
} else if( algoArg.getValue() == "sat" ) {
algo = 1;
cerr << "WARNING: SAT not implemented for general convexes. Using 'sphere'";
} else if( algoArg.getValue() == "gjk" ) {
algo = 2;
} else if( algoArg.getValue() == "sphered" ) {
algo = 3;
} else if( algoArg.getValue() == "gjkd" ) {
algo = 4;
} else if( algoArg.getValue() == "cmpd" ) {
algo = 5;
}
if( algo == 0 ) {
Polyhedron_3 poly;
for( int i = 0; i < N; ++i ) {
Vec3f center = genConvex(nbv, poly);
convexes.emplace_back(poly);
convexes.back().center_ = center;
}
} else {
for( int i = 0; i < N; ++i ) {
convexes.emplace_back(nbv, drand48() * gSpread);
}
}
myChrono.start();
for( int j = 1; j < N; ++j )
for( int i = 0; i < j; ++i ) { // TEST GENERAL
planeStatPerPair = 0;
switch( algo ) {
case 0: if( ! testDisjoint(convexes[i], convexes[j]) ) ++gNumHits; break;
case 1: if( ! sphericalDisjoint(convexes[i], convexes[j]) ) ++gNumHits; break;
case 2: if( ! gjkDisjoint(convexes[i], convexes[j]) ) ++gNumHits; break;
case 3: if( 0.0f == sphericalDistance(convexes[i], convexes[j]) ) ++gNumHits; break;
case 4: if( 0.0f == gjkDistance(convexes[i], convexes[j]) ) ++gNumHits; break;
case 5: {
const float gjkd = gjkDistance(convexes[i], convexes[j]);
const float sphd = sphericalDistance(convexes[i], convexes[j]);
if( sphd > 0.0f && gjkd > 0.0f ) {
if( fabs(1.0f-sphd/gjkd) > 0.01f ) {
cerr << "** ";
cerr << "DSS=" << sphd << ". GJKD=" << gjkd << endl;
}
}
else
++gNumHits;
break;
}
default: break;
}
planeStat.addSample(planeStatPerPair);
}
myChrono.stop();
size_t pairs = planeStat.nbSamples();
cout << gNumHits << " hits over " << pairs << " pairs (";
cout << (100.0*(double)gNumHits)/pairs << "%) in " << myChrono.elapsed_time() << " seconds ( ";
cout << (pairs / myChrono.elapsed_time() / 1000.0) << " KPairs/s ).";
cout << endl << planeStat.min() << " / " << planeStat.mean() << "+/-" << planeStat.dev() << " / " << planeStat.second_max() << " / " << planeStat.max()
<< " min/avg/dev/premax/max directions/pair.";
cout << endl << "FAILS:" << nbFails << endl << flush;
}
// =============================================================
int obbDisjoint(const OBB & a, const OBB & b) {
Matrix3f R;
Matrix::transMul(a.rotation_, b.rotation_, R);
Vec3f T = a.rotation_.tMulVec3(b.center_ - a.center_);
return obb_disjoint(R.d, T.data(), a.radii_.data(), b.radii_.data());
}
void test_obb(const int N) {
if( gSpread == 0.0f )
gSpread = 1e-2;
vector<OBB> obbs;
obbs.reserve(N);
for( int j = 0; j < N; ++j ) {
float x = drand48() * gSpread - gSpread / 2.0f;
float y = drand48() * gSpread - gSpread / 2.0f;
float z = drand48() * gSpread - gSpread / 2.0f;
float dx = drand48();
float dy = drand48();
float dz = drand48();
obbs.emplace_back(Vec3f(x,y,z), Vec3f(dx,dy,dz));
}
int algo = 0;
if( algoArg.getValue() == "sat" )
algo = 0;
else if( algoArg.getValue() == "gjk" )
algo = 2;
else
algo = 1;
OBB o;
myChrono.start();
for( int j = 1; j < N; ++j ) {
o.radii_ = obbs[j].radii_;
for( int i = 0; i < j; ++i ) {
planeStatPerPair = 0;
switch( algo ) {
case 0:
planeStatPerPair = obbDisjoint(obbs[i], obbs[j]);
if( planeStatPerPair == 0 ) {
++gNumHits; // in case of a hit, 15 SAT tests have been performed
planeStatPerPair = 15;
}
break;
case 1:
Matrix::transMul(obbs[i].rotation_, obbs[j].rotation_, o.rotation_);
o.center_ = obbs[i].rotation_.tMulVec3(obbs[j].center_ - obbs[i].center_);
if( ! sphericalDisjointOBB(obbs[i], o) ) {
++gNumHits;
}
break;
case 2:
Matrix::transMul(obbs[i].rotation_, obbs[j].rotation_, o.rotation_);
o.center_ = obbs[i].rotation_.tMulVec3(obbs[j].center_ - obbs[i].center_);
if( ! gjkDisjointOBB(obbs[i], o) )
++gNumHits;
break;
}
planeStat.addSample(planeStatPerPair);
}
}
myChrono.stop();
size_t pairs = planeStat.nbSamples();
cout << gNumHits << " hits over " << pairs << " OBBs (";
cout << (100.0*(double)gNumHits)/pairs << "%) in " << myChrono.elapsed_time() << " seconds ( ";
cout << (pairs / myChrono.elapsed_time() / 1000.0) << " KPairs/s ).";
cout << endl << planeStat.min() << " / " << planeStat.mean() << "+/-" << planeStat.dev() << " / " << planeStat.second_max() << " / " << planeStat.max()
<< " min/avg/dev/premax/max directions/pair.";
cout << endl << flush;
}
// =============================================================
void test_aabb_frustum(const int N) {
vector<OBB> boxes;