batching_pomp  1.0
This is an implementation of an algorithmic framework for anytime motion planning on large dense roadmaps.
RoadmapFromFile.hpp
1 /***********************************************************************
2 Copyright (c) 2017, Carnegie Mellon University
3 All rights reserved.
4 Authors: Chris Dellin <cdellin@gmail.com>
5  Shushman Choudhury <shushmanchoudhury@gmail.com>
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are
9 met:
10  Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12  Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *************************************************************************/
27 
28 #ifndef BATCHING_POMP_UTIL_ROADMAPFROMFILE_HPP_
29 #define BATCHING_POMP_UTIL_ROADMAPFROMFILE_HPP_
30 
31 #include <sstream>
32 #include <iostream>
33 #include <fstream>
34 #include <type_traits>
35 #include <boost/property_map/dynamic_property_map.hpp>
36 #include <boost/graph/graphml.hpp>
37 #include <boost/graph/adjacency_list.hpp>
38 #include <ompl/base/State.h>
39 #include <ompl/base/ScopedState.h>
40 #include <ompl/base/StateSpace.h>
41 #include <ompl/base/spaces/RealVectorStateSpace.h>
42 #include "rv_state_map_string_adaptor.hpp"
43 
44 namespace batching_pomp {
45 namespace util {
46 
48 
51 template <class PropMap, class StateCon>
53 {
54 public:
55  typedef boost::writable_property_map_tag category;
56  typedef typename boost::property_traits<PropMap>::key_type key_type;
57  typedef std::string value_type;
58  typedef std::string reference;
59 
60  const PropMap mPropMap;
61  ompl::base::StateSpacePtr mSpace;
62  const unsigned int mDim;
63 
64  RoadmapFromFilePutStateMap(PropMap _propMap, ompl::base::StateSpacePtr _space, unsigned int _dim)
65  : mPropMap{_propMap}
66  , mSpace{_space}
67  , mDim{_dim}
68  {
69  }
70 };
71 
73 template <class PropMap, class StateCon>
74 inline std::string
76  const typename RoadmapFromFilePutStateMap<PropMap,StateCon>::key_type & k)
77 {
78  abort();
79 }
80 
82 template <class PropMap, class StateCon>
83 inline void
85  const typename RoadmapFromFilePutStateMap<PropMap,StateCon>::key_type & k,
86  const std::string representation)
87 {
88  get(map.mPropMap, k).reset(new StateCon(map.mSpace));
89  ompl::base::State* ver_state{get(map.mPropMap, k)->state};
90  double * values{ver_state->as<ompl::base::RealVectorStateSpace::StateType>()->values};
91  std::stringstream ss(representation);
92  for (unsigned int ui=0; ui<map.mDim; ui++){
93  ss >> values[ui];
94  }
95 }
96 
98 
106 template <class Graph, class VStateMap, class StateCon, class EDistance>
108 {
109 typedef boost::graph_traits<Graph> GraphTypes;
110 typedef typename GraphTypes::vertex_descriptor Vertex;
111 typedef typename GraphTypes::vertex_iterator VertexIter;
112 typedef typename GraphTypes::edge_descriptor Edge;
113 typedef typename GraphTypes::edge_iterator EdgeIter;
114 
115 public:
116 
117  const std::string mFilename;
118 
120  const ompl::base::StateSpacePtr _space,
121  std::string _filename)
122  : mSpace(_space)
123  , mFilename(_filename)
124  , mBounds(0)
125  {
126  if (mSpace->getType() != ompl::base::STATE_SPACE_REAL_VECTOR) {
127  throw std::runtime_error("This only supports real vector state spaces!");
128  }
129 
130  mDim = mSpace->getDimension();
131  mBounds = mSpace->as<ompl::base::RealVectorStateSpace>()->getBounds();
132  }
133 
134  ~RoadmapFromFile() {}
135 
136  void generateVertices(Graph& _roadmap,
137  VStateMap _stateMap)
138  {
139  std::ifstream fp;
140  fp.open(mFilename.c_str());
141 
142  boost::dynamic_properties props;
143  props.property("state",
144  RoadmapFromFilePutStateMap<VStateMap,StateCon>(_stateMap, mSpace, mDim));
145 
146  boost::read_graphml(fp, _roadmap, props);
147  }
148 
149  void generateEdges(Graph& _roadmap,
150  VStateMap _stateMap,
151  EDistance _distanceMap)
152  {
153  EdgeIter ei, ei_end;
154  for (boost::tie(ei,ei_end)=edges(_roadmap); ei!=ei_end; ++ei)
155  {
156  ompl::base::State * state1 = get(_stateMap, source(*ei,_roadmap))->state;
157  ompl::base::State * state2 = get(_stateMap, target(*ei,_roadmap))->state;
158  put(_distanceMap, *ei, mSpace->distance(state1,state2));
159  }
160  }
161 
162 private:
163 
164  unsigned int mDim;
165  ompl::base::RealVectorBounds mBounds;
166  const ompl::base::StateSpacePtr mSpace;
167 
168 };
169 
170 
171 
172 } // namespace util
173 } // namespace batching_pomp
174 
175 #endif //BATCHING_POMP_UTIL_ROADMAPFROMFILE_HPP_
Reads a roadmap encoded as a .graphml file and creates the corresponding Boost Graph.
Definition: RoadmapFromFile.hpp:107
Definition: BatchingManager.hpp:36
The map used to decode the .graphml file and populate vertex states.
Definition: RoadmapFromFile.hpp:52
Composite struct to associate the state space with each state.
Definition: BatchingPOMP.hpp:55