Search in sources :

Example 1 with TurnCost

use of com.graphhopper.routing.ev.TurnCost in project graphhopper by graphhopper.

the class CHPreparationGraph method buildTurnCostFunctionFromTurnCostStorage.

/**
 * Builds a turn cost function for a given graph('s turn cost storage) and a weighting.
 * The trivial implementation would be simply returning {@link Weighting#calcTurnWeight}. However, it turned out
 * that reading all turn costs for the current encoder and then storing them in separate arrays upfront speeds up
 * edge-based CH preparation by about 25%. See #2084
 */
public static TurnCostFunction buildTurnCostFunctionFromTurnCostStorage(Graph graph, Weighting weighting) {
    FlagEncoder encoder = weighting.getFlagEncoder();
    String key = TurnCost.key(encoder.toString());
    if (!encoder.hasEncodedValue(key))
        return (inEdge, viaNode, outEdge) -> 0;
    DecimalEncodedValue turnCostEnc = encoder.getDecimalEncodedValue(key);
    TurnCostStorage turnCostStorage = graph.getTurnCostStorage();
    // we maintain a list of inEdge/outEdge/turn-cost triples (we use two arrays for this) that is sorted by nodes
    LongArrayList turnCostEdgePairs = new LongArrayList();
    DoubleArrayList turnCosts = new DoubleArrayList();
    // for each node we store the index of the first turn cost entry/triple in the list
    final int[] turnCostNodes = new int[graph.getNodes() + 1];
    TurnCostStorage.TurnRelationIterator tcIter = turnCostStorage.getAllTurnRelations();
    int lastNode = -1;
    while (tcIter.next()) {
        int viaNode = tcIter.getViaNode();
        if (viaNode < lastNode)
            throw new IllegalStateException();
        long edgePair = BitUtil.LITTLE.combineIntsToLong(tcIter.getFromEdge(), tcIter.getToEdge());
        // note that as long as we only use OSM turn restrictions all the turn costs are infinite anyway
        double turnCost = tcIter.getCost(turnCostEnc);
        int index = turnCostEdgePairs.size();
        turnCostEdgePairs.add(edgePair);
        turnCosts.add(turnCost);
        if (viaNode != lastNode) {
            for (int i = lastNode + 1; i <= viaNode; i++) {
                turnCostNodes[i] = index;
            }
        }
        lastNode = viaNode;
    }
    for (int i = lastNode + 1; i <= turnCostNodes.length - 1; i++) {
        turnCostNodes[i] = turnCostEdgePairs.size();
    }
    turnCostNodes[turnCostNodes.length - 1] = turnCostEdgePairs.size();
    // currently the u-turn costs are the same for all junctions, so for now we just get them for one of them
    double uTurnCosts = weighting.calcTurnWeight(1, 0, 1);
    return (inEdge, viaNode, outEdge) -> {
        if (!EdgeIterator.Edge.isValid(inEdge) || !EdgeIterator.Edge.isValid(outEdge))
            return 0;
        else if (inEdge == outEdge)
            return uTurnCosts;
        // traverse all turn cost entries we have for this viaNode and return the turn costs if we find a match
        for (int i = turnCostNodes[viaNode]; i < turnCostNodes[viaNode + 1]; i++) {
            long l = turnCostEdgePairs.get(i);
            if (inEdge == BitUtil.LITTLE.getIntLow(l) && outEdge == BitUtil.LITTLE.getIntHigh(l))
                return turnCosts.get(i);
        }
        return 0;
    };
}
Also used : IndirectSort(com.carrotsearch.hppc.sorting.IndirectSort) com.carrotsearch.hppc(com.carrotsearch.hppc) TurnCost(com.graphhopper.routing.ev.TurnCost) EdgeIterator(com.graphhopper.util.EdgeIterator) GHUtility(com.graphhopper.util.GHUtility) BitUtil(com.graphhopper.util.BitUtil) ArrayUtil.zero(com.graphhopper.util.ArrayUtil.zero) IndirectComparator(com.carrotsearch.hppc.sorting.IndirectComparator) TurnCostStorage(com.graphhopper.storage.TurnCostStorage) Weighting(com.graphhopper.routing.weighting.Weighting) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) Graph(com.graphhopper.storage.Graph) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) AllEdgesIterator(com.graphhopper.routing.util.AllEdgesIterator) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) TurnCostStorage(com.graphhopper.storage.TurnCostStorage)

Aggregations

com.carrotsearch.hppc (com.carrotsearch.hppc)1 IndirectComparator (com.carrotsearch.hppc.sorting.IndirectComparator)1 IndirectSort (com.carrotsearch.hppc.sorting.IndirectSort)1 DecimalEncodedValue (com.graphhopper.routing.ev.DecimalEncodedValue)1 TurnCost (com.graphhopper.routing.ev.TurnCost)1 AllEdgesIterator (com.graphhopper.routing.util.AllEdgesIterator)1 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)1 Weighting (com.graphhopper.routing.weighting.Weighting)1 Graph (com.graphhopper.storage.Graph)1 TurnCostStorage (com.graphhopper.storage.TurnCostStorage)1 ArrayUtil.zero (com.graphhopper.util.ArrayUtil.zero)1 BitUtil (com.graphhopper.util.BitUtil)1 EdgeIterator (com.graphhopper.util.EdgeIterator)1 GHUtility (com.graphhopper.util.GHUtility)1