feat: solve direct edit constraints
This commit is contained in:
1 parent
67e67e66bf
commit
0eb404d8f7
3 files changed
+390
-47
No files matched your search
@@ -16,7 +16,14 @@ const {
|
||||
const RESOLUTION_SCHEMA = 'road-edit-resolution/v1';
|
||||
const HANDLE_MANIFEST_SCHEMA = 'road-edit-handles/v1';
|
||||
const ACTIVE_STATUSES = new Set(['exact', 'recheck']);
|
||||
const SOLVED_KINDS = new Set();
|
||||
const SOLVED_KINDS = new Set([
|
||||
'road-edge-offset',
|
||||
'road-sidewalk-width',
|
||||
'road-lane-divider',
|
||||
'junction-approach-width',
|
||||
'junction-cutback',
|
||||
'junction-corner-radius',
|
||||
]);
|
||||
const KINDS = [
|
||||
'road-edge-offset',
|
||||
'road-sidewalk-width',
|
||||
@@ -41,28 +48,6 @@ function constraintState(constraint, applied, reason) {
|
||||
return { constraintId: constraint.id, kind: constraint.kind, status: constraint.status, applied, reason };
|
||||
}
|
||||
|
||||
function planConstraints(constraints, diagnostics) {
|
||||
return constraints.map((constraint) => {
|
||||
if (!constraint.enabled) return constraintState(constraint, false, 'disabled');
|
||||
if (!ACTIVE_STATUSES.has(constraint.status))
|
||||
return constraintState(constraint, false, `status-${constraint.status}`);
|
||||
if (!SOLVED_KINDS.has(constraint.kind)) {
|
||||
diagnostics.push(
|
||||
diagnostic(
|
||||
'info',
|
||||
`constraint:${constraint.id}`,
|
||||
[],
|
||||
'direct-edit-kind-not-solved',
|
||||
`直接编辑约束 ${constraint.kind} 尚未接入求解器,本次编译按基线几何输出。`,
|
||||
null,
|
||||
),
|
||||
);
|
||||
return constraintState(constraint, false, 'kind-not-solved');
|
||||
}
|
||||
return constraintState(constraint, true, null);
|
||||
});
|
||||
}
|
||||
|
||||
function coordinateAt(line, station) {
|
||||
if (!Array.isArray(line) || line.length === 0) return null;
|
||||
if (line.length === 1) return [...line[0]];
|
||||
@@ -112,7 +97,7 @@ function constraintFor(constraints, kind, anchor) {
|
||||
item.anchor.type === 'road-interval' &&
|
||||
item.anchor.roadId === anchor.roadId &&
|
||||
(item.anchor.side || null) === (anchor.side || null) &&
|
||||
(kind !== 'road-lane-divider' || item.anchor.boundaryIndex === anchor.boundaryIndex)
|
||||
(kind !== 'road-lane-divider' || item.value?.boundaryIndex === anchor.boundaryIndex)
|
||||
);
|
||||
}
|
||||
if (kind === 'junction-corner-radius')
|
||||
@@ -178,6 +163,231 @@ function junctionReserves(model) {
|
||||
return { reserves, approaches };
|
||||
}
|
||||
|
||||
function roadForAnchor(model, roadId) {
|
||||
return (model.roads || []).find(
|
||||
(road) => road.id === roadId || road.sourceRoadId === roadId || road.segmentId === roadId,
|
||||
);
|
||||
}
|
||||
|
||||
function blocking(diagnostics, constraint, rule, message, coordinate = null) {
|
||||
diagnostics.push(diagnostic('error', `constraint:${constraint.id}`, [], rule, message, coordinate));
|
||||
}
|
||||
|
||||
function profileFor(profiles, road) {
|
||||
if (!profiles.has(road.id))
|
||||
profiles.set(road.id, {
|
||||
roadId: road.id,
|
||||
segmentId: road.segmentId,
|
||||
interval: null,
|
||||
edgeOffsets: { left: 0, right: 0 },
|
||||
sidewalkWidths: {
|
||||
left: road.sidewalkLeft ? 2 : 0,
|
||||
right: road.sidewalkRight ? 2 : 0,
|
||||
},
|
||||
laneDividerOffsets: {},
|
||||
transitions: {},
|
||||
widthMeters: road.widthMeters,
|
||||
});
|
||||
return profiles.get(road.id);
|
||||
}
|
||||
|
||||
function applyRoadConstraint(model, constraint, profiles, diagnostics) {
|
||||
const road = roadForAnchor(model, constraint.anchor?.roadId);
|
||||
if (!road) {
|
||||
blocking(diagnostics, constraint, 'direct-edit-road-anchor-missing', '直接编辑约束的道路锚点不存在。');
|
||||
return false;
|
||||
}
|
||||
const anchor = constraint.anchor;
|
||||
if (
|
||||
anchor.type !== 'road-interval' ||
|
||||
!Number.isFinite(anchor.startStation) ||
|
||||
!Number.isFinite(anchor.endStation) ||
|
||||
anchor.startStation < 0 ||
|
||||
anchor.endStation > 1 ||
|
||||
anchor.startStation >= anchor.endStation
|
||||
) {
|
||||
blocking(
|
||||
diagnostics,
|
||||
constraint,
|
||||
'direct-edit-invalid-interval',
|
||||
'道路编辑区间必须按 0..1 的归一化 station 严格递增。',
|
||||
road.centerline?.[0],
|
||||
);
|
||||
return false;
|
||||
}
|
||||
const profile = profileFor(profiles, road);
|
||||
profile.interval = profile.interval
|
||||
? {
|
||||
startStation: Math.min(profile.interval.startStation, anchor.startStation),
|
||||
endStation: Math.max(profile.interval.endStation, anchor.endStation),
|
||||
}
|
||||
: { startStation: anchor.startStation, endStation: anchor.endStation };
|
||||
const sides = anchor.side ? [anchor.side] : ['left', 'right'];
|
||||
if (sides.some((side) => !['left', 'right'].includes(side))) {
|
||||
blocking(diagnostics, constraint, 'direct-edit-invalid-side', '道路约束 side 必须是 left 或 right。');
|
||||
return false;
|
||||
}
|
||||
if (constraint.kind === 'road-edge-offset') {
|
||||
const offset = Number(constraint.value?.offsetMeters);
|
||||
if (!Number.isFinite(offset)) {
|
||||
blocking(diagnostics, constraint, 'direct-edit-invalid-value', '外缘偏移必须是有限米数。');
|
||||
return false;
|
||||
}
|
||||
for (const side of sides) {
|
||||
profile.edgeOffsets[side] = offset;
|
||||
profile.transitions[`road-edge-offset:${side}`] = constraint.value?.transition || 'smoothstep';
|
||||
}
|
||||
} else if (constraint.kind === 'road-sidewalk-width') {
|
||||
const width = Number(constraint.value?.widthMeters);
|
||||
if (!Number.isFinite(width) || width < 0) {
|
||||
blocking(diagnostics, constraint, 'direct-edit-invalid-value', '步行带宽度必须是非负米数。');
|
||||
return false;
|
||||
}
|
||||
for (const side of sides) {
|
||||
profile.sidewalkWidths[side] = width;
|
||||
profile.transitions[`road-sidewalk-width:${side}`] = constraint.value?.transition || 'smoothstep';
|
||||
}
|
||||
} else if (constraint.kind === 'road-lane-divider') {
|
||||
const boundaryIndex = constraint.value?.boundaryIndex;
|
||||
const offset = Number(constraint.value?.offsetMeters);
|
||||
if (!Number.isInteger(boundaryIndex) || boundaryIndex < 1 || boundaryIndex >= road.laneCount) {
|
||||
blocking(diagnostics, constraint, 'direct-edit-lane-boundary-stale', '车道分隔 boundaryIndex 已超出当前车道数。');
|
||||
return false;
|
||||
}
|
||||
if (!Number.isFinite(offset)) {
|
||||
blocking(diagnostics, constraint, 'direct-edit-invalid-value', '车道分隔偏移必须是有限米数。');
|
||||
return false;
|
||||
}
|
||||
const laneWidth = road.widthMeters / road.laneCount;
|
||||
if (laneWidth - Math.abs(offset) < 2.4) {
|
||||
blocking(diagnostics, constraint, 'direct-edit-min-lane-width', '车道分隔调整会使相邻车道小于 2.4 米。');
|
||||
return false;
|
||||
}
|
||||
profile.laneDividerOffsets[boundaryIndex] = offset;
|
||||
profile.transitions[`road-lane-divider:${boundaryIndex}`] = constraint.value?.transition || 'smoothstep';
|
||||
}
|
||||
profile.widthMeters = road.widthMeters + profile.edgeOffsets.left + profile.edgeOffsets.right;
|
||||
if (
|
||||
Math.abs(profile.edgeOffsets.left) > road.widthMeters / 2 ||
|
||||
Math.abs(profile.edgeOffsets.right) > road.widthMeters / 2 ||
|
||||
profile.widthMeters < road.laneCount * 2.4
|
||||
) {
|
||||
blocking(diagnostics, constraint, 'direct-edit-outer-edges-cross', '左右外缘调整后无法容纳最小车道宽度。');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function applyJunctionConstraint(model, constraint, junctionPlans, junctionData, diagnostics) {
|
||||
const anchor = constraint.anchor || {};
|
||||
const approaches = junctionData.approaches.get(String(anchor.nodeId));
|
||||
const approach = approaches?.find((item) => item.segmentId === anchor.segmentId);
|
||||
if (constraint.kind !== 'junction-corner-radius' && !approach) {
|
||||
blocking(diagnostics, constraint, 'direct-edit-junction-anchor-missing', '直接编辑约束的路口进口锚点不存在。');
|
||||
return false;
|
||||
}
|
||||
if (constraint.kind === 'junction-corner-radius') {
|
||||
const incoming = approaches?.find((item) => item.segmentId === anchor.incomingRoadId);
|
||||
const outgoing = approaches?.find((item) => item.segmentId === anchor.outgoingRoadId);
|
||||
const radius = Number(constraint.value?.radiusMeters);
|
||||
if (!incoming || !outgoing) {
|
||||
blocking(diagnostics, constraint, 'direct-edit-junction-corner-missing', '直接编辑约束的路口角部锚点不存在。');
|
||||
return false;
|
||||
}
|
||||
if (!Number.isFinite(radius) || radius < 0.5 || radius > 30) {
|
||||
blocking(diagnostics, constraint, 'direct-edit-corner-radius-invalid', '路口角部半径必须在 0.5 到 30 米之间。');
|
||||
return false;
|
||||
}
|
||||
if (!junctionPlans.has(String(anchor.nodeId)))
|
||||
junctionPlans.set(String(anchor.nodeId), { nodeId: String(anchor.nodeId), approaches: {}, corners: {} });
|
||||
const plan = junctionPlans.get(String(anchor.nodeId));
|
||||
plan.corners[`${anchor.incomingRoadId}->${anchor.outgoingRoadId}`] = radius;
|
||||
return true;
|
||||
}
|
||||
if (!junctionPlans.has(String(anchor.nodeId)))
|
||||
junctionPlans.set(String(anchor.nodeId), { nodeId: String(anchor.nodeId), approaches: {}, corners: {} });
|
||||
const plan = junctionPlans.get(String(anchor.nodeId));
|
||||
const entry = (plan.approaches[anchor.segmentId] ||= {
|
||||
widthMeters: approach.items.reduce((sum, item) => sum + item.road.widthMeters, 0),
|
||||
cutbackMeters: approach.cutback,
|
||||
});
|
||||
if (constraint.kind === 'junction-approach-width') {
|
||||
const width = Number(constraint.value?.widthMeters);
|
||||
if (!Number.isFinite(width) || width < 2.4) {
|
||||
blocking(diagnostics, constraint, 'direct-edit-min-approach-width', '路口进口宽度不能小于 2.4 米。');
|
||||
return false;
|
||||
}
|
||||
entry.widthMeters = width;
|
||||
} else {
|
||||
const cutback = Number(constraint.value?.cutbackMeters);
|
||||
if (!Number.isFinite(cutback) || cutback < 1 || cutback > approach.length * 0.45) {
|
||||
blocking(
|
||||
diagnostics,
|
||||
constraint,
|
||||
'direct-edit-cutback-invalid',
|
||||
'路口 cutback 必须落在进口可用长度的 45% 以内。',
|
||||
);
|
||||
return false;
|
||||
}
|
||||
entry.cutbackMeters = cutback;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function solveConstraints(model, constraints, junctionData, diagnostics) {
|
||||
const roadProfiles = new Map();
|
||||
const junctionPlans = new Map();
|
||||
const states = [];
|
||||
for (const constraint of constraints) {
|
||||
if (!constraint.enabled) {
|
||||
states.push(constraintState(constraint, false, 'disabled'));
|
||||
continue;
|
||||
}
|
||||
if (!ACTIVE_STATUSES.has(constraint.status)) {
|
||||
states.push(constraintState(constraint, false, `status-${constraint.status}`));
|
||||
continue;
|
||||
}
|
||||
if (!SOLVED_KINDS.has(constraint.kind)) {
|
||||
states.push(constraintState(constraint, false, 'kind-not-solved'));
|
||||
continue;
|
||||
}
|
||||
const roadSnapshot = new Map(
|
||||
[...roadProfiles].map(([key, profile]) => [
|
||||
key,
|
||||
{
|
||||
...profile,
|
||||
edgeOffsets: { ...profile.edgeOffsets },
|
||||
sidewalkWidths: { ...profile.sidewalkWidths },
|
||||
laneDividerOffsets: { ...profile.laneDividerOffsets },
|
||||
transitions: { ...profile.transitions },
|
||||
interval: profile.interval && { ...profile.interval },
|
||||
},
|
||||
]),
|
||||
);
|
||||
const junctionSnapshot = new Map(
|
||||
[...junctionPlans].map(([key, plan]) => [
|
||||
key,
|
||||
{
|
||||
...plan,
|
||||
approaches: Object.fromEntries(Object.entries(plan.approaches).map(([id, entry]) => [id, { ...entry }])),
|
||||
corners: { ...plan.corners },
|
||||
},
|
||||
]),
|
||||
);
|
||||
const 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);
|
||||
}
|
||||
states.push(constraintState(constraint, applied, applied ? null : 'invalid'));
|
||||
}
|
||||
return { roadProfiles, junctionPlans, states };
|
||||
}
|
||||
|
||||
function roadAffects(roads, model, side, boundaryIndex) {
|
||||
const ids = [];
|
||||
for (const road of roads) {
|
||||
@@ -406,6 +616,7 @@ function resolveDirectEditConstraints(model, editDocument = null, context = {})
|
||||
const handles = emptyHandleManifest(context);
|
||||
const groups = roadGroups(model || {});
|
||||
const junctionData = junctionReserves(model || {});
|
||||
const solved = solveConstraints(model || {}, constraints, junctionData, diagnostics);
|
||||
handles.reserves = junctionData.reserves;
|
||||
handles.handles = [
|
||||
...makeRoadHandles(model || {}, groups, handles.reserves, constraints),
|
||||
@@ -413,10 +624,10 @@ function resolveDirectEditConstraints(model, editDocument = null, context = {})
|
||||
];
|
||||
return {
|
||||
schema: RESOLUTION_SCHEMA,
|
||||
roadProfiles: new Map(),
|
||||
junctionPlans: new Map(),
|
||||
roadProfiles: solved.roadProfiles,
|
||||
junctionPlans: solved.junctionPlans,
|
||||
handles,
|
||||
constraintStates: planConstraints(constraints, diagnostics),
|
||||
constraintStates: solved.states,
|
||||
diagnostics,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user