feat(junction): enable complex carriageway width editing

This commit is contained in:
que01 committed 2026-09-08 12:09:44 +08:00
1 parent 56647fcdaa
commit c4bcbe8ff9
22 files changed
+449 -52

No files matched your search

+164 -9
View File
@@ -13,6 +13,7 @@ const {
junctionSurfaceId,
connectionConnectorId,
} = require('./native-ids');
const { topologyIdentity } = require('./junction-topology');
const RESOLUTION_SCHEMA = 'road-edit-resolution/v1';
const HANDLE_MANIFEST_SCHEMA = 'road-edit-handles/v1';
@@ -22,6 +23,7 @@ const SOLVED_KINDS = new Set([
'road-sidewalk-width',
'road-lane-divider',
'junction-approach-width',
'cluster-carriageway-width',
'junction-cutback',
'junction-corner-radius',
'junction-crosswalk-inset',
@@ -34,6 +36,7 @@ const KINDS = [
'road-sidewalk-width',
'road-lane-divider',
'junction-approach-width',
'cluster-carriageway-width',
'junction-cutback',
'junction-corner-radius',
'junction-crosswalk-inset',
@@ -154,6 +157,12 @@ function constraintFor(constraints, kind, anchor) {
item.anchor.incomingRoadId === anchor.incomingRoadId &&
item.anchor.outgoingRoadId === anchor.outgoingRoadId
);
if (kind === 'cluster-carriageway-width')
return (
item.anchor.type === 'cluster-carriageway' &&
item.anchor.clusterId === anchor.clusterId &&
item.anchor.carriagewayKey === anchor.carriagewayKey
);
if (kind === 'junction-crosswalk-inset' || kind === 'junction-stop-line-offset')
return (
item.anchor.type === 'junction-crossing' &&
@@ -171,6 +180,49 @@ function constraintFor(constraints, kind, anchor) {
});
}
function clusterCarriageways(model, junctionData, clusters) {
const result = new Map();
for (const cluster of Array.isArray(clusters) ? clusters : []) {
if (!cluster?.id || !Array.isArray(cluster.nodeIds)) continue;
const nodeIds = new Set(cluster.nodeIds.map(String));
const items = [];
for (const nodeId of nodeIds) {
for (const approach of junctionData.approaches.get(nodeId) || []) {
const roads = approach.items.map((item) => item.road);
const sourced = roads.filter((road) => road.sourceNodeIds?.length >= 2);
const internal =
sourced.length &&
sourced.every(
(road) => nodeIds.has(String(road.sourceNodeIds[0])) && nodeIds.has(String(road.sourceNodeIds.at(-1))),
);
if (internal || !roads.length) continue;
const source = {
nodeId: String(nodeId),
approach: { segmentId: approach.segmentId, roadIds: roads.map((road) => road.id) },
roads,
};
items.push({
clusterId: String(cluster.id),
nodeId: String(nodeId),
approach,
roads,
key: `cluster:${encodeURIComponent(cluster.id)}:carriageway:${topologyIdentity(source)}`,
sourceNodeIds: [...new Set(roads.flatMap((road) => (road.sourceNodeIds || []).map(String)))].sort(),
sourceRoadIds: roads.map((road) => String(road.id)).sort(),
widthMeters: approach.items.reduce((sum, item) => sum + item.road.widthMeters, 0),
laneCount: roads.reduce((sum, road) => sum + (Number(road.laneCount) || 0), 0),
});
}
}
const seen = new Set();
result.set(
String(cluster.id),
items.filter((item) => !seen.has(item.key) && seen.add(item.key)),
);
}
return result;
}
function numericValue(constraint, key, fallback) {
const value = constraint?.value?.[key];
return Number.isFinite(value) ? value : fallback;
@@ -243,6 +295,11 @@ function roadForLaneAnchor(model, laneAnchorId) {
}
function reconcileConstraints(model, constraints, base, junctionData, context) {
const clusterItems = clusterCarriageways(
model,
junctionData,
context.junctionTemplates?.clusters || context.junctionClusters || [],
);
const versionChanged = Boolean(
context.compilerGeometryVersion &&
base?.compilerGeometryVersion &&
@@ -258,6 +315,19 @@ function reconcileConstraints(model, constraints, base, junctionData, context) {
if (versionChanged) return { ...constraint, status: 'recheck' };
return constraint;
}
if (constraint.kind === 'cluster-carriageway-width') {
const item = clusterItems
.get(String(constraint.anchor.clusterId))
?.find((candidate) => candidate.key === constraint.anchor.carriagewayKey);
const sourcesMatch =
item &&
JSON.stringify(item.sourceNodeIds) ===
JSON.stringify([...constraint.anchor.sourceNodeIds].map(String).sort()) &&
JSON.stringify(item.sourceRoadIds) === JSON.stringify([...constraint.anchor.sourceRoadIds].map(String).sort());
if (!sourcesMatch) 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);
@@ -600,11 +670,13 @@ function applyJunctionConstraint(model, constraint, junctionPlans, junctionData,
}
}
function solveConstraints(model, constraints, junctionData, diagnostics) {
function solveConstraints(model, constraints, junctionData, diagnostics, clusters = []) {
const roadProfiles = new Map();
const junctionPlans = new Map();
const laneManeuvers = new Map();
const crossingControls = new Map();
const clusterPlans = new Map();
const clusterItems = clusterCarriageways(model, junctionData, clusters);
const states = [];
for (const constraint of constraints) {
if (!constraint.enabled) {
@@ -634,6 +706,7 @@ function solveConstraints(model, constraints, junctionData, diagnostics) {
);
const laneSnapshot = new Map(laneManeuvers);
const crossingSnapshot = new Map([...crossingControls].map(([key, value]) => [key, { ...value }]));
const clusterSnapshot = new Map([...clusterPlans].map(([key, value]) => [key, new Map(value)]));
const junctionSnapshot = new Map(
[...junctionPlans].map(([key, plan]) => [
key,
@@ -645,7 +718,32 @@ function solveConstraints(model, constraints, junctionData, diagnostics) {
]),
);
let applied;
if (constraint.kind === 'lane-maneuver') {
if (constraint.kind === 'cluster-carriageway-width') {
const item = clusterItems
.get(String(constraint.anchor?.clusterId))
?.find((candidate) => candidate.key === constraint.anchor?.carriagewayKey);
const width = Number(constraint.value?.widthMeters);
if (!item) {
blocking(diagnostics, constraint, 'direct-edit-cluster-carriageway-missing', '复杂路口车行道锚点不存在。');
applied = false;
} else if (
!Number.isFinite(width) ||
width < Math.max(MIN_LANE_WIDTH_METERS, item.laneCount * MIN_LANE_WIDTH_METERS) ||
width > item.widthMeters * 2
) {
blocking(
diagnostics,
constraint,
'direct-edit-cluster-carriageway-width-invalid',
'复杂路口车行道宽度超出当前可用范围。',
);
applied = false;
} else {
if (!clusterPlans.has(item.clusterId)) clusterPlans.set(item.clusterId, new Map());
clusterPlans.get(item.clusterId).set(item.key, width);
applied = true;
}
} else if (constraint.kind === 'lane-maneuver') {
const laneId = constraint.anchor?.laneId;
const laneExists = Boolean(roadForLaneAnchor(model, laneId));
const maneuver = constraint.value?.maneuver;
@@ -680,10 +778,12 @@ function solveConstraints(model, constraints, junctionData, diagnostics) {
for (const [key, value] of laneSnapshot) laneManeuvers.set(key, value);
crossingControls.clear();
for (const [key, value] of crossingSnapshot) crossingControls.set(key, value);
clusterPlans.clear();
for (const [key, value] of clusterSnapshot) clusterPlans.set(key, value);
}
states.push(constraintState(constraint, applied, applied ? null : 'invalid'));
}
return { roadProfiles, junctionPlans, laneManeuvers, crossingControls, states };
return { roadProfiles, junctionPlans, laneManeuvers, crossingControls, clusterPlans, states };
}
function roadAffects(roads, model, side, boundaryIndex) {
@@ -1079,6 +1179,45 @@ function makeJunctionHandles(model, junctionData, constraints) {
return handles;
}
function makeClusterCarriagewayHandles(model, junctionData, constraints, clusters) {
const handles = [];
for (const [clusterId, items] of clusterCarriageways(model, junctionData, clusters)) {
for (const item of items) {
const road = item.roads[0];
const line = item.approach.endpoint.side === 'start' ? road.centerline : [...road.centerline].reverse();
const station = Math.min(0.95, Math.max(0.05, item.approach.cutback / Math.max(item.approach.length, 1)));
const point = coordinateAt(line, station);
const constraint = constraintFor(constraints, 'cluster-carriageway-width', {
clusterId,
carriagewayKey: item.key,
});
handles.push({
handleId: `handle:cluster-carriageway-width:${item.key}`,
kind: 'cluster-carriageway-width',
anchor: {
type: 'cluster-carriageway',
clusterId,
carriagewayKey: item.key,
sourceNodeIds: item.sourceNodeIds,
sourceRoadIds: item.sourceRoadIds,
},
position: point,
axisAzimuth: (tangentAzimuth(line, station) + 90) % 360,
value: {
current: numericValue(constraint, 'widthMeters', item.widthMeters),
min: Math.max(MIN_LANE_WIDTH_METERS, item.laneCount * MIN_LANE_WIDTH_METERS),
max: Math.max(item.widthMeters * 2, item.laneCount * MIN_LANE_WIDTH_METERS),
unit: 'meter',
},
...(constraint ? { constraintId: constraint.id } : {}),
affects: [`complex-junction:${clusterId}:carriageway:${item.approach.segmentId}`],
editable: true,
});
}
}
return handles;
}
/**
* Which handles a selection wants.
*
@@ -1093,8 +1232,10 @@ function makeJunctionHandles(model, junctionData, constraints) {
*/
function selectHandles(handles, model, selection) {
if (!selection || !selection.type) return handles;
// Cluster object identities are read-only until their geometry edit path exists.
if (selection.type === 'cluster') return [];
if (selection.type === 'cluster')
return handles.filter(
(handle) => handle.kind === 'cluster-carriageway-width' && handle.anchor?.clusterId === String(selection.id),
);
if (selection.type === 'segment') {
const segmentOf = new Map((model.roads || []).map((road) => [road.id, road.segmentId]));
const wanted = String(selection.id);
@@ -1124,18 +1265,21 @@ function resolveDirectEditConstraints(model, editDocument = null, context = {})
const groups = roadGroups(model || {});
const junctionData = junctionReserves(model || {});
const reconciled = reconcileConstraints(model || {}, constraints, editDocument?.base, junctionData, context);
const solved = solveConstraints(model || {}, reconciled, junctionData, diagnostics);
const clusters = context.junctionTemplates?.clusters || context.junctionClusters || [];
const solved = solveConstraints(model || {}, reconciled, junctionData, diagnostics, clusters);
const effectiveJunctionPlans = buildEffectiveJunctionPlans(
model || {},
solved.junctionPlans,
junctionData,
context.junctionTemplates?.clusters || context.junctionClusters || [],
clusters,
solved.clusterPlans,
);
handles.reserves = junctionData.reserves;
handles.handles = selectHandles(
[
...makeRoadHandles(model || {}, groups, handles.reserves, reconciled, solved.roadProfiles),
...makeJunctionHandles(model || {}, junctionData, reconciled),
...makeClusterCarriagewayHandles(model || {}, junctionData, reconciled, clusters),
...makeLaneManeuverHandles(model || {}, reconciled),
],
model || {},
@@ -1147,6 +1291,7 @@ function resolveDirectEditConstraints(model, editDocument = null, context = {})
junctionPlans: solved.junctionPlans,
effectiveJunctionPlans,
crossingControls: solved.crossingControls,
clusterPlans: solved.clusterPlans,
laneManeuvers: solved.laneManeuvers,
handles,
constraintStates: solved.states,
@@ -1158,7 +1303,7 @@ function resolveDirectEditConstraints(model, editDocument = null, context = {})
// JunctionTools. Clusters deliberately remain read-only in this phase: their
// effective plan is assembled from member nodes without rewriting constraints or
// silently splitting the cluster into independent editors.
function buildEffectiveJunctionPlans(model, solvedPlans, junctionData, clusters = []) {
function buildEffectiveJunctionPlans(model, solvedPlans, junctionData, clusters = [], clusterPlans = new Map()) {
const nodePlan = (nodeId) => {
const id = String(nodeId);
const solved = solvedPlans.get(id);
@@ -1206,13 +1351,23 @@ function buildEffectiveJunctionPlans(model, solvedPlans, junctionData, clusters
})),
);
const corners = Object.assign({}, ...members.map((member) => member.corners || {}));
const carriageways = clusterCarriageways(model, junctionData, [cluster]).get(String(cluster.id)) || [];
const widths = clusterPlans.get(String(cluster.id)) || new Map();
result.set(`cluster:${cluster.id}`, {
schema: 'junction-effective-plan/v1',
junctionRef: { type: 'cluster', id: String(cluster.id) },
nodeIds: cluster.nodeIds.map(String),
approaches,
carriageways: carriageways.map((item) => ({
key: item.key,
nodeId: item.nodeId,
segmentId: item.approach.segmentId,
widthMeters: widths.get(item.key) ?? item.widthMeters,
sourceNodeIds: item.sourceNodeIds,
sourceRoadIds: item.sourceRoadIds,
})),
corners,
readOnly: true,
readOnly: false,
});
}
return result;