| | 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 forward = 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 = forward ? factor.ForwardFactor * length : factor.BackwardFactor * length; |
| 6 | 62 | | var canAccess = forward ? 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); |
| 0 | 67 | | var turnCosts = edgeEnumerator.GetTurnCostToTail(turn.Value); |
| | 68 | |
|
| 0 | 69 | | foreach (var (turnCostType, attributes, turnCost, prefixEdges) in turnCosts) |
| 0 | 70 | | { |
| | 71 | | // TODO: compare prefix edges with the previous edges. |
| | 72 | |
|
| 0 | 73 | | var turnCostFactor = _turnCostFactorCache.Get(turnCostType); |
| 0 | 74 | | if (turnCostFactor == null) |
| 0 | 75 | | { |
| 0 | 76 | | turnCostFactor = _profile.TurnCostFactor(attributes); |
| 0 | 77 | | _turnCostFactorCache.Set(turnCostType, turnCostFactor.Value); |
| 0 | 78 | | } |
| 0 | 79 | | if (turnCostFactor.Value.IsBinary && turnCost > 0) |
| 0 | 80 | | { |
| 0 | 81 | | totalTurnCost = double.MaxValue; |
| 0 | 82 | | break; |
| | 83 | | } |
| | 84 | |
|
| 0 | 85 | | totalTurnCost += turnCostFactor.Value.CostFactor * turnCost; |
| 0 | 86 | | } |
| | 87 | |
|
| 0 | 88 | | return (canAccess, factor.CanStop, cost, totalTurnCost); |
| 6 | 89 | | } |
| | 90 | | } |