@@ -50,7 +50,6 @@ public class Path {
50
50
private int fromNode = EdgeIterator .NO_EDGE ;
51
51
private TIntList edgeIds ;
52
52
private PointList cachedPoints ;
53
- private TIntList cachedNodes ;
54
53
55
54
Path () {
56
55
this (null , ShortestCarCalc .DEFAULT );
@@ -68,7 +67,7 @@ public Path(Graph graph, WeightCalculation weightCalculation) {
68
67
public Path (Path p ) {
69
68
this (p .graph , p .weightCalculation );
70
69
weight = p .weight ;
71
- edgeIds = new TIntArrayList (edgeIds );
70
+ edgeIds = new TIntArrayList (edgeIds );
72
71
edgeEntry = p .edgeEntry ;
73
72
}
74
73
@@ -81,8 +80,12 @@ protected void addEdge(int edge) {
81
80
edgeIds .add (edge );
82
81
}
83
82
84
- public void setFound (boolean found ) {
85
- this .found = found ;
83
+ /**
84
+ * We need to remember fromNode explicitely as its not saved in one edgeId
85
+ * of edgeIds.
86
+ */
87
+ protected void setFromNode (int node ) {
88
+ fromNode = node ;
86
89
}
87
90
88
91
public int getFromNode () {
@@ -130,24 +133,8 @@ public void weight(double weight) {
130
133
this .weight = weight ;
131
134
}
132
135
133
- @ Override public String toString () {
134
- return "weight:" + weight () + ", edges:" + edgeIds ;
135
- }
136
-
137
- public String toDetailsString () {
138
- String str = "" ;
139
- TIntList nodes = nodes ();
140
- for (int i = 0 ; i < nodes .size (); i ++) {
141
- if (i > 0 )
142
- str += "->" ;
143
-
144
- str += nodes .get (i );
145
- }
146
- return toString () + ", " + str ;
147
- }
148
-
149
136
/**
150
- * Extract path from shortest-path-tree.
137
+ * Extracts the Path from the shortest-path-tree determined by edgeEntry .
151
138
*/
152
139
public Path extract () {
153
140
EdgeEntry goalEdge = edgeEntry ;
@@ -161,14 +148,17 @@ public Path extract() {
161
148
return found (true );
162
149
}
163
150
164
- protected void processWeight (int tmpEdge , int endNode ) {
165
- calcWeight (graph .getEdgeProps (tmpEdge , endNode ));
166
- addEdge (tmpEdge );
151
+ /**
152
+ * Calls calcWeight and adds the edgeId.
153
+ */
154
+ protected void processWeight (int edgeId , int endNode ) {
155
+ calcWeight (graph .getEdgeProps (edgeId , endNode ));
156
+ addEdge (edgeId );
167
157
}
168
158
169
159
/**
170
160
* This method calculates not only the weight but also the distance in
171
- * kilometer.
161
+ * kilometer for the specified edge .
172
162
*/
173
163
public void calcWeight (EdgeIterator iter ) {
174
164
double dist = iter .distance ();
@@ -179,19 +169,17 @@ public void calcWeight(EdgeIterator iter) {
179
169
}
180
170
181
171
/**
182
- * @return the node indices of the tower nodes in this path .
172
+ * Used in combination with forEveryEdge .
183
173
*/
184
- public TIntList nodes () {
185
- if (cachedNodes == null )
186
- calcNodes ();
187
- return cachedNodes ;
174
+ public static interface EdgeVisitor {
175
+
176
+ void next (EdgeIterator iter );
188
177
}
189
-
190
- public TDoubleList calcDistances () {
191
- TDoubleList distances = new TDoubleArrayList (edgeIds .size ());
192
- if (edgeIds .isEmpty ())
193
- return distances ;
194
178
179
+ /**
180
+ * Iterates over all edges in this path and calls the visitor for it.
181
+ */
182
+ public void forEveryEdge (EdgeVisitor visitor ) {
195
183
int tmpNode = getFromNode ();
196
184
int len = edgeIds .size ();
197
185
for (int i = 0 ; i < len ; i ++) {
@@ -201,69 +189,76 @@ public TDoubleList calcDistances() {
201
189
+ " was empty when requested with node " + tmpNode
202
190
+ ", edgeIndex:" + i + ", edges:" + edgeIds .size ());
203
191
tmpNode = iter .baseNode ();
204
- distances . add (iter . distance () );
192
+ visitor . next (iter );
205
193
}
206
- return distances ;
207
194
}
208
195
209
- private TIntList calcNodes () {
210
- cachedNodes = new TIntArrayList (edgeIds .size () + 1 );
196
+ /**
197
+ * @return the uncached node indices of the tower nodes in this path.
198
+ */
199
+ public TIntList calcNodes () {
200
+ final TIntArrayList nodes = new TIntArrayList (edgeIds .size () + 1 );
211
201
if (edgeIds .isEmpty ())
212
- return cachedNodes ;
202
+ return nodes ;
213
203
214
204
int tmpNode = getFromNode ();
215
- cachedNodes .add (tmpNode );
216
- int len = edgeIds .size ();
217
- for (int i = 0 ; i < len ; i ++) {
218
- EdgeIterator iter = graph .getEdgeProps (edgeIds .get (i ), tmpNode );
219
- if (iter .isEmpty ())
220
- throw new IllegalStateException ("Edge " + edgeIds .get (i )
221
- + " was empty when requested with node " + tmpNode
222
- + ", edgeIndex:" + i + ", edges:" + edgeIds .size ());
223
- cachedNodes .add (tmpNode = iter .baseNode ());
224
- }
225
- return cachedNodes ;
205
+ nodes .add (tmpNode );
206
+ forEveryEdge (new EdgeVisitor () {
207
+ @ Override public void next (EdgeIterator iter ) {
208
+ nodes .add (iter .baseNode ());
209
+ }
210
+ });
211
+ return nodes ;
226
212
}
227
213
228
- public PointList points () {
229
- if (cachedPoints == null )
230
- calcPoints ();
231
- return cachedPoints ;
232
- }
233
-
234
- private PointList calcPoints () {
214
+ /**
215
+ * @return the cached list of lat,lon for this path
216
+ */
217
+ public PointList calcPoints () {
218
+ if (cachedPoints != null )
219
+ return cachedPoints ;
235
220
cachedPoints = new PointList (edgeIds .size () + 1 );
221
+ if (edgeIds .isEmpty ())
222
+ return cachedPoints ;
236
223
int tmpNode = getFromNode ();
237
224
cachedPoints .add (graph .getLatitude (tmpNode ), graph .getLongitude (tmpNode ));
238
- int len = edgeIds .size ();
239
- for (int i = 0 ; i < len ; i ++) {
240
- int edgeId = edgeIds .get (i );
241
- EdgeIterator iter = graph .getEdgeProps (edgeId , tmpNode );
242
- if (iter .isEmpty ())
243
- throw new IllegalStateException ("Edge " + edgeId
244
- + " was empty when requested with node " + tmpNode
245
- + ", edgeIndex:" + i + ", edges:" + edgeIds .size ());
246
- tmpNode = iter .baseNode ();
247
- PointList pl = iter .pillarNodes ();
248
- pl .reverse ();
249
- for (int j = 0 ; j < pl .size (); j ++) {
250
- cachedPoints .add (pl .latitude (j ), pl .longitude (j ));
251
- }
252
- cachedPoints .add (graph .getLatitude (tmpNode ), graph .getLongitude (tmpNode ));
253
- }
225
+ forEveryEdge (new EdgeVisitor () {
226
+ @ Override public void next (EdgeIterator iter ) {
227
+ PointList pl = iter .pillarNodes ();
228
+ pl .reverse ();
229
+ for (int j = 0 ; j < pl .size (); j ++) {
230
+ cachedPoints .add (pl .latitude (j ), pl .longitude (j ));
231
+ }
232
+ int baseNode = iter .baseNode ();
233
+ cachedPoints .add (graph .getLatitude (baseNode ), graph .getLongitude (baseNode ));
234
+ }
235
+ });
254
236
return cachedPoints ;
255
237
}
256
238
239
+ public TDoubleList calcDistances () {
240
+ final TDoubleList distances = new TDoubleArrayList (edgeIds .size ());
241
+ if (edgeIds .isEmpty ())
242
+ return distances ;
243
+
244
+ forEveryEdge (new EdgeVisitor () {
245
+ @ Override public void next (EdgeIterator iter ) {
246
+ distances .add (iter .distance ());
247
+ }
248
+ });
249
+ return distances ;
250
+ }
251
+
257
252
public TIntSet calculateIdenticalNodes (Path p2 ) {
258
253
TIntHashSet thisSet = new TIntHashSet ();
259
254
TIntHashSet retSet = new TIntHashSet ();
260
- TIntList nodes = nodes ();
255
+ TIntList nodes = calcNodes ();
261
256
int max = nodes .size ();
262
257
for (int i = 0 ; i < max ; i ++) {
263
258
thisSet .add (nodes .get (i ));
264
259
}
265
260
266
- nodes = p2 .nodes ();
261
+ nodes = p2 .calcNodes ();
267
262
max = nodes .size ();
268
263
for (int i = 0 ; i < max ; i ++) {
269
264
if (thisSet .contains (nodes .get (i )))
@@ -272,11 +267,19 @@ public TIntSet calculateIdenticalNodes(Path p2) {
272
267
return retSet ;
273
268
}
274
269
275
- /**
276
- * We need to remember fromNode explicitely as its not saved in one edgeId
277
- * of edgeIds.
278
- */
279
- protected void setFromNode (int node ) {
280
- fromNode = node ;
270
+ @ Override public String toString () {
271
+ return "weight:" + weight () + ", edges:" + edgeIds .size ();
272
+ }
273
+
274
+ public String toDetailsString () {
275
+ String str = "" ;
276
+ TIntList nodes = calcNodes ();
277
+ for (int i = 0 ; i < nodes .size (); i ++) {
278
+ if (i > 0 )
279
+ str += "->" ;
280
+
281
+ str += nodes .get (i );
282
+ }
283
+ return toString () + ", " + str ;
281
284
}
282
285
}
0 commit comments