91 lines
3.6 KiB
TypeScript
91 lines
3.6 KiB
TypeScript
import { azimuthBetween, coordinateAtStation, offsetCoordinate, polylineLengthMeters, type Coordinate } from './meters';
|
|
import type { GeoFeature } from '../types/state';
|
|
|
|
type Ring = Coordinate[];
|
|
|
|
const angularTemplate = (assetId: string): Ring[] => {
|
|
const shaftWidth = 0.3;
|
|
const shaftHalf = shaftWidth / 2;
|
|
const straightBase = 1.18;
|
|
const straightTip = 1.92;
|
|
const throughHead = (): Ring => [
|
|
[0, straightTip],
|
|
[-0.42, straightBase],
|
|
[-shaftHalf, straightBase],
|
|
[-shaftHalf, 0],
|
|
[shaftHalf, 0],
|
|
[shaftHalf, straightBase],
|
|
[0.42, straightBase],
|
|
[0, straightTip],
|
|
];
|
|
const diagonalShaft = (side: number): Ring => {
|
|
const start: Coordinate = [0, 0.56];
|
|
const end: Coordinate = [side * 0.72, 0.96];
|
|
const length = Math.hypot(end[0] - start[0], end[1] - start[1]);
|
|
const normal = [(-(end[1] - start[1]) / length) * shaftHalf, ((end[0] - start[0]) / length) * shaftHalf];
|
|
return [
|
|
[start[0] + normal[0], start[1] + normal[1]],
|
|
[end[0] + normal[0], end[1] + normal[1]],
|
|
[end[0] - normal[0], end[1] - normal[1]],
|
|
[start[0] - normal[0], start[1] - normal[1]],
|
|
[start[0] + normal[0], start[1] + normal[1]],
|
|
];
|
|
};
|
|
const diagonalHead = (side: number): Ring => {
|
|
const base: Coordinate = [side * 0.6, 0.89];
|
|
const tip: Coordinate = [side * 1.22, 1.24];
|
|
const length = Math.hypot(tip[0] - base[0], tip[1] - base[1]);
|
|
const normal = [(-(tip[1] - base[1]) / length) * 0.36, ((tip[0] - base[0]) / length) * 0.36];
|
|
return [tip, [base[0] + normal[0], base[1] + normal[1]], [base[0] - normal[0], base[1] - normal[1]], tip];
|
|
};
|
|
const turnStem = (side: number): Ring => {
|
|
const cutMidpoint = 0.73;
|
|
const cutRise = side * 0.084;
|
|
return [
|
|
[-shaftHalf, 0],
|
|
[shaftHalf, 0],
|
|
[shaftHalf, cutMidpoint + cutRise],
|
|
[-shaftHalf, cutMidpoint - cutRise],
|
|
[-shaftHalf, 0],
|
|
];
|
|
};
|
|
if (assetId === 'through') return [throughHead()];
|
|
if (assetId === 'right') return [turnStem(1), diagonalShaft(1), diagonalHead(1)];
|
|
if (assetId === 'left') return [turnStem(-1), diagonalShaft(-1), diagonalHead(-1)];
|
|
if (assetId === 'through;right') return [throughHead(), diagonalShaft(1), diagonalHead(1)];
|
|
if (assetId === 'through;left') return [throughHead(), diagonalShaft(-1), diagonalHead(-1)];
|
|
if (assetId === 'through;left;right')
|
|
return [throughHead(), diagonalShaft(-1), diagonalHead(-1), diagonalShaft(1), diagonalHead(1)];
|
|
return [];
|
|
};
|
|
|
|
/** Build the same angular arrow parts used by the compiler, at a lane station. */
|
|
export function arrowPreviewRings(maneuver: string, line: Coordinate[], distance: number): Ring[] {
|
|
const total = polylineLengthMeters(line);
|
|
if (line.length < 2 || !(total > 0)) return [];
|
|
const station = Math.max(0, Math.min(1, 1 - distance / total));
|
|
const center = coordinateAtStation(line, station);
|
|
if (!center) return [];
|
|
const before = coordinateAtStation(line, Math.max(0, station - Math.min(0.01, station))) || line[0];
|
|
const axis = azimuthBetween(before, center);
|
|
return angularTemplate(maneuver).map((template) => {
|
|
const ring = template.map(([side, forward]) =>
|
|
offsetCoordinate(offsetCoordinate(center, axis, forward), axis + 90, side),
|
|
);
|
|
return ring;
|
|
});
|
|
}
|
|
|
|
export function previewArrowFeatures(
|
|
laneId: string,
|
|
maneuver: string,
|
|
line: Coordinate[],
|
|
distance: number,
|
|
): GeoFeature[] {
|
|
return arrowPreviewRings(maneuver, line, distance).map((ring, part) => ({
|
|
type: 'Feature',
|
|
properties: { native_id: `lane-preview:${laneId}:${maneuver}:${part}` },
|
|
geometry: { type: 'Polygon', coordinates: [ring] },
|
|
}));
|
|
}
|