| | 1 | | using System; |
| | 2 | |
|
| | 3 | | namespace Itinero.Network; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Represents a edge id composed of a tile id and a local id. |
| | 7 | | /// </summary> |
| | 8 | | public readonly struct EdgeId : IEquatable<EdgeId> |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// The minimum id for edges crossing tile boundaries. |
| | 12 | | /// </summary> |
| | 13 | | internal const uint MinCrossId = MaxLocalId + 1; |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// The maximum number of internal edges in one tile. |
| | 17 | | /// </summary> |
| | 18 | | internal const uint MaxLocalId = (uint.MaxValue / 2) - 1; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Creates a new edge id. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="tileId">The tile id.</param> |
| | 24 | | /// <param name="localId">The local id.</param> |
| | 25 | | public EdgeId(uint tileId, uint localId) |
| 987 | 26 | | { |
| 987 | 27 | | this.TileId = tileId; |
| 987 | 28 | | this.LocalId = localId; |
| 987 | 29 | | } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Creates a new cross edge id. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="tileId">The tile id.</param> |
| | 35 | | /// <param name="localId">The local id.</param> |
| | 36 | | /// <returns>An edge id represented as a id crossing tiles.</returns> |
| | 37 | | public static EdgeId CrossEdgeId(uint tileId, uint localId) |
| 11 | 38 | | { |
| 11 | 39 | | return new EdgeId(tileId, EdgeId.MinCrossId + localId); |
| 11 | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Gets or sets the tile id. |
| | 44 | | /// </summary> |
| 3254 | 45 | | public uint TileId { get; } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Gets or sets the local id. |
| | 49 | | /// </summary> |
| 3436 | 50 | | public uint LocalId { get; } |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Returns an empty edge id. |
| | 54 | | /// </summary> |
| 1 | 55 | | public static readonly EdgeId Empty = new(uint.MaxValue, uint.MaxValue); |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Returns true if this edge id is empty. |
| | 59 | | /// </summary> |
| | 60 | | /// <returns></returns> |
| | 61 | | public bool IsEmpty() |
| 0 | 62 | | { |
| 0 | 63 | | return this.TileId == uint.MaxValue; |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Returns a human readable description. |
| | 68 | | /// </summary> |
| | 69 | | /// <returns></returns> |
| | 70 | | public override string ToString() |
| 0 | 71 | | { |
| 0 | 72 | | if (this.LocalId >= MinCrossId) |
| 0 | 73 | | { |
| 0 | 74 | | return $"{this.LocalId} (X {this.LocalId - MinCrossId}) @ {this.TileId} "; |
| | 75 | | } |
| | 76 | |
|
| 0 | 77 | | return $"{this.LocalId} @ {this.TileId}"; |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Returns true if the two edges represent the same id. |
| | 82 | | /// </summary> |
| | 83 | | /// <returns></returns> |
| | 84 | | public static bool operator ==(EdgeId vertex1, EdgeId vertex2) |
| 412 | 85 | | { |
| 412 | 86 | | return vertex1.LocalId == vertex2.LocalId && |
| 412 | 87 | | vertex1.TileId == vertex2.TileId; |
| 412 | 88 | | } |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Returns true if the two edges don't represent the same id. |
| | 92 | | /// </summary> |
| | 93 | | /// <returns></returns> |
| | 94 | | public static bool operator !=(EdgeId vertex1, EdgeId vertex2) |
| 74 | 95 | | { |
| 74 | 96 | | return !(vertex1 == vertex2); |
| 74 | 97 | | } |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// Returns true if the given edge represent the same id. |
| | 101 | | /// </summary> |
| | 102 | | /// <param name="other"></param> |
| | 103 | | /// <returns></returns> |
| | 104 | | public bool Equals(EdgeId other) |
| 405 | 105 | | { |
| 405 | 106 | | return this.LocalId == other.LocalId && this.TileId == other.TileId; |
| 405 | 107 | | } |
| | 108 | |
|
| | 109 | | /// <inheritdoc/> |
| | 110 | | public override bool Equals(object obj) |
| 0 | 111 | | { |
| 0 | 112 | | return obj is EdgeId other && this.Equals(other); |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | /// <inheritdoc/> |
| | 116 | | public override int GetHashCode() |
| 467 | 117 | | { |
| | 118 | | unchecked |
| 467 | 119 | | { |
| 467 | 120 | | return ((int)this.TileId * 397) ^ (int)this.LocalId; |
| | 121 | | } |
| 467 | 122 | | } |
| | 123 | |
|
| | 124 | | /// <summary> |
| | 125 | | /// Encodes the info in this edge into one 64bit unsigned integer. |
| | 126 | | /// </summary> |
| | 127 | | /// <returns>An encoded version of this edge.</returns> |
| | 128 | | internal ulong Encode() |
| 0 | 129 | | { |
| 0 | 130 | | return ((ulong)this.TileId << 32) + this.LocalId; |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | /// <summary> |
| | 134 | | /// Decodes the given encoded edge id. |
| | 135 | | /// </summary> |
| | 136 | | /// <param name="encoded">The encoded version an edge.</param> |
| | 137 | | /// <returns>The decoded version of edge.</returns> |
| | 138 | | internal static EdgeId Decode(ulong encoded) |
| 0 | 139 | | { |
| 0 | 140 | | var tileId = (uint)(encoded >> 32); |
| 0 | 141 | | var localId = (uint)(encoded - ((ulong)tileId << 32)); |
| | 142 | |
|
| 0 | 143 | | return new EdgeId(tileId, localId); |
| 0 | 144 | | } |
| | 145 | | } |