1 // GEOM PARTITION : partition algorithm
3 // Copyright (C) 2003 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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : Partition_Inter2d.cxx
25 // Author : Benedicte MARTIN
30 #include "Partition_Inter2d.ixx"
32 #include "utilities.h"
34 #include <BRepAdaptor_Curve.hxx>
35 #include <BRepAlgo_AsDes.hxx>
36 #include <BRepLib_MakeVertex.hxx>
37 #include <BRep_Builder.hxx>
38 #include <BRep_Tool.hxx>
39 #include <Geom_Surface.hxx>
40 #include <Precision.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopOpeBRepDS_Transition.hxx>
44 #include <TopOpeBRep_EdgesIntersector.hxx>
45 #include <TopOpeBRep_Point2d.hxx>
46 #include <TopTools_ListIteratorOfListOfShape.hxx>
47 #include <TopTools_ListOfShape.hxx>
48 #include <TopTools_MapIteratorOfMapOfShape.hxx>
49 #include <TopTools_MapOfShape.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <TopoDS_Vertex.hxx>
56 static Standard_Boolean TestEdges = 0;
57 static Standard_Integer NbF2d = 0;
58 static Standard_Integer NbE2d = 0;
61 //=======================================================================
62 //function : getOtherShape
64 //=======================================================================
66 static TopoDS_Shape getOtherShape(const TopoDS_Shape& theS,
67 const TopTools_ListOfShape& theSList)
69 TopTools_ListIteratorOfListOfShape anIt( theSList );
70 for ( ; anIt.More(); anIt.Next() )
71 if (!theS.IsSame( anIt.Value() ))
74 return TopoDS_Shape();
77 //=======================================================================
79 //purpose : on theE, find a vertex close to theV, such that an edge
80 // passing through it is an itersection of theF1 and theF2.
81 // theE intersects theE2 at theV
82 //=======================================================================
84 static Standard_Boolean findVOnE(const TopoDS_Vertex & theV,
85 const TopoDS_Edge& theE,
86 const TopoDS_Edge& theE2,
87 const TopoDS_Shape& theF1,
88 const TopoDS_Shape& theF2,
89 const Handle(BRepAlgo_AsDes)& theAsDes,
90 TopoDS_Vertex & theFoundV)
92 Standard_Real MinDist2 = ::RealLast();
95 // check all vertices on theE
96 const TopTools_ListOfShape& aVList = theAsDes->Descendant( theE );
97 TopTools_ListIteratorOfListOfShape anIt( aVList );
99 P = BRep_Tool::Pnt( theV );
100 for ( ; anIt.More(); anIt.Next() )
103 TopoDS_Vertex & V = TopoDS::Vertex( anIt.Value() );
104 Standard_Real dist2 = P.SquareDistance( BRep_Tool::Pnt( V ));
105 if (dist2 < MinDist2)
110 // V is a candidate if among edges passing through V there is one
111 // which is an intersection of theF1 and theF2
112 TopTools_ListIteratorOfListOfShape anEIt( theAsDes->Ascendant( V ));
113 Standard_Boolean isOk = Standard_False;
114 for ( ; !isOk && anEIt.More(); anEIt.Next() )
116 const TopoDS_Shape & E2 = anEIt.Value();
117 if ( theE2.IsSame( E2 ))
119 const TopTools_ListOfShape & aFList = theAsDes->Ascendant( E2 );
120 if (aFList.IsEmpty())
122 if ( theF1.IsSame( aFList.First() ))
123 isOk = theF2.IsSame( aFList.Last() );
125 isOk = theF2.IsSame( aFList.First() ) && theF1.IsSame( aFList.Last() );
131 if (theFoundV.IsNull())
132 return Standard_False;
134 // check that MinDist2 is not too large
137 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( theE, L, f, l );
138 gp_Pnt P1 = aCurve->Value( f );
139 gp_Pnt P2 = aCurve->Value( 0.3 * f + 0.7 * l );
140 //gp_Pnt P2 = aCurve->Value( 0.5 * ( f + l ));
141 if (MinDist2 > P1.SquareDistance( P2 ))
142 return Standard_False;
145 cout << "findVOnE: found MinDist = " << sqrt (MinDist2) << endl;
148 return Standard_True;
151 //=======================================================================
153 //purpose : Put V in AsDes as intersection of E1 and E2.
154 // Check that vertex equal to V already exists on one
155 // of edges, in such a case, V is not added but
156 // existing vertex is updated to be on E1 and E2 and
157 // is returned insead of V.
158 //=======================================================================
160 TopoDS_Vertex Partition_Inter2d::AddVonE(const TopoDS_Vertex& theV,
161 const TopoDS_Edge& E1,
162 const TopoDS_Edge& E2,
163 const Handle(BRepAlgo_AsDes)& AsDes,
164 const TopoDS_Face& theF)
167 //-------------------------------------------------------------
168 // test if the points of intersection already exist. If not,
169 // add as descendants of the edges.
170 // nb: theses points are only vertices of intersection.
171 //-------------------------------------------------------------
172 const TopTools_ListOfShape& VOnE1 = AsDes->Descendant(E1);
173 const TopTools_ListOfShape& VOnE2 = AsDes->Descendant(E2);
176 TopTools_ListIteratorOfListOfShape it;
178 TopAbs_Orientation O1,O2;
180 Standard_Real Tol,Tol1,Tol2;
181 Standard_Boolean OnE1,OnE2;
183 TopoDS_Vertex V = theV;
185 U1 = BRep_Tool::Parameter(V,E1);
186 U2 = BRep_Tool::Parameter(V,E2);
187 O1 = V.Orientation();
189 P1 = BRep_Tool::Pnt(V);
190 Tol = BRep_Tool::Tolerance( V );
191 OnE1 = OnE2 = Standard_False;
193 //-----------------------------------------------------------------
194 // Search if the point of intersection is a vertex of E1.
195 //-----------------------------------------------------------------
196 for (it.Initialize(VOnE1); it.More(); it.Next()) {
197 const TopoDS_Vertex& CV = TopoDS::Vertex( it.Value() );
198 if (V.IsSame( CV )) {
200 OnE1 = Standard_True;
203 P2 = BRep_Tool::Pnt( CV );
204 Tol1 = 1.1*(Tol + BRep_Tool::Tolerance( CV ));
205 if (P1.SquareDistance(P2) <= Tol1*Tol1) {
208 OnE1 = Standard_True;
213 //-----------------------------------------------------------------
214 // Search if the vertex found is still on E2.
215 //-----------------------------------------------------------------
216 for (it.Initialize(VOnE2); it.More(); it.Next()) {
217 if (V.IsSame( it.Value() )) {
218 OnE2 = Standard_True;
225 for (it.Initialize(VOnE2); it.More(); it.Next()) {
226 //-----------------------------------------------------------------
227 // Search if the point of intersection is a vertex of E2.
228 //-----------------------------------------------------------------
229 const TopoDS_Vertex& CV = TopoDS::Vertex( it.Value() );
230 P2 = BRep_Tool::Pnt( CV );
231 Tol2 = 1.1*(Tol + BRep_Tool::Tolerance( CV ));
232 if (P1.SquareDistance(P2) <= Tol2*Tol2) {
235 OnE2 = Standard_True;
242 if (!OnE1 && !OnE2 && !theF.IsNull())
244 // analitically find vertices E1 and E2 must pass trough
246 TopoDS_Shape F1 = getOtherShape( theF, AsDes->Ascendant( E1 ));
247 TopoDS_Shape F2 = getOtherShape( theF, AsDes->Ascendant( E2 ));
248 if (!F1.IsNull() && !F2.IsNull())
250 OnE1 = findVOnE ( theV, E1, E2, F1, F2, AsDes, V1 );
251 OnE2 = findVOnE ( theV, E2, E1, F1, F2, AsDes, V2 );
258 if (!V1.IsSame(V2)) {
259 // replace V1 with V2 on all edges V1 is on
263 const TopTools_ListOfShape& EdgeWithV1 = AsDes->Ascendant(V1);
265 for (it.Initialize(EdgeWithV1); it.More(); it.Next()) {
266 EWE1 = TopoDS::Edge(it.Value());
268 VI.Orientation(TopAbs_INTERNAL);
269 UV1 = BRep_Tool::Parameter(VI,EWE1);
271 VI.Orientation(TopAbs_INTERNAL);
272 B.UpdateVertex( VI, UV1, EWE1, GetTolerance( VI, UV1, EWE1, AsDes));
274 AsDes->Replace(V1,V2);
279 // add existing vertices instead of new ones
282 V.Orientation(TopAbs_INTERNAL);
283 B.UpdateVertex (V, U1, E1, GetTolerance( V, U1, E1, AsDes));
290 V.Orientation(TopAbs_INTERNAL);
291 B.UpdateVertex (V, U2, E2, GetTolerance( V, U2, E2, AsDes ));
300 //=======================================================================
301 //function : FindEndVertex
302 //purpose : Returns a vertex from <VertList> having parameter on
303 // <E> closest to <f> or <l>. <isFirst> is True if
304 // found vertex is closer to <f>. <DU> returns parameter
306 //=======================================================================
308 TopoDS_Vertex Partition_Inter2d::FindEndVertex(const TopTools_ListOfShape& LV,
309 const Standard_Real f,
310 const Standard_Real l,
311 const TopoDS_Edge& E,
312 Standard_Boolean& isFirst,
313 Standard_Real& minDU)
316 Standard_Real U, endU, min;
319 TopTools_ListIteratorOfListOfShape it;
321 for (; it.More(); it.Next()) {
322 const TopoDS_Vertex& v = TopoDS::Vertex(it.Value());
323 U = BRep_Tool::Parameter(v, E);
324 min = Min( Abs(U-f), Abs(U-l) );
331 if (Abs(endU-f) < Abs(endU-l))
332 isFirst = Standard_True;
334 isFirst = Standard_False;
339 //=======================================================================
340 //function : treatClosed
341 //purpose : add second vertex to closed edge. Vertex is one of <LV1>
342 //=======================================================================
344 static void treatClosed (const TopoDS_Edge& E1,
345 const Standard_Real f,
346 const Standard_Real l,
347 TopTools_ListOfShape& LV1,
348 TopTools_ListOfShape& /*LV2*/)
350 Standard_Boolean isFirst=0;
351 Standard_Real minDU = 1.e10;
353 endV = Partition_Inter2d::FindEndVertex(LV1, f,l, E1, isFirst,minDU);
355 if (minDU > Precision::PConfusion())
356 return; // not end point
364 // update end parameter
366 endV.Orientation(TopAbs_INTERNAL);
367 B.UpdateVertex(endV,newU,E1,BRep_Tool::Tolerance(endV));
370 //=======================================================================
371 //function : EdgesPartition
373 //=======================================================================
375 static void EdgesPartition(const TopoDS_Face& F,
376 const TopoDS_Edge& E1,
377 const TopoDS_Edge& E2,
378 const Handle(BRepAlgo_AsDes)& AsDes,
379 const TopTools_MapOfShape& NewEdges,
380 const Standard_Boolean WithOri)
383 Standard_Real f[3],l[3];
384 Standard_Real MilTol2;
385 Standard_Real Tol = Max (BRep_Tool::Tolerance(E1),
386 BRep_Tool::Tolerance(E2));
387 MilTol2 = Tol * Tol * 10;
389 BRep_Tool::Range(E1, f[1], l[1]);
390 BRep_Tool::Range(E2, f[2], l[2]);
392 BRepAdaptor_Curve CE1(E1,F);
393 BRepAdaptor_Curve CE2(E2,F);
395 TopoDS_Edge EI[3]; EI[1] = E1; EI[2] = E2;
396 TopTools_ListOfShape LV1; // new vertices at intersections on E1
397 TopTools_ListOfShape LV2; // ... on E2
400 // if E1 and E2 are results of intersection of F and two connex faces then
401 // no need to intersect edges, they can contact by vertices only
402 // (encounted an exception in TopOpeBRep_EdgesIntersector in such a case)
403 Standard_Boolean intersect = Standard_True;
404 TopTools_IndexedMapOfShape ME;
405 TopExp::MapShapes(F, TopAbs_EDGE, ME);
406 if (!ME.Contains(E1) && ! ME.Contains(E2)) { // if E1 and E2 are new on F
408 const TopTools_ListOfShape& LF1 = AsDes->Ascendant( E1 );
409 F1 = F.IsSame( LF1.First() ) ? LF1.Last() : LF1.First();
410 const TopTools_ListOfShape& LF2 = AsDes->Ascendant( E2 );
411 F2 = F.IsSame( LF2.First() ) ? LF2.Last() : LF2.First();
412 if (!F.IsSame(F2) && !F.IsSame(F1) ) {
413 TopExp_Explorer exp(F2, TopAbs_EDGE);
414 TopExp::MapShapes(F1, TopAbs_EDGE, ME);
415 for (; exp.More(); exp.Next()) {
416 if (ME.Contains( exp.Current())) {
417 intersect = Standard_False;
425 //------------------------------------------------------
426 // compute the points of Intersection in 2D
427 //-----------------------------------------------------
428 // i.e. fill LV1 and LV2
429 TopOpeBRep_EdgesIntersector EInter;
430 EInter.SetFaces(F,F);
431 Standard_Real TolDub = 1.e-7;
432 EInter.ForceTolerances(TolDub,TolDub);
433 Standard_Boolean reducesegments = Standard_False;
434 EInter.Perform (E1,E2,reducesegments);
436 Standard_Boolean rejectreducedsegmentpoints = Standard_False;
437 EInter.InitPoint(rejectreducedsegmentpoints);
438 for ( ; EInter.MorePoint(); EInter.NextPoint() )
440 const TopOpeBRep_Point2d& P2D = EInter.Point();
441 const gp_Pnt& P = P2D.Value();
442 TopoDS_Vertex V = BRepLib_MakeVertex(P);
444 //-------------------------
445 // control the point found.
446 //-------------------------
447 gp_Pnt P1 = CE1.Value(P2D.Parameter(1));
448 gp_Pnt P2 = CE2.Value(P2D.Parameter(2));
449 Standard_Real sqd1 = P1.SquareDistance(P);
450 Standard_Real sqd2 = P2.SquareDistance(P);
451 if (sqd1 > MilTol2 || sqd2 > MilTol2 )
454 // add a new vertex to the both edges
455 Standard_Real toler = Max( Tol, sqrt( Max( sqd1, sqd2 )));
457 for (i = 1; i <= 2; i++) {
458 Standard_Real U = P2D.Parameter(i);
459 V.Orientation(TopAbs_INTERNAL);
460 B.UpdateVertex( V,U,EI[i], toler);
461 TopAbs_Orientation OO = TopAbs_REVERSED;
464 OO = P2D.Vertex(i).Orientation();
465 else if (P2D.Transition(i).Before() == TopAbs_OUT) {
469 if (i == 1) LV1.Append(V);
476 //----------------------------------
477 // Test the extremities of the edges.
478 //----------------------------------
479 // add to LV* vertices for vertex-vertex closeness
481 Standard_Real TolConf2, TolConf;
482 TopoDS_Vertex V1[2],V2[2];
483 TopExp::Vertices(E1,V1[0],V1[1]);
484 TopExp::Vertices(E2,V2[0],V2[1]);
486 Standard_Integer i,j,k;
487 for (j = 0; j < 2; j++) {
488 if (V1[j].IsNull()) continue;
489 for ( k = 0; k < 2; k++) {
490 if (V2[k].IsNull()) continue;
491 gp_Pnt P1 = BRep_Tool::Pnt(V1[j]);
492 gp_Pnt P2 = BRep_Tool::Pnt(V2[k]);
493 TolConf = BRep_Tool::Tolerance(V1[j]) + BRep_Tool::Tolerance(V2[k]);
494 TolConf = Max (Tol, TolConf);
495 TolConf2 = TolConf * TolConf;
498 Standard_Real SqDist = P1.SquareDistance(P2);
500 if (SqDist <= TolConf2) {
501 TopoDS_Vertex V = BRepLib_MakeVertex(P1);
502 V.Orientation(TopAbs_INTERNAL);
503 U1 = (j == 0) ? f[1] : l[1];
504 U2 = (k == 0) ? f[2] : l[2];
505 B.UpdateVertex(V,U1,E1,TolConf);
506 B.UpdateVertex(V,U2,E2,TolConf);
507 LV1.Prepend(V.Oriented(V1[j].Orientation()));
508 LV2.Prepend(V.Oriented(V2[k].Orientation()));
513 Standard_Boolean AffichPurge = Standard_False;
515 if ( LV1.IsEmpty()) return;
517 //----------------------------------
518 // Purge of all the vertices.
519 //----------------------------------
520 // remove one of close vertices
521 TopTools_ListIteratorOfListOfShape it1LV1,it1LV2,it2LV1;
523 Standard_Boolean Purge = Standard_True;
527 Purge = Standard_False;
528 for (it1LV1.Initialize(LV1),it1LV2.Initialize(LV2);
530 it1LV1.Next(),it1LV2.Next()) {
532 it2LV1.Initialize(LV1);
534 const TopoDS_Vertex& VE1 = TopoDS::Vertex(it1LV1.Value());
535 const TopoDS_Vertex& VE2 = TopoDS::Vertex(it2LV1.Value());
536 Standard_Real Tol1 = BRep_Tool::Tolerance( VE1 );
537 Standard_Real Tol2 = BRep_Tool::Tolerance( VE2 );
538 P1 = BRep_Tool::Pnt( VE1 );
539 P2 = BRep_Tool::Pnt( VE2 );
540 if (P1.IsEqual(P2, Tol1 + Tol2)) {
543 Purge = Standard_True;
554 // care of new closed edges, they always intersect with seam at end
555 if (V1[0].IsSame( V1[1] ) && NewEdges.Contains(E1) )
556 treatClosed (E1, f[1], l[1], LV1, LV2);
557 if (V2[0].IsSame( V2[1] ) && NewEdges.Contains(E2) )
558 treatClosed (E2, f[2], l[2], LV2, LV1);
564 for ( it1LV1.Initialize( LV1 ); it1LV1.More(); it1LV1.Next())
565 Partition_Inter2d::AddVonE (TopoDS::Vertex( it1LV1.Value()),
569 //=======================================================================
570 //function : CompletPart2d
571 //purpose : Computes the intersections between the edges stored
572 // is AsDes as descendants of <F> . Intersections is computed
573 // between two edges if one of them is bound in NewEdges.
574 //=======================================================================
576 void Partition_Inter2d::CompletPart2d (const Handle(BRepAlgo_AsDes)& AsDes,
577 const TopoDS_Face& F,
578 const TopTools_MapOfShape& NewEdges)
586 //Do not intersect the edges of a face
587 TopTools_IndexedMapOfShape EdgesOfFace;
588 TopExp::MapShapes( F, TopAbs_EDGE , EdgesOfFace);
590 //-------------------------------------------------------------------
591 // compute the intersection2D on the faces touched by the intersection3D
592 //-------------------------------------------------------------------
593 TopTools_ListIteratorOfListOfShape it1LE ;
594 TopTools_ListIteratorOfListOfShape it2LE ;
596 //-----------------------------------------------
597 // Intersection edge-edge.
598 //-----------------------------------------------
599 const TopTools_ListOfShape& LE = AsDes->Descendant(F);
601 Standard_Integer j, i = 1;
604 FF.Orientation(TopAbs_FORWARD);
606 for ( it1LE.Initialize(LE) ; it1LE.More(); it1LE.Next()) {
607 const TopoDS_Edge& E1 = TopoDS::Edge(it1LE.Value());
609 it2LE.Initialize(LE);
611 while (j < i && it2LE.More()) {
612 const TopoDS_Edge& E2 = TopoDS::Edge(it2LE.Value());
613 //----------------------------------------------------------
614 // Intersections of the new edges obtained by intersection
615 // between them and with the restrictions edges
616 //----------------------------------------------------------
617 if ( (!EdgesOfFace.Contains(E1) || !EdgesOfFace.Contains(E2)) &&
618 (NewEdges.Contains(E1) || NewEdges.Contains(E2)) ) {
619 EdgesPartition(FF,E1,E2,AsDes,NewEdges,Standard_True);
628 //=======================================================================
629 //function : GetTolerance
630 //purpose : Returns tolerance theV must have atfer its
631 // addition to theE with theU parameter. theAsDes is
632 // used to find pcurves of theE
633 //=======================================================================
635 Standard_Real Partition_Inter2d::GetTolerance
636 (const TopoDS_Vertex & theV,
637 const Standard_Real theU,
638 const TopoDS_Edge & theE,
639 const Handle(BRepAlgo_AsDes)& theAsDes)
641 Standard_Real aTol = BRep_Tool::Tolerance( theV );
642 gp_Pnt aPnt = BRep_Tool::Pnt( theV );
644 // check point on 3D curve
646 Handle(Geom_Curve) C = BRep_Tool::Curve( theE, f, l );
648 aTol = Max ( aTol, aPnt.Distance( C->Value( theU )));
650 // check points on pcurves
651 const TopTools_ListOfShape& aFList = theAsDes->Ascendant( theE );
652 TopTools_ListIteratorOfListOfShape aFIt( aFList );
653 for ( ; aFIt.More(); aFIt.Next() )
655 const TopoDS_Face& F = TopoDS::Face( aFIt.Value() );
656 Handle(Geom2d_Curve) pcurve = BRep_Tool::CurveOnSurface( theE, F, f, l );
657 if (!pcurve.IsNull())
659 gp_Pnt2d aPnt2d = pcurve->Value( theU );
661 Handle(Geom_Surface) S = BRep_Tool::Surface( F, L );
662 gp_Pnt aPntOnS = S->Value( aPnt2d.X(), aPnt2d.Y() );
664 aPntOnS.Transform( L.Transformation() );
665 aTol = Max ( aTol, aPnt.Distance( aPntOnS ));