@maplat/tin 0.8.2 → 0.9.2
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/es5/index.js +1 -1
- package/es5/index.js.LICENSE.txt +10 -1
- package/es5/index.js.map +1 -1
- package/lib/constrained-tin.js +12 -610
- package/lib/constrained-tin.js.map +1 -1
- package/lib/index.js.map +1 -1
- package/package.json +38 -34
package/lib/constrained-tin.js
CHANGED
|
@@ -1,568 +1,5 @@
|
|
|
1
1
|
import { polygon, featureCollection } from "@turf/helpers";
|
|
2
|
-
|
|
3
|
-
constructor(x, y) {
|
|
4
|
-
this.x = x;
|
|
5
|
-
this.y = y;
|
|
6
|
-
}
|
|
7
|
-
dot(p1) {
|
|
8
|
-
return this.x * p1.x + this.y * p1.y;
|
|
9
|
-
}
|
|
10
|
-
add(p1) {
|
|
11
|
-
return new Point(this.x + p1.x, this.y + p1.y);
|
|
12
|
-
}
|
|
13
|
-
sub(p1) {
|
|
14
|
-
return new Point(this.x - p1.x, this.y - p1.y);
|
|
15
|
-
}
|
|
16
|
-
scale(s) {
|
|
17
|
-
return new Point(this.x * s, this.y * s);
|
|
18
|
-
}
|
|
19
|
-
sqDistanceTo(p1) {
|
|
20
|
-
return ((this.x - p1.x) * (this.x - p1.x) + (this.y - p1.y) * (this.y - p1.y));
|
|
21
|
-
}
|
|
22
|
-
toStr() {
|
|
23
|
-
return `(${this.x.toFixed(3)}, ${this.y.toFixed(3)})`;
|
|
24
|
-
}
|
|
25
|
-
copyFrom(p) {
|
|
26
|
-
this.x = p.x;
|
|
27
|
-
this.y = p.y;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
function cross(vec0, vec1) {
|
|
31
|
-
return vec0.x * vec1.y - vec0.y * vec1.x;
|
|
32
|
-
}
|
|
33
|
-
function getPointOrientation(edge, p) {
|
|
34
|
-
const vec_edge01 = edge[1].sub(edge[0]);
|
|
35
|
-
const vec_edge0_to_p = p.sub(edge[0]);
|
|
36
|
-
return cross(vec_edge01, vec_edge0_to_p);
|
|
37
|
-
}
|
|
38
|
-
const fieldOrigin = new Point(0.0, 0.0);
|
|
39
|
-
const fieldSize = 1.0;
|
|
40
|
-
const boundingTriangleSize = 1000;
|
|
41
|
-
function binSorter(a, b) {
|
|
42
|
-
if (a.bin == b.bin) {
|
|
43
|
-
return 0;
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
return a.bin < b.bin ? -1 : 1;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
function isQuadConvex(p0, p1, p2, p3) {
|
|
50
|
-
const diag0 = [p0, p2];
|
|
51
|
-
const diag1 = [p1, p3];
|
|
52
|
-
return isEdgeIntersecting(diag0, diag1);
|
|
53
|
-
}
|
|
54
|
-
function isSameEdge(edge0, edge1) {
|
|
55
|
-
return ((edge0[0] == edge1[0] && edge0[1] == edge1[1]) ||
|
|
56
|
-
(edge0[1] == edge1[0] && edge0[0] == edge1[1]));
|
|
57
|
-
}
|
|
58
|
-
function isEdgeIntersecting(edgeA, edgeB) {
|
|
59
|
-
const vecA0A1 = edgeA[1].sub(edgeA[0]);
|
|
60
|
-
const vecA0B0 = edgeB[0].sub(edgeA[0]);
|
|
61
|
-
const vecA0B1 = edgeB[1].sub(edgeA[0]);
|
|
62
|
-
const AxB0 = cross(vecA0A1, vecA0B0);
|
|
63
|
-
const AxB1 = cross(vecA0A1, vecA0B1);
|
|
64
|
-
if ((AxB0 > 0 && AxB1 > 0) || (AxB0 < 0 && AxB1 < 0))
|
|
65
|
-
return false;
|
|
66
|
-
const vecB0B1 = edgeB[1].sub(edgeB[0]);
|
|
67
|
-
const vecB0A0 = edgeA[0].sub(edgeB[0]);
|
|
68
|
-
const vecB0A1 = edgeA[1].sub(edgeB[0]);
|
|
69
|
-
const BxA0 = cross(vecB0B1, vecB0A0);
|
|
70
|
-
const BxA1 = cross(vecB0B1, vecB0A1);
|
|
71
|
-
if ((BxA0 > 0 && BxA1 > 0) || (BxA0 < 0 && BxA1 < 0))
|
|
72
|
-
return false;
|
|
73
|
-
if (Math.abs(AxB0) < 1e-14 && Math.abs(AxB1) < 1e-14) {
|
|
74
|
-
if (Math.max(edgeB[0].x, edgeB[1].x) < Math.min(edgeA[0].x, edgeA[1].x) ||
|
|
75
|
-
Math.min(edgeB[0].x, edgeB[1].x) > Math.max(edgeA[0].x, edgeA[1].x))
|
|
76
|
-
return false;
|
|
77
|
-
if (Math.max(edgeB[0].y, edgeB[1].y) < Math.min(edgeA[0].y, edgeA[1].y) ||
|
|
78
|
-
Math.min(edgeB[0].y, edgeB[1].y) > Math.max(edgeA[0].y, edgeA[1].y))
|
|
79
|
-
return false;
|
|
80
|
-
}
|
|
81
|
-
return true;
|
|
82
|
-
}
|
|
83
|
-
function setupDelaunay(meshData) {
|
|
84
|
-
const nVertex = meshData.vert.length;
|
|
85
|
-
const nBinsX = Math.round(Math.pow(nVertex, 0.25));
|
|
86
|
-
const scaledverts = [];
|
|
87
|
-
const bin_index = [];
|
|
88
|
-
for (let i = 0; i < nVertex; i++) {
|
|
89
|
-
const scaled_x = (meshData.vert[i].x - fieldOrigin.x) / fieldSize;
|
|
90
|
-
const scaled_y = (meshData.vert[i].y - fieldOrigin.y) / fieldSize;
|
|
91
|
-
scaledverts.push(new Point(scaled_x, scaled_y));
|
|
92
|
-
const ind_i = Math.round((nBinsX - 1) * scaled_x);
|
|
93
|
-
const ind_j = Math.round((nBinsX - 1) * scaled_y);
|
|
94
|
-
let bin_id;
|
|
95
|
-
if (ind_j % 2 === 0) {
|
|
96
|
-
bin_id = ind_j * nBinsX + ind_i;
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
bin_id = (ind_j + 1) * nBinsX - ind_i - 1;
|
|
100
|
-
}
|
|
101
|
-
bin_index.push({ ind: i, bin: bin_id });
|
|
102
|
-
}
|
|
103
|
-
const D = boundingTriangleSize;
|
|
104
|
-
scaledverts.push(new Point(-D + 0.5, -D / Math.sqrt(3) + 0.5));
|
|
105
|
-
scaledverts.push(new Point(D + 0.5, -D / Math.sqrt(3) + 0.5));
|
|
106
|
-
scaledverts.push(new Point(0.5, (2 * D) / Math.sqrt(3) + 0.5));
|
|
107
|
-
for (let i = nVertex; i < nVertex + 3; i++)
|
|
108
|
-
meshData.vert.push(new Point(fieldSize * scaledverts[i].x + fieldOrigin.x, fieldSize * scaledverts[i].y + fieldOrigin.y));
|
|
109
|
-
bin_index.sort(binSorter);
|
|
110
|
-
meshData.scaled_vert = scaledverts;
|
|
111
|
-
meshData.bin = bin_index;
|
|
112
|
-
meshData.tri = [[nVertex, nVertex + 1, nVertex + 2]];
|
|
113
|
-
meshData.adj = [[-1, -1, -1]];
|
|
114
|
-
meshData.vert_to_tri = [];
|
|
115
|
-
}
|
|
116
|
-
function delaunay(meshData) {
|
|
117
|
-
setupDelaunay(meshData);
|
|
118
|
-
const verts = meshData.scaled_vert;
|
|
119
|
-
const bins = meshData.bin;
|
|
120
|
-
const triangles = meshData.tri;
|
|
121
|
-
const adjacency = meshData.adj;
|
|
122
|
-
const N = verts.length - 3;
|
|
123
|
-
let ind_tri = 0;
|
|
124
|
-
for (let i = 0; i < N; i++) {
|
|
125
|
-
const new_i = bins[i].ind;
|
|
126
|
-
const res = findEnclosingTriangle(verts[new_i], meshData, ind_tri);
|
|
127
|
-
ind_tri = res[0];
|
|
128
|
-
if (ind_tri === -1)
|
|
129
|
-
throw "Could not find a triangle containing the new vertex!";
|
|
130
|
-
const cur_tri = triangles[ind_tri];
|
|
131
|
-
const new_tri0 = [cur_tri[0], cur_tri[1], new_i];
|
|
132
|
-
const new_tri1 = [new_i, cur_tri[1], cur_tri[2]];
|
|
133
|
-
const new_tri2 = [cur_tri[0], new_i, cur_tri[2]];
|
|
134
|
-
triangles[ind_tri] = new_tri0;
|
|
135
|
-
const N_tri = triangles.length;
|
|
136
|
-
const cur_tri_adj = adjacency[ind_tri];
|
|
137
|
-
adjacency[ind_tri] = [N_tri, N_tri + 1, cur_tri_adj[2]];
|
|
138
|
-
triangles.push(new_tri1);
|
|
139
|
-
triangles.push(new_tri2);
|
|
140
|
-
adjacency.push([cur_tri_adj[0], N_tri + 1, ind_tri]);
|
|
141
|
-
adjacency.push([N_tri, cur_tri_adj[1], ind_tri]);
|
|
142
|
-
const stack = [];
|
|
143
|
-
if (cur_tri_adj[2] >= 0) {
|
|
144
|
-
const neigh_adj_ind = adjacency[cur_tri_adj[2]].indexOf(ind_tri);
|
|
145
|
-
stack.push([cur_tri_adj[2], neigh_adj_ind]);
|
|
146
|
-
}
|
|
147
|
-
if (cur_tri_adj[0] >= 0) {
|
|
148
|
-
const neigh_adj_ind = adjacency[cur_tri_adj[0]].indexOf(ind_tri);
|
|
149
|
-
adjacency[cur_tri_adj[0]][neigh_adj_ind] = N_tri;
|
|
150
|
-
stack.push([cur_tri_adj[0], neigh_adj_ind]);
|
|
151
|
-
}
|
|
152
|
-
if (cur_tri_adj[1] >= 0) {
|
|
153
|
-
const neigh_adj_ind = adjacency[cur_tri_adj[1]].indexOf(ind_tri);
|
|
154
|
-
adjacency[cur_tri_adj[1]][neigh_adj_ind] = N_tri + 1;
|
|
155
|
-
stack.push([cur_tri_adj[1], neigh_adj_ind]);
|
|
156
|
-
}
|
|
157
|
-
restoreDelaunay(new_i, meshData, stack);
|
|
158
|
-
}
|
|
159
|
-
removeBoundaryTriangles(meshData);
|
|
160
|
-
}
|
|
161
|
-
function findEnclosingTriangle(target_vertex, meshData, ind_tri_cur) {
|
|
162
|
-
const vertices = meshData.scaled_vert;
|
|
163
|
-
const triangles = meshData.tri;
|
|
164
|
-
const adjacency = meshData.adj;
|
|
165
|
-
const max_hops = Math.max(10, adjacency.length);
|
|
166
|
-
let nhops = 0;
|
|
167
|
-
const found_tri = false;
|
|
168
|
-
const path = [];
|
|
169
|
-
while (!found_tri && nhops < max_hops) {
|
|
170
|
-
if (ind_tri_cur === -1)
|
|
171
|
-
return [ind_tri_cur, nhops];
|
|
172
|
-
const tri_cur = triangles[ind_tri_cur];
|
|
173
|
-
const orients = [
|
|
174
|
-
getPointOrientation([vertices[tri_cur[1]], vertices[tri_cur[2]]], target_vertex),
|
|
175
|
-
getPointOrientation([vertices[tri_cur[2]], vertices[tri_cur[0]]], target_vertex),
|
|
176
|
-
getPointOrientation([vertices[tri_cur[0]], vertices[tri_cur[1]]], target_vertex)
|
|
177
|
-
];
|
|
178
|
-
if (orients[0] >= 0 && orients[1] >= 0 && orients[2] >= 0)
|
|
179
|
-
return [ind_tri_cur, nhops];
|
|
180
|
-
let base_ind = -1;
|
|
181
|
-
for (let iedge = 0; iedge < 3; iedge++) {
|
|
182
|
-
if (orients[iedge] >= 0) {
|
|
183
|
-
base_ind = iedge;
|
|
184
|
-
break;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
const base_p1_ind = (base_ind + 1) % 3;
|
|
188
|
-
const base_p2_ind = (base_ind + 2) % 3;
|
|
189
|
-
if (orients[base_p1_ind] >= 0 && orients[base_p2_ind] < 0) {
|
|
190
|
-
ind_tri_cur = adjacency[ind_tri_cur][base_p2_ind];
|
|
191
|
-
path[nhops] = vertices[tri_cur[base_ind]]
|
|
192
|
-
.add(vertices[tri_cur[base_p1_ind]])
|
|
193
|
-
.scale(0.5);
|
|
194
|
-
}
|
|
195
|
-
else if (orients[base_p1_ind] < 0 && orients[base_p2_ind] >= 0) {
|
|
196
|
-
ind_tri_cur = adjacency[ind_tri_cur][base_p1_ind];
|
|
197
|
-
path[nhops] = vertices[tri_cur[base_p2_ind]]
|
|
198
|
-
.add(vertices[tri_cur[base_ind]])
|
|
199
|
-
.scale(0.5);
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
const vec0 = vertices[tri_cur[base_p1_ind]].sub(vertices[tri_cur[base_ind]]);
|
|
203
|
-
const vec1 = target_vertex.sub(vertices[tri_cur[base_ind]]);
|
|
204
|
-
if (vec0.dot(vec1) > 0) {
|
|
205
|
-
ind_tri_cur = adjacency[ind_tri_cur][base_p2_ind];
|
|
206
|
-
path[nhops] = vertices[tri_cur[base_ind]]
|
|
207
|
-
.add(vertices[tri_cur[base_p1_ind]])
|
|
208
|
-
.scale(0.5);
|
|
209
|
-
}
|
|
210
|
-
else {
|
|
211
|
-
ind_tri_cur = adjacency[ind_tri_cur][base_p1_ind];
|
|
212
|
-
path[nhops] = vertices[tri_cur[base_p2_ind]]
|
|
213
|
-
.add(vertices[tri_cur[base_ind]])
|
|
214
|
-
.scale(0.5);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
nhops++;
|
|
218
|
-
}
|
|
219
|
-
if (!found_tri) {
|
|
220
|
-
throw `Could not locate the triangle that encloses (${target_vertex.x}, ${target_vertex.y})!`;
|
|
221
|
-
}
|
|
222
|
-
return [ind_tri_cur, nhops - 1];
|
|
223
|
-
}
|
|
224
|
-
function restoreDelaunay(ind_vert, meshData, stack) {
|
|
225
|
-
const vertices = meshData.scaled_vert;
|
|
226
|
-
const triangles = meshData.tri;
|
|
227
|
-
const adjacency = meshData.adj;
|
|
228
|
-
const v_new = vertices[ind_vert];
|
|
229
|
-
while (stack.length > 0) {
|
|
230
|
-
const ind_tri_pair = stack.pop();
|
|
231
|
-
const ind_tri = ind_tri_pair[0];
|
|
232
|
-
const ind_tri_vert = triangles[ind_tri];
|
|
233
|
-
const v_tri = [];
|
|
234
|
-
for (let i = 0; i < 3; i++)
|
|
235
|
-
v_tri[i] = vertices[ind_tri_vert[i]];
|
|
236
|
-
if (!isDelaunay2(v_tri, v_new)) {
|
|
237
|
-
const outernode_tri = ind_tri_pair[1];
|
|
238
|
-
const ind_tri_neigh = adjacency[ind_tri][outernode_tri];
|
|
239
|
-
if (ind_tri_neigh < 0)
|
|
240
|
-
throw "negative index";
|
|
241
|
-
swapDiagonal(meshData, ind_tri, ind_tri_neigh);
|
|
242
|
-
const new_node_ind_tri = triangles[ind_tri].indexOf(ind_vert);
|
|
243
|
-
const ind_tri_outerp2 = adjacency[ind_tri][new_node_ind_tri];
|
|
244
|
-
if (ind_tri_outerp2 >= 0) {
|
|
245
|
-
const neigh_node = adjacency[ind_tri_outerp2].indexOf(ind_tri);
|
|
246
|
-
stack.push([ind_tri_outerp2, neigh_node]);
|
|
247
|
-
}
|
|
248
|
-
const new_node_ind_tri_neigh = triangles[ind_tri_neigh].indexOf(ind_vert);
|
|
249
|
-
const ind_tri_neigh_outer = adjacency[ind_tri_neigh][new_node_ind_tri_neigh];
|
|
250
|
-
if (ind_tri_neigh_outer >= 0) {
|
|
251
|
-
const neigh_node = adjacency[ind_tri_neigh_outer].indexOf(ind_tri_neigh);
|
|
252
|
-
stack.push([ind_tri_neigh_outer, neigh_node]);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
function swapDiagonal(meshData, ind_triA, ind_triB) {
|
|
258
|
-
const triangles = meshData.tri;
|
|
259
|
-
const adjacency = meshData.adj;
|
|
260
|
-
const vert2tri = meshData.vert_to_tri;
|
|
261
|
-
const outernode_triA = adjacency[ind_triA].indexOf(ind_triB);
|
|
262
|
-
const outernode_triB = adjacency[ind_triB].indexOf(ind_triA);
|
|
263
|
-
const outernode_triA_p1 = (outernode_triA + 1) % 3;
|
|
264
|
-
const outernode_triA_p2 = (outernode_triA + 2) % 3;
|
|
265
|
-
const outernode_triB_p1 = (outernode_triB + 1) % 3;
|
|
266
|
-
const outernode_triB_p2 = (outernode_triB + 2) % 3;
|
|
267
|
-
triangles[ind_triA][outernode_triA_p2] = triangles[ind_triB][outernode_triB];
|
|
268
|
-
triangles[ind_triB][outernode_triB_p2] = triangles[ind_triA][outernode_triA];
|
|
269
|
-
adjacency[ind_triA][outernode_triA] = adjacency[ind_triB][outernode_triB_p1];
|
|
270
|
-
adjacency[ind_triB][outernode_triB] = adjacency[ind_triA][outernode_triA_p1];
|
|
271
|
-
const ind_triA_neigh_outerp1 = adjacency[ind_triA][outernode_triA_p1];
|
|
272
|
-
if (ind_triA_neigh_outerp1 >= 0) {
|
|
273
|
-
const neigh_node = adjacency[ind_triA_neigh_outerp1].indexOf(ind_triA);
|
|
274
|
-
adjacency[ind_triA_neigh_outerp1][neigh_node] = ind_triB;
|
|
275
|
-
}
|
|
276
|
-
const ind_triB_neigh_outerp1 = adjacency[ind_triB][outernode_triB_p1];
|
|
277
|
-
if (ind_triB_neigh_outerp1 >= 0) {
|
|
278
|
-
const neigh_node = adjacency[ind_triB_neigh_outerp1].indexOf(ind_triB);
|
|
279
|
-
adjacency[ind_triB_neigh_outerp1][neigh_node] = ind_triA;
|
|
280
|
-
}
|
|
281
|
-
adjacency[ind_triA][outernode_triA_p1] = ind_triB;
|
|
282
|
-
adjacency[ind_triB][outernode_triB_p1] = ind_triA;
|
|
283
|
-
if (vert2tri.length > 0) {
|
|
284
|
-
vert2tri[triangles[ind_triA][outernode_triA]].push(ind_triB);
|
|
285
|
-
vert2tri[triangles[ind_triB][outernode_triB]].push(ind_triA);
|
|
286
|
-
let local_ind = vert2tri[triangles[ind_triA][outernode_triA_p1]].indexOf(ind_triB);
|
|
287
|
-
vert2tri[triangles[ind_triA][outernode_triA_p1]].splice(local_ind, 1);
|
|
288
|
-
local_ind = vert2tri[triangles[ind_triB][outernode_triB_p1]].indexOf(ind_triA);
|
|
289
|
-
vert2tri[triangles[ind_triB][outernode_triB_p1]].splice(local_ind, 1);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
function removeBoundaryTriangles(meshData) {
|
|
293
|
-
const verts = meshData.scaled_vert;
|
|
294
|
-
const triangles = meshData.tri;
|
|
295
|
-
const adjacency = meshData.adj;
|
|
296
|
-
const N = verts.length - 3;
|
|
297
|
-
let del_count = 0;
|
|
298
|
-
const indmap = [];
|
|
299
|
-
for (let i = 0; i < triangles.length; i++) {
|
|
300
|
-
const prev_del_count = del_count;
|
|
301
|
-
for (let j = i; j < triangles.length; j++) {
|
|
302
|
-
if (triangles[j][0] < N && triangles[j][1] < N && triangles[j][2] < N) {
|
|
303
|
-
indmap[i + del_count] = i;
|
|
304
|
-
break;
|
|
305
|
-
}
|
|
306
|
-
else {
|
|
307
|
-
indmap[i + del_count] = -1;
|
|
308
|
-
del_count++;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
const del_length = del_count - prev_del_count;
|
|
312
|
-
if (del_length > 0) {
|
|
313
|
-
triangles.splice(i, del_length);
|
|
314
|
-
adjacency.splice(i, del_length);
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
for (let i = 0; i < adjacency.length; i++)
|
|
318
|
-
for (let j = 0; j < 3; j++)
|
|
319
|
-
adjacency[i][j] = indmap[adjacency[i][j]];
|
|
320
|
-
meshData.scaled_vert.splice(-3, 3);
|
|
321
|
-
meshData.vert.splice(-3, 3);
|
|
322
|
-
}
|
|
323
|
-
function isDelaunay2(v_tri, p) {
|
|
324
|
-
const vecp0 = v_tri[0].sub(p);
|
|
325
|
-
const vecp1 = v_tri[1].sub(p);
|
|
326
|
-
const vecp2 = v_tri[2].sub(p);
|
|
327
|
-
const p0_sq = vecp0.x * vecp0.x + vecp0.y * vecp0.y;
|
|
328
|
-
const p1_sq = vecp1.x * vecp1.x + vecp1.y * vecp1.y;
|
|
329
|
-
const p2_sq = vecp2.x * vecp2.x + vecp2.y * vecp2.y;
|
|
330
|
-
const det = vecp0.x * (vecp1.y * p2_sq - p1_sq * vecp2.y) -
|
|
331
|
-
vecp0.y * (vecp1.x * p2_sq - p1_sq * vecp2.x) +
|
|
332
|
-
p0_sq * (vecp1.x * vecp2.y - vecp1.y * vecp2.x);
|
|
333
|
-
return det <= 0;
|
|
334
|
-
}
|
|
335
|
-
function constrainEdges(meshData) {
|
|
336
|
-
if (meshData.con_edge.length == 0)
|
|
337
|
-
return;
|
|
338
|
-
buildVertexConnectivity(meshData);
|
|
339
|
-
const con_edges = meshData.con_edge;
|
|
340
|
-
const triangles = meshData.tri;
|
|
341
|
-
const verts = meshData.scaled_vert;
|
|
342
|
-
const adjacency = meshData.adj;
|
|
343
|
-
const vert2tri = meshData.vert_to_tri;
|
|
344
|
-
const newEdgeList = [];
|
|
345
|
-
for (let iedge = 0; iedge < con_edges.length; iedge++) {
|
|
346
|
-
let intersections = getEdgeIntersections(meshData, iedge);
|
|
347
|
-
let iter = 0;
|
|
348
|
-
const maxIter = Math.max(intersections.length, 1);
|
|
349
|
-
while (intersections.length > 0 && iter < maxIter) {
|
|
350
|
-
fixEdgeIntersections(meshData, intersections, iedge, newEdgeList);
|
|
351
|
-
intersections = getEdgeIntersections(meshData, iedge);
|
|
352
|
-
iter++;
|
|
353
|
-
}
|
|
354
|
-
if (intersections.length > 0)
|
|
355
|
-
throw `Could not add edge ${iedge} to triangulation after ${maxIter} iterations!`;
|
|
356
|
-
}
|
|
357
|
-
while (true) {
|
|
358
|
-
let num_diagonal_swaps = 0;
|
|
359
|
-
for (let iedge = 0; iedge < newEdgeList.length; iedge++) {
|
|
360
|
-
const new_edge_nodes = newEdgeList[iedge];
|
|
361
|
-
let is_con_edge = false;
|
|
362
|
-
for (let jedge = 0; jedge < con_edges.length; jedge++) {
|
|
363
|
-
if (isSameEdge(new_edge_nodes, con_edges[jedge])) {
|
|
364
|
-
is_con_edge = true;
|
|
365
|
-
break;
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
if (is_con_edge)
|
|
369
|
-
continue;
|
|
370
|
-
const tri_around_v0 = vert2tri[new_edge_nodes[0]];
|
|
371
|
-
let tri_count = 0;
|
|
372
|
-
const tri_ind_pair = [-1, -1];
|
|
373
|
-
for (let itri = 0; itri < tri_around_v0.length; itri++) {
|
|
374
|
-
const cur_tri = triangles[tri_around_v0[itri]];
|
|
375
|
-
if (cur_tri[0] == new_edge_nodes[1] ||
|
|
376
|
-
cur_tri[1] == new_edge_nodes[1] ||
|
|
377
|
-
cur_tri[2] == new_edge_nodes[1]) {
|
|
378
|
-
tri_ind_pair[tri_count] = tri_around_v0[itri];
|
|
379
|
-
tri_count++;
|
|
380
|
-
if (tri_count == 2)
|
|
381
|
-
break;
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
if (tri_ind_pair[0] == -1)
|
|
385
|
-
continue;
|
|
386
|
-
const triA_verts = [
|
|
387
|
-
verts[triangles[tri_ind_pair[0]][0]],
|
|
388
|
-
verts[triangles[tri_ind_pair[0]][1]],
|
|
389
|
-
verts[triangles[tri_ind_pair[0]][2]]
|
|
390
|
-
];
|
|
391
|
-
const outer_nodeB_ind = adjacency[tri_ind_pair[1]].indexOf(tri_ind_pair[0]);
|
|
392
|
-
const triB_vert = verts[triangles[tri_ind_pair[1]][outer_nodeB_ind]];
|
|
393
|
-
if (!isDelaunay2(triA_verts, triB_vert)) {
|
|
394
|
-
const outer_nodeA_ind = adjacency[tri_ind_pair[0]].indexOf(tri_ind_pair[1]);
|
|
395
|
-
swapDiagonal(meshData, tri_ind_pair[0], tri_ind_pair[1]);
|
|
396
|
-
num_diagonal_swaps++;
|
|
397
|
-
newEdgeList[iedge] = [
|
|
398
|
-
triangles[tri_ind_pair[0]][outer_nodeA_ind],
|
|
399
|
-
triangles[tri_ind_pair[1]][outer_nodeB_ind]
|
|
400
|
-
];
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
if (num_diagonal_swaps == 0)
|
|
404
|
-
break;
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
function buildVertexConnectivity(meshData) {
|
|
408
|
-
const triangles = meshData.tri;
|
|
409
|
-
meshData.vert_to_tri = [];
|
|
410
|
-
const vConnectivity = meshData.vert_to_tri;
|
|
411
|
-
for (let itri = 0; itri < triangles.length; itri++) {
|
|
412
|
-
for (let node = 0; node < 3; node++) {
|
|
413
|
-
if (vConnectivity[triangles[itri][node]] == undefined)
|
|
414
|
-
vConnectivity[triangles[itri][node]] = [itri];
|
|
415
|
-
else
|
|
416
|
-
vConnectivity[triangles[itri][node]].push(itri);
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
function getEdgeIntersections(meshData, iedge) {
|
|
421
|
-
const triangles = meshData.tri;
|
|
422
|
-
const verts = meshData.scaled_vert;
|
|
423
|
-
const adjacency = meshData.adj;
|
|
424
|
-
const con_edges = meshData.con_edge;
|
|
425
|
-
const vert2tri = meshData.vert_to_tri;
|
|
426
|
-
const edge_v0_ind = con_edges[iedge][0];
|
|
427
|
-
const edge_v1_ind = con_edges[iedge][1];
|
|
428
|
-
const edge_coords = [verts[edge_v0_ind], verts[edge_v1_ind]];
|
|
429
|
-
const tri_around_v0 = vert2tri[edge_v0_ind];
|
|
430
|
-
let edge_in_triangulation = false;
|
|
431
|
-
const intersections = [];
|
|
432
|
-
for (let itri = 0; itri < tri_around_v0.length; itri++) {
|
|
433
|
-
const cur_tri = triangles[tri_around_v0[itri]];
|
|
434
|
-
const v0_node = cur_tri.indexOf(edge_v0_ind);
|
|
435
|
-
const v0p1_node = (v0_node + 1) % 3;
|
|
436
|
-
const v0p2_node = (v0_node + 2) % 3;
|
|
437
|
-
if (edge_v1_ind == cur_tri[v0p1_node]) {
|
|
438
|
-
edge_in_triangulation = true;
|
|
439
|
-
break;
|
|
440
|
-
}
|
|
441
|
-
else if (edge_v1_ind == cur_tri[v0p2_node]) {
|
|
442
|
-
edge_in_triangulation = true;
|
|
443
|
-
break;
|
|
444
|
-
}
|
|
445
|
-
const opposite_edge_coords = [
|
|
446
|
-
verts[cur_tri[v0p1_node]],
|
|
447
|
-
verts[cur_tri[v0p2_node]]
|
|
448
|
-
];
|
|
449
|
-
if (isEdgeIntersecting(edge_coords, opposite_edge_coords)) {
|
|
450
|
-
intersections.push([tri_around_v0[itri], v0_node]);
|
|
451
|
-
break;
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
if (!edge_in_triangulation) {
|
|
455
|
-
if (intersections.length == 0)
|
|
456
|
-
throw "Cannot have no intersections!";
|
|
457
|
-
while (true) {
|
|
458
|
-
const prev_intersection = intersections[intersections.length - 1];
|
|
459
|
-
const tri_ind = adjacency[prev_intersection[0]][prev_intersection[1]];
|
|
460
|
-
if (triangles[tri_ind][0] == edge_v1_ind ||
|
|
461
|
-
triangles[tri_ind][1] == edge_v1_ind ||
|
|
462
|
-
triangles[tri_ind][2] == edge_v1_ind) {
|
|
463
|
-
break;
|
|
464
|
-
}
|
|
465
|
-
const prev_edge_ind = adjacency[tri_ind].indexOf(prev_intersection[0]);
|
|
466
|
-
if (prev_edge_ind == -1)
|
|
467
|
-
throw "Could not find edge!";
|
|
468
|
-
const cur_tri = triangles[tri_ind];
|
|
469
|
-
for (let offset = 1; offset < 3; offset++) {
|
|
470
|
-
const v0_node = (prev_edge_ind + offset + 1) % 3;
|
|
471
|
-
const v1_node = (prev_edge_ind + offset + 2) % 3;
|
|
472
|
-
const cur_edge_coords = [
|
|
473
|
-
verts[cur_tri[v0_node]],
|
|
474
|
-
verts[cur_tri[v1_node]]
|
|
475
|
-
];
|
|
476
|
-
if (isEdgeIntersecting(edge_coords, cur_edge_coords)) {
|
|
477
|
-
intersections.push([tri_ind, (prev_edge_ind + offset) % 3]);
|
|
478
|
-
break;
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
return intersections;
|
|
484
|
-
}
|
|
485
|
-
function fixEdgeIntersections(meshData, intersectionList, con_edge_ind, newEdgeList) {
|
|
486
|
-
const triangles = meshData.tri;
|
|
487
|
-
const verts = meshData.scaled_vert;
|
|
488
|
-
const adjacency = meshData.adj;
|
|
489
|
-
const con_edges = meshData.con_edge;
|
|
490
|
-
const con_edge_nodes = con_edges[con_edge_ind];
|
|
491
|
-
const cur_con_edge_coords = [
|
|
492
|
-
verts[con_edge_nodes[0]],
|
|
493
|
-
verts[con_edge_nodes[1]]
|
|
494
|
-
];
|
|
495
|
-
const nIntersections = intersectionList.length;
|
|
496
|
-
for (let i = 0; i < nIntersections; i++) {
|
|
497
|
-
const tri0_ind = intersectionList[nIntersections - 1 - i][0];
|
|
498
|
-
const tri0_node = intersectionList[nIntersections - 1 - i][1];
|
|
499
|
-
const tri1_ind = adjacency[tri0_ind][tri0_node];
|
|
500
|
-
const tri1_node = adjacency[tri1_ind].indexOf(tri0_ind);
|
|
501
|
-
const quad_v0 = verts[triangles[tri0_ind][tri0_node]];
|
|
502
|
-
const quad_v1 = verts[triangles[tri0_ind][(tri0_node + 1) % 3]];
|
|
503
|
-
const quad_v2 = verts[triangles[tri1_ind][tri1_node]];
|
|
504
|
-
const quad_v3 = verts[triangles[tri0_ind][(tri0_node + 2) % 3]];
|
|
505
|
-
const isConvex = isQuadConvex(quad_v0, quad_v1, quad_v2, quad_v3);
|
|
506
|
-
if (isConvex) {
|
|
507
|
-
swapDiagonal(meshData, tri0_ind, tri1_ind);
|
|
508
|
-
const newDiagonal_nodes = [
|
|
509
|
-
triangles[tri0_ind][tri0_node],
|
|
510
|
-
triangles[tri1_ind][tri1_node]
|
|
511
|
-
];
|
|
512
|
-
const newDiagonal_coords = [quad_v0, quad_v2];
|
|
513
|
-
const hasCommonNode = newDiagonal_nodes[0] == con_edge_nodes[0] ||
|
|
514
|
-
newDiagonal_nodes[0] == con_edge_nodes[1] ||
|
|
515
|
-
newDiagonal_nodes[1] == con_edge_nodes[0] ||
|
|
516
|
-
newDiagonal_nodes[1] == con_edge_nodes[1];
|
|
517
|
-
if (hasCommonNode ||
|
|
518
|
-
!isEdgeIntersecting(cur_con_edge_coords, newDiagonal_coords)) {
|
|
519
|
-
newEdgeList.push([newDiagonal_nodes[0], newDiagonal_nodes[1]]);
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
function loadEdges(meshData, edges) {
|
|
525
|
-
const nVertex = meshData.vert.length;
|
|
526
|
-
meshData.con_edge = [];
|
|
527
|
-
for (let i = 0; i < edges.length; i++) {
|
|
528
|
-
const edge = edges[i];
|
|
529
|
-
if (edge[0] < 0 ||
|
|
530
|
-
edge[0] >= nVertex ||
|
|
531
|
-
edge[1] < 0 ||
|
|
532
|
-
edge[1] >= nVertex) {
|
|
533
|
-
throw `Vertex indices of edge ${i} need to be non-negative and less than the number of input vertices.`;
|
|
534
|
-
}
|
|
535
|
-
if (edge[0] === edge[1]) {
|
|
536
|
-
throw `Edge ${i} is degenerate!`;
|
|
537
|
-
}
|
|
538
|
-
if (!isEdgeValid(edge, meshData.con_edge, meshData.vert)) {
|
|
539
|
-
throw `Edge ${i} already exists or intersects with an existing edge!`;
|
|
540
|
-
}
|
|
541
|
-
meshData.con_edge.push([edge[0], edge[1]]);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
function isEdgeValid(newEdge, edgeList, vertices) {
|
|
545
|
-
const new_edge_verts = [
|
|
546
|
-
vertices[newEdge[0]],
|
|
547
|
-
vertices[newEdge[1]]
|
|
548
|
-
];
|
|
549
|
-
for (let i = 0; i < edgeList.length; i++) {
|
|
550
|
-
if ((edgeList[i][0] == newEdge[0] && edgeList[i][1] == newEdge[1]) ||
|
|
551
|
-
(edgeList[i][0] == newEdge[1] && edgeList[i][1] == newEdge[0]))
|
|
552
|
-
return false;
|
|
553
|
-
const hasCommonNode = edgeList[i][0] == newEdge[0] ||
|
|
554
|
-
edgeList[i][0] == newEdge[1] ||
|
|
555
|
-
edgeList[i][1] == newEdge[0] ||
|
|
556
|
-
edgeList[i][1] == newEdge[1];
|
|
557
|
-
const edge_verts = [
|
|
558
|
-
vertices[edgeList[i][0]],
|
|
559
|
-
vertices[edgeList[i][1]]
|
|
560
|
-
];
|
|
561
|
-
if (!hasCommonNode && isEdgeIntersecting(edge_verts, new_edge_verts))
|
|
562
|
-
return false;
|
|
563
|
-
}
|
|
564
|
-
return true;
|
|
565
|
-
}
|
|
2
|
+
import cdt2d from "cdt2d";
|
|
566
3
|
export default function (points, edges, z) {
|
|
567
4
|
if (!edges)
|
|
568
5
|
edges = [];
|
|
@@ -570,55 +7,20 @@ export default function (points, edges, z) {
|
|
|
570
7
|
throw "Argument points must be FeatureCollection";
|
|
571
8
|
if (!Array.isArray(edges))
|
|
572
9
|
throw "Argument points must be Array of Array";
|
|
573
|
-
|
|
574
|
-
const
|
|
575
|
-
const xy = point.geometry.coordinates;
|
|
576
|
-
prev[0].push(xy[0]);
|
|
577
|
-
prev[1].push(xy[1]);
|
|
578
|
-
if (z) {
|
|
579
|
-
prev[2].push(point.properties[z]);
|
|
580
|
-
}
|
|
581
|
-
else if (xy.length === 3) {
|
|
582
|
-
isPointZ = true;
|
|
583
|
-
prev[2].push(point.geometry.coordinates[2]);
|
|
584
|
-
}
|
|
585
|
-
return prev;
|
|
586
|
-
}, [[], [], []]);
|
|
587
|
-
const xMax = Math.max.apply(null, xyzs[0]);
|
|
588
|
-
const xMin = Math.min.apply(null, xyzs[0]);
|
|
589
|
-
const yMax = Math.max.apply(null, xyzs[1]);
|
|
590
|
-
const yMin = Math.min.apply(null, xyzs[1]);
|
|
591
|
-
const xDiff = xMax - xMin;
|
|
592
|
-
const xCenter = (xMax + xMin) / 2.0;
|
|
593
|
-
const yDiff = yMax - yMin;
|
|
594
|
-
const yCenter = (yMax + yMin) / 2.0;
|
|
595
|
-
const maxDiff = Math.max(xDiff, yDiff) * 1.1;
|
|
596
|
-
const normPoints = points.features.map(point => {
|
|
597
|
-
const xy = point.geometry.coordinates;
|
|
598
|
-
const normXy = [
|
|
599
|
-
(xy[0] - xCenter) / maxDiff + 0.5,
|
|
600
|
-
(xy[1] - yCenter) / maxDiff + 0.5
|
|
601
|
-
];
|
|
602
|
-
return new Point(normXy[0], normXy[1]);
|
|
603
|
-
});
|
|
604
|
-
const meshData = {
|
|
605
|
-
vert: normPoints
|
|
606
|
-
};
|
|
607
|
-
loadEdges(meshData, edges);
|
|
608
|
-
delaunay(meshData);
|
|
609
|
-
constrainEdges(meshData);
|
|
10
|
+
const del_points = points.features.map(point => point.geometry.coordinates);
|
|
11
|
+
const tris = cdt2d(del_points, edges);
|
|
610
12
|
const keys = ["a", "b", "c"];
|
|
611
|
-
return featureCollection(
|
|
13
|
+
return featureCollection(tris.map(indices => {
|
|
612
14
|
const properties = {};
|
|
613
15
|
const coords = indices.map((index, i) => {
|
|
614
|
-
const
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
16
|
+
const point = points.features[index];
|
|
17
|
+
const xyz = point.geometry.coordinates;
|
|
18
|
+
const coord = [xyz[0], xyz[1]];
|
|
19
|
+
if (xyz.length === 3) {
|
|
20
|
+
coord[2] = xyz[2];
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
properties[keys[i]] = point.properties[z];
|
|
622
24
|
}
|
|
623
25
|
return coord;
|
|
624
26
|
});
|