mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
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:
@@ -1,4 +1,4 @@
|
||||
import { lineLength, metresBetween, projectOnSegment } from '../engine/geo';
|
||||
import { lineLength, metresBetween, projectOnSegment } from '../../../lib/geo/geo';
|
||||
import { Access, EdgeKind, type EdgeKindCode, PortageSource } from '../water-graph';
|
||||
|
||||
/** A node while the network is built: where it is, and the lake it opens into (-1 for none). */
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { LocalFrame } from '../engine/geo';
|
||||
import { LocalFrame } from '../../../lib/geo/geo';
|
||||
import type { LakeGrid } from '../water-graph';
|
||||
import { type BuildEdge, type BuildState, PointGrid } from './build-state';
|
||||
import type { CollectedArea, CollectedWater } from './collect';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { projectOnSegment } from '../engine/geo';
|
||||
import { projectOnSegment } from '../../../lib/geo/geo';
|
||||
import { Access, EdgeKind, PortageSource } from '../water-graph';
|
||||
import { type BarrierSite, type BuildEdge, type BuildState, PointGrid } from './build-state';
|
||||
import type { CollectedWater } from './collect';
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
/** Metres per degree of latitude, the figure the paddle 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;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { LocalFrame } from '../../../lib/geo/geo';
|
||||
import { MinHeap } from '../../../lib/geo/min-heap';
|
||||
import { maskHas } from '../build/lakes';
|
||||
import type { LakeGrid } from '../water-graph';
|
||||
import { LocalFrame } from './geo';
|
||||
import { MinHeap } from './min-heap';
|
||||
import type { PaddleLakeStyle } from '@trek/shared';
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
import { metresBetween, projectOnSegment } from '../../../lib/geo/geo';
|
||||
import { MinHeap } from '../../../lib/geo/min-heap';
|
||||
import { LakeIndex } from '../build/lakes';
|
||||
import { Access, EdgeKind, PortageSource, type WaterGraphData } from '../water-graph';
|
||||
import { metresBetween, projectOnSegment } from './geo';
|
||||
import { LakeField, type LakeProfile } from './lake-field';
|
||||
import { MinHeap } from './min-heap';
|
||||
import type {
|
||||
PaddleBoatType,
|
||||
PaddleLakeStyle,
|
||||
|
||||
Reference in New Issue
Block a user