38 #include <opencv2/photo.hpp>
39 #include <grid_map_core/iterators/GridMapIterator.hpp>
40 #include <pcl/io/pcd_io.h>
42 #include <grid_map_core/GridMap.hpp>
49 minMapSize_(
Parameters::defaultGridGlobalMinSize())
68 return toImage(
"elevation", xMin, yMin, cellSize);
73 return toImage(
"colors", xMin, yMin, cellSize);
76 cv::Mat
GridMap::toImage(
const std::string & layer,
float & xMin,
float & yMin,
float & cellSize)
const
80 const grid_map::Matrix&
data = (*gridMap_)[layer];
83 if(layer.compare(
"elevation") == 0)
85 image = cv::Mat::zeros(
gridMap_->getSize()(1),
gridMap_->getSize()(0), CV_32FC1);
88 const float&
value =
data(index(0), index(1));
92 image.at<
float>(image.rows-1-imageIndex(1), image.cols-1-imageIndex(0)) =
value;
96 else if(layer.compare(
"colors") == 0)
98 image = cv::Mat::zeros(
gridMap_->getSize()(1),
gridMap_->getSize()(0), CV_8UC3);
101 const float&
value =
data(index(0), index(1));
105 const int * ptr = (
const int *)&
value;
106 cv::Vec3b & color = image.at<cv::Vec3b>(image.rows-1-imageIndex(1), image.cols-1-imageIndex(0));
107 color[0] = (
unsigned char)(*ptr & 0xFF);
108 color[1] = (
unsigned char)((*ptr >> 8) & 0xFF);
109 color[2] = (
unsigned char)((*ptr >> 16) & 0xFF);
115 UFATAL(
"Unknown layer \"%s\"", layer.c_str());
120 cellSize =
gridMap_->getResolution();
130 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZRGB>);
133 const grid_map::Matrix& dataElevation = (*gridMap_)[
"elevation"];
134 const grid_map::Matrix& dataColors = (*gridMap_)[
"colors"];
136 cloud->width =
gridMap_->getSize()(0);
137 cloud->height =
gridMap_->getSize()(1);
138 cloud->resize(cloud->width * cloud->height);
139 cloud->is_dense =
false;
143 float cellSize =
gridMap_->getResolution();
148 const float&
value = dataElevation(index(0), index(1));
149 const int* color = (
const int*)&dataColors(index(0), index(1));
151 pcl::PointXYZRGB & pt = cloud->at(cloud->width-1-imageIndex(0), imageIndex(1));
154 pt.x = xMin + (cloud->width-1-imageIndex(0)) * cellSize;
155 pt.y = yMin + (cloud->height-1-imageIndex(1)) * cellSize;
157 pt.b = (
unsigned char)(*color & 0xFF);
158 pt.g = (
unsigned char)((*color >> 8) & 0xFF);
159 pt.r = (
unsigned char)((*color >> 16) & 0xFF);
163 pt.x = pt.y = pt.z = std::numeric_limits<float>::quiet_NaN();
172 pcl::PolygonMesh::Ptr mesh(
new pcl::PolygonMesh);
182 pcl::toPCLPointCloud2(*cloud, mesh->cloud);
199 std::map<int, cv::Mat> occupiedLocalMaps;
208 undefinedSize =
false;
211 for(std::list<std::pair<int, Transform> >::const_iterator
iter = newPoses.begin();
iter!=newPoses.end(); ++
iter)
215 float x =
iter->second.x();
216 float y =
iter->second.y();
221 undefinedSize =
false;
239 UDEBUG(
"Updating from cache");
240 for(std::list<std::pair<int, Transform> >::const_iterator
iter = newPoses.begin();
iter!=newPoses.end(); ++
iter)
246 if(!localGrid.
is3D())
248 UWARN(
"It seems the local occupancy grids are not 3d, cannot update GridMap! (ground type=%d, obstacles type=%d, empty type=%d)",
269 const float * vi = localGrid.
groundCells.ptr<
float>(0,
i);
270 float * vo = occupied.ptr<
float>(0,
i);
290 else if(maxX < vo[0])
295 else if(maxY < vo[1])
310 float * vo = occupied.ptr<
float>(0,
i+localGrid.
groundCells.cols);
330 else if(maxX < vo[0])
335 else if(maxY < vo[1])
339 uInsert(occupiedLocalMaps, std::make_pair(
iter->first, occupied));
344 if(minX != maxX && minY != maxY)
347 float xMin = minX-margin;
349 float yMin = minY-margin;
351 float xMax = maxX+margin;
352 float yMax = maxY+margin;
357 UERROR(
"Large map size!! map min=(%f, %f) max=(%f,%f). "
358 "There's maybe an error with the poses provided! The map will not be created!",
359 xMin, yMin, xMax, yMax);
368 grid_map::Length
length = grid_map::Length(xMax - xMin, yMax - yMin);
369 grid_map::Position
position = grid_map::Position((xMax+xMin)/2.0
f, (yMax+yMin)/2.0
f);
378 gridMap_->setBasicLayers({
"elevation"});
383 newMapSize.width ==
gridMap_->getSize()[0] &&
384 newMapSize.height ==
gridMap_->getSize()[1])
411 UDEBUG(
"deltaX=%d, deltaY=%d", deltaX, deltaY);
412 newMapSize.width = (xMax - xMin) /
cellSize_+0.5
f;
413 newMapSize.height = (yMax - yMin) /
cellSize_+0.5
f;
414 UDEBUG(
"%d/%d -> %d/%d",
gridMap_->getSize()[0],
gridMap_->getSize()[1], newMapSize.width, newMapSize.height);
416 UASSERT(newMapSize.width >=
gridMap_->getSize()[0]+deltaX && newMapSize.height >=
gridMap_->getSize()[1]+deltaY);
417 UASSERT(deltaX>=0 && deltaY>=0);
419 grid_map::Length
length = grid_map::Length(xMax - xMin, yMax - yMin);
420 grid_map::Position
position = grid_map::Position((xMax+xMin)/2.0
f, (yMax+yMin)/2.0
f);
422 grid_map::GridMap tmpExtendedMap;
425 UDEBUG(
"%d/%d -> %d/%d",
gridMap_->getSize()[0],
gridMap_->getSize()[1], tmpExtendedMap.getSize()[0], tmpExtendedMap.getSize()[1]);
427 UDEBUG(
"extendToInclude (%f,%f,%f,%f) -> (%f,%f,%f,%f)",
430 tmpExtendedMap.getLength()[0], tmpExtendedMap.getLength()[1],
431 tmpExtendedMap.getPosition()[0], tmpExtendedMap.getPosition()[1]);
432 if(!
gridMap_->extendToInclude(tmpExtendedMap))
434 UERROR(
"Failed to update size of the grid map");
442 UDEBUG(
"first pose= %d last pose=%d", newPoses.begin()->first, newPoses.rbegin()->first);
444 grid_map::Matrix& gridMapData = (*gridMap_)[
"elevation"];
445 grid_map::Matrix& gridMapNodeIds = (*gridMap_)[
"node_ids"];
446 grid_map::Matrix& gridMapColors = (*gridMap_)[
"colors"];
447 for(std::list<std::pair<int, Transform> >::const_iterator kter = newPoses.begin(); kter!=newPoses.end(); ++kter)
449 std::map<int, cv::Mat>::iterator
iter = occupiedLocalMaps.find(kter->first);
450 if(
iter!=occupiedLocalMaps.end())
454 for(
int i=0;
i<
iter->second.cols; ++
i)
456 float * ptf =
iter->second.
ptr<
float>(0,
i);
457 grid_map::Position
position(ptf[0], ptf[1]);
464 gridMapData(index(0), index(1)) = ptf[2];
465 gridMapNodeIds(index(0), index(1)) = kter->first;
466 gridMapColors(index(0), index(1)) = ptf[3];
470 if ((gridMapData(index(0), index(1)) < ptf[2] && (gridMapNodeIds(index(0), index(1)) <= kter->first || kter->first == -1)) ||
471 gridMapNodeIds(index(0), index(1)) < kter->first)
473 gridMapData(index(0), index(1)) = ptf[2];
474 gridMapNodeIds(index(0), index(1)) = kter->first;
475 gridMapColors(index(0), index(1)) = ptf[3];
481 UERROR(
"Outside map!? (%d) (%f,%f) -> (%d,%d)",
i, ptf[0], ptf[1], index[0], index[1]);
491 UDEBUG(
"Occupancy Grid update time = %f s",
timer.ticks());