1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: GEOMAlgo_WireSplitter.cxx
25 // Author: Peter KURNEV
28 #include <GEOMAlgo_WireSplitter.ixx>
30 #include <TColStd_SequenceOfReal.hxx>
31 #include <Precision.hxx>
33 #include <gp_Pnt2d.hxx>
34 #include <gp_Vec2d.hxx>
35 #include <TColgp_SequenceOfPnt2d.hxx>
37 #include <Geom_Curve.hxx>
38 #include <Geom2d_Curve.hxx>
39 #include <GeomAdaptor_Surface.hxx>
41 #include <TopAbs_Orientation.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopoDS_Edge.hxx>
46 #include <TopoDS_Face.hxx>
47 #include <TopoDS_Iterator.hxx>
50 #include <BRep_Tool.hxx>
51 #include <BRepAdaptor_Surface.hxx>
52 #include <BRepAdaptor_Curve2d.hxx>
55 #include <TopExp_Explorer.hxx>
59 #include <TopTools_SequenceOfShape.hxx>
60 #include <TopTools_ListOfShape.hxx>
61 #include <TopTools_ListIteratorOfListOfShape.hxx>
62 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
63 #include <TopTools_IndexedMapOfShape.hxx>
65 #include <BOPTColStd_ListOfListOfShape.hxx>
66 #include <BOPTColStd_ListIteratorOfListOfListOfShape.hxx>
68 #include <BOPTools_Tools2D.hxx>
70 #include <BOP_EdgeInfo.hxx>
71 #include <BOP_ListOfEdgeInfo.hxx>
72 #include <BOP_ListIteratorOfListOfEdgeInfo.hxx>
73 #include <BOP_IndexedDataMapOfVertexListEdgeInfo.hxx>
76 void Path (const GeomAdaptor_Surface& aGAS,
77 const TopoDS_Face& myFace,
78 const TopoDS_Vertex& aVa,
79 const TopoDS_Edge& aEOuta,
80 BOP_EdgeInfo& anEdgeInfo,
81 TopTools_SequenceOfShape& aLS,
82 TopTools_SequenceOfShape& aVertVa,
83 TColgp_SequenceOfPnt2d& aCoordVa,
84 BOPTColStd_ListOfListOfShape& myShapes,
85 BOP_IndexedDataMapOfVertexListEdgeInfo& mySmartMap);
89 Standard_Real Angle (const gp_Dir2d& aDir2D);
93 void GetNextVertex(const TopoDS_Vertex& aV,
94 const TopoDS_Edge& aE,
97 Standard_Real ClockWiseAngle(const Standard_Real aAngleIn,
98 const Standard_Real aAngleOut);
101 Standard_Real AngleIn(const TopoDS_Edge& aEIn,
102 const BOP_ListOfEdgeInfo& aLEInfo);
105 Standard_Real Angle2D (const TopoDS_Vertex& aV,
106 const TopoDS_Edge& anEdge,
107 const TopoDS_Face& myFace,
108 const GeomAdaptor_Surface& aGAS,
109 const Standard_Boolean aFlag);
111 gp_Pnt2d Coord2d (const TopoDS_Vertex& aV1,
112 const TopoDS_Edge& aE1,
113 const TopoDS_Face& aF);
115 gp_Pnt2d Coord2dVf (const TopoDS_Edge& aE,
116 const TopoDS_Face& aF);
118 Standard_Real Tolerance2D (const TopoDS_Vertex& aV,
119 const GeomAdaptor_Surface& aGAS);
122 Standard_Integer NbWaysOut(const BOP_ListOfEdgeInfo& );
125 //=======================================================================
128 //=======================================================================
129 GEOMAlgo_WireSplitter::GEOMAlgo_WireSplitter()
132 myNothingToDo(Standard_False)
135 //=======================================================================
138 //=======================================================================
139 GEOMAlgo_WireSplitter::~GEOMAlgo_WireSplitter()
142 //=======================================================================
145 //=======================================================================
146 void GEOMAlgo_WireSplitter::SetFace(const TopoDS_Face& aFace)
150 //=======================================================================
153 //=======================================================================
154 const TopoDS_Face& GEOMAlgo_WireSplitter::Face()const
158 //=======================================================================
159 // function: SetEdges
161 //=======================================================================
162 void GEOMAlgo_WireSplitter::SetEdges(const TopTools_ListOfShape& aLE)
164 TopTools_ListIteratorOfListOfShape anIt;
167 anIt.Initialize(aLE);
168 for (; anIt.More(); anIt.Next()) {
169 const TopoDS_Shape& aE =anIt.Value();
171 if (aE.Orientation()==TopAbs_INTERNAL){
178 //=======================================================================
181 //=======================================================================
182 const TopTools_ListOfShape& GEOMAlgo_WireSplitter::Edges()const
186 //=======================================================================
187 // function: IsNothingToDo
189 //=======================================================================
190 Standard_Boolean GEOMAlgo_WireSplitter::IsNothingToDo()const
192 return myNothingToDo;
194 //=======================================================================
197 //=======================================================================
198 const BOPTColStd_ListOfListOfShape& GEOMAlgo_WireSplitter::Shapes()const
202 //=======================================================================
205 //=======================================================================
206 void GEOMAlgo_WireSplitter::Perform()
209 myNothingToDo=Standard_True;
211 Standard_Integer index, i, aNb, aCntIn, aCntOut;
212 Standard_Boolean anIsIn;
213 Standard_Real anAngle;
215 BOP_ListOfEdgeInfo emptyInfo;
216 TopTools_ListIteratorOfListOfShape anItList;
218 // 1.Filling mySmartMap
221 anItList.Initialize(myEdges);
222 for (; anItList.More(); anItList.Next()) {
223 const TopoDS_Edge& anEdge = TopoDS::Edge(anItList.Value());
225 if (!BOPTools_Tools2D::HasCurveOnSurface (anEdge, myFace)) {
229 TopExp_Explorer anExpVerts (anEdge, TopAbs_VERTEX);
230 for (; anExpVerts.More(); anExpVerts.Next()) {
231 const TopoDS_Shape& aVertex= anExpVerts.Current();
233 index = mySmartMap.FindIndex(aVertex);
235 index=mySmartMap.Add(aVertex, emptyInfo);
238 BOP_ListOfEdgeInfo& aListOfEInfo=mySmartMap(index);
241 aEInfo.SetEdge(anEdge);
243 TopAbs_Orientation anOr=aVertex.Orientation();
245 if (anOr==TopAbs_FORWARD) {
246 aEInfo.SetInFlag(Standard_False);
249 else if (anOr==TopAbs_REVERSED) {
250 aEInfo.SetInFlag(Standard_True);
253 aListOfEInfo.Append(aEInfo);
257 aNb=mySmartMap.Extent();
260 myNothingToDo=Standard_True;
262 for (i=1; i<=aNb; i++) {
265 const BOP_ListOfEdgeInfo& aLEInfo= mySmartMap(i);
266 BOP_ListIteratorOfListOfEdgeInfo anIt(aLEInfo);
267 for (; anIt.More(); anIt.Next()) {
268 const BOP_EdgeInfo& anEdgeInfo=anIt.Value();
269 anIsIn=anEdgeInfo.IsIn();
277 if (aCntIn!=1 || aCntOut!=1) {
278 myNothingToDo=Standard_False;
283 // Each vertex has one edge In and one - Out. Good. But it is not enought
284 // to consider that nothing to do with this. We must check edges on TShape
285 // coinsidence. If there are such edges there is something to do with.
288 Standard_Integer aNbE, aNbMapEE;
289 TopTools_IndexedDataMapOfShapeListOfShape aMapEE;
290 aNbE=myEdges.Extent();
292 anItList.Initialize(myEdges);
293 for (; anItList.More(); anItList.Next()) {
294 const TopoDS_Shape& aE = anItList.Value();
296 if (!aMapEE.Contains(aE)) {
297 TopTools_ListOfShape aLEx;
299 aMapEE.Add(aE, aLEx);
302 TopTools_ListOfShape& aLEx=aMapEE.ChangeFromKey(aE);
307 Standard_Boolean bFlag;
309 aNbMapEE=aMapEE.Extent();
310 for (i=1; i<=aNbMapEE; i++) {
311 const TopTools_ListOfShape& aLEx=aMapEE(i);
318 const TopoDS_Shape& aE1=aLEx.First();
319 const TopoDS_Shape& aE2=aLEx.Last();
320 if (aE1.IsSame(aE2)) {
321 bFlag=Standard_False;
326 bFlag=Standard_False;
330 myNothingToDo=myNothingToDo && bFlag;
339 // 3. Angles in mySmartMap
340 BRepAdaptor_Surface aBAS(myFace);
341 const GeomAdaptor_Surface& aGAS=aBAS.Surface();
342 for (i=1; i<=aNb; i++) {
343 const TopoDS_Vertex& aV=TopoDS::Vertex (mySmartMap.FindKey(i));
344 const BOP_ListOfEdgeInfo& aLEInfo= mySmartMap(i);
346 BOP_ListIteratorOfListOfEdgeInfo anIt(aLEInfo);
347 for (; anIt.More(); anIt.Next()) {
348 BOP_EdgeInfo& anEdgeInfo=anIt.Value();
349 const TopoDS_Edge& aE=anEdgeInfo.Edge();
351 TopoDS_Vertex aVV=aV;
353 anIsIn=anEdgeInfo.IsIn();
356 aVV.Orientation(TopAbs_REVERSED);
357 anAngle=Angle2D (aVV, aE, myFace, aGAS, Standard_True);
362 aVV.Orientation(TopAbs_FORWARD);
363 anAngle=Angle2D (aVV, aE, myFace, aGAS, Standard_False);
365 anEdgeInfo.SetAngle(anAngle);
372 Standard_Boolean anIsOut, anIsNotPassed;
374 TopTools_SequenceOfShape aLS, aVertVa;
375 TColgp_SequenceOfPnt2d aCoordVa;
377 BOP_ListIteratorOfListOfEdgeInfo anIt;
379 for (i=1; i<=aNb; i++) {
380 const TopoDS_Vertex aVa=TopoDS::Vertex (mySmartMap.FindKey(i));
381 const BOP_ListOfEdgeInfo& aLEInfo=mySmartMap(i);
383 anIt.Initialize(aLEInfo);
384 for (; anIt.More(); anIt.Next()) {
385 BOP_EdgeInfo& anEdgeInfo=anIt.Value();
386 const TopoDS_Edge& aEOuta=anEdgeInfo.Edge();
388 anIsOut=!anEdgeInfo.IsIn();
389 anIsNotPassed=!anEdgeInfo.Passed();
391 if (anIsOut && anIsNotPassed) {
397 Path(aGAS, myFace, aVa, aEOuta, anEdgeInfo, aLS,
398 aVertVa, aCoordVa, myShapes, mySmartMap);
404 Standard_Integer aNbV, aNbE;
405 TopoDS_Vertex aV1, aV2;
406 BOPTColStd_ListOfListOfShape aShapes;
407 BOPTColStd_ListIteratorOfListOfListOfShape anItW(myShapes);
409 for (; anItW.More(); anItW.Next()) {
410 TopTools_IndexedMapOfShape aMV, aME;
411 const TopTools_ListOfShape& aLE=anItW.Value();
412 TopTools_ListIteratorOfListOfShape anItE(aLE);
413 for (; anItE.More(); anItE.Next()) {
414 const TopoDS_Edge& aE=TopoDS::Edge(anItE.Value());
416 TopExp::Vertices(aE, aV1, aV2);
428 anItW.Initialize(aShapes);
429 for (; anItW.More(); anItW.Next()) {
430 const TopTools_ListOfShape& aLE=anItW.Value();
431 myShapes.Append(aLE);
437 //=======================================================================
440 //=======================================================================
441 void Path (const GeomAdaptor_Surface& aGAS,
442 const TopoDS_Face& myFace,
443 const TopoDS_Vertex& aVa,
444 const TopoDS_Edge& aEOuta,
445 BOP_EdgeInfo& anEdgeInfo,
446 TopTools_SequenceOfShape& aLS,
447 TopTools_SequenceOfShape& aVertVa,
448 TColgp_SequenceOfPnt2d& aCoordVa,
449 BOPTColStd_ListOfListOfShape& myShapes,
450 BOP_IndexedDataMapOfVertexListEdgeInfo& mySmartMap)
453 Standard_Integer i,j, aNb, aNbj;
454 Standard_Real aTol, anAngleIn, anAngleOut, anAngle, aMinAngle;
455 Standard_Real aTol2D, aTol2D2;
456 Standard_Real aTol2, aD2;//, aTolUVb, aTolVVb;
457 Standard_Boolean anIsSameV2d, anIsSameV, anIsFound, anIsOut, anIsNotPassed;
458 BOP_ListIteratorOfListOfEdgeInfo anIt;
466 // Do not escape through edge from which you enter
469 const TopoDS_Shape& anEPrev=aLS(aNb);
470 if (anEPrev.IsSame(aEOuta)) {
476 anEdgeInfo.SetPassed(Standard_True);
480 TopoDS_Vertex pVa=aVa;
481 pVa.Orientation(TopAbs_FORWARD);
482 gp_Pnt2d aPa=Coord2d(pVa, aEOuta, myFace);
483 aCoordVa.Append(aPa);
485 GetNextVertex (pVa, aEOuta, aVb);
487 gp_Pnt2d aPb=Coord2d(aVb, aEOuta, myFace);
489 //const BOP_ListOfEdgeInfo& aLEInfoVb=mySmartMap.FindFromKey(aVb);
491 aTol=2.*Tolerance2D(aVb, aGAS);
497 TopTools_ListOfShape aBuf;
499 for (i=aNb; i>0; i--) {
500 const TopoDS_Shape& aVPrev=aVertVa(i);
501 const gp_Pnt2d& aPaPrev=aCoordVa(i);
502 const TopoDS_Shape& aEPrev=aLS(i);
506 anIsSameV=aVPrev.IsSame(aVb);
507 anIsSameV2d=Standard_False;
510 anIsSameV2d = Standard_True;
512 aD2=aPaPrev.SquareDistance(aPb);
513 anIsSameV2d =aD2<aTol2;
516 if (anIsSameV && anIsSameV2d) {
517 myShapes.Append(aBuf);
519 TopTools_SequenceOfShape aLSt, aVertVat;
520 TColgp_SequenceOfPnt2d aCoordVat;
532 aVb=TopoDS::Vertex(aVertVa(i));
534 for (j=1; j<=aNbj; j++) {
536 aVertVat.Append(aVertVa(j));
537 aCoordVat.Append(aCoordVa(j));
553 aTol2D=2.*Tolerance2D(aVb, aGAS);
554 aTol2D2=100.*aTol2D*aTol2D;
556 // anAngleIn in Vb from edge aEOuta
557 const BOP_ListOfEdgeInfo& aLEInfo=mySmartMap.FindFromKey(aVb);
559 anAngleIn=AngleIn(aEOuta, aLEInfo);
562 BOP_EdgeInfo *pEdgeInfo=NULL;
565 anIsFound=Standard_False;
567 Standard_Integer aCurIndexE = 0;
569 anIt.Initialize(aLEInfo);
570 for (; anIt.More(); anIt.Next()) {
571 BOP_EdgeInfo& anEI=anIt.Value();
572 const TopoDS_Edge& aE=anEI.Edge();
573 anIsOut=!anEI.IsIn();
574 anIsNotPassed=!anEI.Passed();
576 if (anIsOut && anIsNotPassed) {
579 // Is there one way to go out of the vertex
580 // we have to use it only.
581 Standard_Integer iCnt;
582 iCnt=NbWaysOut (aLEInfo);
585 // no way to go . (Error)
590 // the one and only way to go out .
592 anIsFound=Standard_True;
596 // Look for minimal angle and make the choice.
599 aP2Dx=Coord2dVf(aE, myFace);
601 aD2=aP2Dx.SquareDistance(aPb);
607 anAngleOut=anEI.Angle();
609 anAngle=ClockWiseAngle(anAngleIn, anAngleOut);
610 if (anAngle < aMinAngle) {
613 anIsFound=Standard_True;
616 } // for (; anIt.More(); anIt.Next())
619 // no way to go . (Error)
623 aEOutb=pEdgeInfo->Edge();
625 Path (aGAS, myFace, aVb, aEOutb, *pEdgeInfo, aLS,
626 aVertVa, aCoordVa, myShapes, mySmartMap);
628 //=======================================================================
629 // function: Coord2dVf
631 //=======================================================================
632 gp_Pnt2d Coord2dVf (const TopoDS_Edge& aE,
633 const TopoDS_Face& aF)
635 Standard_Real aCoord=99.;
636 gp_Pnt2d aP2D1(aCoord, aCoord);
640 for (; aIt.More(); aIt.Next()) {
641 const TopoDS_Shape& aVx=aIt.Value();
642 if (aVx.Orientation()==TopAbs_FORWARD) {
643 const TopoDS_Vertex& aVxx=TopoDS::Vertex(aVx);
644 aP2D1=Coord2d(aVxx, aE, aF);
650 //=======================================================================
651 // function: Tolerance2D
653 //=======================================================================
654 Standard_Real Tolerance2D (const TopoDS_Vertex& aV,
655 const GeomAdaptor_Surface& aGAS)
657 Standard_Real aTol2D, anUr, aVr, aTolV3D;
658 GeomAbs_SurfaceType aType;
660 aType=aGAS.GetType();
661 aTolV3D=BRep_Tool::Tolerance(aV);
663 anUr=aGAS.UResolution(aTolV3D);
664 aVr =aGAS.VResolution(aTolV3D);
665 aTol2D=(aVr>anUr) ? aVr : anUr;
667 if (aType==GeomAbs_BSplineSurface||
668 aType==GeomAbs_Sphere||
669 GeomAbs_SurfaceOfRevolution) {
670 if (aTol2D < aTolV3D) {
674 if (aType==GeomAbs_BSplineSurface) {
681 //=======================================================================
684 //=======================================================================
685 gp_Pnt2d Coord2d (const TopoDS_Vertex& aV1,
686 const TopoDS_Edge& aE1,
687 const TopoDS_Face& aF)
689 Standard_Real aT, aFirst, aLast;
690 Handle(Geom2d_Curve) aC2D;
693 aT=BRep_Tool::Parameter (aV1, aE1, aF);
694 aC2D=BRep_Tool::CurveOnSurface(aE1, aF, aFirst, aLast);
695 aC2D->D0 (aT, aP2D1);
699 //=======================================================================
702 //=======================================================================
703 Standard_Real AngleIn(const TopoDS_Edge& aEIn,
704 const BOP_ListOfEdgeInfo& aLEInfo)
706 Standard_Real anAngleIn;
707 Standard_Boolean anIsIn;
708 BOP_ListIteratorOfListOfEdgeInfo anIt;
710 anIt.Initialize(aLEInfo);
711 for (; anIt.More(); anIt.Next()) {
712 BOP_EdgeInfo& anEdgeInfo=anIt.Value();
713 const TopoDS_Edge& aE=anEdgeInfo.Edge();
714 anIsIn=anEdgeInfo.IsIn();
716 if (anIsIn && aE==aEIn) {
717 anAngleIn=anEdgeInfo.Angle();
724 //=======================================================================
725 // function: ClockWiseAngle
727 //=======================================================================
728 Standard_Real ClockWiseAngle(const Standard_Real aAngleIn,
729 const Standard_Real aAngleOut)
731 Standard_Real aTwoPi=Standard_PI+Standard_PI;
732 Standard_Real dA, A1, A2, AIn, AOut ;
740 if (AOut >= aTwoPi) {
757 //else if (dA <= 1.e-15) {
758 else if (dA <= 1.e-14) {
763 //=======================================================================
764 // function: GetNextVertex
766 //=======================================================================
767 void GetNextVertex(const TopoDS_Vertex& aV,
768 const TopoDS_Edge& aE,
774 for (; aIt.More(); aIt.Next()) {
775 const TopoDS_Shape& aVx=aIt.Value();
776 if (!aVx.IsEqual(aV)) {
777 aV1=TopoDS::Vertex(aVx);
783 //=======================================================================
786 //=======================================================================
787 Standard_Real Angle2D (const TopoDS_Vertex& aV,
788 const TopoDS_Edge& anEdge,
789 const TopoDS_Face& myFace,
790 const GeomAdaptor_Surface& aGAS,
791 const Standard_Boolean aFlag)
793 Standard_Real aFirst, aLast, aToler, dt, aTV, aTV1, anAngle, aTX;
796 Handle(Geom2d_Curve) aC2D;
798 aTV=BRep_Tool::Parameter (aV, anEdge, myFace);
799 if (Precision::IsInfinite(aTV)) {
803 BOPTools_Tools2D::CurveOnSurface (anEdge, myFace, aC2D,
804 aFirst, aLast, aToler, Standard_True);
806 //modified by NIZNHY-PKV Wed Sep 10 14:06:04 2008f
807 //dt=Tolerance2D(aV, aGAS);
808 dt=2.*Tolerance2D(aV, aGAS);
809 //modified by NIZNHY-PKV Wed Sep 10 14:06:07 2008t
811 aTX=0.25*(aLast - aFirst);
813 // to save direction of the curve as much as it possible
814 // in the case of big tolerances
818 if (fabs (aTV-aFirst) < fabs(aTV - aLast)) {
826 aC2D->D0 (aTV1, aPV1);
829 gp_Vec2d aV2DIn(aPV1, aPV);
833 gp_Vec2d aV2DOut(aPV, aPV1);
837 gp_Dir2d aDir2D(aV2D);
838 anAngle=Angle(aDir2D);
842 //=======================================================================
845 //=======================================================================
846 Standard_Real Angle (const gp_Dir2d& aDir2D)
848 gp_Dir2d aRefDir(1., 0.);
849 Standard_Real anAngle = aRefDir.Angle(aDir2D);
852 anAngle += Standard_PI + Standard_PI;
857 //=======================================================================
858 // function: NbWaysOut
860 //=======================================================================
861 Standard_Integer NbWaysOut(const BOP_ListOfEdgeInfo& aLEInfo)
863 Standard_Boolean bIsOut, bIsNotPassed;
864 Standard_Integer iCnt=0;
865 BOP_ListIteratorOfListOfEdgeInfo anIt;
867 anIt.Initialize(aLEInfo);
868 for (; anIt.More(); anIt.Next()) {
869 BOP_EdgeInfo& anEI=anIt.Value();
872 bIsNotPassed=!anEI.Passed();
873 if (bIsOut && bIsNotPassed) {