Search in sources :

Example 11 with PathROIObject

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

the class PointsTool method mousePressed.

@Override
public void mousePressed(MouseEvent e) {
    super.mousePressed(e);
    if (!e.isPrimaryButtonDown() || e.isConsumed()) {
        return;
    }
    // Get a server, if we can
    var viewer = getViewer();
    ImageServer<?> server = viewer.getServer();
    if (server == null)
        return;
    var viewerPlane = viewer.getImagePlane();
    // Find out the coordinates in the image domain
    Point2D p = mouseLocationToImage(e, false, requestPixelSnapping());
    double xx = p.getX();
    double yy = p.getY();
    // If we are outside the image, ignore click
    if (xx < 0 || yy < 0 || xx >= server.getWidth() || yy >= server.getHeight())
        return;
    // See if we have a selected ROI
    PathObject currentObjectTemp = viewer.getSelectedObject();
    if (!(currentObjectTemp == null || currentObjectTemp instanceof PathROIObject))
        return;
    PathROIObject currentObject = (PathROIObject) currentObjectTemp;
    ROI currentROI = currentObject == null ? null : currentObject.getROI();
    RoiEditor editor = viewer.getROIEditor();
    double radius = PathPrefs.pointRadiusProperty().get();
    ROI points = null;
    if (currentROI != null && currentROI.isPoint() && (currentROI.isEmpty() || currentROI.getImagePlane().equals(viewerPlane)))
        points = currentROI;
    // If Alt is pressed, try to delete a point
    if (e.isAltDown()) {
        handleAltClick(xx, yy, currentObject);
    } else // Create a new ROI if we've got Alt & Shift pressed - or we just don't have a point ROI
    if (points == null || (!PathPrefs.multipointToolProperty().get() && !editor.grabHandle(xx, yy, radius, e.isShiftDown())) || (e.isShiftDown() && e.getClickCount() > 1)) {
        // PathPoints is effectively ready from the start - don't need to finalize
        points = ROIs.createPointsROI(xx, yy, viewerPlane);
        currentObject = (PathROIObject) PathObjects.createAnnotationObject(points, PathPrefs.autoSetAnnotationClassProperty().get());
        viewer.getHierarchy().addPathObject(currentObject);
        viewer.setSelectedObject(currentObject);
        // viewer.createAnnotationObject(points);
        editor.setROI(points);
        editor.grabHandle(xx, yy, radius, e.isShiftDown());
    } else if (points != null) {
        // Add point to current ROI, or adjust the position of a nearby point
        ImagePlane plane = points == null || points.isEmpty() ? viewerPlane : points.getImagePlane();
        ROI points2 = addPoint(points, xx, yy, radius, plane);
        if (points2 == points) {
            // If we didn't add a point, try to grab a handle
            if (!editor.grabHandle(xx, yy, radius, e.isShiftDown()))
                return;
            points2 = (PointsROI) editor.setActiveHandlePosition(xx, yy, 0.25, e.isShiftDown());
        } else {
            editor.setROI(points2);
            editor.grabHandle(xx, yy, radius, e.isShiftDown());
        }
        if (points2 != points) {
            currentObject.setROI(points2);
            viewer.getHierarchy().updateObject(currentObject, true);
        // viewer.getHierarchy().fireHierarchyChangedEvent(this, currentObject);
        }
    }
    viewer.repaint();
}
Also used : PathObject(qupath.lib.objects.PathObject) RoiEditor(qupath.lib.roi.RoiEditor) Point2D(java.awt.geom.Point2D) ImagePlane(qupath.lib.regions.ImagePlane) PathROIObject(qupath.lib.objects.PathROIObject) ROI(qupath.lib.roi.interfaces.ROI) PointsROI(qupath.lib.roi.PointsROI)

Example 12 with PathROIObject

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

the class FillAnnotationHolesPlugin method getTasks.

