n-Dimensional Fast Methods  0.7
 All Classes Functions Variables Typedefs Pages
solver.hpp
1 
23 #ifndef SOLVER_H_
24 #define SOLVER_H_
25 
26 #include <iostream>
27 #include <cmath>
28 #include <algorithm>
29 #include <numeric>
30 #include <fstream>
31 #include <array>
32 #include <chrono>
33 
34 #include <boost/concept_check.hpp>
35 
36 #include <fast_methods/console/console.h>
37 
39 template <class grid_t>
40 class Solver {
41 
42  public:
43  Solver() :name_("GenericSolver"), setup_(false) {}
44 
45  Solver(const std::string& name) : name_(name), setup_(false) {}
46 
47  virtual ~Solver() { clear(); }
48 
50  virtual void setEnvironment
51  (grid_t * g) {
52  grid_ = g;
53  grid_->clean();
54  }
55 
57  virtual void setInitialAndGoalPoints
58  (const std::vector<unsigned int> & init_points, unsigned int goal_idx) {
59  init_points_ = init_points;
60  goal_idx_ = goal_idx;
61  }
62 
64  virtual void setInitialPoints
65  (const std::vector<unsigned int> & init_points)
66  {
67  setInitialAndGoalPoints(init_points, -1);
68  }
69 
71  virtual void setInitialAndGoalPoints
72  (const std::array<unsigned int, grid_t::getNDims()> & init_coord, const std::array<unsigned int, grid_t::getNDims()> & goal_coord) {
73  std::vector<unsigned int> init_points;
74  unsigned int idx;
75  grid_->coord2idx(init_coord, idx);
76  init_points.push_back(idx);
77  grid_->coord2idx(goal_coord, idx);
78  setInitialAndGoalPoints(init_points, idx);
79  }
80 
82  virtual void setInitialPoints
83  (const std::array<unsigned int, grid_t::getNDims()> & init_coord)
84  {
85  std::vector<unsigned int> init_points;
86  unsigned int idx;
87  grid_->coord2idx(init_coord, idx);
88  init_points.push_back(idx);
89  setInitialAndGoalPoints(init_points, -1);
90  }
91 
93  virtual void setup
94  () {
95  const int err = sanityChecks();
96  if (err)
97  {
98  console::error("Global sanity checks not successful: ");
99  switch(err) {
100  case 1:
101  console::error("No grid map set.");
102  break;
103  case 2:
104  console::error("Grid map set is not clean.");
105  break;
106  case 3:
107  console::error("Initial points were not set.");
108  break;
109  case 4:
110  console::error("A init point is in a obstacle.");
111  break;
112  case 5:
113  console::error("A goal point is in a obstacle.");
114  break;
115  case 6:
116  console::error("A start is equal to a goal point.");
117  break;
118  default:
119  console::error("Uknown error.");
120  }
121  exit(1);
122  }
123  grid_->setClean(false);
124  setup_ = true;
125  }
126 
128  void compute
129  () {
130  start_ = std::chrono::steady_clock::now();
131  computeInternal();
132  end_ = std::chrono::steady_clock::now();
133  time_ = std::chrono::duration_cast<std::chrono::milliseconds>(end_-start_).count();
134  }
135 
137  virtual void computeInternal() = 0;
138 
140  template<class T>
141  T* as
142  () {
143  // OMPL-inspired function.
144  /* Make sure the type we are casting to is a solver */
145  BOOST_CONCEPT_ASSERT((boost::Convertible<T*, Solver*>));
146  return static_cast<T*>(this);
147  }
148 
150  template<class T>
151  const T* as
152  () const {
153  // OMPL-inspired function.
154  /* Make sure the type we are casting to is a solver */
155  BOOST_CONCEPT_ASSERT((boost::Convertible<T*, Solver*>));
156  return static_cast<const T*>(this);
157  }
158 
159 
161  const std::string& getName() const
162  {
163  return name_;
164  }
165 
167  virtual void clear
168  () {
169  init_points_.clear();
170  goal_idx_ = -1;
171  setup_ = false;
172  }
173 
175  virtual void reset
176  () {
177  setup_ = false;
178  grid_->clean();
179  }
180 
182  grid_t* getGrid() const
183  {
184  return grid_;
185  }
186 
187  virtual double getTime
188  () const {
189  return time_;
190  }
191 
192  virtual void printRunInfo
193  () const {
194  console::warning("No run info available.");
195  }
196 
197  protected:
199  int sanityChecks
200  () {
201  if (grid_ == NULL) return 1;
202  if (!grid_->isClean()) return 2;
203  if (init_points_.empty()) return 3;
204 
205  // When more that 1 initial point is given, this check is ommitted
206  // since it could be FM2-like velocities map computation.
207  if (init_points_.size() == 1 &&
208  grid_->getCell(init_points_[0]).isOccupied()) return 4;
209 
210  if(int(goal_idx_) != -1 && grid_->getCell(goal_idx_).isOccupied()) return 5;
211 
212  for (int ip : init_points_)
213  if(int(goal_idx_) == ip) return 6;
214 
215  return 0;
216  }
217 
219  grid_t* grid_;
220 
222  std::string name_;
223 
225  bool setup_;
226 
228  std::vector<unsigned int> init_points_;
229 
231  unsigned int goal_idx_;
232 
234  std::chrono::time_point<std::chrono::steady_clock> start_, end_;
235 
237  double time_;
238 };
239 
240 #endif /* SOLVER_H_*/
double time_
Time elapsed by the compute method.
Definition: solver.hpp:237
virtual void setEnvironment(grid_t *g)
Sets and cleans the grid in which operations will be performed.
Definition: solver.hpp:51
virtual void computeInternal()=0
Actual compute function to be implemented in each solver.
virtual void setup()
Checks that the solver is ready to run. Sets the grid unclean.
Definition: solver.hpp:94
T * as()
Cast this instance to a desired type.
Definition: solver.hpp:142
static void error(const std::string &val)
const std::string & getName() const
Returns name of the solver.
Definition: solver.hpp:161
void compute()
Computes the distances map. Will call setup() if not done already.
Definition: solver.hpp:129
int sanityChecks()
Performs different check before a solver can proceed.
Definition: solver.hpp:200
grid_t * grid_
Grid container.
Definition: solver.hpp:219
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
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::string name_
Solver name.
Definition: solver.hpp:222
bool setup_
Setup status.
Definition: solver.hpp:225
virtual void setInitialPoints(const std::vector< unsigned int > &init_points)
Sets the initial points by the indices of the grid.
Definition: solver.hpp:65
Abstract class that serves as interface for the actual solvers implemented. It requires (at least) th...
Definition: solver.hpp:40
std::chrono::time_point< std::chrono::steady_clock > start_
Time measurement variables.
Definition: solver.hpp:234
unsigned int goal_idx_
Goal index.
Definition: solver.hpp:231
static void warning(const std::string &val)
grid_t * getGrid() const
Returns a pointer to the grid used.
Definition: solver.hpp:182
virtual void reset()
Clears temporal data, so it is ready to run again.
Definition: solver.hpp:176