From f3924caa5c5e45f4d6880985c10edcc3ccbacedb Mon Sep 17 00:00:00 2001 From: que01 Date: Mon, 31 Aug 2026 17:10:45 +0800 Subject: [PATCH] feat: make crosswalk size a constraint the geometry stage reads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every zebra was six stripes at 3m by 0.45m on a 0.9m pitch, all literals. Depth and stripe count are now a constraint, junction-crosswalk-size, and the loop reads them. The span across the road stays derived rather than stored: it is count times pitch, and pitch is fixed because stripe and gap widths are standardised while the road they cross is not. Widening a crossing means adding stripes, which is what a real one does, and it is why the span quantises in 0.9m steps. That also fixes a span pinned at 4.95m regardless of the street. Measured, every crossing sat on a 6.50m carriageway and covered under four fifths of it. The default now follows the segment width — both directions, since a crossing spans the carriageway — giving eight stripes and 6.74m, reaching the kerb rather than stopping short. Fixture baselines move for exactly two entries on the one area with crossings; the area without them is untouched. No handle is published yet. design.md requires proving compileGeometry() consumes a parameter before any grip is drawn — the rule left over from profile.interval shipping a handle that did nothing — so the grips come next, on a value already known to work. Adding the kind made the client's exhaustive scalar switch fail to compile, which is the check working: size is a pair and has no dragged-scalar form, so that case now says so and throws rather than writing half a value. Co-Authored-By: Claude Opus 5 (1M context) --- .../design.md | 10 ++- src/compile/direct-edit-solver.js | 48 ++++++++++++- src/compile/native-road-edits.js | 9 +++ src/compile/native-road.js | 70 ++++++++++++++++--- .../nantaizi-lake-innovation-valley.json | 6 +- test/direct-edit-solver.js | 31 ++++++++ test/native-road-edits.js | 3 + workbench/client/src/edit/parameter.ts | 1 + workbench/client/src/edit/projection.ts | 6 ++ workbench/client/src/edit/types.ts | 5 ++ 10 files changed, 176 insertions(+), 13 deletions(-) diff --git a/.trellis/tasks/08-26-direct-manipulation-road-editor/design.md b/.trellis/tasks/08-26-direct-manipulation-road-editor/design.md index 56609a6..585d040 100644 --- a/.trellis/tasks/08-26-direct-manipulation-road-editor/design.md +++ b/.trellis/tasks/08-26-direct-manipulation-road-editor/design.md @@ -38,6 +38,7 @@ type RoadConstraintKind = | 'junction-cutback' | 'junction-corner-radius' | 'junction-crosswalk-inset' + | 'junction-crosswalk-size' | 'junction-stop-line-offset' ``` @@ -50,14 +51,21 @@ type RoadConstraintKind = | `junction-cutback` | `junction-approach` | `{ cutbackMeters }` | 米 | JunctionTools | | `junction-corner-radius` | `junction-corner` | `{ radiusMeters }` | 米 | JunctionTools | | `junction-crosswalk-inset` | `junction-approach` | `{ insetMeters }` | 米,斑马线距路口边界的退让 | 主地图 / JunctionTools | +| `junction-crosswalk-size` | `junction-approach` | `{ depthMeters, stripeCount }` | 米 + 整数条带数 | 主地图 | | `junction-stop-line-offset` | `junction-approach` | `{ offsetMeters }` | 米,停止线沿进口后退的距离 | 主地图 / JunctionTools | 前 6 个 kind 对应 PRD 首期范围(外缘、步行带、车道分隔 + 进口、cutback、角部)。 -后 2 个于 2026-08-28 追加,是「更多元素可直接操纵」方向的第一类扩展:控制标线的**位置** +`junction-crosswalk-inset` 与 `junction-stop-line-offset` 于 2026-08-28 追加,是「更多元素可直接操纵」方向的第一类扩展:控制标线的**位置** 成为可编辑参数。它们不改变路口形状,只移动标线,因此不触发路口边界重算。 放置是否成立的判据(`STOP_LINE_MAX_APPROACH_DISTANCE_METERS` 等)不在此范围内。 +`junction-crosswalk-size` 于 2026-08-31 追加,是第一个改变标线**自身尺寸**而非位置的 kind。 +横跨道路的跨度不是存储值,而是 `stripeCount × pitch` 派生;pitch 固定 0.9m 不可编辑, +因为条带宽与间隙在现实中是标准化的,而它横跨的道路不是。加宽靠增加条带数, +所以跨度是**量化**的。同批修掉了「跨度恒为 4.95m,与路宽无关」的缺陷: +无约束时条带数由整段车行道宽度推导。 + 新增 kind 的前提:**几何阶段必须已经消费它**。本项目曾为 `profile.interval` 交付过 一个拖动无效果的手柄(见 `08-26-direct-edit-map-editor/research/interval-not-applied.md`), 所以扩展 kind 时先证明 `compileGeometry()` 读取该参数,再画任何手柄。 diff --git a/src/compile/direct-edit-solver.js b/src/compile/direct-edit-solver.js index 3812f66..82b9e1e 100644 --- a/src/compile/direct-edit-solver.js +++ b/src/compile/direct-edit-solver.js @@ -25,6 +25,7 @@ const SOLVED_KINDS = new Set([ 'junction-cutback', 'junction-corner-radius', 'junction-crosswalk-inset', + 'junction-crosswalk-size', 'junction-stop-line-offset', ]); const KINDS = [ @@ -35,6 +36,7 @@ const KINDS = [ 'junction-cutback', 'junction-corner-radius', 'junction-crosswalk-inset', + 'junction-crosswalk-size', 'junction-stop-line-offset', ]; const MIN_LANE_WIDTH_METERS = 2.4; @@ -47,9 +49,19 @@ const MIN_STOP_LINE_OFFSET_METERS = 0.5; // These live here rather than in native-road.js because the solver must stay free // of fs/path — a test asserts that — so the dependency can only point this way. const CROSSWALK_JUNCTION_INSET_METERS = 1.5; +// Zebra geometry. Depth runs along the approach; the span across the road is +// `stripeCount * pitch`, not a stored number, because stripe and gap widths are +// standardised while the road they cross is not. Pitch is not editable this phase. +const CROSSWALK_DEPTH_METERS = 3; +const CROSSWALK_STRIPE_WIDTH_METERS = 0.45; +const CROSSWALK_STRIPE_PITCH_METERS = 0.9; +const MIN_CROSSWALK_DEPTH_METERS = 1; +const MAX_CROSSWALK_DEPTH_METERS = 12; +const MIN_CROSSWALK_STRIPE_COUNT = 2; +const MAX_CROSSWALK_STRIPE_COUNT = 40; const STOP_LINE_OFFSET_METERS = 2.7; /** Junction-anchored but owned by the main map, unlike the junction shape kinds. */ -const CONTROL_KINDS = new Set(['junction-crosswalk-inset', 'junction-stop-line-offset']); +const CONTROL_KINDS = new Set(['junction-crosswalk-inset', 'junction-crosswalk-size', 'junction-stop-line-offset']); const GEOMETRY_VERSION = 'native-road-package/v1.1'; function emptyHandleManifest(context) { @@ -424,6 +436,35 @@ function applyJunctionConstraint(model, constraint, junctionPlans, junctionData, entry.crosswalkInsetMeters = inset; return true; } + case 'junction-crosswalk-size': { + const depth = Number(constraint.value?.depthMeters); + const stripeCount = constraint.value?.stripeCount; + if (!Number.isFinite(depth) || depth < MIN_CROSSWALK_DEPTH_METERS || depth > MAX_CROSSWALK_DEPTH_METERS) { + blocking( + diagnostics, + constraint, + 'direct-edit-crosswalk-depth-invalid', + `斑马线进深必须在 ${MIN_CROSSWALK_DEPTH_METERS} 到 ${MAX_CROSSWALK_DEPTH_METERS} 米之间。`, + ); + return false; + } + if ( + !Number.isInteger(stripeCount) || + stripeCount < MIN_CROSSWALK_STRIPE_COUNT || + stripeCount > MAX_CROSSWALK_STRIPE_COUNT + ) { + blocking( + diagnostics, + constraint, + 'direct-edit-crosswalk-stripes-invalid', + `斑马线条带数必须是 ${MIN_CROSSWALK_STRIPE_COUNT} 到 ${MAX_CROSSWALK_STRIPE_COUNT} 之间的整数。`, + ); + return false; + } + entry.crosswalkDepthMeters = depth; + entry.crosswalkStripeCount = stripeCount; + return true; + } case 'junction-stop-line-offset': { const offset = Number(constraint.value?.offsetMeters); if (!Number.isFinite(offset) || offset < MIN_STOP_LINE_OFFSET_METERS || offset > approach.length * 0.45) { @@ -874,6 +915,11 @@ function resolveDirectEditConstraints(model, editDocument = null, context = {}) } module.exports = { + CROSSWALK_DEPTH_METERS, + CROSSWALK_STRIPE_PITCH_METERS, + CROSSWALK_STRIPE_WIDTH_METERS, + MAX_CROSSWALK_STRIPE_COUNT, + MIN_CROSSWALK_STRIPE_COUNT, CROSSWALK_JUNCTION_INSET_METERS, STOP_LINE_OFFSET_METERS, RESOLUTION_SCHEMA, diff --git a/src/compile/native-road-edits.js b/src/compile/native-road-edits.js index fa59646..0bc584c 100644 --- a/src/compile/native-road-edits.js +++ b/src/compile/native-road-edits.js @@ -16,6 +16,7 @@ const CONSTRAINT_ANCHORS = { 'junction-cutback': 'junction-approach', 'junction-corner-radius': 'junction-corner', 'junction-crosswalk-inset': 'junction-approach', + 'junction-crosswalk-size': 'junction-approach', 'junction-stop-line-offset': 'junction-approach', }; const CONSTRAINT_KINDS = Object.freeze(Object.keys(CONSTRAINT_ANCHORS)); @@ -120,6 +121,14 @@ function validateValue(scope, kind, value) { // it inside the intersection surface. if (!isFiniteNumber(value.insetMeters) || value.insetMeters < 0) fail(scope, 'value.insetMeters', 'must be a non-negative number of meters'); + } else if (kind === 'junction-crosswalk-size') { + // Depth runs along the approach; the span across the road is derived from the + // stripe count at a fixed pitch, because stripe and gap widths are + // standardised while the road they cross is not. + if (!isFiniteNumber(value.depthMeters) || value.depthMeters <= 0) + fail(scope, 'value.depthMeters', 'must be a positive number of meters'); + if (!Number.isInteger(value.stripeCount) || value.stripeCount < 2) + fail(scope, 'value.stripeCount', 'must be an integer of at least 2'); } else if (kind === 'junction-stop-line-offset') { // Distance from the crossing back along the approach. Zero would put the stop // line on the crosswalk itself. diff --git a/src/compile/native-road.js b/src/compile/native-road.js index 8da408f..afd717d 100644 --- a/src/compile/native-road.js +++ b/src/compile/native-road.js @@ -7,7 +7,15 @@ const { buildComplexJunctionGeometry, complexJunctionMetrics } = require('./comp const { diagnostic } = require('./diagnostics'); // Default marking placement lives with the constraint bounds in the solver, which // is the IO-free module; importing the other way would drag fs into it. -const { CROSSWALK_JUNCTION_INSET_METERS, STOP_LINE_OFFSET_METERS } = require('./direct-edit-solver'); +const { + CROSSWALK_DEPTH_METERS, + CROSSWALK_JUNCTION_INSET_METERS, + CROSSWALK_STRIPE_PITCH_METERS, + CROSSWALK_STRIPE_WIDTH_METERS, + MAX_CROSSWALK_STRIPE_COUNT, + MIN_CROSSWALK_STRIPE_COUNT, + STOP_LINE_OFFSET_METERS, +} = require('./direct-edit-solver'); const { roadSurfaceId, sidewalkId, @@ -861,6 +869,9 @@ function applyDirectJunctionPlans(plans, directEdit) { // reshape the junction, so they deliberately do not set `changed` and trigger // a boundary recompute. if (Number.isFinite(override.crosswalkInsetMeters)) approach.crosswalkInsetMeters = override.crosswalkInsetMeters; + if (Number.isFinite(override.crosswalkDepthMeters)) approach.crosswalkDepthMeters = override.crosswalkDepthMeters; + if (Number.isInteger(override.crosswalkStripeCount)) + approach.crosswalkStripeCount = override.crosswalkStripeCount; if (Number.isFinite(override.stopLineOffsetMeters)) approach.stopLineOffsetMeters = override.stopLineOffsetMeters; } const cutbacks = Object.values(directPlan.approaches || {}) @@ -1253,18 +1264,30 @@ function compileControlMarkings(model, lanes, diagnostics, junctionPlans = new M (a, b) => a.junctionDistanceMeters - b.junctionDistanceMeters || a.placement.distance - b.placement.distance, )[0]; const crosswalkCandidate = approach || candidate; - const junctionInsetMeters = approach ? crossingJunctionInset(approach, junctionPlans) : 0; + const junctionInsetMeters = approach ? crossingJunctionInset(approach, junctionPlans, model) : 0; const controlCenter = offsetByMeters(crossing.coordinate, crosswalkCandidate.placement.axis, junctionInsetMeters); const { axis } = crosswalkCandidate.placement; const across = [-axis[1], axis[0]]; - for (let index = 0; index < 6; index += 1) + // Depth and stripe count are the editable pair; the span across the road is + // `(count - 1) * pitch`, centred on the crossing, and therefore quantised. + const size = approachControls(junctionPlans, crosswalkCandidate.road, model); + const stripeCount = size.crosswalkStripeCount; + const firstOffset = -((stripeCount - 1) * CROSSWALK_STRIPE_PITCH_METERS) / 2; + for (let index = 0; index < stripeCount; index += 1) crosswalks.push( controlFeature( 'crosswalk', crossing, crosswalkCandidate, index + 1, - rectangleAt(controlCenter, axis, across, 3, 0.45, -2.25 + index * 0.9), + rectangleAt( + controlCenter, + axis, + across, + size.crosswalkDepthMeters, + CROSSWALK_STRIPE_WIDTH_METERS, + firstOffset + index * CROSSWALK_STRIPE_PITCH_METERS, + ), { junctionInsetMeters }, ), ); @@ -1288,7 +1311,7 @@ function compileControlMarkings(model, lanes, diagnostics, junctionPlans = new M const stopCenter = offsetByMeters( laneCenterAtCrossing, approach.placement.axis, - -approachControls(junctionPlans, approach.road).stopLineOffsetMeters, + -approachControls(junctionPlans, approach.road, model).stopLineOffsetMeters, ); stopLines.push( controlFeature( @@ -1309,10 +1332,41 @@ function compileControlMarkings(model, lanes, diagnostics, junctionPlans = new M * defaults. The junction a road approaches is the one at its far node, which is * how `crossingJunctionInset()` has always resolved it. */ -function approachControls(junctionPlans, road) { +/** + * Stripes needed to carry a zebra across `widthMeters`. + * + * Span is `(count - 1) * pitch + stripeWidth`, so this inverts that and rounds + * up: a crossing that stops short of the kerb is worse than one that overruns it + * slightly. Pitch is fixed because stripe and gap widths are standardised while + * the road they cross is not — widening by adding stripes is what a real crossing + * does, and it is why the span quantises. + */ +function stripeCountForWidth(widthMeters) { + if (!Number.isFinite(widthMeters) || widthMeters <= 0) return MIN_CROSSWALK_STRIPE_COUNT; + const spans = Math.ceil((widthMeters - CROSSWALK_STRIPE_WIDTH_METERS) / CROSSWALK_STRIPE_PITCH_METERS); + return Math.min(MAX_CROSSWALK_STRIPE_COUNT, Math.max(MIN_CROSSWALK_STRIPE_COUNT, spans + 1)); +} + +/** Total carriageway width at a segment: a crossing spans both directions. */ +function segmentWidthMeters(model, road) { + const roads = (model?.roads || []).filter((item) => item.segmentId === road?.segmentId); + const total = roads.reduce((sum, item) => sum + (Number(item.widthMeters) || 0), 0); + return total > 0 ? total : Number(road?.widthMeters) || 0; +} + +function approachControls(junctionPlans, road, model) { const plan = junctionPlans.get(road?.sourceNodeIds?.at(-1)); const approach = plan?.approaches?.find?.((item) => item.segmentId === road.segmentId); return { + crosswalkDepthMeters: Number.isFinite(approach?.crosswalkDepthMeters) + ? approach.crosswalkDepthMeters + : CROSSWALK_DEPTH_METERS, + // Default follows the road. The span used to be a hard-coded six stripes, + // i.e. 4.95m on every crossing whatever the street was — under half the + // carriageway on a wide one. + crosswalkStripeCount: Number.isInteger(approach?.crosswalkStripeCount) + ? approach.crosswalkStripeCount + : stripeCountForWidth(segmentWidthMeters(model, road)), crosswalkInsetMeters: Number.isFinite(approach?.crosswalkInsetMeters) ? approach.crosswalkInsetMeters : CROSSWALK_JUNCTION_INSET_METERS, @@ -1322,11 +1376,11 @@ function approachControls(junctionPlans, road) { }; } -function crossingJunctionInset(candidate, junctionPlans) { +function crossingJunctionInset(candidate, junctionPlans, model) { const junctionNodeId = candidate.road.sourceNodeIds.at(-1); const plan = junctionPlans.get(junctionNodeId); if (!plan) return 0; - const inset = approachControls(junctionPlans, candidate.road).crosswalkInsetMeters; + const inset = approachControls(junctionPlans, candidate.road, model).crosswalkInsetMeters; const targetDistance = Math.max(0, plan.cutbackMeters - inset); return Math.min(CROSSWALK_MAX_JUNCTION_INSET_METERS, Math.max(0, candidate.junctionDistanceMeters - targetDistance)); } diff --git a/test/baseline/nantaizi-lake-innovation-valley.json b/test/baseline/nantaizi-lake-innovation-valley.json index 5f713db..4e3b40d 100644 --- a/test/baseline/nantaizi-lake-innovation-valley.json +++ b/test/baseline/nantaizi-lake-innovation-valley.json @@ -7,7 +7,7 @@ "bytes": 18111 }, "comparison.json": { - "contentHash": "d8d4fc7d72527fce08c766f1619777a69813080a2def35652c402edd55a64a2e", + "contentHash": "11a057394a8023953386c4dc2b35022281eaf7a5d61f989a82b4f43c33d64a32", "bytes": 1015 }, "compiled.json": { @@ -27,8 +27,8 @@ "bytes": 99978 }, "layers/crosswalks.geojson": { - "contentHash": "72079521c1f6891b77b226b87ee3656c113f3a72c4fb51981dfdaa9dafc4eeb2", - "bytes": 28012 + "contentHash": "3123c6f4ae5f35e91aff854576df6131d20341d6297451fe262b9eed4c7f1cc1", + "bytes": 37342 }, "layers/direction_arrows.geojson": { "contentHash": "5e60d48520fa09aa75a8aa9bb89c4701c7f5dabc4e5ce57c9dc87bdefbbf3d1c", diff --git a/test/direct-edit-solver.js b/test/direct-edit-solver.js index d917286..ac1b39e 100644 --- a/test/direct-edit-solver.js +++ b/test/direct-edit-solver.js @@ -499,6 +499,37 @@ const stopOnly = resolveDirectEditConstraints( ).junctionPlans.get('junction').approaches['segment:a']; assert.equal(stopOnly.cutbackMeters !== 4, true, 'a stop-line offset must not be written as a cutback'); +// Crosswalk size reaches the geometry, and bad values are refused rather than +// clamped. The kind exists only because compileGeometry() reads it — the rule +// design.md sets after `profile.interval` shipped a handle that did nothing. +{ + const sized = resolveDirectEditConstraints( + controlModel, + document([controlConstraint('junction-crosswalk-size', { depthMeters: 4, stripeCount: 10 })]), + {}, + ); + const plan = sized.junctionPlans.get('junction').approaches['segment:a']; + assert.equal(plan.crosswalkDepthMeters, 4); + assert.equal(plan.crosswalkStripeCount, 10); + + for (const [value, rule] of [ + [{ depthMeters: 0, stripeCount: 8 }, 'direct-edit-crosswalk-depth-invalid'], + [{ depthMeters: 3, stripeCount: 1 }, 'direct-edit-crosswalk-stripes-invalid'], + [{ depthMeters: 3, stripeCount: 6.5 }, 'direct-edit-crosswalk-stripes-invalid'], + ]) { + const bad = resolveDirectEditConstraints( + controlModel, + document([controlConstraint('junction-crosswalk-size', value)]), + {}, + ); + assert.equal(bad.constraintStates[0].applied, false); + assert.ok( + bad.diagnostics.some((item) => item.rule === rule), + `${JSON.stringify(value)} must produce ${rule}`, + ); + } +} + // A crosswalk-inset handle must publish the ceiling that still moves the zebra. // // `crossingJunctionInset()` saturates once the inset reaches the cutback, and diff --git a/test/native-road-edits.js b/test/native-road-edits.js index 8bbe7c1..3604667 100644 --- a/test/native-road-edits.js +++ b/test/native-road-edits.js @@ -47,6 +47,8 @@ function valueFor(kind) { return { radiusMeters: 6 }; case 'junction-crosswalk-inset': return { insetMeters: 1.5 }; + case 'junction-crosswalk-size': + return { depthMeters: 3, stripeCount: 8 }; case 'junction-stop-line-offset': return { offsetMeters: 2.7 }; default: @@ -100,6 +102,7 @@ assert.deepEqual(CONSTRAINT_KINDS, [ 'junction-cutback', 'junction-corner-radius', 'junction-crosswalk-inset', + 'junction-crosswalk-size', 'junction-stop-line-offset', ]); diff --git a/workbench/client/src/edit/parameter.ts b/workbench/client/src/edit/parameter.ts index db90a02..1d4cdbe 100644 --- a/workbench/client/src/edit/parameter.ts +++ b/workbench/client/src/edit/parameter.ts @@ -115,6 +115,7 @@ const KIND_LABELS: Record = { 'junction-cutback': '进口 cutback', 'junction-corner-radius': '角部圆角', 'junction-crosswalk-inset': '斑马线退让', + 'junction-crosswalk-size': '斑马线尺寸', 'junction-stop-line-offset': '停止线偏移', }; diff --git a/workbench/client/src/edit/projection.ts b/workbench/client/src/edit/projection.ts index 0e44b71..652422b 100644 --- a/workbench/client/src/edit/projection.ts +++ b/workbench/client/src/edit/projection.ts @@ -108,6 +108,12 @@ export function constraintValueFor( return { insetMeters: Math.max(0, scalar) }; case 'junction-stop-line-offset': return { offsetMeters: Math.max(0, scalar) }; + case 'junction-crosswalk-size': + // Size is a pair — depth and stripe count — so no single dragged scalar + // produces it, and the solver emits no handle of this kind. Reaching here + // means one appeared without a matching input path; failing loudly beats + // writing half a value. + throw new Error('junction-crosswalk-size has no scalar form; it is written by the element size controls'); } } diff --git a/workbench/client/src/edit/types.ts b/workbench/client/src/edit/types.ts index 329700d..8e1112a 100644 --- a/workbench/client/src/edit/types.ts +++ b/workbench/client/src/edit/types.ts @@ -29,6 +29,10 @@ export const JUNCTION_KINDS = [ 'junction-cutback', 'junction-corner-radius', ...CONTROL_KINDS, + // Size is deliberately outside CONTROL_KINDS: it has no handle yet, and its + // value is a pair rather than the scalar the parameter panel renders. It joins + // the panel when the corner grips exist to drive it. + 'junction-crosswalk-size', ] as const; export type RoadConstraintKind = (typeof ROAD_KINDS)[number]; @@ -98,6 +102,7 @@ export type ConstraintValue = | { offsetMeters: number; transition?: Transition } | { widthMeters: number; transition?: Transition } | { boundaryIndex: number; offsetMeters: number; transition?: Transition } + | { depthMeters: number; stripeCount: number } | { cutbackMeters: number } | { radiusMeters: number } | { insetMeters: number };