| 223 | } |
| 224 | |
| 225 | bool GridMapRosConverter::fromOccupancyGrid(const nav_msgs::OccupancyGrid& occupancyGrid, |
| 226 | const std::string& layer, grid_map::GridMap& gridMap) |
| 227 | { |
| 228 | const Size size(occupancyGrid.info.width, occupancyGrid.info.height); |
| 229 | const double resolution = occupancyGrid.info.resolution; |
| 230 | const Length length = resolution * size.cast<double>(); |
| 231 | const string& frameId = occupancyGrid.header.frame_id; |
| 232 | Position position(occupancyGrid.info.origin.position.x, occupancyGrid.info.origin.position.y); |
| 233 | // Different conventions of center of map. |
| 234 | position += 0.5 * length.matrix(); |
| 235 | |
| 236 | const auto& orientation = occupancyGrid.info.origin.orientation; |
| 237 | if (orientation.w != 1.0 && !(orientation.x == 0 && orientation.y == 0 |
| 238 | && orientation.z == 0 && orientation.w == 0)) { |
| 239 | ROS_WARN_STREAM("Conversion of occupancy grid: Grid maps do not support orientation."); |
| 240 | ROS_INFO_STREAM("Orientation of occupancy grid: " << endl << occupancyGrid.info.origin.orientation); |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | if (size.prod() != occupancyGrid.data.size()) { |
| 245 | ROS_WARN_STREAM("Conversion of occupancy grid: Size of data does not correspond to width * height."); |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | // TODO: Split to `initializeFrom` and `from` as for Costmap2d. |
| 250 | if ((gridMap.getSize() != size).any() || gridMap.getResolution() != resolution |
| 251 | || (gridMap.getLength() != length).any() || gridMap.getPosition() != position |
| 252 | || gridMap.getFrameId() != frameId || !gridMap.getStartIndex().isZero()) { |
| 253 | gridMap.setTimestamp(occupancyGrid.header.stamp.toNSec()); |
| 254 | gridMap.setFrameId(frameId); |
| 255 | gridMap.setGeometry(length, resolution, position); |
| 256 | } |
| 257 | |
| 258 | // Reverse iteration is required because of different conventions |
| 259 | // between occupancy grid and grid map. |
| 260 | grid_map::Matrix data(size(0), size(1)); |
| 261 | for (std::vector<int8_t>::const_reverse_iterator iterator = occupancyGrid.data.rbegin(); |
| 262 | iterator != occupancyGrid.data.rend(); ++iterator) { |
| 263 | size_t i = std::distance(occupancyGrid.data.rbegin(), iterator); |
| 264 | data(i) = *iterator != -1 ? *iterator : NAN; |
| 265 | } |
| 266 | |
| 267 | gridMap.add(layer, data); |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | void GridMapRosConverter::toOccupancyGrid(const grid_map::GridMap& gridMap, |
| 272 | const std::string& layer, float dataMin, float dataMax, |
nothing calls this directly
no test coverage detected