n-Dimensional Fast Methods  0.7
 All Classes Functions Variables Typedefs Pages
full_example.cpp
1 /* n-dimensional Fast Methods example with the main functions used */
2 
3 #include <iostream>
4 #include <cmath>
5 #include <chrono>
6 #include <array>
7 #include <string>
8 
9 #include <fast_methods/ndgridmap/fmcell.h>
10 #include <fast_methods/ndgridmap/ndgridmap.hpp>
11 #include <fast_methods/console/console.h>
12 #include <fast_methods/fm/fmm.hpp>
13 #include <fast_methods/fm2/fm2.hpp>
14 #include <fast_methods/fm2/fm2star.hpp>
15 #include <fast_methods/datastructures/fmfibheap.hpp>
16 #include <fast_methods/datastructures/fmpriorityqueue.hpp>
17 #include <fast_methods/datastructures/fmdaryheap.hpp>
18 #include <fast_methods/io/maploader.hpp>
19 #include <fast_methods/io/gridplotter.hpp>
20 #include <fast_methods/io/gridwriter.hpp>
21 #include <fast_methods/io/gridpoints.hpp>
22 #include <fast_methods/gradientdescent/gradientdescent.hpp>
23 
24 using namespace std;
25 using namespace std::chrono;
26 
27 int main(int argc, const char ** argv)
28 {
29  constexpr unsigned int ndims = 2; // Setting two dimensions.
30  constexpr unsigned int ndims3 = 3; // Setting three dimensions.
31 
32  time_point<std::chrono::steady_clock> start, end; // Time measuring.
33  double time_elapsed;
34 
35  console::info("Parsing input arguments.");
36  string filename1, filename2, filename_vels;
37  console::parseArguments(argc,argv, "-map1", filename1);
38  console::parseArguments(argc,argv, "-map2", filename2);
39  console::parseArguments(argc,argv, "-vel", filename_vels);
40 
41  console::info("Creating grid from image and testing Fast Marching Method..");
43  MapLoader::loadMapFromImg(filename2.c_str(), grid);
44 
45  std::array<unsigned int, ndims> coords_init, coords_goal;
46  GridPoints::selectMapPoints(grid, coords_init, coords_goal);
47 
48  vector<unsigned int> init_points;
49  unsigned int idx, goal;
50  grid.coord2idx(coords_init, idx);
51  init_points.push_back(idx);
52  grid.coord2idx(coords_goal, goal);
53 
55  fmm.setEnvironment(&grid);
56  fmm.setInitialAndGoalPoints(init_points, goal);
57  fmm.compute();
58  cout << "\tElapsed FM time: " << fmm.getTime() << " ms" << endl;
59  console::info("Plotting the results and saving into test_fm.txt");
61  GridWriter::saveGridValues("test_fm.txt", grid);
62 
63  console::info("Computing gradient descent ");
64  typedef typename std::vector< std::array<double, ndims> > Path; // A bit of short-hand.
65 
66  Path path;
67  std::vector <double> path_velocity; // Velocities profile
68 
69  start = steady_clock::now();
71  grad.apply(grid,goal,path,path_velocity);
72  end = steady_clock::now();
73  time_elapsed = duration_cast<milliseconds>(end-start).count();
74  cout << "\tElapsed gradient descent time: " << time_elapsed << " ms" << endl;
75  GridWriter::savePath("test_path.txt", grid, path);
76  GridWriter::savePathVelocity("path_velocity.txt", grid, path, path_velocity);
77  GridPlotter::plotMapPath(grid,path);
78 
79  console::info("Testing Fast Marching Square Method.");
80  path_velocity.clear();
81  grid.coord2idx(coords_goal, goal);
82  Path pathFM2;
83 
84  MapLoader::loadMapFromImg(filename2.c_str(), grid);
85 
87  fm2.setEnvironment(&grid);
88  fm2.setInitialAndGoalPoints(init_points, goal);
89  fm2.compute();
90  cout << "\tElapsed FM2 time: " << fm2.getTime() << " ms" << endl;
91  start = steady_clock::now();
92  fm2.computePath(&pathFM2, &path_velocity);
93  end = steady_clock::now();
94  time_elapsed = duration_cast<milliseconds>(end-start).count();
95  cout << "\tElapsed gradient descent time: " << time_elapsed << " ms" << endl;
96 
97  GridPlotter::plotOccupancyPath(grid, pathFM2, fm2.getName());
98  GridPlotter::plotMapPath(grid, pathFM2, fm2.getName());
99 
100  console::info("Testing FM2*.");
101  Path pathFM2star;
103  fm2star.setEnvironment(&grid);
104  fm2star.setInitialAndGoalPoints(init_points, goal);
105  fm2star.compute();
106  cout << "\tElapsed FM2* time: " << fm2star.getTime() << " ms" << endl;
107  start = steady_clock::now();
108  fm2star.computePath(&pathFM2star, &path_velocity);
109  end = steady_clock::now();
110  time_elapsed = duration_cast<milliseconds>(end-start).count();
111  cout << "\tElapsed gradient descent time: " << time_elapsed << " ms" << endl;
112 
113  console::info("Comparing FM2, FM2* paths.");
114  std::vector <Path> paths;
115  paths.push_back(pathFM2);
116  paths.push_back(pathFM2star);
117 
118  GridPlotter::plotMapPaths(grid,paths);
119 
120  console::info("Now let's try different velocities.");
121  nDGridMap<FMCell, ndims> grid_vels;
122  MapLoader::loadMapFromImg(filename_vels.c_str(), grid_vels);
123 
125 
127  init_points.clear();
128  init_points.push_back(80000); // Just an init point as any other.
129  fmm_vels.setEnvironment(&grid_vels);
130  fmm_vels.setInitialPoints(init_points);
131  fmm_vels.compute();
132  cout << "\tElapsed FM time: " << fmm_vels.getTime() << " ms" << endl;
133 
134  console::info("Plotting the results ");
136 
137  console::info("Testing 3D!");
138  nDGridMap<FMCell, ndims3> grid3 (std::array<unsigned int,ndims3>{100,100,50});
139  init_points.clear();
140  grid3.coord2idx(std::array<unsigned int,ndims3>{50,50,25},idx); // Reusing the previous int.
141  init_points.push_back(idx);
142  grid3.coord2idx(std::array<unsigned int, ndims3> {20, 10, 45}, goal);
143 
145  fmm3.setEnvironment(&grid3);
146  fmm3.setInitialAndGoalPoints(init_points, goal);
147  fmm3.compute();
148  cout << "\tElapsed FM time: " << fmm3.getTime() << " ms" << endl;
149 
150  console::info("Saving into file test_fm3d.txt");
151  GridWriter::saveGridValues("test_fm3d.txt", grid3);
152 
153  console::info("Testing 3D gradient descent.");
154  typedef typename std::vector< std::array<double, ndims3> > Path3D; // A bit of short-hand.
155 
156  grid3.coord2idx(std::array<unsigned int, ndims3> {20, 10, 45}, goal);
157 
158  Path3D path3D;
159  start = steady_clock::now();
161  grad3D.apply(grid3,goal,path3D,path_velocity);
162  end = steady_clock::now();
163  time_elapsed = duration_cast<milliseconds>(end-start).count();
164  cout << "\tElapsed gradient descent time: " << time_elapsed << " ms" << endl;
165  GridWriter::savePath("test_path3d.txt", grid3, path3D);
166 
167  return 0;
168 }
static void savePathVelocity(const char *filename, nDGridMap< T, ndims > &grid, std::vector< std::array< double, ndims > > &path, std::vector< double > path_velocity)
Saves the 2D path with velocity values in an ASCII file with the following format: ...
Definition: gridwriter.hpp:144
virtual void setInitialAndGoalPoints(const std::vector< unsigned int > &init_points, unsigned int goal_idx)
Overloaded from FM2. In this case the precomputeDistances() method is called.
Definition: fm2star.hpp:70
static void plotOccupancyMap(nDGridMap< T, ndims > &grid, std::string name="")
Plots the occupancy map of a given grid. It is based on the nDGridMap::getOccupancy(), This function has to be overloaded if another occupancy type is being used.
Definition: gridplotter.hpp:84
virtual void setEnvironment(grid_t *g)
Sets and cleans the grid in which operations will be performed.
Definition: solver.hpp:51
static void saveGridValues(const char *filename, nDGridMap< T, ndims > &grid)
Saves grid values in ASCII format into the specified file.
Definition: gridwriter.hpp:48
static int parseArguments(int argc, const char **argv, const char *str, std::string &val)
static void selectMapPoints(nDGridMap< T, ndims > &grid, std::array< unsigned int, ndims > &coords_init, std::array< unsigned int, ndims > &coords_goal, const bool flipY=1)
Plots the binary map of a given grid and allows the user to select a couple of points (start and goal...
Definition: gridpoints.hpp:55
Implements gradient descent to be used together with nDGridMap and FMCell or derived types...
virtual void setEnvironment(grid_t *g)
Sets the environment to run the solver and sets the sources for the velocities map computation...
Definition: fm2.hpp:71
void compute()
Computes the distances map. Will call setup() if not done already.
Definition: solver.hpp:129
Implements the Fast Marching Square Star (FM2*) planning algorithm.
Definition: fm2star.hpp:51
Templated class which represents a n-dimensional grid map. Its cells are assumed to be cubic...
Definition: ndgridmap.hpp:47
virtual void computePath(path_t *p, std::vector< double > *path_velocity, double step=1)
Encapsulates the path extraction.
Definition: fm2.hpp:161
static void info(const std::string &val)
static void plotOccupancyPath(nDGridMap< T, ndims > &grid, const Path2D &path, std::string name="")
Plots the values map of a given grid. It is based on the nDGridMap::getValue(). This function has to ...
virtual void setInitialAndGoalPoints(const std::vector< unsigned int > &init_points, unsigned int goal_idx)
Sets the initial and goal points by the indices of the grid.
Definition: solver.hpp:58
static void plotMapPath(nDGridMap< T, ndims > &grid, const Path2D &path, std::string name="")
Plots the values map of a given grid. It is based on the nDGridMap::getValue(). This function has to ...
void clear()
Deallocates heap memory.
Definition: fmfibheap.hpp:85
void clear()
Erases the content of the grid. Must be resized later.
Definition: ndgridmap.hpp:291
virtual void setInitialPoints(const std::vector< unsigned int > &init_points)
Sets the initial points by the indices of the grid.
Definition: solver.hpp:65
static void apply(grid_t &grid, unsigned int &idx, Path &path, std::vector< double > &path_velocity, double step=1)
Computes the path over grid from idx to a minimum and extracts the velocity in every point...
static void plotArrivalTimes(nDGridMap< T, ndims > &grid, std::string name="")
Plots the values map of a given grid. It is based on the nDGridMap::getValue(). This function has to ...
static void loadMapFromImg(const char *filename, nDGridMap< T, ndims > &grid)
Loads the initial binary map for a given grid. It is based on the nDGridMap::setOccupancy() which has...
Definition: maploader.hpp:51
Implements the Fast Marching Square (FM2) planning algorithm.
Definition: fm2.hpp:49
static void savePath(const char *filename, nDGridMap< T, ndims > &grid, std::vector< std::array< double, ndims > > &path)
Saves the 2D path in an ASCII file with the following format:
Definition: gridwriter.hpp:112
static void plotMapPaths(nDGridMap< T, ndims > &grid, const Paths2D &paths, std::string name="")
Plots the values map of a given grid. It is based on the nDGridMap::getValue(). This function has to ...
unsigned int coord2idx(const std::array< unsigned int, ndims > &coords, unsigned int &idx)
Transforms from coordinates to index.
Definition: ndgridmap.hpp:213
Implements the Fast Marching Method (FMM).
Definition: fmm.hpp:64