MRSL JPS3D Library  1.1
An implementaion of Jump Point Search on 3D voxel map
graph_search.h
1 
6 #ifndef JPS_GRAPH_SEARCH_H
7 #define JPS_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 JPS
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 dx, dy, dz; // discrete coordinates of this node
49  int parentId = -1;
50 
52  priorityQueue::handle_type heapkey;
53 
55  double g = std::numeric_limits<double>::infinity();
57  double h;
59  bool opened = false;
61  bool closed = false;
62 
64  State(int id, int x, int y, int dx, int dy )
65  : id(id), x(x), y(y), dx(dx), dy(dy)
66  {}
67 
69  State(int id, int x, int y, int z, int dx, int dy, int dz )
70  : id(id), x(x), y(y), z(z), dx(dx), dy(dy), dz(dz)
71  {}
72 
73  };
74 
76  struct JPS2DNeib {
77  // for each (dx,dy) these contain:
78  // ns: neighbors that are always added
79  // f1: forced neighbors to check
80  // f2: neighbors to add if f1 is forced
81  int ns[9][2][8];
82  int f1[9][2][2];
83  int f2[9][2][2];
84  // nsz contains the number of neighbors for the four different types of moves:
85  // no move (norm 0): 8 neighbors always added
86  // 0 forced neighbors to check (never happens)
87  // 0 neighbors to add if forced (never happens)
88  // straight (norm 1): 1 neighbor always added
89  // 2 forced neighbors to check
90  // 2 neighbors to add if forced
91  // diagonal (norm sqrt(2)): 3 neighbors always added
92  // 2 forced neighbors to check
93  // 2 neighbors to add if forced
94  static constexpr int nsz[3][2] = {{8, 0}, {1, 2}, {3, 2}};
95 
96  void print();
97  JPS2DNeib();
98  private:
99  void Neib(int dx, int dy, int norm1, int dev, int& tx, int& ty);
100  void FNeib(int dx, int dy, int norm1, int dev,
101  int& fx, int& fy, int& nx, int& ny);
102  };
103 
104 
106  struct JPS3DNeib {
107  // for each (dx,dy,dz) these contain:
108  // ns: neighbors that are always added
109  // f1: forced neighbors to check
110  // f2: neighbors to add if f1 is forced
111  int ns[27][3][26];
112  int f1[27][3][12];
113  int f2[27][3][12];
114  // nsz contains the number of neighbors for the four different types of moves:
115  // no move (norm 0): 26 neighbors always added
116  // 0 forced neighbors to check (never happens)
117  // 0 neighbors to add if forced (never happens)
118  // straight (norm 1): 1 neighbor always added
119  // 8 forced neighbors to check
120  // 8 neighbors to add if forced
121  // diagonal (norm sqrt(2)): 3 neighbors always added
122  // 8 forced neighbors to check
123  // 12 neighbors to add if forced
124  // diagonal (norm sqrt(3)): 7 neighbors always added
125  // 6 forced neighbors to check
126  // 12 neighbors to add if forced
127  static constexpr int nsz[4][2] = {{26, 0}, {1, 8}, {3, 12}, {7, 12}};
128  JPS3DNeib();
129  private:
130  void Neib(int dx, int dy, int dz, int norm1, int dev, int& tx, int& ty, int& tz);
131  void FNeib( int dx, int dy, int dz, int norm1, int dev,
132  int& fx, int& fy, int& fz,
133  int& nx, int& ny, int& nz);
134  };
135 
136 
143  {
144  public:
154  GraphSearch(const char* cMap, int xDim, int yDim, double eps = 1, bool verbose = false);
165  GraphSearch(const char* cMap, int xDim, int yDim, int zDim, double eps = 1, bool verbose = false);
166 
177  bool plan(int xStart, int yStart, int xGoal, int yGoal, bool useJps, int maxExpand = -1);
190  bool plan(int xStart, int yStart, int zStart, int xGoal, int yGoal, int zGoal, bool useJps, int maxExpand = -1);
191 
193  std::vector<StatePtr> getPath() const;
194 
196  std::vector<StatePtr> getOpenSet() const;
197 
199  std::vector<StatePtr> getCloseSet() const;
200 
202  std::vector<StatePtr> getAllSet() const;
203 
204  private:
206  bool plan(StatePtr& currNode_ptr, int max_expand, int start_id, int goal_id);
208  void getSucc(const StatePtr& curr, std::vector<int>& succ_ids, std::vector<double>& succ_costs);
210  void getJpsSucc(const StatePtr& curr, std::vector<int>& succ_ids, std::vector<double>& succ_costs);
212  std::vector<StatePtr> recoverPath(StatePtr node, int id);
213 
215  int coordToId(int x, int y) const;
217  int coordToId(int x, int y, int z) const;
218 
220  bool isFree(int x, int y) const;
222  bool isFree(int x, int y, int z) const;
223 
225  bool isOccupied(int x, int y) const;
227  bool isOccupied(int x, int y, int z) const;
228 
230  double getHeur(int x, int y) const;
232  double getHeur(int x, int y, int z) const;
233 
235  bool hasForced(int x, int y, int dx, int dy);
237  bool hasForced(int x, int y, int z, int dx, int dy, int dz);
238 
240  bool jump(int x, int y, int dx, int dy, int& new_x, int& new_y);
242  bool jump(int x, int y, int z, int dx, int dy, int dz, int& new_x, int& new_y, int& new_z);
243 
245  void init2DJps();
246 
247  const char* cMap_;
248  int xDim_, yDim_, zDim_;
249  double eps_;
250  bool verbose_;
251 
252  const char val_free_ = 0;
253  int xGoal_, yGoal_, zGoal_;
254  bool use_2d_;
255  bool use_jps_ = false;
256 
257  priorityQueue pq_;
258  std::vector<StatePtr> hm_;
259  std::vector<bool> seen_;
260 
261  std::vector<StatePtr> path_;
262 
263  std::vector<std::vector<int>> ns_;
264  std::shared_ptr<JPS2DNeib> jn2d_;
265  std::shared_ptr<JPS3DNeib> jn3d_;
266  };
267 }
268 #endif
double h
heuristic cost
Definition: graph_search.h:57
Definition: map_util.h:11
State(int id, int x, int y, int dx, int dy)
2D constructor
Definition: graph_search.h:64
int x
Coord.
Definition: graph_search.h:45
Search and prune neighbors for JPS 2D.
Definition: graph_search.h:76
int dx
direction
Definition: graph_search.h:47
Search and prune neighbors for JPS 3D.
Definition: graph_search.h:106
Heap element comparison.
Definition: graph_search.h:19
int id
ID.
Definition: graph_search.h:43
priorityQueue::handle_type heapkey
pointer to heap location
Definition: graph_search.h:52
GraphSearch class.
Definition: graph_search.h:142
State(int id, int x, int y, int z, int dx, int dy, int dz)
3D constructor
Definition: graph_search.h:69
Node of the graph in graph search.
Definition: graph_search.h:40