@Override public PolygonROI requestNewHandle(double x, double y) { if (activeHandle == null) return roi; // Can only add if there is an active handle - distance to this will be used // Move the active handle if it is very close to the requested region // (removed) // Don't add a handle at almost the sample place as an existing handle if (handles.size() >= 2 && activeHandle == handles.get(handles.size() - 1) && handles.get(handles.size() - 2).distanceSq(x, y) < 4) { return roi; } // // If we have 2 points, which are identical, shift instead of creating // if (handles.size() >= 2 && activeHandle == handles.get(handles.size() - 1) && activeHandle.distanceSq(handles.get(handles.size() - 2)) < 0.000001) { // System.err.println("UPDATING HANDLE"); // return updateActiveHandleLocation(x, y, false); // } activeHandle = new MutablePoint(x, y); handles.add(activeHandle); roi = new PolygonROI(createPoint2List(handles), roi.getC(), roi.getZ(), roi.getT()); // System.out.println("UPDATED HANDLES BY REQUEST: " + handles.size()); return roi; }
return pathROI; double dx = x - pTranslateCurrent.getX(); double dy = y - pTranslateCurrent.getY(); pTranslateCurrent.setLocation(x, y);
/** * If a handle has been grabbed, update its displacement. * If minDisplacement is > 0, smaller movements will be discarded to avoid unnecessary work. * * @param x * @param y * @param minDisplacement * @param modifiers - from a MouseEvent - may optionally be used to control how the handle is modified * @return */ public ROI setActiveHandlePosition(double x, double y, double minDisplacement, boolean shiftDown) { // System.err.println("Set position: " + activeHandle); // Check if we have an active handle, or have moved it anything worth considering if (adjuster == null || (activeHandle != null && activeHandle.distanceSq(x, y) < minDisplacement*minDisplacement)) return pathROI; // setROI(adjuster.updateActiveHandleLocation(x, y, shiftDown), false); pathROI = adjuster.updateActiveHandleLocation(x, y, shiftDown); return pathROI; }
protected static int getClosestHandleIndex(List<MutablePoint> handles, double x, double y, double maxDist) { // Get the closest handle within maxDist pixels away double closestDist = Double.POSITIVE_INFINITY; double distSq = maxDist*maxDist; int activeHandleIndex = -1; int counter = 0; for (MutablePoint p : handles) { double dist = p.distanceSq(x, y); if (dist <= distSq && dist <= closestDist) { closestDist = dist; activeHandleIndex = counter; } counter++; } return activeHandleIndex; }
/** * Returns true if the current ROI is translatable, and at the end of this call the translation has started. * * @param pathROI * @param x * @param y * @return */ public boolean startTranslation(double x, double y) { if (!(pathROI instanceof TranslatableROI)) return false; pTranslateOrigin = new MutablePoint(x, y); pTranslateCurrent = new MutablePoint(x, y); isTranslating = true; return true; }
/** * Notify the editor that translation should end. * * @param pathROI * @return true if there is any displacement between the current and starting translation points, false otherwise. */ public boolean finishTranslation() { boolean displacement = isTranslating && pTranslateOrigin.distanceSq(pTranslateCurrent) > 0; isTranslating = false; pTranslateOrigin = null; pTranslateCurrent = null; return displacement; }
public double distanceSq(final MutablePoint point) { return distanceSq(point.getX(), point.getY()); }
@Override public PointsROI requestNewHandle(double x, double y) { activeHandle = new MutablePoint(x, y); handles.add(activeHandle); roi = new PointsROI(createPoint2List(handles), roi.getC(), roi.getZ(), roi.getT()); ensureHandlesUpdated(); activeHandle = grabHandle(x, y, Double.POSITIVE_INFINITY, false); // ensureHandlesUpdated(); // activeHandle = grabHandle(x, y, 1, false); // ensureHandlesUpdated(); // activeHandle = roi.getNearest(x, y, 0.01); return roi; }
static boolean handlesOverlap(final List<MutablePoint> handles) { if (handles.isEmpty()) return false; MutablePoint h1 = handles.get(0); for (MutablePoint h : handles) { if (h1.distanceSq(h) > 0) return false; } return true; }
void updateHandles() { bounds = new Rect(roi.getBoundsX(), roi.getBoundsY(), roi.getBoundsWidth(), roi.getBoundsHeight()); handles.get(TOP_LEFT).setLocation(bounds.getMinX(), bounds.getMinY()); handles.get(TOP_CENTER).setLocation(bounds.getCenterX(), bounds.getMinY()); handles.get(TOP_RIGHT).setLocation(bounds.getMaxX(), bounds.getMinY()); handles.get(CENTER_RIGHT).setLocation(bounds.getMaxX(), bounds.getCenterY()); handles.get(BOTTOM_RIGHT).setLocation(bounds.getMaxX(), bounds.getMaxY()); handles.get(BOTTOM_CENTER).setLocation(bounds.getCenterX(), bounds.getMaxY()); handles.get(BOTTOM_LEFT).setLocation(bounds.getMinX(), bounds.getMaxY()); handles.get(CENTER_LEFT).setLocation(bounds.getMinX(), bounds.getCenterY()); }
BoundedHandleAdjuster(T roi) { this.roi = roi; bounds = new Rect(roi.getBoundsX(), roi.getBoundsY(), roi.getBoundsWidth(), roi.getBoundsHeight()); handles.clear(); for (int i = 0; i < 8; i++) handles.add(new MutablePoint(Double.NaN, Double.NaN)); // updateHandles(); }
@Override PointsROI updateActiveHandleLocation(double xNew, double yNew, boolean shiftDown) { if (activeHandle == null) return roi; activeHandle.setLocation(xNew, yNew); roi = new PointsROI(createPoint2List(handles), roi.getC(), roi.getZ(), roi.getT()); ensureHandlesUpdated(); activeHandle = grabHandle(xNew, yNew, Double.POSITIVE_INFINITY, shiftDown); // System.err.println("Calling: " + activeHandle + " - " + (handles == null ? 0 : handles.size())); return roi; }
@Override void ensureHandlesUpdated() { handles.clear(); handles.add(new MutablePoint(roi.getX1(), roi.getY1())); handles.add(new MutablePoint(roi.getX2(), roi.getY2())); }
@Override PolygonROI updateActiveHandleLocation(double xNew, double yNew, boolean shiftDown) { if (activeHandle == null) return roi; activeHandle.setLocation(xNew, yNew); roi = new PolygonROI(createPoint2List(handles), roi.getC(), roi.getZ(), roi.getT()); // System.out.println("UPDATED HANDLES: " + handles.size() + ", " + roi.nVertices()); return roi; }
@Override void ensureHandlesUpdated() { if (handles == null) handles = new ArrayList<>(); else handles.clear(); addPointsToMutablePointList(handles, roi.getPolygonPoints()); // If we have a single point, create a second handle (which may be adjusted) if (handles.size() == 1) handles.add(new MutablePoint(handles.get(0).getX(), handles.get(0).getY())); }
static void addPointsToMutablePointList(final List<MutablePoint> mutablePoints, final List<Point2> points) { for (Point2 p : points) { mutablePoints.add(new MutablePoint(p.getX(), p.getY())); } }