0024624: Lost word in license statement in source files
[occt.git] / src / BRepFill / BRepFill_OffsetWire.cxx
1 // Created on: 1995-04-20
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 //  Modified by skv - Fri Jul  8 11:21:38 2005 OCC9145
18
19 #include <stdio.h>
20
21 #include <BRepFill_OffsetWire.ixx>
22
23 #include <BRepAdaptor_Curve.hxx>
24 #include <BRepAdaptor_Surface.hxx>
25
26 #include <BRepFill_DataMapOfNodeShape.hxx>
27 #include <BRepFill_DataMapOfShapeSequenceOfPnt.hxx>
28 #include <BRepFill_DataMapOfShapeSequenceOfReal.hxx> 
29 #include <BRepFill_DataMapOfOrientedShapeListOfShape.hxx> 
30 #include <BRepFill_TrimEdgeTool.hxx>
31 #include <BRepLib.hxx>
32 #include <BRepLib_MakeVertex.hxx>
33 #include <BRepLib_MakeFace.hxx>
34 #include <BRepLib_MakeWire.hxx>
35 #include <BRepLib_MakeEdge.hxx>
36 #include <BRepTools.hxx>
37 #include <BRep_Builder.hxx>
38 #include <BRep_Tool.hxx>
39 #include <BRep_TEdge.hxx>
40 #include <BRep_CurveRepresentation.hxx>
41 #include <BRep_GCurve.hxx>
42 #include <BRepTools_WireExplorer.hxx>
43 #include <BRepMAT2d_Explorer.hxx>
44 #include <Geom2dAdaptor_Curve.hxx>
45 #include <Geom2dAdaptor_HCurve.hxx>
46 #include <Adaptor3d_OffsetCurve.hxx>
47 #include <Adaptor3d_Curve.hxx>
48 #include <Geom_Surface.hxx>
49 #include <Geom_Plane.hxx>
50 #include <Geom2d_Curve.hxx>
51 #include <Geom2d_Circle.hxx>
52 #include <Geom2d_Line.hxx>
53 #include <Geom2d_TrimmedCurve.hxx>
54 #include <Geom2d_OffsetCurve.hxx>
55 #include <GeomAPI.hxx>
56 #include <Geom_TrimmedCurve.hxx>
57 #include <Geom_Circle.hxx>
58 #include <Geom_OffsetCurve.hxx>
59 #include <MAT_Arc.hxx>
60 #include <MAT_Node.hxx>
61 #include <MAT_Graph.hxx>
62 #include <MAT2d_CutCurve.hxx>
63 #include <Precision.hxx>
64 #include <Standard_NotImplemented.hxx>
65 #include <TColgp_SequenceOfPnt.hxx>
66 #include <TColStd_SequenceOfReal.hxx> 
67 #include <TopAbs.hxx> 
68 #include <TopExp.hxx>
69 #include <TopExp_Explorer.hxx>
70 #include <TopoDS.hxx>
71 #include <TopoDS_Wire.hxx>
72 #include <TopoDS_Compound.hxx>
73 #include <TopoDS_Iterator.hxx>
74 #include <TopTools_MapOfShape.hxx>
75 #include <TopTools_MapIteratorOfMapOfShape.hxx>
76 #include <TopTools_ListIteratorOfListOfShape.hxx>
77 #include <TopTools_DataMapOfShapeListOfShape.hxx>
78 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
79 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
80 #include <TopTools_SequenceOfShape.hxx>
81 #include <TopTools_ListOfShape.hxx>    
82 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>    
83 #include <TopTools_DataMapOfShapeSequenceOfShape.hxx>
84
85 #include <gp.hxx>
86 #include <gp_Vec.hxx>
87 #include <gp_Ax2.hxx>
88 #include <gp_Pln.hxx>
89 #include <gp_Dir2d.hxx>
90
91 #include <BRep_TVertex.hxx>
92 #include <TopTools_IndexedMapOfShape.hxx>
93 #include <Geom2d_BSplineCurve.hxx>
94 #include <TColgp_Array1OfPnt2d.hxx>
95 #include <TColStd_Array1OfReal.hxx>
96 #include <TColStd_Array1OfInteger.hxx>
97 #include <BRepTools_Substitution.hxx>
98 #include <BRepLib_MakeVertex.hxx>
99 #include <Geom2dLProp_CLProps2d.hxx>
100 #include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
101 #include <Standard_ErrorHandler.hxx>
102
103 #ifdef DRAW
104 #include <Draw.hxx>
105 #include <DrawTrSurf.hxx>
106 #include <DrawTrSurf_Curve2d.hxx>
107 #include <DBRep.hxx>
108 #endif
109
110 #ifdef DEB
111 static Standard_Boolean AffichGeom  = Standard_False;
112 static Standard_Boolean Affich2d    = Standard_False;
113 static Standard_Boolean AffichEdge  = Standard_False;
114 static Standard_Integer NbTRIMEDGES = 0;
115 static Standard_Integer NbOFFSET    = 0;
116 static Standard_Integer NbEDGES     = 0;
117 static Standard_Integer NbBISSEC    = 0;
118 #ifndef WNT
119 static char tname[100];
120 static Standard_CString name = tname ;
121 #endif
122 #endif
123
124 //  Modified by Sergey KHROMOV - Thu Nov 16 17:24:39 2000 Begin
125
126 static void QuasiFleche(const Adaptor3d_Curve& C,
127                         const Standard_Real Deflection2, 
128                         const Standard_Real Udeb,
129                         const gp_Pnt& Pdeb,
130                         const gp_Vec& Vdeb,
131                         const Standard_Real Ufin,
132                         const gp_Pnt& Pfin,
133                         const gp_Vec& Vfin,
134                         const Standard_Integer Nbmin,
135                         const Standard_Real Eps,
136                         TColStd_SequenceOfReal& Parameters,
137                         TColgp_SequenceOfPnt& Points);
138
139 static Standard_Boolean PerformCurve (TColStd_SequenceOfReal& Parameters,
140                                       TColgp_SequenceOfPnt&   Points,
141                                       const Adaptor3d_Curve& C, 
142                                       const Standard_Real Deflection,
143                                       const Standard_Real U1,
144                                       const Standard_Real U2,
145                                       const Standard_Real EPSILON,
146                                       const Standard_Integer Nbmin);
147
148 static void CheckBadEdges(const TopoDS_Face& Spine, const Standard_Real Offset,
149                           const BRepMAT2d_BisectingLocus& Locus, 
150                           const BRepMAT2d_LinkTopoBilo&   Link,
151                           TopTools_ListOfShape& BadEdges);
152
153 static Standard_Integer CutEdge (const TopoDS_Edge& E, 
154                                  const TopoDS_Face& F,
155                                        Standard_Integer ForceCut,
156                                        TopTools_ListOfShape& Cuts);
157
158
159 static void CutCurve (const Handle(Geom2d_TrimmedCurve)& C,
160                       const Standard_Integer nbParts,
161                             TColGeom2d_SequenceOfCurve& theCurves);
162 //  Modified by Sergey KHROMOV - Thu Nov 16 17:24:47 2000 End
163
164
165 static void EdgeVertices (const TopoDS_Edge&   E,
166                                 TopoDS_Vertex& V1, 
167                                 TopoDS_Vertex& V2)
168 {
169   if (E.Orientation() == TopAbs_REVERSED) {
170     TopExp::Vertices(E,V2,V1);
171   }
172   else {
173     TopExp::Vertices(E,V1,V2);
174   }
175 }
176                                       
177 static void UpdateDetromp (TopTools_ListOfShape&           Detromp1, 
178                            TopTools_ListOfShape&           Detromp2, 
179                            const TopTools_SequenceOfShape& Vertices, 
180                            const TColgp_SequenceOfPnt&     Params, 
181                            const Bisector_Bisec&           Bisec,
182                            const Standard_Boolean          SOnE,
183                            const Standard_Boolean          EOnE,
184                            const BRepFill_TrimEdgeTool&    Trim);
185
186 static Standard_Boolean VertexFromNode
187 (const Handle(MAT_Node)&      aNode, 
188  const Standard_Real          Offset,
189  gp_Pnt2d&                    PN,
190  BRepFill_DataMapOfNodeShape& MapNodeVertex,
191  TopoDS_Vertex&               VN);
192
193 static void StoreInMap (const TopoDS_Shape& V1,
194                         const TopoDS_Shape& V2,
195                         TopTools_IndexedDataMapOfShapeShape& MapVV);
196
197 static void TrimEdge (const TopoDS_Edge&              CurrentEdge,
198                       const TopTools_ListOfShape&     D,
199                             TopTools_SequenceOfShape& Sv,  
200                             TColStd_SequenceOfReal&   MapverPar,
201                             TopTools_SequenceOfShape& S,
202                             TopTools_IndexedDataMapOfShapeShape& MapVV);
203
204 static Standard_Boolean DoubleOrNotInside (const TopTools_ListOfShape& EC,
205                                            const TopoDS_Vertex&        V);
206
207 static Standard_Boolean IsSmallClosedEdge(const TopoDS_Edge& anEdge,
208                                           const TopoDS_Vertex& aVertex);
209
210 static void MakeCircle 
211 (const TopoDS_Edge&                          E, 
212  const TopoDS_Vertex&                        V, 
213  const TopoDS_Face&                          F,
214  const Standard_Real                         Offset, 
215  BRepFill_IndexedDataMapOfOrientedShapeListOfShape& Map,
216  const Handle(Geom_Plane)&                   RefPlane);
217
218 static void MakeOffset 
219 (const TopoDS_Edge&                          E,
220  const TopoDS_Face&                          F,
221  const Standard_Real                         Offset, 
222  BRepFill_IndexedDataMapOfOrientedShapeListOfShape& Map,
223  const Handle(Geom_Plane)&                   RefPlane);
224
225
226
227 //=======================================================================
228 //function : CheckFace
229 //purpose  : Check if face contains an edge with C0 continuity
230 //=======================================================================
231 //
232 static void CheckFace(const TopoDS_Face& theFace)
233   {
234   TopExp_Explorer ex(theFace,TopAbs_EDGE);
235   for(; ex.More(); ex.Next())
236     {
237     TopoDS_Edge anEdge=TopoDS::Edge(ex.Current());
238     Standard_Real f,l;
239     const Handle(Geom2d_Curve) C = BRep_Tool::CurveOnSurface(anEdge,theFace,f,l);
240     if (C->Continuity() == GeomAbs_C0)
241       Standard_ConstructionError::Raise("Initial shape contains an edge with C0 continuity");
242     }
243   }
244
245 //=======================================================================
246 //function : KPartCircle
247 //purpose  : 
248 //=======================================================================
249
250 static Standard_Boolean KPartCircle
251 (const TopoDS_Face&  mySpine,
252  const Standard_Real myOffset,
253  const Standard_Real Alt,
254  TopoDS_Shape&       myShape, 
255  BRepFill_IndexedDataMapOfOrientedShapeListOfShape& myMap,
256  Standard_Boolean&    myIsDone)
257 {
258   // The only contour which is a closed circle
259   TopExp_Explorer exp(mySpine,TopAbs_EDGE);
260   Standard_Integer NbEdges = 0;
261   TopoDS_Edge      E;
262
263   for (; exp.More(); exp.Next()) {
264     NbEdges++;
265     E = TopoDS::Edge(exp.Current());
266     if (NbEdges > 1) return Standard_False;
267   }
268   TopoDS_Vertex V1,V2;
269   TopExp::Vertices(E,V1,V2);
270   if (!V1.IsSame(V2)) return Standard_False;
271
272   Standard_Real      f,l;
273   TopLoc_Location    L;
274   Handle(Geom_Curve) C =  BRep_Tool::Curve(E,L,f,l);
275
276   if (C->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
277     Handle(Geom_TrimmedCurve) Ct = Handle(Geom_TrimmedCurve)::DownCast(C);
278     C = Ct->BasisCurve();
279   }
280
281   if (!C->IsKind(STANDARD_TYPE(Geom_Circle))) return Standard_False;
282   Handle(Geom_Circle) CE = Handle(Geom_Circle)::DownCast(C);
283   Standard_Real anOffset = myOffset;
284   if (E.Orientation() == TopAbs_REVERSED) anOffset *= -1;
285   
286   if (anOffset < 0. || Abs(anOffset) < CE->Radius()) {
287     Handle(Geom_Circle) OC = new Geom_Circle (CE->Position(),CE->Radius() - anOffset);
288     myShape = BRepLib_MakeEdge(OC,f,l);
289
290     myShape.Orientation(E.Orientation());
291     myShape.Location(L);
292     if (Alt != 0.) {
293       BRepAdaptor_Surface S(mySpine,0);
294       gp_Ax1 Nor = S.Plane().Axis();
295       gp_Trsf T;
296       gp_Vec Trans(Nor.Direction());
297       Trans = Alt*Trans;
298       T.SetTranslation(Trans);
299       myShape.Move(TopLoc_Location(T));
300     }
301     
302     TopTools_ListOfShape LL;
303     LL.Append(myShape);
304     myMap.Add(E,LL);
305   }
306   myIsDone = Standard_True;
307   return Standard_True;
308 }
309
310 //=======================================================================
311 //function : BRepFill_OffsetWire
312 //purpose  : 
313 //=======================================================================
314
315 BRepFill_OffsetWire::BRepFill_OffsetWire() 
316 :myIsDone(Standard_False)
317 {
318 }
319
320
321 //=======================================================================
322 //function : BRepFill_OffsetWire
323 //purpose  : 
324 //=======================================================================
325
326 BRepFill_OffsetWire::BRepFill_OffsetWire(const TopoDS_Face&     Spine,
327                                          const GeomAbs_JoinType Join )
328 {
329   Init(Spine,Join);
330 }
331
332 //=======================================================================
333 //function : Init
334 //purpose  : 
335 //=======================================================================
336
337 void BRepFill_OffsetWire::Init(const TopoDS_Face&     Spine,
338                                const GeomAbs_JoinType Join )
339 {
340   Standard_NotImplemented_Raise_if(Join > GeomAbs_Arc,
341                                    "Only GeomAbs_Arc is implemented");
342
343   myIsDone   = Standard_False;
344   TopoDS_Shape aLocalShape = Spine.Oriented(TopAbs_FORWARD);
345   mySpine    = TopoDS::Face(aLocalShape);
346 //  mySpine    = TopoDS::Face(Spine.Oriented(TopAbs_FORWARD));
347   myJoinType = Join;
348
349   CheckFace(mySpine);
350   
351   myMap.Clear();
352   myMapSpine.Clear();
353   //------------------------------------------------------------------
354   // cut the spine for bissectors.
355   //------------------------------------------------------------------
356 //  Modified by Sergey KHROMOV - Tue Nov 26 17:39:03 2002 Begin
357   static BRepMAT2d_Explorer Exp;
358
359   Exp.Perform(mySpine);
360
361 //  TopoDS_Face anOldSpine = mySpine;
362
363   mySpine = TopoDS::Face(Exp.ModifiedShape(mySpine));
364   PrepareSpine  ();
365
366 //  Modified by Sergey KHROMOV - Tue Nov 26 17:39:03 2002 End
367   TopoDS_Shape aShape;
368   BRepFill_IndexedDataMapOfOrientedShapeListOfShape aMap;
369   Standard_Boolean Done;
370   if (KPartCircle(myWorkSpine,1.,0.,aShape,aMap,Done)) return;
371   
372
373   //-----------------------------------------------------
374   // Calculate the map of bissectors to the left.  
375   // and Links Topology -> base elements of the map.
376   //-----------------------------------------------------
377   
378 //  Modified by Sergey KHROMOV - Tue Nov 26 17:39:03 2002 Begin
379 //   static BRepMAT2d_Explorer Exp;
380 //  Modified by Sergey KHROMOV - Tue Nov 26 17:39:03 2002 End
381   Exp.Perform(myWorkSpine);
382   myBilo.Compute(Exp,1,MAT_Left);
383   myLink.Perform(Exp,myBilo);
384 }
385
386
387 //=======================================================================
388 //function : IsDone
389 //purpose  : 
390 //=======================================================================
391
392 Standard_Boolean BRepFill_OffsetWire::IsDone() const 
393 {
394   return myIsDone;
395 }
396
397
398 //=======================================================================
399 //function : Spine
400 //purpose  : 
401 //=======================================================================
402
403 const TopoDS_Face& BRepFill_OffsetWire::Spine() const 
404 {
405   return mySpine;
406 }
407
408
409 //=======================================================================
410 //function : Shape
411 //purpose  : 
412 //=======================================================================
413
414 const TopoDS_Shape& BRepFill_OffsetWire::Shape() const 
415 {
416   return myShape;
417 }
418
419
420 //=======================================================================
421 //function : GeneratedShapes
422 //purpose  : 
423 //=======================================================================
424
425 const TopTools_ListOfShape& BRepFill_OffsetWire::GeneratedShapes
426 (const TopoDS_Shape& SpineShape)
427 {  
428   if (!myCallGen) {
429     if (!myMapSpine.IsEmpty()) {
430       // myMapSpine can be empty if passed by PerformWithBilo.
431       TopTools_DataMapIteratorOfDataMapOfShapeShape it(myMapSpine);
432       for (; it.More(); it.Next()) {
433         if (myMap.Contains(it.Key())) {
434           if (!myMap.Contains(it.Value())) {
435             TopTools_ListOfShape L;
436             myMap.Add(it.Value(),L);
437           }
438           if ( !it.Value().IsSame(it.Key())) {
439             myMap.ChangeFromKey(it.Value()).Append(myMap.ChangeFromKey(it.Key()));
440             //myMap.UnBind(it.Key());
441             TopoDS_Shape LastShape = myMap.FindKey(myMap.Extent());
442             TopTools_ListOfShape LastList;
443             LastList.Append(myMap(myMap.Extent()));
444             myMap.RemoveLast();
445             if (myMap.FindIndex(it.Key()) != 0)
446               myMap.Substitute(myMap.FindIndex(it.Key()), LastShape, LastList);
447           }
448         }
449         if (myMap.Contains(it.Key().Reversed())) {
450           if (!myMap.Contains(it.Value().Reversed())) {
451             TopTools_ListOfShape L;
452             myMap.Add(it.Value().Reversed(),L);
453           }
454           if ( !it.Value().IsSame(it.Key())) {
455             myMap.ChangeFromKey(it.Value().Reversed()).Append(myMap.ChangeFromKey(it.Key().Reversed()));
456             //myMap.UnBind(it.Key().Reversed());
457             TopoDS_Shape LastShape = myMap.FindKey(myMap.Extent());
458             TopTools_ListOfShape LastList;
459             LastList.Append(myMap(myMap.Extent()));
460             myMap.RemoveLast();
461             if (myMap.FindIndex(it.Key().Reversed()) != 0)
462               myMap.Substitute(myMap.FindIndex(it.Key().Reversed()), LastShape, LastList);
463           }
464         }
465       }
466     }
467     myCallGen = Standard_True;
468   }
469   
470   if (myMap.Contains(SpineShape)) {
471     return myMap.FindFromKey(SpineShape);
472   }
473   else {
474     static TopTools_ListOfShape Empty;
475     return Empty;
476   }
477 }
478
479
480 //=======================================================================
481 //function : JoinType
482 //purpose  : 
483 //=======================================================================
484
485 GeomAbs_JoinType BRepFill_OffsetWire::JoinType() const 
486 {
487   return myJoinType;
488 }
489
490 //=======================================================================
491 //function : Perform
492 //purpose  : 
493 //=======================================================================
494
495 void BRepFill_OffsetWire::Perform (const Standard_Real Offset,
496                                    const Standard_Real Alt)
497 {
498   //  Modified by skv - Fri Jul  8 11:21:38 2005 OCC9145 Begin
499   try
500   {
501     OCC_CATCH_SIGNALS
502       myCallGen = Standard_False;
503     if (KPartCircle(mySpine,Offset,Alt,myShape,myMap,myIsDone)) return;
504
505     TopoDS_Face oldWorkSpain = myWorkSpine;
506
507     TopTools_ListOfShape BadEdges;
508     CheckBadEdges(myWorkSpine,Offset,myBilo,myLink,BadEdges);
509
510     if(!BadEdges.IsEmpty())
511     {
512       // Modification of myWorkSpine;
513       //cout << "Modification of myWorkSpine : " << BadEdges.Extent() << endl;
514       BRepTools_Substitution aSubst;
515       TopTools_ListIteratorOfListOfShape it(BadEdges);
516       TopTools_ListOfShape aL;
517       Standard_Real aDefl = .01 * Abs(Offset);
518       TColStd_SequenceOfReal Parameters;
519       TColgp_SequenceOfPnt Points;
520
521       for(; it.More(); it.Next()) {
522         aL.Clear();
523         Parameters.Clear();
524         Points.Clear();
525         const TopoDS_Shape& anE = it.Value();
526
527         TopoDS_Vertex Vf, Vl;
528         TopExp::Vertices(TopoDS::Edge(anE), Vf, Vl);
529
530         Standard_Real f, l;
531         Handle(Geom_Curve) G3d = BRep_Tool::Curve(TopoDS::Edge(anE),f,l);
532         GeomAdaptor_Curve  AC(G3d,f,l);
533
534         PerformCurve(Parameters, Points, AC, aDefl, f, 
535                                           l, Precision::Confusion(), 2);
536
537         Standard_Integer NPnts = Points.Length();
538         if(NPnts > 2)
539         {
540           //cout << NPnts << " points " << endl;
541           TopoDS_Vertex FV = Vf;
542           TopoDS_Vertex LV;
543           TopoDS_Edge newE;
544           Standard_Integer np;
545           for(np = 2; np < NPnts; np++) {
546             gp_Pnt LP = Points(np);
547             LV = BRepLib_MakeVertex(LP);
548             newE = BRepLib_MakeEdge(FV, LV);
549             aL.Append(newE);
550             FV = LV;
551           }
552           LV = Vl;
553           newE = BRepLib_MakeEdge(FV, LV);
554           aL.Append(newE);
555         }
556         else
557         {
558           //cout << " 2 points " << endl;
559           TopoDS_Edge newE = BRepLib_MakeEdge(Vf, Vl);
560           aL.Append(newE);
561         }
562         //Update myMapSpine
563         if (myMapSpine.IsBound( anE ))
564         {
565           TopTools_ListIteratorOfListOfShape newit( aL );
566           for (; newit.More(); newit.Next())
567           {
568             TopoDS_Edge NewEdge = TopoDS::Edge( newit.Value() );
569             myMapSpine.Bind( NewEdge, myMapSpine(anE) );
570             TopoDS_Vertex NewV1, NewV2;
571             EdgeVertices( NewEdge, NewV1, NewV2 );
572             if (!myMapSpine.IsBound(NewV1)) myMapSpine.Bind( NewV1, myMapSpine(anE) );
573             if (!myMapSpine.IsBound(NewV2)) myMapSpine.Bind( NewV2, myMapSpine(anE) );
574           }
575           myMapSpine.UnBind( anE );
576         }
577         ///////////////////
578         aSubst.Substitute(anE, aL);
579       }
580
581       TopTools_DataMapOfShapeListOfShape wwmap;
582       TopoDS_Iterator itws( myWorkSpine );
583       for (; itws.More(); itws.Next())
584       {
585         TopoDS_Shape aWire = itws.Value();
586         aSubst.Build( aWire );
587         if (aSubst.IsCopied(aWire))
588         {
589           TopoDS_Wire NewWire = TopoDS::Wire( aSubst.Copy(aWire).First() );
590           NewWire.Closed( aWire.Closed() );
591           TopTools_ListOfShape Lw;
592           Lw.Append( NewWire );
593           wwmap.Bind( aWire, Lw );
594         }
595       }
596       aSubst.Clear();
597       TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itmap( wwmap );
598       for (; itmap.More(); itmap.Next())
599         aSubst.Substitute( itmap.Key(), itmap.Value() );
600       
601       aSubst.Build(myWorkSpine);
602       
603       if(aSubst.IsCopied(myWorkSpine)) {
604         myWorkSpine = TopoDS::Face(aSubst.Copy(myWorkSpine).First());
605         //sprintf(name,"WS1");
606         //DBRep::Set(name,myWorkSpine);
607
608         BRepMAT2d_Explorer newExp;
609         newExp.Perform(myWorkSpine);
610         BRepMAT2d_BisectingLocus newBilo;
611         BRepMAT2d_LinkTopoBilo newLink;
612         newBilo.Compute(newExp,1,MAT_Left);
613
614         if(!newBilo.IsDone())
615         {
616           myShape.Nullify();
617           myIsDone = Standard_False;
618           return;
619         }
620
621         newLink.Perform(newExp,newBilo);
622         PerformWithBiLo(myWorkSpine,Offset,newBilo,newLink,myJoinType,Alt);
623         myWorkSpine = oldWorkSpain;
624       }
625       else {
626         PerformWithBiLo(myWorkSpine,Offset,myBilo,myLink,myJoinType,Alt);
627       }
628     }
629     else
630     {
631       PerformWithBiLo(myWorkSpine,Offset,myBilo,myLink,myJoinType,Alt);
632     }
633   }
634   catch (...)//Every exception was caught.
635   {
636     myShape.Nullify();
637     myIsDone = Standard_False;
638     cout<<"An exception was caught in BRepFill_OffsetWire::Perform : ";
639     Standard_Failure::Caught()->Print(cout);
640     cout<<endl;
641
642     return;
643   }
644
645   //  Modified by skv - Fri Jul  8 11:21:38 2005 OCC9145 End
646   //  Modified by Sergey KHROMOV - Thu Mar 14 10:48:15 2002 Begin
647   TopExp_Explorer anExp(myShape, TopAbs_WIRE);
648
649   for (; anExp.More(); anExp.Next()) {
650     const TopoDS_Shape &aWire = anExp.Current();
651
652     if (!aWire.Closed()) {
653       myShape.Nullify();
654       myIsDone = Standard_False;
655       Standard_ConstructionError::Raise("Offset wire is not closed.");
656     }
657   }
658   //  Modified by Sergey KHROMOV - Thu Mar 14 10:48:16 2002 End
659 }
660
661 //=======================================================================
662 //function : Compute
663 //purpose  : 
664 //=======================================================================
665
666 void Compute (const TopoDS_Face&  Spine,
667                     TopoDS_Shape& aShape,
668                     BRepFill_IndexedDataMapOfOrientedShapeListOfShape& Map,
669               const Standard_Real Alt)
670 {
671   BRep_Builder B;
672   B.MakeCompound(TopoDS::Compound(aShape));
673   Standard_Real ALT = Alt;
674   if ( Spine.Orientation() == TopAbs_REVERSED) ALT = -Alt;
675   gp_Trsf T;
676   T.SetTranslation(gp_Vec(0.,0.,ALT));
677   TopLoc_Location L(T);
678
679   for ( TopExp_Explorer exp(Spine,TopAbs_WIRE); exp.More(); exp.Next()) {
680     const TopoDS_Wire& CurW = TopoDS::Wire(exp.Current());
681     TopoDS_Shape aLocalShape = CurW.Moved(L);
682     TopoDS_Wire        NewW = TopoDS::Wire(aLocalShape);
683 //    TopoDS_Wire        NewW = TopoDS::Wire(CurW.Moved(L));
684     B.Add(aShape,NewW);
685     // update Map.
686     TopoDS_Iterator it1( CurW);
687     TopoDS_Iterator it2( NewW);
688     for ( ; it1.More(); it1.Next(), it2.Next()) {
689       TopTools_ListOfShape List;
690       List.Append(it2.Value());
691       Map.Add(it1.Value(), List);
692     }
693   }
694 }
695
696 //=======================================================================
697 //function : PerformWithBiLo
698 //purpose  : 
699 //=======================================================================
700
701 void BRepFill_OffsetWire::PerformWithBiLo
702 (const TopoDS_Face&              Spine,
703  const Standard_Real             Offset,
704  const BRepMAT2d_BisectingLocus& Locus, 
705        BRepMAT2d_LinkTopoBilo&   Link,
706  const GeomAbs_JoinType          Join,
707  const Standard_Real             Alt)
708 {
709   Standard_NotImplemented_Raise_if (Join > GeomAbs_Arc,
710                                     "Only GeomAbs_Arc is implemented");
711
712   myIsDone     = Standard_False;
713   TopoDS_Shape aLocalShape = Spine.Oriented(TopAbs_FORWARD);
714   myWorkSpine  = TopoDS::Face(aLocalShape);
715 //  myWorkSpine  = TopoDS::Face(Spine.Oriented(TopAbs_FORWARD));
716   myJoinType   = Join;
717   myOffset     = Offset ;
718   myShape.Nullify();
719
720
721   if (mySpine.IsNull()) {
722     TopoDS_Shape aLocalShape = Spine.Oriented(TopAbs_FORWARD);
723     mySpine = TopoDS::Face(aLocalShape);
724 //    mySpine = TopoDS::Face(Spine.Oriented(TopAbs_FORWARD));
725 }
726   myMap.Clear();
727
728   if ( Abs(myOffset) < Precision::Confusion()) {
729     Compute(mySpine,myShape,myMap,Alt);
730     myIsDone = Standard_True;
731     return;
732   }
733
734   //********************************
735   // Calculate for a non null offset 
736   //********************************
737   if (KPartCircle(mySpine,Offset,Alt,myShape,myMap,myIsDone))
738     return;
739
740   BRep_Builder myBuilder;
741   myBuilder.MakeCompound(TopoDS::Compound(myShape));
742   
743   //---------------------------------------------------------------------
744   // MapNodeVertex : associate to each node of the map (key1) and to
745   //                 each element of the profile (key2) a vertex (item).
746   // MapBis        : all edges or vertices (item) generated by 
747   //                 a bisectrice on a face or an edge (key) of revolution tubes.
748   // MapVerPar     : Map of parameters of vertices on parallel edges 
749   //                 the list contained in MapVerPar (E) corresponds to 
750   //                 parameters on E of vertices contained in MapBis(E);
751   //---------------------------------------------------------------------
752
753
754   BRepFill_DataMapOfNodeShape               MapNodeVertex; 
755   TopTools_DataMapOfShapeSequenceOfShape    MapBis;  
756   BRepFill_DataMapOfShapeSequenceOfReal     MapVerPar;
757
758   TopTools_DataMapOfShapeShape              EmptyMap;
759   TopTools_SequenceOfShape                  EmptySeq;
760   TopTools_ListOfShape                      EmptyList;
761   TColStd_SequenceOfReal                    EmptySeqOfReal;
762
763   Standard_Real ALT = Alt;
764   Handle(Geom_Plane) RefPlane = 
765     Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(myWorkSpine));
766   if ( myWorkSpine.Orientation() == TopAbs_REVERSED) ALT = -Alt;
767   RefPlane = Handle(Geom_Plane)::DownCast
768     (RefPlane->Translated( ALT * gp_Vec(RefPlane->Axis().Direction() )));
769
770   //---------------------------------------------------------------
771   // Construction of Circles and OffsetCurves
772   //---------------------------------------------------------------
773  
774   for (Standard_Integer ic = 1; ic <= Locus.NumberOfContours(); ic++) {
775     TopoDS_Shape PEE = Link.GeneratingShape(Locus.BasicElt(ic,Locus.NumberOfElts(ic)));
776     TopoDS_Shape& PE = PEE ;      
777     for (Standard_Integer ie = 1; ie <= Locus.NumberOfElts(ic); ie++) {
778       const TopoDS_Shape& SE = Link.GeneratingShape(Locus.BasicElt(ic,ie));
779       if (SE.ShapeType() == TopAbs_VERTEX) {
780         MakeCircle (TopoDS::Edge(PE),TopoDS::Vertex(SE),
781                     myWorkSpine,myOffset,myMap,RefPlane);
782       }
783       else {
784         MakeOffset (TopoDS::Edge(SE),myWorkSpine,myOffset,myMap,RefPlane);
785         PE = SE;
786       }
787     }
788   }
789
790
791 #ifdef DEB
792   if (AffichEdge) {
793     cout << " End Construction of geometric primitives "<<endl;
794   }
795 #endif
796
797
798   //---------------------------------------------------
799   // Construction of offset vertices.
800   //---------------------------------------------------
801   BRepFill_DataMapOfOrientedShapeListOfShape Detromp;
802   Handle(MAT_Arc)        CurrentArc;
803   Handle(Geom2d_Curve)   Bis, PCurve1, PCurve2 ;
804   Handle(Geom_Curve)     CBis;
805   Standard_Boolean       Reverse;
806   TopoDS_Edge            CurrentEdge;
807   TopoDS_Shape           S       [2];
808   TopoDS_Edge            E       [2];
809   TopLoc_Location        L;
810   Standard_Integer       j, k;
811
812   for (Standard_Integer i = 1; i <= Locus.Graph()->NumberOfArcs(); i++) {
813
814     CurrentArc           = Locus.Graph()->Arc(i);
815     Bisector_Bisec Bisec = Locus.GeomBis(CurrentArc,Reverse);
816     
817 #ifdef DRAW
818   if ( AffichGeom) {
819     sprintf(name,"BISSEC_%d",NbBISSEC++);
820     DrawTrSurf::Set(name,Bisec.Value());
821   }
822 #endif
823
824     //-------------------------------------------------------------------
825     // Return elements of the spine corresponding to separate basicElts.
826     //-------------------------------------------------------------------
827     S [0] = Link.GeneratingShape(CurrentArc->FirstElement());
828     S [1] = Link.GeneratingShape(CurrentArc->SecondElement());
829
830     TopTools_SequenceOfShape Vertices;
831     TColgp_SequenceOfPnt     Params;
832     
833     TopTools_DataMapOfShapeSequenceOfShape MapSeqVer;
834     BRepFill_DataMapOfShapeSequenceOfPnt   MapSeqPar;
835
836     //-----------------------------------------------------------
837     // Return parallel edges on each face.
838     // If no offset generated => move to the next bissectrice. 
839     //--------------------------------------------------------------
840     if (myMap.Contains(S[0]) && myMap.Contains(S[1])) {
841       E [0] = TopoDS::Edge(myMap.FindFromKey(S[0]).First());
842       E [1] = TopoDS::Edge(myMap.FindFromKey(S[1]).First());
843     }
844     else continue;
845
846     //-----------------------------------------------------------
847     // Construction of vertices corresponding to the node of the map.
848     // if they are on the offset.
849     //-----------------------------------------------------------
850     TopoDS_Vertex VS,VE;
851     Handle(MAT_Node) Node1, Node2;
852
853     if (Reverse) {
854       Node1 = CurrentArc->SecondNode();
855       Node2 = CurrentArc->FirstNode();
856     }
857     else  {
858       Node1 = CurrentArc->FirstNode();
859       Node2 = CurrentArc->SecondNode();
860     }
861     
862     Standard_Boolean StartOnEdge = 0, EndOnEdge = 0;
863     
864     if (!Node1->Infinite()) {
865       gp_Pnt2d aLocalPnt2d = Locus.GeomElt(Node1);
866       StartOnEdge = VertexFromNode(Node1, myOffset, aLocalPnt2d ,MapNodeVertex,VS);
867 //      StartOnEdge = VertexFromNode(Node1, myOffset, Locus.GeomElt(Node1),
868 //                                 MapNodeVertex,VS);
869     }
870     if (!Node2->Infinite()) {
871       gp_Pnt2d aLocalPnt2d = Locus.GeomElt(Node2) ;
872       EndOnEdge   = VertexFromNode(Node2, myOffset, aLocalPnt2d ,MapNodeVertex,VE);
873 //      EndOnEdge   = VertexFromNode(Node2, myOffset, Locus.GeomElt(Node2),
874 //                                 MapNodeVertex,VE);
875     }
876
877     //---------------------------------------------
878     // Construction of geometries.
879     //---------------------------------------------
880     BRepFill_TrimEdgeTool Trim (Bisec, 
881                                 Locus.GeomElt(CurrentArc->FirstElement()),
882                                 Locus.GeomElt(CurrentArc->SecondElement()),
883                                 myOffset);
884
885     //-----------------------------------------------------------
886     // Construction of vertices on edges parallel to the spine.
887     //-----------------------------------------------------------
888
889     Trim.IntersectWith(E [0], E [1], Params);
890
891     for (Standard_Integer s = 1; s <= Params.Length(); s++) {
892       TopoDS_Vertex VC;
893       myBuilder.MakeVertex (VC);
894       gp_Pnt2d P2  = Bisec.Value()->Value(Params.Value(s).X());
895       gp_Pnt   PVC(P2.X(),P2.Y(),0.);
896
897       myBuilder.UpdateVertex(VC,PVC,Precision::Confusion());
898       Vertices.Append(VC);
899     }
900     if (StartOnEdge) {
901       Standard_Boolean Start = 1;
902       Trim.AddOrConfuse(Start, E[0], E[1], Params);
903       if (Params.Length() == Vertices.Length()) 
904          Vertices.SetValue(1,VS);
905       
906       else
907         // the point was not found by IntersectWith
908         Vertices.Prepend(VS);
909     }
910     if (EndOnEdge) {      
911       Standard_Boolean Start = 0;
912       Trim.AddOrConfuse(Start, E[0], E[1], Params);
913       if (Params.Length() == Vertices.Length()) 
914          Vertices.SetValue(Params.Length(),VE);
915       
916       else
917         // the point was not found by IntersectWith
918         Vertices.Append(VE);
919     }
920
921     //------------------------------------------------------------
922     // Update Detromp.
923     // Detromp allows to remove vertices on the offset 
924     // corresponding to tangency zones
925     // Detromp ranks the vertices that limit
926     // the parts of the bissectrices located between the spine and the 
927     // offset.
928     //------------------------------------------------------------
929     if (!Detromp.IsBound(S[0])) Detromp.Bind(S[0],EmptyList);
930     if (!Detromp.IsBound(S[1])) Detromp.Bind(S[1],EmptyList);
931
932     
933     UpdateDetromp (Detromp(S[0]), Detromp(S[1]), Vertices, Params, 
934                    Bisec, StartOnEdge, EndOnEdge, Trim);
935     //----------------------------------------------
936     // Storage of vertices on parallel edges.
937     // fill MapBis and MapVerPar.
938     //----------------------------------------------
939     if (!Vertices.IsEmpty()) {
940       for (k = 0; k <= 1; k++) {
941         if (!MapBis.IsBound(E[k])) {
942           MapBis   .Bind(E[k],EmptySeq);
943           MapVerPar.Bind(E[k],EmptySeqOfReal);
944         } 
945         for (Standard_Integer ii = 1; ii <= Vertices.Length(); ii++) {
946           MapBis (E[k]).Append(Vertices.Value(ii));
947           if (k == 0) MapVerPar (E[k]).Append(Params.Value(ii).Y());
948           else        MapVerPar (E[k]).Append(Params.Value(ii).Z());
949         }
950       }
951     }
952     else {
953       //------------------------------------------------------------
954       // FOR COMPLETE CIRCLES. the parallel line can be contained
955       // in the zone without intersection with the border
956       // no intersection 
957       // if myoffset is < distance of nodes the parallel can be valid.
958       //-------------------------------------------------------------
959       for (k = 0; k <= 1; k++) {
960         if (!MapBis.IsBound(E[k])) {
961           if (Node1->Distance() > myOffset && Node2->Distance() > myOffset) {
962             MapBis   .Bind(E[k],EmptySeq); 
963             MapVerPar.Bind(E[k],EmptySeqOfReal);
964           }
965         }
966       }
967     }
968   }
969   
970 #ifdef DEB 
971   if (AffichEdge) {
972     cout << " End Construction of vertices on offsets"<<endl;
973   }
974 #endif
975
976   //----------------------------------
977   // Construction of parallel edges.
978   //----------------------------------
979   TopTools_IndexedDataMapOfShapeShape MapVV;
980
981   TopoDS_Shape CurrentSpine;
982
983   //BRepFill_DataMapIteratorOfDataMapOfOrientedShapeListOfShape ite1;  
984
985   for (j = 1; j <= myMap.Extent(); j++) {
986     CurrentSpine = myMap.FindKey(j);
987     CurrentEdge  = TopoDS::Edge(myMap(j).First());
988
989     myMap(j).Clear();
990     if (MapBis.IsBound(CurrentEdge)) {
991       TopTools_SequenceOfShape S;
992       if (!MapBis(CurrentEdge).IsEmpty()) {
993         TrimEdge (CurrentEdge,
994                   Detromp  (CurrentSpine),
995                   MapBis   (CurrentEdge) ,  
996                   MapVerPar(CurrentEdge) ,
997                   S, MapVV);
998         for ( k = 1; k <= S.Length(); k++) {
999           myMap(j).Append(S.Value(k));
1000         }
1001       }
1002       else {
1003         //-----------------
1004         // Complete circles
1005         //-----------------
1006         myMap(j).Append(CurrentEdge);
1007       }
1008     }
1009   }
1010   
1011   Standard_Integer ind;
1012   for (ind = 1; ind <= MapVV.Extent(); ind++)
1013     {
1014       TopoDS_Vertex OldV = TopoDS::Vertex(MapVV.FindKey(ind));
1015       TopoDS_Vertex NewV = TopoDS::Vertex(MapVV(ind));
1016       gp_Pnt P1 = BRep_Tool::Pnt(OldV);
1017       gp_Pnt P2 = BRep_Tool::Pnt(NewV);
1018       myBuilder.UpdateVertex(NewV, P1.Distance(P2));
1019       TopTools_ListOfShape LV;
1020       LV.Append( NewV.Oriented(TopAbs_FORWARD) );
1021       BRepTools_Substitution aSubst;
1022       aSubst.Substitute( OldV, LV );
1023       for (j = 1; j <= myMap.Extent(); j++)
1024         {
1025           TopTools_ListIteratorOfListOfShape itl(myMap(j));
1026           for (; itl.More(); itl.Next())
1027             {
1028               aSubst.Build(itl.Value());
1029               if (aSubst.IsCopied(itl.Value()))
1030                 {
1031                   const TopTools_ListOfShape& listSh = aSubst.Copy(itl.Value());
1032                   TopAbs_Orientation SaveOr = itl.Value().Orientation();
1033                   itl.Value() = listSh.First();
1034                   itl.Value().Orientation(SaveOr);
1035                 }
1036             }
1037         }
1038     }
1039       
1040   //----------------------------------
1041   // Construction of offset wires.
1042   //----------------------------------
1043   MakeWires ();
1044
1045   // Update vertices ( Constructed in the plane Z = 0) !!!
1046   TopTools_MapOfShape MapVertex;
1047   for ( TopExp_Explorer exp(myShape,TopAbs_VERTEX); exp.More(); exp.Next()) {
1048     TopoDS_Vertex V = TopoDS::Vertex(exp.Current());
1049     if ( MapVertex.Add(V)) {
1050       gp_Pnt        P = BRep_Tool::Pnt(V);
1051       P = RefPlane->Value(P.X(),P.Y());
1052       myBuilder.UpdateVertex(V,P, Precision::Confusion());
1053     }
1054   }
1055
1056   // Construction of curves 3d.
1057   BRepLib::BuildCurves3d(myShape);
1058   MapVertex.Clear();
1059   TopExp_Explorer Explo( myShape, TopAbs_EDGE );
1060   for (; Explo.More(); Explo.Next())
1061     {
1062       TopoDS_Edge E = TopoDS::Edge( Explo.Current() );
1063       TopoDS_Vertex V1, V2;
1064       TopExp::Vertices( E, V1, V2 );
1065       Handle(BRep_TVertex)& TV1 = *((Handle(BRep_TVertex)*) &(V1).TShape());
1066       Handle(BRep_TVertex)& TV2 = *((Handle(BRep_TVertex)*) &(V2).TShape());
1067       
1068       TopLoc_Location loc;
1069       Standard_Real f, l;
1070       Handle( Geom_Curve ) theCurve = BRep_Tool::Curve( E, loc, f, l );
1071       theCurve = Handle( Geom_Curve )::DownCast( theCurve->Copy() );
1072       theCurve->Transform( loc.Transformation() );
1073       gp_Pnt f3d = theCurve->Value( f );
1074       gp_Pnt l3d = theCurve->Value( l );
1075
1076       Standard_Real dist1, dist2;
1077       dist1 = f3d.Distance( TV1->Pnt() );
1078       dist2 = l3d.Distance( TV2->Pnt() );
1079       if (! MapVertex.Contains( V1 ))
1080         {
1081           TV1->Pnt( f3d );
1082           MapVertex.Add( V1 );
1083         }
1084       else
1085         TV1->UpdateTolerance( 1.5*dist1 );
1086       if (! MapVertex.Contains( V2 ))
1087         {
1088           TV2->Pnt( l3d );
1089           MapVertex.Add( V2 );
1090         }
1091       else
1092         TV2->UpdateTolerance( 1.5*dist2 );
1093     }
1094
1095   FixHoles();
1096
1097   myIsDone = Standard_True;
1098 }
1099
1100
1101 //=======================================================================
1102 //function : Generated
1103 //purpose  : 
1104 //=======================================================================
1105
1106 BRepFill_IndexedDataMapOfOrientedShapeListOfShape& 
1107 BRepFill_OffsetWire::Generated() 
1108 {
1109   return myMap;
1110 }
1111
1112
1113 //=======================================================================
1114 //function : PrepareSpine
1115 //purpose  : 
1116 //=======================================================================
1117
1118 void BRepFill_OffsetWire::PrepareSpine()
1119 {
1120   BRep_Builder      B;
1121   TopTools_ListOfShape Cuts;
1122   TopTools_ListIteratorOfListOfShape IteCuts;
1123   TopoDS_Vertex V1,V2;
1124
1125   myWorkSpine.Nullify();
1126   myMapSpine.Clear();
1127
1128   TopLoc_Location L;
1129   const Handle(Geom_Surface)& S    = BRep_Tool::Surface  (mySpine,L);
1130   Standard_Real               TolF = BRep_Tool::Tolerance(mySpine);
1131   B.MakeFace(myWorkSpine,S,L,TolF);
1132   
1133   for (TopoDS_Iterator IteF(mySpine) ; IteF.More(); IteF.Next()) {
1134
1135     TopoDS_Wire NW;
1136     B.MakeWire (NW);
1137
1138 //  Modified by Sergey KHROMOV - Thu Nov 16 17:29:55 2000 Begin
1139     Standard_Integer ForcedCut = 0;
1140     Standard_Integer nbResEdges = -1;
1141     TopTools_IndexedMapOfShape EdgeMap;
1142
1143     TopExp::MapShapes(IteF.Value(), TopAbs_EDGE, EdgeMap);
1144     Standard_Integer nbEdges = EdgeMap.Extent();
1145     
1146     if (nbEdges == 1)
1147       ForcedCut = 2;
1148 //  Modified by Sergey KHROMOV - Thu Nov 16 17:29:48 2000 End
1149
1150     for (TopoDS_Iterator IteW(IteF.Value()); IteW.More(); IteW.Next()) {
1151       
1152       const TopoDS_Edge& E = TopoDS::Edge(IteW.Value());
1153       EdgeVertices(E,V1,V2);
1154       myMapSpine.Bind(V1,V1);
1155       myMapSpine.Bind(V2,V2);
1156       Cuts.Clear();
1157
1158       // Cut
1159       TopoDS_Shape aLocalShape = E.Oriented(TopAbs_FORWARD);
1160 //  Modified by Sergey KHROMOV - Thu Nov 16 17:29:29 2000 Begin
1161       if (nbEdges == 2 && nbResEdges == 0)
1162         ForcedCut = 1;
1163 //  Modified by Sergey KHROMOV - Thu Nov 16 17:29:33 2000 End
1164       nbResEdges = CutEdge (TopoDS::Edge(aLocalShape), mySpine, ForcedCut, Cuts);
1165       
1166       if (Cuts.IsEmpty()) {
1167         B.Add(NW,E);
1168         myMapSpine.Bind(E,E);
1169       }
1170       else {    
1171         for (IteCuts.Initialize(Cuts); IteCuts.More(); IteCuts.Next()) {
1172           TopoDS_Edge NE = TopoDS::Edge(IteCuts.Value());
1173           NE.Orientation(E.Orientation());
1174           B.Add(NW,NE);
1175           myMapSpine.Bind(NE,E);
1176           EdgeVertices(NE,V1,V2);
1177           if (!myMapSpine.IsBound(V1)) myMapSpine.Bind(V1,E);
1178           if (!myMapSpine.IsBound(V2)) myMapSpine.Bind(V2,E);
1179         }
1180       }
1181     }
1182 //  Modified by Sergey KHROMOV - Thu Mar  7 09:17:41 2002 Begin
1183     TopoDS_Vertex aV1;
1184     TopoDS_Vertex aV2;
1185
1186     TopExp::Vertices(NW, aV1, aV2);
1187
1188     NW.Closed(aV1.IsSame(aV2));
1189 //  Modified by Sergey KHROMOV - Thu Mar  7 09:17:43 2002 End
1190     B.Add(myWorkSpine, NW);
1191   }
1192
1193 #ifdef DRAW
1194   if ( AffichEdge) {
1195     sprintf(name,"WS");
1196     DBRep::Set(name,myWorkSpine);
1197   }
1198 #endif
1199
1200 }
1201
1202 //=======================================================================
1203 //function : MakeWires
1204 //purpose  : 
1205 //=======================================================================
1206
1207 void BRepFill_OffsetWire::MakeWires()
1208 {
1209   //--------------------------------------------------------
1210   // creation of a single list of created parallel edges.
1211   //--------------------------------------------------------
1212   TopTools_SequenceOfShape TheEdges;
1213   TopTools_ListOfShape     TheWires;
1214   TopTools_ListIteratorOfListOfShape                          itl;
1215   //BRepFill_DataMapIteratorOfDataMapOfOrientedShapeListOfShape ite;  
1216   TopTools_IndexedDataMapOfShapeListOfShape                   MVE;
1217   //TopTools_DataMapIteratorOfDataMapOfShapeListOfShape         MVEit;
1218   TopoDS_Vertex V1,V2,VF,CV;
1219   Standard_Integer i;
1220
1221   for (i = 1; i <= myMap.Extent(); i++) {    
1222     for (itl.Initialize(myMap(i)); itl.More(); itl.Next()) {
1223       const TopoDS_Edge& E = TopoDS::Edge(itl.Value());
1224       TopExp::Vertices (E,V1,V2);
1225       if (V1.IsSame(V2) && IsSmallClosedEdge(E, V1))
1226         continue; //remove small closed edges
1227       if (!MVE.Contains(V1)) {
1228         TopTools_ListOfShape empty;
1229         MVE.Add(V1,empty);
1230       }
1231       MVE.ChangeFromKey(V1).Append(E);
1232       if (!MVE.Contains(V2)) {
1233         TopTools_ListOfShape empty;
1234         MVE.Add(V2,empty);
1235       }
1236       MVE.ChangeFromKey(V2).Append(E);
1237     }
1238   }
1239
1240   //--------------------------------------
1241   // Creation of parallel wires.
1242   //--------------------------------------
1243   BRep_Builder B;
1244
1245 //  Standard_Integer NbEdges;
1246 //  Standard_Boolean NewWire  = Standard_True;
1247 //  Standard_Boolean AddEdge  = Standard_False;
1248
1249   TopoDS_Wire      NW;
1250   Standard_Boolean End;
1251   TopoDS_Edge CE;
1252
1253   while (!MVE.IsEmpty()) {
1254     B.MakeWire(NW);
1255
1256     //MVEit.Initialize(MVE);
1257     for (i = 1; i <= MVE.Extent(); i++)
1258       if(MVE(i).Extent() == 1)
1259         break;
1260
1261     //if(!MVEit.More()) MVEit.Initialize(MVE);
1262     if (i > MVE.Extent())
1263       i = 1;
1264     
1265     CV  = VF = TopoDS::Vertex(MVE.FindKey(i));
1266     CE  = TopoDS::Edge(MVE(i).First());
1267     End = Standard_False;
1268     MVE.ChangeFromKey(CV).RemoveFirst(); 
1269
1270 //  Modified by Sergey KHROMOV - Thu Mar 14 11:29:59 2002 Begin
1271     Standard_Boolean isClosed = Standard_False;
1272 //  Modified by Sergey KHROMOV - Thu Mar 14 11:30:00 2002 End
1273
1274     while(!End) {      
1275       //-------------------------------
1276       // Construction of a wire.
1277       //-------------------------------
1278       TopExp::Vertices(CE,V1,V2);
1279       if (!CV.IsSame(V1)) CV = V1; else CV = V2;
1280
1281       B.Add (NW,CE);
1282
1283       if (VF.IsSame(CV) || !MVE.Contains(CV)) {
1284 //  Modified by Sergey KHROMOV - Thu Mar 14 11:30:14 2002 Begin
1285         isClosed = VF.IsSame(CV);
1286 //  Modified by Sergey KHROMOV - Thu Mar 14 11:30:15 2002 End
1287         End = Standard_True;
1288         //MVE.UnBind(VF);
1289         TopoDS_Shape LastShape = MVE.FindKey(MVE.Extent());
1290         TopTools_ListOfShape LastList;
1291         LastList.Append(MVE(MVE.Extent()));
1292         MVE.RemoveLast();
1293         if (MVE.FindIndex(VF) != 0)
1294           MVE.Substitute(MVE.FindIndex(VF), LastShape, LastList);
1295       }
1296
1297       if (!End) {
1298         if (MVE.FindFromKey(CV).Extent() > 2) {
1299           //cout <<"vertex on more that 2 edges in a face."<<endl;
1300         }
1301         for ( itl.Initialize(MVE.FindFromKey(CV)); itl.More(); itl.Next()) {
1302           if (itl.Value().IsSame(CE)) {
1303             MVE.ChangeFromKey(CV).Remove(itl);
1304             break;
1305           }
1306         }
1307         if (!MVE.FindFromKey(CV).IsEmpty()) {
1308           CE = TopoDS::Edge(MVE.FindFromKey(CV).First());
1309           MVE.ChangeFromKey(CV).RemoveFirst();
1310         }
1311         if (MVE.FindFromKey(CV).IsEmpty())
1312         {
1313           //MVE.UnBind(CV);
1314           TopoDS_Shape LastShape = MVE.FindKey(MVE.Extent());
1315           TopTools_ListOfShape LastList;
1316           LastList.Append(MVE(MVE.Extent()));
1317           MVE.RemoveLast();
1318           if (MVE.FindIndex(CV) != 0)
1319             MVE.Substitute(MVE.FindIndex(CV), LastShape, LastList);
1320         }
1321       }
1322     }
1323 //  Modified by Sergey KHROMOV - Thu Mar 14 11:29:31 2002 Begin
1324 //     NW.Closed(Standard_True);
1325     NW.Closed(isClosed);
1326 //  Modified by Sergey KHROMOV - Thu Mar 14 11:29:37 2002 End
1327     TheWires.Append(NW);
1328   }
1329
1330   // update myShape :
1331   //      -- if only one wire : myShape is a Wire
1332   //      -- if several wires : myShape is a Compound.
1333   if ( TheWires.Extent() == 1) {
1334     myShape = TheWires.First();
1335   }
1336   else {
1337     TopoDS_Compound R;
1338     B.MakeCompound(R);
1339     TopTools_ListIteratorOfListOfShape it(TheWires);
1340     for ( ; it.More(); it.Next()) {
1341       B.Add(R, it.Value());
1342     }
1343     myShape = R;
1344   }
1345   
1346 }
1347
1348 //=======================================================================
1349 //function : FixHoles
1350 //purpose  : 
1351 //=======================================================================
1352
1353 void BRepFill_OffsetWire::FixHoles()
1354 {
1355   TopTools_SequenceOfShape ClosedWires, UnclosedWires, IsolatedWires;
1356
1357   Standard_Real MaxTol = 0.;
1358   Standard_Integer i;
1359   BRep_Builder BB;
1360
1361   TopExp_Explorer Explo( mySpine, TopAbs_VERTEX );
1362   for (; Explo.More(); Explo.Next())
1363   {
1364     const TopoDS_Vertex& aVertex = TopoDS::Vertex( Explo.Current() );
1365     Standard_Real Tol = BRep_Tool::Tolerance(aVertex);
1366     if (Tol > MaxTol)
1367       MaxTol = Tol;
1368   }
1369   MaxTol *= 100.;
1370
1371   Explo.Init( myShape, TopAbs_WIRE );
1372   for (; Explo.More(); Explo.Next())
1373   {
1374     TopoDS_Shape aWire = Explo.Current();
1375     // Remove duplicated edges
1376     TopTools_DataMapOfShapeListOfShape EEmap;
1377     TopoDS_Iterator it( aWire );
1378     for (; it.More(); it.Next())
1379     {
1380       const TopoDS_Shape& anEdge = it.Value();
1381       if (! EEmap.IsBound( anEdge ))
1382       {
1383         TopTools_ListOfShape LE;
1384         EEmap.Bind( anEdge, LE );
1385       }
1386       else
1387         EEmap(anEdge).Append( anEdge );
1388     }
1389     aWire.Free( Standard_True );
1390     TopTools_DataMapIteratorOfDataMapOfShapeListOfShape mapit( EEmap );
1391     for (; mapit.More(); mapit.Next())
1392     {
1393       const TopTools_ListOfShape& LE = mapit.Value();
1394       TopTools_ListIteratorOfListOfShape itl( LE );
1395       for (; itl.More(); itl.Next())
1396         BB.Remove( aWire, itl.Value() );
1397     }
1398     // Sorting
1399     if (aWire.Closed())
1400       ClosedWires.Append( aWire );
1401     else
1402       UnclosedWires.Append( aWire );
1403   }
1404
1405   while (!UnclosedWires.IsEmpty())
1406   {
1407     TopoDS_Wire& Base = TopoDS::Wire( UnclosedWires(1) );
1408     TopoDS_Vertex Vf, Vl;
1409     TopExp::Vertices( Base, Vf, Vl );
1410     if(Vf.IsNull() || Vl.IsNull())
1411       Standard_Failure::Raise("BRepFill_OffsetWire::FixHoles(): Wrong wire.");
1412     gp_Pnt Pf, Pl;
1413     Pf = BRep_Tool::Pnt(Vf);
1414     Pl = BRep_Tool::Pnt(Vl);
1415     Standard_Real DistF = RealLast(), DistL = RealLast();
1416     Standard_Integer IndexF = 1, IndexL = 1;
1417     Standard_Boolean IsFirstF = Standard_False, IsFirstL = Standard_False;
1418     for (Standard_Integer i = 2; i <= UnclosedWires.Length(); i++)
1419     {
1420       TopoDS_Wire aWire = TopoDS::Wire( UnclosedWires(i) );
1421       TopoDS_Vertex V1, V2;
1422       TopExp::Vertices( aWire, V1, V2 );
1423
1424       if(V1.IsNull() || V2.IsNull())
1425         Standard_Failure::Raise("BRepFill_OffsetWire::FixHoles(): Wrong wire.");
1426
1427       gp_Pnt P1, P2;
1428       P1 = BRep_Tool::Pnt(V1);
1429       P2 = BRep_Tool::Pnt(V2);
1430       Standard_Real dist = Pf.Distance( P1 );
1431       if (dist < DistF)
1432       {
1433         DistF = dist;
1434         IndexF = i;
1435         IsFirstF = Standard_True;
1436       }
1437       dist = Pf.Distance( P2 );
1438       if (dist < DistF)
1439       {
1440         DistF = dist;
1441         IndexF = i;
1442         IsFirstF = Standard_False;
1443       }
1444       dist = Pl.Distance( P1 );
1445       if (dist < DistL)
1446       {
1447         DistL = dist;
1448         IndexL = i;
1449         IsFirstL = Standard_True;
1450       }
1451       dist = Pl.Distance( P2 );
1452       if (dist < DistL)
1453       {
1454         DistL = dist;
1455         IndexL = i;
1456         IsFirstL = Standard_False;
1457       }
1458     }
1459     TopoDS_Wire theWire;
1460     TopoDS_Edge theEdge;
1461     TopoDS_Vertex theVertex;
1462     Standard_Real CommonTol;
1463     Standard_Boolean TryToClose = Standard_True;
1464     if (DistF <= MaxTol && DistL <= MaxTol && IndexF == IndexL && IsFirstF == IsFirstL)
1465     {
1466       if (DistF < DistL)
1467       {
1468         DistL = RealLast();
1469         IndexL++;
1470       }
1471       else
1472       {
1473         DistF = RealLast();
1474         IndexF++;
1475       }
1476       TryToClose = Standard_False;
1477     }
1478     if (DistF <= MaxTol)
1479     {
1480       theWire = TopoDS::Wire( UnclosedWires(IndexF) );
1481       TopoDS_Vertex V1, V2;
1482       TopExp::Vertices( theWire, V1, V2 );
1483       TopTools_IndexedDataMapOfShapeListOfShape VEmap;
1484       TopExp::MapShapesAndAncestors( theWire, TopAbs_VERTEX, TopAbs_EDGE, VEmap );
1485       theEdge = (IsFirstF)? TopoDS::Edge(VEmap.FindFromKey( V1 ).First()) :
1486         TopoDS::Edge(VEmap.FindFromKey( V2 ).First());
1487       TopoDS_Iterator it( theWire );
1488       for (; it.More(); it.Next())
1489       {
1490         TopoDS_Edge anEdge = TopoDS::Edge( it.Value() );
1491         if (IsFirstF) anEdge.Reverse();
1492         if (!anEdge.IsSame( theEdge ))
1493           BB.Add( Base, anEdge );
1494       }
1495       theVertex = (IsFirstF)? V1 : V2;
1496       CommonTol = Max( BRep_Tool::Tolerance(Vf), BRep_Tool::Tolerance(theVertex) );
1497       if (DistF <= CommonTol)
1498       {
1499         theEdge.Free( Standard_True );
1500         Vf.Orientation( theVertex.Orientation() );
1501         BB.Remove( theEdge, theVertex );
1502         BB.Add( theEdge, Vf );
1503         BB.UpdateVertex( Vf, CommonTol );
1504         if (IsFirstF) theEdge.Reverse();
1505         BB.Add( Base, theEdge );
1506       }
1507       else
1508       {
1509         if (IsFirstF) theEdge.Reverse();
1510         BB.Add( Base, theEdge );
1511         // Creating new edge from theVertex to Vf
1512         TopoDS_Edge NewEdge = BRepLib_MakeEdge( theVertex, Vf );
1513         BB.Add( Base, NewEdge );
1514       }
1515     }
1516     if (DistL <= MaxTol && IndexL != IndexF)
1517     {
1518       theWire = TopoDS::Wire( UnclosedWires(IndexL) );
1519       TopoDS_Vertex V1, V2;
1520       TopExp::Vertices( theWire, V1, V2 );
1521       TopTools_IndexedDataMapOfShapeListOfShape VEmap;
1522       TopExp::MapShapesAndAncestors( theWire, TopAbs_VERTEX, TopAbs_EDGE, VEmap );
1523       theEdge = (IsFirstL)? TopoDS::Edge(VEmap.FindFromKey( V1 ).First()) :
1524         TopoDS::Edge(VEmap.FindFromKey( V2 ).First());
1525       TopoDS_Iterator it( theWire );
1526       for (; it.More(); it.Next())
1527       {
1528         TopoDS_Edge anEdge = TopoDS::Edge( it.Value() );
1529         if (!IsFirstL) anEdge.Reverse();
1530         if (!anEdge.IsSame( theEdge ))
1531           BB.Add( Base, anEdge );
1532       }
1533       theVertex = (IsFirstL)? V1 : V2;
1534       CommonTol = Max( BRep_Tool::Tolerance(Vl), BRep_Tool::Tolerance(theVertex) );
1535       if (DistL <= CommonTol)
1536       {
1537         theEdge.Free( Standard_True );
1538         Vl.Orientation( theVertex.Orientation() );
1539         BB.Remove( theEdge, theVertex );
1540         BB.Add( theEdge, Vl );
1541         BB.UpdateVertex( Vl, CommonTol );
1542         if (!IsFirstL) theEdge.Reverse();
1543         BB.Add( Base, theEdge );
1544       }
1545       else
1546       {
1547         if (!IsFirstL) theEdge.Reverse();
1548         BB.Add( Base, theEdge );
1549         // Creating new edge from Vl to theVertex
1550         TopoDS_Edge NewEdge = BRepLib_MakeEdge( Vl, theVertex );
1551         BB.Add( Base, NewEdge );
1552       }
1553     }
1554     // Check if it is possible to close resulting wire
1555     if (TryToClose)
1556     {
1557       TopExp::Vertices( Base, Vf, Vl );
1558       CommonTol = Max( BRep_Tool::Tolerance(Vf), BRep_Tool::Tolerance(Vl) );
1559       TopTools_IndexedDataMapOfShapeListOfShape VEmap;
1560       TopExp::MapShapesAndAncestors( Base, TopAbs_VERTEX, TopAbs_EDGE, VEmap );
1561       TopoDS_Edge Efirst, Elast;
1562       Efirst = TopoDS::Edge(VEmap.FindFromKey( Vf ).First());
1563       Elast  = TopoDS::Edge(VEmap.FindFromKey( Vl ).First());
1564       Pf = BRep_Tool::Pnt(Vf);
1565       Pl = BRep_Tool::Pnt(Vl);
1566       Standard_Real Dist = Pf.Distance(Pl);
1567       if (Dist <= CommonTol)
1568       {
1569         Elast.Free( Standard_True );
1570         Vf.Orientation( Vl.Orientation() );
1571         BB.Remove( Elast, Vl );
1572         BB.Add( Elast, Vf );
1573         BB.UpdateVertex( Vf, CommonTol );
1574         Base.Closed( Standard_True );
1575       }
1576       else if (Dist <= MaxTol)
1577       {
1578         // Creating new edge from Vl to Vf
1579         TopoDS_Edge NewEdge = BRepLib_MakeEdge( Vf, Vl );
1580         BB.Add( Base, NewEdge );
1581         Base.Closed( Standard_True );
1582       }
1583     }
1584     // Updating sequences ClosedWires and UnclosedWires
1585     if (DistF <= MaxTol)
1586       UnclosedWires.Remove( IndexF );
1587     if (DistL <= MaxTol && IndexL != IndexF)
1588     {
1589       if (DistF <= MaxTol && IndexL > IndexF)
1590         IndexL--;
1591       UnclosedWires.Remove( IndexL );
1592     }
1593     if (Base.Closed())
1594     {
1595       ClosedWires.Append( Base );
1596       UnclosedWires.Remove( 1 );
1597     }
1598     else if (DistF > MaxTol && DistL > MaxTol)
1599     {
1600       IsolatedWires.Append( Base );
1601       UnclosedWires.Remove( 1 );
1602     }
1603   }
1604
1605   // Updating myShape
1606   if (ClosedWires.Length() + IsolatedWires.Length() == 1)
1607   {
1608     if (!ClosedWires.IsEmpty())
1609       myShape = ClosedWires.First();
1610     else
1611       myShape = IsolatedWires.First();
1612   }
1613   else
1614   {
1615     TopoDS_Compound R;
1616     BB.MakeCompound( R );
1617     for (i = 1; i <= ClosedWires.Length(); i++)
1618       BB.Add( R, ClosedWires(i) );
1619     for (i = 1; i <= IsolatedWires.Length(); i++)
1620       BB.Add( R, IsolatedWires(i) );
1621     myShape = R;
1622   }
1623 }
1624
1625 //=======================================================================
1626 //function : CutEdge
1627 //purpose  : Cut edge at the extrema of curvatures and points of inflexion.
1628 //           So, closed circles are cut in two.
1629 //           If <Cuts> is empty, the edge is not modified.
1630 //           The first and the last vertex of the initial edge 
1631 //           belong to the first and the last parts respectively.
1632 //=======================================================================
1633 Standard_Integer CutEdge (const TopoDS_Edge& E, 
1634                           const TopoDS_Face& F,
1635                                 Standard_Integer ForceCut,
1636                                 TopTools_ListOfShape& Cuts)
1637 {
1638   Cuts.Clear();
1639   MAT2d_CutCurve              Cuter;
1640   TColGeom2d_SequenceOfCurve  theCurves;
1641   Standard_Real               f,l;
1642   Handle(Geom2d_Curve)        C2d;
1643   Handle(Geom2d_TrimmedCurve) CT2d;
1644 //  Modified by Sergey KHROMOV - Wed Mar  6 17:36:25 2002 Begin
1645   Standard_Real               aTol = BRep_Tool::Tolerance(E);
1646   Handle(Geom_Curve)          aC;
1647 //  Modified by Sergey KHROMOV - Wed Mar  6 17:36:25 2002 End
1648   
1649   TopoDS_Vertex V1,V2,VF,VL;
1650   TopExp::Vertices (E,V1,V2);
1651   BRep_Builder B;
1652   
1653   C2d  = BRep_Tool::CurveOnSurface (E,F,f,l);
1654 //  Modified by Sergey KHROMOV - Wed Mar  6 17:36:54 2002 Begin
1655   aC   = BRep_Tool::Curve(E,f,l);
1656 //  Modified by Sergey KHROMOV - Wed Mar  6 17:36:54 2002 End
1657   CT2d = new Geom2d_TrimmedCurve(C2d,f,l);
1658   //if (E.Orientation() == TopAbs_REVERSED) CT2d->Reverse();
1659
1660   if (CT2d->BasisCurve()->IsKind(STANDARD_TYPE(Geom2d_Circle)) &&
1661       ( Abs(f-l) >= M_PI) ) {
1662     return 0;
1663   }
1664
1665   //-------------------------
1666   // Cut curve.
1667   //-------------------------
1668   Cuter.Perform(CT2d);
1669
1670 //  Modified by Sergey KHROMOV - Thu Nov 16 17:28:29 2000 Begin
1671   if (ForceCut == 0) {
1672     if (Cuter.UnModified()) {
1673     //-----------------------------
1674     // edge not modified => return.
1675     //-----------------------------
1676       return 0;
1677     } else {
1678       for (Standard_Integer k = 1; k <= Cuter.NbCurves(); k++)
1679         theCurves.Append(Cuter.Value(k));
1680     }
1681   } else if (ForceCut == 1) {
1682     if (Cuter.UnModified()) {
1683       CutCurve (CT2d, 2, theCurves);
1684     } else {
1685       for (Standard_Integer k = 1; k <= Cuter.NbCurves(); k++)
1686         theCurves.Append(Cuter.Value(k));
1687     }
1688   } else if (ForceCut == 2) {
1689     if (Cuter.UnModified()) {
1690       CutCurve (CT2d, 3, theCurves);
1691     } else {
1692       if (Cuter.NbCurves() == 2) {
1693         Handle(Geom2d_TrimmedCurve)CC = Cuter.Value(1);
1694
1695         if (CC->LastParameter() > (l+f)/2.) {
1696           CutCurve (CC, 2, theCurves);
1697           theCurves.Append(Cuter.Value(2));
1698         } else {
1699           theCurves.Append(CC);
1700           CutCurve (Cuter.Value(2), 2, theCurves);
1701         }
1702       } else {
1703         for (Standard_Integer k = 1; k <= Cuter.NbCurves(); k++)
1704           theCurves.Append(Cuter.Value(k));
1705       }
1706     }
1707   }
1708 //  Modified by Sergey KHROMOV - Thu Nov 16 17:28:37 2000 End
1709
1710   //--------------------------------------
1711   // Creation of cut edges.
1712   //--------------------------------------
1713   VF = V1;
1714
1715   for (Standard_Integer k = 1; k <= theCurves.Length(); k++) {
1716
1717     Handle(Geom2d_TrimmedCurve)CC = Handle(Geom2d_TrimmedCurve)::DownCast(theCurves.Value(k));
1718
1719     if (k == theCurves.Length()) {VL = V2;}
1720     else { 
1721 //  Modified by Sergey KHROMOV - Wed Mar  6 17:38:02 2002 Begin
1722       gp_Pnt        P = aC->Value(CC->LastParameter());
1723
1724       VL = BRepLib_MakeVertex(P);
1725       B.UpdateVertex(VL, aTol);
1726 //  Modified by Sergey KHROMOV - Wed Mar  6 17:38:05 2002 End
1727     }
1728     TopoDS_Shape aLocalShape = E.EmptyCopied();
1729     TopoDS_Edge NE = TopoDS::Edge(aLocalShape);
1730 //      TopoDS_Edge NE = TopoDS::Edge(E.EmptyCopied());
1731     NE.Orientation(TopAbs_FORWARD);
1732     B.Add  (NE,VF.Oriented(TopAbs_FORWARD));
1733     B.Add  (NE,VL.Oriented(TopAbs_REVERSED));      
1734     B.Range(NE,CC->FirstParameter(),CC->LastParameter());
1735     Cuts.Append(NE.Oriented(E.Orientation()));
1736     VF = VL;
1737   }
1738
1739   return theCurves.Length();
1740 }
1741
1742 //  Modified by Sergey KHROMOV - Thu Nov 16 17:27:56 2000 Begin
1743 //=======================================================================
1744 //function : CutCurve
1745 //purpose  : 
1746 //=======================================================================
1747
1748 void CutCurve (const Handle(Geom2d_TrimmedCurve)& C,
1749                const Standard_Integer nbParts,
1750                      TColGeom2d_SequenceOfCurve& theCurves)
1751 {
1752   Handle(Geom2d_TrimmedCurve) TrimC;
1753   Standard_Real               UF,UL,UC;
1754   Standard_Real               Step;
1755   gp_Pnt2d                    PF,PL,PC;
1756   Standard_Real               PTol  = Precision::PConfusion()*10;
1757   Standard_Real               Tol   = Precision::Confusion()*10;
1758   Standard_Boolean            YaCut = Standard_False;
1759
1760   UF = C->FirstParameter();
1761   UL = C->LastParameter ();
1762   PF = C->Value(UF);
1763   PL = C->Value(UL);
1764
1765   Step = (UL - UF)/nbParts;
1766
1767   for (Standard_Integer i = 1; i < nbParts; i++) {
1768
1769     UC = UF + i*Step;
1770     PC = C->Value(UC);
1771
1772     if (UC - UF > PTol && PC.Distance(PF) > Tol) {
1773       if ( UL - UC < PTol || PL.Distance(PC) < Tol)
1774         continue;
1775
1776       TrimC = new Geom2d_TrimmedCurve(C,UF,UC);
1777       theCurves.Append(TrimC);
1778       UF = UC;
1779       PF = PC;
1780       YaCut = Standard_True;
1781     }
1782   }
1783   if (YaCut) {
1784     TrimC = new Geom2d_TrimmedCurve(C,UF,UL);
1785     theCurves.Append(TrimC);
1786   } else
1787     theCurves.Append(C);
1788 }
1789 //  Modified by Sergey KHROMOV - Thu Nov 16 17:28:13 2000 End
1790
1791 //=======================================================================
1792 //function : MakeCircle
1793 //purpose  : 
1794 //=======================================================================
1795
1796 void MakeCircle (const TopoDS_Edge&          E,
1797                  const TopoDS_Vertex&        V,
1798                  const TopoDS_Face&          F,
1799                  const Standard_Real         Offset, 
1800                        BRepFill_IndexedDataMapOfOrientedShapeListOfShape& Map,
1801                  const Handle(Geom_Plane)&   RefPlane)
1802 {
1803   // evaluate the Axis of the Circle.
1804   Standard_Real f,l;
1805   Handle(Geom2d_Curve) GC = BRep_Tool::CurveOnSurface(E,F,f,l);
1806   gp_Vec2d DX;
1807   gp_Pnt2d P;
1808
1809   if (E.Orientation() == TopAbs_FORWARD) {
1810     GC->D1(l,P,DX);
1811     DX.Reverse();
1812   }
1813   else GC->D1(f,P,DX);
1814
1815   gp_Ax2d Axis(P,gp_Dir2d(DX));
1816   Handle(Geom2d_Circle) Circ 
1817     = new  Geom2d_Circle(Axis, Abs(Offset), Offset < 0.);
1818
1819   // Bind the edges in my Map.
1820   TopoDS_Edge OE = BRepLib_MakeEdge(Circ, RefPlane);
1821   TopTools_ListOfShape LL;
1822
1823   LL.Append(OE);
1824   Map.Add(V,LL);
1825
1826 #ifdef DRAW
1827   if ( AffichGeom && !OE.IsNull()) {
1828     sprintf(name,"OFFSET_%d",++NbOFFSET);
1829     DBRep::Set(name,OE);
1830   }
1831 #endif
1832 }
1833
1834 //=======================================================================
1835 //function : MakeOffset
1836 //purpose  : 
1837 //=======================================================================
1838
1839 void MakeOffset (const TopoDS_Edge&        E, 
1840                  const TopoDS_Face&        F,
1841                  const Standard_Real       Offset, 
1842                        BRepFill_IndexedDataMapOfOrientedShapeListOfShape& Map,
1843                  const Handle(Geom_Plane)& RefPlane)
1844 {
1845   Standard_Real f,l;
1846   Standard_Real anOffset = Offset;
1847
1848   if (E.Orientation() == TopAbs_REVERSED) anOffset *= -1;
1849
1850   Handle(Geom2d_Curve) G2d = BRep_Tool::CurveOnSurface(E,F,f,l);
1851   Handle(Geom2d_Curve) G2dOC;
1852
1853   Geom2dAdaptor_Curve  AC(G2d,f,l);
1854   if ( AC.GetType() == GeomAbs_Circle) {
1855     // if the offset is greater otr equal to the radius and the side of the  
1856     // concavity of the circle => edge null.
1857     gp_Circ2d C1(AC.Circle());
1858     gp_Ax22d axes( C1.Axis());
1859     gp_Dir2d Xd = axes.XDirection();
1860     gp_Dir2d Yd = axes.YDirection();
1861     Standard_Real Crossed = Xd.X()*Yd.Y()-Xd.Y()*Yd.X();
1862     Standard_Real Signe = ( Crossed > 0.) ? 1. : -1.;
1863
1864     if (anOffset*Signe < AC.Circle().Radius()) {
1865
1866       Handle(Geom2dAdaptor_HCurve) AHC = 
1867         new Geom2dAdaptor_HCurve(G2d);
1868       Adaptor3d_OffsetCurve   Off(AHC,-anOffset);
1869       Handle(Geom2d_Circle) CC = new Geom2d_Circle(Off.Circle());      
1870
1871       Standard_Real Delta = 2*M_PI - l + f;
1872       f -= 0.2*Delta; l += 0.2*Delta;
1873
1874       G2dOC = new Geom2d_TrimmedCurve(CC,f,l);
1875     }
1876   }
1877   else if (AC.GetType() == GeomAbs_Line) {
1878     Handle(Geom2dAdaptor_HCurve) AHC = 
1879       new Geom2dAdaptor_HCurve(G2d);
1880     Adaptor3d_OffsetCurve Off(AHC,anOffset);
1881     Handle(Geom2d_Line)       CC = new Geom2d_Line(Off.Line());
1882     Standard_Real Delta = (l - f);
1883     f -= Delta; l += Delta;
1884     G2dOC = new Geom2d_TrimmedCurve(CC,f,l);
1885   }
1886   else {
1887
1888     anOffset = -anOffset;
1889     Handle(Geom2d_TrimmedCurve) G2dT = new Geom2d_TrimmedCurve(G2d,f,l);
1890     G2dOC = new Geom2d_OffsetCurve( G2dT, anOffset);
1891
1892   }
1893
1894   // Bind the edges in my Map.
1895   if (!G2dOC.IsNull()) {
1896     TopoDS_Edge OE = BRepLib_MakeEdge(G2dOC, RefPlane);
1897     OE.Orientation(E.Orientation());
1898     TopTools_ListOfShape LL;
1899     LL.Append(OE);
1900     Map.Add(E,LL);
1901
1902 #ifdef DRAW  
1903     if (AffichGeom && !OE.IsNull()) {
1904       sprintf(name,"OFFSET_%d",++NbOFFSET);
1905       DBRep::Set(name,OE);
1906       Standard_Real ii = 0;
1907     }
1908 #endif
1909     
1910   }
1911 }  
1912
1913 //=======================================================================
1914 //function : UpdateDetromp
1915 //purpose  : For each interval on bissectrice defined by parameters
1916 //           test if the medium point is at a distance > offset 
1917 //           in this case vertices corresponding to the extremities of the interval
1918 //           are ranked in the proofing.
1919 //           => If the same vertex appears in the proofing, the 
1920 //           border of the zone of proximity is tangent to the offset .
1921 //=======================================================================
1922
1923 void UpdateDetromp (TopTools_ListOfShape&           Detromp1,
1924                     TopTools_ListOfShape&           Detromp2, 
1925                     const TopTools_SequenceOfShape& Vertices, 
1926                     const TColgp_SequenceOfPnt&     Params, 
1927                     const Bisector_Bisec&           Bisec,
1928                     const Standard_Boolean          SOnE,
1929                     const Standard_Boolean          EOnE,
1930                     const BRepFill_TrimEdgeTool&    Trim)
1931 {
1932   Standard_Integer ii = 1;
1933   Standard_Real    U1,U2;
1934   TopoDS_Vertex    V1,V2;
1935
1936   Handle(Geom2d_Curve) Bis = Bisec.Value();
1937
1938   U1 = Bis->FirstParameter();
1939   
1940   if (SOnE) { 
1941     // the first point of the bissectrice is on the offset
1942     V1 = TopoDS::Vertex(Vertices.Value(ii));
1943     ii++; 
1944   }
1945
1946   while (ii <= Vertices.Length()) {
1947     U2 = Params.Value(ii).X();
1948     V2 = TopoDS::Vertex(Vertices.Value(ii));
1949
1950     gp_Pnt2d P = Bis->Value((U2 + U1)*0.5);  
1951     if (!Trim.IsInside(P)) {
1952       if (!V1.IsNull()) {
1953           Detromp1.Append(V1);
1954           Detromp2.Append(V1);
1955       }
1956       Detromp1.Append(V2);
1957       Detromp2.Append(V2);
1958     }
1959     U1 = U2;
1960     V1 = V2;
1961     ii ++;
1962   }
1963
1964   // test medium point between the last parameter and the end of the bissectrice.
1965   U2 = Bis->LastParameter();
1966   if (!EOnE) {
1967     if (!Precision::IsInfinite(U2)) {
1968       gp_Pnt2d P = Bis->Value((U2 + U1)*0.5);  
1969       if (!Trim.IsInside(P)) {
1970         if (!V1.IsNull()) {
1971           Detromp1.Append(V1);
1972           Detromp2.Append(V1);
1973         }
1974       }
1975     }
1976     else {
1977       if (!V1.IsNull()) {
1978         Detromp1.Append(V1);
1979         Detromp2.Append(V1);
1980       }
1981     }
1982   }    
1983 }
1984
1985 //=======================================================================
1986 //function : VertexFromNode
1987 //purpose  : 
1988 //=======================================================================
1989
1990 Standard_Boolean VertexFromNode (const Handle(MAT_Node)&      aNode, 
1991                                  const Standard_Real          Offset,
1992                                   gp_Pnt2d&                   PN,
1993                                  BRepFill_DataMapOfNodeShape& MapNodeVertex,
1994                                  TopoDS_Vertex&               VN)
1995 {  
1996   Standard_Boolean Status;
1997   Standard_Real    Tol = Precision::Confusion();
1998   BRep_Builder     B;
1999
2000   if (!aNode->Infinite() && Abs(aNode->Distance()-Offset) < Tol) {
2001     //------------------------------------------------
2002     // the Node gives a vertex on the offset
2003     //------------------------------------------------
2004     if (MapNodeVertex.IsBound(aNode)) {
2005       VN = TopoDS::Vertex(MapNodeVertex(aNode));
2006     }
2007     else { 
2008       gp_Pnt P(PN.X(),PN.Y(),0.);
2009       B.MakeVertex (VN);
2010       B.UpdateVertex(VN,P, Precision::Confusion());
2011       MapNodeVertex.Bind(aNode,VN);
2012     }
2013     Status = Standard_True;
2014   }
2015   else Status = Standard_False;
2016
2017   return Status;
2018 }
2019
2020
2021 //=======================================================================
2022 //function : StoreInMap
2023 //purpose  : 
2024 //=======================================================================
2025
2026 void StoreInMap (const TopoDS_Shape& V1,
2027                  const TopoDS_Shape& V2,
2028                  TopTools_IndexedDataMapOfShapeShape& MapVV)
2029 {
2030   TopoDS_Shape OldV = V1, NewV = V2;
2031   Standard_Integer i;
2032
2033   if (MapVV.Contains(V2))
2034     NewV = MapVV.FindFromKey(V2);
2035
2036   if (MapVV.Contains(V1))
2037     MapVV.ChangeFromKey(V1) = NewV;
2038
2039   for (i = 1; i <= MapVV.Extent(); i++)
2040     if (MapVV(i).IsSame(V1))
2041       MapVV(i) = NewV;
2042
2043   MapVV.Add(V1, NewV);
2044 }
2045
2046 //=======================================================================
2047 //function : TrimEdge
2048 //purpose  : 
2049 //=======================================================================
2050
2051 void TrimEdge (const TopoDS_Edge&              E,
2052                const TopTools_ListOfShape&     Detromp,
2053                      TopTools_SequenceOfShape& TheVer,
2054                      TColStd_SequenceOfReal&   ThePar,
2055                      TopTools_SequenceOfShape& S,
2056                      TopTools_IndexedDataMapOfShapeShape& MapVV)
2057 {
2058   Standard_Boolean         Change = Standard_True;
2059   BRep_Builder             TheBuilder;
2060   S.Clear();
2061
2062   //-----------------------------------------------------------
2063   // Parse two sequences depending on the parameter on the edge.
2064   //-----------------------------------------------------------
2065   while (Change) {
2066     Change = Standard_False;
2067     for (Standard_Integer i = 1; i < ThePar.Length(); i++) {
2068       if (ThePar.Value(i) > ThePar.Value(i+1)) {
2069         ThePar.Exchange(i,i+1);
2070         TheVer.Exchange(i,i+1);
2071         Change = Standard_True;
2072       }
2073     }
2074   }
2075
2076   //----------------------------------------------------------
2077   // If a vertex is not in the proofing, it is eliminated.
2078   //----------------------------------------------------------
2079   if (!BRep_Tool::Degenerated(E)) {
2080     for (Standard_Integer k = 1; k <= TheVer.Length(); k ++) {
2081       if ( DoubleOrNotInside (Detromp,
2082                               TopoDS::Vertex(TheVer.Value(k)))) {
2083         TheVer.Remove(k);
2084         ThePar.Remove(k);
2085         k--;
2086       }
2087     }
2088   }
2089
2090   //----------------------------------------------------------
2091   // If a vertex_double appears twice in the proofing 
2092   // the vertex is removed.
2093   // otherwise preserve only one of its representations.
2094   //----------------------------------------------------------
2095   if (!BRep_Tool::Degenerated(E)) {
2096     for (Standard_Integer k = 1; k < TheVer.Length(); k ++) {
2097       if (TheVer.Value(k).IsSame(TheVer.Value(k+1)) || 
2098          Abs(ThePar.Value(k)-ThePar.Value(k+1)) <= Precision::PConfusion()) {
2099
2100         if(k+1 == TheVer.Length()) {
2101           StoreInMap(TheVer(k), TheVer(k+1), MapVV);
2102           TheVer.Remove(k);
2103           ThePar.Remove(k);
2104         }
2105         else {
2106           StoreInMap(TheVer(k+1), TheVer(k), MapVV);
2107           TheVer.Remove(k+1);
2108           ThePar.Remove(k+1);
2109         }
2110         /*
2111         if ( DoubleOrNotInside (Detromp,
2112                                 TopoDS::Vertex(TheVer.Value(k)))) {
2113           TheVer.Remove(k);
2114           ThePar.Remove(k);
2115           k--;
2116         }
2117         */
2118         k--;
2119       }
2120     }
2121   }
2122   //-----------------------------------------------------------
2123   // Creation of edges.
2124   // the number of vertices should be even. The created edges  
2125   // go from a vertex with uneven index i to vertex i+1;
2126   //-----------------------------------------------------------
2127   for (Standard_Integer k = 1; k < TheVer.Length(); k = k+2) {
2128     TopoDS_Shape aLocalShape = E.EmptyCopied();
2129     TopoDS_Edge NewEdge = TopoDS::Edge(aLocalShape);
2130 //    TopoDS_Edge NewEdge = TopoDS::Edge(E.EmptyCopied());
2131
2132     if (NewEdge.Orientation() == TopAbs_REVERSED) {
2133       TheBuilder.Add  (NewEdge,TheVer.Value(k)  .Oriented(TopAbs_REVERSED));
2134       TheBuilder.Add  (NewEdge,TheVer.Value(k+1).Oriented(TopAbs_FORWARD));
2135     }
2136     else {      
2137       TheBuilder.Add  (NewEdge,TheVer.Value(k)  .Oriented(TopAbs_FORWARD));
2138       TheBuilder.Add  (NewEdge,TheVer.Value(k+1).Oriented(TopAbs_REVERSED));
2139     }
2140
2141
2142     TheBuilder.Range(NewEdge,ThePar.Value(k),ThePar.Value(k+1));
2143
2144 #ifdef DRAW
2145     if ( AffichEdge) {
2146       sprintf(name,"TRIMEDGE_%d",NbTRIMEDGES);
2147       DBRep::Set(name,NewEdge);  
2148     }
2149     if (Affich2d) {
2150       TopLoc_Location L;
2151       Standard_Real f,l;
2152       Handle(Geom_Surface) Surf;  
2153       Handle(Geom2d_Curve) C;
2154       BRep_Tool::CurveOnSurface(NewEdge,C,Surf,L,f,l);
2155       sprintf(name,"OFFSET2d_%d",NbTRIMEDGES++);
2156       Handle(Geom2d_TrimmedCurve) C2d = new Geom2d_TrimmedCurve(C,f,l);
2157       Handle(DrawTrSurf_Curve2d) dr =
2158         new DrawTrSurf_Curve2d(C2d,Standard_False);
2159       dr->SetColor(Draw_bleu);
2160       Draw::Set(name,dr);
2161     }
2162 #endif
2163
2164     S.Append(NewEdge);
2165   }
2166 }
2167
2168 //=======================================================================
2169 //function : DoubleOrNotInside
2170 //purpose  : return True if V appears twice in LV or is not inside.
2171 //=======================================================================
2172
2173 Standard_Boolean DoubleOrNotInside (const TopTools_ListOfShape& LV,
2174                                     const TopoDS_Vertex&        V)
2175 {  
2176   Standard_Boolean Vu = Standard_False;
2177   TopTools_ListIteratorOfListOfShape it(LV);
2178
2179   for ( ; it.More(); it.Next()) {
2180     if (V.IsSame(it.Value())) {
2181       if  (Vu) return Standard_True;
2182       else       Vu = Standard_True;
2183     }
2184   }
2185   if (Vu) return Standard_False;
2186   else    return Standard_True;   
2187 }
2188
2189 Standard_Boolean IsSmallClosedEdge(const TopoDS_Edge& anEdge,
2190                                    const TopoDS_Vertex& aVertex)
2191 {
2192   gp_Pnt PV = BRep_Tool::Pnt(aVertex);
2193   gp_Pnt2d PV2d, Pfirst, Plast, Pmid;
2194   PV2d.SetCoord( PV.X(), PV.Y() );
2195
2196   Handle(Geom2d_Curve) PCurve;
2197   Handle( BRep_CurveRepresentation ) CurveRep =
2198     ((Handle(BRep_TEdge)::DownCast(anEdge.TShape()))->Curves()).First();
2199   PCurve = CurveRep->PCurve();
2200
2201   Standard_Real fpar = (Handle(BRep_GCurve)::DownCast(CurveRep))->First();
2202   Standard_Real lpar = (Handle(BRep_GCurve)::DownCast(CurveRep))->Last();
2203   Pfirst = PCurve->Value(fpar);
2204   Plast  = PCurve->Value(lpar);
2205   Pmid   = PCurve->Value((fpar + lpar)*0.5);
2206
2207   Standard_Real theTol = BRep_Tool::Tolerance(aVertex);
2208   theTol *= 1.5;
2209
2210   Standard_Real dist1 = Pfirst.Distance(PV2d);
2211   Standard_Real dist2 = Plast.Distance(PV2d);
2212   Standard_Real dist3 = Pmid.Distance(PV2d);
2213
2214   if (dist1 <= theTol && dist2 <= theTol && dist3 <= theTol)
2215     return Standard_True;
2216
2217   return Standard_False;
2218 }
2219
2220 static void CheckBadEdges(const TopoDS_Face& Spine, const Standard_Real Offset,
2221                           const BRepMAT2d_BisectingLocus& Locus, 
2222                           const BRepMAT2d_LinkTopoBilo&   Link,
2223                           TopTools_ListOfShape& BadEdges)
2224 {
2225
2226   TopoDS_Face F = TopoDS::Face(Spine.Oriented(TopAbs_FORWARD));
2227   Standard_Real eps = Precision::Confusion(); 
2228   Standard_Real LimCurv = 1./Offset;
2229
2230   TopTools_MapOfShape aMap;
2231
2232   for (Standard_Integer ic = 1; ic <= Locus.NumberOfContours(); ic++) {
2233     for (Standard_Integer ie = 1; ie <= Locus.NumberOfElts(ic); ie++) {
2234       const TopoDS_Shape& SE = Link.GeneratingShape(Locus.BasicElt(ic,ie));
2235       if (SE.ShapeType() == TopAbs_EDGE) {
2236
2237         if (aMap.Contains(SE)) {
2238           //cout << "Edge is treated second time" << endl;
2239           continue;
2240         }
2241
2242         TopoDS_Edge E = TopoDS::Edge(SE);
2243
2244         Standard_Real f,l;
2245
2246         Handle(Geom2d_Curve) G2d = BRep_Tool::CurveOnSurface(E,F,f,l);
2247
2248         Geom2dAdaptor_Curve  AC(G2d,f,l);
2249         GeomAbs_CurveType aCType = AC.GetType();
2250
2251         if(aCType != GeomAbs_Line && aCType != GeomAbs_Circle) {
2252
2253           Standard_Boolean reverse = Standard_False;
2254           if (E.Orientation() == TopAbs_FORWARD) reverse = Standard_True;
2255
2256           gp_Pnt2d P, Pc;
2257           gp_Dir2d N;
2258
2259           Geom2dLProp_CLProps2d aCLProps(G2d, 2, eps);
2260
2261           aCLProps.SetParameter(f);
2262           if(!aCLProps.IsTangentDefined()) {
2263             BadEdges.Append(SE);
2264             aMap.Add(SE);
2265             continue;
2266           }
2267
2268           P = aCLProps.Value();
2269           Standard_Real Crv = aCLProps.Curvature();
2270
2271           if(Crv >= eps) {
2272             aCLProps.Tangent(N);
2273             Standard_Real x = N.Y(), y = -N.X();
2274             N.SetCoord(x, y);
2275             if (reverse) N.Reverse();
2276             aCLProps.CentreOfCurvature(Pc);
2277             gp_Vec2d Dir( P, Pc );
2278             if (N.Dot(Dir) > 0.) {
2279               if (LimCurv <= Crv + eps) {
2280                 BadEdges.Append(SE);
2281                 aMap.Add(SE);
2282                 continue;
2283               }
2284             }
2285           }  
2286
2287           aCLProps.SetParameter(l);
2288           if(!aCLProps.IsTangentDefined()) {
2289             BadEdges.Append(SE);
2290             aMap.Add(SE);
2291             continue;
2292           }
2293
2294           P = aCLProps.Value();
2295           Crv = aCLProps.Curvature();
2296
2297           if(Crv >= eps) {
2298             aCLProps.Tangent(N);
2299             Standard_Real x = N.Y(), y = -N.X();
2300             N.SetCoord(x, y);
2301             if (reverse) N.Reverse();
2302             aCLProps.CentreOfCurvature(Pc);
2303             gp_Vec2d Dir( P, Pc );
2304             if (N.Dot(Dir) > 0.) {
2305               if (LimCurv <= Crv + eps) {
2306                 BadEdges.Append(SE);
2307                 aMap.Add(SE);
2308                 continue;
2309               }
2310             }
2311           }  
2312         }
2313       }
2314     }
2315   }
2316 }
2317
2318
2319 //=======================================================================
2320 //function : PerformCurve
2321 //purpose  : 
2322 //=======================================================================
2323
2324 static Standard_Boolean PerformCurve (TColStd_SequenceOfReal& Parameters,
2325                                       TColgp_SequenceOfPnt&   Points,
2326                                       const Adaptor3d_Curve& C, 
2327                                       const Standard_Real Deflection,
2328                                       const Standard_Real U1,
2329                                       const Standard_Real U2,
2330                                       const Standard_Real EPSILON,
2331                                       const Standard_Integer Nbmin)
2332 {
2333   Standard_Real UU1 = Min(U1, U2);
2334   Standard_Real UU2 = Max(U1, U2);
2335
2336   gp_Pnt Pdeb, Pfin;
2337   gp_Vec Ddeb,Dfin;
2338   C.D1(UU1,Pdeb,Ddeb);
2339   Parameters.Append(UU1);
2340   Points.Append(Pdeb);
2341
2342   C.D1(UU2,Pfin,Dfin);
2343
2344   const Standard_Real aDelta = UU2 - UU1;
2345   const Standard_Real aDist = Pdeb.Distance(Pfin);
2346
2347   if((aDelta/aDist) > 5.0e-14)
2348   {
2349     QuasiFleche(C,Deflection*Deflection,
2350                       UU1,Pdeb,
2351                       Ddeb,
2352                       UU2,Pfin,
2353                       Dfin,
2354                       Nbmin,
2355                       EPSILON*EPSILON,
2356                       Parameters,Points);
2357   }
2358
2359   return Standard_True;
2360 }
2361 //=======================================================================
2362 //function : QuasiFleche
2363 //purpose  : 
2364 //=======================================================================
2365
2366 static void QuasiFleche(const Adaptor3d_Curve& C,
2367                         const Standard_Real Deflection2, 
2368                         const Standard_Real Udeb,
2369                         const gp_Pnt& Pdeb,
2370                         const gp_Vec& Vdeb,
2371                         const Standard_Real Ufin,
2372                         const gp_Pnt& Pfin,
2373                         const gp_Vec& Vfin,
2374                         const Standard_Integer Nbmin,
2375                         const Standard_Real Eps,
2376                         TColStd_SequenceOfReal& Parameters,
2377                         TColgp_SequenceOfPnt& Points)
2378 {
2379   Standard_Integer Ptslength = Points.Length();
2380   Standard_Real Udelta = Ufin-Udeb;
2381   gp_Pnt Pdelta;
2382   gp_Vec Vdelta;
2383   if (Nbmin > 2) {
2384     Udelta /=(Nbmin-1);
2385     C.D1(Udeb+Udelta,Pdelta,Vdelta);
2386   }
2387   else {
2388     Pdelta = Pfin;
2389     Vdelta = Vfin;
2390   }
2391
2392
2393   Standard_Real Norme = gp_Vec(Pdeb,Pdelta).SquareMagnitude();
2394   Standard_Real theFleche=0;
2395   Standard_Boolean flecheok = Standard_False;
2396   if (Norme > Eps) { 
2397     // Evaluation of the arrow by interpolation. See IntWalk_IWalking_5.gxx
2398     Standard_Real N1 = Vdeb.SquareMagnitude();
2399     Standard_Real N2 = Vdelta.SquareMagnitude();
2400     if (N1 > Eps && N2 > Eps) {
2401       Standard_Real Normediff = 
2402         (Vdeb.Normalized().XYZ()-Vdelta.Normalized().XYZ()).SquareModulus();
2403       if (Normediff > Eps) {
2404         theFleche = Normediff*Norme/64.;
2405         flecheok = Standard_True;
2406       }
2407     }
2408   }
2409   if (!flecheok) {
2410     gp_Pnt Pmid((Pdeb.XYZ()+Pdelta.XYZ())/2.);
2411     gp_Pnt Pverif(C.Value(Udeb+Udelta/2.));
2412     theFleche = Pmid.SquareDistance(Pverif);
2413   }
2414
2415   if (theFleche < Deflection2) {
2416     Parameters.Append(Udeb+Udelta);
2417     Points.Append(Pdelta);
2418   }
2419   else {
2420     QuasiFleche(C,Deflection2,Udeb,Pdeb,
2421                 Vdeb,
2422                 Udeb+Udelta,Pdelta,
2423                 Vdelta,
2424                 3,
2425                 Eps,
2426                 Parameters,Points);
2427
2428   }
2429
2430   if (Nbmin > 2) {
2431     QuasiFleche(C,Deflection2,Udeb+Udelta,Pdelta,
2432                 Vdelta,
2433                 Ufin,Pfin,
2434                 Vfin,
2435                 Nbmin-(Points.Length()-Ptslength),
2436                 Eps,
2437                 Parameters,Points);
2438   }
2439 }
2440