feat(workbench): drag control markings as elements

This commit is contained in:
que01 committed 2026-09-02 16:04:38 +08:00
1 parent 834470cfa1
commit 31552a5b6f
9 files changed
+244 -75

No files matched your search

+145 -62
View File
@@ -154,6 +154,14 @@ function constraintFor(constraints, kind, anchor) {
item.anchor.incomingRoadId === anchor.incomingRoadId &&
item.anchor.outgoingRoadId === anchor.outgoingRoadId
);
if (kind === 'junction-crosswalk-inset' || kind === 'junction-stop-line-offset')
return (
item.anchor.type === 'junction-crossing' &&
item.anchor.crossingId === anchor.crossingId &&
item.anchor.nodeId === anchor.nodeId &&
item.anchor.segmentId === anchor.segmentId &&
item.anchor.roadId === anchor.roadId
);
return (
item.anchor.type === 'junction-approach' &&
item.anchor.nodeId === anchor.nodeId &&
@@ -437,8 +445,40 @@ function applyRoadConstraint(model, constraint, profiles, diagnostics) {
return true;
}
function applyJunctionConstraint(model, constraint, junctionPlans, junctionData, diagnostics) {
function applyJunctionConstraint(model, constraint, junctionPlans, junctionData, diagnostics, crossingControls) {
const anchor = constraint.anchor || {};
if (anchor.type === 'junction-crossing') {
const crossing = (model.crossings || []).find((item) => String(item.id) === String(anchor.crossingId));
const road = (model.roads || []).find((item) => item.id === anchor.roadId);
const approach = crossing && road ? approachForCrossing(model, crossing) : null;
if (
!crossing ||
!road ||
!approach ||
approach.road.id !== road.id ||
String(road.segmentId) !== String(anchor.segmentId)
) {
blocking(diagnostics, constraint, 'direct-edit-crossing-anchor-missing', '直接编辑约束的控制元素锚点不存在。');
return false;
}
const value = Number(constraint.value?.insetMeters ?? constraint.value?.offsetMeters);
const valid =
constraint.kind === 'junction-crosswalk-inset'
? Number.isFinite(value) && value >= 0 && value <= MAX_CROSSWALK_INSET_METERS
: Number.isFinite(value) && value >= MIN_STOP_LINE_OFFSET_METERS && value <= approach.junctionDistanceMeters;
if (!valid) {
blocking(diagnostics, constraint, 'direct-edit-crossing-value-invalid', '控制线位置超出当前元素可用范围。');
return false;
}
// Keep the geometry value in a separate per-crossing map. Multiple crossings
// may share one approach, so storing this on the approach would couple them.
const crossingKey = String(anchor.crossingId);
const control = crossingControls.get(crossingKey) || {};
crossingControls.set(crossingKey, control);
if (constraint.kind === 'junction-crosswalk-inset') control.crosswalkInsetMeters = value;
else control.stopLineOffsetMeters = value;
return true;
}
const approaches = junctionData.approaches.get(String(anchor.nodeId));
const approach = approaches?.find((item) => item.segmentId === anchor.segmentId);
if (constraint.kind !== 'junction-corner-radius' && !approach) {
@@ -564,6 +604,7 @@ function solveConstraints(model, constraints, junctionData, diagnostics) {
const roadProfiles = new Map();
const junctionPlans = new Map();
const laneManeuvers = new Map();
const crossingControls = new Map();
const states = [];
for (const constraint of constraints) {
if (!constraint.enabled) {
@@ -592,6 +633,7 @@ function solveConstraints(model, constraints, junctionData, diagnostics) {
]),
);
const laneSnapshot = new Map(laneManeuvers);
const crossingSnapshot = new Map([...crossingControls].map(([key, value]) => [key, { ...value }]));
const junctionSnapshot = new Map(
[...junctionPlans].map(([key, plan]) => [
key,
@@ -627,7 +669,7 @@ function solveConstraints(model, constraints, junctionData, diagnostics) {
} else {
applied = constraint.kind.startsWith('road-')
? applyRoadConstraint(model, constraint, roadProfiles, diagnostics)
: applyJunctionConstraint(model, constraint, junctionPlans, junctionData, diagnostics);
: applyJunctionConstraint(model, constraint, junctionPlans, junctionData, diagnostics, crossingControls);
}
if (!applied) {
roadProfiles.clear();
@@ -636,10 +678,12 @@ function solveConstraints(model, constraints, junctionData, diagnostics) {
for (const [key, plan] of junctionSnapshot) junctionPlans.set(key, plan);
laneManeuvers.clear();
for (const [key, value] of laneSnapshot) laneManeuvers.set(key, value);
crossingControls.clear();
for (const [key, value] of crossingSnapshot) crossingControls.set(key, value);
}
states.push(constraintState(constraint, applied, applied ? null : 'invalid'));
}
return { roadProfiles, junctionPlans, laneManeuvers, states };
return { roadProfiles, junctionPlans, laneManeuvers, crossingControls, states };
}
function roadAffects(roads, model, side, boundaryIndex) {
@@ -820,15 +864,7 @@ function junctionAffects(model, nodeId, segmentIds) {
function makeJunctionHandles(model, junctionData, constraints) {
const handles = [];
// Which crossing each approach road serves, so the crosswalk handle can publish
// a ceiling that actually moves the zebra. Built once: `approachForCrossing()`
// scans the roads per crossing.
const crossingByRoadId = new Map();
const arrivals = arrivalEndpointIds(model || {});
for (const crossing of model.crossings || []) {
const match = approachForCrossing(model, crossing, arrivals);
if (match) crossingByRoadId.set(match.road.id, match);
}
for (const [nodeId, approaches] of junctionData.approaches) {
const segmentIds = new Set(approaches.map((item) => item.segmentId));
for (const approach of approaches) {
@@ -886,60 +922,106 @@ function makeJunctionHandles(model, junctionData, constraints) {
tangent,
cutbackConstraint,
);
// Control markings sit inside the reserve but are not junction *shape*, so
// they drag along the approach tangent like the cutback does.
//
// The ceiling is per-approach, not the flat MAX_CROSSWALK_INSET_METERS this
// used to publish: `crossingJunctionInset()` saturates once the inset
// reaches the cutback, and sooner when the crossing sits far from the
// junction. Measured across eight groups the real ceiling ran 1.0–4.5m
// against a declared 8, leaving 44–88% of the range moving nothing and
// saying nothing — the dead-control failure `flag.ts` exists to prevent.
// `approach.items` can hold both directional roads of a segment, and each
// may serve a different crossing at a different junction. Take the one that
// actually ends here — the same last-source-node rule
// `crossingJunctionInset()` uses to find its junction plan. Without the
// filter, two handles on one segment published the same ceiling, and one of
// them was wrong.
const crossing = approach.items
.filter((item) => String(item.road.sourceNodeIds?.at(-1)) === String(nodeId))
.map((item) => crossingByRoadId.get(item.road.id))
.find(Boolean);
const crosswalkConstraint = constraintFor(constraints, 'junction-crosswalk-inset', anchor);
const insetValue = numericValue(crosswalkConstraint, 'insetMeters', CROSSWALK_JUNCTION_INSET_METERS);
const cutbackValue = numericValue(cutbackConstraint, 'cutbackMeters', approach.cutback);
const ceiling = crossing
? effectiveCrosswalkInsetMax(cutbackValue, crossing.junctionDistanceMeters)
: MAX_CROSSWALK_INSET_METERS;
// Never below the value already in force: a max under `current` would be a
// manifest that contradicts itself. Equal to it is honest — it says this
// approach has no headroom left, which is a real state.
// Synthetic models used by older resolver consumers may not carry crossing
// features. Keep their approach-level controls for compatibility; real
// models below always publish element-owned crossing handles.
if (!(model.crossings || []).length) {
const insetConstraint = constraintFor(constraints, 'junction-crosswalk-inset', anchor);
const insetValue = numericValue(insetConstraint, 'insetMeters', CROSSWALK_JUNCTION_INSET_METERS);
add(
'junction-crosswalk-inset',
insetValue,
0,
MAX_CROSSWALK_INSET_METERS,
(tangent + 180) % 360,
insetConstraint,
controlPoint(insetValue, -1),
);
const stopConstraint = constraintFor(constraints, 'junction-stop-line-offset', anchor);
const stopValue = numericValue(stopConstraint, 'offsetMeters', STOP_LINE_OFFSET_METERS);
add(
'junction-stop-line-offset',
stopValue,
MIN_STOP_LINE_OFFSET_METERS,
Math.max(MIN_STOP_LINE_OFFSET_METERS, approach.length * 0.45),
tangent,
stopConstraint,
controlPoint(stopValue, 1),
);
}
}
// Control-marking handles are element-owned. A single approach may serve
// multiple crossings, so each handle carries the crossing id as well as the
// road context needed to resolve its geometry.
for (const crossing of model.crossings || []) {
const match = approachForCrossing(model, crossing, arrivals);
if (!match || String(match.road.sourceNodeIds?.at(-1)) !== String(nodeId)) continue;
const approach = approaches.find((item) => item.segmentId === match.road.segmentId);
if (!approach) continue;
const road = match.road;
const line = approach.endpoint.side === 'start' ? road.centerline : [...road.centerline].reverse();
const tangent = tangentAzimuth(line, Math.min(0.95, approach.cutback / approach.length));
const placementAzimuth = tangent;
const anchorBase = {
type: 'junction-crossing',
crossingId: String(crossing.id),
nodeId: String(nodeId),
segmentId: approach.segmentId,
roadId: road.id,
};
const controlPoint = (offsetMeters, lateralMeters = 0) => {
const point = coordinateAt(line, Math.min(0.98, Math.max(0.02, offsetMeters / Math.max(approach.length, 1))));
return lateralMeters ? offsetCoordinate(point, (tangent + 90) % 360, lateralMeters) : point;
};
const insetConstraint = constraintFor(constraints, 'junction-crosswalk-inset', anchorBase);
const insetValue = numericValue(insetConstraint, 'insetMeters', CROSSWALK_JUNCTION_INSET_METERS);
// The solver may saturate the inset at the junction boundary; the
// configured value is still the closest stable client-side knob position.
const insetApplied = insetValue;
const insetMax = Math.min(
MAX_CROSSWALK_INSET_METERS,
Math.max(insetValue, Number.isFinite(ceiling) ? ceiling : MAX_CROSSWALK_INSET_METERS),
);
add(
'junction-crosswalk-inset',
insetValue,
0,
insetMax,
// The approach tangent points away from the junction. Increasing an
// inset moves the crosswalk toward the junction, so expose the inverse
// axis to keep positive drag values aligned with the handle's meaning.
(tangent + 180) % 360,
crosswalkConstraint,
controlPoint(insetValue, -1),
);
const stopLineConstraint = constraintFor(constraints, 'junction-stop-line-offset', anchor);
add(
'junction-stop-line-offset',
numericValue(stopLineConstraint, 'offsetMeters', STOP_LINE_OFFSET_METERS),
MIN_STOP_LINE_OFFSET_METERS,
Math.max(MIN_STOP_LINE_OFFSET_METERS, approach.length * 0.45),
tangent,
stopLineConstraint,
controlPoint(numericValue(stopLineConstraint, 'offsetMeters', STOP_LINE_OFFSET_METERS), 1),
Math.max(insetValue, effectiveCrosswalkInsetMax(approach.cutback, match.junctionDistanceMeters) || insetValue),
);
handles.push({
handleId: `handle:junction-crosswalk-inset:crossing/${crossing.id}`,
kind: 'junction-crosswalk-inset',
anchor: anchorBase,
// Anchor the knob to the actual crossing feature. The inset is a value
// along the approach, not a reliable screen station from the junction;
// deriving the point from junction distance put the knob off the zebra.
position: offsetCoordinate(crossing.coordinate, placementAzimuth, insetApplied),
axisAzimuth: (tangent + 180) % 360,
value: { current: insetValue, min: 0, max: insetMax, unit: 'meter' },
...(insetConstraint ? { constraintId: insetConstraint.id } : {}),
affects: [`crosswalk:node/${crossing.id}`],
editable: true,
});
const stopConstraint = constraintFor(constraints, 'junction-stop-line-offset', anchorBase);
const stopValue = numericValue(stopConstraint, 'offsetMeters', STOP_LINE_OFFSET_METERS);
handles.push({
handleId: `handle:junction-stop-line-offset:crossing/${crossing.id}`,
kind: 'junction-stop-line-offset',
anchor: anchorBase,
// The stop line is measured back from the crossing, so its knob follows
// the rendered line while remaining independent of the road cutback.
position: offsetCoordinate(
offsetCoordinate(crossing.coordinate, placementAzimuth, insetApplied),
placementAzimuth,
-stopValue,
),
// The compiled stop line applies `-offset` along the approach axis, so
// this tangent is the positive drag direction for increasing offset.
axisAzimuth: tangent,
value: {
current: stopValue,
min: MIN_STOP_LINE_OFFSET_METERS,
max: Math.max(MIN_STOP_LINE_OFFSET_METERS, approach.length * 0.45),
unit: 'meter',
},
...(stopConstraint ? { constraintId: stopConstraint.id } : {}),
affects: [`stop-line:node/${crossing.id}:1`],
editable: true,
});
}
const ordered = [...approaches].sort((a, b) => a.segmentId.localeCompare(b.segmentId));
for (let index = 0; index < ordered.length; index += 1) {
@@ -1048,6 +1130,7 @@ function resolveDirectEditConstraints(model, editDocument = null, context = {})
schema: RESOLUTION_SCHEMA,
roadProfiles: solved.roadProfiles,
junctionPlans: solved.junctionPlans,
crossingControls: solved.crossingControls,
laneManeuvers: solved.laneManeuvers,
handles,
constraintStates: solved.states,
+23 -2
View File
@@ -21,7 +21,14 @@ const CONSTRAINT_ANCHORS = {
'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', 'lane']);
const ANCHOR_TYPES = new Set([
'road-station',
'road-interval',
'junction-approach',
'junction-crossing',
'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']);
@@ -63,7 +70,11 @@ function validateAnchor(scope, kind, anchor) {
const expected = CONSTRAINT_ANCHORS[kind];
if (!anchor || typeof anchor !== 'object') fail(scope, 'anchor', 'is required');
if (!ANCHOR_TYPES.has(anchor.type)) fail(scope, 'anchor.type', `is not a known anchor type (got ${anchor.type})`);
if (anchor.type !== expected) fail(scope, 'anchor.type', `must be ${expected} for kind ${kind} (got ${anchor.type})`);
// Existing v2 documents used junction-approach for control markings. Accept
// those anchors for replay while new manifests write element-level crossings.
const crossingControl = expected === 'junction-approach' && anchor.type === 'junction-crossing';
if (anchor.type !== expected && !crossingControl)
fail(scope, 'anchor.type', `must be ${expected} for kind ${kind} (got ${anchor.type})`);
if (anchor.side !== undefined && !SIDES.has(anchor.side)) fail(scope, 'anchor.side', 'must be left or right');
if (expected === 'road-station') {
requireString(scope, 'anchor.roadId', anchor.roadId);
@@ -74,9 +85,19 @@ function validateAnchor(scope, kind, anchor) {
requireStation(scope, 'anchor.endStation', anchor.endStation);
if (anchor.startStation >= anchor.endStation)
fail(scope, 'anchor.startStation', 'must be strictly less than anchor.endStation');
} else if (crossingControl) {
requireString(scope, 'anchor.crossingId', anchor.crossingId);
requireString(scope, 'anchor.nodeId', anchor.nodeId);
requireString(scope, 'anchor.segmentId', anchor.segmentId);
requireString(scope, 'anchor.roadId', anchor.roadId);
} else if (expected === 'junction-approach') {
requireString(scope, 'anchor.nodeId', anchor.nodeId);
requireString(scope, 'anchor.segmentId', anchor.segmentId);
} else if (expected === 'junction-crossing') {
requireString(scope, 'anchor.crossingId', anchor.crossingId);
requireString(scope, 'anchor.nodeId', anchor.nodeId);
requireString(scope, 'anchor.segmentId', anchor.segmentId);
requireString(scope, 'anchor.roadId', anchor.roadId);
} else if (expected === 'junction-corner') {
requireString(scope, 'anchor.nodeId', anchor.nodeId);
requireString(scope, 'anchor.incomingRoadId', anchor.incomingRoadId);
+19 -6
View File
@@ -777,7 +777,13 @@ function compileGeometry(model, overrides = { overrides: [] }, options = {}) {
});
const edgeLines =
options.edgeLines === false ? [] : compileEdgeLines(model, overrides, junctionPlans, options, features);
const controls = compileControlMarkings(model, lanes, diagnostics, junctionPlans);
const controls = compileControlMarkings(
model,
lanes,
diagnostics,
junctionPlans,
options.directEdit?.crossingControls,
);
const allControls = {
crosswalks: [...controls.crosswalks, ...generatedComplexCrosswalks],
stopLines: [...controls.stopLines, ...generatedComplexStopLines],
@@ -1223,7 +1229,7 @@ function edgeLineStyle(overrides, roadId, side) {
return value ? { color: value.color, pattern: value.pattern } : { color: 'white', pattern: 'solid' };
}
function compileControlMarkings(model, lanes, diagnostics, junctionPlans = new Map()) {
function compileControlMarkings(model, lanes, diagnostics, junctionPlans = new Map(), crossingControls = new Map()) {
const crosswalks = [];
const stopLines = [];
const arrivalEndpointIds = new Set(
@@ -1267,7 +1273,10 @@ function compileControlMarkings(model, lanes, diagnostics, junctionPlans = new M
(a, b) => a.junctionDistanceMeters - b.junctionDistanceMeters || a.placement.distance - b.placement.distance,
)[0];
const crosswalkCandidate = approach || candidate;
const junctionInsetMeters = approach ? crossingJunctionInset(approach, junctionPlans, model) : 0;
const crossingOverride = crossingControls.get(String(crossing.id));
const junctionInsetMeters = approach
? crossingJunctionInset(approach, junctionPlans, model, crossingOverride?.crosswalkInsetMeters)
: 0;
const controlCenter = offsetByMeters(crossing.coordinate, crosswalkCandidate.placement.axis, junctionInsetMeters);
const { axis } = crosswalkCandidate.placement;
const across = [-axis[1], axis[0]];
@@ -1314,7 +1323,9 @@ function compileControlMarkings(model, lanes, diagnostics, junctionPlans = new M
const stopCenter = offsetByMeters(
laneCenterAtCrossing,
approach.placement.axis,
-approachControls(junctionPlans, approach.road, model).stopLineOffsetMeters,
-(Number.isFinite(crossingOverride?.stopLineOffsetMeters)
? crossingOverride.stopLineOffsetMeters
: approachControls(junctionPlans, approach.road, model).stopLineOffsetMeters),
);
stopLines.push(
controlFeature(
@@ -1379,11 +1390,13 @@ function approachControls(junctionPlans, road, model) {
};
}
function crossingJunctionInset(candidate, junctionPlans, model) {
function crossingJunctionInset(candidate, junctionPlans, model, overrideInset) {
const junctionNodeId = candidate.road.sourceNodeIds.at(-1);
const plan = junctionPlans.get(junctionNodeId);
if (!plan) return 0;
const inset = approachControls(junctionPlans, candidate.road, model).crosswalkInsetMeters;
const inset = Number.isFinite(overrideInset)
? overrideInset
: approachControls(junctionPlans, candidate.road, model).crosswalkInsetMeters;
const targetDistance = Math.max(0, plan.cutbackMeters - inset);
return Math.min(CROSSWALK_MAX_JUNCTION_INSET_METERS, Math.max(0, candidate.junctionDistanceMeters - targetDistance));
}