Commit 3744ba66 authored by JasonChmn's avatar JasonChmn Committed by Pierre Fernbach
Browse files

Test serialization in serialize_test_class => Problem

parent e7701e1f
......@@ -34,6 +34,9 @@ IF(BUILD_PYTHON_INTERFACE)
ADD_SUBDIRECTORY(python)
ENDIF(BUILD_PYTHON_INTERFACE)
#find_package(Boost 1.58 REQUIRED unit_test_framework system serialization)
#SET(BOOST_COMPONENTS unit_test_framework serialization)
SEARCH_FOR_BOOST()
INCLUDE_DIRECTORIES(SYSTEM ${Boost_INCLUDE_DIRS})
......
......@@ -12,6 +12,7 @@ SET(${PROJECT_NAME}_HEADERS
linear_variable.h
cubic_hermite_spline.h
piecewise_curve.h
serialize_test_class.h
)
INSTALL(FILES
......@@ -20,3 +21,4 @@ INSTALL(FILES
)
ADD_SUBDIRECTORY(helpers)
ADD_SUBDIRECTORY(serialization)
......@@ -13,6 +13,8 @@
#define _STRUCT_CURVE_ABC
#include "MathDefs.h"
#include "serialization/archive.hpp"
#include "serialization/eigen-matrix.hpp"
#include <functional>
......
......@@ -11,6 +11,7 @@
#include "curve_abc.h"
#include "curve_conversion.h"
namespace curves
{
/// \class PiecewiseCurve.
......@@ -39,6 +40,10 @@ struct piecewise_curve : public curve_abc<Time, Numeric, Safe, Point>
public:
piecewise_curve()
: size_(0)
{}
/// \brief Constructor.
/// Initialize a piecewise curve by giving the first curve.
/// \param pol : a polynomial curve.
......@@ -47,8 +52,6 @@ struct piecewise_curve : public curve_abc<Time, Numeric, Safe, Point>
{
size_ = 0;
add_curve(cf);
time_curves_.push_back(cf.min());
T_min_ = cf.min();
}
virtual ~piecewise_curve(){}
......@@ -93,6 +96,12 @@ struct piecewise_curve : public curve_abc<Time, Numeric, Safe, Point>
size_ = curves_.size();
T_max_ = cf.max();
time_curves_.push_back(T_max_);
if (size_ == 1)
{
// First curve added
time_curves_.push_back(cf.min());
T_min_ = cf.min();
}
}
/// \brief Check if the curve is continuous of order given.
......@@ -227,7 +236,50 @@ struct piecewise_curve : public curve_abc<Time, Numeric, Safe, Point>
ppc.add_curve(Polynomial(coeffs,time_actual,T_max));
return ppc;
}
/*
template <typename Piecewise_curve, typename Polynomial>
static void serializeToFile(Piecewise_curve pc, std::string file_path)
{
// Get piecewise polynomial curve corresponding
piecewise_curve<Time, Numeric, Dim, Safe, Point, T_Point, Polynomial> ppc = pc.convert_piecewise_curve_to_polynomial<Polynomial>();
// Serialize
std::ofstream ofs(file_path.c_str());
if(ofs)
{
boost::archive::binary_oarchive oa(ofs);
oa << ppc;
}
else
{
throw "PiecewiseCurve, Error while serializing to file";
}
ofs.close();
}
template <typename Piecewise_polynomial_curve, typename Polynomial>
static Piecewise_polynomial_curve deserializeFromFile(std::string file_path)
{
// Get a piecewise polynomial curve
point_t p;
t_point_t t_p;
t_p.push_back(p);
Polynomial pol(t_p.begin(), t_p.end(),0,1);
Piecewise_polynomial_curve ppc(pol);
// Deserialize in it
std::ifstream ifs(file_path.c_str());
if(ifs)
{
boost::archive::binary_iarchive ia(ifs);
ia >> ppc;
}
else
{
throw "PiecewiseCurve, Error while deserializing from file";
}
ifs.close();
return ppc;
}
*/
private:
/// \brief Get index of the interval corresponding to time t for the interpolation.
......@@ -268,6 +320,7 @@ struct piecewise_curve : public curve_abc<Time, Numeric, Safe, Point>
return left_id-1;
}
/*Helpers*/
public:
/// \brief Get the minimum time for which the curve is defined
......
......@@ -23,6 +23,10 @@
#include <functional>
#include <stdexcept>
#include "serialization/archive.hpp"
#include "serialization/eigen-matrix.hpp"
#include <boost/serialization/vector.hpp>
namespace curves
{
/// \class polynomial.
......@@ -45,6 +49,9 @@ struct polynomial : public curve_abc<Time, Numeric, Safe, Point>
/* Constructors - destructors */
public:
polynomial() {}
/// \brief Constructor.
/// \param coefficients : a reference to an Eigen matrix where each column is a coefficient,
/// from the zero order coefficient, up to the highest order. Spline order is given
......@@ -53,7 +60,7 @@ struct polynomial : public curve_abc<Time, Numeric, Safe, Point>
/// \param max : UPPER bound on interval definition of the curve.
polynomial(const coeff_t& coefficients, const time_t min, const time_t max)
: curve_abc_t(),
coefficients_(coefficients), dim_(Dim), degree_(coefficients_.cols()-1), t_min_(min), t_max_(max)
coefficients_(coefficients), dim_(Dim), degree_(coefficients_.cols()-1), T_min_(min), T_max_(max)
{
safe_check();
}
......@@ -67,7 +74,7 @@ struct polynomial : public curve_abc<Time, Numeric, Safe, Point>
polynomial(const T_Point& coefficients, const time_t min, const time_t max)
: curve_abc_t(),
coefficients_(init_coeffs(coefficients.begin(), coefficients.end())),
dim_(Dim), degree_(coefficients_.cols()-1), t_min_(min), t_max_(max)
dim_(Dim), degree_(coefficients_.cols()-1), T_min_(min), T_max_(max)
{
safe_check();
}
......@@ -81,7 +88,7 @@ struct polynomial : public curve_abc<Time, Numeric, Safe, Point>
template<typename In>
polynomial(In zeroOrderCoefficient, In out, const time_t min, const time_t max)
:coefficients_(init_coeffs(zeroOrderCoefficient, out)),
dim_(Dim), degree_(coefficients_.cols()-1), t_min_(min), t_max_(max)
dim_(Dim), degree_(coefficients_.cols()-1), T_min_(min), T_max_(max)
{
safe_check();
}
......@@ -95,7 +102,7 @@ struct polynomial : public curve_abc<Time, Numeric, Safe, Point>
polynomial(const polynomial& other)
: coefficients_(other.coefficients_),
dim_(other.dim_), degree_(other.degree_), t_min_(other.t_min_), t_max_(other.t_max_)
dim_(other.dim_), degree_(other.degree_), T_min_(other.T_min_), T_max_(other.T_max_)
{}
......@@ -106,7 +113,7 @@ struct polynomial : public curve_abc<Time, Numeric, Safe, Point>
{
if(Safe)
{
if(t_min_ > t_max_)
if(T_min_ > T_max_)
{
std::invalid_argument("Tmin should be inferior to Tmax");
}
......@@ -126,8 +133,8 @@ struct polynomial : public curve_abc<Time, Numeric, Safe, Point>
/// \param return \f$x(t)\f$, point corresponding on curve at time t.
virtual point_t operator()(const time_t t) const
{
if((t < t_min_ || t > t_max_) && Safe){ throw std::out_of_range("TODO");}
time_t const dt (t-t_min_);
if((t < T_min_ || t > T_max_) && Safe){ throw std::out_of_range("TODO");}
time_t const dt (t-T_min_);
time_t cdt(1);
point_t currentPoint_ = point_t::Zero();
for(int i = 0; i < degree_+1; ++i, cdt*=dt)
......@@ -141,11 +148,11 @@ struct polynomial : public curve_abc<Time, Numeric, Safe, Point>
/// \return \f$x(t)\f$ point corresponding on spline at time t.
virtual point_t operator()(const time_t t) const
{
if((t < t_min_ || t > t_max_) && Safe)
if((t < T_min_ || t > T_max_) && Safe)
{
throw std::invalid_argument("error in polynomial : time t to evaluate should be in range [Tmin, Tmax] of the curve");
}
time_t const dt (t-t_min_);
time_t const dt (t-T_min_);
point_t h = coefficients_.col(degree_);
for(int i=(int)(degree_-1); i>=0; i--)
{
......@@ -161,11 +168,11 @@ struct polynomial : public curve_abc<Time, Numeric, Safe, Point>
/// \return \f$\frac{d^Nx(t)}{dt^N}\f$ point corresponding on derivative spline at time t.
virtual point_t derivate(const time_t t, const std::size_t order) const
{
if((t < t_min_ || t > t_max_) && Safe)
if((t < T_min_ || t > T_max_) && Safe)
{
throw std::invalid_argument("error in polynomial : time t to evaluate derivative should be in range [Tmin, Tmax] of the curve");
}
time_t const dt (t-t_min_);
time_t const dt (t-T_min_);
time_t cdt(1);
point_t currentPoint_ = point_t::Zero(dim_);
for(int i = (int)(order); i < (int)(degree_+1); ++i, cdt*=dt)
......@@ -192,10 +199,10 @@ struct polynomial : public curve_abc<Time, Numeric, Safe, Point>
public:
/// \brief Get the minimum time for which the curve is defined
/// \return \f$t_{min}\f$ lower bound of time range.
num_t virtual min() const {return t_min_;}
num_t virtual min() const {return T_min_;}
/// \brief Get the maximum time for which the curve is defined.
/// \return \f$t_{max}\f$ upper bound of time range.
num_t virtual max() const {return t_max_;}
num_t virtual max() const {return T_max_;}
/*Helpers*/
/*Attributes*/
......@@ -205,10 +212,11 @@ struct polynomial : public curve_abc<Time, Numeric, Safe, Point>
std::size_t degree_; //const
private:
time_t t_min_, t_max_;
time_t T_min_, T_max_;
/*Attributes*/
private:
template<typename In>
coeff_t init_coeffs(In zeroOrderCoefficient, In highestOrderCoefficient)
{
......@@ -220,6 +228,7 @@ struct polynomial : public curve_abc<Time, Numeric, Safe, Point>
}
return res;
}
}; //class polynomial
} // namespace curves
#endif //_STRUCT_POLYNOMIAL
......
......@@ -8,6 +8,8 @@
#include "curves/cubic_hermite_spline.h"
#include "curves/piecewise_curve.h"
#include "curves/serialize_test_class.h"
#include <string>
#include <iostream>
#include <cmath>
......@@ -1437,6 +1439,45 @@ void piecewiseCurveConversionFromDiscretePointsTest(bool& error)
ComparePoints(p3, ppc(T_max), errMsg, error);
}
void serializationPiecewisePolynomialCurveTest(bool& error)
{
std::string errmsg1("in serializationPiecewisePolynomialCurveTest, Error While serializing : ");
point_t a(1,1,1); // in [0,1[
point_t b(2,1,1); // in [1,2[
point_t c(3,1,1); // in [2,3]
point_t res;
t_point_t vec1, vec2, vec3;
vec1.push_back(a); // x=1, y=1, z=1
vec2.push_back(b); // x=2, y=1, z=1
vec3.push_back(c); // x=3, y=1, z=1
polynomial_t pol1(vec1.begin(), vec1.end(), 0, 1);
polynomial_t pol2(vec2.begin(), vec2.end(), 1, 2);
polynomial_t pol3(vec3.begin(), vec3.end(), 2, 3);
piecewise_polynomial_curve_t pc(pol1);
piecewise_polynomial_curve_t pc_test = pc;
pc.add_curve(pol2);
pc.add_curve(pol3);
// Test serialization
std::string fileName("./testSerialization/fileTest");
//piecewise_polynomial_curve_t::serializeToFile<piecewise_polynomial_curve_t, polynomial_t>(pc, fileName);
//pc.saveAsText(fileName);
// Test deserialization
/*
piecewise_polynomial_curve_t pc_deserialized = piecewise_polynomial_curve_t
::deserializeFromFile<piecewise_polynomial_curve_t, polynomial_t>(fileName);
*/
//pc_test.loadFromText(fileName);
//pol1.saveAsText(fileName);
serialize_test_class stc(10);
stc.serialize_to_file(fileName);
serialize_test_class stc2(0);
stc2.deserialize_from_file(fileName);
std::cout<<"Test ok : "<<stc2.a_<<std::endl;
}
int main(int /*argc*/, char** /*argv[]*/)
{
std::cout << "performing tests... \n";
......
Supports Markdown
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