221 lines
10 KiB
JavaScript
221 lines
10 KiB
JavaScript
'use strict';
|
|
|
|
function buildPedestrianIsland({ key, ring, crossingKeys, provenance = 'inferred' }) {
|
|
if (!key || !Array.isArray(ring) || ring.length < 4) return null;
|
|
const adjacentCrossings = [...new Set(crossingKeys || [])];
|
|
if (adjacentCrossings.length !== 3) return null;
|
|
return { key, ring, adjacentCrossings, provenance };
|
|
}
|
|
|
|
function validateFacilityComposition(island, crossings) {
|
|
if (!island || island.adjacentCrossings.length !== 3) return { valid: false, reason: 'island-needs-three-crossings' };
|
|
const crossingMap = new Map((crossings || []).map((crossing) => [crossing.key, crossing]));
|
|
const linked = island.adjacentCrossings.map((key) => crossingMap.get(key));
|
|
if (linked.some((crossing) => !crossing)) return { valid: false, reason: 'crossing-reference-missing' };
|
|
if (linked.some((crossing) => !crossing.fromBoundary || !crossing.toBoundary))
|
|
return { valid: false, reason: 'crossing-boundary-missing' };
|
|
return { valid: true, reason: null };
|
|
}
|
|
|
|
// 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;
|
|
|
|
// Solve three complete landings together. All coordinates are local metres.
|
|
// Rounded supporting-triangle tips remain outside the green contact 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));
|
|
if (dot(sub(first[0], corner), u) <= 1e-8 || dot(sub(second[0], corner), v) <= 1e-8)
|
|
return fail('contact-crosses-corner');
|
|
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 baseLegLengths = [a * scale, b * scale];
|
|
const minimumLegLength = Number.isFinite(options.minLegLength) ? Math.max(0, options.minLegLength) : 0;
|
|
const legScale = Math.max(1, minimumLegLength / Math.max(...baseLegLengths));
|
|
const legLengths = baseLegLengths.map((value) => value * legScale);
|
|
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 tipCenter = firstTip.map((value, i) => (value + secondTip[i]) / 2);
|
|
// Move the landing toward the corner, leaving the tips on the supporting
|
|
// triangle. Bound the inset by the spare leg length so contacts remain inside.
|
|
const inset = Math.min(0.12, 0.35 * Math.min(1 - a / legLengths[0], 1 - b / legLengths[1]));
|
|
const thirdCenter = add(tipCenter, sub(corner, tipCenter), inset);
|
|
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();
|
|
if (vertices.some((p, i) => vertices.slice(i + 1).some((q) => length(sub(p, q)) < 1e-8)))
|
|
return fail('duplicate-contact-endpoints');
|
|
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');
|
|
// Keep the two complete straight legs. Only their unused ends are rounded;
|
|
// the former six-endpoint outline discarded all three triangle vertices.
|
|
const radius = Math.min(0.45, length(sub(first[0], corner)) / 3, length(sub(second[0], corner)) / 3);
|
|
const firstTrim = Math.min(radius, (legLengths[0] - a) / 3);
|
|
const secondTrim = Math.min(radius, (legLengths[1] - b) / 3);
|
|
const nearFirst = add(corner, u, radius);
|
|
const nearSecond = add(corner, v, radius);
|
|
const tipFirst = add(firstTip, u, -firstTrim);
|
|
const tipSecond = add(secondTip, v, -secondTrim);
|
|
const ring = [nearFirst, ...first, tipFirst];
|
|
const cubic = (start, control1, control2, end) => {
|
|
for (let step = 1; step <= 16; step++) {
|
|
const t = step / 16, q = 1 - t;
|
|
ring.push(start.map((value, axis) =>
|
|
q ** 3 * value + 3 * q * q * t * control1[axis] +
|
|
3 * q * t * t * control2[axis] + t ** 3 * end[axis]));
|
|
}
|
|
};
|
|
// Tangents follow the leg at each tip and the full straight landing at the
|
|
// middle. Thus the long side bows inward without a kink at the third crossing.
|
|
const firstRun = length(sub(thirdContact[0], firstTip)) / 3;
|
|
const secondRun = length(sub(secondTip, thirdContact[1])) / 3;
|
|
cubic(tipFirst, firstTip, add(thirdContact[0], tangent, -firstRun), thirdContact[0]);
|
|
ring.push(thirdContact[1]);
|
|
cubic(thirdContact[1], add(thirdContact[1], tangent, secondRun), secondTip, tipSecond);
|
|
ring.push(second[1], second[0], nearSecond);
|
|
// A small right-angle roundover, independent of the contact-to-corner distance.
|
|
cubic(nearSecond, add(nearSecond, v, -radius * 0.55228475),
|
|
add(nearFirst, u, -radius * 0.55228475), nearFirst);
|
|
const greenRing = [...vertices, vertices[0]];
|
|
return {
|
|
status: 'solved',
|
|
reason: null,
|
|
ring,
|
|
greenRing,
|
|
contacts,
|
|
thirdContact,
|
|
thirdCenter,
|
|
outward,
|
|
thirdOutward: outward,
|
|
legLengths,
|
|
};
|
|
}
|
|
module.exports.solveIslandContacts = solveIslandContacts;
|