Repository navigation
Expand file tree
/
Copy pathprofile-data.ts
More file actions
97 lines (85 loc) · 4.94 KB
/
Copy pathprofile-data.ts
File metadata and controls
97 lines (85 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { formatAlong, formatDistance, formatHeight, heightIn } from '../settings/format';
import type { RoutingLine } from './routing';
import { cumulative, firstIndex } from './geo';
/** The heights along a line, such as a routing line or a route of the route service. */
export type ProfileLine = Pick<RoutingLine, 'coordinates' | 'elevation'>;
export type ProfileSample = { progress: number; height: number | null };
// A route is never changed after it is built, so each route's samples are computed once.
const samplesByRoute = new WeakMap<object, readonly ProfileSample[]>();
export function profileSamples(line?: ProfileLine): readonly ProfileSample[] {
if (!line) return [];
const known = samplesByRoute.get(line);
if (known) return known;
const distance = cumulative(line.coordinates);
const total = distance.at(-1) || 1;
const samples = line.elevation.map((height, i) => ({ progress: distance[i] / total, height }));
samplesByRoute.set(line, samples);
return samples;
}
/** Whole hundreds of the height unit of the rider (`heightUnit()`) that hold every known height of the sample lists, in
* metres; 0 to 100 of that unit without one. */
export function heightRange(...lists: (readonly ProfileSample[])[]): { low: number; high: number } {
let min = Infinity, max = -Infinity;
for (const list of lists) for (const { height } of list) if (height !== null) { min = Math.min(min, height); max = Math.max(max, height); }
const step = 100 / heightIn(1);
if (min === Infinity) return { low: 0, high: step };
const low = Math.floor(min / step) * step;
return { low, high: Math.max(low + step, Math.ceil(max / step) * step) };
}
/** SVG paths of a profile in a 1000 × 105 box between the heights `low` and `high`: one line, and the area below it, for
* each run of known heights. The route spans `width` of the box. */
export function profilePaths(samples: readonly ProfileSample[], low: number, high: number, width = 1): { line: string; area: string } {
const runs: { line: string; area: string }[] = [];
let path = '', first = 0, last = 0;
function close() {
if (path) runs.push({ line: path, area: `${path}L${last} 105L${first} 105Z` });
path = '';
}
for (const sample of samples) {
if (sample.height === null) { close(); continue; }
last = sample.progress * width * 1000;
if (!path) first = last;
path += `${path ? 'L' : 'M'}${last} ${105 - (sample.height - low) / (high - low) * 100}`;
}
close();
return { line: runs.map(run => run.line).join(' '), area: runs.map(run => run.area).join(' ') };
}
/** The samples from `from` to `to` and one more on each side, so that the paths of neighbouring stretches join. */
export function sampleSlice(samples: readonly ProfileSample[], from: number, to: number): readonly ProfileSample[] {
return samples.slice(Math.max(0, sampleIndex(samples, from) - 1), sampleIndex(samples, to) + 1);
}
function profileChange(from: number, to: number, line: ProfileLine | undefined, direction: number): number {
const samples = profileSamples(line);
let ascent = 0;
const last = Math.min(samples.length - 1, sampleIndex(samples, to));
for (let i = Math.max(1, sampleIndex(samples, from)); i <= last; i++) {
const a = samples[i - 1], b = samples[i];
if (a.height === null || b.height === null || b.progress <= a.progress) continue;
const share = Math.max(0, Math.min(b.progress, to) - Math.max(a.progress, from)) / (b.progress - a.progress);
ascent += Math.max(0, (b.height - a.height) * direction) * share;
}
return Math.round(ascent);
}
export function profileAscent(from = 0, to = 1, line?: ProfileLine): number {
return profileChange(from, to, line, 1);
}
export function profileDescent(from = 0, to = 1, line?: ProfileLine): number {
return profileChange(from, to, line, -1);
}
/** The index of the first sample at or past `progress`, or the sample count. */
export function sampleIndex(samples: readonly ProfileSample[], progress: number): number {
return firstIndex(samples.length, i => samples[i].progress >= progress);
}
export function profileHeightAt(progress: number, samples: readonly ProfileSample[]): number | null {
const i = sampleIndex(samples, progress);
if (i === samples.length) return samples.at(-1)?.height ?? null;
if (i === 0) return samples[0].height;
const a = samples[i - 1], b = samples[i];
if (a.height === null || b.height === null) return null;
return a.height + (b.height - a.height) * (progress - a.progress) / (b.progress - a.progress || 1);
}
/** The numbers of a selected part, such as "km 6.1 to km 9.1 · 3.0 km · 200 m up"; a null gain is left out. */
export function formatPart(fromM: number, toM: number, gainM: number | null, lengthM = toM - fromM): string {
return [`${formatAlong(fromM)} to ${formatAlong(toM)}`, formatDistance(lengthM), gainM === null ? '' : `${formatHeight(gainM)} up`]
.filter(Boolean).join(' · ');
}