feat(workbench): preview and drag lane turn arrows
This commit is contained in:
1 parent
9cb715a592
commit
6d80597642
8 files changed
+260
-17
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { arrowPreviewRings, previewArrowFeatures } from './arrow-preview';
|
||||
import { coordinateAtStation, polylineLengthMeters, type Coordinate } from './meters';
|
||||
|
||||
const lane: Coordinate[] = [
|
||||
[116.397, 39.9],
|
||||
[116.397, 39.91],
|
||||
];
|
||||
|
||||
describe('arrow preview geometry', () => {
|
||||
it('uses the compiler part counts for each maneuver', () => {
|
||||
expect(arrowPreviewRings('through', lane, 6)).toHaveLength(1);
|
||||
expect(arrowPreviewRings('left', lane, 6)).toHaveLength(3);
|
||||
expect(arrowPreviewRings('right', lane, 6)).toHaveLength(3);
|
||||
expect(arrowPreviewRings('through;left', lane, 6)).toHaveLength(3);
|
||||
expect(arrowPreviewRings('through;right', lane, 6)).toHaveLength(3);
|
||||
expect(arrowPreviewRings('through;left;right', lane, 6)).toHaveLength(5);
|
||||
});
|
||||
|
||||
it('keeps angular parts instead of replacing them with rectangles', () => {
|
||||
const [head] = arrowPreviewRings('through', lane, 6);
|
||||
expect(new Set(head.map(([lon, lat]) => `${lon.toFixed(8)}:${lat.toFixed(8)}`)).size).toBeGreaterThan(4);
|
||||
expect(head[0]).toEqual(head.at(-1));
|
||||
});
|
||||
|
||||
it('points the arrow tip toward the lane junction end', () => {
|
||||
const distance = 6;
|
||||
const [head] = arrowPreviewRings('through', lane, distance);
|
||||
const total = polylineLengthMeters(lane);
|
||||
const center = coordinateAtStation(lane, 1 - distance / total)!;
|
||||
expect(head[0][1]).toBeGreaterThan(center[1]);
|
||||
});
|
||||
|
||||
it('assigns stable preview ids to every part', () => {
|
||||
const features = previewArrowFeatures('lane:1', 'left', lane, 6);
|
||||
expect(features.map((feature) => feature.properties?.native_id)).toEqual([
|
||||
'lane-preview:lane:1:left:0',
|
||||
'lane-preview:lane:1:left:1',
|
||||
'lane-preview:lane:1:left:2',
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,90 @@
|
||||
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] },
|
||||
}));
|
||||
}
|
||||
@@ -172,6 +172,7 @@ describe('elementSelectionFor', () => {
|
||||
kind: 'lane-maneuver',
|
||||
laneId: 'lane:road:way/-3246:forward:2',
|
||||
maneuver: '',
|
||||
placementDistanceMeters: 6,
|
||||
roadId: 'road:way/-3246:forward',
|
||||
featureIds: ['lane:road:way/-3246:forward:2'],
|
||||
});
|
||||
|
||||
@@ -184,7 +184,9 @@ export function elementSelectionFor(
|
||||
if (!spec && properties?.source === LANE_CENTERLINE_SOURCE) {
|
||||
const laneId = text(properties.native_id);
|
||||
const roadId = text(properties.road_id);
|
||||
return laneId && roadId ? { kind: 'lane-maneuver', laneId, maneuver: '', roadId, featureIds: [laneId] } : null;
|
||||
return laneId && roadId
|
||||
? { kind: 'lane-maneuver', laneId, maneuver: '', placementDistanceMeters: 6, roadId, featureIds: [laneId] }
|
||||
: null;
|
||||
}
|
||||
const elementId = elementIdOf(properties);
|
||||
if (!spec || !elementId) return null;
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
coordinateAtStation,
|
||||
fromLonLat,
|
||||
haversineMeters,
|
||||
metersAlongPolyline,
|
||||
mercatorUnitsForMeters,
|
||||
offsetCoordinate,
|
||||
polylineLengthMeters,
|
||||
@@ -141,6 +142,23 @@ describe('station interpolation', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('polyline projection', () => {
|
||||
const bent: Coordinate[] = [
|
||||
[0, 0],
|
||||
[0, 0.001],
|
||||
[0.001, 0.001],
|
||||
];
|
||||
|
||||
it('uses the closest segment rather than a road-wide drag axis', () => {
|
||||
const along = metersAlongPolyline(bent, [0.00075, 0.00102]);
|
||||
expect(along).toBeCloseTo(polylineLengthMeters(bent) * 0.875, 1);
|
||||
});
|
||||
|
||||
it('returns null when there is no usable segment', () => {
|
||||
expect(metersAlongPolyline([[0, 0]], [0, 0])).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('bearings', () => {
|
||||
it('reads the cardinal directions', () => {
|
||||
expect(azimuthBetween([0, 0], [0, 0.001])).toBeCloseTo(0, 4);
|
||||
|
||||
@@ -55,6 +55,29 @@ export function polylineLengthMeters(line: Coordinate[]): number {
|
||||
return total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Arc distance to the closest point on a polyline, in meters from its first
|
||||
* coordinate. Drag release uses this to turn a map point back into a lane
|
||||
* placement without assuming the lane is straight.
|
||||
*/
|
||||
export function metersAlongPolyline(line: Coordinate[], point: Coordinate): number | null {
|
||||
let traversed = 0;
|
||||
let best: { distance: number; along: number } | null = null;
|
||||
for (let index = 1; index < line.length; index += 1) {
|
||||
const start = line[index - 1];
|
||||
const end = line[index];
|
||||
const { east: dx, north: dy } = eastNorthMeters(start, end);
|
||||
const { east: px, north: py } = eastNorthMeters(start, point);
|
||||
const length = Math.hypot(dx, dy);
|
||||
if (length < 0.001) continue;
|
||||
const ratio = clamp((px * dx + py * dy) / (length * length), 0, 1);
|
||||
const distance = Math.hypot(px - dx * ratio, py - dy * ratio);
|
||||
if (!best || distance < best.distance) best = { distance, along: traversed + length * ratio };
|
||||
traversed += length;
|
||||
}
|
||||
return best?.along ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Local east/north components of a displacement, in meters. Each component is a
|
||||
* spherical distance measured along a parallel or a meridian, so the result
|
||||
|
||||
Reference in new issue
Block a user