Directory: | ./ |
---|---|
File: | include/coal/serialization/eigen.h |
Date: | 2025-04-01 09:23:31 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 31 | 31 | 100.0% |
Branches: | 21 | 40 | 52.5% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | // | ||
2 | // Copyright (c) 2017-2021 CNRS INRIA | ||
3 | // | ||
4 | |||
5 | /* | ||
6 | Code adapted from Pinocchio and | ||
7 | https://gist.githubusercontent.com/mtao/5798888/raw/5be9fa9b66336c166dba3a92c0e5b69ffdb81501/eigen_boost_serialization.hpp | ||
8 | Copyright (c) 2015 Michael Tao | ||
9 | */ | ||
10 | |||
11 | #ifndef COAL_SERIALIZATION_EIGEN_H | ||
12 | #define COAL_SERIALIZATION_EIGEN_H | ||
13 | |||
14 | #ifdef COAL_BACKWARD_COMPATIBILITY_WITH_HPP_FCL | ||
15 | #ifdef HPP_FCL_SKIP_EIGEN_BOOST_SERIALIZATION | ||
16 | #define COAL_SKIP_EIGEN_BOOST_SERIALIZATION | ||
17 | #endif | ||
18 | #endif | ||
19 | |||
20 | #ifndef COAL_SKIP_EIGEN_BOOST_SERIALIZATION | ||
21 | |||
22 | #include <Eigen/Dense> | ||
23 | |||
24 | #include <boost/serialization/split_free.hpp> | ||
25 | #include <boost/serialization/vector.hpp> | ||
26 | #include <boost/serialization/array.hpp> | ||
27 | |||
28 | // Workaround a bug in GCC >= 7 and C++17 | ||
29 | // ref. https://gitlab.com/libeigen/eigen/-/issues/1676 | ||
30 | #ifdef __GNUC__ | ||
31 | #if __GNUC__ >= 7 && __cplusplus >= 201703L | ||
32 | namespace boost { | ||
33 | namespace serialization { | ||
34 | struct U; | ||
35 | } | ||
36 | } // namespace boost | ||
37 | namespace Eigen { | ||
38 | namespace internal { | ||
39 | template <> | ||
40 | struct traits<boost::serialization::U> { | ||
41 | enum { Flags = 0 }; | ||
42 | }; | ||
43 | } // namespace internal | ||
44 | } // namespace Eigen | ||
45 | #endif | ||
46 | #endif | ||
47 | |||
48 | namespace boost { | ||
49 | namespace serialization { | ||
50 | |||
51 | template <class Archive, typename S, int Rows, int Cols, int Options, | ||
52 | int MaxRows, int MaxCols> | ||
53 | 3665030 | void save(Archive& ar, | |
54 | const Eigen::Matrix<S, Rows, Cols, Options, MaxRows, MaxCols>& m, | ||
55 | const unsigned int /*version*/) { | ||
56 | 3665030 | Eigen::DenseIndex rows(m.rows()), cols(m.cols()); | |
57 |
1/2✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
|
54 | if (Rows == Eigen::Dynamic) ar& BOOST_SERIALIZATION_NVP(rows); |
58 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | if (Cols == Eigen::Dynamic) ar& BOOST_SERIALIZATION_NVP(cols); |
59 |
5/9✓ Branch 2 taken 1123247 times.
✓ Branch 3 taken 709268 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1123247 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 709268 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1123247 times.
✗ Branch 10 not taken.
|
3665030 | ar& make_nvp("data", make_array(m.data(), (size_t)m.size())); |
60 | 3665030 | } | |
61 | |||
62 | template <class Archive, typename S, int Rows, int Cols, int Options, | ||
63 | int MaxRows, int MaxCols> | ||
64 | 3665030 | void load(Archive& ar, | |
65 | Eigen::Matrix<S, Rows, Cols, Options, MaxRows, MaxCols>& m, | ||
66 | const unsigned int /*version*/) { | ||
67 | 3665030 | Eigen::DenseIndex rows = Rows, cols = Cols; | |
68 |
1/2✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
|
54 | if (Rows == Eigen::Dynamic) ar >> BOOST_SERIALIZATION_NVP(rows); |
69 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | if (Cols == Eigen::Dynamic) ar >> BOOST_SERIALIZATION_NVP(cols); |
70 |
1/2✓ Branch 1 taken 1832515 times.
✗ Branch 2 not taken.
|
3665030 | m.resize(rows, cols); |
71 |
5/9✓ Branch 2 taken 27 times.
✓ Branch 3 taken 1832488 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 27 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1832488 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 27 times.
✗ Branch 10 not taken.
|
3665030 | ar >> make_nvp("data", make_array(m.data(), (size_t)m.size())); |
72 | 3665030 | } | |
73 | |||
74 | template <class Archive, typename S, int Rows, int Cols, int Options, | ||
75 | int MaxRows, int MaxCols> | ||
76 | 7330060 | void serialize(Archive& ar, | |
77 | Eigen::Matrix<S, Rows, Cols, Options, MaxRows, MaxCols>& m, | ||
78 | const unsigned int version) { | ||
79 | 7330060 | split_free(ar, m, version); | |
80 | 7330060 | } | |
81 | |||
82 | template <class Archive, typename PlainObjectBase, int MapOptions, | ||
83 | typename StrideType> | ||
84 | 30 | void save(Archive& ar, | |
85 | const Eigen::Map<PlainObjectBase, MapOptions, StrideType>& m, | ||
86 | const unsigned int /*version*/) { | ||
87 | 30 | Eigen::DenseIndex rows(m.rows()), cols(m.cols()); | |
88 | if (PlainObjectBase::RowsAtCompileTime == Eigen::Dynamic) | ||
89 | ar& BOOST_SERIALIZATION_NVP(rows); | ||
90 | if (PlainObjectBase::ColsAtCompileTime == Eigen::Dynamic) | ||
91 |
1/2✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
|
30 | ar& BOOST_SERIALIZATION_NVP(cols); |
92 |
2/4✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 15 times.
✗ Branch 8 not taken.
|
30 | ar& make_nvp("data", make_array(m.data(), (size_t)m.size())); |
93 | 30 | } | |
94 | |||
95 | template <class Archive, typename PlainObjectBase, int MapOptions, | ||
96 | typename StrideType> | ||
97 | 30 | void load(Archive& ar, Eigen::Map<PlainObjectBase, MapOptions, StrideType>& m, | |
98 | const unsigned int /*version*/) { | ||
99 | 30 | Eigen::DenseIndex rows = PlainObjectBase::RowsAtCompileTime, | |
100 | 30 | cols = PlainObjectBase::ColsAtCompileTime; | |
101 | if (PlainObjectBase::RowsAtCompileTime == Eigen::Dynamic) | ||
102 | ar >> BOOST_SERIALIZATION_NVP(rows); | ||
103 | if (PlainObjectBase::ColsAtCompileTime == Eigen::Dynamic) | ||
104 |
1/2✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
|
30 | ar >> BOOST_SERIALIZATION_NVP(cols); |
105 | 30 | m.resize(rows, cols); | |
106 |
2/4✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 15 times.
✗ Branch 8 not taken.
|
30 | ar >> make_nvp("data", make_array(m.data(), (size_t)m.size())); |
107 | 30 | } | |
108 | |||
109 | template <class Archive, typename PlainObjectBase, int MapOptions, | ||
110 | typename StrideType> | ||
111 | 60 | void serialize(Archive& ar, | |
112 | Eigen::Map<PlainObjectBase, MapOptions, StrideType>& m, | ||
113 | const unsigned int version) { | ||
114 | 60 | split_free(ar, m, version); | |
115 | 60 | } | |
116 | |||
117 | } // namespace serialization | ||
118 | } // namespace boost | ||
119 | // | ||
120 | #endif // ifned COAL_SKIP_EIGEN_BOOST_SERIALIZATION | ||
121 | |||
122 | #endif // ifndef COAL_SERIALIZATION_EIGEN_H | ||
123 |