Commit 363fdd2e authored by Joseph Mirabel's avatar Joseph Mirabel
Browse files

[Serialization] Use archive from hpp-util.

parent 85e10096
Pipeline #12705 failed with stage
in 3 minutes and 48 seconds
......@@ -75,6 +75,8 @@ namespace hpp {
/// Both pinocchio objects model and data are copied.
/// TODO: this method is not implemented yet (assert if called)
DevicePtr_t cloneConst() const { return createCopyConst(weakPtr_.lock()); }
DevicePtr_t self() const { return weakPtr_.lock(); }
/// Get name of device
const std::string& name () const {return name_;}
......@@ -356,4 +358,6 @@ namespace hpp {
} // namespace pinocchio
} // namespace hpp
BOOST_CLASS_EXPORT_KEY(hpp::pinocchio::Device)
#endif // HPP_PINOCCHIO_DEVICE_HH
......@@ -41,6 +41,8 @@ namespace hpp {
/// \brief Clone as a HumanoidRobot
virtual DevicePtr_t clone () const;
HumanoidRobotPtr_t self() const { return weakPtr_.lock(); }
///
/// @}
///
......
......@@ -164,7 +164,11 @@ namespace hpp {
value_type upperBoundAngularVelocity () const;
/// Maximal distance of joint origin to parent origin
const value_type& maximalDistanceToParent () const { return maximalDistanceToParent_; }
const value_type& maximalDistanceToParent () {
if (maximalDistanceToParent_ < 0)
computeMaximalDistanceToParent();
return maximalDistanceToParent_;
}
/// \}
protected:
......
......@@ -22,22 +22,16 @@
# include <set>
# include <type_traits>
# include <pinocchio/fwd.hpp>
# include <boost/serialization/split_free.hpp>
# include <boost/serialization/shared_ptr.hpp>
# include <boost/serialization/weak_ptr.hpp>
# include <hpp/pinocchio/fwd.hh>
# include <pinocchio/serialization/eigen.hpp>
namespace hpp {
namespace serialization {
struct archive_device_wrapper {
pinocchio::DevicePtr_t device;
virtual ~archive_device_wrapper() {}
};
} // namespace pinocchio
} // namespace hpp
# include <hpp/util/serialization.hh>
# include <hpp/pinocchio/fwd.hh>
# include <hpp/pinocchio/device.hh>
# include <hpp/pinocchio/humanoid-robot.hh>
BOOST_SERIALIZATION_SPLIT_FREE(hpp::pinocchio::DevicePtr_t)
BOOST_SERIALIZATION_SPLIT_FREE(hpp::pinocchio::DeviceWkPtr_t)
......@@ -48,33 +42,35 @@ template<class Archive>
inline void load (Archive& ar, hpp::pinocchio::DevicePtr_t& d, const unsigned int version)
{
load<Archive, hpp::pinocchio::Device> (ar, d, version);
using hpp::serialization::archive_device_wrapper;
archive_device_wrapper* adw = dynamic_cast<archive_device_wrapper*>(&ar);
if (adw) d = adw->device;
auto* har = hpp::serialization::cast(&ar);
if (d && har && har->contains(d->name()))
d = har->template get<hpp::pinocchio::Device>(d->name(), true)->self();
}
template<class Archive>
inline void load (Archive& ar, hpp::pinocchio::DeviceWkPtr_t& d, const unsigned int version)
{
load<Archive, hpp::pinocchio::Device> (ar, d, version);
using hpp::serialization::archive_device_wrapper;
archive_device_wrapper* adw = dynamic_cast<archive_device_wrapper*>(&ar);
if (adw) d = adw->device;
auto* har = hpp::serialization::cast(&ar);
std::string name (d.lock()->name());
if (d.lock() && har && har->contains(name))
d = har->template get<hpp::pinocchio::Device>(name, true)->self();
}
template<class Archive>
inline void load (Archive& ar, hpp::pinocchio::HumanoidRobotPtr_t& d, const unsigned int version)
{
load<Archive, hpp::pinocchio::HumanoidRobot> (ar, d, version);
using hpp::serialization::archive_device_wrapper;
archive_device_wrapper* adw = dynamic_cast<archive_device_wrapper*>(&ar);
if (adw) d = boost::dynamic_pointer_cast<hpp::pinocchio::HumanoidRobot>(adw->device);
auto* har = hpp::serialization::cast(&ar);
if (d && har && har->contains(d->name()))
d = har->template getChildClass<hpp::pinocchio::Device, hpp::pinocchio::HumanoidRobot>(d->name(), true)->self();
}
template<class Archive>
inline void load (Archive& ar, hpp::pinocchio::HumanoidRobotWkPtr_t& d, const unsigned int version)
{
load<Archive, hpp::pinocchio::HumanoidRobot> (ar, d, version);
using hpp::serialization::archive_device_wrapper;
archive_device_wrapper* adw = dynamic_cast<archive_device_wrapper*>(&ar);
if (adw) d = boost::dynamic_pointer_cast<hpp::pinocchio::HumanoidRobot>(adw->device);
auto* har = hpp::serialization::cast(&ar);
std::string name (d.lock()->name());
if (d.lock() && har && har->contains(name))
d = har->template getChildClass<hpp::pinocchio::Device, hpp::pinocchio::HumanoidRobot>(name, true)->self();
}
template <class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
......@@ -103,8 +99,8 @@ struct ptr_less : Compare {
template<typename Derived>
struct eigen_compare {
bool operator() (const Eigen::DenseBase<Derived>& a,
const Eigen::DenseBase<Derived>& b)
bool operator() (const Eigen::PlainObjectBase<Derived>& a,
const Eigen::PlainObjectBase<Derived>& b)
{
if (a.size() < b.size()) return true;
if (a.size() > b.size()) return false;
......
......@@ -535,9 +535,6 @@ namespace hpp {
#endif
}
using boost::serialization::make_nvp;
using hpp::serialization::archive_device_wrapper;
template<typename To, typename Ti, typename UnaryOp>
inline std::vector<To> serialize_to (const std::vector<Ti>& in, UnaryOp op)
{
......@@ -552,9 +549,11 @@ namespace hpp {
{
(void) version;
archive_device_wrapper* adw = dynamic_cast<archive_device_wrapper*>(&ar);
auto* har = hpp::serialization::cast(&ar);
ar & BOOST_SERIALIZATION_NVP(name_);
bool written = (adw == NULL);
// write it is not present in the HPP archive.
bool written = (!har ||
har->template get<hpp::pinocchio::Device>(name_, false) != this);
ar & BOOST_SERIALIZATION_NVP(written);
if (written) {
// AbstractDevice
......@@ -562,7 +561,6 @@ namespace hpp {
//ar & BOOST_SERIALIZATION_NVP(geomModel_);
// Device
ar & BOOST_SERIALIZATION_NVP(name_);
// - grippers_
std::vector<FrameIndex> grippers;
std::transform(grippers_.begin(), grippers_.end(), grippers.begin(),
......@@ -588,14 +586,14 @@ namespace hpp {
bool written;
ar & BOOST_SERIALIZATION_NVP(written);
archive_device_wrapper* adw = dynamic_cast<archive_device_wrapper*>(&ar);
auto* har = hpp::serialization::cast(&ar);
bool dummyDevice = har && har->template get<hpp::pinocchio::Device>(name_, false);
if (written) {
// AbstractDevice
ar & BOOST_SERIALIZATION_NVP(model_);
//ar & BOOST_SERIALIZATION_NVP(geomModel_);
// Device
ar & BOOST_SERIALIZATION_NVP(name_);
// - grippers_
std::vector<FrameIndex> grippers;
ar & BOOST_SERIALIZATION_NVP(grippers);
......@@ -609,8 +607,7 @@ namespace hpp {
size_type nbDeviceData;
ar & BOOST_SERIALIZATION_NVP(nbDeviceData);
if (!adw) { // if archive_device_wrapper, then this device will be
// thrown away. No need to initialize it cleanly.
if (!dummyDevice) { // If this device will not be thrown away, initialize it cleanly.
grippers_.reserve(grippers.size());
std::transform(grippers.begin(), grippers.end(), grippers_.begin(),
[this](FrameIndex i) -> GripperPtr_t {
......@@ -620,10 +617,9 @@ namespace hpp {
createGeomData();
numberDeviceData(nbDeviceData);
}
} else if (!adw) // && !written
} else if (!dummyDevice)
throw std::logic_error("This archive does not contain a valid Device "
"and the archive is not of type archive_device_wrapper.");
// else TODO if (adw->device->name() != name_) ?
"of name " + name_);
}
HPP_SERIALIZATION_SPLIT_IMPLEMENT(Device);
......
......@@ -191,12 +191,14 @@ namespace hpp {
template<class Archive>
void HumanoidRobot::serialize(Archive & ar, const unsigned int version)
{
using hpp::serialization::archive_device_wrapper;
(void) version;
auto* har = hpp::serialization::cast(&ar);
ar & boost::serialization::make_nvp("base", boost::serialization::base_object<Device>(*this));
archive_device_wrapper* adw = dynamic_cast<archive_device_wrapper*>(&ar);
bool written = (adw == NULL);
// TODO we should throw if a Device instance with name name_ and not of
// type HumanoidRobot is found.
bool written = (!har ||
har->template getChildClass<Device, HumanoidRobot>(name_, false) != this);
ar & BOOST_SERIALIZATION_NVP(written);
if (written) {
ar & BOOST_SERIALIZATION_NVP(weakPtr_);
......
......@@ -50,14 +50,14 @@ namespace hpp {
}
Joint::Joint (DeviceWkPtr_t device, JointIndex indexInJointList )
:devicePtr(device)
:maximalDistanceToParent_(-1)
,devicePtr(device)
,jointIndex(indexInJointList)
{
assert (devicePtr.lock());
assert (robot()->modelPtr());
assert (std::size_t(jointIndex)<model().joints.size());
setChildList();
computeMaximalDistanceToParent();
}
void Joint::setChildList()
......@@ -549,10 +549,12 @@ namespace hpp {
void Joint::serialize(Archive & ar, const unsigned int version)
{
(void) version;
ar & BOOST_SERIALIZATION_NVP(maximalDistanceToParent_);
ar & BOOST_SERIALIZATION_NVP(devicePtr);
ar & BOOST_SERIALIZATION_NVP(jointIndex);
ar & BOOST_SERIALIZATION_NVP(children);
if (!Archive::is_saving::value) {
maximalDistanceToParent_ = -1;
setChildList();
}
}
HPP_SERIALIZATION_IMPLEMENT(Joint);
......
......@@ -146,12 +146,6 @@ BOOST_AUTO_TEST_CASE(load_neutral_configuration)
*/
}
struct iarchive :
boost::archive::xml_iarchive, hpp::serialization::archive_device_wrapper
{
iarchive(std::istream& is) : boost::archive::xml_iarchive (is) {}
};
BOOST_AUTO_TEST_CASE(serialization)
{
DevicePtr_t device = unittest::makeDevice (unittest::HumanoidRomeo);
......@@ -159,17 +153,20 @@ BOOST_AUTO_TEST_CASE(serialization)
std::stringstream ss;
{
boost::archive::xml_oarchive oa(ss);
hpp::serialization::xml_oarchive oa(ss);
oa.initialize();
oa << boost::serialization::make_nvp("device", device);
oa << boost::serialization::make_nvp("joint", joint);
}
BOOST_TEST_MESSAGE(ss.str());
DevicePtr_t device2;
JointPtr_t joint2;
{
iarchive ia(ss);
ia.device = device;
//boost::archive::xml_iarchive ia(ss);
hpp::serialization::xml_iarchive ia(ss);
ia.insert(device->name(), device.get());
ia.initialize();
ia >> boost::serialization::make_nvp("device", device2);
ia >> boost::serialization::make_nvp("joint", joint2);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment