0024023: Revamp the OCCT Handle -- downcast (automatic)
[occt.git] / src / IntPatch / IntPatch_PrmPrmIntersection.cxx
index 93c6eda..5b0fef3 100644 (file)
@@ -68,6 +68,46 @@ static void AddWLine(IntPatch_SequenceOfLine      &theLines,
                      const Handle(IntPatch_WLine) &theWLine,
                      const Standard_Real           Deflection);
 
+//=======================================================================
+//function : DublicateOfLinesProcessing
+//purpose  : Decides, if rejecting current line is necessary
+//=======================================================================
+static void DublicateOfLinesProcessing( const IntWalk_PWalking& thePW,
+                                        const Standard_Integer theWLID,
+                                        IntPatch_SequenceOfLine& theLines,
+                                        Standard_Boolean& theIsRejectReq)
+{
+  const Handle(IntPatch_WLine)& anExistWL =
+                      *((Handle(IntPatch_WLine)*)&theLines.Value(theWLID));
+  const Standard_Integer aNbPrevPoints = anExistWL->NbPnts();
+  const Standard_Integer aNbCurrPoints = thePW.NbPoints();
+
+  if(aNbPrevPoints < aNbCurrPoints)
+  {//Remove preview line
+    theLines.Remove(theWLID);
+    theIsRejectReq = Standard_False;
+  }
+  else if(aNbPrevPoints == aNbCurrPoints)
+  {
+    Standard_Real aLPrev = 0.0, aLCurr = 0.0;
+    for(Standard_Integer aNbPP = 1; aNbPP < aNbPrevPoints; aNbPP++)
+    {
+      const gp_Pnt  aP1prev(anExistWL->Point(aNbPP).Value()),
+        aP2prev(anExistWL->Point(aNbPP+1).Value());
+      const gp_Pnt  aP1curr(thePW.Value(aNbPP).Value()),
+        aP2curr(thePW.Value(aNbPP+1).Value());
+
+      aLPrev += aP1prev.Distance(aP2prev);
+      aLCurr += aP1curr.Distance(aP2curr);
+    }
+
+    if(aLPrev < aLCurr)
+    {//Remove preview line
+      theLines.Remove(theWLID);
+      theIsRejectReq = Standard_False;
+    }
+  }
+}
 
 //==================================================================================
 // function : 
@@ -313,7 +353,7 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&
                   Point3dDebut = PW.Value(1).Value();
                   const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
                   Point3dFin   = PointFin.Value();
