-
Notifications
You must be signed in to change notification settings - Fork 24
/
delaunay.js
417 lines (372 loc) · 10.1 KB
/
delaunay.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//
// (c) 2019 Philippe Riviere
//
// https://github.com/Fil/
//
// This software is distributed under the terms of the MIT License
import { Delaunay } from "d3-delaunay";
import { geoRotation, geoStereographic } from "d3-geo";
import { extent } from "d3-array";
import {
asin,
atan,
atan2,
cos,
degrees,
halfPi,
max,
min,
pi,
radians,
sign,
sin,
sqrt,
tan
} from "./math.js";
import {
cartesianNormalize as normalize,
cartesianCross as cross,
cartesianDot as dot,
cartesianAdd
} from "./cartesian.js";
// Converts 3D Cartesian to spherical coordinates (degrees).
function spherical(cartesian) {
return [
atan2(cartesian[1], cartesian[0]) * degrees,
asin(max(-1, min(1, cartesian[2]))) * degrees
];
}
// Converts spherical coordinates (degrees) to 3D Cartesian.
function cartesian(coordinates) {
var lambda = coordinates[0] * radians,
phi = coordinates[1] * radians,
cosphi = cos(phi);
return [cosphi * cos(lambda), cosphi * sin(lambda), sin(phi)];
}
// Spherical excess of a triangle (in spherical coordinates)
export function excess(triangle) {
triangle = triangle.map(p => cartesian(p));
return dot(triangle[0], cross(triangle[2], triangle[1]));
}
export function geoDelaunay(points) {
const delaunay = geo_delaunay_from(points),
triangles = geo_triangles(delaunay),
edges = geo_edges(triangles, points),
neighbors = geo_neighbors(triangles, points.length),
find = geo_find(neighbors, points),
// Voronoi ; could take a center function as an argument
circumcenters = geo_circumcenters(triangles, points),
{ polygons, centers } = geo_polygons(circumcenters, triangles, points),
mesh = geo_mesh(polygons),
hull = geo_hull(triangles, points),
// Urquhart ; returns a function that takes a distance array as argument.
urquhart = geo_urquhart(edges, triangles);
return {
delaunay,
edges,
triangles,
centers,
neighbors,
polygons,
mesh,
hull,
urquhart,
find
};
}
function geo_find(neighbors, points) {
function distance2(a,b) {
let x = a[0] - b[0],
y = a[1] - b[1],
z = a[2] - b[2];
return x * x + y * y + z * z;
}
return function find(x, y, next) {
if (next === undefined) next = 0;
let cell,
dist,
found = next;
const xyz = cartesian([x, y]);
do {
cell = next;
next = null;
dist = distance2(xyz, cartesian(points[cell]));
neighbors[cell].forEach(i => {
let ndist = distance2(xyz, cartesian(points[i]));
if (ndist < dist) {
dist = ndist;
next = i;
found = i;
return;
}
});
} while (next !== null);
return found;
};
}
function geo_delaunay_from(points) {
if (points.length < 2) return {};
// find a valid point to send to infinity
let pivot = 0;
while (isNaN(points[pivot][0]+points[pivot][1]) && pivot++ < points.length) {}
const r = geoRotation(points[pivot]),
projection = geoStereographic()
.translate([0, 0])
.scale(1)
.rotate(r.invert([180, 0]));
points = points.map(projection);
const zeros = [];
let max2 = 1;
for (let i = 0, n = points.length; i < n; i++) {
let m = points[i][0] ** 2 + points[i][1] ** 2;
if (!isFinite(m) || m > 1e32) zeros.push(i);
else if (m > max2) max2 = m;
}
const FAR = 1e6 * sqrt(max2);
zeros.forEach(i => (points[i] = [FAR, 0]));
// Add infinite horizon points
points.push([0,FAR]);
points.push([-FAR,0]);
points.push([0,-FAR]);
const delaunay = Delaunay.from(points);
delaunay.projection = projection;
// clean up the triangulation
const {triangles, halfedges, inedges} = delaunay;
const degenerate = [];
for (let i = 0, l = halfedges.length; i < l; i++) {
if (halfedges[i] < 0) {
const j = i % 3 == 2 ? i - 2 : i + 1;
const k = i % 3 == 0 ? i + 2 : i - 1;
const a = halfedges[j];
const b = halfedges[k];
halfedges[a] = b;
halfedges[b] = a;
halfedges[j] = halfedges[k] = -1;
triangles[i] = triangles[j] = triangles[k] = pivot;
inedges[triangles[a]] = a % 3 == 0 ? a + 2 : a - 1;
inedges[triangles[b]] = b % 3 == 0 ? b + 2 : b - 1;
degenerate.push(Math.min(i,j,k));
i += 2 - i % 3;
} else if (triangles[i] > points.length - 3 - 1) {
triangles[i] = pivot;
}
}
// there should always be 4 degenerate triangles
// console.warn(degenerate);
return delaunay;
}
function geo_edges(triangles, points) {
const _index = new Set;
if (points.length === 2) return [[0, 1]];
triangles.forEach(tri => {
if (tri[0] === tri[1]) return;
if (excess(tri.map(i => points[i])) < 0) return;
for (let i = 0, j; i < 3; i++) {
j = (i + 1) % 3;
_index.add(extent([tri[i], tri[j]]).join("-"));
}
});
return Array.from(_index, d => d.split("-").map(Number));
}
function geo_triangles(delaunay) {
const {triangles} = delaunay;
if (!triangles) return [];
const geo_triangles = [];
for (let i = 0, n = triangles.length / 3; i < n; i++) {
const a = triangles[3 * i],
b = triangles[3 * i + 1],
c = triangles[3 * i + 2];
if (a !== b && b !== c) {
geo_triangles.push([a, c, b]);
}
}
return geo_triangles;
}
function geo_circumcenters(triangles, points) {
// if (!use_centroids) {
return triangles.map(tri => {
const c = tri.map(i => points[i]).map(cartesian),
V = cartesianAdd(
cartesianAdd(cross(c[1], c[0]), cross(c[2], c[1])),
cross(c[0], c[2])
);
return spherical(normalize(V));
});
/*} else {
return triangles.map(tri => {
return d3.geoCentroid({
type: "MultiPoint",
coordinates: tri.map(i => points[i])
});
});
}*/
}
function geo_neighbors(triangles, npoints) {
const neighbors = [];
triangles.forEach((tri, i) => {
for (let j = 0; j < 3; j++) {
const a = tri[j],
b = tri[(j + 1) % 3];
neighbors[a] = neighbors[a] || [];
neighbors[a].push(b);
}
});
// degenerate cases
if (triangles.length === 0) {
if (npoints === 2) (neighbors[0] = [1]), (neighbors[1] = [0]);
else if (npoints === 1) neighbors[0] = [];
}
return neighbors;
}
function geo_polygons(circumcenters, triangles, points) {
const polygons = [];
const centers = circumcenters.slice();
// supplementary centers for degenerate cases like n = 1,2,3
const supplements = [];
if (triangles.length === 0) {
if (points.length < 2) return { polygons, centers };
if (points.length === 2) {
// two hemispheres
const a = cartesian(points[0]),
b = cartesian(points[1]),
m = normalize(cartesianAdd(a, b)),
d = normalize(cross(a, b)),
c = cross(m, d);
const poly = [
m,
cross(m, c),
cross(cross(m, c), c),
cross(cross(cross(m, c), c), c)
]
.map(spherical)
.map(supplement);
return (
polygons.push(poly),
polygons.push(poly.slice().reverse()),
{ polygons, centers }
);
}
}
triangles.forEach((tri, t) => {
for (let j = 0; j < 3; j++) {
const a = tri[j],
b = tri[(j + 1) % 3],
c = tri[(j + 2) % 3];
polygons[a] = polygons[a] || [];
polygons[a].push([b, c, t, [a, b, c]]);
}
});
// reorder each polygon
const reordered = polygons.map(poly => {
const p = [poly[0][2]]; // t
let k = poly[0][1]; // k = c
for (let i = 1; i < poly.length; i++) {
// look for b = k
for (let j = 0; j < poly.length; j++) {
if (poly[j][0] == k) {
k = poly[j][1];
p.push(poly[j][2]);
break;
}
}
}
if (p.length > 2) {
return p;
} else if (p.length == 2) {
const R0 = o_midpoint(
points[poly[0][3][0]],
points[poly[0][3][1]],
centers[p[0]]
),
R1 = o_midpoint(
points[poly[0][3][2]],
points[poly[0][3][0]],
centers[p[0]]
);
const i0 = supplement(R0),
i1 = supplement(R1);
return [p[0], i1, p[1], i0];
}
});
function supplement(point) {
let f = -1;
centers.slice(triangles.length, Infinity).forEach((p, i) => {
if (p[0] === point[0] && p[1] === point[1]) f = i + triangles.length;
});
if (f < 0) (f = centers.length), centers.push(point);
return f;
}
return { polygons: reordered, centers };
}
function o_midpoint(a, b, c) {
a = cartesian(a);
b = cartesian(b);
c = cartesian(c);
const s = sign(dot(cross(b, a), c));
return spherical(normalize(cartesianAdd(a, b)).map(d => s * d));
}
function geo_mesh(polygons) {
const mesh = [];
polygons.forEach(poly => {
if (!poly) return;
let p = poly[poly.length - 1];
for (let q of poly) {
if (q > p) mesh.push([p, q]);
p = q;
}
});
return mesh;
}
function geo_urquhart(edges, triangles) {
return function(distances) {
const _lengths = new Map(),
_urquhart = new Map();
edges.forEach((edge, i) => {
const u = edge.join("-");
_lengths.set(u, distances[i]);
_urquhart.set(u, true);
});
triangles.forEach(tri => {
let l = 0,
remove = -1;
for (var j = 0; j < 3; j++) {
let u = extent([tri[j], tri[(j + 1) % 3]]).join("-");
if (_lengths.get(u) > l) {
l = _lengths.get(u);
remove = u;
}
}
_urquhart.set(remove, false);
});
return edges.map(edge => _urquhart.get(edge.join("-")));
};
}
function geo_hull(triangles, points) {
const _hull = new Set(),
hull = [];
triangles.map(tri => {
if (excess(tri.map(i => points[i > points.length ? 0 : i])) < 0) return;
for (let i = 0; i < 3; i++) {
let e = [tri[i], tri[(i + 1) % 3]],
code = `${e[1]}-${e[0]}`;
if (_hull.has(code)) _hull.delete(code);
else _hull.add(e.join("-"));
}
});
const _index = new Map;
let start;
_hull.forEach(e => {
e = e.split("-").map(Number);
_index.set(e[0],e[1]);
start = e[0];
});
if (start === undefined) return hull;
let next = start;
do {
hull.push(next);
let n = _index.get(next);
_index.set(next, -1);
next = n;
} while (next > -1 && next !== start);
return hull;
}