DMP_BBO library
EigenBoostSerialization.tpp
Go to the documentation of this file.
1 
26 #include <iostream>
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>
31 
32 namespace Eigen {
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)
35 {
36  int rows = matrix.rows();
37  int cols = matrix.cols();
38  bool is_vector = (cols==1 || rows==1);
39 
40  bool is_diagonal = false;
41  if (rows==cols && rows>1)
42  {
43  // Make copy of matrix, set diagional to 0, see if all entries are 0
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())
47  is_diagonal = true;
48  }
49 
50  //ar & boost::serialization::make_nvp("rows", rows);
51  //ar & boost::serialization::make_nvp("cols", cols);
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++)
56  {
57  if (!is_vector && !is_diagonal && i>0) matrix_string += ";";
58  if (is_diagonal)
59  matrix_string += " "+std::to_string(matrix(i,i));
60  else
61  for(int j=0; j<cols; j++)
62  matrix_string += " "+std::to_string(matrix(i,j));
63  }
64  return matrix_string;
65 }
66 
67 }
68 
69 namespace boost { namespace serialization {
70 template<class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
71 inline void save(
72  Archive & ar,
73  const Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & matrix,
74  const unsigned int file_version
75 )
76 {
77  std::string matrix_string = toString(matrix);
78  ar & boost::serialization::make_nvp("m", matrix_string);
79 }
80 
81 template<class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
82 inline void load(
83  Archive & ar,
84  Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & matrix,
85  const unsigned int file_version
86 )
87 {
88  int rows, cols;
89  std::string data;
90  ar & boost::serialization::make_nvp("m", data);
91 
92  std::stringstream string_steam(data);
93  char c;
94  string_steam >> rows;
95  string_steam >> c;
96  bool is_diagonal = (c=='D');
97  string_steam >> cols;
98  string_steam >> c;
99  if (c!=';')
100  {
101  std::cerr << __FILE__ << ":" << __LINE__ << ":";
102  std::cerr << "WARNING: Unexpected character '" << c << "' when deserializing Eigen::Matrix." << std::endl;
103  matrix.resize(0,0);
104  return;
105  }
106 
107  bool is_vector = (cols==1 || rows==1);
108  matrix.resize(rows,cols);
109  if (is_diagonal)
110  matrix.fill(0);
111 
112  double val=0.0;
113  for (int i=0; i<rows; i++)
114  {
115  if (is_diagonal)
116  {
117  string_steam >> val;
118  matrix(i,i) = val;
119  }
120  else
121  {
122  for (int j=0; j<cols; j++)
123  {
124  string_steam >> val;
125  matrix(i,j) = val;
126  }
127  if (!is_vector)
128  {
129  string_steam >> c;
130  if (c!=';')
131  {
132  std::cerr << __FILE__ << ":" << __LINE__ << ":";
133  std::cerr << "WARNING: Unexpected character '" << c << "' when deserializing Eigen::Matrix." << std::endl;
134  matrix.resize(0,0);
135  return;
136  }
137  }
138  }
139  }
140 }
141 
142 }}
143 
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.