forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelaunay.js
266 lines (239 loc) · 7.3 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
import {create, group, path, select, Delaunay} from "d3";
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,
[
{name: "x", value: x, scale: "x", optional: true},
{name: "y", value: y, scale: "y", optional: true},
{name: "z", value: z, optional: true}
],
options,
delaunayLinkDefaults
);
this.curve = Curve(curve, tension);
markers(this, options);
}
render(index, scales, channels, dimensions) {
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")
.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,
[
{name: "x", value: x, scale: "x", optional: true},
{name: "y", value: y, scale: "y", optional: true},
{name: "z", value: zof(options), optional: true}
],
options,
defaults
);
}
render(index, scales, channels, dimensions) {
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")
.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,
[
{name: "x", value: x, scale: "x", optional: true},
{name: "y", value: y, scale: "y", optional: true},
{name: "z", value: z, optional: true}
],
options,
voronoiDefaults
);
}
render(index, scales, channels, dimensions) {
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")
.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});
}
export function delaunayLink(data, options) {
return delaunayMark(DelaunayLink, data, options);
}
export function delaunayMesh(data, options) {
return delaunayMark(DelaunayMesh, data, options);
}
export function hull(data, options) {
return delaunayMark(Hull, data, options);
}
export function voronoi(data, options) {
return delaunayMark(Voronoi, data, options);
}
export function voronoiMesh(data, options) {
return delaunayMark(VoronoiMesh, data, options);
}