1 // Created on: 1993-01-21
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
18 #include <BRep_Tool.hxx>
19 #include <BRepTools.hxx>
20 #include <BRepTools_WireExplorer.hxx>
21 #include <Geom2d_Curve.hxx>
22 #include <Geom_Surface.hxx>
23 #include <GeomAdaptor_Surface.hxx>
24 #include <gp_Pnt2d.hxx>
25 #include <Precision.hxx>
26 #include <Standard_DomainError.hxx>
27 #include <Standard_NoMoreObject.hxx>
28 #include <Standard_NoSuchObject.hxx>
30 #include <TopExp_Explorer.hxx>
32 #include <TopoDS_Edge.hxx>
33 #include <TopoDS_Face.hxx>
34 #include <TopoDS_Iterator.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopoDS_Wire.hxx>
37 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
39 #include <TopTools_ListOfShape.hxx>
40 #include <TopTools_MapIteratorOfMapOfShape.hxx>
41 #include <TopTools_MapOfShape.hxx>
43 //=======================================================================
44 // forward declarations of aux functions
45 //=======================================================================
46 static Standard_Boolean SelectDouble(TopTools_MapOfShape& Doubles,
47 TopTools_ListOfShape& L,
50 static Standard_Boolean SelectDegenerated(TopTools_ListOfShape& L,
53 static Standard_Real GetNextParamOnPC(const Handle(Geom2d_Curve)& aPC,
54 const gp_Pnt2d& aPRef,
55 const Standard_Real& fP,
56 const Standard_Real& lP,
57 const Standard_Real& tolU,
58 const Standard_Real& tolV,
59 const Standard_Boolean& reverse);
61 //=======================================================================
62 //function : BRepTools_WireExplorer
64 //=======================================================================
65 BRepTools_WireExplorer::BRepTools_WireExplorer()
69 //=======================================================================
70 //function : BRepTools_WireExplorer
72 //=======================================================================
73 BRepTools_WireExplorer::BRepTools_WireExplorer(const TopoDS_Wire& W)
75 TopoDS_Face F = TopoDS_Face();
79 //=======================================================================
80 //function : BRepTools_WireExplorer
82 //=======================================================================
83 BRepTools_WireExplorer::BRepTools_WireExplorer(const TopoDS_Wire& W,
89 //=======================================================================
92 //=======================================================================
93 void BRepTools_WireExplorer::Init(const TopoDS_Wire& W)
95 TopoDS_Face F = TopoDS_Face();
99 //=======================================================================
102 //=======================================================================
103 void BRepTools_WireExplorer::Init(const TopoDS_Wire& W,
104 const TopoDS_Face& F)
106 myEdge = TopoDS_Edge();
107 myVertex = TopoDS_Vertex();
115 Standard_Real dfVertToler = 0.;
116 myReverse = Standard_False;
118 if (!myFace.IsNull())
120 BRepTools::Update(myFace);
122 const Handle(Geom_Surface)& aSurf = BRep_Tool::Surface(myFace, aL);
123 GeomAdaptor_Surface aGAS(aSurf);
124 TopExp_Explorer anExp(W, TopAbs_VERTEX);
125 for(; anExp.More(); anExp.Next())
127 const TopoDS_Vertex& aV = TopoDS::Vertex(anExp.Current());
128 dfVertToler = Max(BRep_Tool::Tolerance(aV), dfVertToler);
130 myTolU = 2. * aGAS.UResolution(dfVertToler);
131 myTolV = 2. * aGAS.VResolution(dfVertToler);
133 // uresolution for cone with infinite vmin vmax is too small.
134 if(aGAS.GetType() == GeomAbs_Cone)
136 Standard_Real u1, u2, v1, v2;
137 BRepTools::UVBounds(myFace, u1, u2, v1, v2);
140 aGAS.D1(u1, v1, aP, aD1U, aD1V);
141 Standard_Real tol1, tol2, maxtol = .0005*(u2-u1);
142 Standard_Real a = aD1U.Magnitude();
144 if(a <= Precision::Confusion())
147 tol1 = Min(maxtol, dfVertToler/a);
149 aGAS.D1(u1, v2, aP, aD1U, aD1V);
150 a = aD1U.Magnitude();
151 if(a <= Precision::Confusion())
154 tol2 = Min(maxtol, dfVertToler/a);
156 myTolU = 2. * Max(tol1, tol2);
159 if( aGAS.GetType() == GeomAbs_BSplineSurface ||
160 aGAS.GetType() == GeomAbs_BezierSurface )
162 Standard_Real maxTol = Max(myTolU,myTolV);
167 myReverse = (myFace.Orientation() == TopAbs_REVERSED);
170 // map of vertices to know if the wire is open
171 TopTools_MapOfShape vmap;
172 // Modified by Sergey KHROMOV - Mon May 13 11:50:48 2002 Begin
173 // map of infinite edges
174 TopTools_MapOfShape anInfEmap;
175 // Modified by Sergey KHROMOV - Mon May 13 11:50:49 2002 End
179 TopTools_ListOfShape empty;
181 TopoDS_Iterator it(W);
184 const TopoDS_Edge& E = TopoDS::Edge(it.Value());
185 TopAbs_Orientation Eori = E.Orientation();
186 if (Eori == TopAbs_INTERNAL || Eori == TopAbs_EXTERNAL)
191 TopExp::Vertices(E,V1,V2,Standard_True);
195 if( !myMap.IsBound(V1) )
196 myMap.Bind(V1,empty);
199 // add or remove in the vertex map
200 V1.Orientation(TopAbs_FORWARD);
207 V2.Orientation(TopAbs_REVERSED);
212 // Modified by Sergey KHROMOV - Mon May 13 11:52:20 2002 Begin
213 if (V1.IsNull() || V2.IsNull())
215 Standard_Real aF = 0., aL = 0.;
216 BRep_Tool::Range(E, aF, aL);
218 if(Eori == TopAbs_FORWARD)
220 if (aF == -Precision::Infinite())
224 { // Eori == TopAbs_REVERSED
225 if (aL == Precision::Infinite())
229 // Modified by Sergey KHROMOV - Mon May 13 11:52:20 2002 End
233 //Construction of the set of double edges.
234 TopoDS_Iterator it2(W);
235 TopTools_MapOfShape emap;
237 if (!emap.Add(it2.Value()))
238 myDoubles.Add(it2.Value());
242 // if vmap is not empty the wire is open, let us find the first vertex
243 if (!vmap.IsEmpty()) {
244 TopTools_MapIteratorOfMapOfShape itt(vmap); // skl : I change "it" to "itt"
245 while (itt.Key().Orientation() != TopAbs_FORWARD) {
247 if (!itt.More()) break;
249 if (itt.More()) V1 = TopoDS::Vertex(itt.Key());
252 // Modified by Sergey KHROMOV - Mon May 13 12:05:30 2002 Begin
253 // The wire is infinite Try to find the first vertex. It may be NULL.
254 if (!anInfEmap.IsEmpty()) {
255 TopTools_MapIteratorOfMapOfShape itt(anInfEmap);
257 for (; itt.More(); itt.Next()) {
258 TopoDS_Edge anEdge = TopoDS::Edge(itt.Key());
259 TopAbs_Orientation anOri = anEdge.Orientation();
263 BRep_Tool::Range(anEdge, aF, aL);
264 if ((anOri == TopAbs_FORWARD && aF == -Precision::Infinite()) ||
265 (anOri == TopAbs_REVERSED && aL == Precision::Infinite())) {
267 myVertex = TopoDS_Vertex();
273 // Modified by Sergey KHROMOV - Mon May 13 12:05:31 2002 End
276 // use the first vertex in iterator
279 const TopoDS_Edge& E = TopoDS::Edge(it.Value());
280 TopAbs_Orientation Eori = E.Orientation();
281 if (Eori == TopAbs_INTERNAL || Eori == TopAbs_EXTERNAL) {
282 // JYL 10-03-97 : waiting for correct processing
283 // of INTERNAL/EXTERNAL edges
287 TopExp::Vertices(E,V1,V2,Standard_True);
292 if (V1.IsNull() ) return;
293 if (!myMap.IsBound(V1)) return;
295 TopTools_ListOfShape& l = myMap(V1);
296 myEdge = TopoDS::Edge(l.First());
298 myVertex = TopExp::FirstVertex (myEdge, Standard_True);
302 //=======================================================================
305 //=======================================================================
306 Standard_Boolean BRepTools_WireExplorer::More()const
308 return !myEdge.IsNull();
311 //=======================================================================
314 //=======================================================================
315 void BRepTools_WireExplorer::Next()
317 myVertex = TopExp::LastVertex (myEdge, Standard_True);
319 if (myVertex.IsNull()) {
320 myEdge = TopoDS_Edge();
323 if (!myMap.IsBound(myVertex)) {
324 myEdge = TopoDS_Edge();
328 TopTools_ListOfShape& l = myMap(myVertex);
331 myEdge = TopoDS_Edge();
333 else if (l.Extent() == 1) {
334 // Modified by Sergey KHROMOV - Fri Jun 21 10:28:01 2002 OCC325 Begin
337 TopoDS_Edge aNextEdge = TopoDS::Edge(l.First());
339 TopExp::Vertices(aNextEdge, aV1, aV2, Standard_True);
341 if (!aV1.IsSame(myVertex)) {
342 myEdge = TopoDS_Edge();
345 if (!myFace.IsNull() && aV1.IsSame(aV2)) {
346 Handle(Geom2d_Curve) aPrevPC;
347 Handle(Geom2d_Curve) aNextPC;
348 Standard_Real aPar11, aPar12;
349 Standard_Real aPar21, aPar22;
350 Standard_Real aPrevPar;
351 Standard_Real aNextFPar;
352 Standard_Real aNextLPar;
354 aPrevPC = BRep_Tool::CurveOnSurface(myEdge, myFace, aPar11, aPar12);
355 aNextPC = BRep_Tool::CurveOnSurface(aNextEdge, myFace, aPar21, aPar22);
357 if (aPrevPC.IsNull() || aNextPC.IsNull()) {
358 myEdge = TopoDS_Edge();
362 if (myEdge.Orientation() == TopAbs_FORWARD)
367 if (aNextEdge.Orientation() == TopAbs_FORWARD) {
375 gp_Pnt2d aPPrev = aPrevPC->Value(aPrevPar);
376 gp_Pnt2d aPNextF = aNextPC->Value(aNextFPar);
377 gp_Pnt2d aPNextL = aNextPC->Value(aNextLPar);
379 if (aPPrev.SquareDistance(aPNextF) > aPPrev.SquareDistance(aPNextL)) {
380 myEdge = TopoDS_Edge();
384 // Modified by Sergey KHROMOV - Fri Jun 21 11:08:16 2002 End
385 myEdge = TopoDS::Edge(l.First());
389 if (myFace.IsNull()) {
390 // Without Face - try to return edges
391 // as logically as possible
392 // At first degenerated edges.
393 TopoDS_Edge E = myEdge;
394 if (SelectDegenerated(l,E)) {
398 // At second double edges.
400 if (SelectDouble(myDoubles,l,E)) {
405 TopTools_ListIteratorOfListOfShape it(l);
406 Standard_Boolean notfound = Standard_True;
408 if (!it.Value().IsSame(myEdge)) {
409 myEdge = TopoDS::Edge(it.Value());
411 notfound = Standard_False;
418 myEdge = TopoDS_Edge();
425 // If we have more than one edge attached to the list
426 // probably wire that we explore contains a loop or loops.
427 Standard_Real dfFPar = 0., dfLPar = 0.;
428 Handle(Geom2d_Curve) aPCurve = BRep_Tool::CurveOnSurface (myEdge, myFace, dfFPar, dfLPar);
431 myEdge = TopoDS_Edge();
434 // Note: current < myVertex > which is last on < myEdge >
435 // equals in 2D to following 2D points:
436 // edge is FORWARD - point with MAX parameter on PCurve;
437 // edge is REVERSED - point with MIN parameter on PCurve.
439 // Get 2D point equals to < myVertex > in 2D for current edge.
441 if( myEdge.Orientation() == TopAbs_REVERSED )
442 aPCurve->D0(dfFPar, PRef);
444 aPCurve->D0(dfLPar, PRef);
446 // Get next 2D point from current edge's PCurve with parameter
447 // F + dP (REV) or L - dP (FOR)
448 Standard_Boolean isrevese = ( myEdge.Orientation() == TopAbs_REVERSED );
449 Standard_Real dfMPar = GetNextParamOnPC(aPCurve,PRef,dfFPar,dfLPar,myTolU,myTolV,isrevese);
452 aPCurve->D0(dfMPar, PRefm);
453 // Get vector from PRef to PRefm
454 gp_Vec2d anERefDir(PRef,PRefm);
455 // Search the list of edges looking for the edge having hearest
456 // 2D point of connected vertex to current one and smallest angle.
457 // First process all degenerated edges, then - all others.
459 TopTools_ListIteratorOfListOfShape it;
460 Standard_Integer k = 1, kMin = 0, iDone = 0;
461 Standard_Boolean isDegenerated = Standard_True;
462 Standard_Real dmin = RealLast();
463 Standard_Real dfMinAngle = 3.0*M_PI, dfCurAngle = 3.0*M_PI;
465 for(iDone = 0; iDone < 2; iDone++)
470 const TopoDS_Edge& E = TopoDS::Edge(it.Value());
471 if( E.IsSame(myEdge) )
478 TopoDS_Vertex aVert1, aVert2;
479 TopExp::Vertices (E, aVert1, aVert2, Standard_True);
480 if( aVert1.IsNull() || aVert2.IsNull() )
487 aPCurve = BRep_Tool::CurveOnSurface (E, myFace, dfFPar, dfLPar);
488 if( aPCurve.IsNull() )
496 if( aVert1.IsSame(aVert2) == isDegenerated )
498 if( E.Orientation() == TopAbs_REVERSED )
499 aPCurve->D0(dfLPar, aPEb);
501 aPCurve->D0(dfFPar, aPEb);
503 if( Abs(dfLPar-dfFPar) > Precision::PConfusion() )
505 isrevese = ( E.Orientation() == TopAbs_REVERSED );
506 isrevese = !isrevese;
507 Standard_Real aEPm = GetNextParamOnPC(aPCurve,aPEb,dfFPar,dfLPar,myTolU,myTolV,isrevese);
509 aPCurve->D0 (aEPm, aPEe);
510 if(aPEb.SquareDistance(aPEe) <= gp::Resolution())
512 //seems to be very short curve
514 aPCurve->D1(aEPm, aPEe, aD);
515 if( E.Orientation() == TopAbs_REVERSED )
516 aPEe.SetXY(aPEb.XY()-aD.XY());
518 aPEe.SetXY(aPEb.XY()+aD.XY());
520 if(aPEb.SquareDistance(aPEe) <= gp::Resolution())
527 gp_Vec2d anEDir(aPEb, aPEe);
528 dfCurAngle = Abs( anEDir.Angle(anERefDir) );
531 if( dfCurAngle <= dfMinAngle )
533 Standard_Real d = PRef.SquareDistance(aPEb);
534 if( d <= Precision::PConfusion() )
536 if( Abs(aPEb.X()-PRef.X()) < myTolU && Abs(aPEb.Y()-PRef.Y()) < myTolV )
540 dfMinAngle = dfCurAngle;
553 isDegenerated = Standard_False;
563 // probably unclosed in 2d space wire
564 myEdge = TopoDS_Edge();
568 // Selection the edge.
575 myEdge = TopoDS::Edge(it.Value());
582 }//else face != NULL && l > 1
586 //=======================================================================
589 //=======================================================================
590 const TopoDS_Edge& BRepTools_WireExplorer::Current()const
595 //=======================================================================
596 //function : Orientation
598 //=======================================================================
599 TopAbs_Orientation BRepTools_WireExplorer::Orientation() const
601 TopoDS_Iterator it(myEdge,Standard_False);
603 if (myVertex.IsSame(it.Value()))
604 return it.Value().Orientation();
607 throw Standard_NoSuchObject("BRepTools_WireExplorer::Orientation");
610 //=======================================================================
611 //function : CurrentVertex
613 //=======================================================================
614 const TopoDS_Vertex& BRepTools_WireExplorer::CurrentVertex() const
619 //=======================================================================
622 //=======================================================================
624 void BRepTools_WireExplorer::Clear()
628 myEdge = TopoDS_Edge();
629 myFace = TopoDS_Face();
630 myVertex = TopoDS_Vertex();
634 //=======================================================================
635 //function : SelectDouble
637 //=======================================================================
639 Standard_Boolean SelectDouble(TopTools_MapOfShape& Doubles,
640 TopTools_ListOfShape& L,
643 TopTools_ListIteratorOfListOfShape it(L);
645 for (; it.More(); it.Next()) {
646 const TopoDS_Shape& CE = it.Value();
647 if (Doubles.Contains(CE) && (!E.IsSame(CE))) {
648 E = TopoDS::Edge(CE);
656 //=======================================================================
657 //function : SelectDegenerated
659 //=======================================================================
661 Standard_Boolean SelectDegenerated(TopTools_ListOfShape& L,
664 TopTools_ListIteratorOfListOfShape it(L);
666 if (!it.Value().IsSame(E)) {
667 E = TopoDS::Edge(it.Value());
668 if (BRep_Tool::Degenerated(E)) {
678 //=======================================================================
679 //function : GetNextParamOnPC
681 //=======================================================================
682 Standard_Real GetNextParamOnPC(const Handle(Geom2d_Curve)& aPC,
683 const gp_Pnt2d& aPRef,
684 const Standard_Real& fP,
685 const Standard_Real& lP,
686 const Standard_Real& tolU,
687 const Standard_Real& tolV,
688 const Standard_Boolean& reverse)
690 Standard_Real result = ( reverse ) ? fP : lP;
691 Standard_Real dP = Abs( lP - fP ) / 1000.; // was / 16.;
694 Standard_Real startPar = fP;
695 Standard_Boolean nextPntOnEdge = Standard_False;
696 while( !nextPntOnEdge && startPar < lP )
700 aPC->D0(startPar, pnt);
701 if( Abs( aPRef.X() - pnt.X() ) < tolU && Abs( aPRef.Y() - pnt.Y() ) < tolV )
706 nextPntOnEdge = Standard_True;
719 Standard_Real startPar = lP;
720 Standard_Boolean nextPntOnEdge = Standard_False;
721 while( !nextPntOnEdge && startPar > fP )
725 aPC->D0(startPar, pnt);
726 if( Abs( aPRef.X() - pnt.X() ) < tolU && Abs( aPRef.Y() - pnt.Y() ) < tolV )
731 nextPntOnEdge = Standard_True;