diff --git a/server/src/nest/integrations/dawarich.helpers.ts b/server/src/nest/integrations/dawarich.helpers.ts index 383d957ed..71a0cf24e 100644 --- a/server/src/nest/integrations/dawarich.helpers.ts +++ b/server/src/nest/integrations/dawarich.helpers.ts @@ -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]> { diff --git a/shared/src/geo/recorded-line.spec.ts b/shared/src/geo/recorded-line.spec.ts new file mode 100644 index 000000000..091ce10e7 --- /dev/null +++ b/shared/src/geo/recorded-line.spec.ts @@ -0,0 +1,66 @@ +// RECLINE-001 to RECLINE-004: thinning a recorded line, shared by the Dawarich overlay and the kept days. +import { capLinePoints, thinRecordedLine } from './recorded-line'; + +import { describe, expect, it } from 'vitest'; + +describe('thinRecordedLine', () => { + it('RECLINE-001: leaves a line of two points or fewer alone', () => { + expect(thinRecordedLine([], 0.00005)).toEqual([]); + expect(thinRecordedLine([[1, 2]], 0.00005)).toEqual([[1, 2]]); + expect( + thinRecordedLine( + [ + [1, 2], + [3, 4], + ], + 0.00005, + ), + ).toEqual([ + [1, 2], + [3, 4], + ]); + }); + + it('RECLINE-002: drops the points of a straight run and keeps a corner', () => { + const straight: Array<[number, number]> = Array.from({ length: 50 }, (_, i) => [48 + i * 0.001, 11]); + expect(thinRecordedLine(straight, 0.00005)).toEqual([ + [48, 11], + [48 + 49 * 0.001, 11], + ]); + const corner: Array<[number, number]> = [ + [48, 11], + [48.001, 11], + [48.002, 11], + [48.002, 11.001], + [48.002, 11.002], + ]; + expect(thinRecordedLine(corner, 0.00005)).toEqual([ + [48, 11], + [48.002, 11], + [48.002, 11.002], + ]); + }); + + it('RECLINE-003: thins a line that starts and ends on the same spot', () => { + const loop: Array<[number, number]> = [ + [48, 11], + [48.001, 11], + [48.001, 11.001], + [48, 11], + ]; + expect(thinRecordedLine(loop, 0.00005)).toHaveLength(4); + }); +}); + +describe('capLinePoints', () => { + it('RECLINE-004: keeps an even stride with the first and the last point', () => { + const line: Array<[number, number]> = Array.from({ length: 101 }, (_, i) => [i, 0]); + const capped = capLinePoints(line, 11); + expect(capped).toHaveLength(11); + expect(capped[0]).toEqual([0, 0]); + expect(capped[10]).toEqual([100, 0]); + expect(capped[5]).toEqual([50, 0]); + expect(capLinePoints(line, 200)).toBe(line); + expect(capLinePoints(line, 1)).toBe(line); + }); +}); diff --git a/shared/src/geo/recorded-line.ts b/shared/src/geo/recorded-line.ts new file mode 100644 index 000000000..29ce4baa3 --- /dev/null +++ b/shared/src/geo/recorded-line.ts @@ -0,0 +1,85 @@ +/** + * Thinning a recorded line, the same way for every source that records one: + * the Dawarich overlay and the days a journey keeps from Dawarich or from a + * GPX file. A day of one-second tracking is tens of thousands of points; what + * a map needs is its corners. + */ + +/** `[lat, lng]`, as TREK stores every track. */ +type LatLng = [number, number]; + +/** Perpendicular distance in degrees, longitude scaled so the metric is not latitude-skewed. */ +function perpendicularDistance(point: LatLng, start: LatLng, end: LatLng): 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)); +} + +/** + * Ramer–Douglas–Peucker, iterative, with `epsilon` in degrees (~5e-5 is + * roughly five metres). + * + * 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. + */ +export function thinRecordedLine(points: LatLng[], epsilon: number): LatLng[] { + 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]]; + + for (let range = stack.pop(); range; range = stack.pop()) { + const [first, last] = range; + if (last <= first + 1) continue; + const start = points[first] as LatLng; + const end = points[last] as LatLng; + + let maxDist = 0; + let index = first; + for (let i = first + 1; i < last; i++) { + const dist = perpendicularDistance(points[i] as LatLng, start, end); + 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); +} + +/** + * Cap a polyline at `max` points by keeping an even stride. + * + * A backstop behind `thinRecordedLine`, not a replacement: thinning 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 capLinePoints(points: LatLng[], max: number): LatLng[] { + if (points.length <= max || max < 2) return points; + const stride = (points.length - 1) / (max - 1); + const out: LatLng[] = []; + for (let i = 0; i < max - 1; i++) out.push(points[Math.round(i * stride)] as LatLng); + out.push(points[points.length - 1] as LatLng); + return out; +} diff --git a/shared/src/index.ts b/shared/src/index.ts index 44274e75a..d21bcec68 100644 --- a/shared/src/index.ts +++ b/shared/src/index.ts @@ -121,6 +121,7 @@ export * from './wanderer/wanderer-watch.schema'; export * from './wanderer/wanderer-mcp.schema'; export * from './geo/haversine'; export * from './geo/longitude'; +export * from './geo/recorded-line'; export * from './collection/collection.schema'; export * from './collection/collection-file.schema'; export * from './places-library/places-library.schema';