YARP
Yet Another Robot Platform
MapGrid2D.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef YARP_DEV_MAPGRID2D_H
7 #define YARP_DEV_MAPGRID2D_H
8 
9 #include <string>
10 
11 #include <yarp/os/Portable.h>
13 #include <yarp/sig/Image.h>
14 #include <yarp/math/Vec2D.h>
15 #include <yarp/dev/api.h>
16 #include <yarp/dev/MapGrid2DInfo.h>
17 
21 namespace yarp
22 {
23  namespace dev
24  {
25  namespace Nav2D
26  {
29  {
30  public:
33  //typedef yarp::math::Vec2D<int> XYCell;
34  //typedef yarp::math::Vec2D<double> XYWorld;
35 
36  enum map_flags
37  {
38  MAP_CELL_FREE = 0,
39  MAP_CELL_KEEP_OUT = 1,
40  MAP_CELL_TEMPORARY_OBSTACLE = 2,
41  MAP_CELL_ENLARGED_OBSTACLE = 3,
42  MAP_CELL_WALL = 4,
43  MAP_CELL_UNKNOWN = 5
44  };
45 
46  private:
47  //those two always have the same size
50 
51  double m_occupied_thresh;
52  double m_free_thresh;
53 
54  //std::vector<map_link> links_to_other_maps;
55 
56  private:
57  bool m_compressed_data_over_network;
58  public:
59  bool enable_map_compression_over_network (bool val);
60 
61  private:
62  //performs an obstacles enlargement on the specified cell.
63  void enlargeCell(XYCell cell);
64 
65  //conversion from pixel color to CellFlagData (yarp format) and viceversa
66  CellFlagData PixelToCellFlagData(const yarp::sig::PixelRgb& pixin) const;
67  yarp::sig::PixelRgb CellFlagDataToPixel(const CellFlagData& cellin) const;
68 
69  //conversion from pixel color to CellOccupancyData (occupancy grid, ros format) and viceversa
70  CellOccupancyData PixelToCellOccupancyData(const yarp::sig::PixelMono& pixin) const;
71  yarp::sig::PixelMono CellOccupancyDataToPixel(const CellOccupancyData& cellin) const;
72 
73  //internal methods to read a map from file, either in yarp or ROS format
74  bool loadMapYarpOnly(std::string yarp_img_filename);
75  bool loadMapROSOnly(std::string ros_yaml_filename);
76  bool loadROSParams(std::string ros_yaml_filename, std::string& pgm_occ_filename, double& resolution, double& orig_x, double& orig_y, double& orig_t);
77  bool loadMapYarpAndRos(std::string yarp_img_filename, std::string ros_yaml_filename);
78  bool parseMapParameters(const yarp::os::Property& mapfile);
79 
80  public:
81  MapGrid2D();
82  virtual ~MapGrid2D();
83 
89  bool isWall(XYCell cell) const;
90 
96  bool isFree(XYCell cell) const;
97 
103  bool isNotFree(XYCell cell) const;
104 
111  bool isKeepOut(XYCell cell) const;
112 
118  bool getMapFlag(XYCell cell, map_flags& flag) const;
119 
125  bool setMapFlag(XYCell cell, map_flags flag);
126 
130  void clearMapTemporaryFlags();
131 
137  bool setOccupancyData(XYCell cell, double occupancy);
138 
144  bool getOccupancyData(XYCell cell, double& occupancy) const;
145 
146  bool setMapImage(yarp::sig::ImageOf<yarp::sig::PixelRgb>& image);
147  bool getMapImage(yarp::sig::ImageOf<yarp::sig::PixelRgb>& image) const;
148  bool setOccupancyGrid(yarp::sig::ImageOf<yarp::sig::PixelMono>& image);
149  bool getOccupancyGrid(yarp::sig::ImageOf<yarp::sig::PixelMono>& image) const;
150 
156  bool setOrigin(double x, double y, double theta);
157 
162  void getOrigin(double& x, double& y, double& theta) const;
163 
169  bool setResolution(double resolution);
170 
175  void getResolution(double& resolution) const;
176 
182  bool setSize_in_meters(double x, double y);
183 
189  bool setSize_in_cells(size_t x, size_t y);
190 
195  void getSize_in_meters(double& x, double& y) const;
196 
201  void getSize_in_cells(size_t&x, size_t& y) const;
202 
207  size_t width() const;
208 
213  size_t height() const;
214 
220  bool setMapName(std::string map_name);
221 
226  std::string getMapName() const;
227 
228  //------------------------------utility functions-------------------------------
229 
235  bool crop(int left, int top, int right, int bottom);
236 
237 #if 0
243  bool isInsideMap(XYCell cell) const;
244 
250  bool isInsideMap(XYWorld world) const;
251 #endif
252 
257  bool isIdenticalTo(const MapGrid2D& otherMap) const;
258 
266  bool enlargeObstacles(double size);
267 
268  //-------------------------------file access functions-------------------------------
269 
275  bool loadFromFile(std::string map_filename);
276 
282  bool saveToFile(std::string map_filename) const;
283 
284  /*
285  * Read vector from a connection.
286  * return true iff a vector was read correctly
287  */
288  bool read(yarp::os::ConnectionReader& connection) override;
289 
294  bool write(yarp::os::ConnectionWriter& connection) const override;
295  };
296  }
297  }
298 }
299 
300 #endif // YARP_DEV_MAPGRID2D_H
yarp::sig::PixelMono CellFlagData
Definition: MapGrid2D.h:31
yarp::sig::PixelMono CellOccupancyData
Definition: MapGrid2D.h:32
An interface for reading from a network connection.
An interface for writing to a network connection.
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:26
A class for storing options and configuration information.
Definition: Property.h:34
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:922
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1098
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:447
The main, catch-all namespace for YARP.
Definition: dirs.h:16
Packed RGB pixel type.
Definition: Image.h:464
#define YARP_dev_API
Definition: api.h:18