// Which directional road a pedestrian crossing belongs to. // // The rule lived only inside `compileControlMarkings()`, which meant the solver // could not tell a crossing from a junction and therefore published a flat // `max: 8` for every `junction-crosswalk-inset` handle. Measured against the // geometry, the real ceiling runs 1.0–4.5m and varies per approach, so 44–88% of // that range moved nothing at all and gave no diagnostic — a control that is dead // over most of its travel, which is the failure `flag.ts` exists to prevent. // // Only road-level facts are used, so this module stays free of lane geometry and // of fs/path — the solver may require it. The compiler still chooses a lane, but // it chooses one *within* the road picked here, so there is a single definition // of "the approach this crossing serves". // // Picking by road first is behaviour-preserving: every lane of a road shares its // `junctionDistanceMeters`, so the compiler's (road, lane) sort already grouped // by road, and `placement.distance` only ever broke ties inside one. /** Below this the crossing is inside the junction; above it belongs elsewhere. */ const MIN_APPROACH_DISTANCE_METERS = 2.7; const MAX_APPROACH_DISTANCE_METERS = 25; /** How far an inset may pull the crosswalk from its natural spot. */ const MAX_JUNCTION_INSET_METERS = 4; const EARTH_RADIUS_METERS = 6371008.8; const toRadians = (degrees) => (degrees * Math.PI) / 180; function distanceMeters(a, b) { if (!Array.isArray(a) || !Array.isArray(b)) return Infinity; const lat1 = toRadians(a[1]); const lat2 = toRadians(b[1]); const h = Math.sin(toRadians(b[1] - a[1]) / 2) ** 2 + Math.cos(lat1) * Math.cos(lat2) * Math.sin(toRadians(b[0] - a[0]) / 2) ** 2; return 2 * EARTH_RADIUS_METERS * Math.asin(Math.min(1, Math.sqrt(h))); } /** Endpoints that a connection arrives at, i.e. roads that end in a junction. */ function arrivalEndpointIds(model) { return new Set( (model.connections || []).filter((connection) => connection.enabled).map((connection) => connection.fromEndpointId), ); } /** Roads a crossing could sit on, with their distance to the junction. */ function candidateRoads(model, crossing) { return (model.roads || []) .filter( (road) => Array.isArray(road.osmWayIds) && Array.isArray(crossing.osmWayIds) && crossing.osmWayIds.includes(road.osmWayIds[0]) && Array.isArray(road.centerline) && road.centerline.length >= 2, ) .map((road) => ({ road, junctionDistanceMeters: distanceMeters(crossing.coordinate, road.centerline.at(-1)) })); } /** * The approach a crossing serves, or `null` when it serves none. * * `arrivals` is optional so a caller resolving many crossings can build it once. */ function approachForCrossing(model, crossing, arrivals = arrivalEndpointIds(model)) { if (!crossing || !Array.isArray(crossing.coordinate)) return null; return ( candidateRoads(model, crossing) .filter( (item) => arrivals.has(`endpoint:${item.road.id}:end`) && item.junctionDistanceMeters > MIN_APPROACH_DISTANCE_METERS && item.junctionDistanceMeters <= MAX_APPROACH_DISTANCE_METERS, ) .sort((a, b) => a.junctionDistanceMeters - b.junctionDistanceMeters)[0] || null ); } /** * The largest inset that still moves the crosswalk. * * `crossingJunctionInset()` computes * `min(M, max(0, D - max(0, C - inset)))`, which stops rising once * `targetDistance` bottoms out at zero, i.e. once `inset` reaches `C` — and * earlier still when `D` exceeds the `M` cap. Solving for the saturation point: * * effectiveMax = C - max(0, D - M) * * Verified against eight measured groups: predicted ceilings matched every one * to within the 0.5m sweep step. */ function effectiveCrosswalkInsetMax(cutbackMeters, junctionDistanceMeters) { if (!Number.isFinite(cutbackMeters) || !Number.isFinite(junctionDistanceMeters)) return null; return cutbackMeters - Math.max(0, junctionDistanceMeters - MAX_JUNCTION_INSET_METERS); } module.exports = { MAX_APPROACH_DISTANCE_METERS, MAX_JUNCTION_INSET_METERS, MIN_APPROACH_DISTANCE_METERS, approachForCrossing, arrivalEndpointIds, candidateRoads, distanceMeters, effectiveCrosswalkInsetMax, };