costmap_tools.h
Go to the documentation of this file.
1 #ifndef COSTMAP_TOOLS_H_
2 #define COSTMAP_TOOLS_H_
3 
5 #include <geometry_msgs/PointStamped.h>
6 #include <geometry_msgs/PolygonStamped.h>
7 #include <ros/ros.h>
8 
10 {
18 std::vector<unsigned int> nhood4(unsigned int idx,
19  const costmap_2d::Costmap2D& costmap)
20 {
21  // get 4-connected neighbourhood indexes, check for edge of map
22  std::vector<unsigned int> out;
23 
24  unsigned int size_x_ = costmap.getSizeInCellsX(),
25  size_y_ = costmap.getSizeInCellsY();
26 
27  if (idx > size_x_ * size_y_ - 1) {
28  ROS_WARN("Evaluating nhood for offmap point");
29  return out;
30  }
31 
32  if (idx % size_x_ > 0) {
33  out.push_back(idx - 1);
34  }
35  if (idx % size_x_ < size_x_ - 1) {
36  out.push_back(idx + 1);
37  }
38  if (idx >= size_x_) {
39  out.push_back(idx - size_x_);
40  }
41  if (idx < size_x_ * (size_y_ - 1)) {
42  out.push_back(idx + size_x_);
43  }
44  return out;
45 }
46 
54 std::vector<unsigned int> nhood8(unsigned int idx,
55  const costmap_2d::Costmap2D& costmap)
56 {
57  // get 8-connected neighbourhood indexes, check for edge of map
58  std::vector<unsigned int> out = nhood4(idx, costmap);
59 
60  unsigned int size_x_ = costmap.getSizeInCellsX(),
61  size_y_ = costmap.getSizeInCellsY();
62 
63  if (idx > size_x_ * size_y_ - 1) {
64  return out;
65  }
66 
67  if (idx % size_x_ > 0 && idx >= size_x_) {
68  out.push_back(idx - 1 - size_x_);
69  }
70  if (idx % size_x_ > 0 && idx < size_x_ * (size_y_ - 1)) {
71  out.push_back(idx - 1 + size_x_);
72  }
73  if (idx % size_x_ < size_x_ - 1 && idx >= size_x_) {
74  out.push_back(idx + 1 - size_x_);
75  }
76  if (idx % size_x_ < size_x_ - 1 && idx < size_x_ * (size_y_ - 1)) {
77  out.push_back(idx + 1 + size_x_);
78  }
79 
80  return out;
81 }
82 
91 bool nearestCell(unsigned int& result, unsigned int start, unsigned char val,
92  const costmap_2d::Costmap2D& costmap)
93 {
94  const unsigned char* map = costmap.getCharMap();
95  const unsigned int size_x = costmap.getSizeInCellsX(),
96  size_y = costmap.getSizeInCellsY();
97 
98  if (start >= size_x * size_y) {
99  return false;
100  }
101 
102  // initialize breadth first search
103  std::queue<unsigned int> bfs;
104  std::vector<bool> visited_flag(size_x * size_y, false);
105 
106  // push initial cell
107  bfs.push(start);
108  visited_flag[start] = true;
109 
110  // search for neighbouring cell matching value
111  while (!bfs.empty()) {
112  unsigned int idx = bfs.front();
113  bfs.pop();
114 
115  // return if cell of correct value is found
116  if (map[idx] == val) {
117  result = idx;
118  return true;
119  }
120 
121  // iterate over all adjacent unvisited cells
122  for (unsigned nbr : nhood8(idx, costmap)) {
123  if (!visited_flag[nbr]) {
124  bfs.push(nbr);
125  visited_flag[nbr] = true;
126  }
127  }
128  }
129 
130  return false;
131 }
132 }
133 #endif
std::vector< unsigned int > nhood4(unsigned int idx, const costmap_2d::Costmap2D &costmap)
Determine 4-connected neighbourhood of an input cell, checking for map edges.
Definition: costmap_tools.h:18
#define ROS_WARN(...)
bool nearestCell(unsigned int &result, unsigned int start, unsigned char val, const costmap_2d::Costmap2D &costmap)
Find nearest cell of a specified value.
Definition: costmap_tools.h:91
unsigned char * getCharMap() const
std::vector< unsigned int > nhood8(unsigned int idx, const costmap_2d::Costmap2D &costmap)
Determine 8-connected neighbourhood of an input cell, checking for map edges.
Definition: costmap_tools.h:54
unsigned int getSizeInCellsY() const
unsigned int getSizeInCellsX() const


explore
Author(s): Jiri Horner
autogenerated on Mon Jun 10 2019 13:56:49