Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / coalesce_extra.rs
coalesce_extra.rs340 lines · 14.9 KiBRaw
| 1 | /// The spans of one continuous polyline, in the order they join. |
| 2 | pub(crate) type Run = Vec<crate::coalesce::Span>; |
| 3 | |
| 4 | #[cfg(test)] |
| 5 | mod tests { |
| 6 | use crate::coalesce::{coalesce_lines, coalesce_lines_with_ids, points_of, Stats}; |
| 7 | use tilecodec::mamaps::body::{Carriageway, Feature, Layer, Part, GEOM_LINE, GEOM_POLYGON, WINDING_OUTER}; |
| 8 | |
| 9 | /// One fixture feature: `(kind, geom_type, [part, ...])`, each part a list of points. |
| 10 | type Fixture = (u16, u8, Vec<Vec<(i16, i16)>>); |
| 11 | |
| 12 | /// Build a layer from a list of fixture features. |
| 13 | fn layer_of(features: &[Fixture]) -> Layer { |
| 14 | let mut layer = Layer::new(4); |
| 15 | for (kind, geom_type, parts) in features { |
| 16 | let parts_offset = layer.parts.len() as u32; |
| 17 | for points in parts { |
| 18 | layer.parts.push(Part { |
| 19 | coord_start: layer.coords.len() as u32, |
| 20 | point_count: points.len() as u32, |
| 21 | winding: WINDING_OUTER, |
| 22 | }); |
| 23 | layer.coords.extend_from_slice(points); |
| 24 | } |
| 25 | layer.features.push(Feature { |
| 26 | kind: *kind, |
| 27 | kind_detail: 0, |
| 28 | geom_type: *geom_type, |
| 29 | flags: 0, |
| 30 | name_idx: tilecodec::mamaps::body::NAME_NONE, |
| 31 | parts_offset, |
| 32 | part_count: parts.len() as u32, |
| 33 | transit_color: 0, |
| 34 | transit_ordinal: 0, |
| 35 | transit_lanes: 0, |
| 36 | transit_taper: 0, |
| 37 | lane_count: 0, |
| 38 | }); |
| 39 | } |
| 40 | layer |
| 41 | } |
| 42 | |
| 43 | /// Every part's points, so a test can compare geometry without caring how it is grouped. |
| 44 | fn all_parts(layer: &Layer) -> Vec<Vec<(i16, i16)>> { |
| 45 | layer |
| 46 | .parts |
| 47 | .iter() |
| 48 | .map(|p| points_of(&layer.coords, *p).to_vec()) |
| 49 | .collect() |
| 50 | } |
| 51 | |
| 52 | /// The arena must be tiled exactly by the parts table, in order, or the encoder rejects the |
| 53 | /// body. Asserted on every result below, because it is the invariant easiest to break here. |
| 54 | fn assert_arena_is_tiled(layer: &Layer) { |
| 55 | let mut at = 0u32; |
| 56 | for part in &layer.parts { |
| 57 | assert_eq!(part.coord_start, at, "parts must tile the arena with no gaps"); |
| 58 | at += part.point_count; |
| 59 | } |
| 60 | assert_eq!(at as usize, layer.coords.len(), "the arena must end where the parts do"); |
| 61 | for feature in &layer.features { |
| 62 | let end = feature.parts_offset as usize + feature.part_count as usize; |
| 63 | assert!(end <= layer.parts.len(), "a feature indexes past the parts table"); |
| 64 | assert!(feature.part_count > 0, "a feature with no parts draws nothing"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | #[test] |
| 69 | fn features_of_one_class_become_one_feature() { |
| 70 | // Four disjoint fragments of the same class: one feature, four parts, same geometry. |
| 71 | let mut layer = layer_of(&[ |
| 72 | (7, GEOM_LINE, vec![vec![(0, 0), (1, 1)]]), |
| 73 | (7, GEOM_LINE, vec![vec![(10, 10), (11, 11)]]), |
| 74 | (7, GEOM_LINE, vec![vec![(20, 20), (21, 21)]]), |
| 75 | (7, GEOM_LINE, vec![vec![(30, 30), (31, 31)]]), |
| 76 | ]); |
| 77 | let before = all_parts(&layer); |
| 78 | let stats = coalesce_lines(&mut layer); |
| 79 | assert_arena_is_tiled(&layer); |
| 80 | assert_eq!(layer.features.len(), 1); |
| 81 | assert_eq!(layer.features[0].part_count, 4); |
| 82 | assert_eq!(all_parts(&layer), before, "nothing disjoint may be joined"); |
| 83 | assert_eq!(stats, Stats { features_before: 4, features_after: 1, parts_before: 4, parts_after: 4 }); |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn different_classes_stay_apart() { |
| 88 | let mut layer = layer_of(&[ |
| 89 | (7, GEOM_LINE, vec![vec![(0, 0), (1, 1)]]), |
| 90 | (9, GEOM_LINE, vec![vec![(1, 1), (2, 2)]]), |
| 91 | (7, GEOM_LINE, vec![vec![(5, 5), (6, 6)]]), |
| 92 | ]); |
| 93 | coalesce_lines(&mut layer); |
| 94 | assert_arena_is_tiled(&layer); |
| 95 | assert_eq!(layer.features.len(), 2, "two classes, two features"); |
| 96 | assert_eq!(layer.features[0].kind, 7, "the first class keeps the first position"); |
| 97 | assert_eq!(layer.features[1].kind, 9); |
| 98 | // Class 9 touches class 7's endpoint but must not be spliced onto it. |
| 99 | assert_eq!(layer.features[1].part_count, 1); |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn fragments_that_continue_each_other_are_spliced() { |
| 104 | // Three pieces of one road, given out of order, plus a fourth that is elsewhere. |
| 105 | let mut layer = layer_of(&[ |
| 106 | (7, GEOM_LINE, vec![vec![(0, 0), (1, 0)]]), |
| 107 | (7, GEOM_LINE, vec![vec![(2, 0), (3, 0)]]), |
| 108 | (7, GEOM_LINE, vec![vec![(1, 0), (2, 0)]]), |
| 109 | (7, GEOM_LINE, vec![vec![(50, 50), (51, 50)]]), |
| 110 | ]); |
| 111 | let stats = coalesce_lines(&mut layer); |
| 112 | assert_arena_is_tiled(&layer); |
| 113 | assert_eq!(layer.features.len(), 1); |
| 114 | assert_eq!(stats.parts_before, 4); |
| 115 | assert_eq!(stats.parts_after, 2, "three joined into one, plus the far piece"); |
| 116 | let parts = all_parts(&layer); |
| 117 | assert!( |
| 118 | parts.contains(&vec![(0, 0), (1, 0), (2, 0), (3, 0)]), |
| 119 | "the joint point appears once, not twice: {parts:?}" |
| 120 | ); |
| 121 | assert!(parts.contains(&vec![(50, 50), (51, 50)])); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn a_chain_starts_at_its_head_rather_than_its_middle() { |
| 126 | // If a chain were grown from the middle piece the result would be two runs, not one. |
| 127 | let mut layer = layer_of(&[ |
| 128 | (7, GEOM_LINE, vec![vec![(1, 0), (2, 0)]]), |
| 129 | (7, GEOM_LINE, vec![vec![(0, 0), (1, 0)]]), |
| 130 | (7, GEOM_LINE, vec![vec![(2, 0), (3, 0)]]), |
| 131 | ]); |
| 132 | coalesce_lines(&mut layer); |
| 133 | assert_arena_is_tiled(&layer); |
| 134 | assert_eq!(layer.parts.len(), 1, "one road, one part"); |
| 135 | assert_eq!(all_parts(&layer)[0], vec![(0, 0), (1, 0), (2, 0), (3, 0)]); |
| 136 | } |
| 137 | |
| 138 | /// A ring road: every piece is continued by another, so none is a head. Without the second pass |
| 139 | /// over the leftovers this would emit nothing and the road would vanish. |
| 140 | #[test] |
| 141 | fn a_closed_loop_survives_having_no_head() { |
| 142 | let mut layer = layer_of(&[ |
| 143 | (7, GEOM_LINE, vec![vec![(0, 0), (10, 0)]]), |
| 144 | (7, GEOM_LINE, vec![vec![(10, 0), (10, 10)]]), |
| 145 | (7, GEOM_LINE, vec![vec![(10, 10), (0, 0)]]), |
| 146 | ]); |
| 147 | coalesce_lines(&mut layer); |
| 148 | assert_arena_is_tiled(&layer); |
| 149 | assert_eq!(layer.parts.len(), 1); |
| 150 | assert_eq!( |
| 151 | all_parts(&layer)[0], |
| 152 | vec![(0, 0), (10, 0), (10, 10), (0, 0)], |
| 153 | "the loop closes on the point it started at" |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn polygons_are_untouched_and_keep_their_place() { |
| 159 | let square = vec![(0, 0), (4, 0), (4, 4), (0, 4), (0, 0)]; |
| 160 | let hole = vec![(1, 1), (2, 1), (2, 2), (1, 2), (1, 1)]; |
| 161 | let mut layer = layer_of(&[ |
| 162 | (3, GEOM_POLYGON, vec![square.clone(), hole.clone()]), |
| 163 | (7, GEOM_LINE, vec![vec![(0, 0), (1, 0)]]), |
| 164 | (7, GEOM_LINE, vec![vec![(1, 0), (2, 0)]]), |
| 165 | ]); |
| 166 | coalesce_lines(&mut layer); |
| 167 | assert_arena_is_tiled(&layer); |
| 168 | assert_eq!(layer.features.len(), 2); |
| 169 | assert_eq!(layer.features[0].geom_type, GEOM_POLYGON, "the polygon keeps its position"); |
| 170 | assert_eq!(layer.features[0].part_count, 2, "exterior and hole both survive"); |
| 171 | let parts = all_parts(&layer); |
| 172 | assert_eq!(parts[0], square); |
| 173 | assert_eq!(parts[1], hole); |
| 174 | assert_eq!(parts[2], vec![(0, 0), (1, 0), (2, 0)], "the lines still merged"); |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn a_layer_with_nothing_to_merge_is_left_alone() { |
| 179 | for features in [ |
| 180 | vec![], |
| 181 | vec![(7u16, GEOM_LINE, vec![vec![(0i16, 0i16), (1, 1)]])], |
| 182 | vec![(3, GEOM_POLYGON, vec![vec![(0, 0), (4, 0), (4, 4), (0, 0)]])], |
| 183 | ] { |
| 184 | let mut layer = layer_of(&features); |
| 185 | let before = layer.clone(); |
| 186 | let stats = coalesce_lines(&mut layer); |
| 187 | assert_eq!(layer, before, "a layer with fewer than two lines must not be rebuilt"); |
| 188 | assert_eq!(stats.features_before, stats.features_after); |
| 189 | assert_eq!(stats.parts_before, stats.parts_after); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /// Two transit lines of one mode crossing a tile: same `kind`, different operator colour. They |
| 194 | /// must stay apart, or the merged feature wears whichever colour happened to come first. |
| 195 | #[test] |
| 196 | fn transit_colour_separates_otherwise_identical_features() { |
| 197 | let mut layer = layer_of(&[ |
| 198 | (7, GEOM_LINE, vec![vec![(0, 0), (1, 0)]]), |
| 199 | (7, GEOM_LINE, vec![vec![(1, 0), (2, 0)]]), |
| 200 | ]); |
| 201 | layer.features[0].transit_color = 0x00_54_A5; |
| 202 | layer.features[1].transit_color = 0xE3_1E_24; |
| 203 | coalesce_lines(&mut layer); |
| 204 | assert_arena_is_tiled(&layer); |
| 205 | assert_eq!(layer.features.len(), 2, "two colours, two features"); |
| 206 | assert_eq!(layer.features[0].transit_color, 0x00_54_A5); |
| 207 | assert_eq!(layer.features[1].transit_color, 0xE3_1E_24); |
| 208 | // They touch end to end, so without the colour in the key they would also be spliced. |
| 209 | assert_eq!(all_parts(&layer), vec![vec![(0, 0), (1, 0)], vec![(1, 0), (2, 0)]]); |
| 210 | } |
| 211 | |
| 212 | /// The counterpart: equal colours still merge, so the key is not simply always-distinct. |
| 213 | #[test] |
| 214 | fn equal_transit_colours_still_merge() { |
| 215 | let mut layer = layer_of(&[ |
| 216 | (7, GEOM_LINE, vec![vec![(0, 0), (1, 0)]]), |
| 217 | (7, GEOM_LINE, vec![vec![(1, 0), (2, 0)]]), |
| 218 | ]); |
| 219 | layer.features[0].transit_color = 0x00_54_A5; |
| 220 | layer.features[1].transit_color = 0x00_54_A5; |
| 221 | coalesce_lines(&mut layer); |
| 222 | assert_arena_is_tiled(&layer); |
| 223 | assert_eq!(layer.features.len(), 1); |
| 224 | assert_eq!(all_parts(&layer), vec![vec![(0, 0), (1, 0), (2, 0)]]); |
| 225 | } |
| 226 | |
| 227 | /// Two routes of one colour on different corridor ordinals draw as two parallel lines. |
| 228 | /// Merging them would put both on whichever ordinal came first, which is the whole defect the |
| 229 | /// ordinal exists to fix. |
| 230 | #[test] |
| 231 | fn a_transit_ordinal_separates_two_features_of_one_colour() { |
| 232 | let mut layer = layer_of(&[ |
| 233 | (7, GEOM_LINE, vec![vec![(0, 0), (1, 0)]]), |
| 234 | (7, GEOM_LINE, vec![vec![(1, 0), (2, 0)]]), |
| 235 | ]); |
| 236 | for feature in &mut layer.features { |
| 237 | feature.transit_color = 0x00_54_A5; |
| 238 | feature.transit_lanes = 2; |
| 239 | feature.transit_taper = 255; |
| 240 | } |
| 241 | layer.features[0].transit_ordinal = 0; |
| 242 | layer.features[1].transit_ordinal = 1; |
| 243 | coalesce_lines(&mut layer); |
| 244 | assert_arena_is_tiled(&layer); |
| 245 | assert_eq!(layer.features.len(), 2, "two ordinals, two features"); |
| 246 | assert_eq!(layer.features[0].transit_ordinal, 0); |
| 247 | assert_eq!(layer.features[1].transit_ordinal, 1); |
| 248 | assert_eq!(all_parts(&layer), vec![vec![(0, 0), (1, 0)], vec![(1, 0), (2, 0)]]); |
| 249 | } |
| 250 | |
| 251 | /// **The one part of the merge key that is not on the feature.** Two four-lane roads of the |
| 252 | /// same class that divide 3/1 and 1/3 are drawn as different surfaces with the centre line in |
| 253 | /// different places, and the split lives in a side table rather than on the feature — so |
| 254 | /// without it in the key they would touch end to end, splice, and both come out on whichever |
| 255 | /// division happened to come first. |
| 256 | #[test] |
| 257 | fn a_differing_directional_split_keeps_two_roads_of_one_class_apart() { |
| 258 | let mut layer = layer_of(&[ |
| 259 | (7, GEOM_LINE, vec![vec![(0, 0), (1, 0)]]), |
| 260 | (7, GEOM_LINE, vec![vec![(1, 0), (2, 0)]]), |
| 261 | ]); |
| 262 | for feature in &mut layer.features { |
| 263 | feature.lane_count = 4; |
| 264 | } |
| 265 | let mut carriageways = vec![ |
| 266 | Carriageway { forward: 3, backward: 1, solid_dividers: 0 }, |
| 267 | Carriageway { forward: 1, backward: 3, solid_dividers: 0 }, |
| 268 | ]; |
| 269 | coalesce_lines_with_ids(&mut layer, None, None, None, Some(&mut carriageways)); |
| 270 | assert_arena_is_tiled(&layer); |
| 271 | assert_eq!(layer.features.len(), 2, "two splits, two features"); |
| 272 | assert_eq!( |
| 273 | all_parts(&layer), |
| 274 | vec![vec![(0, 0), (1, 0)], vec![(1, 0), (2, 0)]], |
| 275 | "they touch end to end, so without the split in the key they would be spliced", |
| 276 | ); |
| 277 | // And the side table still describes the features it is parallel to, in order. |
| 278 | assert_eq!( |
| 279 | carriageways, |
| 280 | vec![ |
| 281 | Carriageway { forward: 3, backward: 1, solid_dividers: 0 }, |
| 282 | Carriageway { forward: 1, backward: 3, solid_dividers: 0 }, |
| 283 | ], |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | /// The counterpart, so the key is not simply always-distinct: roads that agree on their split |
| 288 | /// still merge, and the survivor's entry is the split both of them had. |
| 289 | #[test] |
| 290 | fn an_equal_directional_split_still_merges() { |
| 291 | let mut layer = layer_of(&[ |
| 292 | (7, GEOM_LINE, vec![vec![(0, 0), (1, 0)]]), |
| 293 | (7, GEOM_LINE, vec![vec![(1, 0), (2, 0)]]), |
| 294 | ]); |
| 295 | for feature in &mut layer.features { |
| 296 | feature.lane_count = 4; |
| 297 | } |
| 298 | let split = Carriageway { forward: 3, backward: 1, solid_dividers: 0b10 }; |
| 299 | let mut carriageways = vec![split, split]; |
| 300 | coalesce_lines_with_ids(&mut layer, None, None, None, Some(&mut carriageways)); |
| 301 | assert_arena_is_tiled(&layer); |
| 302 | assert_eq!(layer.features.len(), 1); |
| 303 | assert_eq!(all_parts(&layer), vec![vec![(0, 0), (1, 0), (2, 0)]]); |
| 304 | assert_eq!(carriageways, vec![split], "the side table shrinks with the features"); |
| 305 | } |
| 306 | |
| 307 | /// Solid dividers are part of the split, so a road where a lane change is prohibited does not |
| 308 | /// merge with one where it is allowed — the difference is a solid line against a dashed one. |
| 309 | #[test] |
| 310 | fn a_solid_divider_separates_two_otherwise_identical_carriageways() { |
| 311 | let mut layer = layer_of(&[ |
| 312 | (7, GEOM_LINE, vec![vec![(0, 0), (1, 0)]]), |
| 313 | (7, GEOM_LINE, vec![vec![(1, 0), (2, 0)]]), |
| 314 | ]); |
| 315 | let mut carriageways = vec![ |
| 316 | Carriageway { forward: 2, backward: 2, solid_dividers: 0 }, |
| 317 | Carriageway { forward: 2, backward: 2, solid_dividers: 0b1 }, |
| 318 | ]; |
| 319 | coalesce_lines_with_ids(&mut layer, None, None, None, Some(&mut carriageways)); |
| 320 | assert_arena_is_tiled(&layer); |
| 321 | assert_eq!(layer.features.len(), 2); |
| 322 | } |
| 323 | |
| 324 | /// The point of the whole module, in the shape the real data has: thousands of two-point |
| 325 | /// fragments of one class laid end to end. |
| 326 | #[test] |
| 327 | fn a_fragmented_road_collapses_to_one_part() { |
| 328 | let features: Vec<Fixture> = (0..2000) |
| 329 | .map(|i| (7u16, GEOM_LINE, vec![vec![(i as i16, 0), (i as i16 + 1, 0)]])) |
| 330 | .collect(); |
| 331 | let mut layer = layer_of(&features); |
| 332 | let stats = coalesce_lines(&mut layer); |
| 333 | assert_arena_is_tiled(&layer); |
| 334 | assert_eq!(stats.features_before, 2000); |
| 335 | assert_eq!(stats.features_after, 1); |
| 336 | assert_eq!(stats.parts_before, 2000); |
| 337 | assert_eq!(stats.parts_after, 1); |
| 338 | assert_eq!(layer.coords.len(), 2001, "2000 segments share 1999 joints"); |
| 339 | } |
| 340 | } |