Skip to content

Commit fc68f75

Browse files
author
Hendrik Leuschner
committed
Use reverseDirection parameter for Dijkstra and TDDijkstra
1 parent 945d054 commit fc68f75

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

core/src/main/java/com/graphhopper/routing/Dijkstra.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public Path calcPath(int from, int to) {
7878
}
7979

8080
protected void runAlgo() {
81-
EdgeExplorer explorer = outEdgeExplorer;
81+
EdgeExplorer explorer = reverseDirection ? inEdgeExplorer : outEdgeExplorer;
8282
while (true) {
8383
visitedNodes++;
8484
if (isMaxVisitedNodesExceeded() || finished())
@@ -92,7 +92,7 @@ protected void runAlgo() {
9292

9393
// ORS-GH MOD START
9494
// REMOVED: causes test failure, investigate
95-
double tmpWeight = weighting.calcWeight(iter, false, currEdge.edge) + currEdge.weight;
95+
double tmpWeight = weighting.calcWeight(iter, reverseDirection, currEdge.edge) + currEdge.weight;
9696
// Modification by Maxim Rylov: use originalEdge as the previousEdgeId
9797
// double tmpWeight = weighting.calcWeight(iter, reverseDirection, currEdge.originalEdge) + currEdge.weight;
9898
// ORS-GH MOD END
@@ -146,7 +146,7 @@ protected Path extractPath() {
146146
if (currEdge == null || !finished())
147147
return createEmptyPath();
148148

149-
return new Path(graph, weighting).
149+
return new Path(graph, weighting, reverseDirection).
150150
setWeight(currEdge.weight).setSPTEntry(currEdge).extract();
151151
}
152152

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class Path {
6565
protected boolean reverseOrder = true;
6666
protected long time;
6767
protected GHLongArrayList times;
68+
private boolean reverse = false;
6869
/**
6970
* Shortest path tree entry
7071
*/
@@ -89,6 +90,11 @@ public Path(Graph graph, Weighting weighting) {
8990
this.times = new GHLongArrayList();
9091
}
9192

93+
public Path(Graph graph, Weighting weighting, boolean reverse) {
94+
this(graph, weighting);
95+
this.reverse = reverse;
96+
}
97+
9298
/**
9399
* Populates an unextracted path instances from the specified path p.
94100
*/
@@ -221,7 +227,7 @@ public Path extract() {
221227
// the reverse search needs the next edge
222228
nextEdgeValid = EdgeIterator.Edge.isValid(currEdge.parent.edge);
223229
nextEdge = nextEdgeValid ? currEdge.parent.edge : EdgeIterator.NO_EDGE;
224-
processEdge(currEdge.edge, currEdge.adjNode, nextEdge);
230+
processEdge(currEdge.edge, currEdge.adjNode, nextEdge, reverse);
225231
currEdge = currEdge.parent;
226232
}
227233

@@ -254,13 +260,17 @@ public String getDebugInfo() {
254260
*
255261
* @param prevEdgeId the edge that comes before edgeId: --prevEdgeId-x-edgeId-->adjNode
256262
*/
257-
protected void processEdge(int edgeId, int adjNode, int prevEdgeId) {
263+
protected void processEdge(int edgeId, int adjNode, int prevEdgeId, boolean reverse) {
258264
EdgeIteratorState iter = graph.getEdgeIteratorState(edgeId, adjNode);
259265
distance += iter.getDistance();
260-
addTime(weighting.calcMillis(iter, false, prevEdgeId));
266+
addTime(weighting.calcMillis(iter, reverse, prevEdgeId));
261267
addEdge(edgeId);
262268
}
263269

270+
protected void processEdge(int edgeId, int adjNode, int prevEdgeId) {
271+
processEdge(edgeId, adjNode, prevEdgeId, false);
272+
}
273+
264274
/**
265275
* Iterates over all edges in this path sorted from start to end and calls the visitor callback
266276
* for every edge.

core/src/main/java/com/graphhopper/routing/TDDijkstra.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
* @author Andrzej Oles
3535
*/
3636
public class TDDijkstra extends Dijkstra {
37-
private boolean reverse = false;
3837

3938
public TDDijkstra(Graph graph, Weighting weighting, TraversalMode tMode) {
4039
super(graph, weighting, tMode);
@@ -45,8 +44,8 @@ public TDDijkstra(Graph graph, Weighting weighting, TraversalMode tMode) {
4544
@Override
4645
public Path calcPath(int from, int to, long at) {
4746
checkAlreadyRun();
48-
int source = reverse ? to : from;
49-
int target = reverse ? from : to;
47+
int source = reverseDirection ? to : from;
48+
int target = reverseDirection ? from : to;
5049
this.to = target;
5150
currEdge = new SPTEntry(source, 0);
5251
currEdge.time = at;
@@ -59,7 +58,7 @@ public Path calcPath(int from, int to, long at) {
5958

6059
@Override
6160
protected void runAlgo() {
62-
EdgeExplorer explorer = reverse ? inEdgeExplorer : outEdgeExplorer;
61+
EdgeExplorer explorer = reverseDirection ? inEdgeExplorer : outEdgeExplorer;
6362
while (true) {
6463
visitedNodes++;
6564
if (isMaxVisitedNodesExceeded() || finished())
@@ -71,11 +70,11 @@ protected void runAlgo() {
7170
if (!accept(iter, currEdge.edge))
7271
continue;
7372

74-
double tmpWeight = weighting.calcWeight(iter, reverse, currEdge.edge, currEdge.time) + currEdge.weight;
73+
double tmpWeight = weighting.calcWeight(iter, reverseDirection, currEdge.edge, currEdge.time) + currEdge.weight;
7574
if (Double.isInfinite(tmpWeight)) {
7675
continue;
7776
}
78-
int traversalId = traversalMode.createTraversalId(iter, reverse);
77+
int traversalId = traversalMode.createTraversalId(iter, reverseDirection);
7978

8079
SPTEntry nEdge = fromMap.get(traversalId);
8180
if (nEdge == null) {
@@ -89,7 +88,7 @@ protected void runAlgo() {
8988
continue;
9089

9190
nEdge.parent = currEdge;
92-
nEdge.time = (reverse ? -1 : 1) * weighting.calcMillis(iter, reverse, currEdge.edge, currEdge.time) + currEdge.time;
91+
nEdge.time = (reverseDirection ? -1 : 1) * weighting.calcMillis(iter, reverseDirection, currEdge.edge, currEdge.time) + currEdge.time;
9392
fromHeap.add(nEdge);
9493

9594
updateBestPath(iter, nEdge, traversalId);
@@ -109,7 +108,7 @@ protected Path extractPath() {
109108
if (currEdge == null || !finished())
110109
return createEmptyPath();
111110

112-
return new PathTD(graph, weighting).setReverse(reverse).
111+
return new PathTD(graph, weighting).setReverse(reverseDirection).
113112
setWeight(currEdge.weight).setSPTEntry(currEdge).extract();
114113
}
115114

@@ -119,6 +118,6 @@ public String getName() {
119118
}
120119

121120
public void reverse() {
122-
reverse = !reverse;
121+
reverseDirection = !reverseDirection;
123122
}
124123
}

0 commit comments

Comments
 (0)