-
Notifications
You must be signed in to change notification settings - Fork 91
/
EigenBoostSerialization.tpp
142 lines (130 loc) · 4.32 KB
/
EigenBoostSerialization.tpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* @file EigenBoostSerialization.tpp
* @brief Source file for serialization of Eigen matrices.
* @author Freek Stulp
*
* Implementations are in the tpp file
*
* This file is part of DmpBbo, a set of libraries and programs for the
* black-box optimization of dynamical movement primitives.
* Copyright (C) 2014 Freek Stulp, ENSTA-ParisTech
*
* DmpBbo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* DmpBbo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with DmpBbo. If not, see <http://www.gnu.org/licenses/>.
*/
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
namespace Eigen {
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
std::string toString(const Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & matrix)
{
int rows = matrix.rows();
int cols = matrix.cols();
bool is_vector = (cols==1 || rows==1);
bool is_diagonal = false;
if (rows==cols && rows>1)
{
// Make copy of matrix, set diagional to 0, see if all entries are 0
Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> matrix_only_diag = matrix;
for(int i=0; i<rows; i++) matrix_only_diag(i,i) = 0;
if ((matrix_only_diag.array() == 0).all())
is_diagonal = true;
}
//ar & boost::serialization::make_nvp("rows", rows);
//ar & boost::serialization::make_nvp("cols", cols);
std::string matrix_string;
matrix_string += std::to_string(rows)+(is_diagonal?"D":"X");
matrix_string += std::to_string(cols)+";";
for(int i=0; i<rows; i++)
{
if (!is_vector && !is_diagonal && i>0) matrix_string += ";";
if (is_diagonal)
matrix_string += " "+std::to_string(matrix(i,i));
else
for(int j=0; j<cols; j++)
matrix_string += " "+std::to_string(matrix(i,j));
}
return matrix_string;
}
}
namespace boost { namespace serialization {
template<class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
inline void save(
Archive & ar,
const Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & matrix,
const unsigned int file_version
)
{
std::string matrix_string = toString(matrix);
ar & boost::serialization::make_nvp("m", matrix_string);
}
template<class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
inline void load(
Archive & ar,
Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & matrix,
const unsigned int file_version
)
{
int rows, cols;
std::string data;
ar & boost::serialization::make_nvp("m", data);
std::stringstream string_steam(data);
char c;
string_steam >> rows;
string_steam >> c;
bool is_diagonal = (c=='D');
string_steam >> cols;
string_steam >> c;
if (c!=';')
{
std::cerr << __FILE__ << ":" << __LINE__ << ":";
std::cerr << "WARNING: Unexpected character '" << c << "' when deserializing Eigen::Matrix." << std::endl;
matrix.resize(0,0);
return;
}
bool is_vector = (cols==1 || rows==1);
matrix.resize(rows,cols);
if (is_diagonal)
matrix.fill(0);
double val=0.0;
for (int i=0; i<rows; i++)
{
if (is_diagonal)
{
string_steam >> val;
matrix(i,i) = val;
}
else
{
for (int j=0; j<cols; j++)
{
string_steam >> val;
matrix(i,j) = val;
}
if (!is_vector)
{
string_steam >> c;
if (c!=';')
{
std::cerr << __FILE__ << ":" << __LINE__ << ":";
std::cerr << "WARNING: Unexpected character '" << c << "' when deserializing Eigen::Matrix." << std::endl;
matrix.resize(0,0);
return;
}
}
}
}
}
}}