1 // Created by: Peter KURNEV
2 // Copyright (c) 2010-2014 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 // This file is part of Open CASCADE Technology software library.
9 // This library is free software; you can redistribute it and/or modify it under
10 // the terms of the GNU Lesser General Public License version 2.1 as published
11 // by the Free Software Foundation, with special exception defined in the file
12 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
13 // distribution for complete text of the license and disclaimer of any warranty.
15 // Alternatively, this file may be used under the terms of Open CASCADE
16 // commercial license or contractual agreement.
19 #include <Bnd_Box.hxx>
20 #include <BOPAlgo_PaveFiller.hxx>
21 #include <BOPAlgo_Tools.hxx>
22 #include <BOPAlgo_Alerts.hxx>
23 #include <BOPDS_CommonBlock.hxx>
24 #include <BOPDS_CoupleOfPaveBlocks.hxx>
25 #include <BOPDS_DS.hxx>
26 #include <BOPDS_Interf.hxx>
27 #include <BOPDS_Iterator.hxx>
28 #include <BOPDS_MapOfPaveBlock.hxx>
29 #include <BOPDS_Pave.hxx>
30 #include <BOPDS_PaveBlock.hxx>
31 #include <BOPDS_VectorOfInterfEE.hxx>
32 #include <BOPTools_AlgoTools.hxx>
33 #include <BOPTools_AlgoTools2D.hxx>
34 #include <BOPTools_Parallel.hxx>
35 #include <BndLib_Add3dCurve.hxx>
36 #include <BRep_Tool.hxx>
37 #include <BRep_Builder.hxx>
38 #include <BRepAdaptor_Curve.hxx>
39 #include <GeomAPI_ProjectPointOnCurve.hxx>
41 #include <IntTools_CommonPrt.hxx>
42 #include <IntTools_Context.hxx>
43 #include <IntTools_EdgeEdge.hxx>
44 #include <IntTools_Range.hxx>
45 #include <IntTools_SequenceOfCommonPrts.hxx>
46 #include <IntTools_SequenceOfRanges.hxx>
47 #include <IntTools_ShrunkRange.hxx>
48 #include <IntTools_Tools.hxx>
49 #include <NCollection_IncAllocator.hxx>
50 #include <NCollection_Vector.hxx>
51 #include <Precision.hxx>
53 #include <TopoDS_Edge.hxx>
54 #include <TopoDS_Vertex.hxx>
56 /////////////////////////////////////////////////////////////////////////
57 //=======================================================================
58 //class : BOPAlgo_EdgeEdge
60 //=======================================================================
61 class BOPAlgo_EdgeEdge :
62 public IntTools_EdgeEdge,
74 virtual ~BOPAlgo_EdgeEdge(){
77 void SetPaveBlock1(const Handle(BOPDS_PaveBlock)& aPB) {
81 Handle(BOPDS_PaveBlock)& PaveBlock1() {
85 void SetPaveBlock2(const Handle(BOPDS_PaveBlock)& aPB) {
89 Handle(BOPDS_PaveBlock)& PaveBlock2() {
93 void SetFuzzyValue(const Standard_Real theFuzz) {
94 IntTools_EdgeEdge::SetFuzzyValue(theFuzz);
97 virtual void Perform() {
98 BOPAlgo_Algo::UserBreak();
103 IntTools_EdgeEdge::Perform();
105 catch (Standard_Failure)
107 AddError(new BOPAlgo_AlertIntersectionFailed);
112 Handle(BOPDS_PaveBlock) myPB1;
113 Handle(BOPDS_PaveBlock) myPB2;
116 //=======================================================================
117 typedef NCollection_Vector
118 <BOPAlgo_EdgeEdge> BOPAlgo_VectorOfEdgeEdge;
120 typedef BOPTools_Functor
122 BOPAlgo_VectorOfEdgeEdge> BOPAlgo_EdgeEdgeFunctor;
125 <BOPAlgo_EdgeEdgeFunctor,
126 BOPAlgo_VectorOfEdgeEdge> BOPAlgo_EdgeEdgeCnt;
128 /////////////////////////////////////////////////////////////////////////
129 //=======================================================================
130 // function: PerformEE
132 //=======================================================================
133 void BOPAlgo_PaveFiller::PerformEE()
135 FillShrunkData(TopAbs_EDGE, TopAbs_EDGE);
137 myIterator->Initialize(TopAbs_EDGE, TopAbs_EDGE);
138 Standard_Integer iSize = myIterator->ExpectedLength();
143 Standard_Boolean bExpressCompute, bIsPBSplittable1, bIsPBSplittable2;
144 Standard_Integer i, iX, nE1, nE2, aNbCPrts, k, aNbEdgeEdge;
145 Standard_Integer nV11, nV12, nV21, nV22;
146 Standard_Real aTS11, aTS12, aTS21, aTS22, aT11, aT12, aT21, aT22;
147 TopAbs_ShapeEnum aType;
148 BOPDS_ListIteratorOfListOfPaveBlock aIt1, aIt2;
149 Handle(NCollection_BaseAllocator) aAllocator;
150 BOPAlgo_VectorOfEdgeEdge aVEdgeEdge;
151 BOPDS_MapIteratorOfMapOfPaveBlock aItPB;
152 // keep modified edges for further update
153 TColStd_MapOfInteger aMEdges;
155 aAllocator=NCollection_BaseAllocator::CommonBaseAllocator();
156 //-----------------------------------------------------scope f
157 BOPDS_IndexedDataMapOfPaveBlockListOfPaveBlock aMPBLPB(100, aAllocator);
158 BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks aMVCPB(100, aAllocator);
159 BOPAlgo_DataMapOfPaveBlockBndBox aDMPBBox(100, aAllocator);
161 BOPDS_VectorOfInterfEE& aEEs=myDS->InterfEE();
162 aEEs.SetIncrement(iSize);
164 for (; myIterator->More(); myIterator->Next()) {
165 myIterator->Value(nE1, nE2);
167 const BOPDS_ShapeInfo& aSIE1=myDS->ShapeInfo(nE1);
168 if (aSIE1.HasFlag()){
171 const BOPDS_ShapeInfo& aSIE2=myDS->ShapeInfo(nE2);
172 if (aSIE2.HasFlag()){
176 BOPDS_ListOfPaveBlock& aLPB1 = myDS->ChangePaveBlocks(nE1);
177 if (aLPB1.IsEmpty()) {
181 BOPDS_ListOfPaveBlock& aLPB2 = myDS->ChangePaveBlocks(nE2);
182 if (aLPB2.IsEmpty()) {
186 const TopoDS_Edge& aE1=(*(TopoDS_Edge *)(&aSIE1.Shape()));
187 const TopoDS_Edge& aE2=(*(TopoDS_Edge *)(&aSIE2.Shape()));
189 aIt1.Initialize(aLPB1);
190 for (; aIt1.More(); aIt1.Next()) {
193 Handle(BOPDS_PaveBlock)& aPB1=aIt1.ChangeValue();
195 if (!GetPBBox(aE1, aPB1, aDMPBBox, aT11, aT12, aTS11, aTS12, aBB1)) {
199 aPB1->Indices(nV11, nV12);
201 aIt2.Initialize(aLPB2);
202 for (; aIt2.More(); aIt2.Next()) {
205 Handle(BOPDS_PaveBlock)& aPB2=aIt2.ChangeValue();
207 if (!GetPBBox(aE2, aPB2, aDMPBBox, aT21, aT22, aTS21, aTS22, aBB2)) {
211 if (aBB1.IsOut(aBB2)) {
215 aPB2->Indices(nV21, nV22);
217 bExpressCompute=((nV11==nV21 && nV12==nV22) ||
218 (nV12==nV21 && nV11==nV22));
220 BOPAlgo_EdgeEdge& anEdgeEdge=aVEdgeEdge.Appended();
222 anEdgeEdge.UseQuickCoincidenceCheck(bExpressCompute);
224 anEdgeEdge.SetPaveBlock1(aPB1);
225 anEdgeEdge.SetPaveBlock2(aPB2);
227 anEdgeEdge.SetEdge1(aE1, aT11, aT12);
228 anEdgeEdge.SetEdge2(aE2, aT21, aT22);
229 anEdgeEdge.SetFuzzyValue(myFuzzyValue);
230 anEdgeEdge.SetProgressIndicator(myProgressIndicator);
231 }//for (; aIt2.More(); aIt2.Next()) {
232 }//for (; aIt1.More(); aIt1.Next()) {
233 }//for (; myIterator->More(); myIterator->Next()) {
235 aNbEdgeEdge=aVEdgeEdge.Length();
236 //======================================================
237 BOPAlgo_EdgeEdgeCnt::Perform(myRunParallel, aVEdgeEdge);
238 //======================================================
240 for (k = 0; k < aNbEdgeEdge; ++k) {
243 BOPAlgo_EdgeEdge& anEdgeEdge=aVEdgeEdge(k);
244 if (!anEdgeEdge.IsDone() || anEdgeEdge.HasErrors()) {
245 // Warn about failed intersection of sub-shapes
246 const TopoDS_Shape& aE1 = myDS->Shape(anEdgeEdge.PaveBlock1()->OriginalEdge());
247 const TopoDS_Shape& aE2 = myDS->Shape(anEdgeEdge.PaveBlock2()->OriginalEdge());
248 AddIntersectionFailedWarning(aE1, aE2);
252 const IntTools_SequenceOfCommonPrts& aCPrts = anEdgeEdge.CommonParts();
253 aNbCPrts = aCPrts.Length();
257 //--------------------------------------------
258 Handle(BOPDS_PaveBlock)& aPB1=anEdgeEdge.PaveBlock1();
259 nE1=aPB1->OriginalEdge();
260 aPB1->Range(aT11, aT12);
261 if (!aPB1->HasShrunkData()) {
264 bIsPBSplittable1 = Standard_False;
267 aPB1->ShrunkData(aTS11, aTS12, aBB1, bIsPBSplittable1);
270 Handle(BOPDS_PaveBlock)& aPB2=anEdgeEdge.PaveBlock2();
271 nE2=aPB2->OriginalEdge();
272 aPB2->Range(aT21, aT22);
273 if (!aPB2->HasShrunkData()) {
276 bIsPBSplittable2 = Standard_False;
279 aPB2->ShrunkData(aTS21, aTS22, aBB2, bIsPBSplittable2);
282 //--------------------------------------------
283 IntTools_Range aR11(aT11, aTS11), aR12(aTS12, aT12),
284 aR21(aT21, aTS21), aR22(aTS22, aT22);
286 Standard_Boolean bAnalytical = Standard_False;
288 const TopoDS_Edge& aOE1 = *(TopoDS_Edge*)&myDS->Shape(nE1);
289 const TopoDS_Edge& aOE2 = *(TopoDS_Edge*)&myDS->Shape(nE2);
291 BRepAdaptor_Curve aBAC1(aOE1), aBAC2(aOE2);
293 GeomAbs_CurveType aType1 = aBAC1.GetType();
294 GeomAbs_CurveType aType2 = aBAC2.GetType();
296 bAnalytical = (((aType1 == GeomAbs_Line) &&
297 (aType2 == GeomAbs_Line ||
298 aType2 == GeomAbs_Circle)) ||
299 ((aType2 == GeomAbs_Line) &&
300 (aType1 == GeomAbs_Line ||
301 aType1 == GeomAbs_Circle)));
304 for (i=1; i<=aNbCPrts; ++i) {
305 const IntTools_CommonPrt& aCPart=aCPrts(i);
307 const TopoDS_Edge& aE1=aCPart.Edge1();
308 const TopoDS_Edge& aE2=aCPart.Edge2();
312 case TopAbs_VERTEX: {
313 if (!bIsPBSplittable1 || !bIsPBSplittable2) {
317 Standard_Boolean bIsOnPave[4];
318 Standard_Integer nV[4], j;
319 Standard_Real aT1, aT2, aTol;
321 IntTools_Range aCR1, aCR2;
323 IntTools_Tools::VertexParameters(aCPart, aT1, aT2);
324 aTol = Precision::Confusion();
325 aCR1 = aCPart.Range1();
326 aCR2 = aCPart.Ranges2()(1);
328 //decide to keep the pave or not
329 bIsOnPave[0] = IntTools_Tools::IsOnPave1(aT1, aR11, aTol) ||
330 IntTools_Tools::IsOnPave1(aR11.First(), aCR1, aTol);
331 bIsOnPave[1] = IntTools_Tools::IsOnPave1(aT1, aR12, aTol) ||
332 IntTools_Tools::IsOnPave1(aR12.Last(), aCR1, aTol);
333 bIsOnPave[2] = IntTools_Tools::IsOnPave1(aT2, aR21, aTol) ||
334 IntTools_Tools::IsOnPave1(aR21.First(), aCR2, aTol);
335 bIsOnPave[3] = IntTools_Tools::IsOnPave1(aT2, aR22, aTol) ||
336 IntTools_Tools::IsOnPave1(aR22.Last(), aCR2, aTol);
338 aPB1->Indices(nV[0], nV[1]);
339 aPB2->Indices(nV[2], nV[3]);
341 if((bIsOnPave[0] && bIsOnPave[2]) ||
342 (bIsOnPave[0] && bIsOnPave[3]) ||
343 (bIsOnPave[1] && bIsOnPave[2]) ||
344 (bIsOnPave[1] && bIsOnPave[3])) {
348 Standard_Boolean isVExists = Standard_False;
349 for (j = 0; j < 4; ++j)
353 Handle(BOPDS_PaveBlock)& aPB = (j < 2) ? aPB2 : aPB1;
354 bIsOnPave[j] = ForceInterfVE(nV[j], aPB, aMEdges);
355 if (bIsOnPave[j]) isVExists = Standard_True;
359 BOPTools_AlgoTools::MakeNewVertex(aE1, aT1, aE2, aT2, aVnew);
360 const gp_Pnt aPnew = BRep_Tool::Pnt(aVnew);
364 // The found intersection point is located closely to one of the
365 // pave blocks bounds. So, do not create the new vertex in this point.
366 // Check if this point is a real intersection point or just a touching point.
367 // If it is a touching point, do nothing.
368 // If it is an intersection point, update the existing vertex to cover the
369 // intersection point.
370 const gp_Pnt aPOnE1 = BRepAdaptor_Curve(aE1).Value(aT1);
371 const gp_Pnt aPOnE2 = BRepAdaptor_Curve(aE2).Value(aT2);
372 if (aPOnE1.Distance(aPOnE2) > Precision::Intersection())
373 // No intersection point
376 // Real intersection is present.
377 // Update the existing vertex to cover the intersection point.
378 for (j = 0; j < 4; ++j)
382 const TopoDS_Vertex& aV = TopoDS::Vertex(myDS->Shape(nV[j]));
383 const gp_Pnt aP = BRep_Tool::Pnt(aV);
384 Standard_Real aDistPP = aP.Distance(aPnew);
385 // Just update the vertex
386 UpdateVertex(nV[j], aDistPP);
387 myVertsToAvoidExtension.Add(nV[j]);
392 Standard_Real aTolVnew = BRep_Tool::Tolerance(aVnew);
394 // increase tolerance for Line/Line intersection, but do not update
395 // the vertex till its intersection with some other shape
396 Standard_Real aTolMin = (BRepAdaptor_Curve(aE1).GetType() == GeomAbs_Line) ?
397 (aCR1.Last() - aCR1.First()) / 2. : (aCR2.Last() - aCR2.First()) / 2.;
398 if (aTolMin > aTolVnew) {
404 Standard_Integer nVS[2], iFound;
405 Standard_Real aTolVx, aD2, aDT2;
406 TColStd_MapOfInteger aMV;
414 if (aMV.Contains(nV[2])) {
418 if (aMV.Contains(nV[3])) {
423 for (Standard_Integer k1=0; k1<=j; ++k1) {
424 const TopoDS_Vertex& aVx= *(TopoDS_Vertex*)&(myDS->Shape(nVS[k1]));
425 aTolVx=BRep_Tool::Tolerance(aVx);
426 aPx=BRep_Tool::Pnt(aVx);
427 aD2=aPnew.SquareDistance(aPx);
429 aDT2=100.*(aTolVnew+aTolVx)*(aTolVnew+aTolVx);
443 BOPDS_InterfEE& aEE=aEEs.Appended();
445 aEE.SetIndices(nE1, nE2);
446 aEE.SetCommonPart(aCPart);
448 myDS->AddInterf(nE1, nE2);
450 BOPDS_CoupleOfPaveBlocks aCPB;
452 aCPB.SetPaveBlocks(aPB1, aPB2);
453 aCPB.SetIndexInterf(iX);
454 aCPB.SetTolerance(aTolVnew);
455 aMVCPB.Add(aVnew, aCPB);
456 }//case TopAbs_VERTEX:
464 Standard_Boolean bHasSameBounds;
465 bHasSameBounds=aPB1->HasSameBounds(aPB2);
466 if (!bHasSameBounds) {
470 BOPDS_InterfEE& aEE=aEEs.Appended();
472 aEE.SetIndices(nE1, nE2);
473 aEE.SetCommonPart(aCPart);
475 myDS->AddInterf(nE1, nE2);
477 BOPAlgo_Tools::FillMap<Handle(BOPDS_PaveBlock), TColStd_MapTransientHasher>(aPB1, aPB2, aMPBLPB, aAllocator);
483 }//for (i=1; i<=aNbCPrts; i++) {
484 }//for (k=0; k < aNbFdgeEdge; ++k) {
486 //=========================================
488 //=========================================
489 BOPAlgo_Tools::PerformCommonBlocks(aMPBLPB, aAllocator, myDS, myContext);
490 // Update vertices of common blocks with real CB tolerances
491 UpdateVerticesOfCB();
493 PerformNewVertices(aMVCPB, aAllocator);
495 if (aMEdges.Extent()) {
496 Standard_Integer aNbV = aMVCPB.Extent();
497 for (i = 1; i <= aNbV; ++i) {
498 Handle(BOPDS_PaveBlock) aPB1, aPB2;
499 const BOPDS_CoupleOfPaveBlocks& aCPB = aMVCPB.FindFromIndex(i);
500 aCPB.PaveBlocks(aPB1, aPB2);
502 aMEdges.Remove(aPB1->OriginalEdge());
503 aMEdges.Remove(aPB2->OriginalEdge());
506 SplitPaveBlocks(aMEdges, Standard_False);
509 //-----------------------------------------------------scope t
513 //=======================================================================
514 //function : PerformVerticesEE
516 //=======================================================================
517 void BOPAlgo_PaveFiller::PerformNewVertices
518 (BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks& theMVCPB,
519 const Handle(NCollection_BaseAllocator)& theAllocator,
520 const Standard_Boolean bIsEEIntersection)
522 Standard_Integer aNbV = theMVCPB.Extent();
527 Standard_Real aTolAdd = myFuzzyValue / 2.;
529 // 1. Fuse the new vertices
530 TopTools_IndexedDataMapOfShapeListOfShape aImages;
531 TreatNewVertices(theMVCPB, aImages);
533 // 2. Add new vertices to myDS and connect indices to CPB structure
534 BOPDS_VectorOfInterfEE& aEEs = myDS->InterfEE();
535 BOPDS_VectorOfInterfEF& aEFs = myDS->InterfEF();
537 Standard_Integer i, aNb = aImages.Extent();
538 for (i = 1; i <= aNb; ++i) {
539 const TopoDS_Vertex& aV = TopoDS::Vertex(aImages.FindKey(i));
540 const TopTools_ListOfShape& aLVSD = aImages.FindFromIndex(i);
543 aSI.SetShapeType(TopAbs_VERTEX);
545 Standard_Integer iV = myDS->Append(aSI);
547 BOPDS_ShapeInfo& aSIDS = myDS->ChangeShapeInfo(iV);
548 Bnd_Box& aBox = aSIDS.ChangeBox();
549 aBox.Add(BRep_Tool::Pnt(aV));
550 aBox.SetGap(BRep_Tool::Tolerance(aV) + aTolAdd);
552 TopTools_ListIteratorOfListOfShape aItLS(aLVSD);
553 for (; aItLS.More(); aItLS.Next()) {
554 const TopoDS_Shape& aVx = aItLS.Value();
555 BOPDS_CoupleOfPaveBlocks &aCPB = theMVCPB.ChangeFromKey(aVx);
557 // update interference
558 Standard_Integer iX = aCPB.IndexInterf();
559 BOPDS_Interf *aInt = bIsEEIntersection ? (BOPDS_Interf*)(&aEEs(iX)) : (BOPDS_Interf*) (&aEFs(iX));
560 aInt->SetIndexNew(iV);
564 // 3. Map PaveBlock/ListOfVertices to add to this PaveBlock ->aMPBLI
565 BOPDS_IndexedDataMapOfPaveBlockListOfInteger aMPBLI(100, theAllocator);
566 for (i = 1; i <= aNbV; ++i) {
567 const BOPDS_CoupleOfPaveBlocks& aCPB = theMVCPB.FindFromIndex(i);
568 Standard_Integer iV = aCPB.Index();
570 Handle(BOPDS_PaveBlock) aPB[2];
571 aCPB.PaveBlocks(aPB[0], aPB[1]);
572 for (Standard_Integer j = 0; j < 2; ++j) {
573 TColStd_ListOfInteger *pLI = aMPBLI.ChangeSeek(aPB[j]);
575 pLI = &aMPBLI(aMPBLI.Add(aPB[j], TColStd_ListOfInteger(theAllocator)));
579 if (aPB[0] == aPB[1]) {
585 // 4. Compute Extra Paves and split Pave blocks by the Extra paves
586 IntersectVE(aMPBLI, Standard_False);
588 //=======================================================================
589 //function : TreatNewVertices
591 //=======================================================================
592 void BOPAlgo_PaveFiller::TreatNewVertices
593 (const BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks& theMVCPB,
594 TopTools_IndexedDataMapOfShapeListOfShape& myImages)
597 // Prepare for intersection
598 TopTools_IndexedDataMapOfShapeReal aVerts;
599 Standard_Integer i, aNbV = theMVCPB.Extent();
600 for (i = 1; i <= aNbV; ++i) {
601 const TopoDS_Shape& aV = theMVCPB.FindKey(i);
602 Standard_Real aTol = theMVCPB.FindFromIndex(i).Tolerance();
603 aVerts.Add(aV, aTol);
606 // Perform intersection
607 TopTools_ListOfListOfShape aChains;
608 BOPAlgo_Tools::IntersectVertices(aVerts, myRunParallel, myFuzzyValue, aChains);
610 // Treat the results - make new vertices for each chain
611 TopTools_ListOfListOfShape::Iterator aItC(aChains);
612 for (; aItC.More(); aItC.Next()) {
613 const TopTools_ListOfShape& aLVSD = aItC.Value();
616 BOPTools_AlgoTools::MakeVertex(aLVSD, aVNew);
617 myImages.Add(aVNew, aLVSD);
620 //=======================================================================
621 //function : FillShrunkData
623 //=======================================================================
624 void BOPAlgo_PaveFiller::FillShrunkData(Handle(BOPDS_PaveBlock)& thePB)
627 Standard_Integer nV1, nV2;
628 thePB->Indices(nV1, nV2);
629 const TopoDS_Vertex& aV1=(*(TopoDS_Vertex *)(&myDS->Shape(nV1)));
630 const TopoDS_Vertex& aV2=(*(TopoDS_Vertex *)(&myDS->Shape(nV2)));
632 Standard_Integer nE = -1;
633 if (!thePB->HasEdge(nE))
635 nE = thePB->OriginalEdge();
640 const TopoDS_Edge& aE=(*(TopoDS_Edge *)(&myDS->Shape(nE)));
642 Standard_Real aT1, aT2;
643 thePB->Range(aT1, aT2);
645 IntTools_ShrunkRange aSR;
646 aSR.SetContext(myContext);
647 aSR.SetData(aE, aT1, aT2, aV1, aV2);
649 // Analyze the results of computations
650 AnalyzeShrunkData(thePB, aSR);
652 //=======================================================================
653 // function: AnalyzeShrunkData
655 //=======================================================================
656 void BOPAlgo_PaveFiller::AnalyzeShrunkData(const Handle(BOPDS_PaveBlock)& thePB,
657 const IntTools_ShrunkRange& theSR)
659 // in case of error treat the warning status
660 Standard_Boolean bWholeEdge = Standard_False;
661 TopoDS_Shape aWarnShape;
663 if (!theSR.IsDone() || !theSR.IsSplittable()) {
664 Standard_Real aEFirst, aELast, aPBFirst, aPBLast;
665 BRep_Tool::Range(theSR.Edge(), aEFirst, aELast);
666 thePB->Range(aPBFirst, aPBLast);
667 bWholeEdge = !(aPBFirst > aEFirst || aPBLast < aELast);
668 if (bWholeEdge && thePB->OriginalEdge() >= 0) {
669 aWarnShape = theSR.Edge();
672 const TopoDS_Shape& aV1 = myDS->Shape(thePB->Pave1().Index());
673 const TopoDS_Shape& aV2 = myDS->Shape(thePB->Pave2().Index());
674 BRep_Builder().MakeCompound(TopoDS::Compound(aWarnShape));
675 BRep_Builder().Add(aWarnShape, theSR.Edge());
676 BRep_Builder().Add(aWarnShape, aV1);
677 BRep_Builder().Add(aWarnShape, aV2);
680 if (!theSR.IsDone()) {
682 AddWarning (new BOPAlgo_AlertTooSmallEdge (aWarnShape));
684 AddWarning (new BOPAlgo_AlertBadPositioning (aWarnShape));
685 Standard_Real aTS1, aTS2;
686 theSR.ShrunkRange(aTS1, aTS2);
687 thePB->SetShrunkData(aTS1, aTS2, Bnd_Box(), Standard_False);
692 AddWarning (new BOPAlgo_AlertNotSplittableEdge (aWarnShape));
694 AddWarning (new BOPAlgo_AlertBadPositioning (aWarnShape));
697 Standard_Real aTS1, aTS2;
698 theSR.ShrunkRange(aTS1, aTS2);
699 Bnd_Box aBox = theSR.BndBox();
700 aBox.SetGap(aBox.GetGap() + myFuzzyValue / 2.);
701 thePB->SetShrunkData(aTS1, aTS2, aBox, theSR.IsSplittable());
703 //=======================================================================
704 //function : ForceInterfVE
706 //=======================================================================
707 Standard_Boolean BOPAlgo_PaveFiller::ForceInterfVE(const Standard_Integer nV,
708 Handle(BOPDS_PaveBlock)& aPB,
709 TColStd_MapOfInteger& theMEdges)
711 Standard_Integer nE, nVx, nVSD, iFlag;
712 Standard_Real aT, aTolVNew;
714 nE = aPB->OriginalEdge();
716 const BOPDS_ShapeInfo& aSIE=myDS->ShapeInfo(nE);
717 if (aSIE.HasSubShape(nV)) {
718 return Standard_True;
721 if (myDS->HasInterf(nV, nE)) {
722 return Standard_True;
725 if (myDS->HasInterfShapeSubShapes(nV, nE)) {
726 return Standard_True;
729 if (aPB->Pave1().Index() == nV ||
730 aPB->Pave2().Index() == nV) {
731 return Standard_True;
735 if (myDS->HasShapeSD(nV, nVSD)) {
739 const TopoDS_Vertex& aV = *(TopoDS_Vertex*)&myDS->Shape(nVx);
740 const TopoDS_Edge& aE = *(TopoDS_Edge*) &myDS->Shape(nE);
742 iFlag = myContext->ComputeVE(aV, aE, aT, aTolVNew, myFuzzyValue);
743 if (iFlag == 0 || iFlag == -4) {
747 BOPDS_VectorOfInterfVE& aVEs=myDS->InterfVE();
748 aVEs.SetIncrement(10);
750 BOPDS_InterfVE& aVE=aVEs.Appended();
751 aVE.SetIndices(nV, nE);
752 aVE.SetParameter(aT);
754 myDS->AddInterf(nV, nE);
756 // 3 update vertex V/E if necessary
757 nVx=UpdateVertex(nV, aTolVNew);
759 if (myDS->IsNewShape(nVx)) {
760 aVE.SetIndexNew(nVx);
762 // 5 append ext pave to pave block
764 aPave.SetParameter(aT);
765 aPB->AppendExtPave(aPave);
769 // check for self-interference
770 Standard_Integer iRV = myDS->Rank(nV);
771 if (iRV >= 0 && iRV == myDS->Rank(nE)) {
772 // add warning status
774 BRep_Builder().MakeCompound(aWC);
775 BRep_Builder().Add(aWC, aV);
776 BRep_Builder().Add(aWC, aE);
777 AddWarning (new BOPAlgo_AlertSelfInterferingShape (aWC));
779 return Standard_True;
781 return Standard_False;
784 //=======================================================================
785 //function : GetPBBox
787 //=======================================================================
788 Standard_Boolean BOPAlgo_PaveFiller::GetPBBox(const TopoDS_Edge& theE,
789 const Handle(BOPDS_PaveBlock)& thePB,
790 BOPAlgo_DataMapOfPaveBlockBndBox& thePBBox,
791 Standard_Real& theFirst,
792 Standard_Real& theLast,
793 Standard_Real& theSFirst,
794 Standard_Real& theSLast,
797 thePB->Range(theFirst, theLast);
798 // check the validity of PB's range
799 Standard_Boolean bValid = theLast - theFirst > Precision::PConfusion();
805 if (thePB->HasShrunkData()) {
806 Standard_Boolean bIsSplittable;
807 thePB->ShrunkData(theSFirst, theSLast, theBox, bIsSplittable);
811 theSFirst = theFirst;
814 if (thePBBox.IsBound(thePB)) {
815 theBox = thePBBox.Find(thePB);
818 // build bounding box
819 BRepAdaptor_Curve aBAC(theE);
820 Standard_Real aTol = BRep_Tool::Tolerance(theE) + Precision::Confusion();
821 BndLib_Add3dCurve::Add(aBAC, theSFirst, theSLast, aTol, theBox);
822 thePBBox.Bind(thePB, theBox);
827 //=======================================================================
828 //function : UpdateVerticesOfCB
830 //=======================================================================
831 void BOPAlgo_PaveFiller::UpdateVerticesOfCB()
833 // Fence map to avoid checking same Common block twice
834 BOPDS_MapOfPaveBlock aMPBFence;
836 BOPDS_VectorOfListOfPaveBlock& aPBP = myDS->ChangePaveBlocksPool();
837 const Standard_Integer aNbPBP = aPBP.Length();
838 for (Standard_Integer i = 0; i < aNbPBP; ++i)
840 const BOPDS_ListOfPaveBlock& aLPB = aPBP(i);
841 BOPDS_ListIteratorOfListOfPaveBlock itPB(aLPB);
842 for (; itPB.More(); itPB.Next())
844 const Handle(BOPDS_CommonBlock)& aCB = myDS->CommonBlock(itPB.Value());
848 const Handle(BOPDS_PaveBlock)& aPBR = aCB->PaveBlock1();
849 if (!aMPBFence.Add(aPBR))
852 Standard_Real aTolCB = aCB->Tolerance();
855 UpdateVertex(aPBR->Pave1().Index(), aTolCB);
856 UpdateVertex(aPBR->Pave2().Index(), aTolCB);
862 //=======================================================================
863 //function : ForceInterfEE
865 //=======================================================================
866 void BOPAlgo_PaveFiller::ForceInterfEE()
868 // Now that we have vertices increased and unified, try to find additional
869 // common blocks among the pairs of edges.
870 // Since all real intersections should have already happened, here we
871 // are interested in common blocks only, thus we need to check only
872 // those pairs of pave blocks with the same bounding vertices.
874 Handle(NCollection_IncAllocator) anAlloc = new NCollection_IncAllocator;
876 // Initialize pave blocks for all vertices which participated in intersections
877 const Standard_Integer aNbS = myDS->NbSourceShapes();
878 for (Standard_Integer i = 0; i < aNbS; ++i)
880 const BOPDS_ShapeInfo& aSI = myDS->ShapeInfo(i);
881 if (aSI.ShapeType() == TopAbs_VERTEX)
883 if (myDS->HasInterf(i))
884 myDS->InitPaveBlocksForVertex(i);
888 // Fill the connection map from bounding vertices to pave blocks
889 // having those bounding vertices
890 NCollection_IndexedDataMap<BOPDS_Pair,
891 BOPDS_ListOfPaveBlock,
892 BOPDS_PairMapHasher> aPBMap(1, anAlloc);
893 // Fence map of pave blocks
894 BOPDS_MapOfPaveBlock aMPBFence(1, anAlloc);
896 for (Standard_Integer i = 0; i < aNbS; ++i)
898 const BOPDS_ShapeInfo& aSI = myDS->ShapeInfo(i);
899 if (aSI.ShapeType() != TopAbs_EDGE)
903 if (!aSI.HasReference())
904 // Edge has no pave blocks
911 const BOPDS_ListOfPaveBlock& aLPB = myDS->PaveBlocks(i);
912 BOPDS_ListIteratorOfListOfPaveBlock aItLPB(aLPB);
913 for (; aItLPB.More(); aItLPB.Next())
915 const Handle(BOPDS_PaveBlock)& aPB = aItLPB.Value();
916 const Handle(BOPDS_PaveBlock)& aPBR = myDS->RealPaveBlock(aPB);
917 if (!aMPBFence.Add(aPBR))
921 Standard_Integer nV1, nV2;
922 aPBR->Indices(nV1, nV2);
924 // Add pave block to a map
925 BOPDS_Pair aPair(nV1, nV2);
926 BOPDS_ListOfPaveBlock *pList = aPBMap.ChangeSeek(aPair);
928 pList = &aPBMap(aPBMap.Add(aPair, BOPDS_ListOfPaveBlock(anAlloc)));
933 Standard_Integer aNbPB = aPBMap.Extent();
937 // Prepare pave blocks with the same vertices for intersection.
938 BOPAlgo_VectorOfEdgeEdge aVEdgeEdge;
940 for (Standard_Integer i = 1; i <= aNbPB; ++i)
942 const BOPDS_ListOfPaveBlock& aLPB = aPBMap(i);
943 if (aLPB.Extent() < 2)
946 const BOPDS_Pair& aPair = aPBMap.FindKey(i);
947 Standard_Integer nV1, nV2;
948 aPair.Indices(nV1, nV2);
950 const TopoDS_Vertex& aV1 = TopoDS::Vertex(myDS->Shape(nV1));
951 const TopoDS_Vertex& aV2 = TopoDS::Vertex(myDS->Shape(nV2));
953 // Use the max tolerance of vertices as Fuzzy value for intersection
955 Standard_Real aTolAdd = 2 * Max(BRep_Tool::Tolerance(aV1),
956 BRep_Tool::Tolerance(aV2));
958 // All possible pairs combined from the list <aLPB> should be checked
959 BOPDS_ListIteratorOfListOfPaveBlock aItLPB1(aLPB);
960 for (; aItLPB1.More(); aItLPB1.Next())
962 const Handle(BOPDS_PaveBlock)& aPB1 = aItLPB1.Value();
963 const Handle(BOPDS_CommonBlock)& aCB1 = myDS->CommonBlock(aPB1);
964 const Standard_Integer nE1 = aPB1->OriginalEdge();
965 const Standard_Integer iR1 = myDS->Rank(nE1);
966 const TopoDS_Edge& aE1 = TopoDS::Edge(myDS->Shape(nE1));
967 Standard_Real aT11, aT12;
968 aPB1->Range(aT11, aT12);
969 BRepAdaptor_Curve aBAC1(aE1);
972 aBAC1.D1((aT11 + aT12) * 0.5, aPm, aVTgt1);
973 if (aVTgt1.SquareMagnitude() < gp::Resolution())
976 BOPDS_ListIteratorOfListOfPaveBlock aItLPB2 = aItLPB1;
977 for (aItLPB2.Next(); aItLPB2.More(); aItLPB2.Next())
979 const Handle(BOPDS_PaveBlock)& aPB2 = aItLPB2.Value();
980 const Handle(BOPDS_CommonBlock)& aCB2 = myDS->CommonBlock(aPB2);
981 const Standard_Integer nE2 = aPB2->OriginalEdge();
982 const Standard_Integer iR2 = myDS->Rank(nE2);
984 // Check that the edges came from different arguments
987 // If the sharing of the vertices is not original, but has been acquired
988 // during the operation, check the coincidence of the edges even if
989 // they came from the same argument
990 if ((!myDS->IsNewShape(nV1) && (myDS->Rank(nV1) == iR1)) ||
991 (!myDS->IsNewShape(nV2) && (myDS->Rank(nV2) == iR2)))
995 // Check that the Pave blocks do not form the Common block already
996 if (!aCB1.IsNull() && !aCB2.IsNull())
1002 const TopoDS_Edge& aE2 = TopoDS::Edge(myDS->Shape(nE2));
1003 Standard_Real aT21, aT22;
1004 aPB2->Range(aT21, aT22);
1006 // Check the angle between edges in the middle point.
1007 // If the angle is more than 10 degrees, do not use the additional
1008 // tolerance, as it may lead to undesired unification of edges
1009 Standard_Boolean bUseAddTol = Standard_True;
1011 GeomAPI_ProjectPointOnCurve& aProjPC = myContext->ProjPC(aE2);
1012 aProjPC.Perform(aPm);
1013 if (!aProjPC.NbPoints())
1016 BRepAdaptor_Curve aBAC2(aE2);
1019 aBAC2.D1(aProjPC.LowerDistanceParameter(), aPm2, aVTgt2);
1020 if (aVTgt2.SquareMagnitude() < gp::Resolution())
1022 // The angle should be close to zero
1023 Standard_Real aCos = aVTgt1.Dot(aVTgt2);
1024 if (Abs(aCos) < 0.984)
1025 bUseAddTol = Standard_False;
1028 // Add pair for intersection
1029 BOPAlgo_EdgeEdge& anEdgeEdge = aVEdgeEdge.Appended();
1030 anEdgeEdge.UseQuickCoincidenceCheck(Standard_True);
1031 anEdgeEdge.SetPaveBlock1(aPB1);
1032 anEdgeEdge.SetPaveBlock2(aPB2);
1033 anEdgeEdge.SetEdge1(aE1, aT11, aT12);
1034 anEdgeEdge.SetEdge2(aE2, aT21, aT22);
1036 anEdgeEdge.SetFuzzyValue(myFuzzyValue + aTolAdd);
1038 anEdgeEdge.SetFuzzyValue(myFuzzyValue);
1039 anEdgeEdge.SetProgressIndicator(myProgressIndicator);
1044 Standard_Integer aNbPairs = aVEdgeEdge.Length();
1052 // Perform intersection of the found pairs
1053 BOPAlgo_EdgeEdgeCnt::Perform(myRunParallel, aVEdgeEdge);
1055 BOPDS_VectorOfInterfEE& aEEs = myDS->InterfEE();
1057 aEEs.SetIncrement(10);
1059 // Analyze the results of intersection looking for TopAbs_EDGE
1060 // intersection type only.
1062 BOPDS_IndexedDataMapOfPaveBlockListOfPaveBlock aMPBLPB(1, anAlloc);
1064 for (Standard_Integer i = 0; i < aNbPairs; ++i)
1066 BOPAlgo_EdgeEdge& anEdgeEdge = aVEdgeEdge(i);
1067 if (!anEdgeEdge.IsDone() || anEdgeEdge.HasErrors())
1069 // Warn about failed intersection of sub-shapes
1070 const TopoDS_Shape& aE1 = myDS->Shape(anEdgeEdge.PaveBlock1()->OriginalEdge());
1071 const TopoDS_Shape& aE2 = myDS->Shape(anEdgeEdge.PaveBlock2()->OriginalEdge());
1072 AddIntersectionFailedWarning(aE1, aE2);
1076 const IntTools_SequenceOfCommonPrts& aCParts = anEdgeEdge.CommonParts();
1077 if (aCParts.Length() != 1)
1080 const IntTools_CommonPrt& aCP = aCParts(1);
1081 if (aCP.Type() != TopAbs_EDGE)
1084 Handle(BOPDS_PaveBlock) aPB[] = {anEdgeEdge.PaveBlock1(), anEdgeEdge.PaveBlock2()};
1085 const Standard_Integer nE1 = aPB[0]->OriginalEdge();
1086 const Standard_Integer nE2 = aPB[1]->OriginalEdge();
1088 if (myDS->Rank(nE1) == myDS->Rank(nE2))
1090 // Add acquired self-interference warning
1091 TopoDS_Compound aWC;
1092 BRep_Builder().MakeCompound(aWC);
1093 BRep_Builder().Add(aWC, myDS->Shape(nE1));
1094 BRep_Builder().Add(aWC, myDS->Shape(nE2));
1095 AddWarning(new BOPAlgo_AlertAcquiredSelfIntersection(aWC));
1098 BOPDS_InterfEE& aEE = aEEs.Appended();
1099 aEE.SetIndices(nE1, nE2);
1100 aEE.SetCommonPart(aCP);
1101 myDS->AddInterf(nE1, nE2);
1103 // Fill map for common blocks creation
1104 for (Standard_Integer j = 0; j < 2; ++j)
1106 if (myDS->IsCommonBlock(aPB[j]))
1108 const BOPDS_ListOfPaveBlock& aLPBCB = myDS->CommonBlock(aPB[j])->PaveBlocks();
1109 BOPDS_ListIteratorOfListOfPaveBlock aItLPB(aLPBCB);
1110 for (; aItLPB.More(); aItLPB.Next())
1111 BOPAlgo_Tools::FillMap<Handle(BOPDS_PaveBlock),
1112 TColStd_MapTransientHasher>(aPB[j], aItLPB.Value(), aMPBLPB, anAlloc);
1115 BOPAlgo_Tools::FillMap<Handle(BOPDS_PaveBlock),
1116 TColStd_MapTransientHasher>(aPB[0], aPB[1], aMPBLPB, anAlloc);
1119 // Create new common blocks of coinciding pairs.
1120 BOPAlgo_Tools::PerformCommonBlocks(aMPBLPB, anAlloc, myDS);