Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
occ_grid_utils.hpp
1 // Copyright (c) 2008, 2013, Willow Garage, Inc.
2 // Copyright (c) 2023 Samsung R&D Institute Russia
3 // All rights reserved.
4 //
5 // Software License Agreement (BSD License 2.0)
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
17 // * Neither the name of the <ORGANIZATION> nor the names of its
18 // contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 // POSSIBILITY OF SUCH DAMAGE.
33 //
34 // Author: Eitan Marder-Eppstein
35 // David V. Lu!!
36 // Alexey Merzlyakov
37 
38 #ifndef NAV2_UTIL__OCC_GRID_UTILS_HPP_
39 #define NAV2_UTIL__OCC_GRID_UTILS_HPP_
40 
41 #include "nav_msgs/msg/occupancy_grid.hpp"
42 
43 namespace nav2_util
44 {
45 
56 inline bool worldToMap(
57  nav_msgs::msg::OccupancyGrid::ConstSharedPtr map,
58  const double wx, const double wy, unsigned int & mx, unsigned int & my)
59 {
60  const double origin_x = map->info.origin.position.x;
61  const double origin_y = map->info.origin.position.y;
62  const double resolution = map->info.resolution;
63  const unsigned int size_x = map->info.width;
64  const unsigned int size_y = map->info.height;
65 
66  if (wx < origin_x || wy < origin_y) {
67  return false;
68  }
69 
70  mx = static_cast<unsigned int>((wx - origin_x) / resolution);
71  my = static_cast<unsigned int>((wy - origin_y) / resolution);
72  if (mx >= size_x || my >= size_y) {
73  return false;
74  }
75 
76  return true;
77 }
78 
86 inline void mapToWorld(
87  nav_msgs::msg::OccupancyGrid::ConstSharedPtr map,
88  const unsigned int mx, const unsigned int my, double & wx, double & wy)
89 {
90  const double origin_x = map->info.origin.position.x;
91  const double origin_y = map->info.origin.position.y;
92  const double resolution = map->info.resolution;
93 
94  wx = origin_x + (mx + 0.5) * resolution;
95  wy = origin_y + (my + 0.5) * resolution;
96 }
97 
98 } // namespace nav2_util
99 
100 #endif // NAV2_UTIL__OCC_GRID_UTILS_HPP_