@loaders.gl/terrain 4.2.0-alpha.3 → 4.2.0-alpha.5

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