fix(compile): validate direct edit constraint ranges
This commit is contained in:
1 parent
bb108267e3
commit
0318876e6a
7 files changed
+238
-11
No files matched your search
@@ -75,7 +75,13 @@ function emptyHandleManifest(context) {
|
||||
}
|
||||
|
||||
function constraintState(constraint, applied, reason) {
|
||||
return { constraintId: constraint.id, kind: constraint.kind, status: constraint.status, applied, reason };
|
||||
return {
|
||||
constraintId: constraint.id,
|
||||
kind: constraint.kind,
|
||||
status: constraint.status,
|
||||
applied,
|
||||
reason: constraint.status === 'stale' && constraint.replayReason ? constraint.replayReason : reason,
|
||||
};
|
||||
}
|
||||
|
||||
function coordinateAt(line, station) {
|
||||
@@ -221,12 +227,19 @@ function reconcileConstraints(model, constraints, base, junctionData, context) {
|
||||
if (constraint.kind.startsWith('road-')) {
|
||||
const road = roadForAnchor(model, constraint.anchor.roadId);
|
||||
matched = Boolean(road);
|
||||
const roadExists = matched;
|
||||
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' };
|
||||
return {
|
||||
...constraint,
|
||||
status: candidates.length > 1 ? 'conflicted' : 'stale',
|
||||
...(candidates.length || !roadExists
|
||||
? { replayReason: candidates.length ? undefined : 'anchor-missing' }
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
} else if (constraint.kind === 'junction-corner-radius') {
|
||||
const approaches = junctionData.approaches.get(String(constraint.anchor.nodeId));
|
||||
@@ -241,7 +254,13 @@ function reconcileConstraints(model, constraints, base, junctionData, context) {
|
||||
?.some((item) => item.segmentId === constraint.anchor.segmentId),
|
||||
);
|
||||
}
|
||||
if (!matched) return { ...constraint, status: 'stale' };
|
||||
if (!matched) return { ...constraint, status: 'stale', replayReason: 'anchor-missing' };
|
||||
if ((versionChanged || context.replay) && constraint.kind === 'road-edge-offset') {
|
||||
const value = Number(constraint.value?.offsetMeters);
|
||||
const sides = constraint.anchor.side ? [constraint.anchor.side] : ['left', 'right'];
|
||||
if (sides.some((side) => !inRange(value, roadEdgeOffsetRange(model, { ...constraint.anchor, side }, null))))
|
||||
return { ...constraint, status: 'stale', replayReason: 'range-out-of-bounds' };
|
||||
}
|
||||
if (versionChanged) return { ...constraint, status: 'recheck' };
|
||||
return constraint;
|
||||
});
|
||||
@@ -253,6 +272,29 @@ function roadForAnchor(model, roadId) {
|
||||
);
|
||||
}
|
||||
|
||||
function inRange(value, range) {
|
||||
return Boolean(range && Number.isFinite(value) && value >= range.min && value <= range.max);
|
||||
}
|
||||
|
||||
// This is the authoritative range for both the published edge handle and the
|
||||
// solver. A segment can contain two directional roads, but an edge constraint
|
||||
// applies to exactly one `roadId`; using the segment total here was the source
|
||||
// of the previously advertised-but-invalid drag range.
|
||||
function roadEdgeOffsetRange(model, anchor, profile) {
|
||||
const road = roadForAnchor(model, anchor?.roadId);
|
||||
const side = anchor?.side;
|
||||
if (!road || !['left', 'right'].includes(side)) return null;
|
||||
const otherSide = side === 'left' ? 'right' : 'left';
|
||||
const otherOffset = Number(profile?.edgeOffsets?.[otherSide]) || 0;
|
||||
const width = Number(road.widthMeters);
|
||||
const minimumWidth = Number(road.laneCount) * MIN_LANE_WIDTH_METERS;
|
||||
if (!Number.isFinite(width) || !Number.isFinite(minimumWidth)) return null;
|
||||
return {
|
||||
min: Math.max(-width / 2, minimumWidth - width - otherOffset),
|
||||
max: width / 2,
|
||||
};
|
||||
}
|
||||
|
||||
function blocking(diagnostics, constraint, rule, message, coordinate = null) {
|
||||
diagnostics.push(diagnostic('error', `constraint:${constraint.id}`, [], rule, message, coordinate));
|
||||
}
|
||||
@@ -313,8 +355,15 @@ function applyRoadConstraint(model, constraint, profiles, diagnostics) {
|
||||
}
|
||||
if (constraint.kind === 'road-edge-offset') {
|
||||
const offset = Number(constraint.value?.offsetMeters);
|
||||
if (!Number.isFinite(offset)) {
|
||||
blocking(diagnostics, constraint, 'direct-edit-invalid-value', '外缘偏移必须是有限米数。');
|
||||
const ranges = sides.map((side) => roadEdgeOffsetRange(model, { ...anchor, side }, profile));
|
||||
if (ranges.some((range) => !inRange(offset, range))) {
|
||||
const range = ranges.find((item) => item && !inRange(offset, item)) || ranges[0];
|
||||
blocking(
|
||||
diagnostics,
|
||||
constraint,
|
||||
'direct-edit-edge-offset-out-of-range',
|
||||
`外缘偏移必须在 ${range ? `${range.min} 到 ${range.max}` : '当前道路可用范围'} 米之间。`,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
for (const side of sides) {
|
||||
@@ -563,7 +612,7 @@ function roadAffects(roads, model, side, boundaryIndex) {
|
||||
return [...new Set(ids)];
|
||||
}
|
||||
|
||||
function makeRoadHandles(model, groups, reserves, constraints) {
|
||||
function makeRoadHandles(model, groups, reserves, constraints, roadProfiles) {
|
||||
const handles = [];
|
||||
for (const [segmentId, roads] of groups) {
|
||||
const road = roads.find((item) => item.direction === 'forward') || roads[0];
|
||||
@@ -621,12 +670,13 @@ function makeRoadHandles(model, groups, reserves, constraints) {
|
||||
const sideSign = side === 'left' ? -1 : 1;
|
||||
const edgeAnchor = { ...interval, side };
|
||||
const edgeConstraint = constraintFor(constraints, 'road-edge-offset', edgeAnchor);
|
||||
const edgeRange = roadEdgeOffsetRange(model, edgeAnchor, roadProfiles.get(road.id));
|
||||
add(
|
||||
'road-edge-offset',
|
||||
edgeAnchor,
|
||||
numericValue(edgeConstraint, 'offsetMeters', 0),
|
||||
-Math.max(0.1, width * 0.45),
|
||||
Math.max(0.1, width * 0.45),
|
||||
edgeRange.min,
|
||||
edgeRange.max,
|
||||
normal,
|
||||
offsetCoordinate(point, tangent + sideSign * 90, width / 2),
|
||||
roadAffects(roads, model, side),
|
||||
@@ -898,7 +948,7 @@ function resolveDirectEditConstraints(model, editDocument = null, context = {})
|
||||
handles.reserves = junctionData.reserves;
|
||||
handles.handles = selectHandles(
|
||||
[
|
||||
...makeRoadHandles(model || {}, groups, handles.reserves, reconciled),
|
||||
...makeRoadHandles(model || {}, groups, handles.reserves, reconciled, solved.roadProfiles),
|
||||
...makeJunctionHandles(model || {}, junctionData, reconciled),
|
||||
],
|
||||
model || {},
|
||||
@@ -928,5 +978,6 @@ module.exports = {
|
||||
SOLVED_KINDS,
|
||||
KINDS,
|
||||
GEOMETRY_VERSION,
|
||||
roadEdgeOffsetRange,
|
||||
resolveDirectEditConstraints,
|
||||
};
|
||||
Reference in new issue
Block a user