feat(junction): edit complex cutbacks and corners

This commit is contained in:
que01 committed 2026-09-08 14:30:43 +08:00
1 parent d0e1f62f2b
commit 80f122be46
15 files changed
+354 -38

No files matched your search

+202 -11
View File
@@ -24,6 +24,8 @@ const SOLVED_KINDS = new Set([
'road-lane-divider',
'junction-approach-width',
'cluster-carriageway-width',
'cluster-carriageway-cutback',
'cluster-corner-radius',
'junction-cutback',
'junction-corner-radius',
'junction-crosswalk-inset',
@@ -37,6 +39,8 @@ const KINDS = [
'road-lane-divider',
'junction-approach-width',
'cluster-carriageway-width',
'cluster-carriageway-cutback',
'cluster-corner-radius',
'junction-cutback',
'junction-corner-radius',
'junction-crosswalk-inset',
@@ -163,6 +167,18 @@ function constraintFor(constraints, kind, anchor) {
item.anchor.clusterId === anchor.clusterId &&
item.anchor.carriagewayKey === anchor.carriagewayKey
);
if (kind === 'cluster-carriageway-cutback')
return (
item.anchor.type === 'cluster-carriageway' &&
item.anchor.clusterId === anchor.clusterId &&
item.anchor.carriagewayKey === anchor.carriagewayKey
);
if (kind === 'cluster-corner-radius')
return (
item.anchor.type === 'cluster-corner' &&
item.anchor.clusterId === anchor.clusterId &&
item.anchor.cornerKey === anchor.cornerKey
);
if (kind === 'junction-crosswalk-inset' || kind === 'junction-stop-line-offset')
return (
item.anchor.type === 'junction-crossing' &&
@@ -206,6 +222,7 @@ function clusterCarriageways(model, junctionData, clusters) {
nodeId: String(nodeId),
approach,
roads,
identity: topologyIdentity(source),
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(),
@@ -223,6 +240,51 @@ function clusterCarriageways(model, junctionData, clusters) {
return result;
}
function clusterApproachLine(item) {
const road = item.approach.items[0]?.road;
if (!road) return [];
return item.approach.endpoint.side === 'end' ? [...road.centerline].reverse() : road.centerline;
}
function headingDifference(first, second) {
return Math.abs(((first - second + 540) % 360) - 180);
}
function averageHeading(headings) {
const vector = headings.reduce(
(sum, heading) => {
const radians = (heading * Math.PI) / 180;
return [sum[0] + Math.sin(radians), sum[1] + Math.cos(radians)];
},
[0, 0],
);
return ((Math.atan2(vector[0], vector[1]) * 180) / Math.PI + 360) % 360;
}
// Mirrors resolveComplexJunctionTopology(): corner identity belongs to adjacent
// external arms, not whichever individual carriageways happened to be listed.
function clusterArms(clusterId, items) {
const arms = [];
for (const item of [...items].sort((a, b) => a.identity.localeCompare(b.identity))) {
item.heading = tangentAzimuth(clusterApproachLine(item), 0.05);
let arm = arms.find((candidate) => headingDifference(candidate.representative.heading, item.heading) < 20);
if (!arm) {
arm = { representative: item, members: [] };
arms.push(arm);
}
arm.members.push(item);
}
for (const arm of arms) {
arm.heading = averageHeading(arm.members.map((member) => member.heading));
arm.key = `cluster:${encodeURIComponent(clusterId)}:arm:${arm.members
.map((member) => member.identity)
.sort()
.join('|')}`;
}
arms.sort((a, b) => a.heading - b.heading || a.representative.identity.localeCompare(b.representative.identity));
return arms;
}
function numericValue(constraint, key, fallback) {
const value = constraint?.value?.[key];
return Number.isFinite(value) ? value : fallback;
@@ -300,6 +362,17 @@ function reconcileConstraints(model, constraints, base, junctionData, context) {
junctionData,
context.junctionTemplates?.clusters || context.junctionClusters || [],
);
const clusterCorners = new Map(
[...clusterItems].map(([clusterId, items]) => [
clusterId,
new Map(
clusterArms(clusterId, items).map((arm, index, arms) => [
`corner:${arm.key}>${arms[(index + 1) % arms.length].key}`,
[arm.representative.key, arms[(index + 1) % arms.length].representative.key],
]),
),
]),
);
const versionChanged = Boolean(
context.compilerGeometryVersion &&
base?.compilerGeometryVersion &&
@@ -328,6 +401,22 @@ function reconcileConstraints(model, constraints, base, junctionData, context) {
if (versionChanged) return { ...constraint, status: 'recheck' };
return constraint;
}
if (constraint.kind === 'cluster-carriageway-cutback') {
const item = clusterItems
.get(String(constraint.anchor.clusterId))
?.find((candidate) => candidate.key === constraint.anchor.carriagewayKey);
if (!item) return { ...constraint, status: 'stale', replayReason: 'anchor-missing' };
if (versionChanged) return { ...constraint, status: 'recheck' };
return constraint;
}
if (constraint.kind === 'cluster-corner-radius') {
const expected = clusterCorners.get(String(constraint.anchor.clusterId))?.get(constraint.anchor.cornerKey);
const supplied = [...constraint.anchor.carriagewayKeys].sort();
if (!expected || JSON.stringify([...expected].sort()) !== JSON.stringify(supplied))
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);
@@ -676,6 +765,8 @@ function solveConstraints(model, constraints, junctionData, diagnostics, cluster
const laneManeuvers = new Map();
const crossingControls = new Map();
const clusterPlans = new Map();
const clusterCutbacks = new Map();
const clusterCorners = new Map();
const clusterItems = clusterCarriageways(model, junctionData, clusters);
const states = [];
for (const constraint of constraints) {
@@ -707,6 +798,8 @@ function solveConstraints(model, constraints, junctionData, diagnostics, cluster
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 cutbackSnapshot = new Map([...clusterCutbacks].map(([key, value]) => [key, new Map(value)]));
const cornerSnapshot = new Map([...clusterCorners].map(([key, value]) => [key, new Map(value)]));
const junctionSnapshot = new Map(
[...junctionPlans].map(([key, plan]) => [
key,
@@ -718,29 +811,48 @@ function solveConstraints(model, constraints, junctionData, diagnostics, cluster
]),
);
let applied;
if (constraint.kind === 'cluster-carriageway-width') {
if (constraint.kind === 'cluster-carriageway-width' || constraint.kind === 'cluster-carriageway-cutback') {
const item = clusterItems
.get(String(constraint.anchor?.clusterId))
?.find((candidate) => candidate.key === constraint.anchor?.carriagewayKey);
const width = Number(constraint.value?.widthMeters);
const value = Number(
constraint.kind === 'cluster-carriageway-width'
? constraint.value?.widthMeters
: constraint.value?.cutbackMeters,
);
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
!Number.isFinite(value) ||
(constraint.kind === 'cluster-carriageway-width' &&
(value < Math.max(MIN_LANE_WIDTH_METERS, item.laneCount * MIN_LANE_WIDTH_METERS) ||
value > item.widthMeters * 2)) ||
(constraint.kind === 'cluster-carriageway-cutback' && (value < 1 || value > item.approach.length * 0.45))
) {
blocking(
diagnostics,
constraint,
'direct-edit-cluster-carriageway-width-invalid',
'复杂路口车行道宽度超出当前可用范围。',
'direct-edit-cluster-carriageway-geometry-invalid',
'复杂路口车行道参数超出当前可用范围。',
);
applied = false;
} else {
if (!clusterPlans.has(item.clusterId)) clusterPlans.set(item.clusterId, new Map());
clusterPlans.get(item.clusterId).set(item.key, width);
const target = constraint.kind === 'cluster-carriageway-width' ? clusterPlans : clusterCutbacks;
if (!target.has(item.clusterId)) target.set(item.clusterId, new Map());
target.get(item.clusterId).set(item.key, value);
applied = true;
}
} else if (constraint.kind === 'cluster-corner-radius') {
const cluster = clusters.find((item) => String(item.id) === String(constraint.anchor?.clusterId));
const radius = Number(constraint.value?.radiusMeters);
const keys = constraint.anchor?.carriagewayKeys || [];
if (!cluster || !Number.isFinite(radius) || radius < 1 || radius > 25 || keys.length !== 2) {
blocking(diagnostics, constraint, 'direct-edit-cluster-corner-invalid', '复杂路口角部圆角超出当前可用范围。');
applied = false;
} else {
if (!clusterCorners.has(String(cluster.id))) clusterCorners.set(String(cluster.id), new Map());
clusterCorners.get(String(cluster.id)).set(String(constraint.anchor.cornerKey), radius);
applied = true;
}
} else if (constraint.kind === 'lane-maneuver') {
@@ -780,10 +892,23 @@ function solveConstraints(model, constraints, junctionData, diagnostics, cluster
for (const [key, value] of crossingSnapshot) crossingControls.set(key, value);
clusterPlans.clear();
for (const [key, value] of clusterSnapshot) clusterPlans.set(key, value);
clusterCutbacks.clear();
for (const [key, value] of cutbackSnapshot) clusterCutbacks.set(key, value);
clusterCorners.clear();
for (const [key, value] of cornerSnapshot) clusterCorners.set(key, value);
}
states.push(constraintState(constraint, applied, applied ? null : 'invalid'));
}
return { roadProfiles, junctionPlans, laneManeuvers, crossingControls, clusterPlans, states };
return {
roadProfiles,
junctionPlans,
laneManeuvers,
crossingControls,
clusterPlans,
clusterCutbacks,
clusterCorners,
states,
};
}
function roadAffects(roads, model, side, boundaryIndex) {
@@ -1213,6 +1338,69 @@ function makeClusterCarriagewayHandles(model, junctionData, constraints, cluster
affects: [`complex-junction:${clusterId}:carriageway:${item.approach.segmentId}`],
editable: true,
});
const cutbackConstraint = constraintFor(constraints, 'cluster-carriageway-cutback', {
clusterId,
carriagewayKey: item.key,
});
handles.push({
handleId: `handle:cluster-carriageway-cutback:${item.key}`,
kind: 'cluster-carriageway-cutback',
anchor: {
type: 'cluster-carriageway',
clusterId,
carriagewayKey: item.key,
sourceNodeIds: item.sourceNodeIds,
sourceRoadIds: item.sourceRoadIds,
},
position: coordinateAt(
line,
Math.min(0.95, Math.max(0.05, item.approach.cutback / Math.max(item.approach.length, 1))),
),
axisAzimuth: tangentAzimuth(line, 0.1),
value: {
current: numericValue(cutbackConstraint, 'cutbackMeters', item.approach.cutback),
min: 1,
max: item.approach.length * 0.45,
unit: 'meter',
},
...(cutbackConstraint ? { constraintId: cutbackConstraint.id } : {}),
affects: [`complex-junction:${clusterId}:carriageway:${item.approach.segmentId}`],
editable: true,
});
}
}
return handles;
}
function makeClusterCornerHandles(model, junctionData, constraints, clusters) {
const handles = [];
for (const [clusterId, items] of clusterCarriageways(model, junctionData, clusters)) {
const arms = clusterArms(clusterId, items);
for (let index = 0; index < arms.length; index += 1) {
const first = arms[index];
const second = arms[(index + 1) % arms.length];
const cornerKey = `corner:${first.key}>${second.key}`;
const constraint = constraintFor(constraints, 'cluster-corner-radius', { clusterId, cornerKey });
const firstLine = clusterApproachLine(first.representative);
const secondLine = clusterApproachLine(second.representative);
const firstPoint = firstLine[0];
const secondPoint = secondLine[0];
handles.push({
handleId: `handle:cluster-corner-radius:${cornerKey}`,
kind: 'cluster-corner-radius',
anchor: {
type: 'cluster-corner',
clusterId,
cornerKey,
carriagewayKeys: [first.representative.key, second.representative.key],
},
position: [(firstPoint[0] + secondPoint[0]) / 2, (firstPoint[1] + secondPoint[1]) / 2],
axisAzimuth: first.heading,
value: { current: numericValue(constraint, 'radiusMeters', 12), min: 1, max: 25, unit: 'meter' },
...(constraint ? { constraintId: constraint.id } : {}),
affects: [`complex-junction:${clusterId}:corner:${cornerKey}`],
editable: true,
});
}
}
return handles;
@@ -1234,7 +1422,7 @@ function selectHandles(handles, model, selection) {
if (!selection || !selection.type) return handles;
if (selection.type === 'cluster')
return handles.filter(
(handle) => handle.kind === 'cluster-carriageway-width' && handle.anchor?.clusterId === String(selection.id),
(handle) => handle.anchor?.clusterId === String(selection.id) && handle.kind.startsWith('cluster-'),
);
if (selection.type === 'segment') {
const segmentOf = new Map((model.roads || []).map((road) => [road.id, road.segmentId]));
@@ -1280,6 +1468,7 @@ function resolveDirectEditConstraints(model, editDocument = null, context = {})
...makeRoadHandles(model || {}, groups, handles.reserves, reconciled, solved.roadProfiles),
...makeJunctionHandles(model || {}, junctionData, reconciled),
...makeClusterCarriagewayHandles(model || {}, junctionData, reconciled, clusters),
...makeClusterCornerHandles(model || {}, junctionData, reconciled, clusters),
...makeLaneManeuverHandles(model || {}, reconciled),
],
model || {},
@@ -1292,6 +1481,8 @@ function resolveDirectEditConstraints(model, editDocument = null, context = {})
effectiveJunctionPlans,
crossingControls: solved.crossingControls,
clusterPlans: solved.clusterPlans,
clusterCutbacks: solved.clusterCutbacks,
clusterCorners: solved.clusterCorners,
laneManeuvers: solved.laneManeuvers,
handles,
constraintStates: solved.states,