pinocchio  2.5.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
eigen.hpp
1 //
2 // Copyright (c) 2017-2020 CNRS INRIA
3 //
4 
5 /*
6  Code adapted from: https://gist.githubusercontent.com/mtao/5798888/raw/5be9fa9b66336c166dba3a92c0e5b69ffdb81501/eigen_boost_serialization.hpp
7  Copyright (c) 2015 Michael Tao
8 
9  Permission is hereby granted, free of charge, to any person obtaining a copy
10  of this software and associated documentation files (the "Software"), to deal
11  in the Software without restriction, including without limitation the rights
12  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  copies of the Software, and to permit persons to whom the Software is
14  furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  THE SOFTWARE.
26  */
27 
28 #ifndef __pinocchio_serialization_eigen_matrix_hpp__
29 #define __pinocchio_serialization_eigen_matrix_hpp__
30 
31 #include <Eigen/Dense>
32 #include "pinocchio/math/tensor.hpp"
33 
34 #include <boost/serialization/split_free.hpp>
35 #include <boost/serialization/vector.hpp>
36 #include <boost/serialization/array.hpp>
37 
38 namespace boost
39 {
40  namespace serialization
41  {
42 
43  template <class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
44  void save(Archive & ar, const Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> & m, const unsigned int /*version*/)
45  {
46  Eigen::DenseIndex rows(m.rows()), cols(m.cols());
47  ar & BOOST_SERIALIZATION_NVP(rows);
48  ar & BOOST_SERIALIZATION_NVP(cols);
49  ar & make_nvp("data",make_array(m.data(), (size_t)m.size()));
50  }
51 
52  template <class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
53  void load(Archive & ar, Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> & m, const unsigned int /*version*/)
54  {
55  Eigen::DenseIndex rows,cols;
56  ar >> BOOST_SERIALIZATION_NVP(rows);
57  ar >> BOOST_SERIALIZATION_NVP(cols);
58  m.resize(rows,cols);
59 // if(m.size() > 0)
60  ar >> make_nvp("data",make_array(m.data(), (size_t)m.size()));
61  }
62 
63  template <class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
64  void serialize(Archive & ar, Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> & m, const unsigned int version)
65  {
66  split_free(ar,m,version);
67  }
68 
69 #if !defined(PINOCCHIO_WITH_EIGEN_TENSOR_MODULE) && ((__cplusplus <= 199711L && EIGEN_COMP_MSVC < 1900) || defined(__CUDACC__) || defined(EIGEN_AVOID_STL_ARRAY))
70  template <class Archive, typename _IndexType, std::size_t _NumIndices>
71  void save(Archive & ar, const Eigen::array<_IndexType,_NumIndices> & a, const unsigned int /*version*/)
72  {
73  ar & make_nvp("array",make_array(&a.front(),_NumIndices));
74  }
75 
76  template <class Archive, typename _IndexType, std::size_t _NumIndices>
77  void load(Archive & ar, Eigen::array<_IndexType,_NumIndices> & a, const unsigned int /*version*/)
78  {
79  ar >> make_nvp("array",make_array(&a.front(),_NumIndices));
80  }
81 
82  template <class Archive, typename _IndexType, std::size_t _NumIndices>
83  void serialize(Archive & ar, Eigen::array<_IndexType,_NumIndices> & a, const unsigned int version)
84  {
85  split_free(ar,a,version);
86  }
87 #else
88  template <class Archive, class T, std::size_t N>
89  void save(Archive& ar, const std::array<T,N> & a, const unsigned int version)
90  {
91  typedef std::array<T,N> Array;
92  serialize(ar,const_cast<Array&>(a),version);
93  }
94 
95  template <class Archive, class T, std::size_t N>
96  void load(Archive& ar, std::array<T,N> & a, const unsigned int version)
97  {
98  serialize(ar,a,version);
99  }
100 #endif
101 
102 #ifdef PINOCCHIO_WITH_EIGEN_TENSOR_MODULE
103 
104  template <class Archive, typename _IndexType, int _NumIndices>
105  void save(Archive & ar, const Eigen::DSizes<_IndexType,_NumIndices> & ds, const unsigned int version)
106  {
107  save(ar,static_cast<const Eigen::array<_IndexType,_NumIndices> &>(ds),version);
108  }
109 
110  template <class Archive, typename _IndexType, int _NumIndices>
111  void load(Archive & ar, Eigen::DSizes<_IndexType,_NumIndices> & ds, const unsigned int version)
112  {
113  load(ar,static_cast<Eigen::array<_IndexType,_NumIndices> &>(ds),version);
114  }
115 
116  template <class Archive, typename _IndexType, int _NumIndices>
117  void serialize(Archive & ar, Eigen::DSizes<_IndexType,_NumIndices> & ds, const unsigned int version)
118  {
119  split_free(ar,static_cast<Eigen::array<_IndexType,_NumIndices> &>(ds),version);
120  }
121 
122 #endif
123 
124  template <class Archive, typename _Scalar, int _NumIndices, int _Options, typename _IndexType>
125  void save(Archive & ar, const ::pinocchio::Tensor<_Scalar,_NumIndices,_Options,_IndexType> & t, const unsigned int /*version*/)
126  {
127  typedef ::pinocchio::Tensor<_Scalar,_NumIndices,_Options,_IndexType> Tensor;
128  const typename Tensor::Dimensions & dimensions = t.dimensions();
129 
130  ar & BOOST_SERIALIZATION_NVP(dimensions);
131  ar & make_nvp("data",make_array(t.data(), (size_t)t.size()));
132  }
133 
134  template <class Archive, typename _Scalar, int _NumIndices, int _Options, typename _IndexType>
135  void load(Archive & ar, ::pinocchio::Tensor<_Scalar,_NumIndices,_Options,_IndexType> & t, const unsigned int /*version*/)
136  {
137  typedef ::pinocchio::Tensor<_Scalar,_NumIndices,_Options,_IndexType> Tensor;
138  typename Tensor::Dimensions dimensions;
139 
140  ar >> BOOST_SERIALIZATION_NVP(dimensions);
141  t.resize(dimensions);
142 
143  ar >> make_nvp("data",make_array(t.data(), (size_t)t.size()));
144  }
145 
146  template <class Archive, typename _Scalar, int _NumIndices, int _Options, typename _IndexType>
147  void serialize(Archive & ar, ::pinocchio::Tensor<_Scalar,_NumIndices,_Options,_IndexType> & t, const unsigned int version)
148  {
149  split_free(ar,t,version);
150  }
151 
152  }
153 }
154 
155 #endif // ifndef __pinocchio_serialization_eigen_matrix_hpp__
Definition: casadi.hpp:15