refactor(server): move the routing heap and the metre helpers out of the paddle domain

The binary heap and the flat-frame distance helpers are not about water;
the piste router needs the same two. They move to src/lib/geo, where any
domain may import them without reaching into paddle-routing. Nothing
changes in what they do.
This commit is contained in:
Maurice
2026-10-20 19:00:25 +02:00
parent d924dcf99d
commit beea4c762b
7 changed files with 8 additions and 8 deletions
+71
View File
@@ -0,0 +1,71 @@
/** Metres per degree of latitude, the figure the routing code reckons with throughout. */
const M_PER_DEG = 111_195;
export function metresBetween(aLat: number, aLng: number, bLat: number, bLng: number): number {
const cos = Math.cos((((aLat + bLat) / 2) * Math.PI) / 180);
return Math.hypot((bLat - aLat) * M_PER_DEG, (bLng - aLng) * M_PER_DEG * cos);
}
/**
* A flat frame in metres around one spot, good for the few kilometres a lake
* or a stretch of river spans: x east, y north.
*/
export class LocalFrame {
readonly cos: number;
constructor(
readonly lat0: number,
readonly lng0: number,
) {
this.cos = Math.cos((lat0 * Math.PI) / 180);
}
x(lng: number): number {
return (lng - this.lng0) * M_PER_DEG * this.cos;
}
y(lat: number): number {
return (lat - this.lat0) * M_PER_DEG;
}
lat(y: number): number {
return this.lat0 + y / M_PER_DEG;
}
lng(x: number): number {
return this.lng0 + x / (M_PER_DEG * this.cos);
}
}
/**
* Where a point falls on a segment: the share along it (clamped to the
* segment) and the distance to it, in metres.
*/
export function projectOnSegment(
lat: number,
lng: number,
aLat: number,
aLng: number,
bLat: number,
bLng: number,
): { t: number; distance: number } {
const cos = Math.cos((lat * Math.PI) / 180);
const ax = (aLng - lng) * cos;
const ay = aLat - lat;
const dx = (bLng - aLng) * cos;
const dy = bLat - aLat;
const length2 = dx * dx + dy * dy;
const t = length2 === 0 ? 0 : Math.max(0, Math.min(1, -(ax * dx + ay * dy) / length2));
return { t, distance: Math.hypot(ax + t * dx, ay + t * dy) * M_PER_DEG };
}
/** The length of a [lat, lng] line in metres. */
export function lineLength(points: ReadonlyArray<readonly [number, number]>): number {
let total = 0;
for (let i = 1; i < points.length; i++) {
const a = points[i - 1] as readonly [number, number];
const b = points[i] as readonly [number, number];
total += metresBetween(a[0], a[1], b[0], b[1]);
}
return total;
}
+75
View File
@@ -0,0 +1,75 @@
/**
* A binary min-heap of integer ids by a float priority, on typed arrays so a
* search over a few hundred thousand cells or nodes makes no garbage. An id
* may be pushed again with a better priority; the stale entry is skipped by
* the caller when it comes up.
*/
export class MinHeap {
private ids: Int32Array;
private keys: Float64Array;
private length = 0;
constructor(capacity = 1024) {
this.ids = new Int32Array(capacity);
this.keys = new Float64Array(capacity);
}
get size(): number {
return this.length;
}
clear(): void {
this.length = 0;
}
push(id: number, key: number): void {
if (this.length === this.ids.length) this.grow();
let at = this.length++;
while (at > 0) {
const parent = (at - 1) >> 1;
const parentKey = this.keys[parent] as number;
if (parentKey <= key) break;
this.ids[at] = this.ids[parent] as number;
this.keys[at] = parentKey;
at = parent;
}
this.ids[at] = id;
this.keys[at] = key;
}
peekKey(): number {
return this.keys[0] as number;
}
/** The id with the lowest priority, taken off; call only while `size` > 0. */
pop(): number {
const top = this.ids[0] as number;
const last = --this.length;
if (last > 0) {
const id = this.ids[last] as number;
const key = this.keys[last] as number;
let at = 0;
for (;;) {
let child = at * 2 + 1;
if (child >= last) break;
if (child + 1 < last && (this.keys[child + 1] as number) < (this.keys[child] as number)) child++;
if ((this.keys[child] as number) >= key) break;
this.ids[at] = this.ids[child] as number;
this.keys[at] = this.keys[child] as number;
at = child;
}
this.ids[at] = id;
this.keys[at] = key;
}
return top;
}
private grow(): void {
const ids = new Int32Array(this.ids.length * 2);
const keys = new Float64Array(this.keys.length * 2);
ids.set(this.ids);
keys.set(this.keys);
this.ids = ids;
this.keys = keys;
}
}