Salome HOME
Lot 2: change bathy associated to natural object propagated to all cases without...
[modules/hydro.git] / src / HYDROData / HYDROData_StreamLinearInterpolation.cxx
index 9cecdf096761059c2fddb18e0c7b01662e92b813..23eab97ec0a30466d10ae142e26394099fac8f1f 100644 (file)
@@ -107,7 +107,7 @@ static void GetMaxDist(const std::vector<gp_Pnt2d>& PNTS,
 static void InsertPoints(std::vector<gp_Pnt2d>& points, int nbpoints)
 {
   points.reserve(points.size() + nbpoints);
-  while (nbpoints)
+  while (nbpoints>0)
   {
     double dmax=0, dmax2=0;
     int imax=-1;
@@ -124,6 +124,8 @@ static void InsertPoints(std::vector<gp_Pnt2d>& points, int nbpoints)
     double Y0 = p1.Y();
     double X1 = p2.X();
     double Y1 = p2.Y();
+    if (nbPins>nbpoints) //deny to add more points than needed
+      nbPins = nbpoints;
     for (int i=0;i<nbPins;i++)
     {
       double t = ((double)i+1)/((double)nbPins+1);
@@ -134,6 +136,7 @@ static void InsertPoints(std::vector<gp_Pnt2d>& points, int nbpoints)
     }
     nbpoints-=nbPins;
   }
+  assert (nbpoints == 0);
 }
 
 static void PolyToCurve2d(const Handle(HYDROData_PolylineXY)& poly, Handle(Geom2d_Curve)& c2d)
@@ -168,6 +171,7 @@ static void PolyToCurve2d(const Handle(HYDROData_PolylineXY)& poly, Handle(Geom2
 static void InterProfilesAndHAX(const HYDROData_SequenceOfObjects& profiles, 
   const Handle(Geom2d_Curve)& Hax2d, 
   std::map<double, Handle(HYDROData_Profile)>& profToInterParam,
+  std::vector<bool>& paramToSwapFlag,
   std::vector<std::string>* warnings)
 {
   for (int i=1;i<=profiles.Size();i++)
@@ -185,6 +189,7 @@ static void InterProfilesAndHAX(const HYDROData_SequenceOfObjects& profiles,
       continue;
     }
     Handle(Geom2d_Line) lin2d = new Geom2d_Line(P1,gp_Dir2d(P2.XY()-P1.XY()));
+    gp_Dir2d prof_dir2d = lin2d->Direction();
 
     Geom2dAdaptor_Curve linAd(lin2d, 0, d);
     Geom2dAdaptor_Curve haxAd(Hax2d);
@@ -208,6 +213,15 @@ static void InterProfilesAndHAX(const HYDROData_SequenceOfObjects& profiles,
       continue; 
     }
     double param = isec.Point(1).ParamOnSecond();
+    gp_Pnt2d int_p2d;
+    gp_Vec2d vec_on_hax_intersec;
+    haxAd.D1(param, int_p2d, vec_on_hax_intersec);
+    gp_Dir2d d_hax(vec_on_hax_intersec);
+    double ang = d_hax.Angle(prof_dir2d);
+    if (ang>0)    
+      paramToSwapFlag.push_back(true);
+    else
+      paramToSwapFlag.push_back(false);
     profToInterParam[param] = aProfile;
   }
 }
@@ -368,6 +382,11 @@ void BuildFace(const std::map<double, std::pair<IntRes2d_IntersectionPoint, IntR
   }  
 }
 
+void reversePoints(std::vector<gp_Pnt2d>& points)
+{
+  std::reverse(points.begin(), points.end());
+}
+
 void HYDROData_StreamLinearInterpolation::Perform(const HYDROData_SequenceOfObjects& profiles,
   int pointsToInsert,
   double stepOnHA, 
@@ -394,8 +413,9 @@ void HYDROData_StreamLinearInterpolation::Perform(const HYDROData_SequenceOfObje
   }
 
   std::map<double, Handle(HYDROData_Profile)> InterParamToProf;
+  std::vector<bool> paramToSwapFlag;
   //profToInterParam is output param map: profile to intersection param on hydr.axis
-  InterProfilesAndHAX(profiles, Hax2d, InterParamToProf, warnings);
+  InterProfilesAndHAX(profiles, Hax2d, InterParamToProf, paramToSwapFlag, warnings);
   //
   std::vector<std::vector<gp_Pnt2d>> profilesPoints;
   profilesPoints.reserve(InterParamToProf.size());
@@ -490,6 +510,16 @@ void HYDROData_StreamLinearInterpolation::Perform(const HYDROData_SequenceOfObje
     InsertPoints(profilesPoints[i], nbPointsToInsert);
   }
 
+  for (int i=0;i<profilesPoints.size();i++)
+  {
+    bool toSwap = paramToSwapFlag[i];     
+    if (toSwap)
+    {
+      std::vector<gp_Pnt2d>& prof = profilesPoints[i];
+      reversePoints(prof);
+    }
+  }
+
   TopoDS_Compound cmp2d;
   if ( buildPresentationShapes)
   {