Skip to content

Commit e2926f9

Browse files
author
Peter
committed
Path: renamed nodes and points to calcNodes, calcPoints; fix bug in miniGraphUI
1 parent 34e74ce commit e2926f9

16 files changed

+158
-153
lines changed

src/main/java/com/graphhopper/GraphHopper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public GraphHopper forServer() {
8080
}
8181

8282
public GraphHopper forAndroid() {
83+
// no need to simplify as no IO and simplifying costs a bit CPU
84+
simplify = false;
8385
return memoryMapped();
8486
}
8587

@@ -213,7 +215,7 @@ public GHResponse route(GHRequest request) {
213215
RoutingAlgorithm algo = prepare.createAlgo();
214216
Path path = algo.calcPath(from, to);
215217
debug += " routing (" + algo.name() + "):" + sw.stop().getSeconds() + "s";
216-
PointList points = path.points();
218+
PointList points = path.calcPoints();
217219
if (simplify) {
218220
sw = new StopWatch().start();
219221
int del = new DouglasPeucker().setMaxDist(request.minPathPrecision()).simplify(points);

src/main/java/com/graphhopper/routing/Path.java

Lines changed: 85 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public class Path {
5050
private int fromNode = EdgeIterator.NO_EDGE;
5151
private TIntList edgeIds;
5252
private PointList cachedPoints;
53-
private TIntList cachedNodes;
5453

5554
Path() {
5655
this(null, ShortestCarCalc.DEFAULT);
@@ -68,7 +67,7 @@ public Path(Graph graph, WeightCalculation weightCalculation) {
6867
public Path(Path p) {
6968
this(p.graph, p.weightCalculation);
7069
weight = p.weight;
71-
edgeIds = new TIntArrayList(edgeIds);
70+
edgeIds = new TIntArrayList(edgeIds);
7271
edgeEntry = p.edgeEntry;
7372
}
7473

@@ -81,8 +80,12 @@ protected void addEdge(int edge) {
8180
edgeIds.add(edge);
8281
}
8382

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;
8689
}
8790

8891
public int getFromNode() {
@@ -130,24 +133,8 @@ public void weight(double weight) {
130133
this.weight = weight;
131134
}
132135

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-
149136
/**
150-
* Extract path from shortest-path-tree.
137+
* Extracts the Path from the shortest-path-tree determined by edgeEntry.
151138
*/
152139
public Path extract() {
153140
EdgeEntry goalEdge = edgeEntry;
@@ -161,14 +148,17 @@ public Path extract() {
161148
return found(true);
162149
}
163150

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);
167157
}
168158

169159
/**
170160
* This method calculates not only the weight but also the distance in
171-
* kilometer.
161+
* kilometer for the specified edge.
172162
*/
173163
public void calcWeight(EdgeIterator iter) {
174164
double dist = iter.distance();
@@ -179,19 +169,17 @@ public void calcWeight(EdgeIterator iter) {
179169
}
180170

181171
/**
182-
* @return the node indices of the tower nodes in this path.
172+
* Used in combination with forEveryEdge.
183173
*/
184-
public TIntList nodes() {
185-
if (cachedNodes == null)
186-
calcNodes();
187-
return cachedNodes;
174+
public static interface EdgeVisitor {
175+
176+
void next(EdgeIterator iter);
188177
}
189-
190-
public TDoubleList calcDistances() {
191-
TDoubleList distances = new TDoubleArrayList(edgeIds.size());
192-
if (edgeIds.isEmpty())
193-
return distances;
194178

179+
/**
180+
* Iterates over all edges in this path and calls the visitor for it.
181+
*/
182+
public void forEveryEdge(EdgeVisitor visitor) {
195183
int tmpNode = getFromNode();
196184
int len = edgeIds.size();
197185
for (int i = 0; i < len; i++) {
@@ -201,69 +189,76 @@ public TDoubleList calcDistances() {
201189
+ " was empty when requested with node " + tmpNode
202190
+ ", edgeIndex:" + i + ", edges:" + edgeIds.size());
203191
tmpNode = iter.baseNode();
204-
distances.add(iter.distance());
192+
visitor.next(iter);
205193
}
206-
return distances;
207194
}
208195

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);
211201
if (edgeIds.isEmpty())
212-
return cachedNodes;
202+
return nodes;
213203

214204
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;
226212
}
227213

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;
235220
cachedPoints = new PointList(edgeIds.size() + 1);
221+
if (edgeIds.isEmpty())
222+
return cachedPoints;
236223
int tmpNode = getFromNode();
237224
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+
});
254236
return cachedPoints;
255237
}
256238

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+
257252
public TIntSet calculateIdenticalNodes(Path p2) {
258253
TIntHashSet thisSet = new TIntHashSet();
259254
TIntHashSet retSet = new TIntHashSet();
260-
TIntList nodes = nodes();
255+
TIntList nodes = calcNodes();
261256
int max = nodes.size();
262257
for (int i = 0; i < max; i++) {
263258
thisSet.add(nodes.get(i));
264259
}
265260

266-
nodes = p2.nodes();
261+
nodes = p2.calcNodes();
267262
max = nodes.size();
268263
for (int i = 0; i < max; i++) {
269264
if (thisSet.contains(nodes.get(i)))
@@ -272,11 +267,19 @@ public TIntSet calculateIdenticalNodes(Path p2) {
272267
return retSet;
273268
}
274269

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;
281284
}
282285
}

src/main/java/com/graphhopper/routing/util/TestAlgoCollector.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public TestAlgoCollector assertDistance(RoutingAlgorithm algo,
4646
} else if (Math.abs(path.distance() - distance) > 10)
4747
list.add(algo + " returns path not matching the expected distance of " + distance
4848
+ "\t Returned was " + path.distance() + "\t (expected points " + points
49-
+ ", was " + path.points().size() + ") from:" + from + ", to:" + to);
49+
+ ", was " + path.calcPoints().size() + ") from:" + from + ", to:" + to);
5050
// Yes, there are indeed real world instances where A-B-C is identical to A-C (in meter precision).
5151
// And for from:501620, to:155552 the node difference of astar to bi-dijkstra gets even bigger (7!).
52-
if (Math.abs(path.points().size() - points) > 7)
52+
if (Math.abs(path.calcPoints().size() - points) > 7)
5353
list.add(algo + " returns path not matching the expected points of " + points
54-
+ "\t Returned was " + path.points().size() + "\t (expected distance " + distance
54+
+ "\t Returned was " + path.calcPoints().size() + "\t (expected distance " + distance
5555
+ ", was " + path.distance() + ") from:" + from + ", to:" + to);
5656
return this;
5757
}

