Commit 185ff4e6 authored by Joseph Mirabel's avatar Joseph Mirabel
Browse files

Remove old roadmap serialization and add serializeRoadmap function.

parent 948d263f
Pipeline #12679 failed with stage
in 1 minute and 46 seconds
......@@ -139,7 +139,7 @@ SET(${PROJECT_NAME}_HEADERS
include/hpp/core/path-projector/recursive-hermite.hh
include/hpp/core/path-projector.hh
include/hpp/core/nearest-neighbor.hh
include/hpp/core/parser/roadmap-factory.hh
include/hpp/core/parser/roadmap.hh
include/hpp/core/problem-target.hh
include/hpp/core/problem-target/goal-configurations.hh
include/hpp/core/problem-target/task-target.hh
......
// Copyright (c) 2015 CNRS
// Authors: Joseph Mirabel
//
// This file is part of hpp-core
// hpp-core is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version
// 3 of the License, or (at your option) any later version.
//
// hpp-core is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Lesser Public License for more details. You should have
// received a copy of the GNU Lesser General Public License along with
// hpp-core If not, see
// <http://www.gnu.org/licenses/>.
#ifndef HPP_CORE_PARSER_ROADMAP_FACTORY_HH
# define HPP_CORE_PARSER_ROADMAP_FACTORY_HH
# include <hpp/util/parser.hh>
# include <hpp/util/factories/sequence.hh>
# include <hpp/core/fwd.hh>
# include <hpp/core/config.hh>
namespace hpp {
namespace core {
namespace parser {
/// \addtogroup parser
/// \{
using hpp::util::parser::ObjectFactory;
using hpp::util::parser::XMLElement;
typedef hpp::util::parser::SequenceFactory<std::string> StringSequence;
typedef hpp::util::parser::SequenceFactory<double> ConfigurationFactory;
typedef hpp::util::parser::SequenceFactory<unsigned int> IdSequence;
/// Write roadmap to a text file.
void writeRoadmap (std::ostream& o, const ProblemPtr_t& problem,
const RoadmapPtr_t& roadmap);
/// Create a new roadmap from a file.
RoadmapPtr_t readRoadmap (const std::string& filename,
const ProblemPtr_t& problem);
/// Populate an existing roadmap from a file.
RoadmapPtr_t readRoadmap (const std::string& filename,
const RoadmapPtr_t& roadmap, const ProblemPtr_t& problem);
class HPP_CORE_DLLAPI RoadmapFactory : public ObjectFactory {
public:
typedef ::hpp::util::parser::ObjectFactory Parent_t;
/// Constructor using an already created Roadmap
RoadmapFactory (
const RoadmapPtr_t& roadmap, const ProblemPtr_t& problem,
ObjectFactory* parent, const XMLElement* element);
RoadmapPtr_t roadmap () const {
return roadmap_;
}
static ObjectFactory* create (
const RoadmapPtr_t& r, const ProblemPtr_t& p,
ObjectFactory* parent, const XMLElement* el)
{
return new RoadmapFactory (r, p, parent, el);
}
virtual bool finishAttributes ();
virtual void finishTags ();
RoadmapFactory (const ProblemPtr_t& problem,
const RoadmapPtr_t& roadmap, ObjectFactory* parent = NULL);
private:
ProblemPtr_t problem() const
{
return problem_.lock();
}
ProblemWkPtr_t problem_;
void computePermutation (const std::vector <std::string>& jointNames);
ConfigurationPtr_t permuteAndCreateConfiguration
(const std::vector <double>& config);
size_type getNodeIdFromRoadmap (const NodePtr_t& node) const;
RoadmapPtr_t roadmap_;
typedef std::vector <std::size_t> SizeVector_t;
SizeVector_t permutation_;
size_type extraCSsize_;
typedef std::vector <NodePtr_t> Nodes_t;
typedef std::vector <EdgePtr_t> Edges_t;
Nodes_t nodes_;
Edges_t edges_;
}; // class RoadmapVisitor
/// \}
} // namespace parser
} // namespace core
} // namespace hpp
#endif // HPP_CORE_PARSER_ROADMAP_FACTORY_HH
// Copyright (c) 2020 CNRS
// Authors: Joseph Mirabel
//
// This file is part of hpp-core
// hpp-core is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version
// 3 of the License, or (at your option) any later version.
//
// hpp-core is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Lesser Public License for more details. You should have
// received a copy of the GNU Lesser General Public License along with
// hpp-core If not, see
// <http://www.gnu.org/licenses/>.
#ifndef HPP_CORE_PARSER_ROADMAP_HH
# define HPP_CORE_PARSER_ROADMAP_HH
# include <fstream>
# include <hpp/core/roadmap.hh>
# include <hpp/pinocchio/serialization.hh>
namespace hpp {
namespace core {
namespace parser {
namespace internal {
template<typename Parent, typename Child>
struct InsertChildClass : std::pair<std::string, Child*>
{
using std::pair<std::string, Child>::pair;
};
template<class Archive, class A>
inline void insert(Archive& ar, const std::pair<std::string, A*>& a)
{
ar.insert(a.first, a.second);
}
template<class Archive, class Parent, class Child>
inline void insert(Archive& ar, const InsertChildClass<Parent, Child>& a)
{
ar.template insertChildClass<Parent, Child>(a.first, a.second);
}
template<class Archive> inline void inserts(Archive&) {}
template<class Archive, class A, class... B>
inline void inserts(Archive& ar, A& a, B&... b)
{
insert(ar, a);
inserts(ar, b...);
}
} // namespace internal
/// \addtogroup roadmap
/// \{
/// \tparam Archive an archive from hpp::serialization::(xml|binary|text)_(i|o)archive.
/// The only requirement is that it derives from hpp::serialization::archive_ptr_holder.
/// \tparam Args a list of std::pair<std::string, Object*> or InsertChildClass
/// \param roadmap the roadmap to save or fill.
/// \param filename the file to read or write.
/// \param args indicate the name and object pointer to insert in the archive pointer holder.
template<class A>
std::pair<std::string, A*> make_nvp (const std::string& n, A* a)
{ return std::pair<std::string, A*> (n, a); }
template<class Parent, class Child>
internal::InsertChildClass<Parent, Child> make_nvp_with_parent (const std::string& n, Child* a)
{ return internal::InsertChildClass<Parent, Child> (n, a); }
template<class Archive, class... Args>
void serializeRoadmap (RoadmapPtr_t& roadmap, const std::string& filename, Args... args)
{
typename std::conditional<Archive::is_saving::value,
std::ofstream, std::ifstream>::type
fs (filename);
Archive ar (fs);
internal::inserts(ar, args...);
ar.initialize();
ar & hpp::serialization::make_nvp("roadmap", roadmap);
}
/// \}
} // namespace parser
} // namespace core
} // namespace hpp
#endif // HPP_CORE_PARSER_ROADMAP_HH
......@@ -23,6 +23,7 @@
#include <hpp/pinocchio/joint.hh>
#include <hpp/pinocchio/configuration.hh>
#include <hpp/pinocchio/urdf/util.hh>
#include <hpp/pinocchio/serialization.hh>
#include <hpp/core/fwd.hh>
#include <hpp/core/roadmap.hh>
#include <hpp/core/problem.hh>
......@@ -33,18 +34,7 @@
#include <hpp/core/steering-method/straight.hh>
#include <hpp/core/weighed-distance.hh>
// should we include
// #include <hpp/core/serialization.hh>
// instead of the following ?
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <hpp/pinocchio/serialization.hh>
#include <hpp/core/parser/roadmap.hh>
#define BOOST_TEST_MODULE roadmap-1
#include <boost/test/included/unit_test.hpp>
......@@ -391,17 +381,6 @@ BOOST_AUTO_TEST_CASE (nearestNeighbor) {
}
}
template<typename Base>
struct oarchive : Base, hpp::serialization::archive_device_wrapper
{
oarchive(std::ostream& os) : Base (os) {}
};
template<typename Base>
struct iarchive : Base, hpp::serialization::archive_device_wrapper
{
iarchive(std::istream& is) : Base (is) {}
};
BOOST_AUTO_TEST_CASE (serialization) {
// Build robot
DevicePtr_t robot = createRobot();
......@@ -463,52 +442,31 @@ BOOST_AUTO_TEST_CASE (serialization) {
std::cout << *r << std::endl;
// save data to archive
{
std::ofstream ofs("filename.txt");
oarchive<boost::archive::text_oarchive> oa(ofs);
oa << r;
}
parser::serializeRoadmap<hpp::serialization::text_oarchive>(r, "filename.txt",
parser::make_nvp(robot->name(), robot.get()));
// save data to archive
{
std::ofstream ofs("filename.xml");
oarchive<boost::archive::xml_oarchive> oa(ofs);
oa << boost::serialization::make_nvp("roadmap", r);
}
parser::serializeRoadmap<hpp::serialization::xml_oarchive>(r, "filename.xml",
parser::make_nvp(robot->name(), robot.get()));
// save data to archive
{
std::ofstream ofs("filename.bin");
oarchive<boost::archive::binary_oarchive> oa(ofs);
oa << r;
}
parser::serializeRoadmap<hpp::serialization::binary_oarchive>(r, "filename.bin",
parser::make_nvp(robot->name(), robot.get()));
}
BOOST_AUTO_TEST_CASE (deserialization) {
DevicePtr_t device = createRobot();
DevicePtr_t robot = createRobot();
RoadmapPtr_t nr;
{
std::ifstream ifs("filename.txt");
iarchive<boost::archive::text_iarchive> ia(ifs);
ia.device = device;
ia >> nr;
std::cout << *nr << std::endl;
}
{
std::ifstream ifs("filename.xml");
iarchive<boost::archive::xml_iarchive> ia(ifs);
ia.device = device;
ia >> boost::serialization::make_nvp("roadmap", nr);
std::cout << *nr << std::endl;
}
{
std::ifstream ifs("filename.bin");
iarchive<boost::archive::binary_iarchive> ia(ifs);
ia.device = device;
ia >> nr;
std::cout << *nr << std::endl;
}
parser::serializeRoadmap<hpp::serialization::text_iarchive>(nr, "filename.txt",
parser::make_nvp(robot->name(), robot.get()));
std::cout << *nr << std::endl;
parser::serializeRoadmap<hpp::serialization::xml_iarchive>(nr, "filename.xml",
parser::make_nvp(robot->name(), robot.get()));
std::cout << *nr << std::endl;
parser::serializeRoadmap<hpp::serialization::binary_iarchive>(nr, "filename.bin",
parser::make_nvp(robot->name(), robot.get()));
std::cout << *nr << std::endl;
}
BOOST_AUTO_TEST_SUITE_END()
Supports Markdown
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