/// How far to the side of a corridor's reference `p` lies, in metres, signed positive to the /// right of the reference's direction of travel. /// /// The same nearest-segment walk [`distance_along`] does, because [`project`] returns an /// unsigned distance and no side. Against the nearest segment's unit tangent `(ux, uy)` in an /// (east, north) frame — the frame [`directed`] already produces — the side of the offset `d` /// is `d.east * uy - d.north * ux`. Positive is the right-hand side because `tess::stroke` /// emits `(0, +1)` as the normal of an eastward segment and `line.vert` shifts along it, so /// ascending offset is ascending ordinal: `lane_offset_px` gives ordinal zero the most /// negative offset, which is the left of the reference's travel. /// /// Only the reference's own stretch of the corridor, `extent`, is searched. Past the mouth the /// reference has left the corridor too, and the segment nearest an approaching member is then /// as likely to be the reference's departure as the corridor — which reads a member leaving /// west as having no side at all. Clamped, the approach is measured against the reference's /// tangent at the mouth, which is the axis the lanes are laid out across. fn signed_offset(corridor: &Corridor, extent: (f64, f64), p: (i32, i32)) -> f64 { let (points, cum) = (&corridor.points, &corridor.cum); let mut best = (f64::INFINITY, 0.0f64); for at in 0..points.len().saturating_sub(1) { if cum[at + 1] < extent.0 || cum[at] > extent.1 { continue; } let (a, b) = (points[at], points[at + 1]); let (_, distance) = project(p, a, b, 0.0); if distance >= best.0 { continue; } let cos_lat = ((a.0 as f64 + b.0 as f64) * 0.5 * 1e-7).to_radians().cos(); let (east, north) = ((b.1 - a.1) as f64 * cos_lat, (b.0 - a.0) as f64); let length = (east * east + north * north).sqrt(); if length <= 0.0 { continue; } let (ux, uy) = (east / length, north / length); let d_east = (p.1 - a.1) as f64 * 1e-7 * 111_320.0 * cos_lat; let d_north = (p.0 - a.0) as f64 * 1e-7 * 111_320.0; best = (distance, d_east * uy - d_north * ux); } best.1 } /// One probe point along a candidate, with the unit direction of travel there. struct Sample { route: u32, point: (i32, i32), ux: f64, uy: f64, } /// One walked sample of a line: its point and the unit direction of travel there. /// /// Exposed so a caller that runs several of [`Covered`]'s gates over one line can walk it /// once and hand the samples to each, rather than every gate re-walking the same points. pub type Walked = ((i32, i32), f64, f64); /// Walk a polyline at [`SAMPLE_M`], returning each sample with its unit direction of travel. pub fn walk(points: &[(i32, i32)]) -> Vec { let walked = resample(points, SAMPLE_M); if walked.len() < 2 { return Vec::new(); } directed(&walked) } /// Each point of `line` with the unit direction of travel there. One entry per input point: /// a degenerate segment carries the previous direction forward rather than dropping the /// point, so the result stays index-aligned with its input. fn directed(line: &[(i32, i32)]) -> Vec<((i32, i32), f64, f64)> { let mut out = Vec::with_capacity(line.len()); let (mut ux, mut uy) = (1.0f64, 0.0f64); for i in 0..line.len() { let (a, b) = if i + 1 < line.len() { (line[i], line[i + 1]) } else { (line[i.max(1) - 1], line[i]) }; let cos_lat = ((a.0 as f64 + b.0 as f64) * 0.5 * 1e-7).to_radians().cos(); let dy = (b.0 - a.0) as f64; let dx = (b.1 - a.1) as f64 * cos_lat; let length = (dx * dx + dy * dy).sqrt(); if length > 0.0 { (ux, uy) = (dx / length, dy / length); } out.push((line[i], ux, uy)); } out } /// The nine cells a corridor-radius match can live in, centre first. /// /// Returning on the first match means visit order decides how much of the neighbourhood is /// scanned. The point's own cell is by far the likeliest to hold it — a corridor of 30 m inside a /// cell of 40 m — so it goes first, then the four edge neighbours, then the corners, which can /// only match across a cell join. Visiting them in `-1..=1` order put a corner first and scanned /// most of the neighbourhood before reaching the answer. /// /// A pure reordering: the set of samples examined is unchanged, so every result is too. const NEIGHBOURHOOD: [(i32, i32); 9] = [(0, 0), (1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]; /// Track already drawn, for suppressing a line that adds nothing. /// /// Two services of one colour must never draw as parallel lines: nothing distinguishes them, /// so the second is pure over-draw. What stops that is the lane, which is assigned per /// **colour** — two lines of one colour in one corridor take the same offset over the same /// reference geometry and coincide exactly, reading as the one line they are. /// /// This is the other half: a line wholly on top of track already drawn in its colour is not /// worth carrying at all. A route publishes its two directions, its short-turns and its /// branch variants as separate shapes, and every other feed covering the city republishes /// the lot re-surveyed a few metres off. /// /// **Whole lines only.** Subtracting the covered *parts* of a line was tried and is wrong: it /// cut every route into fragments, and a stretch too short to be worth emitting left a hole /// that nothing else drew. A line either adds something or it does not. /// /// The lookup is a `HashMap` of cells rather than the sorted vector the rest of this module /// uses. Nothing iterates it — the answer is a boolean about one point — so it cannot leak an /// order into the output. pub struct Covered { /// Every drawn sample, bucketed by its cell. /// /// **One representative per cell per service**: a bucket holds at most one sample of each /// tag. That is enough for [`covers`](Covered::covers) and [`tags_over`](Covered::tags_over): /// both ask whether *any* sample of a service is within a corridor of a point, and every /// sample of one service in one cell answers that identically. Keeping only the first turns /// a cell's occupancy from overlap depth — a trunk republished by a dozen feeds is a dozen /// sample runs in the same cells — into distinct services, which is what those gates read. /// It also makes "is this service already in this cell" a scan of a handful of entries in a /// bucket already being looked up, rather than a second set of `(cell, tag)` pairs. /// /// Split into [`COVER_SHARDS`] maps by [`shard_of`], so a long line's add fills them all /// at once. A pure partition: every cell lives in exactly one shard, and each shard sees its /// cells' samples in line order, so every bucket ends up holding exactly what one map would. cells: Vec>, /// How many `add` calls this cover has taken; see [`Covered::mark`]. adds: u32, } impl Default for Covered { fn default() -> Self { Covered { cells: (0..COVER_SHARDS).map(|_| Default::default()).collect(), adds: 0 } } } /// Shards per [`Covered`]. Changes no answer; only how wide an add can go. const COVER_SHARDS: usize = 64; /// The shard holding `cell`. Mixed independently of [`CellHasher`], so the cells of one shard /// still spread over its own table. fn shard_of((x, y): Cell) -> usize { let h = (x as u32).wrapping_mul(0x9E37_79B1) ^ (y as u32).wrapping_mul(0x85EB_CA77); (h.wrapping_mul(0xC2B2_AE35) >> 26) as usize % COVER_SHARDS } /// One cell's representatives, in the order they were added. struct Bucket { /// The `adds` count of the last add that appended here. added: u32, /// Inline, because most buckets — every bucket of a colour's cover — hold exactly one. first: Drawn, rest: Vec, } impl Bucket { fn iter(&self) -> impl Iterator { std::iter::once(&self.first).chain(self.rest.iter()) } } /// One drawn sample and the service that drew it. #[derive(Clone, Copy)] struct Drawn { point: (i32, i32), ux: f64, uy: f64, tag: u32, }