feat: solve complete junction island contacts

This commit is contained in:
que01 committed 2026-09-14 08:59:32 +08:00
1 parent 7a7912f302
commit 1b91e280bc
5 files changed
+164 -7

No files matched your search

+44 -5
View File
@@ -10,6 +10,7 @@ const {
orthogonalCrossingHeadings,
stripeLayout,
crossingArcSupport,
solveIslandContacts,
} = require('./junction-facility-composition');
const { complexJunctionMetrics: calculateComplexJunctionMetrics } = require('./junction-metrics');
const { convertGeoJson, boundsOf } = require('../reference/gaode');
@@ -483,8 +484,30 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
second.crosswalkFrame.heading,
second.crosswalkFrame.groupDepth * 2,
);
const islandRing = buildIslandFromCrossingEdges(crossingCorner, outerFirst, outerSecond);
const publishedIslandRing = islandRing;
const lonScale = 111320 * Math.cos((crossingCorner[1] * Math.PI) / 180);
const toLocal = (point) => [(point[0] - crossingCorner[0]) * lonScale, (point[1] - crossingCorner[1]) * 111320];
const fromLocal = (point) => [crossingCorner[0] + point[0] / lonScale, crossingCorner[1] + point[1] / 111320];
const contactSolution = solveIslandContacts(
[[0, 0], toLocal(outerFirst)],
[[0, 0], toLocal(outerSecond)],
COMPLEX_CROSSWALK_DEPTH_METERS,
{ maxLegLength: Math.max(outerRadius * 2, COMPLEX_CROSSWALK_DEPTH_METERS * 4) },
);
if (contactSolution.status !== 'solved') {
cornerDiagnostics.push(
helpers.diagnostic(
'warning',
`corner:${first.cornerKey}`,
[...nodeIds],
'complex-junction-island-infeasible',
contactSolution.reason,
crossingCorner,
),
);
continue;
}
const publishedIslandRing = contactSolution.ring.map(fromLocal);
const greenRing = contactSolution.greenRing.map(fromLocal);
if (!cornerOverlapsExclusion(publishedIslandRing, first.cornerKey, '安全岛'))
islands.push({
type: 'Feature',
@@ -496,9 +519,25 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
corner_key: first.cornerKey,
facility_evidence: 'inferred-complex-junction-corner',
provenance: 'native-road-complex-junction-inferred-island/v1',
island_leg_lengths_m: contactSolution.legLengths,
contact_points: greenRing.slice(0, -1),
},
geometry: { type: 'Polygon', coordinates: [publishedIslandRing] },
});
if (!cornerOverlapsExclusion(greenRing, first.cornerKey, '安全岛绿色接触区'))
islands.push({
type: 'Feature',
properties: {
native_id: `complex-corner-island-green:${cluster.id}:${index + 1}`,
object_key: `island:inferred/${first.cornerKey}`,
cluster_id: cluster.id,
corner_key: first.cornerKey,
kind: 'island-green',
island_key: `island:inferred/${first.cornerKey}`,
provenance: 'native-road-complex-junction-island-contact-hexagon/v1',
},
geometry: { type: 'Polygon', coordinates: [greenRing] },
});
if (
!first.members.every((item) => publishedCarriageways.has(item.key)) ||
@@ -508,14 +547,14 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
// Complex junctions carry a default diagonal crossing across each safe
// corner. Source crossing nodes refine provenance/inset values, but are
// not required for the default visual treatment.
const outerMidpoint = [(outerFirst[0] + outerSecond[0]) / 2, (outerFirst[1] + outerSecond[1]) / 2];
const outerMidpoint = fromLocal(contactSolution.thirdCenter);
const diagonalDepth = COMPLEX_CROSSWALK_DEPTH_METERS;
const diagonalHeading = bearing(outerFirst, outerSecond) - 90;
const outwardHeading = bearing(crossingCorner, outerMidpoint);
const theta = (outwardHeading * Math.PI) / 180;
const lonScale = 111320 * Math.cos((outerMidpoint[1] * Math.PI) / 180);
const diagonalLonScale = 111320 * Math.cos((outerMidpoint[1] * Math.PI) / 180);
const projectedIsland = publishedIslandRing.map((p) => {
const east = (p[0] - outerMidpoint[0]) * lonScale;
const east = (p[0] - outerMidpoint[0]) * diagonalLonScale;
const north = (p[1] - outerMidpoint[1]) * 111320;
return [east * Math.cos(theta) - north * Math.sin(theta), east * Math.sin(theta) + north * Math.cos(theta)];
});
@@ -104,3 +104,94 @@ function crossingArcSupport(ring, halfWidth) {
return candidates.length ? Math.max(...candidates) : null;
}
module.exports.crossingArcSupport = crossingArcSupport;
// Solve three complete landings together. All coordinates are local metres.
// Quadratic connectors round the supporting triangle outside the green hexagon;
// the three contact segments themselves are never shortened by rounding.
function solveIslandContacts(firstSegment, secondSegment, thirdWidth, options = {}) {
const fail = (reason) => ({ status: 'infeasible', reason });
const finitePoint = (p) => Array.isArray(p) && p.length === 2 && p.every(Number.isFinite);
if (
![firstSegment, secondSegment].every((s) => Array.isArray(s) && s.length === 2 && s.every(finitePoint)) ||
!Number.isFinite(thirdWidth) ||
thirdWidth <= 0
)
return fail('invalid-contact');
const sub = (a, b) => a.map((v, i) => v - b[i]);
const add = (a, b, scale = 1) => a.map((v, i) => v + scale * b[i]);
const cross = (a, b) => a[0] * b[1] - a[1] * b[0];
const dot = (a, b) => a[0] * b[0] + a[1] * b[1];
const length = (a) => Math.hypot(...a);
const unit = (a) => a.map((v) => v / length(a));
const d1 = sub(firstSegment[1], firstSegment[0]);
const d2 = sub(secondSegment[1], secondSegment[0]);
if (length(d1) < 1e-8 || length(d2) < 1e-8) return fail('degenerate-contact');
const determinant = cross(d1, d2);
if (Math.abs(determinant) / length(d1) / length(d2) < 1e-6) return fail('parallel-contact-lines');
const corner = add(firstSegment[0], d1, cross(sub(secondSegment[0], firstSegment[0]), d2) / determinant);
const ordered = (segment) => [...segment].sort((a, b) => length(sub(a, corner)) - length(sub(b, corner)));
const first = ordered(firstSegment),
second = ordered(secondSegment);
const u = unit(sub(first[1], corner)),
v = unit(sub(second[1], corner));
// A short edge may begin exactly at the supporting-line intersection; this
// is the intended right-angle landing and is still a complete contact.
const a = length(sub(first[1], corner)),
b = length(sub(second[1], corner));
const chord = sub(second[1], first[1]),
chordLength = length(chord);
const tangent = unit(chord);
// This scale leaves positive connectors on both sides even for unequal depths.
const scale = 1 + thirdWidth / chordLength;
const legLengths = [a * scale, b * scale];
if (
options.maxLegLength !== undefined &&
(!Number.isFinite(options.maxLegLength) || legLengths.some((value) => value > options.maxLegLength))
)
return fail('leg-length-exceeds-space');
const firstTip = add(corner, u, legLengths[0]),
secondTip = add(corner, v, legLengths[1]);
const thirdCenter = firstTip.map((value, i) => (value + secondTip[i]) / 2);
const thirdContact = [add(thirdCenter, tangent, -thirdWidth / 2), add(thirdCenter, tangent, thirdWidth / 2)];
let outward = [tangent[1], -tangent[0]];
if (dot(outward, sub(thirdCenter, corner)) < 0) outward = outward.map((value) => -value);
const contacts = [first, thirdContact, [second[1], second[0]]];
const vertices = contacts.flat();
const hexArea =
Math.abs(
vertices.reduce((sum, point, i) => {
const next = vertices[(i + 1) % 6];
return sum + point[0] * next[1] - next[0] * point[1];
}, 0),
) / 2;
if (hexArea <= 1e-8) return fail('degenerate-contact-hexagon');
const ring = [];
const controls = [firstTip, secondTip, corner];
const steps = 16;
contacts.forEach((contact, index) => {
ring.push(...contact);
const start = contact[1],
end = contacts[(index + 1) % 3][0],
control = controls[index];
for (let step = 1; step < steps; step++) {
const t = step / steps;
ring.push(
start.map((value, axis) => (1 - t) ** 2 * value + 2 * t * (1 - t) * control[axis] + t ** 2 * end[axis]),
);
}
});
ring.push(ring[0]);
return {
status: 'solved',
reason: null,
ring,
greenRing: [...vertices, vertices[0]],
contacts,
thirdContact,
thirdCenter,
outward,
thirdOutward: outward,
legLengths,
};
}
module.exports.solveIslandContacts = solveIslandContacts;