fix: align junction crossings and round pedestrian islands

This commit is contained in:
que01 committed 2026-09-11 17:35:32 +08:00
1 parent 76f81d024a
commit 7a7912f302
18 files changed
+491 -96

No files matched your search

+87 -1
View File
@@ -17,4 +17,90 @@ function validateFacilityComposition(island, crossings) {
return { valid: true, reason: null };
}
module.exports = { buildPedestrianIsland, validateFacilityComposition };
// The two legs are extensions of crossing short edges, in the same coordinate frame.
function buildIslandFromCrossingEdges(corner, firstEnd, secondEnd, segments = 12) {
const control = corner.map((value, axis) => (value + (firstEnd[axis] + secondEnd[axis]) / 2) / 2);
const mix = (a, b, t) => a.map((v, axis) => v * (1 - t) + b[axis] * t);
const evaluate = (t) =>
corner.map((_, axis) => (1 - t) ** 2 * firstEnd[axis] + 2 * t * (1 - t) * control[axis] + t * t * secondEnd[axis]);
const derivative = (t) =>
corner.map((_, axis) => 2 * (1 - t) * (control[axis] - firstEnd[axis]) + 2 * t * (secondEnd[axis] - control[axis]));
const cubic = (a, b, c, d) =>
Array.from({ length: 9 }, (_, i) => {
const t = i / 8,
u = 1 - t;
return a.map((v, axis) => u ** 3 * v + 3 * u * u * t * b[axis] + 3 * u * t * t * c[axis] + t ** 3 * d[axis]);
});
const trim = 0.2;
const left = mix(corner, firstEnd, 1 - trim),
right = mix(corner, secondEnd, 1 - trim);
const arcStart = evaluate(trim),
arcEnd = evaluate(1 - trim);
const leftCurve = cubic(
left,
mix(left, firstEnd, 0.65),
arcStart.map((v, i) => v - (derivative(trim)[i] * trim) / 3),
arcStart,
);
const rightCurve = cubic(
arcEnd,
arcEnd.map((v, i) => v + (derivative(1 - trim)[i] * trim) / 3),
mix(right, secondEnd, 0.65),
right,
);
const nearFirst = mix(corner, firstEnd, trim),
nearSecond = mix(corner, secondEnd, trim);
const nose = Array.from({ length: 9 }, (_, i) => {
const t = i / 8;
return corner.map((v, axis) => (1 - t) ** 2 * nearSecond[axis] + 2 * t * (1 - t) * v + t * t * nearFirst[axis]);
});
const middle = Array.from({ length: segments + 1 }, (_, i) => evaluate(trim + ((1 - 2 * trim) * i) / segments));
const points = [nearFirst, ...leftCurve, ...middle.slice(1), ...rightCurve.slice(1), ...nose.slice(0, -1)];
return [...points, points[0]];
}
module.exports = { buildPedestrianIsland, validateFacilityComposition, buildIslandFromCrossingEdges };
// Fit a common orthogonal frame only for a near-orthogonal four-arm crossing.
function orthogonalCrossingHeadings(headings) {
if (headings.length !== 4) return headings;
const radians = Math.PI / 180;
const axis =
Math.atan2(
headings.reduce((sum, h) => sum + Math.sin(h * radians * 4), 0),
headings.reduce((sum, h) => sum + Math.cos(h * radians * 4), 0),
) /
(4 * radians);
const fitted = headings.map((h) => axis + Math.round((h - axis) / 90) * 90);
if (fitted.some((h, i) => Math.abs(h - headings[i]) > 15)) return headings;
if (new Set(fitted.map((h) => ((h % 360) + 360) % 360)).size !== 4) return headings;
return fitted;
}
function stripeLayout(span, width = 0.42, gap = 0.4) {
const pitch = width + gap;
const count = Math.max(0, Math.floor((span + gap) / pitch));
const margin = (span - (count * width + Math.max(0, count - 1) * gap)) / 2;
return { width, gap, pitch, centers: Array.from({ length: count }, (_, i) => margin + width / 2 + i * pitch) };
}
module.exports.orthogonalCrossingHeadings = orthogonalCrossingHeadings;
module.exports.stripeLayout = stripeLayout;
// Support of the curved island inside the white bar's lateral footprint.
// Coordinates are local metres: x along the bar, y toward the outside.
function crossingArcSupport(ring, halfWidth) {
const candidates = [];
for (let i = 1; i < ring.length; i++) {
const a = ring[i - 1],
b = ring[i];
if (Math.abs(a[0]) <= halfWidth) candidates.push(a[1]);
for (const x of [-halfWidth, halfWidth]) {
if ((a[0] < x && b[0] > x) || (b[0] < x && a[0] > x)) {
const t = (x - a[0]) / (b[0] - a[0]);
candidates.push(a[1] + t * (b[1] - a[1]));
}
}
}
return candidates.length ? Math.max(...candidates) : null;
}
module.exports.crossingArcSupport = crossingArcSupport;