diff --git a/src/compile/complex-junction.js b/src/compile/complex-junction.js index 9f2990f..231dc1b 100644 --- a/src/compile/complex-junction.js +++ b/src/compile/complex-junction.js @@ -11,11 +11,6 @@ const CORNER_FILLET_SEGMENTS = 12; // Must match DEFAULT_SIDEWALK_WIDTH_METERS in native-road.js so the corner band // lines up with the straight strips it joins. const SIDEWALK_WIDTH_METERS = 2; -// The straight strips are trimmed against the cluster boundary using the road -// centerline, so they stop a little beyond the carriageway end. Run the corner -// past that end and let the two overlap rather than chase an exact seam. -const SIDEWALK_CORNER_OVERRUN_METERS = 6; - // Compatibility export for current compiler consumers. The build-plan module is // pure and is the shared authority for geometry and direct-edit identity. const resolveComplexJunctionTopology = resolveJunctionBuildPlan; @@ -426,26 +421,24 @@ function buildComplexJunctionGeometry(model, cluster, helpers) { // The corner faces clockwise from `first` and counter-clockwise from // `second`, so each arm must carry the footway on that facing side. if (!armCarriesSidewalk(first, model, true) || !armCarriesSidewalk(second, model, false)) continue; - const curb = [ - ...edgeRunToRadius( - apex, - edges[0], - tangentDistance, - outerRadius + SIDEWALK_CORNER_OVERRUN_METERS, - center, - helpers, - ).reverse(), - ...curve.slice(1, -1), - ...edgeRunToRadius( - apex, - edges[1], - tangentDistance, - outerRadius + SIDEWALK_CORNER_OVERRUN_METERS, - center, - helpers, - ), - ]; + const seams = edges.map((edge) => exteriorSidewalkSeam(edge, boundaryGraph, helpers)); + if (!seams.every(Boolean)) { + cornerDiagnostics.push( + helpers.diagnostic( + 'warning', + `corner:${first.cornerKey}`, + edges.flatMap((edge) => edge.roadIds), + 'complex-junction-sidewalk-seam-unavailable', + '该角部缺少两侧可验证的人行道接缝,已省略连接,避免生成悬空步行带。', + center, + ), + ); + continue; + } + const curb = [seams[0].curb, ...curve, seams[1].curb]; const outerEdge = offsetPolylineAwayFromCenter(curb, center, SIDEWALK_WIDTH_METERS, helpers); + outerEdge[0] = seams[0].outer; + outerEdge[outerEdge.length - 1] = seams[1].outer; const sidewalkRing = [...curb, ...outerEdge.slice().reverse(), curb[0]]; if (!sidewalkRing.every((point) => point.every(Number.isFinite)) || ringSelfIntersects(sidewalkRing)) { cornerDiagnostics.push( @@ -473,7 +466,7 @@ function buildComplexJunctionGeometry(model, cluster, helpers) { to_heading: second.heading, bisector_heading: bisector, width_m: SIDEWALK_WIDTH_METERS, - overrun_m: SIDEWALK_CORNER_OVERRUN_METERS, + seam_source: 'published-exterior-sidewalk', provenance: 'native-road-complex-junction-sidewalk-corner/v1', }, geometry: { type: 'Polygon', coordinates: [sidewalkRing] }, @@ -729,16 +722,31 @@ function armCarriesSidewalk(arm, model, cornerIsClockwise) { ); } -// Walk outward along a wedge edge from its tangent point until the curb reaches -// `targetRadius`, so the corner band overlaps the straight strip it joins. -function edgeRunToRadius(apex, edge, tangentDistance, targetRadius, center, helpers) { - const points = []; - for (let extra = 0; extra <= 40; extra += 2) { - const point = helpers.offsetCoordinate(apex, edge.heading, tangentDistance + extra); - points.push(point); - if (helpers.distanceMeters(point, center) >= targetRadius) break; +// Use the emitted strip's complete end edge, including its actual width and +// orientation. A radius extension cannot guarantee this seam on curved roads. +function exteriorSidewalkSeam(edge, graph, helpers) { + const section = graph.sections.find((item) => item.carriagewayKey === edge.carriagewayKey); + if ( + !section || + section.ringStatus !== 'supported' || + section.transitionStatus !== 'supported' || + section.exclusionStatus !== 'clear' + ) + return null; + const target = edge.side === 90 ? section.outer.right : section.outer.left; + for (const feature of helpers.exteriorSidewalks || []) { + const ids = String(feature.properties.directional_road_ids || '').split(','); + if (!edge.roadIds.some((id) => ids.includes(String(id)))) continue; + const ring = feature.geometry.coordinates[0]; + const half = (ring.length - 1) / 2; + for (const [curb, outer] of [ + [ring[0], ring[ring.length - 2]], + [ring[half - 1], ring[half]], + ]) { + if (helpers.distanceMeters(curb, target) <= 0.01) return { curb, outer }; + } } - return points; + return null; } // Offset each vertex along the polyline normal that increases distance from the @@ -858,6 +866,9 @@ function cornerEdgeAt(arm, radius, bisector, center, helpers) { return [90, -90].map((side) => ({ point: helpers.offsetCoordinate(point, member.heading + side, halfWidth), heading: member.heading, + carriagewayKey: member.key, + roadIds: member.approach.roadIds, + side, })); }); return ( diff --git a/src/compile/native-road.js b/src/compile/native-road.js index 0bc9880..35cd8b2 100644 --- a/src/compile/native-road.js +++ b/src/compile/native-road.js @@ -781,6 +781,17 @@ function compileGeometry(model, overrides = { overrides: [] }, options = {}) { }); } } + const surfacedRoadIds = new Set( + features.flatMap((feature) => String(feature.properties.directional_road_ids || '').split(',')).filter(Boolean), + ); + const sidewalks = compileSidewalkSurfaces( + model, + diagnostics, + junctionPlans, + options, + complexApproachBoundaries, + surfacedRoadIds, + ); for (const cluster of activeClusters) { if (cluster.template !== 'complex-junction-v1') continue; const generated = buildComplexJunctionGeometry(model, cluster, { @@ -794,6 +805,7 @@ function compileGeometry(model, overrides = { overrides: [] }, options = {}) { headingVector, circleRing, boundaryGraph: complexApproachBoundaries.get(cluster.id)?.boundaryGraph, + exteriorSidewalks: sidewalks, clusterCornerRadii: options.directEdit?.clusterCorners?.get(String(cluster.id)), clusterCrossingControls: options.directEdit?.clusterCrossings?.get(String(cluster.id)), }); @@ -813,6 +825,7 @@ function compileGeometry(model, overrides = { overrides: [] }, options = {}) { stopLines: generatedComplexStopLines, }, complexApproachBoundaries, + surfacedRoadIds, ); const edgeLines = options.edgeLines === false @@ -837,13 +850,13 @@ function compileGeometry(model, overrides = { overrides: [] }, options = {}) { diagnostics, options, complexApproachBoundaries, + surfacedRoadIds, ); 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, complexApproachBoundaries); // Complex crosswalks are generated from native approach tangents; do not // synthesize side strips that can be mistaken for crosswalks. sidewalks.push(...generatedComplexSidewalks); @@ -1154,7 +1167,7 @@ function compileEdgeLines( .filter(Boolean), ); for (const road of model.roads) { - if (surfaced.size && !surfaced.has(road.id)) continue; + if (!surfaced.has(road.id)) continue; // The road surface is one polygon per segment, centred on this centerline // and spanning the sum of both directions. Deriving the edge from a single // direction's width puts it half a carriageway inside the asphalt. @@ -1234,6 +1247,7 @@ function compileCenterLines( diagnostics, options = {}, complexBoundaries = new Map(), + surfacedRoadIds = null, ) { const features = []; const controlFeatures = [...controls.crosswalks, ...controls.stopLines]; @@ -1247,6 +1261,7 @@ function compileCenterLines( const backward = roads.find((road) => road.direction === 'backward'); if (roads.length !== 2 || !forward || !backward || forward.highway === 'service' || backward.highway === 'service') continue; + if (surfacedRoadIds && !surfacedRoadIds.has(forward.id)) continue; const cluster = clusterForRoad(forward, options) || clusterForRoad(backward, options); const internalCluster = cluster && roadInternalToCluster(forward, cluster); const line = cluster @@ -1907,7 +1922,14 @@ function ringsOverlap(first, second) { ); } -function compileSidewalkSurfaces(model, diagnostics, junctionPlans, options = {}, complexBoundaries = new Map()) { +function compileSidewalkSurfaces( + model, + diagnostics, + junctionPlans, + options = {}, + complexBoundaries = new Map(), + surfacedRoadIds = null, +) { const features = []; const byWay = new Map(); for (const road of model.roads) { @@ -1917,6 +1939,7 @@ function compileSidewalkSurfaces(model, diagnostics, junctionPlans, options = {} } for (const [wayKey, directions] of byWay) { const forward = directions.find((road) => road.direction === 'forward') || directions[0]; + if (surfacedRoadIds && !surfacedRoadIds.has(forward.id)) continue; const backward = directions.find((road) => road.id !== forward.id); const totalWidth = directions.reduce((sum, road) => sum + road.widthMeters, 0); const sides = [ @@ -2182,6 +2205,7 @@ function compileLaneCenterlines( options = {}, complexControls = {}, complexBoundaries = new Map(), + surfacedRoadIds = null, ) { const features = []; const controlFeatures = [...(complexControls.crosswalks || []), ...(complexControls.stopLines || [])]; @@ -2243,6 +2267,9 @@ function compileLaneCenterlines( } const lane = { id: laneId(road.id, index + 1), roadId: road.id, index: index + 1, coordinates }; lanes.push(lane); + // Retain internal lanes for movement solving, but publish markings only + // where the external road surface was actually emitted. + if (surfacedRoadIds && !surfacedRoadIds.has(road.id)) continue; // Only the published geometry stops at the crossing. `coordinates` stays // whole because connectors are derived from it; a lane that ends at the // stop bar would otherwise break every turn path through the junction. diff --git a/test/index.js b/test/index.js index 5104000..1542d94 100644 --- a/test/index.js +++ b/test/index.js @@ -268,6 +268,110 @@ assert.equal( false, 'an unsupported transition must not publish floating lanes', ); +// External attachments must follow actual pavement publication, even when a +// short source road is swallowed by a cluster. Internal solver lanes remain private. +const surfacedIds = new Set( + exteriorBaseline.roadSurface.features + .flatMap((feature) => String(feature.properties.directional_road_ids || '').split(',')) + .filter(Boolean), +); +assert.ok( + complexModel.roads.some((road) => !surfacedIds.has(road.id)), + 'fixture exercises omitted roads', +); +for (const name of ['edgeLines', 'centerLines', 'laneCenterlines', 'laneSeparators', 'directionArrows', 'turnArrows']) { + for (const feature of exteriorBaseline[name].features) { + if (feature.properties.cluster_preview || String(feature.properties.native_id).startsWith('complex-')) continue; + assert.ok( + surfacedIds.has(feature.properties.road_id), + `${name} must not publish an attachment without external pavement`, + ); + } +} +const sidewalkModel = { + ...complexModel, + roads: complexModel.roads.map((road) => ({ ...road, sidewalkLeft: true, sidewalkRight: true })), +}; +const sidewalkGeometry = compiler.nativeRoad.compileGeometry( + sidewalkModel, + { overrides: [] }, + { junctionTemplates: boundaryEditOptions.junctionTemplates }, +); +const strips = sidewalkGeometry.sidewalkSurface.features.filter( + (feature) => feature.properties.provenance === 'native-road-sidewalk/v1', +); +for (const strip of strips) + assert.ok( + String(strip.properties.directional_road_ids) + .split(',') + .every((id) => surfacedIds.has(id)), + ); +const corners = sidewalkGeometry.sidewalkSurface.features.filter( + (feature) => feature.properties.kind === 'complex-sidewalk-corner', +); +assert.ok(corners.length > 0, 'fixture must exercise joined corner sidewalks'); +for (const corner of corners) { + const ring = corner.geometry.coordinates[0]; + const half = (ring.length - 1) / 2; + for (const [curb, outer] of [ + [ring[0], ring[ring.length - 2]], + [ring[half - 1], ring[half]], + ]) { + assert.ok( + strips.some((strip) => { + const points = strip.geometry.coordinates[0]; + const middle = (points.length - 1) / 2; + return [ + [points[0], points[points.length - 2]], + [points[middle - 1], points[middle]], + ].some(([first, second]) => JSON.stringify([first, second]) === JSON.stringify([curb, outer])); + }), + 'both ends of each corner must share the complete published sidewalk seam', + ); + } +} +// Empty pavement is a real result, not a signal to disable attachment checks. +const shortPoints = [ + [1, -1, 0], + [2, 1, 0], + [3, -8, 8], + [4, -8, -8], + [5, 8, 8], + [6, 8, -8], +]; +const shortWays = [ + [1, 2], + [1, 3], + [1, 4], + [2, 5], + [2, 6], +]; +const shortXml = `${shortPoints.map(([id, x, y]) => ``).join('')}${shortWays.map(([from, to], index) => ``).join('')}`; +const shortModel = compiler.nativeRoad.compileRoadModel(shortXml, { overrides: [] }); +const shortGeometry = compiler.nativeRoad.compileGeometry( + shortModel, + { overrides: [] }, + { + junctionTemplates: { + enabled: true, + clusters: [{ id: 'short', nodeIds: ['1', '2'], template: 'complex-junction-v1', coreRadiusMeters: 24 }], + }, + }, +); +assert.equal( + shortGeometry.roadSurface.features.some((feature) => feature.properties.directional_road_ids), + false, +); +for (const name of [ + 'edgeLines', + 'centerLines', + 'laneCenterlines', + 'laneSeparators', + 'directionArrows', + 'turnArrows', + 'sidewalkSurface', +]) + assert.equal(shortGeometry[name].features.length, 0, `${name} must remain empty when all source roads are swallowed`); const complexFeatures = Object.values(complexGeometry).flatMap((layer) => layer?.features || []); assert.equal( complexFeatures.some((feature) => feature.properties?.kind === 'complex-corner-island'),