-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathisolines.ts
321 lines (300 loc) · 8.05 KB
/
isolines.ts
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
Adapted from d3-contour https://github.com/d3/d3-contour
Copyright 2012-2023 Mike Bostock
Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
*/
import type { HeightTile } from "./height-tile";
class Fragment {
start: number;
end: number;
points: number[];
constructor(start: number, end: number) {
this.start = start;
this.end = end;
this.points = [];
this.append = this.append.bind(this);
this.prepend = this.prepend.bind(this);
}
append(x: number, y: number) {
this.points.push(Math.round(x), Math.round(y));
}
prepend(x: number, y: number) {
this.points.splice(0, 0, Math.round(x), Math.round(y));
}
lineString() {
return this.toArray();
}
isEmpty() {
return this.points.length < 2;
}
appendFragment(other: Fragment) {
this.points.push(...other.points);
this.end = other.end;
}
toArray() {
return this.points;
}
}
const CASES: [number, number][][][] = [
[],
[
[
[1, 2],
[0, 1],
],
],
[
[
[2, 1],
[1, 2],
],
],
[
[
[2, 1],
[0, 1],
],
],
[
[
[1, 0],
[2, 1],
],
],
[
[
[1, 2],
[0, 1],
],
[
[1, 0],
[2, 1],
],
],
[
[
[1, 0],
[1, 2],
],
],
[
[
[1, 0],
[0, 1],
],
],
[
[
[0, 1],
[1, 0],
],
],
[
[
[1, 2],
[1, 0],
],
],
[
[
[0, 1],
[1, 0],
],
[
[2, 1],
[1, 2],
],
],
[
[
[2, 1],
[1, 0],
],
],
[
[
[0, 1],
[2, 1],
],
],
[
[
[1, 2],
[2, 1],
],
],
[
[
[0, 1],
[1, 2],
],
],
[],
];
function index(width: number, x: number, y: number, point: [number, number]) {
x = x * 2 + point[0];
y = y * 2 + point[1];
return x + y * (width + 1) * 2;
}
function ratio(a: number, b: number, c: number) {
return (b - a) / (c - a);
}
/**
* Generates contour lines from a HeightTile
*
* @param interval Vertical distance between contours
* @param tile The input height tile, where values represent the height at the top-left of each pixel
* @param extent Vector tile extent (default 4096)
* @param buffer How many pixels into each neighboring tile to include in a tile
* @returns an object where keys are the elevation, and values are a list of `[x1, y1, x2, y2, ...]`
* contour lines in tile coordinates
*/
export default function generateIsolines(
interval: number,
tile: HeightTile,
extent: number = 4096,
buffer: number = 1,
): { [ele: number]: number[][] } {
if (!interval) {
return {};
}
const multiplier = extent / (tile.width - 1);
let tld: number, trd: number, bld: number, brd: number;
let r: number, c: number;
const segments: { [ele: string]: number[][] } = {};
const fragmentByStartByLevel: Map<number, Map<number, Fragment>> = new Map();
const fragmentByEndByLevel: Map<number, Map<number, Fragment>> = new Map();
function interpolate(
point: [number, number],
threshold: number,
accept: (x: number, y: number) => void,
) {
if (point[0] === 0) {
// left
accept(
multiplier * (c - 1),
multiplier * (r - ratio(bld, threshold, tld)),
);
} else if (point[0] === 2) {
// right
accept(multiplier * c, multiplier * (r - ratio(brd, threshold, trd)));
} else if (point[1] === 0) {
// top
accept(
multiplier * (c - ratio(trd, threshold, tld)),
multiplier * (r - 1),
);
} else {
// bottom
accept(multiplier * (c - ratio(brd, threshold, bld)), multiplier * r);
}
}
// Most marching-squares implementations (d3-contour, gdal-contour) make one pass through the matrix per threshold.
// This implementation makes a single pass through the matrix, building up all of the contour lines at the
// same time to improve performance.
for (r = 1 - buffer; r < tile.height + buffer; r++) {
trd = tile.get(0, r - 1);
brd = tile.get(0, r);
let minR = Math.min(trd, brd);
let maxR = Math.max(trd, brd);
for (c = 1 - buffer; c < tile.width + buffer; c++) {
tld = trd;
bld = brd;
trd = tile.get(c, r - 1);
brd = tile.get(c, r);
const minL = minR;
const maxL = maxR;
minR = Math.min(trd, brd);
maxR = Math.max(trd, brd);
if (isNaN(tld) || isNaN(trd) || isNaN(brd) || isNaN(bld)) {
continue;
}
const min = Math.min(minL, minR);
const max = Math.max(maxL, maxR);
const start = Math.ceil(min / interval) * interval;
const end = Math.floor(max / interval) * interval;
for (let threshold = start; threshold <= end; threshold += interval) {
const tl = tld > threshold;
const tr = trd > threshold;
const bl = bld > threshold;
const br = brd > threshold;
for (const segment of CASES[
(tl ? 8 : 0) | (tr ? 4 : 0) | (br ? 2 : 0) | (bl ? 1 : 0)
]) {
let fragmentByStart = fragmentByStartByLevel.get(threshold);
if (!fragmentByStart)
fragmentByStartByLevel.set(
threshold,
(fragmentByStart = new Map()),
);
let fragmentByEnd = fragmentByEndByLevel.get(threshold);
if (!fragmentByEnd)
fragmentByEndByLevel.set(threshold, (fragmentByEnd = new Map()));
const start = segment[0];
const end = segment[1];
const startIndex = index(tile.width, c, r, start);
const endIndex = index(tile.width, c, r, end);
let f, g;
if ((f = fragmentByEnd.get(startIndex))) {
fragmentByEnd.delete(startIndex);
if ((g = fragmentByStart.get(endIndex))) {
fragmentByStart.delete(endIndex);
if (f === g) {
// closing a ring
interpolate(end, threshold, f.append);
if (!f.isEmpty()) {
let list = segments[threshold];
if (!list) {
segments[threshold] = list = [];
}
list.push(f.lineString());
}
} else {
// connecting 2 segments
f.appendFragment(g);
fragmentByEnd.set((f.end = g.end), f);
}
} else {
// adding to the end of f
interpolate(end, threshold, f.append);
fragmentByEnd.set((f.end = endIndex), f);
}
} else if ((f = fragmentByStart.get(endIndex))) {
fragmentByStart.delete(endIndex);
// extending the start of f
interpolate(start, threshold, f.prepend);
fragmentByStart.set((f.start = startIndex), f);
} else {
// starting a new fragment
const newFrag = new Fragment(startIndex, endIndex);
interpolate(start, threshold, newFrag.append);
interpolate(end, threshold, newFrag.append);
fragmentByStart.set(startIndex, newFrag);
fragmentByEnd.set(endIndex, newFrag);
}
}
}
}
}
for (const [level, fragmentByStart] of fragmentByStartByLevel.entries()) {
let list: number[][] | null = null;
for (const value of fragmentByStart.values()) {
if (!value.isEmpty()) {
if (list == null) {
list = segments[level] || (segments[level] = []);
}
list.push(value.lineString());
}
}
}
return segments;
}