1 // Created by: Peter KURNEV
2 // Copyright (c) 2010-2012 OPEN CASCADE SAS
3 // Copyright (c) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
4 // Copyright (c) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT,
5 // EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 // The content of this file is subject to the Open CASCADE Technology Public
8 // License Version 6.5 (the "License"). You may not use the content of this file
9 // except in compliance with the License. Please obtain a copy of the License
10 // at http://www.opencascade.org and read it completely before using this file.
12 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
13 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
15 // The Original Code and all software distributed under the License is
16 // distributed on an "AS IS" basis, without warranty of any kind, and the
17 // Initial Developer hereby disclaims all such warranties, including without
18 // limitation, any warranties of merchantability, fitness for a particular
19 // purpose or non-infringement. Please see the License for the specific terms
20 // and conditions governing the rights and limitations under the License.
22 #include <BOPAlgo_Builder.ixx>
24 #include <NCollection_IncAllocator.hxx>
26 #include <TopoDS_Shape.hxx>
27 #include <TopoDS_Face.hxx>
28 #include <TopoDS_Edge.hxx>
29 #include <TopoDS_Vertex.hxx>
30 #include <TopoDS_Compound.hxx>
32 #include <BRep_Tool.hxx>
33 #include <BRep_Builder.hxx>
35 #include <TopExp_Explorer.hxx>
37 #include <BOPCol_ListOfShape.hxx>
38 #include <BOPCol_ListOfInteger.hxx>
39 #include <BOPCol_MapOfInteger.hxx>
40 #include <BOPCol_DataMapOfIntegerListOfShape.hxx>
41 #include <BOPCol_DataMapOfShapeShape.hxx>
43 #include <BOPInt_Context.hxx>
45 #include <BOPDS_PaveBlock.hxx>
46 #include <BOPDS_ShapeInfo.hxx>
47 #include <BOPDS_DS.hxx>
48 #include <BOPDS_FaceInfo.hxx>
49 #include <BOPDS_MapOfPaveBlock.hxx>
50 #include <BOPDS_VectorOfInterfFF.hxx>
51 #include <BOPDS_Interf.hxx>
52 #include <BOPDS_VectorOfCurve.hxx>
53 #include <BOPDS_VectorOfPoint.hxx>
55 #include <BOPTools.hxx>
56 #include <BOPTools_AlgoTools.hxx>
57 #include <BOPTools_AlgoTools3D.hxx>
58 #include <BOPAlgo_BuilderFace.hxx>
59 #include <BOPTools_CoupleOfShape.hxx>
60 #include <BOPTools_ListOfCoupleOfShape.hxx>
61 #include <BOPTools_MapOfSet.hxx>
62 #include <BOPTools_DataMapOfShapeSet.hxx>
65 Standard_Boolean HasPaveBlocksOnIn(const BOPDS_FaceInfo& aFI1,
66 const BOPDS_FaceInfo& aFI2);
68 void FillMap(const TopoDS_Shape& aS1,
69 const TopoDS_Shape& aS2,
70 BOPCol_IndexedDataMapOfShapeListOfShape& aDMSLS,
71 Handle(NCollection_IncAllocator)& aAllocator);
73 void MakeBlocksCnx(const BOPCol_IndexedDataMapOfShapeListOfShape& aMILI,
74 BOPCol_DataMapOfIntegerListOfShape& aMBlocks,
75 Handle(NCollection_IncAllocator)& aAllocator);
77 //=======================================================================
78 //function : FillImagesFaces
80 //=======================================================================
81 void BOPAlgo_Builder::FillImagesFaces()
86 FillSameDomainFaces();
89 //=======================================================================
90 //function : BuildSplitFaces
92 //=======================================================================
93 void BOPAlgo_Builder::BuildSplitFaces()
95 Standard_Boolean bHasFaceInfo, bIsClosed, bIsDegenerated, bToReverse;
96 Standard_Integer i, j, aNbS, aNbPBIn, aNbPBOn, aNbPBSc, aNbAV, nSp;
97 TopoDS_Face aFF, aFSD;
99 TopAbs_Orientation anOriF, anOriE;
100 TopExp_Explorer aExp;
101 BOPCol_ListIteratorOfListOfShape aIt;
102 BOPCol_ListOfInteger aLIAV;
103 BOPCol_MapOfShape aMFence;
104 Handle(NCollection_IncAllocator) aAllocator;
105 BOPCol_ListOfShape aLFIm(myAllocator);
106 BOPCol_MapIteratorOfMapOfShape aItMS;
110 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~scope f
111 aAllocator=new NCollection_IncAllocator();
113 BOPCol_ListOfShape aLE(aAllocator);
114 BOPCol_MapOfShape aMDE(100, aAllocator);
116 aNbS=myDS->NbSourceShapes();
117 for (i=0; i<aNbS; ++i) {
118 const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
119 if (aSI.ShapeType()!=TopAbs_FACE) {
123 const TopoDS_Face& aF=(*(TopoDS_Face*)(&aSI.Shape()));
125 bHasFaceInfo=myDS->HasFaceInfo(i);
130 // aLFIm will contain images of aF
133 const BOPDS_FaceInfo& aFI=myDS->FaceInfo(i);
135 const BOPDS_IndexedMapOfPaveBlock& aMPBIn=aFI.PaveBlocksIn();
136 const BOPDS_IndexedMapOfPaveBlock& aMPBOn=aFI.PaveBlocksOn();
137 const BOPDS_IndexedMapOfPaveBlock& aMPBSc=aFI.PaveBlocksSc();
139 myDS->AloneVertices(i, aLIAV);
141 aNbPBIn=aMPBIn.Extent();
142 aNbPBOn=aMPBOn.Extent();
143 aNbPBSc=aMPBSc.Extent();
144 aNbAV=aLIAV.Extent();
145 if (!aNbPBIn && !aNbPBOn && !aNbPBSc && !aNbAV) { // not compete
151 anOriF=aF.Orientation();
153 aFF.Orientation(TopAbs_FORWARD);
157 // 1. Fill the egdes set for the face aFF -> LE
161 // 1.1 Bounding edges
162 aExp.Init(aFF, TopAbs_EDGE);
163 for (; aExp.More(); aExp.Next()) {
164 const TopoDS_Edge& aE=(*(TopoDS_Edge*)(&aExp.Current()));
165 anOriE=aE.Orientation();
166 bIsDegenerated=BRep_Tool::Degenerated(aE);
167 bIsClosed=BRep_Tool::IsClosed(aE, aF);
169 if (!myImages.IsBound(aE)) {
170 if (anOriE==TopAbs_INTERNAL) {
172 aEE.Orientation(TopAbs_FORWARD);
174 aEE.Orientation(TopAbs_REVERSED);
182 const BOPCol_ListOfShape& aLIE=myImages.Find(aE);
183 aIt.Initialize(aLIE);
184 for (; aIt.More(); aIt.Next()) {
185 aSp=(*(TopoDS_Edge*)(&aIt.Value()));
186 if (bIsDegenerated) {
187 aSp.Orientation(anOriE);
192 if (anOriE==TopAbs_INTERNAL) {
193 aSp.Orientation(TopAbs_FORWARD);
195 aSp.Orientation(TopAbs_REVERSED);
201 if (aMFence.Add(aSp)) {
202 if (!BRep_Tool::IsClosed(aSp, aF)){
203 BOPTools_AlgoTools3D::DoSplitSEAMOnFace(aSp, aF);
206 aSp.Orientation(TopAbs_FORWARD);
208 aSp.Orientation(TopAbs_REVERSED);
210 }// if (aMFence.Add(aSp))
214 aSp.Orientation(anOriE);
215 bToReverse=BOPTools_AlgoTools::IsSplitToReverse(aSp, aE, myContext);
220 }// for (; aIt.More(); aIt.Next()) {
222 }// for (; aExp.More(); aExp.Next()) {
226 for (j=1; j<=aNbPBIn; ++j) {
227 const Handle(BOPDS_PaveBlock)& aPB=aMPBIn(j);
229 aSp=(*(TopoDS_Edge*)(&myDS->Shape(nSp)));
231 aSp.Orientation(TopAbs_FORWARD);
233 aSp.Orientation(TopAbs_REVERSED);
239 for (j=1; j<=aNbPBSc; ++j) {
240 const Handle(BOPDS_PaveBlock)& aPB=aMPBSc(j);
242 aSp=(*(TopoDS_Edge*)(&myDS->Shape(nSp)));
244 aSp.Orientation(TopAbs_FORWARD);
246 aSp.Orientation(TopAbs_REVERSED);
250 // 3 Build split faces
251 BOPAlgo_BuilderFace aBF(aAllocator);
254 //aBF.SetContext(myContext);
262 const BOPCol_ListOfShape& aLFR=aBF.Areas();
263 aIt.Initialize(aLFR);
264 for (; aIt.More(); aIt.Next()) {
265 TopoDS_Shape& aFR=aIt.ChangeValue();
266 if (anOriF==TopAbs_REVERSED) {
267 aFR.Orientation(TopAbs_REVERSED);
269 //aFR.Orientation(anOriF);
273 mySplits.Bind(aF, aLFIm);
274 }// for (i=0; i<aNbS; ++i) {
276 aAllocator.Nullify();
277 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~scope t
279 //=======================================================================
280 //function : FillSameDomainFaces
282 //=======================================================================
283 void BOPAlgo_Builder::FillSameDomainFaces()
285 Standard_Boolean bFlag;
286 Standard_Integer i, aNbFFs, aNbCurves, aNbPoints, nF1, nF2, aNbS;
288 Handle(NCollection_IncAllocator) aAllocator;
289 BOPCol_ListIteratorOfListOfShape aItF1, aItF2;
290 BOPTools_ListOfCoupleOfShape aLCS;
291 BOPCol_ListIteratorOfListOfShape aItLS;
292 BOPCol_MapOfShape aMF;
293 BOPCol_MapIteratorOfMapOfShape aItMF;
297 const BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
299 aNbFFs=aFFs.Extent();
303 //-----------------------------------------------------scope f
304 aAllocator=new NCollection_IncAllocator();
305 BOPCol_IndexedDataMapOfShapeListOfShape aDMSLS(100, aAllocator);
306 BOPCol_DataMapOfIntegerListOfShape aMBlocks(100, aAllocator);
307 BOPTools_DataMapOfShapeSet aMSST(100, aAllocator);
309 BOPCol_ListOfShape aLFIm;
311 for (i=0; i<aNbFFs; ++i) {
312 const BOPDS_InterfFF& aFF=aFFs(i);
313 aFF.Indices(nF1, nF2);
315 const BOPDS_VectorOfCurve& aCurves=aFF.Curves();
316 aNbCurves=aCurves.Extent();
319 bFlag=Standard_False;
320 for (j=0; j<aNbCurves; ++j) {
321 const BOPDS_Curve& aNC=aCurves.Value(j);
333 const BOPDS_VectorOfPoint& aPoints=aFF.Points();
334 aNbPoints=aPoints.Extent();
339 if (!myDS->HasFaceInfo(nF1) || !myDS->HasFaceInfo(nF2) ) {
343 const BOPDS_FaceInfo& aFI1=myDS->FaceInfo(nF1);
344 const BOPDS_FaceInfo& aFI2=myDS->FaceInfo(nF2);
345 bFlag=HasPaveBlocksOnIn(aFI1, aFI2);
347 const TopoDS_Face& aF1=(*(TopoDS_Face*)(&myDS->Shape(nF1)));
348 const TopoDS_Face& aF2=(*(TopoDS_Face*)(&myDS->Shape(nF2)));
349 bFlag=bFlag && (mySplits.IsBound(aF1) && mySplits.IsBound(aF2));
352 //case when the faces have shared bounds
353 if (!aMSST.IsBound(aF1)) {
354 BOPTools_Set aST1(aAllocator);
355 aMSST.Bind(aF1, aST1);
356 BOPTools_Set& aST=aMSST.ChangeFind(aF1);
360 if (!aMSST.IsBound(aF2)) {
361 BOPTools_Set aST2(aAllocator);
362 aMSST.Bind(aF2, aST2);
363 BOPTools_Set& aST=aMSST.ChangeFind(aF2);
367 const BOPTools_Set& aST1=aMSST.Find(aF1);
368 const BOPTools_Set& aST2=aMSST.Find(aF2);
369 if (aST1.IsEqual(aST2)) {
370 bFlag=BOPTools_AlgoTools::AreFacesSameDomain(aF1, aF2, myContext);
372 FillMap(aF1, aF2, aDMSLS, aAllocator);
380 const BOPCol_ListOfShape& aLF1=mySplits.Find(aF1);
381 const BOPCol_ListOfShape& aLF2=mySplits.Find(aF2);
383 aItF1.Initialize(aLF1);
384 for (; aItF1.More(); aItF1.Next()) {
385 const TopoDS_Face& aF1x=(*(TopoDS_Face*)(&aItF1.Value()));
386 if (!aMSST.IsBound(aF1x)) {
387 BOPTools_Set aST1(aAllocator);
389 aMSST.Bind(aF1x, aST1);
390 BOPTools_Set& aST=aMSST.ChangeFind(aF1x);
394 aItF2.Initialize(aLF2);
395 for (; aItF2.More(); aItF2.Next()) {
396 const TopoDS_Face& aF2y=(*(TopoDS_Face*)(&aItF2.Value()));
397 if (!aMSST.IsBound(aF2y)) {
398 BOPTools_Set aST2(aAllocator);
400 aMSST.Bind(aF2y, aST2);
401 BOPTools_Set& aST=aMSST.ChangeFind(aF2y);
405 const BOPTools_Set& aST1=aMSST.Find(aF1x);
406 const BOPTools_Set& aST2=aMSST.Find(aF2y);
408 if (aST1.IsEqual(aST2)) {
409 bFlag=BOPTools_AlgoTools::AreFacesSameDomain(aF1x, aF2y, myContext);
411 FillMap(aF1x, aF2y, aDMSLS, aAllocator);
416 } // for (i=0; i<aNbFFs; ++i) {
419 MakeBlocksCnx(aDMSLS, aMBlocks, aAllocator);
421 // 3. Fill same domain faces map -> aMSDF
422 aNbS = aMBlocks.Extent();
423 for (i=0; i<aNbS; ++i) {
424 const BOPCol_ListOfShape& aLSD=aMBlocks.Find(i);
426 const TopoDS_Shape& aFSD1=aLSD.First();
427 aItLS.Initialize(aLSD);
428 for (; aItLS.More(); aItLS.Next()) {
429 const TopoDS_Shape& aFSD=aItLS.Value();
430 myShapesSD.Bind(aFSD, aFSD1);
435 aItMF.Initialize(aMF);
436 for (; aItMF.More(); aItMF.Next()){
437 const TopoDS_Shape& aF = aItMF.Value();
439 BOPCol_ListOfShape aLS;
441 mySplits.Bind(aF, aLS);
443 //-----------------------------------------------------scope t
447 aAllocator.Nullify();
449 //=======================================================================
450 // function: FillImagesFaces1
452 //=======================================================================
453 void BOPAlgo_Builder::FillImagesFaces1()
455 Standard_Integer i, aNbS, iSense;
457 BOPCol_ListOfInteger aLIAV;
458 BOPCol_ListOfShape aLFIm;
459 BOPCol_ListIteratorOfListOfShape aItLS;
461 aNbS=myDS->NbSourceShapes();
462 for (i=0; i<aNbS; ++i) {
463 const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(i);
464 if (aSI.ShapeType()!=TopAbs_FACE) {
468 const TopoDS_Face& aF=(*(TopoDS_Face*)(&aSI.Shape()));
470 if (!mySplits.IsBound(aF)) {
475 myDS->AloneVertices(i, aLIAV);
478 const BOPCol_ListOfShape& aLSp=mySplits.Find(aF);
479 aItLS.Initialize(aLSp);
480 for (; aItLS.More(); aItLS.Next()) {
481 const TopoDS_Face& aFSp=(*(TopoDS_Face*)(&aItLS.Value()));
482 if (!myShapesSD.IsBound(aFSp)) {
486 aFSD=(*(TopoDS_Face*)(&myShapesSD.Find(aFSp)));
487 iSense=BOPTools_AlgoTools::Sense(aFSp, aFSD);
495 FillInternalVertices(aLFIm, aLIAV);
497 myImages.Bind(aF, aLFIm);
500 aItLS.Initialize(aLFIm);
501 for (; aItLS.More(); aItLS.Next()) {
502 const TopoDS_Face& aFSp=(*(TopoDS_Face*)(&aItLS.Value()));
503 myOrigins.Bind(aFSp, aF);
505 }// for (i=0; i<aNbS; ++i) {
507 //=======================================================================
508 // function: FillInternalVertices
510 //=======================================================================
511 void BOPAlgo_Builder::FillInternalVertices(BOPCol_ListOfShape& aLFIm,
512 BOPCol_ListOfInteger& aLIAV)
514 Standard_Integer nV, iFlag;
515 Standard_Real aU1, aU2;
518 BOPCol_ListIteratorOfListOfInteger aItV;
519 BOPCol_ListIteratorOfListOfShape aItF;
521 aItV.Initialize(aLIAV);
522 for (; aItV.More(); aItV.Next()) {
524 aV=(*(TopoDS_Vertex*)(&myDS->Shape(nV)));
525 aV.Orientation(TopAbs_INTERNAL);
527 aItF.Initialize(aLFIm);
528 for (; aItF.More(); aItF.Next()) {
529 TopoDS_Face& aF=(*(TopoDS_Face*)(&aItF.Value()));
530 iFlag=myContext->ComputeVF(aV, aF, aU1, aU2);
538 //=======================================================================
539 //function : MakeBlocksCnx
541 //=======================================================================
542 void MakeBlocksCnx(const BOPCol_IndexedDataMapOfShapeListOfShape& aMILI,
543 BOPCol_DataMapOfIntegerListOfShape& aMBlocks,
544 Handle(NCollection_IncAllocator)& aAllocator)
546 Standard_Integer aNbV, aNbVS, aNbVP, aNbEC, k, i, j;
547 BOPCol_ListIteratorOfListOfShape aItLI;
549 BOPCol_MapOfShape aMVS(100, aAllocator);
550 BOPCol_IndexedMapOfShape aMEC(100, aAllocator);
551 BOPCol_IndexedMapOfShape aMVP(100, aAllocator);
552 BOPCol_IndexedMapOfShape aMVAdd(100, aAllocator);
556 for (k=0,i=1; i<=aNbV; ++i) {
562 const TopoDS_Shape& nV=aMILI.FindKey(i);
563 if (aMVS.Contains(nV)){
575 for (j=1; j<=aNbVP; ++j) {
576 const TopoDS_Shape& nVP=aMVP(j);
577 const BOPCol_ListOfShape& aLV=aMILI.FindFromKey(nVP);
578 aItLI.Initialize(aLV);
579 for (; aItLI.More(); aItLI.Next()) {
580 const TopoDS_Shape& nVx=aItLI.Value();
581 if (aMEC.Contains(nVx)) {
591 aNbVP=aMVAdd.Extent();
593 break; // from while(1)
597 for (j=1; j<=aNbVP; ++j) {
603 BOPCol_ListOfShape aLIx(aAllocator);
605 aNbEC = aMEC.Extent();
606 for (j=1; j<=aNbEC; ++j) {
607 const TopoDS_Shape& nVx=aMEC(j);
611 aMBlocks.Bind(k, aLIx);
613 }//for (k=0,i=1; i<=aNbV; ++i)
620 //=======================================================================
623 //=======================================================================
624 void FillMap(const TopoDS_Shape& aS1,
625 const TopoDS_Shape& aS2,
626 BOPCol_IndexedDataMapOfShapeListOfShape& aDMSLS,
627 Handle(NCollection_IncAllocator)& aAllocator)
629 if (aDMSLS.Contains(aS1)) {
630 BOPCol_ListOfShape& aLS=aDMSLS.ChangeFromKey(aS1);
634 BOPCol_ListOfShape aLS(aAllocator);
636 aDMSLS.Add(aS1, aLS);
639 if (aDMSLS.Contains(aS2)) {
640 BOPCol_ListOfShape& aLS=aDMSLS.ChangeFromKey(aS2);
644 BOPCol_ListOfShape aLS(aAllocator);
646 aDMSLS.Add(aS2, aLS);
649 //=======================================================================
650 //function :HasPaveBlocksOnIn
652 //=======================================================================
653 Standard_Boolean HasPaveBlocksOnIn(const BOPDS_FaceInfo& aFI1,
654 const BOPDS_FaceInfo& aFI2)
656 Standard_Boolean bRet;
657 BOPDS_MapIteratorOfMapOfPaveBlock aItMPB;
660 const BOPDS_IndexedMapOfPaveBlock& aMPBOn1=aFI1.PaveBlocksOn();
661 const BOPDS_IndexedMapOfPaveBlock& aMPBIn1=aFI1.PaveBlocksIn();
663 const BOPDS_IndexedMapOfPaveBlock& aMPBOn2=aFI2.PaveBlocksOn();
664 aItMPB.Initialize(aMPBOn2);
665 for (; aItMPB.More(); aItMPB.Next()) {
666 const Handle(BOPDS_PaveBlock)& aPB=aItMPB.Value();
667 bRet=aMPBOn1.Contains(aPB) || aMPBIn1.Contains(aPB);
673 const BOPDS_IndexedMapOfPaveBlock& aMPBIn2=aFI2.PaveBlocksIn();
674 aItMPB.Initialize(aMPBIn2);
675 for (; aItMPB.More(); aItMPB.Next()) {
676 const Handle(BOPDS_PaveBlock)& aPB=aItMPB.Value();
677 bRet=aMPBOn1.Contains(aPB) || aMPBIn1.Contains(aPB);
690 BOPCol_ListIteratorOfListOfShape aItx;
692 aBBx.MakeCompound(aCx);
694 aItx.Initialize(aLE);
695 for (; aItx.More(); aItx.Next()) {
696 const TopoDS_Shape& aEx=aItx.Value();