| | 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 forward = true, |
| | 21 | | IEnumerable<(EdgeId edgeId, byte? turn)>? previousEdges = null) |
| 191 | 22 | | { |
| 191 | 23 | | previousEdges ??= ArraySegment<(EdgeId edgeId, byte? turn)>.Empty; |
| | 24 | |
|
| 191 | 25 | | var factor = _profile.FactorInEdgeDirection(edgeEnumerator); |
| 191 | 26 | | var length = edgeEnumerator.Length ?? |
| 191 | 27 | | (uint)(edgeEnumerator.EdgeLength() * 100); |
| 191 | 28 | | var directedFactor = forward ? factor.ForwardFactor : factor.BackwardFactor; |
| 191 | 29 | | var cost = directedFactor * length; |
| 191 | 30 | | var canAccess = directedFactor > 0; |
| | 31 | |
|
| | 32 | | // check for turn costs. |
| 191 | 33 | | var totalTurnCost = 0.0; |
| 191 | 34 | | var (_, turn) = previousEdges.FirstOrDefault(); |
| 378 | 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); |
| 191 | 54 | | } |
| | 55 | | } |