diff --git a/resources/assets/js/annotations/components/annotationCanvas.vue b/resources/assets/js/annotations/components/annotationCanvas.vue index ae1f6c34a..09a2fe119 100644 --- a/resources/assets/js/annotations/components/annotationCanvas.vue +++ b/resources/assets/js/annotations/components/annotationCanvas.vue @@ -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); @@ -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); diff --git a/resources/assets/js/annotations/ol/MagicWandInteraction.js b/resources/assets/js/annotations/ol/MagicWandInteraction.js index 78c4784b5..338be9b40 100644 --- a/resources/assets/js/annotations/ol/MagicWandInteraction.js +++ b/resources/assets/js/annotations/ol/MagicWandInteraction.js @@ -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; @@ -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;