@Override
protected Collection<Runnable> getTasks(final PluginRunner<T> runner) {
    Collection<? extends PathObject> parentObjects = getParentObjects(runner);
    if (parentObjects == null || parentObjects.isEmpty())
        return Collections.emptyList();
    // Add a single task, to avoid multithreading - which may complicate setting parents
    List<Runnable> tasks = new ArrayList<>(1);
    PathObjectHierarchy hierarchy = getHierarchy(runner);
    // Want to reset selection
    PathObject selected = hierarchy.getSelectionModel().getSelectedObject();
    Collection<PathObject> previousSelection = new ArrayList<>(hierarchy.getSelectionModel().getSelectedObjects());
    tasks.add(() -> {
        Map<PathROIObject, ROI> toUpdate = new HashMap<>();
        for (PathObject pathObject : parentObjects) {
            ROI roiOrig = pathObject.getROI();
            if (roiOrig == null || !roiOrig.isArea())
                continue;
            ROI roiUpdated = roiOrig;
            roiUpdated = RoiTools.fillHoles(roiUpdated);
            if (roiOrig != roiUpdated && pathObject instanceof PathROIObject) {
                toUpdate.put((PathROIObject) pathObject, roiUpdated);
            }
        }
        if (toUpdate.isEmpty())
            return;
        hierarchy.getSelectionModel().clearSelection();
        if (!toUpdate.isEmpty()) {
            hierarchy.removeObjects(toUpdate.keySet(), true);
            toUpdate.forEach((p, r) -> p.setROI(r));
            hierarchy.addPathObjects(toUpdate.keySet());
        }
        hierarchy.getSelectionModel().selectObjects(previousSelection);
        hierarchy.getSelectionModel().setSelectedObject(selected, true);
    });
    return tasks;
}
Also used : PathObjectHierarchy(qupath.lib.objects.hierarchy.PathObjectHierarchy) PathObject(qupath.lib.objects.PathObject) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PathROIObject(qupath.lib.objects.PathROIObject) ROI(qupath.lib.roi.interfaces.ROI)

Example 13 with PathROIObject

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

the class RigidObjectEditorCommand method commitChanges.

private void commitChanges(final boolean ignoreChanges) {
    if (this.originalObject == null)
        return;
    // PathObject pathObject = null;
    if (!ignoreChanges) {
        DialogButton option = Dialogs.showYesNoCancelDialog("Affine object editing", "Confirm object changes?");
        if (option == DialogButton.CANCEL)
            return;
        if (option == DialogButton.NO) {
            for (Entry<PathObject, ROI> entry : originalObjectROIs.entrySet()) ((PathROIObject) entry.getKey()).setROI(entry.getValue());
        } else {
            var transform = transformer.transform;
            var values = transform.getMatrixEntries();
            logger.info("Applied ROI transform: {}", String.format("\n %f, %f, %f,\n%f, %f, %f", values[0], values[1], values[2], values[3], values[4], values[5]));
            // Apply clipping now
            for (Entry<PathObject, ROI> entry : originalObjectROIs.entrySet()) {
                ROI roiTransformed = transformer.getTransformedROI(entry.getValue(), true);
                ((PathROIObject) entry.getKey()).setROI(roiTransformed);
            }
            viewer.getHierarchy().fireHierarchyChangedEvent(this, originalObject);
        }
    }
    // Update the mode if the viewer is still active
    qupath.setToolSwitchingEnabled(true);
    if (viewer == qupath.getViewer())
        viewer.setActiveTool(qupath.getSelectedTool());
    viewer.getView().removeEventHandler(MouseEvent.ANY, mouseListener);
    viewer.getCustomOverlayLayers().remove(overlay);
    viewer.removeViewerListener(this);
    // if (pathObject != null)
    // viewer.getHierarchy().addPathObject(pathObject, true);
    // // Ensure the object is selected
    // viewer.setSelectedObject(pathObject);
    viewer = null;
    overlay = null;
    originalObjectROIs.clear();
    originalObject = null;
    transformer = null;
}
Also used : DialogButton(qupath.lib.gui.dialogs.Dialogs.DialogButton) PathObject(qupath.lib.objects.PathObject) PathROIObject(qupath.lib.objects.PathROIObject) PolylineROI(qupath.lib.roi.PolylineROI) ROI(qupath.lib.roi.interfaces.ROI)

Aggregations

PathROIObject (qupath.lib.objects.PathROIObject)13 PathObject (qupath.lib.objects.PathObject)12 ROI (qupath.lib.roi.interfaces.ROI)12 RoiEditor (qupath.lib.roi.RoiEditor)9 Point2D (java.awt.geom.Point2D)8 PolylineROI (qupath.lib.roi.PolylineROI)5 PathObjectHierarchy (qupath.lib.objects.hierarchy.PathObjectHierarchy)4 PolygonROI (qupath.lib.roi.PolygonROI)4 PointsROI (qupath.lib.roi.PointsROI)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Rectangle2D (java.awt.geom.Rectangle2D)1 DialogButton (qupath.lib.gui.dialogs.Dialogs.DialogButton)1 PixelCalibration (qupath.lib.images.servers.PixelCalibration)1 PathAnnotationObject (qupath.lib.objects.PathAnnotationObject)1 TMACoreObject (qupath.lib.objects.TMACoreObject)1 ImagePlane (qupath.lib.regions.ImagePlane)1