diff --git a/.trellis/tasks/09-08-junction-reference-fidelity/implement.md b/.trellis/tasks/09-08-junction-reference-fidelity/implement.md index 7533ef8..92fa577 100644 --- a/.trellis/tasks/09-08-junction-reference-fidelity/implement.md +++ b/.trellis/tasks/09-08-junction-reference-fidelity/implement.md @@ -18,6 +18,7 @@ ## 2. 连续边界与区域求解(最高风险) +- [x] 将复杂路口边界半径映射到来源折线的实际首次相交站点,并把同一车行道裁切传递给道路面、边线、中心线、人行道和车道发布线。 - [ ] 实现接入断面到固定远端过渡;以共享边界构建中心/进口/角部,取消独立半径拼接作为权威。 - [ ] 支持凹形与已知排除区域,检查环、自交、接缝、必要连接;设计边界可行域并由 solver/manifest 共用。 - [ ] 审计 roadSurface、sidewalkSurface、edgeLines、lane/center markings 的每个裁切入口,确保消费同一计划。 diff --git a/src/compile/complex-junction.js b/src/compile/complex-junction.js index 727cf2d..6727bad 100644 --- a/src/compile/complex-junction.js +++ b/src/compile/complex-junction.js @@ -2,7 +2,7 @@ const fs = require('fs'); const { structureFromTopology } = require('./junction-topology'); -const { resolveJunctionBuildPlan } = require('./junction-build-plan'); +const { resolveJunctionBuildPlan, stationAtDistanceFrom } = require('./junction-build-plan'); const { convertGeoJson, boundsOf } = require('../reference/gaode'); const metricsCache = new WeakMap(); const CORNER_FILLET_SEGMENTS = 12; @@ -784,9 +784,8 @@ function positiveHeadingDelta(first, second) { return (((second - first) % 360) + 360) % 360; } function pointOnCarriagewayRadius(item, center, radius, helpers) { - const start = item.approach.line[0]; - const startRadius = directionalProjectionMeters(center, start, item.heading); - return helpers.pointAlongLine(item.approach.line, Math.max(0, Math.min(item.length, radius - startRadius))); + const station = stationAtDistanceFrom(item.approach.line, center, radius, helpers); + return helpers.pointAlongLine(item.approach.line, Math.max(0, Math.min(item.length, station))); } function armEnvelopeAtRadius(arm, radius, center, helpers) { if (!arm.members.length) return null; diff --git a/src/compile/junction-build-plan.js b/src/compile/junction-build-plan.js index 8ef6a7f..45bd775 100644 --- a/src/compile/junction-build-plan.js +++ b/src/compile/junction-build-plan.js @@ -49,6 +49,40 @@ function boundaryAt(point, tangent, widthMeters) { }; } +function interpolate(first, second, ratio) { + return [first[0] + (second[0] - first[0]) * ratio, first[1] + (second[1] - first[1]) * ratio]; +} + +// Return the first source-polyline station whose actual distance to `center` +// reaches the requested boundary. This intentionally does not project a fixed +// initial heading: curved approaches must terminate where their own geometry +// meets the boundary. +function stationAtDistanceFrom(line, center, radius, helpers) { + if (!center || !Array.isArray(line) || line.length < 2 || !Number.isFinite(radius)) return 0; + let station = 0; + let previousDistance = helpers.distanceMeters(line[0], center); + if (previousDistance >= radius) return 0; + for (let index = 1; index < line.length; index += 1) { + const start = line[index - 1]; + const end = line[index]; + const segmentLength = helpers.distanceMeters(start, end); + const endDistance = helpers.distanceMeters(end, center); + if (previousDistance < radius && endDistance >= radius) { + let low = 0; + let high = 1; + for (let iteration = 0; iteration < 32; iteration += 1) { + const middle = (low + high) / 2; + if (helpers.distanceMeters(interpolate(start, end, middle), center) < radius) low = middle; + else high = middle; + } + return station + segmentLength * ((low + high) / 2); + } + station += segmentLength; + previousDistance = endDistance; + } + return station; +} + function entrySectionFor(item, helpers) { const length = item.length; const line = item.approach.line; @@ -148,4 +182,4 @@ function resolveJunctionBuildPlan(model, cluster, helpers) { return { schema: 'junction-build-plan/v1', cluster, nodeIds: [...nodeIds].sort(), carriageways, arms }; } -module.exports = { MIN_ENTRY_SECTION_METERS, resolveJunctionBuildPlan }; +module.exports = { MIN_ENTRY_SECTION_METERS, resolveJunctionBuildPlan, stationAtDistanceFrom }; diff --git a/src/compile/native-road.js b/src/compile/native-road.js index c8eec71..c0aacfe 100644 --- a/src/compile/native-road.js +++ b/src/compile/native-road.js @@ -8,6 +8,7 @@ const { complexJunctionMetrics, resolveComplexJunctionTopology, } = require('./complex-junction'); +const { stationAtDistanceFrom } = require('./junction-build-plan'); 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. @@ -783,12 +784,21 @@ function compileGeometry(model, overrides = { overrides: [] }, options = {}) { if (generated.islands) generatedComplexSidewalks.push(...generated.islands); diagnostics.push(...generated.diagnostics); } - const lanes = compileLaneCenterlines(model, diagnostics, junctionPlans, options, { - crosswalks: generatedComplexCrosswalks, - stopLines: generatedComplexStopLines, - }); + const lanes = compileLaneCenterlines( + model, + diagnostics, + junctionPlans, + options, + { + crosswalks: generatedComplexCrosswalks, + stopLines: generatedComplexStopLines, + }, + complexApproachBoundaries, + ); const edgeLines = - options.edgeLines === false ? [] : compileEdgeLines(model, overrides, junctionPlans, options, features); + options.edgeLines === false + ? [] + : compileEdgeLines(model, overrides, junctionPlans, options, features, complexApproachBoundaries); const controls = compileControlMarkings( model, lanes, @@ -800,13 +810,21 @@ function compileGeometry(model, overrides = { overrides: [] }, options = {}) { crosswalks: [...controls.crosswalks, ...generatedComplexCrosswalks], stopLines: [...controls.stopLines, ...generatedComplexStopLines], }; - const centerLines = compileCenterLines(model, overrides, junctionPlans, allControls, diagnostics, options); + const centerLines = compileCenterLines( + model, + overrides, + junctionPlans, + allControls, + diagnostics, + options, + complexApproachBoundaries, + ); const markings = compileLaneMarkings(model, overrides, lanes, diagnostics, junctionPlans, allControls, options); markings.separators.push( ...compileComplexLaneSeparators(lanes.features, [...allControls.crosswalks, ...allControls.stopLines]), ); markings.directionArrows.push(...compileComplexPreviewArrows(lanes.features, generatedComplexStopLines)); - const sidewalks = compileSidewalkSurfaces(model, diagnostics, junctionPlans, options); + const sidewalks = compileSidewalkSurfaces(model, diagnostics, junctionPlans, options, complexApproachBoundaries); // Complex crosswalks are generated from native approach tangents; do not // synthesize side strips that can be mistaken for crosswalks. sidewalks.push(...generatedComplexSidewalks); @@ -1068,7 +1086,14 @@ function laneWasClipped(original, visible) { return Boolean(visible) && lineLengthMeters(visible) < lineLengthMeters(original) - 0.01; } -function compileEdgeLines(model, overrides, junctionPlans, options = {}, roadSurfaces = []) { +function compileEdgeLines( + model, + overrides, + junctionPlans, + options = {}, + roadSurfaces = [], + complexBoundaries = new Map(), +) { const features = []; const clusters = options.junctionTemplates?.enabled ? options.junctionTemplates.clusters || [] : []; const clusterByNode = new Map( @@ -1104,6 +1129,7 @@ function compileEdgeLines(model, overrides, junctionPlans, options = {}, roadSur junctionPlans, cluster, clusterCenters.get(cluster.id), + complexBoundaries.get(cluster.id)?.get(road.id), ) : trimLineAtJunctions(road.centerline, road.sourceNodeIds, junctionPlans); // On a two-way segment, the inner edge is the road centre boundary and is @@ -1155,7 +1181,15 @@ function edgeLineFeature(road, side, style, ring, part = null) { }; } -function compileCenterLines(model, overrides, junctionPlans, controls, diagnostics, options = {}) { +function compileCenterLines( + model, + overrides, + junctionPlans, + controls, + diagnostics, + options = {}, + complexBoundaries = new Map(), +) { const features = []; const controlFeatures = [...controls.crosswalks, ...controls.stopLines]; const segments = new Map(); @@ -1177,6 +1211,7 @@ function compileCenterLines(model, overrides, junctionPlans, controls, diagnosti junctionPlans, cluster, clusterCenter(cluster, junctionPlans), + complexBoundaries.get(cluster.id)?.get(forward.id), ) : trimLineAtJunctions(forward.centerline, forward.sourceNodeIds, junctionPlans); const length = lineLengthMeters(line); @@ -1827,7 +1862,7 @@ function ringsOverlap(first, second) { ); } -function compileSidewalkSurfaces(model, diagnostics, junctionPlans, options = {}) { +function compileSidewalkSurfaces(model, diagnostics, junctionPlans, options = {}, complexBoundaries = new Map()) { const features = []; const byWay = new Map(); for (const road of model.roads) { @@ -1850,7 +1885,14 @@ function compileSidewalkSurfaces(model, diagnostics, junctionPlans, options = {} const sidewalkWidth = profile?.sidewalkWidths?.[side] ?? DEFAULT_SIDEWALK_WIDTH_METERS; if (!enabled || sidewalkWidth <= 0) continue; const centerline = cluster - ? trimLineAtComplexCluster(forward.centerline, forward.sourceNodeIds, junctionPlans, cluster, center) + ? trimLineAtComplexCluster( + forward.centerline, + forward.sourceNodeIds, + junctionPlans, + cluster, + center, + complexBoundaries.get(cluster.id)?.get(forward.id), + ) : trimLineAtJunctions(forward.centerline, forward.sourceNodeIds, junctionPlans); const ring = sidewalkRing(centerline, totalWidth / 2, totalWidth / 2 + sidewalkWidth, side === 'left' ? 1 : -1); if (!ring) { @@ -2088,7 +2130,14 @@ function pointOnSegment(point, a, b) { // templates already emitted. Ordinary controls cannot be passed here: they are // placed *from* these lane centerlines, so only the template-generated ones // exist this early. -function compileLaneCenterlines(model, diagnostics, junctionPlans, options = {}, complexControls = {}) { +function compileLaneCenterlines( + model, + diagnostics, + junctionPlans, + options = {}, + complexControls = {}, + complexBoundaries = new Map(), +) { const features = []; const controlFeatures = [...(complexControls.crosswalks || []), ...(complexControls.stopLines || [])]; const byRoadId = new Map(); @@ -2115,6 +2164,7 @@ function compileLaneCenterlines(model, diagnostics, junctionPlans, options = {}, junctionPlans, boundaryCluster, clusterCenters.get(boundaryCluster.id), + complexBoundaries.get(boundaryCluster.id)?.get(road.id), ) : trimLineAtJunctions(road.centerline, road.sourceNodeIds, junctionPlans); // OSM centerline is the shared carriageway center. On a two-way road, @@ -3237,12 +3287,7 @@ function complexApproachBoundaryRadii(model, clusters, junctionPlans) { } function distanceAlongLineToRadius(line, center, radius) { - if (!center || line.length < 2) return 0; - const heading = headingAtEndpoint(line); - const vector = project(line[0], center); - const radians = (heading * Math.PI) / 180; - const startRadius = vector[0] * Math.sin(radians) + vector[1] * Math.cos(radians); - return Math.max(0, radius - startRadius); + return stationAtDistanceFrom(line, center, radius, { distanceMeters }); } function trimLineRange(line, startCutback, endCutback) { diff --git a/test/junction-build-plan-counterexamples.js b/test/junction-build-plan-counterexamples.js index c72cd84..9fca9f5 100644 --- a/test/junction-build-plan-counterexamples.js +++ b/test/junction-build-plan-counterexamples.js @@ -3,7 +3,7 @@ // Source-independent counterexamples for the JunctionBuildPlan contract. const assert = require('assert/strict'); -const { resolveJunctionBuildPlan } = require('../src/compile/junction-build-plan'); +const { resolveJunctionBuildPlan, stationAtDistanceFrom } = require('../src/compile/junction-build-plan'); const { classifyJunctionShape } = require('../src/compile/complex-junction'); const metersPerDegree = 111320; @@ -22,6 +22,22 @@ function length(line) { return line.slice(1).reduce((total, item, index) => total + distance(line[index], item), 0); } +function pointAtStation(line, station) { + let remaining = station; + for (let index = 1; index < line.length; index += 1) { + const segmentLength = distance(line[index - 1], line[index]); + if (remaining <= segmentLength) { + const ratio = remaining / segmentLength; + return [ + line[index - 1][0] + (line[index][0] - line[index - 1][0]) * ratio, + line[index - 1][1] + (line[index][1] - line[index - 1][1]) * ratio, + ]; + } + remaining -= segmentLength; + } + return line.at(-1); +} + function fixture(name, entries) { const nodeId = `${name}:node`; const approaches = entries.map((entry, index) => ({ @@ -120,4 +136,10 @@ assert.notEqual( curvedShort.topology.carriageways[0].entrySection.farTangent, ); +const curvedBoundaryLine = [point(0, 0), point(0, 5), point(10, 10)]; +const curvedBoundaryStation = stationAtDistanceFrom(curvedBoundaryLine, point(0, 0), 9, { + distanceMeters: distance, +}); +assert.ok(Math.abs(distance(pointAtStation(curvedBoundaryLine, curvedBoundaryStation), point(0, 0)) - 9) < 0.01); + console.log('junction build-plan counterexample tests passed');