Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / gtfs_ingest / src / bundle.rs
bundle.rs218 lines · 11.5 KiBRaw
| 1 | //! Which routes share a corridor, and which parallel lane each one takes through it. |
| 2 | //! |
| 3 | //! Where several services run over one physical track — six Muni Metro lines through the |
| 4 | //! Market Street subway, BART and Caltrain sharing an alignment — every route's polyline |
| 5 | //! lies on top of every other's and only one colour is visible. The standard transit-map |
| 6 | //! treatment is to fan them out into parallel coloured lines, and this decides which line |
| 7 | //! goes where. |
| 8 | //! |
| 9 | //! # Why here and not in the tiler or the renderer |
| 10 | //! |
| 11 | //! "Which routes share this corridor" is a global property of the feed set. The tiler sees |
| 12 | //! features one tile at a time and the renderer sees one tile at a time in arbitrary order, |
| 13 | //! so either would have to decide it per tile — which is exactly what puts a jog in every |
| 14 | //! route at every tile seam. It is decided once, here, and travels with the feature. |
| 15 | //! |
| 16 | //! # What comes out |
| 17 | //! |
| 18 | //! An ordered list of [`Span`]s per candidate, each a polyline and the inputs to a lane |
| 19 | //! choice. A route that shares nothing is one span: its own geometry, ordinal zero of one. |
| 20 | //! A route that runs through a corridor is cut at the corridor's ends, and the part inside |
| 21 | //! draws the **corridor's own reference polyline** rather than its own survey. That is what |
| 22 | //! makes the members of a corridor exactly parallel and evenly spaced: two agencies' surveys |
| 23 | //! of one track differ by a metre or two, which is a large fraction of the gap between lanes. |
| 24 | //! |
| 25 | //! The lane itself is *not* decided here. A span carries the colour's ordinal among the |
| 26 | //! corridor's distinct colours and how many of those there are, and the renderer turns that |
| 27 | //! into an offset — because how many lanes a corridor draws depends on the camera zoom, which |
| 28 | //! nothing here has. A taper travels alongside as a 0–255 fraction of whatever that offset |
| 29 | //! turns out to be, so a route eases into its lane instead of stepping sideways onto it. The |
| 30 | //! ease is drawn on the route's *own* survey, and only where the route arrives from its own |
| 31 | //! alignment: between two corridors it is in a lane on both sides and steps from one to the |
| 32 | //! other. |
| 33 | //! |
| 34 | //! # How |
| 35 | //! |
| 36 | //! Every candidate is resampled at [`SAMPLE_M`] and its samples are dropped into a spatial |
| 37 | //! grid — the sorted flat `Vec<(cell, id)>` [`crate::index`] uses for footpath transfers, |
| 38 | //! not a `HashMap` of buckets. For each sample, the routes with a sample within |
| 39 | //! [`CORRIDOR_M`] *running parallel to it* are that sample's local membership. |
| 40 | //! |
| 41 | //! A candidate's samples are then cut into maximal **runs** of equal membership. Runs, not |
| 42 | //! one membership per route: a route takes its lane from the corridor it is in *here*, so a |
| 43 | //! service that shares Market Street with nine others and the Bay Bridge with four takes a |
| 44 | //! lane in each. Picking one membership for a whole route was the previous rule, and it is |
| 45 | //! what left BART's five services on `-5, -3, +3, +7, +9` across the bay — slots assigned |
| 46 | //! for Market Street and carried where they meant nothing. |
| 47 | //! |
| 48 | //! Membership flickers sample to sample wherever a route drifts in and out of another's |
| 49 | //! tolerance, so runs shorter than [`MIN_SHARED_M`] are merged into their neighbour until |
| 50 | //! none are left. That threshold already existed for exactly this judgement: two alignments |
| 51 | //! that touch for fifty metres leaving a station are not a corridor. |
| 52 | //! |
| 53 | //! Parallelism is what keeps two tracks crossing at a junction from reading as a corridor: |
| 54 | //! bearings are compared folded to `[0, 180)`, so opposite directions over one track count |
| 55 | //! as parallel and a crossing does not. |
| 56 | //! |
| 57 | //! # Which lane is which |
| 58 | //! |
| 59 | //! The order across a corridor is the order its members arrive in, so a line never crosses |
| 60 | //! its neighbours where corridor membership changes. Each member's side is read off the |
| 61 | //! stretch of its own untouched survey immediately outside the corridor mouth — its |
| 62 | //! **approach** — where two routes that then run over one identical alignment are still tens |
| 63 | //! of metres apart and the sign is unambiguous. Inside the corridor they are within a metre |
| 64 | //! or two of the reference and the sign is survey noise. |
| 65 | //! |
| 66 | //! Nothing propagates an order between corridors. It is read off the geometry at each one |
| 67 | //! independently, and abutting corridors agree because the ground does. |
| 68 | //! |
| 69 | //! Nothing here may depend on `HashMap` iteration order — the exporter's output has to be |
| 70 | //! byte-identical between runs — so every collection that feeds a decision is a sorted |
| 71 | //! vector or a `BTreeMap`. |
| 72 | |
| 73 | use crate::shapes::{distance_m, project, resample}; |
| 74 | use crate::timing::PhaseTimer; |
| 75 | use rayon::prelude::*; |
| 76 | use std::cmp::Ordering; |
| 77 | use std::collections::BTreeMap; |
| 78 | use std::ops::Range; |
| 79 | use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering}; |
| 80 | use std::sync::Mutex; |
| 81 | |
| 82 | /// Two polylines closer than this everywhere are the same line, and two tracks closer than |
| 83 | /// this are one corridor. |
| 84 | /// |
| 85 | /// Well above the few metres two agencies' surveys of one track disagree by, and well below |
| 86 | /// `shapes.rs`' `MAX_STOP_OFFSET_M` (150 m), which is a shape belonging to another pattern. |
| 87 | pub const CORRIDOR_M: f64 = 30.0; |
| 88 | |
| 89 | /// How close a member's own survey has to come to the reference before it stops drawing its |
| 90 | /// own track and hands over to the corridor's. |
| 91 | /// |
| 92 | /// The other half of the judgement [`CORRIDOR_M`] makes, which the two shared until now: 30 m |
| 93 | /// is "these two tracks are one corridor", and this is "they are close enough that drawing |
| 94 | /// one on the other's geometry cannot be seen". Two lines closing at a shallow angle become |
| 95 | /// one corridor while still 30 m apart, and handing over there puts the whole 30 m into a |
| 96 | /// single sideways step at the mouth. Above the metre or two two agencies' surveys of one |
| 97 | /// track disagree by, and well below [`CORRIDOR_M`]. |
| 98 | const SNAP_M: f64 = 8.0; |
| 99 | |
| 100 | /// Spacing of the probe samples along a polyline. |
| 101 | /// |
| 102 | /// Small enough that two parallel tracks are compared point-to-point rather than |
| 103 | /// point-to-segment without the along-track offset mattering: half a step is 5 m against a |
| 104 | /// 30 m radius. |
| 105 | const SAMPLE_M: f64 = 10.0; |
| 106 | |
| 107 | /// Grid cell in metres, just above the search radius so a probe never has to scan more than |
| 108 | /// the nine cells around it. The sizing rule [`crate::index`]'s transfer grid uses. |
| 109 | const CELL_M: f64 = 40.0; |
| 110 | |
| 111 | /// How much track two routes have to share before it counts as a corridor. |
| 112 | /// |
| 113 | /// Also the smoothing window on the runs. Membership flips sample to sample wherever a |
| 114 | /// route grazes another's tolerance, and cutting on every flip would shatter a route into |
| 115 | /// dozens of features; a run shorter than this is absorbed into its neighbour instead. |
| 116 | /// Three hundred metres is well above a station throat and well below any shared trunk |
| 117 | /// worth drawing. |
| 118 | const MIN_SHARED_M: f64 = 300.0; |
| 119 | |
| 120 | /// Cosine of the angle within which two bearings count as parallel (25°). |
| 121 | /// |
| 122 | /// Compared on `|u · v|`, so the fold to `[0, 180)` is free: two routes over one track |
| 123 | /// stored in opposite directions are parallel, two tracks crossing at a junction are not. |
| 124 | const COS_FOLD: f64 = 0.906_307_787; |
| 125 | |
| 126 | /// The shortest ease a member spends coming into its lane, and the whole of it where the |
| 127 | /// member is already on the reference when its run begins. |
| 128 | /// |
| 129 | /// Without it a route steps sideways by up to nine Dp at the corridor mouth, which reads as |
| 130 | /// a break in the line rather than as a fan opening. Where the member converges slowly the |
| 131 | /// ease is longer than this: it runs from the mouth to wherever the survey first comes within |
| 132 | /// [`SNAP_M`]. |
| 133 | const TAPER_M: f64 = 100.0; |
| 134 | |
| 135 | /// Features in one taper. The taper travels as a 0–255 fraction of the full lane offset, so |
| 136 | /// the step count is only how many pieces the ease is cut into — and, because the fraction is |
| 137 | /// an equal-step ramp, how big the jump between two of them is. Eight puts every jump at |
| 138 | /// 255/9, about 1 Dp at a 9 Dp offset. |
| 139 | const TAPER_STEPS: usize = 8; |
| 140 | |
| 141 | /// Per-probe-range part: range index, per-sample local set ids, and the distinct |
| 142 | /// sets' flat `(values, ends)` buffers. |
| 143 | type ProbePart = (usize, Vec<u32>, Vec<u32>, Vec<u32>); |
| 144 | |
| 145 | /// One corridor entry: set id plus its members as `(member, from, to)` spans. |
| 146 | type CorridorMembers = Vec<(usize, f64, f64)>; |
| 147 | |
| 148 | /// How much of a member's own survey, immediately outside the corridor mouth, decides which |
| 149 | /// side of the corridor it takes. |
| 150 | /// |
| 151 | /// Across the shared stretch two routes on one track sit within a couple of metres of the |
| 152 | /// reference and the sign of that is survey noise. On the approach they are still diverging, |
| 153 | /// and the sign is what separates "came from the north" from "came from the south". Above |
| 154 | /// [`TAPER_M`] so it clears the ease-in, and below [`MIN_SHARED_M`] so it cannot reach back |
| 155 | /// into the body of the corridor before. |
| 156 | const APPROACH_M: f64 = 150.0; |
| 157 | |
| 158 | /// Points sampled along the approach. The mean of a handful, because one point lands wherever |
| 159 | /// the survey's own vertices happen to fall. |
| 160 | const APPROACH_SAMPLES: usize = 8; |
| 161 | |
| 162 | /// One route's polyline, offered for slotting. |
| 163 | pub struct Candidate<'a> { |
| 164 | pub points: &'a [(i32, i32)], |
| 165 | /// Which route this polyline belongs to. Two polylines of one route — a branch and its |
| 166 | /// trunk — count as one member of a corridor, because the lane is per route and not per |
| 167 | /// line. |
| 168 | pub route: u32, |
| 169 | /// `0xRRGGBB`. The lane is per colour, and the colour is the tie-break on the order |
| 170 | /// within a corridor when two surveys genuinely coincide. |
| 171 | pub color: u32, |
| 172 | /// The route's display name. The second half of that tie-break, so a rebuild puts the |
| 173 | /// same route in the same lane. |
| 174 | pub name: &'a str, |
| 175 | } |
| 176 | |
| 177 | /// One stretch of a candidate as it should be drawn. |
| 178 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 179 | pub struct Span { |
| 180 | /// The polyline to emit. The candidate's own geometry outside a corridor and across the |
| 181 | /// ease into one, and the corridor's reference geometry over the body. |
| 182 | pub points: Vec<(i32, i32)>, |
| 183 | /// This colour's index among the corridor's distinct colours, from zero. Zero outside a |
| 184 | /// corridor. |
| 185 | /// |
| 186 | /// The order is **positional**: the colours are laid out across the corridor in the order |
| 187 | /// their routes arrive at it, so two groups that merge do not interleave and nothing |
| 188 | /// crosses. Zero is the left-hand side of the reference's direction of travel. |
| 189 | /// |
| 190 | /// The index is over the distinct **colours** of the corridor, not its routes. Two routes |
| 191 | /// of one colour are indistinguishable once drawn, so giving them a lane each spends width |
| 192 | /// on a difference nobody can see — and it is what doubles a network whose feed publishes |
| 193 | /// each direction as its own route (BART's `Yellow-N` and `Yellow-S`) or splits one |
| 194 | /// service into branch variants. Sharing a lane makes them coincide exactly, because they |
| 195 | /// are also drawing the same reference geometry. |
| 196 | pub ordinal: u8, |
| 197 | /// How many distinct colours the corridor carries. One outside a corridor. |
| 198 | /// |
| 199 | /// Unclamped: how many lanes to actually draw is the renderer's decision, because it |
| 200 | /// depends on the camera zoom and the exporter has no zoom. |
| 201 | pub lanes: u8, |
| 202 | /// How far into its lane this piece sits, over 255. 255 outside a corridor and on a |
| 203 | /// corridor's body; less only across a taper at its mouth. |
| 204 | pub taper: u8, |
| 205 | } |
| 206 | |
| 207 | /// Cut every candidate into spans and give each one its lane. One list out per candidate in, |
| 208 | /// in the same order, never empty. |
| 209 | pub fn assign(candidates: &[Candidate]) -> Vec<Vec<Span>> { |
| 210 | assign_with_timer(candidates, None) |
| 211 | } |
| 212 | |
| 213 | include!("bundle_s1.rs"); |
| 214 | include!("bundle_part1.rs"); |
| 215 | include!("bundle_part2.rs"); |
| 216 | include!("bundle_part3.rs"); |
| 217 | include!("bundle_part4.rs"); |
| 218 | include!("bundle_part5.rs"); |