37 #include <pcl/io/pcd_io.h>
43 minMapSize_(
Parameters::defaultGridGlobalMinSize()),
45 footprintRadius_(
Parameters::defaultGridGlobalFootprintRadius())
54 void OccupancyGrid::setMap(
const cv::Mat & map,
float xMin,
float yMin,
float cellSize,
const std::map<int, Transform> & poses)
56 UDEBUG(
"map=%d/%d xMin=%f yMin=%f cellSize=%f poses=%d",
57 map.cols, map.rows, xMin, yMin, cellSize, (
int)poses.size());
59 if(!poses.empty() && !map.empty())
64 mapInfo_ = cv::Mat::zeros(map.size(), CV_32FC4);
84 for(std::map<int, Transform>::const_iterator
iter=poses.lower_bound(1);
iter!=poses.end(); ++
iter)
110 map = cv::Mat(map.size(), map.type());
112 for(
int i=0;
i<map.rows; ++
i)
114 for(
int j=0;
j<map.cols; ++
j)
119 map.at<
char>(
i,
j) = -1;
121 else if(
info[3] >= occThr)
123 map.at<
char>(
i,
j) = 100;
127 map.at<
char>(
i,
j) = 0;
134 if(
erode_ && !map.empty())
137 UDEBUG(
"Eroding map = %fs",
t.ticks());
151 for(
int i=0;
i<map.rows; ++
i)
153 for(
int j=0;
j<map.cols; ++
j)
158 map.at<
char>(
i,
j) = -1;
169 UWARN(
"Map info is empty, cannot generate probabilistic occupancy grid");
185 std::map<int, cv::Mat> emptyLocalMaps;
186 std::map<int, cv::Mat> occupiedLocalMaps;
195 undefinedSize =
false;
198 for(std::list<std::pair<int, Transform> >::const_iterator
iter = newPoses.begin();
iter!=newPoses.end(); ++
iter)
202 float x =
iter->second.x();
203 float y =
iter->second.y();
208 undefinedSize =
false;
226 UDEBUG(
"Updating from cache");
227 for(std::list<std::pair<int, Transform> >::const_iterator
iter = newPoses.begin();
iter!=newPoses.end(); ++
iter)
249 const float * vi = localGrid.
groundCells.ptr<
float>(0,
i);
250 float * vo = ground.ptr<
float>(0,
i);
264 else if(maxX < vo[0])
269 else if(maxY < vo[1])
279 UFATAL(
"Occupancy local maps should be 1 row and X cols! (rows=%d cols=%d)", localGrid.
emptyCells.rows, localGrid.
emptyCells.cols);
283 const float * vi = localGrid.
emptyCells.ptr<
float>(0,
i);
284 float * vo = ground.ptr<
float>(0,
i+localGrid.
groundCells.cols);
298 else if(maxX < vo[0])
303 else if(maxY < vo[1])
307 uInsert(emptyLocalMaps, std::make_pair(
iter->first, ground));
316 cv::Mat obstacles(1, localGrid.
obstacleCells.cols, CV_32FC2);
317 for(
int i=0;
i<obstacles.cols; ++
i)
320 float * vo = obstacles.ptr<
float>(0,
i);
334 else if(maxX < vo[0])
339 else if(maxY < vo[1])
342 uInsert(occupiedLocalMaps, std::make_pair(
iter->first, obstacles));
350 if(minX != maxX && minY != maxY)
353 float xMin = minX-margin;
355 float yMin = minY-margin;
357 float xMax = maxX+margin;
358 float yMax = maxY+margin;
363 UERROR(
"Large map size!! map min=(%f, %f) max=(%f,%f). "
364 "There's maybe an error with the poses provided! The map will not be created!",
365 xMin, yMin, xMax, yMax);
374 map = cv::Mat::ones(newMapSize, CV_8S)*-1;
375 mapInfo = cv::Mat::zeros(newMapSize, CV_32FC4);
380 newMapSize.width ==
map_.cols &&
381 newMapSize.height ==
map_.rows)
410 UDEBUG(
"deltaX=%d, deltaY=%d", deltaX, deltaY);
411 newMapSize.width = (xMax - xMin) /
cellSize_+0.5
f;
412 newMapSize.height = (yMax - yMin) /
cellSize_+0.5
f;
413 UDEBUG(
"%d/%d -> %d/%d",
map_.cols,
map_.rows, newMapSize.width, newMapSize.height);
414 UASSERT(newMapSize.width >=
map_.cols && newMapSize.height >=
map_.rows);
415 UASSERT(newMapSize.width >=
map_.cols+deltaX && newMapSize.height >=
map_.rows+deltaY);
416 UASSERT(deltaX>=0 && deltaY>=0);
417 map = cv::Mat::ones(newMapSize, CV_8S)*-1;
418 mapInfo = cv::Mat::zeros(newMapSize,
mapInfo_.type());
419 map_.copyTo(map(cv::Rect(deltaX, deltaY,
map_.cols,
map_.rows)));
423 UASSERT(map.cols == mapInfo.cols && map.rows == mapInfo.rows);
424 UDEBUG(
"map %d %d", map.cols, map.rows);
427 UDEBUG(
"first pose= %d last pose=%d", newPoses.begin()->first, newPoses.rbegin()->first);
429 for(std::list<std::pair<int, Transform> >::const_iterator kter = newPoses.begin(); kter!=newPoses.end(); ++kter)
431 std::map<int, cv::Mat >::iterator
iter = emptyLocalMaps.find(kter->first);
432 std::map<int, cv::Mat >::iterator jter = occupiedLocalMaps.find(kter->first);
433 if(
iter != emptyLocalMaps.end() || jter!=occupiedLocalMaps.end())
437 if(cter ==
cellCount_.end() && kter->first > 0)
439 cter =
cellCount_.insert(std::make_pair(kter->first, std::pair<int,int>(0,0))).first;
441 if(
iter!=emptyLocalMaps.end())
443 for(
int i=0;
i<
iter->second.cols; ++
i)
445 float * ptf =
iter->second.
ptr<
float>(0,
i);
447 UASSERT_MSG(pt.y >=0 && pt.y < map.rows && pt.x >= 0 && pt.x < map.cols,
448 uFormat(
"%d: pt=(%d,%d) map=%dx%d rawPt=(%f,%f) xMin=%f yMin=%f channels=%dvs%d",
449 kter->first, pt.x, pt.y, map.cols, map.rows, ptf[0], ptf[1], xMin, yMin,
iter->second.channels(), mapInfo.channels()-1).c_str());
450 char &
value = map.at<
char>(pt.y, pt.x);
453 float *
info = mapInfo.ptr<
float>(pt.y, pt.x);
457 if(kter->first > 0 && (kter->first < nodeId || nodeId < 0))
468 eter->second.first -= 1;
470 else if(
value == 100)
472 eter->second.second -= 1;
476 eter->second.first += 1;
485 cter->second.first+=1;
490 if(nodeId != kter->first)
513 if(ptEnd.x >= map.cols)
514 ptEnd.x = map.cols-1;
518 if(ptEnd.y >= map.rows)
519 ptEnd.y = map.rows-1;
521 for(
int i=ptBegin.x;
i<ptEnd.x; ++
i)
523 for(
int j=ptBegin.y;
j<ptEnd.y; ++
j)
526 char &
value = map.at<
char>(
j,
i);
527 float *
info = mapInfo.ptr<
float>(
j,
i);
531 if(kter->first > 0 && (kter->first < nodeId || nodeId < 0))
542 eter->second.first -= 1;
544 else if(
value == 100)
546 eter->second.second -= 1;
550 eter->second.first += 1;
560 cter->second.first+=1;
567 if(jter!=occupiedLocalMaps.end())
569 for(
int i=0;
i<jter->second.cols; ++
i)
571 float * ptf = jter->second.ptr<
float>(0,
i);
573 UASSERT_MSG(pt.y>=0 && pt.y < map.rows && pt.x>=0 && pt.x < map.cols,
574 uFormat(
"%d: pt=(%d,%d) map=%dx%d rawPt=(%f,%f) xMin=%f yMin=%f channels=%dvs%d",
575 kter->first, pt.x, pt.y, map.cols, map.rows, ptf[0], ptf[1], xMin, yMin, jter->second.channels(), mapInfo.channels()-1).c_str());
576 char &
value = map.at<
char>(pt.y, pt.x);
579 float *
info = mapInfo.ptr<
float>(pt.y, pt.x);
583 if(kter->first > 0 && (kter->first < nodeId || nodeId < 0))
594 eter->second.first -= 1;
596 else if(
value == 100)
598 eter->second.second -= 1;
602 eter->second.second += 1;
611 cter->second.second+=1;
615 if(nodeId != kter->first ||
value!=100)
637 for(
int i=1;
i<map.rows-1; ++
i)
639 for(
int j=1;
j<map.cols-1; ++
j)
641 char &
value = map.at<
char>(
i,
j);
659 if(
iter->second.first == 0 &&
iter->second.second == 0)
671 UDEBUG(
"Occupancy Grid update time = %f s",
timer.ticks());
678 memoryUsage +=
map_.total() *
map_.elemSize();
680 memoryUsage +=
cellCount_.size()*(
sizeof(
int)*3 +
sizeof(std::pair<int, int>) +
sizeof(std::map<int, std::pair<int, int> >
::iterator)) +
sizeof(std::map<int, std::pair<int, int> >);