@gisatcz/deckgl-geolib 1.10.1-dev.0 → 1.10.1-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,495 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ // ISC License
6
+ // Copyright(c) 2019, Michael Fogleman, Vladimir Agafonkin
7
+
8
+ // @ts-nocheck
9
+
10
+ /* eslint-disable complexity, max-params, max-statements, max-depth, no-constant-condition */
11
+ export default class Delatin {
12
+ constructor(data, width, height = width) {
13
+ this.data = data; // height data
14
+ this.width = width;
15
+ this.height = height;
16
+
17
+ this.coords = []; // vertex coordinates (x, y)
18
+ this.triangles = []; // mesh triangle indices
19
+
20
+ // additional triangle data
21
+ this._halfedges = [];
22
+ this._candidates = [];
23
+ this._queueIndices = [];
24
+
25
+ this._queue = []; // queue of added triangles
26
+ this._errors = [];
27
+ this._rms = [];
28
+ this._pending = []; // triangles pending addition to queue
29
+ this._pendingLen = 0;
30
+
31
+ this._rmsSum = 0;
32
+
33
+ const x1 = width - 1;
34
+ const y1 = height - 1;
35
+ const p0 = this._addPoint(0, 0);
36
+ const p1 = this._addPoint(x1, 0);
37
+ const p2 = this._addPoint(0, y1);
38
+ const p3 = this._addPoint(x1, y1);
39
+
40
+ // add initial two triangles
41
+ const t0 = this._addTriangle(p3, p0, p2, -1, -1, -1);
42
+ this._addTriangle(p0, p3, p1, t0, -1, -1);
43
+ this._flush();
44
+ }
45
+
46
+ // refine the mesh until its maximum error gets below the given one
47
+ run(maxError = 1) {
48
+ while (this.getMaxError() > maxError) {
49
+ this.refine();
50
+ }
51
+ }
52
+
53
+ // refine the mesh with a single point
54
+ refine() {
55
+ this._step();
56
+ this._flush();
57
+ }
58
+
59
+ // max error of the current mesh
60
+ getMaxError() {
61
+ return this._errors[0];
62
+ }
63
+
64
+ // root-mean-square deviation of the current mesh
65
+ getRMSD() {
66
+ return this._rmsSum > 0 ? Math.sqrt(this._rmsSum / (this.width * this.height)) : 0;
67
+ }
68
+
69
+ // height value at a given position
70
+ heightAt(x, y) {
71
+ return this.data[this.width * y + x];
72
+ }
73
+
74
+ // rasterize and queue all triangles that got added or updated in _step
75
+ _flush() {
76
+ const { coords } = this;
77
+ for (let i = 0; i < this._pendingLen; i++) {
78
+ const t = this._pending[i];
79
+ // rasterize triangle to find maximum pixel error
80
+ const a = 2 * this.triangles[t * 3 + 0];
81
+ const b = 2 * this.triangles[t * 3 + 1];
82
+ const c = 2 * this.triangles[t * 3 + 2];
83
+ this._findCandidate(
84
+ coords[a],
85
+ coords[a + 1],
86
+ coords[b],
87
+ coords[b + 1],
88
+ coords[c],
89
+ coords[c + 1],
90
+ t,
91
+ );
92
+ }
93
+ this._pendingLen = 0;
94
+ }
95
+
96
+ // rasterize a triangle, find its max error, and queue it for processing
97
+ _findCandidate(p0x, p0y, p1x, p1y, p2x, p2y, t) {
98
+ // triangle bounding box
99
+ const minX = Math.min(p0x, p1x, p2x);
100
+ const minY = Math.min(p0y, p1y, p2y);
101
+ const maxX = Math.max(p0x, p1x, p2x);
102
+ const maxY = Math.max(p0y, p1y, p2y);
103
+
104
+ // forward differencing variables
105
+ let w00 = orient(p1x, p1y, p2x, p2y, minX, minY);
106
+ let w01 = orient(p2x, p2y, p0x, p0y, minX, minY);
107
+ let w02 = orient(p0x, p0y, p1x, p1y, minX, minY);
108
+ const a01 = p1y - p0y;
109
+ const b01 = p0x - p1x;
110
+ const a12 = p2y - p1y;
111
+ const b12 = p1x - p2x;
112
+ const a20 = p0y - p2y;
113
+ const b20 = p2x - p0x;
114
+
115
+ // pre-multiplied z values at vertices
116
+ const a = orient(p0x, p0y, p1x, p1y, p2x, p2y);
117
+ const z0 = this.heightAt(p0x, p0y) / a;
118
+ const z1 = this.heightAt(p1x, p1y) / a;
119
+ const z2 = this.heightAt(p2x, p2y) / a;
120
+
121
+ // iterate over pixels in bounding box
122
+ let maxError = 0;
123
+ let mx = 0;
124
+ let my = 0;
125
+ let rms = 0;
126
+ for (let y = minY; y <= maxY; y++) {
127
+ // compute starting offset
128
+ let dx = 0;
129
+ if (w00 < 0 && a12 !== 0) {
130
+ dx = Math.max(dx, Math.floor(-w00 / a12));
131
+ }
132
+ if (w01 < 0 && a20 !== 0) {
133
+ dx = Math.max(dx, Math.floor(-w01 / a20));
134
+ }
135
+ if (w02 < 0 && a01 !== 0) {
136
+ dx = Math.max(dx, Math.floor(-w02 / a01));
137
+ }
138
+
139
+ let w0 = w00 + a12 * dx;
140
+ let w1 = w01 + a20 * dx;
141
+ let w2 = w02 + a01 * dx;
142
+
143
+ let wasInside = false;
144
+
145
+ for (let x = minX + dx; x <= maxX; x++) {
146
+ // check if inside triangle
147
+ if (w0 >= 0 && w1 >= 0 && w2 >= 0) {
148
+ wasInside = true;
149
+
150
+ // compute z using barycentric coordinates
151
+ const z = z0 * w0 + z1 * w1 + z2 * w2;
152
+ const dz = Math.abs(z - this.heightAt(x, y));
153
+ rms += dz * dz;
154
+ if (dz > maxError) {
155
+ maxError = dz;
156
+ mx = x;
157
+ my = y;
158
+ }
159
+ } else if (wasInside) {
160
+ break;
161
+ }
162
+
163
+ w0 += a12;
164
+ w1 += a20;
165
+ w2 += a01;
166
+ }
167
+
168
+ w00 += b12;
169
+ w01 += b20;
170
+ w02 += b01;
171
+ }
172
+
173
+ if ((mx === p0x && my === p0y) || (mx === p1x && my === p1y) || (mx === p2x && my === p2y)) {
174
+ maxError = 0;
175
+ }
176
+
177
+ // update triangle metadata
178
+ this._candidates[2 * t] = mx;
179
+ this._candidates[2 * t + 1] = my;
180
+ this._rms[t] = rms;
181
+
182
+ // add triangle to priority queue
183
+ this._queuePush(t, maxError, rms);
184
+ }
185
+
186
+ // process the next triangle in the queue, splitting it with a new point
187
+ _step() {
188
+ // pop triangle with highest error from priority queue
189
+ const t = this._queuePop();
190
+
191
+ const e0 = t * 3 + 0;
192
+ const e1 = t * 3 + 1;
193
+ const e2 = t * 3 + 2;
194
+
195
+ const p0 = this.triangles[e0];
196
+ const p1 = this.triangles[e1];
197
+ const p2 = this.triangles[e2];
198
+
199
+ const ax = this.coords[2 * p0];
200
+ const ay = this.coords[2 * p0 + 1];
201
+ const bx = this.coords[2 * p1];
202
+ const by = this.coords[2 * p1 + 1];
203
+ const cx = this.coords[2 * p2];
204
+ const cy = this.coords[2 * p2 + 1];
205
+ const px = this._candidates[2 * t];
206
+ const py = this._candidates[2 * t + 1];
207
+
208
+ const pn = this._addPoint(px, py);
209
+
210
+ if (orient(ax, ay, bx, by, px, py) === 0) {
211
+ this._handleCollinear(pn, e0);
212
+ } else if (orient(bx, by, cx, cy, px, py) === 0) {
213
+ this._handleCollinear(pn, e1);
214
+ } else if (orient(cx, cy, ax, ay, px, py) === 0) {
215
+ this._handleCollinear(pn, e2);
216
+ } else {
217
+ const h0 = this._halfedges[e0];
218
+ const h1 = this._halfedges[e1];
219
+ const h2 = this._halfedges[e2];
220
+
221
+ const t0 = this._addTriangle(p0, p1, pn, h0, -1, -1, e0);
222
+ const t1 = this._addTriangle(p1, p2, pn, h1, -1, t0 + 1);
223
+ const t2 = this._addTriangle(p2, p0, pn, h2, t0 + 2, t1 + 1);
224
+
225
+ this._legalize(t0);
226
+ this._legalize(t1);
227
+ this._legalize(t2);
228
+ }
229
+ }
230
+
231
+ // add coordinates for a new vertex
232
+ _addPoint(x, y) {
233
+ const i = this.coords.length >> 1;
234
+ this.coords.push(x, y);
235
+ return i;
236
+ }
237
+
238
+ // add or update a triangle in the mesh
239
+ _addTriangle(a, b, c, ab, bc, ca, e = this.triangles.length) {
240
+ const t = e / 3; // new triangle index
241
+
242
+ // add triangle vertices
243
+ this.triangles[e + 0] = a;
244
+ this.triangles[e + 1] = b;
245
+ this.triangles[e + 2] = c;
246
+
247
+ // add triangle halfedges
248
+ this._halfedges[e + 0] = ab;
249
+ this._halfedges[e + 1] = bc;
250
+ this._halfedges[e + 2] = ca;
251
+
252
+ // link neighboring halfedges
253
+ if (ab >= 0) {
254
+ this._halfedges[ab] = e + 0;
255
+ }
256
+ if (bc >= 0) {
257
+ this._halfedges[bc] = e + 1;
258
+ }
259
+ if (ca >= 0) {
260
+ this._halfedges[ca] = e + 2;
261
+ }
262
+
263
+ // init triangle metadata
264
+ this._candidates[2 * t + 0] = 0;
265
+ this._candidates[2 * t + 1] = 0;
266
+ this._queueIndices[t] = -1;
267
+ this._rms[t] = 0;
268
+
269
+ // add triangle to pending queue for later rasterization
270
+ this._pending[this._pendingLen++] = t;
271
+
272
+ // return first halfedge index
273
+ return e;
274
+ }
275
+
276
+ _legalize(a) {
277
+ // if the pair of triangles doesn't satisfy the Delaunay condition
278
+ // (p1 is inside the circumcircle of [p0, pl, pr]), flip them,
279
+ // then do the same check/flip recursively for the new pair of triangles
280
+ //
281
+ // pl pl
282
+ // /||\ / \
283
+ // al/ || \bl al/ \a
284
+ // / || \ / \
285
+ // / a||b \ flip /___ar___\
286
+ // p0\ || /p1 => p0\---bl---/p1
287
+ // \ || / \ /
288
+ // ar\ || /br b\ /br
289
+ // \||/ \ /
290
+ // pr pr
291
+
292
+ const b = this._halfedges[a];
293
+
294
+ if (b < 0) {
295
+ return;
296
+ }
297
+
298
+ const a0 = a - (a % 3);
299
+ const b0 = b - (b % 3);
300
+ const al = a0 + ((a + 1) % 3);
301
+ const ar = a0 + ((a + 2) % 3);
302
+ const bl = b0 + ((b + 2) % 3);
303
+ const br = b0 + ((b + 1) % 3);
304
+ const p0 = this.triangles[ar];
305
+ const pr = this.triangles[a];
306
+ const pl = this.triangles[al];
307
+ const p1 = this.triangles[bl];
308
+ const { coords } = this;
309
+
310
+ if (
311
+ !inCircle(
312
+ coords[2 * p0],
313
+ coords[2 * p0 + 1],
314
+ coords[2 * pr],
315
+ coords[2 * pr + 1],
316
+ coords[2 * pl],
317
+ coords[2 * pl + 1],
318
+ coords[2 * p1],
319
+ coords[2 * p1 + 1],
320
+ )
321
+ ) {
322
+ return;
323
+ }
324
+
325
+ const hal = this._halfedges[al];
326
+ const har = this._halfedges[ar];
327
+ const hbl = this._halfedges[bl];
328
+ const hbr = this._halfedges[br];
329
+
330
+ this._queueRemove(a0 / 3);
331
+ this._queueRemove(b0 / 3);
332
+
333
+ const t0 = this._addTriangle(p0, p1, pl, -1, hbl, hal, a0);
334
+ const t1 = this._addTriangle(p1, p0, pr, t0, har, hbr, b0);
335
+
336
+ this._legalize(t0 + 1);
337
+ this._legalize(t1 + 2);
338
+ }
339
+
340
+ // handle a case where new vertex is on the edge of a triangle
341
+ _handleCollinear(pn, a) {
342
+ const a0 = a - (a % 3);
343
+ const al = a0 + ((a + 1) % 3);
344
+ const ar = a0 + ((a + 2) % 3);
345
+ const p0 = this.triangles[ar];
346
+ const pr = this.triangles[a];
347
+ const pl = this.triangles[al];
348
+ const hal = this._halfedges[al];
349
+ const har = this._halfedges[ar];
350
+
351
+ const b = this._halfedges[a];
352
+
353
+ if (b < 0) {
354
+ const t0 = this._addTriangle(pn, p0, pr, -1, har, -1, a0);
355
+ const t1 = this._addTriangle(p0, pn, pl, t0, -1, hal);
356
+ this._legalize(t0 + 1);
357
+ this._legalize(t1 + 2);
358
+ return;
359
+ }
360
+
361
+ const b0 = b - (b % 3);
362
+ const bl = b0 + ((b + 2) % 3);
363
+ const br = b0 + ((b + 1) % 3);
364
+ const p1 = this.triangles[bl];
365
+ const hbl = this._halfedges[bl];
366
+ const hbr = this._halfedges[br];
367
+
368
+ this._queueRemove(b0 / 3);
369
+
370
+ const t0 = this._addTriangle(p0, pr, pn, har, -1, -1, a0);
371
+ const t1 = this._addTriangle(pr, p1, pn, hbr, -1, t0 + 1, b0);
372
+ const t2 = this._addTriangle(p1, pl, pn, hbl, -1, t1 + 1);
373
+ const t3 = this._addTriangle(pl, p0, pn, hal, t0 + 2, t2 + 1);
374
+
375
+ this._legalize(t0);
376
+ this._legalize(t1);
377
+ this._legalize(t2);
378
+ this._legalize(t3);
379
+ }
380
+
381
+ // priority queue methods
382
+
383
+ _queuePush(t, error, rms) {
384
+ const i = this._queue.length;
385
+ this._queueIndices[t] = i;
386
+ this._queue.push(t);
387
+ this._errors.push(error);
388
+ this._rmsSum += rms;
389
+ this._queueUp(i);
390
+ }
391
+
392
+ _queuePop() {
393
+ const n = this._queue.length - 1;
394
+ this._queueSwap(0, n);
395
+ this._queueDown(0, n);
396
+ return this._queuePopBack();
397
+ }
398
+
399
+ _queuePopBack() {
400
+ const t = this._queue.pop();
401
+ this._errors.pop();
402
+ this._rmsSum -= this._rms[t];
403
+ this._queueIndices[t] = -1;
404
+ return t;
405
+ }
406
+
407
+ _queueRemove(t) {
408
+ const i = this._queueIndices[t];
409
+ if (i < 0) {
410
+ const it = this._pending.indexOf(t);
411
+ if (it !== -1) {
412
+ this._pending[it] = this._pending[--this._pendingLen];
413
+ } else {
414
+ throw new Error('Broken triangulation (something went wrong).');
415
+ }
416
+ return;
417
+ }
418
+ const n = this._queue.length - 1;
419
+ if (n !== i) {
420
+ this._queueSwap(i, n);
421
+ if (!this._queueDown(i, n)) {
422
+ this._queueUp(i);
423
+ }
424
+ }
425
+ this._queuePopBack();
426
+ }
427
+
428
+ _queueLess(i, j) {
429
+ return this._errors[i] > this._errors[j];
430
+ }
431
+
432
+ _queueSwap(i, j) {
433
+ const pi = this._queue[i];
434
+ const pj = this._queue[j];
435
+ this._queue[i] = pj;
436
+ this._queue[j] = pi;
437
+ this._queueIndices[pi] = j;
438
+ this._queueIndices[pj] = i;
439
+ const e = this._errors[i];
440
+ this._errors[i] = this._errors[j];
441
+ this._errors[j] = e;
442
+ }
443
+
444
+ _queueUp(j0) {
445
+ let j = j0;
446
+ while (true) {
447
+ const i = (j - 1) >> 1;
448
+ if (i === j || !this._queueLess(j, i)) {
449
+ break;
450
+ }
451
+ this._queueSwap(i, j);
452
+ j = i;
453
+ }
454
+ }
455
+
456
+ _queueDown(i0, n) {
457
+ let i = i0;
458
+ while (true) {
459
+ const j1 = 2 * i + 1;
460
+ if (j1 >= n || j1 < 0) {
461
+ break;
462
+ }
463
+ const j2 = j1 + 1;
464
+ let j = j1;
465
+ if (j2 < n && this._queueLess(j2, j1)) {
466
+ j = j2;
467
+ }
468
+ if (!this._queueLess(j, i)) {
469
+ break;
470
+ }
471
+ this._queueSwap(i, j);
472
+ i = j;
473
+ }
474
+ return i > i0;
475
+ }
476
+ }
477
+
478
+ function orient(ax, ay, bx, by, cx, cy) {
479
+ return (bx - cx) * (ay - cy) - (by - cy) * (ax - cx);
480
+ }
481
+
482
+ function inCircle(ax, ay, bx, by, cx, cy, px, py) {
483
+ const dx = ax - px;
484
+ const dy = ay - py;
485
+ const ex = bx - px;
486
+ const ey = by - py;
487
+ const fx = cx - px;
488
+ const fy = cy - py;
489
+
490
+ const ap = dx * dx + dy * dy;
491
+ const bp = ex * ex + ey * ey;
492
+ const cp = fx * fx + fy * fy;
493
+
494
+ return dx * (ey * cp - bp * fy) - dy * (ex * cp - bp * fx) + ap * (ex * fy - ey * fx) < 0;
495
+ }