-
Notifications
You must be signed in to change notification settings - Fork 8
/
serialize.hh
105 lines (94 loc) · 3.69 KB
/
serialize.hh
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
#ifndef ROBOPTIM_SHARED_TESTS_SERIALIZE_HH
# define ROBOPTIM_SHARED_TESTS_SERIALIZE_HH
# include <Eigen/Sparse>
# include <Eigen/Dense>
# include <boost/serialization/split_free.hpp>
# include <boost/serialization/vector.hpp>
// Obtained from: https://gist.github.com/mtao/5798888
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*/)
{
int rows = static_cast<int> (m.rows ()), cols = static_cast<int> (m.cols ());
ar & rows;
ar & cols;
ar & boost::serialization::make_array (m.data (), static_cast<std::size_t> (rows * cols));
}
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*/)
{
int rows,cols;
ar & rows;
ar & cols;
m.resize (rows, cols);
ar & boost::serialization::make_array (m.data (), static_cast<std::size_t> (rows * cols));
}
template <class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
void serialize (Archive & ar, Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> & m, const unsigned int version)
{
split_free (ar,m,version);
}
template <class Archive, typename _Scalar>
void save (Archive & ar, const Eigen::Triplet<_Scalar> & m, const unsigned int /*version*/)
{
ar & m.row ();
ar & m.col ();
ar & m.value ();
}
template <class Archive, typename _Scalar>
void load (Archive & ar, Eigen::Triplet<_Scalar> & m, const unsigned int /*version*/)
{
int row,col;
_Scalar value;
ar & row;
ar & col;
ar & value;
m = Eigen::Triplet<_Scalar> (row, col, value);
}
template <class Archive, typename _Scalar>
void serialize (Archive & ar, Eigen::Triplet<_Scalar> & m, const unsigned int version)
{
split_free (ar,m,version);
}
template <class Archive, typename _Scalar, int _Options,typename _Index>
void save (Archive & ar, const Eigen::SparseMatrix<_Scalar,_Options,_Index> & m, const unsigned int /*version*/)
{
int innerSize = m.innerSize ();
int outerSize = m.outerSize ();
typedef typename Eigen::Triplet<_Scalar> Triplet;
std::vector<Triplet> triplets;
for (int i = 0; i < outerSize; ++i)
{
for (typename Eigen::SparseMatrix<_Scalar,_Options,_Index>::InnerIterator it (m,i); it; ++it)
{
triplets.push_back (Triplet (it.row (), it.col (), it.value ()));
}
}
ar & innerSize;
ar & outerSize;
ar & triplets;
}
template <class Archive, typename _Scalar, int _Options, typename _Index>
void load (Archive & ar, Eigen::SparseMatrix<_Scalar,_Options,_Index> & m, const unsigned int /*version*/)
{
int innerSize;
int outerSize;
ar & innerSize;
ar & outerSize;
int rows = m.IsRowMajor?outerSize:innerSize;
int cols = m.IsRowMajor?innerSize:outerSize;
m.resize (rows,cols);
typedef typename Eigen::Triplet<_Scalar> Triplet;
std::vector<Triplet> triplets;
ar & triplets;
m.setFromTriplets (triplets.begin (), triplets.end ());
}
template <class Archive, typename _Scalar, int _Options, typename _Index>
void serialize (Archive & ar, Eigen::SparseMatrix<_Scalar,_Options,_Index> & m, const unsigned int version)
{
split_free (ar,m,version);
}
}
}
#endif //! ROBOPTIM_SHARED_TESTS_SERIALIZE_HH