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

+6 -2
View File
@@ -153,7 +153,10 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
const islands = [];
const armCrosswalkRadius = coreRadius * 0.68;
for (const item of carriageways) {
const outer = pointOnCarriagewayRadius(item, center, outerRadius, helpers);
// The approach plan owns the editable handoff distance. It shifts only this
// carriageway's outer transition, leaving neighboring arms untouched.
const itemOuterRadius = Math.max(coreRadius + 4, outerRadius - Math.max(0, Number(item.approach.cutback) || 0));
const outer = pointOnCarriagewayRadius(item, center, itemOuterRadius, helpers);
const inner = pointOnCarriagewayRadius(item, center, coreRadius * 0.7, helpers);
const outerHalf = item.approach.widthMeters / 2;
const innerHalf = outerHalf;
@@ -415,10 +418,11 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
const cornerFills = [];
const sidewalkCorners = [];
const cornerDiagnostics = [];
const cornerRadius = Math.max(4, Math.min(25, Number(cluster.cornerRadiusMeters) || 12));
const defaultCornerRadius = Math.max(4, Math.min(25, Number(cluster.cornerRadiusMeters) || 12));
for (let index = 0; index < arms.length; index += 1) {
const first = arms[index];
const second = arms[(index + 1) % arms.length];
const cornerRadius = helpers.clusterCornerRadii?.get(first.cornerKey) ?? defaultCornerRadius;
const delta = positiveHeadingDelta(first.heading, second.heading);
if (delta < 45 || delta > 135) continue;
const bisector = normalizeHeading(first.heading + delta / 2);
+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,
+14 -2
View File
@@ -14,6 +14,8 @@ const CONSTRAINT_ANCHORS = {
'road-lane-divider': 'road-interval',
'junction-approach-width': 'junction-approach',
'cluster-carriageway-width': 'cluster-carriageway',
'cluster-carriageway-cutback': 'cluster-carriageway',
'cluster-corner-radius': 'cluster-corner',
'junction-cutback': 'junction-approach',
'junction-corner-radius': 'junction-corner',
'junction-crosswalk-inset': 'junction-approach',
@@ -29,6 +31,7 @@ const ANCHOR_TYPES = new Set([
'junction-crossing',
'junction-corner',
'cluster-carriageway',
'cluster-corner',
'lane',
]);
const CONSTRAINT_STATUSES = new Set(['exact', 'recheck', 'pending', 'conflicted', 'stale']);
@@ -110,6 +113,15 @@ function validateAnchor(scope, kind, anchor) {
!anchor.sourceRoadIds.every((id) => typeof id === 'string' && id)
)
fail(scope, 'anchor.sourceRoadIds', 'must be a non-empty array of source road IDs');
} else if (expected === 'cluster-corner') {
requireString(scope, 'anchor.clusterId', anchor.clusterId);
requireString(scope, 'anchor.cornerKey', anchor.cornerKey);
if (
!Array.isArray(anchor.carriagewayKeys) ||
anchor.carriagewayKeys.length !== 2 ||
!anchor.carriagewayKeys.every((key) => typeof key === 'string' && key)
)
fail(scope, 'anchor.carriagewayKeys', 'must contain exactly two carriageway keys');
} else if (expected === 'junction-crossing') {
requireString(scope, 'anchor.crossingId', anchor.crossingId);
requireString(scope, 'anchor.nodeId', anchor.nodeId);
@@ -151,10 +163,10 @@ function validateValue(scope, kind, value) {
} else if (kind === 'junction-approach-width' || kind === 'cluster-carriageway-width') {
if (!isFiniteNumber(value.widthMeters) || value.widthMeters <= 0)
fail(scope, 'value.widthMeters', 'must be a positive number of meters');
} else if (kind === 'junction-cutback') {
} else if (kind === 'junction-cutback' || kind === 'cluster-carriageway-cutback') {
if (!isFiniteNumber(value.cutbackMeters) || value.cutbackMeters < 0)
fail(scope, 'value.cutbackMeters', 'must be a non-negative number of meters');
} else if (kind === 'junction-corner-radius') {
} else if (kind === 'junction-corner-radius' || kind === 'cluster-corner-radius') {
if (!isFiniteNumber(value.radiusMeters) || value.radiusMeters < 0)
fail(scope, 'value.radiusMeters', 'must be a non-negative number of meters');
} else if (kind === 'junction-crosswalk-inset') {
+11 -2
View File
@@ -769,6 +769,7 @@ function compileGeometry(model, overrides = { overrides: [] }, options = {}) {
headingAtEndpoint,
headingVector,
circleRing,
clusterCornerRadii: options.directEdit?.clusterCorners?.get(String(cluster.id)),
});
features.push(...generated.features);
if (generated.crosswalks) generatedComplexCrosswalks.push(...generated.crosswalks);
@@ -912,8 +913,14 @@ function applyDirectJunctionPlans(plans, directEdit) {
}
function applyDirectClusterPlans(model, junctionPlans, directEdit, clusters) {
if (!directEdit?.clusterPlans?.size) return;
for (const [clusterId, widths] of directEdit.clusterPlans) {
if (!directEdit?.clusterPlans?.size && !directEdit?.clusterCutbacks?.size) return;
const clusterIds = new Set([
...(directEdit.clusterPlans || new Map()).keys(),
...(directEdit.clusterCutbacks || new Map()).keys(),
]);
for (const clusterId of clusterIds) {
const widths = directEdit.clusterPlans?.get(clusterId) || new Map();
const cutbacks = directEdit.clusterCutbacks?.get(clusterId) || new Map();
const cluster = clusters.find((item) => String(item.id) === String(clusterId));
if (!cluster || cluster.template !== 'complex-junction-v1') continue;
const topology = resolveComplexJunctionTopology(model, cluster, {
@@ -925,6 +932,8 @@ function applyDirectClusterPlans(model, junctionPlans, directEdit, clusters) {
for (const item of topology.carriageways) {
const width = widths.get(item.key);
if (Number.isFinite(width)) item.approach.widthMeters = width;
const cutback = cutbacks.get(item.key);
if (Number.isFinite(cutback)) item.approach.cutback = cutback;
}
}
}