Skip to content

Commit

Permalink
refactor calcMillis to reduce parameter number and prepare for a fix of
Browse files Browse the repository at this point in the history
  • Loading branch information
karussell committed Sep 26, 2016
1 parent 91898ee commit 9da912f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
9 changes: 5 additions & 4 deletions core/src/main/java/com/graphhopper/routing/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,16 @@ protected void processEdge(int edgeId, int adjNode) {
double dist = iter.getDistance();
distance += dist;
// TODO calculate time based on weighting -> weighting.calcMillis
time += calcMillis(dist, iter.getFlags(), false);
time += calcMillis(iter, false);
addEdge(edgeId);
}

/**
* Calculates the time in millis for the specified distance in meter and speed (in km/h) via
* flags.
*/
protected long calcMillis(double distance, long flags, boolean revert) {
protected long calcMillis(EdgeIteratorState edge, boolean revert) {
long flags = edge.getFlags();
if (revert && !encoder.isBackward(flags)
|| !revert && !encoder.isForward(flags))
throw new IllegalStateException("Calculating time should not require to read speed from edge in wrong direction. "
Expand All @@ -241,6 +242,7 @@ protected long calcMillis(double distance, long flags, boolean revert) {
if (speed == 0)
throw new IllegalStateException("Speed cannot be 0 for unblocked edge, use access properties to mark edge blocked! Should only occur for shortest path calculation. See #242.");

double distance = edge.getDistance();
return (long) (distance * 3600 / speed);
}

Expand Down Expand Up @@ -562,8 +564,7 @@ private void updatePointsAndInstruction(EdgeIteratorState edge, PointList pl) {
}
double newDist = edge.getDistance();
prevInstruction.setDistance(newDist + prevInstruction.getDistance());
long flags = edge.getFlags();
prevInstruction.setTime(calcMillis(newDist, flags, false) + prevInstruction.getTime());
prevInstruction.setTime(calcMillis(edge, false) + prevInstruction.getTime());
}
});

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/graphhopper/routing/ch/Path4CH.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private void expandEdge(CHEdgeIteratorState mainEdgeState, boolean reverse) {
double dist = mainEdgeState.getDistance();
distance += dist;
long flags = mainEdgeState.getFlags();
time += calcMillis(dist, flags, reverse);
time += calcMillis(mainEdgeState, reverse);
addEdge(mainEdgeState.getEdge());
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public interface Weighting {
* +Infinity. Make sure your method does not return NaN which can e.g. occur for 0/0.
*/
double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId);


// double calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId);

FlagEncoder getFlagEncoder();

String getName();
Expand Down
6 changes: 4 additions & 2 deletions core/src/test/java/com/graphhopper/routing/PathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ public void testTime() {
GraphHopperStorage g = new GraphBuilder(new EncodingManager(tmpEnc)).create();
Path p = new Path(g, tmpEnc);
long flags = tmpEnc.setSpeed(tmpEnc.setReverseSpeed(tmpEnc.setAccess(0, true, true), 10), 15);
assertEquals(375 * 60 * 1000, p.calcMillis(100000, flags, false));
assertEquals(600 * 60 * 1000, p.calcMillis(100000, flags, true));
EdgeIteratorState edge = GHUtility.createMockedEdgeIteratorState(100000, flags);

assertEquals(375 * 60 * 1000, p.calcMillis(edge, false));
assertEquals(600 * 60 * 1000, p.calcMillis(edge, true));

g.close();
}
Expand Down

0 comments on commit 9da912f

Please sign in to comment.