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.
18 #include <BOPAlgo_PaveFiller.hxx>
19 #include <BOPAlgo_SectionAttribute.hxx>
20 #include <BOPAlgo_Tools.hxx>
21 #include <BOPCol_IndexedMapOfShape.hxx>
22 #include <BOPCol_NCVector.hxx>
23 #include <BOPCol_Parallel.hxx>
24 #include <BOPDS_CommonBlock.hxx>
25 #include <BOPDS_Curve.hxx>
26 #include <BOPDS_DS.hxx>
27 #include <BOPDS_FaceInfo.hxx>
28 #include <BOPDS_Interf.hxx>
29 #include <BOPDS_Iterator.hxx>
30 #include <BOPDS_ListOfPaveBlock.hxx>
31 #include <BOPDS_MapOfPaveBlock.hxx>
32 #include <BOPDS_Pave.hxx>
33 #include <BOPDS_PaveBlock.hxx>
34 #include <BOPDS_ShapeInfo.hxx>
35 #include <BOPDS_VectorOfCurve.hxx>
36 #include <BOPDS_VectorOfFaceInfo.hxx>
37 #include <BOPDS_VectorOfInterfFF.hxx>
38 #include <BOPDS_VectorOfListOfPaveBlock.hxx>
39 #include <BOPTools_AlgoTools.hxx>
40 #include <BOPTools_AlgoTools2D.hxx>
41 #include <BRep_Builder.hxx>
42 #include <BRep_Tool.hxx>
43 #include <BRepBndLib.hxx>
44 #include <Geom2d_Curve.hxx>
45 #include <Geom_Curve.hxx>
46 #include <Geom_Plane.hxx>
47 #include <Geom_RectangularTrimmedSurface.hxx>
48 #include <Geom_Surface.hxx>
49 #include <GeomAPI_ProjectPointOnCurve.hxx>
50 #include <GeomAPI_ProjectPointOnSurf.hxx>
52 #include <IntTools_Context.hxx>
53 #include <IntTools_Tools.hxx>
55 #include <TopExp_Explorer.hxx>
57 #include <TopoDS_Edge.hxx>
58 #include <TopoDS_Face.hxx>
59 #include <TopoDS_Vertex.hxx>
63 Standard_Boolean IsBasedOnPlane(const TopoDS_Face& aF);
66 static void UpdateVertices(const TopoDS_Edge& aE,
67 const TopoDS_Face& aF);
69 //=======================================================================
70 //class : BOPAlgo_SplitEdge
72 //=======================================================================
73 class BOPAlgo_SplitEdge : public BOPAlgo_Algo {
85 virtual ~BOPAlgo_SplitEdge() {
88 void SetData(const TopoDS_Edge& aE,
89 const TopoDS_Vertex& aV1,
90 const Standard_Real aT1,
91 const TopoDS_Vertex& aV2,
92 const Standard_Real aT2) {
101 void SetPaveBlock(const Handle(BOPDS_PaveBlock)& aPB) {
105 Handle(BOPDS_PaveBlock)& PaveBlock() {
109 void SetCommonBlock(const Handle(BOPDS_CommonBlock)& aCB) {
113 Handle(BOPDS_CommonBlock)& CommonBlock() {
117 const TopoDS_Edge& SplitEdge() const {
121 const Bnd_Box Box() {
125 Standard_Real Tolerance() const {
129 void SetDS(const BOPDS_PDS theDS) {
133 void SetContext(const Handle(IntTools_Context)& aContext) {
134 myContext = aContext;
137 virtual void Perform () {
138 BOPAlgo_Algo::UserBreak();
139 myTol = BOPAlgo_Tools::ComputeToleranceOfCB(myCB, myDS, myContext);
140 BOPTools_AlgoTools::MakeSplitEdge(myE,
144 BRepBndLib::Add(myESp, myBox);
145 myBox.SetGap(myBox.GetGap() + Precision::Confusion());
156 Handle(BOPDS_PaveBlock) myPB;
157 Handle(BOPDS_CommonBlock) myCB;
164 Handle(IntTools_Context) myContext;
167 //=======================================================================
168 typedef BOPCol_NCVector
169 <BOPAlgo_SplitEdge> BOPAlgo_VectorOfSplitEdge;
171 typedef BOPCol_ContextFunctor
173 BOPAlgo_VectorOfSplitEdge,
174 Handle(IntTools_Context),
175 IntTools_Context> BOPAlgo_SplitEdgeFunctor;
177 typedef BOPCol_ContextCnt
178 <BOPAlgo_SplitEdgeFunctor,
179 BOPAlgo_VectorOfSplitEdge,
180 Handle(IntTools_Context)> BOPAlgo_SplitEdgeCnt;
182 //=======================================================================
183 //class : BOPAlgo_MPC
185 //=======================================================================
186 class BOPAlgo_MPC : public BOPAlgo_Algo {
189 DEFINE_STANDARD_ALLOC
193 myFlag(Standard_False) {
196 virtual ~BOPAlgo_MPC(){
199 void SetEdge(const TopoDS_Edge& aE) {
203 const TopoDS_Edge& Edge() const {
207 void SetFace(const TopoDS_Face& aF) {
211 const TopoDS_Face& Face() const {
215 void SetFlag(const Standard_Boolean bFlag) {
219 Standard_Boolean Flag() const {
223 void SetData(const TopoDS_Edge& aEz,
224 const TopoDS_Vertex& aV1,
225 const Standard_Real aT1,
226 const TopoDS_Vertex& aV2,
227 const Standard_Real aT2) {
235 void SetContext(const Handle(IntTools_Context)& aContext) {
239 const Handle(IntTools_Context)& Context()const {
243 virtual void Perform() {
244 Standard_Integer iErr;
247 if (!myEz.IsNull()) {
250 BOPTools_AlgoTools::MakeSplitEdge(myEz,myV1, myT1,
254 BOPTools_AlgoTools2D::AttachExistingPCurve(aSpz,
261 BOPTools_AlgoTools2D::BuildPCurveForEdgeOnFace(myE, myF, myContext);
265 UpdateVertices(myE, myF);
270 Standard_Boolean myFlag;
279 Handle(IntTools_Context) myContext;
282 //=======================================================================
283 typedef BOPCol_NCVector
284 <BOPAlgo_MPC> BOPAlgo_VectorOfMPC;
286 typedef BOPCol_ContextFunctor
289 Handle(IntTools_Context),
290 IntTools_Context> BOPAlgo_MPCFunctor;
292 typedef BOPCol_ContextCnt
295 Handle(IntTools_Context)> BOPAlgo_MPCCnt;
297 //=======================================================================
298 //class : BOPAlgo_BPC
300 //=======================================================================
309 void SetFace(const TopoDS_Face& aF) {
313 void SetEdge(const TopoDS_Edge& aE) {
317 const TopoDS_Edge& GetEdge() const {
320 const TopoDS_Face& GetFace() const {
323 const Handle(Geom2d_Curve)& GetCurve2d() const {
326 Standard_Boolean IsToUpdate() const {
331 BOPTools_AlgoTools2D::BuildPCurveForEdgeOnPlane (myE, myF, myCurve, myToUpdate);
337 Handle(Geom2d_Curve) myCurve;
338 Standard_Boolean myToUpdate;
340 //=======================================================================
341 typedef BOPCol_NCVector
342 <BOPAlgo_BPC> BOPAlgo_VectorOfBPC;
344 typedef BOPCol_Functor
346 BOPAlgo_VectorOfBPC> BOPAlgo_BPCFunctor;
350 BOPAlgo_VectorOfBPC> BOPAlgo_BPCCnt;
353 //=======================================================================
354 // function: MakeSplitEdges
356 //=======================================================================
357 void BOPAlgo_PaveFiller::MakeSplitEdges()
359 Standard_Integer aNbPBP;
363 BOPDS_VectorOfListOfPaveBlock& aPBP=myDS->ChangePaveBlocksPool();
364 aNbPBP=aPBP.Extent();
369 Standard_Boolean bCB, bV1, bV2;
370 Standard_Integer i, nE, nV1, nV2, nSp, aNbPB, aNbVBSE, k;
371 Standard_Real aT1, aT2;
372 BOPDS_ListIteratorOfListOfPaveBlock aItPB;
373 Handle(BOPDS_PaveBlock) aPB;
374 BOPDS_MapOfPaveBlock aMPB(100);
375 TopoDS_Vertex aV1, aV2;
377 BOPAlgo_VectorOfSplitEdge aVBSE;
380 UpdateCommonBlocksWithSDVertices();
382 aNbPBP=aPBP.Extent();
384 for (i=0; i<aNbPBP; ++i) {
385 BOPDS_ListOfPaveBlock& aLPB=aPBP(i);
390 aPB->Indices(nV1, nV2);
391 bV1=myDS->IsNewShape(nV1);
392 bV2=myDS->IsNewShape(nV2);
393 bCB=myDS->IsCommonBlock(aPB);
395 if (!(bV1 || bV2)) { // no new vertices here
396 if (!myNonDestructive || !bCB) {
398 if (!aPB->HasEdge()) {
399 const Handle(BOPDS_CommonBlock)& aCB = myDS->CommonBlock(aPB);
400 nE = aCB->PaveBlock1()->OriginalEdge();
402 // Compute tolerance of the common block and update the edge
403 Standard_Real aTol = BOPAlgo_Tools::ComputeToleranceOfCB(aCB, myDS, myContext);
404 myDS->UpdateEdgeTolerance(nE, aTol);
408 nE = aPB->OriginalEdge();
416 aItPB.Initialize(aLPB);
417 for (; aItPB.More(); aItPB.Next()) {
419 nE=aPB->OriginalEdge();
420 const BOPDS_ShapeInfo& aSIE=myDS->ShapeInfo(nE);
425 const Handle(BOPDS_CommonBlock)& aCB=myDS->CommonBlock(aPB);
428 aPB=aCB->PaveBlock1();
432 nE=aPB->OriginalEdge();
433 aPB->Indices(nV1, nV2);
434 aPB->Range(aT1, aT2);
436 aE=(*(TopoDS_Edge *)(&myDS->Shape(nE)));
437 aE.Orientation(TopAbs_FORWARD);
439 aV1=(*(TopoDS_Vertex *)(&myDS->Shape(nV1)));
440 aV1.Orientation(TopAbs_FORWARD);
442 aV2=(*(TopoDS_Vertex *)(&myDS->Shape(nV2)));
443 aV2.Orientation(TopAbs_REVERSED);
445 BOPAlgo_SplitEdge& aBSE=aVBSE.Append1();
447 aBSE.SetData(aE, aV1, aT1, aV2, aT2);
448 aBSE.SetPaveBlock(aPB);
450 aBSE.SetCommonBlock(aCB);
453 aBSE.SetProgressIndicator(myProgressIndicator);
455 } // for (; aItPB.More(); aItPB.Next()) {
456 } // for (i=0; i<aNbPBP; ++i) {
458 aNbVBSE=aVBSE.Extent();
459 //======================================================
460 BOPAlgo_SplitEdgeCnt::Perform(myRunParallel, aVBSE, myContext);
461 //======================================================
465 aSI.SetShapeType(TopAbs_EDGE);
467 for (k=0; k < aNbVBSE; ++k) {
468 BOPAlgo_SplitEdge& aBSE=aVBSE(k);
470 const TopoDS_Edge& aSp=aBSE.SplitEdge();
471 const Bnd_Box& aBox=aBSE.Box();
473 Handle(BOPDS_PaveBlock) aPBk=aBSE.PaveBlock();
474 Handle(BOPDS_CommonBlock)& aCBk=aBSE.CommonBlock();
477 aSI.ChangeBox()=aBox;
479 nSp=myDS->Append(aSI);
481 if (!aCBk.IsNull()) {
482 myDS->UpdateEdgeTolerance(nSp, aBSE.Tolerance());
491 //=======================================================================
492 // function: SplitEdge
494 //=======================================================================
495 Standard_Integer BOPAlgo_PaveFiller::SplitEdge(const Standard_Integer nE,
496 const Standard_Integer nV1,
497 const Standard_Real aT1,
498 const Standard_Integer nV2,
499 const Standard_Real aT2)
501 Standard_Integer nSp;
502 TopoDS_Vertex aV1, aV2;
506 aSI.SetShapeType(TopAbs_EDGE);
508 aE=(*(TopoDS_Edge *)(&myDS->Shape(nE)));
509 aE.Orientation(TopAbs_FORWARD);
511 aV1=(*(TopoDS_Vertex *)(&myDS->Shape(nV1)));
512 aV1.Orientation(TopAbs_FORWARD);
514 aV2=(*(TopoDS_Vertex *)(&myDS->Shape(nV2)));
515 aV2.Orientation(TopAbs_REVERSED);
517 BOPTools_AlgoTools::MakeSplitEdge(aE, aV1, aT1, aV2, aT2, aSp);
521 Bnd_Box& aBox=aSI.ChangeBox();
522 BRepBndLib::Add(aSp, aBox);
523 aBox.SetGap(aBox.GetGap() + Precision::Confusion());
525 nSp=myDS->Append(aSI);
528 //=======================================================================
529 // function: MakePCurves
531 //=======================================================================
532 void BOPAlgo_PaveFiller::MakePCurves()
534 if (myAvoidBuildPCurve ||
535 (!mySectionAttribute.PCurveOnS1() && !mySectionAttribute.PCurveOnS2()))
537 Standard_Boolean bHasPC;
538 Standard_Integer i, nF1, nF2, aNbC, k, nE, aNbFF, aNbFI, nEx;
539 Standard_Integer j, aNbPBIn, aNbPBOn;
540 BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
541 TopoDS_Face aF1F, aF2F;
542 BOPAlgo_VectorOfMPC aVMPC;
546 // 1. Process Common Blocks
547 const BOPDS_VectorOfFaceInfo& aFIP=myDS->FaceInfoPool();
550 for (i=0; i<aNbFI; ++i) {
551 const BOPDS_FaceInfo& aFI=aFIP(i);
554 aF1F=(*(TopoDS_Face *)(&myDS->Shape(nF1)));
555 aF1F.Orientation(TopAbs_FORWARD);
557 const BOPDS_IndexedMapOfPaveBlock& aMPBIn=aFI.PaveBlocksIn();
558 aNbPBIn = aMPBIn.Extent();
559 for (j = 1; j <= aNbPBIn; ++j) {
560 const Handle(BOPDS_PaveBlock)& aPB = aMPBIn(j);
562 const TopoDS_Edge& aE=(*(TopoDS_Edge *)(&myDS->Shape(nE)));
564 BOPAlgo_MPC& aMPC=aVMPC.Append1();
567 aMPC.SetProgressIndicator(myProgressIndicator);
571 const BOPDS_IndexedMapOfPaveBlock& aMPBOn=aFI.PaveBlocksOn();
572 aNbPBOn = aMPBOn.Extent();
573 for (j = 1; j <= aNbPBOn; ++j) {
574 const Handle(BOPDS_PaveBlock)& aPB = aMPBOn(j);
576 const TopoDS_Edge& aE=(*(TopoDS_Edge *)(&myDS->Shape(nE)));
577 bHasPC=BOPTools_AlgoTools2D::HasCurveOnSurface (aE, aF1F);
582 Handle(BOPDS_CommonBlock) aCB=myDS->CommonBlock(aPB);
587 const BOPDS_ListOfPaveBlock& aLPB=aCB->PaveBlocks();
588 if (aLPB.Extent()<2) {
592 BOPAlgo_MPC& aMPC=aVMPC.Append1();
594 aItLPB.Initialize(aLPB);
595 for(; aItLPB.More(); aItLPB.Next()) {
596 const Handle(BOPDS_PaveBlock)& aPBx=aItLPB.Value();
601 nEx=aPBx->OriginalEdge();
602 const TopoDS_Edge& aEx=(*(TopoDS_Edge *)(&myDS->Shape(nEx)));
603 bHasPC=BOPTools_AlgoTools2D::HasCurveOnSurface (aEx, aF1F);
608 Standard_Integer nV1x, nV2x;
609 Standard_Real aT1x, aT2x;
610 TopoDS_Vertex aV1x, aV2x;
614 aEz.Orientation(TopAbs_FORWARD);
616 aPBx->Indices(nV1x, nV2x);
617 aPBx->Range(aT1x, aT2x);
619 aV1x=(*(TopoDS_Vertex *)(&myDS->Shape(nV1x)));
620 aV1x.Orientation(TopAbs_FORWARD);
622 aV2x=(*(TopoDS_Vertex *)(&myDS->Shape(nV2x)));
623 aV2x.Orientation(TopAbs_REVERSED);
625 aMPC.SetData(aEz, aV1x, aT1x, aV2x, aT2x);
632 aMPC.SetProgressIndicator(myProgressIndicator);
634 }// for (i=0; i<aNbFI; ++i) {
636 // 2. Process section edges
637 Standard_Boolean bPCurveOnS[2];
641 bPCurveOnS[0]=mySectionAttribute.PCurveOnS1();
642 bPCurveOnS[1]=mySectionAttribute.PCurveOnS2();
644 if (bPCurveOnS[0] || bPCurveOnS[1]) {
645 BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
647 for (i=0; i<aNbFF; ++i) {
648 const BOPDS_InterfFF& aFF=aFFs(i);
649 aFF.Indices(nF1, nF2);
651 aFf[0]=(*(TopoDS_Face *)(&myDS->Shape(nF1)));
652 aFf[0].Orientation(TopAbs_FORWARD);
654 aFf[1]=(*(TopoDS_Face *)(&myDS->Shape(nF2)));
655 aFf[1].Orientation(TopAbs_FORWARD);
657 const BOPDS_VectorOfCurve& aVNC=aFF.Curves();
659 for (k=0; k<aNbC; ++k) {
660 const BOPDS_Curve& aNC=aVNC(k);
661 const BOPDS_ListOfPaveBlock& aLPB=aNC.PaveBlocks();
662 aItLPB.Initialize(aLPB);
663 for(; aItLPB.More(); aItLPB.Next()) {
664 const Handle(BOPDS_PaveBlock)& aPB=aItLPB.Value();
666 const TopoDS_Edge& aE=(*(TopoDS_Edge *)(&myDS->Shape(nE)));
668 for (m=0; m<2; ++m) {
670 BOPAlgo_MPC& aMPC=aVMPC.Append1();
672 aMPC.SetFace(aFf[m]);
673 aMPC.SetFlag(bPCurveOnS[m]);
674 aMPC.SetProgressIndicator(myProgressIndicator);
679 }// for (i=0; i<aNbFF; ++i) {
680 }//if (bPCurveOnS1 || bPCurveOnS2 ) {
682 //======================================================
683 BOPAlgo_MPCCnt::Perform(myRunParallel, aVMPC, myContext);
684 //======================================================
686 //=======================================================================
687 //function : UpdateVertices
688 //purpose : update tolerances of vertices comparing extremities of
690 //=======================================================================
691 void UpdateVertices(const TopoDS_Edge& aE,
692 const TopoDS_Face& aF)
695 Standard_Real aT[2], aUx, aVx, aTolV2, aD2, aD;
698 Handle(Geom_Surface) aS;
699 Handle(Geom_Curve) aC3D;
700 Handle(Geom2d_Curve) aC2D;
706 aEf.Orientation(TopAbs_FORWARD);
708 TopExp::Vertices(aEf, aV[0], aV[1]);
710 aS=BRep_Tool::Surface(aF);
711 aC3D=BRep_Tool::Curve(aEf, aT[0], aT[1]);
712 aC2D=BRep_Tool::CurveOnSurface(aEf, aF, aT[0], aT[1]);
714 for (j=0; j<2; ++j) {
715 aTolV2=BRep_Tool::Tolerance(aV[j]);
716 aTolV2=aTolV2*aTolV2;
718 aC3D->D0(aT[j], aP3D);
719 aC2D->D0(aT[j], aP2Dx);
720 aP2Dx.Coord(aUx, aVx);
721 aS->D0(aUx, aVx, aP3Dx);
722 aD2=aP3D.SquareDistance(aP3Dx);
725 aBB.UpdateVertex(aV[j], aD);
729 //=======================================================================
732 //=======================================================================
733 void BOPAlgo_PaveFiller::Prepare()
735 if (myNonDestructive) {
736 // do not allow storing pcurves in original edges if non-destructive mode is on
739 TopAbs_ShapeEnum aType[] = {
744 Standard_Boolean bIsBasedOnPlane;
745 Standard_Integer i, aNb, n1, nF, aNbF;
746 TopExp_Explorer aExp;
747 BOPCol_IndexedMapOfShape aMF;
752 for(i=0; i<aNb; ++i) {
753 myIterator->Initialize(aType[i], aType[2]);
754 for (; myIterator->More(); myIterator->Next()) {
755 myIterator->Value(n1, nF);
756 const TopoDS_Face& aF=(*(TopoDS_Face *)(&myDS->Shape(nF)));
758 bIsBasedOnPlane=IsBasedOnPlane(aF);
759 if (bIsBasedOnPlane) {
770 // Build pcurves of edges on planes; first collect pairs edge-face.
771 BOPAlgo_VectorOfBPC aVBPC;
773 for (i = 1; i <= aNbF; ++i) {
774 const TopoDS_Face& aF = *(TopoDS_Face*)&aMF(i);
775 aExp.Init(aF, aType[1]);
776 for (; aExp.More(); aExp.Next()) {
777 const TopoDS_Edge& aE=*((TopoDS_Edge *)&aExp.Current());
778 BOPAlgo_BPC& aBPC=aVBPC.Append1();
784 //======================================================
785 BOPAlgo_BPCCnt::Perform(myRunParallel, aVBPC);
786 //======================================================
788 // pcurves are built, and now update edges
791 for (i = 0; i < aVBPC.Extent(); i++) {
792 const BOPAlgo_BPC& aBPC=aVBPC(i);
793 if (aBPC.IsToUpdate()) {
794 Standard_Real aTolE = BRep_Tool::Tolerance(aBPC.GetEdge());
795 aBB.UpdateEdge(aBPC.GetEdge(), aBPC.GetCurve2d(), aBPC.GetFace(), aTolE);
799 //=======================================================================
800 //function : IsBasedOnPlane
802 //=======================================================================
803 Standard_Boolean IsBasedOnPlane(const TopoDS_Face& aF)
805 TopLoc_Location aLoc;
806 Handle(Geom_RectangularTrimmedSurface) aGRTS;
807 Handle(Geom_Plane) aGP;
809 const Handle(Geom_Surface)& aS = BRep_Tool::Surface(aF, aLoc);
810 aGRTS = Handle(Geom_RectangularTrimmedSurface)::DownCast(aS);
811 if(!aGRTS.IsNull()) {
812 aGP = Handle(Geom_Plane)::DownCast(aGRTS->BasisSurface());
815 aGP = Handle(Geom_Plane)::DownCast(aS);
818 return (!aGP.IsNull());