//! Which routes share a corridor, and which parallel lane each one takes through it. //! //! Where several services run over one physical track — six Muni Metro lines through the //! Market Street subway, BART and Caltrain sharing an alignment — every route's polyline //! lies on top of every other's and only one colour is visible. The standard transit-map //! treatment is to fan them out into parallel coloured lines, and this decides which line //! goes where. //! //! # Why here and not in the tiler or the renderer //! //! "Which routes share this corridor" is a global property of the feed set. The tiler sees //! features one tile at a time and the renderer sees one tile at a time in arbitrary order, //! so either would have to decide it per tile — which is exactly what puts a jog in every //! route at every tile seam. It is decided once, here, and travels with the feature. //! //! # What comes out //! //! An ordered list of [`Span`]s per candidate, each a polyline and the inputs to a lane //! choice. A route that shares nothing is one span: its own geometry, ordinal zero of one. //! A route that runs through a corridor is cut at the corridor's ends, and the part inside //! draws the **corridor's own reference polyline** rather than its own survey. That is what //! makes the members of a corridor exactly parallel and evenly spaced: two agencies' surveys //! of one track differ by a metre or two, which is a large fraction of the gap between lanes. //! //! The lane itself is *not* decided here. A span carries the colour's ordinal among the //! corridor's distinct colours and how many of those there are, and the renderer turns that //! into an offset — because how many lanes a corridor draws depends on the camera zoom, which //! nothing here has. A taper travels alongside as a 0–255 fraction of whatever that offset //! turns out to be, so a route eases into its lane instead of stepping sideways onto it. The //! ease is drawn on the route's *own* survey, and only where the route arrives from its own //! alignment: between two corridors it is in a lane on both sides and steps from one to the //! other. //! //! # How //! //! Every candidate is resampled at [`SAMPLE_M`] and its samples are dropped into a spatial //! grid — the sorted flat `Vec<(cell, id)>` [`crate::index`] uses for footpath transfers, //! not a `HashMap` of buckets. For each sample, the routes with a sample within //! [`CORRIDOR_M`] *running parallel to it* are that sample's local membership. //! //! A candidate's samples are then cut into maximal **runs** of equal membership. Runs, not //! one membership per route: a route takes its lane from the corridor it is in *here*, so a //! service that shares Market Street with nine others and the Bay Bridge with four takes a //! lane in each. Picking one membership for a whole route was the previous rule, and it is //! what left BART's five services on `-5, -3, +3, +7, +9` across the bay — slots assigned //! for Market Street and carried where they meant nothing. //! //! Membership flickers sample to sample wherever a route drifts in and out of another's //! tolerance, so runs shorter than [`MIN_SHARED_M`] are merged into their neighbour until //! none are left. That threshold already existed for exactly this judgement: two alignments //! that touch for fifty metres leaving a station are not a corridor. //! //! Parallelism is what keeps two tracks crossing at a junction from reading as a corridor: //! bearings are compared folded to `[0, 180)`, so opposite directions over one track count //! as parallel and a crossing does not. //! //! # Which lane is which //! //! The order across a corridor is the order its members arrive in, so a line never crosses //! its neighbours where corridor membership changes. Each member's side is read off the //! stretch of its own untouched survey immediately outside the corridor mouth — its //! **approach** — where two routes that then run over one identical alignment are still tens //! of metres apart and the sign is unambiguous. Inside the corridor they are within a metre //! or two of the reference and the sign is survey noise. //! //! Nothing propagates an order between corridors. It is read off the geometry at each one //! independently, and abutting corridors agree because the ground does. //! //! Nothing here may depend on `HashMap` iteration order — the exporter's output has to be //! byte-identical between runs — so every collection that feeds a decision is a sorted //! vector or a `BTreeMap`. use crate::shapes::{distance_m, project, resample}; use crate::timing::PhaseTimer; use rayon::prelude::*; use std::cmp::Ordering; use std::collections::BTreeMap; use std::ops::Range; use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering}; use std::sync::Mutex; /// Two polylines closer than this everywhere are the same line, and two tracks closer than /// this are one corridor. /// /// Well above the few metres two agencies' surveys of one track disagree by, and well below /// `shapes.rs`' `MAX_STOP_OFFSET_M` (150 m), which is a shape belonging to another pattern. pub const CORRIDOR_M: f64 = 30.0; /// How close a member's own survey has to come to the reference before it stops drawing its /// own track and hands over to the corridor's. /// /// The other half of the judgement [`CORRIDOR_M`] makes, which the two shared until now: 30 m /// is "these two tracks are one corridor", and this is "they are close enough that drawing /// one on the other's geometry cannot be seen". Two lines closing at a shallow angle become /// one corridor while still 30 m apart, and handing over there puts the whole 30 m into a /// single sideways step at the mouth. Above the metre or two two agencies' surveys of one /// track disagree by, and well below [`CORRIDOR_M`]. const SNAP_M: f64 = 8.0; /// Spacing of the probe samples along a polyline. /// /// Small enough that two parallel tracks are compared point-to-point rather than /// point-to-segment without the along-track offset mattering: half a step is 5 m against a /// 30 m radius. const SAMPLE_M: f64 = 10.0; /// Grid cell in metres, just above the search radius so a probe never has to scan more than /// the nine cells around it. The sizing rule [`crate::index`]'s transfer grid uses. const CELL_M: f64 = 40.0; /// How much track two routes have to share before it counts as a corridor. /// /// Also the smoothing window on the runs. Membership flips sample to sample wherever a /// route grazes another's tolerance, and cutting on every flip would shatter a route into /// dozens of features; a run shorter than this is absorbed into its neighbour instead. /// Three hundred metres is well above a station throat and well below any shared trunk /// worth drawing. const MIN_SHARED_M: f64 = 300.0; /// Cosine of the angle within which two bearings count as parallel (25°). /// /// Compared on `|u · v|`, so the fold to `[0, 180)` is free: two routes over one track /// stored in opposite directions are parallel, two tracks crossing at a junction are not. const COS_FOLD: f64 = 0.906_307_787; /// The shortest ease a member spends coming into its lane, and the whole of it where the /// member is already on the reference when its run begins. /// /// Without it a route steps sideways by up to nine Dp at the corridor mouth, which reads as /// a break in the line rather than as a fan opening. Where the member converges slowly the /// ease is longer than this: it runs from the mouth to wherever the survey first comes within /// [`SNAP_M`]. const TAPER_M: f64 = 100.0; /// Features in one taper. The taper travels as a 0–255 fraction of the full lane offset, so /// the step count is only how many pieces the ease is cut into — and, because the fraction is /// an equal-step ramp, how big the jump between two of them is. Eight puts every jump at /// 255/9, about 1 Dp at a 9 Dp offset. const TAPER_STEPS: usize = 8; /// Per-probe-range part: range index, per-sample local set ids, and the distinct /// sets' flat `(values, ends)` buffers. type ProbePart = (usize, Vec, Vec, Vec); /// One corridor entry: set id plus its members as `(member, from, to)` spans. type CorridorMembers = Vec<(usize, f64, f64)>; /// How much of a member's own survey, immediately outside the corridor mouth, decides which /// side of the corridor it takes. /// /// Across the shared stretch two routes on one track sit within a couple of metres of the /// reference and the sign of that is survey noise. On the approach they are still diverging, /// and the sign is what separates "came from the north" from "came from the south". Above /// [`TAPER_M`] so it clears the ease-in, and below [`MIN_SHARED_M`] so it cannot reach back /// into the body of the corridor before. const APPROACH_M: f64 = 150.0; /// Points sampled along the approach. The mean of a handful, because one point lands wherever /// the survey's own vertices happen to fall. const APPROACH_SAMPLES: usize = 8; /// One route's polyline, offered for slotting. pub struct Candidate<'a> { pub points: &'a [(i32, i32)], /// Which route this polyline belongs to. Two polylines of one route — a branch and its /// trunk — count as one member of a corridor, because the lane is per route and not per /// line. pub route: u32, /// `0xRRGGBB`. The lane is per colour, and the colour is the tie-break on the order /// within a corridor when two surveys genuinely coincide. pub color: u32, /// The route's display name. The second half of that tie-break, so a rebuild puts the /// same route in the same lane. pub name: &'a str, } /// One stretch of a candidate as it should be drawn. #[derive(Clone, Debug, PartialEq, Eq)] pub struct Span { /// The polyline to emit. The candidate's own geometry outside a corridor and across the /// ease into one, and the corridor's reference geometry over the body. pub points: Vec<(i32, i32)>, /// This colour's index among the corridor's distinct colours, from zero. Zero outside a /// corridor. /// /// The order is **positional**: the colours are laid out across the corridor in the order /// their routes arrive at it, so two groups that merge do not interleave and nothing /// crosses. Zero is the left-hand side of the reference's direction of travel. /// /// The index is over the distinct **colours** of the corridor, not its routes. Two routes /// of one colour are indistinguishable once drawn, so giving them a lane each spends width /// on a difference nobody can see — and it is what doubles a network whose feed publishes /// each direction as its own route (BART's `Yellow-N` and `Yellow-S`) or splits one /// service into branch variants. Sharing a lane makes them coincide exactly, because they /// are also drawing the same reference geometry. pub ordinal: u8, /// How many distinct colours the corridor carries. One outside a corridor. /// /// Unclamped: how many lanes to actually draw is the renderer's decision, because it /// depends on the camera zoom and the exporter has no zoom. pub lanes: u8, /// How far into its lane this piece sits, over 255. 255 outside a corridor and on a /// corridor's body; less only across a taper at its mouth. pub taper: u8, } /// Cut every candidate into spans and give each one its lane. One list out per candidate in, /// in the same order, never empty. pub fn assign(candidates: &[Candidate]) -> Vec> { assign_with_timer(candidates, None) } include!("bundle_s1.rs"); include!("bundle_part1.rs"); include!("bundle_part2.rs"); include!("bundle_part3.rs"); include!("bundle_part4.rs"); include!("bundle_part5.rs");