feat(junction): derive complex approach boundary sections
This commit is contained in:
1 parent
4de971ce68
commit
b4abfdb870
4 files changed
+138
-12
No files matched your search
@@ -0,0 +1,95 @@
|
||||
'use strict';
|
||||
|
||||
const { stationAtDistanceFrom } = require('./junction-build-plan');
|
||||
|
||||
function pointAtStation(line, station, helpers) {
|
||||
let remaining = Math.max(0, station);
|
||||
for (let index = 1; index < line.length; index += 1) {
|
||||
const length = helpers.distanceMeters(line[index - 1], line[index]);
|
||||
if (remaining <= length) {
|
||||
const ratio = length ? remaining / length : 0;
|
||||
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 -= length;
|
||||
}
|
||||
return line.at(-1) || null;
|
||||
}
|
||||
|
||||
function tangentAtStation(line, station, helpers) {
|
||||
let walked = 0;
|
||||
for (let index = 1; index < line.length; index += 1) {
|
||||
const length = helpers.distanceMeters(line[index - 1], line[index]);
|
||||
if (station <= walked + length || index === line.length - 1)
|
||||
return helpers.headingAtEndpoint([line[index - 1], line[index]]);
|
||||
walked += length;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function offsetCoordinate(point, heading, meters) {
|
||||
if (!point || !Number.isFinite(heading)) return null;
|
||||
const radians = (heading * Math.PI) / 180;
|
||||
const lonMeters = 111320 * Math.cos((point[1] * Math.PI) / 180);
|
||||
return [
|
||||
point[0] + (Math.sin(radians) * meters) / Math.max(lonMeters, 1e-9),
|
||||
point[1] + (Math.cos(radians) * meters) / 111320,
|
||||
];
|
||||
}
|
||||
|
||||
function sectionAt(item, radius, center, helpers) {
|
||||
const station = Math.min(item.length, stationAtDistanceFrom(item.approach.line, center, radius, helpers));
|
||||
const point = pointAtStation(item.approach.line, station, helpers);
|
||||
const tangent = tangentAtStation(item.approach.line, station, helpers);
|
||||
const halfWidth = Math.max(0, Number(item.approach.widthMeters) || 0) / 2;
|
||||
return {
|
||||
stationMeters: station,
|
||||
point,
|
||||
tangent,
|
||||
left: offsetCoordinate(point, tangent - 90, halfWidth),
|
||||
right: offsetCoordinate(point, tangent + 90, halfWidth),
|
||||
};
|
||||
}
|
||||
|
||||
// The graph currently owns only source-derived entry edges. Core closure is
|
||||
// deliberately unresolved until a later stage can distinguish paved links,
|
||||
// medians, and exclusion regions from topology alone.
|
||||
function buildJunctionBoundaryGraph(topology, center, radii, helpers) {
|
||||
const sections = topology.carriageways.map((item) => {
|
||||
const outerRadius = radii.outerRadiusFor(item);
|
||||
const innerRadius = radii.innerRadiusFor(item);
|
||||
const outer = sectionAt(item, outerRadius, center, helpers);
|
||||
const inner = sectionAt(item, innerRadius, center, helpers);
|
||||
const key = `boundary:${item.key}`;
|
||||
return {
|
||||
key,
|
||||
carriagewayKey: item.key,
|
||||
armKey: item.armKey,
|
||||
outer,
|
||||
inner,
|
||||
edgeRefs: {
|
||||
left: `${key}:left`,
|
||||
right: `${key}:right`,
|
||||
outer: `${key}:outer`,
|
||||
inner: `${key}:inner`,
|
||||
},
|
||||
};
|
||||
});
|
||||
const edges = sections.flatMap((section) => [
|
||||
{ key: section.edgeRefs.left, from: section.outer.left, to: section.inner.left, owner: section.carriagewayKey },
|
||||
{ key: section.edgeRefs.right, from: section.inner.right, to: section.outer.right, owner: section.carriagewayKey },
|
||||
{ key: section.edgeRefs.outer, from: section.outer.right, to: section.outer.left, owner: section.carriagewayKey },
|
||||
{ key: section.edgeRefs.inner, from: section.inner.left, to: section.inner.right, owner: section.carriagewayKey },
|
||||
]);
|
||||
return {
|
||||
schema: 'junction-boundary-graph/v1',
|
||||
status: 'entry-sections-only',
|
||||
sections,
|
||||
edges,
|
||||
unresolvedClosures: topology.arms.map((arm) => arm.cornerKey),
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { buildJunctionBoundaryGraph };
|
||||
Reference in new issue
Block a user