0024023: Revamp the OCCT Handle -- downcast (automatic)
[occt.git] / src / IntTools / IntTools_FaceFace.cxx
index 2f658e4..0785460 100644 (file)
@@ -87,6 +87,7 @@
 
 #include <Geom2dAPI_InterCurveCurve.hxx>
 #include <Geom2dInt_GInter.hxx>
+#include <Geom2dAdaptor.hxx>
 #include <GeomAdaptor_Curve.hxx>
 #include <GeomAdaptor_HSurface.hxx>
 #include <GeomAdaptor_Surface.hxx>
 #include <IntSurf_ListIteratorOfListOfPntOn2S.hxx>
 #include <GeomInt.hxx>
 
+#include <Approx_CurveOnSurface.hxx>
+#include <GeomAdaptor.hxx>
+#include <GeomInt_IntSS.hxx>
+
 static
   void RefineVector(gp_Vec2d& aV2D);
 #ifdef OCCT_DEBUG_DUMPWLINE
@@ -140,12 +145,6 @@ static
                   Standard_Real&,
                   Standard_Real&);
 
-static 
-  void BuildPCurves (Standard_Real f,Standard_Real l,Standard_Real& Tol,
-                     const Handle (Geom_Surface)& S,
-                     const Handle (Geom_Curve)&   C,
-                     Handle (Geom2d_Curve)& C2d);
-
 static 
   void CorrectSurfaceBoundaries(const TopoDS_Face&  theFace,
                                 const Standard_Real theTolerance,
@@ -153,6 +152,7 @@ static
                                 Standard_Real&      theumax, 
                                 Standard_Real&      thevmin, 
                                 Standard_Real&      thevmax);
