LEMUR Packages: ompl_lemur or_lemur pr_bgl prpy_lemur
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Groups Pages
Roadmap.h
Go to the documentation of this file.
1 
7 namespace ompl_lemur
8 {
9 
10 template <class Graph_, class VState_, class EDistance_, class VBatch_, class EBatch_, class VShadow_, class EVector_, class NN_>
12 {
13  typedef Graph_ Graph;
14  typedef VState_ VState;
15  typedef EDistance_ EDistance;
16  typedef VBatch_ VBatch;
17  typedef EBatch_ EBatch;
18  typedef VShadow_ VShadow;
19  typedef EVector_ EVector;
20  typedef NN_ NN;
22  Graph & g;
23  VState state_map;
24  EDistance distance_map;
25  VBatch vertex_batch_map;
26  EBatch edge_batch_map;
27  VShadow is_shadow_map;
28  EVector edge_vector_map;
29  NN * nn;
32  Graph & g,
33  VState state_map,
34  EDistance distance_map,
35  VBatch vertex_batch_map,
36  EBatch edge_batch_map,
37  VShadow is_shadow_map,
38  EVector edge_vector_map,
39  NN * nn):
40  space(space), g(g),
41  state_map(state_map), distance_map(distance_map),
42  vertex_batch_map(vertex_batch_map), edge_batch_map(edge_batch_map),
43  is_shadow_map(is_shadow_map),
44  edge_vector_map(edge_vector_map), nn(nn)
45  {
46  }
47 };
48 
75 template <class RoadmapArgs>
76 class Roadmap
77 {
78 public:
79  const std::string name;
80  const ompl::base::StateSpacePtr space;
81  const size_t max_batches; // 0 means inf
82  bool initialized;
83  size_t num_batches_generated; // should be incremented by implementation's generate()
84 
85  typename RoadmapArgs::Graph & g;
86  typename RoadmapArgs::VState state_map;
87  typename RoadmapArgs::EDistance distance_map;
88  typename RoadmapArgs::VBatch vertex_batch_map;
89  typename RoadmapArgs::EBatch edge_batch_map;
90  typename RoadmapArgs::VShadow is_shadow_map;
91  typename RoadmapArgs::EVector edge_vector_map;
92  typename RoadmapArgs::NN * nn; // ompl nn-like object, will call add() and nearestR()
93 
94  ompl::base::ParamSet params;
95 
96  Roadmap(RoadmapArgs & args, std::string name, size_t max_batches):
97  name(name),
98  space(args.space),
99  max_batches(max_batches),
100  initialized(false),
101  num_batches_generated(0),
102  g(args.g),
103  state_map(args.state_map),
104  distance_map(args.distance_map),
105  vertex_batch_map(args.vertex_batch_map),
106  edge_batch_map(args.edge_batch_map),
107  is_shadow_map(args.is_shadow_map),
108  edge_vector_map(args.edge_vector_map),
109  nn(args.nn)
110  {
111  }
112  virtual ~Roadmap() {}
113 
114  template<class T>
115  T* as()
116  {
117  BOOST_CONCEPT_ASSERT((boost::Convertible<T*, Roadmap*>));
118  return static_cast<T*>(this);
119  }
120 
121  template<class T>
122  const T* as() const
123  {
124  BOOST_CONCEPT_ASSERT((boost::Convertible<T*, Roadmap*>));
125  return static_cast<const T*>(this);
126  }
127 
128  template<typename T, typename RoadmapType, typename SetterType, typename GetterType>
129  void declareParam(const std::string &name, const RoadmapType &roadmap, const SetterType& setter, const GetterType& getter, const std::string &rangeSuggestion = "")
130  {
131  params.declareParam<T>(name, boost::bind(setter, roadmap, _1), boost::bind(getter, roadmap));
132  if (!rangeSuggestion.empty())
133  params[name].setRangeSuggestion(rangeSuggestion);
134  }
135 
136  template<typename T, typename RoadmapType, typename SetterType>
137  void declareParam(const std::string &name, const RoadmapType &roadmap, const SetterType& setter, const std::string &rangeSuggestion = "")
138  {
139  params.declareParam<T>(name, boost::bind(setter, roadmap, _1));
140  if (!rangeSuggestion.empty())
141  params[name].setRangeSuggestion(rangeSuggestion);
142  }
143 
147  virtual void initialize() = 0;
148 
154  virtual double root_radius(std::size_t i_batch) = 0;
155 
159  virtual void deserialize(const std::string & ser_data) = 0;
160 
165  virtual void generate() = 0;
166 
170  virtual void serialize(std::string & ser_data) = 0;
171 };
172 
173 template <class RoadmapArgs, template<class> class RoadmapTemplate>
175 {
176  ompl_lemur::Roadmap<RoadmapArgs> * operator()(RoadmapArgs args) const
177  {
178  return new RoadmapTemplate<RoadmapArgs>(args);
179  };
180 };
181 
182 template <class RoadmapArgs>
183 std::string roadmap_id(const ompl_lemur::Roadmap<RoadmapArgs> * roadmap)
184 {
185  std::string roadmap_id;
186 
187  roadmap_id += "type=" + roadmap->name;
188 
189  std::map<std::string, std::string> roadmap_params;
190  roadmap->params.getParams(roadmap_params);
191 
192  for (std::map<std::string, std::string>::iterator
193  it=roadmap_params.begin(); it!=roadmap_params.end(); it++)
194  {
195  roadmap_id += " " + it->first + "=" + it->second;
196  }
197 
198  return roadmap_id;
199 }
200 
201 
202 #if 0
203 template <class Graph, class VState>
204 class NNOmplBatched
205 {
206 public:
207  typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
208  Graph & g;
209  VState state_map;
210  const ompl::base::StateSpacePtr space;
212  NNOmplBatched(Graph & g, VState state_map,
213  const ompl::base::StateSpacePtr space,
215  g(g), state_map(state_map), space(space), ompl_nn(ompl_nn)
216  {
217  }
218  inline void nearestR(Vertex v_new, double radius, std::vector< std::pair<Vertex,double> > & vrads_near)
219  {
220  //printf("nearestR NNOmplBatched ...\n");
221  vrads_near.clear();
222  // add in from ompl_nn object
223  std::vector<Vertex> vs_near;
224  ompl_nn->nearestR(v_new, radius, vs_near);
225  for (unsigned int ui=0; ui<vs_near.size(); ui++)
226  {
227  if (vs_near[ui] == v_new)
228  continue;
229  double dist = space->distance(
230  get(state_map, v_new)->state,
231  get(state_map, vs_near[ui])->state);
232  vrads_near.push_back(std::make_pair(vs_near[ui],dist));
233  }
234  //printf(" found %lu from ompl_nn.\n", vrads_near.size());
235  // add in additionals since last sync
236  for (unsigned int ui=ompl_nn->size(); ui<num_vertices(g); ui++)
237  {
238  Vertex v_other = vertex(ui, g);
239  if (v_other == v_new)
240  continue;
241  double dist = this->space->distance(
242  get(state_map, v_new)->state,
243  get(state_map, v_other)->state);
244  if (radius < dist)
245  continue;
246  vrads_near.push_back(std::make_pair(v_other,dist));
247  }
248  //printf(" found %lu total.\n", vrads_near.size());
249  }
250  void sync()
251  {
252  printf("syncing NNOmplBatched ...\n");
253  for (unsigned int ui=ompl_nn->size(); ui<num_vertices(g); ui++)
254  ompl_nn->add(vertex(ui,g));
255  }
256 };
257 #endif
258 
259 } // namespace ompl_lemur
virtual void generate()=0
Generates one additional batch.
Interface for generating roadmaps over OMPL state spaces into Boost Graph objects.
Definition: Roadmap.h:76
virtual void clear()=0
void getParams(std::map< std::string, std::string > &params) const
void declareParam(const std::string &name, const typename SpecificParam< T >::SetterFn &setter, const typename SpecificParam< T >::GetterFn &getter=typename SpecificParam< T >::GetterFn())
virtual void initialize()=0
Initialize roadmap; must be called once after setting parameters.
virtual void deserialize(const std::string &ser_data)=0
Re-constitute the internal generator state from serialized data.
virtual double root_radius(std::size_t i_batch)=0
Calcuate the root radius to be used for connecting to potential root vertices.
Definition: Roadmap.h:174
virtual void serialize(std::string &ser_data)=0
Serialize the internal generator state into the passed string.
Definition: Roadmap.h:11