Skip to content

Commit

Permalink
Avoid consecutive identical nodes when adding a midpoint
Browse files Browse the repository at this point in the history
Previously, adding a midpoint to an invalidly doubled-back
segment (aba) resulted in a self-intersection with an invalid
consecutive node (accba). Now a self-intersection is still
produced, but with only one c node (acba).

Refs #1296
  • Loading branch information
jfirebaugh committed Sep 1, 2013
1 parent 3b16886 commit e11ab96
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions js/id/actions/add_midpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ iD.actions.AddMidpoint = function(midpoint, node) {
(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));

// Add only one midpoint on doubled-back segments,
// turning them into self-intersections.
return;
}
}
});
Expand Down
17 changes: 17 additions & 0 deletions test/spec/actions/add_midpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,21 @@ describe("iD.actions.AddMidpoint", function () {
expect(graph.entity(w1.id).nodes).to.eql([]);
expect(graph.entity(w2.id).nodes).to.eql([b.id, node.id, a.id]);
});

it("turns an invalid double-back into a self-intersection", function () {
// a====b (aba)
// Expected result (converts to a valid loop):
// a---b (acba)
// \ /
// c

var a = iD.Node(),
b = iD.Node(),
c = iD.Node(),
w = iD.Way({nodes: [a.id, b.id, a.id]}),
midpoint = {loc: [1, 2], edge: [a.id, b.id]},
graph = iD.actions.AddMidpoint(midpoint, c)(iD.Graph([a, b, w]));

expect(graph.entity(w.id).nodes).to.eql([a.id, c.id, b.id, a.id]);
});
});

0 comments on commit e11ab96

Please sign in to comment.