diff --git a/.trellis/spec/backend/direct-edit-api.md b/.trellis/spec/backend/direct-edit-api.md index ef59c0c..1823c7b 100644 --- a/.trellis/spec/backend/direct-edit-api.md +++ b/.trellis/spec/backend/direct-edit-api.md @@ -412,6 +412,27 @@ Regression evidence: solver rejects out-of-range/source-mismatched values; API preview measures the requested length and preserves the outer seam; save/read/replay reproduces preview geometry; browser input sends the new kind. +## Complex carriageway direction and edge bounds + +`cluster-carriageway-direction` remains persisted as +`value.offsetDegrees`, including for numeric input and existing documents. Its +manifest handle declares `interaction.kind = rotation`, an EPSG:4326 `center` +at the effective inner entry section, and `radiusMeters`. Pointer projection is +the signed shortest azimuth change around that center; pointer metres must never +be interpreted as degrees. The direction ghost follows the declared circle and +uses degree units. + +Position and width handles use the same effective inner entry section as the +BoundaryGraph. Direction edits rotate the width axis and both edge handles at +that section. + +The manifest and solver share the carriageway edge range. For one side, with +effective width `W`, opposite offset `O`, and minimum lane total `M`, the range +is `max(-W/2, M-W-O)..W/2`. Both endpoints are accepted; a value outside the +range is rejected with a blocking geometry diagnostic. Independent side +constraints and legacy constraints containing both offsets must be evaluated +with both effective offsets present. + Boundary closure reports `sharedSeamErrors` per carriageway and `maxSharedSeamErrorMeters` as the maximum endpoint distance in local metres between the core ring edge and its BoundaryGraph `inner` edge. Missing or diff --git a/.trellis/spec/frontend/direct-edit-client.md b/.trellis/spec/frontend/direct-edit-client.md index 63b374c..a2a4a2e 100644 --- a/.trellis/spec/frontend/direct-edit-client.md +++ b/.trellis/spec/frontend/direct-edit-client.md @@ -332,3 +332,13 @@ A grouped scalar is available only if the individual ranges intersect. Otherwise show why a common value is unavailable and expand individual controls; never render a minimum larger than the maximum. Transition length uses the same input, preview, history, and persistence path as other geometry parameters. + +### Rotational direction handles + +`cluster-carriageway-direction` keeps `value.current` and persisted +`offsetDegrees` in degrees. Its manifest declares +`interaction: { kind: 'rotation', center, radiusMeters }`. Project pointer input +as the shortest signed azimuth change around `center`, clamp it to the manifest +range, and place the ghost on the declared circle. Never convert linear pointer +metres directly into degrees. The ghost label uses `°`; numeric input continues +through the shared scalar edit session and save path. diff --git a/.trellis/tasks/09-08-junction-reference-fidelity/implement.md b/.trellis/tasks/09-08-junction-reference-fidelity/implement.md index 488ab38..ae630d6 100644 --- a/.trellis/tasks/09-08-junction-reference-fidelity/implement.md +++ b/.trellis/tasks/09-08-junction-reference-fidelity/implement.md @@ -87,6 +87,10 @@ complex-junction.js / native-road.js:构建与全部消费者;direct-edit-so 方向控件仍以沿切线的线性拖动米数映射角度,且位置/方向自身手柄仍显示旧收口点。完整旋转交互需要单独明确手柄契约,本轮不据此关闭 A4/A5。 +后续已补齐该缺口:方向手柄发布有效内断面中心和旋转半径,客户端按最短方位角变化生成 `offsetDegrees`,ghost 沿圆周移动并显示角度;位置、方向和宽度手柄统一使用有效内断面。左右边界范围由有效宽度、另一侧偏移和最小车道总宽共同推导,manifest 与 solver 共用端点且越界阻塞。兼容数值输入及既有 `offsetDegrees` 文档。 + +本轮同时修复成功闭合且接缝合格的核心仍被误报为 `complex-junction-core-closure-unsupported` 的条件分支。npm test、191 项客户端单测、类型检查、生产构建、11 项 E2E、format:check 与 git diff --check 通过。区域邻接和排除区共同决定的更大向外边界仍留给区域级求解,不用固定范围冒充完整可行域。 + 本轮质量门:npm test 通过;npm run test:client 通过;npm run test:client:unit 15 文件/188 测试通过;npm run test:e2e(包含 production build)11 测试通过,包含保存后重置与放弃恢复;npm run format:check 与 git diff --check 通过。构建仍有既有 bundle 大小提示。遵循用户“整体任务完成再提交”,本轮未提交、未归档父任务。 下一阶段已实现共享内边的实际米制端点误差:`sharedSeamErrors` 逐车行道报告,缺失/非邻接为 Infinity;回归通过人为 2 米偏移验证指标变化。显式过渡站点统一限制在来源折线范围,超长请求标记来源长度不足并由 solver 拒绝。全图层接缝、凹形多区域和真实 OSM 视觉验收仍未完成。 diff --git a/src/compile/complex-junction.js b/src/compile/complex-junction.js index 9fd2506..0f226a9 100644 --- a/src/compile/complex-junction.js +++ b/src/compile/complex-junction.js @@ -545,7 +545,7 @@ function buildComplexJunctionGeometry(model, cluster, helpers) { center, ), ); - } else { + } else if (!coreRing) { cornerDiagnostics.push( helpers.diagnostic( 'warning', diff --git a/src/compile/direct-edit-solver.js b/src/compile/direct-edit-solver.js index 745ce66..40e62e8 100644 --- a/src/compile/direct-edit-solver.js +++ b/src/compile/direct-edit-solver.js @@ -390,6 +390,19 @@ function clusterCutbackMaximum(cluster, approachLength) { ); } +function clusterEdgeOffsetRange(item, widthMeters, side, edgeOffsets = {}) { + if (!item || !['left', 'right'].includes(side)) return null; + const width = Number(widthMeters); + const minimumWidth = Number(item.laneCount) * MIN_LANE_WIDTH_METERS; + if (!Number.isFinite(width) || !Number.isFinite(minimumWidth)) return null; + const otherSide = side === 'left' ? 'right' : 'left'; + const otherOffset = Number(edgeOffsets[`${otherSide}OffsetMeters`]) || 0; + return { + min: Math.max(-width / 2, minimumWidth - width - otherOffset), + max: width / 2, + }; +} + function roadGroups(model) { const groups = new Map(); for (const road of Array.isArray(model?.roads) ? model.roads : []) { @@ -963,18 +976,30 @@ function solveConstraints(model, constraints, junctionData, diagnostics, cluster const cluster = clusters.find((candidate) => String(candidate.id) === String(item?.clusterId)); const cutbackMax = cluster ? clusterCutbackMaximum(cluster, item?.approach.length || 0) : 0; const transitionMax = item && cluster ? transitionBounds(model, cluster, item, constraints).max : 0; + const widthConstraint = item + ? constraintFor(constraints, 'cluster-carriageway-width', { + clusterId: item.clusterId, + carriagewayKey: item.key, + }) + : null; + const effectiveWidth = item ? numericValue(widthConstraint, 'widthMeters', item.widthMeters) : 0; + const previousEdges = item + ? clusterEdgeOffsets.get(item.clusterId)?.get(item.key) || { leftOffsetMeters: 0, rightOffsetMeters: 0 } + : null; + const edgeSides = constraint.anchor?.side ? [constraint.anchor.side] : ['left', 'right']; if (!item) { blocking(diagnostics, constraint, 'direct-edit-cluster-carriageway-missing', '复杂路口车行道锚点不存在。'); applied = false; } else if ( (constraint.kind !== 'cluster-carriageway-edge-offset' && !Number.isFinite(value)) || (constraint.kind === 'cluster-carriageway-edge-offset' && - (!Number.isFinite(value.leftOffsetMeters) || - !Number.isFinite(value.rightOffsetMeters) || - value.leftOffsetMeters < -10 || - value.leftOffsetMeters > 10 || - value.rightOffsetMeters < -10 || - value.rightOffsetMeters > 10)) || + (edgeSides.some((side) => !['left', 'right'].includes(side)) || + edgeSides.some((side) => { + const field = `${side}OffsetMeters`; + const candidate = value?.[field]; + const combined = { ...previousEdges, ...value, [field]: candidate }; + return !inRange(candidate, clusterEdgeOffsetRange(item, effectiveWidth, side, combined)); + }))) || (constraint.kind === 'cluster-carriageway-width' && (value < Math.max(MIN_LANE_WIDTH_METERS, item.laneCount * MIN_LANE_WIDTH_METERS) || value > item.widthMeters * 2)) || @@ -1520,12 +1545,34 @@ function makeClusterCarriagewayHandles(model, junctionData, constraints, cluster for (const item of items) { const road = item.roads[0]; const line = item.approach.endpoint.side === 'start' ? road.centerline : [...road.centerline].reverse(); - const station = Math.min(0.95, Math.max(0.05, item.approach.cutback / Math.max(item.approach.length, 1))); - const point = coordinateAt(line, station); const constraint = constraintFor(constraints, 'cluster-carriageway-width', { clusterId, carriagewayKey: item.key, }); + const transitionConstraint = constraintFor(constraints, 'cluster-carriageway-transition-length', { + clusterId, + carriagewayKey: item.key, + }); + const bounds = transitionBounds(model, cluster, item, constraints); + const transitionValue = numericValue(transitionConstraint, 'transitionLengthMeters', bounds.current); + const positionEdit = constraintFor(constraints, 'cluster-carriageway-position', { + clusterId, + carriagewayKey: item.key, + }); + const directionEdit = constraintFor(constraints, 'cluster-carriageway-direction', { + clusterId, + carriagewayKey: item.key, + }); + const effectiveStationMeters = Math.max( + 0, + Math.min(item.length, bounds.outer - transitionValue + numericValue(positionEdit, 'offsetMeters', 0)), + ); + const effectiveStation = effectiveStationMeters / Math.max(item.length, 1); + const effectiveCenter = coordinateAt(line, effectiveStation); + const sourceTangent = tangentAzimuth(line, effectiveStation); + const directionValue = numericValue(directionEdit, 'offsetDegrees', 0); + const effectiveTangent = sourceTangent + directionValue; + const effectiveWidth = numericValue(constraint, 'widthMeters', item.widthMeters); handles.push({ handleId: `handle:cluster-carriageway-width:${item.key}`, kind: 'cluster-carriageway-width', @@ -1536,8 +1583,8 @@ function makeClusterCarriagewayHandles(model, junctionData, constraints, cluster sourceNodeIds: item.sourceNodeIds, sourceRoadIds: item.sourceRoadIds, }, - position: point, - axisAzimuth: (tangentAzimuth(line, station) + 90) % 360, + position: effectiveCenter, + axisAzimuth: (effectiveTangent + 90) % 360, value: { current: numericValue(constraint, 'widthMeters', item.widthMeters), min: Math.max(MIN_LANE_WIDTH_METERS, item.laneCount * MIN_LANE_WIDTH_METERS), @@ -1548,12 +1595,6 @@ function makeClusterCarriagewayHandles(model, junctionData, constraints, cluster affects: [`complex-junction:${clusterId}:carriageway:${item.approach.segmentId}`], editable: true, }); - const transitionConstraint = constraintFor(constraints, 'cluster-carriageway-transition-length', { - clusterId, - carriagewayKey: item.key, - }); - const bounds = transitionBounds(model, cluster, item, constraints); - const transitionValue = numericValue(transitionConstraint, 'transitionLengthMeters', bounds.current); const transitionStation = Math.max(0, bounds.outer - transitionValue) / Math.max(item.length, 1); if (bounds.max > 0 && bounds.max >= bounds.min) handles.push({ @@ -1604,7 +1645,6 @@ function makeClusterCarriagewayHandles(model, junctionData, constraints, cluster }); for (const [kind, field, unit, min, max] of [ ['cluster-carriageway-position', 'offsetMeters', 'meter', -20, 20], - ['cluster-carriageway-direction', 'offsetDegrees', 'degree', -15, 15], ]) { const edit = constraintFor(constraints, kind, { clusterId, carriagewayKey: item.key }); handles.push({ @@ -1617,33 +1657,47 @@ function makeClusterCarriagewayHandles(model, junctionData, constraints, cluster sourceNodeIds: item.sourceNodeIds, sourceRoadIds: item.sourceRoadIds, }, - position: point, - axisAzimuth: tangentAzimuth(line, station), + position: effectiveCenter, + axisAzimuth: sourceTangent, value: { current: numericValue(edit, field, 0), min, max, unit }, ...(edit ? { constraintId: edit.id } : {}), affects: [`complex-junction:${clusterId}:carriageway:${item.approach.segmentId}`], editable: true, }); } - const positionEdit = constraintFor(constraints, 'cluster-carriageway-position', { - clusterId, - carriagewayKey: item.key, + const directionRadiusMeters = Math.max(4, effectiveWidth / 2 + 2); + handles.push({ + handleId: `handle:cluster-carriageway-direction:${item.key}`, + kind: 'cluster-carriageway-direction', + anchor: { + type: 'cluster-carriageway', + clusterId, + carriagewayKey: item.key, + sourceNodeIds: item.sourceNodeIds, + sourceRoadIds: item.sourceRoadIds, + }, + position: offsetCoordinate(effectiveCenter, effectiveTangent, directionRadiusMeters), + axisAzimuth: effectiveTangent, + interaction: { kind: 'rotation', center: effectiveCenter, radiusMeters: directionRadiusMeters }, + value: { current: directionValue, min: -15, max: 15, unit: 'degree' }, + ...(directionEdit ? { constraintId: directionEdit.id } : {}), + affects: [`complex-junction:${clusterId}:carriageway:${item.approach.segmentId}`], + editable: true, }); - const directionEdit = constraintFor(constraints, 'cluster-carriageway-direction', { - clusterId, - carriagewayKey: item.key, - }); - // Match JunctionBoundaryGraph's effective inner section. The old handle - // placement used the legacy cutback station, which diverges as soon as a - // transition length is edited (and is especially visible on curves). - const effectiveStationMeters = Math.max( - 0, - Math.min(item.length, bounds.outer - transitionValue + numericValue(positionEdit, 'offsetMeters', 0)), - ); - const effectiveStation = effectiveStationMeters / Math.max(item.length, 1); - const effectiveCenter = coordinateAt(line, effectiveStation); - const effectiveTangent = tangentAzimuth(line, effectiveStation) + numericValue(directionEdit, 'offsetDegrees', 0); - const effectiveWidth = numericValue(constraint, 'widthMeters', item.widthMeters); + const edgeValues = {}; + for (const side of ['left', 'right']) { + const edge = constraints + .filter( + (edit) => + edit.enabled !== false && + edit.kind === 'cluster-carriageway-edge-offset' && + edit.anchor.clusterId === clusterId && + edit.anchor.carriagewayKey === item.key && + (!edit.anchor.side || edit.anchor.side === side), + ) + .at(-1); + edgeValues[`${side}OffsetMeters`] = Number(edge?.value?.[`${side}OffsetMeters`]) || 0; + } for (const side of ['left', 'right']) { const candidates = constraints.filter( (edit) => @@ -1656,6 +1710,7 @@ function makeClusterCarriagewayHandles(model, junctionData, constraints, cluster const edgeConstraint = candidates.at(-1); const edgeValue = edgeConstraint?.value || {}; const current = Number(edgeValue[`${side}OffsetMeters`]) || 0; + const range = clusterEdgeOffsetRange(item, effectiveWidth, side, edgeValues); const sideAzimuth = effectiveTangent + (side === 'left' ? -90 : 90); handles.push({ handleId: `handle:cluster-carriageway-edge-offset:${side}:${item.key}`, @@ -1671,7 +1726,7 @@ function makeClusterCarriagewayHandles(model, junctionData, constraints, cluster position: offsetCoordinate(effectiveCenter, sideAzimuth, effectiveWidth / 2 + current), // The client reverses this shared rightward normal for a left-side drag. axisAzimuth: (effectiveTangent + 90) % 360, - value: { current, min: -10, max: 10, unit: 'meter' }, + value: { current, min: range.min, max: range.max, unit: 'meter' }, ...(edgeConstraint?.anchor.side === side ? { constraintId: edgeConstraint.id } : {}), affects: [`complex-junction:${clusterId}:carriageway:${item.approach.segmentId}`], editable: true, diff --git a/test/direct-edit-solver.js b/test/direct-edit-solver.js index 5ef7d22..69b7a99 100644 --- a/test/direct-edit-solver.js +++ b/test/direct-edit-solver.js @@ -530,6 +530,79 @@ const movedEdge = movedCluster.handles.handles.find( ); assert.ok(haversineMeters(movedEdge.position, clusterWidthHandle.position) > clusterWidthHandle.value.current / 2); assert.notEqual(movedEdge.axisAzimuth, clusterWidthHandle.axisAzimuth); +const movedPosition = movedCluster.handles.handles.find( + (handle) => + handle.kind === 'cluster-carriageway-position' && + handle.anchor.carriagewayKey === clusterWidthHandle.anchor.carriagewayKey, +); +const movedWidth = movedCluster.handles.handles.find( + (handle) => + handle.kind === 'cluster-carriageway-width' && + handle.anchor.carriagewayKey === clusterWidthHandle.anchor.carriagewayKey, +); +const movedDirection = movedCluster.handles.handles.find( + (handle) => + handle.kind === 'cluster-carriageway-direction' && + handle.anchor.carriagewayKey === clusterWidthHandle.anchor.carriagewayKey, +); +assert.deepEqual( + movedWidth.position, + movedPosition.position, + 'width and position controls share the effective inner section', +); +assert.deepEqual(movedDirection.interaction.center, movedPosition.position); +assert.equal(movedDirection.interaction.kind, 'rotation'); +assert.ok( + Math.abs( + haversineMeters(movedDirection.interaction.center, movedDirection.position) - + movedDirection.interaction.radiusMeters, + ) < 0.02, + 'direction control is a radial handle around the effective inner section', +); + +const initialEdges = clusterHandleResolution.handles.handles.filter( + (handle) => + handle.kind === 'cluster-carriageway-edge-offset' && + handle.anchor.carriagewayKey === clusterWidthHandle.anchor.carriagewayKey, +); +for (const edge of initialEdges) { + for (const [suffix, value] of [ + ['min', edge.value.min], + ['max', edge.value.max], + ]) { + const result = resolveDirectEditConstraints( + junctionModel, + document([ + constraint({ + id: `edge-bound-${edge.anchor.side}-${suffix}`, + kind: edge.kind, + anchor: edge.anchor, + value: + edge.anchor.side === 'left' + ? { leftOffsetMeters: value, rightOffsetMeters: 0 } + : { leftOffsetMeters: 0, rightOffsetMeters: value }, + }), + ]), + { junctionClusters: [{ id: 'cluster-1', nodeIds: ['junction'] }] }, + ); + assert.equal(result.constraintStates[0].applied, true, `${edge.anchor.side} ${suffix} must be accepted`); + } + const pastMax = resolveDirectEditConstraints( + junctionModel, + document([ + constraint({ + kind: edge.kind, + anchor: edge.anchor, + value: + edge.anchor.side === 'left' + ? { leftOffsetMeters: edge.value.max + 0.01, rightOffsetMeters: 0 } + : { leftOffsetMeters: 0, rightOffsetMeters: edge.value.max + 0.01 }, + }), + ]), + { junctionClusters: [{ id: 'cluster-1', nodeIds: ['junction'] }] }, + ); + assert.equal(pastMax.constraintStates[0].applied, false, `${edge.anchor.side} beyond max must be rejected`); +} const transitionedCluster = resolveDirectEditConstraints( junctionModel, diff --git a/workbench/client/src/edit/ghost-layer.ts b/workbench/client/src/edit/ghost-layer.ts index 7cedbac..bbd14cb 100644 --- a/workbench/client/src/edit/ghost-layer.ts +++ b/workbench/client/src/edit/ghost-layer.ts @@ -97,7 +97,7 @@ export class EditGhostLayer { this.source.addFeature( new Feature({ geometry: new Point(fromLonLat(handlePositionFor(handle, value))), - label: `${delta >= 0 ? '+' : ''}${delta.toFixed(2)} 米`, + label: `${delta >= 0 ? '+' : ''}${delta.toFixed(2)} ${handle.value.unit === 'degree' ? '°' : '米'}`, clamped, }), ); diff --git a/workbench/client/src/edit/projection.test.ts b/workbench/client/src/edit/projection.test.ts index 63e45c1..2ea6d6b 100644 --- a/workbench/client/src/edit/projection.test.ts +++ b/workbench/client/src/edit/projection.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { fromLonLat, haversineMeters, offsetCoordinate, type Coordinate } from './meters'; +import { azimuthBetween, fromLonLat, haversineMeters, offsetCoordinate, type Coordinate } from './meters'; import { anchorSnapshotFor, constraintValueFor, @@ -384,3 +384,35 @@ describe('complex transition length projection', () => { expect(projectHandleValue(handle, ...dragBy(handle.axisAzimuth, 100))).toBe(20); }); }); + +describe('complex carriageway direction projection', () => { + it('uses angular movement around the published inner-section center', () => { + const center: Coordinate = [121.5, 31.2]; + const handle: EditHandle = { + ...makeHandle( + 'cluster-carriageway-direction', + { + type: 'cluster-carriageway', + clusterId: 'cluster-1', + carriagewayKey: 'entry-1', + sourceNodeIds: ['node-1'], + sourceRoadIds: ['road-1'], + }, + 5, + -15, + 15, + ), + position: offsetCoordinate(center, 35, 6), + axisAzimuth: 35, + interaction: { kind: 'rotation', center, radiusMeters: 6 }, + value: { current: 5, min: -15, max: 15, unit: 'degree' }, + }; + const pointer = fromLonLat(offsetCoordinate(center, 45, 40)); + expect(projectHandleValue(handle, fromLonLat(handle.position), pointer)).toBeCloseTo(15, 4); + expect(constraintValueFor(handle, 15)).toEqual({ offsetDegrees: 15 }); + + const ghost = handlePositionFor(handle, 10); + expect(azimuthBetween(center, ghost)).toBeCloseTo(40, 3); + expect(haversineMeters(center, ghost)).toBeCloseTo(6, 3); + }); +}); diff --git a/workbench/client/src/edit/projection.ts b/workbench/client/src/edit/projection.ts index d941a7f..3444474 100644 --- a/workbench/client/src/edit/projection.ts +++ b/workbench/client/src/edit/projection.ts @@ -7,11 +7,13 @@ // writes a coordinate into a road polygon. import { + azimuthBetween, clamp, haversineMeters, offsetCoordinate, polylineLengthMeters, signedMetersAlongAxis, + toLonLat, type Coordinate, } from './meters'; import type { @@ -59,6 +61,13 @@ function outwardSign(handle: EditHandle): number { * declared. `from` / `to` are EPSG:3857 map coordinates. */ export function projectHandleValue(handle: EditHandle, from: Coordinate, to: Coordinate): number { + if (handle.interaction?.kind === 'rotation') { + const center = handle.interaction.center; + const start = azimuthBetween(center, handle.position); + const target = azimuthBetween(center, toLonLat(to)); + const delta = ((target - start + 540) % 360) - 180; + return clamp(handle.value.current + delta, handle.value.min, handle.value.max); + } const delta = signedMetersAlongAxis(from, to, handle.axisAzimuth) * outwardSign(handle); return clamp(handle.value.current + delta, handle.value.min, handle.value.max); } @@ -74,6 +83,10 @@ export function projectHandleValue(handle: EditHandle, from: Coordinate, to: Coo * from the clamped value instead pins the handle to the limit. */ export function handlePositionFor(handle: EditHandle, value: number): Coordinate { + if (handle.interaction?.kind === 'rotation') { + const delta = value - handle.value.current; + return offsetCoordinate(handle.interaction.center, handle.axisAzimuth + delta, handle.interaction.radiusMeters); + } const outward = outwardSign(handle) < 0 ? handle.axisAzimuth + 180 : handle.axisAzimuth; return offsetCoordinate(handle.position, outward, value - handle.value.current); } diff --git a/workbench/client/src/edit/types.ts b/workbench/client/src/edit/types.ts index 1796e0b..817b1a1 100644 --- a/workbench/client/src/edit/types.ts +++ b/workbench/client/src/edit/types.ts @@ -94,6 +94,8 @@ export interface EditHandle { position: [number, number]; /** Draggable direction, degrees clockwise from true north. */ axisAzimuth: number; + /** Optional non-linear pointer gesture declared by the server manifest. */ + interaction?: { kind: 'rotation'; center: [number, number]; radiusMeters: number }; value: { current: number; min: number; max: number; unit: 'meter' | 'degree' }; constraintId?: string; /** `native_id` of every derived feature this handle changes. */