Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
vector_object_shapes.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_SHAPES_HPP_
16 #define NAV2_MAP_SERVER__VECTOR_OBJECT_SHAPES_HPP_
17 
18 #include <cstddef>
19 #include <memory>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include "rclcpp/rclcpp.hpp"
25 #include "geometry_msgs/msg/polygon.hpp"
26 #include "geometry_msgs/msg/point32.hpp"
27 #include "nav_msgs/msg/occupancy_grid.hpp"
28 
29 #include "nav2_ros_common/tf2_factories.hpp"
30 
31 #include "nav2_msgs/msg/polygon_object.hpp"
32 #include "nav2_msgs/msg/circle_object.hpp"
33 #include "nav2_ros_common/lifecycle_node.hpp"
34 
35 #include "nav2_map_server/vector_object_utils.hpp"
36 
37 namespace nav2_map_server
38 {
39 
41 enum ShapeType
42 {
43  UNKNOWN = 0,
44  POLYGON = 1,
45  CIRCLE = 2
46 };
47 
49 class Shape
50 {
51 public:
56  explicit Shape(const nav2::LifecycleNode::WeakPtr & node);
57 
61  virtual ~Shape();
62 
67  ShapeType getType();
68 
74  bool obtainShapeUUID(const std::string & shape_name, unsigned char * out_uuid);
75 
81  virtual int8_t getValue() const = 0;
82 
88  virtual std::string getFrameID() const = 0;
89 
95  virtual std::string getUUID() const = 0;
96 
103  virtual bool isUUID(const unsigned char * uuid) const = 0;
104 
110  virtual bool isFill() const = 0;
111 
118  virtual bool obtainParams(const std::string & shape_name) = 0;
119 
128  virtual bool toFrame(
129  const std::string & to_frame,
130  const nav2::TransformBuffer::SharedPtr tf_buffer,
131  const double transform_tolerance) = 0;
132 
141  virtual void getBoundaries(double & min_x, double & min_y, double & max_x, double & max_y) = 0;
142 
150  virtual bool isPointInside(const double px, const double py) const = 0;
151 
159  virtual void getRowSpans(
160  const double py, std::vector<std::pair<double, double>> & spans) const = 0;
161 
168  bool putFill(nav_msgs::msg::OccupancyGrid::SharedPtr map, const OverlayType overlay_type);
169 
176  virtual void putBorders(
177  nav_msgs::msg::OccupancyGrid::SharedPtr map, const OverlayType overlay_type) = 0;
178 
179 protected:
188  static void processRun(
189  int8_t * cells, const size_t count, const int8_t shape_val,
190  const OverlayType overlay_type);
191 
193  ShapeType type_;
194 
196  nav2::LifecycleNode::WeakPtr node_;
197 };
198 
200 class Polygon : public Shape
201 {
202 public:
203  /*
204  * @brief Polygon class constructor
205  * @param node Vector Object server node pointer
206  * @note setParams()/obtainParams() should be called after to configure the shape
207  */
208  explicit Polygon(const nav2::LifecycleNode::WeakPtr & node);
209 
214  int8_t getValue() const;
215 
220  std::string getFrameID() const;
221 
226  std::string getUUID() const;
227 
233  bool isUUID(const unsigned char * uuid) const;
234 
239  bool isFill() const;
240 
246  bool obtainParams(const std::string & shape_name);
247 
252  nav2_msgs::msg::PolygonObject::SharedPtr getParams() const;
253 
258  bool setParams(const nav2_msgs::msg::PolygonObject::SharedPtr params);
259 
267  bool toFrame(
268  const std::string & to_frame,
269  const nav2::TransformBuffer::SharedPtr tf_buffer,
270  const double transform_tolerance);
271 
279  void getBoundaries(double & min_x, double & min_y, double & max_x, double & max_y);
280 
287  bool isPointInside(const double px, const double py) const;
288 
294  void getRowSpans(const double py, std::vector<std::pair<double, double>> & spans) const;
295 
301  void putBorders(nav_msgs::msg::OccupancyGrid::SharedPtr map, const OverlayType overlay_type);
302 
303 protected:
308  bool checkConsistency();
309 
311  nav2_msgs::msg::PolygonObject::SharedPtr params_;
313  geometry_msgs::msg::Polygon::SharedPtr polygon_;
314 };
315 
317 class Circle : public Shape
318 {
319 public:
320  /*
321  * @brief Circle class constructor
322  * @param node Vector Object server node pointer
323  * @note setParams()/obtainParams() should be called after to configure the shape
324  */
325  explicit Circle(const nav2::LifecycleNode::WeakPtr & node);
326 
331  int8_t getValue() const;
332 
337  std::string getFrameID() const;
338 
343  std::string getUUID() const;
344 
350  bool isUUID(const unsigned char * uuid) const;
351 
356  bool isFill() const;
357 
363  bool obtainParams(const std::string & shape_name);
364 
369  nav2_msgs::msg::CircleObject::SharedPtr getParams() const;
370 
375  bool setParams(const nav2_msgs::msg::CircleObject::SharedPtr params);
376 
384  bool toFrame(
385  const std::string & to_frame,
386  const nav2::TransformBuffer::SharedPtr tf_buffer,
387  const double transform_tolerance);
388 
396  void getBoundaries(double & min_x, double & min_y, double & max_x, double & max_y);
397 
404  bool isPointInside(const double px, const double py) const;
405 
411  void getRowSpans(const double py, std::vector<std::pair<double, double>> & spans) const;
412 
418  void putBorders(nav_msgs::msg::OccupancyGrid::SharedPtr map, const OverlayType overlay_type);
419 
420 protected:
425  bool checkConsistency();
426 
436  bool centerToMap(
437  nav_msgs::msg::OccupancyGrid::ConstSharedPtr map,
438  unsigned int & mcx, unsigned int & mcy);
439 
447  void putPoint(
448  unsigned int mx, unsigned int my,
449  nav_msgs::msg::OccupancyGrid::SharedPtr map,
450  const OverlayType overlay_type);
451 
453  nav2_msgs::msg::CircleObject::SharedPtr params_;
455  geometry_msgs::msg::Point32::SharedPtr center_;
456 };
457 
458 } // namespace nav2_map_server
459 
460 #endif // NAV2_MAP_SERVER__VECTOR_OBJECT_SHAPES_HPP_
bool isUUID(const unsigned char *uuid) const
Checks whether the shape is equal to a given UUID.
int8_t getValue() const
Gets the value of the shape.
bool toFrame(const std::string &to_frame, const nav2::TransformBuffer::SharedPtr tf_buffer, const double transform_tolerance)
Transforms shape coordinates to a new frame.
void putPoint(unsigned int mx, unsigned int my, nav_msgs::msg::OccupancyGrid::SharedPtr map, const OverlayType overlay_type)
Put Circle's point on map.
bool isPointInside(const double px, const double py) const
Is the point inside the shape.
std::string getUUID() const
Gets UUID of the shape.
void getBoundaries(double &min_x, double &min_y, double &max_x, double &max_y)
Gets shape box-boundaries.
std::string getFrameID() const
Gets frame ID of the shape.
bool centerToMap(nav_msgs::msg::OccupancyGrid::ConstSharedPtr map, unsigned int &mcx, unsigned int &mcy)
Converts circle center to map coordinates considering FP-accuracy losing on small values when using c...
bool isFill() const
Whether the shape to be filled or only its borders to be put on map.
void getRowSpans(const double py, std::vector< std::pair< double, double >> &spans) const
Gets X-interval covered by the circle on the horizontal line y = py.
nav2_msgs::msg::CircleObject::SharedPtr params_
Input circle parameters (could be in any frame)
bool setParams(const nav2_msgs::msg::CircleObject::SharedPtr params)
Tries to update Circle parameters.
void putBorders(nav_msgs::msg::OccupancyGrid::SharedPtr map, const OverlayType overlay_type)
Puts shape borders on map.
nav2_msgs::msg::CircleObject::SharedPtr getParams() const
Gets Circle parameters.
bool checkConsistency()
Checks that shape is consistent for further operation.
geometry_msgs::msg::Point32::SharedPtr center_
Circle center in the map's frame.
bool obtainParams(const std::string &shape_name)
Supporting routine obtaining ROS-parameters for the given vector object.
std::string getUUID() const
Gets UUID of the shape.
void getRowSpans(const double py, std::vector< std::pair< double, double >> &spans) const
Gets X-intervals covered by the polygon on the horizontal line y = py.
void getBoundaries(double &min_x, double &min_y, double &max_x, double &max_y)
Gets shape box-boundaries.
void putBorders(nav_msgs::msg::OccupancyGrid::SharedPtr map, const OverlayType overlay_type)
Puts shape borders on map.
bool toFrame(const std::string &to_frame, const nav2::TransformBuffer::SharedPtr tf_buffer, const double transform_tolerance)
Transforms shape coordinates to a new frame.
bool isUUID(const unsigned char *uuid) const
Checks whether the shape is equal to a given UUID.
nav2_msgs::msg::PolygonObject::SharedPtr getParams() const
Gets Polygon parameters.
int8_t getValue() const
Gets the value of the shape.
bool setParams(const nav2_msgs::msg::PolygonObject::SharedPtr params)
Tries to update Polygon parameters.
std::string getFrameID() const
Gets frame ID of the shape.
nav2_msgs::msg::PolygonObject::SharedPtr params_
Input polygon parameters (could be in any frame)
bool checkConsistency()
Checks that shape is consistent for further operation.
bool isPointInside(const double px, const double py) const
Is the point inside the shape.
bool isFill() const
Whether the shape to be filled or only its borders to be put on map.
bool obtainParams(const std::string &shape_name)
Supporting routine obtaining ROS-parameters for the given vector object.
geometry_msgs::msg::Polygon::SharedPtr polygon_
Polygon in the map's frame.
Basic class, other vector objects to be inherited from.
virtual bool isFill() const =0
Whether the shape to be filled or only its borders to be put on map. Empty virtual method intended to...
virtual bool obtainParams(const std::string &shape_name)=0
Supporting routine obtaining ROS-parameters for the given vector object. Empty virtual method intende...
virtual void putBorders(nav_msgs::msg::OccupancyGrid::SharedPtr map, const OverlayType overlay_type)=0
Puts shape borders on map. Empty virtual method intended to be used in child implementations.
bool obtainShapeUUID(const std::string &shape_name, unsigned char *out_uuid)
Supporting routine obtaining shape UUID from ROS-parameters for the given shape object.
virtual void getBoundaries(double &min_x, double &min_y, double &max_x, double &max_y)=0
Gets shape box-boundaries. Empty virtual method intended to be used in child implementations.
virtual bool isPointInside(const double px, const double py) const =0
Is the point inside the shape. Empty virtual method intended to be used in child implementations.
ShapeType type_
Type of shape.
ShapeType getType()
Returns type of the shape.
nav2::LifecycleNode::WeakPtr node_
VectorObjectServer node.
virtual std::string getFrameID() const =0
Gets frame ID of the shape. Empty virtual method intended to be used in child implementations.
virtual void getRowSpans(const double py, std::vector< std::pair< double, double >> &spans) const =0
Gets X-intervals covered by the shape on the horizontal line y = py. Intervals may be slightly wider ...
virtual bool toFrame(const std::string &to_frame, const nav2::TransformBuffer::SharedPtr tf_buffer, const double transform_tolerance)=0
Transforms shape coordinates to a new frame. Empty virtual method intended to be used in child implem...
virtual ~Shape()
Shape destructor.
Shape(const nav2::LifecycleNode::WeakPtr &node)
Shape basic class constructor.
bool putFill(nav_msgs::msg::OccupancyGrid::SharedPtr map, const OverlayType overlay_type)
Puts filled shape on map.
static void processRun(int8_t *cells, const size_t count, const int8_t shape_val, const OverlayType overlay_type)
Updates a run of consecutive cells with given shape value according to the overlay type.
virtual int8_t getValue() const =0
Gets the value of the shape. Empty virtual method intended to be used in child implementations.
virtual std::string getUUID() const =0
Gets UUID of the shape. Empty virtual method intended to be used in child implementations.
virtual bool isUUID(const unsigned char *uuid) const =0
Checks whether the shape is equal to a given UUID. Empty virtual method intended to be used in child ...