'use strict'; const { solveComplexPavementRegion, segmentCoveredByPolygon } = require('./junction-pavement-region'); const { intersection } = require('./junction-polygon-ops'); function hasPositiveArea(regions) { return regions.some((polygon) => polygon.some((ring, index) => { const area = Math.abs( ring .slice(1) .reduce( (sum, point, pointIndex) => sum + ring[pointIndex][0] * point[1] - point[0] * ring[pointIndex][1], 0, ) / 2, ); return index === 0 ? area > 1e-16 : false; }), ); } function buildCoreSurface({ cluster, helpers, center, nodeIds, arms, carriageways, shape, coreRadius, coreClosure }) { const coreRing = coreClosure.status === 'supported' ? coreClosure.ring : null; const cornerDiagnostics = []; let coreFeature = null; if (coreRing) coreFeature = { type: 'Feature', properties: { native_id: `complex-junction:${cluster.id}:core`, cluster_id: cluster.id, kind: 'complex-core', complex_part: 'core', center, radius_m: coreRadius, configured_radius_m: cluster.coreRadiusMeters, approach_count: arms.length, carriageway_count: carriageways.length, shape_kind: shape.kind, shape_label: shape.label, arm_count: arms.length, through_pair_count: shape.throughPairCount, approach_headings: arms.map((arm) => Math.round(arm.representative.heading * 10) / 10), boundary_refs: coreClosure.sharedEdgeRefs, closure_count: coreClosure.closureCount, shared_seam_error_m: coreClosure.maxSharedSeamErrorMeters, shared_seam_errors: coreClosure.sharedSeamErrors, connected_entry_count: coreClosure.connectedEntryCount, required_pavement_connected: coreClosure.requiredPavementConnected, core_ring_area: coreClosure.ringArea, core_ring_self_intersects: coreClosure.ringSelfIntersects, exclusion_hole_count: (coreClosure.holes || []).length, region_status: coreClosure.holes?.length ? 'supported-with-holes' : 'supported', provenance: 'native-road-complex-junction/v7-shared-entry-core', }, geometry: { type: 'Polygon', coordinates: [coreRing, ...(coreClosure.holes || [])] }, }; if (coreRing && coreClosure.maxSharedSeamErrorMeters > 0.01) { cornerDiagnostics.push( helpers.diagnostic( 'warning', `junction-cluster:${cluster.id}`, [...nodeIds], 'complex-junction-shared-seam-exceeds-tolerance', `核心区域与入口断面的最大接缝误差为 ${coreClosure.maxSharedSeamErrorMeters.toFixed(3)} 米,超过 1 厘米验收阈值。`, center, ), ); } else if (!coreRing) { cornerDiagnostics.push( helpers.diagnostic( 'warning', `junction-cluster:${cluster.id}`, [...nodeIds], 'complex-junction-core-closure-unsupported', `路口中心区域缺少可验证的闭合边(${coreClosure.reason}),已保留各进口道路面。`, center, ), ); } return { feature: coreFeature, diagnostics: cornerDiagnostics }; } function buildApproachSurfaceFeatures(boundaryParts, model, cluster) { const features = []; for (const { item, section } of boundaryParts) { const ring = section.ring; features.push({ type: 'Feature', properties: { native_id: `complex-junction:${cluster.id}:carriageway:${item.approach.segmentId}`, cluster_id: cluster.id, kind: 'complex-approach', object_key: item.key, carriageway_key: item.key, arm_key: item.armKey, complex_part: 'carriageway', heading_deg: item.heading, lane_count: item.approach.roadIds.reduce( (sum, roadId) => sum + (model.roads.find((road) => road.id === roadId)?.laneCount || 0), 0, ), width_m: item.approach.widthMeters, outer_width_m: section.outerWidthMeters, inner_width_m: section.innerWidthMeters, transition_length_m: Math.round(section.transitionMeters * 10) / 10, transition_status: section.transitionStatus, ring_status: section.ringStatus, exclusion_status: section.exclusionStatus, ...(section.exclusionRegionId ? { exclusion_region_id: section.exclusionRegionId } : {}), boundary_refs: section.edgeRefs, provenance: 'native-road-complex-junction/v6-shared-transition-sections', }, geometry: { type: 'Polygon', coordinates: [ring] }, }); } return features; } function appendPavementRegion( features, crossingRegions, cluster, center, nodeIds, helpers, exclusionRegions, boundaryParts, topology, ) { const cornerDiagnostics = []; // Publish a single canonical pavement region last. The individual approach, // core and corner polygons above remain structural inputs for editing and // diagnostics; they are no longer the visible authority at the junction. const basePavement = features.map((feature) => feature.geometry.coordinates[0]); const pavementRegion = solveComplexPavementRegion( basePavement, crossingRegions, exclusionRegions.map((region) => region.ring), ); const excludedIds = exclusionRegions.map((region) => String(region.id ?? '')); const missingCarriageways = []; if (exclusionRegions.length && pavementRegion.status === 'supported') { try { if (exclusionRegions.some((region) => hasPositiveArea(intersection(pavementRegion.regions, [region.ring])))) pavementRegion.status = 'exclusion-overlap'; for (const item of topology.carriageways) { const part = boundaryParts.find((part) => part.item.key === item.key); if ( !part || !['inner', 'outer'].every((edge) => segmentCoveredByPolygon(part.section[edge].left, part.section[edge].right, pavementRegion.regions[0]), ) ) missingCarriageways.push(item.key); } if (missingCarriageways.length) pavementRegion.status = 'required-seam-lost'; } catch (error) { pavementRegion.status = 'unsupported'; pavementRegion.reason = error.message; } } if (pavementRegion.status === 'supported') { for (const feature of features) feature.properties.pavement_input = true; features.push({ type: 'Feature', properties: { native_id: `complex-junction:${cluster.id}:pavement-region`, cluster_id: cluster.id, kind: 'complex-pavement-region', complex_part: 'pavement-region', center, region_status: 'supported', region_input_count: pavementRegion.inputCount, crossing_connector_count: 0, provenance: 'native-road-complex-junction-pavement-region/v1', }, geometry: { type: 'MultiPolygon', coordinates: pavementRegion.regions }, }); } else { const diagnostic = helpers.diagnostic( 'warning', `junction-cluster:${cluster.id}`, [...nodeIds], pavementRegion.status === 'unsupported' ? 'complex-junction-pavement-region-assembly-failed' : `complex-junction-pavement-region-${pavementRegion.status}`, pavementRegion.status === 'unsupported' ? '统一铺装区域无法可靠装配,保留结构输入面并拒绝发布统一铺装面。' : `统一铺装区域不满足连续通行约束(${pavementRegion.status}),保留结构输入面并拒绝发布统一铺装面。`, center, ); if (exclusionRegions.length) { diagnostic.exclusion_region_ids = excludedIds; diagnostic.carriageway_keys = missingCarriageways; } cornerDiagnostics.push(diagnostic); } return cornerDiagnostics; } function assemblePavementSurfaces({ model, cluster, helpers, center, nodeIds, topology, shape, coreRadius, coreClosure, boundaryParts, cornerFills, crossingRegions, }) { const { feature: coreFeature, diagnostics } = buildCoreSurface({ cluster, helpers, center, nodeIds, arms: topology.arms, carriageways: topology.carriageways, shape, coreRadius, coreClosure, }); const features = buildApproachSurfaceFeatures(boundaryParts, model, cluster); // Preserve the structural surface order before publishing canonical pavement. features.push(...cornerFills); if (coreFeature) features.push(coreFeature); diagnostics.push( ...appendPavementRegion( features, crossingRegions, cluster, center, nodeIds, helpers, model.exclusionRegions || [], boundaryParts, topology, ), ); return { features, diagnostics }; } module.exports = { assemblePavementSurfaces };