MRSL JPS3D Library  1.1
An implementaion of Jump Point Search on 3D voxel map
graph_search.h
1 
6 #ifndef DMP_GRAPH_SEARCH_H
7 #define DMP_GRAPH_SEARCH_H
8 
9 #include <boost/heap/d_ary_heap.hpp> // boost::heap::d_ary_heap
10 #include <memory> // std::shared_ptr
11 #include <limits> // std::numeric_limits
12 #include <vector> // std::vector
13 #include <unordered_map> // std::unordered_map
14 
15 namespace DMP
16 {
18  template <class T>
20  {
21  bool operator()(T a1, T a2) const
22  {
23  double f1 = a1->g + a1->h;
24  double f2 = a2->g + a2->h;
25  if( ( f1 >= f2 - 0.000001) && (f1 <= f2 +0.000001) )
26  return a1->g < a2->g; // if equal compare gvals
27  return f1 > f2;
28  }
29  };
30 
31 
33  struct State; // forward declaration
35  using StatePtr = std::shared_ptr<State>;
36  using priorityQueue = boost::heap::d_ary_heap<StatePtr, boost::heap::mutable_<true>,
37  boost::heap::arity<2>, boost::heap::compare< compare_state<StatePtr> >>;
38 
40  struct State
41  {
43  int id;
45  int x, y, z = 0;
47  int parentId = -1;
48 
50  priorityQueue::handle_type heapkey;
51 
53  double g = std::numeric_limits<double>::infinity();
55  double h;
57  bool opened = false;
59  bool closed = false;
60 
62  State(int id, int x, int y)
63  : id(id), x(x), y(y)
64  {}
65 
67  State(int id, int x, int y, int z)
68  : id(id), x(x), y(y), z(z)
69  {}
70 
71  };
72 
73 
80  {
81  public:
92  GraphSearch(const int8_t* cMap, int xDim, int yDim, double eps = 1, double cweight = 0.1, bool verbose = false);
104  GraphSearch(const int8_t* cMap, int xDim, int yDim, int zDim, double eps = 1, double cweight = 0.1, bool verbose = false);
105 
117  double plan(int xStart, int yStart, int xGoal, int yGoal, std::vector<bool> in_region = std::vector<bool>());
131  double plan(int xStart, int yStart, int zStart, int xGoal, int yGoal, int zGoal, std::vector<bool> in_region = std::vector<bool>());
132 
134  std::vector<StatePtr> getPath() const;
135 
137  std::vector<StatePtr> getOpenSet() const;
138 
140  std::vector<StatePtr> getCloseSet() const;
141 
143  std::vector<StatePtr> getAllSet() const;
144 
145  private:
147  double plan(StatePtr& currNode_ptr, int start_id, int goal_id);
149  void getSucc(const StatePtr& curr, std::vector<int>& succ_ids, std::vector<double>& succ_costs);
151  std::vector<StatePtr> recoverPath(StatePtr node, int id);
152 
154  int coordToId(int x, int y) const;
156  int coordToId(int x, int y, int z) const;
157 
159  bool isFree(int x, int y) const;
161  bool isFree(int x, int y, int z) const;
162 
164  double getHeur(int x, int y) const;
166  double getHeur(int x, int y, int z) const;
167 
168  const int8_t* cMap_;
169  int xDim_, yDim_, zDim_;
171  double eps_;
173  double cweight_;
174  bool verbose_;
175 
176  const int8_t val_free_ = 0;
177  const int8_t val_occ_ = 100;
178  int xGoal_, yGoal_, zGoal_;
179  bool use_2d_;
180  bool global_;
181 
182  priorityQueue pq_;
183  std::vector<StatePtr> hm_;
184  std::vector<bool> seen_;
185  std::vector<bool> in_region_;
186 
187  std::vector<StatePtr> path_;
188 
189  std::vector<std::vector<int>> ns_;
190  };
191 }
192 #endif
GraphSearch class.
Definition: graph_search.h:79
double h
heuristic cost
Definition: graph_search.h:55
priorityQueue::handle_type heapkey
pointer to heap location
Definition: graph_search.h:50
double eps_
weight of heuristic
Definition: graph_search.h:171
int x
Coord.
Definition: graph_search.h:45
Definition: graph_search.h:15
Node of the graph in graph search.
Definition: graph_search.h:40
State(int id, int x, int y, int z)
3D constructor
Definition: graph_search.h:67
double cweight_
weight of distance map
Definition: graph_search.h:173
Heap element comparison.
Definition: graph_search.h:19
int id
ID.
Definition: graph_search.h:43
State(int id, int x, int y)
2D constructor
Definition: graph_search.h:62