n-Dimensional Fast Methods  0.7
 All Classes Functions Variables Typedefs Pages
maploader.hpp
1 
20 #ifndef MAPLOADER_H_
21 #define MAPLOADER_H_
22 
23 #include <CImg.h>
24 
25 #include <fast_methods/ndgridmap/ndgridmap.hpp>
26 
27 
28 using namespace cimg_library;
29 
32 
33 class MapLoader {
34  public:
49  template<class T, size_t ndims>
50  static void loadMapFromImg
51  (const char * filename, nDGridMap<T, ndims> & grid) {
52  std::vector<unsigned int> obs;
53  CImg<double> img(filename);
54  std::array<unsigned int, ndims> dimsize;
55  dimsize[0] = img.width();
56  dimsize[1] = img.height();
57  grid.resize(dimsize);
58 
59  // Filling the grid flipping Y dim. We want bottom left to be the (0,0).
60  cimg_forXY(img,x,y) {
61  double occupancy = img(x,y)/255;
62  unsigned int idx = img.width()*(img.height()-y-1)+x;
63  grid[idx].setOccupancy(occupancy);
64  if (grid[idx].isOccupied())
65  obs.push_back(idx);
66  }
67  grid.setOccupiedCells(std::move(obs));
68  }
69 
94  template<class T, size_t ndims>
95  static int loadMapFromText
96  (const char * filename, nDGridMap<T, ndims> & grid) {
97  std::ifstream file;
98  std::vector<unsigned int> obs;
99  file.open(filename);
100 
101  if (file.is_open())
102  {
103  std::string val;
104  std::getline(file, val);
105 
106  double leafsize;
107  size_t ndims_aux;
108 
109  file >> leafsize;
110  file >> ndims_aux;
111 
112  if (ndims_aux != ndims) {
113  console::error("Number of dimensions specified does not match the loaded grid.");
114  exit(1);
115  }
116 
117  std::array<unsigned int, ndims> dimsize;
118  for (unsigned int &i : dimsize) {
119  file >> i;
120  }
121 
122  grid.resize(dimsize);
123  grid.setLeafSize(leafsize);
124 
125  double occupancy;
126  for (unsigned int i = 0; i < grid.size(); ++i)
127  {
128  file >> occupancy;
129  grid[i].setOccupancy(occupancy);
130 
131  if (grid[i].isOccupied())
132  obs.push_back(i);
133  }
134  grid.setOccupiedCells(std::move(obs));
135  return 1;
136  }
137  else
138  {
139  console::error("File not found.");
140  return 0;
141  }
142  }
143 };
144 
145 #endif /* MAPLOADER_H_ */
void resize(const std::array< unsigned int, ndims > &dimsize)
Resizes each dimension of the grid according dimsize.
Definition: ndgridmap.hpp:78
static void error(const std::string &val)
Auxiliar static class which helps to load maps into nDGridMap.
Definition: maploader.hpp:33
void setOccupiedCells(const std::vector< unsigned int > &obs)
Sets the cells which are occupied. Usually called by grid loaders.
Definition: ndgridmap.hpp:307
Templated class which represents a n-dimensional grid map. Its cells are assumed to be cubic...
Definition: ndgridmap.hpp:47
unsigned int size() const
Returns number of cells in the grid.
Definition: ndgridmap.hpp:252