Search in sources :

Example 51 with PathObject

use of qupath.lib.objects.PathObject in project qupath by qupath.

the class AbstractPathROITool method commitObjectToHierarchy.

/**
 * When drawing an object is complete, add it to the hierarchy - or whatever else is required.
 *
 * @param e
 * @param pathObject
 */
void commitObjectToHierarchy(MouseEvent e, PathObject pathObject) {
    if (pathObject == null)
        return;
    var viewer = getViewer();
    PathObjectHierarchy hierarchy = viewer.getHierarchy();
    var currentROI = pathObject.getROI();
    // If we are in selection mode, try to get objects to select
    if (PathPrefs.selectionModeProperty().get()) {
        var pathClass = PathPrefs.autoSetAnnotationClassProperty().get();
        var toSelect = hierarchy.getObjectsForROI(null, currentROI);
        if (!toSelect.isEmpty() && pathClass != null) {
            boolean retainIntensityClass = !(PathClassTools.isPositiveOrGradedIntensityClass(pathClass) || PathClassTools.isNegativeClass(pathClass));
            var reclassified = toSelect.stream().filter(p -> p.getPathClass() != pathClass).map(p -> new Reclassifier(p, pathClass, retainIntensityClass)).filter(r -> r.apply()).map(r -> r.getPathObject()).collect(Collectors.toList());
            if (!reclassified.isEmpty()) {
                hierarchy.fireObjectClassificationsChangedEvent(this, reclassified);
            }
        }
        if (pathObject.getParent() != null)
            hierarchy.removeObject(pathObject, true);
        // viewer.getHierarchy().fireHierarchyChangedEvent(this);
        if (toSelect.isEmpty())
            viewer.setSelectedObject(null);
        else if (e.isShiftDown()) {
            hierarchy.getSelectionModel().deselectObject(pathObject);
            hierarchy.getSelectionModel().selectObjects(toSelect);
        } else
            hierarchy.getSelectionModel().setSelectedObjects(toSelect, null);
    } else {
        if (!requestParentClipping(e)) {
            if (currentROI.isEmpty()) {
                pathObject = null;
            } else
                // Ensure object is within the hierarchy
                hierarchy.addPathObject(pathObject);
        } else {
            ROI roiNew = refineROIByParent(pathObject.getROI());
            if (roiNew.isEmpty()) {
                hierarchy.removeObject(pathObject, true);
                pathObject = null;
            } else {
                ((PathAnnotationObject) pathObject).setROI(roiNew);
                hierarchy.addPathObjectBelowParent(getCurrentParent(), pathObject, true);
            }
        }
        if (pathObject != null)
            viewer.setSelectedObject(pathObject);
        else
            viewer.getHierarchy().getSelectionModel().clearSelection();
    }
    var editor = viewer.getROIEditor();
    editor.ensureHandlesUpdated();
    editor.resetActiveHandle();
    if (preferReturnToMove()) {
        var qupath = QuPathGUI.getInstance();
        if (qupath != null)
            qupath.setSelectedTool(PathTools.MOVE);
    }
}
Also used : Logger(org.slf4j.Logger) Point2D(java.awt.geom.Point2D) PathClassTools(qupath.lib.objects.classes.PathClassTools) MouseEvent(javafx.scene.input.MouseEvent) PathObjects(qupath.lib.objects.PathObjects) LoggerFactory(org.slf4j.LoggerFactory) PathObjectHierarchy(qupath.lib.objects.hierarchy.PathObjectHierarchy) PolylineROI(qupath.lib.roi.PolylineROI) Collectors(java.util.stream.Collectors) PathAnnotationObject(qupath.lib.objects.PathAnnotationObject) PathObject(qupath.lib.objects.PathObject) Cursor(javafx.scene.Cursor) ROI(qupath.lib.roi.interfaces.ROI) RoiEditor(qupath.lib.roi.RoiEditor) ImagePlane(qupath.lib.regions.ImagePlane) Reclassifier(qupath.lib.objects.classes.Reclassifier) PolygonROI(qupath.lib.roi.PolygonROI) Collections(java.util.Collections) PathPrefs(qupath.lib.gui.prefs.PathPrefs) QuPathGUI(qupath.lib.gui.QuPathGUI) PathObjectHierarchy(qupath.lib.objects.hierarchy.PathObjectHierarchy) PathAnnotationObject(qupath.lib.objects.PathAnnotationObject) PolylineROI(qupath.lib.roi.PolylineROI) ROI(qupath.lib.roi.interfaces.ROI) PolygonROI(qupath.lib.roi.PolygonROI) Reclassifier(qupath.lib.objects.classes.Reclassifier)

