Salome HOME
NRI : Merge BRANCH_V1_2c
[modules/geom.git] / src / PARTITION / Partition_Inter2d.cxx
1 //  GEOM PARTITION : partition algorithm
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R& D, LEG, PRINCIPIA R& D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : Partition_Inter2d.cxx
25 //  Author : Benedicte MARTIN
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "Partition_Inter2d.ixx"
31
32 #include "utilities.h"
33
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>
41 #include <TopExp.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>
50 #include <TopoDS.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <TopoDS_Vertex.hxx>
53 #include <gp_Pnt.hxx>
54
55 #ifdef DEB
56 static Standard_Boolean TestEdges = 0;
57 static Standard_Integer NbF2d = 0;
58 static Standard_Integer NbE2d = 0;
59 #endif
60
61 //=======================================================================
62 //function : getOtherShape
63 //purpose  :
64 //=======================================================================
65
66 static TopoDS_Shape getOtherShape(const TopoDS_Shape&         theS,
67                                   const TopTools_ListOfShape& theSList)
68 {
69   TopTools_ListIteratorOfListOfShape anIt( theSList );
70   for ( ; anIt.More(); anIt.Next() )
71     if (!theS.IsSame( anIt.Value() ))
72       return anIt.Value();
73
74   return TopoDS_Shape();
75 }
76
77 //=======================================================================
78 //function : findVOnE
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 //=======================================================================
83
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)
91 {
92   Standard_Real MinDist2 = ::RealLast();
93   gp_Pnt P;
94
95   // check all vertices on theE
96   const TopTools_ListOfShape& aVList = theAsDes->Descendant( theE );
97   TopTools_ListIteratorOfListOfShape anIt( aVList );
98   if (anIt.More())
99     P = BRep_Tool::Pnt( theV );
100   for ( ; anIt.More(); anIt.Next() )
101   {
102     // check by distance
103     TopoDS_Vertex & V = TopoDS::Vertex( anIt.Value() );
104     Standard_Real dist2 = P.SquareDistance( BRep_Tool::Pnt( V ));
105     if (dist2 < MinDist2)
106       MinDist2 = dist2;
107     else
108       continue;
109
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() )
115     {
116       const TopoDS_Shape & E2 = anEIt.Value();
117       if ( theE2.IsSame( E2 ))
118         continue;
119       const TopTools_ListOfShape & aFList = theAsDes->Ascendant( E2 );
120       if (aFList.IsEmpty())
121         continue;
122       if ( theF1.IsSame( aFList.First() ))
123         isOk = theF2.IsSame( aFList.Last() );
124       else
125         isOk = theF2.IsSame( aFList.First() ) && theF1.IsSame( aFList.Last() );
126     }
127     if (isOk)
128       theFoundV = V;
129   }
130
131   if (theFoundV.IsNull())
132     return Standard_False;
133
134   // check that MinDist2 is not too large
135   Standard_Real f, l;
136   TopLoc_Location L;
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;
143
144 #ifdef DEB
145   cout << "findVOnE: found MinDist = " << sqrt (MinDist2) << endl;
146 #endif
147
148   return Standard_True;
149 }
150
151 //=======================================================================
152 //function : AddVonE
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 //=======================================================================
159
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)
165
166 {
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);
174   gp_Pnt                      P1,P2;
175   TopoDS_Vertex               V1,V2;
176   TopTools_ListIteratorOfListOfShape it;
177   BRep_Builder                       B;
178   TopAbs_Orientation                 O1,O2;
179   Standard_Real                      U1,U2;
180   Standard_Real                      Tol,Tol1,Tol2;
181   Standard_Boolean                   OnE1,OnE2;
182
183   TopoDS_Vertex V    = theV;
184
185   U1 = BRep_Tool::Parameter(V,E1);
186   U2 = BRep_Tool::Parameter(V,E2);
187   O1 = V.Orientation();
188   O2 = O1;
189   P1  = BRep_Tool::Pnt(V);
190   Tol = BRep_Tool::Tolerance( V );
191   OnE1 = OnE2 = Standard_False;
192
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 )) {
199       V1   = V;
200       OnE1 = Standard_True;
201       break;
202     }
203     P2 = BRep_Tool::Pnt( CV );
204     Tol1 = 1.1*(Tol + BRep_Tool::Tolerance( CV ));
205     if (P1.SquareDistance(P2) <= Tol1*Tol1) {
206       V    = CV;
207       V1   = V;
208       OnE1 = Standard_True;
209       break;
210     }
211   }
212   if (OnE1) {
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;
219         V2   = V;
220         break;
221       }
222     }
223   }
224   if (!OnE2) {
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) {
233         V  = CV;
234         V2 = V;
235         OnE2 = Standard_True;
236         break;
237       }
238     }
239   }
240
241
242   if (!OnE1 && !OnE2 && !theF.IsNull())
243   {
244     // analitically find vertices E1 and E2 must pass trough
245
246     TopoDS_Shape F1 = getOtherShape( theF, AsDes->Ascendant( E1 ));
247     TopoDS_Shape F2 = getOtherShape( theF, AsDes->Ascendant( E2 ));
248     if (!F1.IsNull() && !F2.IsNull())
249     {
250       OnE1 = findVOnE ( theV, E1, E2, F1, F2, AsDes, V1 );
251       OnE2 = findVOnE ( theV, E2, E1, F1, F2, AsDes, V2 );
252       if (OnE2) V = V2;
253       if (OnE1) V = V1;
254     }
255   }
256
257   if (OnE1 && OnE2) {
258     if (!V1.IsSame(V2)) {
259       // replace V1 with V2 on all edges V1 is on
260       Standard_Real UV1;
261       TopoDS_Edge   EWE1;
262       TopoDS_Vertex VI;
263       const TopTools_ListOfShape& EdgeWithV1 = AsDes->Ascendant(V1);
264
265       for (it.Initialize(EdgeWithV1); it.More(); it.Next()) {
266         EWE1  = TopoDS::Edge(it.Value());
267         VI = V1;
268         VI.Orientation(TopAbs_INTERNAL);
269         UV1 = BRep_Tool::Parameter(VI,EWE1);
270         VI = V2;
271         VI.Orientation(TopAbs_INTERNAL);
272         B.UpdateVertex( VI, UV1, EWE1, GetTolerance( VI, UV1, EWE1, AsDes));
273       }
274       AsDes->Replace(V1,V2);
275       V = V2;
276     }
277   }
278
279   // add existing vertices instead of new ones
280   if (!OnE1) {
281     if (OnE2) {
282       V.Orientation(TopAbs_INTERNAL);
283       B.UpdateVertex (V, U1, E1, GetTolerance( V, U1, E1, AsDes));
284     }
285     V.Orientation(O1);
286     AsDes->Add(E1,V);
287   }
288   if (!OnE2) {
289     if (OnE1) {
290       V.Orientation(TopAbs_INTERNAL);
291       B.UpdateVertex (V, U2, E2, GetTolerance( V, U2, E2, AsDes ));
292     }
293     V.Orientation(O2);
294     AsDes->Add(E2,V);
295   }
296
297   return V;
298 }
299
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
305 //           difference.
306 //=======================================================================
307
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)
314 {
315   TopoDS_Vertex endV;
316   Standard_Real U, endU, min;
317   minDU = 1.e10;
318
319   TopTools_ListIteratorOfListOfShape it;
320   it.Initialize(LV);
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) );
325     if (min < minDU) {
326       endV = v;
327       endU = U;
328       minDU = min;
329     }
330   }
331   if (Abs(endU-f) < Abs(endU-l))
332     isFirst = Standard_True;
333   else
334     isFirst = Standard_False;
335
336   return endV;
337 }
338
339 //=======================================================================
340 //function : treatClosed
341 //purpose  : add second vertex to closed edge. Vertex is one of <LV1>
342 //=======================================================================
343
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*/)
349 {
350   Standard_Boolean isFirst=0;
351   Standard_Real    minDU = 1.e10;
352   TopoDS_Vertex endV;
353   endV = Partition_Inter2d::FindEndVertex(LV1, f,l, E1, isFirst,minDU);
354
355   if (minDU > Precision::PConfusion())
356     return; // not end point
357
358   Standard_Real newU;
359   if (isFirst)
360     newU = f + (l - f);
361   else
362     newU = l - (l - f);
363
364   // update end parameter
365   BRep_Builder B;
366   endV.Orientation(TopAbs_INTERNAL);
367   B.UpdateVertex(endV,newU,E1,BRep_Tool::Tolerance(endV));
368 }
369
370 //=======================================================================
371 //function : EdgesPartition
372 //purpose  :
373 //=======================================================================
374
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)
381 {
382
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;
388
389   BRep_Tool::Range(E1, f[1], l[1]);
390   BRep_Tool::Range(E2, f[2], l[2]);
391
392   BRepAdaptor_Curve CE1(E1,F);
393   BRepAdaptor_Curve CE2(E2,F);
394
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
398   BRep_Builder                B;
399
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
407     TopoDS_Shape F1, F2;
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;
418           break;
419         }
420       }
421     }
422   }
423
424   if (intersect) {
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);
435
436     Standard_Boolean rejectreducedsegmentpoints = Standard_False;
437     EInter.InitPoint(rejectreducedsegmentpoints);
438     for ( ; EInter.MorePoint(); EInter.NextPoint() )
439     {
440       const TopOpeBRep_Point2d& P2D = EInter.Point();
441       const gp_Pnt&    P    = P2D.Value();
442       TopoDS_Vertex    V    = BRepLib_MakeVertex(P);
443
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  )
452         continue;
453
454       // add a new vertex to the both edges
455       Standard_Real toler = Max( Tol, sqrt( Max( sqd1, sqd2 )));
456       Standard_Integer i;
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;
462         if (WithOri) {
463           if (P2D.IsVertex(i))
464             OO = P2D.Vertex(i).Orientation();
465           else if (P2D.Transition(i).Before() == TopAbs_OUT) {
466             OO = TopAbs_FORWARD;
467           }
468           V.Orientation(OO);
469           if (i == 1) LV1.Append(V);
470           else        LV2.Append(V);
471         }
472       }
473     }
474   } // if (intersect)
475
476   //----------------------------------
477   // Test the extremities of the edges.
478   //----------------------------------
479   // add to LV* vertices for vertex-vertex closeness
480   Standard_Real U1,U2;
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]);
485
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;
496       if (!intersect)
497         TolConf2 *= 100;
498       Standard_Real SqDist = P1.SquareDistance(P2);
499
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()));
509       }
510     }
511   }
512
513   Standard_Boolean AffichPurge = Standard_False;
514
515   if ( LV1.IsEmpty()) return;
516
517   //----------------------------------
518   // Purge of all the vertices.
519   //----------------------------------
520   // remove one of close vertices
521   TopTools_ListIteratorOfListOfShape it1LV1,it1LV2,it2LV1;
522   gp_Pnt P1,P2;
523   Standard_Boolean Purge = Standard_True;
524
525   while (Purge) {
526     i = 1;
527     Purge = Standard_False;
528     for (it1LV1.Initialize(LV1),it1LV2.Initialize(LV2);
529          it1LV1.More();
530          it1LV1.Next(),it1LV2.Next()) {
531       j = 1;
532       it2LV1.Initialize(LV1);
533       while (j < i) {
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)) {
541           LV1.Remove(it1LV1);
542           LV2.Remove(it1LV2);
543           Purge = Standard_True;
544           break;
545         }
546         j++;
547         it2LV1.Next();
548       }
549       if (Purge) break;
550       i++;
551     }
552   }
553
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);
559
560   //----------------
561   // Stocking vertex
562   //----------------
563
564   for ( it1LV1.Initialize( LV1 ); it1LV1.More(); it1LV1.Next())
565     Partition_Inter2d::AddVonE (TopoDS::Vertex( it1LV1.Value()),
566                                 E1, E2, AsDes, F);
567 }
568
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 //=======================================================================
575
576 void Partition_Inter2d::CompletPart2d (const Handle(BRepAlgo_AsDes)&   AsDes,
577                                        const TopoDS_Face&              F,
578                                        const TopTools_MapOfShape&      NewEdges)
579 {
580
581 #ifdef DEB
582   NbF2d++;
583   NbE2d = 0;
584 #endif
585
586   //Do not intersect the edges of a face
587   TopTools_IndexedMapOfShape EdgesOfFace;
588   TopExp::MapShapes( F, TopAbs_EDGE , EdgesOfFace);
589
590   //-------------------------------------------------------------------
591   // compute the intersection2D on the faces touched by the intersection3D
592   //-------------------------------------------------------------------
593   TopTools_ListIteratorOfListOfShape it1LE ;
594   TopTools_ListIteratorOfListOfShape it2LE ;
595
596   //-----------------------------------------------
597   // Intersection edge-edge.
598   //-----------------------------------------------
599   const TopTools_ListOfShape&        LE = AsDes->Descendant(F);
600   TopoDS_Vertex                      V1,V2;
601   Standard_Integer                   j, i = 1;
602
603   TopoDS_Face FF = F;
604   FF.Orientation(TopAbs_FORWARD);
605
606   for ( it1LE.Initialize(LE) ; it1LE.More(); it1LE.Next()) {
607     const TopoDS_Edge& E1 = TopoDS::Edge(it1LE.Value());
608     j = 1;
609     it2LE.Initialize(LE);
610
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);
620       }
621       it2LE.Next();
622       j++;
623     }
624     i++;
625   }
626 }
627
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 //=======================================================================
634
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)
640 {
641   Standard_Real aTol = BRep_Tool::Tolerance( theV );
642   gp_Pnt aPnt = BRep_Tool::Pnt( theV );
643
644   // check point on 3D curve
645   Standard_Real f,l;
646   Handle(Geom_Curve) C = BRep_Tool::Curve( theE, f, l );
647   if (!C.IsNull())
648     aTol = Max ( aTol, aPnt.Distance( C->Value( theU )));
649
650   // check points on pcurves
651   const TopTools_ListOfShape& aFList = theAsDes->Ascendant( theE );
652   TopTools_ListIteratorOfListOfShape aFIt( aFList );
653   for (  ; aFIt.More(); aFIt.Next() )
654   {
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())
658     {
659       gp_Pnt2d aPnt2d = pcurve->Value( theU );
660       TopLoc_Location L;
661       Handle(Geom_Surface) S = BRep_Tool::Surface( F, L );
662       gp_Pnt aPntOnS = S->Value( aPnt2d.X(), aPnt2d.Y() );
663       if (!L.IsIdentity())
664         aPntOnS.Transform( L.Transformation() );
665       aTol = Max ( aTol, aPnt.Distance( aPntOnS ));
666     }
667   }
668
669   return aTol;
670 }