forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelaunay.js
More file actions
352 lines (325 loc) · 10.3 KB
/
delaunay.js
File metadata and controls
352 lines (325 loc) · 10.3 KB
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
import {group, path, select, Delaunay} from "d3";
import {create} from "../context.js";
import {Curve} from "../curve.js";
import {constant, maybeTuple, maybeZ} from "../options.js";
import {Mark} from "../plot.js";
import {
applyChannelStyles,
applyDirectStyles,
applyFrameAnchor,
applyIndirectStyles,
applyTransform
} from "../style.js";
import {markers, applyMarkers} from "./marker.js";
const delaunayLinkDefaults = {
ariaLabel: "delaunay link",
fill: "none",
stroke: "currentColor",
strokeMiterlimit: 1
};
const delaunayMeshDefaults = {
ariaLabel: "delaunay mesh",
fill: null,
stroke: "currentColor",
strokeOpacity: 0.2
};
const hullDefaults = {
ariaLabel: "hull",
fill: "none",
stroke: "currentColor",
strokeWidth: 1.5,
strokeMiterlimit: 1
};
const voronoiDefaults = {
ariaLabel: "voronoi",
fill: "none",
stroke: "currentColor",
strokeMiterlimit: 1
};
const voronoiMeshDefaults = {
ariaLabel: "voronoi mesh",
fill: null,
stroke: "currentColor",
strokeOpacity: 0.2
};
class DelaunayLink extends Mark {
constructor(data, options = {}) {
const {x, y, z, curve, tension} = options;
super(
data,
{
x: {value: x, scale: "x", optional: true},
y: {value: y, scale: "y", optional: true},
z: {value: z, optional: true}
},
options,
delaunayLinkDefaults
);
this.curve = Curve(curve, tension);
markers(this, options);
}
render(index, scales, channels, dimensions, context) {
const {x: X, y: Y, z: Z} = channels;
const {curve} = this;
const [cx, cy] = applyFrameAnchor(this, dimensions);
const xi = X ? (i) => X[i] : constant(cx);
const yi = Y ? (i) => Y[i] : constant(cy);
const mark = this;
function links(index) {
let i = -1;
const newIndex = [];
const newChannels = {};
for (const k in channels) newChannels[k] = [];
const X1 = [];
const X2 = [];
const Y1 = [];
const Y2 = [];
function link(ti, tj) {
ti = index[ti];
tj = index[tj];
newIndex.push(++i);
X1[i] = xi(ti);
Y1[i] = yi(ti);
X2[i] = xi(tj);
Y2[i] = yi(tj);
for (const k in channels) newChannels[k].push(channels[k][tj]);
}
const {halfedges, hull, triangles} = Delaunay.from(index, xi, yi);
for (let i = 0; i < halfedges.length; ++i) {
// inner edges
const j = halfedges[i];
if (j > i) link(triangles[i], triangles[j]);
}
for (let i = 0; i < hull.length; ++i) {
// convex hull
link(hull[i], hull[(i + 1) % hull.length]);
}
select(this)
.selectAll()
.data(newIndex)
.join("path")
.call(applyDirectStyles, mark)
.attr("d", (i) => {
const p = path();
const c = curve(p);
c.lineStart();
c.point(X1[i], Y1[i]);
c.point(X2[i], Y2[i]);
c.lineEnd();
return p;
})
.call(applyChannelStyles, mark, newChannels)
.call(applyMarkers, mark, newChannels);
}
return create("svg:g", context)
.call(applyIndirectStyles, this, scales, dimensions)
.call(applyTransform, this, scales)
.call(
Z
? (g) =>
g
.selectAll()
.data(group(index, (i) => Z[i]).values())
.enter()
.append("g")
.each(links)
: (g) => g.datum(index).each(links)
)
.node();
}
}
class AbstractDelaunayMark extends Mark {
constructor(data, options = {}, defaults, zof = ({z}) => z) {
const {x, y} = options;
super(
data,
{
x: {value: x, scale: "x", optional: true},
y: {value: y, scale: "y", optional: true},
z: {value: zof(options), optional: true}
},
options,
defaults
);
}
render(index, scales, channels, dimensions, context) {
const {x: X, y: Y, z: Z} = channels;
const [cx, cy] = applyFrameAnchor(this, dimensions);
const xi = X ? (i) => X[i] : constant(cx);
const yi = Y ? (i) => Y[i] : constant(cy);
const mark = this;
function mesh(index) {
const delaunay = Delaunay.from(index, xi, yi);
select(this)
.append("path")
.datum(index[0])
.call(applyDirectStyles, mark)
.attr("d", mark._render(delaunay, dimensions))
.call(applyChannelStyles, mark, channels);
}
return create("svg:g", context)
.call(applyIndirectStyles, this, scales, dimensions)
.call(applyTransform, this, scales)
.call(
Z
? (g) =>
g
.selectAll()
.data(group(index, (i) => Z[i]).values())
.enter()
.append("g")
.each(mesh)
: (g) => g.datum(index).each(mesh)
)
.node();
}
}
class DelaunayMesh extends AbstractDelaunayMark {
constructor(data, options = {}) {
super(data, options, delaunayMeshDefaults);
this.fill = "none";
}
_render(delaunay) {
return delaunay.render();
}
}
class Hull extends AbstractDelaunayMark {
constructor(data, options = {}) {
super(data, options, hullDefaults, maybeZ);
}
_render(delaunay) {
return delaunay.renderHull();
}
}
class Voronoi extends Mark {
constructor(data, options = {}) {
const {x, y, z} = options;
super(
data,
{
x: {value: x, scale: "x", optional: true},
y: {value: y, scale: "y", optional: true},
z: {value: z, optional: true}
},
options,
voronoiDefaults
);
}
render(index, scales, channels, dimensions, context) {
const {x: X, y: Y, z: Z} = channels;
const [cx, cy] = applyFrameAnchor(this, dimensions);
const xi = X ? (i) => X[i] : constant(cx);
const yi = Y ? (i) => Y[i] : constant(cy);
function cells(index) {
const delaunay = Delaunay.from(index, xi, yi);
const voronoi = voronoiof(delaunay, dimensions);
select(this)
.selectAll()
.data(index)
.enter()
.append("path")
.call(applyDirectStyles, this)
.attr("d", (_, i) => voronoi.renderCell(i))
.call(applyChannelStyles, this, channels);
}
return create("svg:g", context)
.call(applyIndirectStyles, this, scales, dimensions)
.call(applyTransform, this, scales)
.call(
Z
? (g) =>
g
.selectAll()
.data(group(index, (i) => Z[i]).values())
.enter()
.append("g")
.each(cells)
: (g) => g.datum(index).each(cells)
)
.node();
}
}
class VoronoiMesh extends AbstractDelaunayMark {
constructor(data, options) {
super(data, options, voronoiMeshDefaults);
this.fill = "none";
}
_render(delaunay, dimensions) {
return voronoiof(delaunay, dimensions).render();
}
}
function voronoiof(delaunay, dimensions) {
const {width, height, marginTop, marginRight, marginBottom, marginLeft} = dimensions;
return delaunay.voronoi([marginLeft, marginTop, width - marginRight, height - marginBottom]);
}
function delaunayMark(DelaunayMark, data, {x, y, ...options} = {}) {
[x, y] = maybeTuple(x, y);
return new DelaunayMark(data, {...options, x, y});
}
/**
* Draws links for each edge of the Delaunay triangulation of the points given
* by the **x** and **y** channels. Supports the same options as the [link
* mark](#link), except that **x1**, **y1**, **x2**, and **y2** are derived
* automatically from **x** and **y**. When an aesthetic channel is specified
* (such as **stroke** or **strokeWidth**), the link inherits the corresponding
* channel value from one of its two endpoints arbitrarily.
*
* If a **z** channel is specified, the input points are grouped by *z*, and
* separate Delaunay triangulations are constructed for each group.
*/
export function delaunayLink(data, options) {
return delaunayMark(DelaunayLink, data, options);
}
/**
* Draws a mesh of the Delaunay triangulation of the points given by the **x**
* and **y** channels. The **stroke** option defaults to _currentColor_, and the
* **strokeOpacity** defaults to 0.2. The **fill** option is not supported. When
* an aesthetic channel is specified (such as **stroke** or **strokeWidth**),
* the mesh inherits the corresponding channel value from one of its constituent
* points arbitrarily.
*
* If a **z** channel is specified, the input points are grouped by *z*, and
* separate Delaunay triangulations are constructed for each group.
*/
export function delaunayMesh(data, options) {
return delaunayMark(DelaunayMesh, data, options);
}
/**
* Draws a convex hull around the points given by the **x** and **y** channels.
* The **stroke** option defaults to _currentColor_ and the **fill** option
* defaults to _none_. When an aesthetic channel is specified (such as
* **stroke** or **strokeWidth**), the hull inherits the corresponding channel
* value from one of its constituent points arbitrarily.
*
* If a **z** channel is specified, the input points are grouped by *z*, and
* separate convex hulls are constructed for each group. If the **z** channel is
* not specified, it defaults to either the **fill** channel, if any, or the
* **stroke** channel, if any.
*/
export function hull(data, options) {
return delaunayMark(Hull, data, options);
}
/**
* Draws polygons for each cell of the Voronoi tesselation of the points given
* by the **x** and **y** channels.
*
* If a **z** channel is specified, the input points are grouped by *z*, and
* separate Voronoi tesselations are constructed for each group.
*/
export function voronoi(data, options) {
return delaunayMark(Voronoi, data, options);
}
/**
* Draws a mesh for the cell boundaries of the Voronoi tesselation of the points
* given by the **x** and **y** channels. The **stroke** option defaults to
* _currentColor_, and the **strokeOpacity** defaults to 0.2. The **fill**
* option is not supported. When an aesthetic channel is specified (such as
* **stroke** or **strokeWidth**), the mesh inherits the corresponding channel
* value from one of its constituent points arbitrarily.
*
* If a **z** channel is specified, the input points are grouped by *z*, and
* separate Voronoi tesselations are constructed for each group.
*/
export function voronoiMesh(data, options) {
return delaunayMark(VoronoiMesh, data, options);
}