Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / schema / junction_part3.rs
junction_part3.rs500 lines · 26.5 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod tests { |
| 3 | use super::*; |
| 4 | use crate::schema::traffic_extra::tests::{write_graph, EdgeSpec, GraphFixture}; |
| 5 | |
| 6 | /// A four-arm crossroads: node 0 in the middle, nodes 1..4 due north, east, south and west of |
| 7 | /// it, every arm two-way and primary (arterial, so the arterial gate keeps |
| 8 | /// the node). Roughly 100 m out on each side, which is a real |
| 9 | /// intersection's scale and comfortably longer than [`SETBACK_M`]. |
| 10 | /// |
| 11 | /// Built with the traffic layer's own byte-for-byte v6 writer, so the reader here is exercised |
| 12 | /// against the same layout the contract specifies rather than a mock. |
| 13 | pub(super) fn crossroads(lanes: &[(u32, Vec<u16>)]) -> GraphFixture { |
| 14 | crossroads_at(350_000_000, 12_000, lanes) |
| 15 | } |
| 16 | |
| 17 | /// The same crossroads at an arbitrary latitude. `lon_step_e7` is chosen by the caller so the |
| 18 | /// east and west arms stay roughly 100 m out as `cos φ` shrinks a degree of longitude. |
| 19 | fn crossroads_at( |
| 20 | lat_e7: i32, |
| 21 | lon_step_e7: i32, |
| 22 | lanes: &[(u32, Vec<u16>)], |
| 23 | ) -> GraphFixture { |
| 24 | let coords: [(i32, i32); 5] = [ |
| 25 | (lat_e7, -1_200_000_000), // 0: the junction |
| 26 | (lat_e7 + 9_000, -1_200_000_000), // 1: north, ~100 m |
| 27 | (lat_e7, -1_200_000_000 + lon_step_e7), // 2: east |
| 28 | (lat_e7 - 9_000, -1_200_000_000), // 3: south |
| 29 | (lat_e7, -1_200_000_000 - lon_step_e7), // 4: west |
| 30 | ]; |
| 31 | let edge = |source, target| EdgeSpec { source, target, type_: 3, interior: Vec::new() }; |
| 32 | write_graph( |
| 33 | "junction", |
| 34 | &coords, |
| 35 | &[ |
| 36 | // Edges 0..3 leave the junction; edges 4..7 return to it. Grouped by source, which |
| 37 | // is what makes `nodes.bin`'s edge_ptr a CSR row pointer. |
| 38 | edge(0, 1), |
| 39 | edge(0, 2), |
| 40 | edge(0, 3), |
| 41 | edge(0, 4), |
| 42 | edge(1, 0), |
| 43 | edge(2, 0), |
| 44 | edge(3, 0), |
| 45 | edge(4, 0), |
| 46 | ], |
| 47 | lanes, |
| 48 | ) |
| 49 | } |
| 50 | |
| 51 | pub(super) fn stream(fixture: &GraphFixture) -> (u64, Vec<Vec<(f64, f64)>>) { |
| 52 | let spill = fixture.dir.join("features.tmp"); |
| 53 | let mut sink = Sink::create(&spill).expect("sink"); |
| 54 | let emitted = |
| 55 | stream_junctions(&fixture.dir, &Conventions::default(), &mut sink).expect("stream"); |
| 56 | let store = sink.finish(&spill).expect("finish"); |
| 57 | let mut reader = store.reader().expect("reader"); |
| 58 | let mut lines = Vec::new(); |
| 59 | while let Some(feature) = reader.next().expect("read") { |
| 60 | assert_eq!(feature.class.layer, LAYER_JUNCTION); |
| 61 | assert_eq!(feature.class.min_zoom, MIN_ZOOM); |
| 62 | assert!(!feature.class.area, "a connector is a line, not a ring"); |
| 63 | match &feature.geometry { |
| 64 | Geometry::Lines(parts) => { |
| 65 | assert_eq!(parts.len(), 1, "one connector is one part"); |
| 66 | lines.push(parts[0].clone()); |
| 67 | } |
| 68 | other => panic!("a connector came back as {other:?}"), |
| 69 | } |
| 70 | } |
| 71 | let _ = std::fs::remove_file(&spill); |
| 72 | (emitted, lines) |
| 73 | } |
| 74 | |
| 75 | /// **The property the parallel junction streamer exists to keep.** The |
| 76 | /// spill is a function of the graph, not of the thread count. |
| 77 | /// |
| 78 | /// The crossroads fixture is tiny, so this pins the ordering discipline |
| 79 | /// (parallel map, serial ordered drain) rather than measuring a speedup: |
| 80 | /// 1 thread is the configuration where the pool is degenerate, 3 is the |
| 81 | /// awkward one where completion order is guaranteed not to be read |
| 82 | /// order. The count and the decoded geometry must agree everywhere — |
| 83 | /// `sink.push` order is the spill order is the archive, so any drift |
| 84 | /// here is a byte drift downstream. |
| 85 | #[test] |
| 86 | fn connectors_are_identical_at_every_thread_count() { |
| 87 | let outer = crossroads(&[(4u32, vec![LANE_LEFT, LANE_THROUGH, LANE_RIGHT])]); |
| 88 | // The tagged north approach keeps left + right (through dropped); |
| 89 | // the three untagged approaches keep two turns each: 2 + 6. |
| 90 | par::set_threads(1); |
| 91 | let (emitted_one, lines_one) = stream(&outer); |
| 92 | assert_eq!(emitted_one as usize, lines_one.len()); |
| 93 | for threads in [2usize, 3, 8] { |
| 94 | par::set_threads(threads); |
| 95 | // A fresh fixture per count: `stream` spills beside the graph |
| 96 | // dir, and reusing one dir would serialize the runs through the |
| 97 | // same `features.tmp`. |
| 98 | let fixture = |
| 99 | crossroads(&[(4u32, vec![LANE_LEFT, LANE_THROUGH, LANE_RIGHT])]); |
| 100 | let (emitted, lines) = stream(&fixture); |
| 101 | assert_eq!( |
| 102 | emitted, emitted_one, |
| 103 | "{threads} threads changed the connector count" |
| 104 | ); |
| 105 | assert_eq!( |
| 106 | lines, lines_one, |
| 107 | "{threads} threads changed the connector bytes" |
| 108 | ); |
| 109 | } |
| 110 | par::set_threads( |
| 111 | std::thread::available_parallelism().map(|n| n.get()).unwrap_or(4), |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | /// **A connector must start and end on the asphalt the renderer actually paints.** |
| 116 | /// |
| 117 | /// Two defects met here and neither could be fixed alone. The ends disagreed — the approach was |
| 118 | /// widened to the junction's exit count while the exit kept `lanes.bin`'s, so [`exit_lane`] |
| 119 | /// returned lane 0 for every movement and all `d - 1` connectors into an arm terminated on one |
| 120 | /// shared point. And the width itself was fabricated: `legal.len()` counts exits, not lanes. |
| 121 | /// Mirroring the fabricated width would have made the two ends agree and put *both* of them off |
| 122 | /// the road, which is why this pins position rather than spread. |
| 123 | /// |
| 124 | /// An untagged two-way road is one lane each way — the carriageway renderer's own default — so |
| 125 | /// the kerb is one lane width from the centreline and every connector end belongs at half of |
| 126 | /// one, on the driver's side. Under the fabricated count the outermost lane sat at 2.5 widths |
| 127 | /// against that kerb at 1.0, so this fails on the starts; mirror it and it fails on the ends |
| 128 | /// too. |
| 129 | /// |
| 130 | /// Distinct endpoints are deliberately *not* asserted. With one real lane per direction the |
| 131 | /// movements out of an arm share it, so `d` distinct endpoints at a degree-`d` junction is the |
| 132 | /// correct answer; separating them would mean drawing lanes that are not there. |
| 133 | /// |
| 134 | /// **This test assumes the carriageway is one lane each way, and that assumption is no longer |
| 135 | /// universally true.** It holds here because the fixture is untagged and [`effective_lanes`] |
| 136 | /// and the renderer then agree on 1. [`crate::lanefill`] now fills the lane *count* of a |
| 137 | /// junction stub from the road it interrupts, so at a divided arterial the carriageway is |
| 138 | /// painted several lanes wide while this layer, which reads `turn:lanes` masks that `lanefill` |
| 139 | /// deliberately does not write, still lays connectors out one lane per direction. The two |
| 140 | /// disagree there and this test cannot see it: connectors hug the centreline of a wide |
| 141 | /// intersection rather than running off it, so nothing here fails. Closing that needs the |
| 142 | /// filled count carried to the directed edge this layer sees, which is not yet possible. |
| 143 | #[test] |
| 144 | fn a_connector_starts_and_ends_on_the_painted_carriageway() { |
| 145 | let (emitted, lines) = stream(&crossroads(&[])); |
| 146 | // Four approaches x two turns each: through movements carry no |
| 147 | // connector (the carriageway ribbon already paints the straight path). |
| 148 | assert_eq!(emitted, 8, "four approaches x two turns"); |
| 149 | |
| 150 | // The fixture's arms run due north, east, south and west, so of a point's two displacements |
| 151 | // from the node the larger is the along-road setback and the smaller is the lateral offset. |
| 152 | // This inverts `offset_degrees`, which reads longitude at the node's own latitude. |
| 153 | let lane = lane_width_ground_m(35.0); |
| 154 | let lateral = |(lon, lat): (f64, f64)| { |
| 155 | let east = (lon + 120.0) * METRES_PER_DEGREE * 35.0f64.to_radians().cos(); |
| 156 | let north = (lat - 35.0) * METRES_PER_DEGREE; |
| 157 | east.abs().min(north.abs()) |
| 158 | }; |
| 159 | for line in &lines { |
| 160 | for (end, p) in [("start", line[0]), ("end", *line.last().expect("points"))] { |
| 161 | let off = lateral(p); |
| 162 | assert!( |
| 163 | off < lane, |
| 164 | "a connector's {end} sits {off:.2} m from the centreline, past the kerb at \ |
| 165 | {lane:.2} m. An untagged two-way road is one lane each way, so this connector \ |
| 166 | is drawn clean off the road it is meant to join — a lane count has been \ |
| 167 | fabricated from something that is not a lane count.", |
| 168 | ); |
| 169 | assert!( |
| 170 | (off - lane * 0.5).abs() < 0.05, |
| 171 | "a connector's {end} belongs in the middle of the arm's single lane, at \ |
| 172 | {:.2} m from the centreline, not {off:.2} m", |
| 173 | lane * 0.5, |
| 174 | ); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Both ends reading the same count is what lets a turning movement |
| 179 | // keep its lane and therefore its offset. Through movements carry no |
| 180 | // connector at all (dropped in stream_junctions), so no two-point |
| 181 | // straight survives: every remaining connector is a sampled curve. |
| 182 | assert_eq!( |
| 183 | lines.iter().filter(|l| l.len() == 2).count(), |
| 184 | 0, |
| 185 | "through movements are dropped, so nothing draws straight", |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | /// The single source both ends read, and the default it falls back to. |
| 190 | /// |
| 191 | /// The fallback is not a guess. It is the carriageway renderer's `lane_count 0 -> oneway ? 1 : 2` |
| 192 | /// read in this module's per-direction units, where both branches come to one lane, so a |
| 193 | /// connector is laid out across exactly the asphalt that gets painted. |
| 194 | #[test] |
| 195 | fn an_arms_lane_count_is_its_tagged_one_or_the_renderers_own_default() { |
| 196 | let arm_with = |masks: Option<Vec<u16>>, two_way: bool| Arm { |
| 197 | neighbour: 1, |
| 198 | heading: 0.0, |
| 199 | length_m: 100.0, |
| 200 | two_way, |
| 201 | masks, |
| 202 | road_class: 7, |
| 203 | }; |
| 204 | assert_eq!( |
| 205 | effective_lanes(&arm_with(Some(vec![LANE_LEFT, LANE_THROUGH]), true)), |
| 206 | 2, |
| 207 | "`turn:lanes` is the only real lane count reachable from the routing graph", |
| 208 | ); |
| 209 | assert_eq!( |
| 210 | effective_lanes(&arm_with(None, true)), |
| 211 | 1, |
| 212 | "a two-way road is two lanes across both directions, so one each way", |
| 213 | ); |
| 214 | assert_eq!( |
| 215 | effective_lanes(&arm_with(None, false)), |
| 216 | 1, |
| 217 | "and a one-way road is one lane, which is the renderer's other branch", |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | /// **A tiler-side `min_zoom` past the archive's deepest zoom deletes a layer in silence.** |
| 222 | /// |
| 223 | /// The tiler writes a feature into tiles from its `min_zoom` down. Nothing deeper than z14 |
| 224 | /// is ever built, so a layer gated deeper than that is computed in full, costs its whole run |
| 225 | /// time, and then lands in no tile at all — while the build exits zero and prints the count it |
| 226 | /// generated. Junction shipped at 16 against a 14-deep archive and the only symptom was a |
| 227 | /// layer missing from the archive's own summary. |
| 228 | /// |
| 229 | /// Pinned against [`crate::DEFAULT_MAX_ZOOM`] itself, so the bound is a compile-time |
| 230 | /// dependency on the build's own default rather than a copy of it, and for both layers read |
| 231 | /// out of the routing graph, because they share the failure. |
| 232 | #[test] |
| 233 | fn a_graph_derived_layer_is_tiled_no_deeper_than_the_archive_is_built() { |
| 234 | let max_zoom = crate::DEFAULT_MAX_ZOOM; |
| 235 | let layers = [("junction", MIN_ZOOM), ("traffic", crate::schema::traffic::MIN_ZOOM)]; |
| 236 | for (layer, min_zoom) in layers { |
| 237 | assert!( |
| 238 | min_zoom <= max_zoom, |
| 239 | "`{layer}` is tiled from z{min_zoom}, but archives are only built to z{max_zoom}, \ |
| 240 | so no tile that could hold it is ever written: every {layer} feature will be \ |
| 241 | computed and then discarded, and the layer will be silently absent from every \ |
| 242 | archive while the build still reports success. This constant is the TILING gate \ |
| 243 | and must be at most z{max_zoom}; the zoom the layer is DRAWN at is the style's \ |
| 244 | `minzoom` in basemap.flat.json and is set there, independently.", |
| 245 | ); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /// The angle sign convention the whole layer rests on: positive is a right turn. |
| 250 | /// |
| 251 | /// Wrong here and every lane pairing is mirrored, which is the one failure that would look |
| 252 | /// plausible on a screenshot — connectors that curve smoothly into the wrong road. |
| 253 | #[test] |
| 254 | fn a_turn_angle_is_positive_to_the_right() { |
| 255 | // Heading north, leaving east. |
| 256 | assert_eq!(classify_turn(normalise_degrees(90.0 - 0.0)), Turn::Right); |
| 257 | // Heading south, leaving east: east is on the driver's left. |
| 258 | assert_eq!(classify_turn(normalise_degrees(90.0 - 180.0)), Turn::Left); |
| 259 | // The wrap is what makes that true rather than a 270-degree right. |
| 260 | assert_eq!(normalise_degrees(90.0 - 180.0), -90.0); |
| 261 | assert_eq!(normalise_degrees(350.0), -10.0); |
| 262 | assert_eq!(normalise_degrees(-350.0), 10.0); |
| 263 | assert_eq!(classify_turn(0.0), Turn::Through); |
| 264 | assert_eq!(classify_turn(-15.0), Turn::Through); |
| 265 | assert_eq!(classify_turn(30.0), Turn::SlightRight); |
| 266 | assert_eq!(classify_turn(-30.0), Turn::SlightLeft); |
| 267 | assert_eq!(classify_turn(150.0), Turn::SharpRight); |
| 268 | assert_eq!(classify_turn(-150.0), Turn::SharpLeft); |
| 269 | assert_eq!(classify_turn(179.0), Turn::Reverse); |
| 270 | } |
| 271 | |
| 272 | /// Headings come from `atan2` over a geometry step, because the graph stores none. |
| 273 | #[test] |
| 274 | fn a_heading_is_computed_from_the_geometry_and_corrected_for_latitude() { |
| 275 | let origin = (350_000_000, -1_200_000_000); |
| 276 | assert!((bearing_degrees(origin, (350_009_000, -1_200_000_000)) - 0.0).abs() < 0.01, "north"); |
| 277 | assert!((bearing_degrees(origin, (349_991_000, -1_200_000_000)) - 180.0).abs() < 0.01, "south"); |
| 278 | assert!((bearing_degrees(origin, (350_000_000, -1_199_988_000)) - 90.0).abs() < 0.01, "east"); |
| 279 | assert!((bearing_degrees(origin, (350_000_000, -1_200_012_000)) + 90.0).abs() < 0.01, "west"); |
| 280 | // Without the cos(lat) correction a due-northeast step at latitude 35 comes out at 45 |
| 281 | // degrees only by accident of the coordinate deltas; with it, equal *ground* distances do. |
| 282 | let north_m = 1000.0; |
| 283 | let east_m = 1000.0; |
| 284 | let (lon, lat) = offset_degrees(origin, east_m, north_m); |
| 285 | let to = ((lat * 1e7) as i32, (lon * 1e7) as i32); |
| 286 | assert!((bearing_degrees(origin, to) - 45.0).abs() < 0.05, "{}", bearing_degrees(origin, to)); |
| 287 | } |
| 288 | |
| 289 | /// A node nothing forks at emits nothing. The traffic layer's own square fixture is four nodes |
| 290 | /// of degree two or less, so it is exactly this case. |
| 291 | #[test] |
| 292 | fn a_chain_of_degree_two_nodes_produces_no_connectors() { |
| 293 | let coords: [(i32, i32); 3] = [ |
| 294 | (350_000_000, -1_200_000_000), |
| 295 | (350_000_000, -1_199_990_000), |
| 296 | (350_000_000, -1_199_980_000), |
| 297 | ]; |
| 298 | let edge = |source, target| EdgeSpec { source, target, type_: 7, interior: Vec::new() }; |
| 299 | let fixture = write_graph( |
| 300 | "junction_chain", |
| 301 | &coords, |
| 302 | &[edge(0, 1), edge(1, 0), edge(1, 2), edge(2, 1)], |
| 303 | &[], |
| 304 | ); |
| 305 | let (emitted, lines) = stream(&fixture); |
| 306 | assert_eq!(emitted, 0, "node 1 has two neighbours, so nothing turns there"); |
| 307 | assert!(lines.is_empty()); |
| 308 | } |
| 309 | |
| 310 | /// The inferred path, which is what the overwhelming majority of roads take. |
| 311 | /// |
| 312 | /// Four approaches, two turns each (through movements carry no connector — |
| 313 | /// the carriageway already paints the straight path): eight connectors. |
| 314 | /// The four outer nodes are degree one and contribute nothing, which is |
| 315 | /// what pins the count to node 0 alone. |
| 316 | #[test] |
| 317 | fn a_crossroads_with_no_tagged_lanes_infers_one_connector_per_legal_movement() { |
| 318 | let fixture = crossroads(&[]); |
| 319 | let (emitted, lines) = stream(&fixture); |
| 320 | assert_eq!(emitted, 8, "four approaches x two turns"); |
| 321 | assert_eq!(lines.len(), 8); |
| 322 | for line in &lines { |
| 323 | assert!(line.len() >= 2, "a connector needs at least two points"); |
| 324 | assert!(line.len() <= 14, "the sampler is bounded"); |
| 325 | for (lon, lat) in line { |
| 326 | assert!(lon.is_finite() && lat.is_finite()); |
| 327 | // Every connector stays inside the intersection box, which is two setbacks across. |
| 328 | assert!((lat - 35.0).abs() < 0.001, "{lat} ran outside the junction"); |
| 329 | assert!((lon + 120.0).abs() < 0.001, "{lon} ran outside the junction"); |
| 330 | } |
| 331 | } |
| 332 | // A turning connector is a sampled curve; a straight-through one need not be. |
| 333 | assert!( |
| 334 | lines.iter().any(|l| l.len() > 4), |
| 335 | "no connector was sampled as a curve, so the bezier is not being walked", |
| 336 | ); |
| 337 | } |
| 338 | |
| 339 | /// The data path: a tagged `turn:lanes` on one approach replaces the inference for that |
| 340 | /// approach only, and a shared `through;right` lane emits both of its movements. |
| 341 | #[test] |
| 342 | fn a_tagged_turn_lanes_approach_pairs_each_lane_with_its_own_exit() { |
| 343 | // Edge 4 is node1 -> node0, the approach from the north, arriving heading south. Its lanes, |
| 344 | // left to right: a left-turn lane, a through lane, and a shared through/right lane. |
| 345 | let masks = vec![ |
| 346 | LANE_LEFT, |
| 347 | LANE_THROUGH, |
| 348 | LANE_THROUGH | LANE_RIGHT, |
| 349 | ]; |
| 350 | let fixture = crossroads(&[(4u32, masks)]); |
| 351 | let (emitted, _) = stream(&fixture); |
| 352 | // The tagged approach: one left + one right (its two throughs are |
| 353 | // dropped). The other three approaches are untagged and infer two |
| 354 | // turns each = 6. |
| 355 | assert_eq!(emitted, 8, "the tagged approach keeps left and right only"); |
| 356 | } |
| 357 | |
| 358 | /// Spreading lanes across exits, both ways round. A dual left turn is two ribbons into one |
| 359 | /// road; a single lane onto a two-lane exit fans into both. |
| 360 | #[test] |
| 361 | fn lanes_and_exits_are_distributed_so_neither_side_is_left_unfed() { |
| 362 | assert_eq!(distribute(&[0, 1], &[7]), vec![(0, 7), (1, 7)], "a dual left turn"); |
| 363 | assert_eq!(distribute(&[3], &[5, 8]), vec![(3, 5), (3, 8)], "one lane fanning"); |
| 364 | assert_eq!(distribute(&[0, 1], &[4, 9]), vec![(0, 4), (1, 9)], "one to one"); |
| 365 | assert!(distribute(&[], &[1]).is_empty()); |
| 366 | assert!(distribute(&[1], &[]).is_empty()); |
| 367 | } |
| 368 | |
| 369 | /// A tagged indication no exit's own bucket matches still connects, to the closest exit by |
| 370 | /// angle — a threshold disagreement is far likelier than a lane that leads nowhere. |
| 371 | #[test] |
| 372 | fn a_tagged_turn_with_no_matching_exit_falls_back_to_the_closest_one() { |
| 373 | // Exits at a sharp right and straight on; nothing the classifier calls a plain right. |
| 374 | let legal = vec![(0usize, 150.0, Turn::SharpRight), (1usize, 5.0, Turn::Through)]; |
| 375 | assert_eq!(exits_for(Turn::Right, &legal), vec![0], "90 is nearer 150 than 5"); |
| 376 | assert_eq!(exits_for(Turn::Through, &legal), vec![1], "an exact bucket wins"); |
| 377 | assert!(exits_for(Turn::Left, &[]).is_empty()); |
| 378 | } |
| 379 | |
| 380 | /// Lane offsets, which are what stop every connector from stacking on the road's centreline. |
| 381 | #[test] |
| 382 | fn a_lane_sits_on_its_own_side_of_the_centreline() { |
| 383 | let w = LANE_WIDTH_MERCATOR_M; |
| 384 | // A one-way, two lanes: centred on the road, half a lane either side. |
| 385 | assert!((lane_offset_m(0, 2, false, false, w) + w / 2.0).abs() < 1e-9); |
| 386 | assert!((lane_offset_m(1, 2, false, false, w) - w / 2.0).abs() < 1e-9); |
| 387 | // Two-way, right-hand traffic: the whole carriageway sits right of the centreline, so both |
| 388 | // lanes are strictly positive and the inner one is half a lane out. |
| 389 | assert!((lane_offset_m(0, 2, true, false, w) - w * 0.5).abs() < 1e-9); |
| 390 | assert!((lane_offset_m(1, 2, true, false, w) - w * 1.5).abs() < 1e-9); |
| 391 | // Left-hand traffic mirrors it, and only it: lane order within the carriageway does not |
| 392 | // flip, because `lanes.bin` stores masks left-to-right in the direction of travel. |
| 393 | assert!((lane_offset_m(0, 2, true, true, w) + w * 1.5).abs() < 1e-9); |
| 394 | assert!((lane_offset_m(1, 2, true, true, w) + w * 0.5).abs() < 1e-9); |
| 395 | // A one-way is the same either side of the world: there is no opposing carriageway to sit |
| 396 | // beside. |
| 397 | assert_eq!(lane_offset_m(0, 3, false, true, w), lane_offset_m(0, 3, false, false, w)); |
| 398 | } |
| 399 | |
| 400 | /// **The latitude invariant, and the whole reason the offset is not baked in ground metres.** |
| 401 | /// |
| 402 | /// The renderer's lane width is a screen-space quantity, and Web Mercator carries a `1 / cos φ` |
| 403 | /// stretch, so a connector offset by a fixed *ground* distance drifts off the carriageway as |
| 404 | /// you go north — by 2.3x at 60°N, which is more than two lane widths. Offsetting by a fixed |
| 405 | /// *projected* distance is what makes it land, at every latitude and with no wire change. |
| 406 | /// |
| 407 | /// Measured end to end through the real encoder rather than on the helper, and asserted from |
| 408 | /// both sides: the projected spread must match, and the ground spread must not — otherwise the |
| 409 | /// test would still pass if the correction were quietly dropped. |
| 410 | #[test] |
| 411 | fn a_connectors_lateral_offset_is_the_same_projected_size_at_every_latitude() { |
| 412 | // The correction itself: a lane's ground width shrinks toward the poles exactly as fast as |
| 413 | // Mercator stretches it. |
| 414 | assert!((lane_width_ground_m(0.0) - LANE_WIDTH_MERCATOR_M).abs() < 1e-9); |
| 415 | assert!( |
| 416 | (lane_width_ground_m(60.0) - LANE_WIDTH_MERCATOR_M * 0.5).abs() < 1e-6, |
| 417 | "cos 60 is a half", |
| 418 | ); |
| 419 | |
| 420 | // The first two connectors are the north approach's turns, in |
| 421 | // `incoming.of(0)` order (through is dropped, so left + right remain). |
| 422 | // They share a setback and a heading, so the spread between their start |
| 423 | // points is purely the lane offset. |
| 424 | let spread = |lines: &[Vec<(f64, f64)>], project: &dyn Fn((f64, f64)) -> (f64, f64)| { |
| 425 | let starts: Vec<(f64, f64)> = lines[..2].iter().map(|l| project(l[0])).collect(); |
| 426 | let mut worst: f64 = 0.0; |
| 427 | for a in &starts { |
| 428 | for b in &starts { |
| 429 | worst = worst.max(((a.0 - b.0).powi(2) + (a.1 - b.1).powi(2)).sqrt()); |
| 430 | } |
| 431 | } |
| 432 | worst |
| 433 | }; |
| 434 | |
| 435 | // 0.0009 deg of latitude is ~100 m anywhere; a degree of longitude shrinks with cos, so the |
| 436 | // east/west arms need a wider step at 60 to stay the same distance out. |
| 437 | // |
| 438 | // The north approach is tagged with three lanes, because an untagged arm is one lane each |
| 439 | // way and one lane has no spread to measure. `turn:lanes` is the only real lane count this |
| 440 | // module can reach, so it is also the only fixture in which several connectors leave one |
| 441 | // arm at several different offsets — which is exactly what this test needs to compare. |
| 442 | let tagged = || (4u32, vec![LANE_LEFT, LANE_THROUGH, LANE_RIGHT]); |
| 443 | let (_, at_equator) = stream(&crossroads_at(0, 9_000, &[tagged()])); |
| 444 | let (_, at_sixty) = stream(&crossroads_at(600_000_000, 18_000, &[tagged()])); |
| 445 | // The tagged north approach keeps left + right (through dropped); the |
| 446 | // three untagged approaches keep two turns each: 2 + 6. |
| 447 | assert_eq!(at_equator.len(), 8); |
| 448 | assert_eq!(at_sixty.len(), 8); |
| 449 | |
| 450 | // In projected units — what the renderer actually draws — the two must agree. z20 is an |
| 451 | // arbitrary scale; Mercator is self-similar, so any zoom gives the same ratio. |
| 452 | let mercator = |(lon, lat): (f64, f64)| tile_build::geom::project(lon, lat, 20); |
| 453 | let projected_equator = spread(&at_equator, &mercator); |
| 454 | let projected_sixty = spread(&at_sixty, &mercator); |
| 455 | assert!(projected_equator > 0.0, "the lanes did not separate at all"); |
| 456 | assert!( |
| 457 | (projected_sixty / projected_equator - 1.0).abs() < 0.02, |
| 458 | "a lane must be the same projected width everywhere: {projected_equator} at the \ |
| 459 | equator vs {projected_sixty} at 60N", |
| 460 | ); |
| 461 | |
| 462 | // And in ground units they must differ by cos 60, which is what proves the correction is |
| 463 | // being applied rather than the assertion above being vacuous. A degree of longitude is |
| 464 | // `cos φ` shorter on the ground, and the north approach's offset is purely east-west. |
| 465 | let ground = |(lon, lat): (f64, f64)| { |
| 466 | (lon * METRES_PER_DEGREE * lat.to_radians().cos(), lat * METRES_PER_DEGREE) |
| 467 | }; |
| 468 | let ground_ratio = spread(&at_sixty, &ground) / spread(&at_equator, &ground); |
| 469 | assert!( |
| 470 | (ground_ratio - 0.5).abs() < 0.02, |
| 471 | "a lane's ground width should halve by 60N, not stay put: ratio {ground_ratio}", |
| 472 | ); |
| 473 | } |
| 474 | |
| 475 | /// Which lane of the exit a connector lands in. |
| 476 | /// |
| 477 | /// The single-lane cases pass `rank` 0 of `siblings` 1, which is what a movement only one |
| 478 | /// approach lane makes looks like, and reduce to "the exit's leftmost" or "its rightmost". |
| 479 | #[test] |
| 480 | fn a_turn_lands_in_the_exit_lane_its_direction_implies() { |
| 481 | assert_eq!(exit_lane(Turn::Left, 0, 3, 2, 0, 1), 0, "a left turn takes the exit's left lane"); |
| 482 | assert_eq!(exit_lane(Turn::SharpLeft, 2, 3, 4, 0, 1), 0); |
| 483 | assert_eq!(exit_lane(Turn::Right, 2, 3, 3, 0, 1), 2, "a right turn takes the rightmost"); |
| 484 | assert_eq!(exit_lane(Turn::Through, 0, 2, 2, 0, 1), 0, "a through movement holds its place"); |
| 485 | assert_eq!(exit_lane(Turn::Through, 1, 2, 2, 0, 1), 1); |
| 486 | // Narrowing: the outermost lane cannot land past the exit's last. |
| 487 | assert_eq!(exit_lane(Turn::Through, 3, 4, 2, 0, 1), 1); |
| 488 | // A zero-lane exit cannot underflow. |
| 489 | assert_eq!(exit_lane(Turn::Right, 0, 1, 0, 0, 1), 0); |
| 490 | // Several lanes making one movement rank across the exit rather than stacking on one lane; |
| 491 | // `a_movement_shared_by_several_lanes_fills_the_exit_from_its_own_side` owns that rule. |
| 492 | // What is pinned here is only that the rank cannot run off the far side of the road. |
| 493 | assert_eq!(exit_lane(Turn::Left, 2, 3, 2, 2, 3), 1); |
| 494 | assert_eq!(exit_lane(Turn::Right, 0, 3, 2, 0, 3), 0); |
| 495 | assert_eq!(exit_lane(Turn::Left, 1, 3, 1, 1, 2), 0); |
| 496 | assert_eq!(exit_lane(Turn::Right, 1, 3, 1, 0, 2), 0); |
| 497 | assert_eq!(exit_lane(Turn::Right, 2, 3, 1, 1, 2), 0); |
| 498 | } |
| 499 | |
| 500 | } |