Skip to content

Commit

Permalink
Prevent degenerate ways caused by deleting a corner of a triangle
Browse files Browse the repository at this point in the history
  • Loading branch information
bhousel committed Nov 29, 2023
1 parent 5fd6668 commit 9525e59
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 19 deletions.
3 changes: 1 addition & 2 deletions modules/modes/DrawAreaMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ export class DrawAreaMode extends AbstractMode {
// If any issues, revert back to how things were before we started.
const graph = editor.stable.graph;
const drawWay = this.drawWayID && graph.hasEntity(this.drawWayID);
const length = drawWay?.nodes?.length || 0;
if (length < 4) {
if (!drawWay || drawWay.isDegenerate()) {
if (DEBUG) {
console.log('DrawAreaMode: draw way invalid, rolling back'); // eslint-disable-line no-console
}
Expand Down
3 changes: 1 addition & 2 deletions modules/modes/DrawLineMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ export class DrawLineMode extends AbstractMode {
// If any issues, revert back to how things were before we started.
const graph = editor.stable.graph;
const drawWay = this.drawWayID && graph.hasEntity(this.drawWayID);
const length = drawWay?.nodes?.length || 0;
if (length < 2 || this.firstNodeID === this.lastNodeID) {
if (!drawWay || drawWay.isDegenerate()) {
if (DEBUG) {
console.log('DrawLineMode: draw way invalid, rolling back'); // eslint-disable-line no-console
}
Expand Down
2 changes: 1 addition & 1 deletion modules/osm/way.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ Object.assign(osmWay.prototype, {


isDegenerate: function() {
return (new Set(this.nodes).size < (this.isArea() ? 3 : 2));
return (new Set(this.nodes).size < (this.isClosed() ? 3 : 2));
},


Expand Down
14 changes: 0 additions & 14 deletions test/spec/actions/circularize.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,6 @@ describe('actionCircularize', function () {
expect(graph.entity('-').nodes).to.have.length(20);
});

it('circularizes a closed single line way', function () {
var graph = new Rapid.Graph([
Rapid.osmNode({id: 'a', loc: [0, 0]}),
Rapid.osmNode({id: 'b', loc: [0, 2]}),
Rapid.osmWay({id: '-', nodes: ['a', 'b', 'a']})
]);

expect(area('-', graph)).to.eql(0);

graph = Rapid.actionCircularize('-', projection)(graph);

expect(isCircular('-', graph)).to.be.ok;
});

it('not disable circularize when its not circular', function(){
var graph = new Rapid.Graph([
Rapid.osmNode({id: 'a', loc: [0, 0]}),
Expand Down
4 changes: 4 additions & 0 deletions test/spec/osm/way.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@ describe('osmWay', function () {
expect(Rapid.osmWay({nodes: ['a', 'b']}).isDegenerate()).to.equal(false);
});

it('returns true for a linear way that doubles back on itself', function () {
expect(iD.osmWay({nodes: ['a', 'b', 'a']}).isDegenerate()).to.equal(true);
});

it('returns true for an area with zero, one, or two unique nodes', function () {
expect(Rapid.osmWay({tags: {area: 'yes'}, nodes: []}).isDegenerate()).to.equal(true);
expect(Rapid.osmWay({tags: {area: 'yes'}, nodes: ['a', 'a']}).isDegenerate()).to.equal(true);
Expand Down

0 comments on commit 9525e59

Please sign in to comment.