Split on self-intersections

This commit is contained in:
John Firebaugh
2013-03-29 14:32:06 -07:00
parent bbd4cb80b6
commit 2bd6178f07
2 changed files with 53 additions and 4 deletions

View File

@@ -15,9 +15,17 @@ iD.actions.Split = function(nodeId, newWayIds) {
parents = graph.parentWays(node);
return parents.filter(function(parent) {
return parent.isClosed() ||
(parent.first() !== nodeId &&
parent.last() !== nodeId);
if (parent.isClosed()) {
return true;
}
for (var i = 1; i < parent.nodes.length - 1; i++) {
if (parent.nodes[i] === nodeId) {
return true;
}
}
return false;
});
}
@@ -41,7 +49,7 @@ iD.actions.Split = function(nodeId, newWayIds) {
nodesB = nodes.slice(idxB).concat(nodes.slice(0, idxA + 1));
}
} else {
var idx = _.indexOf(wayA.nodes, nodeId);
var idx = _.indexOf(wayA.nodes, nodeId, 1);
nodesA = wayA.nodes.slice(0, idx + 1);
nodesB = wayA.nodes.slice(idx);
}

View File

@@ -25,6 +25,18 @@ describe("iD.actions.Split", function () {
expect(iD.actions.Split('*').disabled(graph)).not.to.be.ok;
});
it("returns falsy for a self-intersection", function () {
var graph = iD.Graph({
'a': iD.Node({id: 'a'}),
'b': iD.Node({id: 'b'}),
'c': iD.Node({id: 'c'}),
'd': iD.Node({id: 'c'}),
'-': iD.Way({id: '-', nodes: ['a', 'b', 'c', 'a', 'd']})
});
expect(iD.actions.Split('a').disabled(graph)).not.to.be.ok;
});
it("returns 'not_eligible' for the first node of a single way", function () {
var graph = iD.Graph({
'a': iD.Node({id: 'a'}),
@@ -148,6 +160,35 @@ describe("iD.actions.Split", function () {
expect(graph.entity('¦').nodes).to.eql(['*', 'd']);
});
it("splits self-intersecting ways", function () {
// Situation:
// b
// / |
// / |
// c - a -- d
//
// Split at a.
//
// Expected result:
// b
// / |
// / |
// c - a == d
//
var graph = iD.Graph({
'a': iD.Node({id: 'a'}),
'b': iD.Node({id: 'b'}),
'c': iD.Node({id: 'c'}),
'd': iD.Node({id: 'c'}),
'-': iD.Way({id: '-', nodes: ['a', 'b', 'c', 'a', 'd']})
});
graph = iD.actions.Split('a', ['='])(graph);
expect(graph.entity('-').nodes).to.eql(['a', 'b', 'c', 'a']);
expect(graph.entity('=').nodes).to.eql(['a', 'd']);
});
it("splits a closed way at the given point and its antipode", function () {
// Situation:
// a ---- b