Skip to content

Commit 63f00e1

Browse files
authored
Work towards removing TurnWeighting (graphhopper#1842)
* Move INFINITE_U_TURN_COSTS from TurnWeighting to Weighting * Add Weighting.calcEdgeWeight/Millis for convenience * Replace supports(TurnWeighting.class) with supportsTurnCosts() in FlagEncoder
1 parent e72c67d commit 63f00e1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+162
-100
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767

6868
import static com.graphhopper.routing.ch.CHAlgoFactoryDecorator.EdgeBasedCHMode.EDGE_OR_NODE;
6969
import static com.graphhopper.routing.ch.CHAlgoFactoryDecorator.EdgeBasedCHMode.OFF;
70-
import static com.graphhopper.routing.weighting.TurnWeighting.INFINITE_U_TURN_COSTS;
70+
import static com.graphhopper.routing.weighting.Weighting.INFINITE_U_TURN_COSTS;
7171
import static com.graphhopper.util.Helper.*;
7272
import static com.graphhopper.util.Parameters.Algorithms.*;
7373
import static com.graphhopper.util.Parameters.Routing.CURBSIDE;
@@ -826,10 +826,10 @@ private void initCHAlgoFactoryDecorator() {
826826
int uTurnCosts = config.getInt(Routing.U_TURN_COSTS, INFINITE_U_TURN_COSTS);
827827

828828
CHAlgoFactoryDecorator.EdgeBasedCHMode edgeBasedCHMode = chFactoryDecorator.getEdgeBasedCHMode();
829-
if (!(edgeBasedCHMode == EDGE_OR_NODE && encoder.supports(TurnWeighting.class))) {
829+
if (!(edgeBasedCHMode == EDGE_OR_NODE && encoder.supportsTurnCosts())) {
830830
chFactoryDecorator.addCHProfile(CHProfile.nodeBased(createWeighting(new HintsMap(chWeightingStr), encoder, null)));
831831
}
832-
if (edgeBasedCHMode != OFF && encoder.supports(TurnWeighting.class)) {
832+
if (edgeBasedCHMode != OFF && encoder.supportsTurnCosts()) {
833833
chFactoryDecorator.addCHProfile(CHProfile.edgeBased(createWeighting(new HintsMap(chWeightingStr), encoder, null), uTurnCosts));
834834
}
835835
}
@@ -973,7 +973,7 @@ public Weighting createWeighting(HintsMap hintsMap, FlagEncoder encoder, Graph g
973973
*/
974974
public Weighting createTurnWeighting(Graph graph, Weighting weighting, TraversalMode tMode, double uTurnCosts) {
975975
FlagEncoder encoder = weighting.getFlagEncoder();
976-
if (encoder.supports(TurnWeighting.class) && tMode.isEdgeBased())
976+
if (encoder.supportsTurnCosts() && tMode.isEdgeBased())
977977
return new TurnWeighting(weighting, graph.getTurnCostStorage(), uTurnCosts);
978978
return weighting;
979979
}
@@ -1013,11 +1013,11 @@ public List<Path> calcPaths(GHRequest request, GHResponse ghRsp) {
10131013

10141014
// we use edge-based routing if the encoder supports turn-costs *unless* the edge_based parameter is set
10151015
// explicitly.
1016-
TraversalMode tMode = encoder.supports(TurnWeighting.class) ? TraversalMode.EDGE_BASED : TraversalMode.NODE_BASED;
1016+
TraversalMode tMode = encoder.supportsTurnCosts() ? TraversalMode.EDGE_BASED : TraversalMode.NODE_BASED;
10171017
if (hints.has(Routing.EDGE_BASED))
10181018
tMode = hints.getBool(Routing.EDGE_BASED, false) ? TraversalMode.EDGE_BASED : TraversalMode.NODE_BASED;
10191019

1020-
if (tMode.isEdgeBased() && !encoder.supports(TurnWeighting.class)) {
1020+
if (tMode.isEdgeBased() && !encoder.supportsTurnCosts()) {
10211021
throw new IllegalArgumentException("You need to set up a turn cost storage to make use of edge_based=true, e.g. use car|turn_costs=true");
10221022
}
10231023

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ protected void updateBestPath(EdgeIteratorState edgeState, SPTEntry entry, int t
317317

318318
// prevents the path to contain the edge at the meeting point twice and subtracts the weight (excluding turn weight => no previous edge)
319319
entry = entry.getParent();
320-
weight -= weighting.calcWeight(edgeState, reverse, EdgeIterator.NO_EDGE);
320+
weight -= weighting.calcEdgeWeight(edgeState, reverse);
321321
}
322322

323323
if (weight < bestWeight) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private boolean entryIsStallable(SPTEntry entry, IntObjectMap<SPTEntry> bestWeig
7373
// we have to be careful because of rounded shortcut weights in combination with virtual via nodes, see #1574
7474
final double precision = 0.001;
7575
if (adjNode != null &&
76-
adjNode.weight + weighting.calcWeight(iter, !reverse, getIncomingEdge(entry)) - entry.weight < -precision) {
76+
adjNode.weight + weighting.calcEdgeWeight(iter, !reverse) - entry.weight < -precision) {
7777
return true;
7878
}
7979
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ public Path extractPath() {
9393
}
9494
EdgeIteratorState edgeState = graph.getEdgeIteratorState(edge, node);
9595
path.addDistance(edgeState.getDistance());
96-
path.addTime(weighting.calcMillis(edgeState, false, EdgeIterator.NO_EDGE));
96+
// todo: we do not yet account for turn times here!
97+
path.addTime(weighting.calcEdgeMillis(edgeState, false));
9798
path.addEdge(edge);
9899
node = parents[node];
99100
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,8 @@ private void updatePointsAndInstruction(EdgeIteratorState edge, PointList pl) {
496496
}
497497
double newDist = edge.getDistance();
498498
prevInstruction.setDistance(newDist + prevInstruction.getDistance());
499-
prevInstruction.setTime(weighting.calcMillis(edge, false, EdgeIterator.NO_EDGE)
500-
+ prevInstruction.getTime());
499+
// todonow: why do we not account for turn times here ?
500+
prevInstruction.setTime(weighting.calcEdgeMillis(edge, false) + prevInstruction.getTime());
501501
}
502502

503503
}

core/src/main/java/com/graphhopper/routing/ch/CHProfileSelector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.util.ArrayList;
2626
import java.util.List;
2727

28-
import static com.graphhopper.routing.weighting.TurnWeighting.INFINITE_U_TURN_COSTS;
28+
import static com.graphhopper.routing.weighting.Weighting.INFINITE_U_TURN_COSTS;
2929

3030
/**
3131
* This class is used to determine the appropriate CH profile given (or not given) some (request) parameters

core/src/main/java/com/graphhopper/routing/ch/CHWeighting.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.graphhopper.util.CHEdgeIteratorState;
2424
import com.graphhopper.util.EdgeIteratorState;
2525

26+
import static com.graphhopper.util.EdgeIterator.NO_EDGE;
27+
2628
/**
2729
* Used by CH algorithms and therefore assumed that all edges are of type CHEdgeIteratorState
2830
* <p>
@@ -42,6 +44,11 @@ public final double getMinWeight(double distance) {
4244
return userWeighting.getMinWeight(distance);
4345
}
4446

47+
@Override
48+
public final double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) {
49+
return calcWeight(edgeState, reverse, NO_EDGE);
50+
}
51+
4552
@Override
4653
public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
4754
CHEdgeIteratorState tmp = (CHEdgeIteratorState) edgeState;
@@ -52,6 +59,11 @@ public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevO
5259
return userWeighting.calcWeight(edgeState, reverse, prevOrNextEdgeId);
5360
}
5461

62+
@Override
63+
public final long calcEdgeMillis(EdgeIteratorState edgeState, boolean reverse) {
64+
return calcMillis(edgeState, reverse, NO_EDGE);
65+
}
66+
5567
@Override
5668
public long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
5769
if (edgeState instanceof CHEdgeIteratorState && ((CHEdgeIteratorState) edgeState).isShortcut()) {

core/src/main/java/com/graphhopper/routing/ch/NodeBasedCHBidirPathExtractor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import com.graphhopper.storage.ShortcutUnpacker;
2525
import com.graphhopper.util.EdgeIteratorState;
2626

27-
import static com.graphhopper.util.EdgeIterator.NO_EDGE;
28-
2927
public class NodeBasedCHBidirPathExtractor extends BidirPathExtractor {
3028
private final ShortcutUnpacker shortcutUnpacker;
3129

@@ -48,7 +46,7 @@ private ShortcutUnpacker createShortcutUnpacker(Graph routingGraph, final Weight
4846
@Override
4947
public void visit(EdgeIteratorState edge, boolean reverse, int prevOrNextEdgeId) {
5048
path.addDistance(edge.getDistance());
51-
path.addTime(weighting.calcMillis(edge, reverse, NO_EDGE));
49+
path.addTime(weighting.calcEdgeMillis(edge, reverse));
5250
path.addEdge(edge.getEdge());
5351
}
5452
}, false);

core/src/main/java/com/graphhopper/routing/ch/PrepareCHEdgeIteratorImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private double getOrigEdgeWeight(boolean reverse, boolean needWeight) {
101101
if (!needWeight) {
102102
return 0;
103103
}
104-
return weighting.calcWeight(chIterator, reverse, EdgeIterator.NO_EDGE);
104+
return weighting.calcEdgeWeight(chIterator, reverse);
105105
}
106106

107107
@Override

core/src/main/java/com/graphhopper/routing/lm/LandmarkStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ boolean isInfinity(long pointer) {
583583
}
584584

585585
int calcWeight(EdgeIteratorState edge, boolean reverse) {
586-
return (int) (weighting.calcWeight(edge, reverse, EdgeIterator.NO_EDGE) / factor);
586+
return (int) (weighting.calcEdgeWeight(edge, reverse) / factor);
587587
}
588588

589589
// From all available landmarks pick just a few active ones

core/src/main/java/com/graphhopper/routing/util/AbstractFlagEncoder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.graphhopper.reader.osm.conditional.ConditionalOSMTagInspector;
2424
import com.graphhopper.reader.osm.conditional.DateRangeParser;
2525
import com.graphhopper.routing.profiles.*;
26-
import com.graphhopper.routing.weighting.TurnWeighting;
2726
import com.graphhopper.storage.IntsRef;
2827
import com.graphhopper.util.*;
2928
import org.slf4j.Logger;
@@ -495,10 +494,12 @@ public void setEncodedValueLookup(EncodedValueLookup encodedValueLookup) {
495494
}
496495

497496
@Override
498-
public boolean supports(Class<?> feature) {
499-
if (TurnWeighting.class.isAssignableFrom(feature))
500-
return maxTurnCosts > 0;
497+
public boolean supportsTurnCosts() {
498+
return maxTurnCosts > 0;
499+
}
501500

501+
@Override
502+
public boolean supports(Class<?> feature) {
502503
return false;
503504
}
504505

core/src/main/java/com/graphhopper/routing/util/EncodingManager.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.graphhopper.reader.osm.conditional.DateRangeParser;
2525
import com.graphhopper.routing.profiles.*;
2626
import com.graphhopper.routing.util.parsers.*;
27-
import com.graphhopper.routing.weighting.TurnWeighting;
2827
import com.graphhopper.storage.*;
2928
import com.graphhopper.util.EdgeIteratorState;
3029
import com.graphhopper.util.Helper;
@@ -352,7 +351,7 @@ public EncodingManager build() {
352351

353352
// FlagEncoder can demand TurnCostParsers => add them after the explicitly added ones
354353
for (AbstractFlagEncoder encoder : flagEncoderList) {
355-
if (encoder.supports(TurnWeighting.class) && !em.turnCostParsers.containsKey(encoder.toString()))
354+
if (encoder.supportsTurnCosts() && !em.turnCostParsers.containsKey(encoder.toString()))
356355
_addTurnCostParser(new OSMTurnRelationParser(encoder.toString(), encoder.getMaxTurnCosts()));
357356
}
358357

@@ -741,7 +740,7 @@ public List<FlagEncoder> fetchEdgeEncoders() {
741740

742741
public boolean needsTurnCostsSupport() {
743742
for (FlagEncoder encoder : edgeEncoders) {
744-
if (encoder.supports(TurnWeighting.class))
743+
if (encoder.supportsTurnCosts())
745744
return true;
746745
}
747746
return false;

core/src/main/java/com/graphhopper/routing/util/FlagEncoder.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@
1717
*/
1818
package com.graphhopper.routing.util;
1919

20-
import com.graphhopper.reader.OSMTurnRelation;
2120
import com.graphhopper.routing.profiles.BooleanEncodedValue;
2221
import com.graphhopper.routing.profiles.DecimalEncodedValue;
2322
import com.graphhopper.routing.profiles.EncodedValueLookup;
24-
import com.graphhopper.storage.IntsRef;
25-
import com.graphhopper.util.InstructionAnnotation;
26-
import com.graphhopper.util.Translation;
2723

2824
/**
2925
* This class provides methods to define how a value (like speed or direction) converts to a flag
@@ -54,6 +50,8 @@ public interface FlagEncoder extends EncodedValueLookup {
5450
*/
5551
DecimalEncodedValue getAverageSpeedEnc();
5652

53+
boolean supportsTurnCosts();
54+
5755
/**
5856
* Returns true if the feature class is supported like TurnWeighting or PriorityWeighting.
5957
* Use support(String) instead.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public TestAlgoCollector assertDistance(EncodingManager encodingManager, AlgoHel
5151
QueryGraph queryGraph = QueryGraph.lookup(algoEntry.getForQueryGraph(), queryList);
5252
AlgorithmOptions opts = algoEntry.getAlgorithmOptions();
5353
FlagEncoder encoder = opts.getWeighting().getFlagEncoder();
54-
if (encoder.supports(TurnWeighting.class)) {
54+
if (encoder.supportsTurnCosts()) {
5555
if (!opts.getTraversalMode().isEdgeBased()) {
5656
errors.add("Cannot use TurnWeighting with node based traversal");
5757
return this;

core/src/main/java/com/graphhopper/routing/weighting/AbstractAdjustedWeighting.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import com.graphhopper.routing.util.HintsMap;
2222
import com.graphhopper.util.EdgeIteratorState;
2323

24+
import static com.graphhopper.util.EdgeIterator.NO_EDGE;
25+
2426
/**
2527
* The AdjustedWeighting wraps another Weighting.
2628
*
@@ -40,6 +42,16 @@ public double getMinWeight(double distance) {
4042
return superWeighting.getMinWeight(distance);
4143
}
4244

45+
@Override
46+
public final double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) {
47+
return calcWeight(edgeState, reverse, NO_EDGE);
48+
}
49+
50+
@Override
51+
public final long calcEdgeMillis(EdgeIteratorState edgeState, boolean reverse) {
52+
return calcMillis(edgeState, reverse, NO_EDGE);
53+
}
54+
4355
@Override
4456
public long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
4557
return superWeighting.calcMillis(edgeState, reverse, prevOrNextEdgeId);

core/src/main/java/com/graphhopper/routing/weighting/AbstractWeighting.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.graphhopper.routing.util.HintsMap;
2424
import com.graphhopper.util.EdgeIteratorState;
2525

26+
import static com.graphhopper.util.EdgeIterator.NO_EDGE;
2627
import static com.graphhopper.util.Helper.toLowerCase;
2728

2829
/**
@@ -44,6 +45,16 @@ protected AbstractWeighting(FlagEncoder encoder) {
4445
accessEnc = encoder.getAccessEnc();
4546
}
4647

48+
@Override
49+
public final double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) {
50+
return calcWeight(edgeState, reverse, NO_EDGE);
51+
}
52+
53+
@Override
54+
public final long calcEdgeMillis(EdgeIteratorState edgeState, boolean reverse) {
55+
return calcMillis(edgeState, reverse, NO_EDGE);
56+
}
57+
4758
@Override
4859
public long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
4960
// special case for loop edges: since they do not have a meaningful direction we always need to read them in
@@ -95,7 +106,7 @@ public boolean equals(Object obj) {
95106
return toString().equals(other.toString());
96107
}
97108

98-
static final boolean isValidName(String name) {
109+
static boolean isValidName(String name) {
99110
if (name == null || name.isEmpty())
100111
return false;
101112

core/src/main/java/com/graphhopper/routing/weighting/TurnWeighting.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import static com.graphhopper.routing.profiles.TurnCost.EV_SUFFIX;
3030
import static com.graphhopper.routing.util.EncodingManager.getKey;
31+
import static com.graphhopper.util.EdgeIterator.NO_EDGE;
3132

3233
/**
3334
* Provides methods to retrieve turn costs for a specific turn.
@@ -36,7 +37,6 @@
3637
* @author Peter Karich
3738
*/
3839
public class TurnWeighting implements Weighting {
39-
public static final int INFINITE_U_TURN_COSTS = -1;
4040
private final DecimalEncodedValue turnCostEnc;
4141
private final TurnCostStorage turnCostStorage;
4242
private final Weighting superWeighting;
@@ -76,6 +76,11 @@ public double getMinWeight(double distance) {
7676
return superWeighting.getMinWeight(distance);
7777
}
7878

79+
@Override
80+
public final double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) {
81+
return calcWeight(edgeState, reverse, NO_EDGE);
82+
}
83+
7984
@Override
8085
public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
8186
double weight = superWeighting.calcWeight(edgeState, reverse, prevOrNextEdgeId);
@@ -89,6 +94,11 @@ public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevO
8994
return weight + turnCosts;
9095
}
9196

97+
@Override
98+
public final long calcEdgeMillis(EdgeIteratorState edgeState, boolean reverse) {
99+
return calcMillis(edgeState, reverse, NO_EDGE);
100+
}
101+
92102
@Override
93103
public long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
94104
long millis = superWeighting.calcMillis(edgeState, reverse, prevOrNextEdgeId);

0 commit comments

Comments
 (0)