Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / gtfs_ingest / src / bundle_part2_s1.rs
bundle_part2_s1.rs175 lines · 8.2 KiBRaw
| 1 | /// How far to the side of a corridor's reference `p` lies, in metres, signed positive to the |
| 2 | /// right of the reference's direction of travel. |
| 3 | /// |
| 4 | /// The same nearest-segment walk [`distance_along`] does, because [`project`] returns an |
| 5 | /// unsigned distance and no side. Against the nearest segment's unit tangent `(ux, uy)` in an |
| 6 | /// (east, north) frame — the frame [`directed`] already produces — the side of the offset `d` |
| 7 | /// is `d.east * uy - d.north * ux`. Positive is the right-hand side because `tess::stroke` |
| 8 | /// emits `(0, +1)` as the normal of an eastward segment and `line.vert` shifts along it, so |
| 9 | /// ascending offset is ascending ordinal: `lane_offset_px` gives ordinal zero the most |
| 10 | /// negative offset, which is the left of the reference's travel. |
| 11 | /// |
| 12 | /// Only the reference's own stretch of the corridor, `extent`, is searched. Past the mouth the |
| 13 | /// reference has left the corridor too, and the segment nearest an approaching member is then |
| 14 | /// as likely to be the reference's departure as the corridor — which reads a member leaving |
| 15 | /// west as having no side at all. Clamped, the approach is measured against the reference's |
| 16 | /// tangent at the mouth, which is the axis the lanes are laid out across. |
| 17 | fn signed_offset(corridor: &Corridor, extent: (f64, f64), p: (i32, i32)) -> f64 { |
| 18 | let (points, cum) = (&corridor.points, &corridor.cum); |
| 19 | let mut best = (f64::INFINITY, 0.0f64); |
| 20 | for at in 0..points.len().saturating_sub(1) { |
| 21 | if cum[at + 1] < extent.0 || cum[at] > extent.1 { |
| 22 | continue; |
| 23 | } |
| 24 | let (a, b) = (points[at], points[at + 1]); |
| 25 | let (_, distance) = project(p, a, b, 0.0); |
| 26 | if distance >= best.0 { |
| 27 | continue; |
| 28 | } |
| 29 | let cos_lat = ((a.0 as f64 + b.0 as f64) * 0.5 * 1e-7).to_radians().cos(); |
| 30 | let (east, north) = ((b.1 - a.1) as f64 * cos_lat, (b.0 - a.0) as f64); |
| 31 | let length = (east * east + north * north).sqrt(); |
| 32 | if length <= 0.0 { |
| 33 | continue; |
| 34 | } |
| 35 | let (ux, uy) = (east / length, north / length); |
| 36 | let d_east = (p.1 - a.1) as f64 * 1e-7 * 111_320.0 * cos_lat; |
| 37 | let d_north = (p.0 - a.0) as f64 * 1e-7 * 111_320.0; |
| 38 | best = (distance, d_east * uy - d_north * ux); |
| 39 | } |
| 40 | best.1 |
| 41 | } |
| 42 | |
| 43 | /// One probe point along a candidate, with the unit direction of travel there. |
| 44 | struct Sample { |
| 45 | route: u32, |
| 46 | point: (i32, i32), |
| 47 | ux: f64, |
| 48 | uy: f64, |
| 49 | } |
| 50 | |
| 51 | /// One walked sample of a line: its point and the unit direction of travel there. |
| 52 | /// |
| 53 | /// Exposed so a caller that runs several of [`Covered`]'s gates over one line can walk it |
| 54 | /// once and hand the samples to each, rather than every gate re-walking the same points. |
| 55 | pub type Walked = ((i32, i32), f64, f64); |
| 56 | |
| 57 | /// Walk a polyline at [`SAMPLE_M`], returning each sample with its unit direction of travel. |
| 58 | pub fn walk(points: &[(i32, i32)]) -> Vec<Walked> { |
| 59 | let walked = resample(points, SAMPLE_M); |
| 60 | if walked.len() < 2 { |
| 61 | return Vec::new(); |
| 62 | } |
| 63 | directed(&walked) |
| 64 | } |
| 65 | |
| 66 | /// Each point of `line` with the unit direction of travel there. One entry per input point: |
| 67 | /// a degenerate segment carries the previous direction forward rather than dropping the |
| 68 | /// point, so the result stays index-aligned with its input. |
| 69 | fn directed(line: &[(i32, i32)]) -> Vec<((i32, i32), f64, f64)> { |
| 70 | let mut out = Vec::with_capacity(line.len()); |
| 71 | let (mut ux, mut uy) = (1.0f64, 0.0f64); |
| 72 | for i in 0..line.len() { |
| 73 | let (a, b) = if i + 1 < line.len() { (line[i], line[i + 1]) } else { (line[i.max(1) - 1], line[i]) }; |
| 74 | let cos_lat = ((a.0 as f64 + b.0 as f64) * 0.5 * 1e-7).to_radians().cos(); |
| 75 | let dy = (b.0 - a.0) as f64; |
| 76 | let dx = (b.1 - a.1) as f64 * cos_lat; |
| 77 | let length = (dx * dx + dy * dy).sqrt(); |
| 78 | if length > 0.0 { |
| 79 | (ux, uy) = (dx / length, dy / length); |
| 80 | } |
| 81 | out.push((line[i], ux, uy)); |
| 82 | } |
| 83 | out |
| 84 | } |
| 85 | |
| 86 | /// The nine cells a corridor-radius match can live in, centre first. |
| 87 | /// |
| 88 | /// Returning on the first match means visit order decides how much of the neighbourhood is |
| 89 | /// scanned. The point's own cell is by far the likeliest to hold it — a corridor of 30 m inside a |
| 90 | /// cell of 40 m — so it goes first, then the four edge neighbours, then the corners, which can |
| 91 | /// only match across a cell join. Visiting them in `-1..=1` order put a corner first and scanned |
| 92 | /// most of the neighbourhood before reaching the answer. |
| 93 | /// |
| 94 | /// A pure reordering: the set of samples examined is unchanged, so every result is too. |
| 95 | const NEIGHBOURHOOD: [(i32, i32); 9] = |
| 96 | [(0, 0), (1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]; |
| 97 | |
| 98 | /// Track already drawn, for suppressing a line that adds nothing. |
| 99 | /// |
| 100 | /// Two services of one colour must never draw as parallel lines: nothing distinguishes them, |
| 101 | /// so the second is pure over-draw. What stops that is the lane, which is assigned per |
| 102 | /// **colour** — two lines of one colour in one corridor take the same offset over the same |
| 103 | /// reference geometry and coincide exactly, reading as the one line they are. |
| 104 | /// |
| 105 | /// This is the other half: a line wholly on top of track already drawn in its colour is not |
| 106 | /// worth carrying at all. A route publishes its two directions, its short-turns and its |
| 107 | /// branch variants as separate shapes, and every other feed covering the city republishes |
| 108 | /// the lot re-surveyed a few metres off. |
| 109 | /// |
| 110 | /// **Whole lines only.** Subtracting the covered *parts* of a line was tried and is wrong: it |
| 111 | /// cut every route into fragments, and a stretch too short to be worth emitting left a hole |
| 112 | /// that nothing else drew. A line either adds something or it does not. |
| 113 | /// |
| 114 | /// The lookup is a `HashMap` of cells rather than the sorted vector the rest of this module |
| 115 | /// uses. Nothing iterates it — the answer is a boolean about one point — so it cannot leak an |
| 116 | /// order into the output. |
| 117 | pub struct Covered { |
| 118 | /// Every drawn sample, bucketed by its cell. |
| 119 | /// |
| 120 | /// **One representative per cell per service**: a bucket holds at most one sample of each |
| 121 | /// tag. That is enough for [`covers`](Covered::covers) and [`tags_over`](Covered::tags_over): |
| 122 | /// both ask whether *any* sample of a service is within a corridor of a point, and every |
| 123 | /// sample of one service in one cell answers that identically. Keeping only the first turns |
| 124 | /// a cell's occupancy from overlap depth — a trunk republished by a dozen feeds is a dozen |
| 125 | /// sample runs in the same cells — into distinct services, which is what those gates read. |
| 126 | /// It also makes "is this service already in this cell" a scan of a handful of entries in a |
| 127 | /// bucket already being looked up, rather than a second set of `(cell, tag)` pairs. |
| 128 | /// |
| 129 | /// Split into [`COVER_SHARDS`] maps by [`shard_of`], so a long line's add fills them all |
| 130 | /// at once. A pure partition: every cell lives in exactly one shard, and each shard sees its |
| 131 | /// cells' samples in line order, so every bucket ends up holding exactly what one map would. |
| 132 | cells: Vec<std::collections::HashMap<Cell, Bucket, CellHash>>, |
| 133 | /// How many `add` calls this cover has taken; see [`Covered::mark`]. |
| 134 | adds: u32, |
| 135 | } |
| 136 | |
| 137 | impl Default for Covered { |
| 138 | fn default() -> Self { |
| 139 | Covered { cells: (0..COVER_SHARDS).map(|_| Default::default()).collect(), adds: 0 } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /// Shards per [`Covered`]. Changes no answer; only how wide an add can go. |
| 144 | const COVER_SHARDS: usize = 64; |
| 145 | |
| 146 | /// The shard holding `cell`. Mixed independently of [`CellHasher`], so the cells of one shard |
| 147 | /// still spread over its own table. |
| 148 | fn shard_of((x, y): Cell) -> usize { |
| 149 | let h = (x as u32).wrapping_mul(0x9E37_79B1) ^ (y as u32).wrapping_mul(0x85EB_CA77); |
| 150 | (h.wrapping_mul(0xC2B2_AE35) >> 26) as usize % COVER_SHARDS |
| 151 | } |
| 152 | |
| 153 | /// One cell's representatives, in the order they were added. |
| 154 | struct Bucket { |
| 155 | /// The `adds` count of the last add that appended here. |
| 156 | added: u32, |
| 157 | /// Inline, because most buckets — every bucket of a colour's cover — hold exactly one. |
| 158 | first: Drawn, |
| 159 | rest: Vec<Drawn>, |
| 160 | } |
| 161 | |
| 162 | impl Bucket { |
| 163 | fn iter(&self) -> impl Iterator<Item = &Drawn> { |
| 164 | std::iter::once(&self.first).chain(self.rest.iter()) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /// One drawn sample and the service that drew it. |
| 169 | #[derive(Clone, Copy)] |
| 170 | struct Drawn { |
| 171 | point: (i32, i32), |
| 172 | ux: f64, |
| 173 | uy: f64, |
| 174 | tag: u32, |
| 175 | } |