diff --git a/cmake b/cmake index 441552634e4c427956be7b2834a6bbf894b24f0c..cea261e3da7d383844530070356bca76d20197a8 160000 --- a/cmake +++ b/cmake @@ -1 +1 @@ -Subproject commit 441552634e4c427956be7b2834a6bbf894b24f0c +Subproject commit cea261e3da7d383844530070356bca76d20197a8 diff --git a/include/curves/piecewise_curve.h b/include/curves/piecewise_curve.h index 582a596245bbcf6ea5aeda90c1405b411ead7fa0..f45b68b9bbff524e5ebb79a44a2db301ae0f23e6 100644 --- a/include/curves/piecewise_curve.h +++ b/include/curves/piecewise_curve.h @@ -337,6 +337,37 @@ struct piecewise_curve : public curve_abc<Time, Numeric, Safe, Point> std::size_t size_; // Number of segments in piecewise curve = size of curves_ Time T_min_, T_max_; static const double MARGIN; + + public: + // Serialization of the class + friend class boost::serialization::access; + + template<class Archive> + void serialize(Archive& ar, const unsigned int version){ + ar & curves_; + ar & time_curves_; + ar & size_; + 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> diff --git a/include/curves/polynomial.h b/include/curves/polynomial.h index 2cf1bbd66de18b2e9f5eb902c66749dbb8c4302a..7d60ec0727ffc7d2466f6d075a61a42566f27837 100644 --- a/include/curves/polynomial.h +++ b/include/curves/polynomial.h @@ -23,9 +23,14 @@ #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" -#include <boost/serialization/vector.hpp> namespace curves { @@ -60,7 +65,9 @@ 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(); } @@ -74,7 +81,8 @@ 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(); } @@ -87,8 +95,9 @@ struct polynomial : public curve_abc<Time, Numeric, Safe, Point> /// \param max : UPPER bound on interval definition of the spline. 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) + : coefficients_(init_coeffs(zeroOrderCoefficient, out)), + dim_(Dim), degree_(coefficients_.cols()-1), + T_min_(min), T_max_(max) { safe_check(); } @@ -103,7 +112,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_) - {} + {} //polynomial& operator=(const polynomial& other); @@ -229,6 +238,37 @@ struct polynomial : public curve_abc<Time, Numeric, Safe, Point> return res; } + public: + // Serialization of the class + friend class boost::serialization::access; + + 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; + } + }; //class polynomial } // namespace curves #endif //_STRUCT_POLYNOMIAL diff --git a/include/curves/serialization/CMakeLists.txt b/include/curves/serialization/CMakeLists.txt index 5961ad6d802763da650e61ee75540c7e60f70ef5..63009f494e7c15e14d39c141ba54b3a0232e1ee0 100644 --- a/include/curves/serialization/CMakeLists.txt +++ b/include/curves/serialization/CMakeLists.txt @@ -1,4 +1,5 @@ SET(${PROJECT_NAME}_SERIALIZATION_HEADERS + aligned-vector.hpp archive.hpp eigen-matrix.hpp fwd.hpp diff --git a/include/curves/serialization/eigen-matrix.hpp b/include/curves/serialization/eigen-matrix.hpp index ca84a5fde37685ad55d03f44b3145af70f3b3111..e9422a44bb6b13bc6b3b3f5ca7a71bd9963a7f6b 100644 --- a/include/curves/serialization/eigen-matrix.hpp +++ b/include/curves/serialization/eigen-matrix.hpp @@ -35,9 +35,8 @@ namespace boost{ namespace serialization{ - template <class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> - void save(Archive & ar, const Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> & m, const unsigned int /*version*/) + void save(Archive & ar, const Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> & m, const unsigned int) // version { Eigen::DenseIndex rows(m.rows()), cols(m.cols()); ar & BOOST_SERIALIZATION_NVP(rows); @@ -46,7 +45,7 @@ namespace boost{ } template <class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> - void load(Archive & ar, Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> & m, const unsigned int /*version*/) + void load(Archive & ar, Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> & m, const unsigned int) // version { Eigen::DenseIndex rows,cols; ar >> BOOST_SERIALIZATION_NVP(rows); @@ -61,7 +60,6 @@ namespace boost{ { split_free(ar,m,version); } - } } diff --git a/include/curves/serialize_test_class.h b/include/curves/serialize_test_class.h index 35cbb78f54930d5a5a2354075bde8a3e399a9ca4..724e0f68f2bf5908e9e72016a46c5a85f556fc24 100644 --- a/include/curves/serialize_test_class.h +++ b/include/curves/serialize_test_class.h @@ -34,35 +34,91 @@ namespace curves { -struct serialize_test_class : public serialization::Serializable< serialize_test_class > + +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(int a) :a_(a) {} - - int a_; + 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 << (*this); // sérialisation de d + 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 >> (*this); // désérialisation dans d - } - - // Serialization of the class - friend class boost::serialization::access; - - template<class Archive> - void serialize(Archive & ar, const unsigned int version) - { - ar & BOOST_SERIALIZATION_NVP(a_); + iTextArchive >> t2; // deserialization } }; //class polynomial diff --git a/python/curves_python.cpp b/python/curves_python.cpp index 74928d787569cdc5adbc8b8502da4961bb1dc3b6..5f3c3ce4fd1c41180916d50492813dc8534ab097 100644 --- a/python/curves_python.cpp +++ b/python/curves_python.cpp @@ -177,6 +177,27 @@ 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) { @@ -450,6 +471,14 @@ 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 **/ } diff --git a/python/test/test.py b/python/test/test.py index a8d23feda748b85cc3646daf6ac4558e14c5d08d..4718481772b3d26f358b626d3d06dd9206efdc41 100644 --- a/python/test/test.py +++ b/python/test/test.py @@ -3,11 +3,14 @@ import unittest from numpy import matrix from numpy.linalg import norm +#from curves import ( serialize_polynomial, deserialize_polynomial, serialize_piecewise_polynomial_curve, deserialize_piecewise_polynomial_curve ) + from curves import (bezier3, bezier6, bezier_from_hermite, bezier_from_polynomial, cubic_hermite_spline, curve_constraints, exact_cubic, hermite_from_bezier, hermite_from_polynomial, piecewise_bezier3_curve, piecewise_bezier6_curve, piecewise_cubic_hermite_curve, piecewise_polynomial_curve, polynomial, polynomial_from_bezier, polynomial_from_hermite) +#import curves class TestCurves(unittest.TestCase): #def print_str(self, inStr): @@ -81,7 +84,7 @@ class TestCurves(unittest.TestCase): def test_polynomial(self): # To test : - # - Functions : constructor, min, max, derivate + # - Functions : constructor, min, max, derivate, serialize, deserialize waypoints = matrix([[1., 2., 3.], [4., 5., 6.]]).transpose() a = polynomial(waypoints) # Defined on [0.,1.] a = polynomial(waypoints, -1., 3.) # Defined on [-1.,3.] @@ -90,11 +93,15 @@ class TestCurves(unittest.TestCase): a(0.4) 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"); + #self.assertTrue((a(0.4) == b(0.4)).all()) return def test_piecewise_polynomial_curve(self): # To test : - # - Functions : constructor, min, max, derivate, add_curve, is_continuous + # - Functions : constructor, min, max, derivate, add_curve, is_continuous, serialize, deserialize waypoints1 = matrix([[1., 1., 1.]]).transpose() waypoints2 = matrix([[1., 1., 1.], [1., 1., 1.]]).transpose() a = polynomial(waypoints1, 0., 1.) @@ -108,6 +115,10 @@ class TestCurves(unittest.TestCase): ppc.derivate(0.4, 2) ppc.is_continuous(0) ppc.is_continuous(1) + # Test serialization + #serialize_piecewise_polynomial_curve("serialize_ppc.test",ppc); + #ppc2 = deserialize_piecewise_polynomial_curve("serialize_ppc.test"); + #self.assertTrue((ppc(0.4) == ppc2(0.4)).all()) return def test_piecewise_bezier3_curve(self): diff --git a/tests/Main.cpp b/tests/Main.cpp index d85d9ab4097575bff4dda18b0d534612dc176eae..800dfb53cec8ef8f731212be8807d443af517e14 100644 --- a/tests/Main.cpp +++ b/tests/Main.cpp @@ -1441,7 +1441,8 @@ void piecewiseCurveConversionFromDiscretePointsTest(bool& error) void serializationPiecewisePolynomialCurveTest(bool& error) { - std::string errmsg1("in serializationPiecewisePolynomialCurveTest, Error While serializing : "); + std::string errMsg1("in serializationPiecewisePolynomialCurveTest, Error While serializing Polynomials : "); + std::string errMsg2("in serializationPiecewisePolynomialCurveTest, Error While serializing Piecewise curves : "); 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] @@ -1457,32 +1458,30 @@ void serializationPiecewisePolynomialCurveTest(bool& error) 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); + std::string fileName("fileTest.test"); + + // 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); + 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); // 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; + piecewise_polynomial_curve_t ppc_deserialized = piecewise_polynomial_curve_t + ::deserialize_from_file<piecewise_polynomial_curve_t>(fileName); + CompareCurves<piecewise_polynomial_curve_t,piecewise_polynomial_curve_t>(pc, ppc_deserialized, errMsg2, error); + std::cout<<"OK"<<std::endl; } int main(int /*argc*/, char** /*argv[]*/) { std::cout << "performing tests... \n"; bool error = false; - CubicFunctionTest(error); ExactCubicNoErrorTest(error); ExactCubicPointsCrossedTest(error); // checks that given wayPoints are crossed @@ -1507,6 +1506,7 @@ int main(int /*argc*/, char** /*argv[]*/) toPolynomialConversionTest(error); cubicConversionTest(error); curveAbcDimDynamicTest(error); + serializationPiecewisePolynomialCurveTest(error); if(error) {