@loaders.gl/terrain 4.0.0-alpha.8 → 4.0.0-beta.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.
@@ -1,227 +0,0 @@
1
- "use strict";
2
- // Copyright (C) 2018-2019 HERE Europe B.V.
3
- //
4
- // Permission is hereby granted, free of charge, to any person obtaining a copy
5
- // of this software and associated documentation files (the "Software"), to deal
6
- // in the Software without restriction, including without limitation the rights
7
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- // copies of the Software, and to permit persons to whom the Software is
9
- // furnished to do so, subject to the following conditions:
10
- //
11
- // The above copyright notice and this permission notice shall be included in
12
- // all copies or substantial portions of the Software.
13
- //
14
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
- // SOFTWARE.
21
- Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.DECODING_STEPS = void 0;
23
- const QUANTIZED_MESH_HEADER = new Map([
24
- ['centerX', Float64Array.BYTES_PER_ELEMENT],
25
- ['centerY', Float64Array.BYTES_PER_ELEMENT],
26
- ['centerZ', Float64Array.BYTES_PER_ELEMENT],
27
- ['minHeight', Float32Array.BYTES_PER_ELEMENT],
28
- ['maxHeight', Float32Array.BYTES_PER_ELEMENT],
29
- ['boundingSphereCenterX', Float64Array.BYTES_PER_ELEMENT],
30
- ['boundingSphereCenterY', Float64Array.BYTES_PER_ELEMENT],
31
- ['boundingSphereCenterZ', Float64Array.BYTES_PER_ELEMENT],
32
- ['boundingSphereRadius', Float64Array.BYTES_PER_ELEMENT],
33
- ['horizonOcclusionPointX', Float64Array.BYTES_PER_ELEMENT],
34
- ['horizonOcclusionPointY', Float64Array.BYTES_PER_ELEMENT],
35
- ['horizonOcclusionPointZ', Float64Array.BYTES_PER_ELEMENT]
36
- ]);
37
- function decodeZigZag(value) {
38
- return (value >> 1) ^ -(value & 1);
39
- }
40
- function decodeHeader(dataView) {
41
- let position = 0;
42
- const header = {};
43
- for (const [key, bytesCount] of QUANTIZED_MESH_HEADER) {
44
- const getter = bytesCount === 8 ? dataView.getFloat64 : dataView.getFloat32;
45
- header[key] = getter.call(dataView, position, true);
46
- position += bytesCount;
47
- }
48
- return { header, headerEndPosition: position };
49
- }
50
- function decodeVertexData(dataView, headerEndPosition) {
51
- let position = headerEndPosition;
52
- const elementsPerVertex = 3;
53
- const vertexCount = dataView.getUint32(position, true);
54
- const vertexData = new Uint16Array(vertexCount * elementsPerVertex);
55
- position += Uint32Array.BYTES_PER_ELEMENT;
56
- const bytesPerArrayElement = Uint16Array.BYTES_PER_ELEMENT;
57
- const elementArrayLength = vertexCount * bytesPerArrayElement;
58
- const uArrayStartPosition = position;
59
- const vArrayStartPosition = uArrayStartPosition + elementArrayLength;
60
- const heightArrayStartPosition = vArrayStartPosition + elementArrayLength;
61
- let u = 0;
62
- let v = 0;
63
- let height = 0;
64
- for (let i = 0; i < vertexCount; i++) {
65
- u += decodeZigZag(dataView.getUint16(uArrayStartPosition + bytesPerArrayElement * i, true));
66
- v += decodeZigZag(dataView.getUint16(vArrayStartPosition + bytesPerArrayElement * i, true));
67
- height += decodeZigZag(dataView.getUint16(heightArrayStartPosition + bytesPerArrayElement * i, true));
68
- vertexData[i] = u;
69
- vertexData[i + vertexCount] = v;
70
- vertexData[i + vertexCount * 2] = height;
71
- }
72
- position += elementArrayLength * 3;
73
- return { vertexData, vertexDataEndPosition: position };
74
- }
75
- function decodeIndex(buffer, position, indicesCount, bytesPerIndex, encoded = true) {
76
- let indices;
77
- if (bytesPerIndex === 2) {
78
- indices = new Uint16Array(buffer, position, indicesCount);
79
- }
80
- else {
81
- indices = new Uint32Array(buffer, position, indicesCount);
82
- }
83
- if (!encoded) {
84
- return indices;
85
- }
86
- let highest = 0;
87
- for (let i = 0; i < indices.length; ++i) {
88
- const code = indices[i];
89
- indices[i] = highest - code;
90
- if (code === 0) {
91
- ++highest;
92
- }
93
- }
94
- return indices;
95
- }
96
- function decodeTriangleIndices(dataView, vertexData, vertexDataEndPosition) {
97
- let position = vertexDataEndPosition;
98
- const elementsPerVertex = 3;
99
- const vertexCount = vertexData.length / elementsPerVertex;
100
- const bytesPerIndex = vertexCount > 65536 ? Uint32Array.BYTES_PER_ELEMENT : Uint16Array.BYTES_PER_ELEMENT;
101
- if (position % bytesPerIndex !== 0) {
102
- position += bytesPerIndex - (position % bytesPerIndex);
103
- }
104
- const triangleCount = dataView.getUint32(position, true);
105
- position += Uint32Array.BYTES_PER_ELEMENT;
106
- const triangleIndicesCount = triangleCount * 3;
107
- const triangleIndices = decodeIndex(dataView.buffer, position, triangleIndicesCount, bytesPerIndex);
108
- position += triangleIndicesCount * bytesPerIndex;
109
- return {
110
- triangleIndicesEndPosition: position,
111
- triangleIndices
112
- };
113
- }
114
- function decodeEdgeIndices(dataView, vertexData, triangleIndicesEndPosition) {
115
- let position = triangleIndicesEndPosition;
116
- const elementsPerVertex = 3;
117
- const vertexCount = vertexData.length / elementsPerVertex;
118
- const bytesPerIndex = vertexCount > 65536 ? Uint32Array.BYTES_PER_ELEMENT : Uint16Array.BYTES_PER_ELEMENT;
119
- const westVertexCount = dataView.getUint32(position, true);
120
- position += Uint32Array.BYTES_PER_ELEMENT;
121
- const westIndices = decodeIndex(dataView.buffer, position, westVertexCount, bytesPerIndex, false);
122
- position += westVertexCount * bytesPerIndex;
123
- const southVertexCount = dataView.getUint32(position, true);
124
- position += Uint32Array.BYTES_PER_ELEMENT;
125
- const southIndices = decodeIndex(dataView.buffer, position, southVertexCount, bytesPerIndex, false);
126
- position += southVertexCount * bytesPerIndex;
127
- const eastVertexCount = dataView.getUint32(position, true);
128
- position += Uint32Array.BYTES_PER_ELEMENT;
129
- const eastIndices = decodeIndex(dataView.buffer, position, eastVertexCount, bytesPerIndex, false);
130
- position += eastVertexCount * bytesPerIndex;
131
- const northVertexCount = dataView.getUint32(position, true);
132
- position += Uint32Array.BYTES_PER_ELEMENT;
133
- const northIndices = decodeIndex(dataView.buffer, position, northVertexCount, bytesPerIndex, false);
134
- position += northVertexCount * bytesPerIndex;
135
- return {
136
- edgeIndicesEndPosition: position,
137
- westIndices,
138
- southIndices,
139
- eastIndices,
140
- northIndices
141
- };
142
- }
143
- function decodeVertexNormalsExtension(extensionDataView) {
144
- return new Uint8Array(extensionDataView.buffer, extensionDataView.byteOffset, extensionDataView.byteLength);
145
- }
146
- function decodeWaterMaskExtension(extensionDataView) {
147
- return extensionDataView.buffer.slice(extensionDataView.byteOffset, extensionDataView.byteOffset + extensionDataView.byteLength);
148
- }
149
- function decodeExtensions(dataView, indicesEndPosition) {
150
- const extensions = {};
151
- if (dataView.byteLength <= indicesEndPosition) {
152
- return { extensions, extensionsEndPosition: indicesEndPosition };
153
- }
154
- let position = indicesEndPosition;
155
- while (position < dataView.byteLength) {
156
- const extensionId = dataView.getUint8(position, true);
157
- position += Uint8Array.BYTES_PER_ELEMENT;
158
- const extensionLength = dataView.getUint32(position, true);
159
- position += Uint32Array.BYTES_PER_ELEMENT;
160
- const extensionView = new DataView(dataView.buffer, position, extensionLength);
161
- switch (extensionId) {
162
- case 1: {
163
- extensions.vertexNormals = decodeVertexNormalsExtension(extensionView);
164
- break;
165
- }
166
- case 2: {
167
- extensions.waterMask = decodeWaterMaskExtension(extensionView);
168
- break;
169
- }
170
- default: {
171
- // console.warn(`Unknown extension with id ${extensionId}`)
172
- }
173
- }
174
- position += extensionLength;
175
- }
176
- return { extensions, extensionsEndPosition: position };
177
- }
178
- exports.DECODING_STEPS = {
179
- header: 0,
180
- vertices: 1,
181
- triangleIndices: 2,
182
- edgeIndices: 3,
183
- extensions: 4
184
- };
185
- const DEFAULT_OPTIONS = {
186
- maxDecodingStep: exports.DECODING_STEPS.extensions
187
- };
188
- function decode(data, userOptions) {
189
- const options = Object.assign({}, DEFAULT_OPTIONS, userOptions);
190
- const view = new DataView(data);
191
- const { header, headerEndPosition } = decodeHeader(view);
192
- if (options.maxDecodingStep < exports.DECODING_STEPS.vertices) {
193
- return { header };
194
- }
195
- const { vertexData, vertexDataEndPosition } = decodeVertexData(view, headerEndPosition);
196
- if (options.maxDecodingStep < exports.DECODING_STEPS.triangleIndices) {
197
- return { header, vertexData };
198
- }
199
- const { triangleIndices, triangleIndicesEndPosition } = decodeTriangleIndices(view, vertexData, vertexDataEndPosition);
200
- if (options.maxDecodingStep < exports.DECODING_STEPS.edgeIndices) {
201
- return { header, vertexData, triangleIndices };
202
- }
203
- const { westIndices, southIndices, eastIndices, northIndices, edgeIndicesEndPosition } = decodeEdgeIndices(view, vertexData, triangleIndicesEndPosition);
204
- if (options.maxDecodingStep < exports.DECODING_STEPS.extensions) {
205
- return {
206
- header,
207
- vertexData,
208
- triangleIndices,
209
- westIndices,
210
- northIndices,
211
- eastIndices,
212
- southIndices
213
- };
214
- }
215
- const { extensions } = decodeExtensions(view, edgeIndicesEndPosition);
216
- return {
217
- header,
218
- vertexData,
219
- triangleIndices,
220
- westIndices,
221
- northIndices,
222
- eastIndices,
223
- southIndices,
224
- extensions
225
- };
226
- }
227
- exports.default = decode;
@@ -1,418 +0,0 @@
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();
44
- }
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();
49
- }
50
- }
51
- // refine the mesh with a single point
52
- refine() {
53
- this._step();
54
- this._flush();
55
- }
56
- // max error of the current mesh
57
- getMaxError() {
58
- return this._errors[0];
59
- }
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;
63
- }
64
- // height value at a given position
65
- heightAt(x, y) {
66
- return this.data[this.width * y + x];
67
- }
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;
80
- }
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);
158
- }
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
- }
198
- }
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;
204
- }
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;
235
- }
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;
321
- this._queueUp(i);
322
- }
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;
370
- }
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;
401
- }
402
- }
403
- exports.default = Delatin;
404
- function orient(ax, ay, bx, by, cx, cy) {
405
- return (bx - cx) * (ay - cy) - (by - cy) * (ax - cx);
406
- }
407
- function inCircle(ax, ay, bx, by, cx, cy, px, py) {
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;
418
- }