'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. // 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)); 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 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(); 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'); 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;