-
Notifications
You must be signed in to change notification settings - Fork 1
/
calcUtils.cpp
2582 lines (2406 loc) · 92.3 KB
/
calcUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "calcUtils.h"
unsigned long CCalcUtils::getDjb2Hash(const char* inputStr,size_t size)
{ // taken from http://www.cse.yorku.ca/~oz/hash.html
unsigned long hash = 5381;
for (size_t i=0;i<size;i++)
hash = ((hash << 5) + hash) + int(inputStr[i]);
return hash;
}
bool CCalcUtils::doCollide_tri_tri(const C3Vector& p1,const C3Vector& v1,const C3Vector& w1,int tri1Index,const C3Vector& p2,const C3Vector& v2,const C3Vector& w2,int tri2Index,std::vector<double>* intersections,int* cachingTri1,int* cachingTri2)
{
C3Vector n1((v1^w1).getNormalized());
C3Vector n2((v2^w2).getNormalized());
if (n1.isColinear(n2,double(0.99999)))
return(false); // colinear!
// Now check for separating axes:
C3Vector u1(w1-v1);
C3Vector u2(w2-v2);
C3Vector tr1Edges[3]={v1,w1,u1};
C3Vector tr2Edges[3]={v2,w2,u2};
if (getProjectedDistance_tri_tri(n1,p1,v1,w1,p2,v2,w2)>0.0)
return(false); // n1 is the separating axis!
if (getProjectedDistance_tri_tri(n2,p1,v1,w1,p2,v2,w2)>0.0)
return(false); // n2 is the separating axis!
for (size_t i=0;i<3;i++)
{
for (size_t j=0;j<3;j++)
{
C3Vector axis=tr1Edges[i]^tr2Edges[j];
if ( (axis(0)!=0.0)||(axis(1)!=0.0)||(axis(2)!=0.0) )
{ // Edges are not parallel
if (getProjectedDistance_tri_tri(axis.getNormalized(),p1,v1,w1,p2,v2,w2)>0.0)
return(false); // that axis is separating!
}
}
}
if (intersections!=nullptr)
{ // get the two intersections, i.e. tri1Surface vs tri2Edges + tri2Surface vs tri1Edges:
const C3Vector* triP[2]={&p1,&p2};
const C3Vector* triV[2]={&v1,&v2};
const C3Vector* triW[2]={&w1,&w2};
C3Vector intersection1;
bool intersection1Done=false;
C3Vector segP,segL;
for (size_t j=0;j<2;j++)
{
C3Vector p(triP[j][0]);
C3Vector v(triV[j][0]);
C3Vector w(triW[j][0]);
C3Vector n(v^w);
size_t j2=1;
if (j==1)
j2=0;
for (size_t i=0;i<3;i++)
{
if (i<2)
segP=triP[j2][0];
else
segP=triP[j2][0]+triV[j2][0];
if (i==0)
segL=triV[j2][0];
if (i==1)
segL=triW[j2][0];
if (i==2)
segL=triW[j2][0]-triV[j2][0];
// Does the segment maybe collide with the triangle?
double dd=-(p*n);
double denom=n*segL;
if (denom!=0.0)
{ // Seg and tri are not parallel
double t=-((n*segP)+dd)/denom;
if ( (t>=0.0)&&(t<=1.0) )
{ // the segment collides with the triangle's plane
C3Vector intersection(segP+(segL*t));
// is intersection within triangle's borders?
C3Vector vect(intersection-p);
if ((vect^w)*n>0.0)
{ // within border1
if ((v^vect)*n>0.0)
{ // within border2
vect-=v;
C3Vector wv(w-v);
if ((wv^vect)*n>0.0)
{ // within border3
if (intersection1Done)
{
intersections->push_back(intersection1(0));
intersections->push_back(intersection1(1));
intersections->push_back(intersection1(2));
intersections->push_back(intersection(0));
intersections->push_back(intersection(1));
intersections->push_back(intersection(2));
}
else
{
intersection1=intersection;
intersection1Done=true;
}
}
}
}
}
}
}
}
}
if (cachingTri1!=nullptr)
cachingTri1[0]=tri1Index;
if (cachingTri2!=nullptr)
cachingTri2[0]=tri2Index;
return(true);
}
bool CCalcUtils::doCollide_tri_segp(const C3Vector& p1,const C3Vector& v1,const C3Vector& w1,int tri1Index,const C3Vector& segP,const C3Vector& segL,C3Vector* intersection,int* cachingTri1)
{
bool retVal=false;
C3Vector n(v1^w1);
// Does the segment maybe collide with the triangle?
double dd=-(p1*n);
double denom=n*segL;
if (denom!=0.0)
{ // Seg and tri are not parallel
double t=-((n*segP)+dd)/denom;
if ( (t>=0.0)&&(t<=1.0) )
{ // the segment collides with the triangle's plane
C3Vector intersect(segP+(segL*t));
// is intersection within triangle's borders?
C3Vector vect(intersect-p1);
if ((vect^w1)*n>0.0)
{ // within border1
if ((v1^vect)*n>0.0)
{ // within border2
vect-=v1;
C3Vector wv(w1-v1);
if ((wv^vect)*n>0.0)
{ // within border3
retVal=true;
if (intersection!=nullptr)
intersection[0]=intersect;
}
}
}
}
}
if (cachingTri1!=nullptr)
cachingTri1[0]=tri1Index;
return(retVal);
}
bool CCalcUtils::doCollide_box_cell(const C4X4Matrix& box,const C3Vector& boxHs,double cellHs,bool solidBoxAndCell)
{ // Cell is at the origin
// optimize!!!
C4X4Matrix box2;
box2.setIdentity();
C3Vector sv(cellHs,cellHs,cellHs);
return(doCollide_box_box(box2,sv,box,boxHs,solidBoxAndCell));
}
bool CCalcUtils::doCollide_box_box(const C4X4Matrix& box1,const C3Vector& box1Hs,const C4X4Matrix& box2,const C3Vector& box2Hs,bool solidBoxes)
{
for (size_t i=0;i<3;i++)
{ // box 1's axes
double box1Box2Proj=fabs((box2.X-box1.X)*box1.M.axis[i]);
double box2Overlap=fabs(box2.M.axis[0]*box1.M.axis[i])*box2Hs(0)+fabs(box2.M.axis[1]*box1.M.axis[i])*box2Hs(1)+fabs(box2.M.axis[2]*box1.M.axis[i])*box2Hs(2);
if (box1Hs(i)+box2Overlap<box1Box2Proj)
return(false);
}
for (size_t i=0;i<3;i++)
{ // box 2's axes
double box1Box2Proj=fabs((box2.X-box1.X)*box2.M.axis[i]);
double box1Overlap=fabs(box1.M.axis[0]*box2.M.axis[i])*box1Hs(0)+fabs(box1.M.axis[1]*box2.M.axis[i])*box1Hs(1)+fabs(box1.M.axis[2]*box2.M.axis[i])*box1Hs(2);
if (box2Hs(i)+box1Overlap<box1Box2Proj)
return(false);
}
for (size_t i=0;i<3;i++)
{
for (size_t j=0;j<3;j++)
{
C3Vector a(box1.M.axis[i]^box2.M.axis[j]);
if ( (a(0)!=0.0)||(a(1)!=0.0)||(a(2)!=0.0) )
{
a.normalize();
double box1Box2Proj=fabs((box2.X-box1.X)*a);
double box1Overlap=fabs(box1.M.axis[0]*a)*box1Hs(0)+fabs(box1.M.axis[1]*a)*box1Hs(1)+fabs(box1.M.axis[2]*a)*box1Hs(2);
double box2Overlap=fabs(box2.M.axis[0]*a)*box2Hs(0)+fabs(box2.M.axis[1]*a)*box2Hs(1)+fabs(box2.M.axis[2]*a)*box2Hs(2);
if (box1Overlap+box2Overlap<box1Box2Proj)
return(false);
}
}
}
if (!solidBoxes)
{
std::vector<C3Vector> b1pts;
std::vector<C3Vector> b2pts;
for (double z=-1.0;z<2.0;z+=2.0)
{
C3Vector a1;
C3Vector a2;
a1(2)=z*box1Hs(2);
a2(2)=z*box2Hs(2);
for (double y=-1.0;y<2.0;y+=2.0)
{
a1(1)=y*box1Hs(1);
a2(1)=y*box2Hs(1);
for (double x=-1.0;x<2.0;x+=2.0)
{
a1(0)=x*box1Hs(0);
a2(0)=x*box2Hs(0);
b1pts.push_back(box1*a1);
b2pts.push_back(box2*a2);
}
}
}
if (doCollide_box_allpts(box1,box1Hs,b2pts))
return(false); // box2 is contained in box1
if (doCollide_box_allpts(box2,box2Hs,b1pts))
return(false); // box1 is contained in box2
}
return(true);
}
bool CCalcUtils::doCollide_box_tri(const C4X4Matrix& box,const C3Vector& boxHs,bool solidBox,const C3Vector& p,const C3Vector& v,const C3Vector& w)
{
C3Vector n((v^w).getNormalized());
if (getProjectedDistance_box_tri(n,box,boxHs,p,v,w)>0.0)
return(false); // tri normal vect. is separating axis
for (size_t i=0;i<3;i++)
{
if (getProjectedDistance_box_tri(box.M.axis[i],box,boxHs,p,v,w)>0.0)
return(false); // one of the box's edges is separating axis
}
// Now check the tri edges:
C3Vector u(w-v);
C3Vector trEdges[3]={v,w,u};
for (size_t i=0;i<3;i++)
{
for (size_t j=0;j<3;j++)
{
C3Vector axis=box.M.axis[i]^trEdges[j];
if ( (axis(0)!=0.0)||(axis(1)!=0.0)||(axis(2)!=0.0) )
{
if (getProjectedDistance_box_tri(axis.getNormalized(),box,boxHs,p,v,w)>0.0)
return(false);
}
}
}
if (!solidBox)
{
std::vector<C3Vector> pts;
pts.push_back(p);
pts.push_back(p+v);
pts.push_back(p+w);
if (doCollide_box_allpts(box,boxHs,pts))
return(false); // triangle is contained in the box
}
return(true);
}
bool CCalcUtils::doCollide_box_segp(const C4X4Matrix& box,const C3Vector& boxHs,bool solidBox,const C3Vector& segP,const C3Vector& segL)
{ // Segment is defined as segP (end-point1) and segP+segL (end-point2)
C3Vector segHs(segL*0.5);
C3Vector segCenter(segP+segHs);
return(doCollide_box_seg(box,boxHs,solidBox,segCenter,segHs));
}
bool CCalcUtils::doCollide_box_seg(const C4X4Matrix& box,const C3Vector& boxHs,bool solidBox,const C3Vector& segCenter,const C3Vector& segHs)
{ // Segment is defined as segCenter (center of segment) and segHs (segment vector half-size)
// Box-segment segment:
if (getProjectedDistance_box_seg(segCenter.getNormalized(),box,boxHs,segCenter,segHs)>0.0)
return(false);
// Segment axis:
if (getProjectedDistance_box_seg(segHs.getNormalized(),box,boxHs,segCenter,segHs)>0.0)
return(false);
// Box axes:
for (size_t i=0;i<3;i++)
{
if (getProjectedDistance_box_seg(box.M.axis[i],box,boxHs,segCenter,segHs)>0.0)
return(false);
}
// Combination of Box and segment axes:
for (size_t i=0;i<3;i++)
{
if (!segHs.isColinear(box.M.axis[i],double(0.99999)))
{
if (getProjectedDistance_box_seg((segHs^box.M.axis[i]).getNormalized(),box,boxHs,segCenter,segHs)>0.0)
return(false);
}
}
if (!solidBox)
{
std::vector<C3Vector> pts;
pts.push_back(segCenter-segHs);
pts.push_back(segCenter+segHs);
if (doCollide_box_allpts(box,boxHs,pts))
return(false); // segment is contained in the box
}
return(true);
}
bool CCalcUtils::doCollide_box_allpts(const C4X4Matrix& box,const C3Vector& boxHs,const std::vector<C3Vector>& points)
{ // true if all points are within the box
C4X4Matrix boxInv(box.getInverse());
for (size_t i=0;i<points.size();i++)
{
C3Vector transfPt(boxInv*points[i]);
if (fabs(transfPt(0))>boxHs(0))
return(false);
if (fabs(transfPt(1))>boxHs(1))
return(false);
if (fabs(transfPt(2))>boxHs(2))
return(false);
}
return(true);
}
bool CCalcUtils::doCollide_box_onept(const C4X4Matrix& box,const C3Vector& boxHs,const std::vector<C3Vector>& points,C3Vector* insidePt/*=nullptr*/)
{ // true is at least one point is within the box
C4X4Matrix boxInv(box.getInverse());
for (size_t i=0;i<points.size();i++)
{
C3Vector transfPt(boxInv*points[i]);
if (fabs(transfPt(0))<=boxHs(0))
{
if (fabs(transfPt(1))<=boxHs(1))
{
if (fabs(transfPt(2))<=boxHs(2))
{
if (insidePt!=nullptr)
insidePt[0]=points[i];
return(true);
}
}
}
}
return(false);
}
bool CCalcUtils::doCollide_cell_segp(double cellHs,bool solidCell,const C3Vector& segP,const C3Vector& segL)
{ // Cell is at the origin
// Segment is defined as segP (end-point1) and segP+segL (end-point2)
C3Vector segHs(segL*0.5);
C3Vector segCenter(segP+segHs);
return(doCollide_cell_seg(cellHs,solidCell,segCenter,segHs));
}
bool CCalcUtils::doCollide_cell_pt(double cellHs,const C3Vector& pt)
{
if (fabs(pt(0))>cellHs)
return(false);
if (fabs(pt(1))>cellHs)
return(false);
if (fabs(pt(2))>cellHs)
return(false);
return(true);
}
bool CCalcUtils::doCollide_cell_seg(double cellHs,bool solidCell,const C3Vector& segCenter,const C3Vector& segHs)
{ // Cell is at the origin
// Segment is defined as segCenter (center of segment) and segHs (segment vector half-size)
// optimize!!!
C4X4Matrix box;
box.setIdentity();
C3Vector sv(cellHs,cellHs,cellHs);
return(doCollide_box_seg(box,sv,solidCell,segCenter,segHs));
}
bool CCalcUtils::doCollide_cell_tri(double cellHs,bool solidCell,const C3Vector& p,const C3Vector& v,const C3Vector& w)
{ // Cell is at the origin
// optimize!!!
C4X4Matrix box;
box.setIdentity();
C3Vector sv(cellHs,cellHs,cellHs);
return(doCollide_box_tri(box,sv,solidCell,p,v,w));
}
double CCalcUtils::getProjectedDistance_box_tri(const C3Vector& axis,const C4X4Matrix& box,const C3Vector& boxHs,const C3Vector& p,const C3Vector& v,const C3Vector& w)
{
double boxPProjDist=axis*(p-box.X);
C3Vector boxP(fabs(box.M.axis[0]*axis),fabs(box.M.axis[1]*axis),fabs(box.M.axis[2]*axis));
double boxOverlap=boxP*boxHs;
double triEdge1ProjDist=axis*v;
double triEdge2ProjDist=axis*w;
double overlapTriEdge1=0.0;
double overlapTriEdge2=0.0;
if (boxPProjDist*triEdge1ProjDist<0.0)
overlapTriEdge1=fabs(triEdge1ProjDist);
if (boxPProjDist*triEdge2ProjDist<0.0)
overlapTriEdge2=fabs(triEdge2ProjDist);
double retVal=fabs(boxPProjDist)-std::max<double>(overlapTriEdge1,overlapTriEdge2)-boxOverlap;
if (retVal<0.0)
retVal=0.0;
return(retVal);
}
double CCalcUtils::getProjectedDistance_box_segp(const C3Vector& axis,const C4X4Matrix& box,const C3Vector& boxHs,const C3Vector& segP,const C3Vector& segL)
{ // Segment is defined as segP (end-point1) and segP+segL (end-point2)
C3Vector segHs(segL*0.5);
C3Vector segCenter(segP+segHs);
return(getProjectedDistance_box_seg(axis,box,boxHs,segCenter,segHs));
}
double CCalcUtils::getProjectedDistance_box_seg(const C3Vector& axis,const C4X4Matrix& box,const C3Vector& boxHs,const C3Vector& segCenter,const C3Vector& segHs)
{ // Segment is defined as segCenter (center of segment) and segHs (segment vector half-size)
double boxSegProjDist=fabs(axis*(segCenter-box.X));
C3Vector boxP(fabs(box.M.axis[0]*axis),fabs(box.M.axis[1]*axis),fabs(box.M.axis[2]*axis));
double boxOverlap=boxP*boxHs;
double segOverlap=fabs(segHs*axis);
double retVal=boxSegProjDist-boxOverlap-segOverlap;
if (retVal<0.0)
retVal=0.0;
return(retVal);
}
double CCalcUtils::getProjectedDistance_box_pt(const C3Vector& axis,const C4X4Matrix& box,const C3Vector& boxHs,const C3Vector& pt)
{
double boxSegProjDist=fabs(axis*(pt-box.X));
C3Vector boxP(fabs(box.M.axis[0]*axis),fabs(box.M.axis[1]*axis),fabs(box.M.axis[2]*axis));
double boxOverlap=boxP*boxHs;
double retVal=boxSegProjDist-boxOverlap;
if (retVal<0.0)
retVal=0.0;
return(retVal);
}
double CCalcUtils::getProjectedDistance_cell_tri(const C3Vector& axis,double cellHs,const C3Vector& p,const C3Vector& v,const C3Vector& w)
{ // Cell is at the origin
// optimize!!!
C4X4Matrix box;
box.setIdentity();
C3Vector sv(cellHs,cellHs,cellHs);
return(getProjectedDistance_box_tri(axis,box,sv,p,v,w));
}
double CCalcUtils::getProjectedDistance_cell_segp(const C3Vector& axis,double cellHs,const C3Vector& segP,const C3Vector& segL)
{ // Cell is at the origin
// Segment is defined as segP (end-point1) and segP+segL (end-point2)
C3Vector segHs(segL*0.5);
C3Vector segCenter(segP+segHs);
return(getProjectedDistance_cell_seg(axis,cellHs,segCenter,segHs));
}
double CCalcUtils::getProjectedDistance_cell_seg(const C3Vector& axis,double cellHs,const C3Vector& segCenter,const C3Vector& segHs)
{ // Cell is at the origin
// Segment is defined as segCenter (center of segment) and segHs (segment vector half-size)
// optimize!!!
C4X4Matrix box;
box.setIdentity();
C3Vector sv(cellHs,cellHs,cellHs);
return(getProjectedDistance_box_seg(axis,box,sv,segCenter,segHs));
}
double CCalcUtils::getProjectedDistance_tri_tri(const C3Vector& axis,const C3Vector& p1,const C3Vector& v1,const C3Vector& w1,const C3Vector& p2,const C3Vector& v2,const C3Vector& w2)
{
double p1p2ProjDist=axis*(p2-p1);
double tri1Edge1ProjDist=axis*v1;
double tri1Edge2ProjDist=axis*w1;
double tri2Edge1ProjDist=axis*v2;
double tri2Edge2ProjDist=axis*w2;
double overlapTri1Edge1=0.0;
double overlapTri1Edge2=0.0;
if (p1p2ProjDist*tri1Edge1ProjDist>0.0)
overlapTri1Edge1=fabs(tri1Edge1ProjDist);
if (p1p2ProjDist*tri1Edge2ProjDist>0.0)
overlapTri1Edge2=fabs(tri1Edge2ProjDist);
double overlapTri2Edge1=0.0;
double overlapTri2Edge2=0.0;
if (p1p2ProjDist*tri2Edge1ProjDist<0.0)
overlapTri2Edge1=fabs(tri2Edge1ProjDist);
if (p1p2ProjDist*tri2Edge2ProjDist<0.0)
overlapTri2Edge2=fabs(tri2Edge2ProjDist);
double retVal=fabs(p1p2ProjDist)-std::max<double>(overlapTri1Edge1,overlapTri1Edge2)-std::max<double>(overlapTri2Edge1,overlapTri2Edge2);
if (retVal<0.0)
retVal=0.0;
return(retVal);
}
bool CCalcUtils::isApproxDistanceSmaller_box_box(const C4X4Matrix& box1,const C3Vector& box1Hs,const C4X4Matrix& box2,const C3Vector& box2Hs,double& dist)
{ // The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
if (dist==0.0)
return(false);
double d=0.0;
for (size_t i=0;i<3;i++)
{ // box 1's axes
double box1Box2Proj=fabs((box2.X-box1.X)*box1.M.axis[i]);
double box2Overlap=fabs(box2.M.axis[0]*box1.M.axis[i])*box2Hs(0)+fabs(box2.M.axis[1]*box1.M.axis[i])*box2Hs(1)+fabs(box2.M.axis[2]*box1.M.axis[i])*box2Hs(2);
double dd=box1Box2Proj-box1Hs(i)-box2Overlap;
if (dd>=dist)
return(false);
d=dd;
}
for (size_t i=0;i<3;i++)
{ // box 2's axes
double box1Box2Proj=fabs((box2.X-box1.X)*box2.M.axis[i]);
double box1Overlap=fabs(box1.M.axis[0]*box2.M.axis[i])*box1Hs(0)+fabs(box1.M.axis[1]*box2.M.axis[i])*box1Hs(1)+fabs(box1.M.axis[2]*box2.M.axis[i])*box1Hs(2);
double dd=box1Box2Proj-box2Hs(i)-box1Overlap;
if (dd>=dist)
return(false);
d=std::max<double>(d,dd);
}
for (size_t i=0;i<3;i++)
{
for (size_t j=0;j<3;j++)
{
C3Vector a(box1.M.axis[i]^box2.M.axis[j]);
if ( (a(0)!=0.0)||(a(1)!=0.0)||(a(2)!=0.0) )
{
a.normalize();
double box1Box2Proj=fabs((box2.X-box1.X)*a);
double box1Overlap=fabs(box1.M.axis[0]*a)*box1Hs(0)+fabs(box1.M.axis[1]*a)*box1Hs(1)+fabs(box1.M.axis[2]*a)*box1Hs(2);
double box2Overlap=fabs(box2.M.axis[0]*a)*box2Hs(0)+fabs(box2.M.axis[1]*a)*box2Hs(1)+fabs(box2.M.axis[2]*a)*box2Hs(2);
double dd=box1Box2Proj-box1Overlap-box2Overlap;
if (dd>=dist)
return(false);
d=std::max<double>(d,dd);
}
}
}
if (d<0.0)
d=0.0;
dist=d;
return(true);
}
bool CCalcUtils::isApproxDistanceSmaller_box_box_fast(const C4X4Matrix& box1,const C3Vector& box1Hs,const C4X4Matrix& box2,const C3Vector& box2Hs,double& dist)
{ // Similar to isApproxDistanceSmaller_box_box, but only one projection axis is tested (i.e. the axis between the two boxes' origin)
// The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
if (dist==0.0)
return(false);
C3Vector axis((box2.X-box1.X).getNormalized());
double box1Box2Proj=fabs((box2.X-box1.X)*axis);
double box1Overlap=fabs(box1.M.axis[0]*axis)*box1Hs(0)+fabs(box1.M.axis[1]*axis)*box1Hs(1)+fabs(box1.M.axis[2]*axis)*box1Hs(2);
double box2Overlap=fabs(box2.M.axis[0]*axis)*box2Hs(0)+fabs(box2.M.axis[1]*axis)*box2Hs(1)+fabs(box2.M.axis[2]*axis)*box2Hs(2);
double d=box1Box2Proj-box1Overlap-box2Overlap;
if (d>=dist)
return(false);
if (d<0.0)
d=0.0;
dist=d;
return(true);
}
bool CCalcUtils::isApproxDistanceSmaller_box_cell(const C4X4Matrix& box,const C3Vector& boxHs,double cellHs,double& dist)
{ // The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
// Cell is at the origin
// optimize!!!
if (dist==0.0)
return(false);
C4X4Matrix box2;
box2.setIdentity();
C3Vector sv(cellHs,cellHs,cellHs);
return(isApproxDistanceSmaller_box_box(box2,sv,box,boxHs,dist));
}
bool CCalcUtils::isApproxDistanceSmaller_box_cell_fast(const C4X4Matrix& box,const C3Vector& boxHs,double cellHs,double& dist)
{ // Similar to isApproxDistanceSmaller_box_cell, but only one projection axis is tested (i.e. the axis between the two boxes' origin)
// The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
// Cell is at the origin
// optimize!!!
if (dist==0.0)
return(false);
C4X4Matrix box2;
box2.setIdentity();
C3Vector sv(cellHs,cellHs,cellHs);
return(isApproxDistanceSmaller_box_box_fast(box2,sv,box,boxHs,dist));
}
bool CCalcUtils::isApproxDistanceSmaller_box_tri(const C4X4Matrix& box,const C3Vector& boxHs,const C3Vector& p,const C3Vector& v,const C3Vector& w,double& dist)
{ // The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
if (dist==0.0)
return(false);
double d=0.0;
// tri normal vect:
C3Vector n((v^w).getNormalized());
double dd=getProjectedDistance_box_tri(n,box,boxHs,p,v,w);
if (dd>=dist)
return(false);
d=dd;
// Box's edges:
for (size_t i=0;i<3;i++)
{
double dd=getProjectedDistance_box_tri(box.M.axis[i],box,boxHs,p,v,w);
if (dd>=dist)
return(false);
d=std::max<double>(d,dd);
}
// Now check the tri edges ^ box's axes:
C3Vector u(w-v);
C3Vector trEdges[3]={v,w,u};
for (size_t i=0;i<3;i++)
{
for (size_t j=0;j<3;j++)
{
C3Vector axis=box.M.axis[i]^trEdges[j];
if ( (axis(0)!=0.0)||(axis(1)!=0.0)||(axis(2)!=0.0) )
{
double dd=getProjectedDistance_box_tri(axis.getNormalized(),box,boxHs,p,v,w);
if (dd>=dist)
return(false);
d=std::max<double>(d,dd);
}
}
}
if (d<0.0)
d=0.0;
dist=d;
return(true);
}
bool CCalcUtils::isApproxDistanceSmaller_box_tri_fast(const C4X4Matrix& box,const C3Vector& boxHs,const C3Vector& p,const C3Vector& v,const C3Vector& w,double& dist)
{ // Similar to isApproxDistanceSmaller_box_tri, but only one projection axis is tested (i.e. the axis between the two boxes' origin)
// The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
if (dist==0.0)
return(false);
C3Vector axis((p-box.X).getNormalized());
double d=getProjectedDistance_box_tri(axis,box,boxHs,p,v,w);
if (d>=dist)
return(false);
dist=d;
return(true);
}
bool CCalcUtils::isApproxDistanceSmaller_box_segp_fast(const C4X4Matrix& box,const C3Vector& boxHs,const C3Vector& segP,const C3Vector& segL,double& dist)
{ // Similar to isApproxDistanceSmaller_box_segp, but only one projection axis is tested (i.e. the axis between the two boxes' origin)
// The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
// Segment is defined as segP (end-point1) and segP+segL (end-point2)
C3Vector segHs(segL*0.5);
C3Vector segCenter(segP+segHs);
return(isApproxDistanceSmaller_box_seg_fast(box,boxHs,segCenter,segHs,dist));
}
bool CCalcUtils::isApproxDistanceSmaller_box_segp(const C4X4Matrix& box,const C3Vector& boxHs,const C3Vector& segP,const C3Vector& segL,double& dist)
{ // The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
// Segment is defined as segP (end-point1) and segP+segL (end-point2)
C3Vector segHs(segL*0.5);
C3Vector segCenter(segP+segHs);
return(isApproxDistanceSmaller_box_seg(box,boxHs,segCenter,segHs,dist));
}
bool CCalcUtils::isApproxDistanceSmaller_box_seg_fast(const C4X4Matrix& box,const C3Vector& boxHs,const C3Vector& segCenter,const C3Vector& segHs,double& dist)
{ // Similar to isApproxDistanceSmaller_box_seg, but only one projection axis is tested (i.e. the axis between the two boxes' origin)
// The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
// Segment is defined as segCenter (center of segment) and segHs (segment vector half-size)
if (dist==0.0)
return(false);
double d=getProjectedDistance_box_seg((segCenter-box.X).getNormalized(),box,boxHs,segCenter,segHs);
if (d>=dist)
return(false);
dist=d;
return(true);
}
bool CCalcUtils::isApproxDistanceSmaller_box_pt_fast(const C4X4Matrix& box,const C3Vector& boxHs,const C3Vector& pt,double& dist)
{ // The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
if (dist==0.0)
return(false);
double d=getProjectedDistance_box_pt((pt-box.X).getNormalized(),box,boxHs,pt);
if (d>=dist)
return(false);
dist=d;
return(true);
}
bool CCalcUtils::isApproxDistanceSmaller_box_seg(const C4X4Matrix& box,const C3Vector& boxHs,const C3Vector& segCenter,const C3Vector& segHs,double& dist)
{ // The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
// Segment is defined as segCenter (center of segment) and segHs (segment vector half-size)
if (dist==0.0)
return(false);
double d=0.0;
// Box-segment segment:
double dd=getProjectedDistance_box_seg((segCenter-box.X).getNormalized(),box,boxHs,segCenter,segHs);
if (dd>=dist)
return(false);
d=dd;
// Segment axis:
dd=getProjectedDistance_box_seg(segHs.getNormalized(),box,boxHs,segCenter,segHs);
if (dd>=dist)
return(false);
d=std::max<double>(d,dd);
// Box axes:
for (size_t i=0;i<3;i++)
{
dd=getProjectedDistance_box_seg(box.M.axis[i],box,boxHs,segCenter,segHs);
if (dd>=dist)
return(false);
d=std::max<double>(d,dd);
}
// Combination of Box and segment axes:
for (size_t i=0;i<3;i++)
{
if (!segHs.isColinear(box.M.axis[i],double(0.99999)))
{
dd=getProjectedDistance_box_seg((segHs^box.M.axis[i]).getNormalized(),box,boxHs,segCenter,segHs);
if (dd>=dist)
return(false);
d=std::max<double>(d,dd);
}
}
if (d<0.0)
d=0.0;
dist=d;
return(true);
}
bool CCalcUtils::isApproxDistanceSmaller_cell_tri(double cellHs,const C3Vector& p,const C3Vector& v,const C3Vector& w,double& dist)
{ // The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
// Cell is at the origin
// optimize!!!
if (dist==0.0)
return(false);
C4X4Matrix box;
box.setIdentity();
C3Vector sv(cellHs,cellHs,cellHs);
return(isApproxDistanceSmaller_box_tri(box,sv,p,v,w,dist));
}
bool CCalcUtils::isApproxDistanceSmaller_cell_tri_fast(double cellHs,const C3Vector& p,const C3Vector& v,const C3Vector& w,double& dist)
{ // Similar to isApproxDistanceSmaller_cell_tri, but only one projection axis is tested (i.e. the axis between the two boxes' origin)
// The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
// Cell is at the origin
// optimize!!!
if (dist==0.0)
return(false);
C4X4Matrix box;
box.setIdentity();
C3Vector sv(cellHs,cellHs,cellHs);
return(isApproxDistanceSmaller_box_tri_fast(box,sv,p,v,w,dist));
}
bool CCalcUtils::isApproxDistanceSmaller_cell_segp(double cellHs,const C3Vector& segP,const C3Vector& segL,double& dist)
{ // The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
// Cell is at the origin
// Segment is defined as segP (end-point1) and segP+segL (end-point2)
C3Vector segHs(segL*0.5);
C3Vector segCenter(segP+segHs);
return(isApproxDistanceSmaller_cell_seg(cellHs,segCenter,segHs,dist));
}
bool CCalcUtils::isApproxDistanceSmaller_cell_segp_fast(double cellHs,const C3Vector& segP,const C3Vector& segL,double& dist)
{ // Similar to isApproxDistanceSmaller_cell_segp, but only one projection axis is tested (i.e. the axis between the two boxes' origin)
// The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
// Cell is at the origin
// Segment is defined as segP (end-point1) and segP+segL (end-point2)
C3Vector segHs(segL*0.5);
C3Vector segCenter(segP+segHs);
return(isApproxDistanceSmaller_cell_seg_fast(cellHs,segCenter,segHs,dist));
}
bool CCalcUtils::isApproxDistanceSmaller_cell_seg(double cellHs,const C3Vector& segCenter,const C3Vector& segHs,double& dist)
{ // The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
// Cell is at the origin
// Segment is defined as segCenter (center of segment) and segHs (segment vector half-size)
// optimize!!!
if (dist==0.0)
return(false);
C4X4Matrix box;
box.setIdentity();
C3Vector sv(cellHs,cellHs,cellHs);
return(isApproxDistanceSmaller_box_seg(box,sv,segCenter,segHs,dist));
}
bool CCalcUtils::isApproxDistanceSmaller_cell_seg_fast(double cellHs,const C3Vector& segCenter,const C3Vector& segHs,double& dist)
{ // Similar to isApproxDistanceSmaller_cell_seg, but only one projection axis is tested (i.e. the axis between the two boxes' origin)
// The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
// Cell is at the origin
// Segment is defined as segCenter (center of segment) and segHs (segment vector half-size)
// optimize!!!
if (dist==0.0)
return(false);
C4X4Matrix box;
box.setIdentity();
C3Vector sv(cellHs,cellHs,cellHs);
return(isApproxDistanceSmaller_box_seg_fast(box,sv,segCenter,segHs,dist));
}
bool CCalcUtils::isApproxDistanceSmaller_tri_tri(const C3Vector& p1,const C3Vector& v1,const C3Vector& w1,const C3Vector& p2,const C3Vector& v2,const C3Vector& w2,double& dist)
{ // The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
if (dist==0.0)
return(false);
double d=0.0;
// Now check for separating axes:
C3Vector u1(w1-v1);
C3Vector u2(w2-v2);
C3Vector tr1Edges[3]={v1,w1,u1};
C3Vector tr2Edges[3]={v2,w2,u2};
C3Vector n1((v1^w1).getNormalized());
double dd=getProjectedDistance_tri_tri(n1,p1,v1,w1,p2,v2,w2);
if (dd>=dist)
return(false);
d=dd;
C3Vector n2((v2^w2).getNormalized());
dd=getProjectedDistance_tri_tri(n2,p1,v1,w1,p2,v2,w2);
if (dd>=dist)
return(false);
d=std::max<double>(d,dd);
for (size_t i=0;i<3;i++)
{
for (size_t j=0;j<3;j++)
{
C3Vector axis=tr1Edges[i]^tr2Edges[j];
if ( (axis(0)!=0.0)||(axis(1)!=0.0)||(axis(2)!=0.0) )
{ // Edges are not parallel
dd=getProjectedDistance_tri_tri(axis.getNormalized(),p1,v1,w1,p2,v2,w2);
if (dd>=dist)
return(false);
d=std::max<double>(d,dd);
}
}
}
if (d<0.0)
d=0.0;
dist=d;
return(true);
}
bool CCalcUtils::isApproxDistanceSmaller_tri_tri_fast(const C3Vector& p1,const C3Vector& v1,const C3Vector& w1,const C3Vector& p2,const C3Vector& v2,const C3Vector& w2,double& dist)
{ // Similar to isApproxDistanceSmaller_tri_tri, but only one projection axis is tested (i.e. the axis between the two boxes' origin)
// The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
if (dist==0.0)
return(false);
double d=getProjectedDistance_tri_tri((p2-p1).getNormalized(),p1,v1,w1,p2,v2,w2);
if (d>=dist)
return(false);
dist=d;
return(true);
}
bool CCalcUtils::isApproxDistanceSmaller_tri_pt_fast(const C3Vector& p,const C3Vector& v,const C3Vector& w,const C3Vector& pt,double& dist)
{ // The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
if (dist==0.0)
return(false);
C3Vector p_tri(p-pt);
double l=p_tri.getLength();
if (l>0.0)
{
p_tri/=l;
double triS1=0.0;
double triS1P=p_tri*v;
if (triS1P<0.0)
triS1=fabs(triS1P);
double triS2=0.0;
double triS2P=p_tri*w;
if (triS2P<0.0)
triS2=fabs(triS2P);
double d=l-std::max<double>(triS1,triS2);
if (d<dist)
{
if (d<0.0)
d=0.0;
dist=d;
return(true);
}
return(false);
}
dist=0.0;
return(true);
}
bool CCalcUtils::isApproxDistanceSmaller_segp_segp_fast(const C3Vector& seg1P,C3Vector seg1L,const C3Vector& seg2P,C3Vector seg2L,double& dist)
{ // The real distance is always larger or equal than the approximate distance
// dist is modified if return is true
if (dist==0.0)
return(false);
// check if projection on segCenter-segCenter axis is larger than dist
seg1L*=0.5;
seg2L*=0.5;
C3Vector distS((seg1P+seg1L)-(seg2P+seg2L));
double d=distS.getLength();
if (d!=0.0)
{
distS=distS/d;
d-=fabs(seg1L*distS);
d-=fabs(seg2L*distS);
if (d>=dist)
return(false);
}
dist=d;
return(true);
}
double CCalcUtils::getApproxDistance_box_box(const C4X4Matrix& box1,const C3Vector& box1Hs,const C4X4Matrix& box2,const C3Vector& box2Hs)
{ // Convenience function
double d=DBL_MAX;
isApproxDistanceSmaller_box_box_fast(box1,box1Hs,box2,box2Hs,d);
return(d);
}
double CCalcUtils::getApproxDistance_box_cell(const C4X4Matrix& box,const C3Vector& boxHs,double cellHs)
{ // Convenience function
double d=DBL_MAX;
isApproxDistanceSmaller_box_cell_fast(box,boxHs,cellHs,d);
return(d);
}
double CCalcUtils::getApproxDistance_box_tri(const C4X4Matrix& box,const C3Vector& boxHs,const C3Vector& p,const C3Vector& v,const C3Vector& w)
{ // Convenience function
double d=DBL_MAX;
isApproxDistanceSmaller_box_tri_fast(box,boxHs,p,v,w,d);
return(d);
}
double CCalcUtils::getApproxDistance_box_seg(const C4X4Matrix& box,const C3Vector& boxHs,const C3Vector& segCenter,const C3Vector& segHs)
{ // Convenience function
double d=DBL_MAX;
isApproxDistanceSmaller_box_seg_fast(box,boxHs,segCenter,segHs,d);
return(d);
}
double CCalcUtils::getApproxDistance_box_segp(const C4X4Matrix& box,const C3Vector& boxHs,const C3Vector& segP,const C3Vector& segL)
{ // Convenience function
double d=DBL_MAX;
isApproxDistanceSmaller_box_segp_fast(box,boxHs,segP,segL,d);
return(d);
}
double CCalcUtils::getApproxDistance_box_pt(const C4X4Matrix& box,const C3Vector& boxHs,const C3Vector& pt)
{ // Convenience function
double d=DBL_MAX;
isApproxDistanceSmaller_box_pt_fast(box,boxHs,pt,d);
return(d);
}
double CCalcUtils::getApproxDistance_cell_tri(double cellHs,const C3Vector& p,const C3Vector& v,const C3Vector& w)
{ // Convenience function
double d=DBL_MAX;
isApproxDistanceSmaller_cell_tri_fast(cellHs,p,v,w,d);
return(d);
}
double CCalcUtils::getApproxDistance_cell_seg(double cellHs,const C3Vector& segCenter,const C3Vector& segHs)
{ // Convenience function
double d=DBL_MAX;
isApproxDistanceSmaller_cell_seg_fast(cellHs,segCenter,segHs,d);
return(d);
}
double CCalcUtils::getApproxDistance_cell_segp(double cellHs,const C3Vector& segP,const C3Vector& segL)
{ // Convenience function
double d=DBL_MAX;
isApproxDistanceSmaller_cell_segp_fast(cellHs,segP,segL,d);
return(d);
}
double CCalcUtils::getApproxDistance_tri_tri(const C3Vector& p1,const C3Vector& v1,const C3Vector& w1,const C3Vector& p2,const C3Vector& v2,const C3Vector& w2)
{ // Convenience function
double d=DBL_MAX;
isApproxDistanceSmaller_tri_tri_fast(p1,v1,w1,p2,v2,w2,d);
return(d);
}
bool CCalcUtils::getDistance_box_box_alt(const C4X4Matrix& box1,const C3Vector& box1Hs,const C4X4Matrix& box2,const C3Vector& box2Hs,bool solidBoxes,double& dist,C3Vector* minDistSegPt1,C3Vector* minDistSegPt2)
{ // optimize! Does simple triangle/triangle dist. calc. between boxes' triangles
if (dist==0.0)
return(false);
// do first an approximate calculation:
double d=dist;
if (!isApproxDistanceSmaller_box_box_fast(box1,box1Hs,box2,box2Hs,d))
return(false);
bool retVal=false;
std::vector<C3Vector> b1pts;
std::vector<C3Vector> b2pts;
for (double z=-1.0;z<2.0;z+=2.0)
{
C3Vector a1;
C3Vector a2;