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
|
#include <cereal/cereal.hpp>
#include <cereal/archives/binary.hpp>
#include <eigen/Eigen/Dense>
#include <fstream>
using Eigen::Dynamic;
using Eigen::RowMajor;
typedef Eigen::Matrix<double, Dynamic, Dynamic> MatrixD;
typedef Eigen::Matrix<int, Dynamic, Dynamic> MatrixI;
typedef Eigen::Matrix<double, 1, Dynamic> VectorD;
typedef Eigen::Matrix<int, 1, Dynamic> VectorI;
using Eigen::MatrixBase;
using namespace Eigen;
namespace cereal
{
template <class Archive, class _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> inline
typename std::enable_if<traits::is_output_serializable<BinaryData<_Scalar>, Archive>::value, void>::type
save(Archive & ar, Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> const & m)
{
int32_t rows = m.rows();
int32_t cols = m.cols();
ar(rows);
ar(cols);
ar(binary_data(m.data(), rows * cols * sizeof(_Scalar)));
}
template <class Archive, class _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> inline
typename std::enable_if<traits::is_input_serializable<BinaryData<_Scalar>, Archive>::value, void>::type
load(Archive & ar, Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & m)
{
int32_t rows;
int32_t cols;
ar(rows);
ar(cols);
m.resize(rows, cols);
ar(binary_data(m.data(), static_cast<std::size_t>(rows * cols * sizeof(_Scalar))));
}
}
int main() {
// Eigen::MatrixXd test = Eigen::MatrixXd::Random(10, 3);
MatrixD test = MatrixD::Random(5, 5);
{
std::ofstream out("eigen.cereal", std::ios::binary);
cereal::BinaryOutputArchive archive_o(out);
archive_o(test);
}
std::cout << "test:" << std::endl << test << std::endl;
MatrixD test_loaded;
{
std::ifstream in("eigen.cereal", std::ios::binary);
cereal::BinaryInputArchive archive_i(in);
archive_i(test_loaded);
}
std::cout << "test loaded:" << std::endl << test_loaded << std::endl;
}
|