Skip to content

Commit 15ec3e8

Browse files
committed
Optimise voronoi for default accessors.
1 parent 112ec6f commit 15ec3e8

4 files changed

Lines changed: 33 additions & 32 deletions

File tree

d3.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4073,12 +4073,11 @@ d3 = function() {
40734073
var size = null, x = d3_svg_lineX, y = d3_svg_lineY, clip;
40744074
if (arguments.length) return voronoi(points);
40754075
function voronoi(data) {
4076-
var points = [], polygons = data.map(function() {
4076+
var points, polygons = data.map(function() {
40774077
return [];
40784078
}), fx = d3_functor(x), fy = d3_functor(y), d, i, n = data.length, Z = 1e6;
4079-
if (fx === d3_svg_lineX && fy === d3_svg_lineY) {
4080-
points = data;
4081-
} else for (points = [], i = 0; i < n; ++i) {
4079+
if (fx === d3_svg_lineX && fy === d3_svg_lineY) points = data; else for (points = [],
4080+
i = 0; i < n; ++i) {
40824081
points.push([ +fx.call(this, d = data[i], i), +fy.call(this, d, i) ]);
40834082
}
40844083
d3_geom_voronoiTessellate(points, function(e) {
@@ -4159,10 +4158,11 @@ d3 = function() {
41594158
return voronoi;
41604159
};
41614160
voronoi.links = function(data) {
4162-
var points = [], graph = [], links = [], fx = d3_functor(x), fy = d3_functor(y), d, i, n = data.length;
4163-
for (i = 0; i < n; ++i) {
4161+
var points, graph = data.map(function() {
4162+
return [];
4163+
}), links = [], fx = d3_functor(x), fy = d3_functor(y), d, i, n = data.length;
4164+
if (fx === d3_svg_lineX && fy === d3_svg_lineY) points = data; else for (i = 0; i < n; ++i) {
41644165
points.push([ +fx.call(this, d = data[i], i), +fy.call(this, d, i) ]);
4165-
graph.push([]);
41664166
}
41674167
d3_geom_voronoiTessellate(points, function(e) {
41684168
var l = e.region.l.index, r = e.region.r.index;
@@ -4176,17 +4176,18 @@ d3 = function() {
41764176
return links;
41774177
};
41784178
voronoi.triangles = function(data) {
4179-
var points = [], point, fx = d3_functor(x), fy = d3_functor(y), d, i, n = data.length;
4180-
for (i = 0; i < n; ++i) {
4179+
var points, point, fx = d3_functor(x), fy = d3_functor(y), d, i, n = data.length, wrap = fx !== d3_svg_lineX || fy !== d3_svg_lineY;
4180+
if (wrap) for (i = 0; i < n; ++i) {
41814181
point = [ +fx.call(this, d = data[i], i), +fy.call(this, d, i) ];
41824182
point.data = d;
41834183
points.push(point);
4184-
}
4185-
return d3.geom.delaunay(points).map(function(triangle) {
4184+
} else points = data;
4185+
var triangles = d3.geom.delaunay(points);
4186+
return wrap ? triangles.map(function(triangle) {
41864187
return triangle.map(function(point) {
41874188
return point.data;
41884189
});
4189-
});
4190+
}) : triangles;
41904191
};
41914192
return voronoi;
41924193
};

d3.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/geom/voronoi.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ d3.geom.voronoi = function(points) {
3636
if (arguments.length) return voronoi(points);
3737

3838
function voronoi(data) {
39-
var points = [],
39+
var points,
4040
polygons = data.map(function() { return []; }),
4141
fx = d3_functor(x),
4242
fy = d3_functor(y),
@@ -45,9 +45,8 @@ d3.geom.voronoi = function(points) {
4545
n = data.length,
4646
Z = 1e6;
4747

48-
if (fx === d3_svg_lineX && fy === d3_svg_lineY) {
49-
points = data;
50-
} else for (points = [], i = 0; i < n; ++i) {
48+
if (fx === d3_svg_lineX && fy === d3_svg_lineY) points = data;
49+
else for (points = [], i = 0; i < n; ++i) {
5150
points.push([+fx.call(this, d = data[i], i), +fy.call(this, d, i)]);
5251
}
5352

@@ -151,18 +150,18 @@ d3.geom.voronoi = function(points) {
151150
};
152151

153152
voronoi.links = function(data) {
154-
var points = [],
155-
graph = [],
153+
var points,
154+
graph = data.map(function() { return []; }),
156155
links = [],
157156
fx = d3_functor(x),
158157
fy = d3_functor(y),
159158
d,
160159
i,
161160
n = data.length;
162161

163-
for (i = 0; i < n; ++i) {
162+
if (fx === d3_svg_lineX && fy === d3_svg_lineY) points = data;
163+
else for (i = 0; i < n; ++i) {
164164
points.push([+fx.call(this, d = data[i], i), +fy.call(this, d, i)]);
165-
graph.push([]);
166165
}
167166

168167
d3_geom_voronoiTessellate(points, function(e) {
@@ -177,25 +176,28 @@ d3.geom.voronoi = function(points) {
177176
};
178177

179178
voronoi.triangles = function(data) {
180-
var points = [],
179+
var points,
181180
point,
182181
fx = d3_functor(x),
183182
fy = d3_functor(y),
184183
d,
185184
i,
186-
n = data.length;
185+
n = data.length,
186+
wrap = fx !== d3_svg_lineX || fy !== d3_svg_lineY;
187187

188-
for (i = 0; i < n; ++i) {
188+
if (wrap) for (i = 0; i < n; ++i) {
189189
point = [+fx.call(this, d = data[i], i), +fy.call(this, d, i)];
190190
point.data = d;
191191
points.push(point);
192-
}
192+
} else points = data;
193193

194-
return d3.geom.delaunay(points).map(function(triangle) {
194+
var triangles = d3.geom.delaunay(points);
195+
196+
return wrap ? triangles.map(function(triangle) {
195197
return triangle.map(function(point) {
196198
return point.data;
197199
});
198-
});
200+
}) : triangles;
199201
};
200202

201203
return voronoi;

test/geom/voronoi-test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ suite.addBatch({
8484
},
8585
"triangles": {
8686
"for three points": function(v) {
87-
assert.deepEqual(v.triangles([[200, 200], [500, 250], [760, 300]]), [
87+
assert.deepEqual(asArray(v.triangles([[200, 200], [500, 250], [760, 300]])), [
8888
[[200, 200], [760, 300], [500, 250]]
8989
]);
9090
}
@@ -185,7 +185,5 @@ suite.addBatch({
185185
suite.export(module);
186186

187187
function asArray(array) {
188-
return array.map(function(d) {
189-
return Array.prototype.slice.call(d);
190-
});
188+
return Array.isArray(array) ? array.map(asArray) : array;
191189
}

0 commit comments

Comments
 (0)