-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeom.cpp
1781 lines (1649 loc) · 82.1 KB
/
geom.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 "geom.h"
#include <iostream>
#include <cstdio>
#include "calcUtils.h"
bool geom_getMeshMeshCollision(const CObbStruct* mesh1ObbStruct,const C7Vector& mesh1Transformation,const CObbStruct* mesh2ObbStruct,const C7Vector& mesh2Transformation,std::vector<double>* intersections/*=nullptr*/,int* mesh1Caching/*=nullptr*/,int* mesh2Caching/*=nullptr*/)
{
bool retVal=false;
if ( (mesh1Caching!=nullptr)&&(mesh2Caching!=nullptr)&&(intersections==nullptr) )
{
if ( (mesh1Caching[0]>=0)&&(mesh1Caching[0]<mesh1ObbStruct->indices.size()/3)&&(mesh2Caching[0]>=0)&&(mesh2Caching[0]<mesh2ObbStruct->indices.size()/3) )
{
int tri1Ind[3]={mesh1ObbStruct->indices[3*mesh1Caching[0]+0],mesh1ObbStruct->indices[3*mesh1Caching[0]+1],mesh1ObbStruct->indices[3*mesh1Caching[0]+2]};
int tri2Ind[3]={mesh2ObbStruct->indices[3*mesh2Caching[0]+0],mesh2ObbStruct->indices[3*mesh2Caching[0]+1],mesh2ObbStruct->indices[3*mesh2Caching[0]+2]};
C3Vector p11(&mesh1ObbStruct->vertices[3*tri1Ind[0]]);
C3Vector p12(&mesh1ObbStruct->vertices[3*tri1Ind[1]]);
C3Vector p13(&mesh1ObbStruct->vertices[3*tri1Ind[2]]);
C3Vector p21(&mesh2ObbStruct->vertices[3*tri2Ind[0]]);
C3Vector p22(&mesh2ObbStruct->vertices[3*tri2Ind[1]]);
C3Vector p23(&mesh2ObbStruct->vertices[3*tri2Ind[2]]);
p11=mesh1Transformation*p11;
p12=mesh1Transformation*p12;
p13=mesh1Transformation*p13;
p21=mesh2Transformation*p21;
p22=mesh2Transformation*p22;
p23=mesh2Transformation*p23;
retVal=CCalcUtils::doCollide_tri_tri(p11,p12-p11,p13-p11,-1,p21,p22-p21,p23-p21,-1,nullptr,nullptr,nullptr);
}
}
retVal=retVal||mesh1ObbStruct->obb->checkCollision_obb(mesh1ObbStruct,mesh1Transformation.getMatrix(),mesh2ObbStruct->obb,mesh2ObbStruct,mesh2Transformation.getMatrix(),intersections,mesh1Caching,mesh2Caching);
return(retVal);
}
bool geom_getMeshOctreeCollision(const CObbStruct* meshObbStruct,const C7Vector& meshTransformation,const COcStruct* ocStruct,const C7Vector& octreeTransformation,int* meshCaching/*=nullptr*/,unsigned long long int* ocCaching/*=nullptr*/)
{
bool retVal=false;
if ( (meshCaching!=nullptr)&&(ocCaching!=nullptr) )
{
if ( (meshCaching[0]>=0)&&(meshCaching[0]<meshObbStruct->indices.size()/3) )
{
C4X4Matrix cellM;
double cellS;
if (ocStruct->getCell(octreeTransformation.getMatrix(),(const unsigned long long int)ocCaching[0],cellS,cellM,nullptr))
{
C4X4Matrix cellMi(cellM.getInverse());
int triInd[3]={meshObbStruct->indices[3*meshCaching[0]+0],meshObbStruct->indices[3*meshCaching[0]+1],meshObbStruct->indices[3*meshCaching[0]+2]};
C3Vector p1(&meshObbStruct->vertices[3*triInd[0]]);
C3Vector p2(&meshObbStruct->vertices[3*triInd[1]]);
C3Vector p3(&meshObbStruct->vertices[3*triInd[2]]);
p1=cellMi*meshTransformation*p1;
p2=cellMi*meshTransformation*p2;
p3=cellMi*meshTransformation*p3;
retVal=CCalcUtils::doCollide_cell_tri(cellS*0.5,true,p1,p2-p1,p3-p1);
}
}
}
retVal=retVal||ocStruct->doCollide_shape(octreeTransformation.getMatrix(),meshObbStruct,meshTransformation.getMatrix(),ocCaching,meshCaching);
return(retVal);
}
bool geom_getMeshTriangleCollision(const CObbStruct* meshObbStruct,const C7Vector& meshTransformation,const C3Vector& p,const C3Vector& v,const C3Vector& w,std::vector<double>* intersections/*=nullptr*/,int* caching/*=nullptr*/)
{
bool retVal=false;
if ( (caching!=nullptr)&&(intersections==nullptr) )
{
if ( (caching[0]>=0)&&(caching[0]<meshObbStruct->indices.size()/3) )
{
int triInd[3]={meshObbStruct->indices[3*caching[0]+0],meshObbStruct->indices[3*caching[0]+1],meshObbStruct->indices[3*caching[0]+2]};
C3Vector p1(&meshObbStruct->vertices[3*triInd[0]]);
C3Vector p2(&meshObbStruct->vertices[3*triInd[1]]);
C3Vector p3(&meshObbStruct->vertices[3*triInd[2]]);
p1=meshTransformation*p1;
p2=meshTransformation*p2;
p3=meshTransformation*p3;
retVal=CCalcUtils::doCollide_tri_tri(p1,p2-p1,p3-p1,-1,p,v,w,-1,nullptr,nullptr,nullptr);
}
}
retVal=retVal||meshObbStruct->obb->checkCollision_tri(meshObbStruct,meshTransformation.getMatrix(),p,v,w,0,intersections,caching,nullptr);
return(retVal);
}
bool geom_getMeshSegmentCollision(const CObbStruct* meshObbStruct,const C7Vector& meshTransformation,const C3Vector& segmentExtremity,const C3Vector& segmentVector,std::vector<double>* intersections/*=nullptr*/,int* caching/*=nullptr*/)
{
bool retVal=false;
if ( (caching!=nullptr)&&(intersections==nullptr) )
{
if ( (caching[0]>=0)&&(caching[0]<meshObbStruct->indices.size()/3) )
{
int triInd[3]={meshObbStruct->indices[3*caching[0]+0],meshObbStruct->indices[3*caching[0]+1],meshObbStruct->indices[3*caching[0]+2]};
C3Vector p1(&meshObbStruct->vertices[3*triInd[0]]);
C3Vector p2(&meshObbStruct->vertices[3*triInd[1]]);
C3Vector p3(&meshObbStruct->vertices[3*triInd[2]]);
p1=meshTransformation*p1;
p2=meshTransformation*p2;
p3=meshTransformation*p3;
retVal=CCalcUtils::doCollide_tri_segp(p1,p2-p1,p3-p1,-1,segmentExtremity,segmentVector,nullptr,nullptr);
}
}
retVal=retVal||meshObbStruct->obb->checkCollision_segp(meshObbStruct,meshTransformation.getMatrix(),segmentExtremity,segmentVector,intersections,caching);
return(retVal);
}
bool geom_getMeshSegmentCollision_withTriInfo(const CObbStruct* meshObbStruct,const C7Vector& meshTransformation,const C3Vector& segmentExtremity,const C3Vector& segmentVector,C3Vector& intersection,int& triangleIndex)
{
bool retVal=meshObbStruct->obb->checkCollision_segp_withTriInfo(meshObbStruct,meshTransformation.getMatrix(),segmentExtremity,segmentVector,intersection,triangleIndex);
return(retVal);
}
bool geom_getOctreeOctreeCollision(const COcStruct* oc1Struct,const C7Vector& octree1Transformation,const COcStruct* oc2Struct,const C7Vector& octree2Transformation,unsigned long long int* oc1Caching/*=nullptr*/,unsigned long long int* oc2Caching/*=nullptr*/)
{
bool retVal=false;
if ( (oc1Caching!=nullptr)&&(oc2Caching!=nullptr) )
{
C4X4Matrix cell1M;
double cell1S;
if (oc1Struct->getCell(octree1Transformation.getMatrix(),(const unsigned long long int)oc1Caching[0],cell1S,cell1M,nullptr))
{
C3Vector _cell1S(cell1S*0.5,cell1S*0.5,cell1S*0.5);
C4X4Matrix cell2M;
double cell2S;
if (oc2Struct->getCell(octree2Transformation.getMatrix(),(const unsigned long long int)oc2Caching[0],cell2S,cell2M,nullptr))
{
C3Vector _cell2S(cell2S*0.5,cell2S*0.5,cell2S*0.5);
retVal=CCalcUtils::doCollide_box_box(cell1M,_cell1S,cell2M,_cell2S,true);
}
}
}
retVal=retVal||oc1Struct->doCollide_octree(octree1Transformation.getMatrix(),oc2Struct,octree2Transformation.getMatrix(),oc1Caching,oc2Caching);
return(retVal);
}
bool geom_getOctreePtcloudCollision(const COcStruct* ocStruct,const C7Vector& octreeTransformation,const CPcStruct* pcStruct,const C7Vector& ptcloudTransformation,unsigned long long int* ocCaching/*=nullptr*/,unsigned long long int* pcCaching/*=nullptr*/)
{
bool retVal=false;
if ( (ocCaching!=nullptr)&&(pcCaching!=nullptr) )
{
C4X4Matrix cellM;
double cellS;
if (ocStruct->getCell(octreeTransformation.getMatrix(),(const unsigned long long int)ocCaching[0],cellS,cellM,nullptr))
{
C4X4Matrix tr;
size_t cnt=0;
const double* pts=pcStruct->getPoints(ptcloudTransformation.getMatrix(),(const unsigned long long int)pcCaching[0],&cnt,tr);
if ( (pts!=nullptr)&&(cnt>0) )
{
C4X4Matrix m(cellM.getInverse()*tr);
for (size_t i=0;i<cnt;i++)
{
C3Vector pt(pts+3*i);
pt*=m;
if (CCalcUtils::doCollide_cell_pt(cellS*0.5,pt))
{
retVal=true;
break;
}
}
}
}
}
retVal=retVal||ocStruct->doCollide_ptcloud(octreeTransformation.getMatrix(),pcStruct,ptcloudTransformation.getMatrix(),ocCaching,pcCaching);
return(retVal);
}
bool geom_getOctreeTriangleCollision(const COcStruct* ocStruct,const C7Vector& octreeTransformation,const C3Vector& p,const C3Vector& v,const C3Vector& w,unsigned long long int* caching/*=nullptr*/)
{
bool retVal=false;
if (caching!=nullptr)
{
C4X4Matrix cellM;
double cellS;
if (ocStruct->getCell(octreeTransformation.getMatrix(),(const unsigned long long int)caching[0],cellS,cellM,nullptr))
{
C4X4Matrix cellMi(cellM.getInverse());
C3Vector _p(cellMi*p);
cellMi.X.clear();
C3Vector _v(cellMi*v);
C3Vector _w(cellMi*w);
retVal=CCalcUtils::doCollide_cell_tri(cellS*0.5,true,_p,_v,_w);
}
}
if (!retVal)
{
C7Vector trInv(octreeTransformation.getInverse());
C3Vector _p(trInv*p);
trInv.X.clear();
C3Vector _v(trInv*v);
C3Vector _w(trInv*w);
retVal=ocStruct->doCollide_tri(_p,_v,_w,nullptr,caching);
}
return(retVal);
}
bool geom_getOctreeSegmentCollision(const COcStruct* ocStruct,const C7Vector& octreeTransformation,const C3Vector& segmentExtremity,const C3Vector& segmentVector,unsigned long long int* caching/*=nullptr*/)
{
bool retVal=false;
if (caching!=nullptr)
{
C4X4Matrix cellM;
double cellS;
if (ocStruct->getCell(octreeTransformation.getMatrix(),(const unsigned long long int)caching[0],cellS,cellM,nullptr))
{
C4X4Matrix cellMi(cellM.getInverse());
C3Vector _segP(cellMi*segmentExtremity);
cellMi.X.clear();
C3Vector _segL(cellMi*segmentVector);
retVal=CCalcUtils::doCollide_cell_segp(cellS*0.5,true,_segP,_segL);
}
}
if (!retVal)
{
C7Vector trInv(octreeTransformation.getInverse());
C3Vector _segP(trInv*segmentExtremity);
trInv.X.clear();
C3Vector _segL(trInv*segmentVector);
retVal=ocStruct->doCollide_seg(_segP+_segL*0.5,_segL*0.5,nullptr,caching);
}
return(retVal);
}
bool geom_getOctreePointsCollision(const COcStruct* ocStruct,const C7Vector& octreeTransformation,const double* points,int pointCount)
{
bool retVal=false;
C7Vector trInv(octreeTransformation.getInverse());
std::vector<double> _points;
_points.resize(pointCount*3);
for (int i=0;i<pointCount;i++)
{
C3Vector p(points+3*i);
p*=trInv;
_points[3*i+0]=p(0);
_points[3*i+1]=p(1);
_points[3*i+2]=p(2);
}
retVal=ocStruct->doCollide_pts(_points);
return(retVal);
}
bool geom_getOctreePointCollision(const COcStruct* ocStruct,const C7Vector& octreeTransformation,const C3Vector& point,unsigned int* usrData/*=nullptr*/,unsigned long long int* caching/*=nullptr*/)
{
bool retVal=false;
if (caching!=nullptr)
{
C4X4Matrix cellM;
double cellS;
if (ocStruct->getCell(octreeTransformation.getMatrix(),(const unsigned long long int)caching[0],cellS,cellM,usrData))
{
C4X4Matrix cellMi(cellM.getInverse());
C3Vector _point(cellMi*point);
retVal=CCalcUtils::doCollide_cell_pt(cellS*0.5,_point);
}
}
if (!retVal)
{
C7Vector trInv(octreeTransformation.getInverse());
C3Vector _point(trInv*point);
retVal=ocStruct->doCollide_pt(_point,usrData,caching);
}
return(retVal);
}
bool geom_getBoxBoxCollision(const C7Vector& box1Transformation,const C3Vector& box1HalfSize,const C7Vector& box2Transformation,const C3Vector& box2HalfSize,bool boxesAreSolid)
{
bool retVal=CCalcUtils::doCollide_box_box(box1Transformation.getMatrix(),box1HalfSize,box2Transformation.getMatrix(),box2HalfSize,boxesAreSolid);
return(retVal);
}
bool geom_getBoxTriangleCollision(const C7Vector& boxTransformation,const C3Vector& boxHalfSize,bool boxIsSolid,const C3Vector& p,const C3Vector& v,const C3Vector& w)
{
bool retVal=CCalcUtils::doCollide_box_tri(boxTransformation.getMatrix(),boxHalfSize,boxIsSolid,p,v,w);
return(retVal);
}
bool geom_getBoxSegmentCollision(const C7Vector& boxTransformation,const C3Vector& boxHalfSize,bool boxIsSolid,const C3Vector& segmentEndPoint,const C3Vector& segmentVector)
{
bool retVal=CCalcUtils::doCollide_box_segp(boxTransformation.getMatrix(),boxHalfSize,boxIsSolid,segmentEndPoint,segmentVector);
return(retVal);
}
bool geom_getBoxPointCollision(const C7Vector& boxTransformation,const C3Vector& boxHalfSize,const C3Vector& point)
{
C3Vector pt(point);
pt*=boxTransformation.getInverse();
if (fabs(pt(0))>boxHalfSize(0))
return(false);
if (fabs(pt(1))>boxHalfSize(1))
return(false);
if (fabs(pt(2))>boxHalfSize(2))
return(false);
return(true);
}
bool geom_getTriangleTriangleCollision(const C3Vector& p1,const C3Vector& v1,const C3Vector& w1,const C3Vector& p2,const C3Vector& v2,const C3Vector& w2,std::vector<double>* intersections/*=nullptr*/)
{
bool retVal=false;
if (intersections!=nullptr)
{
size_t c=intersections->size();
if (CCalcUtils::doCollide_tri_tri(p1,v1,w1,-1,p2,v2,w2,-1,intersections,nullptr,nullptr))
retVal=(intersections->size()==c+6); // when intersections are computed, we might get different results for border-line cases
}
else
retVal=CCalcUtils::doCollide_tri_tri(p1,v1,w1,-1,p2,v2,w2,-1,nullptr,nullptr,nullptr);
return(retVal);
}
bool geom_getTriangleSegmentCollision(const C3Vector& p,const C3Vector& v,const C3Vector& w,const C3Vector& segmentEndPoint,const C3Vector& segmentVector,std::vector<double>* intersections/*=nullptr*/)
{
C3Vector _intersect;
C3Vector* intersect=nullptr;
if (intersections!=nullptr)
intersect=&_intersect;
bool retVal=CCalcUtils::doCollide_tri_segp(p,v,w,-1,segmentEndPoint,segmentVector,intersect,nullptr);
if (retVal)
{
intersections->push_back(_intersect(0));
intersections->push_back(_intersect(1));
intersections->push_back(_intersect(2));
intersections->push_back(_intersect(0));
intersections->push_back(_intersect(1));
intersections->push_back(_intersect(2));
}
return(retVal);
}
bool geom_getBoxBoxDistanceIfSmaller(const C7Vector& box1Transformation,const C3Vector& box1HalfSize,const C7Vector& box2Transformation,const C3Vector& box2HalfSize,bool boxesAreSolid,double& dist,C3Vector* distSegPt1/*=nullptr*/,C3Vector* distSegPt2/*=nullptr*/,bool altRoutine/*=false*/)
{
bool retVal;
if (altRoutine)
retVal=CCalcUtils::getDistance_box_box_alt(box1Transformation.getMatrix(),box1HalfSize,box2Transformation.getMatrix(),box2HalfSize,boxesAreSolid,dist,distSegPt1,distSegPt2);
else
retVal=CCalcUtils::getDistance_box_box(box1Transformation.getMatrix(),box1HalfSize,box2Transformation.getMatrix(),box2HalfSize,boxesAreSolid,dist,distSegPt1,distSegPt2);
return(retVal);
}
double geom_getBoxBoxDistance(const C7Vector& box1Transformation,const C3Vector& box1HalfSize,const C7Vector& box2Transformation,const C3Vector& box2HalfSize,bool boxesAreSolid,C3Vector* distSegPt1/*=nullptr*/,C3Vector* distSegPt2/*=nullptr*/,bool altRoutine/*=false*/)
{
double dist=DBL_MAX;
geom_getBoxBoxDistanceIfSmaller(box1Transformation.getMatrix(),box1HalfSize,box2Transformation.getMatrix(),box2HalfSize,boxesAreSolid,dist,distSegPt1,distSegPt2,altRoutine);
return(dist);
}
double geom_getApproxBoxBoxDistance(const C7Vector& box1Transformation,const C3Vector& box1HalfSize,const C7Vector& box2Transformation,const C3Vector& box2HalfSize)
{
double retVal=CCalcUtils::getApproxDistance_box_box(box1Transformation.getMatrix(),box1HalfSize,box2Transformation.getMatrix(),box2HalfSize);
return(retVal);
}
bool geom_getBoxCubeDistanceIfSmaller(const C7Vector& boxTransformation,const C3Vector& boxHalfSize,const C7Vector& cubeTransformation,double cubeHalfSize,bool boxAndCubeAreSolid,double& dist,C3Vector* distSegPt1/*=nullptr*/,C3Vector* distSegPt2/*=nullptr*/)
{
C3Vector chs(cubeHalfSize,cubeHalfSize,cubeHalfSize);
bool retVal=geom_getBoxBoxDistanceIfSmaller(boxTransformation.getMatrix(),boxHalfSize,cubeTransformation.getMatrix(),chs,boxAndCubeAreSolid,dist,distSegPt1,distSegPt2);
return(retVal);
}
double geom_getBoxCubeDistance(const C7Vector& boxTransformation,const C3Vector& boxHalfSize,const C7Vector& cubeTransformation,double cubeHalfSize,bool boxAndCubeAreSolid,C3Vector* distSegPt1/*=nullptr*/,C3Vector* distSegPt2/*=nullptr*/)
{
C3Vector chs(cubeHalfSize,cubeHalfSize,cubeHalfSize);
double dist=geom_getBoxBoxDistance(boxTransformation,boxHalfSize,cubeTransformation,chs,boxAndCubeAreSolid,distSegPt1,distSegPt2);
return(dist);
}
bool geom_getBoxTriangleDistanceIfSmaller(const C7Vector& boxTransformation,const C3Vector& boxHalfSize,bool boxIsSolid,const C3Vector& p,const C3Vector& v,const C3Vector& w,double& dist,C3Vector* distSegPt1/*=nullptr*/,C3Vector* distSegPt2/*=nullptr*/,bool altRoutine/*=false*/)
{
bool retVal;
if (altRoutine)
retVal=CCalcUtils::getDistance_box_tri_alt(boxTransformation.getMatrix(),boxHalfSize,boxIsSolid,p,v,w,dist,distSegPt1,distSegPt2);
else
retVal=CCalcUtils::getDistance_box_tri(boxTransformation.getMatrix(),boxHalfSize,boxIsSolid,p,v,w,dist,distSegPt1,distSegPt2);
return(retVal);
}
double geom_getBoxTriangleDistance(const C7Vector& boxTransformation,const C3Vector& boxHalfSize,bool boxIsSolid,const C3Vector& p,const C3Vector& v,const C3Vector& w,C3Vector* distSegPt1/*=nullptr*/,C3Vector* distSegPt2/*=nullptr*/,bool altRoutine/*=false*/)
{
double dist=DBL_MAX;
geom_getBoxTriangleDistanceIfSmaller(boxTransformation,boxHalfSize,boxIsSolid,p,v,w,dist,distSegPt1,distSegPt2,altRoutine);
return(dist);
}
bool geom_getBoxSegmentDistanceIfSmaller(const C7Vector& boxTransformation,const C3Vector& boxHalfSize,bool boxIsSolid,const C3Vector& segmentEndPoint,const C3Vector& segmentVector,double& dist,C3Vector* distSegPt1/*=nullptr*/,C3Vector* distSegPt2/*=nullptr*/,bool altRoutine/*=false*/)
{
bool retVal;
if (altRoutine)
retVal=CCalcUtils::getDistance_box_segp_alt(boxTransformation.getMatrix(),boxHalfSize,boxIsSolid,segmentEndPoint,segmentVector,dist,distSegPt1,distSegPt2);
else
retVal=CCalcUtils::getDistance_box_segp(boxTransformation.getMatrix(),boxHalfSize,boxIsSolid,segmentEndPoint,segmentVector,dist,distSegPt1,distSegPt2);
return(retVal);
}
double geom_getBoxSegmentDistance(const C7Vector& boxTransformation,const C3Vector& boxHalfSize,bool boxIsSolid,const C3Vector& segmentEndPoint,const C3Vector& segmentVector,C3Vector* distSegPt1/*=nullptr*/,C3Vector* distSegPt2/*=nullptr*/,bool altRoutine/*=false*/)
{
double dist=DBL_MAX;
geom_getBoxSegmentDistanceIfSmaller(boxTransformation.getMatrix(),boxHalfSize,boxIsSolid,segmentEndPoint,segmentVector,dist,distSegPt1,distSegPt2,altRoutine);
return(dist);
}
bool geom_getBoxPointDistanceIfSmaller(const C7Vector& boxTransformation,const C3Vector& boxHalfSize,bool boxIsSolid,const C3Vector& point,double& dist,C3Vector* distSegPt1/*=nullptr*/)
{
bool retVal=CCalcUtils::getDistance_box_pt(boxTransformation.getMatrix(),boxHalfSize,boxIsSolid,point,dist,distSegPt1,nullptr);
return(retVal);
}
double geom_getBoxPointDistance(const C7Vector& boxTransformation,const C3Vector& boxHalfSize,bool boxIsSolid,const C3Vector& point,C3Vector* distSegPt1/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getBoxPointDistanceIfSmaller(boxTransformation,boxHalfSize,boxIsSolid,point,dist,distSegPt1);
return(dist);
}
bool geom_getTriangleTriangleDistanceIfSmaller(const C3Vector& p1,const C3Vector& v1,const C3Vector& w1,const C3Vector& p2,const C3Vector& v2,const C3Vector& w2,double& dist,C3Vector* minDistSegPt1/*=nullptr*/,C3Vector* minDistSegPt2/*=nullptr*/)
{
bool retVal=CCalcUtils::getDistance_tri_tri(p1,v1,w1,p2,v2,w2,dist,minDistSegPt1,minDistSegPt2);
return(retVal);
}
double geom_getTriangleTriangleDistance(const C3Vector& p1,const C3Vector& v1,const C3Vector& w1,const C3Vector& p2,const C3Vector& v2,const C3Vector& w2,C3Vector* minDistSegPt1/*=nullptr*/,C3Vector* minDistSegPt2/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getTriangleTriangleDistanceIfSmaller(p1,v1,w1,p2,v2,w2,dist,minDistSegPt1,minDistSegPt2);
return(dist);
}
bool geom_getTriangleSegmentDistanceIfSmaller(const C3Vector& p,const C3Vector& v,const C3Vector& w,const C3Vector& segmentEndPoint,const C3Vector& segmentVector,double& dist,C3Vector* minDistSegPt1/*=nullptr*/,C3Vector* minDistSegPt2/*=nullptr*/)
{
bool retVal=CCalcUtils::getDistance_tri_segp(p,v,w,segmentEndPoint,segmentVector,dist,minDistSegPt1,minDistSegPt2);
return(retVal);
}
double geom_getTriangleSegmentDistance(const C3Vector& p,const C3Vector& v,const C3Vector& w,const C3Vector& segmentEndPoint,const C3Vector& segmentVector,C3Vector* minDistSegPt1/*=nullptr*/,C3Vector* minDistSegPt2/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getTriangleSegmentDistanceIfSmaller(p,v,w,segmentEndPoint,segmentVector,dist,minDistSegPt1,minDistSegPt2);
return(dist);
}
bool geom_getTrianglePointDistanceIfSmaller(const C3Vector& p,const C3Vector& v,const C3Vector& w,const C3Vector& point,double& dist,C3Vector* minDistSegPt/*=nullptr*/)
{
bool retVal=CCalcUtils::getDistance_tri_pt(p,v,w,point,dist,minDistSegPt);
return(retVal);
}
double geom_getTrianglePointDistance(const C3Vector& p,const C3Vector& v,const C3Vector& w,const C3Vector& point,C3Vector* minDistSegPt/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getTrianglePointDistanceIfSmaller(p,v,w,point,dist,minDistSegPt);
return(dist);
}
bool geom_getSegmentSegmentDistanceIfSmaller(const C3Vector& segment1EndPoint,const C3Vector& segment1Vector,const C3Vector& segment2EndPoint,const C3Vector& segment2Vector,double& dist,C3Vector* minDistSegPt1/*=nullptr*/,C3Vector* minDistSegPt2/*=nullptr*/)
{
bool retVal=CCalcUtils::getDistance_segp_segp(segment1EndPoint,segment1Vector,segment2EndPoint,segment2Vector,dist,minDistSegPt1,minDistSegPt2);
return(retVal);
}
double geom_getSegmentSegmentDistance(const C3Vector& segment1EndPoint,const C3Vector& segment1Vector,const C3Vector& segment2EndPoint,const C3Vector& segment2Vector,C3Vector* minDistSegPt1/*=nullptr*/,C3Vector* minDistSegPt2/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getSegmentSegmentDistanceIfSmaller(segment1EndPoint,segment1Vector,segment2EndPoint,segment2Vector,dist,minDistSegPt1,minDistSegPt2);
return(dist);
}
bool geom_getSegmentPointDistanceIfSmaller(const C3Vector& segmentEndPoint,const C3Vector& segmentVector,const C3Vector& point,double& dist,C3Vector* minDistSegPt/*=nullptr*/)
{
bool retVal=CCalcUtils::getDistance_segp_pt(segmentEndPoint,segmentVector,point,dist,minDistSegPt);
return(retVal);
}
double geom_getSegmentPointDistance(const C3Vector& segmentEndPoint,const C3Vector& segmentVector,const C3Vector& point,C3Vector* minDistSegPt/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getSegmentPointDistanceIfSmaller(segmentEndPoint,segmentVector,point,dist,minDistSegPt);
return(dist);
}
CObbStruct* geom_createMesh(const double* vertices,int verticesSize,const int* indices,int indicesSize,const C7Vector* meshOrigin/*=nullptr*/,double triangleEdgeMaxLength/*=double(0.3)*/,int maxTrianglesInBoundingBox/*=8*/)
{
std::vector<double> _vertices;
_vertices.resize(size_t(verticesSize));
C7Vector trInv;
trInv.setIdentity();
if (meshOrigin!=nullptr)
trInv=meshOrigin->getInverse();
for (size_t i=0;i<size_t(verticesSize)/3;i++)
{
C3Vector v(vertices+3*i);
v*=trInv;
_vertices[3*i+0]=v(0);
_vertices[3*i+1]=v(1);
_vertices[3*i+2]=v(2);
}
CObbStruct* newObbStruct=CObbStruct::copyObbStructFromExisting(&_vertices[0],verticesSize,indices,indicesSize,triangleEdgeMaxLength,maxTrianglesInBoundingBox);
if (newObbStruct==nullptr)
newObbStruct=new CObbStruct(&_vertices[0],verticesSize,indices,indicesSize,triangleEdgeMaxLength,maxTrianglesInBoundingBox);
CObbStruct::addObbStruct(newObbStruct);
return(newObbStruct);
}
CObbStruct* geom_copyMesh(const CObbStruct* meshObbStruct)
{
CObbStruct* newObbStruct=meshObbStruct->copyYourself();
CObbStruct::addObbStruct(newObbStruct);
return(newObbStruct);
}
void geom_scaleMesh(CObbStruct* meshObbStruct,double scalingFactor)
{
meshObbStruct->scaleYourself(scalingFactor);
}
void geom_getMeshSerializationData(const CObbStruct* meshObbStruct,std::vector<unsigned char>& serializationData)
{
int s;
unsigned char* data=meshObbStruct->serialize(s);
serializationData.assign(data,data+s);
delete[] data;
}
CObbStruct* geom_getMeshFromSerializationData(const unsigned char* serializationData)
{
CObbStruct* newObbStruct=new CObbStruct();
if (newObbStruct->deserialize(serializationData))
{
CObbStruct::addObbStruct(newObbStruct);
return(newObbStruct);
}
else
{
delete newObbStruct;
return(nullptr);
}
}
void geom_destroyMesh(CObbStruct* meshObbStruct)
{
CObbStruct::removeObbStruct(meshObbStruct);
}
double geom_getMeshRootObbVolume(const CObbStruct* meshObbStruct)
{
return(meshObbStruct->obb->boxHs(0)*meshObbStruct->obb->boxHs(1)*meshObbStruct->obb->boxHs(2)*8);
}
bool geom_getMeshMeshDistanceIfSmaller(const CObbStruct* mesh1ObbStruct,const C7Vector& mesh1Transformation,const CObbStruct* mesh2ObbStruct,const C7Vector& mesh2Transformation,double& dist,C3Vector* minDistSegPt1/*=nullptr*/,C3Vector* minDistSegPt2/*=nullptr*/,int* mesh1Caching/*=nullptr*/,int* mesh2Caching/*=nullptr*/)
{
bool retVal=false;
if ( (mesh1Caching!=nullptr)&&(mesh2Caching!=nullptr) )
{
if ( (mesh1Caching[0]>=0)&&(mesh1Caching[0]<mesh1ObbStruct->indices.size()/3)&&(mesh2Caching[0]>=0)&&(mesh2Caching[0]<mesh2ObbStruct->indices.size()/3) )
{
int tri1Ind[3]={mesh1ObbStruct->indices[3*mesh1Caching[0]+0],mesh1ObbStruct->indices[3*mesh1Caching[0]+1],mesh1ObbStruct->indices[3*mesh1Caching[0]+2]};
int tri2Ind[3]={mesh2ObbStruct->indices[3*mesh2Caching[0]+0],mesh2ObbStruct->indices[3*mesh2Caching[0]+1],mesh2ObbStruct->indices[3*mesh2Caching[0]+2]};
C3Vector p11(&mesh1ObbStruct->vertices[3*tri1Ind[0]]);
C3Vector p12(&mesh1ObbStruct->vertices[3*tri1Ind[1]]);
C3Vector p13(&mesh1ObbStruct->vertices[3*tri1Ind[2]]);
C3Vector p21(&mesh2ObbStruct->vertices[3*tri2Ind[0]]);
C3Vector p22(&mesh2ObbStruct->vertices[3*tri2Ind[1]]);
C3Vector p23(&mesh2ObbStruct->vertices[3*tri2Ind[2]]);
p11=mesh1Transformation*p11;
p12=mesh1Transformation*p12;
p13=mesh1Transformation*p13;
p21=mesh2Transformation*p21;
p22=mesh2Transformation*p22;
p23=mesh2Transformation*p23;
retVal=CCalcUtils::getDistance_tri_tri(p11,p12-p11,p13-p11,p21,p22-p21,p23-p21,dist,minDistSegPt1,minDistSegPt2);
}
}
bool b=mesh1ObbStruct->obb->checkDistance_obb(mesh1ObbStruct,mesh1Transformation.getMatrix(),mesh2ObbStruct->obb,mesh2ObbStruct,mesh2Transformation.getMatrix(),dist,minDistSegPt1,minDistSegPt2,mesh1Caching,mesh2Caching);
retVal=retVal||b;
return(retVal);
}
bool geom_getMeshTriangleDistanceIfSmaller(const CObbStruct* meshObbStruct,const C7Vector& meshTransformation,const C3Vector& p,const C3Vector& v,const C3Vector& w,double& dist,C3Vector* minDistSegPt1/*=nullptr*/,C3Vector* minDistSegPt2/*=nullptr*/,int* caching/*=nullptr*/)
{
bool retVal=false;
if (caching!=nullptr)
{
if ( (caching[0]>=0)&&(caching[0]<meshObbStruct->indices.size()/3) )
{
int triInd[3]={meshObbStruct->indices[3*caching[0]+0],meshObbStruct->indices[3*caching[0]+1],meshObbStruct->indices[3*caching[0]+2]};
C3Vector p1(&meshObbStruct->vertices[3*triInd[0]]);
C3Vector p2(&meshObbStruct->vertices[3*triInd[1]]);
C3Vector p3(&meshObbStruct->vertices[3*triInd[2]]);
p1=meshTransformation*p1;
p2=meshTransformation*p2;
p3=meshTransformation*p3;
retVal=CCalcUtils::getDistance_tri_tri(p1,p2-p1,p3-p1,p,v,w,dist,minDistSegPt1,minDistSegPt2);
}
}
bool b=meshObbStruct->obb->checkDistance_tri(meshObbStruct,meshTransformation.getMatrix(),p,v,w,-1,dist,minDistSegPt1,minDistSegPt2,caching,nullptr);
retVal=retVal||b;
return(retVal);
}
double geom_getMeshTriangleDistance(const CObbStruct* meshObbStruct,const C7Vector& meshTransformation,const C3Vector& p,const C3Vector& v,const C3Vector& w,C3Vector* minDistSegPt1/*=nullptr*/,C3Vector* minDistSegPt2/*=nullptr*/,int* caching/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getMeshTriangleDistanceIfSmaller(meshObbStruct,meshTransformation,p,v,w,dist,minDistSegPt1,minDistSegPt2,caching);
return(dist);
}
double geom_getMeshMeshDistance(const CObbStruct* mesh1ObbStruct,const C7Vector& mesh1Transformation,const CObbStruct* mesh2ObbStruct,const C7Vector& mesh2Transformation,C3Vector* minDistSegPt1/*=nullptr*/,C3Vector* minDistSegPt2/*=nullptr*/,int* mesh1Caching/*=nullptr*/,int* mesh2Caching/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getMeshMeshDistanceIfSmaller(mesh1ObbStruct,mesh1Transformation,mesh2ObbStruct,mesh2Transformation,dist,minDistSegPt1,minDistSegPt2,mesh1Caching,mesh2Caching);
return(dist);
}
bool geom_getMeshSegmentDistanceIfSmaller(const CObbStruct* meshObbStruct,const C7Vector& meshTransformation,const C3Vector& segmentEndPoint,const C3Vector& segmentVector,double& dist,C3Vector* minDistSegPt1/*=nullptr*/,C3Vector* minDistSegPt2/*=nullptr*/,int* caching/*=nullptr*/)
{
bool retVal=false;
if (caching!=nullptr)
{
if ( (caching[0]>=0)&&(caching[0]<meshObbStruct->indices.size()/3) )
{
int triInd[3]={meshObbStruct->indices[3*caching[0]+0],meshObbStruct->indices[3*caching[0]+1],meshObbStruct->indices[3*caching[0]+2]};
C3Vector p1(&meshObbStruct->vertices[3*triInd[0]]);
C3Vector p2(&meshObbStruct->vertices[3*triInd[1]]);
C3Vector p3(&meshObbStruct->vertices[3*triInd[2]]);
p1=meshTransformation*p1;
p2=meshTransformation*p2;
p3=meshTransformation*p3;
retVal=CCalcUtils::getDistance_tri_segp(p1,p2-p1,p3-p1,segmentEndPoint,segmentVector,dist,minDistSegPt1,minDistSegPt2);
}
}
bool b=meshObbStruct->obb->checkDistance_segp(meshObbStruct,meshTransformation.getMatrix(),segmentEndPoint,segmentVector,dist,minDistSegPt1,minDistSegPt2,caching);
retVal=retVal||b;
return(retVal);
}
double geom_getMeshSegmentDistance(const CObbStruct* meshObbStruct,const C7Vector& meshTransformation,const C3Vector& segmentEndPoint,const C3Vector& segmentVector,C3Vector* minDistSegPt1/*=nullptr*/,C3Vector* minDistSegPt2/*=nullptr*/,int* caching/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getMeshSegmentDistanceIfSmaller(meshObbStruct,meshTransformation,segmentEndPoint,segmentVector,dist,minDistSegPt1,minDistSegPt2,caching);
return(dist);
}
bool geom_getMeshPointDistanceIfSmaller(const CObbStruct* meshObbStruct,const C7Vector& meshTransformation,const C3Vector& point,double& dist,C3Vector* minDistSegPt/*=nullptr*/,int* caching/*=nullptr*/)
{
bool retVal=false;
if (caching!=nullptr)
{
if ( (caching[0]>=0)&&(caching[0]<meshObbStruct->indices.size()/3) )
{
int triInd[3]={meshObbStruct->indices[3*caching[0]+0],meshObbStruct->indices[3*caching[0]+1],meshObbStruct->indices[3*caching[0]+2]};
C3Vector p1(&meshObbStruct->vertices[3*triInd[0]]);
C3Vector p2(&meshObbStruct->vertices[3*triInd[1]]);
C3Vector p3(&meshObbStruct->vertices[3*triInd[2]]);
p1=meshTransformation*p1;
p2=meshTransformation*p2;
p3=meshTransformation*p3;
retVal=CCalcUtils::getDistance_tri_pt(p1,p2-p1,p3-p1,point,dist,minDistSegPt);
}
}
bool b=meshObbStruct->obb->checkDistance_pt(meshObbStruct,meshTransformation.getMatrix(),point,dist,minDistSegPt,caching);
retVal=retVal||b;
return(retVal);
}
double geom_getMeshPointDistance(const CObbStruct* meshObbStruct,const C7Vector& meshTransformation,const C3Vector& point,C3Vector* minDistSegPt/*=nullptr*/,int* caching/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getMeshPointDistanceIfSmaller(meshObbStruct,meshTransformation,point,dist,minDistSegPt,caching);
return(dist);
}
bool geom_getMeshOctreeDistanceIfSmaller(const CObbStruct* meshObbStruct,const C7Vector& meshTransformation,const COcStruct* ocStruct,const C7Vector& octreeTransformation,double& dist,C3Vector* meshMinDistPt/*=nullptr*/,C3Vector* ocMinDistPt/*=nullptr*/,int* meshCaching/*=nullptr*/,unsigned long long int* ocCaching/*=nullptr*/)
{
bool retVal=false;
if ( (meshCaching!=nullptr)&&(ocCaching!=nullptr) )
{
if ( (meshCaching[0]>=0)&&(meshCaching[0]<meshObbStruct->indices.size()/3) )
{
C4X4Matrix cellM;
double cellS;
if (ocStruct->getCell(octreeTransformation.getMatrix(),(const unsigned long long int)ocCaching[0],cellS,cellM,nullptr))
{
C4X4Matrix cellMi(cellM.getInverse());
int triInd[3]={meshObbStruct->indices[3*meshCaching[0]+0],meshObbStruct->indices[3*meshCaching[0]+1],meshObbStruct->indices[3*meshCaching[0]+2]};
C3Vector p1(&meshObbStruct->vertices[3*triInd[0]]);
C3Vector p2(&meshObbStruct->vertices[3*triInd[1]]);
C3Vector p3(&meshObbStruct->vertices[3*triInd[2]]);
p1=cellMi*meshTransformation*p1;
p2=cellMi*meshTransformation*p2;
p3=cellMi*meshTransformation*p3;
retVal=CCalcUtils::getDistance_cell_tri(cellS*0.5,true,p1,p2-p1,p3-p1,dist,ocMinDistPt,meshMinDistPt);
if (retVal)
{
if (ocMinDistPt!=nullptr)
ocMinDistPt[0]=cellM*ocMinDistPt[0];
if (meshMinDistPt!=nullptr)
meshMinDistPt[0]=cellM*meshMinDistPt[0];
}
}
}
}
bool b=ocStruct->getDistance_shape(octreeTransformation.getMatrix(),meshObbStruct,meshTransformation.getMatrix(),dist,ocMinDistPt,meshMinDistPt,ocCaching,meshCaching);
retVal=retVal||b;
return(retVal);
}
double geom_getMeshOctreeDistance(const CObbStruct* meshObbStruct,const C7Vector& meshTransformation,const COcStruct* ocStruct,const C7Vector& octreeTransformation,C3Vector* meshMinDistPt/*=nullptr*/,C3Vector* ocMinDistPt/*=nullptr*/,int* meshCaching/*=nullptr*/,unsigned long long int* ocCaching/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getMeshOctreeDistanceIfSmaller(meshObbStruct,meshTransformation,ocStruct,octreeTransformation,dist,meshMinDistPt,ocMinDistPt,meshCaching,ocCaching);
return(dist);
}
bool geom_getMeshPtcloudDistanceIfSmaller(const CObbStruct* meshObbStruct,const C7Vector& meshTransformation,const CPcStruct* pcStruct,const C7Vector& pcTransformation,double& dist,C3Vector* meshMinDistPt/*=nullptr*/,C3Vector* pcMinDistPt/*=nullptr*/,int* meshCaching/*=nullptr*/,unsigned long long int* pcCaching/*=nullptr*/)
{
bool retVal=false;
if ( (meshCaching!=nullptr)&&(pcCaching!=nullptr) )
{
if ( (meshCaching[0]>=0)&&(meshCaching[0]<meshObbStruct->indices.size()/3) )
{
C4X4Matrix tr;
size_t cnt=0;
const double* pts=pcStruct->getPoints(pcTransformation.getMatrix(),(const unsigned long long int)pcCaching[0],&cnt,tr);
if ( (pts!=nullptr)&&(cnt>0) )
{
C4X4Matrix tri(tr.getInverse());
int triInd[3]={meshObbStruct->indices[3*meshCaching[0]+0],meshObbStruct->indices[3*meshCaching[0]+1],meshObbStruct->indices[3*meshCaching[0]+2]};
C3Vector p1(&meshObbStruct->vertices[3*triInd[0]]);
C3Vector p2(&meshObbStruct->vertices[3*triInd[1]]);
C3Vector p3(&meshObbStruct->vertices[3*triInd[2]]);
p1=tri*meshTransformation*p1;
p2=tri*meshTransformation*p2;
p3=tri*meshTransformation*p3;
for (size_t i=0;i<cnt;i++)
{
C3Vector pt(pts+3*i);
if (CCalcUtils::getDistance_tri_pt(p1,p2-p1,p3-p1,pt,dist,meshMinDistPt))
{
retVal=true;
if (pcMinDistPt!=nullptr)
pcMinDistPt[0]=tr*pt;
if (meshMinDistPt!=nullptr)
meshMinDistPt[0]=tr*meshMinDistPt[0];
}
}
}
}
}
bool b=pcStruct->getDistance_shape(meshObbStruct,pcTransformation.getMatrix(),meshTransformation.getMatrix(),dist,pcMinDistPt,meshMinDistPt,pcCaching,meshCaching);
retVal=retVal||b;
return(retVal);
}
double geom_getMeshPtcloudDistance(const CObbStruct* meshObbStruct,const C7Vector& meshTransformation,const CPcStruct* pcStruct,const C7Vector& pcTransformation,C3Vector* meshMinDistPt/*=nullptr*/,C3Vector* pcMinDistPt/*=nullptr*/,int* meshCaching/*=nullptr*/,unsigned long long int* pcCaching/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getMeshPtcloudDistanceIfSmaller(meshObbStruct,meshTransformation,pcStruct,pcTransformation,dist,meshMinDistPt,pcMinDistPt,meshCaching,pcCaching);
return(dist);
}
bool geom_getOctreePtcloudDistanceIfSmaller(const COcStruct* ocStruct,const C7Vector& octreeTransformation,const CPcStruct* pcStruct,const C7Vector& pcTransformation,double& dist,C3Vector* ocMinDistPt/*=nullptr*/,C3Vector* pcMinDistPt/*=nullptr*/,unsigned long long int* ocCaching/*=nullptr*/,unsigned long long int* pcCaching/*=nullptr*/)
{
bool retVal=false;
if ( (ocCaching!=nullptr)&&(pcCaching!=nullptr) )
{
C4X4Matrix cellM;
double cellS;
if (ocStruct->getCell(octreeTransformation.getMatrix(),(const unsigned long long int)ocCaching[0],cellS,cellM,nullptr))
{
C4X4Matrix tr;
size_t cnt=0;
const double* pts=pcStruct->getPoints(pcTransformation.getMatrix(),(const unsigned long long int)pcCaching[0],&cnt,tr);
if ( (pts!=nullptr)&&(cnt>0) )
{
C4X4Matrix m(cellM.getInverse()*tr);
for (size_t i=0;i<cnt;i++)
{
C3Vector pt(pts+3*i);
pt*=m;
if (CCalcUtils::getDistance_cell_pt(cellS*0.5,true,pt,dist,ocMinDistPt,nullptr))
{
retVal=true;
if (ocMinDistPt!=nullptr)
ocMinDistPt[0]=cellM*ocMinDistPt[0];
if (pcMinDistPt!=nullptr)
pcMinDistPt[0]=cellM*pt;
}
}
}
}
}
bool b=ocStruct->getDistance_ptcloud(octreeTransformation.getMatrix(),pcStruct,pcTransformation.getMatrix(),dist,ocMinDistPt,pcMinDistPt,ocCaching,pcCaching);
retVal=retVal||b;
return(retVal);
}
double geom_getOctreePtcloudDistance(const COcStruct* ocStruct,const C7Vector& octreeTransformation,const CPcStruct* pcStruct,const C7Vector& pcTransformation,C3Vector* ocMinDistPt/*=nullptr*/,C3Vector* pcMinDistPt/*=nullptr*/,unsigned long long int* ocCaching/*=nullptr*/,unsigned long long int* pcCaching/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getOctreePtcloudDistanceIfSmaller(ocStruct,octreeTransformation,pcStruct,pcTransformation,dist,ocMinDistPt,pcMinDistPt,ocCaching,pcCaching);
return(dist);
}
bool geom_getOctreeOctreeDistanceIfSmaller(const COcStruct* oc1Struct,const C7Vector& octree1Transformation,const COcStruct* oc2Struct,const C7Vector& octree2Transformation,double& dist,C3Vector* oc1MinDistPt/*=nullptr*/,C3Vector* oc2MinDistPt/*=nullptr*/,unsigned long long int* oc1Caching/*=nullptr*/,unsigned long long int* oc2Caching/*=nullptr*/)
{
bool retVal=false;
if ( (oc1Caching!=nullptr)&&(oc2Caching!=nullptr) )
{
C4X4Matrix cell1M;
double cell1S;
if (oc1Struct->getCell(octree1Transformation.getMatrix(),(const unsigned long long int)oc1Caching[0],cell1S,cell1M,nullptr))
{
C4X4Matrix cell2M;
double cell2S;
if (oc2Struct->getCell(octree2Transformation.getMatrix(),(const unsigned long long int)oc2Caching[0],cell2S,cell2M,nullptr))
retVal=CCalcUtils::getDistance_cell_cell(cell1M,cell1S*0.5,cell2M,cell2S*0.5,true,dist,oc1MinDistPt,oc2MinDistPt);
}
}
bool b=oc1Struct->getDistance_octree(octree1Transformation.getMatrix(),oc2Struct,octree2Transformation.getMatrix(),dist,oc1MinDistPt,oc2MinDistPt,oc1Caching,oc2Caching);
retVal=retVal||b;
return(retVal);
}
double geom_getOctreeOctreeDistance(const COcStruct* oc1Struct,const C7Vector& octree1Transformation,const COcStruct* oc2Struct,const C7Vector& octree2Transformation,C3Vector* oc1MinDistPt/*=nullptr*/,C3Vector* oc2MinDistPt/*=nullptr*/,unsigned long long int* oc1Caching/*=nullptr*/,unsigned long long int* oc2Caching/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getOctreeOctreeDistanceIfSmaller(oc1Struct,octree1Transformation,oc2Struct,octree2Transformation,dist,oc1MinDistPt,oc2MinDistPt,oc1Caching,oc2Caching);
return(dist);
}
bool geom_getOctreeTriangleDistanceIfSmaller(const COcStruct* ocStruct,const C7Vector& octreeTransformation,const C3Vector& p,const C3Vector& v,const C3Vector& w,double& dist,C3Vector* ocMinDistPt/*=nullptr*/,C3Vector* triMinDistPt/*=nullptr*/,unsigned long long int* ocCaching/*=nullptr*/)
{
bool retVal=false;
if (ocCaching!=nullptr)
{
C4X4Matrix cellM;
double cellS;
if (ocStruct->getCell(octreeTransformation.getMatrix(),(const unsigned long long int)ocCaching[0],cellS,cellM,nullptr))
{
C4X4Matrix cellMi(cellM.getInverse());
C3Vector _p(cellMi*p);
cellMi.X.clear();
C3Vector _v(cellMi*v);
C3Vector _w(cellMi*w);
retVal=CCalcUtils::getDistance_cell_tri(cellS*0.5,true,_p,_v,_w,dist,ocMinDistPt,triMinDistPt);
if (retVal)
{
if (ocMinDistPt!=nullptr)
ocMinDistPt[0]=cellM*ocMinDistPt[0];
if (triMinDistPt!=nullptr)
triMinDistPt[0]=cellM*triMinDistPt[0];
}
}
}
C7Vector trInv(octreeTransformation.getInverse());
C3Vector _p(trInv*p);
trInv.X.clear();
C3Vector _v(trInv*v);
C3Vector _w(trInv*w);
bool b=ocStruct->getDistance_tri(_p,_v,_w,dist,ocMinDistPt,triMinDistPt,ocCaching);
if (b)
{
if (ocMinDistPt!=nullptr)
ocMinDistPt[0]=octreeTransformation*ocMinDistPt[0];
if (triMinDistPt!=nullptr)
triMinDistPt[0]=octreeTransformation*triMinDistPt[0];
}
retVal=retVal||b;
return(retVal);
}
double geom_getOctreeTriangleDistance(const COcStruct* ocStruct,const C7Vector& octreeTransformation,const C3Vector& p,const C3Vector& v,const C3Vector&w,C3Vector* ocMinDistPt/*=nullptr*/,C3Vector* triMinDistPt/*=nullptr*/,unsigned long long int* ocCaching/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getOctreeTriangleDistanceIfSmaller(ocStruct,octreeTransformation,p,v,w,dist,ocMinDistPt,triMinDistPt,ocCaching);
return(dist);
}
bool geom_getOctreeSegmentDistanceIfSmaller(const COcStruct* ocStruct,const C7Vector& octreeTransformation,const C3Vector& segmentEndPoint,const C3Vector& segmentVector,double& dist,C3Vector* ocMinDistPt/*=nullptr*/,C3Vector* segMinDistPt/*=nullptr*/,unsigned long long int* ocCaching/*=nullptr*/)
{
bool retVal=false;
if (ocCaching!=nullptr)
{
C4X4Matrix cellM;
double cellS;
if (ocStruct->getCell(octreeTransformation.getMatrix(),(const unsigned long long int)ocCaching[0],cellS,cellM,nullptr))
{
C4X4Matrix cellMi(cellM.getInverse());
C3Vector _segP(cellMi*segmentEndPoint);
cellMi.X.clear();
C3Vector _segL(cellMi*segmentVector);
retVal=CCalcUtils::getDistance_cell_segp(cellS*0.5,true,_segP,_segL,dist,ocMinDistPt,segMinDistPt);
if (retVal)
{
if (ocMinDistPt!=nullptr)
ocMinDistPt[0]=cellM*ocMinDistPt[0];
if (segMinDistPt!=nullptr)
segMinDistPt[0]=cellM*segMinDistPt[0];
}
}
}
C7Vector trInv(octreeTransformation.getInverse());
C3Vector _segP(trInv*segmentEndPoint);
trInv.X.clear();
C3Vector _segL(trInv*segmentVector);
bool b=ocStruct->getDistance_seg(_segP+_segL*0.5,_segL*0.5,dist,ocMinDistPt,segMinDistPt,ocCaching);
if (b)
{
if (ocMinDistPt!=nullptr)
ocMinDistPt[0]=octreeTransformation*ocMinDistPt[0];
if (segMinDistPt!=nullptr)
segMinDistPt[0]=octreeTransformation*segMinDistPt[0];
}
retVal=retVal||b;
return(retVal);
}
double geom_getOctreeSegmentDistance(const COcStruct* ocStruct,const C7Vector& octreeTransformation,const C3Vector& segmentEndPoint,const C3Vector& segmentVector,C3Vector* ocMinDistPt/*=nullptr*/,C3Vector* segMinDistPt/*=nullptr*/,unsigned long long int* ocCaching/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getOctreeSegmentDistanceIfSmaller(ocStruct,octreeTransformation,segmentEndPoint,segmentVector,dist,ocMinDistPt,segMinDistPt,ocCaching);
return(dist);
}
bool geom_getOctreePointDistanceIfSmaller(const COcStruct* ocStruct,const C7Vector& octreeTransformation,const C3Vector& point,double& dist,C3Vector* ocMinDistPt/*=nullptr*/,unsigned long long int* ocCaching/*=nullptr*/)
{
bool retVal=false;
if (ocCaching!=nullptr)
{
C4X4Matrix cellM;
double cellS;
if (ocStruct->getCell(octreeTransformation.getMatrix(),(const unsigned long long int)ocCaching[0],cellS,cellM,nullptr))
{
C4X4Matrix cellMi(cellM.getInverse());
C3Vector _point(cellMi*point);
retVal=CCalcUtils::getDistance_cell_pt(cellS*0.5,true,_point,dist,ocMinDistPt,nullptr);
if (retVal)
{
if (ocMinDistPt!=nullptr)
ocMinDistPt[0]=cellM*ocMinDistPt[0];
}
}
}
C7Vector trInv(octreeTransformation.getInverse());
C3Vector _point(trInv*point);
bool b=ocStruct->getDistance_pt(_point,dist,ocMinDistPt,ocCaching);
if (b)
{
if (ocMinDistPt!=nullptr)
ocMinDistPt[0]=octreeTransformation*ocMinDistPt[0];
}
retVal=retVal||b;
return(retVal);
}
double geom_getOctreePointDistance(const COcStruct* ocStruct,const C7Vector& octreeTransformation,const C3Vector& point,C3Vector* ocMinDistPt/*=nullptr*/,unsigned long long int* ocCaching/*=nullptr*/)
{
double dist=DBL_MAX;
geom_getOctreePointDistanceIfSmaller(ocStruct,octreeTransformation,point,dist,ocMinDistPt,ocCaching);
return(dist);
}
bool geom_getPtcloudPtcloudDistanceIfSmaller(const CPcStruct* pc1Struct,const C7Vector& pc1Transformation,const CPcStruct* pc2Struct,const C7Vector& pc2Transformation,double& dist,C3Vector* pc1MinDistPt/*=nullptr*/,C3Vector* pc2MinDistPt/*=nullptr*/,unsigned long long int* pc1Caching/*=nullptr*/,unsigned long long int* pc2Caching/*=nullptr*/)
{
bool retVal=false;
if ( (pc1Caching!=nullptr)&&(pc2Caching!=nullptr) )
{
C4X4Matrix tr1;
size_t cnt1=0;
const double* pts1=pc1Struct->getPoints(pc1Transformation.getMatrix(),(const unsigned long long int)pc1Caching[0],&cnt1,tr1);
if ( (pts1!=nullptr)&&(cnt1>0) )
{
C4X4Matrix tr2;
size_t cnt2=0;
const double* pts2=pc2Struct->getPoints(pc2Transformation.getMatrix(),(const unsigned long long int)pc2Caching[0],&cnt2,tr2);
if ( (pts2!=nullptr)&&(cnt2>0) )
{
C4X4Matrix tr1Rel(tr2.getInverse()*tr1);
for (size_t i=0;i<cnt1;i++)
{
C3Vector pt1(pts1+3*i);
pt1*=tr1Rel;
for (size_t j=0;j<cnt2;j++)
{
C3Vector pt2(pts2+3*j);
double d=(pt2-pt1).getLength();
if (d<dist)
{
dist=d;
retVal=true;
if (pc1MinDistPt!=nullptr)
pc1MinDistPt[0]=tr2*pt1;
if (pc2MinDistPt!=nullptr)
pc2MinDistPt[0]=tr2*pt2;
}
}
}
}
}
}
bool b=pc1Struct->getDistance_ptcloud(pc2Struct,pc1Transformation.getMatrix(),pc2Transformation.getMatrix(),dist,pc1MinDistPt,pc2MinDistPt,pc1Caching,pc2Caching);
retVal=retVal||b;
return(retVal);
}