Commit 899dd9db authored by Pierre Fernbach's avatar Pierre Fernbach
Browse files

[piecewise] refactor/simplify constructor from discret point

parent 4cc0b596
...@@ -216,51 +216,23 @@ namespace curves ...@@ -216,51 +216,23 @@ namespace curves
} }
template<typename Polynomial> template<typename Polynomial>
static piecewise_curve<Time, Numeric, Safe, Point, T_Point, Polynomial> static piecewise_curve<Time, Numeric, Safe, Point, T_Point, Polynomial>
convert_discrete_points_to_polynomial(T_Point points, Time T_min, Time T_max) convert_discrete_points_to_polynomial(T_Point points, t_time_t time_points)
{ {
if(Safe &! (points.size()>1)) if(Safe &! (points.size()>1))
{ {
//std::cout<<"[Min,Max]=["<<T_min_<<","<<T_max_<<"]"<<" t="<<t<<std::endl; //std::cout<<"[Min,Max]=["<<T_min_<<","<<T_max_<<"]"<<" t="<<t<<std::endl;
throw std::invalid_argument("piecewise_curve -> convert_discrete_points_to_polynomial, Error, less than 2 discrete points"); throw std::invalid_argument("piecewise_curve::convert_discrete_points_to_polynomial: Error, less than 2 discrete points");
} }
typedef piecewise_curve<Time, Numeric, Safe, Point, T_Point, Polynomial> piecewise_curve_out_t; if(points.size() != time_points.size()){
Time discretization_step = (T_max-T_min)/Time(points.size()-1); throw std::invalid_argument("piecewise_curve::convert_discrete_points_to_polynomial: Error, points and time_points must have the same size.");
Time time_actual = T_min; }
// Initialization at first points piecewise_curve<Time, Numeric, Safe, Point, T_Point, Polynomial> piecewise_res;
point_t actual_point = points[0];
point_t next_point = points[1]; for(size_t i = 1 ; i < points.size() ; ++i){
point_t coeff_order_zero(actual_point); piecewise_res.add_curve(Polynomial(points[i-1],points[i],time_points[i-1],time_points[i]));
point_t coeff_order_one((next_point-actual_point)/discretization_step);
t_point_t coeffs;
coeffs.push_back(coeff_order_zero);
coeffs.push_back(coeff_order_one);
Polynomial pol(coeffs,time_actual,time_actual+discretization_step);
piecewise_curve_out_t ppc(pol);
time_actual += discretization_step;
// Other points
for (std::size_t i=1; i<points.size()-2; i++)
{
coeffs.clear();
actual_point = points[i];
next_point = points[i+1];
coeff_order_zero = actual_point;
coeff_order_one = (next_point-actual_point)/discretization_step;
coeffs.push_back(coeff_order_zero);
coeffs.push_back(coeff_order_one);
ppc.add_curve(Polynomial(coeffs,time_actual,time_actual+discretization_step));
time_actual += discretization_step;
} }
// Last points return piecewise_res;
coeffs.clear();
actual_point = points[points.size()-2];
next_point = points[points.size()-1];
coeff_order_zero = actual_point;
coeff_order_one = (next_point-actual_point)/discretization_step;
coeffs.push_back(coeff_order_zero);
coeffs.push_back(coeff_order_one);
ppc.add_curve(Polynomial(coeffs,time_actual,T_max));
return ppc;
} }
private: private:
...@@ -357,4 +329,4 @@ namespace curves ...@@ -357,4 +329,4 @@ namespace curves
} // end namespace } // end namespace
#endif // _CLASS_PIECEWISE_CURVE #endif // _CLASS_PIECEWISE_CURVE
\ No newline at end of file
...@@ -1335,37 +1335,28 @@ void curveAbcDimDynamicTest(bool& error) ...@@ -1335,37 +1335,28 @@ void curveAbcDimDynamicTest(bool& error)
void piecewiseCurveConversionFromDiscretePointsTest(bool& error) void piecewiseCurveConversionFromDiscretePointsTest(bool& error)
{ {
try std::string errMsg("piecewiseCurveConversionFromDiscretePointsTest, Error, value on curve is wrong : ");
{ point_t p0(0.,0.,0.);
std::string errMsg("piecewiseCurveConversionFromDiscretePointsTest, Error, value on curve is wrong : "); point_t p1(1.,2.,3.);
point_t p0(0.,0.,0.); point_t p2(4.,4.,4.);
point_t p1(1.,2.,3.); point_t p3(10.,10.,10.);
point_t p2(4.,4.,4.); point_t p_test_0_5 = (p0+p1)/2.0;
point_t p3(10.,10.,10.); t_point_t points;
point_t p_test_0_5 = (p0+p1)/2.0; points.push_back(p0);
t_point_t points; points.push_back(p1);
points.push_back(p0); points.push_back(p2);
points.push_back(p1); points.push_back(p3);
points.push_back(p2); double T_min = 1.0;
points.push_back(p3); double T_max = 3.0;
double T_min = 1.0; double timestep = (T_max-T_min)/double(points.size()-1);
double T_max = 3.0; std::vector<double> time_points;
double timestep = (T_max-T_min)/double(points.size()-1); for(size_t i=0;i<points.size();++i)
piecewise_polynomial_curve_t ppc = piecewise_polynomial_curve_t:: time_points.push_back(T_min+i*timestep);
convert_discrete_points_to_polynomial<polynomial_t>(points,T_min,T_max); piecewise_polynomial_curve_t ppc = piecewise_polynomial_curve_t::
if (!ppc.is_continuous(0)) convert_discrete_points_to_polynomial<polynomial_t>(points,time_points);
{ if (!ppc.is_continuous(0))
std::cout<<"piecewiseCurveConversionFromDiscretePointsTest, Error, piecewise curve is not C0"<<std::endl; {
error = true; std::cout<<"piecewiseCurveConversionFromDiscretePointsTest, Error, piecewise curve is not C0"<<std::endl;
}
ComparePoints(p0, ppc(T_min), errMsg, error);
ComparePoints(p_test_0_5, ppc(T_min+timestep/2.0), errMsg, error);
ComparePoints(p1, ppc(T_min+timestep), errMsg, error);
ComparePoints(p2, ppc(T_min+2*timestep), errMsg, error);
ComparePoints(p3, ppc(T_max), errMsg, error);
}
catch(...)
{
error = true; error = true;
std::cout<<"Error in piecewiseCurveConversionFromDiscretePointsTest"<<std::endl; std::cout<<"Error in piecewiseCurveConversionFromDiscretePointsTest"<<std::endl;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment