| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace Itinero.Network.TurnCosts; |
| | | 4 | | |
| | | 5 | | internal static class OrderCoder |
| | | 6 | | { |
| | | 7 | | // TODO: widen the tile format so a vertex can carry more than 4 bits of turn cost order. |
| | | 8 | | // Tail and head orders are packed into one byte here, capping a vertex at 15 turn cost |
| | | 9 | | // edges. Real data exceeds that - the AP-6 toll plaza (OSM node 2151903449) ends 26 ways |
| | | 10 | | // on one node - and callers currently drop the restriction, so the turn is left allowed. |
| | | 11 | | // Fixing it properly means a wider encoding and a tile format version bump. |
| | | 12 | | internal const int MaxOrderHeadTail = 14; |
| | | 13 | | |
| | | 14 | | public static void SetTailHeadOrder(this byte[] data, long i, byte? tail, byte? head) |
| | 75443 | 15 | | { |
| | 75443 | 16 | | if (tail.HasValue && tail.Value > MaxOrderHeadTail) |
| | 0 | 17 | | { |
| | 0 | 18 | | throw new ArgumentOutOfRangeException(nameof(tail), |
| | 0 | 19 | | $"Maximum order exceeded."); |
| | | 20 | | } |
| | | 21 | | |
| | 75443 | 22 | | if (head.HasValue && head.Value > MaxOrderHeadTail) |
| | 0 | 23 | | { |
| | 0 | 24 | | throw new ArgumentOutOfRangeException(nameof(head), |
| | 0 | 25 | | $"Maximum order exceeded."); |
| | | 26 | | } |
| | | 27 | | |
| | 75443 | 28 | | var d = 0; |
| | 75443 | 29 | | if (tail.HasValue) |
| | 5558 | 30 | | { |
| | 5558 | 31 | | d = tail.Value + 1; |
| | 5558 | 32 | | } |
| | | 33 | | |
| | 75443 | 34 | | if (head.HasValue) |
| | 5140 | 35 | | { |
| | 5140 | 36 | | d += (head.Value + 1) * 16; |
| | 5140 | 37 | | } |
| | | 38 | | |
| | 75443 | 39 | | data[(int)i] = (byte)d; |
| | 75443 | 40 | | } |
| | | 41 | | |
| | | 42 | | public static void GetTailHeadOrder(this byte[] data, long i, ref byte? tail, ref byte? head) |
| | 6578272 | 43 | | { |
| | 6578272 | 44 | | tail = null; |
| | 6578272 | 45 | | head = null; |
| | | 46 | | |
| | 6578272 | 47 | | var d = data[(int)i]; |
| | 6578272 | 48 | | if (d == 0) |
| | 5592605 | 49 | | { |
| | 5592605 | 50 | | return; |
| | | 51 | | } |
| | | 52 | | |
| | 985667 | 53 | | tail = null; |
| | 985667 | 54 | | var t = d % 16; |
| | 985667 | 55 | | if (t > 0) |
| | 531828 | 56 | | { |
| | 531828 | 57 | | tail = (byte)(t - 1); |
| | 531828 | 58 | | } |
| | | 59 | | |
| | 985667 | 60 | | var h = d / 16; |
| | 985667 | 61 | | if (h > 0) |
| | 529857 | 62 | | { |
| | 529857 | 63 | | head = (byte)(h - 1); |
| | 529857 | 64 | | } |
| | 6578272 | 65 | | } |
| | | 66 | | } |