n-Dimensional Fast Methods  0.7
 All Classes Functions Variables Typedefs Pages
gridplotter.hpp
1 
20 #ifndef GRIDPLOTTER_H_
21 #define GRIDPLOTTER_H_
22 
23 #include <string>
24 #include <array>
25 
26 #include <CImg.h>
27 
28 #include <fast_methods/ndgridmap/fmcell.h>
29 #include <fast_methods/ndgridmap/ndgridmap.hpp>
30 
31 
32 using namespace cimg_library;
33 
36 class GridPlotter {
38  typedef typename std::array<unsigned int, 2> Coord2D;
39 
41  typedef typename std::array<double, 2> Point2D;
42 
44  typedef typename std::vector <Point2D> Path2D;
45 
47  typedef typename std::vector <Path2D> Paths2D;
48 
49  public:
59  template<class T, size_t ndims>
60  static void plotMap
61  (nDGridMap<T, ndims> & grid, std::string name = "") {
62  std::array<unsigned int,2> d = grid.getDimSizes();
63  CImg<bool> img(d[0],d[1],1,1,0);
64  // Filling the image flipping Y dim. We want now top left to be the (0,0).
65  cimg_forXY(img,x,y) {img(x,y) = !grid[img.width()*(img.height()-y-1)+x].isOccupied(); }
66  // Code for not-inverted Y axis.
67  //cimg_forXY(img,x,y) { if(grid[img.width()*y+x].getOccupancy() > 0.5) img(x,y) = true;
68  // else img(x,y) = false; }
69  name += " Map";
70  img.display(name.c_str(), false);
71  }
72 
82  template<class T, size_t ndims>
83  static void plotOccupancyMap
84  (nDGridMap<T, ndims> & grid, std::string name = "") {
85  std::array<unsigned int,2> d = grid.getDimSizes();
86  CImg<double> img(d[0],d[1],1,1,0);
87  // Filling the image flipping Y dim. We want now top left to be the (0,0).
88  cimg_forXY(img,x,y) { img(x,y) = grid[img.width()*(img.height()-y-1)+x].getOccupancy()*255; }
89  name += " Occupancy Map";
90  img.display(name.c_str(), false);
91  }
92 
102  template<class T, size_t ndims = 2>
103  static void plotArrivalTimes
104  (nDGridMap<T, ndims> & grid, std::string name = "") {
105  std::array<unsigned int,2> d = grid.getDimSizes();
106  double max_val = grid.getMaxValue();
107  CImg<double> img(d[0],d[1],1,1,0);
108  // Filling the image flipping Y dim. We want now top left to be the (0,0).
109  cimg_forXY(img,x,y) { img(x,y) = grid[img.width()*(img.height()-y-1)+x].getValue()/max_val*255; }
110  img.map( CImg<double>::jet_LUT256() );
111  name += " Grid values";
112  img.display(name.c_str(), false);
113  }
114 
124  template<class T, size_t ndims = 2>
125  static void plotMapPath
126  (nDGridMap<T, ndims> & grid, const Path2D & path, std::string name = "") {
127  std::array<unsigned int,2> d = grid.getDimSizes();
128  CImg<double> img(d[0],d[1],1,3,0);
129 
130  // Filling the image flipping Y dim. We want now top left to be the (0,0).
131  cimg_forXYZC(img,x,y,z,c) { img(x,y,z,c) = (!grid[img.width()*(img.height()-y-1)+x].isOccupied())*255; }
132 
133  for (unsigned int i = 0; i< path.size(); ++i)
134  {
135  img(static_cast<unsigned int>(path[i][0]), (img.height()-static_cast<unsigned int>(path[i][1])-1), 0, 1) = 0;
136  img(static_cast<unsigned int>(path[i][0]), (img.height()-static_cast<unsigned int>(path[i][1])-1), 0, 2) = 0;
137  }
138 
139  name += " Map and Path";
140  img.display(name.c_str(), false);
141  }
142 
152  template<class T, size_t ndims = 2>
153  static void plotOccupancyPath
154  (nDGridMap<T, ndims> & grid, const Path2D & path, std::string name = "") {
155  std::array<unsigned int,2> d = grid.getDimSizes();
156  CImg<double> img(d[0],d[1],1,3,0);
157  // Filling the image flipping Y dim. We want now top left to be the (0,0).
158  cimg_forXYZC(img,x,y,z,c) { img(x,y,z,c) = grid[img.width()*(img.height()-y-1)+x].getOccupancy()*255; }
159 
160  for (unsigned int i = 0; i< path.size(); ++i)
161  {
162  img(static_cast<unsigned int>(path[i][0]), (img.height()-static_cast<unsigned int>(path[i][1])-1), 0, 1) = 0;
163  img(static_cast<unsigned int>(path[i][0]), (img.height()-static_cast<unsigned int>(path[i][1])-1), 0, 2) = 0;
164  }
165  name += " Map and Path";
166  img.display(name.c_str(), false);
167  }
168 
178  template<class T, size_t ndims = 2>
179  static void plotMapPaths
180  (nDGridMap<T, ndims> & grid, const Paths2D & paths, std::string name = "") {
181  std::array<unsigned int,2> d = grid.getDimSizes();
182  CImg<double> img(d[0],d[1],1,3,0);
183 
184  // Filling the image flipping Y dim. We want now top left to be the (0,0).
185  cimg_forXYZC(img,x,y,z,c) { img(x,y,z,c) = (!grid[img.width()*(img.height()-y-1)+x].isOccupied())*255; }
186 
187  // Draw the path using different colours
188  for (unsigned int j = 0; j < paths.size(); ++j)
189  {
190  Path2D path = paths[j];
191  for (unsigned int i = 0; i< path.size(); ++i)
192  {
193  img(static_cast<unsigned int>(path[i][0]), (img.height()-static_cast<unsigned int>(path[i][1])-1), 0, j) = 0;
194  img(static_cast<unsigned int>(path[i][0]), (img.height()-static_cast<unsigned int>(path[i][1])-1), 0, j+1) = 0;
195  }
196  }
197  name += " Map and Paths";
198  img.display(name.c_str(), false);
199  }
200 
210  template<class T, size_t ndims = 2>
211  static void plotArrivalTimesPath
212  (nDGridMap<T, ndims> & grid, const Path2D & path, std::string name = "") {
213  std::array<unsigned int,2> d = grid.getDimSizes();
214  double max_val = grid.getMaxValue();
215  CImg<double> img(d[0],d[1],1,1,0);
216 
217  // Filling the image flipping Y dim. We want now top left to be the (0,0).
218  cimg_forXY(img,x,y) { img(x,y) = grid[img.width()*(img.height()-y-1)+x].getValue()/max_val*255; }
219 
220  for (unsigned int i = 0; i< path.size(); ++i)
221  img(static_cast<unsigned int>(path[i][0]), (img.height()-static_cast<unsigned int>(path[i][1])-1)) = 255;
222 
223  img.map( CImg<double>::jet_LUT256() );
224  name += " Values and Path";
225  img.display(name.c_str(), false);
226  }
227 
234  template<class T, size_t ndims = 2>
235  static void plotFMStates
236  (nDGridMap<T, ndims> & grid, std::string name = "") {
237  std::array<unsigned int,2> d = grid.getDimSizes();
238  //double max_val = grid.getMaxValue();
239  CImg<unsigned int> img(d[0],d[1],1,1,0);
240  // Filling the image flipping Y dim. We want now top left to be the (0,0).
241  cimg_forXY(img,x,y) {
242  FMState state = grid[img.width()*(img.height()-y-1)+x].getState();
243  unsigned int val = 0;
244  if (state == FMState::NARROW)
245  val = 127;
246  else if (state == FMState::OPEN)
247  val = 255;
248  img(x,y) = val;
249  }
250  //img.map( CImg<double>::jet_LUT256() );
251  name += "FMStates";
252  img.display(name.c_str(), false);
253  }
254 
255 };
256 
257 #endif /* GRIDPLOTTER_H_ */
double getMaxValue() const
Returns the maximum value of the cells in the grid.
Definition: ndgridmap.hpp:258
Templated class which represents a n-dimensional grid map. Its cells are assumed to be cubic...
Definition: ndgridmap.hpp:47
std::vector< Path2D > Paths2D
Shorthand for vector of 2D paths of real points.
Definition: gridplotter.hpp:47
std::vector< Point2D > Path2D
Shorthand for 2D paths of real points.
Definition: gridplotter.hpp:44
std::array< unsigned int, ndims > getDimSizes() const
Returns the size of each dimension.
Definition: ndgridmap.hpp:115
std::array< unsigned int, 2 > Coord2D
Shorthand for 2D coordinates.
Definition: gridplotter.hpp:38
Auxiliar class which helps to visualise Fast Marching steps and results.
Definition: gridplotter.hpp:36
std::array< double, 2 > Point2D
Shorthand for 2D real points.
Definition: gridplotter.hpp:41