GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/hpp/fcl/serialization/eigen.h Lines: 31 31 100.0 %
Date: 2024-02-09 12:57:42 Branches: 40 74 54.1 %

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 HPP_FCL_SERIALIZATION_EIGEN_H
12
#define HPP_FCL_SERIALIZATION_EIGEN_H
13
14
#include <Eigen/Dense>
15
16
#include <boost/serialization/split_free.hpp>
17
#include <boost/serialization/vector.hpp>
18
#include <boost/serialization/array.hpp>
19
20
// Workaround a bug in GCC >= 7 and C++17
21
// ref. https://gitlab.com/libeigen/eigen/-/issues/1676
22
#ifdef __GNUC__
23
#if __GNUC__ >= 7 && __cplusplus >= 201703L
24
namespace boost {
25
namespace serialization {
26
struct U;
27
}
28
}  // namespace boost
29
namespace Eigen {
30
namespace internal {
31
template <>
32
struct traits<boost::serialization::U> {
33
  enum { Flags = 0 };
34
};
35
}  // namespace internal
36
}  // namespace Eigen
37
#endif
38
#endif
39
40
namespace boost {
41
namespace serialization {
42
43
#ifndef HPP_FCL_SKIP_EIGEN_BOOST_SERIALIZATION
44
45
template <class Archive, typename Scalar, int Rows, int Cols, int Options,
46
          int MaxRows, int MaxCols>
47
1576488
void save(Archive& ar,
48
          const Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>& m,
49
          const unsigned int /*version*/) {
50

1576488
  Eigen::DenseIndex rows(m.rows()), cols(m.cols());
51

24
  if (Rows == Eigen::Dynamic) ar& BOOST_SERIALIZATION_NVP(rows);
52

8
  if (Cols == Eigen::Dynamic) ar& BOOST_SERIALIZATION_NVP(cols);
53



1576488
  ar& make_nvp("data", make_array(m.data(), (size_t)m.size()));
54
1576488
}
55
56
template <class Archive, typename Scalar, int Rows, int Cols, int Options,
57
          int MaxRows, int MaxCols>
58
1576488
void load(Archive& ar,
59
          Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>& m,
60
          const unsigned int /*version*/) {
61
1576488
  Eigen::DenseIndex rows = Rows, cols = Cols;
62

24
  if (Rows == Eigen::Dynamic) ar >> BOOST_SERIALIZATION_NVP(rows);
63

8
  if (Cols == Eigen::Dynamic) ar >> BOOST_SERIALIZATION_NVP(cols);
64
1576488
  m.resize(rows, cols);
65



1576488
  ar >> make_nvp("data", make_array(m.data(), (size_t)m.size()));
66
1576488
}
67
68
template <class Archive, typename Scalar, int Rows, int Cols, int Options,
69
          int MaxRows, int MaxCols>
70
3152976
void serialize(Archive& ar,
71
               Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>& m,
72
               const unsigned int version) {
73
3152976
  split_free(ar, m, version);
74
3152976
}
75
76
template <class Archive, typename PlainObjectBase, int MapOptions,
77
          typename StrideType>
78
18
void save(Archive& ar,
79
          const Eigen::Map<PlainObjectBase, MapOptions, StrideType>& m,
80
          const unsigned int /*version*/) {
81
18
  Eigen::DenseIndex rows(m.rows()), cols(m.cols());
82
  if (PlainObjectBase::RowsAtCompileTime == Eigen::Dynamic)
83
    ar& BOOST_SERIALIZATION_NVP(rows);
84
  if (PlainObjectBase::ColsAtCompileTime == Eigen::Dynamic)
85

18
    ar& BOOST_SERIALIZATION_NVP(cols);
86


18
  ar& make_nvp("data", make_array(m.data(), (size_t)m.size()));
87
18
}
88
89
template <class Archive, typename PlainObjectBase, int MapOptions,
90
          typename StrideType>
91
18
void load(Archive& ar, Eigen::Map<PlainObjectBase, MapOptions, StrideType>& m,
92
          const unsigned int /*version*/) {
93
18
  Eigen::DenseIndex rows = PlainObjectBase::RowsAtCompileTime,
94
18
                    cols = PlainObjectBase::ColsAtCompileTime;
95
  if (PlainObjectBase::RowsAtCompileTime == Eigen::Dynamic)
96
    ar >> BOOST_SERIALIZATION_NVP(rows);
97
  if (PlainObjectBase::ColsAtCompileTime == Eigen::Dynamic)
98

18
    ar >> BOOST_SERIALIZATION_NVP(cols);
99
18
  m.resize(rows, cols);
100


18
  ar >> make_nvp("data", make_array(m.data(), (size_t)m.size()));
101
18
}
102
103
template <class Archive, typename PlainObjectBase, int MapOptions,
104
          typename StrideType>
105
36
void serialize(Archive& ar,
106
               Eigen::Map<PlainObjectBase, MapOptions, StrideType>& m,
107
               const unsigned int version) {
108
36
  split_free(ar, m, version);
109
36
}
110
111
#endif  // ifned HPP_FCL_SKIP_EIGEN_BOOST_SERIALIZATION
112
}  // namespace serialization
113
}  // namespace boost
114
115
#endif  // ifndef HPP_FCL_SERIALIZATION_EIGEN_H