Enforce minimum sample interval for resampling.

The minimum sample interval is somewhat arbitrarily set to 16 × δ, where
δ is the Douglas–Peucker threshold.  This fixes cases where resampling
stops too early due to the perpendicular distance being below the
threshold; particularly for points that are far apart and whose
great-circle arc is projected to an S-shape.  The midpoint lies along a
straight line, failing the Douglas–Peucker check, but further resampling
produces an S-shape.

Ideally, resampling should also detect cases where an intermediate point
does not need to be drawn, i.e. where there truly is a straight line.
This fix causes points to be drawn at the minimum sample interval, and
thus may degrade performance slightly due to drawing redundant points.

Fixes #1162.
This commit is contained in:
Jason Davies 2012-12-14 15:21:51 +00:00 коммит произвёл Mike Bostock
Родитель e15ac86caa
Коммит 77aedd59a9
4 изменённых файлов: 30 добавлений и 9 удалений

2
d3.js поставляемый
Просмотреть файл

@ -2970,7 +2970,7 @@ d3 = function() {
var dx = x1 - x0, dy = y1 - y0, d2 = dx * dx + dy * dy;
if (d2 > 4 * δ2 && depth--) {
var a = a0 + a1, b = b0 + b1, c = c0 + c1, m = Math.sqrt(a * a + b * b + c * c), φ2 = Math.asin(c /= m), λ2 = Math.abs(Math.abs(c) - 1) < ε ? (λ0 + λ1) / 2 : Math.atan2(b, a), p = project(λ2, φ2), x2 = p[0], y2 = p[1], dx2 = x2 - x0, dy2 = y2 - y0, dz = dy * dx2 - dx * dy2;
if (dz * dz / d2 > δ2 || Math.abs((dx * dx2 + dy * dy2) / d2 - .5) > .3) {
if (dz * dz / d2 > δ2 || Math.abs((dx * dx2 + dy * dy2) / d2 - .5) > .3 || dx2 * dx2 + dy2 * dy2 > 256 * δ2) {
resampleLineTo(x0, y0, λ0, a0, b0, c0, x2, y2, λ2, a /= m, b /= m, c, depth, stream);
stream.point(x2, y2);
resampleLineTo(x2, y2, λ2, a, b, c, x1, y1, λ1, a1, b1, c1, depth, stream);

2
d3.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -75,7 +75,7 @@ function d3_geo_resample(project) {
dx2 = x2 - x0,
dy2 = y2 - y0,
dz = dy * dx2 - dx * dy2;
if (dz * dz / d2 > δ2 || Math.abs((dx * dx2 + dy * dy2) / d2 - .5) > .3) {
if (dz * dz / d2 > δ2 || Math.abs((dx * dx2 + dy * dy2) / d2 - .5) > .3 || dx2 * dx2 + dy2 * dy2 > 256 * δ2) {
resampleLineTo(x0, y0, λ0, a0, b0, c0, x2, y2, λ2, a /= m, b /= m, c, depth, stream);
stream.point(x2, y2);
resampleLineTo(x2, y2, λ2, a, b, c, x1, y1, λ1, a1, b1, c1, depth, stream);

Просмотреть файл

@ -287,14 +287,16 @@ suite.addBatch({
},
"area of a polygon": function(p) {
var area = p.area({type: "Polygon", coordinates: [[[-122, 37], [-71, 42], [-80, 25], [-122, 37]]]});
assert.inDelta(area, 124884.274, 1e-3);
assert.inDelta(area, 125257.618, 1e-3);
},
"bounds of a line string": function(p) {
assert.inDelta(p.bounds({type: "LineString", coordinates: [[-122, 37], [-74, 40], [-100, 0]]}),
[[109.378, 189.584], [797.758, 504.660]], 1e-3);
},
"centroid of a line string": function(p) {
assert.inDelta(p.centroid({type: "LineString", coordinates: [[-122, 37], [-74, 40], [-100, 0]]}), [545.130, 253.859], 1e-3);
var centroid = p.centroid({type: "LineString", coordinates: [[-122, 37], [-74, 40], [-100, 0]]});
assert.inDelta(centroid[0], 545.169, 1e-3);
assert.inDelta(centroid[1], 253.753, 1e-3);
}
},
@ -757,19 +759,38 @@ suite.addBatch({
},
"with an Albers projection and adaptive resampling": {
topic: function(path) {
return path()
"correctly resamples near the poles": function(path) {
var p = path()
.context(testContext)
.projection(_.geo.albers()
.scale(140)
.rotate([0, 0])
.precision(1));
},
"correctly resamples near the poles": function(p) {
p({type: "LineString", coordinates: [[0, 88], [180, 89]]});
assert.isTrue(testContext.buffer().filter(function(d) { return d.type === "lineTo"; }).length > 1);
p({type: "LineString", coordinates: [[180, 90], [1, 89.5]]});
assert.isTrue(testContext.buffer().filter(function(d) { return d.type === "lineTo"; }).length > 1);
},
"rotate([11.5, 285])": function(path) {
var p = path()
.context(testContext)
.projection(_.geo.albers()
.scale(140)
.rotate([11.5, 285])
.precision(1));
p({type: "LineString", coordinates: [[170, 20], [170, 0]]});
assert.isTrue(testContext.buffer().filter(function(d) { return d.type === "lineTo"; }).length > 1);
},
"wavy projection": function(path) {
var p = path()
.context(testContext)
.projection(_.geo.projection(function(λ, φ) {
return [λ, Math.sin(λ * 4)];
})
.scale(140)
.precision(1));
p({type: "LineString", coordinates: [[-45, 0], [45, 0]]});
assert.isTrue(testContext.buffer().filter(function(d) { return d.type === "lineTo"; }).length > 1);
}
},