Skip to content

Commit

Permalink
Intelligent polygon ediing
Browse files Browse the repository at this point in the history
  • Loading branch information
bsekachev committed Jul 21, 2020
1 parent 7ecdcf1 commit b543c51
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 44 deletions.
2 changes: 1 addition & 1 deletion cvat-canvas/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cvat-canvas/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cvat-canvas",
"version": "2.0.1",
"version": "2.0.2",
"description": "Part of Computer Vision Annotation Tool which presents its canvas library",
"main": "src/canvas.ts",
"scripts": {
Expand Down
118 changes: 76 additions & 42 deletions cvat-canvas/src/typescript/editHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,87 @@ export class EditHandlerImpl implements EditHandler {
const [start, stop] = [this.editData.pointID, stopPointID]
.sort((a, b): number => +a - +b);

if (this.editData.state.shapeType === 'polygon') {
if (start !== this.editData.pointID) {
linePoints.reverse();
if (this.editData.state.shapeType !== 'polygon') {
let points = null;
const { offset } = this.geometry;

if (this.editData.state.shapeType === 'polyline') {
if (start !== this.editData.pointID) {
linePoints.reverse();
}
points = oldPoints.slice(0, start)
.concat(linePoints)
.concat(oldPoints.slice(stop + 1));
} else {
points = oldPoints.concat(linePoints.slice(0, -1));
}

const firstPart = oldPoints.slice(0, start)
.concat(linePoints)
.concat(oldPoints.slice(stop + 1));
points = pointsToNumberArray(points.join(' '))
.map((coord: number): number => coord - offset);

const { state } = this.editData;
this.edit({
enabled: false,
});
this.onEditDone(state, points);

const secondPart = oldPoints.slice(start, stop)
.concat(linePoints.slice(1).reverse());
return;
}

if (firstPart.length < 3 || secondPart.length < 3) {
this.cancel();
return;
const cutIndexes1 = oldPoints.reduce((acc: string[], _: string, i: number) =>
i >= stop || i <= start ? [...acc, i] : acc, []);
const cutIndexes2 = oldPoints.reduce((acc: string[], _: string, i: number) =>
i <= stop && i >= start ? [...acc, i] : acc, []);

const curveLength = (indexes: number[]) => {
const points = indexes.map((index: number): string => oldPoints[index])
.map((point: string): string[] => point.split(','))
.map((point: string[]): number[] => [+point[0], +point[1]]);
let length = 0;
for (let i = 1; i < points.length; i++) {
length += Math.sqrt(
(points[i][0] - points[i - 1][0]) ** 2
+ (points[i][1] - points[i - 1][1]) ** 2,
);
}

return length;
}

const pointsCriteria = cutIndexes1.length > cutIndexes2.length;
const lengthCriteria = curveLength(cutIndexes1) > curveLength(cutIndexes2);

if (start !== this.editData.pointID) {
linePoints.reverse();
}

const firstPart = oldPoints.slice(0, start)
.concat(linePoints)
.concat(oldPoints.slice(stop + 1));
const secondPart = oldPoints.slice(start, stop)
.concat(linePoints.slice(1).reverse());

if (firstPart.length < 3 || secondPart.length < 3) {
this.cancel();
return;
}

// We do not need these events any more
this.canvas.off('mousedown.edit');
this.canvas.off('mousemove.edit');

(this.editLine as any).draw('stop');
this.editLine.remove();
this.editLine = null;

if (pointsCriteria && lengthCriteria) {
this.clones.push(this.canvas.polygon(firstPart.join(' ')));
this.selectPolygon(this.clones[0]);
// left indexes1 and
} else if (!pointsCriteria && !lengthCriteria) {
this.clones.push(this.canvas.polygon(secondPart.join(' ')));
this.selectPolygon(this.clones[0]);
} else {
for (const points of [firstPart, secondPart]) {
this.clones.push(this.canvas.polygon(points.join(' '))
.attr('fill', this.editedShape.attr('fill'))
Expand All @@ -212,39 +276,9 @@ export class EditHandlerImpl implements EditHandler {
clone.removeClass('cvat_canvas_shape_splitting');
});
}

// We do not need these events any more
this.canvas.off('mousedown.edit');
this.canvas.off('mousemove.edit');

(this.editLine as any).draw('stop');
this.editLine.remove();
this.editLine = null;

return;
}

let points = null;
const { offset } = this.geometry;
if (this.editData.state.shapeType === 'polyline') {
if (start !== this.editData.pointID) {
linePoints.reverse();
}
points = oldPoints.slice(0, start)
.concat(linePoints)
.concat(oldPoints.slice(stop + 1));
} else {
points = oldPoints.concat(linePoints.slice(0, -1));
}

points = pointsToNumberArray(points.join(' '))
.map((coord: number): number => coord - offset);

const { state } = this.editData;
this.edit({
enabled: false,
});
this.onEditDone(state, points);
return;
}

private setupPoints(enabled: boolean): void {
Expand Down

0 comments on commit b543c51

Please sign in to comment.