feat(workbench): unify lane maneuver edits in v2

This commit is contained in:
que01 committed 2026-09-01 16:46:22 +08:00
1 parent 118dc8e9aa
commit c804a373ca
15 files changed
+337 -263

No files matched your search

+96 -4
View File
@@ -27,6 +27,7 @@ const SOLVED_KINDS = new Set([
'junction-crosswalk-inset',
'junction-crosswalk-size',
'junction-stop-line-offset',
'lane-maneuver',
]);
const KINDS = [
'road-edge-offset',
@@ -38,7 +39,17 @@ const KINDS = [
'junction-crosswalk-inset',
'junction-crosswalk-size',
'junction-stop-line-offset',
'lane-maneuver',
];
const SUPPORTED_MANEUVERS = new Set([
'',
'through',
'left',
'right',
'through;left',
'through;right',
'through;left;right',
]);
const MIN_LANE_WIDTH_METERS = 2.4;
// Bounds for the control-marking offsets. The crosswalk may sit anywhere from the
// junction boundary out to a few metres; the stop line must stay clear of the
@@ -214,6 +225,15 @@ function snapshotCandidates(model, constraint) {
return roads;
}
function roadForLaneAnchor(model, laneAnchorId) {
if (typeof laneAnchorId !== 'string') return null;
for (const road of model.roads || []) {
const count = Number(road.laneCount) || 0;
for (let index = 1; index <= count; index += 1) if (laneId(road.id, index) === laneAnchorId) return road;
}
return null;
}
function reconcileConstraints(model, constraints, base, junctionData, context) {
const versionChanged = Boolean(
context.compilerGeometryVersion &&
@@ -224,6 +244,12 @@ function reconcileConstraints(model, constraints, base, junctionData, context) {
if (!constraint.enabled || !constraint.anchor || !['exact', 'recheck'].includes(constraint.status))
return constraint;
let matched = false;
if (constraint.kind === 'lane-maneuver') {
if (!roadForLaneAnchor(model, constraint.anchor.laneId))
return { ...constraint, status: 'stale', replayReason: 'anchor-missing' };
if (versionChanged) return { ...constraint, status: 'recheck' };
return constraint;
}
if (constraint.kind.startsWith('road-')) {
const road = roadForAnchor(model, constraint.anchor.roadId);
matched = Boolean(road);
@@ -537,6 +563,7 @@ function applyJunctionConstraint(model, constraint, junctionPlans, junctionData,
function solveConstraints(model, constraints, junctionData, diagnostics) {
const roadProfiles = new Map();
const junctionPlans = new Map();
const laneManeuvers = new Map();
const states = [];
for (const constraint of constraints) {
if (!constraint.enabled) {
@@ -564,6 +591,7 @@ function solveConstraints(model, constraints, junctionData, diagnostics) {
},
]),
);
const laneSnapshot = new Map(laneManeuvers);
const junctionSnapshot = new Map(
[...junctionPlans].map(([key, plan]) => [
key,
@@ -574,18 +602,44 @@ function solveConstraints(model, constraints, junctionData, diagnostics) {
},
]),
);
const applied = constraint.kind.startsWith('road-')
? applyRoadConstraint(model, constraint, roadProfiles, diagnostics)
: applyJunctionConstraint(model, constraint, junctionPlans, junctionData, diagnostics);
let applied;
if (constraint.kind === 'lane-maneuver') {
const laneId = constraint.anchor?.laneId;
const laneExists = Boolean(roadForLaneAnchor(model, laneId));
const maneuver = constraint.value?.maneuver;
const placement = constraint.value?.placementDistanceMeters;
if (!laneExists) {
blocking(diagnostics, constraint, 'direct-edit-lane-anchor-missing', '导向箭头车道锚点不存在。');
applied = false;
} else if (!SUPPORTED_MANEUVERS.has(maneuver)) {
blocking(diagnostics, constraint, 'direct-edit-lane-maneuver-invalid', '导向箭头 maneuver 不受支持。');
applied = false;
} else if (placement !== undefined && (!Number.isFinite(placement) || placement < 0)) {
blocking(diagnostics, constraint, 'direct-edit-lane-placement-invalid', '导向箭头位置必须是非负米数。');
applied = false;
} else {
laneManeuvers.set(laneId, {
maneuver,
...(placement === undefined ? {} : { placementDistanceMeters: placement }),
});
applied = true;
}
} else {
applied = constraint.kind.startsWith('road-')
? applyRoadConstraint(model, constraint, roadProfiles, diagnostics)
: applyJunctionConstraint(model, constraint, junctionPlans, junctionData, diagnostics);
}
if (!applied) {
roadProfiles.clear();
for (const [key, profile] of roadSnapshot) roadProfiles.set(key, profile);
junctionPlans.clear();
for (const [key, plan] of junctionSnapshot) junctionPlans.set(key, plan);
laneManeuvers.clear();
for (const [key, value] of laneSnapshot) laneManeuvers.set(key, value);
}
states.push(constraintState(constraint, applied, applied ? null : 'invalid'));
}
return { roadProfiles, junctionPlans, states };
return { roadProfiles, junctionPlans, laneManeuvers, states };
}
function roadAffects(roads, model, side, boundaryIndex) {
@@ -717,6 +771,38 @@ function makeRoadHandles(model, groups, reserves, constraints, roadProfiles) {
return handles;
}
function makeLaneManeuverHandles(model, constraints) {
const handles = [];
for (const road of model.roads || []) {
const laneCount = Number(road.laneCount) || 0;
const length = polylineLength(road.centerline);
if (!(length > 0) || road.centerline.length < 2) continue;
for (let index = 1; index <= laneCount; index += 1) {
const laneAnchorId = laneId(road.id, index);
const constraint = constraints.find(
(item) => item.kind === 'lane-maneuver' && item.anchor?.type === 'lane' && item.anchor.laneId === laneAnchorId,
);
const value = constraint?.value || {};
const placement = Number.isFinite(value.placementDistanceMeters) ? value.placementDistanceMeters : 6;
const station = Math.max(0, Math.min(1, 1 - placement / length));
const position = coordinateAt(road.centerline, station);
const axis = tangentAzimuth(road.centerline, station);
handles.push({
handleId: `handle:lane-maneuver:${laneAnchorId}`,
kind: 'lane-maneuver',
anchor: { type: 'lane', laneId: laneAnchorId, roadId: road.id },
position,
axisAzimuth: axis,
value: { current: placement, min: 0, max: length, unit: 'meter' },
...(constraint ? { constraintId: constraint.id } : {}),
affects: [`turn-arrow:${laneAnchorId}`],
editable: true,
});
}
}
return handles;
}
function junctionAffects(model, nodeId, segmentIds) {
const roads = (model.roads || []).filter((road) => segmentIds.has(road.segmentId));
const ids = [junctionSurfaceId(nodeId), ...roadAffects(roads, model)];
@@ -927,6 +1013,9 @@ function selectHandles(handles, model, selection) {
// they name their segment directly. Junction *shape* kinds stay out: those
// are JunctionTools', and the main map must not offer a second way in.
if (CONTROL_KINDS.has(handle.kind)) return handle.anchor.segmentId === wanted;
if (handle.kind === 'lane-maneuver') {
return handle.anchor?.type === 'lane' && segmentOf.get(handle.anchor.roadId) === wanted;
}
return handle.kind.startsWith('road-') && segmentOf.get(handle.anchor.roadId) === wanted;
});
}
@@ -950,6 +1039,7 @@ function resolveDirectEditConstraints(model, editDocument = null, context = {})
[
...makeRoadHandles(model || {}, groups, handles.reserves, reconciled, solved.roadProfiles),
...makeJunctionHandles(model || {}, junctionData, reconciled),
...makeLaneManeuverHandles(model || {}, reconciled),
],
model || {},
context.selection,
@@ -958,6 +1048,7 @@ function resolveDirectEditConstraints(model, editDocument = null, context = {})
schema: RESOLUTION_SCHEMA,
roadProfiles: solved.roadProfiles,
junctionPlans: solved.junctionPlans,
laneManeuvers: solved.laneManeuvers,
handles,
constraintStates: solved.states,
diagnostics,
@@ -979,5 +1070,6 @@ module.exports = {
KINDS,
GEOMETRY_VERSION,
roadEdgeOffsetRange,
SUPPORTED_MANEUVERS,
resolveDirectEditConstraints,
};
+13 -1
View File
@@ -18,9 +18,10 @@ const CONSTRAINT_ANCHORS = {
'junction-crosswalk-inset': 'junction-approach',
'junction-crosswalk-size': 'junction-approach',
'junction-stop-line-offset': 'junction-approach',
'lane-maneuver': 'lane',
};
const CONSTRAINT_KINDS = Object.freeze(Object.keys(CONSTRAINT_ANCHORS));
const ANCHOR_TYPES = new Set(['road-station', 'road-interval', 'junction-approach', 'junction-corner']);
const ANCHOR_TYPES = new Set(['road-station', 'road-interval', 'junction-approach', 'junction-corner', 'lane']);
const CONSTRAINT_STATUSES = new Set(['exact', 'recheck', 'pending', 'conflicted', 'stale']);
const TRANSITIONS = new Set(['smoothstep', 'linear']);
const SIDES = new Set(['left', 'right']);
@@ -80,6 +81,8 @@ function validateAnchor(scope, kind, anchor) {
requireString(scope, 'anchor.nodeId', anchor.nodeId);
requireString(scope, 'anchor.incomingRoadId', anchor.incomingRoadId);
requireString(scope, 'anchor.outgoingRoadId', anchor.outgoingRoadId);
} else if (expected === 'lane') {
requireString(scope, 'anchor.laneId', anchor.laneId);
}
}
@@ -134,6 +137,15 @@ function validateValue(scope, kind, value) {
// line on the crosswalk itself.
if (!isFiniteNumber(value.offsetMeters) || value.offsetMeters <= 0)
fail(scope, 'value.offsetMeters', 'must be a positive number of meters');
} else if (kind === 'lane-maneuver') {
const supported = new Set(['', 'through', 'left', 'right', 'through;left', 'through;right', 'through;left;right']);
if (typeof value.maneuver !== 'string' || !supported.has(value.maneuver))
fail(scope, 'value.maneuver', `must be one of ${[...supported].filter(Boolean).join(', ')} or an empty string`);
if (
value.placementDistanceMeters !== undefined &&
(!isFiniteNumber(value.placementDistanceMeters) || value.placementDistanceMeters < 0)
)
fail(scope, 'value.placementDistanceMeters', 'must be a non-negative number of meters');
}
}
+2 -29
View File
@@ -54,14 +54,6 @@ const DEFAULT_SIDEWALK_WIDTH_METERS = 2;
// The maneuvers `turn-lane-arrows.js` has templates for. `normalizeManeuver()`
// returns unknown input unchanged rather than failing, so an override has to be
// checked here — it cannot lean on normalisation to reject a typo.
const SUPPORTED_MANEUVERS = new Set([
'through',
'left',
'right',
'through;left',
'through;right',
'through;left;right',
]);
const DIRECTION_ARROW_INTERVAL_METERS = 32;
const DIRECTION_ARROW_ENDPOINT_BUFFER_METERS = 14;
const STOP_LINE_MAX_APPROACH_DISTANCE_METERS = 25;
@@ -275,8 +267,6 @@ function staleOverrideTarget(item, sets) {
if (item.kind === 'road' && typeof item.roadId === 'string' && !sets.roadIds.has(item.roadId)) return item.roadId;
if (item.kind === 'lane-separator-style' && typeof item.roadId === 'string' && !sets.roadIds.has(item.roadId))
return item.roadId;
if (item.kind === 'lane-maneuver' && typeof item.laneId === 'string' && !sets.laneIds.has(item.laneId))
return item.laneId;
if (item.kind === 'edge-line-style' && typeof item.roadId === 'string' && !sets.directionalRoadIds.has(item.roadId))
return item.roadId;
if (item.kind === 'center-line-style' && typeof item.segmentId === 'string' && !sets.segmentIds.has(item.segmentId))
@@ -338,21 +328,6 @@ function validateOverrides(value, model, options = {}) {
throw new Error(`Invalid road override ${key}.`);
for (const key of ['sidewalkLeft', 'sidewalkRight'])
if (item[key] !== undefined && typeof item[key] !== 'boolean') throw new Error(`Invalid road override ${key}.`);
} else if (item.kind === 'lane-maneuver') {
if (typeof item.laneId !== 'string' || (laneIds && !laneIds.has(item.laneId)))
throw new Error(`Unknown lane maneuver override target: ${item.laneId}`);
// '' is meaningful: it removes an arrow OSM asked for. Anything else must be
// a maneuver there is a template for.
if (typeof item.maneuver !== 'string' || (item.maneuver !== '' && !SUPPORTED_MANEUVERS.has(item.maneuver)))
throw new Error(
`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' ||
@@ -1553,7 +1528,8 @@ function compileLaneMarkings(model, overrides, lanes, diagnostics, junctionPlans
// marked — the tag being absent is exactly the case a user needs to fix.
// 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 v2Override = lane ? options.directEdit?.laneManeuvers?.get?.(lane.id) : undefined;
const override = v2Override;
const explicitManeuver = override ? override.maneuver : maneuvers[index];
if (!explicitManeuver) continue;
const maneuver = normalizeManeuver(explicitManeuver);
@@ -2375,9 +2351,6 @@ function compileConnectors(model, lanes, diagnostics, overrides, junctionPlans)
return { features, movements };
}
/** The maneuver an override sets for a lane, or `undefined` when none applies. */
function laneManeuverOverride(overrides, laneId) {
return (overrides?.overrides || []).find((entry) => entry.kind === 'lane-maneuver' && entry.laneId === laneId);
}
function safeTurnArrowPlacement(line, maneuver, axis, distance, controls) {
return (