n-Dimensional Fast Methods  0.7
 All Classes Functions Variables Typedefs Pages
fm2.hpp
1 
32 #ifndef FM2_HPP_
33 #define FM2_HPP_
34 
35 #include <iostream>
36 #include <cmath>
37 #include <algorithm>
38 #include <numeric>
39 #include <fstream>
40 #include <array>
41 #include <limits>
42 
43 #include <fast_methods/fm/fmm.hpp>
44 #include <fast_methods/gradientdescent/gradientdescent.hpp>
45 
47 //template < class grid_t, class solver_t = FMM<grid_t> > class FM2 : public Solver<grid_t> {
48 
49 template < class grid_t, class heap_t = FMDaryHeap<FMCell> > class FM2 : public Solver<grid_t> {
50  public:
51 
53  typedef std::vector< std::array<double, grid_t::getNDims()> > path_t;
54 
56  FM2
57  (double maxDistance = -1) : Solver<grid_t>("FM2"), maxDistance_(maxDistance) {
59  }
60 
62  FM2
63  (const char * name, double maxDistance = -1) : Solver<grid_t>(name), maxDistance_(maxDistance) {
65  }
66 
67  virtual ~FM2 () { clear(); }
68 
70  virtual void setEnvironment
71  (grid_t * g) {
73  grid_->getOccupiedCells(fm2_sources_);
74  solver_->setEnvironment(grid_);
75  }
76 
78  virtual void setup
79  () {
81 
82  if(init_points_.size() > 1) {
83  console::error("FM2-based solvers currently allow only 1 initial point.");
84  exit(1);
85  }
86 
87  if (fm2_sources_.empty()) {
88  console::error("Map has no obstacles. FM2-based solver is not running.");
89  exit(1);
90  }
91  }
92 
94  virtual void computeInternal
95  () {
96  if (!setup_)
97  setup();
98 
100 
101  // Reset time counter so that time_ returns the time of the second wave.
102  start_ = std::chrono::steady_clock::now();
103 
104  // According to the theoretical basis the wave is expanded from the goal point to the initial point.
105  std::vector <unsigned int> wave_init;
106  wave_init.push_back(goal_idx_);
107  unsigned int wave_goal = init_points_[0];
108 
109  solver_->setInitialAndGoalPoints(wave_init, wave_goal);
110  solver_->compute();
111  // Restore the actual grid status.
112  grid_->setClean(false);
113  }
114 
118  () {
119  // Forces not to clean the grid.
120  grid_->setClean(true);
121  solver_->setInitialPoints(fm2_sources_);
122  solver_->compute();
123  time_vels_ = solver_->getTime();
124  start_ = std::chrono::steady_clock::now();
125  // Rescaling and saturating to relative velocities: [0,1]
126  double maxValue = grid_->getMaxValue();
127  double maxVelocity = 0;
128 
129  if (maxDistance_ != -1)
130  maxVelocity = maxDistance_ / grid_->getLeafSize();
131 
132  for (unsigned int i = 0; i < grid_->size(); ++i) {
133  double vel = grid_->getCell(i).getValue() / maxValue;
134 
135  if (maxDistance_ != -1)
136  if (vel < maxVelocity)
137  grid_->getCell(i).setVelocity(vel / maxVelocity);
138  else
139  grid_->getCell(i).setVelocity(1);
140  else
141  grid_->getCell(i).setVelocity(vel);
142 
143  // Restarting grid values for second wave expasion.
144  grid_->getCell(i).setValue(std::numeric_limits<double>::infinity());
145  grid_->getCell(i).setState(FMState::OPEN);
146  grid_->setClean(true);
147  }
148  end_ = std::chrono::steady_clock::now();
149  time_vels_ += std::chrono::duration_cast<std::chrono::milliseconds>(end_-start_).count();
150  }
151 
160  virtual void computePath
161  (path_t * p, std::vector <double> * path_velocity, double step = 1) {
162  path_t* path_ = p;
164  grad.apply(*grid_,init_points_[0],*path_, *path_velocity, step);
165  }
166 
167  virtual void clear
168  () {
170  fm2_sources_.clear();
171  maxDistance_ = -1;
172  delete solver_;
173  }
174 
175  virtual void reset
176  () {
178  solver_->reset();
179  }
180 
182  virtual double getTimeVelocities
183  () const {
184  return time_vels_;
185  }
186 
187  protected:
188  using Solver<grid_t>::grid_;
192  using Solver<grid_t>::time_;
194  using Solver<grid_t>::end_;
195 
197  std::vector<unsigned int> fm2_sources_;
198 
201 
203  double maxDistance_;
204 
206  double time_vels_;
207 };
208 
209 #endif /* FM2_H_*/
FMM< grid_t, heap_t > * solver_
Underlying FMM-based solver.
Definition: fm2.hpp:200
virtual void setEnvironment(grid_t *g)
Sets and cleans the grid in which operations will be performed.
Definition: solver.hpp:51
virtual void setup()
Checks that the solver is ready to run. Sets the grid unclean.
Definition: solver.hpp:94
virtual void computeInternal()
Implements the actual FM2 method.
Definition: fm2.hpp:95
virtual void reset()
Clears temporal data, so it is ready to run again.
Definition: fm2.hpp:176
static void error(const std::string &val)
void computeVelocitiesMap()
Computes the velocities map of the FM2 algorithm. If maxDistance_ != -1 then the map is saturated to ...
Definition: fm2.hpp:118
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
virtual double getTimeVelocities() const
Returns velocities map computation time.
Definition: fm2.hpp:183
grid_t * grid_
Grid container.
Definition: solver.hpp:219
virtual void computePath(path_t *p, std::vector< double > *path_velocity, double step=1)
Encapsulates the path extraction.
Definition: fm2.hpp:161
double maxDistance_
Distance value to saturate the first potential.
Definition: fm2.hpp:203
std::vector< unsigned int > init_points_
Initial index.
Definition: solver.hpp:228
virtual void clear()
Clears the solver, it is not recommended to be used out of the destructor.
Definition: solver.hpp:168
std::vector< unsigned int > fm2_sources_
Wave propagation sources for the Fast Marching Square velocities map computation. ...
Definition: fm2.hpp:197
bool setup_
Setup status.
Definition: solver.hpp:225
Abstract class that serves as interface for the actual solvers implemented. It requires (at least) th...
Definition: solver.hpp:40
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...
virtual void setup()
Sets up the solver to check whether is ready to run.
Definition: fm2.hpp:79
std::chrono::time_point< std::chrono::steady_clock > start_
Time measurement variables.
Definition: solver.hpp:234
std::vector< std::array< double, grid_t::getNDims()> > path_t
Path type encapsulation.
Definition: fm2.hpp:53
Implements the Fast Marching Square (FM2) planning algorithm.
Definition: fm2.hpp:49
unsigned int goal_idx_
Goal index.
Definition: solver.hpp:231
double time_vels_
Time elapsed in the velocities map computation.
Definition: fm2.hpp:206
Implements the Fast Marching Method (FMM).
Definition: fmm.hpp:64
FM2(double maxDistance=-1)
maxDistance sets the velocities map saturation distance in real units (before normalization).
Definition: fm2.hpp:57
virtual void clear()
Clears the solver, it is not recommended to be used out of the destructor.
Definition: fm2.hpp:168
virtual void reset()
Clears temporal data, so it is ready to run again.
Definition: solver.hpp:176