feat: drag lane turn arrows along their lanes

This commit is contained in:
que01 committed 2026-09-01 10:24:59 +08:00
1 parent 5457f960ff
commit 9cb715a592
12 files changed
+335 -24

No files matched your search

+38 -11
View File
@@ -348,6 +348,11 @@ function validateOverrides(value, model, options = {}) {
`Invalid lane maneuver override: ${item.maneuver}. Use '' to remove an arrow, or one of ` +
`${[...SUPPORTED_MANEUVERS].join(', ')} — components must be in that order.`,
);
if (
item.placementDistanceMeters !== undefined &&
(!Number.isFinite(item.placementDistanceMeters) || item.placementDistanceMeters < 0)
)
throw new Error(`Invalid lane maneuver placement: ${item.placementDistanceMeters}.`);
} else if (item.kind === 'junction-connection') {
if (
typeof item.fromEndpointId !== 'string' ||
@@ -1549,7 +1554,7 @@ function compileLaneMarkings(model, overrides, lanes, diagnostics, junctionPlans
// An empty string is a deliberate "no arrow here", so it is distinguished
// from "no override" rather than falling through to the tag.
const override = lane ? laneManeuverOverride(overrides, lane.id) : undefined;
const explicitManeuver = override !== undefined ? override : maneuvers[index];
const explicitManeuver = override ? override.maneuver : maneuvers[index];
if (!explicitManeuver) continue;
const maneuver = normalizeManeuver(explicitManeuver);
if (!lane) {
@@ -1598,14 +1603,17 @@ function compileLaneMarkings(model, overrides, lanes, diagnostics, junctionPlans
const length = Math.hypot(-vector[0], -vector[1]);
const axis = length ? [-vector[0] / length, -vector[1] / length] : null;
const placement = axis
? [6, 10, 14, 18, 22].find(
(distance) =>
distance < lineLengthMeters(lane.coordinates) - 2 &&
!ringsOverlapControl(
arrowRingsAt(maneuver, pointAlongLine([...lane.coordinates].reverse(), distance), axis),
controlFeatures,
),
)
? override?.placementDistanceMeters !== undefined
? nearestSafeTurnArrowPlacement(
lane.coordinates,
maneuver,
axis,
override.placementDistanceMeters,
controlFeatures,
)
: [6, 10, 14, 18, 22].find((distance) =>
safeTurnArrowPlacement(lane.coordinates, maneuver, axis, distance, controlFeatures),
)
: null;
if (!placement) {
diagnostics.push(
@@ -2368,8 +2376,27 @@ function compileConnectors(model, lanes, diagnostics, overrides, junctionPlans)
}
/** The maneuver an override sets for a lane, or `undefined` when none applies. */
function laneManeuverOverride(overrides, laneId) {
const item = (overrides?.overrides || []).find((entry) => entry.kind === 'lane-maneuver' && entry.laneId === laneId);
return item ? item.maneuver : undefined;
return (overrides?.overrides || []).find((entry) => entry.kind === 'lane-maneuver' && entry.laneId === laneId);
}
function safeTurnArrowPlacement(line, maneuver, axis, distance, controls) {
return (
distance >= 2 &&
distance < lineLengthMeters(line) - 2 &&
!ringsOverlapControl(arrowRingsAt(maneuver, pointAlongLine([...line].reverse(), distance), axis), controls)
);
}
/** Clamp a dragged placement to the nearest 25cm safe position along its lane. */
function nearestSafeTurnArrowPlacement(line, maneuver, axis, desired, controls) {
const step = 0.25;
const limit = Math.max(0, lineLengthMeters(line) - 2);
const target = Math.max(2, Math.min(limit, desired));
for (let offset = 0; offset <= limit; offset += step) {
for (const distance of offset ? [target - offset, target + offset] : [target])
if (safeTurnArrowPlacement(line, maneuver, axis, distance, controls)) return Math.round(distance * 100) / 100;
}
return null;
}
function laneOverride(overrides, fromLaneId, toLaneId) {