feat: classify direct edit replay states

This commit is contained in:
que01 committed 2026-08-27 12:43:35 +08:00
1 parent 94e319c717
commit 735ce69d26
3 files changed
+91 -18

No files matched your search

+60 -3
View File
@@ -33,6 +33,7 @@ const KINDS = [
'junction-corner-radius',
];
const MIN_LANE_WIDTH_METERS = 2.4;
const GEOMETRY_VERSION = 'native-road-package/v1.1';
function emptyHandleManifest(context) {
return {
@@ -163,6 +164,60 @@ function junctionReserves(model) {
return { reserves, approaches };
}
function snapshotCandidates(model, constraint) {
const snapshot = constraint.anchorSnapshot;
if (!snapshot || !Array.isArray(snapshot.coordinate)) return [];
const roads = (model.roads || []).filter((road) => {
if (!road.centerline?.length) return false;
const nodeMatch = snapshot.osmNodeIds?.some((id) => road.sourceNodeIds?.includes(String(id)));
const nearest = road.centerline.reduce(
(best, point) => Math.min(best, haversineMeters(snapshot.coordinate, point)),
Infinity,
);
return nodeMatch && nearest <= 50;
});
return roads;
}
function reconcileConstraints(model, constraints, base, junctionData, context) {
const versionChanged = Boolean(
context.compilerGeometryVersion &&
base?.compilerGeometryVersion &&
context.compilerGeometryVersion !== base.compilerGeometryVersion,
);
return constraints.map((constraint) => {
if (!constraint.enabled || !constraint.anchor || !['exact', 'recheck'].includes(constraint.status))
return constraint;
let matched = false;
if (constraint.kind.startsWith('road-')) {
const road = roadForAnchor(model, constraint.anchor.roadId);
matched = Boolean(road);
if (matched && constraint.kind === 'road-lane-divider')
matched = Number.isInteger(constraint.value?.boundaryIndex) && constraint.value.boundaryIndex < road.laneCount;
if (!matched) {
const candidates = snapshotCandidates(model, constraint);
if (candidates.length === 1) return { ...constraint, status: 'pending' };
return { ...constraint, status: candidates.length > 1 ? 'conflicted' : 'stale' };
}
} else if (constraint.kind === 'junction-corner-radius') {
const approaches = junctionData.approaches.get(String(constraint.anchor.nodeId));
matched = Boolean(
approaches?.some((item) => item.segmentId === constraint.anchor.incomingRoadId) &&
approaches?.some((item) => item.segmentId === constraint.anchor.outgoingRoadId),
);
} else {
matched = Boolean(
junctionData.approaches
.get(String(constraint.anchor.nodeId))
?.some((item) => item.segmentId === constraint.anchor.segmentId),
);
}
if (!matched) return { ...constraint, status: 'stale' };
if (versionChanged) return { ...constraint, status: 'recheck' };
return constraint;
});
}
function roadForAnchor(model, roadId) {
return (model.roads || []).find(
(road) => road.id === roadId || road.sourceRoadId === roadId || road.segmentId === roadId,
@@ -616,11 +671,12 @@ 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);
const reconciled = reconcileConstraints(model || {}, constraints, editDocument?.base, junctionData, context);
const solved = solveConstraints(model || {}, reconciled, junctionData, diagnostics);
handles.reserves = junctionData.reserves;
handles.handles = [
...makeRoadHandles(model || {}, groups, handles.reserves, constraints),
...makeJunctionHandles(model || {}, junctionData, constraints),
...makeRoadHandles(model || {}, groups, handles.reserves, reconciled),
...makeJunctionHandles(model || {}, junctionData, reconciled),
];
return {
schema: RESOLUTION_SCHEMA,
@@ -638,5 +694,6 @@ module.exports = {
ACTIVE_STATUSES,
SOLVED_KINDS,
KINDS,
GEOMETRY_VERSION,
resolveDirectEditConstraints,
};