Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
vector_object_utils.hpp
1 // Copyright (c) 2023 Samsung R&D Institute Russia
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef NAV2_MAP_SERVER__VECTOR_OBJECT_UTILS_HPP_
16 #define NAV2_MAP_SERVER__VECTOR_OBJECT_UTILS_HPP_
17 
18 #include <uuid/uuid.h>
19 #include <stdexcept>
20 #include <string>
21 
22 #include "rclcpp/rclcpp.hpp"
23 #include "nav_msgs/msg/occupancy_grid.hpp"
24 
25 #include "nav2_ros_common/lifecycle_node.hpp"
26 #include "nav2_ros_common/node_utils.hpp"
27 #include "nav2_util/occ_grid_values.hpp"
28 
29 namespace nav2_map_server
30 {
31 
32 // ---------- Working with UUID-s ----------
33 
39 inline std::string unparseUUID(const unsigned char * uuid)
40 {
41  char uuid_str[37];
42  uuid_unparse(uuid, uuid_str);
43  return std::string(uuid_str);
44 }
45 
46 // ---------- Working with shapes' overlays ----------
47 
49 enum class OverlayType : uint8_t
50 {
51  OVERLAY_SEQ = 0, // Vector objects are superimposed in the order in which they have arrived
52  OVERLAY_MAX = 1, // Maximum value from vector objects and map is being chosen
53  OVERLAY_MIN = 2 // Minimum value from vector objects and map is being chosen
54 };
55 
63 inline void processVal(
64  int8_t & map_val, const int8_t shape_val,
65  const OverlayType overlay_type)
66 {
67  switch (overlay_type) {
68  case OverlayType::OVERLAY_SEQ:
69  map_val = shape_val;
70  return;
71  case OverlayType::OVERLAY_MAX:
72  if (shape_val > map_val) {
73  map_val = shape_val;
74  }
75  return;
76  case OverlayType::OVERLAY_MIN:
77  if ((map_val == nav2_util::OCC_GRID_UNKNOWN || shape_val < map_val) &&
78  shape_val != nav2_util::OCC_GRID_UNKNOWN)
79  {
80  map_val = shape_val;
81  }
82  return;
83  default:
84  throw std::runtime_error{"Unknown overlay type"};
85  }
86 }
87 
95 inline void processCell(
96  nav_msgs::msg::OccupancyGrid::SharedPtr map,
97  const unsigned int offset,
98  const int8_t shape_val,
99  const OverlayType overlay_type)
100 {
101  int8_t map_val = map->data[offset];
102  processVal(map_val, shape_val, overlay_type);
103  map->data[offset] = map_val;
104 }
105 
108 {
109 public:
117  nav_msgs::msg::OccupancyGrid::SharedPtr map,
118  int8_t value, OverlayType overlay_type)
119  : map_(map), value_(value), overlay_type_(overlay_type)
120  {}
121 
126  inline void operator()(unsigned int offset)
127  {
128  processCell(map_, offset, value_, overlay_type_);
129  }
130 
131 protected:
133  nav_msgs::msg::OccupancyGrid::SharedPtr map_;
135  int8_t value_;
137  OverlayType overlay_type_;
138 };
139 
140 } // namespace nav2_map_server
141 
142 #endif // NAV2_MAP_SERVER__VECTOR_OBJECT_UTILS_HPP_
Functor class used in raytraceLine algorithm.
OverlayType overlay_type_
Overlay type.
void operator()(unsigned int offset)
Map' cell updating operator.
MapAction(nav_msgs::msg::OccupancyGrid::SharedPtr map, int8_t value, OverlayType overlay_type)
MapAction constructor.
nav_msgs::msg::OccupancyGrid::SharedPtr map_
Output map pointer.
int8_t value_
Value to put on map.