+
 static
   Standard_Boolean NotUseSurfacesForApprox
           (const TopoDS_Face& aF1,
@@ -281,34 +281,15 @@ static
   Standard_Integer IndexType(const GeomAbs_SurfaceType aType);
 
 //
-static
-  Standard_Real MaxSquareDistance (const Standard_Real aT,
-                                   const Handle(Geom_Curve)& aC3D,
-                                   const Handle(Geom2d_Curve)& aC2D1,
-                                   const Handle(Geom2d_Curve)& aC2D2,
-                                   const Handle(GeomAdaptor_HSurface) myHS1,
-                                   const Handle(GeomAdaptor_HSurface) myHS2,
-                                   const TopoDS_Face& aF1,
-                                   const TopoDS_Face& aF2,
-                                   const Handle(IntTools_Context)& aCtx);
-
 static
   Standard_Boolean CheckPCurve(const Handle(Geom2d_Curve)& aPC, 
                                const TopoDS_Face& aFace);
 
-//
 static
-  Standard_Real FindMaxSquareDistance (const Standard_Real aA,
-                                       const Standard_Real aB,
-                                       const Standard_Real aEps,
-                                       const Handle(Geom_Curve)& aC3D,
-                                       const Handle(Geom2d_Curve)& aC2D1,
-                                       const Handle(Geom2d_Curve)& aC2D2,
-                                       const Handle(GeomAdaptor_HSurface)& myHS1,
-                                       const Handle(GeomAdaptor_HSurface)& myHS2,
-                                       const TopoDS_Face& aF1,
-                                       const TopoDS_Face& aF2,
-                                       const Handle(IntTools_Context)& aCtx);
+  void CorrectPlaneBoundaries(Standard_Real& aUmin,
+                              Standard_Real& aUmax, 
+                              Standard_Real& aVmin, 
+                              Standard_Real& aVmax);
 
 //=======================================================================
 //function : 
@@ -510,7 +491,7 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
 //function : Perform
 //purpose  : intersect surfaces of the faces
 //=======================================================================
-  void IntTools_FaceFace::Perform(const TopoDS_Face& aF1,
+void IntTools_FaceFace::Perform(const TopoDS_Face& aF1,
                                 const TopoDS_Face& aF2)
 {
   Standard_Boolean RestrictLine = Standard_False, hasCone = Standard_False;
@@ -580,24 +561,13 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
 
   if(aType1==GeomAbs_Plane && aType2==GeomAbs_Plane)  {
     Standard_Real umin, umax, vmin, vmax;
-    Standard_Real dU, dV;
     //
     BRepTools::UVBounds(myFace1, umin, umax, vmin, vmax);
-    dU=0.1*(umax-umin);
-    dV=0.1*(vmax-vmin);
-    umin=umin-dU;
-    umax=umax+dU;
-    vmin=vmin-dV;
-    vmax=vmax+dV;
+    CorrectPlaneBoundaries(umin, umax, vmin, vmax);
     myHS1->ChangeSurface().Load(S1, umin, umax, vmin, vmax);
     //
     BRepTools::UVBounds(myFace2, umin, umax, vmin, vmax);
-    dU=0.1*(umax-umin);
-    dV=0.1*(vmax-vmin);
-    umin=umin-dU;
-    umax=umax+dU;
-    vmin=vmin-dV;
-    vmax=vmax+dV;
+    CorrectPlaneBoundaries(umin, umax, vmin, vmax);
     myHS2->ChangeSurface().Load(S2, umin, umax, vmin, vmax);
     //
     Standard_Real TolAng = 1.e-8;
@@ -637,18 +607,10 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
 
   if ((aType1==GeomAbs_Plane) && isFace2Quad)
   {
-    Standard_Real dU, dV;
-
-    // F1
     Standard_Real umin, umax, vmin, vmax;
-    BRepTools::UVBounds(myFace1, umin, umax, vmin, vmax);
-
-    dU=0.1*(umax-umin);
-    dV=0.1*(vmax-vmin);
-    umin=umin-dU;
-    umax=umax+dU;
-    vmin=vmin-dV;
-    vmax=vmax+dV;
+    // F1
+    BRepTools::UVBounds(myFace1, umin, umax, vmin, vmax); 
+    CorrectPlaneBoundaries(umin, umax, vmin, vmax);
     myHS1->ChangeSurface().Load(S1, umin, umax, vmin, vmax);
     // F2
     BRepTools::UVBounds(myFace2, umin, umax, vmin, vmax);
@@ -662,21 +624,14 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
   }
   else if ((aType2==GeomAbs_Plane) && isFace1Quad)
   {
-    Standard_Real dU, dV;
-
-    //F1
     Standard_Real umin, umax, vmin, vmax;
+    //F1
     BRepTools::UVBounds(myFace1, umin, umax, vmin, vmax);
     CorrectSurfaceBoundaries(myFace1, (aTolF1 + aTolF2) * 2., umin, umax, vmin, vmax);
     myHS1->ChangeSurface().Load(S1, umin, umax, vmin, vmax);
     // F2
     BRepTools::UVBounds(myFace2, umin, umax, vmin, vmax);
-    dU=0.1*(umax-umin);
-    dV=0.1*(vmax-vmin);
-    umin=umin-dU;
-    umax=umax+dU;
-    vmin=vmin-dV;
-    vmax=vmax+dV;
+    CorrectPlaneBoundaries(umin, umax, vmin, vmax);
     myHS2->ChangeSurface().Load(S2, umin, umax, vmin, vmax);
     //
     if( aType1==GeomAbs_Cone ) {
@@ -793,6 +748,7 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
     }
 
     // Points
+    Standard_Boolean bValid2D1, bValid2D2;
     Standard_Real U1,V1,U2,V2;
     IntTools_PntOnFace aPntOnF1, aPntOnF2;
     IntTools_PntOn2Faces aPntOn2Faces;
@@ -803,6 +759,19 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       const IntSurf_PntOn2S& aISPnt=myIntersector.Point(i).PntOn2S();
       const gp_Pnt& aPnt=aISPnt.Value();
       aISPnt.Parameters(U1,V1,U2,V2);
+      //
+      // check the validity of the intersection point for the faces
+      bValid2D1 = myContext->IsPointInOnFace(myFace1, gp_Pnt2d(U1, V1));
+      if (!bValid2D1) {
+        continue;
+      }
+      //
+      bValid2D2 = myContext->IsPointInOnFace(myFace2, gp_Pnt2d(U2, V2));
+      if (!bValid2D2) {
+        continue;
+      }
+      //
+      // add the intersection point
       aPntOnF1.Init(myFace1, aPnt, U1, V1);
       aPntOnF2.Init(myFace2, aPnt, U2, V2);
       //
@@ -822,13 +791,65 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
   }
 }
 
+//=======================================================================
+//function : ComputeTolerance
+//purpose  : 
+//=======================================================================
+Standard_Real IntTools_FaceFace::ComputeTolerance()
+{
+  Standard_Integer i, j, aNbLin;
+  Standard_Real aFirst, aLast, aD, aDMax, aT;
+  Handle(Geom_Surface) aS1, aS2;
+  //
+  aDMax = 0;
+  aNbLin = mySeqOfCurve.Length();
+  //
+  aS1 = myHS1->ChangeSurface().Surface();
+  aS2 = myHS2->ChangeSurface().Surface();
+  //
+  for (i = 1; i <= aNbLin; ++i)
+  {
+    const IntTools_Curve& aIC = mySeqOfCurve(i);
+    const Handle(Geom_Curve)& aC3D = aIC.Curve();
+    if (aC3D.IsNull())
+    {
+      continue;
+    }
+    //
+    aFirst = aC3D->FirstParameter();
+    aLast  = aC3D->LastParameter();
+    //
+    const Handle(Geom2d_Curve)& aC2D1 = aIC.FirstCurve2d();
+    const Handle(Geom2d_Curve)& aC2D2 = aIC.SecondCurve2d();
+    //
+    for (j = 0; j < 2; ++j)
+    {
+      const Handle(Geom2d_Curve)& aC2D = !j ? aC2D1 : aC2D2;
+      const Handle(Geom_Surface)& aS = !j ? aS1 : aS2;
+      //
+      if (!aC2D.IsNull())
+      {
+        if (IntTools_Tools::ComputeTolerance
+            (aC3D, aC2D, aS, aFirst, aLast, aD, aT))
+        {
+          if (aD > aDMax)
+          {
+            aDMax = aD;
+          }
+        }
+      }
+    }
+  }
+  //
+  return aDMax;
+}
+
 //=======================================================================
 //function :ComputeTolReached3d 
 //purpose  : 
 //=======================================================================
   void IntTools_FaceFace::ComputeTolReached3d()
 {
-  Standard_Boolean bCase1;
   Standard_Integer aNbLin, i;
   GeomAbs_SurfaceType aType1, aType2;
   //
@@ -840,9 +861,6 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
   aType1=myHS1->Surface().GetType();
   aType2=myHS2->Surface().GetType();
   //
-  bCase1=((aType1==GeomAbs_Plane && aType2==GeomAbs_SurfaceOfExtrusion) ||
-          (aType2==GeomAbs_Plane && aType1==GeomAbs_SurfaceOfExtrusion));
-  //
   if (aType1==GeomAbs_Cylinder && aType2==GeomAbs_Cylinder) {
     if (aNbLin==2){ 
       Handle(IntPatch_Line) aIL1, aIL2;
@@ -871,45 +889,12 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
     }
     //ZZ
     if (aNbLin) {// Check the distances
-      Standard_Integer  aNbP, j ;
-      Standard_Real aT1, aT2, dT, aD2, aD2Max, aEps, aT11, aT12;
-      //
-      aD2Max=0.;
-      aNbP=10;
-      aNbLin=mySeqOfCurve.Length();
-      //
-      for (i=1; i<=aNbLin; ++i) {
-        const IntTools_Curve& aIC=mySeqOfCurve(i);
-        const Handle(Geom_Curve)& aC3D=aIC.Curve();
-        const Handle(Geom2d_Curve)& aC2D1=aIC.FirstCurve2d();
-        const Handle(Geom2d_Curve)& aC2D2=aIC.SecondCurve2d();
-        //
-        if (aC3D.IsNull()) {
-          continue;
-        }
-        const Handle(Geom_BSplineCurve)& aBC=
-          Handle(Geom_BSplineCurve)::DownCast(aC3D);
-        if (aBC.IsNull()) {
-          continue;
-        }
-        //
-        aT1=aBC->FirstParameter();
-        aT2=aBC->LastParameter();
-        //
-        aEps=0.01*(aT2-aT1);
-        dT=(aT2-aT1)/aNbP;
-        for (j=1; j<aNbP; ++j) {
-          aT11=aT1+j*dT;
-          aT12=aT11+dT;
-          aD2=FindMaxSquareDistance(aT11, aT12, aEps, aC3D, aC2D1, aC2D2,
-                                    myHS1, myHS2, myFace1, myFace2, myContext);
-          if (aD2>aD2Max) {
-            aD2Max=aD2;
-          }
-        }
-      }//for (i=1; i<=aNbLin; ++i) {
+      Standard_Real aDMax;
       //
-      myTolReached3d=sqrt(aD2Max);
+      aDMax = ComputeTolerance();
+      if (aDMax > 0.) {
+        myTolReached3d = aDMax;
+      }
     }// if (aNbLin) 
   }// if (aType1==GeomAbs_Cylinder && aType2==GeomAbs_Cylinder) {
   //
@@ -978,8 +963,7 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
     //
     const IntTools_Curve& aIC=mySeqOfCurve(1);
     const Handle(Geom_Curve)& aC3D=aIC.Curve();
-    const Handle(Geom_BSplineCurve)& aBS=
-      Handle(Geom_BSplineCurve)::DownCast(aC3D);
+    Handle(Geom_BSplineCurve) aBS (Handle(Geom_BSplineCurve)::DownCast(aC3D));
     if (aBS.IsNull()) {
       return;
     }
@@ -1023,151 +1007,24 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
   }// if ((aType1==GeomAbs_Plane && aType2==GeomAbs_Torus) ||
   //
   else if ((aType1==GeomAbs_SurfaceOfRevolution && aType2==GeomAbs_Cylinder) ||
-           (aType2==GeomAbs_SurfaceOfRevolution && aType1==GeomAbs_Cylinder)) {
-    Standard_Integer j, aNbP;
-    Standard_Real aT, aT1, aT2, dT, aD2max, aD2;
-    //
-    aNbLin=mySeqOfCurve.Length();
-    aD2max=0.;
-    aNbP=11;
+           (aType2==GeomAbs_SurfaceOfRevolution && aType1==GeomAbs_Cylinder) ||
+           (aType1==GeomAbs_Plane && aType2==GeomAbs_Sphere) ||
+           (aType2==GeomAbs_Plane && aType1==GeomAbs_Sphere) ||
+           (aType1==GeomAbs_Plane && aType2==GeomAbs_SurfaceOfExtrusion) ||
+           (aType2==GeomAbs_Plane && aType1==GeomAbs_SurfaceOfExtrusion) ||
+           (aType1==GeomAbs_Plane && aType2==GeomAbs_BSplineSurface) ||
+           (aType2==GeomAbs_Plane && aType1==GeomAbs_BSplineSurface) ||
+           !myApprox) {
     //
-    for (i=1; i<=aNbLin; ++i) {
-      const IntTools_Curve& aIC=mySeqOfCurve(i);
-      const Handle(Geom_Curve)& aC3D=aIC.Curve();
-      const Handle(Geom2d_Curve)& aC2D1=aIC.FirstCurve2d();
-      const Handle(Geom2d_Curve)& aC2D2=aIC.SecondCurve2d();
-      //
-      if (aC3D.IsNull()) {
-        continue;
-      }
-      const Handle(Geom_BSplineCurve)& aBC=
-        Handle(Geom_BSplineCurve)::DownCast(aC3D);
-      if (aBC.IsNull()) {
-        return;
-      }
-      //
-      aT1=aBC->FirstParameter();
-      aT2=aBC->LastParameter();
-      //
-      dT=(aT2-aT1)/(aNbP-1);
-      for (j=0; j<aNbP; ++j) {
-        aT=aT1+j*dT;
-        if (j==aNbP-1) {
-          aT=aT2;
-        }
-        //
-        aD2=MaxSquareDistance(aT, aC3D, aC2D1, aC2D2,
-                              myHS1, myHS2, myFace1, myFace2, myContext);
-        if (aD2>aD2max) {
-          aD2max=aD2;
-        }
-      }//for (j=0; j<aNbP; ++j) {
-     
-    }//for (i=1; i<=aNbLin; ++i) {
-    //
-    aD2=myTolReached3d*myTolReached3d;
-    if (aD2max > aD2) {
-      myTolReached3d=sqrt(aD2max);
-    }
-  }//if((aType1==GeomAbs_SurfaceOfRevolution ...
-  else if ((aType1==GeomAbs_Plane && aType2==GeomAbs_Sphere) ||
-           (aType2==GeomAbs_Plane && aType1==GeomAbs_Sphere)) {
-    Standard_Integer  j, aNbP;
-    Standard_Real aT1, aT2, dT, aD2max, aD2, aEps, aT11, aT12;
-    //
-    aNbLin=mySeqOfCurve.Length();
-    aD2max=0.;
-    aNbP=10;
-    //
-    for (i=1; i<=aNbLin; ++i) {
-      const IntTools_Curve& aIC=mySeqOfCurve(i);
-      const Handle(Geom_Curve)& aC3D=aIC.Curve();
-      const Handle(Geom2d_Curve)& aC2D1=aIC.FirstCurve2d();
-      const Handle(Geom2d_Curve)& aC2D2=aIC.SecondCurve2d();
-      //
-      const Handle(Geom2d_BSplineCurve)& aBC2D1=
-        Handle(Geom2d_BSplineCurve)::DownCast(aC2D1);
-      const Handle(Geom2d_BSplineCurve)& aBC2D2=
-        Handle(Geom2d_BSplineCurve)::DownCast(aC2D2);
-      //
-      if (aBC2D1.IsNull() && aBC2D2.IsNull()) {
-        return;
-      }
-      //
-      if (!aBC2D1.IsNull()) {
-        aT1=aBC2D1->FirstParameter();
-        aT2=aBC2D1->LastParameter();
-      }
-      else {
-        aT1=aBC2D2->FirstParameter();
-        aT2=aBC2D2->LastParameter();
-      }
-      //
-      aEps=0.01*(aT2-aT1);
-      dT=(aT2-aT1)/aNbP;
-      for (j=0; j<aNbP; ++j) {
-        aT11=aT1+j*dT;
-        aT12=aT11+dT;
-        if (j==aNbP-1) {
-          aT12=aT2;
-        }
-        //
-        aD2=FindMaxSquareDistance(aT11, aT12, aEps, aC3D, aC2D1, aC2D2,
-                                  myHS1, myHS2, myFace1, myFace2, myContext);
-        if (aD2>aD2max) {
-          aD2max=aD2;
-        }
-      }//for (j=0; j<aNbP; ++j) {
-     
-    }//for (i=1; i<=aNbLin; ++i) {
+    Standard_Real aDMax;
     //
-    aD2=myTolReached3d*myTolReached3d;
-    if (aD2max > aD2) {
-      myTolReached3d=sqrt(aD2max);
+    aDMax = ComputeTolerance();
+    if (aDMax > myTolReached3d) {
+      myTolReached3d = aDMax;
     }
-  }//else if ((aType1==GeomAbs_Plane && aType2==GeomAbs_Sphere) ...
-  else if (!myApprox || bCase1) {
-  //else if (!myApprox) {
-    Standard_Integer aNbP, j;
-    Standard_Real aT1, aT2, dT, aD2, aD2Max, aEps, aT11, aT12;
-    //
-    aD2Max=0.;
-    aNbLin=mySeqOfCurve.Length();
-    //
-    for (i=1; i<=aNbLin; ++i) {
-      const IntTools_Curve& aIC=mySeqOfCurve(i);
-      const Handle(Geom_Curve)& aC3D=aIC.Curve();
-      const Handle(Geom2d_Curve)& aC2D1=aIC.FirstCurve2d();
-      const Handle(Geom2d_Curve)& aC2D2=aIC.SecondCurve2d();
-      //
-      if (aC3D.IsNull()) {
-        continue;
-}
-      const Handle(Geom_BSplineCurve)& aBC=
-        Handle(Geom_BSplineCurve)::DownCast(aC3D);
-      if (aBC.IsNull()) {
-        continue;
-      }
-      //
-      aT1=aBC->FirstParameter();
-      aT2=aBC->LastParameter();
-      //
-      aEps=0.0001*(aT2-aT1);
-      aNbP=11;
-      dT=(aT2-aT1)/aNbP;
-      for (j=1; j<aNbP-1; ++j) {
-        aT11=aT1+j*dT;
-        aT12=aT11+dT;
-        aD2=FindMaxSquareDistance(aT11, aT12, aEps, aC3D, aC2D1, aC2D2,
-                                  myHS1, myHS2, myFace1, myFace2, myContext);
-        if (aD2>aD2Max) {
-          aD2Max=aD2;
-        }
-      }
-    }//for (i=1; i<=aNbLin; ++i) {
-    myTolReached3d=sqrt(aD2Max);
   }
 }
+
 //=======================================================================
 //function : MakeCurve
 //purpose  : 
@@ -1203,8 +1060,7 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
   if(typl==IntPatch_Walking) {
     Handle(IntPatch_Line) anewL;
     //
-    const Handle(IntPatch_WLine)& aWLine=
-      Handle(IntPatch_WLine)::DownCast(L);
+    Handle(IntPatch_WLine) aWLine (Handle(IntPatch_WLine)::DownCast(L));
     //DumpWLine(aWLine);
 
     anewL = ComputePurgedWLine(aWLine);
@@ -1213,7 +1069,7 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
     }
     L = anewL;
     
-    //const Handle(IntPatch_WLine)& aWLineX = Handle(IntPatch_WLine)::DownCast(L);
+    //Handle(IntPatch_WLine) aWLineX (Handle(IntPatch_WLine)::DownCast(L));
     //DumpWLine(aWLineX);
 
     //
@@ -1232,20 +1088,32 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       bAvoidLineConstructor = Standard_False;
     }
   }
+
+  typl=L->ArcType();
+
   //
   // Line Constructor
   if(!bAvoidLineConstructor) {
     myLConstruct.Perform(L);
     //
     bDone=myLConstruct.IsDone();
-    aNbParts=myLConstruct.NbParts();
-    if (!bDone|| !aNbParts) {
+    if(!bDone)
+    {
       return;
     }
+
+    if(typl != IntPatch_Restriction)
+    {
+      aNbParts=myLConstruct.NbParts();
+      if (aNbParts <= 0)
+      {
+        return;
+      }
+    }
   }
-  //
   // Do the Curve
-  //
+  
+  
   switch (typl) {
   //########################################  
   // Line, Parabola, Hyperbola
@@ -1278,7 +1146,7 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       Standard_Boolean bFNIt, bLPIt;
       //
       myLConstruct.Part(i, fprm, lprm);
-      //
+        //
       bFNIt=Precision::IsNegativeInfinite(fprm);
       bLPIt=Precision::IsPositiveInfinite(lprm);
       //
@@ -1300,7 +1168,8 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
         aCurve.SetCurve(new Geom_TrimmedCurve(newc, fprm, lprm));
         if(myApprox1) { 
           Handle (Geom2d_Curve) C2d;
-          BuildPCurves(fprm, lprm, Tolpc, myHS1->ChangeSurface().Surface(), newc, C2d);
+          GeomInt_IntSS::BuildPCurves(fprm, lprm, Tolpc,
+                myHS1->ChangeSurface().Surface(), newc, C2d);
           if(Tolpc>myTolReached2d || myTolReached2d==0.) { 
             myTolReached2d=Tolpc;
           }
@@ -1315,7 +1184,8 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
         //
         if(myApprox2) { 
           Handle (Geom2d_Curve) C2d;
-          BuildPCurves(fprm,lprm,Tolpc,myHS2->ChangeSurface().Surface(),newc,C2d);
+          GeomInt_IntSS::BuildPCurves(fprm, lprm, Tolpc,
+                    myHS2->ChangeSurface().Surface(), newc, C2d);
           if(Tolpc>myTolReached2d || myTolReached2d==0.) { 
             myTolReached2d=Tolpc;
           }
@@ -1483,7 +1353,8 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
         if (typl == IntPatch_Circle || typl == IntPatch_Ellipse) {//// 
           if(myApprox1) { 
             Handle (Geom2d_Curve) C2d;
-            BuildPCurves(fprm,lprm,Tolpc,myHS1->ChangeSurface().Surface(),newc,C2d);
+            GeomInt_IntSS::BuildPCurves(fprm, lprm, Tolpc, 
+                        myHS1->ChangeSurface().Surface(), newc, C2d);
             if(Tolpc>myTolReached2d || myTolReached2d==0) { 
               myTolReached2d=Tolpc;
             }
@@ -1498,7 +1369,8 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
 
           if(myApprox2) { 
             Handle (Geom2d_Curve) C2d;
-            BuildPCurves(fprm,lprm,Tolpc,myHS2->ChangeSurface().Surface(),newc,C2d);
+            GeomInt_IntSS::BuildPCurves(fprm,lprm,Tolpc,
+                    myHS2->ChangeSurface().Surface(),newc,C2d);
             if(Tolpc>myTolReached2d || myTolReached2d==0) { 
               myTolReached2d=Tolpc;
             }
@@ -1534,7 +1406,8 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
             
             if(myApprox1) { 
               Handle (Geom2d_Curve) C2d;
-              BuildPCurves(fprm,lprm,Tolpc,myHS1->ChangeSurface().Surface(),newc,C2d);
+              GeomInt_IntSS::BuildPCurves(fprm,lprm,Tolpc,
+                    myHS1->ChangeSurface().Surface(),newc,C2d);
               if(Tolpc>myTolReached2d || myTolReached2d==0) { 
                 myTolReached2d=Tolpc;
               }
@@ -1548,7 +1421,8 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
 
             if(myApprox2) { 
               Handle (Geom2d_Curve) C2d;
-              BuildPCurves(fprm,lprm,Tolpc,myHS2->ChangeSurface().Surface(),newc,C2d);
+              GeomInt_IntSS::BuildPCurves(fprm,lprm,Tolpc,
+                    myHS2->ChangeSurface().Surface(),newc,C2d);
               if(Tolpc>myTolReached2d || myTolReached2d==0) { 
                 myTolReached2d=Tolpc;
               }
@@ -1585,7 +1459,8 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
               
               if(myApprox1) { 
                 Handle (Geom2d_Curve) C2d;
-                BuildPCurves(fprm, lprm, Tolpc, myHS1->ChangeSurface().Surface(), newc, C2d);
+                GeomInt_IntSS::BuildPCurves(fprm, lprm, Tolpc, 
+                        myHS1->ChangeSurface().Surface(), newc, C2d);
                 if(Tolpc>myTolReached2d || myTolReached2d==0) { 
                   myTolReached2d=Tolpc;
                 }
@@ -1599,7 +1474,8 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
                 
               if(myApprox2) { 
                 Handle (Geom2d_Curve) C2d;
-                BuildPCurves(fprm, lprm, Tolpc,myHS2->ChangeSurface().Surface(), newc, C2d);
+                GeomInt_IntSS::BuildPCurves(fprm, lprm, Tolpc,
+                        myHS2->ChangeSurface().Surface(), newc, C2d);
                 if(Tolpc>myTolReached2d || myTolReached2d==0) { 
                   myTolReached2d=Tolpc;
                 }
@@ -2264,7 +2140,8 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
 
                   Handle(Geom2d_Curve) C2d;
                   Standard_Real aTol = myTolApprox;
-                  BuildPCurves(fprm, lprm, aTol, myHS1->ChangeSurface().Surface(), BS, C2d);
+                  GeomInt_IntSS::BuildPCurves(fprm, lprm, aTol,
+                            myHS1->ChangeSurface().Surface(), BS, C2d);
                   BS1 = Handle(Geom2d_BSplineCurve)::DownCast(C2d);
                   aCurve.SetFirstCurve2d(BS1);
                 }
@@ -2294,7 +2171,8 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
 
                   Handle(Geom2d_Curve) C2d;
                   Standard_Real aTol = myTolApprox;
-                  BuildPCurves(fprm, lprm, aTol, myHS2->ChangeSurface().Surface(), BS, C2d);
+                  GeomInt_IntSS::BuildPCurves(fprm, lprm, aTol,
+                            myHS2->ChangeSurface().Surface(), BS, C2d);
                   BS2 = Handle(Geom2d_BSplineCurve)::DownCast(C2d);
                   aCurve.SetSecondCurve2d(BS2);
                 }
@@ -2316,104 +2194,114 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
     break;
     
   case IntPatch_Restriction: 
-    break;
-  default:
-    break;
+    {
+      GeomAbs_SurfaceType typS1 = myHS1->Surface().GetType();
+      GeomAbs_SurfaceType typS2 = myHS2->Surface().GetType();
+      Standard_Boolean isAnalS1 = Standard_False;
+      switch (typS1)
+      {
+      case GeomAbs_Plane:
+      case GeomAbs_Cylinder:
+      case GeomAbs_Sphere:
+      case GeomAbs_Cone: 
+      case GeomAbs_Torus: isAnalS1 = Standard_True; break;
+      default: break;
+      }
 
-  }
-}
+      Standard_Integer isAnalS2 = Standard_False;
+      switch (typS2)
+      {
+      case GeomAbs_Plane:
+      case GeomAbs_Cylinder:
+      case GeomAbs_Sphere:
+      case GeomAbs_Cone: 
+      case GeomAbs_Torus: isAnalS2 = Standard_True; break;
+      default: break;
+      }
 
-//=======================================================================
-//function : BuildPCurves
-//purpose  : 
-//=======================================================================
-void BuildPCurves (Standard_Real f,
-                   Standard_Real l,
-                   Standard_Real& Tol,
-                   const Handle (Geom_Surface)& S,
-                   const Handle (Geom_Curve)&   C,
-                   Handle (Geom2d_Curve)& C2d)
-{
-  if (!C2d.IsNull()) {
-    return;
-  }
-  //
-  Standard_Real umin,umax,vmin,vmax;
-  // 
-  S->Bounds(umin, umax, vmin, vmax);
-  // in class ProjLib_Function the range of parameters is shrank by 1.e-09
-  if((l - f) > 2.e-09) {
-    C2d = GeomProjLib::Curve2d(C,f,l,S,umin,umax,vmin,vmax,Tol);
-    //
-    if (C2d.IsNull()) {
-      // proj. a circle that goes through the pole on a sphere to the sphere     
-      Tol += Precision::Confusion();
-      C2d = GeomProjLib::Curve2d(C,f,l,S,Tol);
-    }
-  }
-  else {
-    if((l - f) > Epsilon(Abs(f))) {
-      GeomAPI_ProjectPointOnSurf aProjector1, aProjector2;
-      gp_Pnt P1 = C->Value(f);
-      gp_Pnt P2 = C->Value(l);
-      aProjector1.Init(P1, S);
-      aProjector2.Init(P2, S);
-      
-      if(aProjector1.IsDone() && aProjector2.IsDone()) {
-        Standard_Real U=0., V=0.;
-        aProjector1.LowerDistanceParameters(U, V);
-        gp_Pnt2d p1(U, V);
-        
-        aProjector2.LowerDistanceParameters(U, V);
-        gp_Pnt2d p2(U, V);
+      Handle(IntPatch_RLine) RL = 
+        Handle(IntPatch_RLine)::DownCast(L);
+      Handle(Geom_Curve) aC3d;
+      Handle(Geom2d_Curve) aC2d1, aC2d2;
+      Standard_Real aTolReached;
+      GeomInt_IntSS::TreatRLine(RL, myHS1, myHS2, aC3d,
+                                  aC2d1, aC2d2, aTolReached);
+
+      if(aC3d.IsNull())
+        break;
+
+      Bnd_Box2d aBox1, aBox2;
+
+      const Standard_Real aU1f = myHS1->FirstUParameter(),
+                          aV1f = myHS1->FirstVParameter(),
+                          aU1l = myHS1->LastUParameter(),
+                          aV1l = myHS1->LastVParameter();
+      const Standard_Real aU2f = myHS2->FirstUParameter(),
+                          aV2f = myHS2->FirstVParameter(),
+                          aU2l = myHS2->LastUParameter(),
+                          aV2l = myHS2->LastVParameter();
+
+      aBox1.Add(gp_Pnt2d(aU1f, aV1f));
+      aBox1.Add(gp_Pnt2d(aU1l, aV1l));
+      aBox2.Add(gp_Pnt2d(aU2f, aV2f));
+      aBox2.Add(gp_Pnt2d(aU2l, aV2l));
+
+      GeomInt_VectorOfReal anArrayOfParameters;
         
-        if(p1.Distance(p2) > gp::Resolution()) {
-          TColgp_Array1OfPnt2d poles(1,2);
-          TColStd_Array1OfReal knots(1,2);
-          TColStd_Array1OfInteger mults(1,2);
-          poles(1) = p1;
-          poles(2) = p2;
-          knots(1) = f;
-          knots(2) = l;
-          mults(1) = mults(2) = 2;
-          
-          C2d = new Geom2d_BSplineCurve(poles,knots,mults,1);
-          
-          // compute reached tolerance.begin
-          gp_Pnt PMid = C->Value((f + l) * 0.5);
-          aProjector1.Perform(PMid);
-          
-          if(aProjector1.IsDone()) {
-            aProjector1.LowerDistanceParameters(U, V);
-            gp_Pnt2d pmidproj(U, V);
-            gp_Pnt2d pmidcurve2d = C2d->Value((f + l) * 0.5);
-            Standard_Real adist = pmidcurve2d.Distance(pmidproj);
-            Tol = (adist > Tol) ? adist : Tol;
-          }
-          // compute reached tolerance.end
+      //We consider here that the intersection line is same-parameter-line
+      anArrayOfParameters.Append(aC3d->FirstParameter());
+      anArrayOfParameters.Append(aC3d->LastParameter());
+
+      GeomInt_IntSS::
+        TrimILineOnSurfBoundaries(aC2d1, aC2d2, aBox1, aBox2, anArrayOfParameters);
+
+      const Standard_Integer aNbIntersSolutionsm1 = anArrayOfParameters.Length() - 1;
+
+      //Trim RLine found.
+      for(Standard_Integer anInd = 0; anInd < aNbIntersSolutionsm1; anInd++)
+      {
+        const Standard_Real aParF = anArrayOfParameters(anInd),
+                            aParL = anArrayOfParameters(anInd+1);
+
+        if((aParL - aParF) <= Precision::PConfusion())
+          continue;
+
+        const Standard_Real aPar = 0.5*(aParF + aParL);
+        gp_Pnt2d aPt;
+
+        Handle(Geom2d_Curve) aCurv2d1, aCurv2d2;
+        if(!aC2d1.IsNull())
+        {
+          aC2d1->D0(aPar, aPt);
+
+          if(aBox1.IsOut(aPt))
+            continue;
+
+          if(myApprox1)
+            aCurv2d1 = new Geom2d_TrimmedCurve(aC2d1, aParF, aParL);
+        }
+
+        if(!aC2d2.IsNull())
+        {
+          aC2d2->D0(aPar, aPt);
+
+          if(aBox2.IsOut(aPt))
+            continue;
+
+          if(myApprox2)
+            aCurv2d2 = new Geom2d_TrimmedCurve(aC2d2, aParF, aParL);
         }
+
+        Handle(Geom_Curve) aCurv3d = new Geom_TrimmedCurve(aC3d, aParF, aParL);
+
+        IntTools_Curve aIC(aCurv3d, aCurv2d1, aCurv2d2);
+        mySeqOfCurve.Append(aIC);
       }
     }
-  }
-  //
-  if (S->IsUPeriodic() && !C2d.IsNull()) {
-    // Recadre dans le domaine UV de la face
-    Standard_Real aTm, U0, aEps, period, du, U0x;
-    Standard_Boolean bAdjust;
-    //
-    aEps = Precision::PConfusion();
-    period = S->UPeriod();
-    //
-    aTm = .5*(f + l);
-    gp_Pnt2d pm = C2d->Value(aTm);
-    U0 = pm.X();
-    //
-    bAdjust = 
-      GeomInt::AdjustPeriodic(U0, umin, umax, period, U0x, du, aEps);
-    if (bAdjust) {
-      gp_Vec2d T1(du, 0.);
-      C2d->Translate(T1);
-    }
+    break;
+  default:
+    break;
+
   }
 }
 
@@ -2662,26 +2550,30 @@ Handle(Geom2d_BSplineCurve) MakeBSpline2d(const Handle(IntPatch_WLine)& theWLine
   //
   if(!isuperiodic && enlarge) {
 
-    if((theumin - uinf) > delta )
+    if(!Precision::IsInfinite(theumin) &&
+        ((theumin - uinf) > delta))
       theumin -= delta;
     else {
       theumin = uinf;
     }
 
-    if((usup - theumax) > delta )
+    if(!Precision::IsInfinite(theumax) &&
+        ((usup - theumax) > delta))
       theumax += delta;
     else
       theumax = usup;
   }
   //
   if(!isvperiodic && enlarge) {
-    if((thevmin - vinf) > delta ) {
+    if(!Precision::IsInfinite(thevmin) &&
+        ((thevmin - vinf) > delta)) {
       thevmin -= delta;
     }
     else { 
       thevmin = vinf;
     }
-    if((vsup - thevmax) > delta ) {
+    if(!Precision::IsInfinite(thevmax) &&
+        ((vsup - thevmax) > delta)) {
       thevmax += delta;
     }
     else {
@@ -3887,7 +3779,7 @@ Standard_Boolean DecompositionOfWLine(const Handle(IntPatch_WLine)& theWLine,
               bCheckAngle1 = Standard_True;
               aNewVec = gp_Vec2d(gp_Pnt2d(nU1, nV1), gp_Pnt2d(anewU, anewV));
 
-              if(aNewVec.SquareMagnitude() < (gp::Resolution() * gp::Resolution())) {
+              if(aNewVec.SquareMagnitude() < gp::Resolution()) {
                 aNewP.SetValue((surfit == 0), anewU, anewV);
                 bCheckAngle1 = Standard_False;
               }
@@ -3896,7 +3788,7 @@ Standard_Boolean DecompositionOfWLine(const Handle(IntPatch_WLine)& theWLine,
               bCheckAngle2 = Standard_True;
               aNewVec = gp_Vec2d(gp_Pnt2d(nU1, nV1), gp_Pnt2d(U, V));
 
-              if(aNewVec.SquareMagnitude() < (gp::Resolution() * gp::Resolution())) {
+              if(aNewVec.SquareMagnitude() < gp::Resolution()) {
                 bCheckAngle2 = Standard_False;
               }
             }
@@ -3916,7 +3808,7 @@ Standard_Boolean DecompositionOfWLine(const Handle(IntPatch_WLine)& theWLine,
                   aPrevNeighbourPoint.ParametersOnS2(nU2, nV2);
                 gp_Vec2d aVecOld(gp_Pnt2d(nU2, nV2), gp_Pnt2d(nU1, nV1));
 
-                if(aVecOld.SquareMagnitude() <= (gp::Resolution() * gp::Resolution())) {
+                if(aVecOld.SquareMagnitude() <= gp::Resolution()) {
                   continue;
                 }
                 else {
@@ -4033,7 +3925,7 @@ Standard_Boolean DecompositionOfWLine(const Handle(IntPatch_WLine)& theWLine,
               ap2.SetX(nU2);
               ap2.SetY(nV2);
 
-              if(ap1.SquareDistance(ap2) > (gp::Resolution() * gp::Resolution())) {
+              if(ap1.SquareDistance(ap2) > gp::Resolution()) {
                 break;
               }
             }  
@@ -4924,119 +4816,23 @@ void RefineVector(gp_Vec2d& aV2D)
   }
   aV2D.SetCoord(aC[0], aC[1]);
 }
+
 //=======================================================================
-//function : FindMaxSquareDistance
-//purpose  : 
-//=======================================================================
-Standard_Real FindMaxSquareDistance (const Standard_Real aT1,
-                                     const Standard_Real aT2,
-                                     const Standard_Real aEps,
-                                     const Handle(Geom_Curve)& aC3D,
-                                     const Handle(Geom2d_Curve)& aC2D1,
-                                     const Handle(Geom2d_Curve)& aC2D2,
-                                     const Handle(GeomAdaptor_HSurface)& myHS1,
-                                     const Handle(GeomAdaptor_HSurface)& myHS2,
-                                     const TopoDS_Face& myFace1,
-                                     const TopoDS_Face& myFace2,
-                                     const Handle(IntTools_Context)& myContext)
-{
-  Standard_Real aA, aB, aCf, aX1, aX2, aF1, aF2, aX, aF;
-  //
-  aCf=1.6180339887498948482045868343656;// =0.5*(1.+sqrt(5.));
-  aA=aT1;
-  aB=aT2;
-  aX1=aB-(aB-aA)/aCf;  
-  aF1=MaxSquareDistance(aX1, 
-                        aC3D, aC2D1, aC2D2, myHS1, myHS2, myFace1, myFace2, myContext);
-  aX2=aA+(aB-aA)/aCf;
-  aF2=MaxSquareDistance(aX2, 
-                        aC3D, aC2D1, aC2D2, myHS1, myHS2, myFace1, myFace2, myContext);
-  //
-  for(;;) {
-    //
-    if (fabs(aA-aB)<aEps) {
-      aX=0.5*(aA+aB);
-      aF=MaxSquareDistance(aX, 
-                        aC3D, aC2D1, aC2D2, myHS1, myHS2, myFace1, myFace2, myContext);
-      break;
-    }
-    if (aF1<aF2){
-      aA=aX1;
-      aX1=aX2;
-      aF1=aF2;
-      aX2=aA+(aB-aA)/aCf;
-      aF2=MaxSquareDistance(aX2, 
-                            aC3D, aC2D1, aC2D2, myHS1, myHS2, myFace1, myFace2, myContext);
-      
-    }
-    else {
-      aB=aX2;
-      aX2=aX1;
-      aF2=aF1;
-      aX1=aB-(aB-aA)/aCf; 
-      aF1=MaxSquareDistance(aX1, 
-                            aC3D, aC2D1, aC2D2, myHS1, myHS2, myFace1, myFace2, myContext);
-    }
-  }
-  return aF;
-}
-//=======================================================================
-//function : MaxSquareDistance
-//purpose  : 
+// Function : MaxDistance
+// purpose : 
 //=======================================================================
-Standard_Real MaxSquareDistance (const Standard_Real aT,
-                                 const Handle(Geom_Curve)& aC3D,
-                                 const Handle(Geom2d_Curve)& aC2D1,
-                                 const Handle(Geom2d_Curve)& aC2D2,
-                                 const Handle(GeomAdaptor_HSurface) myHS1,
-                                 const Handle(GeomAdaptor_HSurface) myHS2,
-                                 const TopoDS_Face& aF1,
-                                 const TopoDS_Face& aF2,
-                                 const Handle(IntTools_Context)& aCtx)
+Standard_Real MaxDistance(const Handle(Geom_Curve)& theC,
+                          const Standard_Real aT,
+                          GeomAPI_ProjectPointOnSurf& theProjPS)
 {
-  Standard_Boolean bIsDone;
-  Standard_Integer i;
-  Standard_Real aU, aV, aD2Max, aD2;
-  gp_Pnt2d aP2D;
-  gp_Pnt aP, aPS;
-  //
-  aD2Max=0.;
+  Standard_Real aD;
+  gp_Pnt aP;
   //
-  aC3D->D0(aT, aP);
-  if (aC3D.IsNull()) {
-    return aD2Max;
-  }
+  theC->D0(aT, aP);
+  theProjPS.Perform(aP);
+  aD = theProjPS.NbPoints() ? theProjPS.LowerDistance() : 0.;
   //
-  for (i=0; i<2; ++i) {
-    const Handle(GeomAdaptor_HSurface)& aGHS=(!i) ? myHS1 : myHS2;
-    const TopoDS_Face &aF=(!i) ? aF1 : aF2;
-    const Handle(Geom2d_Curve)& aC2D=(!i) ? aC2D1 : aC2D2;
-    //
-    if (!aC2D.IsNull()) {
-      aC2D->D0(aT, aP2D);
-      aP2D.Coord(aU, aV);
-      aGHS->D0(aU, aV, aPS);
-      aD2=aP.SquareDistance(aPS);
-      if (aD2>aD2Max) {
-        aD2Max=aD2;
-      }
-    }
-    //
-    GeomAPI_ProjectPointOnSurf& aProjector=aCtx->ProjPS(aF);
-    //
-    aProjector.Perform(aP);
-    bIsDone=aProjector.IsDone();
-    if (bIsDone) {
-      aProjector.LowerDistanceParameters(aU, aV);
-      aGHS->D0(aU, aV, aPS);
-      aD2=aP.SquareDistance(aPS);
-      if (aD2>aD2Max) {
-        aD2Max=aD2;
-      }
-    }
-  }
-  //
-  return aD2Max;
+  return aD;
 }
 
 //=======================================================================
@@ -5112,3 +4908,29 @@ Standard_Real MaxSquareDistance (const Standard_Real aT,
   }
   return !bRet;
 }
+//=======================================================================
+//function : CorrectPlaneBoundaries
+//purpose  : 
+//=======================================================================
+ void CorrectPlaneBoundaries(Standard_Real& aUmin,
+                             Standard_Real& aUmax, 
+                             Standard_Real& aVmin, 
+                             Standard_Real& aVmax) 
+{
+  if (!(Precision::IsInfinite(aUmin) ||
+        Precision::IsInfinite(aUmax))) { 
+    Standard_Real dU;
+    //
+    dU=0.1*(aUmax-aUmin);
+    aUmin=aUmin-dU;
+    aUmax=aUmax+dU;
+  }
+  if (!(Precision::IsInfinite(aVmin) ||
+        Precision::IsInfinite(aVmax))) { 
+    Standard_Real dV;
+    //
+    dV=0.1*(aVmax-aVmin);
+    aVmin=aVmin-dV;
+    aVmax=aVmax+dV;
+  }
+}