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

Use serialization/archive functions for serialization / Delete test class for...

Use serialization/archive functions for serialization / Delete test class for serialization / Make test ok (not python test for serialization)
parent 0a81724b
......@@ -12,7 +12,6 @@ SET(${PROJECT_NAME}_HEADERS
linear_variable.h
cubic_hermite_spline.h
piecewise_curve.h
serialize_test_class.h
)
INSTALL(FILES
......
......@@ -28,7 +28,8 @@ template<typename Time= double, typename Numeric=Time, std::size_t Dim=3, bool S
typename T_Point= std::vector<Point,Eigen::aligned_allocator<Point> >,
typename Curve= curve_abc<Time, Numeric, Safe, Point>
>
struct piecewise_curve : public curve_abc<Time, Numeric, Safe, Point>
struct piecewise_curve : public curve_abc<Time, Numeric, Safe, Point>,
public serialization::Serializable< piecewise_curve<Time, Numeric, Dim, Safe, Point, T_Point, Curve> >
{
typedef Point point_t;
typedef T_Point t_point_t;
......@@ -236,50 +237,7 @@ 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.
......@@ -350,24 +308,6 @@ struct piecewise_curve : public curve_abc<Time, Numeric, Safe, Point>
ar & T_min_ & T_max_;
}
template<typename Piecewise_polynomial_curve, typename Polynomial>
static void serialize_to_file(std::string filename, Piecewise_polynomial_curve ppc)
{
std::ofstream ofile(filename.c_str());
boost::archive::text_oarchive oTextArchive(ofile);
oTextArchive << ppc; //piecewise_curve.convert_piecewise_curve_to_polynomial<Polynomial>();
}
template<typename Piecewise_polynomial_curve>
static Piecewise_polynomial_curve deserialize_from_file(std::string filename)
{
Piecewise_polynomial_curve ppc;
std::ifstream ifile(filename.c_str());
boost::archive::text_iarchive iTextArchive(ifile);
iTextArchive >> ppc; // désérialisation dans d
return ppc;
}
};
template<typename Time, typename Numeric, std::size_t Dim, bool Safe, typename Point, typename T_Point, typename Curve>
......
......@@ -42,7 +42,8 @@ namespace curves
///
template<typename Time= double, typename Numeric=Time, std::size_t Dim=3, bool Safe=false,
typename Point= Eigen::Matrix<Numeric, Eigen::Dynamic, 1>, typename T_Point =std::vector<Point,Eigen::aligned_allocator<Point> > >
struct polynomial : public curve_abc<Time, Numeric, Safe, Point>
struct polynomial : public curve_abc<Time, Numeric, Safe, Point>,
public serialization::Serializable< polynomial<Time, Numeric, Dim, Safe, Point, T_Point> >
{
typedef Point point_t;
typedef T_Point t_point_t;
......@@ -244,32 +245,15 @@ struct polynomial : public curve_abc<Time, Numeric, Safe, Point>
template<class Archive>
void serialize(Archive& ar, const unsigned int version){
ar & coefficients_;
ar & dim_;
ar & degree_;
ar & T_min_ ;
ar & T_max_;
}
template<typename Polynomial>
static void serialize_to_file(std::string filename, Polynomial pol)
{
std::ofstream ofile(filename.c_str());
boost::archive::text_oarchive oTextArchive(ofile);
oTextArchive << pol;
}
template<typename Polynomial>
static Polynomial deserialize_from_file(std::string filename)
{
Polynomial pol;
std::ifstream ifile(filename.c_str());
boost::archive::text_iarchive iTextArchive(ifile);
iTextArchive >> pol; // désérialisation dans d
return pol;
ar & boost::serialization::make_nvp("coefficients", coefficients_);
ar & boost::serialization::make_nvp("dim", dim_);
ar & boost::serialization::make_nvp("degree", degree_);
ar & boost::serialization::make_nvp("T_min", T_min_);
ar & boost::serialization::make_nvp("T_max", T_max_);
}
}; //class polynomial
} // namespace curves
#endif //_STRUCT_POLYNOMIAL
/**
* \file polynomial.h
* \brief Definition of a cubic spline.
* \author Steve T.
* \version 0.1
* \date 06/17/2013
*
* This file contains definitions for the polynomial struct.
* It allows the creation and evaluation of natural
* smooth splines of arbitrary dimension and order
*/
#ifndef _STRUCT_TEST_SER
#define _STRUCT_TEST_SER
#include "MathDefs.h"
#include "curve_abc.h"
#include <iostream>
#include <algorithm>
#include <functional>
#include <stdexcept>
#include <fstream>
#include <string>
#include <stdexcept>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include "serialization/archive.hpp"
#include "serialization/eigen-matrix.hpp"
namespace curves
{
template<typename Time, std::size_t Dim=3>
struct TestStructParent {
TestStructParent(){}
virtual ~TestStructParent(){}
};
template<typename Time= double, typename Numeric=Time, std::size_t Dim=3, bool Safe=false,
typename Point= Eigen::Matrix<Numeric, Eigen::Dynamic, 1>, typename T_Point =std::vector<Point,Eigen::aligned_allocator<Point> > >
struct TestStruct : public TestStructParent<Time, Dim> {//: public curve_abc<Time, Numeric, Safe, Point>{
public:
TestStruct(){}
TestStruct(int i, std::string c, float f1, float f2, Eigen::Matrix<double, Eigen::Dynamic, 1> mat){
a=i;
s=c;
v.push_back(f1);
v.push_back(f2);
matrix_test = mat;
}
~TestStruct()
{
// NOTHING
}
Point operator()(const time_t t) const
{
return Point(0);
}
/// \brief Evaluation of the derivative of order N of spline at time t.
/// \param t : the time when to evaluate the spline.
/// \param order : order of derivative.
/// \return \f$\frac{d^Nx(t)}{dt^N}\f$ point corresponding on derivative spline at time t.
Point derivate(const time_t t, const std::size_t order) const
{
return Point(0);
}
/// \brief Get the minimum time for which the curve is defined
/// \return \f$t_{min}\f$ lower bound of time range.
Numeric min() const {return 0;}
/// \brief Get the maximum time for which the curve is defined.
/// \return \f$t_{max}\f$ upper bound of time range.
Numeric max() const {return 0;}
template<class Archive>
void serialize(Archive& ar, const unsigned int version){
ar & a & s & v & matrix_test;
}
int a;
std::string s;
std::vector<float> v;
Eigen::Matrix<double, Eigen::Dynamic, 1> matrix_test;
};
struct serialize_test_class
{
public:
typedef Eigen::Matrix<double,1,1> point_one;
typedef TestStruct<double, double, 1, true, point_one> test_t;
test_t t1, t2;
serialize_test_class()
{
Eigen::Matrix<double, 3, 1> mat1(1,2,3);
Eigen::Matrix<double, 3, 1> mat2(4,4,4);
t1 = test_t(2, std::string("essai 1"), 2.0, 3.5, mat1);
t2 = test_t(0, std::string("essai 2"), 0.0, 0.5, mat2);
}
void serialize_to_file(std::string path)
{
std::cout<<"serialize"<<std::endl;
std::ofstream ofile(path.c_str());
boost::archive::text_oarchive oTextArchive(ofile);
oTextArchive << t1; // serialization
}
void deserialize_from_file(std::string path)
{
std::cout<<"deserialize"<<std::endl;
std::ifstream ifile(path.c_str());
boost::archive::text_iarchive iTextArchive(ifile);
iTextArchive >> t2; // deserialization
}
}; //class polynomial
} // namespace curves
#endif //_STRUCT_POLYNOMIAL
......@@ -9,6 +9,7 @@
#include "python_definitions.h"
#include "python_variables.h"
#include "archive_python_binding.h"
#include <vector>
......@@ -177,27 +178,6 @@ piecewise_cubic_hermite_curve_t* wrapPiecewiseCubicHermiteCurveConstructor(const
}
/* end wrap piecewise polynomial curve */
/* Wrap serialize and deserialize functions in Polynomial and Piecewise Polynomial curve */
/*
void wrapSerializePolynomial(std::string file_path, polynomial_t pol)
{
polynomial_t::serialize_to_file<polynomial_t>(file_path, pol);
}
polynomial_t wrapDeserializePolynomial(std::string file_path)
{
return polynomial_t::deserialize_from_file<polynomial_t>(file_path);
}
void wrapSerializePiecewisePolynomialCurve(std::string file_path, piecewise_polynomial_curve_t ppc)
{
piecewise_polynomial_curve_t::serialize_to_file<piecewise_polynomial_curve_t, polynomial_t>(file_path, ppc);
}
piecewise_polynomial_curve_t wrapDeserializePiecewisePolynomialCurve(std::string file_path)
{
return piecewise_polynomial_curve_t::deserialize_from_file<piecewise_polynomial_curve_t>(file_path);
}
*/
/*End wrap serialize and deserialize functions in Polynomial and Piecewise Polynomial curve */
/* Wrap exact cubic spline */
t_waypoint_t getWayPoints(const coeff_t& array, const time_waypoints_t& time_wp)
{
......@@ -371,6 +351,7 @@ BOOST_PYTHON_MODULE(curves)
.def("max", &polynomial_t::max)
.def("__call__", &polynomial_t::operator())
.def("derivate", &polynomial_t::derivate)
//.def(SerializableVisitor<polynomial_t>())
;
/** END polynomial function**/
......@@ -471,15 +452,6 @@ BOOST_PYTHON_MODULE(curves)
def("hermite_from_polynomial", hermite_from_curve<cubic_hermite_spline_t, polynomial_t>);
/** END curves conversion**/
/** BEGIN serialization of polynomial and piecewise polynomial curve **/
/*
def("serialize_polynomial", wrapSerializePolynomial);
def("deserialize_polynomial", wrapDeserializePolynomial);
def("serialize_piecewise_polynomial_curve", wrapSerializePiecewisePolynomialCurve);
def("deserialize_piecewise_polynomial_curve", wrapDeserializePiecewisePolynomialCurve);
*/
/** END serialization of polynomial and piecewise polynomial curve **/
}
} // namespace curves
......@@ -94,8 +94,10 @@ class TestCurves(unittest.TestCase):
self.assertTrue((a.derivate(0.4, 0) == a(0.4)).all())
a.derivate(0.4, 2)
# Test serialization
#serialize_polynomial("serialize_polynomial.test",a);
#b = deserialize_polynomial("serialize_polynomial.test");
#a.saveAsText("serialiation_polynomial.test")
#waypoints = matrix([[0,0,0,], [0,0,0,]]).transpose()
#b = polynomial(waypoints)
#b.loadFromText("serialiation_polynomial.test")
#self.assertTrue((a(0.4) == b(0.4)).all())
return
......
......@@ -8,8 +8,6 @@
#include "curves/cubic_hermite_spline.h"
#include "curves/piecewise_curve.h"
#include "curves/serialize_test_class.h"
#include <string>
#include <iostream>
#include <cmath>
......@@ -1462,18 +1460,19 @@ void serializationPiecewisePolynomialCurveTest(bool& error)
// Test serialization on Polynomial
std::cout<<"Serialize test : Polynomial => ";
polynomial_t::serialize_to_file<polynomial_t>(fileName, pol1);
polynomial_t pol_test = pol2;
pol_test = polynomial_t::deserialize_from_file<polynomial_t>(fileName);
pol1.saveAsText(fileName);
// Test deserialization
polynomial_t pol_test;
pol_test.loadFromText(fileName);
CompareCurves<polynomial_t, polynomial_t>(pol1, pol_test, errMsg1, error);
std::cout<<"OK"<<std::endl;
// Test serialization on Piecewise curves
std::cout<<"Serialize test : Piecewise polynomial curve => ";
piecewise_polynomial_curve_t::serialize_to_file<piecewise_polynomial_curve_t, polynomial_t>(fileName, pc);
pc.saveAsText(fileName);
// Test deserialization
piecewise_polynomial_curve_t ppc_deserialized = piecewise_polynomial_curve_t
::deserialize_from_file<piecewise_polynomial_curve_t>(fileName);
piecewise_polynomial_curve_t ppc_deserialized;
ppc_deserialized.loadFromText(fileName);
CompareCurves<piecewise_polynomial_curve_t,piecewise_polynomial_curve_t>(pc, ppc_deserialized, errMsg2, error);
std::cout<<"OK"<<std::endl;
}
......
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