Support straightening of points

(closes #6217)

- Split `actionStraighten` into `actionStraightenWay` and `actionStraightenNodes`
- Now `operationStraighten` chooses the correct action depending on selected entities
- Also move `getSmallestSurroundingRectangle` from `actionReflect` to `geo.js`
This commit is contained in:
Bryan Housel
2019-04-24 01:52:34 -04:00
parent bc3353d386
commit 05949608aa
13 changed files with 314 additions and 124 deletions
+2 -1
View File
@@ -59,7 +59,8 @@
<script src='spec/actions/reverse.js'></script>
<script src='spec/actions/revert.js'></script>
<script src='spec/actions/split.js'></script>
<script src='spec/actions/straighten.js'></script>
<script src='spec/actions/straighten_nodes.js'></script>
<script src='spec/actions/straighten_way.js'></script>
<script src='spec/actions/unrestrict_turn.js'></script>
<script src='spec/actions/reflect.js'></script>
<script src='spec/actions/extract.js'></script>
+88
View File
@@ -0,0 +1,88 @@
describe('iD.actionStraightenNodes', function () {
var projection = function (l) { return l; };
projection.invert = projection;
it('straightens points', function() {
var graph = iD.coreGraph([
iD.osmNode({ id: 'a', loc: [0, -1] }),
iD.osmNode({ id: 'b', loc: [5, 1], tags: { foo: 'bar' } }),
iD.osmNode({ id: 'c', loc: [10, -1] }), // untagged
iD.osmNode({ id: 'd', loc: [15, 1] })
]);
graph = iD.actionStraightenNodes(['a','b','c','d'], projection)(graph);
expect(graph.entity('a').loc[0]).to.be.closeTo(0, 1e-6);
expect(graph.entity('a').loc[1]).to.be.closeTo(0, 1e-6);
expect(graph.entity('b').loc[0]).to.be.closeTo(5, 1e-6);
expect(graph.entity('b').loc[1]).to.be.closeTo(0, 1e-6);
expect(graph.entity('c').loc[0]).to.be.closeTo(10, 1e-6); // doesn't delete untagged
expect(graph.entity('c').loc[1]).to.be.closeTo(0, 1e-6); // doesn't delete untagged
expect(graph.entity('d').loc[0]).to.be.closeTo(15, 1e-6);
expect(graph.entity('d').loc[1]).to.be.closeTo(0, 1e-6);
});
describe('transitions', function () {
it('is transitionable', function() {
expect(iD.actionStraightenNodes().transitionable).to.be.true;
});
it('straighten at t = 0', function() {
var graph = iD.coreGraph([
iD.osmNode({ id: 'a', loc: [0, -1] }),
iD.osmNode({ id: 'b', loc: [5, 1], tags: { foo: 'bar' } }),
iD.osmNode({ id: 'c', loc: [10, -1] }), // untagged
iD.osmNode({ id: 'd', loc: [15, 1] })
]);
graph = iD.actionStraightenNodes(['a','b','c','d'], projection)(graph, 0);
expect(graph.entity('a').loc[0]).to.be.closeTo(0, 1e-6);
expect(graph.entity('a').loc[1]).to.be.closeTo(-1, 1e-6);
expect(graph.entity('b').loc[0]).to.be.closeTo(5, 1e-6);
expect(graph.entity('b').loc[1]).to.be.closeTo(1, 1e-6);
expect(graph.entity('c').loc[0]).to.be.closeTo(10, 1e-6); // doesn't delete untagged
expect(graph.entity('c').loc[1]).to.be.closeTo(-1, 1e-6); // doesn't delete untagged
expect(graph.entity('d').loc[0]).to.be.closeTo(15, 1e-6);
expect(graph.entity('d').loc[1]).to.be.closeTo(1, 1e-6);
});
it('straighten at t = 0.5', function() {
var graph = iD.coreGraph([
iD.osmNode({ id: 'a', loc: [0, -1] }),
iD.osmNode({ id: 'b', loc: [5, 1], tags: { foo: 'bar' } }),
iD.osmNode({ id: 'c', loc: [10, -1] }), // untagged
iD.osmNode({ id: 'd', loc: [15, 1] })
]);
graph = iD.actionStraightenNodes(['a','b','c','d'], projection)(graph, 0.5);
expect(graph.entity('a').loc[0]).to.be.closeTo(0, 1e-6);
expect(graph.entity('a').loc[1]).to.be.closeTo(-0.5, 1e-6);
expect(graph.entity('b').loc[0]).to.be.closeTo(5, 1e-6);
expect(graph.entity('b').loc[1]).to.be.closeTo(0.5, 1e-6);
expect(graph.entity('c').loc[0]).to.be.closeTo(10, 1e-6); // doesn't delete untagged
expect(graph.entity('c').loc[1]).to.be.closeTo(-0.5, 1e-6); // doesn't delete untagged
expect(graph.entity('d').loc[0]).to.be.closeTo(15, 1e-6);
expect(graph.entity('d').loc[1]).to.be.closeTo(0.5, 1e-6);
});
it('straighten at t = 1', function() {
var graph = iD.coreGraph([
iD.osmNode({ id: 'a', loc: [0, -1] }),
iD.osmNode({ id: 'b', loc: [5, 1], tags: { foo: 'bar' } }),
iD.osmNode({ id: 'c', loc: [10, -1] }), // untagged
iD.osmNode({ id: 'd', loc: [15, 1] })
]);
graph = iD.actionStraightenNodes(['a','b','c','d'], projection)(graph, 1);
expect(graph.entity('a').loc[0]).to.be.closeTo(0, 1e-6);
expect(graph.entity('a').loc[1]).to.be.closeTo(0, 1e-6);
expect(graph.entity('b').loc[0]).to.be.closeTo(5, 1e-6);
expect(graph.entity('b').loc[1]).to.be.closeTo(0, 1e-6);
expect(graph.entity('c').loc[0]).to.be.closeTo(10, 1e-6); // doesn't delete untagged
expect(graph.entity('c').loc[1]).to.be.closeTo(0, 1e-6); // doesn't delete untagged
expect(graph.entity('d').loc[0]).to.be.closeTo(15, 1e-6);
expect(graph.entity('d').loc[1]).to.be.closeTo(0, 1e-6);
});
});
});
@@ -1,4 +1,4 @@
describe('iD.actionStraighten', function () {
describe('iD.actionStraightenWay', function () {
var projection = d3.geoMercator();
describe('#disabled', function () {
@@ -10,7 +10,7 @@ describe('iD.actionStraighten', function () {
iD.osmNode({id: 'd', loc: [3, 0]}),
iD.osmWay({id: '-', nodes: ['a', 'b', 'c', 'd']})
]);
expect(iD.actionStraighten(['-'], projection).disabled(graph)).not.to.be.ok;
expect(iD.actionStraightenWay(['-'], projection).disabled(graph)).not.to.be.ok;
});
it('returns \'too_bendy\' for ways with internal nodes far off centerline', function () {
@@ -21,7 +21,7 @@ describe('iD.actionStraighten', function () {
iD.osmNode({id: 'd', loc: [3, 0]}),
iD.osmWay({id: '-', nodes: ['a', 'b', 'c', 'd']})
]);
expect(iD.actionStraighten(['-'], projection).disabled(graph)).to.equal('too_bendy');
expect(iD.actionStraightenWay(['-'], projection).disabled(graph)).to.equal('too_bendy');
});
it('returns \'too_bendy\' for ways with coincident start/end nodes', function () {
@@ -32,7 +32,7 @@ describe('iD.actionStraighten', function () {
iD.osmNode({id: 'd', loc: [0, 0]}),
iD.osmWay({id: '-', nodes: ['a', 'b', 'c', 'd']})
]);
expect(iD.actionStraighten(['-'], projection).disabled(graph)).to.equal('too_bendy');
expect(iD.actionStraightenWay(['-'], projection).disabled(graph)).to.equal('too_bendy');
});
});
@@ -45,7 +45,7 @@ describe('iD.actionStraighten', function () {
iD.osmWay({id: '-', nodes: ['a', 'b', 'c']})
]);
graph = iD.actionStraighten(['-'], projection)(graph);
graph = iD.actionStraightenWay(['-'], projection)(graph);
expect(graph.entity('-').nodes).to.eql(['a', 'c']);
expect(graph.hasEntity('b')).to.eq(undefined);
});
@@ -58,7 +58,7 @@ describe('iD.actionStraighten', function () {
iD.osmWay({id: '-', nodes: ['a', 'b', 'c']})
]);
graph = iD.actionStraighten(['-'], projection)(graph);
graph = iD.actionStraightenWay(['-'], projection)(graph);
expect(graph.entity('-').nodes).to.eql(['a', 'b', 'c']);
expect(graph.entity('b').loc[0]).to.be.closeTo(1, 1e-6);
expect(graph.entity('b').loc[1]).to.be.closeTo(0, 1e-6);
@@ -73,7 +73,7 @@ describe('iD.actionStraighten', function () {
iD.osmWay({id: '=', nodes: ['b']})
]);
graph = iD.actionStraighten(['-'], projection)(graph);
graph = iD.actionStraightenWay(['-'], projection)(graph);
expect(graph.entity('-').nodes).to.eql(['a', 'b', 'c']);
expect(graph.entity('b').loc[0]).to.be.closeTo(1, 1e-6);
expect(graph.entity('b').loc[1]).to.be.closeTo(0, 1e-6);
@@ -94,7 +94,7 @@ describe('iD.actionStraighten', function () {
iD.Way({id: '--', nodes: ['d', 'e', 'f', 'g', 'h']})
]);
graph = iD.actionStraighten(['-', '--'], projection)(graph);
graph = iD.actionStraightenWay(['-', '--'], projection)(graph);
expect(graph.entity('-').nodes).to.eql(['a', 'b', 'd']);
expect(graph.entity('--').nodes).to.eql(['d', 'f', 'h']);
expect(graph.entity('f').loc[0]).to.be.closeTo(5, 1e-6);
@@ -117,7 +117,7 @@ describe('iD.actionStraighten', function () {
iD.Way({id: '--', nodes: ['h', 'g', 'f', 'e', 'd']})
]);
graph = iD.actionStraighten(['-', '--'], projection)(graph);
graph = iD.actionStraightenWay(['-', '--'], projection)(graph);
expect(graph.entity('-').nodes).to.eql(['a', 'b', 'd']);
expect(graph.entity('--').nodes).to.eql(['h', 'f', 'd']);
expect(graph.entity('f').loc[0]).to.be.closeTo(5, 1e-6);
@@ -127,7 +127,7 @@ describe('iD.actionStraighten', function () {
describe('transitions', function () {
it('is transitionable', function() {
expect(iD.actionStraighten().transitionable).to.be.true;
expect(iD.actionStraightenWay().transitionable).to.be.true;
});
it('straighten at t = 0', function() {
@@ -139,7 +139,7 @@ describe('iD.actionStraighten', function () {
iD.osmWay({id: '-', nodes: ['a', 'b', 'c', 'd']})
]);
graph = iD.actionStraighten(['-'], projection)(graph, 0);
graph = iD.actionStraightenWay(['-'], projection)(graph, 0);
expect(graph.entity('-').nodes).to.eql(['a', 'b', 'c', 'd']);
expect(graph.entity('b').loc[0]).to.be.closeTo(1, 1e-6);
expect(graph.entity('b').loc[1]).to.be.closeTo(0.01, 1e-6);
@@ -156,7 +156,7 @@ describe('iD.actionStraighten', function () {
iD.osmWay({id: '-', nodes: ['a', 'b', 'c', 'd']})
]);
graph = iD.actionStraighten(['-'], projection)(graph, 0.5);
graph = iD.actionStraightenWay(['-'], projection)(graph, 0.5);
expect(graph.entity('-').nodes).to.eql(['a', 'b', 'c', 'd']);
expect(graph.entity('b').loc[0]).to.be.closeTo(1, 1e-6);
expect(graph.entity('b').loc[1]).to.be.closeTo(0.005, 1e-6);
@@ -173,7 +173,7 @@ describe('iD.actionStraighten', function () {
iD.osmWay({id: '-', nodes: ['a', 'b', 'c', 'd']})
]);
graph = iD.actionStraighten(['-'], projection)(graph, 1);
graph = iD.actionStraightenWay(['-'], projection)(graph, 1);
expect(graph.entity('-').nodes).to.eql(['a', 'b', 'd']);
expect(graph.entity('b').loc[0]).to.be.closeTo(1, 1e-6);
expect(graph.entity('b').loc[1]).to.be.closeTo(0, 1e-6);
+13
View File
@@ -446,6 +446,19 @@ describe('iD.geo - geometry', function() {
});
});
describe('geoGetSmallestSurroundingRectangle', function() {
it('calculates a smallest surrounding rectangle', function() {
// +----b---------d
// | |
// a---------c----+
var points = [[0, -1], [5, 1], [10, -1], [15, 1]];
var ssr = iD.geoGetSmallestSurroundingRectangle(points);
expect(ssr.poly).to.eql([[0, -1], [0, 1], [15, 1], [15, -1], [0, -1]]);
expect(ssr.angle).to.eql(0);
});
});
describe('geoPathLength', function() {
it('calculates a simple path length', function() {
var path = [[0, 0], [0, 1], [3, 5]];