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