27 #include <boost/archive/text_oarchive.hpp> 28 #include <boost/archive/text_iarchive.hpp> 29 #include <boost/archive/xml_oarchive.hpp> 30 #include <boost/archive/xml_iarchive.hpp> 33 template<
typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
int _MaxCols>
34 std::string
toString(
const Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & matrix)
36 int rows = matrix.rows();
37 int cols = matrix.cols();
38 bool is_vector = (cols==1 || rows==1);
40 bool is_diagonal =
false;
41 if (rows==cols && rows>1)
44 Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> matrix_only_diag = matrix;
45 for(
int i=0; i<rows; i++) matrix_only_diag(i,i) = 0;
46 if ((matrix_only_diag.array() == 0).all())
52 std::string matrix_string;
53 matrix_string += std::to_string(rows)+(is_diagonal?
"D":
"X");
54 matrix_string += std::to_string(cols)+
";";
55 for(
int i=0; i<rows; i++)
57 if (!is_vector && !is_diagonal && i>0) matrix_string +=
";";
59 matrix_string +=
" "+std::to_string(matrix(i,i));
61 for(
int j=0; j<cols; j++)
62 matrix_string +=
" "+std::to_string(matrix(i,j));
69 namespace boost {
namespace serialization {
70 template<
class Archive,
typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
int _MaxCols>
73 const Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & matrix,
74 const unsigned int file_version
77 std::string matrix_string = toString(matrix);
78 ar & boost::serialization::make_nvp(
"m", matrix_string);
81 template<
class Archive,
typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
int _MaxCols>
84 Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & matrix,
85 const unsigned int file_version
90 ar & boost::serialization::make_nvp(
"m", data);
92 std::stringstream string_steam(data);
96 bool is_diagonal = (c==
'D');
101 std::cerr << __FILE__ <<
":" << __LINE__ <<
":";
102 std::cerr <<
"WARNING: Unexpected character '" << c <<
"' when deserializing Eigen::Matrix." << std::endl;
107 bool is_vector = (cols==1 || rows==1);
108 matrix.resize(rows,cols);
113 for (
int i=0; i<rows; i++)
122 for (
int j=0; j<cols; j++)
132 std::cerr << __FILE__ <<
":" << __LINE__ <<
":";
133 std::cerr <<
"WARNING: Unexpected character '" << c <<
"' when deserializing Eigen::Matrix." << std::endl;
void save(Archive &ar, const Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &matrix, const unsigned int file_version)
Serialize an Eigen matrix to an archive.
void load(Archive &ar, Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &matrix, const unsigned int file_version)
Load ("deserialize") an Eigen matrix from an archive.
std::string toString(const Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &matrix)
Convert an Eigen matrix to a string.