-                  for( ver = 1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
+                  for( ver = 1 ; ver<= NbLigCalculee ; ver++) { 
                     const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
 
                     // Check end point if it is on existing line.
@@ -325,12 +365,19 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&
 
                     const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
                     const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
-                    if( Point3dDebut.Distance(verPointDebut.Value()) <= TolTangency ) { 
-                      if(Point3dFin.Distance(verPointFin.Value()) <= TolTangency)
-                        RejetLigne = Standard_True; 
+                    if( (Point3dDebut.Distance(verPointDebut.Value()) <= TolTangency) &&
+                        (Point3dFin.Distance(verPointFin.Value()) <= TolTangency))
+                    { 
+                      RejetLigne = Standard_True; 
+                      break;
                     }
                   }
 
+                  if(RejetLigne)
+                  {
+                    DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
+                  }
+
                   if(!RejetLigne) { 
                     // Calculation transition
                     IntSurf_TypeTrans trans1,trans2;
@@ -451,7 +498,7 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&
               Point3dDebut = PW.Value(1).Value();
               const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
               Point3dFin   = PointFin.Value();
-              for(ver=1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
+              for(ver=1 ; ver<= NbLigCalculee ; ver++) { 
                 const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
 
                 // Check end point if it is on existing line.
@@ -463,14 +510,19 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&
 
                 const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
                 const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
-                if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency)
+                if( (Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) ||
+                    (Point3dFin.Distance(verPointFin.Value()) < TolTangency))
+                {
                   RejetLigne = Standard_True; 
-                else { 
-                  if(Point3dFin.Distance(verPointFin.Value()) < TolTangency)
-                    RejetLigne = Standard_True; 
+                  break;
                 }
               }
 
+              if(RejetLigne)
+              {
+                DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
+              }
+
               if(!RejetLigne) { 
                 IntSurf_TypeTrans trans1,trans2;
                 Standard_Real locu,locv;
@@ -676,18 +728,23 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&
                   RejetLigne = Standard_False;
                   Point3dDebut = PW.Value(1).Value();
                   Point3dFin   = PW.Value(PW.NbPoints()).Value();
-                  for(ver=1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
+                  for(ver=1 ; ver<= NbLigCalculee ; ver++) { 
                     const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
                     const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
                     const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
-                    if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency)
+                    if( (Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) ||
+                        (Point3dFin.Distance(verPointFin.Value()) < TolTangency)) 
+                    {
                       RejetLigne = Standard_True; 
-                    else { 
-                      if(Point3dFin.Distance(verPointFin.Value()) < TolTangency)
-                        RejetLigne = Standard_True; 
+                      break;
                     }
                   }
 
+                  if(RejetLigne)
+                  {
+                    DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
+                  }
+
                   if(!RejetLigne) { 
                     IntSurf_TypeTrans trans1,trans2;
                     Standard_Real locu,locv;
@@ -737,7 +794,7 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&
                       imax++;
 
                     if(imin<imax) { 
-                      Handle_IntSurf_LineOn2S PWLine = new IntSurf_LineOn2S();
+                      Handle(IntSurf_LineOn2S) PWLine = new IntSurf_LineOn2S();
                       for(i=imin;i<=imax;i++) 
                         PWLine->Add(PW.Line()->Value(i));
 
@@ -844,18 +901,23 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&
                 RejetLigne = Standard_False;
                 Point3dDebut = PW.Value(1).Value();
                 Point3dFin   = PW.Value(PW.NbPoints()).Value();
-                for( ver = 1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
+                for( ver = 1 ; ver<= NbLigCalculee ; ver++) { 
                   const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
                   const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
                   const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
-                  if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency)
+                  if( (Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) ||
+                      (Point3dFin.Distance(verPointFin.Value()) < TolTangency))
+                  {
                     RejetLigne = Standard_True; 
-                  else { 
-                    if(Point3dFin.Distance(verPointFin.Value()) < TolTangency)
-                      RejetLigne = Standard_True; 
+                    break;
                   }
                 }
 
+                if(RejetLigne)
+                {
+                  DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
+                }
+
                 if(!RejetLigne)        { 
                   IntSurf_TypeTrans trans1,trans2;
                   Standard_Real locu,locv;
@@ -905,7 +967,7 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&
                     imax++;
 
                   if(imin<imax) { 
-                    Handle_IntSurf_LineOn2S PWLine = new IntSurf_LineOn2S();
+                    Handle(IntSurf_LineOn2S) PWLine = new IntSurf_LineOn2S();
                     for(i=imin;i<=imax;i++)
                       PWLine->Add(PW.Line()->Value(i));
 
@@ -959,7 +1021,7 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&
 // function : NewLine
 // purpose  : 
 //==================================================================================
-Handle_IntPatch_Line IntPatch_PrmPrmIntersection::NewLine (const Handle(Adaptor3d_HSurface)&    Surf1,
+Handle(IntPatch_Line) IntPatch_PrmPrmIntersection::NewLine (const Handle(Adaptor3d_HSurface)&    Surf1,
                                                            const Handle(Adaptor3d_HSurface)&    Surf2,
                                                            const Standard_Integer NumLine,
                                                            const Standard_Integer Low,
@@ -1316,8 +1378,102 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&
   Periods[3] = (Surf2->IsVPeriodic())? Surf2->VPeriod() : 0.;
 
   IntSurf_ListIteratorOfListOfPntOn2S IterLOP1(LOfPnts);
+  if (Surf1->IsUClosed() || Surf1->IsVClosed() ||
+      Surf2->IsUClosed() || Surf2->IsVClosed())
+  {
+    Standard_Real TolPar = Precision::PConfusion();
+    IntSurf_ListOfPntOn2S AdditionalPnts;
+    Standard_Real NewU1, NewV1, NewU2, NewV2;
+    for(; IterLOP1.More(); IterLOP1.Next())
+    {
+      IntSurf_PntOn2S Pnt = IterLOP1.Value();
+      Pnt.Parameters(U1, V1, U2, V2);
+      IntSurf_PntOn2S NewPnt;
+      if (Surf1->IsUClosed())
+      {
+        if (Abs(U1 - Surf1->FirstUParameter()) <= TolPar)
+        {
+          NewU1 = Surf1->LastUParameter();
+          NewPnt.SetValue( NewU1, V1, U2, V2 );
+          AdditionalPnts.Append(NewPnt);
+        }
+        else if (Abs(U1 - Surf1->LastUParameter()) <= TolPar)
+        {
+          NewU1 = Surf1->FirstUParameter();
+          NewPnt.SetValue( NewU1, V1, U2, V2 );
+          AdditionalPnts.Append(NewPnt);
+        }
+      }
+      if (Surf1->IsVClosed())
+      {
+        if (Abs(V1 - Surf1->FirstVParameter()) <= TolPar)
+        {
+          NewV1 = Surf1->LastVParameter();
+          NewPnt.SetValue( U1, NewV1, U2, V2 );
+          AdditionalPnts.Append(NewPnt);
+        }
+        else if (Abs(V1 - Surf1->LastVParameter()) <= TolPar)
+        {
+          NewV1 = Surf1->FirstVParameter();
+          NewPnt.SetValue( U1, NewV1, U2, V2 );
+          AdditionalPnts.Append(NewPnt);
+        }
+      }
+      if (Surf2->IsUClosed())
+      {
+        if (Abs(U2 - Surf2->FirstUParameter()) <= TolPar)
+        {
+          NewU2 = Surf2->LastUParameter();
+          NewPnt.SetValue( U1, V1, NewU2, V2);
+          AdditionalPnts.Append(NewPnt);
+        }
+        else if (Abs(U2 - Surf2->LastUParameter()) <= TolPar)
+        {
+          NewU2 = Surf2->FirstUParameter();
+          NewPnt.SetValue( U1, V1, NewU2, V2);
+          AdditionalPnts.Append(NewPnt);
+        }
+      }
+      if (Surf2->IsVClosed())
+      {
+        if (Abs(V2 - Surf2->FirstVParameter()) <= TolPar)
+        {
+          NewV2 = Surf2->LastVParameter();
+          NewPnt.SetValue( U1, V1, U2, NewV2 );
+          AdditionalPnts.Append(NewPnt);
+        }
+        else if (Abs(V2 - Surf2->LastVParameter()) <= TolPar)
+        {
+          NewV2 = Surf2->FirstVParameter();
+          NewPnt.SetValue( U1, V1, U2, NewV2 );
+          AdditionalPnts.Append(NewPnt);
+        }
+      }
+    }
+    //Cut repeated points
+    for (IterLOP1.Initialize(LOfPnts); IterLOP1.More(); IterLOP1.Next())
+    {
+      IntSurf_PntOn2S aPnt = IterLOP1.Value();
+      aPnt.Parameters(U1, V1, U2, V2);
+      IntSurf_ListIteratorOfListOfPntOn2S iter2(AdditionalPnts);
+      while (iter2.More())
+      {
+        IntSurf_PntOn2S aNewPnt = iter2.Value();
+        aNewPnt.Parameters(NewU1, NewV1, NewU2, NewV2);
+        if (Abs(U1 - NewU1) <= TolPar &&
+            Abs(V1 - NewV1) <= TolPar &&
+            Abs(U2 - NewU2) <= TolPar &&
+            Abs(V2 - NewV2) <= TolPar)
+          AdditionalPnts.Remove(iter2);
+        else
+          iter2.Next();
+      }
+    }
 
-  for(; IterLOP1.More(); IterLOP1.Next()){
+    LOfPnts.Append(AdditionalPnts);
+  }
+
+  for(IterLOP1.Initialize(LOfPnts); IterLOP1.More(); IterLOP1.Next()){
     IntSurf_PntOn2S Pnt = IterLOP1.Value();
     Pnt.Parameters(U1, V1, U2, V2);
     if(U1>UmaxLig1) UmaxLig1=U1;
@@ -1379,7 +1535,7 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&
             Point3dDebut = PW.Value(1).Value();
             const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
             Point3dFin   = PointFin.Value();
-            for( ver = 1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
+            for( ver = 1 ; ver<= NbLigCalculee ; ver++) { 
               const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
 
               // Check end point if it is on existing line.
@@ -1391,12 +1547,19 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&
 
               const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
               const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
-              if(Point3dDebut.Distance(verPointDebut.Value()) <= TolTangency) { 
-                if(Point3dFin.Distance(verPointFin.Value()) <= TolTangency)
-                  RejetLigne = Standard_True; 
+              if( (Point3dDebut.Distance(verPointDebut.Value()) <= TolTangency) &&
+                  (Point3dFin.Distance(verPointFin.Value()) <= TolTangency)) 
+              {
+                RejetLigne = Standard_True; 
+                break;
               }
             }
 
+            if(RejetLigne)
+            {
+              DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
+            }
+
             if(!RejetLigne) {
               IntSurf_TypeTrans trans1,trans2;
               Standard_Real locu,locv;
@@ -1458,7 +1621,7 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&
                     IntPatch_IType aType = aSLine->ArcType();
                     if( aType != IntPatch_Walking)
                       continue;
-                    const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
+                    Handle(IntPatch_WLine) aWLine (Handle(IntPatch_WLine)::DownCast (aSLine));
                     Standard_Integer tnbV = aWLine->NbVertex();
                     Standard_Integer tiV;
                     for( tiV = 1; tiV <= tnbV; tiV++ ) {
@@ -1482,7 +1645,7 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&
 
                   if( VDMin != 0 ) {
                     const Handle(IntPatch_Line)& aSLine = SLin.Value(WLDMin);
-                    const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
+                    Handle(IntPatch_WLine) aWLine (Handle(IntPatch_WLine)::DownCast (aSLine));
                     Standard_Integer tiVpar = (Standard_Integer)aWLine->Vertex(VDMin).ParameterOnLine();
                     Standard_Integer ciVpar = (Standard_Integer)wline->Vertex(ciV).ParameterOnLine();
                     Standard_Real u11 = 0., u12 = 0., v11 = 0., v12 = 0.;
@@ -1633,7 +1796,7 @@ void IntPatch_PrmPrmIntersection::Perform(const Handle(Adaptor3d_HSurface)&    S
 
   //    Standard_Real nIncrement=Increment;
   //    if(MaxOscill>10) { 
-  //  #ifdef DEB 
+  //  #ifdef OCCT_DEBUG
   //      cout<<"\n IntPatch_PrmPrmIntersection.gxx : Increment:"<<Increment<<" -> "<<Increment/(0.5*MaxOscill)<<endl;
   //  #endif
   //      nIncrement/=0.5*MaxOscill;
@@ -2197,6 +2360,7 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
                             //modified by NIZNHY-PKV Tue May 10 11:08:13 2011t
 
                             RejectLine = Standard_True;
+                            ver--;
                             break;
                           }
                         }//for(m=1; m<iPWNbPoints; ++m){
@@ -2204,6 +2368,11 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
                     }// for( ver = 1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
                     //
 
+                    if(RejectLine)
+                    {
+                      DublicateOfLinesProcessing(PW, ver, SLin, RejectLine);
+                    }
+
                     if(!RejectLine)
                     {
                       IntSurf_TypeTrans trans1,trans2;
@@ -2272,7 +2441,7 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
                             IntPatch_IType aType = aSLine->ArcType();
                             if( aType != IntPatch_Walking)
                               continue;
-                            const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
+                            Handle(IntPatch_WLine) aWLine (Handle(IntPatch_WLine)::DownCast (aSLine));
                             Standard_Integer tnbV = aWLine->NbVertex();
                             Standard_Integer tiV;
                             for( tiV = 1; tiV <= tnbV; tiV++ )
@@ -2300,7 +2469,7 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
                           if( VDMin != 0 )
                           {
                             const Handle(IntPatch_Line)& aSLine = SLin.Value(WLDMin);
-                            const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
+                            Handle(IntPatch_WLine) aWLine (Handle(IntPatch_WLine)::DownCast (aSLine));
                             Standard_Integer tiVpar = (Standard_Integer)aWLine->Vertex(VDMin).ParameterOnLine();
                             Standard_Integer ciVpar = (Standard_Integer)wline->Vertex(ciV).ParameterOnLine();
                             Standard_Real u11 = 0., u12 = 0., v11 = 0., v12 = 0.;
@@ -2734,7 +2903,7 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
             const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
             Point3dFin   = PointFin.Value();
 
-            for(ver=1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++)
+            for(ver=1 ; ver<= NbLigCalculee ; ver++)
             {
               const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
               //-- Handle(IntPatch_WLine) verwline=Handle(IntPatch_WLine)::DownCast(SLin.Value(ver));
@@ -2749,19 +2918,19 @@ void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
 
               const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
               const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
-              if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency)
+              if( (Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) ||
+                  (Point3dFin.Distance(verPointFin.Value()) < TolTangency))
               {
                 RejetLigne = Standard_True; 
-              }
-              else
-              {
-                if(Point3dFin.Distance(verPointFin.Value()) < TolTangency)
-                {
-                  RejetLigne = Standard_True; 
-                }
+                break;
               }
             }
 
+            if(RejetLigne)
+            {
+              DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
+            }
+
             if(!RejetLigne)
             {
               IntSurf_TypeTrans trans1,trans2;
@@ -2904,7 +3073,14 @@ public:
     const Standard_Integer j) { 
       return myP2[Index(i,j)];
   };
-protected:
+
+private:
+
+  IntPatch_InfoPD (const IntPatch_InfoPD&);
+  IntPatch_InfoPD& operator=(const IntPatch_InfoPD&);
+
+private:
+
   Standard_Integer myNBI;
   char *myP1DS2;
   char *myP2DS1;