| | 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 | |
|
| 388 | 12 | | internal EdgeEnumerator(T graph) |
| 388 | 13 | | { |
| 388 | 14 | | this.Network = graph; |
| | 15 | |
|
| 388 | 16 | | _tileEnumerator = new NetworkTileEnumerator(); |
| 388 | 17 | | } |
| | 18 | |
|
| | 19 | | private (double longitude, double latitude, float? e) GetVertex(VertexId vertex) |
| 522 | 20 | | { |
| 522 | 21 | | var tile = _tileEnumerator.Tile; |
| 522 | 22 | | if (tile == null || tile.TileId != vertex.TileId) |
| 1 | 23 | | { |
| 1 | 24 | | tile = this.Network.GetTileForRead(vertex.TileId); |
| 1 | 25 | | } |
| | 26 | |
|
| 522 | 27 | | if (tile == null) |
| 0 | 28 | | { |
| 0 | 29 | | throw new ArgumentOutOfRangeException(nameof(vertex), $"Vertex {vertex} not found!"); |
| | 30 | | } |
| | 31 | |
|
| 522 | 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 | |
|
| 522 | 37 | | return (longitude, latitude, e); |
| 522 | 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) |
| 321 | 46 | | { |
| 321 | 47 | | _tailLocation = null; |
| 321 | 48 | | _headLocation = null; |
| | 49 | |
|
| 321 | 50 | | if (_tileEnumerator.TileId == vertex.TileId) |
| 217 | 51 | | { |
| 217 | 52 | | return _tileEnumerator.MoveTo(vertex); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | // move to the tile. |
| 104 | 56 | | var tile = this.Network.GetTileForRead(vertex.TileId); |
| 104 | 57 | | if (tile == null) |
| 1 | 58 | | { |
| 1 | 59 | | return false; |
| | 60 | | } |
| | 61 | |
|
| 103 | 62 | | _tileEnumerator.MoveTo(tile); |
| | 63 | |
|
| 103 | 64 | | return _tileEnumerator.MoveTo(vertex); |
| 321 | 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) |
| 594 | 73 | | { |
| 594 | 74 | | _tailLocation = null; |
| 594 | 75 | | _headLocation = null; |
| | 76 | |
|
| 594 | 77 | | if (_tileEnumerator.TileId == edgeId.TileId) |
| 334 | 78 | | { |
| 334 | 79 | | return _tileEnumerator.MoveTo(edgeId, forward); |
| | 80 | | } |
| | 81 | |
|
| | 82 | | // move to the tile. |
| 260 | 83 | | var tile = this.Network.GetTileForRead(edgeId.TileId); |
| 260 | 84 | | if (tile == null) |
| 0 | 85 | | { |
| 0 | 86 | | return false; |
| | 87 | | } |
| | 88 | |
|
| 260 | 89 | | _tileEnumerator.MoveTo(tile); |
| | 90 | |
|
| 260 | 91 | | return _tileEnumerator.MoveTo(edgeId, forward); |
| 594 | 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() |
| 643 | 110 | | { |
| 643 | 111 | | _tailLocation = null; |
| 643 | 112 | | _headLocation = null; |
| | 113 | |
|
| 643 | 114 | | return _tileEnumerator.MoveNext(); |
| 643 | 115 | | } |
| | 116 | |
|
| | 117 | | /// <inheritdoc/> |
| 365 | 118 | | public T Network { get; } |
| | 119 | |
|
| | 120 | | /// <summary> |
| | 121 | | /// Returns true if the edge is from -> to, false otherwise. |
| | 122 | | /// </summary> |
| 474 | 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 |
| 342 | 130 | | { |
| 342 | 131 | | _tailLocation ??= this.GetVertex(this.Tail); |
| | 132 | |
|
| 342 | 133 | | return _tailLocation.Value; |
| 342 | 134 | | } |
| | 135 | | } |
| | 136 | |
|
| | 137 | | /// <summary> |
| | 138 | | /// Gets the source vertex. |
| | 139 | | /// </summary> |
| 507 | 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 |
| 342 | 147 | | { |
| 342 | 148 | | _headLocation ??= this.GetVertex(this.Head); |
| | 149 | |
|
| 342 | 150 | | return _headLocation.Value; |
| 342 | 151 | | } |
| | 152 | | } |
| | 153 | |
|
| | 154 | | /// <summary> |
| | 155 | | /// Gets the target vertex. |
| | 156 | | /// </summary> |
| 526 | 157 | | public VertexId Head => _tileEnumerator.Head; |
| | 158 | |
|
| | 159 | | /// <summary> |
| | 160 | | /// Gets the edge id. |
| | 161 | | /// </summary> |
| 684 | 162 | | public EdgeId EdgeId => _tileEnumerator.EdgeId; |
| | 163 | |
|
| | 164 | | /// <summary> |
| | 165 | | /// Gets the shape. |
| | 166 | | /// </summary> |
| | 167 | | /// <returns>The shape.</returns> |
| 349 | 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> |
| 257 | 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> |
| 428 | 184 | | public uint? Length => _tileEnumerator.Length; |
| | 185 | |
|
| | 186 | | /// <summary> |
| | 187 | | /// Gets the head index. |
| | 188 | | /// </summary> |
| 123 | 189 | | public byte? HeadOrder => _tileEnumerator.HeadOrder; |
| | 190 | |
|
| | 191 | | /// <summary> |
| | 192 | | /// Gets the tail index. |
| | 193 | | /// </summary> |
| 30 | 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 | | } |