LEMUR Packages: ompl_lemur or_lemur pr_bgl prpy_lemur
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Groups Pages
RoadmapAAGrid.h
Go to the documentation of this file.
1 
7 namespace ompl_lemur
8 {
9 
10 template <class RoadmapArgs>
11 class RoadmapAAGrid : public Roadmap<RoadmapArgs>
12 {
13  typedef boost::graph_traits<typename RoadmapArgs::Graph> GraphTypes;
14  typedef typename GraphTypes::vertex_descriptor Vertex;
15  typedef typename GraphTypes::edge_descriptor Edge;
16 
17  // set on construction
18  unsigned int _dim;
20 
21  // params
22  double _res;
23 
24 public:
25  RoadmapAAGrid(RoadmapArgs & args):
26  Roadmap<RoadmapArgs>(args, "AAGrid", 1),
27  _dim(0),
28  _bounds(0),
29  _res(0.0)
30  {
31  // check that we're in a real vector state space
32  if (this->space->getType() != ompl::base::STATE_SPACE_REAL_VECTOR)
33  throw std::runtime_error("RoadmapAAGrid only supports rel vector state spaces!");
34  _dim = this->space->getDimension();
35  ompl::base::StateSpacePtr myspace(this->space);
36  _bounds = myspace->as<ompl::base::RealVectorStateSpace>()->getBounds();
37 
38  this->template declareParam<double>("res", this,
39  &RoadmapAAGrid::setRes,
40  &RoadmapAAGrid::getRes);
41  }
42 
43  void setRes(double res)
44  {
45  if (res == _res)
46  return;
47  if (this->initialized)
48  throw std::runtime_error("cannot set res, already initialized!");
49  _res = res;
50  }
51 
52  double getRes() const
53  {
54  return _res;
55  }
56 
57  void initialize()
58  {
59  if (_res == 0.0)
60  throw std::runtime_error("cannot initialize, res not set!");
61  this->initialized = true;
62  }
63 
64  void deserialize(const std::string & ser_data)
65  {
66  throw std::runtime_error("RoadmapAAGrid deserialize from ser_data not supported!");
67  }
68 
69  // should be stateless
70  double root_radius(std::size_t i_batch)
71  {
72  return _res;
73  }
74 
75  // sets all of these maps
76  // generates one additional batch
77  void generate()
78  {
79  if (this->max_batches < this->num_batches_generated + 1)
80  throw std::runtime_error("this roadmap gen doesnt support that many batches!");
81  // ok, generate all nodes!
82  // first, compute offset and number of vertices per dimension
83  std::vector<double> dim_offsets;
84  std::vector<std::size_t> dim_numverts;
85  std::size_t total_numverts = 1;
86  for (std::size_t idim=0; idim<_dim; idim++)
87  {
88  double dim_len = _bounds.high[idim] - _bounds.low[idim];
89  std::size_t numverts = floor(0.5 + dim_len/_res);
90  double offset = 0.5*(dim_len - (numverts-1)*_res);
91  total_numverts *= numverts;
92  dim_numverts.push_back(numverts);
93  dim_offsets.push_back(offset);
94  printf("dim:%lu numerts:%lu offset:%f\n",
95  idim, numverts, offset);
96  }
97  // ok, add vertices, and keep all descriptors for now ...
98  std::vector<Vertex> vertices(total_numverts);
99  for (std::size_t ivert=0; ivert<total_numverts; ivert++)
100  {
101  Vertex v_new = add_vertex(this->g);
102  vertices[ivert] = v_new;
103 
104  put(this->vertex_batch_map, v_new, 0);
105  put(this->is_shadow_map, v_new, false);
106 
107  // allocate a new state for this vertex
108  put(this->state_map, v_new, this->space->allocState());
109  ompl::base::State * v_state = get(this->state_map, v_new);
110  double * values = v_state->as<ompl::base::RealVectorStateSpace::StateType>()->values;
111  this->nn->add(v_new);
112 
113  std::size_t ivert_used = ivert;
114  std::size_t dim_stride = 1;
115  for (std::size_t idim=_dim-1; ; idim--) // termination condition at end!
116  {
117  // set state components
118  // vert[x][y][z] = vert[x*(ny*nz)+y*(nz)+z]
119  std::size_t idimvert = ivert_used % dim_numverts[idim];
120  values[idim] = _bounds.low[idim] + dim_offsets[idim] + _res*idimvert;
121  // add edge to previous vertex along this dim
122  if (idimvert)
123  {
124  std::size_t ivert_dimprev = ivert - dim_stride;
125  Edge e = add_edge(vertices[ivert_dimprev], v_new, this->g).first;
126  put(this->distance_map, e, _res);
127  put(this->edge_batch_map, e, 0);
128  }
129  // continue
130  if (idim == 0) break;
131  ivert_used /= dim_numverts[idim];
132  dim_stride *= dim_numverts[idim];
133  }
134  }
135  this->num_batches_generated++;
136  }
137 
138  void serialize(std::string & ser_data)
139  {
140  throw std::runtime_error("RoadmapAAGrid serialize to ser_data not supported!");
141  }
142 };
143 
144 } // namespace ompl_lemur
std::vector< double > low
void deserialize(const std::string &ser_data)
Re-constitute the internal generator state from serialized data.
Definition: RoadmapAAGrid.h:64
Interface for generating roadmaps over OMPL state spaces into Boost Graph objects.
Definition: Roadmap.h:76
void generate()
Generates one additional batch.
Definition: RoadmapAAGrid.h:77
void serialize(std::string &ser_data)
Serialize the internal generator state into the passed string.
Definition: RoadmapAAGrid.h:138
std::vector< double > high
Definition: RoadmapAAGrid.h:11
double root_radius(std::size_t i_batch)
Calcuate the root radius to be used for connecting to potential root vertices.
Definition: RoadmapAAGrid.h:70
const T * as() const
void initialize()
Initialize roadmap; must be called once after setting parameters.
Definition: RoadmapAAGrid.h:57
Definition: Roadmap.h:11