Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / scripts / maps / mamaps_build / src / lanefill_part1.rs

lanefill_part1.rs303 lines · 14.1 KiBRaw
1fn polyline_length_m(line: &[(f64, f64)]) -> f64 {
2 line.windows(2)
3 .map(|p| {
4 let north = (p[1].1 - p[0].1) * METRES_PER_DEGREE;
5 let east = (p[1].0 - p[0].0) * METRES_PER_DEGREE * p[0].1.to_radians().cos();
6 (north * north + east * east).sqrt()
7 })
8 .sum()
9}
10
11#[cfg(test)]
12mod tests {
13 use super::*;
14
15 /// Broadway × Rollins Road, Burlingame — the junction this module was written for.
16 const JUNCTION: (f64, f64) = (-122.3622, 37.5889);
17
18 fn offset(east_m: f64, north_m: f64) -> (f64, f64) {
19 (
20 JUNCTION.0 + east_m / (METRES_PER_DEGREE * JUNCTION.1.to_radians().cos()),
21 JUNCTION.1 + north_m / METRES_PER_DEGREE,
22 )
23 }
24
25 /// A straight way between two points on the local tangent plane, in metres east/north of the
26 /// junction. `nodes` are given so tests can decide what touches what.
27 struct Straight {
28 nodes: [i64; 2],
29 line: [(f64, f64); 2],
30 }
31
32 fn straight(from_node: i64, from: (f64, f64), to_node: i64, to: (f64, f64)) -> Straight {
33 Straight {
34 nodes: [from_node, to_node],
35 line: [offset(from.0, from.1), offset(to.0, to.1)],
36 }
37 }
38
39 fn segment<'a>(
40 id: i64,
41 name: &'a str,
42 lanes: u8,
43 oneway: bool,
44 s: &'a Straight,
45 ) -> Segment<'a> {
46 Segment { id, name, lanes, oneway, nodes: &s.nodes, line: &s.line }
47 }
48
49 fn scratch(name: &str) -> PathBuf {
50 std::env::temp_dir().join(format!("mamaps_lanefill_{}_{name}", std::process::id()))
51 }
52
53 /// Push every segment and resolve. Takes the segments as a closure so the borrow of the
54 /// `Straight`s outlives nothing awkward.
55 fn run(name: &str, push: impl FnOnce(&mut Collector) -> Result<()>) -> Vec<(i64, u8)> {
56 let mut collector = Collector::create(&scratch(name)).expect("partitions");
57 push(&mut collector).expect("push");
58 collector.finish().expect("resolve")
59 }
60
61 /// THE case this module exists for, to the real geometry: Rollins Road runs east-west across
62 /// Broadway's two carriageways, is tagged 6 lanes on one side and 4 on the other, and the
63 /// nineteen metres between the carriageways carries no `lanes` tag at all. Way 417329335.
64 #[test]
65 fn the_stub_between_two_carriageways_takes_the_narrower_neighbours_lane_count() {
66 let west = straight(1, (-120.0, 0.0), 2, (-10.0, 0.0));
67 let stub = straight(2, (-10.0, 0.0), 3, (9.6, 0.0));
68 let east = straight(3, (9.6, 0.0), 4, (120.0, 0.0));
69 let out = run("reported", |c| {
70 c.push(&segment(417_329_340, "Rollins Road", 6, false, &west))?;
71 c.push(&segment(417_329_335, "Rollins Road", 0, false, &stub))?;
72 c.push(&segment(504_486_258, "Rollins Road", 4, false, &east))
73 });
74 assert_eq!(
75 out,
76 vec![(417_329_335, 4)],
77 "the stub inherits 4 - the minimum of its 6 and 4 neighbours - against a default of 2",
78 );
79 }
80
81 /// The one condition that is not a heuristic. A way that states its own lane count keeps it,
82 /// whatever its neighbours say, so this pass can never contradict a survey.
83 #[test]
84 fn a_way_that_carries_a_lanes_tag_is_never_touched() {
85 let west = straight(1, (-120.0, 0.0), 2, (-10.0, 0.0));
86 let middle = straight(2, (-10.0, 0.0), 3, (10.0, 0.0));
87 let east = straight(3, (10.0, 0.0), 4, (120.0, 0.0));
88 let out = run("tagged", |c| {
89 c.push(&segment(10, "Rollins Road", 6, false, &west))?;
90 // Tagged 3, between neighbours tagged 6. Its own tag wins.
91 c.push(&segment(11, "Rollins Road", 3, false, &middle))?;
92 c.push(&segment(12, "Rollins Road", 6, false, &east))
93 });
94 assert!(out.is_empty(), "a tagged way is not a recipient: {out:?}");
95 }
96
97 /// A donor at one end is a road that happens to end here, not a road this way interrupts.
98 /// Without both ends, every untagged street leaving a tagged one would widen.
99 #[test]
100 fn one_donor_is_not_enough() {
101 let west = straight(1, (-120.0, 0.0), 2, (-10.0, 0.0));
102 let stub = straight(2, (-10.0, 0.0), 3, (10.0, 0.0));
103 let out = run("oneend", |c| {
104 c.push(&segment(10, "Rollins Road", 6, false, &west))?;
105 c.push(&segment(11, "Rollins Road", 0, false, &stub))
106 });
107 assert!(out.is_empty(), "no donor at the far end: {out:?}");
108 }
109
110 /// Collinearity alone is not the same road. A road changing its name at a crossroads is
111 /// ordinary, and inheriting across the change would take a width from a different street.
112 #[test]
113 fn a_collinear_neighbour_with_another_name_is_a_different_road() {
114 let west = straight(1, (-120.0, 0.0), 2, (-10.0, 0.0));
115 let stub = straight(2, (-10.0, 0.0), 3, (10.0, 0.0));
116 let east = straight(3, (10.0, 0.0), 4, (120.0, 0.0));
117 let out = run("names", |c| {
118 c.push(&segment(10, "Rollins Road", 6, false, &west))?;
119 c.push(&segment(11, "Rollins Road", 0, false, &stub))?;
120 c.push(&segment(12, "Carolan Avenue", 6, false, &east))
121 });
122 assert!(out.is_empty(), "the east arm is a different street: {out:?}");
123 }
124
125 /// The name alone is not the same road either: a street turning a right angle at a junction is
126 /// a different carriageway, and one of the two is very often the wider.
127 #[test]
128 fn a_same_named_neighbour_round_a_hard_corner_is_a_different_carriageway() {
129 let west = straight(1, (-120.0, 0.0), 2, (-10.0, 0.0));
130 let stub = straight(2, (-10.0, 0.0), 3, (10.0, 0.0));
131 // Same name, touching the stub's east end, but leaving due north.
132 let north = straight(3, (10.0, 0.0), 4, (10.0, 120.0));
133 let out = run("corner", |c| {
134 c.push(&segment(10, "Rollins Road", 6, false, &west))?;
135 c.push(&segment(11, "Rollins Road", 0, false, &stub))?;
136 c.push(&segment(12, "Rollins Road", 6, false, &north))
137 });
138 assert!(out.is_empty(), "a 90-degree turn is not a continuation: {out:?}");
139 }
140
141 /// `lanes` counts both directions, so a two-way six inherited onto a one-way stub would paint
142 /// six lanes running one way where the truth is about three. A doubling, not a rounding.
143 #[test]
144 fn a_one_way_stub_does_not_inherit_from_a_two_way_road() {
145 let west = straight(1, (-120.0, 0.0), 2, (-10.0, 0.0));
146 let stub = straight(2, (-10.0, 0.0), 3, (10.0, 0.0));
147 let east = straight(3, (10.0, 0.0), 4, (120.0, 0.0));
148 let out = run("oneway", |c| {
149 c.push(&segment(10, "Rollins Road", 6, false, &west))?;
150 c.push(&segment(11, "Rollins Road", 0, true, &stub))?;
151 c.push(&segment(12, "Rollins Road", 6, false, &east))
152 });
153 assert!(out.is_empty(), "the donors are two-way and the stub is not: {out:?}");
154 }
155
156 /// The bound is what separates "the pavement inside a junction" from "a road". Without it,
157 /// hundreds of metres of genuinely untagged road silently take a neighbour's width.
158 #[test]
159 fn a_road_too_long_to_be_a_junction_stub_does_not_inherit() {
160 let west = straight(1, (-400.0, 0.0), 2, (-150.0, 0.0));
161 let long = straight(2, (-150.0, 0.0), 3, (150.0, 0.0));
162 let east = straight(3, (150.0, 0.0), 4, (400.0, 0.0));
163 assert!(
164 polyline_length_m(&long.line) > MAX_STUB_M,
165 "the fixture has to be longer than the bound to test it",
166 );
167 let out = run("toolong", |c| {
168 c.push(&segment(10, "Rollins Road", 6, false, &west))?;
169 c.push(&segment(11, "Rollins Road", 0, false, &long))?;
170 c.push(&segment(12, "Rollins Road", 6, false, &east))
171 });
172 assert!(out.is_empty(), "300 m is a road, not a junction stub: {out:?}");
173 }
174
175 /// A donor must carry a real tag, so a width travels exactly one way and stops. Two untagged
176 /// stubs in a row leaves each with a tagged donor at one end only, and neither inherits --
177 /// which is what makes the blast radius a property of the rule rather than a limit to police.
178 #[test]
179 fn an_inherited_count_never_becomes_a_donor() {
180 let west = straight(1, (-120.0, 0.0), 2, (-30.0, 0.0));
181 let first = straight(2, (-30.0, 0.0), 3, (0.0, 0.0));
182 let second = straight(3, (0.0, 0.0), 4, (30.0, 0.0));
183 let east = straight(4, (30.0, 0.0), 5, (120.0, 0.0));
184 let out = run("chain", |c| {
185 c.push(&segment(10, "Rollins Road", 6, false, &west))?;
186 c.push(&segment(11, "Rollins Road", 0, false, &first))?;
187 c.push(&segment(12, "Rollins Road", 0, false, &second))?;
188 c.push(&segment(13, "Rollins Road", 6, false, &east))
189 });
190 assert!(out.is_empty(), "neither stub has a tagged donor at both ends: {out:?}");
191 }
192
193 /// Where the answer is the answer the default would have given, say nothing. Keeps the lookup
194 /// table to the ways that actually move, the way `corridor::promote` does.
195 #[test]
196 fn a_stub_that_would_inherit_its_own_default_is_left_out() {
197 let west = straight(1, (-120.0, 0.0), 2, (-10.0, 0.0));
198 let stub = straight(2, (-10.0, 0.0), 3, (10.0, 0.0));
199 let east = straight(3, (10.0, 0.0), 4, (120.0, 0.0));
200 let out = run("noop", |c| {
201 c.push(&segment(10, "Rollins Road", 2, false, &west))?;
202 c.push(&segment(11, "Rollins Road", 0, false, &stub))?;
203 c.push(&segment(12, "Rollins Road", 2, false, &east))
204 });
205 assert!(out.is_empty(), "two lanes is what the default already draws: {out:?}");
206 }
207
208 /// The archive has to be byte-identical however the build is run, so the resolution cannot
209 /// depend on the order ways arrived in -- which, with the records split across partitions and
210 /// sorted inside them, is the property worth pinning rather than assuming.
211 #[test]
212 fn the_result_does_not_depend_on_the_order_ways_were_offered() {
213 let west = straight(1, (-120.0, 0.0), 2, (-10.0, 0.0));
214 let stub = straight(2, (-10.0, 0.0), 3, (10.0, 0.0));
215 let east = straight(3, (10.0, 0.0), 4, (120.0, 0.0));
216 let forward = run("order_a", |c| {
217 c.push(&segment(10, "Rollins Road", 6, false, &west))?;
218 c.push(&segment(11, "Rollins Road", 0, false, &stub))?;
219 c.push(&segment(12, "Rollins Road", 5, false, &east))
220 });
221 let backward = run("order_b", |c| {
222 c.push(&segment(12, "Rollins Road", 5, false, &east))?;
223 c.push(&segment(11, "Rollins Road", 0, false, &stub))?;
224 c.push(&segment(10, "Rollins Road", 6, false, &west))
225 });
226 assert_eq!(forward, backward);
227 assert_eq!(forward, vec![(11, 5)], "the minimum of 6 and 5");
228 }
229
230 /// A closed way has one endpoint rather than two, so "a donor at both ends" is not a question
231 /// that can be asked of it, and a loop is not a junction stub in any case.
232 #[test]
233 fn a_closed_way_is_not_a_stub() {
234 let ring_nodes = [7i64, 7];
235 let ring_line = [offset(0.0, 0.0), offset(0.0, 0.0)];
236 let arm = straight(7, (0.0, 0.0), 8, (120.0, 0.0));
237 let out = run("closed", |c| {
238 c.push(&Segment {
239 id: 11,
240 name: "Rollins Road",
241 lanes: 0,
242 oneway: false,
243 nodes: &ring_nodes,
244 line: &ring_line,
245 })?;
246 c.push(&segment(10, "Rollins Road", 6, false, &arm))
247 });
248 assert!(out.is_empty(), "{out:?}");
249 }
250
251 /// A way whose extract cut some of its nodes away has coordinates that no longer line up with
252 /// its refs, so its endpoints are not where they claim to be.
253 #[test]
254 fn a_way_whose_geometry_is_short_of_its_refs_is_skipped() {
255 let mut collector = Collector::create(&scratch("cut")).expect("partitions");
256 let nodes = [1i64, 2, 3];
257 let line = [offset(-10.0, 0.0), offset(10.0, 0.0)];
258 collector
259 .push(&Segment {
260 id: 11,
261 name: "Rollins Road",
262 lanes: 0,
263 oneway: false,
264 nodes: &nodes,
265 line: &line,
266 })
267 .expect("push");
268 assert!(collector.finish().expect("resolve").is_empty());
269 }
270
271 /// Both bearings point away from the node they share, so a straight-through road scores zero
272 /// and the measure has to wrap correctly at north.
273 #[test]
274 fn deviation_is_zero_for_a_straight_continuation_and_wraps_at_north() {
275 assert!(deviation(90.0, -90.0).abs() < 1e-9, "east meeting west runs straight through");
276 assert!(deviation(10.0, -170.0).abs() < 1e-9, "and so does this, across the wrap");
277 assert!((deviation(0.0, 0.0) - 180.0).abs() < 1e-9, "a doubling back is the far extreme");
278 assert!((deviation(0.0, 90.0) - 90.0).abs() < 1e-9, "a right angle is 90 from straight");
279 // Symmetric, because neither way of the pair is privileged.
280 assert!((deviation(35.0, -120.0) - deviation(-120.0, 35.0)).abs() < 1e-9);
281 }
282
283 /// Peak memory is one partition, so an uneven spread is the thing that breaks. The hazard is
284 /// not a run of consecutive ids -- those spread evenly under a plain modulo -- but a *strided*
285 /// one, which a modulo folds onto a single partition. Node ids are handed out by editors and
286 /// import scripts, so a stride sharing a factor with [`PARTITIONS`] is not exotic.
287 #[test]
288 fn a_strided_run_of_node_ids_still_spreads_across_the_partitions() {
289 let mut hit = vec![0u32; PARTITIONS];
290 for k in 0..100_000i64 {
291 hit[partition_of(1_000_000_000 + k * PARTITIONS as i64)] += 1;
292 }
293 assert_eq!(
294 hit.iter().filter(|n| **n > 0).count(),
295 PARTITIONS,
296 "a plain modulo would have put all 100,000 in one partition",
297 );
298 let (low, high) = (*hit.iter().min().unwrap(), *hit.iter().max().unwrap());
299 // 100,000 over 256 averages 390. Within a factor of two of each other is far tighter than
300 // a fold-onto-one failure and loose enough not to pin the hash's exact output.
301 assert!(high < low * 2, "the spread is even: {low}..{high}");
302 }
303}