'use strict'; const assert = require('assert/strict'); const { buildPedestrianIsland, validateFacilityComposition } = require('../src/compile/junction-facility-composition'); const ring = [ [0, 0], [4, 0], [4, 2], [0, 2], [0, 0], ]; const crossings = [1, 2, 3].map((id) => ({ key: `crossing:${id}`, fromBoundary: `walk:${id}`, toBoundary: 'island:1', })); const island = buildPedestrianIsland({ key: 'island:1', ring, crossingKeys: crossings.map((item) => item.key) }); assert.equal(validateFacilityComposition(island, crossings).valid, true); assert.equal(buildPedestrianIsland({ key: 'bad', ring, crossingKeys: ['a', 'b'] }), null); assert.equal(validateFacilityComposition(island, crossings.slice(0, 2)).reason, 'crossing-reference-missing'); console.log('junction facility composition tests passed'); // User's sketch: a horizontal crossing to the right, a vertical crossing down, // and the island in the upper-left quadrant, bounded by doubled short edges. const { buildIslandFromCrossingEdges } = require('../src/compile/junction-facility-composition'); const clipping = require('polygon-clipping'); const sketchIsland = buildIslandFromCrossingEdges([0, 0], [0, 6.8], [-6.8, 0]); assert.deepEqual(sketchIsland[0], sketchIsland.at(-1)); assert.ok(sketchIsland.every(([x, y]) => x <= 0 && y >= 0)); const arcMiddle = sketchIsland[7]; assert.ok(arcMiddle[1] - arcMiddle[0] < 6.8, 'hypotenuse bends toward the right-angle vertex'); for (const crossing of [ [ [0, 0], [12, 0], [12, 3.4], [0, 3.4], [0, 0], ], [ [0, 0], [0, -12], [-3.4, -12], [-3.4, 0], [0, 0], ], ]) assert.deepEqual(clipping.intersection([sketchIsland], [crossing]), [], 'short-edge attachment has no area overlap'); const rotate = ([x, y]) => [-y + 20, x - 10]; const rotatedIsland = buildIslandFromCrossingEdges( ...[ [0, 0], [0, 6.8], [-6.8, 0], ].map(rotate), ); rotatedIsland.forEach((p, i) => assert.ok( Math.hypot(...p.map((v, j) => v - rotate(sketchIsland[i])[j])) < 1e-10, 'composition is independent of world orientation', ), ); const { stripeLayout, orthogonalCrossingHeadings } = require('../src/compile/junction-facility-composition'); for (const span of [3.4, 8.123, 31.7]) { const layout = stripeLayout(span); for (let i = 1; i < layout.centers.length; i++) assert.ok(Math.abs(layout.centers[i] - layout.centers[i - 1] - layout.width - layout.gap) < 1e-10); assert.ok(layout.centers[0] >= layout.width / 2); assert.ok(layout.centers.at(-1) + layout.width / 2 <= span + 1e-10); } const fitted = orthogonalCrossingHeadings([62.2, 150.4, 240.7, 332]); for (let i = 1; i < 4; i++) assert.ok(Math.abs(fitted[i] - fitted[i - 1] - 90) < 1e-10); assert.deepEqual(orthogonalCrossingHeadings([0, 120, 240]), [0, 120, 240]); assert.deepEqual(orthogonalCrossingHeadings([0, 40, 180, 270]), [0, 40, 180, 270]); const { crossingArcSupport, solveIslandContacts } = require('../src/compile/junction-facility-composition'); assert.ok(!sketchIsland.some(([x, y]) => x === 0 && y === 0), 'right-angle vertex is rounded'); assert.ok(!sketchIsland.some(([x, y]) => x === 0 && y === 6.8), 'first arc/leg vertex is rounded'); assert.ok(!sketchIsland.some(([x, y]) => x === -6.8 && y === 0), 'second arc/leg vertex is rounded'); // Curve support must consider the ends of a finite-width bar, not only its centre. const curved = [ [-3, 2], [-1, 0], [1, 0], [3, 2], [3, -2], [-3, -2], [-3, 2], ]; assert.equal(crossingArcSupport(curved, 2), 1); assert.deepEqual( clipping.intersection( [curved], [ [ [-2, 1], [2, 1], [2, 1.42], [-2, 1.42], [-2, 1], ], ], ), [], ); const solvedContacts = solveIslandContacts( [ [0, 0], [0, 6.8], ], [ [0, 0], [-6.8, 0], ], 3.4, { maxLegLength: 20 }, ); assert.equal(solvedContacts.status, 'solved'); assert.equal(solvedContacts.contacts.length, 3); assert.equal(solvedContacts.greenRing.length, 7); assert.equal(solvedContacts.legLengths.length, 2); assert.ok(solvedContacts.legLengths.every((value) => value > 6.8));