src/main/java/com/graphhopper/ui/MiniGraphUI.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,15 @@ public MiniGraphUI(OSMReader reader, boolean debug) {
184184
return;
185185
}
186186

187-
logger.info("found path in " + sw.getSeconds() + "s with " + path.nodes() + " nodes: " + path);
187+
logger.info("found path in " + sw.getSeconds() + "s with " + path.calcNodes().size() + " nodes: " + path);
188188
g2.setColor(Color.BLUE.brighter().brighter());
189-
PointList list = path.points();
189+
PointList list = path.calcPoints();
190190
double prevLat = Double.NaN;
191191
double prevLon = Double.NaN;
192192
for (int i = 0; i < list.size(); i++) {
193193
double lat = list.latitude(i);
194194
double lon = list.longitude(i);
195-
if (Double.isNaN(prevLat))
195+
if (!Double.isNaN(prevLat))
196196
mg.plotEdge(g2, prevLat, prevLon, lat, lon, 3);
197197

198198
prevLat = lat;
@@ -227,17 +227,17 @@ private Path plotPath(Path tmpPath, Graphics2D g2, int w) {
227227

228228
double lastLat = Double.NaN;
229229
double lastLon = Double.NaN;
230-
for (int i = 0; i < tmpPath.points().size(); i++) {
231-
double lat = tmpPath.points().latitude(i);
232-
double lon = tmpPath.points().longitude(i);
230+
for (int i = 0; i < tmpPath.calcPoints().size(); i++) {
231+
double lat = tmpPath.calcPoints().latitude(i);
232+
double lon = tmpPath.calcPoints().longitude(i);
233233
if (!Double.isNaN(lastLat))
234234
mg.plotEdge(g2, lastLat, lastLon, lat, lon, w);
235235
else
236236
mg.plot(g2, lat, lon, w);
237237
lastLat = lat;
238238
lastLon = lon;
239239
}
240-
logger.info("dist:" + tmpPath.distance() + ", path points:" + tmpPath.points().size());
240+
logger.info("dist:" + tmpPath.distance() + ", path points:" + tmpPath.calcPoints().size());
241241
return tmpPath;
242242
}
243243
private int dijkstraFromId = -1;

0 commit comments

Comments
 (0)