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.
23 #include <BOPAlgo_PaveFiller.ixx>
25 #include <Precision.hxx>
26 #include <NCollection_IncAllocator.hxx>
27 #include <NCollection_UBTreeFiller.hxx>
29 #include <Bnd_Box.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <TopoDS_Vertex.hxx>
33 #include <TopoDS_Compound.hxx>
34 #include <BRep_Tool.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRepTools.hxx>
37 #include <BRepBndLib.hxx>
39 #include <IntTools_EdgeEdge.hxx>
40 #include <IntTools_Range.hxx>
41 #include <IntTools_SequenceOfCommonPrts.hxx>
42 #include <IntTools_CommonPrt.hxx>
43 #include <IntTools_SequenceOfRanges.hxx>
45 #include <BOPTools_AlgoTools.hxx>
47 #include <BOPCol_DataMapOfShapeInteger.hxx>
48 #include <BOPCol_DataMapOfIntegerShape.hxx>
49 #include <BOPCol_IndexedDataMapOfShapeBox.hxx>
51 #include <BOPInt_Context.hxx>
52 #include <BOPInt_ShrunkRange.hxx>
53 #include <BOPInt_Tools.hxx>
55 #include <BOPDS_DataMapOfPaveBlockListOfPaveBlock.hxx>
56 #include <BOPDS_MapOfPaveBlock.hxx>
57 #include <BOPDS_CommonBlock.hxx>
58 #include <BOPDS_CoupleOfPaveBlocks.hxx>
59 #include <BOPDS_DataMapOfPaveBlockListOfInteger.hxx>
60 #include <BOPDS_Iterator.hxx>
61 #include <BOPDS_VectorOfInterfEE.hxx>
62 #include <BOPDS_Interf.hxx>
63 #include <BOPDS_Pave.hxx>
64 #include <BOPDS_BoxBndTree.hxx>
66 #include <BOPAlgo_Tools.hxx>
68 //=======================================================================
69 // function: PerformEE
71 //=======================================================================
72 void BOPAlgo_PaveFiller::PerformEE()
74 Standard_Boolean bJustAdd, bOrder;
75 Standard_Integer i, iX, iSize, nE1, nE2, aDiscretize;
76 Standard_Integer aNbCPrts, nWhat, nWith;
77 Standard_Real aTS11, aTS12, aTS21, aTS22;
78 Standard_Real aTolE1, aTolE2, aDeflection;
79 TopAbs_ShapeEnum aType;
80 TopoDS_Edge aEWhat, aEWith;
81 BOPDS_ListIteratorOfListOfPaveBlock aIt1, aIt2;
82 Handle(NCollection_IncAllocator) aAllocator;
86 myIterator->Initialize(TopAbs_EDGE, TopAbs_EDGE);
87 iSize=myIterator->ExpectedLength();
92 //-----------------------------------------------------scope f
93 aAllocator=new NCollection_IncAllocator();
94 BOPDS_IndexedDataMapOfPaveBlockListOfPaveBlock aMPBLPB(100, aAllocator);
95 BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks aMVCPB(100, aAllocator);
100 BOPDS_VectorOfInterfEE& aEEs=myDS->InterfEE();
101 aEEs.SetStartSize(iSize);
102 aEEs.SetIncrement(iSize);
105 for (; myIterator->More(); myIterator->Next()) {
106 myIterator->Value(nE1, nE2, bJustAdd);
111 const BOPDS_ShapeInfo& aSIE1=myDS->ShapeInfo(nE1);
112 if (aSIE1.HasFlag()){
115 const BOPDS_ShapeInfo& aSIE2=myDS->ShapeInfo(nE2);
116 if (aSIE2.HasFlag()){
120 const TopoDS_Edge& aE1=(*(TopoDS_Edge *)(&aSIE1.Shape()));
121 const TopoDS_Edge& aE2=(*(TopoDS_Edge *)(&aSIE2.Shape()));
123 aTolE1=BRep_Tool::Tolerance(aE1);
124 aTolE2=BRep_Tool::Tolerance(aE2);
126 BOPDS_ListOfPaveBlock& aLPB1=myDS->ChangePaveBlocks(nE1);
127 BOPDS_ListOfPaveBlock& aLPB2=myDS->ChangePaveBlocks(nE2);
129 aIt1.Initialize(aLPB1);
130 for (; aIt1.More(); aIt1.Next()) {
133 Handle(BOPDS_PaveBlock)& aPB1=aIt1.ChangeValue();
134 if (!aPB1->HasShrunkData()) {
135 FillShrunkData(aPB1);
136 if (myWarningStatus) {
140 aPB1->ShrunkData(aTS11, aTS12, aBB1);
142 aIt2.Initialize(aLPB2);
143 for (; aIt2.More(); aIt2.Next()) {
146 Handle(BOPDS_PaveBlock)& aPB2=aIt2.ChangeValue();
147 if (!aPB2->HasShrunkData()) {
148 FillShrunkData(aPB2);
149 if (myWarningStatus) {
153 aPB2->ShrunkData(aTS21, aTS22, aBB2);
155 if (aBB1.IsOut(aBB2)) {
161 //printf(" nE1=%d nE2=%d\n", nE1, nE2);
163 IntTools_EdgeEdge aEdgeEdge;
165 aEdgeEdge.SetEdge1 (aE1);
166 aEdgeEdge.SetEdge2 (aE2);
167 aEdgeEdge.SetTolerance1 (aTolE1);
168 aEdgeEdge.SetTolerance2 (aTolE2);
169 aEdgeEdge.SetDiscretize (aDiscretize);
170 aEdgeEdge.SetDeflection (aDeflection);
172 IntTools_Range aSR1(aTS11, aTS12);
173 IntTools_Range aSR2(aTS21, aTS22);
174 IntTools_Range anewSR1 = aSR1;
175 IntTools_Range anewSR2 = aSR2;
177 BOPTools_AlgoTools::CorrectRange (aE1, aE2, aSR1, anewSR1);
178 BOPTools_AlgoTools::CorrectRange (aE2, aE1, aSR2, anewSR2);
180 aEdgeEdge.SetRange1(anewSR1);
181 aEdgeEdge.SetRange2(anewSR2);
184 if (!aEdgeEdge.IsDone()) {
188 bOrder=aEdgeEdge.Order();
202 const IntTools_SequenceOfCommonPrts& aCPrts=aEdgeEdge.CommonParts();
204 aNbCPrts=aCPrts.Length();
205 for (i=1; i<=aNbCPrts; ++i) {
206 const IntTools_CommonPrt& aCPart=aCPrts(i);
209 case TopAbs_VERTEX: {
210 Standard_Boolean bIsOnPave1, bIsOnPave2;
211 Standard_Real aT1, aT2, aTol;
212 IntTools_Range aR1, aR2;
215 BOPInt_Tools::VertexParameters(aCPart, aT1, aT2);
216 aTol=Precision::Confusion();
218 //decide to keep the pave or not
219 aR1 = (bOrder) ? anewSR2 : anewSR1;
220 aR2 = (bOrder) ? anewSR1 : anewSR2;
222 bIsOnPave1=BOPInt_Tools::IsOnPave(aT1, aR1, aTol);
223 bIsOnPave2=BOPInt_Tools::IsOnPave(aT2, aR2, aTol);
225 if(bIsOnPave1 || bIsOnPave2) {
229 BOPTools_AlgoTools::MakeNewVertex(aEWhat, aT1, aEWith, aT2, aVnew);
232 Standard_Integer nV11, nV12, nV21, nV22, nVS[2], k, j, iFound;
233 Standard_Real aTolVx, aTolVnew, aD2, aDT2;
234 BOPCol_MapOfInteger aMV;
239 nV11=aPB1->Pave1().Index();
240 nV12=aPB1->Pave2().Index();
241 nV21=aPB2->Pave1().Index();
242 nV22=aPB2->Pave2().Index();
246 if (aMV.Contains(nV21)) {
250 if (aMV.Contains(nV22)) {
255 aTolVnew=BRep_Tool::Tolerance(aVnew);
256 aPnew=BRep_Tool::Pnt(aVnew);
258 for (k=0; k<=j; ++k) {
259 const TopoDS_Vertex& aVx= *(TopoDS_Vertex*)&(myDS->Shape(nVS[k]));
260 aTolVx=BRep_Tool::Tolerance(aVx);
261 aPx=BRep_Tool::Pnt(aVx);
262 aD2=aPnew.SquareDistance(aPx);
264 aDT2=100.*(aTolVnew+aTolVx)*(aTolVnew+aTolVx);
279 BOPDS_InterfEE& aEE=aEEs(iX);
280 aEE.SetIndices(nWhat, nWith);
281 aEE.SetCommonPart(aCPart);
283 myDS->AddInterf(nWhat, nWith);
285 BOPDS_CoupleOfPaveBlocks aCPB;
287 aCPB.SetPaveBlocks(aPB1, aPB2);
288 aCPB.SetIndexInterf(iX);
289 aMVCPB.Add(aVnew, aCPB);
290 }//case TopAbs_VERTEX:
294 Standard_Boolean bHasSameBounds;
295 Standard_Integer aNbComPrt2;
297 aNbComPrt2=aCPart.Ranges2().Length();
302 bHasSameBounds=aPB1->HasSameBounds(aPB2);
303 if (!bHasSameBounds) {
308 BOPDS_InterfEE& aEE=aEEs(iX);
309 aEE.SetIndices(nWhat, nWith);
310 aEE.SetCommonPart(aCPart);
312 myDS->AddInterf(nWhat, nWith);
314 BOPAlgo_Tools::FillMap(aPB1, aPB2, aMPBLPB, aAllocator);
320 }//for (i=1; i<=aNbCPrts; i++) {
323 }// for (; aIt2.More(); aIt2.Next()) {
324 }// for (; aIt1.More(); aIt1.Next()) {
327 //=========================================
329 //=========================================
330 BOPAlgo_Tools::PerformCommonBlocks(aMPBLPB, aAllocator, myDS);
331 PerformVerticesEE(aMVCPB, aAllocator);
332 //-----------------------------------------------------scope t
335 aAllocator.Nullify();
337 //=======================================================================
338 //function : PerformVertices
340 //=======================================================================
341 Standard_Integer BOPAlgo_PaveFiller::PerformVerticesEE
342 (BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks& theMVCPB,
343 Handle(NCollection_BaseAllocator)& theAllocator)
345 Standard_Integer aNbV, iRet;
348 aNbV=theMVCPB.Extent();
353 Standard_Integer nVx, iV, j, nE, iFlag, iX, i, aNb;
356 BOPCol_ListIteratorOfListOfShape aItLS;
357 BOPCol_ListIteratorOfListOfInteger aItLI;
358 BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
362 BOPDS_IndexedDataMapOfPaveBlockListOfInteger aMPBLI(100, theAllocator);
363 BOPCol_ListOfShape aLS(theAllocator);
364 BOPCol_IndexedDataMapOfShapeInteger aMVI(100, theAllocator);
365 BOPCol_IndexedDataMapOfShapeListOfShape aImages;
367 aSI.SetShapeType(TopAbs_VERTEX);
368 BOPDS_VectorOfInterfEE& aEEs=myDS->InterfEE();
370 // 1 prepare arguments
373 for (i=1; i<=aNbV; ++i) {
374 const TopoDS_Shape& aS=theMVCPB.FindKey(i);
375 const BOPDS_CoupleOfPaveBlocks& aCPB=theMVCPB.FindFromIndex(i);
376 iV=aCPB.IndexInterf();
381 TreatNewVertices(aMVI, aImages);
383 // 3 Add new vertices to myDS;
384 // connect indices to CPB structure
385 aNb = aImages.Extent();
386 for (i=1; i<=aNb; ++i) {
387 const TopoDS_Vertex& aV=(*(TopoDS_Vertex*)(&aImages.FindKey(i)));
388 const BOPCol_ListOfShape& aLVSD=aImages.FindFromIndex(i);
391 iV=myDS->Append(aSI);
393 BOPDS_ShapeInfo& aSIDS=myDS->ChangeShapeInfo(iV);
394 Bnd_Box& aBox=aSIDS.ChangeBox();
395 BRepBndLib::Add(aV, aBox);
397 aItLS.Initialize(aLVSD);
398 for (; aItLS.More(); aItLS.Next()) {
399 const TopoDS_Shape& aVx = aItLS.Value();
400 BOPDS_CoupleOfPaveBlocks &aCPB=theMVCPB.ChangeFromKey(aVx);
402 // update EE interference
403 iX=aCPB.IndexInterf();
404 BOPDS_InterfEE& aEE=aEEs(iX);
409 // 4 Map PaveBlock/ListOfVertices to add to this PaveBlock ->aMPBLI
411 Handle(BOPDS_PaveBlock) aPB[2];
413 for (i=1; i<=aNbV; ++i) {
414 const BOPDS_CoupleOfPaveBlocks& aCPB=theMVCPB.FindFromIndex(i);
416 aCPB.PaveBlocks(aPB[0], aPB[1]);
417 for (j=0; j<2; ++j) {
418 if (aMPBLI.Contains(aPB[j])) {
419 BOPCol_ListOfInteger& aLI=aMPBLI.ChangeFromKey(aPB[j]);
423 BOPCol_ListOfInteger aLI(theAllocator);
425 aMPBLI.Add(aPB[j], aLI);
432 // 5.1 Compute Extra Paves and
433 // 5.2. Add Extra Paves to the PaveBlocks
435 for(i=1; i<=aNb; ++i) {
436 Handle(BOPDS_PaveBlock) aPB=aMPBLI.FindKey(i);
437 nE=aPB->OriginalEdge();
438 const TopoDS_Edge& aE=(*(TopoDS_Edge *)(&myDS->Shape(nE)));
440 const BOPCol_ListOfInteger& aLI=aMPBLI.FindFromIndex(i);
441 aItLI.Initialize(aLI);
442 for (; aItLI.More(); aItLI.Next()) {
444 const TopoDS_Vertex& aVx=(*(TopoDS_Vertex *)(&myDS->Shape(nVx)));
446 iFlag=myContext->ComputeVE (aVx, aE, aT);
449 aPave.SetParameter(aT);
450 aPB->AppendExtPave(aPave);
454 // 6 Split PaveBlocksa
456 for(i=1; i<=aNb; ++i) {
457 Handle(BOPDS_PaveBlock) aPB=aMPBLI.FindKey(i);
458 nE=aPB->OriginalEdge();
460 if (!myDS->IsCommonBlock(aPB)) {
461 myDS->UpdatePaveBlock(aPB);
464 const Handle(BOPDS_CommonBlock)& aCB=myDS->CommonBlock(aPB);
465 myDS->UpdateCommonBlock(aCB);
467 }//for (; aItMPBLI.More(); aItMPBLI.Next()) {
472 //=======================================================================
473 //function : TreatNewVertices
475 //=======================================================================
476 void BOPAlgo_PaveFiller::TreatNewVertices(
477 const BOPCol_IndexedDataMapOfShapeInteger& aMapVI,
478 BOPCol_IndexedDataMapOfShapeListOfShape& myImages)
480 Standard_Integer j, i, aNbV, aNbVSD;
484 BOPCol_IndexedMapOfShape aMVProcessed;
486 BOPCol_ListIteratorOfListOfInteger aIt;
487 BOPCol_IndexedDataMapOfShapeListOfShape aMVLV;
488 BOPCol_DataMapOfIntegerShape aMIS;
489 BOPCol_IndexedDataMapOfShapeBox aMSB;
491 BOPDS_BoxBndTreeSelector aSelector;
492 BOPDS_BoxBndTree aBBTree;
493 NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller(aBBTree);
495 aNbV = aMapVI.Extent();
496 for (i=1; i<=aNbV; ++i) {
497 const TopoDS_Shape& aV=aMapVI.FindKey(i);
500 aTol=BRep_Tool::Tolerance(*(TopoDS_Vertex*)(&aV));
502 BRepBndLib::Add(aV, aBox);
504 aTreeFiller.Add(i, aBox);
513 for (i=1; i<=aNbV; ++i) {
514 const TopoDS_Shape& aV=aMapVI.FindKey(i);
516 if (aMVProcessed.Contains(aV)) {
520 Standard_Integer aNbIP, aIP, aNbIP1, aIP1;
521 BOPCol_ListOfShape aLVSD;
522 BOPCol_MapOfInteger aMIP, aMIP1, aMIPC;
523 BOPCol_MapIteratorOfMapOfInteger aIt1;
528 aIt1.Initialize(aMIP);
529 for(; aIt1.More(); aIt1.Next()) {
531 if (aMIPC.Contains(aIP)) {
535 const TopoDS_Shape& aVP=aMIS.Find(aIP);
536 const Bnd_Box& aBoxVP=aMSB.FindFromKey(aVP);
539 aSelector.SetBox(aBoxVP);
541 aNbVSD=aBBTree.Select(aSelector);
543 continue; // it must not be
546 const BOPCol_ListOfInteger& aLI=aSelector.Indices();
548 for (; aIt.More(); aIt.Next()) {
550 if (aMIP.Contains(aIP1)) {
554 } //for (; aIt.More(); aIt.Next()) {
555 }//for(; aIt1.More(); aIt1.Next()) {
557 aNbIP1=aMIP1.Extent();
559 break; // from while(1)
562 aIt1.Initialize(aMIP);
563 for(; aIt1.More(); aIt1.Next()) {
569 aIt1.Initialize(aMIP1);
570 for(; aIt1.More(); aIt1.Next()) {
577 aNbIP=aMIPC.Extent();
582 aIt1.Initialize(aMIPC);
583 for(j=0; aIt1.More(); aIt1.Next(), ++j) {
585 const TopoDS_Shape& aVP=aMIS.Find(aIP);
590 aMVProcessed.Add(aVP);
592 aMVLV.Add(aVF, aLVSD);
593 }// for (i=1; i<=aNbV; ++i) {
597 for (i=1; i<=aNbV; ++i) {
598 const TopoDS_Shape& aV=aMVLV.FindKey(i);
599 BOPCol_ListOfShape& aLVSD=aMVLV.ChangeFromIndex(i);
600 aNbVSD=aLVSD.Extent();
602 BOPTools_AlgoTools::MakeVertex(aLVSD, aVnew);
603 myImages.Add(aVnew, aLVSD);
605 myImages.Add(aV, aLVSD);
610 //=======================================================================
611 //function : FillShrunkData
613 //=======================================================================
614 void BOPAlgo_PaveFiller::FillShrunkData(Handle(BOPDS_PaveBlock)& thePB)
616 Standard_Integer nE, nV1, nV2, iErr;
617 Standard_Real aT1, aT2, aTS1, aTS2;
618 BOPInt_ShrunkRange aSR;
623 const BOPDS_Pave& aPave1=thePB->Pave1();
625 aT1=aPave1.Parameter();
626 const TopoDS_Vertex& aV1=(*(TopoDS_Vertex *)(&myDS->Shape(nV1)));
628 const BOPDS_Pave& aPave2=thePB->Pave2();
630 aT2=aPave2.Parameter();
631 const TopoDS_Vertex& aV2=(*(TopoDS_Vertex *)(&myDS->Shape(nV2)));
633 nE=thePB->OriginalEdge();
634 const TopoDS_Edge& aE=(*(TopoDS_Edge *)(&myDS->Shape(nE)));
636 aSR.SetData(aE, aT1, aT2, aV1, aV2, myContext);
639 iErr=aSR.ErrorStatus();
646 aSR.ShrunkRange(aTS1, aTS2);
647 const Bnd_Box& aBox=aSR.BndBox();
649 thePB->SetShrunkData(aTS1, aTS2, aBox);
656 aBBx.MakeCompound(aCx);
657 aItMVCPB.Initialize(theMVCPB);
658 for (; aItMVCPB.More(); aItMVCPB.Next()) {
659 const TopoDS_Shape& aS=aItMVCPB.Key();
662 BRepTools::Write(aCx, "cx");