GCC Code Coverage Report


Directory: ./
File: include/pinocchio/serialization/eigen.hpp
Date: 2024-08-27 18:20:05
Exec Total Coverage
Lines: 0 42 0.0%
Branches: 0 56 0.0%

Line Branch Exec Source
1 //
2 // Copyright (c) 2017-2020 CNRS INRIA
3 //
4
5 #ifndef __pinocchio_serialization_eigen_matrix_hpp__
6 #define __pinocchio_serialization_eigen_matrix_hpp__
7
8 #include <Eigen/Dense>
9 #include "pinocchio/math/tensor.hpp"
10
11 #include <boost/serialization/split_free.hpp>
12 #include <boost/serialization/vector.hpp>
13 #include <boost/serialization/array.hpp>
14
15 // If hpp-fcl < 3.0.0 The GCC Eigen/Boost.Serialization workaround
16 // is already defined.
17 // If we don't link against hpp-fcl or hpp-fcl >= 3.0.0 then we must define
18 // the workaround.
19 #if defined PINOCCHIO_WITH_HPP_FCL
20 #include <hpp/fcl/config.hh>
21 #if !HPP_FCL_VERSION_AT_LEAST(3, 0, 0) // hpp-fcl < 3.0.0
22 #define HPP_FCL_SKIP_EIGEN_BOOST_SERIALIZATION
23 #include <hpp/fcl/serialization/eigen.h>
24 #else // hpp-fcl >= 3.0.0
25 // Workaround a bug in GCC >= 7 and C++17.
26 // ref. https://gitlab.com/libeigen/eigen/-/issues/1676
27 #ifdef __GNUC__
28 #if __GNUC__ >= 7 && __cplusplus >= 201703L
29 namespace boost
30 {
31 namespace serialization
32 {
33 struct U;
34 }
35 } // namespace boost
36 namespace Eigen
37 {
38 namespace internal
39 {
40 template<>
41 struct traits<boost::serialization::U>
42 {
43 enum
44 {
45 Flags = 0
46 };
47 };
48 } // namespace internal
49 } // namespace Eigen
50 #endif
51 #endif
52 #endif
53 #else // !PINOCCHIO_WITH_HPP_FCL
54 // Workaround a bug in GCC >= 7 and C++17.
55 // ref. https://gitlab.com/libeigen/eigen/-/issues/1676
56 #ifdef __GNUC__
57 #if __GNUC__ >= 7 && __cplusplus >= 201703L
58 namespace boost
59 {
60 namespace serialization
61 {
62 struct U;
63 }
64 } // namespace boost
65 namespace Eigen
66 {
67 namespace internal
68 {
69 template<>
70 struct traits<boost::serialization::U>
71 {
72 enum
73 {
74 Flags = 0
75 };
76 };
77 } // namespace internal
78 } // namespace Eigen
79 #endif
80 #endif
81 #endif
82
83 namespace boost
84 {
85 namespace serialization
86 {
87
88 template<
89 class Archive,
90 typename Scalar,
91 int Rows,
92 int Cols,
93 int Options,
94 int MaxRows,
95 int MaxCols>
96 void save(
97 Archive & ar,
98 const Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> & m,
99 const unsigned int /*version*/)
100 {
101 Eigen::DenseIndex rows(m.rows()), cols(m.cols());
102 if (Rows == Eigen::Dynamic)
103 ar & BOOST_SERIALIZATION_NVP(rows);
104 if (Cols == Eigen::Dynamic)
105 ar & BOOST_SERIALIZATION_NVP(cols);
106 ar & make_nvp("data", make_array(m.data(), (size_t)m.size()));
107 }
108
109 template<
110 class Archive,
111 typename Scalar,
112 int Rows,
113 int Cols,
114 int Options,
115 int MaxRows,
116 int MaxCols>
117 void load(
118 Archive & ar,
119 Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> & m,
120 const unsigned int /*version*/)
121 {
122 Eigen::DenseIndex rows = Rows, cols = Cols;
123 if (Rows == Eigen::Dynamic)
124 ar >> BOOST_SERIALIZATION_NVP(rows);
125 if (Cols == Eigen::Dynamic)
126 ar >> BOOST_SERIALIZATION_NVP(cols);
127 m.resize(rows, cols);
128 ar >> make_nvp("data", make_array(m.data(), (size_t)m.size()));
129 }
130
131 template<
132 class Archive,
133 typename Scalar,
134 int Rows,
135 int Cols,
136 int Options,
137 int MaxRows,
138 int MaxCols>
139 void serialize(
140 Archive & ar,
141 Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> & m,
142 const unsigned int version)
143 {
144 split_free(ar, m, version);
145 }
146
147 template<class Archive, typename PlainObjectBase, int MapOptions, typename StrideType>
148 void save(
149 Archive & ar,
150 const Eigen::Map<PlainObjectBase, MapOptions, StrideType> & m,
151 const unsigned int /*version*/)
152 {
153 Eigen::DenseIndex rows(m.rows()), cols(m.cols());
154 if (PlainObjectBase::RowsAtCompileTime == Eigen::Dynamic)
155 ar & BOOST_SERIALIZATION_NVP(rows);
156 if (PlainObjectBase::ColsAtCompileTime == Eigen::Dynamic)
157 ar & BOOST_SERIALIZATION_NVP(cols);
158 ar & make_nvp("data", make_array(m.data(), (size_t)m.size()));
159 }
160
161 template<class Archive, typename PlainObjectBase, int MapOptions, typename StrideType>
162 void load(
163 Archive & ar,
164 Eigen::Map<PlainObjectBase, MapOptions, StrideType> & m,
165 const unsigned int /*version*/)
166 {
167 Eigen::DenseIndex rows = PlainObjectBase::RowsAtCompileTime,
168 cols = PlainObjectBase::ColsAtCompileTime;
169 if (PlainObjectBase::RowsAtCompileTime == Eigen::Dynamic)
170 ar >> BOOST_SERIALIZATION_NVP(rows);
171 if (PlainObjectBase::ColsAtCompileTime == Eigen::Dynamic)
172 ar >> BOOST_SERIALIZATION_NVP(cols);
173 m.resize(rows, cols);
174 ar >> make_nvp("data", make_array(m.data(), (size_t)m.size()));
175 }
176
177 template<class Archive, typename PlainObjectBase, int MapOptions, typename StrideType>
178 void serialize(
179 Archive & ar,
180 Eigen::Map<PlainObjectBase, MapOptions, StrideType> & m,
181 const unsigned int version)
182 {
183 split_free(ar, m, version);
184 }
185
186 #if !defined(PINOCCHIO_WITH_EIGEN_TENSOR_MODULE) \
187 && ((__cplusplus <= 199711L && EIGEN_COMP_MSVC < 1900) || defined(__CUDACC__) || defined(EIGEN_AVOID_STL_ARRAY))
188 template<class Archive, typename _IndexType, std::size_t _NumIndices>
189 void save(
190 Archive & ar, const Eigen::array<_IndexType, _NumIndices> & a, const unsigned int /*version*/)
191 {
192 ar & make_nvp("array", make_array(&a.front(), _NumIndices));
193 }
194
195 template<class Archive, typename _IndexType, std::size_t _NumIndices>
196 void
197 load(Archive & ar, Eigen::array<_IndexType, _NumIndices> & a, const unsigned int /*version*/)
198 {
199 ar >> make_nvp("array", make_array(&a.front(), _NumIndices));
200 }
201
202 template<class Archive, typename _IndexType, std::size_t _NumIndices>
203 void
204 serialize(Archive & ar, Eigen::array<_IndexType, _NumIndices> & a, const unsigned int version)
205 {
206 split_free(ar, a, version);
207 }
208 #else
209 template<class Archive, class T, std::size_t N>
210 void save(Archive & ar, const std::array<T, N> & a, const unsigned int version)
211 {
212 typedef std::array<T, N> Array;
213 serialize(ar, const_cast<Array &>(a), version);
214 }
215
216 template<class Archive, class T, std::size_t N>
217 void load(Archive & ar, std::array<T, N> & a, const unsigned int version)
218 {
219 serialize(ar, a, version);
220 }
221 #endif
222
223 #ifdef PINOCCHIO_WITH_EIGEN_TENSOR_MODULE
224
225 template<class Archive, typename _IndexType, int _NumIndices>
226 void save(
227 Archive & ar, const Eigen::DSizes<_IndexType, _NumIndices> & ds, const unsigned int version)
228 {
229 save(ar, static_cast<const Eigen::array<_IndexType, _NumIndices> &>(ds), version);
230 }
231
232 template<class Archive, typename _IndexType, int _NumIndices>
233 void load(Archive & ar, Eigen::DSizes<_IndexType, _NumIndices> & ds, const unsigned int version)
234 {
235 load(ar, static_cast<Eigen::array<_IndexType, _NumIndices> &>(ds), version);
236 }
237
238 template<class Archive, typename _IndexType, int _NumIndices>
239 void
240 serialize(Archive & ar, Eigen::DSizes<_IndexType, _NumIndices> & ds, const unsigned int version)
241 {
242 split_free(ar, static_cast<Eigen::array<_IndexType, _NumIndices> &>(ds), version);
243 }
244
245 #endif
246
247 template<class Archive, typename _Scalar, int _NumIndices, int _Options, typename _IndexType>
248 void save(
249 Archive & ar,
250 const ::pinocchio::Tensor<_Scalar, _NumIndices, _Options, _IndexType> & t,
251 const unsigned int /*version*/)
252 {
253 typedef ::pinocchio::Tensor<_Scalar, _NumIndices, _Options, _IndexType> Tensor;
254 const typename Tensor::Dimensions & dimensions = t.dimensions();
255
256 ar & BOOST_SERIALIZATION_NVP(dimensions);
257 ar & make_nvp("data", make_array(t.data(), (size_t)t.size()));
258 }
259
260 template<class Archive, typename _Scalar, int _NumIndices, int _Options, typename _IndexType>
261 void load(
262 Archive & ar,
263 ::pinocchio::Tensor<_Scalar, _NumIndices, _Options, _IndexType> & t,
264 const unsigned int /*version*/)
265 {
266 typedef ::pinocchio::Tensor<_Scalar, _NumIndices, _Options, _IndexType> Tensor;
267 typename Tensor::Dimensions dimensions;
268
269 ar >> BOOST_SERIALIZATION_NVP(dimensions);
270 t.resize(dimensions);
271
272 ar >> make_nvp("data", make_array(t.data(), (size_t)t.size()));
273 }
274
275 template<class Archive, typename _Scalar, int _NumIndices, int _Options, typename _IndexType>
276 void serialize(
277 Archive & ar,
278 ::pinocchio::Tensor<_Scalar, _NumIndices, _Options, _IndexType> & t,
279 const unsigned int version)
280 {
281 split_free(ar, t, version);
282 }
283
284 } // namespace serialization
285 } // namespace boost
286
287 #endif // ifndef __pinocchio_serialization_eigen_matrix_hpp__
288