Skip to content

Commit

Permalink
r for relabeling Seg2d (#443)
Browse files Browse the repository at this point in the history
* r for redrawing

* revert label if relabeling fails; added "r" in help page
  • Loading branch information
haofengac authored and fyu committed Sep 12, 2018
1 parent d5b0497 commit bd2b373
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 36 deletions.
3 changes: 3 additions & 0 deletions app/src/annotation/image.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
<div>
<p><span><kbd>c</kbd></span> <span>When hovering over midpoint, change the edge to bezier curve</span></p>
</div>
<div>
<p><span><kbd>r</kbd></span> <span>redraw a selected segmentation label</span></p>
</div>
<div>
<p><span><kbd>s</kbd></span> <span>Toggle on/off quick draw mode</span></p>
</div>
Expand Down
119 changes: 83 additions & 36 deletions app/src/js/seg2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,8 @@ export function Seg2d(sat, id, optionalAttributes) {
if (mousePos) {
// if mousePos given, start drawing
// set label type
if (Seg2d.closed) {
this.newPoly = new Polygon();
this.tempPoly = new Polygon();
} else {
this.newPoly = new Path();
this.tempPoly = new Path();
}
this.setState(SegStates.DRAW);

let occupiedShape = this.satItem.getOccupiedShape(mousePos);
if (occupiedShape instanceof Vertex) {
this.newPoly.pushVertex(occupiedShape);
this.tempPoly.pushVertex(occupiedShape);
this.satItem.pushToHiddenMap([occupiedShape]);
this.latestSharedVertex = occupiedShape;
} else {
let firstVertex = new Vertex(mousePos.x, mousePos.y);
this.newPoly.pushVertex(firstVertex);
this.tempPoly.pushVertex(firstVertex);
this.satItem.pushToHiddenMap([firstVertex]);
this.latestSharedVertex = null;
}

this.tempVertex = new Vertex(mousePos.x, mousePos.y);
this.tempPoly.pushVertex(this.tempVertex);
this.selectedShape = null;
this.initDrawing(mousePos);
}
}

Expand All @@ -82,6 +58,33 @@ Seg2d.useDoubleClick = true;
Seg2d.closed =
document.getElementById('label_type').innerHTML === 'segmentation';

Seg2d.prototype.initDrawing = function(mousePos) {
if (Seg2d.closed) {
this.newPoly = new Polygon();
this.tempPoly = new Polygon();
} else {
this.newPoly = new Path();
this.tempPoly = new Path();
}
let occupiedShape = this.satItem.getOccupiedShape(mousePos);
if (occupiedShape instanceof Vertex) {
this.newPoly.pushVertex(occupiedShape);
this.tempPoly.pushVertex(occupiedShape);
this.satItem.pushToHiddenMap([occupiedShape]);
this.latestSharedVertex = occupiedShape;
} else {
let firstVertex = new Vertex(mousePos.x, mousePos.y);
this.newPoly.pushVertex(firstVertex);
this.tempPoly.pushVertex(firstVertex);
this.satItem.pushToHiddenMap([firstVertex]);
this.latestSharedVertex = null;
}

this.tempVertex = new Vertex(mousePos.x, mousePos.y);
this.tempPoly.pushVertex(this.tempVertex);
this.selectedShape = null;
};

Seg2d.prototype.identityInterpolation = function(startLabel, endLabel, weight) { // eslint-disable-line
this.deleteAllPolyline();
let targetLabel;
Expand Down Expand Up @@ -179,6 +182,9 @@ Seg2d.prototype.setState = function(state) {
this.satItem.resetHiddenMap(this.getAllHiddenShapes());
this.satItem.redrawHiddenCanvas();
}
this.newPoly = null;
this.tempPoly = null;
this.tempVertex = null;
} else if (state === SegStates.DRAW) {
// reset hiddenMap with all existing vertices
let shapes = [];
Expand Down Expand Up @@ -338,11 +344,21 @@ Seg2d.prototype.handleQuickdraw = function() {

if (this.newPoly.isValidShape()) {
this.addPolyline(this.newPoly);
this.tempVertex.delete();
this.tempPoly.delete();
this.tempVertex = null;
this.tempPoly = null;
if (this.polyBuffer) {
for (let poly of this.polyBuffer) {
poly.delete();
}
}
} else if (this.polyBuffer) {
for (let poly of this.polyBuffer) {
this.polys.push(poly);
}
}
this.polyBuffer = null;
this.tempVertex.delete();
this.tempPoly.delete();
this.tempVertex = null;
this.tempPoly = null;

this.setState(SegStates.FREE);
this.selectedShape = this.newPoly;
Expand Down Expand Up @@ -532,7 +548,8 @@ Seg2d.prototype.redrawLabelCanvas = function(mainCtx) {
this.setPolygonLine(mainCtx);
this.setPolygonFill(mainCtx);

if (this.state === SegStates.DRAW || this.state === SegStates.QUICK_DRAW) {
if ((this.state === SegStates.DRAW ||
this.state === SegStates.QUICK_DRAW) && this.tempPoly) {
this.tempPoly.draw(mainCtx, this.satItem,
this.isTargeted() && this.state !== SegStates.DRAW
&& this.state !== SegStates.QUICK_DRAW);
Expand Down Expand Up @@ -798,6 +815,10 @@ Seg2d.prototype.doubleclick = function() {
Seg2d.prototype.mouseup = function(e) {
let mousePos = this.satItem.getMousePos(e);
if (this.state === SegStates.DRAW) {
if (!this.tempVertex) {
this.initDrawing(mousePos);
return;
}
this.tempVertex.xy = [mousePos.x, mousePos.y];
let occupiedShape = this.satItem.getOccupiedShape(mousePos);
if (occupiedShape && (occupiedShape instanceof Vertex)) {
Expand Down Expand Up @@ -827,12 +848,21 @@ Seg2d.prototype.mouseup = function(e) {

if (this.newPoly.isValidShape()) {
this.addPolyline(this.newPoly);
this.tempVertex.delete();
this.tempPoly.delete();
this.tempVertex = null;
this.tempPoly = null;
if (this.polyBuffer) {
for (let poly of this.polyBuffer) {
poly.delete();
}
}
} else if (this.polyBuffer) {
for (let poly of this.polyBuffer) {
this.polys.push(poly);
}
}

this.polyBuffer = null;
this.tempVertex.delete();
this.tempPoly.delete();
this.tempVertex = null;
this.tempPoly = null;
this.setState(SegStates.FREE);
this.selectedShape = this.newPoly;
if (this.parent) {
Expand Down Expand Up @@ -912,7 +942,7 @@ Seg2d.prototype.mousemove = function(e) {
let mousePos = this.satItem.getMousePos(e);
// handling according to state
if ((this.state === SegStates.DRAW || this.state === SegStates.QUICK_DRAW)
&& !this.satItem.isMouseDown) {
&& !this.satItem.isMouseDown && this.tempVertex) {
this.tempVertex.xy = [mousePos.x, mousePos.y];
} else if (this.state === SegStates.RESIZE
&& this.selectedShape instanceof Vertex) {
Expand Down Expand Up @@ -979,6 +1009,12 @@ Seg2d.prototype.mouseleave = function(e) { // eslint-disable-line
Seg2d.prototype.keydown = function(e) {
let keyID = e.KeyCode ? e.KeyCode : e.which;
if (keyID === 27) { // Esc
if (this.polyBuffer) {
for (let poly of this.polyBuffer) {
this.polys.push(poly);
}
this.polyBuffer = null;
}
this.setState(SegStates.FREE);
} else if (keyID === 85) {
// u for unlinking the selected label
Expand Down Expand Up @@ -1031,5 +1067,16 @@ Seg2d.prototype.keydown = function(e) {
this.tempVertex = null;
this.selectedShape = this.newPoly;
}
} else if (keyID === 82) {
// r for relabeling a seg2d
if (this.state === SegStates.FREE) {
this.polyBuffer = [];
for (let poly of this.polys) {
this.polyBuffer.push(poly);
}
this.polys = [];
this.setState(SegStates.DRAW);
this.satItem.redrawLabelCanvas();
}
}
};

0 comments on commit bd2b373

Please sign in to comment.