Skip to content

Commit

Permalink
Move magic wand methods to MagicWandInteraction.js
Browse files Browse the repository at this point in the history
  • Loading branch information
lehecht committed Oct 2, 2023
1 parent e9ac4f6 commit a96d245
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 25 deletions.
26 changes: 2 additions & 24 deletions resources/assets/js/annotations/components/annotationCanvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -524,14 +524,6 @@ export default {
});
return;
}
let curatedPointSet = this.removeDuplicatedPoints(points);
if (points.length !== curatedPointSet.length) {
points = curatedPointSet;
// link polygon start and end by adding polygon start at the end
points.push(points[0]); // x coordinate
points.push(points[1]); // y coordinate
}
}
e.feature.set('color', this.selectedLabel.color);
Expand All @@ -557,24 +549,10 @@ export default {
},
isInvalidPolygon(e) {
let geometry = e.feature.getGeometry();
let points = geometry.getCoordinates();
return this.getPointStringSet(points[0]).size < 3;
let points = geometry.getCoordinates()[0];
return (new Set(points.map(xy => String([xy])))).size < 3;
},
removeDuplicatedPoints(points) {
let x = points.filter((_,i) => i%2==0);
let y = points.filter((_,i) => i%2==1);
let coordinates = x.map((xi,i) => {return [xi,y[i]];});
let pointStringSet = Array.from(this.getPointStringSet(coordinates));
return pointStringSet.map(xy => this.convertStringToPoint(xy)).flat();
},
getPointStringSet(points) {
return new Set(points.map(xy => String([xy])));
},
convertStringToPoint(xy) {
return xy.split(',').map(Number);
},
deleteSelectedAnnotations() {
if (!this.modifyInProgress && this.hasSelectedAnnotations && confirm('Are you sure you want to delete all selected annotations?')) {
this.$emit('delete', this.selectedAnnotations);
Expand Down
75 changes: 74 additions & 1 deletion resources/assets/js/annotations/ol/MagicWandInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ class MagicWandInteraction extends PointerInteraction {
if (this.isShowingCross) {
this.sketchSource.removeFeature(this.sketchFeature);
} else {
this.dispatchEvent({type: 'drawend', feature: this.sketchFeature});
this.filterSketchCoordinates();
this.dispatchEvent({ type: 'drawend', feature: this.sketchFeature });
}

this.sketchFeature = null;
Expand Down Expand Up @@ -375,6 +376,78 @@ class MagicWandInteraction extends PointerInteraction {
}
}
}

/**
* Removes duplicated points and dangeling lines
*/
filterSketchCoordinates() {
let points = this.sketchFeature.getGeometry().getCoordinates()[0];
points = this.removeDuplicatedPoints(points);
points = this.removeDangelingLines(points);
points.push(points[0]);
this.sketchFeature.getGeometry().setCoordinates([points]);
}

/**
* Removes duplicated points
*/
removeDuplicatedPoints(points) {
let pointStringSet = Array.from(new Set(points.map(xy => String([xy]))));
return pointStringSet.map(xy => xy.split(',').map(Number));
}

/*
* Removes dangeling lines by deleting middle point of three subsequent points with equal x or y values
*/
removeDangelingLines(coordinates) {
// filter by x values
let i = 0;
let endNotReached = true;
while (endNotReached) {
try {
if (coordinates[i][0] === coordinates[i + 1][0]) {
if (coordinates[i + 1][0] === coordinates[i + 2][0]) {
coordinates.splice(i + 1, 1)
if (coordinates[i + 1][0] !== coordinates[i + 2][0]) {
i++;
}
} else {
i++;
}
} else {
i++;
}
} catch (err) {
break;
}
endNotReached = i < coordinates.length - 2;
};

// filter by y values
i = 0;
endNotReached = true;
while (endNotReached) {
try {
if (coordinates[i][1] === coordinates[i + 1][1]) {
if (coordinates[i + 1][1] === coordinates[i + 2][1]) {
coordinates.splice(i + 1, 1)
if (coordinates[i + 1][1] !== coordinates[i + 2][1]) {
i++;
}
} else {
i++;
}
} else {
i++;
}
} catch (err) {
break;
}
endNotReached = i < coordinates.length - 2;
};

return coordinates;
}
}

export default MagicWandInteraction;

0 comments on commit a96d245

Please sign in to comment.