Eigen Matrix serialization using boost.serialization

I am trying to serialize an Eigen matrix. So that I can serialize a more complex object. I use Matrix as a base class and enable serialization in a derived class. I am confused about how to access Matrix.data (), which returns a c-style array (if I'm right). This is my attempt:

#include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> template < class TEigenMatrix> class VariableType : public TEigenMatrix { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & this.data(); } public: }; 

I would like to use it as a "wrapper":

 VariableType<Matrix<double,3,1>> serializableVector; 

instead

 Matrix<double,3,1> vector; 
+5
source share
2 answers

By putting the following free function into your compilation, you effectively make Boost.Serialization aware of how to serialize Eigen types:

 namespace boost { template<class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> inline void serialize( Archive & ar, Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & t, const unsigned int file_version ) { for(size_t i=0; i<t.size(); i++) ar & t.data()[i]; } } 

In the above example, you should be able (unchecked):

 void serialize(Archive & ar, const unsigned int version) { ar & *this; } 

Take a look at my previous answer on serializing Eigen types using Boost.Serialization for a more detailed example.

+8
source

Since the matrix in Eigen is dense, you can replace the for-loop in Jacob's make_array answer as:

ar and boost :: serialization :: make_array (t.data (), t.size ());

I made a more detailed answer in this post: fooobar.com/questions/952287 / ...

+9
source

Source: https://habr.com/ru/post/952288/


All Articles