forked from Ohohcakester/Any-Angle-Pathfinding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathFindingAlgorithm.java
More file actions
227 lines (186 loc) · 6.13 KB
/
Copy pathPathFindingAlgorithm.java
File metadata and controls
227 lines (186 loc) · 6.13 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
package algorithms;
import grid.GridGraph;
import java.awt.Color;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import algorithms.datatypes.Memory;
import algorithms.datatypes.SnapshotItem;
/**
* ABSTRACT<br>
* Template for all Path Finding Algorithms used.<br>
*/
public abstract class PathFindingAlgorithm {
private static final int SNAPSHOT_INTERVAL = 0;
private int snapshotCountdown = 0;
private ArrayList<List<SnapshotItem>> snapshotList;
protected GridGraph graph;
protected int parent[];
protected final int sizeX;
protected final int sizeXplusOne;
protected final int sizeY;
protected final int sx;
protected final int sy;
protected final int ex;
protected final int ey;
private int ticketNumber = -1;
private boolean recordingMode;
private boolean usingStaticMemory = false;
public PathFindingAlgorithm(GridGraph graph, int sizeX, int sizeY,
int sx, int sy, int ex, int ey) {
this.graph = graph;
this.sizeX = sizeX;
this.sizeXplusOne = sizeX+1;
this.sizeY = sizeY;
this.sx = sx;
this.sy = sy;
this.ex = ex;
this.ey = ey;
snapshotList = new ArrayList<>();
}
protected void initialiseMemory(int size, float defaultDistance, int defaultParent, boolean defaultVisited) {
usingStaticMemory = true;
ticketNumber = Memory.initialise(size, defaultDistance, defaultParent, defaultVisited);
}
/**
* Call to start tracing the algorithm's operation.
*/
public void startRecording() {
recordingMode = true;
}
/**
* Call to stop tracing the algorithm's operation.
*/
public void stopRecording() {
recordingMode = false;
}
/**
* @return retrieve the trace of the algorithm that has been recorded.
*/
public ArrayList<List<SnapshotItem>> retrieveSnapshotList() {
return snapshotList;
}
/**
* Call this to compute the path.
*/
public abstract void computePath();
/**
* @return retrieve the path computed by the algorithm
*/
public abstract int[][] getPath();
/**
* @return directly get path length without computing path.
* Has to run fast, unlike getPath.
*/
public abstract float getPathLength();
/**
* An optimal overridable method which prints some statistics when called for.
*/
public void printStatistics() {
}
protected final int toOneDimIndex(int x, int y) {
return graph.toOneDimIndex(x, y);
}
protected final int toTwoDimX(int index) {
return graph.toTwoDimX(index);
}
protected final int toTwoDimY(int index) {
return graph.toTwoDimY(index);
}
protected final void maybeSaveSearchSnapshot() {
if (recordingMode) {
if (usingStaticMemory && ticketNumber != Memory.currentTicket())
throw new UnsupportedOperationException("Ticket does not match!");
saveSearchSnapshot();
}
}
public List<SnapshotItem> getCurrentSearchSnapshot() {
return computeSearchSnapshot();
}
protected final boolean isRecording() {
return recordingMode;
}
private void saveSearchSnapshot() {
if (snapshotCountdown > 0) {
snapshotCountdown--;
return;
}
snapshotCountdown = SNAPSHOT_INTERVAL;
snapshotList.add(computeSearchSnapshot());
}
protected final void addSnapshot(List<SnapshotItem> snapshotItemList) {
snapshotList.add(snapshotItemList);
}
protected int goalParentIndex() {
return toOneDimIndex(ex,ey);
}
private int getParent(int index) {
if (usingStaticMemory) return Memory.parent(index);
else return parent[index];
}
private void setParent(int index, int value) {
if (usingStaticMemory) Memory.setParent(index, value);
else parent[index] = value;
}
protected int getSize() {
if (usingStaticMemory) return Memory.size();
else return parent.length;
}
protected List<SnapshotItem> computeSearchSnapshot() {
List<SnapshotItem> list = new ArrayList<>();
int current = goalParentIndex();
Set<Integer> finalPathSet = null;
if (getParent(current) >= 0) {
finalPathSet = new HashSet<Integer>();
while (current >= 0) {
finalPathSet.add(current);
current = getParent(current);
}
}
int size = getSize();
for (int i=0; i<size; i++) {
if (getParent(i) != -1) {
if (finalPathSet != null && finalPathSet.contains(i)) {
list.add(SnapshotItem.generate(snapshotEdge(i), Color.BLUE));
} else {
list.add(SnapshotItem.generate(snapshotEdge(i)));
}
}
Integer[] vertexSnapshot = snapshotVertex(i);
if (vertexSnapshot != null) {
list.add(SnapshotItem.generate(vertexSnapshot));
}
}
return list;
}
protected Integer[] snapshotEdge(int endIndex) {
Integer[] edge = new Integer[4];
int startIndex = getParent(endIndex);
edge[2] = toTwoDimX(endIndex);
edge[3] = toTwoDimY(endIndex);
if (startIndex < 0) {
edge[0] = edge[2];
edge[1] = edge[3];
} else {
edge[0] = toTwoDimX(startIndex);
edge[1] = toTwoDimY(startIndex);
}
return edge;
}
protected Integer[] snapshotVertex(int index) {
if (selected(index)) {
Integer[] edge = new Integer[2];
edge[0] = toTwoDimX(index);
edge[1] = toTwoDimY(index);
return edge;
}
return null;
}
protected boolean selected(int index) {
return false;
}
protected void inheritSnapshotListFrom(PathFindingAlgorithm algo) {
this.snapshotList = algo.snapshotList;
}
}