Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / schema / junction_part1_s1.rs
junction_part1_s1.rs157 lines · 7.9 KiBRaw
| 1 | /// How many lanes to spread one arm's connector ends across, in its direction of travel. |
| 2 | /// |
| 3 | /// **A lane count is a property of the arm and of nothing else.** That is why this takes only an |
| 4 | /// [`Arm`] and why it is the single way to ask: both ends of a connector read it, so neither can |
| 5 | /// drift from the other, and no caller can substitute a number that merely happened to be in scope. |
| 6 | /// An earlier version took the junction's shape as a fallback and produced a count that varied with |
| 7 | /// which approach was looking at the arm. |
| 8 | /// |
| 9 | /// `turn:lanes` from `lanes.bin` is the only real value reachable here — the v6 routing graph |
| 10 | /// carries no lane count, and OSM's own `lanes` tag lives in the extract path keyed by way rather |
| 11 | /// than by directed edge, so joining to it is a separate piece of work. |
| 12 | /// |
| 13 | /// **The fallback of 1 is the carriageway renderer's own default, not a guess.** `tile/geometry.rs` |
| 14 | /// turns `lane_count 0` into `oneway ? 1 : 2`, counted across *both* directions; a two-way road is |
| 15 | /// therefore one lane each way and a one-way road one lane. [`lane_offset_m`] counts per direction |
| 16 | /// and adds the opposing half-carriageway itself, so both of those cases are 1 in this function's |
| 17 | /// units. Matching it is the whole point: a connector then starts and ends where the asphalt is |
| 18 | /// actually painted instead of where a second, disagreeing default put it. |
| 19 | /// |
| 20 | /// The cost is honest and worth stating. At an untagged junction every movement out of an arm |
| 21 | /// shares that arm's single lane, so the connectors meet at the arm's mouth rather than fanning. |
| 22 | /// That is what a single-lane road *is*. Inventing a wider fan to separate them draws lanes that |
| 23 | /// are not there and puts the endpoints off the carriageway, which is the defect this replaces. |
| 24 | fn effective_lanes(arm: &Arm) -> usize { |
| 25 | arm.masks.as_ref().map_or(1, Vec::len) |
| 26 | } |
| 27 | |
| 28 | /// Sample the connector from lane `in_lane` of `approach` into lane `out_lane` of `exit`. |
| 29 | /// |
| 30 | /// A cubic bezier in a local tangent plane centred on the junction node, with its handles along the |
| 31 | /// two headings so the curve leaves the approach and meets the exit tangentially. Sampled here, in |
| 32 | /// `f64` and once per build, rather than on device once per frame — which is the whole point of |
| 33 | /// carrying a polyline instead of control points. |
| 34 | /// Inputs for [`connector`], grouped so the one call site names them. |
| 35 | struct ConnectorArgs<'a> { |
| 36 | node: (i32, i32), |
| 37 | approach: &'a Arm, |
| 38 | in_lane: usize, |
| 39 | approach_lanes: usize, |
| 40 | exit: &'a Arm, |
| 41 | out_lane: usize, |
| 42 | exit_lanes: usize, |
| 43 | turn: Turn, |
| 44 | left_hand: bool, |
| 45 | } |
| 46 | |
| 47 | fn connector(args: ConnectorArgs<'_>) -> Option<Vec<(f64, f64)>> { |
| 48 | let ConnectorArgs { node, approach, in_lane, approach_lanes, exit, out_lane, exit_lanes, turn, left_hand } = args; |
| 49 | let back = (SETBACK_M).min(approach.length_m * SETBACK_EDGE_FRACTION); |
| 50 | let ahead = (SETBACK_M).min(exit.length_m * SETBACK_EDGE_FRACTION); |
| 51 | if back < MIN_SETBACK_M || ahead < MIN_SETBACK_M { |
| 52 | return None; |
| 53 | } |
| 54 | // The setback is real ground distance: the intersection is that big and the roads it joins are |
| 55 | // where they are. The lateral offset is not — it shrinks with `cos φ` so that it stays a fixed |
| 56 | // size in projected units, and therefore a fixed size in pixels, at every latitude. |
| 57 | // |
| 58 | // Both ends are placed by the same call with the same rule. Taking the counts as arguments |
| 59 | // rather than off each `Arm` is what makes that structural: an arm's own `lanes` is whatever |
| 60 | // `lanes.bin` happened to say, and reading it directly at one end while the caller overrode it |
| 61 | // at the other is exactly how every connector into an exit came to share one endpoint. |
| 62 | let width = lane_width_ground_m(f64::from(node.0) * 1e-7); |
| 63 | let in_off = lane_offset_m(in_lane, approach_lanes, approach.two_way, left_hand, width); |
| 64 | let out_off = lane_offset_m(out_lane, exit_lanes, exit.two_way, left_hand, width); |
| 65 | |
| 66 | let (fin_e, fin_n) = forward(approach.heading); |
| 67 | let (rin_e, rin_n) = rightward(approach.heading); |
| 68 | let (fout_e, fout_n) = forward(exit.heading); |
| 69 | let (rout_e, rout_n) = rightward(exit.heading); |
| 70 | |
| 71 | // Start: back along the approach from the node, then across to the lane's centre. |
| 72 | let p0 = (-fin_e * back + rin_e * in_off, -fin_n * back + rin_n * in_off); |
| 73 | // End: forward along the exit, then across. |
| 74 | let p3 = (fout_e * ahead + rout_e * out_off, fout_n * ahead + rout_n * out_off); |
| 75 | let p1 = (p0.0 + fin_e * back * HANDLE, p0.1 + fin_n * back * HANDLE); |
| 76 | let p2 = (p3.0 - fout_e * ahead * HANDLE, p3.1 - fout_n * ahead * HANDLE); |
| 77 | |
| 78 | // A straight-through movement needs two points; a U-turn needs every one of these. Anything |
| 79 | // flatter than the sampling would resolve is dropped by the tiler's simplifier anyway. |
| 80 | // |
| 81 | // Sampled at roughly half the historical density: a z14 connector spans |
| 82 | // tens of metres, so ~5 points resolve its curve at display scale and the |
| 83 | // rest were coordinate bytes. Minimum 3 keeps a curve a curve. |
| 84 | let sweep = normalise_degrees(exit.heading - approach.heading).abs(); |
| 85 | let samples = if turn == Turn::Through && sweep < 3.0 && (in_off - out_off).abs() < 0.05 { |
| 86 | 2 |
| 87 | } else { |
| 88 | (3.0 + sweep / 36.0).round().clamp(3.0, 7.0) as usize |
| 89 | }; |
| 90 | |
| 91 | let mut out = Vec::with_capacity(samples); |
| 92 | for i in 0..samples { |
| 93 | let t = i as f64 / (samples - 1) as f64; |
| 94 | let u = 1.0 - t; |
| 95 | let (w0, w1, w2, w3) = (u * u * u, 3.0 * u * u * t, 3.0 * u * t * t, t * t * t); |
| 96 | let east = w0 * p0.0 + w1 * p1.0 + w2 * p2.0 + w3 * p3.0; |
| 97 | let north = w0 * p0.1 + w1 * p1.1 + w2 * p2.1 + w3 * p3.1; |
| 98 | out.push(offset_degrees(node, east, north)); |
| 99 | } |
| 100 | Some(out) |
| 101 | } |
| 102 | |
| 103 | /// Which lane of the exit a connector lands in. |
| 104 | /// |
| 105 | /// Inference, and the ordinary road convention; the rule is written out in full in the module docs. |
| 106 | /// `rank` is this lane's place, left→right, among the `siblings` approach lanes making the same |
| 107 | /// movement into the same exit — which is what keeps a dual left turn two ribbons wide instead of |
| 108 | /// two ribbons on one point. A movement only one lane makes passes `rank` 0 and `siblings` 1, and |
| 109 | /// reduces to "the exit's leftmost" or "its rightmost". |
| 110 | fn exit_lane( |
| 111 | turn: Turn, |
| 112 | in_lane: usize, |
| 113 | in_lanes: usize, |
| 114 | out_lanes: usize, |
| 115 | rank: usize, |
| 116 | siblings: usize, |
| 117 | ) -> usize { |
| 118 | let last = out_lanes.saturating_sub(1); |
| 119 | if turn.is_left() { |
| 120 | rank.min(last) |
| 121 | } else if turn.is_right() { |
| 122 | last - siblings.saturating_sub(1).saturating_sub(rank).min(last) |
| 123 | } else { |
| 124 | (in_lane * out_lanes / in_lanes.max(1)).min(last) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /// The exits a lane tagged for `turn` should connect to. |
| 129 | /// |
| 130 | /// Exact bucket first: every legal exit the junction's own geometry classes as this movement. When |
| 131 | /// none matches — a `turn:lanes=right` onto a road that leaves at 140°, say — the single closest |
| 132 | /// exit by angle is used instead, because a tagged indication with nowhere to go is far more likely |
| 133 | /// to be a threshold disagreement than a lane that leads nowhere. |
| 134 | fn exits_for(turn: Turn, legal: &[(usize, f64, Turn)]) -> Vec<usize> { |
| 135 | // A lane tagged `turn:lanes=reverse` has nowhere to go, because reversals are not legal |
| 136 | // movements here. Returning empty rather than falling through matters: `legal` holds no |
| 137 | // reversal, so the closest-by-angle fallback below would attach the lane to whatever sharp |
| 138 | // turn happens to be nearest and emit a connector into a road the lane does not feed. |
| 139 | if turn == Turn::Reverse { |
| 140 | return Vec::new(); |
| 141 | } |
| 142 | let exact: Vec<usize> = |
| 143 | legal.iter().filter(|(_, _, t)| *t == turn).map(|(i, _, _)| *i).collect(); |
| 144 | if !exact.is_empty() { |
| 145 | return exact; |
| 146 | } |
| 147 | let nominal = turn.nominal_degrees(); |
| 148 | legal |
| 149 | .iter() |
| 150 | .min_by(|a, b| { |
| 151 | normalise_degrees(a.1 - nominal) |
| 152 | .abs() |
| 153 | .total_cmp(&normalise_degrees(b.1 - nominal).abs()) |
| 154 | }) |
| 155 | .map(|(i, _, _)| vec![*i]) |
| 156 | .unwrap_or_default() |
| 157 | } |