| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Linq; |
| | | 5 | | using Itinero.IO; |
| | | 6 | | using Itinero.Network.Storage; |
| | | 7 | | using Itinero.Network.Tiles.Standalone.Global; |
| | | 8 | | using Itinero.Network.TurnCosts; |
| | | 9 | | |
| | | 10 | | namespace Itinero.Network.Tiles.Standalone; |
| | | 11 | | |
| | | 12 | | public partial class StandaloneNetworkTile |
| | | 13 | | { |
| | | 14 | | // here we store all the data about global restrictions that could not be turned into turn costs yet because the net |
| | | 15 | | // the edge of the tiles. we store the global edge ids of the restriction along with the type so we can create the t |
| | | 16 | | // is being completed |
| | | 17 | | private uint _globalRestrictionsPointer; |
| | 0 | 18 | | private byte[] _globalRestrictions = new byte[0]; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Adds a global restriction for processing when the tile is loaded. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="sequence">The sequence of global edge ids with local edge ids if they are already known.</param> |
| | | 24 | | /// <param name="isProhibitory">The type of restriction.</param> |
| | | 25 | | /// <param name="turnCostTypeId">The turn cost type, already determined.</param> |
| | | 26 | | /// <param name="attributes">The raw attributes of the restriction.</param> |
| | | 27 | | public void AddGlobalRestriction(IEnumerable<(GlobalEdgeId globalEdgeId, EdgeId? edge)> sequence, |
| | | 28 | | bool isProhibitory, uint turnCostTypeId, IEnumerable<(string key, string value)> attributes) |
| | 0 | 29 | | { |
| | 0 | 30 | | var edges = sequence.ToList(); |
| | 0 | 31 | | if (edges.Count > OrderCoder.MaxOrderHeadTail) throw new ArgumentException( |
| | 0 | 32 | | $"Cannot add turn costs for vertices with more than {OrderCoder.MaxOrderHeadTail} edges."); |
| | | 33 | | |
| | | 34 | | // make sure there is space in the turn cost array. |
| | 0 | 35 | | var maxLength = _globalRestrictionsPointer + 5 + 5 + 5 + |
| | 0 | 36 | | (edges.Count * (9 + 5 + 5 + 5)); |
| | 0 | 37 | | while (_globalRestrictions.Length < maxLength) |
| | 0 | 38 | | { |
| | 0 | 39 | | Array.Resize(ref _globalRestrictions, (int)(_globalRestrictions.Length + 256)); |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | // add turn. |
| | 0 | 43 | | var a = this.SetAttributes(attributes); |
| | 0 | 44 | | _globalRestrictionsPointer += _globalRestrictions.SetDynamicUInt32(_globalRestrictionsPointer, a); |
| | 0 | 45 | | if (isProhibitory) |
| | 0 | 46 | | { |
| | | 47 | | // isProhibitory if turnCostTypeId is encoded as a positive number. |
| | 0 | 48 | | _globalRestrictionsPointer += _globalRestrictions.SetDynamicInt32(_globalRestrictionsPointer, (int)(turnCost |
| | 0 | 49 | | } |
| | | 50 | | else |
| | 0 | 51 | | { |
| | | 52 | | // not isProhibitory if turnCostTypeId is encoded as a negative number. |
| | 0 | 53 | | _globalRestrictionsPointer += _globalRestrictions.SetDynamicInt32(_globalRestrictionsPointer, -(int)(turnCos |
| | 0 | 54 | | } |
| | 0 | 55 | | _globalRestrictionsPointer += _globalRestrictions.SetDynamicUInt32(_globalRestrictionsPointer, (uint)edges.Count |
| | 0 | 56 | | foreach (var (globalEdgeId, edgeId) in edges) |
| | 0 | 57 | | { |
| | 0 | 58 | | _globalRestrictionsPointer += _globalRestrictions.SetGlobalEdgeId(_globalRestrictionsPointer, globalEdgeId); |
| | 0 | 59 | | if (edgeId == null) |
| | 0 | 60 | | { |
| | 0 | 61 | | _globalRestrictionsPointer += _globalRestrictions.SetDynamicUInt32Nullable(_globalRestrictionsPointer, |
| | 0 | 62 | | null); |
| | 0 | 63 | | } |
| | | 64 | | else |
| | 0 | 65 | | { |
| | 0 | 66 | | _globalRestrictionsPointer += _globalRestrictions.SetDynamicUInt32Nullable(_globalRestrictionsPointer, |
| | 0 | 67 | | edgeId.Value.LocalId); |
| | 0 | 68 | | } |
| | 0 | 69 | | } |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Gets all the global restrictions. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <returns></returns> |
| | | 76 | | public IEnumerable<(IReadOnlyList<(GlobalEdgeId globalEdgeId, EdgeId? edgeId)> edges, bool isProhibitory, uint turnC |
| | | 77 | | IEnumerable<(string key, string value)> attributes)> GetGlobalRestrictions() |
| | 0 | 78 | | { |
| | 0 | 79 | | var pointer = 0L; |
| | 0 | 80 | | while (pointer < _globalRestrictionsPointer) |
| | 0 | 81 | | { |
| | 0 | 82 | | pointer += _globalRestrictions.GetDynamicUInt32(pointer, out var a); |
| | 0 | 83 | | pointer += _globalRestrictions.GetDynamicInt32(pointer, out var turnCostTypeSigned); |
| | | 84 | | uint turnCostType; |
| | | 85 | | bool isProhibitory; |
| | 0 | 86 | | if (turnCostTypeSigned > 0) |
| | 0 | 87 | | { |
| | 0 | 88 | | isProhibitory = true; |
| | 0 | 89 | | turnCostType = (uint)turnCostTypeSigned - 1; |
| | 0 | 90 | | } |
| | | 91 | | else |
| | 0 | 92 | | { |
| | 0 | 93 | | isProhibitory = false; |
| | 0 | 94 | | turnCostType = (uint)(-turnCostTypeSigned - 1); |
| | 0 | 95 | | } |
| | 0 | 96 | | pointer += _globalRestrictions.GetDynamicUInt32(pointer, out var edgeCount); |
| | 0 | 97 | | var edges = new (GlobalEdgeId globalEdgeId, EdgeId? edge)[edgeCount]; |
| | 0 | 98 | | for (var i = 0; i < edgeCount; i++) |
| | 0 | 99 | | { |
| | 0 | 100 | | pointer += _globalRestrictions.GetGlobalEdgeId(pointer, out var globalEdgeId); |
| | 0 | 101 | | pointer += _globalRestrictions.GetDynamicUInt32Nullable(pointer, |
| | 0 | 102 | | out var localId); |
| | | 103 | | |
| | 0 | 104 | | EdgeId? edgeId = null; |
| | 0 | 105 | | if (localId != null) |
| | 0 | 106 | | { |
| | 0 | 107 | | edgeId = new EdgeId(this.TileId, localId.Value); |
| | 0 | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | edges[i] = (globalEdgeId, edgeId); |
| | 0 | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | yield return (edges, isProhibitory, turnCostType, this.GetAttributes(a)); |
| | 0 | 114 | | } |
| | 0 | 115 | | } |
| | | 116 | | |
| | | 117 | | private void WriteGlobal(Stream stream) |
| | 0 | 118 | | { |
| | 0 | 119 | | stream.WriteVarUInt32(_globalRestrictionsPointer); |
| | 0 | 120 | | for (var i = 0; i < _globalRestrictionsPointer; i++) |
| | 0 | 121 | | { |
| | 0 | 122 | | stream.WriteByte(_globalRestrictions[i]); |
| | 0 | 123 | | } |
| | 0 | 124 | | } |
| | | 125 | | |
| | | 126 | | private void ReadGlobal(Stream stream) |
| | 0 | 127 | | { |
| | 0 | 128 | | _globalRestrictionsPointer = stream.ReadVarUInt32(); |
| | 0 | 129 | | _globalRestrictions = new byte[_globalRestrictionsPointer]; |
| | 0 | 130 | | for (var i = 0; i < _globalRestrictionsPointer; i++) |
| | 0 | 131 | | { |
| | 0 | 132 | | _globalRestrictions[i] = (byte)stream.ReadByte(); |
| | 0 | 133 | | } |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | private void ReadGlobal(byte[] data, ref int offset) |
| | 0 | 137 | | { |
| | 0 | 138 | | _globalRestrictionsPointer = BitCoderBuffer.GetVarUInt32(data, ref offset); |
| | 0 | 139 | | _globalRestrictions = new byte[_globalRestrictionsPointer]; |
| | 0 | 140 | | Buffer.BlockCopy(data, offset, _globalRestrictions, 0, (int)_globalRestrictionsPointer); |
| | 0 | 141 | | offset += (int)_globalRestrictionsPointer; |
| | 0 | 142 | | } |
| | | 143 | | |
| | | 144 | | private void WriteGlobal(byte[] data, ref int offset) |
| | 0 | 145 | | { |
| | 0 | 146 | | BitCoderBuffer.SetVarUInt32(data, ref offset, _globalRestrictionsPointer); |
| | 0 | 147 | | Buffer.BlockCopy(_globalRestrictions, 0, data, offset, (int)_globalRestrictionsPointer); |
| | 0 | 148 | | offset += (int)_globalRestrictionsPointer; |
| | 0 | 149 | | } |
| | | 150 | | } |