hpp-pinocchio 9.0.2
Wrapping of the kinematic/dynamic chain Pinocchio for HPP.
serialization.hh
Go to the documentation of this file.
1//
2// Copyright (c) 2020 CNRS
3// Author: Joseph Mirabel
4//
5
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// 1. Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12//
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28// DAMAGE.
29
30#ifndef HPP_PINOCCHIO_SERIALIZATION_HH
31#define HPP_PINOCCHIO_SERIALIZATION_HH
32
33#include <boost/serialization/shared_ptr.hpp>
34#include <boost/serialization/split_free.hpp>
35#include <boost/serialization/weak_ptr.hpp>
37#include <hpp/pinocchio/fwd.hh>
40#include <pinocchio/config.hpp>
41#include <pinocchio/fwd.hpp>
42#include <pinocchio/serialization/eigen.hpp>
43#include <set>
44#include <type_traits>
45
46BOOST_SERIALIZATION_SPLIT_FREE(hpp::pinocchio::DevicePtr_t)
47BOOST_SERIALIZATION_SPLIT_FREE(hpp::pinocchio::DeviceWkPtr_t)
48
49namespace boost {
50namespace serialization {
51template <class Archive>
52inline void load(Archive& ar, hpp::pinocchio::DevicePtr_t& d,
53 const unsigned int version) {
54 load<Archive, hpp::pinocchio::Device>(ar, d, version);
55 auto* har = hpp::serialization::cast(&ar);
56 if (d && har && har->contains(d->name()))
57 d = har->template get<hpp::pinocchio::Device>(d->name(), true)->self();
58}
59template <class Archive>
60inline void load(Archive& ar, hpp::pinocchio::DeviceWkPtr_t& d,
61 const unsigned int version) {
62 load<Archive, hpp::pinocchio::Device>(ar, d, version);
63 auto* har = hpp::serialization::cast(&ar);
64 std::string name(d.lock()->name());
65 if (d.lock() && har && har->contains(name))
66 d = har->template get<hpp::pinocchio::Device>(name, true)->self();
67}
68template <class Archive>
69inline void load(Archive& ar, hpp::pinocchio::HumanoidRobotPtr_t& d,
70 const unsigned int version) {
71 load<Archive, hpp::pinocchio::HumanoidRobot>(ar, d, version);
72 auto* har = hpp::serialization::cast(&ar);
73 if (d && har && har->contains(d->name()))
74 d = har->template getChildClass<hpp::pinocchio::Device,
76 true)
77 ->self();
78}
79template <class Archive>
80inline void load(Archive& ar, hpp::pinocchio::HumanoidRobotWkPtr_t& d,
81 const unsigned int version) {
82 load<Archive, hpp::pinocchio::HumanoidRobot>(ar, d, version);
83 auto* har = hpp::serialization::cast(&ar);
84 std::string name(d.lock()->name());
85 if (d.lock() && har && har->contains(name))
86 d = har->template getChildClass<hpp::pinocchio::Device,
88 ->self();
89}
90
91#if PINOCCHIO_VERSION_AT_MOST(3, 90, 0)
92template <class Archive, typename _Scalar, int _Rows, int _Cols, int _Options,
93 int _MaxRows, int _MaxCols>
94inline void serialize(
95 Archive& ar,
96 Eigen::Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& m,
97 const unsigned int version) {
98 (void)version;
99 Eigen::DenseIndex rows(m.rows()), cols(m.cols());
100 ar& BOOST_SERIALIZATION_NVP(rows);
101 ar& BOOST_SERIALIZATION_NVP(cols);
102 if (!Archive::is_saving::value) m.resize(rows, cols);
103 if (m.size() > 0)
104 ar& make_nvp("data", make_array(m.data(), (size_t)m.size()));
105}
106#endif
107
108} // namespace serialization
109} // namespace boost
110
111namespace hpp {
112namespace serialization {
113namespace remove_duplicate {
114template <typename Key, typename Compare = std::less<Key>>
115struct ptr_less : Compare {
116 inline bool operator()(Key const* t1, Key const* t2) const {
117 return Compare::operator()(*t1, *t2);
118 }
119};
120
121template <typename Derived>
123 bool operator()(const Eigen::PlainObjectBase<Derived>& a,
124 const Eigen::PlainObjectBase<Derived>& b) const {
125 if (a.size() < b.size()) return true;
126 if (a.size() > b.size()) return false;
127 for (Eigen::Index i = 0; i < a.size(); ++i) {
128 if (a.derived().data()[i] < b.derived().data()[i]) return true;
129 if (a.derived().data()[i] > b.derived().data()[i]) return false;
130 }
131 return false;
132 }
133};
134
135template <typename Key, typename Compare = std::less<Key>>
136struct archive {
137 typedef Compare compare_type;
139 std::set<Key const*, ptr_compare_type> datas;
141
143};
144
145typedef archive<::hpp::pinocchio::vector_t,
146 eigen_compare<::hpp::pinocchio::vector_t>>
148
149template <class Archive, typename Key>
151 const char* name, Key& key,
152 const unsigned int version) {
153 (void)version;
154 Key* value = &key;
155 ar& boost::serialization::make_nvp(name, value);
156 if (!Archive::is_saving::value) key = *value;
157}
158
159template <class Archive, typename Key>
160inline void save_impl(Archive& ar, const char* name, const Key& key,
161 const unsigned int version) {
162 (void)version;
163 Key const* value = &key;
164 ar << boost::serialization::make_nvp(name, value);
165}
166
167template <class Archive, typename Key, typename Compare = std::less<Key>>
168inline void save_impl(Archive& ar,
169 std::set<Key const*, ptr_less<Key, Compare>>& set,
170 int& hitcount, const char* name, const Key& key,
171 const unsigned int version) {
172 (void)version;
173 if (!Archive::is_saving::value)
174 throw std::logic_error(
175 "HPP serialization: cannot load into a const element. This should "
176 "never happen.");
177 auto result = set.insert(&key);
178 bool inserted = result.second;
179 Key const* k = (inserted ? &key : *result.first);
180 ar& boost::serialization::make_nvp(name, k);
181 if (!inserted) hitcount++;
182}
183
184template <class Archive, typename Key, typename Compare = std::less<Key>>
185inline void serialize(Archive& ar,
186 std::set<Key const*, ptr_less<Key, Compare>>& set,
187 int& hitcount, const char* name, Key& key,
188 const unsigned int version) {
189 if (Archive::is_saving::value) {
190 save_impl(ar, set, hitcount, name, key, version);
191 } else {
193 }
194}
195
196template <bool is_base>
198 template <typename Archive, typename Key, typename Compare = std::less<Key>>
199 static inline void run(Archive& ar, const char* name, Key& key,
200 const unsigned int version) {
201 archive<Key, Compare>& rda = dynamic_cast<archive<Key, Compare>&>(ar);
202 serialize(ar, rda.datas, rda.hitcount, name, key, version);
203 }
204 template <typename Archive, typename Key, typename Compare = std::less<Key>>
205 static inline void save(Archive& ar, const char* name, const Key& key,
206 const unsigned int version) {
207 archive<Key, Compare>& rda = dynamic_cast<archive<Key, Compare>&>(ar);
208 save_impl(ar, rda.datas, rda.hitcount, name, key, version);
209 }
210};
211
212template <>
213struct serialiaze_impl<false> {
214 template <typename Archive, typename Key, typename Compare = std::less<Key>>
215 static inline void run(Archive& ar, const char* name, Key& key,
216 const unsigned int version) {
217 if (dynamic_cast<archive<Key, Compare>*>(&ar) != NULL)
219 else
221 }
222 template <typename Archive, typename Key, typename Compare = std::less<Key>>
223 static inline void save(Archive& ar, const char* name, const Key& key,
224 const unsigned int version) {
225 if (dynamic_cast<archive<Key, Compare>*>(&ar) != NULL)
227 version);
228 else
229 save_impl(ar, name, key, version);
230 }
231};
232
233template <typename Archive, typename Key, typename Compare = std::less<Key>,
234 bool is_base = std::is_base_of<archive<Key, Compare>, Archive>::value>
235inline void serialize(Archive& ar, const char* name, Key& key,
236 const unsigned int version) {
237 serialiaze_impl<is_base>::template run<Archive, Key, Compare>(ar, name, key,
238 version);
239}
240
241template <typename Archive, typename Key, typename Compare = std::less<Key>,
242 bool is_base = std::is_base_of<archive<Key, Compare>, Archive>::value>
243inline void save(Archive& ar, const char* name, const Key& key,
244 const unsigned int version) {
245 serialiaze_impl<is_base>::template save<Archive, Key, Compare>(ar, name, key,
246 version);
247}
248
249template <typename Archive>
250inline void serialize_vector(Archive& ar, const char* name,
252 const unsigned int version) {
253 serialize<Archive, ::hpp::pinocchio::vector_t, vector_archive::compare_type>(
254 ar, name, key, version);
255}
256
257template <typename Archive>
258inline void save_vector(Archive& ar, const char* name,
260 const unsigned int version) {
261 save<Archive, ::hpp::pinocchio::vector_t, vector_archive::compare_type>(
262 ar, name, key, version);
263}
264
265} // namespace remove_duplicate
266} // namespace serialization
267} // namespace hpp
268
269#endif // HPP_PINOCCHIO_SERIALIZATION_HH
Robot with geometric and dynamic pinocchio.
Definition: device.hh:60
Humanoid robot.
Definition: humanoid-robot.hh:44
void serialize(Archive &ar, hpp::pinocchio::liegroup::VectorSpaceOperation< Size, rot > &lg, const unsigned int version)
Definition: serialization.hh:40
void load(Archive &ar, hpp::pinocchio::DevicePtr_t &d, const unsigned int version)
Definition: serialization.hh:52
shared_ptr< Device > DevicePtr_t
Definition: fwd.hh:121
Eigen::Matrix< value_type, Eigen::Dynamic, 1 > vector_t
Definition: fwd.hh:91
shared_ptr< HumanoidRobot > HumanoidRobotPtr_t
Definition: fwd.hh:124
archive<::hpp::pinocchio::vector_t, eigen_compare<::hpp::pinocchio::vector_t > > vector_archive
Definition: serialization.hh:147
void save_impl(Archive &ar, const char *name, const Key &key, const unsigned int version)
Definition: serialization.hh:160
void save(Archive &ar, const char *name, const Key &key, const unsigned int version)
Definition: serialization.hh:243
void save_vector(Archive &ar, const char *name, const ::hpp::pinocchio::vector_t &key, const unsigned int version)
Definition: serialization.hh:258
void serialize(Archive &ar, std::set< Key const *, ptr_less< Key, Compare > > &set, int &hitcount, const char *name, Key &key, const unsigned int version)
Definition: serialization.hh:185
void serialize_vector(Archive &ar, const char *name, ::hpp::pinocchio::vector_t &key, const unsigned int version)
Definition: serialization.hh:250
void load_or_save_no_remove_duplicate_check(Archive &ar, const char *name, Key &key, const unsigned int version)
Definition: serialization.hh:150
archive_ptr_holder & cast(Archive &ar)
HPP_UTIL_DLLAPI const char * version
Utility functions.
Definition: serialization.hh:136
int hitcount
Definition: serialization.hh:140
Compare compare_type
Definition: serialization.hh:137
archive()
Definition: serialization.hh:142
std::set< Key const *, ptr_compare_type > datas
Definition: serialization.hh:139
ptr_less< Key, Compare > ptr_compare_type
Definition: serialization.hh:138
bool operator()(const Eigen::PlainObjectBase< Derived > &a, const Eigen::PlainObjectBase< Derived > &b) const
Definition: serialization.hh:123
Definition: serialization.hh:115
bool operator()(Key const *t1, Key const *t2) const
Definition: serialization.hh:116
static void run(Archive &ar, const char *name, Key &key, const unsigned int version)
Definition: serialization.hh:215
static void save(Archive &ar, const char *name, const Key &key, const unsigned int version)
Definition: serialization.hh:223
static void save(Archive &ar, const char *name, const Key &key, const unsigned int version)
Definition: serialization.hh:205
static void run(Archive &ar, const char *name, Key &key, const unsigned int version)
Definition: serialization.hh:199