Example 52 with PathObject

use of qupath.lib.objects.PathObject in project qupath by qupath.

the class AbstractPolyROITool method mouseMoved.

@Override
public void mouseMoved(MouseEvent e) {
    super.mouseMoved(e);
    var viewer = getViewer();
    ROI currentROI = viewer.getCurrentROI();
    RoiEditor editor = viewer.getROIEditor();
    if (isPolyROI(currentROI) && editor.getROI() == currentROI) {
        Point2D p = mouseLocationToImage(e, true, requestPixelSnapping());
        ROI roiUpdated = editor.setActiveHandlePosition(p.getX(), p.getY(), viewer.getDownsampleFactor(), e.isShiftDown());
        PathObject pathObject = viewer.getSelectedObject();
        if (roiUpdated != currentROI && pathObject instanceof PathROIObject) {
            ((PathROIObject) pathObject).setROI(roiUpdated);
            viewer.getHierarchy().fireObjectsChangedEvent(this, Collections.singleton(pathObject), true);
        }
    }
}
Also used : PathObject(qupath.lib.objects.PathObject) RoiEditor(qupath.lib.roi.RoiEditor) Point2D(java.awt.geom.Point2D) PathROIObject(qupath.lib.objects.PathROIObject) PolylineROI(qupath.lib.roi.PolylineROI) ROI(qupath.lib.roi.interfaces.ROI) PolygonROI(qupath.lib.roi.PolygonROI)

Example 53 with PathObject

use of qupath.lib.objects.PathObject in project qupath by qupath.

the class AbstractPolyROITool method mouseReleased.

@Override
public void mouseReleased(MouseEvent e) {
    super.mouseReleased(e);
    if (isFreehandPolyROI) {
        var viewer = getViewer();
        PathObject currentObject = viewer.getSelectedObject();
        ROI currentROI = currentObject == null ? null : currentObject.getROI();
        if (isPolyROI(currentROI) && currentROI.isEmpty()) {
            isFreehandPolyROI = false;
        } else if (PathPrefs.enableFreehandToolsProperty().get()) {
            RoiEditor editor = viewer.getROIEditor();
            Point2D p2 = mouseLocationToImage(e, true, requestPixelSnapping());
            ROI roiUpdated = editor.setActiveHandlePosition(p2.getX(), p2.getY(), viewer.getDownsampleFactor(), e.isShiftDown());
            if (currentObject != null && currentObject.getROI() != roiUpdated && currentObject instanceof PathROIObject) {
                ((PathROIObject) currentObject).setROI(roiUpdated);
            }
            commitObjectToHierarchy(e, currentObject);
        // completePolygon(e);
        }
    }
}
Also used : PathObject(qupath.lib.objects.PathObject) RoiEditor(qupath.lib.roi.RoiEditor) Point2D(java.awt.geom.Point2D) PathROIObject(qupath.lib.objects.PathROIObject) PolylineROI(qupath.lib.roi.PolylineROI) ROI(qupath.lib.roi.interfaces.ROI) PolygonROI(qupath.lib.roi.PolygonROI)

Example 54 with PathObject

use of qupath.lib.objects.PathObject in project qupath by qupath.

the class AbstractPolyROITool method mousePressed.

