YARP
Yet Another Robot Platform
MapGrid2D.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_DEV_MAPGRID2D_H
10 #define YARP_DEV_MAPGRID2D_H
11 
12 #include <string>
13 
14 #include <yarp/os/Portable.h>
16 #include <yarp/sig/Image.h>
17 #include <yarp/math/Vec2D.h>
18 #include <yarp/dev/api.h>
19 #include <yarp/dev/MapGrid2DInfo.h>
20 
24 namespace yarp
25 {
26  namespace dev
27  {
28  namespace Nav2D
29  {
32  {
33  public:
35  //typedef yarp::math::Vec2D<int> XYCell;
36  //typedef yarp::math::Vec2D<double> XYWorld;
37 
38  enum map_flags
39  {
40  MAP_CELL_FREE = 0,
41  MAP_CELL_KEEP_OUT = 1,
42  MAP_CELL_TEMPORARY_OBSTACLE = 2,
43  MAP_CELL_ENLARGED_OBSTACLE = 3,
44  MAP_CELL_WALL = 4,
45  MAP_CELL_UNKNOWN = 5
46  };
47 
48  private:
49  //those two always have the same size
50  yarp::sig::ImageOf<CellData> m_map_occupancy;
51  yarp::sig::ImageOf<CellData> m_map_flags;
52 
53  double m_occupied_thresh;
54  double m_free_thresh;
55 
56  //std::vector<map_link> links_to_other_maps;
57 
58  private:
59  //performs an obstacles enlargement on the specified cell.
60  void enlargeCell(XYCell cell);
61 
62  //conversion from pixel color to CellData and viceversa
63  CellData PixelToCellData(const yarp::sig::PixelRgb& pixin) const;
64  yarp::sig::PixelRgb CellDataToPixel(const CellData& pixin) const;
65 
66  //internal methods to read a map from file, either in yarp or ROS format
67  bool loadMapYarpOnly(std::string yarp_img_filename);
68  bool loadMapROSOnly(std::string ros_yaml_filename);
69  bool loadROSParams(std::string ros_yaml_filename, std::string& pgm_occ_filename, double& resolution, double& orig_x, double& orig_y, double& orig_t);
70  bool loadMapYarpAndRos(std::string yarp_img_filename, std::string ros_yaml_filename);
71  bool parseMapParameters(const yarp::os::Property& mapfile);
72 
73  public:
74  MapGrid2D();
75  virtual ~MapGrid2D();
76 
82  bool isWall(XYCell cell) const;
88  bool isFree(XYCell cell) const;
94  bool isNotFree(XYCell cell) const;
101  bool isKeepOut(XYCell cell) const;
107  bool getMapFlag(XYCell cell, map_flags& flag) const;
113  bool setMapFlag(XYCell cell, map_flags flag);
119  bool setOccupancyData(XYCell cell, double occupancy);
125  bool getOccupancyData(XYCell cell, double& occupancy) const;
126 
127  bool setMapImage(yarp::sig::ImageOf<yarp::sig::PixelRgb>& image);
128  bool getMapImage(yarp::sig::ImageOf<yarp::sig::PixelRgb>& image) const;
129  bool setOccupancyGrid(yarp::sig::ImageOf<yarp::sig::PixelMono>& image);
130  bool getOccupancyGrid(yarp::sig::ImageOf<yarp::sig::PixelMono>& image) const;
131 
137  bool setOrigin(double x, double y, double theta);
138 
143  void getOrigin(double& x, double& y, double& theta) const;
144 
150  bool setResolution(double resolution);
151 
156  void getResolution(double& resolution) const;
157 
163  bool setSize_in_meters(double x, double y);
169  bool setSize_in_cells(size_t x, size_t y);
174  void getSize_in_meters(double& x, double& y) const;
179  void getSize_in_cells(size_t&x, size_t& y) const;
180 
185  size_t width() const;
186 
191  size_t height() const;
192 
198  bool setMapName(std::string map_name);
199 
204  std::string getMapName() const;
205 
206  //------------------------------utility functions-------------------------------
207 
213  bool crop(int left, int top, int right, int bottom);
214 
215 #if 0
216 
221  bool isInsideMap(XYCell cell) const;
222 
228  bool isInsideMap(XYWorld world) const;
229 #endif
230 
235  bool isIdenticalTo(const MapGrid2D& otherMap) const;
236 
244  bool enlargeObstacles(double size);
245 
246  //-------------------------------file access functions-------------------------------
247 
253  bool loadFromFile(std::string map_filename);
254 
260  bool saveToFile(std::string map_filename) const;
261 
262  /*
263  * Read vector from a connection.
264  * return true iff a vector was read correctly
265  */
266  bool read(yarp::os::ConnectionReader& connection) override;
267 
272  bool write(yarp::os::ConnectionWriter& connection) const override;
273  };
274  }
275  }
276 }
277 
278 #endif // YARP_DEV_MAPGRID2D_H
yarp::os::Portable
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:29
yarp::sig::file::read
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:827
yarp::dev::Nav2D::MapGrid2D::~MapGrid2D
virtual ~MapGrid2D()
Portable.h
yarp::math::Vec2D
Definition: Vec2D.h:26
yarp::sig::ImageOf< CellData >
yarp::dev::Nav2D::MapGrid2D::CellData
yarp::sig::PixelMono CellData
Definition: MapGrid2D.h:34
yarp::sig::PixelMono
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:436
yarp::dev::Nav2D::MapGrid2D
Definition: MapGrid2D.h:32
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
yarp::dev::Nav2D::MapGrid2D::map_flags
map_flags
Definition: MapGrid2D.h:39
YARP_dev_API
#define YARP_dev_API
Definition: api.h:19
Image.h
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
yarp::sig::PixelRgb
Packed RGB pixel type.
Definition: Image.h:453
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
Vec2D.h
yarp::dev::Nav2D::MapGrid2DInfo
Definition: MapGrid2DInfo.h:54
yarp::sig::file::write
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:971
MapGrid2DInfo.h
api.h
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37
ConnectionReader.h