@loaders.gl/terrain 3.1.3 → 4.0.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.
- package/dist/bundle.js +2 -2
- package/dist/bundle.js.map +1 -0
- package/dist/index.js +14 -24
- package/dist/index.js.map +1 -0
- package/dist/lib/decode-quantized-mesh.js +241 -200
- package/dist/lib/decode-quantized-mesh.js.map +1 -0
- package/dist/lib/delatin/index.js +443 -397
- package/dist/lib/delatin/index.js.map +1 -0
- package/dist/lib/helpers/skirt.js +91 -119
- package/dist/lib/helpers/skirt.js.map +1 -0
- package/dist/lib/parse-quantized-mesh.js +90 -89
- package/dist/lib/parse-quantized-mesh.js.map +1 -0
- package/dist/lib/parse-terrain.js +166 -148
- package/dist/lib/parse-terrain.js.map +1 -0
- package/dist/lib/utils/version.js +2 -7
- package/dist/lib/utils/version.js.map +1 -0
- package/dist/quantized-mesh-loader.js +16 -21
- package/dist/quantized-mesh-loader.js.map +1 -0
- package/dist/quantized-mesh-worker.js +1 -1
- package/dist/terrain-loader.js +24 -32
- package/dist/terrain-loader.js.map +1 -0
- package/dist/terrain-worker.js +1 -1
- package/dist/workers/quantized-mesh-worker.js +4 -5
- package/dist/workers/quantized-mesh-worker.js.map +1 -0
- package/dist/workers/terrain-worker.js +4 -5
- package/dist/workers/terrain-worker.js.map +1 -0
- package/package.json +8 -8
- package/dist/es5/bundle.js +0 -7
- package/dist/es5/bundle.js.map +0 -1
- package/dist/es5/index.js +0 -77
- package/dist/es5/index.js.map +0 -1
- package/dist/es5/lib/decode-quantized-mesh.js +0 -299
- package/dist/es5/lib/decode-quantized-mesh.js.map +0 -1
- package/dist/es5/lib/delatin/index.js +0 -506
- package/dist/es5/lib/delatin/index.js.map +0 -1
- package/dist/es5/lib/helpers/skirt.js +0 -120
- package/dist/es5/lib/helpers/skirt.js.map +0 -1
- package/dist/es5/lib/parse-quantized-mesh.js +0 -119
- package/dist/es5/lib/parse-quantized-mesh.js.map +0 -1
- package/dist/es5/lib/parse-terrain.js +0 -219
- package/dist/es5/lib/parse-terrain.js.map +0 -1
- package/dist/es5/lib/utils/version.js +0 -9
- package/dist/es5/lib/utils/version.js.map +0 -1
- package/dist/es5/quantized-mesh-loader.js +0 -28
- package/dist/es5/quantized-mesh-loader.js.map +0 -1
- package/dist/es5/terrain-loader.js +0 -36
- package/dist/es5/terrain-loader.js.map +0 -1
- package/dist/es5/workers/quantized-mesh-worker.js +0 -8
- package/dist/es5/workers/quantized-mesh-worker.js.map +0 -1
- package/dist/es5/workers/terrain-worker.js +0 -8
- package/dist/es5/workers/terrain-worker.js.map +0 -1
- package/dist/esm/bundle.js +0 -5
- package/dist/esm/bundle.js.map +0 -1
- package/dist/esm/index.js +0 -16
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/lib/decode-quantized-mesh.js +0 -268
- package/dist/esm/lib/decode-quantized-mesh.js.map +0 -1
- package/dist/esm/lib/delatin/index.js +0 -464
- package/dist/esm/lib/delatin/index.js.map +0 -1
- package/dist/esm/lib/helpers/skirt.js +0 -99
- package/dist/esm/lib/helpers/skirt.js.map +0 -1
- package/dist/esm/lib/parse-quantized-mesh.js +0 -96
- package/dist/esm/lib/parse-quantized-mesh.js.map +0 -1
- package/dist/esm/lib/parse-terrain.js +0 -180
- package/dist/esm/lib/parse-terrain.js.map +0 -1
- package/dist/esm/lib/utils/version.js +0 -2
- package/dist/esm/lib/utils/version.js.map +0 -1
- package/dist/esm/quantized-mesh-loader.js +0 -18
- package/dist/esm/quantized-mesh-loader.js.map +0 -1
- package/dist/esm/terrain-loader.js +0 -26
- package/dist/esm/terrain-loader.js.map +0 -1
- package/dist/esm/workers/quantized-mesh-worker.js +0 -4
- package/dist/esm/workers/quantized-mesh-worker.js.map +0 -1
- package/dist/esm/workers/terrain-worker.js +0 -4
- package/dist/esm/workers/terrain-worker.js.map +0 -1
|
@@ -1,418 +1,464 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
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();
|
|
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();
|
|
44
38
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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);
|
|
63
69
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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;
|
|
78
131
|
}
|
|
79
|
-
|
|
132
|
+
|
|
133
|
+
w0 += a12;
|
|
134
|
+
w1 += a20;
|
|
135
|
+
w2 += a01;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
w00 += b12;
|
|
139
|
+
w01 += b20;
|
|
140
|
+
w02 += b01;
|
|
80
141
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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);
|
|
142
|
+
|
|
143
|
+
if (mx === p0x && my === p0y || mx === p1x && my === p1y || mx === p2x && my === p2y) {
|
|
144
|
+
maxError = 0;
|
|
158
145
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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);
|
|
198
196
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
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;
|
|
204
216
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
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);
|
|
217
|
+
|
|
218
|
+
if (bc >= 0) {
|
|
219
|
+
this._halfedges[bc] = e + 1;
|
|
313
220
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
this._queueIndices[t] = i;
|
|
318
|
-
this._queue.push(t);
|
|
319
|
-
this._errors.push(error);
|
|
320
|
-
this._rmsSum += rms;
|
|
321
|
-
this._queueUp(i);
|
|
221
|
+
|
|
222
|
+
if (ca >= 0) {
|
|
223
|
+
this._halfedges[ca] = e + 2;
|
|
322
224
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
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;
|
|
328
239
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
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;
|
|
335
255
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
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;
|
|
356
296
|
}
|
|
357
|
-
|
|
358
|
-
|
|
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;
|
|
359
370
|
}
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
this._errors[j] = e;
|
|
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)) {
|
|
378
|
+
this._queueUp(i);
|
|
379
|
+
}
|
|
370
380
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
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;
|
|
381
414
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
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;
|
|
401
441
|
}
|
|
442
|
+
|
|
443
|
+
return i > i0;
|
|
444
|
+
}
|
|
445
|
+
|
|
402
446
|
}
|
|
403
|
-
|
|
447
|
+
|
|
404
448
|
function orient(ax, ay, bx, by, cx, cy) {
|
|
405
|
-
|
|
449
|
+
return (bx - cx) * (ay - cy) - (by - cy) * (ax - cx);
|
|
406
450
|
}
|
|
451
|
+
|
|
407
452
|
function inCircle(ax, ay, bx, by, cx, cy, px, py) {
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
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;
|
|
418
463
|
}
|
|
464
|
+
//# sourceMappingURL=index.js.map
|