forked from Ohohcakester/Any-Angle-Pathfinding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpPointSearch.java
More file actions
360 lines (326 loc) · 12.2 KB
/
Copy pathJumpPointSearch.java
File metadata and controls
360 lines (326 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package algorithms;
import grid.GridGraph;
import algorithms.priorityqueue.ReusableIndirectHeap;
public final class JumpPointSearch extends AStarStaticMemory {
private final int[] neighboursdX;
private final int[] neighboursdY;
private int neighbourCount;
public JumpPointSearch(GridGraph graph, int sx, int sy, int ex, int ey) {
super(graph, sx, sy, ex, ey);
neighboursdX = new int[8];
neighboursdY = new int[8];
}
public static final JumpPointSearch postSmooth(GridGraph graph, int sx, int sy, int ex, int ey) {
JumpPointSearch algo = new JumpPointSearch(graph, sx, sy, ex, ey);
algo.postSmoothingOn = true;
algo.repeatedPostSmooth = false;
return algo;
}
public static final JumpPointSearch repeatedPostSmooth(GridGraph graph, int sx, int sy, int ex, int ey) {
JumpPointSearch algo = new JumpPointSearch(graph, sx, sy, ex, ey);
algo.postSmoothingOn = true;
algo.repeatedPostSmooth = true;
return algo;
}
@Override
public final void computePath() {
neighbourCount = 0;
int totalSize = (graph.sizeX+1) * (graph.sizeY+1);
int start = graph.toOneDimIndex(sx, sy);
finish = graph.toOneDimIndex(ex, ey);
pq = new ReusableIndirectHeap(totalSize);
this.initialiseMemory(totalSize, Float.POSITIVE_INFINITY, -1, false);
initialise(start);
while (!pq.isEmpty()) {
int current = pq.popMinIndex();
if (current == finish || distance(current) == Float.POSITIVE_INFINITY) {
maybeSaveSearchSnapshot();
break;
}
setVisited(current, true);
int x = graph.toTwoDimX(current);
int y = graph.toTwoDimY(current);
computeNeighbours(current, x, y); // stores neighbours in attribute.
for (int i=0;i<neighbourCount;++i) {
int dx = neighboursdX[i];
int dy = neighboursdY[i];
int successor = jump(x, y, dx, dy);
if (successor != -1) {
tryRelax(current, x, y, successor);
}
}
maybeSaveSearchSnapshot();
}
maybePostSmooth();
}
private final int jump(int x, int y, int dx, int dy) {
if (dx < 0) {
if (dy < 0) {
return jumpDL(x,y);
} else if (dy > 0) {
return jumpUL(x,y);
} else {
return jumpL(x,y);
}
} else if (dx > 0) {
if (dy < 0) {
return jumpDR(x,y);
} else if (dy > 0) {
return jumpUR(x,y);
} else {
return jumpR(x,y);
}
} else {
if (dy < 0) {
return jumpD(x,y);
} else {
return jumpU(x,y);
}
}
}
private final int jumpDL(int x, int y) {
while(true) {
x -= 1;
y -= 1;
if (graph.isBlocked(x, y)) return -1;
if (x == ex && y == ey) return graph.toOneDimIndex(x,y);
// diagonal cannot be forced on vertices.
if (jumpL(x,y) != -1) return graph.toOneDimIndex(x,y);
if (jumpD(x,y) != -1) return graph.toOneDimIndex(x,y);
}
}
private final int jumpDR(int x, int y) {
while(true) {
x += 1;
y -= 1;
if (graph.isBlocked(x-1, y)) return -1;
if (x == ex && y == ey) return graph.toOneDimIndex(x,y);
// diagonal cannot be forced on vertices.
if (jumpD(x,y) != -1) return graph.toOneDimIndex(x,y);
if (jumpR(x,y) != -1) return graph.toOneDimIndex(x,y);
}
}
private final int jumpUL(int x, int y) {
while(true) {
x -= 1;
y += 1;
if (graph.isBlocked(x, y-1)) return -1;
if (x == ex && y == ey) return graph.toOneDimIndex(x,y);
// diagonal cannot be forced on vertices.
if (jumpL(x,y) != -1) return graph.toOneDimIndex(x,y);
if (jumpU(x,y) != -1) return graph.toOneDimIndex(x,y);
}
}
private final int jumpUR(int x, int y) {
while(true) {
x += 1;
y += 1;
if (graph.isBlocked(x-1, y-1)) return -1;
if (x == ex && y == ey) return graph.toOneDimIndex(x,y);
// diagonal cannot be forced on vertices.
if (jumpU(x,y) != -1) return graph.toOneDimIndex(x,y);
if (jumpR(x,y) != -1) return graph.toOneDimIndex(x,y);
}
}
private final int jumpL(int x, int y) {
while(true) {
x -= 1;
if (graph.isBlocked(x, y)) {
if (graph.isBlocked(x, y-1)) {
return -1;
} else {
if (!graph.isBlocked(x-1, y)) return graph.toOneDimIndex(x,y);
}
}
if (graph.isBlocked(x, y-1)) {
if (!graph.isBlocked(x-1, y-1)) return graph.toOneDimIndex(x,y);
}
if (x == ex && y == ey) return graph.toOneDimIndex(x,y);
}
}
private final int jumpR(int x, int y) {
while(true) {
x += 1;
if (graph.isBlocked(x-1, y)) {
if (graph.isBlocked(x-1, y-1)) {
return -1;
} else {
if (!graph.isBlocked(x, y)) return graph.toOneDimIndex(x,y);
}
}
if (graph.isBlocked(x-1, y-1)) {
if (!graph.isBlocked(x, y-1)) return graph.toOneDimIndex(x,y);
}
if (x == ex && y == ey) return graph.toOneDimIndex(x,y);
}
}
private final int jumpD(int x, int y) {
while(true) {
y -= 1;
if (graph.isBlocked(x, y)) {
if (graph.isBlocked(x-1, y)) {
return -1;
} else {
if (!graph.isBlocked(x, y-1)) return graph.toOneDimIndex(x,y);
}
}
if (graph.isBlocked(x-1, y)) {
if (!graph.isBlocked(x-1, y-1)) return graph.toOneDimIndex(x,y);
}
if (x == ex && y == ey) return graph.toOneDimIndex(x,y);
}
}
private final int jumpU(int x, int y) {
while(true) {
y += 1;
if (graph.isBlocked(x, y-1)) {
if (graph.isBlocked(x-1, y-1)) {
return -1;
} else {
if (!graph.isBlocked(x, y)) return graph.toOneDimIndex(x,y);
}
}
if (graph.isBlocked(x-1, y-1)) {
if (!graph.isBlocked(x-1, y)) return graph.toOneDimIndex(x,y);
}
if (x == ex && y == ey) return graph.toOneDimIndex(x,y);
}
}
private final void computeNeighbours(int currentIndex, int cx, int cy) {
neighbourCount = 0;
int parentIndex = parent(currentIndex);
if (parentIndex == -1) {
// is start node.
for (int y=-1;y<=1;++y) {
for (int x=-1;x<=1;++x) {
if (x == 0 && y == 0) continue;
int px = cx+x;
int py = cy+y;
if (graph.neighbourLineOfSight(cx,cy,px,py)) {
addNeighbour(x, y);
}
}
}
return;
}
int dirX = cx - graph.toTwoDimX(parentIndex);
int dirY = cy - graph.toTwoDimY(parentIndex);
if (dirX < 0) {
if (dirY < 0) {
// down-left
if (!graph.isBlocked(cx-1, cy-1)) {
addNeighbour(-1,-1);
addNeighbour(-1,0);
addNeighbour(0,-1);
} else {
if (!graph.isBlocked(cx-1, cy)) addNeighbour(-1,0);
if (!graph.isBlocked(cx, cy-1)) addNeighbour(0,-1);
}
} else if (dirY > 0) {
// up-left
if (!graph.isBlocked(cx-1, cy)) {
addNeighbour(-1,1);
addNeighbour(-1,0);
addNeighbour(0,1);
} else {
if (!graph.isBlocked(cx-1, cy-1)) addNeighbour(-1,0);
if (!graph.isBlocked(cx, cy)) addNeighbour(0,1);
}
} else {
// left
if (graph.isBlocked(cx,cy)) {
//assert !graph.isBlocked(cx-1, cy);
addNeighbour(-1,1);
addNeighbour(0,1);
addNeighbour(-1,0);
} else {
//assert graph.isBlocked(cx,cy-1);
//assert !graph.isBlocked(cx-1, cy-1);
addNeighbour(-1,-1);
addNeighbour(0,-1);
addNeighbour(-1,0);
}
}
} else if (dirX > 0) {
if (dirY < 0) {
// down-right
if (!graph.isBlocked(cx, cy-1)) {
addNeighbour(1,-1);
addNeighbour(1,0);
addNeighbour(0,-1);
} else {
if (!graph.isBlocked(cx, cy)) addNeighbour(1,0);
if (!graph.isBlocked(cx-1, cy-1)) addNeighbour(0,-1);
}
} else if (dirY > 0) {
// up-right
if (!graph.isBlocked(cx, cy)) {
addNeighbour(1,1);
addNeighbour(1,0);
addNeighbour(0,1);
} else {
if (!graph.isBlocked(cx, cy-1)) addNeighbour(1,0);
if (!graph.isBlocked(cx-1, cy)) addNeighbour(0,1);
}
} else {
// right
if (graph.isBlocked(cx-1,cy)) {
//assert !graph.isBlocked(cx, cy);
addNeighbour(1,1);
addNeighbour(0,1);
addNeighbour(1,0);
} else {
//assert graph.isBlocked(cx-1,cy-1);
//assert !graph.isBlocked(cx, cy-1);
addNeighbour(1,-1);
addNeighbour(0,-1);
addNeighbour(1,0);
}
}
} else {
if (dirY < 0) {
// down
if (graph.isBlocked(cx,cy)) {
//assert !graph.isBlocked(cx, cy-1);
addNeighbour(1,-1);
addNeighbour(1,0);
addNeighbour(0,-1);
} else {
//assert graph.isBlocked(cx-1,cy);
//assert !graph.isBlocked(cx-1, cy-1);
addNeighbour(-1,-1);
addNeighbour(-1,0);
addNeighbour(0,-1);
}
} else { //dirY > 0
// up
if (graph.isBlocked(cx,cy-1)) {
//assert !graph.isBlocked(cx, cy);
addNeighbour(1,1);
addNeighbour(1,0);
addNeighbour(0,1);
} else {
//assert graph.isBlocked(cx-1,cy-1);
//assert !graph.isBlocked(cx-1, cy);
addNeighbour(-1,1);
addNeighbour(-1,0);
addNeighbour(0,1);
}
}
}
}
private final void addNeighbour(int x, int y) {
neighboursdX[neighbourCount] = x;
neighboursdY[neighbourCount] = y;
neighbourCount++;
}
private final void tryRelax(int current, int currX, int currY, int destination) {
if (visited(destination)) return;
int destX = graph.toTwoDimX(destination);
int destY = graph.toTwoDimY(destination);
if (relax(current, destination, graph.octileDistance(currX, currY, destX, destY))) {
// If relaxation is done.
pq.decreaseKey(destination, distance(destination) + graph.octileDistance(destX, destY, ex, ey));
}
}
}