forked from Ohohcakester/Any-Angle-Pathfinding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAStar.java
More file actions
259 lines (197 loc) · 7.19 KB
/
Copy pathAStar.java
File metadata and controls
259 lines (197 loc) · 7.19 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
package algorithms;
import algorithms.priorityqueue.IndirectHeap;
import grid.GridGraph;
public class AStar extends PathFindingAlgorithm {
protected boolean postSmoothingOn = false;
protected boolean repeatedPostSmooth = true;
protected float heuristicWeight = 1f;
protected Float[] distance;
protected boolean[] visited;
protected IndirectHeap<Float> pq;
protected int finish;
public AStar(GridGraph graph, int sx, int sy, int ex, int ey) {
super(graph, graph.sizeX, graph.sizeY, sx, sy, ex, ey);
}
public static AStar postSmooth(GridGraph graph, int sx, int sy, int ex, int ey) {
AStar aStar = new AStar(graph, sx, sy, ex, ey);
aStar.postSmoothingOn = true;
return aStar;
}
public static AStar dijkstra(GridGraph graph, int sx, int sy, int ex, int ey) {
AStar aStar = new AStar(graph, sx, sy, ex, ey);
aStar.heuristicWeight = 0;
return aStar;
}
@Override
public void computePath() {
int totalSize = (graph.sizeX+1) * (graph.sizeY+1);
int start = toOneDimIndex(sx, sy);
finish = toOneDimIndex(ex, ey);
distance = new Float[totalSize];
parent = new int[totalSize];
initialise(start);
visited = new boolean[totalSize];
pq = new IndirectHeap<Float>(distance, true);
pq.heapify();
//float lastDist = -1;
while (!pq.isEmpty()) {
//float dist = pq.getMinValue();
int current = pq.popMinIndex();
maybeSaveSearchSnapshot();
//if (Math.abs(dist - lastDist) > 0.01f) { maybeSaveSearchSnapshot(); lastDist = dist;}
if (current == finish || distance[current] == Float.POSITIVE_INFINITY) {
//maybeSaveSearchSnapshot();
break;
}
visited[current] = true;
int x = toTwoDimX(current);
int y = toTwoDimY(current);
tryRelaxNeighbour(current, x, y, x-1, y-1);
tryRelaxNeighbour(current, x, y, x, y-1);
tryRelaxNeighbour(current, x, y, x+1, y-1);
tryRelaxNeighbour(current, x, y, x-1, y);
tryRelaxNeighbour(current, x, y, x+1, y);
tryRelaxNeighbour(current, x, y, x-1, y+1);
tryRelaxNeighbour(current, x, y, x, y+1);
tryRelaxNeighbour(current, x, y, x+1, y+1);
//maybeSaveSearchSnapshot();
}
maybePostSmooth();
}
protected void tryRelaxNeighbour(int current, int currentX, int currentY, int x, int y) {
if (!graph.isValidCoordinate(x, y))
return;
int destination = toOneDimIndex(x,y);
if (visited[destination])
return;
if (!graph.neighbourLineOfSight(currentX, currentY, x, y))
return;
if (relax(current, destination, weight(currentX, currentY, x, y))) {
// If relaxation is done.
pq.decreaseKey(destination, distance[destination] + heuristic(x,y));
}
}
protected float heuristic(int x, int y) {
//return 0;
return heuristicWeight*graph.distance(x, y, ex, ey);
}
protected float weight(int x1, int y1, int x2, int y2) {
return graph.distance(x1, y1, x2, y2);
}
public Float[] getDistance(){return distance;}
public int[] getParent(){return parent;}
protected boolean relax(int u, int v, float weightUV) {
// return true iff relaxation is done.
float newWeight = distance[u] + weightUV;
if (newWeight < distance[v]) {
distance[v] = newWeight;
parent[v] = u;
//maybeSaveSearchSnapshot();
return true;
}
return false;
}
protected final void initialise(int s) {
for (int i=0; i<distance.length; i++) {
distance[i] = Float.POSITIVE_INFINITY;
parent[i] = -1;
}
distance[s] = 0f;
}
private int pathLength() {
int length = 0;
int current = finish;
while (current != -1) {
current = parent[current];
length++;
}
return length;
}
protected boolean lineOfSight(int node1, int node2) {
int x1 = toTwoDimX(node1);
int y1 = toTwoDimY(node1);
int x2 = toTwoDimX(node2);
int y2 = toTwoDimY(node2);
return graph.lineOfSight(x1, y1, x2, y2);
}
protected float physicalDistance(int node1, int node2) {
int x1 = toTwoDimX(node1);
int y1 = toTwoDimY(node1);
int x2 = toTwoDimX(node2);
int y2 = toTwoDimY(node2);
return graph.distance(x1, y1, x2, y2);
}
protected float physicalDistance(int x1, int y1, int node2) {
int x2 = toTwoDimX(node2);
int y2 = toTwoDimY(node2);
return graph.distance(x1, y1, x2, y2);
}
protected void maybePostSmooth() {
if (postSmoothingOn) {
if (repeatedPostSmooth) {
while(postSmooth());
} else {
postSmooth();
}
}
}
private boolean postSmooth() {
boolean didSomething = false;
int current = finish;
while (current != -1) {
int next = parent[current]; // we can skip checking this one as it always has LoS to current.
if (next != -1) {
next = parent[next];
while (next != -1) {
if (lineOfSight(current,next)) {
parent[current] = next;
next = parent[next];
didSomething = true;
maybeSaveSearchSnapshot();
} else {
next = -1;
}
}
}
current = parent[current];
}
return didSomething;
}
public int[][] getPath() {
int length = pathLength();
int[][] path = new int[length][];
int current = finish;
int index = length-1;
while (current != -1) {
int x = toTwoDimX(current);
int y = toTwoDimY(current);
path[index] = new int[2];
path[index][0] = x;
path[index][1] = y;
index--;
current = parent[current];
}
return path;
}
public float getPathLength() {
int current = finish;
if (current == -1) return -1;
float pathLength = 0;
int prevX = toTwoDimX(current);
int prevY = toTwoDimY(current);
current = parent[current];
while (current != -1) {
int x = toTwoDimX(current);
int y = toTwoDimY(current);
pathLength += graph.distance(x, y, prevX, prevY);
current = parent[current];
prevX = x;
prevY = y;
}
return pathLength;
}
@Override
protected boolean selected(int index) {
return visited[index];
}
}