00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00039 #include <occupancy_grid_utils/shortest_path.h>
00040 #include <ros/assert.h>
00041 #include <queue>
00042
00043 namespace occupancy_grid_utils
00044 {
00045
00046 namespace nm=nav_msgs;
00047
00048 using std::vector;
00049 using boost::optional;
00050 using std::max;
00051
00052 struct ShortestPathResult
00053 {
00054 nm::MapMetaData info;
00055 index_t src_ind;
00056 vector<optional<index_t> > back_pointers;
00057 vector<optional<double> > potential;
00058 };
00059
00060 TerminationCondition::TerminationCondition ()
00061 {}
00062
00063 TerminationCondition::TerminationCondition (const double max_distance) :
00064 max_distance_(max_distance)
00065 {}
00066
00067 TerminationCondition::TerminationCondition (const Cells& goals) :
00068 goals_(goals)
00069 {}
00070
00071 TerminationCondition::TerminationCondition (const Cells& goals, const double max_distance) :
00072 max_distance_(max_distance), goals_(goals)
00073 {}
00074
00075
00076 typedef vector<Cell> Path;
00077 typedef boost::shared_ptr<nm::OccupancyGrid> GridPtr;
00078
00079 boost::optional<double> distance(ResultPtr res, const Cell& dest)
00080 {
00081 return res->potential[cellIndex(res->info, dest)];
00082 }
00083
00084
00085
00086 boost::optional<Path> extractPath(ResultPtr res, const Cell& dest)
00087 {
00088 boost::optional<Path> p;
00089 index_t current=cellIndex(res->info, dest);
00090 if (!res->back_pointers[current])
00091 return p;
00092
00093 index_t max=res->back_pointers.size();
00094 index_t n=0;
00095 p = Path();
00096
00097 do {
00098 if (n++ > max) {
00099 ROS_FATAL("Cycle in extractPath");
00100 ROS_BREAK();
00101 }
00102 p->push_back(indexCell(res->info, current));
00103 ROS_ASSERT_MSG(res->back_pointers[current], "Back pointer not found for %u", current);
00104 current=*(res->back_pointers[current]);
00105 } while (current!=res->src_ind);
00106 p->push_back(indexCell(res->info, current));
00107 reverse(p->begin(), p->end());
00108 return p;
00109 }
00110
00111
00112
00113 struct QueueItem
00114 {
00115 index_t ind;
00116 double potential;
00117 QueueItem (const index_t ind, const double potential) :
00118 ind(ind), potential(potential) {}
00119 };
00120
00121 bool operator< (const QueueItem& i1, const QueueItem& i2)
00122 {
00123 return i1.potential > i2.potential;
00124 }
00125
00126
00127 ResultPtr singleSourceShortestPaths (const nm::OccupancyGrid& g, const Cell& src)
00128 {
00129 return singleSourceShortestPaths(g, src, TerminationCondition());
00130 }
00131
00132 ResultPtr singleSourceShortestPaths (const nm::OccupancyGrid& g, const Cell& src, const TerminationCondition& t)
00133 {
00134 ShortestPathResult* res = new ShortestPathResult();
00135 ResultPtr result(res);
00136 res->info = g.info;
00137 res->src_ind = cellIndex(g.info, src);
00138 res->back_pointers.resize(g.data.size());
00139 res->potential.resize(g.data.size());
00140
00141 std::priority_queue<QueueItem> q;
00142 q.push(QueueItem(res->src_ind, 0.0));
00143 res->potential[res->src_ind] = 0.0;
00144 ROS_DEBUG_NAMED ("shortest_path", "Computing single source shortest paths from %d, %d", src.x, src.y);
00145
00146 Cells remaining_goals;
00147 if (t.goals_)
00148 remaining_goals = *t.goals_;
00149
00150 while (!q.empty()) {
00151 const QueueItem i=q.top();
00152 q.pop();
00153
00154 const Cell cell = indexCell(g.info, i.ind);
00155 const double dist = i.potential;
00156 if (t.max_distance_ && *t.max_distance_ < dist)
00157 break;
00158 if (t.goals_) {
00159 remaining_goals.erase(cell);
00160 if (remaining_goals.empty())
00161 break;
00162 }
00163
00164 for (int dx=-1; dx<=1; dx++) {
00165 for (int dy=-1; dy<=1; dy++) {
00166 if ((dx==0) && (dy==0))
00167 continue;
00168 const double d = ((dx==0) || (dy==0)) ? 1 : sqrt(2);
00169 const Cell neighbor(cell.x+dx, cell.y+dy);
00170 if (withinBounds(g.info, neighbor)) {
00171 const index_t ind=cellIndex(g.info, neighbor);
00172 const double new_dist = dist+d;
00173 if (g.data[ind]==UNOCCUPIED &&
00174 (!res->potential[ind] || *(res->potential[ind]) > new_dist) &&
00175 (!t.max_distance_ || *t.max_distance_ >= new_dist)) {
00176 ROS_DEBUG_NAMED ("shortest_path_propagation", "Adding cell %d, %d, at distance %.2f",
00177 neighbor.x, neighbor.y, new_dist);
00178 res->potential[ind] = new_dist;
00179 res->back_pointers[ind] = i.ind;
00180 q.push(QueueItem(ind, new_dist));
00181 }
00182 }
00183 }
00184 }
00185 }
00186 ROS_DEBUG_NAMED ("shortest_path", "Done computing single source shortest paths from %d, %d", src.x, src.y);
00187 return result;
00188 }
00189
00190
00191 inline
00192 bool myGt (const signed char x, const signed char y)
00193 {
00194 return (x==-1 && y==0) || x>y;
00195 }
00196
00197 struct InflationQueueItem
00198 {
00199 InflationQueueItem (const Cell& cell, const signed char cost) :
00200 cell(cell), cost(cost)
00201 {}
00202 Cell cell;
00203 signed char cost;
00204 };
00205
00206 GridPtr inflateObstacles (const nm::OccupancyGrid& g, const double r,
00207 const bool allow_unknown)
00208 {
00209
00210 ROS_ASSERT (r>0);
00211 const int radius = ceil(r/g.info.resolution);
00212 typedef vector<InflationQueueItem> QueueVec;
00213 vector<QueueVec> queues(radius);
00214 GridPtr g2(new nm::OccupancyGrid(g));
00215 const nm::MapMetaData& geom=g.info;
00216
00217
00218 for (coord_t x=0; x<(int)geom.width; x++) {
00219 for (coord_t y=0; y<(int)geom.height; y++) {
00220 const Cell cell(x, y);
00221 const signed char val=g.data[cellIndex(geom, cell)];
00222 if ((allow_unknown && val>=1) || (!allow_unknown && val!=0))
00223 queues[0].push_back(InflationQueueItem(cell, val));
00224 }
00225 }
00226
00227 while (true)
00228 {
00229 int ind=-1;
00230 for (int i=0; i<radius; i++)
00231 {
00232 if (queues[i].size()>0)
00233 {
00234 ind=i;
00235 break;
00236 }
00237 }
00238 if (ind<0)
00239 break;
00240
00241 const InflationQueueItem& q=queues[ind].back();
00242 const index_t index = cellIndex(geom, q.cell);
00243 if (myGt(q.cost, g2->data[index]) || ind==0)
00244 {
00245 g2->data[index] = q.cost;
00246 if (ind<radius-1)
00247 {
00248 for (int vert=0; vert<2; vert ++)
00249 {
00250 for (int d=-1; d<=1; d += 2)
00251 {
00252 const int dx = vert ? 0 : d;
00253 const int dy = vert ? d : 0;
00254 const Cell c2(q.cell.x+dx, q.cell.y+dy);
00255 if (withinBounds(geom, c2))
00256 {
00257 queues[ind+1].push_back(InflationQueueItem(c2, q.cost));
00258 }
00259 }
00260 }
00261 }
00262 }
00263
00264 queues[ind].pop_back();
00265 }
00266 return g2;
00267 }
00268
00269
00270
00271
00272
00273 typedef std::pair<Path, double> AStarResult;
00274 struct PQItem
00275 {
00276 index_t ind;
00277 double g_cost;
00278 double h_cost;
00279 PQItem (index_t ind, double g_cost, double h_cost) :
00280 ind(ind), g_cost(g_cost), h_cost(h_cost) {}
00281
00282 bool operator< (const PQItem& i2) const
00283 {
00284 return ((g_cost + h_cost) > (i2.g_cost + i2.h_cost));
00285 }
00286 };
00287
00288
00289 inline double manhattanHeuristic (const Cell& c1, const Cell& c2)
00290 {
00291 return fabs(c1.x-c2.x) + fabs(c1.y-c2.y);
00292 }
00293
00294 optional<AStarResult> shortestPath(const nm::OccupancyGrid& g, const Cell& src, const Cell& dest)
00295 {
00296 std::priority_queue<PQItem> open_list;
00297 const unsigned num_cells = g.info.height*g.info.width;
00298 std::vector<bool> seen(num_cells);
00299 open_list.push(PQItem(cellIndex(g.info, src), 0, manhattanHeuristic(src, dest)));
00300 std::vector<index_t> parent(num_cells);
00301 const index_t dest_ind = cellIndex(g.info, dest);
00302
00303 optional<AStarResult> res;
00304 ROS_DEBUG_STREAM_NAMED ("shortest_path", "Computing shortest path from " << src << " to " << dest);
00305
00306 while (!open_list.empty()) {
00307 const PQItem current = open_list.top();
00308 open_list.pop();
00309 const Cell c = indexCell(g.info, current.ind);
00310 if (seen[current.ind])
00311 continue;
00312 seen[current.ind] = true;
00313 ROS_DEBUG_STREAM_NAMED ("shortest_path_internal", " Considering " << c << " with cost " <<
00314 current.g_cost << " + " << current.h_cost);
00315 if (current.ind == dest_ind) {
00316 res = AStarResult();
00317 res->second = current.g_cost;
00318
00319 break;
00320 }
00321
00322 for (int d=-1; d<=1; d+=2) {
00323 for (int vertical=0; vertical<2; vertical++) {
00324 const int cx = c.x + d*(1-vertical);
00325 const int cy = c.y + d*vertical;
00326 if (cx>=0 && cy>=0) {
00327 const Cell c2((coord_t) cx, (coord_t) cy);
00328 if (withinBounds(g.info, c2)) {
00329 const index_t ind = cellIndex(g.info, c2);
00330 if (g.data[ind]==UNOCCUPIED && !seen[ind]) {
00331 open_list.push(PQItem(ind, current.g_cost + 1, manhattanHeuristic(c2, dest)));
00332 parent[ind] = current.ind;
00333 }
00334 ROS_DEBUG_STREAM_COND_NAMED (g.data[ind]!=UNOCCUPIED, "shortest_path_internal",
00335 " Skipping cell " << indexCell(g.info, ind) <<
00336 " with cost " << (unsigned) g.data[ind]);
00337 }
00338 }
00339 }
00340 }
00341 }
00342
00343 ROS_DEBUG_STREAM_NAMED ("shortest_path", "Computed shortest path. Found = " << (bool) res.is_initialized());
00344 return res;
00345 }
00346
00347
00348
00349
00350
00351
00352
00354 ResultPtr shortestPathResultFromMessage (const NavigationFunction& msg)
00355 {
00356 ShortestPathResult* res = new ShortestPathResult();
00357 ResultPtr retval(res);
00358 res->info = msg.info;
00359 res->src_ind = msg.source;
00360
00361 const index_t num_cells = msg.valid.size();
00362 ROS_ASSERT (num_cells == msg.back_pointers.size());
00363 ROS_ASSERT (num_cells == msg.potential.size());
00364 res->back_pointers.resize(num_cells);
00365 res->potential.resize(num_cells);
00366
00367 for (index_t i=0; i<num_cells; i++) {
00368 if (i==msg.source) {
00369
00370 ROS_ASSERT (msg.back_pointers[i] == 123456);
00371 ROS_ASSERT (msg.potential[i] == -1.0);
00372 res->potential[i] = 0.0;
00373 }
00374 else if (msg.valid[i]) {
00375 res->back_pointers[i] = msg.back_pointers[i];
00376 res->potential[i] = msg.potential[i];
00377 }
00378 else {
00379
00380 ROS_ASSERT (msg.back_pointers[i] == 234567);
00381 ROS_ASSERT (msg.potential[i] == -2.0);
00382 }
00383 }
00384
00385 return retval;
00386 }
00387
00389 NavigationFunction shortestPathResultToMessage (ResultPtr res)
00390 {
00391 NavigationFunction msg;
00392 msg.info = res->info;
00393 msg.source = res->src_ind;
00394
00395 const index_t num_cells=res->back_pointers.size();
00396 ROS_ASSERT (num_cells == res->potential.size());
00397 msg.valid.resize(num_cells);
00398 msg.back_pointers.resize(num_cells);
00399 msg.potential.resize(num_cells);
00400
00401 for (index_t i=0; i<num_cells; i++) {
00402 if (i==msg.source) {
00403 msg.valid[i] = false;
00404 msg.potential[i] = -1.0;
00405 msg.back_pointers[i] = 123456;
00406 }
00407 else if (res->potential[i]) {
00408 msg.valid[i] = true;
00409 msg.potential[i] = *res->potential[i];
00410 msg.back_pointers[i] = *res->back_pointers[i];
00411 }
00412 else {
00413 msg.valid[i] = false;
00414 msg.potential[i] = -2.0;
00415 msg.back_pointers[i] = 234567;
00416 }
00417 }
00418
00419 return msg;
00420 }
00421
00422
00423
00424 }