Every junction-crosswalk-inset handle advertised max 8. Sweeping the real response across eight groups, the ceiling ran 1.0 to 4.5 and varied per approach, so 44-88% of the declared range moved nothing and returned no diagnostic. Two approaches had a ceiling at or below the 1.5 default, meaning dragging outward there did nothing at all — which is what made direct editing look broken rather than limited. crossingJunctionInset() stops responding once the inset reaches the cutback, and sooner still when the crossing sits beyond the 4m cap, giving cutback - max(0, junctionDistance - 4). Predicted ceilings matched all eight measurements within the 0.5m sweep step, and the published max now matches them 8/8 where it used to be a flat 8. The distance needs to know which crossing an approach serves, which lived only inside compileControlMarkings(). crossing-approach.js now holds that rule at road level, which is model-only and so usable from the solver. Behaviour is unchanged: every lane of a road shares its junction distance, so the compiler's (road, lane) ordering already grouped by road — fixtures confirm the geometry is byte-identical. Both directional roads of a segment can meet at one junction serving different crossings; taking the first match published one road's ceiling on the other's handle. The road that ends at this node wins, which is the same rule the client uses to find the handle and the same one crossingJunctionInset() applies. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
106 lines
4.2 KiB
JavaScript
106 lines
4.2 KiB
JavaScript
// 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,
|
||
};
|