| | | 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 | | |
| | | 8 | | namespace Itinero.Routing.Costs; |
| | | 9 | | |
| | | 10 | | internal class ProfileCostFunction : ICostFunction |
| | | 11 | | { |
| | | 12 | | private readonly Profile _profile; |
| | | 13 | | |
| | 30 | 14 | | public ProfileCostFunction(Profile profile) |
| | 30 | 15 | | { |
| | 30 | 16 | | _profile = profile; |
| | 30 | 17 | | } |
| | | 18 | | |
| | | 19 | | public (bool canAccess, bool canStop, double cost, double turnCost) Get( |
| | | 20 | | IEdgeEnumerator<RoutingNetwork> edgeEnumerator, bool tailToHead = true, |
| | | 21 | | IEnumerable<(EdgeId edgeId, byte? turn)>? previousEdges = null) |
| | 262 | 22 | | { |
| | 262 | 23 | | previousEdges ??= ArraySegment<(EdgeId edgeId, byte? turn)>.Empty; |
| | | 24 | | |
| | 262 | 25 | | var factor = _profile.FactorInEdgeDirection(edgeEnumerator); |
| | 262 | 26 | | var length = edgeEnumerator.Length ?? |
| | 262 | 27 | | (uint)(edgeEnumerator.EdgeLength() * 100); |
| | 262 | 28 | | var directedFactor = tailToHead ? factor.ForwardFactor : factor.BackwardFactor; |
| | 262 | 29 | | var cost = directedFactor * length; |
| | 262 | 30 | | var canAccess = directedFactor > 0; |
| | | 31 | | |
| | | 32 | | // check for turn costs. |
| | 262 | 33 | | var totalTurnCost = 0.0; |
| | 262 | 34 | | var (_, turn) = previousEdges.FirstOrDefault(); |
| | 520 | 35 | | if (turn == null) return (canAccess, factor.CanStop, cost, totalTurnCost); |
| | | 36 | | |
| | | 37 | | // there are turn costs. |
| | 4 | 38 | | var turnCosts = edgeEnumerator.GetTurnCostToTail(turn.Value); |
| | 16 | 39 | | foreach (var (_, attributes, turnCost, prefixEdges) in turnCosts) |
| | 4 | 40 | | { |
| | | 41 | | // TODO: compare prefix edges with the previous edges. |
| | | 42 | | |
| | 4 | 43 | | var turnCostFactor = _profile.TurnCostFactor(attributes); |
| | 4 | 44 | | if (turnCostFactor.IsBinary && turnCost > 0) |
| | 4 | 45 | | { |
| | 4 | 46 | | totalTurnCost = double.MaxValue; |
| | 4 | 47 | | break; |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | totalTurnCost += turnCostFactor.CostFactor * turnCost; |
| | 0 | 51 | | } |
| | | 52 | | |
| | 4 | 53 | | return (canAccess, factor.CanStop, cost, totalTurnCost); |
| | 262 | 54 | | } |
| | | 55 | | } |