| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using Itinero.Network.Tiles; |
| | | 4 | | |
| | | 5 | | namespace Itinero.Network.Enumerators.Edges; |
| | | 6 | | |
| | | 7 | | public abstract class EdgeEnumerator<T> : |
| | | 8 | | IEdgeEnumerator<T> where T : IEdgeEnumerable |
| | | 9 | | { |
| | | 10 | | private readonly NetworkTileEnumerator _tileEnumerator; |
| | | 11 | | |
| | 449 | 12 | | internal EdgeEnumerator(T graph) |
| | 449 | 13 | | { |
| | 449 | 14 | | this.Network = graph; |
| | | 15 | | |
| | 449 | 16 | | _tileEnumerator = new NetworkTileEnumerator(); |
| | 449 | 17 | | } |
| | | 18 | | |
| | | 19 | | private (double longitude, double latitude, float? e) GetVertex(VertexId vertex) |
| | 550 | 20 | | { |
| | 550 | 21 | | var tile = _tileEnumerator.Tile; |
| | 550 | 22 | | if (tile == null || tile.TileId != vertex.TileId) |
| | 1 | 23 | | { |
| | 1 | 24 | | tile = this.Network.GetTileForRead(vertex.TileId); |
| | 1 | 25 | | } |
| | | 26 | | |
| | 550 | 27 | | if (tile == null) |
| | 0 | 28 | | { |
| | 0 | 29 | | throw new ArgumentOutOfRangeException(nameof(vertex), $"Vertex {vertex} not found!"); |
| | | 30 | | } |
| | | 31 | | |
| | 550 | 32 | | if (!tile.TryGetVertex(vertex, out var longitude, out var latitude, out var e)) |
| | 0 | 33 | | { |
| | 0 | 34 | | throw new ArgumentOutOfRangeException(nameof(vertex), $"Vertex {vertex} not found!"); |
| | | 35 | | } |
| | | 36 | | |
| | 550 | 37 | | return (longitude, latitude, e); |
| | 550 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Moves the enumerator to the first edge of the given vertex. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="vertex">The vertex.</param> |
| | | 44 | | /// <returns>True if the vertex exists.</returns> |
| | | 45 | | public bool MoveTo(VertexId vertex) |
| | 357 | 46 | | { |
| | 357 | 47 | | _tailLocation = null; |
| | 357 | 48 | | _headLocation = null; |
| | | 49 | | |
| | 357 | 50 | | if (_tileEnumerator.TileId == vertex.TileId) |
| | 249 | 51 | | { |
| | 249 | 52 | | return _tileEnumerator.MoveTo(vertex); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | // move to the tile. |
| | 108 | 56 | | var tile = this.Network.GetTileForRead(vertex.TileId); |
| | 108 | 57 | | if (tile == null) |
| | 1 | 58 | | { |
| | 1 | 59 | | return false; |
| | | 60 | | } |
| | | 61 | | |
| | 107 | 62 | | _tileEnumerator.MoveTo(tile); |
| | | 63 | | |
| | 107 | 64 | | return _tileEnumerator.MoveTo(vertex); |
| | 357 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Moves the enumerator to the given edge. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="edgeId">The edge id.</param> |
| | | 71 | | /// <param name="forward">The forward flag, when false the enumerator is in a state as it was enumerated to the edge |
| | | 72 | | public bool MoveTo(EdgeId edgeId, bool forward = true) |
| | 668 | 73 | | { |
| | 668 | 74 | | _tailLocation = null; |
| | 668 | 75 | | _headLocation = null; |
| | | 76 | | |
| | 668 | 77 | | if (_tileEnumerator.TileId == edgeId.TileId) |
| | 351 | 78 | | { |
| | 351 | 79 | | return _tileEnumerator.MoveTo(edgeId, forward); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | // move to the tile. |
| | 317 | 83 | | var tile = this.Network.GetTileForRead(edgeId.TileId); |
| | 317 | 84 | | if (tile == null) |
| | 0 | 85 | | { |
| | 0 | 86 | | return false; |
| | | 87 | | } |
| | | 88 | | |
| | 317 | 89 | | _tileEnumerator.MoveTo(tile); |
| | | 90 | | |
| | 317 | 91 | | return _tileEnumerator.MoveTo(edgeId, forward); |
| | 668 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Resets this enumerator. |
| | | 96 | | /// </summary> |
| | | 97 | | public void Reset() |
| | 0 | 98 | | { |
| | 0 | 99 | | _tailLocation = null; |
| | 0 | 100 | | _headLocation = null; |
| | | 101 | | |
| | 0 | 102 | | _tileEnumerator.Reset(); |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Moves this enumerator to the next edge. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <returns>True if there is data available.</returns> |
| | | 109 | | public bool MoveNext() |
| | 724 | 110 | | { |
| | 724 | 111 | | _tailLocation = null; |
| | 724 | 112 | | _headLocation = null; |
| | | 113 | | |
| | 724 | 114 | | return _tileEnumerator.MoveNext(); |
| | 724 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <inheritdoc/> |
| | 426 | 118 | | public T Network { get; } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Returns true if the edge is from -> to, false otherwise. |
| | | 122 | | /// </summary> |
| | 597 | 123 | | public bool Forward => _tileEnumerator.Forward; |
| | | 124 | | |
| | | 125 | | private (double longitude, double latitude, float? e)? _tailLocation; |
| | | 126 | | |
| | | 127 | | public (double longitude, double latitude, float? e) TailLocation |
| | | 128 | | { |
| | | 129 | | get |
| | 413 | 130 | | { |
| | 413 | 131 | | _tailLocation ??= this.GetVertex(this.Tail); |
| | | 132 | | |
| | 413 | 133 | | return _tailLocation.Value; |
| | 413 | 134 | | } |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Gets the source vertex. |
| | | 139 | | /// </summary> |
| | 499 | 140 | | public VertexId Tail => _tileEnumerator.Tail; |
| | | 141 | | |
| | | 142 | | private (double longitude, double latitude, float? e)? _headLocation; |
| | | 143 | | |
| | | 144 | | public (double longitude, double latitude, float? e) HeadLocation |
| | | 145 | | { |
| | | 146 | | get |
| | 413 | 147 | | { |
| | 413 | 148 | | _headLocation ??= this.GetVertex(this.Head); |
| | | 149 | | |
| | 413 | 150 | | return _headLocation.Value; |
| | 413 | 151 | | } |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Gets the target vertex. |
| | | 156 | | /// </summary> |
| | 585 | 157 | | public VertexId Head => _tileEnumerator.Head; |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Gets the edge id. |
| | | 161 | | /// </summary> |
| | 797 | 162 | | public EdgeId EdgeId => _tileEnumerator.EdgeId; |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Gets the shape. |
| | | 166 | | /// </summary> |
| | | 167 | | /// <returns>The shape.</returns> |
| | 420 | 168 | | public IEnumerable<(double longitude, double latitude, float? e)> Shape => _tileEnumerator.Shape; |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// Gets the attributes. |
| | | 172 | | /// </summary> |
| | | 173 | | /// <returns>The attributes.</returns> |
| | 328 | 174 | | public IEnumerable<(string key, string value)> Attributes => _tileEnumerator.Attributes; |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Gets the edge profile id. |
| | | 178 | | /// </summary> |
| | 35 | 179 | | public uint? EdgeTypeId => _tileEnumerator.EdgeTypeId; |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Gets the length in centimeters, if any. |
| | | 183 | | /// </summary> |
| | 570 | 184 | | public uint? Length => _tileEnumerator.Length; |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Gets the head index. |
| | | 188 | | /// </summary> |
| | 154 | 189 | | public byte? HeadOrder => _tileEnumerator.HeadOrder; |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// Gets the tail index. |
| | | 193 | | /// </summary> |
| | 31 | 194 | | public byte? TailOrder => _tileEnumerator.TailOrder; |
| | | 195 | | |
| | | 196 | | /// <summary> |
| | | 197 | | /// Gets the turn cost at the tail turn (source -> [tail -> head]). |
| | | 198 | | /// </summary> |
| | | 199 | | /// <param name="sourceOrder">The order of the source edge.</param> |
| | | 200 | | /// <returns>The turn costs if any.</returns> |
| | | 201 | | public IEnumerable<(uint turnCostType, IEnumerable<(string key, string value)> attributes, uint cost, IEnumerable<Ed |
| | | 202 | | byte sourceOrder) |
| | 13 | 203 | | { |
| | 13 | 204 | | return _tileEnumerator.GetTurnCostToTail(sourceOrder); |
| | 13 | 205 | | } |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Gets the turn cost at the tail turn ([head -> tail] -> target). |
| | | 209 | | /// </summary> |
| | | 210 | | /// <param name="targetOrder">The order of the target edge.</param> |
| | | 211 | | /// <returns>The turn costs if any.</returns> |
| | | 212 | | public IEnumerable<(uint turnCostType, IEnumerable<(string key, string value)> attributes, uint cost, IEnumerable<Ed |
| | | 213 | | byte targetOrder) |
| | 0 | 214 | | { |
| | 0 | 215 | | return _tileEnumerator.GetTurnCostFromTail(targetOrder); |
| | 0 | 216 | | } |
| | | 217 | | |
| | | 218 | | /// <summary> |
| | | 219 | | /// Gets the turn cost at the tail turn (source -> [head -> tail]). |
| | | 220 | | /// </summary> |
| | | 221 | | /// <param name="sourceOrder">The order of the source edge.</param> |
| | | 222 | | /// <returns>The turn costs if any.</returns> |
| | | 223 | | public IEnumerable<(uint turnCostType, IEnumerable<(string key, string value)> attributes, uint cost, IEnumerable<Ed |
| | | 224 | | byte sourceOrder) |
| | 0 | 225 | | { |
| | 0 | 226 | | return _tileEnumerator.GetTurnCostToHead(sourceOrder); |
| | 0 | 227 | | } |
| | | 228 | | |
| | | 229 | | /// <summary> |
| | | 230 | | /// Gets the turn cost at the tail turn ([tail -> head] -> target). |
| | | 231 | | /// </summary> |
| | | 232 | | /// <param name="targetOrder">The order of the target edge.</param> |
| | | 233 | | /// <returns>The turn costs if any.</returns> |
| | | 234 | | public IEnumerable<(uint turnCostType, IEnumerable<(string key, string value)> attributes, uint cost, IEnumerable<Ed |
| | | 235 | | byte targetOrder) |
| | 0 | 236 | | { |
| | 0 | 237 | | return _tileEnumerator.GetTurnCostFromHead(targetOrder); |
| | 0 | 238 | | } |
| | | 239 | | } |