Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / schema / junction_part4.rs
junction_part4.rs276 lines · 13.2 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod tests2 { |
| 3 | use super::*; |
| 4 | use super::tests::{crossroads, stream}; |
| 5 | use crate::schema::traffic_extra::tests::{write_graph, EdgeSpec, GraphFixture}; |
| 6 | |
| 7 | /// A movement several approach lanes share stays several ribbons wide, which is the whole |
| 8 | /// reason [`exit_lane`] takes a rank at all: without it a dual left turn puts both its lanes on |
| 9 | /// the exit's leftmost and draws one connector twice on top of itself. |
| 10 | /// |
| 11 | /// This pins the **direction each side fills from**, which the clamped cases above cannot: they |
| 12 | /// all saturate, so they would still pass if left and right filled the same way. A dual left |
| 13 | /// takes the exit's two leftmost lanes and a dual right its two rightmost, and the difference |
| 14 | /// between those is the thing worth defending. |
| 15 | #[test] |
| 16 | fn a_movement_shared_by_several_lanes_fills_the_exit_from_its_own_side() { |
| 17 | // Two left-turn lanes into a three-lane exit: the exit's left two, in order. |
| 18 | assert_eq!(exit_lane(Turn::Left, 0, 3, 3, 0, 2), 0); |
| 19 | assert_eq!(exit_lane(Turn::Left, 1, 3, 3, 1, 2), 1); |
| 20 | // Two right-turn lanes into the same exit: the right two. Lanes 1 and 2, not 0 and 1 — |
| 21 | // the outermost approach lane takes the outermost exit lane, and a right turn that fed |
| 22 | // the exit's left lane would cross the traffic beside it. |
| 23 | assert_eq!(exit_lane(Turn::Right, 1, 3, 3, 0, 2), 1); |
| 24 | assert_eq!(exit_lane(Turn::Right, 2, 3, 3, 1, 2), 2); |
| 25 | // The two sides genuinely disagree at the same rank, which is what makes the rule a rule |
| 26 | // rather than a shared clamp. |
| 27 | assert_ne!( |
| 28 | exit_lane(Turn::Left, 0, 3, 3, 0, 2), |
| 29 | exit_lane(Turn::Right, 1, 3, 3, 0, 2), |
| 30 | "left and right must fill from opposite ends of the exit", |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | /// `lanes.bin` is sparse, so absence is the answer for most edges and a missing file is not an |
| 35 | /// error at all. |
| 36 | #[test] |
| 37 | fn a_sparse_lane_table_answers_only_for_the_edges_it_holds() { |
| 38 | let fixture = crossroads(&[(1u32, vec![LANE_THROUGH]), (4u32, vec![LANE_LEFT, LANE_RIGHT])]); |
| 39 | let table = LaneTable::load(&fixture.dir).expect("load").expect("a file was written"); |
| 40 | assert_eq!(table.masks(1), Some(vec![LANE_THROUGH])); |
| 41 | assert_eq!(table.masks(4), Some(vec![LANE_LEFT, LANE_RIGHT])); |
| 42 | assert_eq!(table.masks(0), None, "edge 0 is not in the index"); |
| 43 | assert_eq!(table.masks(3), None); |
| 44 | assert_eq!(table.masks(99), None, "past every entry"); |
| 45 | |
| 46 | let bare = crossroads(&[]); |
| 47 | assert!( |
| 48 | LaneTable::load(&bare.dir).expect("load").is_none(), |
| 49 | "a graph built without lane data is not an error", |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | /// The reverse index, which is the whole reason an approach can be found: `nodes.bin` is |
| 54 | /// out-edges only. |
| 55 | #[test] |
| 56 | fn incoming_edges_are_recovered_from_a_graph_that_stores_only_outgoing_ones() { |
| 57 | let fixture = crossroads(&[]); |
| 58 | let graph = Graph::load(&fixture.dir).expect("graph"); |
| 59 | let incoming = InEdges::build(&graph).expect("reverse index"); |
| 60 | // Edges 4..7 all target the junction. |
| 61 | assert_eq!(incoming.of(0), &[4, 5, 6, 7]); |
| 62 | // Each outer node is fed by exactly the one edge leaving the junction toward it. |
| 63 | assert_eq!(incoming.of(1), &[0]); |
| 64 | assert_eq!(incoming.of(4), &[3]); |
| 65 | // And every in-edge's source is recoverable from the same `edge_ptr` array. |
| 66 | assert_eq!(source_of(&graph, 4), 1); |
| 67 | assert_eq!(source_of(&graph, 7), 4); |
| 68 | assert_eq!(source_of(&graph, 0), 0); |
| 69 | assert_eq!(source_of(&graph, 3), 0); |
| 70 | } |
| 71 | |
| 72 | /// **A divided highway's median U-turn slot, which is the case the neighbour test cannot see.** |
| 73 | /// |
| 74 | /// Node 1 is the approaching carriageway and node 2 the opposing one running alongside it, so a |
| 75 | /// reversal at node 0 leaves toward a *different* node and survives |
| 76 | /// `exit.neighbour != approach.neighbour`. The median is ~9 m, which puts the exit 5 degrees off |
| 77 | /// due north and the movement at 175 degrees — inside [`classify_turn`]'s 170-degree reversal |
| 78 | /// threshold, and a realistic width rather than one tuned to just clear it. |
| 79 | fn divided_highway() -> GraphFixture { |
| 80 | let coords: [(i32, i32); 4] = [ |
| 81 | (350_000_000, -1_200_000_000), // 0: the junction |
| 82 | (350_009_000, -1_200_000_000), // 1: north, the carriageway traffic arrives on |
| 83 | (350_009_000, -1_199_999_000), // 2: north, the opposing carriageway across the median |
| 84 | (350_000_000, -1_199_988_000), // 3: east, so the node has three neighbours |
| 85 | ]; |
| 86 | let edge = |source, target| EdgeSpec { source, target, type_: 3, interior: Vec::new() }; |
| 87 | write_graph( |
| 88 | "junction_divided", |
| 89 | &coords, |
| 90 | &[edge(0, 2), edge(0, 3), edge(1, 0), edge(3, 0)], |
| 91 | &[], |
| 92 | ) |
| 93 | } |
| 94 | |
| 95 | /// **A reversal onto a different node is not a legal movement, and used to draw a fishhook.** |
| 96 | /// |
| 97 | /// `exit.neighbour != approach.neighbour` excludes the U-turn back the way you came and nothing |
| 98 | /// else, so a 180-degree movement reaching a different node came through as legal. Its geometry |
| 99 | /// is degenerate rather than merely ugly: with `f_out = -f_in` the four control points along |
| 100 | /// the approach's forward axis are `-back`, `-0.45*back`, `-0.45*ahead`, `-ahead` — every one |
| 101 | /// negative, so the whole curve stays behind the node and never enters the junction. At the |
| 102 | /// usual 14 m setback its apex sits 8.2 m short of the node while it swings half a lane either |
| 103 | /// side of the centreline: a flat hook doubling back along the approach. |
| 104 | /// |
| 105 | /// Pinned on both the count and the shape. The count alone would pass if the reversal were |
| 106 | /// merely redirected somewhere, and the shape alone would pass if it were dropped for an |
| 107 | /// unrelated reason. |
| 108 | #[test] |
| 109 | fn a_reversal_onto_a_different_node_is_not_emitted() { |
| 110 | let (emitted, lines) = stream(÷d_highway()); |
| 111 | // Three movements reach the legality filter: from the north, the reversal onto the opposing |
| 112 | // carriageway and the left onto the east arm; from the east, the right onto the opposing |
| 113 | // carriageway. Only the reversal is dropped. |
| 114 | assert_eq!(emitted, 2, "the reversal onto the opposing carriageway must not be emitted"); |
| 115 | assert_eq!(lines.len(), 2); |
| 116 | |
| 117 | // And no survivor doubles back. Both ends of the fishhook sat ~14 m up the approach; a |
| 118 | // connector that genuinely crosses the junction has at most one end far up any single arm. |
| 119 | // The threshold is well clear of the half-lane lateral offset a legitimate end carries. |
| 120 | let north_m = |p: (f64, f64)| (p.1 - 35.0) * METRES_PER_DEGREE; |
| 121 | for line in &lines { |
| 122 | let (start, end) = (line[0], *line.last().expect("a connector has points")); |
| 123 | assert!( |
| 124 | !(north_m(start) > 5.0 && north_m(end) > 5.0), |
| 125 | "a connector runs from {:.1} m to {:.1} m north of the junction, both ends up the \ |
| 126 | same arm: it doubles back along the approach instead of crossing the node", |
| 127 | north_m(start), |
| 128 | north_m(end), |
| 129 | ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /// A one-way approach is the case [`InEdges`] exists for: the junction has no out-edge back |
| 134 | /// toward it, so [`Graph::has_drivable_twin`] could never find it. |
| 135 | #[test] |
| 136 | fn a_one_way_approach_is_found_even_though_nothing_leaves_the_junction_toward_it() { |
| 137 | let coords: [(i32, i32); 4] = [ |
| 138 | (350_000_000, -1_200_000_000), // 0: the junction |
| 139 | (350_009_000, -1_200_000_000), // 1: north, feeds in one-way |
| 140 | (350_000_000, -1_199_988_000), // 2: east |
| 141 | (349_991_000, -1_200_000_000), // 3: south |
| 142 | ]; |
| 143 | let edge = |source, target| EdgeSpec { source, target, type_: 3, interior: Vec::new() }; |
| 144 | let fixture = write_graph( |
| 145 | "junction_oneway", |
| 146 | &coords, |
| 147 | &[ |
| 148 | // The junction leaves only east and south. Nothing runs back north. |
| 149 | edge(0, 2), |
| 150 | edge(0, 3), |
| 151 | // The one-way approach, plus two-way arms so the node has three neighbours. |
| 152 | edge(1, 0), |
| 153 | edge(2, 0), |
| 154 | edge(3, 0), |
| 155 | ], |
| 156 | &[], |
| 157 | ); |
| 158 | let graph = Graph::load(&fixture.dir).expect("graph"); |
| 159 | assert!( |
| 160 | !graph.has_drivable_twin(0, 1).expect("twin"), |
| 161 | "the fixture is only meaningful if nothing leaves the junction northward", |
| 162 | ); |
| 163 | let (emitted, _) = stream(&fixture); |
| 164 | // Three approaches. From the north: east and south are both legal (2). |
| 165 | // From the east: south only, since the U-turn back east is excluded and |
| 166 | // nothing runs north (1). From the south: east only (1). One of the |
| 167 | // four is a through movement and carries no connector. |
| 168 | assert_eq!(emitted, 3, "the one-way approach contributes its two turns"); |
| 169 | } |
| 170 | |
| 171 | /// A three-way node with one arm 30° off: north, south, and an arm |
| 172 | /// bearing ~030°. `edge_type` is the caller's to choose, so the arterial |
| 173 | /// gate and the slight-turn rule can be pinned on identical geometry. |
| 174 | fn tee_with_slight_arm(edge_type: u8) -> GraphFixture { |
| 175 | let coords: [(i32, i32); 4] = [ |
| 176 | (350_000_000, -1_200_000_000), // 0: the junction |
| 177 | (350_009_000, -1_200_000_000), // 1: north, ~100 m |
| 178 | (349_991_000, -1_200_000_000), // 2: south, ~100 m |
| 179 | (350_007_779, -1_199_994_517), // 3: ~30° east of north, ~100 m |
| 180 | ]; |
| 181 | let edge = |source, target| EdgeSpec { source, target, type_: edge_type, interior: Vec::new() }; |
| 182 | write_graph( |
| 183 | "junction_tee", |
| 184 | &coords, |
| 185 | &[ |
| 186 | edge(0, 1), |
| 187 | edge(0, 2), |
| 188 | edge(0, 3), |
| 189 | edge(1, 0), |
| 190 | edge(2, 0), |
| 191 | edge(3, 0), |
| 192 | ], |
| 193 | &[], |
| 194 | ) |
| 195 | } |
| 196 | |
| 197 | /// **Slight turns at a degree-3 node are road bends, not turns.** |
| 198 | /// |
| 199 | /// From the south approach the 30° arm is a slight right, and from that |
| 200 | /// arm south is a slight left — both dropped. The sharp pair |
| 201 | /// (north ↔ 30° arm, ±150°) survives. Two connectors, not four. |
| 202 | #[test] |
| 203 | fn a_slight_turn_at_a_three_way_node_is_not_emitted() { |
| 204 | let (emitted, lines) = stream(&tee_with_slight_arm(3)); |
| 205 | assert_eq!(emitted, 2, "only the sharp pair survives at degree 3"); |
| 206 | assert_eq!(lines.len(), 2); |
| 207 | } |
| 208 | |
| 209 | /// **The same geometry at degree 4 keeps everything.** The crossroads |
| 210 | /// fixture's arms leave at 90°, so no movement is slight — but the pin |
| 211 | /// matters in the other direction: the rule must not fire where it |
| 212 | /// should not. |
| 213 | #[test] |
| 214 | fn a_four_way_node_keeps_all_of_its_turns() { |
| 215 | let (emitted, _) = stream(&crossroads(&[])); |
| 216 | assert_eq!(emitted, 8, "four approaches x two turns, none slight"); |
| 217 | } |
| 218 | |
| 219 | /// **A junction no arterial touches emits nothing.** Identical geometry |
| 220 | /// to the tee above, but every arm residential: the whole node goes |
| 221 | /// quiet rather than a subset of its movements. |
| 222 | #[test] |
| 223 | fn a_junction_with_no_arterial_arm_emits_nothing() { |
| 224 | let (emitted, lines) = stream(&tee_with_slight_arm(7)); |
| 225 | assert_eq!(emitted, 0, "no arm is tertiary or better"); |
| 226 | assert!(lines.is_empty()); |
| 227 | } |
| 228 | |
| 229 | /// **Connectors sample at roughly half the old density.** A 90° turn |
| 230 | /// used to walk 9 points (`4 + 90/18`); now it walks 6 (`3 + 90/36`). |
| 231 | #[test] |
| 232 | fn a_right_angle_connector_is_sampled_sparsely_but_still_a_curve() { |
| 233 | let (_, lines) = stream(&crossroads(&[])); |
| 234 | assert!(!lines.is_empty()); |
| 235 | for line in &lines { |
| 236 | assert_eq!(line.len(), 6, "every crossroads turn is 90°: 3 + 90/36 rounds to 6"); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /// Left-hand traffic mirrors the connectors rather than leaving them on the wrong side. |
| 241 | #[test] |
| 242 | fn a_left_hand_traffic_country_gets_its_connectors_on_the_other_side() { |
| 243 | let fixture = crossroads(&[]); |
| 244 | let spill = fixture.dir.join("lht.tmp"); |
| 245 | |
| 246 | let mut left = Conventions::default(); |
| 247 | // A ring around the fixture's coordinates, so its z6 cell is claimed for a left-hand |
| 248 | // country. `add` takes lon/lat rings, outer first. |
| 249 | left.add( |
| 250 | "GB", |
| 251 | &[vec![vec![ |
| 252 | (-121.0, 34.0), |
| 253 | (-119.0, 34.0), |
| 254 | (-119.0, 36.0), |
| 255 | (-121.0, 36.0), |
| 256 | (-121.0, 34.0), |
| 257 | ]]], |
| 258 | ); |
| 259 | |
| 260 | let mut sink = Sink::create(&spill).expect("sink"); |
| 261 | stream_junctions(&fixture.dir, &left, &mut sink).expect("stream"); |
| 262 | let store = sink.finish(&spill).expect("finish"); |
| 263 | let mut reader = store.reader().expect("reader"); |
| 264 | let mut mirrored = Vec::new(); |
| 265 | while let Some(feature) = reader.next().expect("read") { |
| 266 | if let Geometry::Lines(parts) = &feature.geometry { |
| 267 | mirrored.push(parts[0].clone()); |
| 268 | } |
| 269 | } |
| 270 | let _ = std::fs::remove_file(&spill); |
| 271 | |
| 272 | let (_, right) = stream(&fixture); |
| 273 | assert_eq!(mirrored.len(), right.len(), "the same movements exist either side of the world"); |
| 274 | assert_ne!(mirrored, right, "left-hand traffic must not draw the right-hand connectors"); |
| 275 | } |
| 276 | } |