grid_map_2d.cpp
Go to the documentation of this file.
1 // SVN $HeadURL: http://alufr-ros-pkg.googlecode.com/svn/trunk/humanoid_stacks/humanoid_navigation/vigir_gridmap_2d/src/GridMap2D.cpp $
2 // SVN $Id: GridMap2D.cpp 3276 2012-09-27 12:39:16Z hornunga@informatik.uni-freiburg.de $
3 
4 /*
5  * A simple 2D gridmap structure
6  *
7  * Copyright 2011 Armin Hornung, University of Freiburg
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * * Neither the name of the University of Freiburg nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <ros/console.h>
36 
37 
38 
39 namespace vigir_gridmap_2d {
40 
42 : m_frameId("/map")
43 {
44 
45 }
46 
47 GridMap2D::GridMap2D(const nav_msgs::OccupancyGridConstPtr& gridMap)
48 {
49  setMap(gridMap);
50 }
51 
53 {
54 }
55 
56 void GridMap2D::setMap(const nav_msgs::OccupancyGridConstPtr& gridMap)
57 {
58  m_mapInfo = gridMap->info;
59  m_frameId = gridMap->header.frame_id;
60  // allocate map structs so that x/y in the world correspond to x/y in the image
61  // (=> cv::Mat is rotated by 90 deg, because it's row-major!)
62  m_binaryMap = cv::Mat(m_mapInfo.width, m_mapInfo.height, CV_8UC1);
63  m_distMap = cv::Mat(m_binaryMap.size(), CV_32FC1);
64 
65  std::vector<signed char>::const_iterator mapDataIter = gridMap->data.begin();
66 
67  //TODO check / param
68  unsigned char map_occ_thres = 70;
69 
70  // iterate over map, store in image
71  // (0,0) is lower left corner of OccupancyGrid
72  for(unsigned int j = 0; j < m_mapInfo.height; ++j){
73  for(unsigned int i = 0; i < m_mapInfo.width; ++i){
74  if (*mapDataIter > map_occ_thres){
75  // m_mapInfo.height-1-i
76  m_binaryMap.at<uchar>(i,j) = 0;
77  } else{
78  m_binaryMap.at<uchar>(i,j) = 255;
79  }
80  mapDataIter++;
81  }
82  }
83  cv::distanceTransform(m_binaryMap, m_distMap, CV_DIST_L2, CV_DIST_MASK_PRECISE);
84  // distance map now contains distance in meters:
85  m_distMap = m_distMap * m_mapInfo.resolution;
86 
87  //ROS_INFO("GridMap2D created with %d x %d cells at %f resolution.", m_mapInfo.width, m_mapInfo.height, m_mapInfo.resolution);
88 }
89 
90 void GridMap2D::setMap(const cv::Mat& binaryMap)
91 {
92  m_binaryMap = binaryMap.clone();
93  m_distMap = cv::Mat(m_binaryMap.size(), CV_32FC1);
94 
95  cv::distanceTransform(m_binaryMap, m_distMap, CV_DIST_L2, CV_DIST_MASK_PRECISE);
96  // distance map now contains distance in meters:
97  m_distMap = m_distMap * m_mapInfo.resolution;
98 
99  ROS_INFO("GridMap2D copied from existing cv::Mat with %d x %d cells at %f resolution.", m_mapInfo.width, m_mapInfo.height, m_mapInfo.resolution);
100 
101 }
102 
103 void GridMap2D::inflateMap(double inflationRadius)
104 {
105  m_binaryMap = (m_distMap <= inflationRadius );
106  // recompute distance map with new binary map:
107  cv::distanceTransform(m_binaryMap, m_distMap, CV_DIST_L2, CV_DIST_MASK_PRECISE);
108  m_distMap = m_distMap * m_mapInfo.resolution;
109 }
110 
111 // See costmap2D for mapToWorld / worldToMap implementations:
112 
113 void GridMap2D::mapToWorld(unsigned int mx, unsigned int my, double& wx, double& wy) const
114 {
115  wx = m_mapInfo.origin.position.x + (mx+0.5) * m_mapInfo.resolution;
116  wy = m_mapInfo.origin.position.y + (my+0.5) * m_mapInfo.resolution;
117 }
118 
119 void GridMap2D::worldToMapNoBounds(double wx, double wy, unsigned int& mx, unsigned int& my) const
120 {
121  mx = (int) ((wx - m_mapInfo.origin.position.x) / m_mapInfo.resolution);
122  my = (int) ((wy - m_mapInfo.origin.position.y) / m_mapInfo.resolution);
123 }
124 
125 bool GridMap2D::worldToMap(double wx, double wy, unsigned int& mx, unsigned int& my) const
126 {
127  if(wx < m_mapInfo.origin.position.x || wy < m_mapInfo.origin.position.y)
128  return false;
129 
130  mx = (int) ((wx - m_mapInfo.origin.position.x) / m_mapInfo.resolution);
131  my = (int) ((wy - m_mapInfo.origin.position.y) / m_mapInfo.resolution);
132 
133  if(mx < m_mapInfo.width && my < m_mapInfo.height)
134  return true;
135 
136  return false;
137 }
138 
139 bool GridMap2D::inMapBounds(double wx, double wy) const
140 {
141  unsigned mx, my;
142  return worldToMap(wx,wy,mx,my);
143 }
144 
145 float GridMap2D::distanceMapAt(double wx, double wy) const
146 {
147  unsigned mx, my;
148 
149  if (worldToMap(wx, wy, mx, my))
150  return m_distMap.at<float>(mx, my);
151  else
152  return -1.0f;
153 }
154 
155 uchar GridMap2D::binaryMapAt(double wx, double wy) const
156 {
157  unsigned mx, my;
158 
159  if (worldToMap(wx, wy, mx, my))
160  return m_binaryMap.at<uchar>(mx, my);
161  else
162  return 0;
163 }
164 
165 float GridMap2D::distanceMapAtCell(unsigned int mx, unsigned int my) const
166 {
167  return m_distMap.at<float>(mx, my);
168 }
169 
170 uchar GridMap2D::binaryMapAtCell(unsigned int mx, unsigned int my) const
171 {
172  return m_binaryMap.at<uchar>(mx, my);
173 }
174 
175 bool GridMap2D::isOccupiedAtCell(unsigned int mx, unsigned int my) const
176 {
177  return (m_binaryMap.at<uchar>(mx, my) < 255);
178 }
179 
180 bool GridMap2D::isOccupiedAt(double wx, double wy) const
181 {
182  unsigned mx, my;
183  if (worldToMap(wx, wy, mx, my))
184  return isOccupiedAtCell(mx, my);
185  else
186  return true;
187 }
188 }
189 
190 
float distanceMapAtCell(unsigned int mx, unsigned int my) const
Returns distance (in m) at map cell <mx, my> in m; -1 if out of bounds!
void setMap(const nav_msgs::OccupancyGridConstPtr &gridMap)
Initialize map from a ROS OccupancyGrid message.
Definition: grid_map_2d.cpp:56
bool inMapBounds(double wx, double wy) const
check if a coordinate is covered by the map extent (same as worldToMap)
void inflateMap(double inflationRaduis)
uchar binaryMapAtCell(unsigned int mx, unsigned int my) const
Returns map value at map cell <mx, my>; out of bounds will be returned as 0!
cv::Mat m_binaryMap
binary occupancy map. 255: free, 0 occupied.
Definition: grid_map_2d.h:130
std::string m_frameId
"map" frame where ROS OccupancyGrid originated from
Definition: grid_map_2d.h:133
void worldToMapNoBounds(double wx, double wy, unsigned int &mx, unsigned int &my) const
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const
#define ROS_INFO(...)
uchar binaryMapAt(double wx, double wy) const
Returns map value at world coordinates <wx, wy>; out of bounds will be returned as 0! ...
float distanceMapAt(double wx, double wy) const
Returns distance (in m) at world coordinates <wx,wy> in m; -1 if out of bounds!
bool isOccupiedAt(double wx, double wy) const
const cv::Mat & binaryMap() const
Definition: grid_map_2d.h:125
void mapToWorld(unsigned int mx, unsigned int my, double &wx, double &wy) const
bool isOccupiedAtCell(unsigned int mx, unsigned int my) const
nav_msgs::MapMetaData m_mapInfo
Definition: grid_map_2d.h:132
cv::Mat m_distMap
distance map (in meter)
Definition: grid_map_2d.h:131


vigir_footstep_planning_default_plugins
Author(s): Alexander Stumpf
autogenerated on Sun Nov 17 2019 03:30:01