/// How many lanes to spread one arm's connector ends across, in its direction of travel. /// /// **A lane count is a property of the arm and of nothing else.** That is why this takes only an /// [`Arm`] and why it is the single way to ask: both ends of a connector read it, so neither can /// drift from the other, and no caller can substitute a number that merely happened to be in scope. /// An earlier version took the junction's shape as a fallback and produced a count that varied with /// which approach was looking at the arm. /// /// `turn:lanes` from `lanes.bin` is the only real value reachable here — the v6 routing graph /// carries no lane count, and OSM's own `lanes` tag lives in the extract path keyed by way rather /// than by directed edge, so joining to it is a separate piece of work. /// /// **The fallback of 1 is the carriageway renderer's own default, not a guess.** `tile/geometry.rs` /// turns `lane_count 0` into `oneway ? 1 : 2`, counted across *both* directions; a two-way road is /// therefore one lane each way and a one-way road one lane. [`lane_offset_m`] counts per direction /// and adds the opposing half-carriageway itself, so both of those cases are 1 in this function's /// units. Matching it is the whole point: a connector then starts and ends where the asphalt is /// actually painted instead of where a second, disagreeing default put it. /// /// The cost is honest and worth stating. At an untagged junction every movement out of an arm /// shares that arm's single lane, so the connectors meet at the arm's mouth rather than fanning. /// That is what a single-lane road *is*. Inventing a wider fan to separate them draws lanes that /// are not there and puts the endpoints off the carriageway, which is the defect this replaces. fn effective_lanes(arm: &Arm) -> usize { arm.masks.as_ref().map_or(1, Vec::len) } /// Sample the connector from lane `in_lane` of `approach` into lane `out_lane` of `exit`. /// /// A cubic bezier in a local tangent plane centred on the junction node, with its handles along the /// two headings so the curve leaves the approach and meets the exit tangentially. Sampled here, in /// `f64` and once per build, rather than on device once per frame — which is the whole point of /// carrying a polyline instead of control points. /// Inputs for [`connector`], grouped so the one call site names them. struct ConnectorArgs<'a> { node: (i32, i32), approach: &'a Arm, in_lane: usize, approach_lanes: usize, exit: &'a Arm, out_lane: usize, exit_lanes: usize, turn: Turn, left_hand: bool, } fn connector(args: ConnectorArgs<'_>) -> Option> { let ConnectorArgs { node, approach, in_lane, approach_lanes, exit, out_lane, exit_lanes, turn, left_hand } = args; let back = (SETBACK_M).min(approach.length_m * SETBACK_EDGE_FRACTION); let ahead = (SETBACK_M).min(exit.length_m * SETBACK_EDGE_FRACTION); if back < MIN_SETBACK_M || ahead < MIN_SETBACK_M { return None; } // The setback is real ground distance: the intersection is that big and the roads it joins are // where they are. The lateral offset is not — it shrinks with `cos φ` so that it stays a fixed // size in projected units, and therefore a fixed size in pixels, at every latitude. // // Both ends are placed by the same call with the same rule. Taking the counts as arguments // rather than off each `Arm` is what makes that structural: an arm's own `lanes` is whatever // `lanes.bin` happened to say, and reading it directly at one end while the caller overrode it // at the other is exactly how every connector into an exit came to share one endpoint. let width = lane_width_ground_m(f64::from(node.0) * 1e-7); let in_off = lane_offset_m(in_lane, approach_lanes, approach.two_way, left_hand, width); let out_off = lane_offset_m(out_lane, exit_lanes, exit.two_way, left_hand, width); let (fin_e, fin_n) = forward(approach.heading); let (rin_e, rin_n) = rightward(approach.heading); let (fout_e, fout_n) = forward(exit.heading); let (rout_e, rout_n) = rightward(exit.heading); // Start: back along the approach from the node, then across to the lane's centre. let p0 = (-fin_e * back + rin_e * in_off, -fin_n * back + rin_n * in_off); // End: forward along the exit, then across. let p3 = (fout_e * ahead + rout_e * out_off, fout_n * ahead + rout_n * out_off); let p1 = (p0.0 + fin_e * back * HANDLE, p0.1 + fin_n * back * HANDLE); let p2 = (p3.0 - fout_e * ahead * HANDLE, p3.1 - fout_n * ahead * HANDLE); // A straight-through movement needs two points; a U-turn needs every one of these. Anything // flatter than the sampling would resolve is dropped by the tiler's simplifier anyway. // // Sampled at roughly half the historical density: a z14 connector spans // tens of metres, so ~5 points resolve its curve at display scale and the // rest were coordinate bytes. Minimum 3 keeps a curve a curve. let sweep = normalise_degrees(exit.heading - approach.heading).abs(); let samples = if turn == Turn::Through && sweep < 3.0 && (in_off - out_off).abs() < 0.05 { 2 } else { (3.0 + sweep / 36.0).round().clamp(3.0, 7.0) as usize }; let mut out = Vec::with_capacity(samples); for i in 0..samples { let t = i as f64 / (samples - 1) as f64; let u = 1.0 - t; let (w0, w1, w2, w3) = (u * u * u, 3.0 * u * u * t, 3.0 * u * t * t, t * t * t); let east = w0 * p0.0 + w1 * p1.0 + w2 * p2.0 + w3 * p3.0; let north = w0 * p0.1 + w1 * p1.1 + w2 * p2.1 + w3 * p3.1; out.push(offset_degrees(node, east, north)); } Some(out) } /// Which lane of the exit a connector lands in. /// /// Inference, and the ordinary road convention; the rule is written out in full in the module docs. /// `rank` is this lane's place, left→right, among the `siblings` approach lanes making the same /// movement into the same exit — which is what keeps a dual left turn two ribbons wide instead of /// two ribbons on one point. A movement only one lane makes passes `rank` 0 and `siblings` 1, and /// reduces to "the exit's leftmost" or "its rightmost". fn exit_lane( turn: Turn, in_lane: usize, in_lanes: usize, out_lanes: usize, rank: usize, siblings: usize, ) -> usize { let last = out_lanes.saturating_sub(1); if turn.is_left() { rank.min(last) } else if turn.is_right() { last - siblings.saturating_sub(1).saturating_sub(rank).min(last) } else { (in_lane * out_lanes / in_lanes.max(1)).min(last) } } /// The exits a lane tagged for `turn` should connect to. /// /// Exact bucket first: every legal exit the junction's own geometry classes as this movement. When /// none matches — a `turn:lanes=right` onto a road that leaves at 140°, say — the single closest /// exit by angle is used instead, because a tagged indication with nowhere to go is far more likely /// to be a threshold disagreement than a lane that leads nowhere. fn exits_for(turn: Turn, legal: &[(usize, f64, Turn)]) -> Vec { // A lane tagged `turn:lanes=reverse` has nowhere to go, because reversals are not legal // movements here. Returning empty rather than falling through matters: `legal` holds no // reversal, so the closest-by-angle fallback below would attach the lane to whatever sharp // turn happens to be nearest and emit a connector into a road the lane does not feed. if turn == Turn::Reverse { return Vec::new(); } let exact: Vec = legal.iter().filter(|(_, _, t)| *t == turn).map(|(i, _, _)| *i).collect(); if !exact.is_empty() { return exact; } let nominal = turn.nominal_degrees(); legal .iter() .min_by(|a, b| { normalise_degrees(a.1 - nominal) .abs() .total_cmp(&normalise_degrees(b.1 - nominal).abs()) }) .map(|(i, _, _)| vec![*i]) .unwrap_or_default() }