'use strict'; // Source-independent counterexamples for the JunctionBuildPlan contract. const assert = require('assert/strict'); const { resolveJunctionBuildPlan, stationAtDistanceFrom } = require('../src/compile/junction-build-plan'); const { buildJunctionBoundaryGraph, buildCoreClosure, ringsOverlap, } = require('../src/compile/junction-boundary-graph'); const { classifyJunctionShape } = require('../src/compile/complex-junction'); const metersPerDegree = 111320; const point = (east, north) => [east / metersPerDegree, north / metersPerDegree]; const square = (x, y, size = 2) => [ [x, y], [x + size, y], [x + size, y + size], [x, y + size], [x, y], ].map(([east, north]) => point(east, north)); for (const [other, expected] of [ [square(10, 0), false], [square(0, 10), false], [square(2, 0), true], [square(2, 2), true], [square(1, 1), true], [square(0.5, 0.5, 0.5), true], ]) { assert.equal(ringsOverlap(square(0, 0), other), expected); assert.equal(ringsOverlap(other, square(0, 0)), expected); assert.equal(ringsOverlap(square(0, 0).reverse(), other.slice().reverse()), expected); } function heading(line) { const [from, to] = line; return (Math.atan2(to[0] - from[0], to[1] - from[1]) * 180) / Math.PI; } function distance(first, second) { return Math.hypot((first[0] - second[0]) * metersPerDegree, (first[1] - second[1]) * metersPerDegree); } function length(line) { return line.slice(1).reduce((total, item, index) => total + distance(line[index], item), 0); } function pointAtStation(line, station) { let remaining = station; for (let index = 1; index < line.length; index += 1) { const segmentLength = distance(line[index - 1], line[index]); if (remaining <= segmentLength) { const ratio = remaining / segmentLength; return [ line[index - 1][0] + (line[index][0] - line[index - 1][0]) * ratio, line[index - 1][1] + (line[index][1] - line[index - 1][1]) * ratio, ]; } remaining -= segmentLength; } return line.at(-1); } function fixture(name, entries) { const nodeId = `${name}:node`; const approaches = entries.map((entry, index) => ({ segmentId: `${name}:segment:${index + 1}`, roadIds: [`${name}:road:${index + 1}`], widthMeters: entry.widthMeters, line: entry.line, })); return { model: { roads: entries.map((entry, index) => ({ id: `${name}:road:${index + 1}`, segmentId: `${name}:segment:${index + 1}`, sourceNodeIds: [nodeId, `${name}:outer:${index + 1}`], laneCount: 1, })), }, cluster: { id: name, nodeIds: [nodeId, `${name}:internal`] }, helpers: { junctionPlans: new Map([ [nodeId, { node: point(0, 0), approaches }], [`${name}:internal`, { node: point(1, 1), approaches: [] }], ]), headingAtEndpoint: heading, lineLengthMeters: length, distanceMeters: distance, }, }; } function topology(name, entries) { const input = fixture(name, entries); return { ...input, topology: resolveJunctionBuildPlan(input.model, input.cluster, input.helpers) }; } // A divided approach keeps independently addressable entry sections even when // both carriageways share an arm. const divided = topology('divided', [ { widthMeters: 4, line: [point(-5, 0), point(-5, 50)] }, { widthMeters: 5, line: [point(5, 0), point(5, 50)] }, { widthMeters: 7, line: [point(0, 0), point(50, 0)] }, { widthMeters: 7, line: [point(0, 0), point(0, -50)] }, ]); assert.equal(divided.topology.carriageways.length, 4); assert.equal(divided.topology.arms.length, 3); assert.equal(divided.topology.arms.find((arm) => arm.members.length === 2)?.members.length, 2); assert.equal(new Set(divided.topology.carriageways.map((item) => item.entrySection.carriagewayKey)).size, 4); assert.ok( divided.topology.carriageways.every( (item) => item.entrySection.boundaryEndpoints.near.left[0] !== item.entrySection.boundaryEndpoints.near.right[0], ), ); const dividedBoundaryGraph = buildJunctionBoundaryGraph( divided.topology, point(0, 0), { outerRadiusFor: () => 25, innerRadiusFor: () => 10 }, divided.helpers, ); assert.equal(dividedBoundaryGraph.status, 'entry-sections-only'); assert.equal(dividedBoundaryGraph.sections.length, divided.topology.carriageways.length); assert.equal(new Set(dividedBoundaryGraph.sections.map((section) => section.carriagewayKey)).size, 4); assert.equal(dividedBoundaryGraph.edges.length, dividedBoundaryGraph.sections.length * 4); assert.equal( buildCoreClosure(divided.topology, dividedBoundaryGraph, point(0, 0)).reason, 'parallel-carriageway-closure-unresolved', ); // A T junction has explicit, source-stable entry sections. Closure edges are a // later BoundaryGraph concern and must not be fabricated by topology parsing. const tee = topology('tee', [ { widthMeters: 8, line: [point(0, 0), point(0, 45)] }, { widthMeters: 4, line: [point(0, 0), point(45, 0)] }, { widthMeters: 4, line: [point(0, 0), point(-45, 0)] }, ]); assert.equal(tee.topology.arms.length, 3); assert.equal(classifyJunctionShape(tee.topology.arms, point(0, 0), tee.helpers).kind, 't-junction'); assert.ok(tee.topology.carriageways.every((item) => item.entrySection.feasibility === 'supported')); const teeBoundaryGraph = buildJunctionBoundaryGraph( tee.topology, point(0, 0), { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, tee.helpers, ); const teeCore = buildCoreClosure(tee.topology, teeBoundaryGraph, point(0, 0)); for (const [field, value] of [ ['positionOffsetMeters', 1], ['directionOffsetDegrees', 5], ['leftEdgeOffsetMeters', 0.5], ['rightEdgeOffsetMeters', 0.5], ]) { const edited = structuredClone(tee.topology); edited.carriageways[0].approach[field] = value; const result = buildJunctionBoundaryGraph( edited, point(0, 0), { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, tee.helpers, ); const before = teeBoundaryGraph.sections[0]; const after = result.sections[0]; assert.deepEqual(after.outer, before.outer, `${field} preserves external seam`); assert.notDeepEqual(after.inner, before.inner, `${field} changes actual geometry`); assert.equal(after.transitionStatus, 'supported'); if (field === 'leftEdgeOffsetMeters') assert.deepEqual(after.inner.right, before.inner.right); if (field === 'rightEdgeOffsetMeters') assert.deepEqual(after.inner.left, before.inner.left); assert.deepEqual(result.sections.slice(1), teeBoundaryGraph.sections.slice(1)); } assert.equal(teeCore.status, 'supported'); assert.equal(teeCore.sharedEdgeRefs.length, teeBoundaryGraph.sections.length); assert.ok(teeCore.ring.every((coordinate) => coordinate.every(Number.isFinite))); const safeHole = buildJunctionBoundaryGraph( tee.topology, point(0, 0), { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, tee.helpers, [{ id: 'island:hole', isHole: true, ring: [point(-2, -2), point(2, -2), point(2, 2), point(-2, 2), point(-2, -2)] }], ); const holedCore = buildCoreClosure(tee.topology, safeHole, point(0, 0)); assert.equal(holedCore.status, 'supported'); assert.equal(holedCore.holes.length, 1); const exclusionRing = [point(-1, -1), point(1, -1), point(1, 1), point(-1, 1), point(-1, -1)]; const teeWithIsland = buildJunctionBoundaryGraph( tee.topology, point(0, 0), { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, tee.helpers, [{ id: 'island:known', ring: exclusionRing }], ); assert.equal(buildCoreClosure(tee.topology, teeWithIsland, point(0, 0)).reason, 'known-exclusion-region'); const enclosingExclusion = [point(-30, -30), point(30, -30), point(30, 30), point(-30, 30), point(-30, -30)]; const teeInsideExclusion = buildJunctionBoundaryGraph( tee.topology, point(0, 0), { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, tee.helpers, [{ id: 'island:enclosing', ring: enclosingExclusion }], ); assert.equal(buildCoreClosure(tee.topology, teeInsideExclusion, point(0, 0)).reason, 'known-exclusion-region'); // Offset and unequal-width approaches preserve individual source sections and // local section tangents rather than a single arm envelope. const offsetUnequal = topology('offset-unequal', [ { widthMeters: 12, line: [point(-8, 0), point(-8, 48)] }, { widthMeters: 4, line: [point(0, 18), point(48, 18)] }, { widthMeters: 6, line: [point(9, 0), point(9, -28)] }, { widthMeters: 9, line: [point(0, -12), point(-48, -12)] }, ]); assert.equal(offsetUnequal.topology.arms.length, 4); assert.equal( classifyJunctionShape(offsetUnequal.topology.arms, point(0, 0), offsetUnequal.helpers).kind, 'offset-cross', ); assert.deepEqual( offsetUnequal.topology.carriageways.map((item) => item.entrySection.farStationMeters), offsetUnequal.topology.carriageways.map((item) => item.length), ); // A short curved approach reports bounded feasibility while retaining distinct // local tangents at each end of its source polyline. const curvedShort = topology('curved-short', [ { widthMeters: 6, line: [point(0, 0), point(0, 3), point(7, 7)] }, { widthMeters: 6, line: [point(0, 0), point(35, 0)] }, { widthMeters: 6, line: [point(0, 0), point(0, -35)] }, ]); assert.equal(curvedShort.topology.carriageways[0].heading, 0, 'current heading samples only the first segment'); assert.ok(curvedShort.topology.carriageways[0].length < 12, 'fixture is shorter than the minimum complex core radius'); assert.equal(curvedShort.topology.carriageways[0].entrySection.feasibility, 'short-approach'); assert.equal(curvedShort.topology.carriageways[0].entrySection.reason, 'entry-section-too-short'); assert.notEqual( curvedShort.topology.carriageways[0].entrySection.nearTangent, curvedShort.topology.carriageways[0].entrySection.farTangent, ); const curvedBoundaryGraph = buildJunctionBoundaryGraph( curvedShort.topology, point(0, 0), { outerRadiusFor: () => 8, innerRadiusFor: () => 2 }, curvedShort.helpers, ); const curvedSection = curvedBoundaryGraph.sections.find( (section) => section.carriagewayKey === curvedShort.topology.carriageways[0].key, ); assert.notEqual(curvedSection.outer.tangent, curvedSection.inner.tangent); const curvedBoundaryLine = [point(0, 0), point(0, 5), point(10, 10)]; const curvedBoundaryStation = stationAtDistanceFrom(curvedBoundaryLine, point(0, 0), 9, { distanceMeters: distance, }); assert.ok(Math.abs(distance(pointAtStation(curvedBoundaryLine, curvedBoundaryStation), point(0, 0)) - 9) < 0.01); // Six-arm clusters remain within the supported matrix and keep semantic keys // stable when the source geometry is rotated. Geometry may change with the // transform, but identity and arm count must not depend on input orientation. const sixArmEntries = [0, 60, 120, 180, 240, 300].map((angle) => ({ widthMeters: 6, line: [point(0, 0), point(Math.sin((angle * Math.PI) / 180) * 45, Math.cos((angle * Math.PI) / 180) * 45)], })); const sixArm = topology('six-arm', sixArmEntries); assert.equal(sixArm.topology.arms.length, 6); assert.equal(classifyJunctionShape(sixArm.topology.arms, point(0, 0), sixArm.helpers).kind, 'multi-arm'); const sixArmGraph = buildJunctionBoundaryGraph( sixArm.topology, point(0, 0), { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, sixArm.helpers, ); assert.equal(buildCoreClosure(sixArm.topology, sixArmGraph, point(0, 0)).status, 'supported'); const rotate = (coordinate, degrees) => { const radians = (degrees * Math.PI) / 180; return point( coordinate[0] * metersPerDegree * Math.cos(radians) - coordinate[1] * metersPerDegree * Math.sin(radians), coordinate[0] * metersPerDegree * Math.sin(radians) + coordinate[1] * metersPerDegree * Math.cos(radians), ); }; const rotatedSixArmEntries = sixArmEntries.map((entry) => ({ ...entry, line: entry.line.map((coordinate) => rotate(coordinate, 37)), })); const rotatedSixArm = fixture('six-arm', rotatedSixArmEntries); const rotatedTopology = resolveJunctionBuildPlan(rotatedSixArm.model, rotatedSixArm.cluster, rotatedSixArm.helpers); assert.deepEqual(rotatedTopology.arms.map((arm) => arm.key).sort(), sixArm.topology.arms.map((arm) => arm.key).sort()); assert.equal(rotatedTopology.arms.length, sixArm.topology.arms.length); console.log('junction build-plan counterexample tests passed'); function ringArea(ring) { return Math.abs( ring .slice(1) .reduce((area, coordinate, index) => area + ring[index][0] * coordinate[1] - coordinate[0] * ring[index][1], 0) / 2, ); } function ringHasSelfIntersection(ring) { const cross = (a, b, c) => (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); const intersects = (a, b, c, d) => { const abC = cross(a, b, c); const abD = cross(a, b, d); const cdA = cross(c, d, a); const cdB = cross(c, d, b); return ((abC > 0 && abD < 0) || (abC < 0 && abD > 0)) && ((cdA > 0 && cdB < 0) || (cdA < 0 && cdB > 0)); }; for (let first = 0; first < ring.length - 1; first += 1) { for (let second = first + 2; second < ring.length - 1; second += 1) { if (first === 0 && second === ring.length - 2) continue; if (intersects(ring[first], ring[first + 1], ring[second], ring[second + 1])) return true; } } return false; } function assertCoreGeometry(topologyValue, graphValue, centerValue) { const closure = buildCoreClosure(topologyValue, graphValue, centerValue); assert.equal(closure.status, 'supported'); assert.deepEqual(closure.ring[0], closure.ring.at(-1), 'core ring must be explicitly closed'); assert.ok(closure.ring.slice(0, -1).every((coordinate) => coordinate.every(Number.isFinite))); assert.ok(ringArea(closure.ring) > 0, 'core ring must have positive area'); assert.equal(ringHasSelfIntersection(closure.ring), false, 'core ring must not self-intersect'); for (const section of graphValue.sections) { const samePoint = (first, second) => first?.[0] === second?.[0] && first?.[1] === second?.[1]; const hasSeam = closure.ring.slice(0, -1).some((coordinate, index) => { const next = closure.ring[index + 1]; return ( (samePoint(coordinate, section.inner.left) && samePoint(next, section.inner.right)) || (samePoint(coordinate, section.inner.right) && samePoint(next, section.inner.left)) ); }); assert.equal(hasSeam, true, `inner seam must remain a contiguous ring edge for ${section.carriagewayKey}`); } } assertCoreGeometry(tee.topology, teeBoundaryGraph, point(0, 0)); assertCoreGeometry(sixArm.topology, sixArmGraph, point(0, 0)); const fiveArm = topology( 'five-arm', [0, 72, 144, 216, 288].map((angle) => ({ widthMeters: 5, line: [point(0, 0), point(Math.sin((angle * Math.PI) / 180) * 45, Math.cos((angle * Math.PI) / 180) * 45)], })), ); const fiveArmGraph = buildJunctionBoundaryGraph( fiveArm.topology, point(0, 0), { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, fiveArm.helpers, ); assertCoreGeometry(fiveArm.topology, fiveArmGraph, point(0, 0)); // Reordering source approaches must not change semantic identity. The model // keeps the same source IDs; only the resolver input order is changed. const reorderedApproaches = [...sixArm.helpers.junctionPlans.values()][0].approaches.slice().reverse(); const reorderedHelpers = { ...sixArm.helpers, junctionPlans: new Map([ ['six-arm:node', { node: point(0, 0), approaches: reorderedApproaches }], ['six-arm:internal', { node: point(1, 1), approaches: [] }], ]), }; const reorderedTopology = resolveJunctionBuildPlan(sixArm.model, sixArm.cluster, reorderedHelpers); assert.deepEqual( reorderedTopology.carriageways.map((item) => item.key).sort(), sixArm.topology.carriageways.map((item) => item.key).sort(), ); assert.deepEqual( reorderedTopology.arms.map((arm) => arm.key).sort(), sixArm.topology.arms.map((arm) => arm.key).sort(), ); // A collapsed inner seam is a geometric failure even if the remaining ring // still encloses a positive area. The closure must reject zero-length edges. const collapsedGraph = JSON.parse(JSON.stringify(teeBoundaryGraph)); const collapsedSection = collapsedGraph.sections[0]; collapsedSection.inner.right = collapsedSection.inner.left; assert.equal(buildCoreClosure(tee.topology, collapsedGraph, point(0, 0)).reason, 'core-closure-invalid'); // Width corrections are represented by one shared trapezoid: the outer seam // remains source width, the inner seam carries the edited width, and the // transition distance is explicit for downstream validators and editors. const tapered = topology('tapered', [ { widthMeters: 10, line: [point(0, 0), point(0, 50)] }, { widthMeters: 4, line: [point(0, 0), point(50, 0)] }, { widthMeters: 4, line: [point(0, 0), point(-50, 0)] }, ]); tapered.topology.carriageways[0].approach.sourceWidthMeters = 10; tapered.topology.carriageways[0].approach.widthMeters = 6; const taperedGraph = buildJunctionBoundaryGraph( tapered.topology, point(0, 0), { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, tapered.helpers, ); const taperedSection = taperedGraph.sections.find( (section) => section.carriagewayKey === tapered.topology.carriageways[0].key, ); assert.equal(taperedSection.outerWidthMeters, 10); assert.equal(taperedSection.innerWidthMeters, 6); assert.ok(taperedSection.transitionMeters > 0); assert.equal(taperedSection.transitionStatus, 'supported'); tapered.topology.carriageways[0].approach.transitionLengthMeters = 100; const clippedTransitionGraph = buildJunctionBoundaryGraph( tapered.topology, point(0, 0), { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, tapered.helpers, ); const clippedTransition = clippedTransitionGraph.sections.find( (section) => section.carriagewayKey === tapered.topology.carriageways[0].key, ); assert.equal(clippedTransition.inner.stationMeters, 0, 'overlong transitions must clamp to the source start'); assert.equal(clippedTransition.transitionStatus, 'insufficient-source-length'); const approachExclusion = [point(-6, 8), point(6, 8), point(6, 24), point(-6, 24), point(-6, 8)]; const graphWithApproachExclusion = buildJunctionBoundaryGraph( tee.topology, point(0, 0), { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, tee.helpers, [{ id: 'island:approach', ring: approachExclusion }], ); const excludedSection = graphWithApproachExclusion.sections.find( (section) => section.outer.point[1] > section.outer.point[1] - 1, ); assert.equal(excludedSection.exclusionStatus, 'overlaps-known-region'); assert.equal(excludedSection.exclusionRegionId, 'island:approach'); const offsetUnequalGraph = buildJunctionBoundaryGraph( offsetUnequal.topology, point(0, 0), { outerRadiusFor: () => 24, innerRadiusFor: () => 8 }, offsetUnequal.helpers, ); assertCoreGeometry(offsetUnequal.topology, offsetUnequalGraph, point(0, 0)); const wrappedRotationEntries = sixArmEntries.map((entry) => ({ ...entry, line: entry.line.map((coordinate) => rotate(coordinate, 350)), })); const wrappedRotation = fixture('six-arm', wrappedRotationEntries); const wrappedTopology = resolveJunctionBuildPlan( wrappedRotation.model, wrappedRotation.cluster, wrappedRotation.helpers, ); assert.deepEqual( wrappedTopology.arms.map((arm) => arm.key).sort(), sixArm.topology.arms.map((arm) => arm.key).sort(), 'arm identity must remain stable across the -180/180 heading wrap', ); assert.equal(wrappedTopology.arms.length, sixArm.topology.arms.length); const translate = (coordinate, eastMeters, northMeters) => point(coordinate[0] * metersPerDegree + eastMeters, coordinate[1] * metersPerDegree + northMeters); const translatedEntries = sixArmEntries.map((entry) => ({ ...entry, line: entry.line.map((coordinate) => translate(coordinate, 120, -75)), })); const translatedBase = fixture('six-arm', translatedEntries); translatedBase.helpers.junctionPlans.set('six-arm:node', { node: translate(point(0, 0), 120, -75), approaches: translatedBase.helpers.junctionPlans.get('six-arm:node').approaches, }); translatedBase.helpers.junctionPlans.set('six-arm:internal', { node: translate(point(1, 1), 120, -75), approaches: [], }); const translatedTopology = resolveJunctionBuildPlan( translatedBase.model, translatedBase.cluster, translatedBase.helpers, ); assert.deepEqual( translatedTopology.arms.map((arm) => arm.key).sort(), sixArm.topology.arms.map((arm) => arm.key).sort(), 'translation must not change arm identity', ); assert.deepEqual( translatedTopology.carriageways.map((item) => item.entrySection.feasibility), sixArm.topology.carriageways.map((item) => item.entrySection.feasibility), ); const tooShort = topology('too-short-transition', [ { widthMeters: 6, line: [point(0, 0), point(0, 7)] }, { widthMeters: 6, line: [point(0, 0), point(35, 0)] }, { widthMeters: 6, line: [point(0, 0), point(-35, 0)] }, ]); const tooShortGraph = buildJunctionBoundaryGraph( tooShort.topology, point(0, 0), { outerRadiusFor: () => 24, innerRadiusFor: () => 10 }, tooShort.helpers, ); const tooShortSection = tooShortGraph.sections.find( (section) => section.carriagewayKey === tooShort.topology.carriageways[0].key, ); assert.equal(tooShortSection.transitionStatus, 'insufficient-source-length'); assert.equal(tooShortSection.transitionMeters, 0); // Foreign junction plans outside the selected cluster must not leak into its // topology or change stable source identities. const isolatedHelpers = { ...sixArm.helpers, junctionPlans: new Map([ ...sixArm.helpers.junctionPlans, [ 'foreign:node', { node: point(500, 500), approaches: [ { segmentId: 'foreign:segment', roadIds: ['foreign:road'], widthMeters: 20, line: [point(500, 500), point(560, 500)], }, ], }, ], ]), }; const isolatedTopology = resolveJunctionBuildPlan(sixArm.model, sixArm.cluster, isolatedHelpers); assert.deepEqual( isolatedTopology.carriageways.map((item) => item.key).sort(), sixArm.topology.carriageways.map((item) => item.key).sort(), 'foreign junction plans must not contaminate selected cluster topology', ); const deterministicTopology = resolveJunctionBuildPlan(sixArm.model, sixArm.cluster, sixArm.helpers); const deterministicGraph = buildJunctionBoundaryGraph( deterministicTopology, point(0, 0), { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, sixArm.helpers, ); assert.deepEqual(deterministicTopology, sixArm.topology, 'repeated topology resolution must be deterministic'); assert.deepEqual(deterministicGraph, sixArmGraph, 'repeated boundary graph construction must be deterministic'); const skewThreeArm = topology( 'skew-three-arm', [20, 145, 265].map((angle, index) => ({ widthMeters: 4 + index, line: [point(0, 0), point(Math.sin((angle * Math.PI) / 180) * 42, Math.cos((angle * Math.PI) / 180) * 42)], })), ); assert.equal(skewThreeArm.topology.arms.length, 3); const skewThreeArmGraph = buildJunctionBoundaryGraph( skewThreeArm.topology, point(0, 0), { outerRadiusFor: () => 28, innerRadiusFor: () => 9 }, skewThreeArm.helpers, ); assertCoreGeometry(skewThreeArm.topology, skewThreeArmGraph, point(0, 0)); const skewFourArm = topology( 'skew-four-arm', [15, 100, 205, 300].map((angle, index) => ({ widthMeters: [3.5, 6.5, 4.25, 8][index], line: [point(0, 0), point(Math.sin((angle * Math.PI) / 180) * 48, Math.cos((angle * Math.PI) / 180) * 48)], })), ); assert.equal(skewFourArm.topology.arms.length, 4); const skewFourArmGraph = buildJunctionBoundaryGraph( skewFourArm.topology, point(0, 0), { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, skewFourArm.helpers, ); assertCoreGeometry(skewFourArm.topology, skewFourArmGraph, point(0, 0)); const irregularFiveArm = topology( 'irregular-five-arm', [5, 78, 151, 238, 319].map((angle, index) => ({ widthMeters: [4, 7, 5.5, 3.75, 8.25][index], line: [point(0, 0), point(Math.sin((angle * Math.PI) / 180) * 46, Math.cos((angle * Math.PI) / 180) * 46)], })), ); assert.equal(irregularFiveArm.topology.arms.length, 5); const irregularFiveArmGraph = buildJunctionBoundaryGraph( irregularFiveArm.topology, point(0, 0), { outerRadiusFor: () => 29, innerRadiusFor: () => 10 }, irregularFiveArm.helpers, ); assertCoreGeometry(irregularFiveArm.topology, irregularFiveArmGraph, point(0, 0)); const irregularSixArm = topology( 'irregular-six-arm', [2, 61, 119, 184, 247, 311].map((angle, index) => ({ widthMeters: [4, 6, 5, 7.5, 3.5, 8][index], line: [point(0, 0), point(Math.sin((angle * Math.PI) / 180) * 47, Math.cos((angle * Math.PI) / 180) * 47)], })), ); assert.equal(irregularSixArm.topology.arms.length, 6); const irregularSixArmGraph = buildJunctionBoundaryGraph( irregularSixArm.topology, point(0, 0), { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, irregularSixArm.helpers, ); assertCoreGeometry(irregularSixArm.topology, irregularSixArmGraph, point(0, 0)); const touchingExclusion = [point(-3, 10), point(3, 10), point(3, 16), point(-3, 16), point(-3, 10)]; const touchingGraph = buildJunctionBoundaryGraph( tee.topology, point(0, 0), { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, tee.helpers, [{ id: 'island:touching', ring: touchingExclusion }], ); assert.ok( touchingGraph.sections.some((section) => section.exclusionStatus === 'overlaps-known-region'), 'a boundary-touching exclusion must remain protected', ); assert.deepEqual( teeBoundaryGraph.sections[0].ring, [ teeBoundaryGraph.sections[0].outer.right, teeBoundaryGraph.sections[0].inner.right, teeBoundaryGraph.sections[0].inner.left, teeBoundaryGraph.sections[0].outer.left, teeBoundaryGraph.sections[0].outer.right, ], 'BoundaryGraph must expose the canonical entry ring consumed by compilation', ); assert.ok(teeBoundaryGraph.sections.every((section) => section.ringStatus === 'supported')); const zeroWidth = topology('zero-width-entry', [ { widthMeters: 0, line: [point(0, 0), point(0, 40)] }, { widthMeters: 5, line: [point(0, 0), point(40, 0)] }, { widthMeters: 5, line: [point(0, 0), point(-40, 0)] }, ]); const zeroWidthGraph = buildJunctionBoundaryGraph( zeroWidth.topology, point(0, 0), { outerRadiusFor: () => 24, innerRadiusFor: () => 10 }, zeroWidth.helpers, ); assert.equal(zeroWidthGraph.sections[0].ringStatus, 'invalid'); assert.ok( buildCoreClosure(tee.topology, teeBoundaryGraph, point(0, 0)).maxSharedSeamErrorMeters <= 0.01, 'shared core seams must remain within the 1 cm tolerance', ); const offsetSeamGraph = JSON.parse(JSON.stringify(teeBoundaryGraph)); offsetSeamGraph.edges.find((edge) => edge.key === teeBoundaryGraph.sections[0].edgeRefs.inner).to[0] += 2 / metersPerDegree; const offsetSeam = buildCoreClosure(tee.topology, offsetSeamGraph, point(0, 0)); assert.ok(offsetSeam.maxSharedSeamErrorMeters > 1.9, 'shared seam metric must measure endpoint displacement in meters'); assert.ok( offsetSeam.sharedSeamErrors.find((item) => item.carriagewayKey === teeBoundaryGraph.sections[0].carriagewayKey) .errorMeters > 1.9, ); const { complexJunctionMetrics, transitionLengthForCutback } = require('../src/compile/junction-metrics'); const transitionMetrics = complexJunctionMetrics({ coreRadiusMeters: 20, outerRadiusExtraMeters: 18 }); assert.equal(transitionLengthForCutback(transitionMetrics, 0), 24); assert.equal(transitionLengthForCutback(transitionMetrics, 5), 19); assert.equal(transitionLengthForCutback(transitionMetrics, 100), 0); assert.equal(transitionLengthForCutback(transitionMetrics, 0, 6), 6); assert.equal(transitionLengthForCutback(transitionMetrics, 0, 0), 0); const curvedTransition = topology('curved-transition', [ { widthMeters: 4, line: [point(0, 0), point(0, 15), point(15, 30), point(15, 50)] }, { widthMeters: 4, line: [point(0, 0), point(50, 0)] }, { widthMeters: 4, line: [point(0, 0), point(-50, 0)] }, ]); const curvedRadii = { outerRadiusFor: () => 35, innerRadiusFor: () => 10 }; const originalCurvedGraph = buildJunctionBoundaryGraph( curvedTransition.topology, point(0, 0), curvedRadii, curvedTransition.helpers, ); curvedTransition.topology.carriageways[0].approach.transitionLengthMeters = 16; const editedCurvedGraph = buildJunctionBoundaryGraph( curvedTransition.topology, point(0, 0), curvedRadii, curvedTransition.helpers, ); assert.deepEqual(editedCurvedGraph.sections[0].outer, originalCurvedGraph.sections[0].outer); assert.ok(Math.abs(editedCurvedGraph.sections[0].transitionMeters - 16) < 1e-9); assert.notDeepEqual(editedCurvedGraph.sections[0].inner, originalCurvedGraph.sections[0].inner);