Go to the documentation of this file.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
00031
00032
00033
00034 #include <costmap_queue/costmap_queue.h>
00035 #include <algorithm>
00036 #include <vector>
00037
00038 namespace costmap_queue
00039 {
00040
00041 CostmapQueue::CostmapQueue(nav_core2::Costmap& costmap, bool manhattan) :
00042 MapBasedQueue(false), costmap_(costmap), seen_(0), manhattan_(manhattan), cached_max_distance_(-1)
00043 {
00044 reset();
00045 }
00046
00047 void CostmapQueue::reset()
00048 {
00049 seen_.setInfo(costmap_.getInfo());
00050 seen_.reset();
00051 computeCache();
00052 MapBasedQueue::reset();
00053 }
00054
00055 void CostmapQueue::enqueueCell(unsigned int x, unsigned int y)
00056 {
00057 enqueueCell(x, y, x, y);
00058 }
00059
00060 void CostmapQueue::enqueueCell(unsigned int cur_x, unsigned int cur_y,
00061 unsigned int src_x, unsigned int src_y)
00062 {
00063 if (seen_(cur_x, cur_y)) return;
00064
00065
00066 double distance = distanceLookup(cur_x, cur_y, src_x, src_y);
00067 CellData data(distance, cur_x, cur_y, src_x, src_y);
00068 if (validCellToQueue(data))
00069 {
00070 seen_.setValue(cur_x, cur_y, 1);
00071 enqueue(distance, data);
00072 }
00073 }
00074
00075 CellData CostmapQueue::getNextCell()
00076 {
00077
00078 CellData current_cell = front();
00079 pop();
00080
00081 unsigned int mx = current_cell.x_;
00082 unsigned int my = current_cell.y_;
00083 unsigned int sx = current_cell.src_x_;
00084 unsigned int sy = current_cell.src_y_;
00085
00086
00087 if (mx > 0)
00088 enqueueCell(mx - 1, my, sx, sy);
00089 if (my > 0)
00090 enqueueCell(mx, my - 1, sx, sy);
00091 if (mx < costmap_.getWidth() - 1)
00092 enqueueCell(mx + 1, my, sx, sy);
00093 if (my < costmap_.getHeight() - 1)
00094 enqueueCell(mx, my + 1, sx, sy);
00095
00096 return current_cell;
00097 }
00098
00099 void CostmapQueue::computeCache()
00100 {
00101 int max_distance = getMaxDistance();
00102 if (max_distance == cached_max_distance_) return;
00103 cached_distances_.clear();
00104
00105 cached_distances_.resize(max_distance + 2);
00106
00107 for (unsigned int i = 0; i < cached_distances_.size(); ++i)
00108 {
00109 cached_distances_[i].resize(max_distance + 2);
00110 for (unsigned int j = 0; j < cached_distances_[i].size(); ++j)
00111 {
00112 if (manhattan_)
00113 {
00114 cached_distances_[i][j] = i + j;
00115 }
00116 else
00117 {
00118 cached_distances_[i][j] = hypot(i, j);
00119 }
00120 }
00121 }
00122 cached_max_distance_ = max_distance;
00123 }
00124
00125 }