mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
refactor(shared): share the thinning of a recorded line
Ramer-Douglas-Peucker and the even-stride cap move from the Dawarich helpers into @trek/shared as thinRecordedLine and capLinePoints, so a second source of recorded days thins its lines the same way. The Dawarich helpers keep their names for them.
This commit is contained in:
@@ -9,6 +9,8 @@ import type {
|
||||
import {
|
||||
DAWARICH_WINDOW_MAX_DAYS,
|
||||
RECORDING_GAP_METERS,
|
||||
capLinePoints,
|
||||
thinRecordedLine,
|
||||
type DawarichTrackDay,
|
||||
type DawarichTrackSegment,
|
||||
} from '@trek/shared';
|
||||
@@ -295,77 +297,11 @@ export function distanceMeters(aLat: number, aLng: number, bLat: number, bLng: n
|
||||
}
|
||||
|
||||
/**
|
||||
* Ramer–Douglas–Peucker, iterative.
|
||||
*
|
||||
* Recursion is the textbook shape and the wrong one here: a day of one-second
|
||||
* tracking is tens of thousands of points and a nearly straight motorway run is
|
||||
* its worst case, which is exactly where the recursion depth peaks.
|
||||
* The line thinning every recorded source shares (`@trek/shared`): Ramer–Douglas–
|
||||
* Peucker in degrees, then an even-stride cap as the backstop.
|
||||
*/
|
||||
export function simplify(points: Array<[number, number]>, epsilon: number): Array<[number, number]> {
|
||||
if (points.length <= 2) return points;
|
||||
|
||||
const keep = new Uint8Array(points.length);
|
||||
keep[0] = 1;
|
||||
keep[points.length - 1] = 1;
|
||||
const stack: Array<[number, number]> = [[0, points.length - 1]];
|
||||
|
||||
while (stack.length > 0) {
|
||||
const [first, last] = stack.pop()!;
|
||||
if (last <= first + 1) continue;
|
||||
|
||||
let maxDist = 0;
|
||||
let index = first;
|
||||
for (let i = first + 1; i < last; i++) {
|
||||
const dist = perpendicularDistance(points[i]!, points[first]!, points[last]!);
|
||||
if (dist > maxDist) {
|
||||
maxDist = dist;
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (maxDist > epsilon) {
|
||||
keep[index] = 1;
|
||||
stack.push([first, index], [index, last]);
|
||||
}
|
||||
}
|
||||
|
||||
return points.filter((_, i) => keep[i] === 1);
|
||||
}
|
||||
|
||||
/** Perpendicular distance in degrees, longitude scaled so the metric is not latitude-skewed. */
|
||||
function perpendicularDistance(point: [number, number], start: [number, number], end: [number, number]): number {
|
||||
const scale = Math.cos((point[0] * Math.PI) / 180) || 1;
|
||||
const px = point[1] * scale;
|
||||
const py = point[0];
|
||||
const sx = start[1] * scale;
|
||||
const sy = start[0];
|
||||
const ex = end[1] * scale;
|
||||
const ey = end[0];
|
||||
|
||||
const dx = ex - sx;
|
||||
const dy = ey - sy;
|
||||
if (dx === 0 && dy === 0) return Math.hypot(px - sx, py - sy);
|
||||
|
||||
const t = Math.max(0, Math.min(1, ((px - sx) * dx + (py - sy) * dy) / (dx * dx + dy * dy)));
|
||||
return Math.hypot(px - (sx + t * dx), py - (sy + t * dy));
|
||||
}
|
||||
|
||||
/**
|
||||
* Cap a polyline at `max` points by keeping an even stride.
|
||||
*
|
||||
* A backstop behind `simplify`, not a replacement: simplification keeps corners
|
||||
* and drops straights, which is what a route should look like, but a
|
||||
* sufficiently jittery recording survives it. The first and last point are
|
||||
* always kept so the line still starts and ends where the day did.
|
||||
*/
|
||||
export function capPoints(points: Array<[number, number]>, max: number): Array<[number, number]> {
|
||||
if (points.length <= max || max < 2) return points;
|
||||
const stride = (points.length - 1) / (max - 1);
|
||||
const out: Array<[number, number]> = [];
|
||||
for (let i = 0; i < max - 1; i++) out.push(points[Math.round(i * stride)]!);
|
||||
out.push(points[points.length - 1]!);
|
||||
return out;
|
||||
}
|
||||
export const simplify = thinRecordedLine;
|
||||
export const capPoints = capLinePoints;
|
||||
|
||||
/** `[lng, lat]` GeoJSON pairs → TREK's `[lat, lng]`, skipping anything unusable. */
|
||||
export function geoJsonToLatLng(coordinates: unknown): Array<[number, number]> {
|
||||
|
||||
Reference in New Issue
Block a user