| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using Itinero.Network.Storage; |
| | 4 | | using Itinero.Network.TurnCosts; |
| | 5 | | using Reminiscence.Arrays; |
| | 6 | |
|
| | 7 | | namespace Itinero.Network.Tiles.Standalone; |
| | 8 | |
|
| | 9 | | public partial class StandaloneNetworkTile |
| | 10 | | { |
| 3 | 11 | | private uint _turnCostPointer = 0; |
| 3 | 12 | | private readonly ArrayBase<byte> _turnCosts = new MemoryArray<byte>(0); |
| | 13 | |
|
| | 14 | | internal void AddGlobalTurnCosts((Guid globalEdgeId, bool forward)[] edges, uint[,] costs, uint turnCostType, |
| | 15 | | IEnumerable<(string key, string value)> attributes) |
| 0 | 16 | | { |
| 0 | 17 | | if (edges.Length > OrderCoder.MaxOrderHeadTail) |
| 0 | 18 | | { |
| 0 | 19 | | throw new ArgumentException( |
| 0 | 20 | | $"Cannot add turn costs for vertices with more than {OrderCoder.MaxOrderHeadTail} edges."); |
| | 21 | | } |
| | 22 | |
|
| | 23 | | // make sure there is space in the turn cost array. |
| 0 | 24 | | var maxLength = _turnCostPointer + 5 + 5 + 5 + |
| 0 | 25 | | (edges.Length * (16 + 5)) + |
| 0 | 26 | | (costs.GetLength(0) * costs.GetLength(1) * 5); |
| 0 | 27 | | while (_turnCosts.Length < maxLength) |
| 0 | 28 | | { |
| 0 | 29 | | _turnCosts.Resize(_turnCosts.Length + 256); |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | // add turn. |
| 0 | 33 | | var a = this.SetAttributes(attributes); |
| 0 | 34 | | _turnCostPointer += (uint)_turnCosts.SetDynamicUInt32(_turnCostPointer, a); |
| 0 | 35 | | _turnCostPointer += (uint)_turnCosts.SetDynamicUInt32(_turnCostPointer, turnCostType); |
| 0 | 36 | | _turnCostPointer += (uint)_turnCosts.SetDynamicUInt32(_turnCostPointer, (uint)edges.Length); |
| 0 | 37 | | for (var i = 0; i < edges.Length; i++) |
| 0 | 38 | | { |
| 0 | 39 | | var (globalEdgeId, forward) = edges[i]; |
| | 40 | |
|
| 0 | 41 | | _turnCostPointer += (uint)_turnCosts.SetGuid(_turnCostPointer, globalEdgeId); |
| 0 | 42 | | _turnCostPointer += (uint)_turnCosts.SetDynamicUInt32(_turnCostPointer, (uint)(forward ? 1 : 0)); |
| 0 | 43 | | } |
| | 44 | |
|
| 0 | 45 | | for (var x = 0; x < costs.GetLength(0); x++) |
| 0 | 46 | | for (var y = 0; y < costs.GetLength(1); y++) |
| 0 | 47 | | { |
| 0 | 48 | | _turnCostPointer += (uint)_turnCosts.SetDynamicUInt32(_turnCostPointer, costs[x, y]); |
| 0 | 49 | | } |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Gets all the global turn costs. |
| | 54 | | /// </summary> |
| | 55 | | /// <returns></returns> |
| | 56 | | public IEnumerable<((Guid globalEdgeId, bool forward)[] edges, uint[,] costs, uint turnCostType, |
| | 57 | | IEnumerable<(string key, string value)> attributes)> GetGlobalTurnCost() |
| 0 | 58 | | { |
| 0 | 59 | | var pointer = 0L; |
| 0 | 60 | | while (pointer < _turnCostPointer) |
| 0 | 61 | | { |
| 0 | 62 | | pointer += _turnCosts.GetDynamicUInt32(pointer, out var a); |
| 0 | 63 | | pointer += _turnCosts.GetDynamicUInt32(pointer, out var turnCostType); |
| 0 | 64 | | pointer += _turnCosts.GetDynamicUInt32(pointer, out var edgeCount); |
| 0 | 65 | | var edges = new (Guid globalEdgeId, bool forward)[edgeCount]; |
| 0 | 66 | | for (var i = 0; i < edgeCount; i++) |
| 0 | 67 | | { |
| 0 | 68 | | _turnCostPointer += (uint)_turnCosts.GetGuid(_turnCostPointer, out var globalEdgeId); |
| 0 | 69 | | _turnCostPointer += (uint)_turnCosts.GetDynamicUInt32(_turnCostPointer, out var forwardValue); |
| 0 | 70 | | var forward = forwardValue == 1; |
| | 71 | |
|
| 0 | 72 | | edges[i] = (globalEdgeId, forward); |
| 0 | 73 | | } |
| | 74 | |
|
| 0 | 75 | | var costs = new uint[edges.Length, edges.Length]; |
| 0 | 76 | | for (var x = 0; x < costs.GetLength(0); x++) |
| 0 | 77 | | for (var y = 0; y < costs.GetLength(1); y++) |
| 0 | 78 | | { |
| 0 | 79 | | pointer += _crossings.GetDynamicUInt32(pointer, out var cost); |
| 0 | 80 | | costs[x, y] = cost; |
| 0 | 81 | | } |
| | 82 | |
|
| 0 | 83 | | yield return (edges, costs, turnCostType, this.GetAttributes(a)); |
| 0 | 84 | | } |
| 0 | 85 | | } |
| | 86 | |
|
| 3 | 87 | | private uint _globalIdPointer = 0; |
| 3 | 88 | | private readonly ArrayBase<byte> _globalIds = new MemoryArray<byte>(0); |
| | 89 | |
|
| | 90 | | internal void AddGlobalIdFor(EdgeId edgeId, Guid globalEdgeId) |
| 2 | 91 | | { |
| | 92 | | // make sure there is space. |
| 2 | 93 | | var maxLength = _globalIdPointer + 16 + 5; |
| 3 | 94 | | while (_globalIds.Length < maxLength) |
| 1 | 95 | | { |
| 1 | 96 | | _globalIds.Resize(_globalIds.Length + 64); |
| 1 | 97 | | } |
| | 98 | |
|
| 2 | 99 | | _globalIdPointer += (uint)_globalIds.SetGuid(_globalIdPointer, globalEdgeId); |
| 2 | 100 | | _globalIdPointer += (uint)_globalIds.SetDynamicInt32(_globalIdPointer, (int)edgeId.LocalId); |
| 2 | 101 | | } |
| | 102 | |
|
| | 103 | | internal void AddGlobalIdFor(BoundaryEdgeId boundaryEdgeId, Guid globalEdgeId) |
| 0 | 104 | | { |
| | 105 | | // make sure there is space. |
| 0 | 106 | | var maxLength = _globalIdPointer + 16 + 5; |
| 0 | 107 | | while (_globalIds.Length < maxLength) |
| 0 | 108 | | { |
| 0 | 109 | | _globalIds.Resize(_globalIds.Length + 64); |
| 0 | 110 | | } |
| | 111 | |
|
| 0 | 112 | | _globalIdPointer += (uint)_globalIds.SetGuid(_globalIdPointer, globalEdgeId); |
| 0 | 113 | | _globalIdPointer += (uint)_globalIds.SetDynamicInt32(_globalIdPointer, (int)-(boundaryEdgeId.LocalId + 1)); |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// Gets all the global edge ids. |
| | 118 | | /// </summary> |
| | 119 | | /// <returns></returns> |
| | 120 | | public IEnumerable<(Guid globalEdgeId, EdgeId? edgeId, BoundaryEdgeId? boundaryEdgeId)> GetGlobalEdgeIds() |
| 0 | 121 | | { |
| 0 | 122 | | var pointer = 0L; |
| 0 | 123 | | while (pointer < _turnCostPointer) |
| 0 | 124 | | { |
| 0 | 125 | | pointer += _turnCosts.GetGuid(pointer, out var globalEdgeId); |
| 0 | 126 | | pointer += _turnCosts.GetDynamicInt32(pointer, out var localId); |
| | 127 | |
|
| 0 | 128 | | if (localId >= 0) |
| 0 | 129 | | { |
| 0 | 130 | | yield return (globalEdgeId, new EdgeId(this.TileId, (uint)localId), null); |
| 0 | 131 | | } |
| | 132 | | else |
| 0 | 133 | | { |
| 0 | 134 | | yield return (globalEdgeId, null, new BoundaryEdgeId((uint)-localId - 1)); |
| 0 | 135 | | } |
| 0 | 136 | | } |
| 0 | 137 | | } |
| | 138 | | } |