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 <BOPCol_MapOfInteger.hxx>
23 #include <BOPCol_NCVector.hxx>
24 #include <BOPCol_Parallel.hxx>
25 #include <BOPDS_CommonBlock.hxx>
26 #include <BOPDS_CoupleOfPaveBlocks.hxx>
27 #include <BOPDS_Curve.hxx>
28 #include <BOPDS_DS.hxx>
29 #include <BOPDS_Interf.hxx>
30 #include <BOPDS_Iterator.hxx>
31 #include <BOPDS_MapOfPaveBlock.hxx>
32 #include <BOPDS_Pave.hxx>
33 #include <BOPDS_PaveBlock.hxx>
34 #include <BOPTools_AlgoTools.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRep_Tool.hxx>
37 #include <BRepAdaptor_Curve.hxx>
39 #include <IntTools_CommonPrt.hxx>
40 #include <IntTools_Context.hxx>
41 #include <IntTools_EdgeFace.hxx>
42 #include <IntTools_Range.hxx>
43 #include <IntTools_SequenceOfCommonPrts.hxx>
44 #include <IntTools_Tools.hxx>
45 #include <Precision.hxx>
47 #include <TopoDS_Edge.hxx>
48 #include <TopoDS_Face.hxx>
49 #include <TopoDS_Vertex.hxx>
51 //=======================================================================
52 //class : BOPAlgo_EdgeFace
54 //=======================================================================
55 class BOPAlgo_EdgeFace :
56 public IntTools_EdgeFace,
68 virtual ~BOPAlgo_EdgeFace(){
71 void SetIndices(const Standard_Integer nE,
72 const Standard_Integer nF) {
77 void Indices(Standard_Integer& nE,
78 Standard_Integer& nF) {
83 void SetNewSR(const IntTools_Range& aR){
87 IntTools_Range& NewSR(){
91 void SetPaveBlock(const Handle(BOPDS_PaveBlock)& aPB) {
95 Handle(BOPDS_PaveBlock)& PaveBlock() {
99 void SetFuzzyValue(const Standard_Real theFuzz) {
100 IntTools_EdgeFace::SetFuzzyValue(theFuzz);
103 virtual void Perform() {
104 BOPAlgo_Algo::UserBreak();
105 IntTools_EdgeFace::Perform();
109 Standard_Integer myIE;
110 Standard_Integer myIF;
111 IntTools_Range myNewSR;
112 Handle(BOPDS_PaveBlock) myPB;
115 //=======================================================================
116 typedef BOPCol_NCVector<BOPAlgo_EdgeFace> BOPAlgo_VectorOfEdgeFace;
118 typedef BOPCol_ContextFunctor
120 BOPAlgo_VectorOfEdgeFace,
121 Handle(IntTools_Context),
122 IntTools_Context> BOPAlgo_EdgeFaceFunctor;
124 typedef BOPCol_ContextCnt
125 <BOPAlgo_EdgeFaceFunctor,
126 BOPAlgo_VectorOfEdgeFace,
127 Handle(IntTools_Context)> BOPAlgo_EdgeFaceCnt;
129 //=======================================================================
130 //function : PerformEF
132 //=======================================================================
133 void BOPAlgo_PaveFiller::PerformEF()
137 FillShrunkData(TopAbs_EDGE, TopAbs_FACE);
139 myIterator->Initialize(TopAbs_EDGE, TopAbs_FACE);
140 Standard_Integer iSize = myIterator->ExpectedLength();
145 Standard_Integer nE, nF;
147 if (myGlue == BOPAlgo_GlueFull) {
148 // there is no need to intersect edges with faces in this mode
149 // just initialize FaceInfo for faces
150 for (; myIterator->More(); myIterator->Next()) {
151 myIterator->Value(nE, nF);
152 if (!myDS->ShapeInfo(nE).HasFlag()) {
153 myDS->ChangeFaceInfo(nF);
159 Standard_Boolean bV[2], bIsPBSplittable;
160 Standard_Boolean bV1, bV2, bExpressCompute;
161 Standard_Integer nV1, nV2;
162 Standard_Integer aDiscretize, i, aNbCPrts, iX, nV[2];
163 Standard_Integer aNbEdgeFace, k;
164 Standard_Real aTolE, aTolF, aTS1, aTS2, aT1, aT2, aDeflection;
165 Handle(NCollection_BaseAllocator) aAllocator;
166 TopAbs_ShapeEnum aType;
167 BOPDS_ListIteratorOfListOfPaveBlock aIt;
168 BOPAlgo_VectorOfEdgeFace aVEdgeFace;
169 //-----------------------------------------------------scope f
171 aAllocator=NCollection_BaseAllocator::CommonBaseAllocator();
173 BOPCol_MapOfInteger aMIEFC(100, aAllocator);
174 BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks aMVCPB(100, aAllocator);
175 BOPDS_IndexedDataMapOfPaveBlockListOfInteger aMPBLI(100, aAllocator);
176 BOPAlgo_DataMapOfPaveBlockBndBox aDMPBBox(100, aAllocator);
181 BOPDS_VectorOfInterfEF& aEFs=myDS->InterfEF();
182 aEFs.SetIncrement(iSize);
184 for (; myIterator->More(); myIterator->Next()) {
185 myIterator->Value(nE, nF);
187 const BOPDS_ShapeInfo& aSIE=myDS->ShapeInfo(nE);
188 if (aSIE.HasFlag()){//degenerated
192 const TopoDS_Edge& aE=(*(TopoDS_Edge *)(&aSIE.Shape()));
193 const TopoDS_Face& aF=(*(TopoDS_Face *)(&myDS->Shape(nF)));
194 const Bnd_Box& aBBF=myDS->ShapeInfo(nF).Box();
196 BOPDS_FaceInfo& aFI=myDS->ChangeFaceInfo(nF);
197 const BOPDS_IndexedMapOfPaveBlock& aMPBF=aFI.PaveBlocksOn();
199 const BOPCol_MapOfInteger& aMVIn=aFI.VerticesIn();
200 const BOPCol_MapOfInteger& aMVOn=aFI.VerticesOn();
202 aTolE=BRep_Tool::Tolerance(aE);
203 aTolF=BRep_Tool::Tolerance(aF);
205 BOPDS_ListOfPaveBlock& aLPB=myDS->ChangePaveBlocks(nE);
206 aIt.Initialize(aLPB);
207 for (; aIt.More(); aIt.Next()) {
208 Handle(BOPDS_PaveBlock)& aPB=aIt.ChangeValue();
210 const Handle(BOPDS_PaveBlock) aPBR=myDS->RealPaveBlock(aPB);
211 if (aMPBF.Contains(aPBR)) {
216 if (!GetPBBox(aE, aPB, aDMPBBox, aT1, aT2, aTS1, aTS2, aBBE)) {
220 if (aBBF.IsOut (aBBE)) {
224 aPBR->Indices(nV1, nV2);
225 bV1=aMVIn.Contains(nV1) || aMVOn.Contains(nV1);
226 bV2=aMVIn.Contains(nV2) || aMVOn.Contains(nV2);
227 bExpressCompute=bV1 && bV2;
229 BOPAlgo_EdgeFace& aEdgeFace=aVEdgeFace.Append1();
231 aEdgeFace.SetIndices(nE, nF);
232 aEdgeFace.SetPaveBlock(aPB);
234 aEdgeFace.SetEdge (aE);
235 aEdgeFace.SetFace (aF);
236 aEdgeFace.SetFuzzyValue(myFuzzyValue);
237 aEdgeFace.SetDiscretize (aDiscretize);
238 aEdgeFace.SetDeflection (aDeflection);
239 aEdgeFace.UseQuickCoincidenceCheck(bExpressCompute);
241 IntTools_Range aSR(aTS1, aTS2);
242 IntTools_Range anewSR=aSR;
243 BOPTools_AlgoTools::CorrectRange(aE, aF, aSR, anewSR);
244 aEdgeFace.SetNewSR(anewSR);
246 IntTools_Range aPBRange(aT1, aT2);
248 BOPTools_AlgoTools::CorrectRange(aE, aF, aSR, aPBRange);
249 aEdgeFace.SetRange (aPBRange);
250 aEdgeFace.SetProgressIndicator(myProgressIndicator);
252 }//for (; aIt.More(); aIt.Next()) {
253 }//for (; myIterator->More(); myIterator->Next()) {
255 aNbEdgeFace=aVEdgeFace.Extent();
256 //=================================================================
257 BOPAlgo_EdgeFaceCnt::Perform(myRunParallel, aVEdgeFace, myContext);
258 //=================================================================
260 for (k=0; k < aNbEdgeFace; ++k) {
261 BOPAlgo_EdgeFace& aEdgeFace=aVEdgeFace(k);
262 if (!aEdgeFace.IsDone()) {
266 const IntTools_SequenceOfCommonPrts& aCPrts=aEdgeFace.CommonParts();
267 aNbCPrts = aCPrts.Length();
272 aEdgeFace.Indices(nE, nF);
274 const TopoDS_Edge& aE=aEdgeFace.Edge();
275 const TopoDS_Face& aF=aEdgeFace.Face();
277 aTolE=BRep_Tool::Tolerance(aE);
278 aTolF=BRep_Tool::Tolerance(aF);
279 const IntTools_Range& anewSR=aEdgeFace.NewSR();
280 Handle(BOPDS_PaveBlock)& aPB=aEdgeFace.PaveBlock();
282 aPB->Range(aT1, aT2);
283 aPB->Indices(nV[0], nV[1]);
284 bIsPBSplittable = aPB->IsSplittable();
286 anewSR.Range(aTS1, aTS2);
288 if (aCPrts(1).Type() == TopAbs_VERTEX) {
289 // for the intersection type VERTEX
290 // extend vertices ranges using Edge/Edge intersections
291 // between the edge aE and the edges of the face aF.
292 // thereby the edge's intersection range is reduced
293 ReduceIntersectionRange(nV[0], nV[1], nE, nF, aTS1, aTS2);
296 IntTools_Range aR1(aT1, aTS1), aR2(aTS2, aT2);
298 BOPDS_FaceInfo& aFI=myDS->ChangeFaceInfo(nF);
299 const BOPCol_MapOfInteger& aMIFOn=aFI.VerticesOn();
300 const BOPCol_MapOfInteger& aMIFIn=aFI.VerticesIn();
302 Standard_Boolean bLinePlane = Standard_False;
304 BRepAdaptor_Curve aBAC(aE);
305 bLinePlane = (aBAC.GetType() == GeomAbs_Line &&
306 myContext->SurfaceAdaptor(aF).GetType() == GeomAbs_Plane);
309 for (i=1; i<=aNbCPrts; ++i) {
310 const IntTools_CommonPrt& aCPart=aCPrts(i);
313 case TopAbs_VERTEX: {
314 Standard_Boolean bIsOnPave[2];
316 Standard_Real aT, aTolToDecide;
319 IntTools_Tools::VertexParameter(aCPart, aT);
320 BOPTools_AlgoTools::MakeNewVertex(aE, aT, aF, aVnew);
322 const IntTools_Range& aR=aCPart.Range1();
325 bIsOnPave[0]=IntTools_Tools::IsInRange(aR1, aR, aTolToDecide);
326 bIsOnPave[1]=IntTools_Tools::IsInRange(aR2, aR, aTolToDecide);
328 if ((bIsOnPave[0] && bIsOnPave[1]) ||
329 (bLinePlane && (bIsOnPave[0] || bIsOnPave[1]))) {
330 bV[0]=CheckFacePaves(nV[0], aMIFOn, aMIFIn);
331 bV[1]=CheckFacePaves(nV[1], aMIFOn, aMIFIn);
332 if (bV[0] && bV[1]) {
333 IntTools_CommonPrt aCP = aCPart;
334 aCP.SetType(TopAbs_EDGE);
335 BOPDS_InterfEF& aEF=aEFs.Append1();
337 aEF.SetIndices(nE, nF);
338 aEF.SetCommonPart(aCP);
339 myDS->AddInterf(nE, nF);
343 BOPAlgo_Tools::FillMap(aPB, nF, aMPBLI, aAllocator);
348 if (!bIsPBSplittable) {
352 for (j=0; j<2; ++j) {
354 bV[j]=CheckFacePaves(nV[j], aMIFOn, aMIFIn);
356 const TopoDS_Vertex& aV=
357 (*(TopoDS_Vertex *)(&myDS->Shape(nV[j])));
359 Standard_Real f, l, aTolVnew, aDistPP, aTolPC, aTolV;
361 const Handle(Geom_Curve)& aCur = BRep_Tool::Curve(aE, f, l);
363 gp_Pnt aP1 = BRep_Tool::Pnt(aV);
364 gp_Pnt aP2 = aCur->Value(aT);
366 aDistPP=aP1.Distance(aP2);
368 aTolPC=Precision::PConfusion();
369 aTolV=BRep_Tool::Tolerance(aV);
370 if (aDistPP > (aTolV+aTolPC)) {
371 aTolVnew=Max(aTolE, aDistPP);
372 UpdateVertex(nV[j], aTolVnew);
376 bIsOnPave[j] = ForceInterfVF(nV[j], nF);
381 if (!bIsOnPave[0] && !bIsOnPave[1]) {
382 if (CheckFacePaves(aVnew, aMIFOn)) {
386 Standard_Real aTolVnew = BRep_Tool::Tolerance(aVnew);
387 aTolVnew = Max(aTolVnew, Max(aTolE, aTolF));
388 BRep_Builder().UpdateVertex(aVnew, aTolVnew);
390 // increase tolerance for Line/Plane intersection, but do not update
391 // the vertex till its intersection with some other shape
392 IntTools_Range aCR = aCPart.Range1();
393 aTolVnew = Max(aTolVnew, (aCR.Last() - aCR.First()) / 2.);
396 const gp_Pnt& aPnew = BRep_Tool::Pnt(aVnew);
398 if (!myContext->IsPointInFace(aPnew, aF, aTolVnew)) {
404 BOPDS_InterfEF& aEF=aEFs.Append1();
406 aEF.SetIndices(nE, nF);
407 aEF.SetCommonPart(aCPart);
409 myDS->AddInterf(nE, nF);
411 BOPDS_CoupleOfPaveBlocks aCPB;
413 aCPB.SetPaveBlocks(aPB, aPB);
414 aCPB.SetIndexInterf(iX);
415 aCPB.SetTolerance(aTolVnew);
416 aMVCPB.Add(aVnew, aCPB);
424 BOPDS_InterfEF& aEF=aEFs.Append1();
426 aEF.SetIndices(nE, nF);
428 bV[0]=CheckFacePaves(nV[0], aMIFOn, aMIFIn);
429 bV[1]=CheckFacePaves(nV[1], aMIFOn, aMIFIn);
430 if (!bV[0] || !bV[1]) {
431 myDS->AddInterf(nE, nF);
434 aEF.SetCommonPart(aCPart);
436 myDS->AddInterf(nE, nF);
438 BOPAlgo_Tools::FillMap(aPB, nF, aMPBLI, aAllocator);
445 }//for (i=1; i<=aNbCPrts; ++i) {
446 }// for (k=0; k < aNbEdgeEdge; ++k) {
448 //=========================================
450 //=========================================
451 BOPAlgo_Tools::PerformCommonBlocks(aMPBLI, aAllocator, myDS);
452 PerformNewVertices(aMVCPB, aAllocator, Standard_False);
454 // Update FaceInfoIn for all faces having EF common parts
455 BOPCol_MapIteratorOfMapOfInteger aItMI;
456 aItMI.Initialize(aMIEFC);
457 for (; aItMI.More(); aItMI.Next()) {
459 myDS->UpdateFaceInfoIn(nF);
461 //-----------------------------------------------------scope t
465 ////aAllocator.Nullify();
467 //=======================================================================
468 // function: CheckFacePaves
470 //=======================================================================
471 Standard_Boolean BOPAlgo_PaveFiller::CheckFacePaves
472 (const Standard_Integer nVx,
473 const BOPCol_MapOfInteger& aMIFOn,
474 const BOPCol_MapOfInteger& aMIFIn)
476 Standard_Boolean bRet;
478 BOPCol_MapIteratorOfMapOfInteger aIt;
482 aIt.Initialize(aMIFOn);
483 for (; aIt.More(); aIt.Next()) {
490 aIt.Initialize(aMIFIn);
491 for (; aIt.More(); aIt.Next()) {
501 //=======================================================================
502 // function: CheckFacePaves
504 //=======================================================================
505 Standard_Boolean BOPAlgo_PaveFiller::CheckFacePaves
506 (const TopoDS_Vertex& aVnew,
507 const BOPCol_MapOfInteger& aMIF)
509 Standard_Boolean bRet;
510 Standard_Integer nV, iFlag;
511 BOPCol_MapIteratorOfMapOfInteger aIt;
515 aIt.Initialize(aMIF);
516 for (; aIt.More(); aIt.Next()) {
518 const TopoDS_Vertex& aV=(*(TopoDS_Vertex *)(&myDS->Shape(nV)));
519 iFlag=BOPTools_AlgoTools::ComputeVV(aVnew, aV);
527 //=======================================================================
528 //function : ForceInterfVF
530 //=======================================================================
531 Standard_Boolean BOPAlgo_PaveFiller::ForceInterfVF
532 (const Standard_Integer nV,
533 const Standard_Integer nF)
535 Standard_Boolean bRet;
536 Standard_Integer iFlag, nVx;
537 Standard_Real U, V, aTolVNew;
539 bRet = Standard_False;
540 const TopoDS_Vertex& aV = *(TopoDS_Vertex*)&myDS->Shape(nV);
541 const TopoDS_Face& aF = *(TopoDS_Face*) &myDS->Shape(nF);
543 iFlag = myContext->ComputeVF(aV, aF, U, V, aTolVNew, myFuzzyValue);
544 if (iFlag == 0 || iFlag == -2) {
547 BOPDS_VectorOfInterfVF& aVFs=myDS->InterfVF();
548 aVFs.SetIncrement(10);
550 BOPDS_InterfVF& aVF=aVFs.Append1();
552 aVF.SetIndices(nV, nF);
555 myDS->AddInterf(nV, nF);
557 // 3 update vertex V/F if necessary
558 nVx=UpdateVertex(nV, aTolVNew);
560 if (myDS->IsNewShape(nVx)) {
561 aVF.SetIndexNew(nVx);
564 BOPDS_FaceInfo& aFI=myDS->ChangeFaceInfo(nF);
565 BOPCol_MapOfInteger& aMVIn=aFI.ChangeVerticesIn();
570 //=======================================================================
571 //function : ReduceIntersectionRange
573 //=======================================================================
574 void BOPAlgo_PaveFiller::ReduceIntersectionRange(const Standard_Integer theV1,
575 const Standard_Integer theV2,
576 const Standard_Integer theE,
577 const Standard_Integer theF,
578 Standard_Real& theTS1,
579 Standard_Real& theTS2)
581 if (!myDS->IsNewShape(theV1) &&
582 !myDS->IsNewShape(theV2)) {
586 if (!myDS->HasInterfShapeSubShapes(theE, theF)) {
590 BOPDS_VectorOfInterfEE& aEEs = myDS->InterfEE();
591 Standard_Integer aNbEEs = aEEs.Extent();
596 Standard_Integer i, nV, nE1, nE2;
597 Standard_Real aTR1, aTR2;
599 // get face's edges to check that E/E contains the edge from the face
600 BOPCol_MapOfInteger aMFE;
601 const BOPCol_ListOfInteger& aLI = myDS->ShapeInfo(theF).SubShapes();
602 BOPCol_ListIteratorOfListOfInteger aItLI(aLI);
603 for (; aItLI.More(); aItLI.Next()) {
605 if (myDS->ShapeInfo(nE1).ShapeType() == TopAbs_EDGE) {
610 for (i = 0; i < aNbEEs; ++i) {
611 BOPDS_InterfEE& aEE = aEEs(i);
612 if (!aEE.HasIndexNew()) {
618 if (nV != theV1 && nV != theV2) {
622 // check that the intersection is between the edge
623 // and one of the face's edge
624 aEE.Indices(nE1, nE2);
625 if (((theE != nE1) && (theE != nE2)) ||
626 (!aMFE.Contains(nE1) && !aMFE.Contains(nE2))) {
630 // update the intersection range
631 const IntTools_CommonPrt& aCPart = aEE.CommonPart();
632 const IntTools_Range& aCRange =
633 (theE == nE1) ? aCPart.Range1() : aCPart.Ranges2().First();
634 aCRange.Range(aTR1, aTR2);