MIRA
GridMapIO.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Redistribution and modification of this code is strictly prohibited.
9  *
10  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
11  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
12  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
13  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
16  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
18  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
19  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
20  */
21 
30 #ifndef _MIRA_GRIDMAP_IO_H_
31 #define _MIRA_GRIDMAP_IO_H_
32 
33 #include <opencv2/highgui/highgui.hpp>
34 
36 
37 #include <maps/GridMap.h>
38 
39 namespace mira { namespace maps {
40 
42 
45 {
46  template<typename Reflector>
47  void reflect(Reflector& r)
48  {
49  r.member("Image", image, "Filename of the map image");
50  r.member("CellSize", cellSize, "Resolution of the map in [m/cell]");
51  r.member("Offset", offset, "Offset of the origin of the image [cells]", Point2i(0, 0));
52  }
53 
54  std::string image;
55  float cellSize;
57 };
58 
61 {
62  template<typename Reflector>
63  void reflect(Reflector& r)
64  {
66  r.member("Frame", frame, "Frame ID of the map", "MapFrame");
67  }
68 
69  std::string frame;
70 };
71 
74 {
75  template<typename Reflector>
76  void reflect(Reflector& r)
77  {
79  r.member("Channel", channel, "The channel name where to publish the map");
80  }
81 
82  std::string channel;
83 };
84 
85 template<typename T, int Channels>
87 {
88  static void truncate(Img<T, Channels>& img, T truncateTo) {
89  using Pixel = typename Img<T, Channels>::Pixel;
90  foreach(Pixel& p, img) {
91  for (int i = 0; i < Channels; ++i)
92  p[i] = std::min(p[i], truncateTo);
93  }
94  }
95 };
96 
97 template<typename T>
98 struct PixelTruncateHelper<T, 1>
99 {
100  static void truncate(Img<T, 1>& img, T truncateTo) {
101  foreach(T& p, img) {
102  p = std::min(p, truncateTo);
103  }
104  }
105 };
106 
111 template<typename T, int Channels = 1>
113  float cellSize,
114  const Point2i& offset,
115  T truncateTo = std::numeric_limits<T>::max())
116 {
117  cv::Mat flipped;
118  cv::flip(img.getMat(), flipped, 0);
119 
121  PixelTruncateHelper<T, Channels>::truncate(mapImg, truncateTo);
122 
123  return GridMap<T, Channels>(mapImg, cellSize, offset);
124 }
125 
130 template<typename T, int Channels = 1>
132  float cellSize,
133  const Point2i& offset,
134  T truncateTo = std::numeric_limits<T>::max())
135 {
136  Path path = resolvePath(filename);
137  if ( !boost::filesystem::exists(path) )
138  MIRA_THROW(XFileNotFound, "The map '" << path.string() << "' does not exist");
139 
140  Img<> data(cv::imread(path.string()));
141  if ( data.isEmpty() )
142  MIRA_THROW(XIO, "The map '" << path.string() << "' could not be loaded");
143 
144  return loadGridMapFromImage<T, Channels>(data, cellSize, offset, truncateTo);
145 }
146 
150 template<typename T, int Channels = 1>
152  T truncateTo = std::numeric_limits<T>::max())
153 {
154  return loadGridMapFromFile<T, Channels>(info.image, info.cellSize, info.offset, truncateTo);
155 }
156 
161 template<typename T, int Channels = 1>
162 GridMap<T, Channels> loadGridMap(const Path& descriptionFilename,
163  T truncateTo = std::numeric_limits<T>::max())
164 {
165  XMLDom xml;
166  xml.loadFromFile(descriptionFilename);
167 
168  XMLDeserializer s(xml);
169  maps::GridMapInfo info;
170  s.deserialize("Map", info);
171 
172  return loadGridMap<T, Channels>(info, truncateTo);
173 }
174 
178 template<typename T, int Channels>
179 void saveGridMapToFile(Path filename,
180  const GridMap<T, Channels>& map,
181  bool fullImagePath = true)
182 {
183  if(!filename.has_extension())
184  filename.replace_extension(".png");
185 
186  // save the image
187  cv::Mat flipped;
188  cv::flip(map, flipped, 0);
189  cv::imwrite(filename.string(), flipped);
190 
191  // save the xml info
192  Path xmlFilename = filename;
193  xmlFilename.replace_extension(".xml");
194 
195  maps::GridMapInfo info;
196  info.cellSize = map.getCellSize();
197  info.offset = map.getMapOffset();
198  if (fullImagePath)
199  info.image = filename.string();
200  else
201  info.image = filename.filename().string();
202 
203  XMLDom xml;
204  XMLSerializer s(xml);
205  s.serialize("Map", info);
206  xml.saveToFile(xmlFilename);
207 }
208 
210 
211 }}
212 
213 #endif
Point2i offset
Definition: GridMapIO.h:56
static void truncate(Img< T, 1 > &img, T truncateTo)
Definition: GridMapIO.h:100
void saveToFile(const Path &filename, const std::string &encoding="UTF-8", bool resolve=true) const
A struct representing some information about a grid map and an additional frame.
Definition: GridMapIO.h:60
void reflect(Reflector &r)
Definition: GridMapIO.h:76
Definition: GridMapIO.h:86
Definition: GridMap.h:67
void deserialize(const std::string &name, T &value)
boost::filesystem::path Path
Description.
void reflect(Reflector &r)
Definition: GridMapIO.h:63
Path resolvePath(const Path &path, bool makeAbsolute=true)
A struct representing some information about a grid map and an additional channel.
Definition: GridMapIO.h:73
Point< int, 2 > Point2i
#define MIRA_REFLECT_BASE(reflector, BaseClass)
Point2i getMapOffset() const
Provided for backward compatibility. Use getOffset() instead.
Definition: GridMap.h:133
void loadFromFile(const Path &filename, bool resolve=true)
void serialize(const std::string &name, const T &value, const std::string &comment="")
static void truncate(Img< T, Channels > &img, T truncateTo)
Definition: GridMapIO.h:88
#define MIRA_THROW(ex, msg)
GridMap< T, Channels > loadGridMap(const GridMapInfo &info, T truncateTo=std::numeric_limits< T >::max())
Loads the image specified by info as map.
Definition: GridMapIO.h:151
void saveGridMapToFile(Path filename, const GridMap< T, Channels > &map, bool fullImagePath=true)
Saves the specified grid map as image + description xml.
Definition: GridMapIO.h:179
static Img< T, TChannels > convertFrom(const cv::Mat &other, bool alwaysCopy=false)
void reflect(Reflector &r)
Definition: GridMapIO.h:47
std::string frame
Definition: GridMapIO.h:69
std::string channel
Definition: GridMapIO.h:82
float getCellSize() const
Returns the size of each cell in meter.
Definition: GridMap.h:126
const cv::Mat & getMat() const
float cellSize
Definition: GridMapIO.h:55
A struct representing some information about a grid map.
Definition: GridMapIO.h:44
std::string image
Definition: GridMapIO.h:54
GridMap< T, Channels > loadGridMapFromFile(const Path &filename, float cellSize, const Point2i &offset, T truncateTo=std::numeric_limits< T >::max())
Loads the specified image file as grid map.
Definition: GridMapIO.h:131
GridMap< T, Channels > loadGridMapFromImage(const Img<> &img, float cellSize, const Point2i &offset, T truncateTo=std::numeric_limits< T >::max())
Loads the specified image as grid map.
Definition: GridMapIO.h:112
typedef Mat