Skip to content

Commit

Permalink
Simplify and fix midpoint drawing logic (fixes #2136)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed May 28, 2014
1 parent 26d7206 commit 0a83545
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
25 changes: 1 addition & 24 deletions js/id/renderer/map.js
Expand Up @@ -98,30 +98,7 @@ iD.Map = function(context) {
if (difference) {
var complete = difference.complete(map.extent());
all = _.compact(_.values(complete));
filter = function(d) {
if (d.type === 'midpoint') {

var a = d.edge[0],
b = d.edge[1];

// redraw a midpoint if it needs to be
// - moved (either edge node moved)
// - deleted (edge nodes not consecutive in any parent way)
if (a in complete || b in complete) return true;

var parentsWays = graph.parentWays({ id: a });
for (var i = 0; i < parentsWays.length; i++) {
var nodes = parentsWays[i].nodes;
for (var n = 0; n < nodes.length; n++) {
if (nodes[n] === a && (nodes[n - 1] === b || nodes[n + 1] === b)) return false;
}
}
return true;

} else {
return d.id in complete;
}
};
filter = function(d) { return d.id in complete; };

} else if (extent) {
all = context.intersects(map.extent().intersection(extent));
Expand Down
35 changes: 23 additions & 12 deletions js/id/svg/midpoints.js
Expand Up @@ -5,39 +5,50 @@ iD.svg.Midpoints = function(projection, context) {
for (var i = 0; i < entities.length; i++) {
var entity = entities[i];

if (entity.type !== 'way') continue;
if (context.selectedIDs().indexOf(entity.id) < 0) continue;
if (entity.type !== 'way')
continue;
if (!filter(entity))
continue;
if (context.selectedIDs().indexOf(entity.id) < 0)
continue;

var nodes = graph.childNodes(entity);

// skip the last node because it is always repeated
for (var j = 0; j < nodes.length - 1; j++) {

var a = nodes[j],
b = nodes[j + 1],
id = [a.id, b.id].sort().join('-');

// Redraw midpoints in two cases:
// 1. One of the two endpoint nodes changed (e.g. was moved).
// 2. A node was deleted. The midpoint between the two new
// endpoints needs to be redrawn. In this case only the
// way will be in the diff.
if (!midpoints[id] && (filter(a) || filter(b) || filter(entity))) {
if (midpoints[id]) {
midpoints[id].parents.push(entity);
} else {
var loc = iD.geo.interp(a.loc, b.loc, 0.5);
if (extent.intersects(loc) && iD.geo.euclideanDistance(projection(a.loc), projection(b.loc)) > 40) {
midpoints[id] = {
type: 'midpoint',
id: id,
loc: loc,
edge: [a.id, b.id]
edge: [a.id, b.id],
parents: [entity]
};
}
}
}
}

function midpointFilter(d) {
if (midpoints[d.id])
return true;

for (var i = 0; i < d.parents.length; i++)
if (filter(d.parents[i]))
return true;

return false;
}

var groups = surface.select('.layer-hit').selectAll('g.midpoint')
.filter(filter)
.filter(midpointFilter)
.data(_.values(midpoints), function(d) { return d.id; });

var group = groups.enter()
Expand Down

0 comments on commit 0a83545

Please sign in to comment.