n-Dimensional Fast Methods  0.7
 All Classes Functions Variables Typedefs Pages
test_fm.cpp
1 /* Runs different versions of FMM over an empty, generated grid. */
2 
3 #include <iostream>
4 #include <array>
5 
6 #include <fast_methods/ndgridmap/fmcell.h>
7 #include <fast_methods/ndgridmap/ndgridmap.hpp>
8 
9 #include <fast_methods/fm/fmm.hpp>
10 #include <fast_methods/fm/sfmm.hpp>
11 #include <fast_methods/fm/sfmmstar.hpp>
12 #include <fast_methods/fm/fmmstar.hpp>
13 #include <fast_methods/datastructures/fmfibheap.hpp>
14 #include <fast_methods/datastructures/fmpriorityqueue.hpp>
15 #include <fast_methods/fm/fim.hpp>
16 #include <fast_methods/fm/gmm.hpp>
17 #include <fast_methods/fm/ufmm.hpp>
18 #include <fast_methods/fm/fsm.hpp>
19 #include <fast_methods/fm/lsm.hpp>
20 #include <fast_methods/fm/ddqm.hpp>
21 
22 #include <fast_methods/io/gridplotter.hpp>
23 
24 using namespace std;
25 using namespace std::chrono;
26 
27 int main()
28 {
29  // A bit of shorthand.
30  typedef nDGridMap<FMCell, 2> FMGrid2D;
31  typedef array<unsigned int, 2> Coord2D;
32 
33  // Grid, start and goal definition.
34  Coord2D dimsize {300,300};
35  FMGrid2D grid_fmm (dimsize);
36  Coord2D init_point = {150, 150};
37  Coord2D goal_point = {250, 250};
38 
39  // Solvers declaration.
40  std::vector<Solver<FMGrid2D>*> solvers;
41  solvers.push_back(new FMM<FMGrid2D>);
42  solvers.push_back(new FMMStar<FMGrid2D>);
43  solvers.push_back(new FMMStar<FMGrid2D>("FMM*_Dist", DISTANCE));
44  solvers.push_back(new FMM<FMGrid2D, FMFibHeap<FMCell> >("FMFib"));
45  solvers.push_back(new FMMStar<FMGrid2D, FMFibHeap<FMCell> >("FMM*Fib"));
46  solvers.push_back(new FMMStar<FMGrid2D, FMFibHeap<FMCell> >("FMM*Fib_Dist", DISTANCE));
47  solvers.push_back(new SFMM<FMGrid2D>);
48  solvers.push_back(new SFMMStar<FMGrid2D>);
49  solvers.push_back(new SFMMStar<FMGrid2D>("SFMM*_Dist", DISTANCE));
50  solvers.push_back(new GMM<FMGrid2D>);
51  solvers.push_back(new FIM<FMGrid2D>);
52  solvers.push_back(new UFMM<FMGrid2D>);
53  solvers.push_back(new FSM<FMGrid2D>);
54  solvers.push_back(new LSM<FMGrid2D>);
55  solvers.push_back(new DDQM<FMGrid2D>);
56  // GMM, FIM and UFMM have some parameters, can be set by overloaded constructors.
57 
58  // Executing every solver individually over the same grid.
59  for (Solver<FMGrid2D>* s :solvers)
60  {
61  s->setEnvironment(&grid_fmm);
62  //s->setInitialPoints(init_point); // If no goal_idx is set. Comment next line if this one is uncommented.
63  s->setInitialAndGoalPoints(init_point, goal_point);
64  s->setup(); // Not mandatory, but in FMMstar we want to precompute distances
65  // out of compute().
66  s->compute();
67  cout << "\tElapsed "<< s->getName() <<" time: " << s->getTime() << " ms" << '\n';
68  GridPlotter::plotArrivalTimes(grid_fmm, s->getName());
69  }
70 
71  // Showing different type conversions methodologies. Solver names could be compared
72  // but it is less reliable since they are user-defined.
73  // Direct cast
74  FMM<FMGrid2D>* FMMtest = dynamic_cast< FMM<FMGrid2D>* >(solvers[6]);
75  if (FMMtest)
76  std::cout << "Solver type SFMM" <<'\n';
77  else
78  std::cout << "Solver NOT type SFMM" <<'\n';
79 
80  FMM<FMGrid2D, FMPriorityQueue<FMCell>>* FMMtest2 = dynamic_cast< FMM<FMGrid2D, FMPriorityQueue<FMCell>>* >(solvers[6]);
81  if (FMMtest2)
82  std::cout << "Solver type SFMM" <<'\n';
83  else
84  std::cout << "Solver NOT type SFMM" <<'\n';
85 
86  // Solver::as member function.
87  // solvers[6]->setHeuristics(true) would not compile since there is not a Solver::setHeuristics
88  solvers[6]->as< FMM<FMGrid2D, FMPriorityQueue<FMCell>> >()->setHeuristics(HeurStrategy::DISTANCE);
89  std::cout << solvers[6]->as< FMM<FMGrid2D, FMPriorityQueue<FMCell>> >()->getHeuristics() << '\n';
90 
91  // Preventing memory leaks.
92  for (auto & s : solvers)
93  delete s;
94 
95  return 0;
96 }
Implements Fast Sweeping Method.
Definition: fsm.hpp:41
Fast Marching Method using a untidy priority queue (UFMM).
Definition: ufmm.hpp:37
T * as()
Cast this instance to a desired type.
Definition: solver.hpp:142
Encapsulates the calls to FMM with heuristics enabled.
Definition: fmmstar.hpp:61
Implements the Simplified Fast Marching Method, encapsulating FMM with a priority queue...
Definition: sfmm.hpp:44
Templated class which represents a n-dimensional grid map. Its cells are assumed to be cubic...
Definition: ndgridmap.hpp:47
Implements Double Dynamic Queue Method.
Definition: ddqm.hpp:41
Abstract class that serves as interface for the actual solvers implemented. It requires (at least) th...
Definition: solver.hpp:40
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 ...
Implements Lock Sweeping Method.
Definition: lsm.hpp:40
Implements Fast Iterative Method.
Definition: fim.hpp:36
Templated class which computes Group Marching Method (GMM).
Definition: gmm.hpp:33
Implements the Fast Marching Method (FMM).
Definition: fmm.hpp:64