-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathdelaunay.js
240 lines (227 loc) · 7.67 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
import Delaunator from "delaunator";
import Path from "./path.js";
import Polygon from "./polygon.js";
import Voronoi from "./voronoi.js";
const tau = 2 * Math.PI, pow = Math.pow;
function pointX(p) {
return p[0];
}
function pointY(p) {
return p[1];
}
// A triangulation is collinear if all its triangles have a non-null area
function collinear(d) {
const {triangles, coords} = d;
for (let i = 0; i < triangles.length; i += 3) {
const a = 2 * triangles[i],
b = 2 * triangles[i + 1],
c = 2 * triangles[i + 2],
cross = (coords[c] - coords[a]) * (coords[b + 1] - coords[a + 1])
- (coords[b] - coords[a]) * (coords[c + 1] - coords[a + 1]);
if (cross > 1e-10) return false;
}
return true;
}
export default class Delaunay {
static from(points, fx = pointX, fy = pointY, that) {
return new Delaunay("length" in points
? flatArray(points, fx, fy, that)
: Float64Array.from(flatIterable(points, fx, fy, that)));
}
constructor(points) {
this._delaunator = new Delaunator(points);
this.inedges = new Int32Array(points.length / 2);
this._hullIndex = new Int32Array(points.length / 2);
this.points = this._delaunator.coords;
this._init();
}
update() {
this._delaunator.update();
this._init();
return this;
}
_init() {
const d = this._delaunator, points = this.points;
// check for collinear
if (d.hull && d.hull.length > 2 && collinear(d)) {
this.collinear = Int32Array.from({length: points.length / 2}, (_, i) => i)
.sort((i, j) => points[2 * i] - points[2 * j] || points[2 * i + 1] - points[2 * j + 1]); // for exact neighbors
const e = this.collinear[0] << 1, f = this.collinear[this.collinear.length - 1] << 1;
const r = 1e-9 * Math.hypot(points[f] - points[e], points[f + 1] - points[e + 1]);
for (let i = 0; i < points.length; ++i) points[i] += r * Math.sin(i + 0.5);
this._delaunator = new Delaunator(points);
} else {
delete this.collinear;
}
const halfedges = this.halfedges = this._delaunator.halfedges;
const hull = this.hull = this._delaunator.hull;
const triangles = this.triangles = this._delaunator.triangles;
const inedges = this.inedges.fill(-1);
const hullIndex = this._hullIndex.fill(-1);
// Compute an index from each point to an (arbitrary) incoming halfedge
// Used to give the first neighbor of each point; for this reason,
// on the hull we give priority to exterior halfedges
for (let e = 0, n = halfedges.length; e < n; ++e) {
const p = triangles[e % 3 === 2 ? e - 2 : e + 1];
if (halfedges[e] === -1 || inedges[p] === -1) inedges[p] = e;
}
for (let i = 0, n = hull.length; i < n; ++i) {
hullIndex[hull[i]] = i;
}
// degenerate case: 1 or 2 (distinct) points
if (hull.length <= 2 && hull.length > 0) {
this.triangles = new Int32Array(3).fill(-1);
this.halfedges = new Int32Array(3).fill(-1);
this.triangles[0] = hull[0];
inedges[hull[0]] = 1;
if (hull.length === 2) {
inedges[hull[1]] = 0;
this.triangles[1] = hull[1];
this.triangles[2] = hull[1];
}
}
}
voronoi(bounds) {
return new Voronoi(this, bounds);
}
*neighbors(i) {
const {inedges, hull, _hullIndex, halfedges, triangles, collinear} = this;
// degenerate case with several collinear points
if (collinear) {
const l = collinear.indexOf(i);
if (l > 0) yield collinear[l - 1];
if (l < collinear.length - 1) yield collinear[l + 1];
return;
}
const e0 = inedges[i];
if (e0 === -1) return; // coincident point
let e = e0, p0 = -1;
do {
yield p0 = triangles[e];
e = e % 3 === 2 ? e - 2 : e + 1;
if (triangles[e] !== i) return; // bad triangulation
e = halfedges[e];
if (e === -1) {
const p = hull[(_hullIndex[i] + 1) % hull.length];
if (p !== p0) yield p;
return;
}
} while (e !== e0);
}
find(x, y, i = 0) {
if ((x = +x, x !== x) || (y = +y, y !== y)) return -1;
const i0 = i;
let c;
while ((c = this._step(i, x, y)) >= 0 && c !== i && c !== i0) i = c;
return c;
}
_step(i, x, y) {
const {inedges, hull, _hullIndex, halfedges, triangles, points} = this;
if (points.length < 2) return -1;
if (inedges[i] === -1) return (i + 1) % (points.length >> 1);
let c = i;
let dc = pow(x - points[i * 2], 2) + pow(y - points[i * 2 + 1], 2);
const e0 = inedges[i];
let e = e0;
do {
let t = triangles[e];
const dt = pow(x - points[t * 2], 2) + pow(y - points[t * 2 + 1], 2);
if (dt < dc) dc = dt, c = t;
e = e % 3 === 2 ? e - 2 : e + 1;
if (triangles[e] !== i) break; // bad triangulation
e = halfedges[e];
if (e === -1) {
e = hull[(_hullIndex[i] + 1) % hull.length];
if (e !== t) {
if (pow(x - points[e * 2], 2) + pow(y - points[e * 2 + 1], 2) < dc) return e;
}
break;
}
} while (e !== e0);
return c;
}
render(context) {
const buffer = context == null ? context = new Path : undefined;
const {points, halfedges, triangles} = this;
for (let i = 0, n = halfedges.length; i < n; ++i) {
const j = halfedges[i];
if (j < i) continue;
const ti = triangles[i] * 2;
const tj = triangles[j] * 2;
context.moveTo(points[ti], points[ti + 1]);
context.lineTo(points[tj], points[tj + 1]);
}
this.renderHull(context);
return buffer && buffer.value();
}
renderPoints(context, r) {
if (r === undefined && (!context || typeof context.moveTo !== "function")) r = context, context = null;
r = r == undefined ? 2 : +r;
const buffer = context == null ? context = new Path : undefined;
const {points} = this;
for (let i = 0, n = points.length & ~1; i < n; i += 2) { // round down if points has odd length
const x = points[i], y = points[i + 1];
context.moveTo(x + r, y);
context.arc(x, y, r, 0, tau);
}
return buffer && buffer.value();
}
renderHull(context) {
const buffer = context == null ? context = new Path : undefined;
const {hull, points} = this;
const h = hull[0] * 2, n = hull.length;
context.moveTo(points[h], points[h + 1]);
for (let i = 1; i < n; ++i) {
const h = 2 * hull[i];
context.lineTo(points[h], points[h + 1]);
}
context.closePath();
return buffer && buffer.value();
}
hullPolygon() {
const polygon = new Polygon;
this.renderHull(polygon);
return polygon.value();
}
renderTriangle(i, context) {
const buffer = context == null ? context = new Path : undefined;
const {points, triangles} = this;
const t0 = triangles[i *= 3] * 2;
const t1 = triangles[i + 1] * 2;
const t2 = triangles[i + 2] * 2;
context.moveTo(points[t0], points[t0 + 1]);
context.lineTo(points[t1], points[t1 + 1]);
context.lineTo(points[t2], points[t2 + 1]);
context.closePath();
return buffer && buffer.value();
}
*trianglePolygons() {
const {triangles} = this;
for (let i = 0, n = triangles.length / 3; i < n; ++i) {
yield this.trianglePolygon(i);
}
}
trianglePolygon(i) {
const polygon = new Polygon;
this.renderTriangle(i, polygon);
return polygon.value();
}
}
function flatArray(points, fx, fy, that) {
const n = points.length;
const array = new Float64Array(n * 2);
for (let i = 0; i < n; ++i) {
const p = points[i];
array[i * 2] = fx.call(that, p, i, points);
array[i * 2 + 1] = fy.call(that, p, i, points);
}
return array;
}
function* flatIterable(points, fx, fy, that) {
let i = 0;
for (const p of points) {
yield fx.call(that, p, i, points);
yield fy.call(that, p, i, points);
++i;
}
}