@Override
public void mousePressed(MouseEvent e) {
    super.mousePressed(e);
    // If we double-clicked a polygon, we're done with it
    var viewer = getViewer();
    PathObject currentObject = viewer == null ? null : viewer.getSelectedObject();
    if (currentObject != null && e.getClickCount() == 1) {
        RoiEditor editor = viewer.getROIEditor();
        logger.trace("Adjusting polygon {}", e);
        Point2D p2 = mouseLocationToImage(e, true, requestPixelSnapping());
        ROI roiUpdated = editor.requestNewHandle(p2.getX(), p2.getY());
        if (currentObject != null && currentObject.getROI() != roiUpdated && currentObject instanceof PathROIObject) {
            ((PathROIObject) currentObject).setROI(roiUpdated);
        }
        isFreehandPolyROI = false;
        viewer.repaint();
    } else {
        commitObjectToHierarchy(e, currentObject);
    }
    ROI currentROI = currentObject == null ? null : currentObject.getROI();
    if (isPolyROI(currentROI) && currentROI.isEmpty() && (currentROI.getNumPoints() == 1 || new HashSet<>(currentROI.getAllPoints()).size() == 1))
        isFreehandPolyROI = true;
}
Also used : PathObject(qupath.lib.objects.PathObject) RoiEditor(qupath.lib.roi.RoiEditor) Point2D(java.awt.geom.Point2D) PathROIObject(qupath.lib.objects.PathROIObject) PolylineROI(qupath.lib.roi.PolylineROI) ROI(qupath.lib.roi.interfaces.ROI) PolygonROI(qupath.lib.roi.PolygonROI)

Example 55 with PathObject

use of qupath.lib.objects.PathObject in project qupath by qupath.

the class AbstractPolyROITool method mouseDragged.

@Override
public void mouseDragged(MouseEvent e) {
    // Note: if the 'freehand' part of the polygon creation isn't desired, just comment out this whole method
    super.mouseDragged(e);
    if (!e.isPrimaryButtonDown()) {
        return;
    }
    var viewer = getViewer();
    ROI currentROI = viewer.getCurrentROI();
    RoiEditor editor = viewer.getROIEditor();
    if (isPolyROI(currentROI) && editor.getROI() == currentROI) {
        Point2D p = mouseLocationToImage(e, true, requestPixelSnapping());
        ROI roiUpdated = editor.requestNewHandle(p.getX(), p.getY());
        PathObject pathObject = viewer.getSelectedObject();
        if (roiUpdated != currentROI && pathObject instanceof PathROIObject) {
            ((PathROIObject) pathObject).setROI(roiUpdated);
            viewer.getHierarchy().fireObjectsChangedEvent(this, Collections.singleton(pathObject), true);
        }
    }
}
Also used : PathObject(qupath.lib.objects.PathObject) RoiEditor(qupath.lib.roi.RoiEditor) Point2D(java.awt.geom.Point2D) PathROIObject(qupath.lib.objects.PathROIObject) PolylineROI(qupath.lib.roi.PolylineROI) ROI(qupath.lib.roi.interfaces.ROI) PolygonROI(qupath.lib.roi.PolygonROI)

Aggregations

PathObject (qupath.lib.objects.PathObject)182 ArrayList (java.util.ArrayList)84 ROI (qupath.lib.roi.interfaces.ROI)74 PathObjectHierarchy (qupath.lib.objects.hierarchy.PathObjectHierarchy)61 List (java.util.List)48 BufferedImage (java.awt.image.BufferedImage)37 IOException (java.io.IOException)37 PathClass (qupath.lib.objects.classes.PathClass)37 Collectors (java.util.stream.Collectors)35 PathAnnotationObject (qupath.lib.objects.PathAnnotationObject)34 Map (java.util.Map)33 Logger (org.slf4j.Logger)33 LoggerFactory (org.slf4j.LoggerFactory)33 ImageData (qupath.lib.images.ImageData)31 TMACoreObject (qupath.lib.objects.TMACoreObject)31 Collection (java.util.Collection)29 Collections (java.util.Collections)29 HashMap (java.util.HashMap)28 PathObjectTools (qupath.lib.objects.PathObjectTools)26 Arrays (java.util.Arrays)25