| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Threading; |
| | | 4 | | |
| | | 5 | | namespace Itinero.Routing.Isochrones; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Marching squares over a cost grid: turns "cost at each grid corner" into closed rings at a |
| | | 9 | | /// given cost. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// Segments are emitted with the reachable side on the left, so an outer boundary comes out |
| | | 13 | | /// counter-clockwise and a hole clockwise — which is both how they are told apart here and the |
| | | 14 | | /// winding GeoJSON asks for. |
| | | 15 | | /// </remarks> |
| | | 16 | | internal static class ContourTracer |
| | | 17 | | { |
| | | 18 | | // Which cell side a crossing sits on. Used to give every crossing an exact integer identity, |
| | | 19 | | // so segments are joined by lattice position rather than by comparing interpolated doubles. |
| | | 20 | | private const int Bottom = 0; |
| | | 21 | | private const int Right = 1; |
| | | 22 | | private const int Top = 2; |
| | | 23 | | private const int Left = 3; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Traces the band at <paramref name="band"/> out of a corner-cost grid. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="cost">Cost per corner, row-major, <see cref="double.MaxValue"/> if unreached.</param> |
| | | 29 | | /// <param name="width">Corners per row.</param> |
| | | 30 | | /// <param name="height">Rows of corners.</param> |
| | | 31 | | public static List<IsochronePolygon> Trace(double[] cost, int width, int height, double band, |
| | | 32 | | double minLon, double minLat, double lonPerCell, double latPerCell, double denoise, |
| | | 33 | | double squareMetersPerSquareDegree = 0, double fillHolesBelow = 0, |
| | | 34 | | CancellationToken cancellationToken = default) |
| | 24 | 35 | | { |
| | 24 | 36 | | var segments = new List<(long from, long to)>(); |
| | 24 | 37 | | var points = new Dictionary<long, (double longitude, double latitude)>(); |
| | | 38 | | |
| | 2282 | 39 | | for (var y = 0; y < height - 1; y++) |
| | 1118 | 40 | | { |
| | | 41 | | // Per row: a band over a wide area is millions of cells, once per band. |
| | 1118 | 42 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 90118 | 43 | | for (var x = 0; x < width - 1; x++) |
| | 43942 | 44 | | { |
| | 43942 | 45 | | var c0 = cost[y * width + x]; |
| | 43942 | 46 | | var c1 = cost[y * width + x + 1]; |
| | 43942 | 47 | | var c2 = cost[(y + 1) * width + x + 1]; |
| | 43942 | 48 | | var c3 = cost[(y + 1) * width + x]; |
| | | 49 | | |
| | 43942 | 50 | | var mask = 0; |
| | 67866 | 51 | | if (c0 <= band) mask |= 1; |
| | 67866 | 52 | | if (c1 <= band) mask |= 2; |
| | 67866 | 53 | | if (c2 <= band) mask |= 4; |
| | 67866 | 54 | | if (c3 <= band) mask |= 8; |
| | 83328 | 55 | | if (mask == 0 || mask == 15) continue; |
| | | 56 | | |
| | 4556 | 57 | | switch (mask) |
| | | 58 | | { |
| | 270 | 59 | | case 1: Add(Bottom, Left); break; |
| | 274 | 60 | | case 2: Add(Right, Bottom); break; |
| | 1074 | 61 | | case 3: Add(Right, Left); break; |
| | 274 | 62 | | case 4: Add(Top, Right); break; |
| | 1882 | 63 | | case 6: Add(Top, Bottom); break; |
| | 524 | 64 | | case 7: Add(Top, Left); break; |
| | 270 | 65 | | case 8: Add(Left, Top); break; |
| | 1890 | 66 | | case 9: Add(Bottom, Top); break; |
| | 520 | 67 | | case 11: Add(Right, Top); break; |
| | 1074 | 68 | | case 12: Add(Left, Right); break; |
| | 520 | 69 | | case 13: Add(Bottom, Right); break; |
| | 524 | 70 | | case 14: Add(Left, Bottom); break; |
| | | 71 | | |
| | | 72 | | // Saddles. The two diagonal corners may be one region pinched through the |
| | | 73 | | // cell or two separate ones, and the grid cannot tell; the cell's mean |
| | | 74 | | // decides, which is the usual convention and keeps rings closed either way. |
| | | 75 | | case 5: |
| | 12 | 76 | | if (Centre() <= band) { Add(Bottom, Right); Add(Top, Left); } |
| | 8 | 77 | | else { Add(Bottom, Left); Add(Top, Right); } |
| | 4 | 78 | | break; |
| | | 79 | | case 10: |
| | 12 | 80 | | if (Centre() <= band) { Add(Left, Bottom); Add(Right, Top); } |
| | 8 | 81 | | else { Add(Right, Bottom); Add(Left, Top); } |
| | 4 | 82 | | break; |
| | | 83 | | } |
| | | 84 | | |
| | 4556 | 85 | | continue; |
| | | 86 | | |
| | | 87 | | double Centre() |
| | 8 | 88 | | { |
| | | 89 | | // An unreached corner has no finite cost to average, so the mean is taken |
| | | 90 | | // over the reached ones only; all four unreached cannot reach a saddle. |
| | 8 | 91 | | var sum = 0d; |
| | 8 | 92 | | var n = 0; |
| | 88 | 93 | | foreach (var c in stackalloc[] { c0, c1, c2, c3 }) |
| | 32 | 94 | | { |
| | 40 | 95 | | if (c >= double.MaxValue) continue; |
| | 24 | 96 | | sum += c; |
| | 24 | 97 | | n++; |
| | 24 | 98 | | } |
| | | 99 | | |
| | 8 | 100 | | return n == 0 ? double.MaxValue : sum / n; |
| | 8 | 101 | | } |
| | | 102 | | |
| | | 103 | | void Add(int fromSide, int toSide) |
| | 4564 | 104 | | { |
| | 4564 | 105 | | var from = Crossing(fromSide); |
| | 4564 | 106 | | var to = Crossing(toSide); |
| | 9128 | 107 | | if (from != to) segments.Add((from, to)); |
| | 4564 | 108 | | } |
| | | 109 | | |
| | | 110 | | long Crossing(int side) |
| | 9128 | 111 | | { |
| | | 112 | | // Horizontal sides are keyed by the corner left of them, vertical sides by |
| | | 113 | | // the corner below, so the two cells sharing a side agree on its identity. |
| | | 114 | | long key; |
| | | 115 | | double a, b; |
| | | 116 | | int ax, ay, bx, by; |
| | 9128 | 117 | | switch (side) |
| | | 118 | | { |
| | 21504 | 119 | | case Bottom: key = Key(0, x, y); a = c0; b = c1; ax = x; ay = y; bx = x + 1; by = y; break; |
| | 15008 | 120 | | case Right: key = Key(1, x + 1, y); a = c1; b = c2; ax = x + 1; ay = y; bx = x + 1; by = y + 1; |
| | 21504 | 121 | | case Top: key = Key(0, x, y + 1); a = c3; b = c2; ax = x; ay = y + 1; bx = x + 1; by = y + 1; br |
| | 15008 | 122 | | default: key = Key(1, x, y); a = c0; b = c3; ax = x; ay = y; bx = x; by = y + 1; break; |
| | | 123 | | } |
| | | 124 | | |
| | 9128 | 125 | | if (!points.ContainsKey(key)) |
| | 4564 | 126 | | { |
| | 4564 | 127 | | var t = Fraction(a, b, band); |
| | 4564 | 128 | | points[key] = ( |
| | 4564 | 129 | | minLon + (ax + (bx - ax) * t) * lonPerCell, |
| | 4564 | 130 | | minLat + (ay + (by - ay) * t) * latPerCell); |
| | 4564 | 131 | | } |
| | | 132 | | |
| | 9128 | 133 | | return key; |
| | 9128 | 134 | | } |
| | | 135 | | } |
| | 1117 | 136 | | } |
| | | 137 | | |
| | 23 | 138 | | return Assemble(segments, points, denoise, squareMetersPerSquareDegree, fillHolesBelow); |
| | | 139 | | |
| | 9128 | 140 | | long Key(int kind, int x, int y) => ((long)kind << 60) | ((long)x << 30) | (uint)y; |
| | 23 | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Where between two corners the band falls. Pinned to the reached corner when the other was |
| | | 145 | | /// never reached: claiming half a cell of ground the search never saw would be an invention. |
| | | 146 | | /// </summary> |
| | | 147 | | private static double Fraction(double a, double b, double band) |
| | 4564 | 148 | | { |
| | 5871 | 149 | | if (a >= double.MaxValue) return b >= double.MaxValue ? 0.5 : 1.0; |
| | 4566 | 150 | | if (b >= double.MaxValue) return 0.0; |
| | | 151 | | |
| | 1948 | 152 | | var span = b - a; |
| | 1948 | 153 | | if (Math.Abs(span) < double.Epsilon) return 0.5; |
| | | 154 | | |
| | 1948 | 155 | | var t = (band - a) / span; |
| | 1948 | 156 | | return t < 0 ? 0 : t > 1 ? 1 : t; |
| | 4564 | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Links segments end to end into rings, then nests holes under the ring containing them. |
| | | 161 | | /// </summary> |
| | | 162 | | private static List<IsochronePolygon> Assemble(List<(long from, long to)> segments, |
| | | 163 | | Dictionary<long, (double longitude, double latitude)> points, double denoise, |
| | | 164 | | double squareMetersPerSquareDegree, double fillHolesBelow) |
| | 23 | 165 | | { |
| | 23 | 166 | | var next = new Dictionary<long, long>(segments.Count); |
| | 13761 | 167 | | foreach (var (from, to) in segments) next[from] = to; |
| | | 168 | | |
| | 23 | 169 | | var rings = new List<IsochroneRing>(); |
| | 23 | 170 | | var used = new HashSet<long>(); |
| | 9197 | 171 | | foreach (var (start, _) in segments) |
| | 4564 | 172 | | { |
| | 8957 | 173 | | if (!used.Add(start)) continue; |
| | | 174 | | |
| | 171 | 175 | | var coordinates = new List<(double longitude, double latitude)> { points[start] }; |
| | 171 | 176 | | var at = start; |
| | 4564 | 177 | | while (next.TryGetValue(at, out var to) && to != start) |
| | 4393 | 178 | | { |
| | 4393 | 179 | | if (!used.Add(to)) break; |
| | 4393 | 180 | | coordinates.Add(points[to]); |
| | 4393 | 181 | | at = to; |
| | 4393 | 182 | | } |
| | | 183 | | |
| | | 184 | | // Two points cannot enclose anything; a non-closing chain means the grid edge was |
| | | 185 | | // reached, which only happens if the bbox was too tight. |
| | 171 | 186 | | if (coordinates.Count < 3) continue; |
| | 171 | 187 | | rings.Add(new IsochroneRing(coordinates, SignedArea(coordinates))); |
| | 171 | 188 | | } |
| | | 189 | | |
| | 23 | 190 | | if (rings.Count == 0) return []; |
| | | 191 | | |
| | 23 | 192 | | var largest = 0d; |
| | 582 | 193 | | foreach (var ring in rings) largest = Math.Max(largest, Math.Abs(ring.SignedArea)); |
| | | 194 | | |
| | 23 | 195 | | var keep = new List<IsochroneRing>(); |
| | 411 | 196 | | foreach (var ring in rings) |
| | 171 | 197 | | { |
| | 268 | 198 | | if (Math.Abs(ring.SignedArea) >= largest * denoise) keep.Add(ring); |
| | 171 | 199 | | } |
| | | 200 | | |
| | 23 | 201 | | var polygons = new List<IsochronePolygon>(); |
| | 23 | 202 | | var outers = new List<IsochroneRing>(); |
| | 23 | 203 | | var holes = new List<IsochroneRing>(); |
| | 263 | 204 | | foreach (var ring in keep) |
| | 97 | 205 | | { |
| | 194 | 206 | | if (ring.SignedArea > 0) outers.Add(ring); else holes.Add(ring); |
| | 97 | 207 | | } |
| | | 208 | | |
| | 23 | 209 | | var byOuter = new Dictionary<IsochroneRing, List<IsochroneRing>>(); |
| | 138 | 210 | | foreach (var outer in outers) byOuter[outer] = []; |
| | | 211 | | |
| | 217 | 212 | | foreach (var hole in holes) |
| | 74 | 213 | | { |
| | | 214 | | // Half the shoelace sum is the area; in square degrees until scaled. |
| | 74 | 215 | | if (fillHolesBelow > 0 && squareMetersPerSquareDegree > 0 && |
| | 74 | 216 | | Math.Abs(hole.SignedArea) / 2.0 * squareMetersPerSquareDegree < fillHolesBelow) |
| | 0 | 217 | | { |
| | 0 | 218 | | continue; |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | // Smallest container, so a hole inside an island inside a hole lands on the right |
| | | 222 | | // ring rather than the outermost one. |
| | 74 | 223 | | IsochroneRing? owner = null; |
| | 370 | 224 | | foreach (var outer in outers) |
| | 74 | 225 | | { |
| | 74 | 226 | | if (!Contains(outer, hole.Coordinates[0])) continue; |
| | 74 | 227 | | if (owner == null || Math.Abs(outer.SignedArea) < Math.Abs(owner.SignedArea)) |
| | 74 | 228 | | owner = outer; |
| | 74 | 229 | | } |
| | | 230 | | |
| | 148 | 231 | | if (owner != null) byOuter[owner].Add(hole); |
| | 74 | 232 | | } |
| | | 233 | | |
| | 138 | 234 | | foreach (var outer in outers) polygons.Add(new IsochronePolygon(outer, byOuter[outer])); |
| | | 235 | | |
| | 23 | 236 | | return polygons; |
| | 23 | 237 | | } |
| | | 238 | | |
| | | 239 | | private static double SignedArea(List<(double longitude, double latitude)> ring) |
| | 171 | 240 | | { |
| | 171 | 241 | | var sum = 0d; |
| | 9470 | 242 | | for (var i = 0; i < ring.Count; i++) |
| | 4564 | 243 | | { |
| | 4564 | 244 | | var (x1, y1) = ring[i]; |
| | 4564 | 245 | | var (x2, y2) = ring[(i + 1) % ring.Count]; |
| | 4564 | 246 | | sum += x1 * y2 - x2 * y1; |
| | 4564 | 247 | | } |
| | | 248 | | |
| | 171 | 249 | | return sum; |
| | 171 | 250 | | } |
| | | 251 | | |
| | | 252 | | private static bool Contains(IsochroneRing ring, (double longitude, double latitude) point) |
| | 74 | 253 | | { |
| | 74 | 254 | | var inside = false; |
| | 74 | 255 | | var coordinates = ring.Coordinates; |
| | 28638 | 256 | | for (int i = 0, j = coordinates.Count - 1; i < coordinates.Count; j = i++) |
| | 14208 | 257 | | { |
| | 14208 | 258 | | var (xi, yi) = coordinates[i]; |
| | 14208 | 259 | | var (xj, yj) = coordinates[j]; |
| | 14208 | 260 | | if (yi > point.latitude != yj > point.latitude && |
| | 14208 | 261 | | point.longitude < (xj - xi) * (point.latitude - yi) / (yj - yi) + xi) |
| | 94 | 262 | | { |
| | 94 | 263 | | inside = !inside; |
| | 94 | 264 | | } |
| | 14208 | 265 | | } |
| | | 266 | | |
| | 74 | 267 | | return inside; |
| | 74 | 268 | | } |
| | | 269 | | } |