/
Algorithm.cpp
1961 lines (1681 loc) · 71.2 KB
/
Algorithm.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
/***************************************************************************
* Copyright (c) 2005 Imetric 3D GmbH *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <algorithm>
#endif
#include "Algorithm.h"
#include "Approximation.h"
#include "Elements.h"
#include "Iterator.h"
#include "Grid.h"
#include "Triangulation.h"
#include <Base/Console.h>
#include <Base/Sequencer.h>
using namespace MeshCore;
using Base::BoundBox3f;
using Base::BoundBox2D;
using Base::Polygon2D;
bool MeshAlgorithm::IsVertexVisible (const Base::Vector3f &rcVertex, const Base::Vector3f &rcView, const MeshFacetGrid &rclGrid ) const
{
Base::Vector3f cDirection = rcVertex-rcView;
float fDistance = cDirection.Length();
Base::Vector3f cIntsct; unsigned long uInd;
// search for the nearest facet to rcView in direction to rcVertex
if ( NearestFacetOnRay( rcView, cDirection, /*1.2f*fDistance,*/ rclGrid, cIntsct, uInd) )
{
// now check if the facet overlays the point
float fLen = Base::Distance( rcView, cIntsct );
if ( fLen < fDistance )
{
// is it the same point?
if ( Base::Distance(rcVertex, cIntsct) > 0.001f )
{
// ok facet overlays the vertex
return false;
}
}
}
return true; // no facet between the two points
}
bool MeshAlgorithm::NearestFacetOnRay (const Base::Vector3f &rclPt, const Base::Vector3f &rclDir, Base::Vector3f &rclRes,
unsigned long &rulFacet) const
{
Base::Vector3f clProj, clRes;
bool bSol = false;
unsigned long ulInd = 0;
// langsame Ausfuehrung ohne Grid
MeshFacetIterator clFIter(_rclMesh);
for (clFIter.Init(); clFIter.More(); clFIter.Next()) {
if (clFIter->Foraminate( rclPt, rclDir, clRes ) == true) {
if (bSol == false) { // erste Loesung
bSol = true;
clProj = clRes;
ulInd = clFIter.Position();
}
else { // liegt Punkt naeher
if ((clRes - rclPt).Length() < (clProj - rclPt).Length()) {
clProj = clRes;
ulInd = clFIter.Position();
}
}
}
}
if (bSol) {
rclRes = clProj;
rulFacet = ulInd;
}
return bSol;
}
bool MeshAlgorithm::NearestFacetOnRay (const Base::Vector3f &rclPt, const Base::Vector3f &rclDir, const MeshFacetGrid &rclGrid,
Base::Vector3f &rclRes, unsigned long &rulFacet) const
{
std::vector<unsigned long> aulFacets;
MeshGridIterator clGridIter(rclGrid);
if (clGridIter.InitOnRay(rclPt, rclDir, aulFacets) == true) {
if (RayNearestField(rclPt, rclDir, aulFacets, rclRes, rulFacet) == false) {
aulFacets.clear();
while (clGridIter.NextOnRay(aulFacets) == true) {
if (RayNearestField(rclPt, rclDir, aulFacets, rclRes, rulFacet) == true)
return true;
}
}
else
return true;
}
return false;
}
bool MeshAlgorithm::NearestFacetOnRay (const Base::Vector3f &rclPt, const Base::Vector3f &rclDir, float fMaxSearchArea,
const MeshFacetGrid &rclGrid, Base::Vector3f &rclRes, unsigned long &rulFacet) const
{
std::vector<unsigned long> aulFacets;
MeshGridIterator clGridIter(rclGrid);
if (clGridIter.InitOnRay(rclPt, rclDir, fMaxSearchArea, aulFacets) == true) {
if (RayNearestField(rclPt, rclDir, aulFacets, rclRes, rulFacet, 1.75f) == false) {
aulFacets.clear();
while (clGridIter.NextOnRay(aulFacets) == true) {
if (RayNearestField(rclPt, rclDir, aulFacets, rclRes, rulFacet, 1.75f) == true)
return true;
}
}
else
return true;
}
return false;
}
bool MeshAlgorithm::NearestFacetOnRay (const Base::Vector3f &rclPt, const Base::Vector3f &rclDir, const std::vector<unsigned long> &raulFacets,
Base::Vector3f &rclRes, unsigned long &rulFacet) const
{
Base::Vector3f clProj, clRes;
bool bSol = false;
unsigned long ulInd = 0;
for (std::vector<unsigned long>::const_iterator pI = raulFacets.begin(); pI != raulFacets.end(); ++pI) {
MeshGeomFacet rclSFacet = _rclMesh.GetFacet(*pI);
if (rclSFacet.Foraminate(rclPt, rclDir, clRes) == true) {
if (bSol == false) {// erste Loesung
bSol = true;
clProj = clRes;
ulInd = *pI;
}
else { // liegt Punkt naeher
if ((clRes - rclPt).Length() < (clProj - rclPt).Length()) {
clProj = clRes;
ulInd = *pI;
}
}
}
}
if (bSol) {
rclRes = clProj;
rulFacet = ulInd;
}
return bSol;
}
bool MeshAlgorithm::RayNearestField (const Base::Vector3f &rclPt, const Base::Vector3f &rclDir, const std::vector<unsigned long> &raulFacets,
Base::Vector3f &rclRes, unsigned long &rulFacet, float fMaxAngle) const
{
Base::Vector3f clProj, clRes;
bool bSol = false;
unsigned long ulInd = 0;
for (std::vector<unsigned long>::const_iterator pF = raulFacets.begin(); pF != raulFacets.end(); ++pF) {
if (_rclMesh.GetFacet(*pF).Foraminate(rclPt, rclDir, clRes/*, fMaxAngle*/) == true) {
if (bSol == false) { // erste Loesung
bSol = true;
clProj = clRes;
ulInd = *pF;
}
else { // liegt Punkt naeher
if ((clRes - rclPt).Length() < (clProj - rclPt).Length()) {
clProj = clRes;
ulInd = *pF;
}
}
}
}
if (bSol) {
rclRes = clProj;
rulFacet = ulInd;
}
return bSol;
}
bool MeshAlgorithm::FirstFacetToVertex(const Base::Vector3f &rPt, float fMaxDistance, const MeshFacetGrid &rGrid, unsigned long &uIndex) const
{
const float fEps = 0.001f;
bool found = false;
std::vector<unsigned long> facets;
// get the facets of the grid the point lies into
rGrid.GetElements(rPt, facets);
// Check all facets inside the grid if the point is part of it
for (std::vector<unsigned long>::iterator it = facets.begin(); it != facets.end(); ++it) {
MeshGeomFacet cFacet = this->_rclMesh.GetFacet(*it);
if (cFacet.IsPointOfFace(rPt, fMaxDistance)) {
found = true;
uIndex = *it;
break;
}
else {
// if not then check the distance to the border of the triangle
Base::Vector3f res = rPt;
float fDist;
unsigned short uSide;
cFacet.ProjectPointToPlane(res);
cFacet.NearestEdgeToPoint(res, fDist, uSide);
if (fDist < fEps) {
found = true;
uIndex = *it;
break;
}
}
}
return found;
}
float MeshAlgorithm::GetAverageEdgeLength() const
{
float fLen = 0.0f;
MeshFacetIterator cF(_rclMesh);
for (cF.Init(); cF.More(); cF.Next()) {
for (int i=0; i<3; i++)
fLen += Base::Distance(cF->_aclPoints[i], cF->_aclPoints[(i+1)%3]);
}
fLen = fLen / (3.0f * _rclMesh.CountFacets() );
return fLen;
}
Base::Vector3f MeshAlgorithm::GetGravityPoint() const
{
Base::Vector3f center;
MeshPointIterator cP(_rclMesh);
for (cP.Init(); cP.More(); cP.Next()) {
center += *cP;
}
return center / (float)_rclMesh.CountPoints();
}
void MeshAlgorithm::GetMeshBorders (std::list<std::vector<Base::Vector3f> > &rclBorders) const
{
std::vector<unsigned long> aulAllFacets(_rclMesh.CountFacets());
unsigned long k = 0;
for (std::vector<unsigned long>::iterator pI = aulAllFacets.begin(); pI != aulAllFacets.end(); ++pI)
*pI = k++;
GetFacetBorders(aulAllFacets, rclBorders);
}
void MeshAlgorithm::GetMeshBorders (std::list<std::vector<unsigned long> > &rclBorders) const
{
std::vector<unsigned long> aulAllFacets(_rclMesh.CountFacets());
unsigned long k = 0;
for (std::vector<unsigned long>::iterator pI = aulAllFacets.begin(); pI != aulAllFacets.end(); ++pI)
*pI = k++;
GetFacetBorders(aulAllFacets, rclBorders, true);
}
void MeshAlgorithm::GetFacetBorders (const std::vector<unsigned long> &raulInd, std::list<std::vector<Base::Vector3f> > &rclBorders) const
{
#if 1
const MeshPointArray &rclPAry = _rclMesh._aclPointArray;
std::list<std::vector<unsigned long> > aulBorders;
GetFacetBorders (raulInd, aulBorders, true);
for ( std::list<std::vector<unsigned long> >::iterator it = aulBorders.begin(); it != aulBorders.end(); ++it )
{
std::vector<Base::Vector3f> boundary;
boundary.reserve( it->size() );
for ( std::vector<unsigned long>::iterator jt = it->begin(); jt != it->end(); ++jt )
boundary.push_back(rclPAry[*jt]);
rclBorders.push_back( boundary );
}
#else
const MeshFacetArray &rclFAry = _rclMesh._aclFacetArray;
// alle Facets markieren die in der Indizie-Liste vorkommen
ResetFacetFlag(MeshFacet::VISIT);
for (std::vector<unsigned long>::const_iterator pIter = raulInd.begin(); pIter != raulInd.end(); pIter++)
rclFAry[*pIter].SetFlag(MeshFacet::VISIT);
std::list<std::pair<unsigned long, unsigned long> > aclEdges;
// alle Randkanten suchen und ablegen (unsortiert)
for (std::vector<unsigned long>::const_iterator pIter2 = raulInd.begin(); pIter2 != raulInd.end(); pIter2++)
{
const MeshFacet &rclFacet = rclFAry[*pIter2];
for (int i = 0; i < 3; i++)
{
unsigned long ulNB = rclFacet._aulNeighbours[i];
if (ulNB != ULONG_MAX)
{
if (rclFAry[ulNB].IsFlag(MeshFacet::VISIT) == true)
continue;
}
aclEdges.push_back(rclFacet.GetEdge(i));
}
}
if (aclEdges.size() == 0)
return; // no borders found (=> solid)
// Kanten aus der unsortieren Kantenliste suchen
const MeshPointArray &rclPAry = _rclMesh._aclPointArray;
unsigned long ulFirst, ulLast;
std::list<Base::Vector3f> clBorder;
ulFirst = aclEdges.begin()->first;
ulLast = aclEdges.begin()->second;
aclEdges.erase(aclEdges.begin());
clBorder.push_back(rclPAry[ulFirst]);
clBorder.push_back(rclPAry[ulLast]);
while (aclEdges.size() > 0)
{
// naechste anliegende Kante suchen
std::list<std::pair<unsigned long, unsigned long> >::iterator pEI;
for (pEI = aclEdges.begin(); pEI != aclEdges.end(); pEI++)
{
if (pEI->first == ulLast)
{
ulLast = pEI->second;
clBorder.push_back(rclPAry[ulLast]);
aclEdges.erase(pEI);
break;
}
else if (pEI->second == ulLast)
{
ulLast = pEI->first;
clBorder.push_back(rclPAry[ulLast]);
aclEdges.erase(pEI);
break;
}
else if (pEI->first == ulFirst)
{
ulFirst = pEI->second;
clBorder.push_front(rclPAry[ulFirst]);
aclEdges.erase(pEI);
break;
}
else if (pEI->second == ulFirst)
{
ulFirst = pEI->first;
clBorder.push_front(rclPAry[ulFirst]);
aclEdges.erase(pEI);
break;
}
}
if ((pEI == aclEdges.end()) || (ulLast == ulFirst))
{ // keine weitere Kante gefunden bzw. Polylinie geschlossen
rclBorders.push_back(std::vector<Base::Vector3f>(clBorder.begin(), clBorder.end()));
clBorder.clear();
if (aclEdges.size() > 0)
{ // neue Border anfangen
ulFirst = aclEdges.begin()->first;
ulLast = aclEdges.begin()->second;
aclEdges.erase(aclEdges.begin());
clBorder.push_back(rclPAry[ulFirst]);
clBorder.push_back(rclPAry[ulLast]);
}
}
}
#endif
}
void MeshAlgorithm::GetFacetBorders (const std::vector<unsigned long> &raulInd,
std::list<std::vector<unsigned long> > &rclBorders,
bool ignoreOrientation) const
{
const MeshFacetArray &rclFAry = _rclMesh._aclFacetArray;
// mark all facets that are in the indices list
ResetFacetFlag(MeshFacet::VISIT);
for (std::vector<unsigned long>::const_iterator it = raulInd.begin(); it != raulInd.end(); ++it)
rclFAry[*it].SetFlag(MeshFacet::VISIT);
// collect all boundary edges (unsorted)
std::list<std::pair<unsigned long, unsigned long> > aclEdges;
for (std::vector<unsigned long>::const_iterator it = raulInd.begin(); it != raulInd.end(); ++it) {
const MeshFacet &rclFacet = rclFAry[*it];
for (int i = 0; i < 3; i++) {
unsigned long ulNB = rclFacet._aulNeighbours[i];
if (ulNB != ULONG_MAX) {
if (rclFAry[ulNB].IsFlag(MeshFacet::VISIT) == true)
continue;
}
aclEdges.push_back(rclFacet.GetEdge(i));
}
}
if (aclEdges.size() == 0)
return; // no borders found (=> solid)
// search for edges in the unsorted list
unsigned long ulFirst, ulLast;
std::list<unsigned long> clBorder;
ulFirst = aclEdges.begin()->first;
ulLast = aclEdges.begin()->second;
aclEdges.erase(aclEdges.begin());
clBorder.push_back(ulFirst);
clBorder.push_back(ulLast);
while (aclEdges.size() > 0) {
// get adjacent edge
std::list<std::pair<unsigned long, unsigned long> >::iterator pEI;
for (pEI = aclEdges.begin(); pEI != aclEdges.end(); ++pEI) {
if (pEI->first == ulLast) {
ulLast = pEI->second;
clBorder.push_back(ulLast);
aclEdges.erase(pEI);
pEI = aclEdges.begin();
break;
}
else if (pEI->second == ulFirst) {
ulFirst = pEI->first;
clBorder.push_front(ulFirst);
aclEdges.erase(pEI);
pEI = aclEdges.begin();
break;
}
// Note: Using this might result into boundaries with wrong orientation.
// But if the mesh has some facets with wrong orientation we might get
// broken boundary curves.
else if (pEI->second == ulLast && ignoreOrientation) {
ulLast = pEI->first;
clBorder.push_back(ulLast);
aclEdges.erase(pEI);
pEI = aclEdges.begin();
break;
}
else if (pEI->first == ulFirst && ignoreOrientation) {
ulFirst = pEI->second;
clBorder.push_front(ulFirst);
aclEdges.erase(pEI);
pEI = aclEdges.begin();
break;
}
}
// Note: Calling erase on list iterators doesn't force a re-allocation and
// thus doesn't invalidate the iterator itself, only the referenced object
if ((pEI == aclEdges.end()) || aclEdges.empty() || (ulLast == ulFirst)) {
// no further edge found or closed polyline, respectively
rclBorders.push_back(std::vector<unsigned long>(clBorder.begin(), clBorder.end()));
clBorder.clear();
if (aclEdges.size() > 0) {
// start new boundary
ulFirst = aclEdges.begin()->first;
ulLast = aclEdges.begin()->second;
aclEdges.erase(aclEdges.begin());
clBorder.push_back(ulFirst);
clBorder.push_back(ulLast);
}
}
}
}
void MeshAlgorithm::GetMeshBorder(unsigned long uFacet, std::list<unsigned long>& rBorder) const
{
const MeshFacetArray &rFAry = _rclMesh._aclFacetArray;
std::list<std::pair<unsigned long, unsigned long> > openEdges;
if (uFacet >= rFAry.size())
return;
// add the open edge to the beginning of the list
MeshFacetArray::_TConstIterator face = rFAry.begin() + uFacet;
for (int i = 0; i < 3; i++)
{
if (face->_aulNeighbours[i] == ULONG_MAX)
openEdges.push_back(face->GetEdge(i));
}
if (openEdges.empty())
return; // facet is not a border facet
for (MeshFacetArray::_TConstIterator it = rFAry.begin(); it != rFAry.end(); ++it)
{
if (it == face)
continue;
for (int i = 0; i < 3; i++)
{
if (it->_aulNeighbours[i] == ULONG_MAX)
openEdges.push_back(it->GetEdge(i));
}
}
// Start with the edge that is associated to uFacet
unsigned long ulFirst = openEdges.begin()->first;
unsigned long ulLast = openEdges.begin()->second;
openEdges.erase(openEdges.begin());
rBorder.push_back(ulFirst);
rBorder.push_back(ulLast);
while (ulLast != ulFirst)
{
// find adjacent edge
std::list<std::pair<unsigned long, unsigned long> >::iterator pEI;
for (pEI = openEdges.begin(); pEI != openEdges.end(); ++pEI)
{
if (pEI->first == ulLast)
{
ulLast = pEI->second;
rBorder.push_back(ulLast);
openEdges.erase(pEI);
pEI = openEdges.begin();
break;
}
else if (pEI->second == ulFirst)
{
ulFirst = pEI->first;
rBorder.push_front(ulFirst);
openEdges.erase(pEI);
pEI = openEdges.begin();
break;
}
}
// cannot close the border
if (pEI == openEdges.end())
break;
}
}
void MeshAlgorithm::SplitBoundaryLoops( std::list<std::vector<unsigned long> >& aBorders )
{
// Count the number of open edges for each point
std::map<unsigned long, int> openPointDegree;
for (MeshFacetArray::_TConstIterator jt = _rclMesh._aclFacetArray.begin();
jt != _rclMesh._aclFacetArray.end(); ++jt) {
for (int i=0; i<3; i++) {
if (jt->_aulNeighbours[i] == ULONG_MAX) {
openPointDegree[jt->_aulPoints[i]]++;
openPointDegree[jt->_aulPoints[(i+1)%3]]++;
}
}
}
// go through all boundaries and split them if needed
std::list<std::vector<unsigned long> > aSplitBorders;
for (std::list<std::vector<unsigned long> >::iterator it = aBorders.begin();
it != aBorders.end(); ++it) {
bool split=false;
for (std::vector<unsigned long>::iterator jt = it->begin(); jt != it->end(); ++jt) {
// two (ore more) boundaries meet in one non-manifold point
if (openPointDegree[*jt] > 2) {
split = true;
break;
}
}
if (!split)
aSplitBorders.push_back( *it );
else
SplitBoundaryLoops( *it, aSplitBorders );
}
aBorders = aSplitBorders;
}
void MeshAlgorithm::SplitBoundaryLoops(const std::vector<unsigned long>& rBound,
std::list<std::vector<unsigned long> >& aBorders)
{
std::map<unsigned long, int> aPtDegree;
std::vector<unsigned long> cBound;
for (std::vector<unsigned long>::const_iterator it = rBound.begin(); it != rBound.end(); ++it) {
int deg = (aPtDegree[*it]++);
if (deg > 0) {
for (std::vector<unsigned long>::iterator jt = cBound.begin(); jt != cBound.end(); ++jt) {
if (*jt == *it) {
std::vector<unsigned long> cBoundLoop;
cBoundLoop.insert(cBoundLoop.end(), jt, cBound.end());
cBoundLoop.push_back(*it);
cBound.erase(jt, cBound.end());
aBorders.push_back(cBoundLoop);
(aPtDegree[*it]--);
break;
}
}
}
cBound.push_back(*it);
}
}
bool MeshAlgorithm::FillupHole(const std::vector<unsigned long>& boundary,
AbstractPolygonTriangulator& cTria,
MeshFacetArray& rFaces, MeshPointArray& rPoints,
int level, const MeshRefPointToFacets* pP2FStructure) const
{
if (boundary.front() == boundary.back()) {
// first and last vertex are identical
if (boundary.size() < 4)
return false; // something strange
}
else if (boundary.size() < 3) {
return false; // something strange
}
// Get a facet as reference coordinate system
MeshGeomFacet rTriangle;
MeshFacet rFace;
unsigned long refPoint0 = *(boundary.begin());
unsigned long refPoint1 = *(boundary.begin()+1);
if (pP2FStructure) {
const std::set<unsigned long>& ring1 = (*pP2FStructure)[refPoint0];
const std::set<unsigned long>& ring2 = (*pP2FStructure)[refPoint1];
std::vector<unsigned long> f_int;
std::set_intersection(ring1.begin(), ring1.end(), ring2.begin(), ring2.end(),
std::back_insert_iterator<std::vector<unsigned long> >(f_int));
if (f_int.size() != 1)
return false; // error, this must be an open edge!
rFace = _rclMesh._aclFacetArray[f_int.front()];
rTriangle = _rclMesh.GetFacet(rFace);
}
else {
bool ready = false;
for (MeshFacetArray::_TConstIterator it = _rclMesh._aclFacetArray.begin(); it != _rclMesh._aclFacetArray.end(); ++it) {
for (int i=0; i<3; i++) {
if (((it->_aulPoints[i] == refPoint0) && (it->_aulPoints[(i+1)%3] == refPoint1)) ||
((it->_aulPoints[i] == refPoint1) && (it->_aulPoints[(i+1)%3] == refPoint0))) {
rFace = *it;
rTriangle = _rclMesh.GetFacet(*it);
ready = true;
break;
}
}
if (ready)
break;
}
}
// add points to the polygon
std::vector<Base::Vector3f> polygon;
for (std::vector<unsigned long>::const_iterator jt = boundary.begin(); jt != boundary.end(); ++jt) {
polygon.push_back(_rclMesh._aclPointArray[*jt]);
rPoints.push_back(_rclMesh._aclPointArray[*jt]);
}
// remove the last added point if it is duplicated
std::vector<unsigned long> bounds = boundary;
if (boundary.front() == boundary.back()) {
bounds.pop_back();
polygon.pop_back();
rPoints.pop_back();
}
// There is no easy way to check whether the boundary is interior (a hole) or exterior before performing the triangulation.
// Afterwards we can compare the normals of the created triangles with the z-direction of our local coordinate system.
// If the scalar product is positive it was a hole, otherwise not.
cTria.SetPolygon(polygon);
cTria.SetIndices(bounds);
std::vector<Base::Vector3f> surf_pts = cTria.GetPolygon();
if (pP2FStructure && level > 0) {
std::set<unsigned long> index = pP2FStructure->NeighbourPoints(boundary, level);
for (std::set<unsigned long>::iterator it = index.begin(); it != index.end(); ++it) {
Base::Vector3f pt(_rclMesh._aclPointArray[*it]);
surf_pts.push_back(pt);
}
}
if (cTria.TriangulatePolygon()) {
// if we have enough points then we fit a surface through the points and project
// the added points onto this surface
cTria.PostProcessing(surf_pts);
// get the facets and add the additional points to the array
rFaces.insert(rFaces.end(), cTria.GetFacets().begin(), cTria.GetFacets().end());
std::vector<Base::Vector3f> newVertices = cTria.AddedPoints();
for (std::vector<Base::Vector3f>::iterator pt = newVertices.begin(); pt != newVertices.end(); ++pt) {
rPoints.push_back((*pt));
}
// Unfortunately, some algorithms do not care about the orientation of the polygon so we cannot rely on the normal
// criterion to decide whether it's a hole or not.
//
std::vector<MeshFacet> faces = cTria.GetFacets();
// Special case handling for a hole with three edges: the resulting facet might be coincident with the
// reference facet
if (faces.size()==1){
MeshFacet first = faces.front();
if (cTria.NeedsReindexing()) {
first._aulPoints[0] = boundary[first._aulPoints[0]];
first._aulPoints[1] = boundary[first._aulPoints[1]];
first._aulPoints[2] = boundary[first._aulPoints[2]];
}
if (first.IsEqual(rFace)) {
rFaces.clear();
rPoints.clear();
cTria.Discard();
return false;
}
}
// Get the new neighbour to our reference facet
MeshFacet facet;
unsigned short ref_side = rFace.Side(refPoint0, refPoint1);
unsigned short tri_side = USHRT_MAX;
if (cTria.NeedsReindexing()) {
// the referenced indices of the polyline
refPoint0 = 0;
refPoint1 = 1;
}
if (ref_side < USHRT_MAX) {
for (std::vector<MeshFacet>::iterator it = faces.begin(); it != faces.end(); ++it) {
tri_side = it->Side(refPoint0, refPoint1);
if (tri_side < USHRT_MAX) {
facet = *it;
break;
}
}
}
// in case the reference facet has not an open edge print a log message
if (ref_side == USHRT_MAX || tri_side == USHRT_MAX) {
Base::Console().Log("MeshAlgorithm::FillupHole: Expected open edge for facet <%d, %d, %d>\n",
rFace._aulPoints[0], rFace._aulPoints[1], rFace._aulPoints[2]);
rFaces.clear();
rPoints.clear();
cTria.Discard();
return false;
}
#if 1
MeshGeomFacet triangle;
triangle = cTria.GetTriangle(rPoints, facet);
// Now we have two adjacent triangles which we check for overlaps.
// Therefore we build a separation plane that must separate the two diametrically opposed points.
Base::Vector3f planeNormal = rTriangle.GetNormal() % (rTriangle._aclPoints[(ref_side+1)%3]-rTriangle._aclPoints[ref_side]);
Base::Vector3f planeBase = rTriangle._aclPoints[ref_side];
Base::Vector3f ref_point = rTriangle._aclPoints[(ref_side+2)%3];
Base::Vector3f tri_point = triangle._aclPoints[(tri_side+2)%3];
float ref_dist = (ref_point - planeBase) * planeNormal;
float tri_dist = (tri_point - planeBase) * planeNormal;
if (ref_dist * tri_dist > 0.0f) {
rFaces.clear();
rPoints.clear();
cTria.Discard();
return false;
}
// we know to have filled a polygon, now check for the orientation
if ( triangle.GetNormal() * rTriangle.GetNormal() <= 0.0f ) {
for (MeshFacetArray::_TIterator it = rFaces.begin(); it != rFaces.end(); ++it)
it->FlipNormal();
}
#endif
return true;
}
return false;
}
void MeshAlgorithm::SetFacetsProperty(const std::vector<unsigned long> &raulInds, const std::vector<unsigned long> &raulProps) const
{
if (raulInds.size() != raulProps.size()) return;
std::vector<unsigned long>::const_iterator iP = raulProps.begin();
for (std::vector<unsigned long>::const_iterator i = raulInds.begin(); i != raulInds.end(); ++i, ++iP)
_rclMesh._aclFacetArray[*i].SetProperty(*iP);
}
void MeshAlgorithm::SetFacetsFlag (const std::vector<unsigned long> &raulInds, MeshFacet::TFlagType tF) const
{
for (std::vector<unsigned long>::const_iterator i = raulInds.begin(); i != raulInds.end(); ++i)
_rclMesh._aclFacetArray[*i].SetFlag(tF);
}
void MeshAlgorithm::SetPointsFlag (const std::vector<unsigned long> &raulInds, MeshPoint::TFlagType tF) const
{
for (std::vector<unsigned long>::const_iterator i = raulInds.begin(); i != raulInds.end(); ++i)
_rclMesh._aclPointArray[*i].SetFlag(tF);
}
void MeshAlgorithm::GetFacetsFlag (std::vector<unsigned long> &raulInds, MeshFacet::TFlagType tF) const
{
raulInds.reserve(raulInds.size() + CountFacetFlag(tF));
MeshFacetArray::_TConstIterator beg = _rclMesh._aclFacetArray.begin();
MeshFacetArray::_TConstIterator end = _rclMesh._aclFacetArray.end();
for (MeshFacetArray::_TConstIterator it = beg; it != end; ++it) {
if (it->IsFlag(tF))
raulInds.push_back(it-beg);
}
}
void MeshAlgorithm::GetPointsFlag (std::vector<unsigned long> &raulInds, MeshPoint::TFlagType tF) const
{
raulInds.reserve(raulInds.size() + CountPointFlag(tF));
MeshPointArray::_TConstIterator beg = _rclMesh._aclPointArray.begin();
MeshPointArray::_TConstIterator end = _rclMesh._aclPointArray.end();
for (MeshPointArray::_TConstIterator it = beg; it != end; ++it) {
if (it->IsFlag(tF))
raulInds.push_back(it-beg);
}
}
void MeshAlgorithm::ResetFacetsFlag (const std::vector<unsigned long> &raulInds, MeshFacet::TFlagType tF) const
{
for (std::vector<unsigned long>::const_iterator i = raulInds.begin(); i != raulInds.end(); ++i)
_rclMesh._aclFacetArray[*i].ResetFlag(tF);
}
void MeshAlgorithm::ResetPointsFlag (const std::vector<unsigned long> &raulInds, MeshPoint::TFlagType tF) const
{
for (std::vector<unsigned long>::const_iterator i = raulInds.begin(); i != raulInds.end(); ++i)
_rclMesh._aclPointArray[*i].ResetFlag(tF);
}
void MeshAlgorithm::SetFacetFlag (MeshFacet::TFlagType tF) const
{
_rclMesh._aclFacetArray.SetFlag(tF);
}
void MeshAlgorithm::SetPointFlag (MeshPoint::TFlagType tF) const
{
_rclMesh._aclPointArray.SetFlag(tF);
}
void MeshAlgorithm::ResetFacetFlag (MeshFacet::TFlagType tF) const
{
_rclMesh._aclFacetArray.ResetFlag(tF);
}
void MeshAlgorithm::ResetPointFlag (MeshPoint::TFlagType tF) const
{
_rclMesh._aclPointArray.ResetFlag(tF);
}
unsigned long MeshAlgorithm::CountFacetFlag (MeshFacet::TFlagType tF) const
{
return std::count_if(_rclMesh._aclFacetArray.begin(), _rclMesh._aclFacetArray.end(),
std::bind2nd(MeshIsFlag<MeshFacet>(), tF));
}
unsigned long MeshAlgorithm::CountPointFlag (MeshPoint::TFlagType tF) const
{
return std::count_if(_rclMesh._aclPointArray.begin(), _rclMesh._aclPointArray.end(),
std::bind2nd(MeshIsFlag<MeshPoint>(), tF));
}
void MeshAlgorithm::GetFacetsFromToolMesh( const MeshKernel& rToolMesh, const Base::Vector3f& rcDir, std::vector<unsigned long> &raclCutted ) const
{
MeshFacetIterator cFIt(_rclMesh);
MeshFacetIterator cTIt(rToolMesh);
BoundBox3f cBB = rToolMesh.GetBoundBox();
Base::SequencerLauncher seq("Check facets...", _rclMesh.CountFacets());
// check all facets
Base::Vector3f tmp;
for (cFIt.Init(); cFIt.More(); cFIt.Next()) {
// check each point of each facet
for (int i=0; i<3; i++) {
// at least the point must be inside the bounding box of the tool mesh
if (cBB.IsInBox( cFIt->_aclPoints[i])) {
// should not cause performance problems since the tool mesh is usually rather lightweight
int ct=0;
for (cTIt.Init(); cTIt.More(); cTIt.Next()) {
if (cTIt->IsPointOfFace( cFIt->_aclPoints[i], MeshPoint::epsilon())) {
ct=1;
break; // the point lies on the tool mesh
}
else if (cTIt->Foraminate( cFIt->_aclPoints[i], rcDir, tmp)) {
// check if the intersection point lies in direction rcDir of the considered point
if ((tmp - cFIt->_aclPoints[i]) * rcDir > 0)
ct++;
}
}
// odd number => point is inside the tool mesh
if (ct % 2 == 1) {
raclCutted.push_back( cFIt.Position() );
break;
}
}
}
seq.next();
}
}
void MeshAlgorithm::GetFacetsFromToolMesh(const MeshKernel& rToolMesh, const Base::Vector3f& rcDir,
const MeshFacetGrid& rGrid, std::vector<unsigned long> &raclCutted) const
{
// iterator over grid structure
MeshGridIterator clGridIter(rGrid);
BoundBox3f cBB = rToolMesh.GetBoundBox();
Base::Vector3f tmp;
MeshFacetIterator cFIt(_rclMesh);
MeshFacetIterator cTIt(rToolMesh);
MeshAlgorithm cToolAlg(rToolMesh);
// To speed up the algorithm we use the grid built up from the associated mesh. For each grid
// element we check whether it lies completely inside or outside the toolmesh or even intersect
// with the toolmesh. So we can reduce the number of facets with further tests dramatically.
// If the grid box is outside the toolmesh all the facets inside can be skipped. If the grid
// box is inside the toolmesh all facets are stored with no further tests because they must
// also lie inside the toolmesh. Finally, if the grid box intersect with the toolmesh we must
// also check for each whether it intersect we the toolmesh as well.
std::vector<unsigned long> aulInds;
for (clGridIter.Init(); clGridIter.More(); clGridIter.Next()) {
int ret = cToolAlg.Surround(clGridIter.GetBoundBox(), rcDir);
// the box is completely inside the toolmesh
if (ret == 1) {
// these facets can be removed without more checks
clGridIter.GetElements(raclCutted);
}
// the box intersect with toolmesh
else if (ret == 0) {
// these facets must be tested for intersectons with the toolmesh
clGridIter.GetElements(aulInds);
}
// the box is outside the toolmesh but this could still mean that the triangles
// inside the grid intersect with the toolmesh
else if (ret == -1) {
// these facets must be tested for intersectons with the toolmesh
clGridIter.GetElements(aulInds);
}
}
// remove duplicates
std::sort(aulInds.begin(), aulInds.end());
aulInds.erase(std::unique(aulInds.begin(), aulInds.end()), aulInds.end());
std::sort(raclCutted.begin(), raclCutted.end());
raclCutted.erase(std::unique(raclCutted.begin(), raclCutted.end()), raclCutted.end());
Base::SequencerLauncher seq("Check facets...", aulInds.size());
// check all facets
for (std::vector<unsigned long>::iterator it = aulInds.begin(); it != aulInds.end(); ++it) {
cFIt.Set(*it);
// check each point of each facet
for (int i=0; i<3; i++) {
// at least the point must be inside the bounding box of the tool mesh
if (cBB.IsInBox(cFIt->_aclPoints[i])) {
// should not cause performance problems since the tool mesh is usually rather lightweight
int ct=0;
for (cTIt.Init(); cTIt.More(); cTIt.Next()) {
if (cTIt->IsPointOfFace(cFIt->_aclPoints[i], MeshPoint::epsilon())) {
ct=1;
break; // the point lies on the tool mesh
}
else if (cTIt->Foraminate(cFIt->_aclPoints[i], rcDir, tmp)) {
// check if the intersection point lies in direction rcDir of the considered point
if ((tmp - cFIt->_aclPoints[i]) * rcDir > 0)
ct++;
}
}
// odd number => point is inside the tool mesh
if (ct % 2 == 1) {
raclCutted.push_back(cFIt.Position());
break;
}