Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat smart copy paste #106

Open
wants to merge 6 commits into
base: main-ce
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 70 additions & 14 deletions src/components/viewer/DrawTools.vue
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ import IconPolygonFreeHand from '@/components/icons/IconPolygonFreeHand';
import IconLineFreeHand from '@/components/icons/IconLineFreeHand';

import WKT from 'ol/format/WKT';
import {containsExtent} from 'ol/extent';
import {containsExtent, getCenter, getIntersection} from 'ol/extent';

import {Cytomine, Annotation, AnnotationType} from 'cytomine-client';
import {Action, updateTermProperties, updateTrackProperties} from '@/utils/annotation-utils.js';
Expand Down Expand Up @@ -515,6 +515,14 @@ export default {
this.$store.commit(this.viewerModule + 'setCopiedAnnot', annot);
}
},
copiedAnnotImageInstance: {
get() {
return this.viewerWrapper.copiedAnnotImageInstance;
},
set(image) {
this.$store.commit(this.viewerModule + 'setCopiedAnnotImageInstance', image);
}
},
disabledPaste() {
return this.disabledDraw || !this.copiedAnnot;
},
Expand Down Expand Up @@ -649,29 +657,77 @@ export default {
return;
}

this.copiedAnnotImageInstance = this.image;
this.copiedAnnot = feature.properties.annot.clone();
this.$notify({type: 'success', text: this.$t('notif-success-annotation-copy')});
},
async paste() {
if (!this.copiedAnnot) {
return;
convertLocation(copiedAnnot, destImage) {
/* If we want to paste in the same image but in another slice */
if (destImage.id === copiedAnnot.image) {
return copiedAnnot.location;
}

let location;
let geometry = new WKT().readGeometry(this.copiedAnnot.location);
let geometry = new WKT().readGeometry(copiedAnnot.location);
let wrapper = this.imageWrapper;
let centerExtent = getCenter(geometry.getExtent());

/* Translate the original location of the annotation to the center of the current FOV */
geometry.translate(wrapper.view.center[0] - centerExtent[0], wrapper.view.center[1] - centerExtent[1]);

/* Compute the rescaling factors if the resolution is known for both images */
let scaleX = 1;
let scaleY = 1;
let srcImage = this.copiedAnnotImageInstance;
let hasPhysicalSizeX = srcImage.physicalSizeX !== null && destImage.physicalSizeX !== null;
let hasPhysicalSizeY = srcImage.physicalSizeY !== null && destImage.physicalSizeY !== null;

if (this.image.id === this.copiedAnnot.image || containsExtent(this.imageExtent, geometry.getExtent())) {
location = this.copiedAnnot.location;
if (hasPhysicalSizeX && hasPhysicalSizeY) {
scaleX = srcImage.physicalSizeX / destImage.physicalSizeX;
scaleY = srcImage.physicalSizeY / destImage.physicalSizeY;
}
else {
let extent = geometry.getExtent();
let center = [(extent[0] + extent[2]) / 2, (extent[1] + extent[3]) / 2];
geometry.translate(this.image.width / 2 - center[0], this.image.height / 2 - center[1]);
if (containsExtent(this.imageExtent, geometry.getExtent())) {
location = new WKT().writeGeometry(geometry);
else if (hasPhysicalSizeX) {
scaleX = srcImage.physicalSizeX / destImage.physicalSizeX;
scaleY = scaleX;
}

/* Rescale the annotation */
geometry.scale(scaleX, scaleY);

/* Rescale the annotation if it is larger than the destination image size */
let annotExtent = geometry.getExtent();
let annotWidth = annotExtent[2] - annotExtent[0];
let annotHeight = annotExtent[3] - annotExtent[1];
if (annotWidth > destImage.width || annotHeight > destImage.height) {
let scale = annotHeight > annotWidth ? annotWidth / annotHeight : annotHeight / annotWidth;
geometry.scale(scale);
}

/* Check if the translation is within the image boundaries */
let imageExtent = [0, 0, destImage.width, destImage.height];
if (!containsExtent(imageExtent, geometry.getExtent())) {
let geomExtent = geometry.getExtent();
/* Get the part of annotation within the boundaries */
let intersection = getIntersection(imageExtent, geomExtent);

/* Get the difference between the parts inside and outside the image boundaries */
let difference = [];
for (let i = 0; i < intersection.length; i++) {
difference[i] = intersection[i] - geomExtent[i];
}

/* Translate the difference to have the complete annotation inside the image boundaries */
geometry.translate(difference[0] + difference[2], difference[1] + difference[3]);
}

return new WKT().writeGeometry(geometry);
},
async paste() {
if (!this.copiedAnnot) {
return;
}

/* Convert the location if it is needed */
let location = this.convertLocation(this.copiedAnnot, this.image);
if (!location) {
this.$notify({type: 'error', text: this.$t('notif-error-annotation-paste')});
return;
Expand Down
7 changes: 6 additions & 1 deletion src/store/modules/project_modules/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export default {
activeImage: 0,
indexNextImage: 0,

copiedAnnot: null
copiedAnnot: null,
copiedAnnotImageInstance: null
};
},

Expand All @@ -52,6 +53,10 @@ export default {
state.copiedAnnot = annot;
},

setCopiedAnnotImageInstance(state, image) {
state.copiedAnnotImageInstance = image;
},

setLinkMode(state, mode) {
state.linkMode = mode;
},
Expand Down