@loaders.gl/terrain 4.0.0-alpha.5 → 4.0.0-alpha.6

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