0024313: BRepAlgoAPI_Section and IntTools_FaceFace aren't written to handle Geom_Surf...
[occt.git] / src / IntTools / IntTools_FaceFace.cxx
1 // Created on: 2000-11-23
2 // Created by: Michael KLOKOV
3 // Copyright (c) 2000-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21
22 #include <IntTools_FaceFace.ixx>
23
24 #include <Precision.hxx>
25
26 #include <TColStd_HArray1OfReal.hxx>
27 #include <TColStd_Array1OfReal.hxx>
28 #include <TColStd_Array1OfInteger.hxx>
29 #include <TColStd_SequenceOfReal.hxx>
30 #include <TColStd_ListOfInteger.hxx>
31 #include <TColStd_ListIteratorOfListOfInteger.hxx>
32 #include <TColStd_Array1OfListOfInteger.hxx>
33
34 #include <gp_Lin2d.hxx>
35 #include <gp_Ax22d.hxx>
36 #include <gp_Circ2d.hxx>
37 #include <gp_Torus.hxx>
38 #include <gp_Cylinder.hxx>
39
40 #include <Bnd_Box.hxx>
41
42 #include <TColgp_HArray1OfPnt2d.hxx>
43 #include <TColgp_SequenceOfPnt2d.hxx>
44 #include <TColgp_Array1OfPnt.hxx>
45 #include <TColgp_Array1OfPnt2d.hxx>
46
47 #include <IntAna_QuadQuadGeo.hxx>
48
49 #include <IntSurf_PntOn2S.hxx>
50 #include <IntSurf_LineOn2S.hxx>
51 #include <IntSurf_PntOn2S.hxx>
52 #include <IntSurf_ListOfPntOn2S.hxx>
53 #include <IntRes2d_Domain.hxx>
54 #include <ProjLib_Plane.hxx>
55
56 #include <IntPatch_GLine.hxx>
57 #include <IntPatch_RLine.hxx>
58 #include <IntPatch_WLine.hxx>
59 #include <IntPatch_ALine.hxx>
60 #include <IntPatch_ALineToWLine.hxx>
61
62 #include <ElSLib.hxx>
63 #include <ElCLib.hxx>
64
65 #include <Extrema_ExtCC.hxx>
66 #include <Extrema_POnCurv.hxx>
67 #include <BndLib_AddSurface.hxx>
68
69 #include <Adaptor3d_SurfacePtr.hxx>
70 #include <Adaptor2d_HLine2d.hxx>
71
72 #include <GeomAbs_SurfaceType.hxx>
73 #include <GeomAbs_CurveType.hxx>
74
75 #include <Geom_Surface.hxx>
76 #include <Geom_Line.hxx>
77 #include <Geom_Circle.hxx>
78 #include <Geom_Ellipse.hxx>
79 #include <Geom_Parabola.hxx>
80 #include <Geom_Hyperbola.hxx>
81 #include <Geom_TrimmedCurve.hxx>
82 #include <Geom_BSplineCurve.hxx>
83 #include <Geom_RectangularTrimmedSurface.hxx>
84 #include <Geom_OffsetSurface.hxx>
85 #include <Geom_Curve.hxx>
86 #include <Geom_Conic.hxx>
87
88 #include <Geom2d_TrimmedCurve.hxx>
89 #include <Geom2d_BSplineCurve.hxx>
90 #include <Geom2d_Line.hxx>
91 #include <Geom2d_Curve.hxx>
92 #include <Geom2d_Circle.hxx>
93
94 #include <Geom2dAPI_InterCurveCurve.hxx>
95 #include <Geom2dInt_GInter.hxx>
96 #include <GeomAdaptor_Curve.hxx>
97 #include <GeomAdaptor_HSurface.hxx>
98 #include <GeomAdaptor_Surface.hxx>
99 #include <GeomLib_CheckBSplineCurve.hxx>
100 #include <GeomLib_Check2dBSplineCurve.hxx>
101
102 #include <GeomInt_WLApprox.hxx>
103 #include <GeomProjLib.hxx>
104 #include <GeomAPI_ProjectPointOnSurf.hxx>
105 #include <Geom2dAdaptor_Curve.hxx>
106 #include <TopoDS.hxx>
107 #include <TopoDS_Edge.hxx>
108 #include <TopExp_Explorer.hxx>
109
110 #include <BRep_Tool.hxx>
111 #include <BRepTools.hxx>
112 #include <BRepAdaptor_Surface.hxx>
113
114 #include <IntTools_Curve.hxx>
115 #include <IntTools_Tools.hxx>
116 #include <IntTools_Tools.hxx>
117 #include <IntTools_TopolTool.hxx>
118 #include <IntTools_PntOnFace.hxx>
119 #include <IntTools_PntOn2Faces.hxx>
120 #include <BOPInt_Context.hxx>
121 #include <IntSurf_ListIteratorOfListOfPntOn2S.hxx>
122
123 static
124   void RefineVector(gp_Vec2d& aV2D);
125 #ifdef DEB_DUMPWLINE
126 static
127   void DumpWLine(const Handle(IntPatch_WLine)& aWLine);
128 #endif
129 //
130 static
131   void TolR3d(const TopoDS_Face& ,
132               const TopoDS_Face& ,
133               Standard_Real& );
134 static 
135   Handle(Geom_Curve) MakeBSpline (const Handle(IntPatch_WLine)&,
136                                   const Standard_Integer,
137                                   const Standard_Integer);
138
139 static 
140   void Parameters(const Handle(GeomAdaptor_HSurface)&,
141                   const Handle(GeomAdaptor_HSurface)&,
142                   const gp_Pnt&,
143                   Standard_Real&,
144                   Standard_Real&,
145                   Standard_Real&,
146                   Standard_Real&);
147
148 static 
149   void BuildPCurves (Standard_Real f,Standard_Real l,Standard_Real& Tol,
150                      const Handle (Geom_Surface)& S,
151                      const Handle (Geom_Curve)&   C,
152                      Handle (Geom2d_Curve)& C2d);
153
154 static 
155   void CorrectSurfaceBoundaries(const TopoDS_Face&  theFace,
156                                 const Standard_Real theTolerance,
157                                 Standard_Real&      theumin,
158                                 Standard_Real&      theumax, 
159                                 Standard_Real&      thevmin, 
160                                 Standard_Real&      thevmax);
161 static
162   Standard_Boolean NotUseSurfacesForApprox
163           (const TopoDS_Face& aF1,
164            const TopoDS_Face& aF2,
165            const Handle(IntPatch_WLine)& WL,
166            const Standard_Integer ifprm,
167            const Standard_Integer ilprm);
168
169 static 
170   Handle(IntPatch_WLine) ComputePurgedWLine(const Handle(IntPatch_WLine)& theWLine);
171
172 static 
173   Standard_Real AdjustPeriodic(const Standard_Real theParameter,
174                                const Standard_Real parmin,
175                                const Standard_Real parmax,
176                                const Standard_Real thePeriod,
177                                Standard_Real&      theOffset);
178
179 static 
180   Handle(Geom2d_BSplineCurve) MakeBSpline2d(const Handle(IntPatch_WLine)& theWLine,
181                                             const Standard_Integer                         ideb,
182                                             const Standard_Integer                         ifin,
183                                             const Standard_Boolean                         onFirst);
184
185 static 
186   Standard_Boolean DecompositionOfWLine(const Handle(IntPatch_WLine)& theWLine,
187                                         const Handle(GeomAdaptor_HSurface)&            theSurface1, 
188                                         const Handle(GeomAdaptor_HSurface)&            theSurface2,
189                                         const TopoDS_Face&                             theFace1,
190                                         const TopoDS_Face&                             theFace2,
191                                         const IntTools_LineConstructor&                theLConstructor,
192                                         const Standard_Boolean                         theAvoidLConstructor,
193                                         IntPatch_SequenceOfLine&                       theNewLines,
194                                         Standard_Real&                                 theReachedTol3d,
195                                         const Handle(BOPInt_Context)& );
196
197 static 
198   Standard_Boolean ParameterOutOfBoundary(const Standard_Real       theParameter, 
199                                           const Handle(Geom_Curve)& theCurve, 
200                                           const TopoDS_Face&        theFace1, 
201                                           const TopoDS_Face&        theFace2,
202                                           const Standard_Real       theOtherParameter,
203                                           const Standard_Boolean    bIncreasePar,
204                                           Standard_Real&            theNewParameter,
205                                           const Handle(BOPInt_Context)& );
206
207 static 
208   Standard_Boolean IsCurveValid(Handle(Geom2d_Curve)& thePCurve);
209
210 static 
211   Standard_Boolean IsPointOnBoundary(const Standard_Real theParameter,
212                                      const Standard_Real theFirstBoundary,
213                                      const Standard_Real theSecondBoundary,
214                                      const Standard_Real theResolution,
215                                      Standard_Boolean&   IsOnFirstBoundary);
216 static
217   Standard_Boolean FindPoint(const gp_Pnt2d&     theFirstPoint,
218                              const gp_Pnt2d&     theLastPoint,
219                              const Standard_Real theUmin, 
220                              const Standard_Real theUmax,
221                              const Standard_Real theVmin,
222                              const Standard_Real theVmax,
223                              gp_Pnt2d&           theNewPoint);
224
225
226 static 
227   Standard_Integer ComputeTangentZones( const Handle(GeomAdaptor_HSurface)& theSurface1,
228                                        const Handle(GeomAdaptor_HSurface)&  theSurface2,
229                                        const TopoDS_Face&                   theFace1,
230                                        const TopoDS_Face&                   theFace2,
231                                        Handle(TColgp_HArray1OfPnt2d)&       theResultOnS1,
232                                        Handle(TColgp_HArray1OfPnt2d)&       theResultOnS2,
233                                        Handle(TColStd_HArray1OfReal)&       theResultRadius,
234                                        const Handle(BOPInt_Context)& );
235
236 static
237   Standard_Boolean FindPoint(const gp_Pnt2d&     theFirstPoint,
238                              const gp_Pnt2d&     theLastPoint,
239                              const Standard_Real theUmin, 
240                              const Standard_Real theUmax,
241                              const Standard_Real theVmin,
242                              const Standard_Real theVmax,
243                              const gp_Pnt2d&     theTanZoneCenter,
244                              const Standard_Real theZoneRadius,
245                              Handle(GeomAdaptor_HSurface) theGASurface,
246                              gp_Pnt2d&           theNewPoint);
247
248 static
249   Standard_Boolean IsInsideTanZone(const gp_Pnt2d&     thePoint,
250                                    const gp_Pnt2d&     theTanZoneCenter,
251                                    const Standard_Real theZoneRadius,
252                                    Handle(GeomAdaptor_HSurface) theGASurface);
253
254 static
255   gp_Pnt2d AdjustByNeighbour(const gp_Pnt2d&     theaNeighbourPoint,
256                              const gp_Pnt2d&     theOriginalPoint,
257                              Handle(GeomAdaptor_HSurface) theGASurface);
258 static
259   Standard_Boolean  ApproxWithPCurves(const gp_Cylinder& theCyl, 
260                                       const gp_Sphere& theSph);
261
262 static void  PerformPlanes(const Handle(GeomAdaptor_HSurface)& theS1, 
263                            const Handle(GeomAdaptor_HSurface)& theS2, 
264                            const Standard_Real TolAng, 
265                            const Standard_Real TolTang, 
266                            const Standard_Boolean theApprox1,
267                            const Standard_Boolean theApprox2,
268                            IntTools_SequenceOfCurves& theSeqOfCurve, 
269                            Standard_Boolean& theTangentFaces);
270
271 static Standard_Boolean ClassifyLin2d(const Handle(GeomAdaptor_HSurface)& theS, 
272                                       const gp_Lin2d& theLin2d, 
273                                       const Standard_Real theTol,
274                                       Standard_Real& theP1, 
275                                       Standard_Real& theP2);
276 //
277 static
278   void ApproxParameters(const Handle(GeomAdaptor_HSurface)& aHS1,
279                         const Handle(GeomAdaptor_HSurface)& aHS2,
280                         Standard_Integer& iDegMin,
281                         Standard_Integer& iNbIter,
282                         Standard_Integer& iDegMax);
283
284 static
285   void Tolerances(const Handle(GeomAdaptor_HSurface)& aHS1,
286                   const Handle(GeomAdaptor_HSurface)& aHS2,
287                   Standard_Real& aTolTang);
288
289 static
290   Standard_Boolean SortTypes(const GeomAbs_SurfaceType aType1,
291                              const GeomAbs_SurfaceType aType2);
292 static
293   Standard_Integer IndexType(const GeomAbs_SurfaceType aType);
294
295 //
296 static
297   Standard_Real MaxSquareDistance (const Standard_Real aT,
298                                    const Handle(Geom_Curve)& aC3D,
299                                    const Handle(Geom2d_Curve)& aC2D1,
300                                    const Handle(Geom2d_Curve)& aC2D2,
301                                    const Handle(GeomAdaptor_HSurface) myHS1,
302                                    const Handle(GeomAdaptor_HSurface) myHS2,
303                                    const TopoDS_Face& aF1,
304                                    const TopoDS_Face& aF2,
305                                    const Handle(BOPInt_Context)& aCtx);
306
307 static
308   Standard_Boolean CheckPCurve(const Handle(Geom2d_Curve)& aPC, 
309                                const TopoDS_Face& aFace);
310
311 //
312 static
313   Standard_Real FindMaxSquareDistance (const Standard_Real aA,
314                                        const Standard_Real aB,
315                                        const Standard_Real aEps,
316                                        const Handle(Geom_Curve)& aC3D,
317                                        const Handle(Geom2d_Curve)& aC2D1,
318                                        const Handle(Geom2d_Curve)& aC2D2,
319                                        const Handle(GeomAdaptor_HSurface)& myHS1,
320                                        const Handle(GeomAdaptor_HSurface)& myHS2,
321                                        const TopoDS_Face& aF1,
322                                        const TopoDS_Face& aF2,
323                                        const Handle(BOPInt_Context)& aCtx);
324
325 //=======================================================================
326 //function : 
327 //purpose  : 
328 //=======================================================================
329 IntTools_FaceFace::IntTools_FaceFace()
330 {
331   myIsDone=Standard_False;
332   myTangentFaces=Standard_False;
333   //
334   myHS1 = new GeomAdaptor_HSurface ();
335   myHS2 = new GeomAdaptor_HSurface ();
336   myTolReached2d=0.; 
337   myTolReached3d=0.;
338   SetParameters(Standard_True, Standard_True, Standard_True, 1.e-07);
339   
340 }
341 //=======================================================================
342 //function : SetContext
343 //purpose  : 
344 //======================================================================= 
345 void IntTools_FaceFace::SetContext(const Handle(BOPInt_Context)& aContext)
346 {
347   myContext=aContext;
348 }
349 //=======================================================================
350 //function : Context
351 //purpose  : 
352 //======================================================================= 
353 const Handle(BOPInt_Context)& IntTools_FaceFace::Context()const
354 {
355   return myContext;
356 }
357 //=======================================================================
358 //function : Face1
359 //purpose  : 
360 //======================================================================= 
361 const TopoDS_Face& IntTools_FaceFace::Face1() const
362 {
363   return myFace1;
364 }
365 //=======================================================================
366 //function : Face2
367 //purpose  : 
368 //======================================================================= 
369 const TopoDS_Face& IntTools_FaceFace::Face2() const
370 {
371   return myFace2;
372 }
373 //=======================================================================
374 //function : TangentFaces
375 //purpose  : 
376 //======================================================================= 
377 Standard_Boolean IntTools_FaceFace::TangentFaces() const
378 {
379   return myTangentFaces;
380 }
381 //=======================================================================
382 //function : Points
383 //purpose  : 
384 //======================================================================= 
385 const IntTools_SequenceOfPntOn2Faces& IntTools_FaceFace::Points() const
386 {
387   return myPnts;
388 }
389 //=======================================================================
390 //function : IsDone
391 //purpose  : 
392 //======================================================================= 
393 Standard_Boolean IntTools_FaceFace::IsDone() const
394 {
395   return myIsDone;
396 }
397 //=======================================================================
398 //function : TolReached3d
399 //purpose  : 
400 //=======================================================================
401 Standard_Real IntTools_FaceFace::TolReached3d() const
402 {
403   return myTolReached3d;
404 }
405 //=======================================================================
406 //function : Lines
407 //purpose  : return lines of intersection
408 //=======================================================================
409 const IntTools_SequenceOfCurves& IntTools_FaceFace::Lines() const
410 {
411   StdFail_NotDone_Raise_if
412     (!myIsDone,
413      "IntTools_FaceFace::Lines() => myIntersector NOT DONE");
414   return mySeqOfCurve;
415 }
416 //=======================================================================
417 //function : TolReached2d
418 //purpose  : 
419 //=======================================================================
420 Standard_Real IntTools_FaceFace::TolReached2d() const
421 {
422   return myTolReached2d;
423 }
424 // =======================================================================
425 // function: SetParameters
426 //
427 // =======================================================================
428 void IntTools_FaceFace::SetParameters(const Standard_Boolean ToApproxC3d,
429                                       const Standard_Boolean ToApproxC2dOnS1,
430                                       const Standard_Boolean ToApproxC2dOnS2,
431                                       const Standard_Real ApproximationTolerance) 
432 {
433   myApprox = ToApproxC3d;
434   myApprox1 = ToApproxC2dOnS1;
435   myApprox2 = ToApproxC2dOnS2;
436   myTolApprox = ApproximationTolerance;
437 }
438 //=======================================================================
439 //function : SetList
440 //purpose  : 
441 //=======================================================================
442 void IntTools_FaceFace::SetList(IntSurf_ListOfPntOn2S& aListOfPnts)
443 {
444   myListOfPnts = aListOfPnts;  
445 }
446
447
448 static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
449                                         const TopoDS_Face& theF2)
450 {
451   const Standard_Real Tolang = 1.e-8;
452   const Standard_Real aTolF1=BRep_Tool::Tolerance(theF1);
453   const Standard_Real aTolF2=BRep_Tool::Tolerance(theF2);
454   const Standard_Real aTolSum = aTolF1 + aTolF2;
455   Standard_Real aHigh = 0.0;
456
457   const BRepAdaptor_Surface aBAS1(theF1), aBAS2(theF2);
458   const GeomAbs_SurfaceType aType1=aBAS1.GetType();
459   const GeomAbs_SurfaceType aType2=aBAS2.GetType();
460   
461   gp_Pln aS1;
462   gp_Cylinder aS2;
463   if(aType1 == GeomAbs_Plane)
464   {
465     aS1=aBAS1.Plane();
466   }
467   else if(aType2 == GeomAbs_Plane)
468   {
469     aS1=aBAS2.Plane();
470   }
471   else
472   {
473     return Standard_True;
474   }
475
476   if(aType1 == GeomAbs_Cylinder)
477   {
478     aS2=aBAS1.Cylinder();
479     const Standard_Real VMin = aBAS1.FirstVParameter();
480     const Standard_Real VMax = aBAS1.LastVParameter();
481
482     if( Precision::IsNegativeInfinite(VMin) ||
483         Precision::IsPositiveInfinite(VMax))
484           return Standard_True;
485     else
486       aHigh = VMax - VMin;
487   }
488   else if(aType2 == GeomAbs_Cylinder)
489   {
490     aS2=aBAS2.Cylinder();
491
492     const Standard_Real VMin = aBAS2.FirstVParameter();
493     const Standard_Real VMax = aBAS2.LastVParameter();
494
495     if( Precision::IsNegativeInfinite(VMin) ||
496         Precision::IsPositiveInfinite(VMax))
497           return Standard_True;
498     else
499       aHigh = VMax - VMin;
500   }
501   else
502   {
503     return Standard_True;
504   }
505
506   IntAna_QuadQuadGeo inter;
507   inter.Perform(aS1,aS2,Tolang,aTolSum, aHigh);
508   if(inter.TypeInter() == IntAna_Ellipse)
509   {
510     const gp_Elips anEl = inter.Ellipse(1);
511     const Standard_Real aMajorR = anEl.MajorRadius();
512     const Standard_Real aMinorR = anEl.MinorRadius();
513     
514     return (aMajorR < 100000.0 * aMinorR);
515   }
516   else
517   {
518     return inter.IsDone();
519   }
520 }
521
522
523
524 //=======================================================================
525 //function : Perform
526 //purpose  : intersect surfaces of the faces
527 //=======================================================================
528   void IntTools_FaceFace::Perform(const TopoDS_Face& aF1,
529                                   const TopoDS_Face& aF2)
530 {
531   Standard_Boolean RestrictLine = Standard_False, hasCone = Standard_False;
532   
533   if (myContext.IsNull()) {
534     myContext=new BOPInt_Context;
535   }
536
537   mySeqOfCurve.Clear();
538   myTolReached2d=0.;
539   myTolReached3d=0.;
540   myIsDone = Standard_False;
541   myNbrestr=0;//?
542
543   myFace1=aF1;
544   myFace2=aF2;
545
546   const BRepAdaptor_Surface aBAS1(myFace1, Standard_False);
547   const BRepAdaptor_Surface aBAS2(myFace2, Standard_False);
548   GeomAbs_SurfaceType aType1=aBAS1.GetType();
549   GeomAbs_SurfaceType aType2=aBAS2.GetType();
550
551   const Standard_Boolean bReverse=SortTypes(aType1, aType2);
552   if (bReverse)
553   {
554     myFace1=aF2;
555     myFace2=aF1;
556     aType1=aBAS2.GetType();
557     aType2=aBAS1.GetType();
558
559     if (myListOfPnts.Extent())
560     {
561       Standard_Real aU1,aV1,aU2,aV2;
562       IntSurf_ListIteratorOfListOfPntOn2S aItP2S;
563       //
564       aItP2S.Initialize(myListOfPnts);
565       for (; aItP2S.More(); aItP2S.Next())
566       {
567         IntSurf_PntOn2S& aP2S=aItP2S.Value();
568         aP2S.Parameters(aU1,aV1,aU2,aV2);
569         aP2S.SetValue(aU2,aV2,aU1,aV1);
570       }
571     }
572   }
573
574
575   const Handle(Geom_Surface) S1=BRep_Tool::Surface(myFace1);
576   const Handle(Geom_Surface) S2=BRep_Tool::Surface(myFace2);
577
578   const Standard_Real aTolF1=BRep_Tool::Tolerance(myFace1);
579   const Standard_Real aTolF2=BRep_Tool::Tolerance(myFace2);
580
581   Standard_Real TolArc = aTolF1 + aTolF2;
582   Standard_Real TolTang = TolArc;
583
584   const Standard_Boolean isFace1Quad = (aType1 == GeomAbs_Cylinder ||
585                                         aType1 == GeomAbs_Cone ||
586                                         aType1 == GeomAbs_Torus);
587
588   const Standard_Boolean isFace2Quad = (aType2 == GeomAbs_Cylinder ||
589                                         aType2 == GeomAbs_Cone ||
590                                         aType2 == GeomAbs_Torus);
591
592   if(aType1==GeomAbs_Plane && aType2==GeomAbs_Plane)
593   {
594     Standard_Real umin, umax, vmin, vmax;
595     BRepTools::UVBounds(myFace1, umin, umax, vmin, vmax);
596     myHS1->ChangeSurface().Load(S1, umin, umax, vmin, vmax);
597     //
598     BRepTools::UVBounds(myFace2, umin, umax, vmin, vmax);
599     myHS2->ChangeSurface().Load(S2, umin, umax, vmin, vmax);
600     Standard_Real TolAng = 1.e-8;
601
602     PerformPlanes(myHS1, myHS2, TolAng, TolTang, myApprox1, myApprox2,
603                                           mySeqOfCurve, myTangentFaces);
604
605     myIsDone = Standard_True;
606     
607     if(!myTangentFaces)
608     {
609       const Standard_Integer NbLinPP = mySeqOfCurve.Length();
610       if(NbLinPP)
611       {
612         Standard_Real aTolFMax;
613         myTolReached3d = 1.e-7;
614         aTolFMax=Max(aTolF1, aTolF2);
615         if (aTolFMax>myTolReached3d)
616         {
617           myTolReached3d=aTolFMax;
618         }
619
620         myTolReached2d = myTolReached3d;
621
622         if (bReverse)
623         {
624           Handle(Geom2d_Curve) aC2D1, aC2D2;
625           const Standard_Integer aNbLin = mySeqOfCurve.Length();
626           for (Standard_Integer i = 1; i <= aNbLin; ++i)
627           {
628             IntTools_Curve& aIC=mySeqOfCurve(i);
629             aC2D1=aIC.FirstCurve2d();
630             aC2D2=aIC.SecondCurve2d();
631             aIC.SetFirstCurve2d(aC2D2);
632             aIC.SetSecondCurve2d(aC2D1);
633           }
634         }
635       }
636     }
637
638     return;
639   }//if(aType1==GeomAbs_Plane && aType2==GeomAbs_Plane){
640
641   if ((aType1==GeomAbs_Plane) && isFace2Quad)
642   {
643     Standard_Real dU, dV;
644
645     // F1
646     Standard_Real umin, umax, vmin, vmax;
647     BRepTools::UVBounds(myFace1, umin, umax, vmin, vmax);
648
649     dU=0.1*(umax-umin);
650     dV=0.1*(vmax-vmin);
651     umin=umin-dU;
652     umax=umax+dU;
653     vmin=vmin-dV;
654     vmax=vmax+dV;
655     myHS1->ChangeSurface().Load(S1, umin, umax, vmin, vmax);
656     // F2
657     BRepTools::UVBounds(myFace2, umin, umax, vmin, vmax);
658     CorrectSurfaceBoundaries(myFace2, (aTolF1 + aTolF2) * 2., umin, umax, vmin, vmax);
659     myHS2->ChangeSurface().Load(S2, umin, umax, vmin, vmax);
660     //
661     if( aType2==GeomAbs_Cone ) { 
662       TolArc = 0.0001; 
663       hasCone = Standard_True; 
664     }
665   }
666   else if ((aType2==GeomAbs_Plane) && isFace1Quad)
667   {
668     Standard_Real dU, dV;
669
670     //F1
671     Standard_Real umin, umax, vmin, vmax;
672     BRepTools::UVBounds(myFace1, umin, umax, vmin, vmax);
673     CorrectSurfaceBoundaries(myFace1, (aTolF1 + aTolF2) * 2., umin, umax, vmin, vmax);
674     myHS1->ChangeSurface().Load(S1, umin, umax, vmin, vmax);
675     // F2
676     BRepTools::UVBounds(myFace2, umin, umax, vmin, vmax);
677     dU=0.1*(umax-umin);
678     dV=0.1*(vmax-vmin);
679     umin=umin-dU;
680     umax=umax+dU;
681     vmin=vmin-dV;
682     vmax=vmax+dV;
683     myHS2->ChangeSurface().Load(S2, umin, umax, vmin, vmax);
684     //
685     if( aType1==GeomAbs_Cone ) {
686       TolArc = 0.0001; 
687       hasCone = Standard_True; 
688     }
689   }
690   else
691   {
692     Standard_Real umin, umax, vmin, vmax;
693     BRepTools::UVBounds(myFace1, umin, umax, vmin, vmax);
694     CorrectSurfaceBoundaries(myFace1, (aTolF1 + aTolF2) * 2., umin, umax, vmin, vmax);
695     myHS1->ChangeSurface().Load(S1, umin, umax, vmin, vmax);
696     BRepTools::UVBounds(myFace2, umin, umax, vmin, vmax);
697     CorrectSurfaceBoundaries(myFace2, (aTolF1 + aTolF2) * 2., umin, umax, vmin, vmax);
698     myHS2->ChangeSurface().Load(S2, umin, umax, vmin, vmax);
699   }
700
701   const Handle(IntTools_TopolTool) dom1 = new IntTools_TopolTool(myHS1);
702   const Handle(IntTools_TopolTool) dom2 = new IntTools_TopolTool(myHS2);
703
704   myLConstruct.Load(dom1, dom2, myHS1, myHS2);
705   
706
707   Tolerances(myHS1, myHS2, TolTang);
708
709   {
710     const Standard_Real UVMaxStep = 0.001;
711     const Standard_Real Deflection = (hasCone) ? 0.085 : 0.1;
712     myIntersector.SetTolerances(TolArc, TolTang, UVMaxStep, Deflection);
713   }
714   
715   if((myHS1->IsUClosed() && !myHS1->IsUPeriodic()) || 
716      (myHS1->IsVClosed() && !myHS1->IsVPeriodic()) ||
717      (myHS2->IsUClosed() && !myHS2->IsUPeriodic()) || 
718      (myHS2->IsVClosed() && !myHS2->IsVPeriodic()))
719   {
720     RestrictLine = Standard_True;
721   }
722   //
723   if((aType1 != GeomAbs_BSplineSurface) &&
724      (aType1 != GeomAbs_BezierSurface) &&
725      (aType1 != GeomAbs_OtherSurface)  &&
726      (aType2 != GeomAbs_BSplineSurface) &&
727      (aType2 != GeomAbs_BezierSurface) &&
728      (aType2 != GeomAbs_OtherSurface))
729   {
730     RestrictLine = Standard_True;
731
732     if ((aType1 == GeomAbs_Torus) ||
733         (aType2 == GeomAbs_Torus))
734     {
735       myListOfPnts.Clear();
736     }
737   }
738
739   //
740   if(!RestrictLine)
741   {
742     TopExp_Explorer aExp;
743     for(Standard_Integer i = 0; (!RestrictLine) && (i < 2); i++)
744     {
745       const TopoDS_Face& aF=(!i) ? myFace1 : myFace2;
746       aExp.Init(aF, TopAbs_EDGE);
747       for(; aExp.More(); aExp.Next())
748       {
749         const TopoDS_Edge& aE=TopoDS::Edge(aExp.Current());
750
751         if(BRep_Tool::Degenerated(aE))
752         {
753           RestrictLine = Standard_True;
754           break;
755         }
756       }
757     }
758   }
759
760   const Standard_Boolean isGeomInt = isTreatAnalityc(aF1, aF2);
761   myIntersector.Perform(myHS1, dom1, myHS2, dom2, TolArc, TolTang, 
762                                   myListOfPnts, RestrictLine, isGeomInt);
763
764   myIsDone = myIntersector.IsDone();
765
766   if (myIsDone)
767   {
768     myTangentFaces=myIntersector.TangentFaces();
769     if (myTangentFaces) {
770       return;
771     }
772     //
773     if(RestrictLine) {
774       myListOfPnts.Clear(); // to use LineConstructor
775     }
776     //
777     const Standard_Integer aNbLin = myIntersector.NbLines();
778     for (Standard_Integer i=1; i <= aNbLin; ++i) {
779       MakeCurve(i, dom1, dom2);
780     }
781     //
782     ComputeTolReached3d();
783     //
784     if (bReverse) {
785       Handle(Geom2d_Curve) aC2D1, aC2D2;
786       //
787       const Standard_Integer aNbLin=mySeqOfCurve.Length();
788       for (Standard_Integer i=1; i<=aNbLin; ++i)
789       {
790         IntTools_Curve& aIC=mySeqOfCurve(i);
791         aC2D1=aIC.FirstCurve2d();
792         aC2D2=aIC.SecondCurve2d();
793         aIC.SetFirstCurve2d(aC2D2);
794         aIC.SetSecondCurve2d(aC2D1);
795       }
796     }
797
798     // Points
799     Standard_Real U1,V1,U2,V2;
800     IntTools_PntOnFace aPntOnF1, aPntOnF2;
801     IntTools_PntOn2Faces aPntOn2Faces;
802     //
803     const Standard_Integer aNbPnts = myIntersector.NbPnts();
804     for (Standard_Integer i=1; i <= aNbPnts; ++i)
805     {
806       const IntSurf_PntOn2S& aISPnt=myIntersector.Point(i).PntOn2S();
807       const gp_Pnt& aPnt=aISPnt.Value();
808       aISPnt.Parameters(U1,V1,U2,V2);
809       aPntOnF1.Init(myFace1, aPnt, U1, V1);
810       aPntOnF2.Init(myFace2, aPnt, U2, V2);
811       //
812       if (!bReverse)
813       {
814         aPntOn2Faces.SetP1(aPntOnF1);
815         aPntOn2Faces.SetP2(aPntOnF2);
816       }
817       else
818       {
819         aPntOn2Faces.SetP2(aPntOnF1);
820         aPntOn2Faces.SetP1(aPntOnF2);
821       }
822
823       myPnts.Append(aPntOn2Faces);
824     }
825   }
826 }
827
828 //=======================================================================
829 //function :ComputeTolReached3d 
830 //purpose  : 
831 //=======================================================================
832   void IntTools_FaceFace::ComputeTolReached3d()
833 {
834   Standard_Boolean bCase1;
835   Standard_Integer aNbLin, i;
836   GeomAbs_SurfaceType aType1, aType2;
837   //
838   aNbLin=myIntersector.NbLines();
839   if (!aNbLin) {
840     return;
841   }
842   //
843   aType1=myHS1->Surface().GetType();
844   aType2=myHS2->Surface().GetType();
845   //
846   bCase1=((aType1==GeomAbs_Plane && aType2==GeomAbs_SurfaceOfExtrusion) ||
847           (aType2==GeomAbs_Plane && aType1==GeomAbs_SurfaceOfExtrusion));
848   //
849   if (aType1==GeomAbs_Cylinder && aType2==GeomAbs_Cylinder) {
850     if (aNbLin==2){ 
851       Handle(IntPatch_Line) aIL1, aIL2;
852       IntPatch_IType aTL1, aTL2;
853       //
854       aIL1=myIntersector.Line(1);
855       aIL2=myIntersector.Line(2);
856       aTL1=aIL1->ArcType();
857       aTL2=aIL2->ArcType();
858       if (aTL1==IntPatch_Lin && aTL2==IntPatch_Lin) {
859         Standard_Real aD, aDTresh, dTol;
860         gp_Lin aL1, aL2;
861         //
862         dTol=1.e-8;
863         aDTresh=1.5e-6;
864         //
865         aL1=Handle(IntPatch_GLine)::DownCast(aIL1)->Line();
866         aL2=Handle(IntPatch_GLine)::DownCast(aIL2)->Line();
867         aD=aL1.Distance(aL2);
868         aD=0.5*aD;
869         if (aD<aDTresh) {
870           myTolReached3d=aD+dTol;
871         }
872         return;
873       }
874     }
875     //ZZ
876     if (aNbLin) {// Check the distances
877       Standard_Integer  aNbP, j ;
878       Standard_Real aT1, aT2, dT, aD2, aD2Max, aEps, aT11, aT12;
879       //
880       aD2Max=0.;
881       aNbP=10;
882       aNbLin=mySeqOfCurve.Length();
883       //
884       for (i=1; i<=aNbLin; ++i) {
885         const IntTools_Curve& aIC=mySeqOfCurve(i);
886         const Handle(Geom_Curve)& aC3D=aIC.Curve();
887         const Handle(Geom2d_Curve)& aC2D1=aIC.FirstCurve2d();
888         const Handle(Geom2d_Curve)& aC2D2=aIC.SecondCurve2d();
889         //
890         if (aC3D.IsNull()) {
891           continue;
892         }
893         const Handle(Geom_BSplineCurve)& aBC=
894           Handle(Geom_BSplineCurve)::DownCast(aC3D);
895         if (aBC.IsNull()) {
896           continue;
897         }
898         //
899         aT1=aBC->FirstParameter();
900         aT2=aBC->LastParameter();
901         //
902         aEps=0.01*(aT2-aT1);
903         dT=(aT2-aT1)/aNbP;
904         for (j=1; j<aNbP; ++j) {
905           aT11=aT1+j*dT;
906           aT12=aT11+dT;
907           aD2=FindMaxSquareDistance(aT11, aT12, aEps, aC3D, aC2D1, aC2D2,
908                                     myHS1, myHS2, myFace1, myFace2, myContext);
909           if (aD2>aD2Max) {
910             aD2Max=aD2;
911           }
912         }
913       }//for (i=1; i<=aNbLin; ++i) {
914       //
915       myTolReached3d=sqrt(aD2Max);
916     }// if (aNbLin) 
917   }// if (aType1==GeomAbs_Cylinder && aType2==GeomAbs_Cylinder) {
918   //
919   //904/G3 f
920   else if (aType1==GeomAbs_Plane && aType2==GeomAbs_Plane) {
921     Standard_Real aTolF1, aTolF2, aTolFMax, aTolTresh;
922     //
923     aTolTresh=1.e-7;
924     //
925     aTolF1 = BRep_Tool::Tolerance(myFace1);
926     aTolF2 = BRep_Tool::Tolerance(myFace2);
927     aTolFMax=Max(aTolF1, aTolF2);
928     //
929     if (aTolFMax>aTolTresh) {
930       myTolReached3d=aTolFMax;
931     }
932   }//if (aType1==GeomAbs_Plane && aType2==GeomAbs_Plane) {
933   //t
934   //IFV Bug OCC20297 
935   else if((aType1 == GeomAbs_Cylinder && aType2 == GeomAbs_Plane) ||
936           (aType2 == GeomAbs_Cylinder && aType1 == GeomAbs_Plane)) {
937     if(aNbLin == 1) {
938       const Handle(IntPatch_Line)& aIL1 = myIntersector.Line(1);
939       if(aIL1->ArcType() == IntPatch_Circle) {
940         gp_Circ aCir = Handle(IntPatch_GLine)::DownCast(aIL1)->Circle();
941         gp_XYZ aCirDir = aCir.Axis().Direction().XYZ();
942         gp_XYZ aPlDir;
943         gp_Pln aPln;
944         if(aType1 == GeomAbs_Plane) {
945           aPln = myHS1->Surface().Plane();
946         }
947         else {
948           aPln = myHS2->Surface().Plane();
949         }
950         aPlDir = aPln.Axis().Direction().XYZ();
951         Standard_Real cs = aCirDir*aPlDir;
952         if(cs < 0.) aPlDir.Reverse();
953         Standard_Real eps = 1.e-14;
954         if(!aPlDir.IsEqual(aCirDir, eps)) {
955           Standard_Integer aNbP = 11;
956           Standard_Real dt = 2.*M_PI / (aNbP - 1), t;
957           for(t = 0.; t < 2.*M_PI; t += dt) {
958             Standard_Real d = aPln.Distance(ElCLib::Value(t, aCir)); 
959             if(myTolReached3d < d) myTolReached3d = d;
960           }
961           myTolReached3d *= 1.1;
962         }
963       } //aIL1->ArcType() == IntPatch_Circle
964     } //aNbLin == 1
965   } // aType1 == GeomAbs_Cylinder && aType2 == GeomAbs_Plane) 
966   //End IFV Bug OCC20297
967   //
968   else if ((aType1==GeomAbs_Plane && aType2==GeomAbs_Torus) ||
969            (aType2==GeomAbs_Plane && aType1==GeomAbs_Torus)) {
970     aNbLin=mySeqOfCurve.Length();
971     if (aNbLin!=1) {
972       return;
973     }
974     //
975     Standard_Integer aNbP;
976     Standard_Real aT, aT1, aT2, dT, aUT, aVT, aUP, aVP;
977     Standard_Real aDP, aDT, aDmax;
978     gp_Pln aPln;
979     gp_Torus aTorus;
980     gp_Pnt aP, aPP, aPT;
981     //
982     const IntTools_Curve& aIC=mySeqOfCurve(1);
983     const Handle(Geom_Curve)& aC3D=aIC.Curve();
984     const Handle(Geom_BSplineCurve)& aBS=
985       Handle(Geom_BSplineCurve)::DownCast(aC3D);
986     if (aBS.IsNull()) {
987       return;
988     }
989     //
990     aT1=aBS->FirstParameter();
991     aT2=aBS->LastParameter();
992     //
993     aPln  =(aType1==GeomAbs_Plane) ? myHS1->Plane() : myHS2->Plane();
994     aTorus=(aType1==GeomAbs_Plane) ? myHS2->Torus() : myHS1->Torus();
995     //
996     aDmax=-1.;
997     aNbP=11;
998     dT=(aT2-aT1)/(aNbP-1);
999     for (i=0; i<aNbP; ++i) {
1000       aT=aT1+i*dT;
1001       if (i==aNbP-1) {
1002         aT=aT2;
1003       }
1004       //
1005       aC3D->D0(aT, aP);
1006       //
1007       ElSLib::Parameters(aPln, aP, aUP, aVP);
1008       aPP=ElSLib::Value(aUP, aVP, aPln);
1009       aDP=aP.SquareDistance(aPP);
1010       if (aDP>aDmax) {
1011         aDmax=aDP;
1012       }
1013       //
1014       ElSLib::Parameters(aTorus, aP, aUT, aVT);
1015       aPT=ElSLib::Value(aUT, aVT, aTorus);
1016       aDT=aP.SquareDistance(aPT);
1017       if (aDT>aDmax) {
1018         aDmax=aDT;
1019       }
1020     }
1021     //
1022     if (aDmax > myTolReached3d*myTolReached3d) {
1023       myTolReached3d=sqrt(aDmax);
1024       myTolReached3d=1.1*myTolReached3d;
1025     }
1026   }// if ((aType1==GeomAbs_Plane && aType2==GeomAbs_Torus) ||
1027   //
1028   else if ((aType1==GeomAbs_SurfaceOfRevolution && aType2==GeomAbs_Cylinder) ||
1029            (aType2==GeomAbs_SurfaceOfRevolution && aType1==GeomAbs_Cylinder)) {
1030     Standard_Integer j, aNbP;
1031     Standard_Real aT, aT1, aT2, dT, aD2max, aD2;
1032     //
1033     aNbLin=mySeqOfCurve.Length();
1034     aD2max=0.;
1035     aNbP=11;
1036     //
1037     for (i=1; i<=aNbLin; ++i) {
1038       const IntTools_Curve& aIC=mySeqOfCurve(i);
1039       const Handle(Geom_Curve)& aC3D=aIC.Curve();
1040       const Handle(Geom2d_Curve)& aC2D1=aIC.FirstCurve2d();
1041       const Handle(Geom2d_Curve)& aC2D2=aIC.SecondCurve2d();
1042       //
1043       if (aC3D.IsNull()) {
1044         continue;
1045       }
1046       const Handle(Geom_BSplineCurve)& aBC=
1047         Handle(Geom_BSplineCurve)::DownCast(aC3D);
1048       if (aBC.IsNull()) {
1049         return;
1050       }
1051       //
1052       aT1=aBC->FirstParameter();
1053       aT2=aBC->LastParameter();
1054       //
1055       dT=(aT2-aT1)/(aNbP-1);
1056       for (j=0; j<aNbP; ++j) {
1057         aT=aT1+j*dT;
1058         if (j==aNbP-1) {
1059           aT=aT2;
1060         }
1061         //
1062         aD2=MaxSquareDistance(aT, aC3D, aC2D1, aC2D2,
1063                               myHS1, myHS2, myFace1, myFace2, myContext);
1064         if (aD2>aD2max) {
1065           aD2max=aD2;
1066         }
1067       }//for (j=0; j<aNbP; ++j) {
1068      
1069     }//for (i=1; i<=aNbLin; ++i) {
1070     //
1071     aD2=myTolReached3d*myTolReached3d;
1072     if (aD2max > aD2) {
1073       myTolReached3d=sqrt(aD2max);
1074     }
1075   }//if((aType1==GeomAbs_SurfaceOfRevolution ...
1076   else if ((aType1==GeomAbs_Plane && aType2==GeomAbs_Sphere) ||
1077            (aType2==GeomAbs_Plane && aType1==GeomAbs_Sphere)) {
1078     Standard_Integer  j, aNbP;
1079     Standard_Real aT1, aT2, dT, aD2max, aD2, aEps, aT11, aT12;
1080     //
1081     aNbLin=mySeqOfCurve.Length();
1082     aD2max=0.;
1083     aNbP=10;
1084     //
1085     for (i=1; i<=aNbLin; ++i) {
1086       const IntTools_Curve& aIC=mySeqOfCurve(i);
1087       const Handle(Geom_Curve)& aC3D=aIC.Curve();
1088       const Handle(Geom2d_Curve)& aC2D1=aIC.FirstCurve2d();
1089       const Handle(Geom2d_Curve)& aC2D2=aIC.SecondCurve2d();
1090       //
1091       const Handle(Geom2d_BSplineCurve)& aBC2D1=
1092         Handle(Geom2d_BSplineCurve)::DownCast(aC2D1);
1093       const Handle(Geom2d_BSplineCurve)& aBC2D2=
1094         Handle(Geom2d_BSplineCurve)::DownCast(aC2D2);
1095       //
1096       if (aBC2D1.IsNull() && aBC2D2.IsNull()) {
1097         return;
1098       }
1099       //
1100       if (!aBC2D1.IsNull()) {
1101         aT1=aBC2D1->FirstParameter();
1102         aT2=aBC2D1->LastParameter();
1103       }
1104       else {
1105         aT1=aBC2D2->FirstParameter();
1106         aT2=aBC2D2->LastParameter();
1107       }
1108       //
1109       aEps=0.01*(aT2-aT1);
1110       dT=(aT2-aT1)/aNbP;
1111       for (j=0; j<aNbP; ++j) {
1112         aT11=aT1+j*dT;
1113         aT12=aT11+dT;
1114         if (j==aNbP-1) {
1115           aT12=aT2;
1116         }
1117         //
1118         aD2=FindMaxSquareDistance(aT11, aT12, aEps, aC3D, aC2D1, aC2D2,
1119                                   myHS1, myHS2, myFace1, myFace2, myContext);
1120         if (aD2>aD2max) {
1121           aD2max=aD2;
1122         }
1123       }//for (j=0; j<aNbP; ++j) {
1124      
1125     }//for (i=1; i<=aNbLin; ++i) {
1126     //
1127     aD2=myTolReached3d*myTolReached3d;
1128     if (aD2max > aD2) {
1129       myTolReached3d=sqrt(aD2max);
1130     }
1131   }//else if ((aType1==GeomAbs_Plane && aType2==GeomAbs_Sphere) ...
1132   else if (!myApprox || bCase1) {
1133   //else if (!myApprox) {
1134     Standard_Integer aNbP, j;
1135     Standard_Real aT1, aT2, dT, aD2, aD2Max, aEps, aT11, aT12;
1136     //
1137     aD2Max=0.;
1138     aNbLin=mySeqOfCurve.Length();
1139     //
1140     for (i=1; i<=aNbLin; ++i) {
1141       const IntTools_Curve& aIC=mySeqOfCurve(i);
1142       const Handle(Geom_Curve)& aC3D=aIC.Curve();
1143       const Handle(Geom2d_Curve)& aC2D1=aIC.FirstCurve2d();
1144       const Handle(Geom2d_Curve)& aC2D2=aIC.SecondCurve2d();
1145       //
1146       if (aC3D.IsNull()) {
1147         continue;
1148       }
1149       const Handle(Geom_BSplineCurve)& aBC=
1150         Handle(Geom_BSplineCurve)::DownCast(aC3D);
1151       if (aBC.IsNull()) {
1152         continue;
1153       }
1154       //
1155       aT1=aBC->FirstParameter();
1156       aT2=aBC->LastParameter();
1157       //
1158       aEps=0.0001*(aT2-aT1);
1159       aNbP=11;
1160       dT=(aT2-aT1)/aNbP;
1161       for (j=1; j<aNbP-1; ++j) {
1162         aT11=aT1+j*dT;
1163         aT12=aT11+dT;
1164         aD2=FindMaxSquareDistance(aT11, aT12, aEps, aC3D, aC2D1, aC2D2,
1165                                   myHS1, myHS2, myFace1, myFace2, myContext);
1166         if (aD2>aD2Max) {
1167           aD2Max=aD2;
1168         }
1169       }
1170     }//for (i=1; i<=aNbLin; ++i) {
1171     myTolReached3d=sqrt(aD2Max);
1172   }
1173 }
1174 //=======================================================================
1175 //function : MakeCurve
1176 //purpose  : 
1177 //=======================================================================
1178   void IntTools_FaceFace::MakeCurve(const Standard_Integer Index,
1179                                     const Handle(Adaptor3d_TopolTool)& dom1,
1180                                     const Handle(Adaptor3d_TopolTool)& dom2) 
1181 {
1182   Standard_Boolean bDone, rejectSurface, reApprox, bAvoidLineConstructor,
1183     bPCurvesOk;
1184   Standard_Boolean ok;
1185   Standard_Integer i, j, aNbParts;
1186   Standard_Real fprm, lprm;
1187   Standard_Real Tolpc;
1188   Handle(IntPatch_Line) L;
1189   IntPatch_IType typl;
1190   Handle(Geom_Curve) newc;
1191   //
1192   const Standard_Real TOLCHECK   =0.0000001;
1193   const Standard_Real TOLANGCHECK=0.1;
1194   //
1195   rejectSurface = Standard_False;
1196   reApprox = Standard_False;
1197   //
1198   bPCurvesOk = Standard_True;
1199
1200 reapprox:;
1201
1202   Tolpc = myTolApprox;
1203   bAvoidLineConstructor = Standard_False;
1204   L = myIntersector.Line(Index);
1205   typl = L->ArcType();
1206   //
1207   if(typl==IntPatch_Walking) {
1208     Handle(IntPatch_Line) anewL;
1209     //
1210     const Handle(IntPatch_WLine)& aWLine=
1211       Handle(IntPatch_WLine)::DownCast(L);
1212 #ifdef DEB_DUMPWLINE
1213     DumpWLine(aWLine);
1214 #endif
1215     anewL = ComputePurgedWLine(aWLine);
1216     if(anewL.IsNull()) {
1217       return;
1218     }
1219     L = anewL;
1220 #ifdef DEB_DUMPWLINE
1221     const Handle(IntPatch_WLine)& aWLineX = Handle(IntPatch_WLine)::DownCast(L);
1222     DumpWLine(aWLineX);
1223 #endif
1224     //
1225     if(!myListOfPnts.IsEmpty()) {
1226       bAvoidLineConstructor = Standard_True;
1227     }
1228
1229     Standard_Integer nbp = aWLine->NbPnts();
1230     const IntSurf_PntOn2S& p1 = aWLine->Point(1);
1231     const IntSurf_PntOn2S& p2 = aWLine->Point(nbp);
1232
1233     const gp_Pnt& P1 = p1.Value();
1234     const gp_Pnt& P2 = p2.Value();
1235
1236     if(P1.SquareDistance(P2) < 1.e-14) {
1237       bAvoidLineConstructor = Standard_False;
1238     }
1239
1240   }
1241   //
1242   // Line Constructor
1243   if(!bAvoidLineConstructor) {
1244     myLConstruct.Perform(L);
1245     //
1246     bDone=myLConstruct.IsDone();
1247     aNbParts=myLConstruct.NbParts();
1248     if (!bDone|| !aNbParts) {
1249       return;
1250     }
1251   }
1252   // Do the Curve
1253
1254
1255   typl=L->ArcType();
1256   switch (typl) {
1257     //########################################  
1258     // Line, Parabola, Hyperbola
1259     //########################################  
1260   case IntPatch_Lin:
1261   case IntPatch_Parabola: 
1262   case IntPatch_Hyperbola: {
1263     if (typl == IntPatch_Lin) {
1264       newc = 
1265         new Geom_Line (Handle(IntPatch_GLine)::DownCast(L)->Line());
1266     }
1267
1268     else if (typl == IntPatch_Parabola) {
1269       newc = 
1270         new Geom_Parabola(Handle(IntPatch_GLine)::DownCast(L)->Parabola());
1271     }
1272
1273     else if (typl == IntPatch_Hyperbola) {
1274       newc = 
1275         new Geom_Hyperbola (Handle(IntPatch_GLine)::DownCast(L)->Hyperbola());
1276     }
1277     //
1278     // myTolReached3d
1279     if (typl == IntPatch_Lin) {
1280       TolR3d (myFace1, myFace2, myTolReached3d);
1281     }
1282     //
1283     aNbParts=myLConstruct.NbParts();
1284     for (i=1; i<=aNbParts; i++) {
1285       myLConstruct.Part(i, fprm, lprm);
1286
1287       if (!Precision::IsNegativeInfinite(fprm) && 
1288           !Precision::IsPositiveInfinite(lprm)) {
1289         //
1290         IntTools_Curve aCurve;
1291         //
1292         Handle(Geom_TrimmedCurve) aCT3D=new Geom_TrimmedCurve(newc, fprm, lprm);
1293         aCurve.SetCurve(aCT3D);
1294         if (typl == IntPatch_Parabola) {
1295           Standard_Real aTolF1, aTolF2, aTolBase;
1296
1297           aTolF1 = BRep_Tool::Tolerance(myFace1);
1298           aTolF2 = BRep_Tool::Tolerance(myFace2);
1299           aTolBase=aTolF1+aTolF2;
1300           myTolReached3d=IntTools_Tools::CurveTolerance(aCT3D, aTolBase);
1301         }
1302         //
1303         aCurve.SetCurve(new Geom_TrimmedCurve(newc, fprm, lprm));
1304         if(myApprox1) { 
1305           Handle (Geom2d_Curve) C2d;
1306           BuildPCurves(fprm, lprm, Tolpc, myHS1->ChangeSurface().Surface(), newc, C2d);
1307           if(Tolpc>myTolReached2d || myTolReached2d==0.) { 
1308             myTolReached2d=Tolpc;
1309           }
1310           //       
1311           aCurve.SetFirstCurve2d(new Geom2d_TrimmedCurve(C2d,fprm,lprm));
1312         }
1313         else { 
1314           Handle(Geom2d_BSplineCurve) H1;
1315           //
1316           aCurve.SetFirstCurve2d(H1);
1317         }
1318
1319         if(myApprox2) { 
1320           Handle (Geom2d_Curve) C2d;
1321           BuildPCurves(fprm,lprm,Tolpc,myHS2->ChangeSurface().Surface(),newc,C2d);
1322           if(Tolpc>myTolReached2d || myTolReached2d==0.) { 
1323             myTolReached2d=Tolpc;
1324           }
1325           //
1326           aCurve.SetSecondCurve2d(new Geom2d_TrimmedCurve(C2d,fprm,lprm));
1327         }
1328         else { 
1329           Handle(Geom2d_BSplineCurve) H1;
1330           //
1331           aCurve.SetSecondCurve2d(H1);
1332         }
1333         mySeqOfCurve.Append(aCurve);
1334       } // end of if (!Precision::IsNegativeInfinite(fprm) &&  !Precision::IsPositiveInfinite(lprm))
1335       else {
1336         //  on regarde si on garde
1337         //
1338         Standard_Boolean bFNIt, bLPIt;
1339         Standard_Real aTestPrm, dT=100.;
1340
1341         bFNIt=Precision::IsNegativeInfinite(fprm);
1342         bLPIt=Precision::IsPositiveInfinite(lprm);
1343
1344         aTestPrm=0.;
1345
1346         if (bFNIt && !bLPIt) {
1347           aTestPrm=lprm-dT;
1348         }
1349         else if (!bFNIt && bLPIt) {
1350           aTestPrm=fprm+dT;
1351         }
1352
1353         gp_Pnt ptref(newc->Value(aTestPrm));
1354         //
1355         GeomAbs_SurfaceType typS1 = myHS1->GetType();
1356         GeomAbs_SurfaceType typS2 = myHS2->GetType();
1357         if( typS1 == GeomAbs_SurfaceOfExtrusion ||
1358             typS1 == GeomAbs_OffsetSurface ||
1359             typS1 == GeomAbs_SurfaceOfRevolution ||
1360             typS2 == GeomAbs_SurfaceOfExtrusion ||
1361             typS2 == GeomAbs_OffsetSurface ||
1362             typS2 == GeomAbs_SurfaceOfRevolution) 
1363         {
1364           Handle(Geom2d_BSplineCurve) H1;
1365           mySeqOfCurve.Append(IntTools_Curve(newc, H1, H1));
1366           continue;
1367         }
1368
1369         Standard_Real u1, v1, u2, v2, Tol;
1370
1371         Tol = Precision::Confusion();
1372         Parameters(myHS1, myHS2, ptref,  u1, v1, u2, v2);
1373         ok = (dom1->Classify(gp_Pnt2d(u1, v1), Tol) != TopAbs_OUT);
1374         if(ok) { 
1375           ok = (dom2->Classify(gp_Pnt2d(u2,v2),Tol) != TopAbs_OUT); 
1376         }
1377         if (ok) {
1378           Handle(Geom2d_BSplineCurve) H1;
1379           mySeqOfCurve.Append(IntTools_Curve(newc, H1, H1));
1380         }
1381       }
1382     }// end of for (i=1; i<=myLConstruct.NbParts(); i++)
1383   }// case IntPatch_Lin:  case IntPatch_Parabola:  case IntPatch_Hyperbola:
1384   break;
1385
1386                            //########################################  
1387                            // Circle and Ellipse
1388                            //########################################  
1389   case IntPatch_Circle: 
1390   case IntPatch_Ellipse: {
1391
1392     if (typl == IntPatch_Circle) {
1393       newc = new Geom_Circle
1394         (Handle(IntPatch_GLine)::DownCast(L)->Circle());
1395     }
1396     else { //IntPatch_Ellipse
1397       newc = new Geom_Ellipse
1398         (Handle(IntPatch_GLine)::DownCast(L)->Ellipse());
1399     }
1400     //
1401     // myTolReached3d
1402     TolR3d (myFace1, myFace2, myTolReached3d);
1403     //
1404     aNbParts=myLConstruct.NbParts();
1405     //
1406     Standard_Real aPeriod, aNul;
1407     TColStd_SequenceOfReal aSeqFprm,  aSeqLprm;
1408
1409     aNul=0.;
1410     aPeriod=M_PI+M_PI;
1411
1412     for (i=1; i<=aNbParts; i++) {
1413       myLConstruct.Part(i, fprm, lprm);
1414
1415       if (fprm < aNul && lprm > aNul) {
1416         // interval that goes through 0. is divided on two intervals;
1417         while (fprm<aNul || fprm>aPeriod) fprm=fprm+aPeriod;
1418         while (lprm<aNul || lprm>aPeriod) lprm=lprm+aPeriod;
1419         //
1420         if((aPeriod - fprm) > Tolpc) {
1421           aSeqFprm.Append(fprm);
1422           aSeqLprm.Append(aPeriod);
1423         }
1424         else {
1425           gp_Pnt P1 = newc->Value(fprm);
1426           gp_Pnt P2 = newc->Value(aPeriod);
1427           Standard_Real aTolDist = BRep_Tool::Tolerance(myFace1) + BRep_Tool::Tolerance(myFace2);
1428           aTolDist = (myTolReached3d > aTolDist) ? myTolReached3d : aTolDist;
1429
1430           if(P1.Distance(P2) > aTolDist) {
1431             Standard_Real anewpar = fprm;
1432
1433             if(ParameterOutOfBoundary(fprm, newc, myFace1, myFace2, lprm, Standard_False, anewpar, myContext)) {
1434               fprm = anewpar;
1435             }
1436             aSeqFprm.Append(fprm);
1437             aSeqLprm.Append(aPeriod);
1438           }
1439         }
1440
1441         //
1442         if((lprm - aNul) > Tolpc) {
1443           aSeqFprm.Append(aNul);
1444           aSeqLprm.Append(lprm);
1445         }
1446         else {
1447           gp_Pnt P1 = newc->Value(aNul);
1448           gp_Pnt P2 = newc->Value(lprm);
1449           Standard_Real aTolDist = BRep_Tool::Tolerance(myFace1) + BRep_Tool::Tolerance(myFace2);
1450           aTolDist = (myTolReached3d > aTolDist) ? myTolReached3d : aTolDist;
1451
1452           if(P1.Distance(P2) > aTolDist) {
1453             Standard_Real anewpar = lprm;
1454
1455             if(ParameterOutOfBoundary(lprm, newc, myFace1, myFace2, fprm, Standard_True, anewpar, myContext)) {
1456               lprm = anewpar;
1457             }
1458             aSeqFprm.Append(aNul);
1459             aSeqLprm.Append(lprm);
1460           }
1461         }
1462       }
1463       else {
1464         // usual interval 
1465         aSeqFprm.Append(fprm);
1466         aSeqLprm.Append(lprm);
1467       }
1468     }
1469
1470     //
1471     aNbParts=aSeqFprm.Length();
1472     for (i=1; i<=aNbParts; i++) {
1473       fprm=aSeqFprm(i);
1474       lprm=aSeqLprm(i);
1475       //
1476       Standard_Real aRealEpsilon=RealEpsilon();
1477       if (Abs(fprm) > aRealEpsilon || Abs(lprm-2.*M_PI) > aRealEpsilon) {
1478         //==============================================
1479         ////
1480         IntTools_Curve aCurve;
1481         Handle(Geom_TrimmedCurve) aTC3D=new Geom_TrimmedCurve(newc,fprm,lprm);
1482         aCurve.SetCurve(aTC3D);
1483         fprm=aTC3D->FirstParameter();
1484         lprm=aTC3D->LastParameter ();
1485         ////    
1486         if (typl == IntPatch_Circle || typl == IntPatch_Ellipse) {//// 
1487           if(myApprox1) { 
1488             Handle (Geom2d_Curve) C2d;
1489             BuildPCurves(fprm,lprm,Tolpc,myHS1->ChangeSurface().Surface(),newc,C2d);
1490             if(Tolpc>myTolReached2d || myTolReached2d==0) { 
1491               myTolReached2d=Tolpc;
1492             }
1493             //
1494             aCurve.SetFirstCurve2d(C2d);
1495           }
1496           else { //// 
1497             Handle(Geom2d_BSplineCurve) H1;
1498             aCurve.SetFirstCurve2d(H1);
1499           }
1500
1501
1502           if(myApprox2) { 
1503             Handle (Geom2d_Curve) C2d;
1504             BuildPCurves(fprm,lprm,Tolpc,myHS2->ChangeSurface().Surface(),newc,C2d);
1505             if(Tolpc>myTolReached2d || myTolReached2d==0) { 
1506               myTolReached2d=Tolpc;
1507             }
1508             //
1509             aCurve.SetSecondCurve2d(C2d);
1510           }
1511           else { 
1512             Handle(Geom2d_BSplineCurve) H1;
1513             aCurve.SetSecondCurve2d(H1);
1514           }
1515         }
1516
1517         else { 
1518           Handle(Geom2d_BSplineCurve) H1;
1519           aCurve.SetFirstCurve2d(H1);
1520           aCurve.SetSecondCurve2d(H1);
1521         }
1522         mySeqOfCurve.Append(aCurve);
1523         //==============================================        
1524       } //if (Abs(fprm) > RealEpsilon() || Abs(lprm-2.*M_PI) > RealEpsilon())
1525
1526       else {
1527         //  on regarde si on garde
1528         //
1529         if (aNbParts==1) {
1530           //      if (Abs(fprm) < RealEpsilon() &&  Abs(lprm-2.*M_PI) < RealEpsilon()) {
1531           if (Abs(fprm) <= aRealEpsilon && Abs(lprm-2.*M_PI) <= aRealEpsilon) {
1532             IntTools_Curve aCurve;
1533             Handle(Geom_TrimmedCurve) aTC3D=new Geom_TrimmedCurve(newc,fprm,lprm);
1534             aCurve.SetCurve(aTC3D);
1535             fprm=aTC3D->FirstParameter();
1536             lprm=aTC3D->LastParameter ();
1537
1538             if(myApprox1) { 
1539               Handle (Geom2d_Curve) C2d;
1540               BuildPCurves(fprm,lprm,Tolpc,myHS1->ChangeSurface().Surface(),newc,C2d);
1541               if(Tolpc>myTolReached2d || myTolReached2d==0) { 
1542                 myTolReached2d=Tolpc;
1543               }
1544               //
1545               aCurve.SetFirstCurve2d(C2d);
1546             }
1547             else { //// 
1548               Handle(Geom2d_BSplineCurve) H1;
1549               aCurve.SetFirstCurve2d(H1);
1550             }
1551
1552             if(myApprox2) { 
1553               Handle (Geom2d_Curve) C2d;
1554               BuildPCurves(fprm,lprm,Tolpc,myHS2->ChangeSurface().Surface(),newc,C2d);
1555               if(Tolpc>myTolReached2d || myTolReached2d==0) { 
1556                 myTolReached2d=Tolpc;
1557               }
1558               //
1559               aCurve.SetSecondCurve2d(C2d);
1560             }
1561             else { 
1562               Handle(Geom2d_BSplineCurve) H1;
1563               aCurve.SetSecondCurve2d(H1);
1564             }
1565             mySeqOfCurve.Append(aCurve);
1566             break;
1567           }
1568         }
1569         //
1570         Standard_Real aTwoPIdiv17, u1, v1, u2, v2, Tol;
1571
1572         aTwoPIdiv17=2.*M_PI/17.;
1573
1574         for (j=0; j<=17; j++) {
1575           gp_Pnt ptref (newc->Value (j*aTwoPIdiv17));
1576           Tol = Precision::Confusion();
1577
1578           Parameters(myHS1, myHS2, ptref, u1, v1, u2, v2);
1579           ok = (dom1->Classify(gp_Pnt2d(u1,v1),Tol) != TopAbs_OUT);
1580           if(ok) { 
1581             ok = (dom2->Classify(gp_Pnt2d(u2,v2),Tol) != TopAbs_OUT);
1582           }
1583           if (ok) {
1584             IntTools_Curve aCurve;
1585             aCurve.SetCurve(newc);
1586             //==============================================
1587             if (typl == IntPatch_Circle || typl == IntPatch_Ellipse) {
1588
1589               if(myApprox1) { 
1590                 Handle (Geom2d_Curve) C2d;
1591                 BuildPCurves(fprm, lprm, Tolpc, myHS1->ChangeSurface().Surface(), newc, C2d);
1592                 if(Tolpc>myTolReached2d || myTolReached2d==0) { 
1593                   myTolReached2d=Tolpc;
1594                 }
1595                 // 
1596                 aCurve.SetFirstCurve2d(C2d);
1597               }
1598               else { 
1599                 Handle(Geom2d_BSplineCurve) H1;
1600                 aCurve.SetFirstCurve2d(H1);
1601               }
1602
1603               if(myApprox2) { 
1604                 Handle (Geom2d_Curve) C2d;
1605                 BuildPCurves(fprm, lprm, Tolpc,myHS2->ChangeSurface().Surface(), newc, C2d);
1606                 if(Tolpc>myTolReached2d || myTolReached2d==0) { 
1607                   myTolReached2d=Tolpc;
1608                 }
1609                 //              
1610                 aCurve.SetSecondCurve2d(C2d);
1611               }
1612
1613               else { 
1614                 Handle(Geom2d_BSplineCurve) H1;
1615                 aCurve.SetSecondCurve2d(H1);
1616               }
1617             }//  end of if (typl == IntPatch_Circle || typl == IntPatch_Ellipse)
1618
1619             else { 
1620               Handle(Geom2d_BSplineCurve) H1;
1621               //        
1622               aCurve.SetFirstCurve2d(H1);
1623               aCurve.SetSecondCurve2d(H1);
1624             }
1625             //==============================================    
1626             //
1627             mySeqOfCurve.Append(aCurve);
1628             break;
1629
1630           }//  end of if (ok) {
1631         }//  end of for (Standard_Integer j=0; j<=17; j++)
1632       }//  end of else { on regarde si on garde
1633     }// for (i=1; i<=myLConstruct.NbParts(); i++)
1634   }// IntPatch_Circle: IntPatch_Ellipse:
1635   break;
1636
1637   case IntPatch_Analytic: {
1638     IntSurf_Quadric quad1,quad2;
1639     GeomAbs_SurfaceType typs = myHS1->Surface().GetType();
1640
1641     switch (typs) {
1642     case GeomAbs_Plane:
1643       quad1.SetValue(myHS1->Surface().Plane());
1644       break;
1645     case GeomAbs_Cylinder:
1646       quad1.SetValue(myHS1->Surface().Cylinder());
1647       break;
1648     case GeomAbs_Cone:
1649       quad1.SetValue(myHS1->Surface().Cone());
1650       break;
1651     case GeomAbs_Sphere:
1652       quad1.SetValue(myHS1->Surface().Sphere());
1653       break;
1654     default:
1655       Standard_ConstructionError::Raise("GeomInt_IntSS::MakeCurve 1");
1656     }
1657
1658     typs = myHS2->Surface().GetType();
1659
1660     switch (typs) {
1661     case GeomAbs_Plane:
1662       quad2.SetValue(myHS2->Surface().Plane());
1663       break;
1664     case GeomAbs_Cylinder:
1665       quad2.SetValue(myHS2->Surface().Cylinder());
1666       break;
1667     case GeomAbs_Cone:
1668       quad2.SetValue(myHS2->Surface().Cone());
1669       break;
1670     case GeomAbs_Sphere:
1671       quad2.SetValue(myHS2->Surface().Sphere());
1672       break;
1673     default:
1674       Standard_ConstructionError::Raise("GeomInt_IntSS::MakeCurve 2");
1675     }
1676     //
1677     //=========
1678     IntPatch_ALineToWLine convert (quad1, quad2);
1679
1680     if (!myApprox) {
1681       aNbParts=myLConstruct.NbParts();
1682       for (i=1; i<=aNbParts; i++) {
1683         myLConstruct.Part(i, fprm, lprm);
1684         Handle(IntPatch_WLine) WL = 
1685           convert.MakeWLine(Handle(IntPatch_ALine)::DownCast(L), fprm, lprm);
1686         //
1687         Handle(Geom2d_BSplineCurve) H1;
1688         Handle(Geom2d_BSplineCurve) H2;
1689
1690         if(myApprox1) {
1691           H1 = MakeBSpline2d(WL, 1, WL->NbPnts(), Standard_True);
1692         }
1693
1694         if(myApprox2) {
1695           H2 = MakeBSpline2d(WL, 1, WL->NbPnts(), Standard_False);
1696         }
1697         //       
1698         mySeqOfCurve.Append(IntTools_Curve(MakeBSpline(WL,1,WL->NbPnts()), H1, H2));
1699       }
1700     } // if (!myApprox)
1701
1702     else { // myApprox=TRUE
1703       GeomInt_WLApprox theapp3d;
1704       // 
1705       Standard_Real tol2d = myTolApprox;
1706       //        
1707       theapp3d.SetParameters(myTolApprox, tol2d, 4, 8, 0, Standard_True);
1708
1709       aNbParts=myLConstruct.NbParts();
1710       for (i=1; i<=aNbParts; i++) {
1711         myLConstruct.Part(i, fprm, lprm);
1712         Handle(IntPatch_WLine) WL = 
1713           convert.MakeWLine(Handle(IntPatch_ALine):: DownCast(L),fprm,lprm);
1714
1715         theapp3d.Perform(myHS1,myHS2,WL,Standard_True,myApprox1,myApprox2, 1, WL->NbPnts());
1716
1717         if (!theapp3d.IsDone()) {
1718           //
1719           Handle(Geom2d_BSplineCurve) H1;
1720           Handle(Geom2d_BSplineCurve) H2;
1721
1722           if(myApprox1) {
1723             H1 = MakeBSpline2d(WL, 1, WL->NbPnts(), Standard_True);
1724           }
1725
1726           if(myApprox2) {
1727             H2 = MakeBSpline2d(WL, 1, WL->NbPnts(), Standard_False);
1728           }
1729           //     
1730           mySeqOfCurve.Append(IntTools_Curve(MakeBSpline(WL,1,WL->NbPnts()), H1, H2));
1731         }
1732
1733         else {
1734           if(myApprox1 || myApprox2) { 
1735             if( theapp3d.TolReached2d()>myTolReached2d || myTolReached2d==0) { 
1736               myTolReached2d = theapp3d.TolReached2d();
1737             }
1738           }
1739
1740           if( theapp3d.TolReached3d()>myTolReached3d || myTolReached3d==0) { 
1741             myTolReached3d = theapp3d.TolReached3d();
1742           }
1743
1744           Standard_Integer aNbMultiCurves, nbpoles;
1745           aNbMultiCurves=theapp3d.NbMultiCurves();
1746           for (j=1; j<=aNbMultiCurves; j++) {
1747             const AppParCurves_MultiBSpCurve& mbspc = theapp3d.Value(j);
1748             nbpoles = mbspc.NbPoles();
1749
1750             TColgp_Array1OfPnt tpoles(1, nbpoles);
1751             mbspc.Curve(1, tpoles);
1752             Handle(Geom_BSplineCurve) BS=new Geom_BSplineCurve(tpoles,
1753               mbspc.Knots(),
1754               mbspc.Multiplicities(),
1755               mbspc.Degree());
1756
1757             GeomLib_CheckBSplineCurve Check(BS,TOLCHECK,TOLANGCHECK);
1758             Check.FixTangent(Standard_True,Standard_True);
1759             // 
1760             IntTools_Curve aCurve;
1761             aCurve.SetCurve(BS);
1762
1763             if(myApprox1) { 
1764               TColgp_Array1OfPnt2d tpoles2d(1,nbpoles);
1765               mbspc.Curve(2,tpoles2d);
1766               Handle(Geom2d_BSplineCurve) BS2=new Geom2d_BSplineCurve(tpoles2d,
1767                 mbspc.Knots(),
1768                 mbspc.Multiplicities(),
1769                 mbspc.Degree());
1770
1771               GeomLib_Check2dBSplineCurve newCheck(BS2,TOLCHECK,TOLANGCHECK);
1772               newCheck.FixTangent(Standard_True,Standard_True);
1773               //                
1774               aCurve.SetFirstCurve2d(BS2);
1775             }
1776             else {
1777               Handle(Geom2d_BSplineCurve) H1;
1778               aCurve.SetFirstCurve2d(H1);
1779             }
1780
1781             if(myApprox2) { 
1782               TColgp_Array1OfPnt2d tpoles2d(1, nbpoles);
1783               Standard_Integer TwoOrThree;
1784               TwoOrThree=myApprox1 ? 3 : 2;
1785               mbspc.Curve(TwoOrThree, tpoles2d);
1786               Handle(Geom2d_BSplineCurve) BS2 =new Geom2d_BSplineCurve(tpoles2d,
1787                 mbspc.Knots(),
1788                 mbspc.Multiplicities(),
1789                 mbspc.Degree());
1790
1791               GeomLib_Check2dBSplineCurve newCheck(BS2,TOLCHECK,TOLANGCHECK);
1792               newCheck.FixTangent(Standard_True,Standard_True);
1793               //        
1794               aCurve.SetSecondCurve2d(BS2);
1795             }
1796             else { 
1797               Handle(Geom2d_BSplineCurve) H2;
1798               aCurve.SetSecondCurve2d(H2);
1799             }
1800             // 
1801             mySeqOfCurve.Append(aCurve);
1802
1803           }// for (j=1; j<=aNbMultiCurves; j++) {
1804         }// else from if (!theapp3d.IsDone())
1805       }// for (i=1; i<=aNbParts; i++) {
1806     }// else { // myApprox=TRUE
1807   }// case IntPatch_Analytic:
1808   break;
1809
1810   case IntPatch_Walking:{
1811     Handle(IntPatch_WLine) WL = 
1812       Handle(IntPatch_WLine)::DownCast(L);
1813     //
1814     Standard_Integer ifprm, ilprm;
1815     //
1816     if (!myApprox) {
1817       aNbParts = 1;
1818       if(!bAvoidLineConstructor){
1819         aNbParts=myLConstruct.NbParts();
1820       }
1821       for (i=1; i<=aNbParts; ++i) {
1822         Handle(Geom2d_BSplineCurve) H1, H2;
1823         Handle(Geom_Curve) aBSp;
1824         //
1825         if(bAvoidLineConstructor) {
1826           ifprm = 1;
1827           ilprm = WL->NbPnts();
1828         }
1829         else {
1830           myLConstruct.Part(i, fprm, lprm);
1831           ifprm=(Standard_Integer)fprm;
1832           ilprm=(Standard_Integer)lprm;
1833         }
1834         //
1835         if(myApprox1) {
1836           H1 = MakeBSpline2d(WL, ifprm, ilprm, Standard_True);
1837         }
1838         //
1839         if(myApprox2) {
1840           H2 = MakeBSpline2d(WL, ifprm, ilprm, Standard_False);
1841         }
1842         //        
1843         aBSp=MakeBSpline(WL, ifprm, ilprm);
1844         IntTools_Curve aIC(aBSp, H1, H2);
1845         mySeqOfCurve.Append(aIC);
1846       }// for (i=1; i<=aNbParts; ++i) {
1847     }// if (!myApprox) {
1848     //
1849     else { // X
1850       Standard_Boolean bIsDecomposited;
1851       Standard_Integer nbiter, aNbSeqOfL;
1852       Standard_Real tol2d;
1853       IntPatch_SequenceOfLine aSeqOfL;
1854       GeomInt_WLApprox theapp3d;
1855       Approx_ParametrizationType aParType = Approx_ChordLength;
1856       //
1857       Standard_Boolean anApprox1 = myApprox1;
1858       Standard_Boolean anApprox2 = myApprox2;
1859
1860       tol2d = myTolApprox;
1861
1862       GeomAbs_SurfaceType typs1, typs2;
1863       typs1 = myHS1->Surface().GetType();
1864       typs2 = myHS2->Surface().GetType();
1865       Standard_Boolean anWithPC = Standard_True;
1866
1867       if(typs1 == GeomAbs_Cylinder && typs2 == GeomAbs_Sphere) {
1868         anWithPC = 
1869           ApproxWithPCurves(myHS1->Surface().Cylinder(), myHS2->Surface().Sphere());
1870       }
1871       else if (typs1 == GeomAbs_Sphere && typs2 == GeomAbs_Cylinder) {
1872         anWithPC = 
1873           ApproxWithPCurves(myHS2->Surface().Cylinder(), myHS1->Surface().Sphere());
1874       }
1875       if(!anWithPC) {
1876         //aParType = Approx_Centripetal; 
1877         myTolApprox = 1.e-5; 
1878         anApprox1 = Standard_False;
1879         anApprox2 = Standard_False;
1880         //      
1881         tol2d = myTolApprox;
1882       }
1883
1884       if(myHS1 == myHS2) { 
1885         //
1886         theapp3d.SetParameters(myTolApprox, tol2d, 4, 8, 0, Standard_False, aParType);
1887         rejectSurface = Standard_True;
1888       }
1889       else { 
1890         if(reApprox && !rejectSurface)
1891           theapp3d.SetParameters(myTolApprox, tol2d, 4, 8, 0, Standard_False, aParType);
1892         else {
1893           Standard_Integer iDegMax, iDegMin, iNbIter;
1894           //
1895           ApproxParameters(myHS1, myHS2, iDegMin, iDegMax, iNbIter);
1896           theapp3d.SetParameters(myTolApprox, tol2d, iDegMin, iDegMax, iNbIter, Standard_True, aParType);
1897           //
1898         }
1899       }
1900       //
1901       Standard_Real aReachedTol = Precision::Confusion();
1902       bIsDecomposited=DecompositionOfWLine(WL,
1903         myHS1, 
1904         myHS2, 
1905         myFace1, 
1906         myFace2, 
1907         myLConstruct, 
1908         bAvoidLineConstructor, 
1909         aSeqOfL, 
1910         aReachedTol,
1911         myContext);
1912       if ( bIsDecomposited && ( myTolReached3d < aReachedTol ) )
1913         myTolReached3d = aReachedTol;
1914
1915       //
1916       aNbSeqOfL=aSeqOfL.Length();
1917       //
1918       if (bIsDecomposited) {
1919         nbiter=aNbSeqOfL;
1920       }
1921       else {
1922         nbiter=1;
1923         aNbParts=1;
1924         if (!bAvoidLineConstructor) {
1925           aNbParts=myLConstruct.NbParts();
1926           nbiter=aNbParts;
1927         }
1928       }
1929       //
1930       // nbiter=(bIsDecomposited) ? aSeqOfL.Length() :
1931       //   ((bAvoidLineConstructor) ? 1 :aNbParts);
1932       //
1933       for(i = 1; i <= nbiter; ++i) {
1934         if(bIsDecomposited) {
1935           WL = Handle(IntPatch_WLine)::DownCast(aSeqOfL.Value(i));
1936           ifprm = 1;
1937           ilprm = WL->NbPnts();
1938         }
1939         else {
1940           if(bAvoidLineConstructor) {
1941             ifprm = 1;
1942             ilprm = WL->NbPnts();
1943           }
1944           else {
1945             myLConstruct.Part(i, fprm, lprm);
1946             ifprm = (Standard_Integer)fprm;
1947             ilprm = (Standard_Integer)lprm;
1948           }
1949         }
1950         //-- lbr : 
1951         //-- Si une des surfaces est un plan , on approxime en 2d
1952         //-- sur cette surface et on remonte les points 2d en 3d.
1953         if(typs1 == GeomAbs_Plane) { 
1954           theapp3d.Perform(myHS1, myHS2, WL, Standard_False,Standard_True, myApprox2,ifprm,ilprm);
1955         }         
1956         else if(typs2 == GeomAbs_Plane) { 
1957           theapp3d.Perform(myHS1,myHS2,WL,Standard_False,myApprox1,Standard_True,ifprm,ilprm);
1958         }
1959         else { 
1960           //
1961           if (myHS1 != myHS2){
1962             if ((typs1==GeomAbs_BezierSurface || typs1==GeomAbs_BSplineSurface) &&
1963               (typs2==GeomAbs_BezierSurface || typs2==GeomAbs_BSplineSurface)) {
1964
1965               theapp3d.SetParameters(myTolApprox, tol2d, 4, 8, 0, Standard_True, aParType);
1966
1967               Standard_Boolean bUseSurfaces;
1968               bUseSurfaces=NotUseSurfacesForApprox(myFace1, myFace2, WL, ifprm,  ilprm);
1969               if (bUseSurfaces) {
1970                 // ######
1971                 rejectSurface = Standard_True;
1972                 // ######
1973                 theapp3d.SetParameters(myTolApprox, tol2d, 4, 8, 0, Standard_False, aParType);
1974               }
1975             }
1976           }
1977           //
1978           theapp3d.Perform(myHS1,myHS2,WL,Standard_True,anApprox1,anApprox2,ifprm,ilprm);
1979         }
1980
1981         if (!theapp3d.IsDone()) {
1982           //      
1983           Handle(Geom2d_BSplineCurve) H1;
1984           //      
1985           Handle(Geom_Curve) aBSp=MakeBSpline(WL,ifprm, ilprm);
1986           Handle(Geom2d_BSplineCurve) H2;
1987
1988           if(myApprox1) {
1989             H1 = MakeBSpline2d(WL, ifprm, ilprm, Standard_True);
1990           }
1991
1992           if(myApprox2) {
1993             H2 = MakeBSpline2d(WL, ifprm, ilprm, Standard_False);
1994           }
1995           //      
1996           IntTools_Curve aIC(aBSp, H1, H2);
1997           mySeqOfCurve.Append(aIC);
1998         }
1999
2000         else {
2001           if(myApprox1 || myApprox2 || (typs1==GeomAbs_Plane || typs2==GeomAbs_Plane)) { 
2002             if( theapp3d.TolReached2d()>myTolReached2d || myTolReached2d==0.) { 
2003               myTolReached2d = theapp3d.TolReached2d();
2004             }
2005           }
2006           if(typs1==GeomAbs_Plane || typs2==GeomAbs_Plane) { 
2007             myTolReached3d = myTolReached2d;
2008             //
2009             if (typs1==GeomAbs_Torus || typs2==GeomAbs_Torus) {
2010               if (myTolReached3d<1.e-6) {
2011                 myTolReached3d = theapp3d.TolReached3d();
2012                 myTolReached3d=1.e-6;
2013               }
2014             }
2015             //
2016           }
2017           else  if( theapp3d.TolReached3d()>myTolReached3d || myTolReached3d==0.) { 
2018             myTolReached3d = theapp3d.TolReached3d();
2019           }
2020
2021           Standard_Integer aNbMultiCurves, nbpoles;
2022           aNbMultiCurves=theapp3d.NbMultiCurves(); 
2023           for (j=1; j<=aNbMultiCurves; j++) {
2024             if(typs1 == GeomAbs_Plane) {
2025               const AppParCurves_MultiBSpCurve& mbspc = theapp3d.Value(j);
2026               nbpoles = mbspc.NbPoles();
2027
2028               TColgp_Array1OfPnt2d tpoles2d(1,nbpoles);
2029               TColgp_Array1OfPnt   tpoles(1,nbpoles);
2030
2031               mbspc.Curve(1,tpoles2d);
2032               const gp_Pln&  Pln = myHS1->Surface().Plane();
2033               //
2034               Standard_Integer ik; 
2035               for(ik = 1; ik<= nbpoles; ik++) { 
2036                 tpoles.SetValue(ik,
2037                   ElSLib::Value(tpoles2d.Value(ik).X(),
2038                   tpoles2d.Value(ik).Y(),
2039                   Pln));
2040               }
2041               //
2042               Handle(Geom_BSplineCurve) BS = 
2043                 new Geom_BSplineCurve(tpoles,
2044                 mbspc.Knots(),
2045                 mbspc.Multiplicities(),
2046                 mbspc.Degree());
2047               GeomLib_CheckBSplineCurve Check(BS,TOLCHECK,TOLANGCHECK);
2048               Check.FixTangent(Standard_True, Standard_True);
2049               //        
2050               IntTools_Curve aCurve;
2051               aCurve.SetCurve(BS);
2052
2053               if(myApprox1) { 
2054                 Handle(Geom2d_BSplineCurve) BS1 = 
2055                   new Geom2d_BSplineCurve(tpoles2d,
2056                   mbspc.Knots(),
2057                   mbspc.Multiplicities(),
2058                   mbspc.Degree());
2059                 GeomLib_Check2dBSplineCurve Check1(BS1,TOLCHECK,TOLANGCHECK);
2060                 Check1.FixTangent(Standard_True,Standard_True);
2061                 //
2062                 // ############################################
2063                 if(!rejectSurface && !reApprox) {
2064                   Standard_Boolean isValid = IsCurveValid(BS1);
2065                   if(!isValid) {
2066                     reApprox = Standard_True;
2067                     goto reapprox;
2068                   }
2069                 }
2070                 // ############################################
2071                 aCurve.SetFirstCurve2d(BS1);
2072               }
2073               else {
2074                 Handle(Geom2d_BSplineCurve) H1;
2075                 aCurve.SetFirstCurve2d(H1);
2076               }
2077
2078               if(myApprox2) { 
2079                 mbspc.Curve(2, tpoles2d);
2080
2081                 Handle(Geom2d_BSplineCurve) BS2 = new Geom2d_BSplineCurve(tpoles2d,
2082                   mbspc.Knots(),
2083                   mbspc.Multiplicities(),
2084                   mbspc.Degree());
2085                 GeomLib_Check2dBSplineCurve newCheck(BS2,TOLCHECK,TOLANGCHECK);
2086                 newCheck.FixTangent(Standard_True,Standard_True);
2087
2088                 // ###########################################
2089                 if(!rejectSurface && !reApprox) {
2090                   Standard_Boolean isValid = IsCurveValid(BS2);
2091                   if(!isValid) {
2092                     reApprox = Standard_True;
2093                     goto reapprox;
2094                   }
2095                 }
2096                 // ###########################################
2097                 // 
2098                 aCurve.SetSecondCurve2d(BS2);
2099               }
2100               else { 
2101                 Handle(Geom2d_BSplineCurve) H2;
2102                 //              
2103                 aCurve.SetSecondCurve2d(H2);
2104               }
2105               //
2106               mySeqOfCurve.Append(aCurve);
2107             }
2108
2109             else if(typs2 == GeomAbs_Plane) { 
2110               const AppParCurves_MultiBSpCurve& mbspc = theapp3d.Value(j);
2111               nbpoles = mbspc.NbPoles();
2112
2113               TColgp_Array1OfPnt2d tpoles2d(1,nbpoles);
2114               TColgp_Array1OfPnt   tpoles(1,nbpoles);
2115               mbspc.Curve((myApprox1==Standard_True)? 2 : 1,tpoles2d);
2116               const gp_Pln&  Pln = myHS2->Surface().Plane();
2117               //
2118               Standard_Integer ik; 
2119               for(ik = 1; ik<= nbpoles; ik++) { 
2120                 tpoles.SetValue(ik,
2121                   ElSLib::Value(tpoles2d.Value(ik).X(),
2122                   tpoles2d.Value(ik).Y(),
2123                   Pln));
2124
2125               }
2126               //
2127               Handle(Geom_BSplineCurve) BS=new Geom_BSplineCurve(tpoles,
2128                 mbspc.Knots(),
2129                 mbspc.Multiplicities(),
2130                 mbspc.Degree());
2131               GeomLib_CheckBSplineCurve Check(BS,TOLCHECK,TOLANGCHECK);
2132               Check.FixTangent(Standard_True,Standard_True);
2133               //        
2134               IntTools_Curve aCurve;
2135               aCurve.SetCurve(BS);
2136
2137               if(myApprox2) {
2138                 Handle(Geom2d_BSplineCurve) BS1=new Geom2d_BSplineCurve(tpoles2d,
2139                   mbspc.Knots(),
2140                   mbspc.Multiplicities(),
2141                   mbspc.Degree());
2142                 GeomLib_Check2dBSplineCurve Check1(BS1,TOLCHECK,TOLANGCHECK);
2143                 Check1.FixTangent(Standard_True,Standard_True);
2144                 //      
2145                 // ###########################################
2146                 if(!rejectSurface && !reApprox) {
2147                   Standard_Boolean isValid = IsCurveValid(BS1);
2148                   if(!isValid) {
2149                     reApprox = Standard_True;
2150                     goto reapprox;
2151                   }
2152                 }
2153                 // ###########################################
2154                 bPCurvesOk = CheckPCurve(BS1, myFace2);
2155                 aCurve.SetSecondCurve2d(BS1);
2156               }
2157               else {
2158                 Handle(Geom2d_BSplineCurve) H2;
2159                 aCurve.SetSecondCurve2d(H2);
2160               }
2161
2162               if(myApprox1) { 
2163                 mbspc.Curve(1,tpoles2d);
2164                 Handle(Geom2d_BSplineCurve) BS2=new Geom2d_BSplineCurve(tpoles2d,
2165                   mbspc.Knots(),
2166                   mbspc.Multiplicities(),
2167                   mbspc.Degree());
2168                 GeomLib_Check2dBSplineCurve Check2(BS2,TOLCHECK,TOLANGCHECK);
2169                 Check2.FixTangent(Standard_True,Standard_True);
2170                 //
2171                 // ###########################################
2172                 if(!rejectSurface && !reApprox) {
2173                   Standard_Boolean isValid = IsCurveValid(BS2);
2174                   if(!isValid) {
2175                     reApprox = Standard_True;
2176                     goto reapprox;
2177                   }
2178                 }
2179                 // ###########################################
2180                 bPCurvesOk = bPCurvesOk && CheckPCurve(BS2, myFace1);
2181                 aCurve.SetFirstCurve2d(BS2);
2182               }
2183               else { 
2184                 Handle(Geom2d_BSplineCurve) H1;
2185                 //              
2186                 aCurve.SetFirstCurve2d(H1);
2187               }
2188               //
2189               //if points of the pcurves are out of the faces bounds
2190               //create 3d and 2d curves without approximation
2191               if (!bPCurvesOk) {
2192                 Handle(Geom2d_BSplineCurve) H1, H2;
2193                 bPCurvesOk = Standard_True;
2194                 //        
2195                 Handle(Geom_Curve) aBSp=MakeBSpline(WL,ifprm, ilprm);
2196
2197                 if(myApprox1) {
2198                   H1 = MakeBSpline2d(WL, ifprm, ilprm, Standard_True);
2199                   bPCurvesOk = CheckPCurve(H1, myFace1);
2200                 }
2201
2202                 if(myApprox2) {
2203                   H2 = MakeBSpline2d(WL, ifprm, ilprm, Standard_False);
2204                   bPCurvesOk = bPCurvesOk && CheckPCurve(H2, myFace2);
2205                 }
2206                 //
2207                 //if pcurves created without approximation are out of the 
2208                 //faces bounds, use approximated 3d and 2d curves
2209                 if (bPCurvesOk) {
2210                   IntTools_Curve aIC(aBSp, H1, H2);
2211                   mySeqOfCurve.Append(aIC);
2212                 } else {
2213                   mySeqOfCurve.Append(aCurve);
2214                 }
2215               } else {
2216                 mySeqOfCurve.Append(aCurve);
2217               }
2218             }
2219             else { 
2220               const AppParCurves_MultiBSpCurve& mbspc = theapp3d.Value(j);
2221               nbpoles = mbspc.NbPoles();
2222               TColgp_Array1OfPnt tpoles(1,nbpoles);
2223               mbspc.Curve(1,tpoles);
2224               Handle(Geom_BSplineCurve) BS=new Geom_BSplineCurve(tpoles,
2225                 mbspc.Knots(),
2226                 mbspc.Multiplicities(),
2227                 mbspc.Degree());
2228               GeomLib_CheckBSplineCurve Check(BS,TOLCHECK,TOLANGCHECK);
2229               Check.FixTangent(Standard_True,Standard_True);
2230               //                
2231               IntTools_Curve aCurve;
2232               aCurve.SetCurve(BS);
2233
2234               if(myApprox1) { 
2235                 if(anApprox1) {
2236                   TColgp_Array1OfPnt2d tpoles2d(1,nbpoles);
2237                   mbspc.Curve(2,tpoles2d);
2238                   Handle(Geom2d_BSplineCurve) BS1=new Geom2d_BSplineCurve(tpoles2d,
2239                     mbspc.Knots(),
2240                     mbspc.Multiplicities(),
2241                     mbspc.Degree());
2242                   GeomLib_Check2dBSplineCurve newCheck(BS1,TOLCHECK,TOLANGCHECK);
2243                   newCheck.FixTangent(Standard_True,Standard_True);
2244                   //    
2245                   aCurve.SetFirstCurve2d(BS1);
2246                 }
2247                 else {
2248                   Handle(Geom2d_BSplineCurve) BS1;
2249                   fprm = BS->FirstParameter();
2250                   lprm = BS->LastParameter();
2251
2252                   Handle(Geom2d_Curve) C2d;
2253                   Standard_Real aTol = myTolApprox;
2254                   BuildPCurves(fprm, lprm, aTol, myHS1->ChangeSurface().Surface(), BS, C2d);
2255                   BS1 = Handle(Geom2d_BSplineCurve)::DownCast(C2d);
2256                   aCurve.SetFirstCurve2d(BS1);
2257                 }
2258
2259               }
2260               else {
2261                 Handle(Geom2d_BSplineCurve) H1;
2262                 //              
2263                 aCurve.SetFirstCurve2d(H1);
2264               }
2265               if(myApprox2) { 
2266                 if(anApprox2) {
2267                   TColgp_Array1OfPnt2d tpoles2d(1,nbpoles);
2268                   mbspc.Curve((myApprox1==Standard_True)? 3 : 2,tpoles2d);
2269                   Handle(Geom2d_BSplineCurve) BS2=new Geom2d_BSplineCurve(tpoles2d,
2270                     mbspc.Knots(),
2271                     mbspc.Multiplicities(),
2272                     mbspc.Degree());
2273                   GeomLib_Check2dBSplineCurve newCheck(BS2,TOLCHECK,TOLANGCHECK);
2274                   newCheck.FixTangent(Standard_True,Standard_True);
2275                   //            
2276                   aCurve.SetSecondCurve2d(BS2);
2277                 }
2278                 else {
2279                   Handle(Geom2d_BSplineCurve) BS2;
2280                   fprm = BS->FirstParameter();
2281                   lprm = BS->LastParameter();
2282
2283                   Handle(Geom2d_Curve) C2d;
2284                   Standard_Real aTol = myTolApprox;
2285                   BuildPCurves(fprm, lprm, aTol, myHS2->ChangeSurface().Surface(), BS, C2d);
2286                   BS2 = Handle(Geom2d_BSplineCurve)::DownCast(C2d);
2287                   aCurve.SetSecondCurve2d(BS2);
2288                 }
2289
2290               }
2291               else { 
2292                 Handle(Geom2d_BSplineCurve) H2;
2293                 //              
2294                 aCurve.SetSecondCurve2d(H2);
2295               }
2296               //
2297               mySeqOfCurve.Append(aCurve);
2298             }
2299           }
2300         }
2301       }
2302     }// else { // X
2303   }// case IntPatch_Walking:{
2304   break;
2305
2306   case IntPatch_Restriction: 
2307     break;
2308
2309   }
2310 }
2311
2312 //=======================================================================
2313 //function : BuildPCurves
2314 //purpose  : 
2315 //=======================================================================
2316  void BuildPCurves (Standard_Real f,
2317                     Standard_Real l,
2318                     Standard_Real& Tol,
2319                     const Handle (Geom_Surface)& S,
2320                     const Handle (Geom_Curve)&   C,
2321                     Handle (Geom2d_Curve)& C2d)
2322 {
2323
2324   Standard_Real umin,umax,vmin,vmax;
2325   // 
2326
2327   if (C2d.IsNull()) {
2328
2329     // in class ProjLib_Function the range of parameters is shrank by 1.e-09
2330     if((l - f) > 2.e-09) {
2331       C2d = GeomProjLib::Curve2d(C,f,l,S,Tol);
2332       //
2333       if (C2d.IsNull()) {
2334         // proj. a circle that goes through the pole on a sphere to the sphere     
2335         Tol=Tol+1.e-7;
2336         C2d = GeomProjLib::Curve2d(C,f,l,S,Tol);
2337       }
2338     }
2339     else {
2340       if((l - f) > Epsilon(Abs(f))) {
2341         GeomAPI_ProjectPointOnSurf aProjector1, aProjector2;
2342         gp_Pnt P1 = C->Value(f);
2343         gp_Pnt P2 = C->Value(l);
2344         aProjector1.Init(P1, S);
2345         aProjector2.Init(P2, S);
2346
2347         if(aProjector1.IsDone() && aProjector2.IsDone()) {
2348           Standard_Real U=0., V=0.;
2349           aProjector1.LowerDistanceParameters(U, V);
2350           gp_Pnt2d p1(U, V);
2351
2352           aProjector2.LowerDistanceParameters(U, V);
2353           gp_Pnt2d p2(U, V);
2354
2355           if(p1.Distance(p2) > gp::Resolution()) {
2356             TColgp_Array1OfPnt2d poles(1,2);
2357             TColStd_Array1OfReal knots(1,2);
2358             TColStd_Array1OfInteger mults(1,2);
2359             poles(1) = p1;
2360             poles(2) = p2;
2361             knots(1) = f;
2362             knots(2) = l;
2363             mults(1) = mults(2) = 2;
2364
2365             C2d = new Geom2d_BSplineCurve(poles,knots,mults,1);
2366
2367             // compute reached tolerance.begin
2368             gp_Pnt PMid = C->Value((f + l) * 0.5);
2369             aProjector1.Perform(PMid);
2370
2371             if(aProjector1.IsDone()) {
2372               aProjector1.LowerDistanceParameters(U, V);
2373               gp_Pnt2d pmidproj(U, V);
2374               gp_Pnt2d pmidcurve2d = C2d->Value((f + l) * 0.5);
2375               Standard_Real adist = pmidcurve2d.Distance(pmidproj);
2376               Tol = (adist > Tol) ? adist : Tol;
2377             }
2378             // compute reached tolerance.end
2379           }
2380         }
2381       }
2382     }
2383     //
2384     S->Bounds(umin, umax, vmin, vmax);
2385
2386     if (S->IsUPeriodic() && !C2d.IsNull()) {
2387       // Recadre dans le domaine UV de la face
2388       Standard_Real period, U0, du, aEps; 
2389       
2390       du =0.0;
2391       aEps=Precision::PConfusion();
2392       period = S->UPeriod();
2393       gp_Pnt2d Pf = C2d->Value(f);
2394       U0=Pf.X();
2395       //
2396       gp_Pnt2d Pl = C2d->Value(l);
2397       
2398       U0 = Min(Pl.X(), U0);
2399 //       while(U0-umin<aEps) { 
2400       while(U0-umin<-aEps) { 
2401         U0+=period;
2402         du+=period;
2403       }
2404       //
2405       while(U0-umax>aEps) { 
2406         U0-=period;
2407         du-=period;
2408       }
2409       if (du != 0) {
2410         gp_Vec2d T1(du,0.);
2411         C2d->Translate(T1);
2412       }
2413     }
2414   }
2415 }
2416
2417 //=======================================================================
2418 //function : Parameters
2419 //purpose  : 
2420 //=======================================================================
2421  void Parameters(const Handle(GeomAdaptor_HSurface)& HS1,
2422                  const Handle(GeomAdaptor_HSurface)& HS2,
2423                  const gp_Pnt& Ptref,
2424                  Standard_Real& U1,
2425                  Standard_Real& V1,
2426                  Standard_Real& U2,
2427                  Standard_Real& V2)
2428 {
2429
2430   IntSurf_Quadric quad1,quad2;
2431   GeomAbs_SurfaceType typs = HS1->Surface().GetType();
2432
2433   switch (typs) {
2434   case GeomAbs_Plane:
2435     quad1.SetValue(HS1->Surface().Plane());
2436     break;
2437   case GeomAbs_Cylinder:
2438     quad1.SetValue(HS1->Surface().Cylinder());
2439     break;
2440   case GeomAbs_Cone:
2441     quad1.SetValue(HS1->Surface().Cone());
2442     break;
2443   case GeomAbs_Sphere:
2444     quad1.SetValue(HS1->Surface().Sphere());
2445     break;
2446   default:
2447     Standard_ConstructionError::Raise("GeomInt_IntSS::MakeCurve");
2448   }
2449   
2450   typs = HS2->Surface().GetType();
2451   switch (typs) {
2452   case GeomAbs_Plane:
2453     quad2.SetValue(HS2->Surface().Plane());
2454     break;
2455   case GeomAbs_Cylinder:
2456     quad2.SetValue(HS2->Surface().Cylinder());
2457     break;
2458   case GeomAbs_Cone:
2459     quad2.SetValue(HS2->Surface().Cone());
2460     break;
2461   case GeomAbs_Sphere:
2462     quad2.SetValue(HS2->Surface().Sphere());
2463     break;
2464   default:
2465     Standard_ConstructionError::Raise("GeomInt_IntSS::MakeCurve");
2466   }
2467
2468   quad1.Parameters(Ptref,U1,V1);
2469   quad2.Parameters(Ptref,U2,V2);
2470 }
2471
2472 //=======================================================================
2473 //function : MakeBSpline
2474 //purpose  : 
2475 //=======================================================================
2476 Handle(Geom_Curve) MakeBSpline  (const Handle(IntPatch_WLine)& WL,
2477                                  const Standard_Integer ideb,
2478                                  const Standard_Integer ifin)
2479 {
2480   Standard_Integer i,nbpnt = ifin-ideb+1;
2481   TColgp_Array1OfPnt poles(1,nbpnt);
2482   TColStd_Array1OfReal knots(1,nbpnt);
2483   TColStd_Array1OfInteger mults(1,nbpnt);
2484   Standard_Integer ipidebm1;
2485   for(i=1,ipidebm1=i+ideb-1; i<=nbpnt;ipidebm1++, i++) {
2486     poles(i) = WL->Point(ipidebm1).Value();
2487     mults(i) = 1;
2488     knots(i) = i-1;
2489   }
2490   mults(1) = mults(nbpnt) = 2;
2491   return
2492     new Geom_BSplineCurve(poles,knots,mults,1);
2493 }
2494 //
2495
2496 //=======================================================================
2497 //function : MakeBSpline2d
2498 //purpose  : 
2499 //=======================================================================
2500 Handle(Geom2d_BSplineCurve) MakeBSpline2d(const Handle(IntPatch_WLine)& theWLine,
2501                                           const Standard_Integer ideb,
2502                                           const Standard_Integer ifin,
2503                                           const Standard_Boolean onFirst)
2504 {
2505   Standard_Integer i, nbpnt = ifin-ideb+1;
2506   TColgp_Array1OfPnt2d poles(1,nbpnt);
2507   TColStd_Array1OfReal knots(1,nbpnt);
2508   TColStd_Array1OfInteger mults(1,nbpnt);
2509   Standard_Integer ipidebm1;
2510
2511   for(i = 1, ipidebm1 = i+ideb-1; i <= nbpnt; ipidebm1++, i++) {
2512       Standard_Real U, V;
2513       if(onFirst)
2514         theWLine->Point(ipidebm1).ParametersOnS1(U, V);
2515       else
2516         theWLine->Point(ipidebm1).ParametersOnS2(U, V);
2517       poles(i).SetCoord(U, V);
2518       mults(i) = 1;
2519       knots(i) = i-1;
2520     }
2521     mults(1) = mults(nbpnt) = 2;
2522
2523   return new Geom2d_BSplineCurve(poles,knots,mults,1);
2524 }
2525 //=======================================================================
2526 //function : PrepareLines3D
2527 //purpose  : 
2528 //=======================================================================
2529   void IntTools_FaceFace::PrepareLines3D(const Standard_Boolean bToSplit)
2530 {
2531   Standard_Integer i, aNbCurves;
2532   GeomAbs_SurfaceType aType1, aType2;
2533   IntTools_SequenceOfCurves aNewCvs;
2534   //
2535   // 1. Treatment closed  curves
2536   aNbCurves=mySeqOfCurve.Length();
2537   for (i=1; i<=aNbCurves; ++i) {
2538     const IntTools_Curve& aIC=mySeqOfCurve(i);
2539     //
2540     if (bToSplit) {
2541       Standard_Integer j, aNbC;
2542       IntTools_SequenceOfCurves aSeqCvs;
2543       //
2544       aNbC=IntTools_Tools::SplitCurve(aIC, aSeqCvs);
2545       if (aNbC) {
2546         for (j=1; j<=aNbC; ++j) {
2547           const IntTools_Curve& aICNew=aSeqCvs(j);
2548           aNewCvs.Append(aICNew);
2549         }
2550       }
2551       else {
2552         aNewCvs.Append(aIC);
2553       }
2554     }
2555     else {
2556       aNewCvs.Append(aIC);
2557     }
2558   }
2559   //
2560   // 2. Plane\Cone intersection when we had 4 curves
2561   aType1=myHS1->GetType();
2562   aType2=myHS2->GetType();
2563   aNbCurves=aNewCvs.Length();
2564   //
2565   if ((aType1==GeomAbs_Plane && aType2==GeomAbs_Cone) ||
2566       (aType2==GeomAbs_Plane && aType1==GeomAbs_Cone)) {
2567     if (aNbCurves==4) {
2568       GeomAbs_CurveType aCType1;
2569       //
2570       aCType1=aNewCvs(1).Type();
2571       if (aCType1==GeomAbs_Line) {
2572         IntTools_SequenceOfCurves aSeqIn, aSeqOut;
2573         //
2574         for (i=1; i<=aNbCurves; ++i) {
2575           const IntTools_Curve& aIC=aNewCvs(i);
2576           aSeqIn.Append(aIC);
2577         }
2578         //
2579         IntTools_Tools::RejectLines(aSeqIn, aSeqOut);
2580         //
2581         aNewCvs.Clear();
2582         aNbCurves=aSeqOut.Length(); 
2583         for (i=1; i<=aNbCurves; ++i) {
2584           const IntTools_Curve& aIC=aSeqOut(i);
2585           aNewCvs.Append(aIC);
2586         }
2587       }
2588     }
2589   }// if ((aType1==GeomAbs_Plane && aType2==GeomAbs_Cone)...
2590   //
2591   // 3. Fill  mySeqOfCurve
2592   mySeqOfCurve.Clear();
2593   aNbCurves=aNewCvs.Length();
2594   for (i=1; i<=aNbCurves; ++i) {
2595     const IntTools_Curve& aIC=aNewCvs(i);
2596     mySeqOfCurve.Append(aIC);
2597   }
2598 }
2599 //=======================================================================
2600 //function : CorrectSurfaceBoundaries
2601 //purpose  : 
2602 //=======================================================================
2603  void CorrectSurfaceBoundaries(const TopoDS_Face&  theFace,
2604                                const Standard_Real theTolerance,
2605                                Standard_Real&      theumin,
2606                                Standard_Real&      theumax, 
2607                                Standard_Real&      thevmin, 
2608                                Standard_Real&      thevmax) 
2609 {
2610   Standard_Boolean enlarge, isuperiodic, isvperiodic;
2611   Standard_Real uinf, usup, vinf, vsup, delta;
2612   GeomAbs_SurfaceType aType;
2613   Handle(Geom_Surface) aSurface;
2614   //
2615   aSurface = BRep_Tool::Surface(theFace);
2616   aSurface->Bounds(uinf, usup, vinf, vsup);
2617   delta = theTolerance;
2618   enlarge = Standard_False;
2619   //
2620   GeomAdaptor_Surface anAdaptorSurface(aSurface);
2621   //
2622   if(aSurface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
2623     Handle(Geom_Surface) aBasisSurface = 
2624       (Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface))->BasisSurface();
2625     
2626     if(aBasisSurface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface)) ||
2627        aBasisSurface->IsKind(STANDARD_TYPE(Geom_OffsetSurface))) {
2628       return;
2629     }
2630   }
2631   //
2632   if(aSurface->IsKind(STANDARD_TYPE(Geom_OffsetSurface))) {
2633     Handle(Geom_Surface) aBasisSurface = 
2634       (Handle(Geom_OffsetSurface)::DownCast(aSurface))->BasisSurface();
2635     
2636     if(aBasisSurface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface)) ||
2637        aBasisSurface->IsKind(STANDARD_TYPE(Geom_OffsetSurface))) {
2638       return;
2639     }
2640   }
2641   //
2642   isuperiodic = anAdaptorSurface.IsUPeriodic();
2643   isvperiodic = anAdaptorSurface.IsVPeriodic();
2644   //
2645   aType=anAdaptorSurface.GetType();
2646   if((aType==GeomAbs_BezierSurface) ||
2647      (aType==GeomAbs_BSplineSurface) ||
2648      (aType==GeomAbs_SurfaceOfExtrusion) ||
2649      (aType==GeomAbs_SurfaceOfRevolution)) {
2650     enlarge=Standard_True;
2651   }
2652   //
2653   if(!isuperiodic && enlarge) {
2654
2655     if((theumin - uinf) > delta )
2656       theumin -= delta;
2657     else {
2658       theumin = uinf;
2659     }
2660
2661     if((usup - theumax) > delta )
2662       theumax += delta;
2663     else
2664       theumax = usup;
2665   }
2666   //
2667   if(!isvperiodic && enlarge) {
2668     if((thevmin - vinf) > delta ) {
2669       thevmin -= delta;
2670     }
2671     else { 
2672       thevmin = vinf;
2673     }
2674     if((vsup - thevmax) > delta ) {
2675       thevmax += delta;
2676     }
2677     else {
2678       thevmax = vsup;
2679     }
2680   }
2681   //
2682   {
2683     Standard_Integer aNbP;
2684     Standard_Real aXP, dXfact, aXmid, aX1, aX2, aTolPA;
2685     //
2686     aTolPA=Precision::Angular();
2687     // U
2688     if (isuperiodic) {
2689       aXP=anAdaptorSurface.UPeriod();
2690       dXfact=theumax-theumin;
2691       if (dXfact-aTolPA>aXP) {
2692         aXmid=0.5*(theumax+theumin);
2693         aNbP=RealToInt(aXmid/aXP);
2694         if (aXmid<0.) {
2695           aNbP=aNbP-1;
2696         }
2697         aX1=aNbP*aXP;
2698         if (theumin>aTolPA) {
2699           aX1=theumin+aNbP*aXP;
2700         }
2701         aX2=aX1+aXP;
2702         if (theumin<aX1) {
2703           theumin=aX1;
2704         }
2705         if (theumax>aX2) {
2706           theumax=aX2;
2707         }
2708       }
2709     }
2710     // V
2711     if (isvperiodic) {
2712       aXP=anAdaptorSurface.VPeriod();
2713       dXfact=thevmax-thevmin;
2714       if (dXfact-aTolPA>aXP) {
2715         aXmid=0.5*(thevmax+thevmin);
2716         aNbP=RealToInt(aXmid/aXP);
2717         if (aXmid<0.) {
2718           aNbP=aNbP-1;
2719         }
2720         aX1=aNbP*aXP;
2721         if (thevmin>aTolPA) {
2722           aX1=thevmin+aNbP*aXP;
2723         }
2724         aX2=aX1+aXP;
2725         if (thevmin<aX1) {
2726           thevmin=aX1;
2727         }
2728         if (thevmax>aX2) {
2729           thevmax=aX2;
2730         }
2731       }
2732     }
2733   }
2734   //
2735   if(isuperiodic || isvperiodic) {
2736     Standard_Boolean correct = Standard_False;
2737     Standard_Boolean correctU = Standard_False;
2738     Standard_Boolean correctV = Standard_False;
2739     Bnd_Box2d aBox;
2740     TopExp_Explorer anExp;
2741
2742     for(anExp.Init(theFace, TopAbs_EDGE); anExp.More(); anExp.Next()) {
2743       if(BRep_Tool::IsClosed(TopoDS::Edge(anExp.Current()), theFace)) {
2744         correct = Standard_True;
2745         Standard_Real f, l;
2746         TopoDS_Edge anEdge = TopoDS::Edge(anExp.Current());
2747         
2748         for(Standard_Integer i = 0; i < 2; i++) {
2749           if(i==0) {
2750             anEdge.Orientation(TopAbs_FORWARD);
2751           }
2752           else {
2753             anEdge.Orientation(TopAbs_REVERSED);
2754           }
2755           Handle(Geom2d_Curve) aCurve = BRep_Tool::CurveOnSurface(anEdge, theFace, f, l);
2756           
2757           if(aCurve.IsNull()) {
2758             correct = Standard_False;
2759             break;
2760           }
2761           Handle(Geom2d_Line) aLine = Handle(Geom2d_Line)::DownCast(aCurve);
2762
2763           if(aLine.IsNull()) {
2764             correct = Standard_False;
2765             break;
2766           }
2767           gp_Dir2d anUDir(1., 0.);
2768           gp_Dir2d aVDir(0., 1.);
2769           Standard_Real anAngularTolerance = Precision::Angular();
2770
2771           correctU = correctU || aLine->Position().Direction().IsParallel(aVDir, anAngularTolerance);
2772           correctV = correctV || aLine->Position().Direction().IsParallel(anUDir, anAngularTolerance);
2773           
2774           gp_Pnt2d pp1 = aCurve->Value(f);
2775           aBox.Add(pp1);
2776           gp_Pnt2d pp2 = aCurve->Value(l);
2777           aBox.Add(pp2);
2778         }
2779         if(!correct)
2780           break;
2781       }
2782     }
2783
2784     if(correct) {
2785       Standard_Real umin, vmin, umax, vmax;
2786       aBox.Get(umin, vmin, umax, vmax);
2787
2788       if(isuperiodic && correctU) {
2789         
2790         if(theumin < umin)
2791           theumin = umin;
2792         
2793         if(theumax > umax) {
2794           theumax = umax;
2795         }
2796       }
2797       if(isvperiodic && correctV) {
2798         
2799         if(thevmin < vmin)
2800           thevmin = vmin;
2801         if(thevmax > vmax)
2802           thevmax = vmax;
2803       }
2804     }
2805   }
2806 }
2807 //
2808 //
2809 // The block is dedicated to determine whether WLine [ifprm, ilprm]
2810 // crosses the degenerated zone on each given surface or not.
2811 // If Yes -> We will not use info about surfaces during approximation
2812 // because inside degenerated zone of the surface the approx. algo.
2813 // uses wrong values of normal, etc., and resulting curve will have
2814 // oscillations that we would not like to have. 
2815  
2816
2817
2818 static
2819   Standard_Boolean IsDegeneratedZone(const gp_Pnt2d& aP2d,
2820                                      const Handle(Geom_Surface)& aS,
2821                                      const Standard_Integer iDir);
2822 static
2823   Standard_Boolean IsPointInDegeneratedZone(const IntSurf_PntOn2S& aP2S,
2824                                             const TopoDS_Face& aF1,
2825                                             const TopoDS_Face& aF2);
2826 //=======================================================================
2827 //function :  NotUseSurfacesForApprox
2828 //purpose  : 
2829 //=======================================================================
2830 Standard_Boolean NotUseSurfacesForApprox(const TopoDS_Face& aF1,
2831                                          const TopoDS_Face& aF2,
2832                                          const Handle(IntPatch_WLine)& WL,
2833                                          const Standard_Integer ifprm,
2834                                          const Standard_Integer ilprm)
2835 {
2836   Standard_Boolean bPInDZ;
2837
2838   Handle(IntSurf_LineOn2S) aLineOn2S=WL->Curve();
2839   
2840   const IntSurf_PntOn2S& aP2Sfprm=aLineOn2S->Value(ifprm);
2841   bPInDZ=IsPointInDegeneratedZone(aP2Sfprm, aF1, aF2);
2842   if (bPInDZ) {
2843     return bPInDZ;
2844   }
2845
2846   const IntSurf_PntOn2S& aP2Slprm=aLineOn2S->Value(ilprm);
2847   bPInDZ=IsPointInDegeneratedZone(aP2Slprm, aF1, aF2);
2848   
2849   return bPInDZ;
2850 }
2851 //=======================================================================
2852 //function : IsPointInDegeneratedZone
2853 //purpose  : 
2854 //=======================================================================
2855 Standard_Boolean IsPointInDegeneratedZone(const IntSurf_PntOn2S& aP2S,
2856                                           const TopoDS_Face& aF1,
2857                                           const TopoDS_Face& aF2)
2858                                           
2859 {
2860   Standard_Boolean bFlag=Standard_True;
2861   Standard_Real US11, US12, VS11, VS12, US21, US22, VS21, VS22;
2862   Standard_Real U1, V1, U2, V2, aDelta, aD;
2863   gp_Pnt2d aP2d;
2864
2865   Handle(Geom_Surface)aS1 = BRep_Tool::Surface(aF1);
2866   aS1->Bounds(US11, US12, VS11, VS12);
2867   GeomAdaptor_Surface aGAS1(aS1);
2868
2869   Handle(Geom_Surface)aS2 = BRep_Tool::Surface(aF2);
2870   aS1->Bounds(US21, US22, VS21, VS22);
2871   GeomAdaptor_Surface aGAS2(aS2);
2872   //
2873   //const gp_Pnt& aP=aP2S.Value();
2874   aP2S.Parameters(U1, V1, U2, V2);
2875   //
2876   aDelta=1.e-7;
2877   // Check on Surf 1
2878   aD=aGAS1.UResolution(aDelta);
2879   aP2d.SetCoord(U1, V1);
2880   if (fabs(U1-US11) < aD) {
2881     bFlag=IsDegeneratedZone(aP2d, aS1, 1);
2882     if (bFlag) {
2883       return bFlag;
2884     }
2885   }
2886   if (fabs(U1-US12) < aD) {
2887     bFlag=IsDegeneratedZone(aP2d, aS1, 1);
2888     if (bFlag) {
2889       return bFlag;
2890     }
2891   }
2892   aD=aGAS1.VResolution(aDelta);
2893   if (fabs(V1-VS11) < aDelta) {
2894     bFlag=IsDegeneratedZone(aP2d, aS1, 2);
2895     if (bFlag) {
2896       return bFlag;
2897     }
2898   }
2899   if (fabs(V1-VS12) < aDelta) {
2900     bFlag=IsDegeneratedZone(aP2d, aS1, 2);
2901     if (bFlag) {
2902       return bFlag;
2903     }
2904   }
2905   // Check on Surf 2
2906   aD=aGAS2.UResolution(aDelta);
2907   aP2d.SetCoord(U2, V2);
2908   if (fabs(U2-US21) < aDelta) {
2909     bFlag=IsDegeneratedZone(aP2d, aS2, 1);
2910     if (bFlag) {
2911       return bFlag;
2912     }
2913   }
2914   if (fabs(U2-US22) < aDelta) {
2915     bFlag=IsDegeneratedZone(aP2d, aS2, 1);
2916     if (bFlag) {
2917       return bFlag;
2918     }
2919   }
2920   aD=aGAS2.VResolution(aDelta);
2921   if (fabs(V2-VS21) < aDelta) {
2922     bFlag=IsDegeneratedZone(aP2d, aS2, 2);
2923     if (bFlag) {  
2924       return bFlag;
2925     }
2926   }
2927   if (fabs(V2-VS22) < aDelta) {
2928     bFlag=IsDegeneratedZone(aP2d, aS2, 2);
2929     if (bFlag) {
2930       return bFlag;
2931     }
2932   }
2933   return !bFlag;
2934 }
2935
2936 //=======================================================================
2937 //function : IsDegeneratedZone
2938 //purpose  : 
2939 //=======================================================================
2940 Standard_Boolean IsDegeneratedZone(const gp_Pnt2d& aP2d,
2941                                    const Handle(Geom_Surface)& aS,
2942                                    const Standard_Integer iDir)
2943 {
2944   Standard_Boolean bFlag=Standard_True;
2945   Standard_Real US1, US2, VS1, VS2, dY, dX, d1, d2, dD;
2946   Standard_Real aXm, aYm, aXb, aYb, aXe, aYe;
2947   aS->Bounds(US1, US2, VS1, VS2); 
2948
2949   gp_Pnt aPm, aPb, aPe;
2950   
2951   aXm=aP2d.X();
2952   aYm=aP2d.Y();
2953   
2954   aS->D0(aXm, aYm, aPm); 
2955   
2956   dX=1.e-5;
2957   dY=1.e-5;
2958   dD=1.e-12;
2959
2960   if (iDir==1) {
2961     aXb=aXm;
2962     aXe=aXm;
2963     aYb=aYm-dY;
2964     if (aYb < VS1) {
2965       aYb=VS1;
2966     }
2967     aYe=aYm+dY;
2968     if (aYe > VS2) {
2969       aYe=VS2;
2970     }
2971     aS->D0(aXb, aYb, aPb);
2972     aS->D0(aXe, aYe, aPe);
2973     
2974     d1=aPm.Distance(aPb);
2975     d2=aPm.Distance(aPe);
2976     if (d1 < dD && d2 < dD) {
2977       return bFlag;
2978     }
2979     return !bFlag;
2980   }
2981   //
2982   else if (iDir==2) {
2983     aYb=aYm;
2984     aYe=aYm;
2985     aXb=aXm-dX;
2986     if (aXb < US1) {
2987       aXb=US1;
2988     }
2989     aXe=aXm+dX;
2990     if (aXe > US2) {
2991       aXe=US2;
2992     }
2993     aS->D0(aXb, aYb, aPb);
2994     aS->D0(aXe, aYe, aPe);
2995     
2996     d1=aPm.Distance(aPb);
2997     d2=aPm.Distance(aPe);
2998     if (d1 < dD && d2 < dD) {
2999       return bFlag;
3000     }
3001     return !bFlag;
3002   }
3003   return !bFlag;
3004 }
3005
3006 //=========================================================================
3007 // static function : ComputePurgedWLine
3008 // purpose : Removes equal points (leave one of equal points) from theWLine
3009 //           and recompute vertex parameters.
3010 //           Returns new WLine or null WLine if the number
3011 //           of the points is less than 2.
3012 //=========================================================================
3013 Handle(IntPatch_WLine) ComputePurgedWLine(const Handle(IntPatch_WLine)& theWLine) {
3014  
3015   Standard_Integer i, k, v, nb, nbvtx;
3016   Handle(IntPatch_WLine) aResult;
3017   nbvtx = theWLine->NbVertex();
3018   nb = theWLine->NbPnts();
3019   if (nb==2) {
3020     const IntSurf_PntOn2S& p1 = theWLine->Point(1);
3021     const IntSurf_PntOn2S& p2 = theWLine->Point(2);
3022     if(p1.Value().IsEqual(p2.Value(), gp::Resolution())) {
3023       return aResult;
3024     }
3025   }
3026   //
3027   Handle(IntPatch_WLine) aLocalWLine;
3028   Handle(IntPatch_WLine) aTmpWLine = theWLine;
3029   Handle(IntSurf_LineOn2S) aLineOn2S = new IntSurf_LineOn2S();
3030   aLocalWLine = new IntPatch_WLine(aLineOn2S, Standard_False);
3031   for(i = 1; i <= nb; i++) {
3032     aLineOn2S->Add(theWLine->Point(i));
3033   }
3034
3035   for(v = 1; v <= nbvtx; v++) {
3036     aLocalWLine->AddVertex(theWLine->Vertex(v));
3037   }
3038   
3039   for(i = 1; i <= aLineOn2S->NbPoints(); i++) {
3040     Standard_Integer aStartIndex = i + 1;
3041     Standard_Integer anEndIndex = i + 5;
3042     nb = aLineOn2S->NbPoints();
3043     anEndIndex = (anEndIndex > nb) ? nb : anEndIndex;
3044
3045     if((aStartIndex > nb) || (anEndIndex <= 1)) {
3046       continue;
3047     }
3048     k = aStartIndex;
3049
3050     while(k <= anEndIndex) {
3051       
3052       if(i != k) {
3053         IntSurf_PntOn2S p1 = aLineOn2S->Value(i);
3054         IntSurf_PntOn2S p2 = aLineOn2S->Value(k);
3055         
3056         if(p1.Value().IsEqual(p2.Value(), gp::Resolution())) {
3057           aTmpWLine = aLocalWLine;
3058           aLocalWLine = new IntPatch_WLine(aLineOn2S, Standard_False);
3059
3060           for(v = 1; v <= aTmpWLine->NbVertex(); v++) {
3061             IntPatch_Point aVertex = aTmpWLine->Vertex(v);
3062             Standard_Integer avertexindex = (Standard_Integer)aVertex.ParameterOnLine();
3063
3064             if(avertexindex >= k) {
3065               aVertex.SetParameter(aVertex.ParameterOnLine() - 1.);
3066             }
3067             aLocalWLine->AddVertex(aVertex);
3068           }
3069           aLineOn2S->RemovePoint(k);
3070           anEndIndex--;
3071           continue;
3072         }
3073       }
3074       k++;
3075     }
3076   }
3077
3078   if(aLineOn2S->NbPoints() > 1) {
3079     aResult = aLocalWLine;
3080   }
3081   return aResult;
3082 }
3083
3084 //=======================================================================
3085 //function : TolR3d
3086 //purpose  : 
3087 //=======================================================================
3088 void TolR3d(const TopoDS_Face& aF1,
3089             const TopoDS_Face& aF2,
3090             Standard_Real& myTolReached3d)
3091 {
3092   Standard_Real aTolF1, aTolF2, aTolFMax, aTolTresh;
3093       
3094   aTolTresh=2.999999e-3;
3095   aTolF1 = BRep_Tool::Tolerance(aF1);
3096   aTolF2 = BRep_Tool::Tolerance(aF2);
3097   aTolFMax=Max(aTolF1, aTolF2);
3098   
3099   if (aTolFMax>aTolTresh) {
3100     myTolReached3d=aTolFMax;
3101   }
3102 }
3103 //=======================================================================
3104 //function : AdjustPeriodic
3105 //purpose  : 
3106 //=======================================================================
3107 Standard_Real AdjustPeriodic(const Standard_Real theParameter,
3108                              const Standard_Real parmin,
3109                              const Standard_Real parmax,
3110                              const Standard_Real thePeriod,
3111                              Standard_Real&      theOffset) 
3112 {
3113   Standard_Real aresult;
3114   //
3115   theOffset = 0.;
3116   aresult = theParameter;
3117   while(aresult < parmin) {
3118     aresult += thePeriod;
3119     theOffset += thePeriod;
3120   }
3121
3122   while(aresult > parmax) {
3123     aresult -= thePeriod;
3124     theOffset -= thePeriod;
3125   }
3126   return aresult;
3127 }
3128 //=======================================================================
3129 //function : IsPointOnBoundary
3130 //purpose  : 
3131 //=======================================================================
3132 Standard_Boolean IsPointOnBoundary(const Standard_Real theParameter,
3133                                    const Standard_Real theFirstBoundary,
3134                                    const Standard_Real theSecondBoundary,
3135                                    const Standard_Real theResolution,
3136                                    Standard_Boolean&   IsOnFirstBoundary) 
3137 {
3138   Standard_Boolean bRet;
3139   Standard_Integer i;
3140   Standard_Real adist;
3141   //
3142   bRet=Standard_False;
3143   for(i = 0; i < 2; ++i) {
3144     IsOnFirstBoundary = (i == 0);
3145     if (IsOnFirstBoundary) {
3146       adist = fabs(theParameter - theFirstBoundary);
3147     }
3148     else {
3149       adist = fabs(theParameter - theSecondBoundary);
3150     }
3151     if(adist < theResolution) {
3152       return !bRet;
3153     }
3154   }
3155   return bRet;
3156 }
3157 // ------------------------------------------------------------------------------------------------
3158 // static function: FindPoint
3159 // purpose:
3160 // ------------------------------------------------------------------------------------------------
3161 Standard_Boolean FindPoint(const gp_Pnt2d&     theFirstPoint,
3162                            const gp_Pnt2d&     theLastPoint,
3163                            const Standard_Real theUmin, 
3164                            const Standard_Real theUmax,
3165                            const Standard_Real theVmin,
3166                            const Standard_Real theVmax,
3167                            gp_Pnt2d&           theNewPoint) {
3168   
3169   gp_Vec2d aVec(theFirstPoint, theLastPoint);
3170   Standard_Integer i = 0, j = 0;
3171
3172   for(i = 0; i < 4; i++) {
3173     gp_Vec2d anOtherVec;
3174     gp_Vec2d anOtherVecNormal;
3175     gp_Pnt2d aprojpoint = theLastPoint;    
3176
3177     if((i % 2) == 0) {
3178       anOtherVec.SetX(0.);
3179       anOtherVec.SetY(1.);
3180       anOtherVecNormal.SetX(1.);
3181       anOtherVecNormal.SetY(0.);
3182
3183       if(i < 2)
3184         aprojpoint.SetX(theUmin);
3185       else
3186         aprojpoint.SetX(theUmax);
3187     }
3188     else {
3189       anOtherVec.SetX(1.);
3190       anOtherVec.SetY(0.);
3191       anOtherVecNormal.SetX(0.);
3192       anOtherVecNormal.SetY(1.);
3193
3194       if(i < 2)
3195         aprojpoint.SetY(theVmin);
3196       else
3197         aprojpoint.SetY(theVmax);
3198     }
3199     gp_Vec2d anormvec = aVec;
3200     anormvec.Normalize();
3201     RefineVector(anormvec);
3202     Standard_Real adot1 = anormvec.Dot(anOtherVecNormal);
3203
3204     if(fabs(adot1) < Precision::Angular())
3205       continue;
3206     Standard_Real adist = 0.;
3207     Standard_Boolean bIsOut = Standard_False;
3208
3209     if((i % 2) == 0) {
3210       adist = (i < 2) ? fabs(theLastPoint.X() - theUmin) : fabs(theLastPoint.X() - theUmax);
3211       bIsOut = (i < 2) ? (theLastPoint.X() < theUmin) : (theLastPoint.X() > theUmax);
3212     }
3213     else {
3214       adist = (i < 2) ? fabs(theLastPoint.Y() - theVmin) : fabs(theLastPoint.Y() - theVmax);
3215       bIsOut = (i < 2) ? (theLastPoint.Y() < theVmin) : (theLastPoint.Y() > theVmax);
3216     }
3217     Standard_Real anoffset = adist * anOtherVec.Dot(anormvec) / adot1;
3218
3219     for(j = 0; j < 2; j++) {
3220       anoffset = (j == 0) ? anoffset : -anoffset;
3221       gp_Pnt2d acurpoint(aprojpoint.XY() + (anOtherVec.XY()*anoffset));
3222       gp_Vec2d acurvec(theLastPoint, acurpoint);
3223       if ( bIsOut )
3224         acurvec.Reverse();
3225
3226       Standard_Real aDotX, anAngleX;
3227       //
3228       aDotX = aVec.Dot(acurvec);
3229       anAngleX = aVec.Angle(acurvec);
3230       //
3231       if(aDotX > 0. && fabs(anAngleX) < Precision::PConfusion()) {
3232         if((i % 2) == 0) {
3233           if((acurpoint.Y() >= theVmin) &&
3234              (acurpoint.Y() <= theVmax)) {
3235             theNewPoint = acurpoint;
3236             return Standard_True;
3237           }
3238         }
3239         else {
3240           if((acurpoint.X() >= theUmin) &&
3241              (acurpoint.X() <= theUmax)) {
3242             theNewPoint = acurpoint;
3243             return Standard_True;
3244           }
3245         }
3246       }
3247     }
3248   }
3249   return Standard_False;
3250 }
3251
3252
3253 // ------------------------------------------------------------------------------------------------
3254 // static function: FindPoint
3255 // purpose: Find point on the boundary of radial tangent zone
3256 // ------------------------------------------------------------------------------------------------
3257 Standard_Boolean FindPoint(const gp_Pnt2d&     theFirstPoint,
3258                            const gp_Pnt2d&     theLastPoint,
3259                            const Standard_Real theUmin, 
3260                            const Standard_Real theUmax,
3261                            const Standard_Real theVmin,
3262                            const Standard_Real theVmax,
3263                            const gp_Pnt2d&     theTanZoneCenter,
3264                            const Standard_Real theZoneRadius,
3265                            Handle(GeomAdaptor_HSurface) theGASurface,
3266                            gp_Pnt2d&           theNewPoint) {
3267   theNewPoint = theLastPoint;
3268
3269   if ( !IsInsideTanZone( theLastPoint, theTanZoneCenter, theZoneRadius, theGASurface) )
3270     return Standard_False;
3271
3272   Standard_Real aUResolution = theGASurface->UResolution( theZoneRadius );
3273   Standard_Real aVResolution = theGASurface->VResolution( theZoneRadius );
3274
3275   Standard_Real aRadius = ( aUResolution < aVResolution ) ? aUResolution : aVResolution;
3276   gp_Ax22d anAxis( theTanZoneCenter, gp_Dir2d(1, 0), gp_Dir2d(0, 1) );
3277   gp_Circ2d aCircle( anAxis, aRadius );
3278   
3279   //
3280   gp_Vec2d aDir( theLastPoint.XY() - theFirstPoint.XY() );
3281   Standard_Real aLength = aDir.Magnitude();
3282   if ( aLength <= gp::Resolution() )
3283     return Standard_False;
3284   gp_Lin2d aLine( theFirstPoint, aDir );
3285
3286   //
3287   Handle(Geom2d_Line) aCLine = new Geom2d_Line( aLine );
3288   Handle(Geom2d_TrimmedCurve) aC1 = new Geom2d_TrimmedCurve( aCLine, 0, aLength );
3289   Handle(Geom2d_Circle) aC2 = new Geom2d_Circle( aCircle );
3290
3291   Standard_Real aTol = aRadius * 0.001;
3292   aTol = ( aTol < Precision::PConfusion() ) ? Precision::PConfusion() : aTol;
3293
3294   Geom2dAPI_InterCurveCurve anIntersector;
3295   anIntersector.Init( aC1, aC2, aTol );
3296
3297   if ( anIntersector.NbPoints() == 0 )
3298     return Standard_False;
3299
3300   Standard_Boolean aFound = Standard_False;
3301   Standard_Real aMinDist = aLength * aLength;
3302   Standard_Integer i = 0;
3303   for ( i = 1; i <= anIntersector.NbPoints(); i++ ) {
3304     gp_Pnt2d aPInt = anIntersector.Point( i );
3305     if ( aPInt.SquareDistance( theFirstPoint ) < aMinDist ) {
3306       if ( ( aPInt.X() >= theUmin ) && ( aPInt.X() <= theUmax ) &&
3307            ( aPInt.Y() >= theVmin ) && ( aPInt.Y() <= theVmax ) ) {
3308         theNewPoint = aPInt;
3309         aFound = Standard_True;
3310       }
3311     }
3312   }
3313
3314   return aFound;
3315 }
3316
3317 // ------------------------------------------------------------------------------------------------
3318 // static function: IsInsideTanZone
3319 // purpose: Check if point is inside a radial tangent zone
3320 // ------------------------------------------------------------------------------------------------
3321 Standard_Boolean IsInsideTanZone(const gp_Pnt2d&     thePoint,
3322                                  const gp_Pnt2d&     theTanZoneCenter,
3323                                  const Standard_Real theZoneRadius,
3324                                  Handle(GeomAdaptor_HSurface) theGASurface) {
3325
3326   Standard_Real aUResolution = theGASurface->UResolution( theZoneRadius );
3327   Standard_Real aVResolution = theGASurface->VResolution( theZoneRadius );
3328   Standard_Real aRadiusSQR = ( aUResolution < aVResolution ) ? aUResolution : aVResolution;
3329   aRadiusSQR *= aRadiusSQR;
3330   if ( thePoint.SquareDistance( theTanZoneCenter ) <= aRadiusSQR )
3331     return Standard_True;
3332   return Standard_False;
3333 }
3334
3335 // ------------------------------------------------------------------------------------------------
3336 // static function: CheckTangentZonesExist
3337 // purpose: Check if tangent zone exists
3338 // ------------------------------------------------------------------------------------------------
3339 Standard_Boolean CheckTangentZonesExist( const Handle(GeomAdaptor_HSurface)& theSurface1,
3340                                         const Handle(GeomAdaptor_HSurface)&  theSurface2 ) 
3341 {
3342   if ( ( theSurface1->GetType() != GeomAbs_Torus ) ||
3343       ( theSurface2->GetType() != GeomAbs_Torus ) )
3344     return Standard_False;
3345
3346   gp_Torus aTor1 = theSurface1->Torus();
3347   gp_Torus aTor2 = theSurface2->Torus();
3348
3349   if ( aTor1.Location().Distance( aTor2.Location() ) > Precision::Confusion() )
3350     return Standard_False;
3351
3352   if ( ( fabs( aTor1.MajorRadius() - aTor2.MajorRadius() ) > Precision::Confusion() ) ||
3353        ( fabs( aTor1.MinorRadius() - aTor2.MinorRadius() ) > Precision::Confusion() ) )
3354     return Standard_False;
3355
3356   if ( ( aTor1.MajorRadius() < aTor1.MinorRadius() ) ||
3357        ( aTor2.MajorRadius() < aTor2.MinorRadius() ) )
3358     return Standard_False;
3359   return Standard_True;
3360 }
3361
3362 // ------------------------------------------------------------------------------------------------
3363 // static function: ComputeTangentZones
3364 // purpose: 
3365 // ------------------------------------------------------------------------------------------------
3366 Standard_Integer ComputeTangentZones( const Handle(GeomAdaptor_HSurface)& theSurface1,
3367                                      const Handle(GeomAdaptor_HSurface)&  theSurface2,
3368                                      const TopoDS_Face&                   theFace1,
3369                                      const TopoDS_Face&                   theFace2,
3370                                      Handle(TColgp_HArray1OfPnt2d)&       theResultOnS1,
3371                                      Handle(TColgp_HArray1OfPnt2d)&       theResultOnS2,
3372                                      Handle(TColStd_HArray1OfReal)&       theResultRadius,
3373                                      const Handle(BOPInt_Context)& aContext)
3374 {
3375   Standard_Integer aResult = 0;
3376   if ( !CheckTangentZonesExist( theSurface1, theSurface2 ) )
3377     return aResult;
3378
3379
3380   TColgp_SequenceOfPnt2d aSeqResultS1, aSeqResultS2;
3381   TColStd_SequenceOfReal aSeqResultRad;
3382
3383   gp_Torus aTor1 = theSurface1->Torus();
3384   gp_Torus aTor2 = theSurface2->Torus();
3385
3386   gp_Ax2 anax1( aTor1.Location(), aTor1.Axis().Direction() );
3387   gp_Ax2 anax2( aTor2.Location(), aTor2.Axis().Direction() );
3388   Standard_Integer j = 0;
3389
3390   for ( j = 0; j < 2; j++ ) {
3391     Standard_Real aCoef = ( j == 0 ) ? -1 : 1;
3392     Standard_Real aRadius1 = fabs(aTor1.MajorRadius() + aCoef * aTor1.MinorRadius());
3393     Standard_Real aRadius2 = fabs(aTor2.MajorRadius() + aCoef * aTor2.MinorRadius());
3394
3395     gp_Circ aCircle1( anax1, aRadius1 );
3396     gp_Circ aCircle2( anax2, aRadius2 );
3397
3398     // roughly compute radius of tangent zone for perpendicular case
3399     Standard_Real aCriteria = Precision::Confusion() * 0.5;
3400
3401     Standard_Real aT1 = aCriteria;
3402     Standard_Real aT2 = aCriteria;
3403     if ( j == 0 ) {
3404       // internal tangency
3405       Standard_Real aR = ( aRadius1 > aTor2.MinorRadius() ) ? aRadius1 : aTor2.MinorRadius();
3406       //aT1 = aCriteria * aCriteria + aR * aR - ( aR - aCriteria ) * ( aR - aCriteria );
3407       aT1 = 2. * aR * aCriteria;
3408       aT2 = aT1;
3409     }
3410     else {
3411       // external tangency
3412       Standard_Real aRb = ( aRadius1 > aTor2.MinorRadius() ) ? aRadius1 : aTor2.MinorRadius();
3413       Standard_Real aRm = ( aRadius1 < aTor2.MinorRadius() ) ? aRadius1 : aTor2.MinorRadius();
3414       Standard_Real aDelta = aRb - aCriteria;
3415       aDelta *= aDelta;
3416       aDelta -= aRm * aRm;
3417       aDelta /= 2. * (aRb - aRm);
3418       aDelta -= 0.5 * (aRb - aRm);
3419       
3420       aT1 = 2. * aRm * (aRm - aDelta);
3421       aT2 = aT1;
3422     }
3423     aCriteria = ( aT1 > aT2) ? aT1 : aT2;
3424     if ( aCriteria > 0 )
3425       aCriteria = sqrt( aCriteria );
3426
3427     if ( aCriteria > 0.5 * aTor1.MinorRadius() ) {
3428       // too big zone -> drop to minimum
3429       aCriteria = Precision::Confusion();
3430     }
3431
3432     GeomAdaptor_Curve aC1( new Geom_Circle(aCircle1) );
3433     GeomAdaptor_Curve aC2( new Geom_Circle(aCircle2) );
3434     Extrema_ExtCC anExtrema(aC1, aC2, 0, 2. * M_PI, 0, 2. * M_PI, 
3435                             Precision::PConfusion(), Precision::PConfusion());
3436         
3437     if ( anExtrema.IsDone() ) {
3438
3439       Standard_Integer i = 0;
3440       for ( i = 1; i <= anExtrema.NbExt(); i++ ) {
3441         if ( anExtrema.SquareDistance(i) > aCriteria * aCriteria )