LEMUR Packages: ompl_lemur or_lemur pr_bgl prpy_lemur
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Groups Pages
RoadmapFromFile.h
Go to the documentation of this file.
1 
7 namespace ompl_lemur
8 {
9 
10 template <class RoadmapArgs>
11 class RoadmapFromFile : public Roadmap<RoadmapArgs>
12 {
13  typedef boost::graph_traits<typename RoadmapArgs::Graph> GraphTypes;
14  typedef typename GraphTypes::vertex_descriptor Vertex;
15  typedef typename GraphTypes::vertex_iterator VertexIter;
16  typedef typename GraphTypes::edge_descriptor Edge;
17  typedef typename GraphTypes::edge_iterator EdgeIter;
18 
19  // params
20  std::string _filename;
21  double _root_radius;
22 
23 public:
25  Roadmap<RoadmapArgs>(args, "FromFile", 1),
26  _filename(""),
27  _root_radius(0.0)
28  {
29  // check that we're in a real vector state space
30  if (this->space->getType() != ompl::base::STATE_SPACE_REAL_VECTOR)
31  throw std::runtime_error("RoadmapFromFile only supports rel vector state spaces!");
32 
33  this->template declareParam<std::string>("filename", this,
34  &RoadmapFromFile::setFilename,
35  &RoadmapFromFile::getFilename);
36  this->template declareParam<double>("root_radius", this,
37  &RoadmapFromFile::setRootRadius,
38  &RoadmapFromFile::getRootRadius);
39  }
40 
41  void setFilename(std::string filename)
42  {
43  if (filename == _filename)
44  return;
45  if (this->initialized)
46  throw std::runtime_error("cannot set filename, already initialized!");
47  _filename = filename;
48  }
49 
50  std::string getFilename() const
51  {
52  return _filename;
53  }
54 
55  void setRootRadius(double root_radius)
56  {
57  if (root_radius == _root_radius)
58  return;
59  if (this->initialized)
60  throw std::runtime_error("cannot set root_radius, already initialized!");
61  _root_radius = root_radius;
62  }
63 
64  double getRootRadius() const
65  {
66  return _root_radius;
67  }
68 
69  void initialize()
70  {
71  std::vector<std::string> missings;
72  if (_filename == "")
73  missings.push_back("filename");
74  if (_root_radius == 0.0)
75  missings.push_back("root_radius");
76  if (missings.size())
77  {
78  std::string str = "Cannot initialize, parameters not set:";
79  for (unsigned int ui=0; ui<missings.size(); ui++)
80  str += " " + missings[ui];
81  throw std::runtime_error(str);
82  }
83  this->initialized = true;
84  }
85 
86  void deserialize(const std::string & ser_data)
87  {
88  throw std::runtime_error("RoadmapFromFile deserialize from ser_data not supported!");
89  }
90 
91  // should be stateless
92  double root_radius(std::size_t i_batch)
93  {
94  return _root_radius;
95  }
96 
97  // sets all of these maps
98  // generates one additional batch
99  void generate()
100  {
101  if (this->max_batches < this->num_batches_generated + 1)
102  throw std::runtime_error("this roadmap gen doesnt support that many batches!");
103 
104  std::ifstream fp;
105  fp.open(_filename.c_str());
106 
107  boost::dynamic_properties props;
108  const ompl::base::StateSpacePtr & myspace = this->space;
109  props.property("state",
110  ompl_lemur::make_rvstate_map_string_adaptor(
111  this->state_map,
112  myspace->as<ompl::base::RealVectorStateSpace>()));
113  boost::read_graphml(fp, this->g, props);
114 
115  VertexIter vi, vi_end;
116  for (boost::tie(vi,vi_end)=vertices(this->g); vi!=vi_end; ++vi)
117  {
118  put(this->vertex_batch_map, *vi, 0);
119  put(this->is_shadow_map, *vi, false);
120  this->nn->add(*vi);
121  }
122 
123  EdgeIter ei, ei_end;
124  for (boost::tie(ei,ei_end)=edges(this->g); ei!=ei_end; ++ei)
125  {
126  put(this->edge_batch_map, *ei, 0);
127  ompl::base::State * state1 = get(this->state_map, source(*ei,this->g));
128  ompl::base::State * state2 = get(this->state_map, target(*ei,this->g));
129  put(this->distance_map, *ei, this->space->distance(state1, state2));
130  }
131 
132  this->num_batches_generated++;
133  }
134 
135  void serialize(std::string & ser_data)
136  {
137  throw std::runtime_error("RoadmapFromFile serialize to ser_data not supported!");
138  }
139 };
140 
141 } // namespace ompl_lemur
Definition: RoadmapFromFile.h:11
void deserialize(const std::string &ser_data)
Re-constitute the internal generator state from serialized data.
Definition: RoadmapFromFile.h:86
Interface for generating roadmaps over OMPL state spaces into Boost Graph objects.
Definition: Roadmap.h:76
void generate()
Generates one additional batch.
Definition: RoadmapFromFile.h:99
void serialize(std::string &ser_data)
Serialize the internal generator state into the passed string.
Definition: RoadmapFromFile.h:135
double root_radius(std::size_t i_batch)
Calcuate the root radius to be used for connecting to potential root vertices.
Definition: RoadmapFromFile.h:92
void initialize()
Initialize roadmap; must be called once after setting parameters.
Definition: RoadmapFromFile.h:69
Definition: Roadmap.h:11