Calculate participating ways in AddMidpoint

Due to differenced redraw, midpoint.ways was getting stale,
so sometimes dragging a midpoint didn't add the new node to
all ways. Better to calculate participating ways when needed.
This also simplifies the code for adding a midpoint via double
click. It does make filtering midpoints on redraw a bit more
expensive, but a cursory profile didn't show it being a problem.

Fixes #763.
This commit is contained in:
John Firebaugh
2013-02-27 20:41:22 -08:00
parent f4b19133e7
commit b7894ceaf0
5 changed files with 44 additions and 37 deletions

View File

@@ -2,8 +2,19 @@ iD.actions.AddMidpoint = function(midpoint, node) {
return function(graph) {
graph = graph.replace(node.move(midpoint.loc));
midpoint.ways.forEach(function(way) {
graph = graph.replace(graph.entity(way.id).addNode(node.id, way.index));
var parents = _.intersection(
graph.parentWays(graph.entity(midpoint.edge[0])),
graph.parentWays(graph.entity(midpoint.edge[1])));
parents.forEach(function(way) {
for (var i = 0; i < way.nodes.length - 1; i++) {
if ((way.nodes[i] === midpoint.edge[0] &&
way.nodes[i + 1] === midpoint.edge[1]) ||
(way.nodes[i] === midpoint.edge[1] &&
way.nodes[i + 1] === midpoint.edge[0])) {
graph = graph.replace(graph.entity(way.id).addNode(node.id, i + 1));
}
}
});
return graph;

View File

@@ -139,31 +139,13 @@ iD.modes.Select = function(context, selection, initial) {
if (datum instanceof iD.Way && !target.classed('fill')) {
var choice = iD.geo.chooseIndex(datum,
d3.mouse(context.surface().node()), context),
node = iD.Node({ loc: choice.loc });
node = iD.Node();
var prev = datum.nodes[choice.index - 1],
next = datum.nodes[choice.index],
prevParents = context.graph().parentWays({ id: prev }),
ways = [];
next = datum.nodes[choice.index];
for (var i = 0; i < prevParents.length; i++) {
var p = prevParents[i];
for (var k = 0; k < p.nodes.length; k++) {
if (p.nodes[k] === prev) {
if (p.nodes[k-1] === next) {
ways.push({ id: p.id, index: k});
break;
} else if (p.nodes[k+1] === next) {
ways.push({ id: p.id, index: k+1});
break;
}
}
}
}
context.perform(iD.actions.AddEntity(node),
iD.actions.AddMidpoint({ ways: ways, loc: node.loc }, node),
context.perform(
iD.actions.AddMidpoint({loc: choice.loc, edge: [prev, next]}, node),
t('operations.add.annotation.vertex'));
d3.event.preventDefault();

View File

@@ -72,9 +72,11 @@ iD.Map = function(context) {
all = _.compact(_.values(complete));
filter = function(d) {
if (d.type === 'midpoint') {
for (var i = 0; i < d.ways.length; i++) {
if (d.ways[i].id in complete) return true;
}
var a = graph.entity(d.edge[0]),
b = graph.entity(d.edge[1]);
return !a || !b ||
_.intersection(graph.parentWays(a), all).length ||
_.intersection(graph.parentWays(b), all).length;
} else {
return d.id in complete;
}

View File

@@ -19,17 +19,14 @@ iD.svg.Midpoints = function(projection) {
b = nodes[j + 1],
id = [a.id, b.id].sort().join('-');
if (midpoints[id]) {
midpoints[id].ways.push({id: entity.id, index: j + 1});
} else {
if (!midpoints[id]) {
var loc = iD.geo.interp(a.loc, b.loc, 0.5);
if (extent.intersects(loc) && iD.geo.dist(projection(a.loc), projection(b.loc)) > 40) {
midpoints[id] = {
type: 'midpoint',
id: id,
loc: loc,
ways: [{id: entity.id, index: j + 1}]
edge: [a.id, b.id]
};
}
}

View File

@@ -1,22 +1,37 @@
describe("iD.actions.AddMidpoint", function () {
it("adds the node at the midpoint location", function () {
var node = iD.Node(),
midpoint = {loc: [1, 2], ways: []},
graph = iD.actions.AddMidpoint(midpoint, node)(iD.Graph());
a = iD.Node(),
b = iD.Node(),
midpoint = {loc: [1, 2], edge: [a.id, b.id]},
graph = iD.actions.AddMidpoint(midpoint, node)(iD.Graph([a, b]));
expect(graph.entity(node.id).loc).to.eql([1, 2]);
});
it("adds the node to all ways at the respective indexes", function () {
it("adds the node to a way that contains the given edge in forward order", function () {
var node = iD.Node(),
a = iD.Node(),
b = iD.Node(),
w1 = iD.Way(),
w2 = iD.Way({nodes: [a.id, b.id]}),
midpoint = {loc: [1, 2], ways: [{id: w1.id, index: 0}, {id: w2.id, index: 1}]},
midpoint = {loc: [1, 2], edge: [a.id, b.id]},
graph = iD.actions.AddMidpoint(midpoint, node)(iD.Graph([a, b, w1, w2]));
expect(graph.entity(w1.id).nodes).to.eql([node.id]);
expect(graph.entity(w1.id).nodes).to.eql([]);
expect(graph.entity(w2.id).nodes).to.eql([a.id, node.id, b.id]);
});
it("adds the node to a way that contains the given edge in reverse order", function () {
var node = iD.Node(),
a = iD.Node(),
b = iD.Node(),
w1 = iD.Way(),
w2 = iD.Way({nodes: [b.id, a.id]}),
midpoint = {loc: [1, 2], edge: [a.id, b.id]},
graph = iD.actions.AddMidpoint(midpoint, node)(iD.Graph([a, b, w1, w2]));
expect(graph.entity(w1.id).nodes).to.eql([]);
expect(graph.entity(w2.id).nodes).to.eql([b.id, node.id, a.id]);
});
});