| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using Itinero.Network; |
| | | 5 | | using Itinero.Network.Enumerators.Edges; |
| | | 6 | | using Itinero.Profiles; |
| | | 7 | | using Itinero.Routing.Costs.Caches; |
| | | 8 | | |
| | | 9 | | namespace Itinero.Routing.Costs; |
| | | 10 | | |
| | | 11 | | internal class ProfileCostFunctionCached : ICostFunction |
| | | 12 | | { |
| | | 13 | | private readonly Profile _profile; |
| | | 14 | | private readonly EdgeFactorCache _edgeFactorCache; |
| | | 15 | | private readonly TurnCostFactorCache _turnCostFactorCache; |
| | | 16 | | |
| | 5 | 17 | | internal ProfileCostFunctionCached(Profile profile, EdgeFactorCache edgeFactorCache, TurnCostFactorCache turnCostFac |
| | 5 | 18 | | { |
| | 5 | 19 | | _profile = profile; |
| | 5 | 20 | | _edgeFactorCache = edgeFactorCache; |
| | 5 | 21 | | _turnCostFactorCache = turnCostFactorCache; |
| | 5 | 22 | | } |
| | | 23 | | |
| | | 24 | | public (bool canAccess, bool canStop, double cost, double turnCost) Get( |
| | | 25 | | IEdgeEnumerator<RoutingNetwork> edgeEnumerator, bool tailToHead = true, |
| | | 26 | | IEnumerable<(EdgeId edgeId, byte? turn)>? previousEdges = null) |
| | 6 | 27 | | { |
| | 6 | 28 | | previousEdges ??= ArraySegment<(EdgeId edgeId, byte? turn)>.Empty; |
| | | 29 | | |
| | | 30 | | // get edge factor and length. |
| | | 31 | | EdgeFactor factor; |
| | 6 | 32 | | var edgeTypeId = edgeEnumerator.EdgeTypeId; |
| | 6 | 33 | | if (edgeTypeId == null) |
| | 0 | 34 | | { |
| | 0 | 35 | | factor = _profile.FactorInEdgeDirection(edgeEnumerator); |
| | 0 | 36 | | } |
| | | 37 | | else |
| | 6 | 38 | | { |
| | 6 | 39 | | var edgeFactor = _edgeFactorCache.Get(edgeTypeId.Value); |
| | 6 | 40 | | if (edgeFactor == null) |
| | 5 | 41 | | { |
| | | 42 | | // no cached value, cache forward value. |
| | 5 | 43 | | factor = _profile.Factor(edgeEnumerator.Attributes); |
| | 5 | 44 | | _edgeFactorCache.Set(edgeTypeId.Value, factor); |
| | 5 | 45 | | } |
| | | 46 | | else |
| | 1 | 47 | | { |
| | 1 | 48 | | factor = edgeFactor.Value; |
| | 1 | 49 | | } |
| | | 50 | | |
| | | 51 | | // cached value is always forward. |
| | 6 | 52 | | if (!edgeEnumerator.Forward) |
| | 2 | 53 | | { |
| | 2 | 54 | | factor = factor.Reverse; |
| | 2 | 55 | | } |
| | 6 | 56 | | } |
| | | 57 | | |
| | 6 | 58 | | var lengthNullable = edgeEnumerator.Length; |
| | 6 | 59 | | var length = lengthNullable ?? |
| | 6 | 60 | | (uint)(edgeEnumerator.EdgeLength() * 100); |
| | 6 | 61 | | var cost = tailToHead ? factor.ForwardFactor * length : factor.BackwardFactor * length; |
| | 6 | 62 | | var canAccess = tailToHead ? factor.ForwardFactor > 0 : factor.BackwardFactor > 0; |
| | | 63 | | |
| | 6 | 64 | | var totalTurnCost = 0.0; |
| | 6 | 65 | | var (_, turn) = previousEdges.FirstOrDefault(); |
| | 12 | 66 | | if (turn == null) return (canAccess, factor.CanStop, cost, totalTurnCost); |
| | | 67 | | |
| | 0 | 68 | | if (tailToHead) |
| | 0 | 69 | | { |
| | 0 | 70 | | var turnCosts = edgeEnumerator.GetTurnCostToTail(turn.Value); |
| | | 71 | | |
| | 0 | 72 | | foreach (var (turnCostType, attributes, turnCost, prefixEdges) in turnCosts) |
| | 0 | 73 | | { |
| | | 74 | | // TODO: compare prefix edges with the previous edges. |
| | | 75 | | |
| | 0 | 76 | | var turnCostFactor = _turnCostFactorCache.Get(turnCostType); |
| | 0 | 77 | | if (turnCostFactor == null) |
| | 0 | 78 | | { |
| | 0 | 79 | | turnCostFactor = _profile.TurnCostFactor(attributes); |
| | 0 | 80 | | _turnCostFactorCache.Set(turnCostType, turnCostFactor.Value); |
| | 0 | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | if (turnCostFactor.Value.IsBinary && turnCost > 0) |
| | 0 | 84 | | { |
| | 0 | 85 | | totalTurnCost = double.MaxValue; |
| | 0 | 86 | | break; |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | totalTurnCost += turnCostFactor.Value.CostFactor * turnCost; |
| | 0 | 90 | | } |
| | 0 | 91 | | } |
| | | 92 | | else |
| | 0 | 93 | | { |
| | 0 | 94 | | var turnCosts = edgeEnumerator.GetTurnCostFromTail(turn.Value); |
| | | 95 | | |
| | 0 | 96 | | foreach (var (turnCostType, attributes, turnCost, prefixEdges) in turnCosts) |
| | 0 | 97 | | { |
| | | 98 | | // TODO: compare prefix edges with the previous edges. |
| | | 99 | | |
| | 0 | 100 | | var turnCostFactor = _turnCostFactorCache.Get(turnCostType); |
| | 0 | 101 | | if (turnCostFactor == null) |
| | 0 | 102 | | { |
| | 0 | 103 | | turnCostFactor = _profile.TurnCostFactor(attributes); |
| | 0 | 104 | | _turnCostFactorCache.Set(turnCostType, turnCostFactor.Value); |
| | 0 | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | if (turnCostFactor.Value.IsBinary && turnCost > 0) |
| | 0 | 108 | | { |
| | 0 | 109 | | totalTurnCost = double.MaxValue; |
| | 0 | 110 | | break; |
| | | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | totalTurnCost += turnCostFactor.Value.CostFactor * turnCost; |
| | 0 | 114 | | } |
| | 0 | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | return (canAccess, factor.CanStop, cost, totalTurnCost); |
| | 6 | 118 | | } |
| | | 119 | | } |