Skip to content

Commit

Permalink
AnnotatorTool: improve out of bounds drawing behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
dietzc committed Oct 4, 2016
1 parent 46bdec2 commit 57c4f1f
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,8 @@ public AnnotatorTool(final String name, final String iconPath) {
*/
public void onMouseDoubleClick(final ImgViewerMouseEvent e, final PlaneSelectionEvent selection,
final Overlay overlay, final String... labels) {
if (!e.isInside()) {
setCurrentOverlayElement(null, null);
fireStateChanged();
} else if (e.isLeftDown()) {

if (e.isLeftDown()) {
onMouseDoubleClickLeft(e, m_currentOverlayElement, selection, overlay, labels);
} else if (e.isRightDown()) {
onMouseDoubleClickRight(e, m_currentOverlayElement, selection, overlay, labels);
Expand All @@ -243,10 +241,7 @@ public void onMousePressed(final ImgViewerMouseEvent e, final PlaneSelectionEven
final String... labels) {
m_dragPoint = selection.getPlanePos(e.getPosX(), e.getPosY());

if (!e.isInside()) {
setCurrentOverlayElement(null, null);
fireStateChanged();
} else if (e.isLeftDown()) {
if (e.isLeftDown()) {
onMousePressedLeft(e, m_currentOverlayElement, selection, overlay, labels);
} else if (e.isRightDown()) {
onMousePressedRight(e, m_currentOverlayElement, selection, overlay, labels);
Expand All @@ -266,9 +261,7 @@ public void onMousePressed(final ImgViewerMouseEvent e, final PlaneSelectionEven
public void onMouseReleased(final ImgViewerMouseEvent e, final PlaneSelectionEvent selection, final Overlay overlay,
final String... labels) {

if (!e.isInside()) {
// ignore click
} else if (e.isLeftDown()) {
if (e.isLeftDown()) {
onMouseReleasedLeft(e, m_currentOverlayElement, selection, overlay, labels);
} else if (e.isRightDown()) {
onMouseReleasedRight(e, m_currentOverlayElement, selection, overlay, labels);
Expand All @@ -288,9 +281,6 @@ public void onMouseReleased(final ImgViewerMouseEvent e, final PlaneSelectionEve
*/
public void onMouseDragged(final ImgViewerMouseEvent e, final PlaneSelectionEvent selection, final Overlay overlay,
final String... labels) {
if (!e.isInside()) {
return;
}

if (e.isLeftDown()) {
onMouseDraggedLeft(e, m_currentOverlayElement, selection, overlay, labels);
Expand Down Expand Up @@ -329,9 +319,8 @@ public String getIconPath() {

@SuppressWarnings("javadoc")
public final void setButtonIcon(final AbstractButton jb, final String path) {
final URL icon =
getClass().getClassLoader().getResource(getClass().getPackage().getName().replace('.', '/') + "/"
+ path);
final URL icon = getClass().getClassLoader()
.getResource(getClass().getPackage().getName().replace('.', '/') + "/" + path);
jb.setHorizontalAlignment(SwingConstants.LEFT);
if (icon != null) {
jb.setIcon(new ImageIcon(icon));
Expand All @@ -345,8 +334,8 @@ public final void setButtonIcon(final AbstractButton jb, final String path) {
*/
// Helpers
protected boolean setCurrentOverlayElement(final OverlayElementStatus status, final O os) {
if ((((m_currentOverlayElement == null) && (os == null)) || ((m_currentOverlayElement == os) && (m_currentOverlayElement
.getStatus() == status)))) {
if ((((m_currentOverlayElement == null) && (os == null))
|| ((m_currentOverlayElement == os) && (m_currentOverlayElement.getStatus() == status)))) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public void onMouseDoubleClickLeft(final ImgViewerMouseEvent e, final FreeFormOv

}

@SuppressWarnings("unchecked")
@Override
public void onMousePressedLeft(final ImgViewerMouseEvent e, final FreeFormOverlayElement currentOverlayElement,
final PlaneSelectionEvent selection, final Overlay overlay, final String... labels) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@
import java.util.ArrayList;
import java.util.List;

import org.knime.knip.core.KNIPGateway;
import org.knime.knip.core.awt.labelingcolortable.DefaultLabelingColorTable;
import org.knime.knip.core.awt.labelingcolortable.LabelingColorTable;
import org.knime.knip.core.awt.labelingcolortable.LabelingColorTableUtils;
import org.knime.knip.core.awt.labelingcolortable.RandomMissingColorHandler;
import org.knime.knip.core.types.NativeTypes;
import org.knime.knip.core.ui.event.EventService;
import org.knime.knip.core.ui.event.EventServiceClient;
import org.knime.knip.core.ui.imgviewer.events.ImgRedrawEvent;
import org.knime.knip.core.ui.imgviewer.events.OverlayChgEvent;

import net.imglib2.FinalInterval;
import net.imglib2.Interval;
import net.imglib2.RandomAccessibleInterval;
Expand All @@ -70,17 +81,6 @@
import net.imglib2.type.numeric.integer.ShortType;
import net.imglib2.type.numeric.integer.UnsignedShortType;

import org.knime.knip.core.KNIPGateway;
import org.knime.knip.core.awt.labelingcolortable.DefaultLabelingColorTable;
import org.knime.knip.core.awt.labelingcolortable.LabelingColorTable;
import org.knime.knip.core.awt.labelingcolortable.LabelingColorTableUtils;
import org.knime.knip.core.awt.labelingcolortable.RandomMissingColorHandler;
import org.knime.knip.core.types.NativeTypes;
import org.knime.knip.core.ui.event.EventService;
import org.knime.knip.core.ui.event.EventServiceClient;
import org.knime.knip.core.ui.imgviewer.events.ImgRedrawEvent;
import org.knime.knip.core.ui.imgviewer.events.OverlayChgEvent;

/**
* Overlay
*
Expand Down Expand Up @@ -275,7 +275,6 @@ public RandomAccessibleInterval<LabelingType<String>> renderSegmentationImage(fi
* @param addSegmentID if true, an additional label with a unique id for each segment is added
* @return
*/
@SuppressWarnings("unchecked")
public ImgLabeling<String, ?> renderSegmentationImage(final boolean addSegmentID, final NativeTypes type) {

ImgLabeling<String, ?> res = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@

import net.imglib2.Cursor;
import net.imglib2.Interval;
import net.imglib2.IterableInterval;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.Img;
import net.imglib2.labeling.Labeling;
import net.imglib2.roi.IterableRegionOfInterest;
import net.imglib2.roi.Regions;
import net.imglib2.roi.labeling.LabelingMapping;
import net.imglib2.roi.labeling.LabelingType;
import net.imglib2.util.Intervals;

/**
*
Expand Down Expand Up @@ -106,7 +108,7 @@ public abstract class OverlayElement implements Externalizable {

public abstract boolean contains(long[] point);

public abstract IterableRegionOfInterest getRegionOfInterest();
public abstract IterableInterval<Void> getRegionOfInterest();

public abstract void renderOutline(Graphics g);

Expand Down Expand Up @@ -236,15 +238,15 @@ public void renderOnSegmentationImage(final RandomAccessibleInterval<LabelingTyp
throw new IllegalArgumentException(
"(Sub)Labeling must have the same number of dimensions as the OverlayElement");
}

final Cursor<LabelingType<String>> c = getRegionOfInterest().getIterableIntervalOverROI(labeling).cursor();
;
final Cursor<LabelingType<String>> c = Regions.sample(getRegionOfInterest(), labeling).cursor();

final long[] pos = new long[c.numDimensions()];
while (c.hasNext()) {
c.fwd();
c.localize(pos);

if (contains(pos)) {
if (Intervals.contains(labeling, c) && contains(pos)) {

if (c.get().isEmpty()) {
c.get().clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
Expand All @@ -60,12 +59,14 @@
import net.imglib2.Interval;

/**
*
*
* @author <a href="mailto:dietzc85@googlemail.com">Christian Dietz</a>
* @author <a href="mailto:horn_martin@gmx.de">Martin Horn</a>
* @author <a href="mailto:michael.zinsmaier@googlemail.com">Michael Zinsmaier</a>
*/
public abstract class OverlayElement2D extends OverlayElement implements Externalizable {
public abstract class OverlayElement2D extends OverlayElement {

public static final long serialVersionUID = -826721428402711836l;

private Rectangle m_boundingBox;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package org.knime.knip.core.ui.imgviewer.overlay.elemens;

/*
* ------------------------------------------------------------------------
*
Expand Down Expand Up @@ -46,26 +48,25 @@
* --------------------------------------------------------------------- *
*
*/
package org.knime.knip.core.ui.imgviewer.overlay.elemens;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import net.imglib2.roi.IterableRegionOfInterest;
import net.imglib2.roi.RectangleRegionOfInterest;
import java.util.Arrays;

import org.knime.knip.core.ui.imgviewer.overlay.OverlayElement2D;

import net.imglib2.IterableInterval;
import net.imglib2.Point;
import net.imglib2.roi.geometric.PointCollection;

/**
*
*
* @author <a href="mailto:dietzc85@googlemail.com">Christian Dietz</a>
* @author <a href="mailto:horn_martin@gmx.de">Martin Horn</a>
* @author <a href="mailto:michael.zinsmaier@googlemail.com">Michael Zinsmaier</a>
*
* TODO 2.0.0 move to "elements" package
*/
public class PointOverlayElement extends OverlayElement2D {

Expand All @@ -75,7 +76,7 @@ public class PointOverlayElement extends OverlayElement2D {

private int m_x;

private RectangleRegionOfInterest m_roi;
private PointCollection m_roi;

public PointOverlayElement() {
//
Expand All @@ -86,7 +87,7 @@ public PointOverlayElement(final int x, final int y, final long[] pos, final int
super(pos, orientation, labels);
m_x = x;
m_y = y;
m_roi = new RectangleRegionOfInterest(new double[]{x, y}, new double[]{1, 1});
m_roi = new PointCollection(Arrays.asList(new Point(pos)));
}

@Override
Expand All @@ -111,7 +112,7 @@ public boolean containsPoint(final long x, final long y) {
}

@Override
public IterableRegionOfInterest getRegionOfInterest() {
public IterableInterval<Void> getRegionOfInterest() {
return m_roi;
}

Expand All @@ -133,7 +134,7 @@ public void readExternal(final ObjectInput in) throws IOException, ClassNotFound
super.readExternal(in);
m_x = in.readInt();
m_y = in.readInt();
m_roi = new RectangleRegionOfInterest(new double[]{m_x, m_y}, new double[]{1, 1});
m_roi = new PointCollection(Arrays.asList(new Point(m_x, m_y)));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,33 @@
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import net.imglib2.RealPoint;
import net.imglib2.roi.IterableRegionOfInterest;
import net.imglib2.roi.PolygonRegionOfInterest;
import java.util.ArrayList;

import org.knime.knip.core.ui.imgviewer.overlay.OverlayElement2D;
import org.knime.knip.core.ui.imgviewer.overlay.OverlayElementStatus;

import net.imglib2.IterableInterval;
import net.imglib2.RealPoint;

/**
*
*
* @author <a href="mailto:dietzc85@googlemail.com">Christian Dietz</a>
* @author <a href="mailto:horn_martin@gmx.de">Martin Horn</a>
* @author <a href="mailto:michael.zinsmaier@googlemail.com">Michael Zinsmaier</a>
*/
public abstract class AbstractPolygonOverlayElement extends OverlayElement2D implements Externalizable {
public abstract class AbstractPolygonOverlayElement extends OverlayElement2D {

public static final long serialVersionUID = 6357560205835443827l;

protected static final int DRAWING_RADIUS = 2;

protected boolean m_isClosed;

protected Polygon m_poly;

protected PolygonRegionOfInterest m_roi;

protected abstract void renderPointOutline(Graphics2D g);

protected abstract void renderPointInterior(Graphics2D g);
Expand All @@ -97,10 +96,6 @@ public AbstractPolygonOverlayElement(final Polygon poly, final long[] planePos,
final String... labels) {
super(planePos, orientation, labels);
m_poly = poly;
m_roi = new PolygonRegionOfInterest();
for (int i = 0; i < m_poly.npoints; i++) {
m_roi.addVertex(i, new RealPoint((double)m_poly.xpoints[i], (double)m_poly.ypoints[i]));
}
}

public Polygon getPolygon() {
Expand All @@ -122,7 +117,6 @@ public boolean add(final long x, final long y) {
}

m_poly.addPoint((int)x, (int)y);
m_roi.addVertex(m_roi.getVertexCount(), new RealPoint((double)x, (double)y));

return true;
}
Expand Down Expand Up @@ -173,14 +167,18 @@ public void translate(final long x, final long y) {
for (int i = 0; i < m_poly.npoints; i++) {
m_poly.xpoints[i] += x;
m_poly.ypoints[i] += y;
m_roi.setVertexPosition(i, new RealPoint((double)m_poly.xpoints[i], (double)m_poly.ypoints[i]));
}
m_poly.invalidate();
}

@Override
public IterableRegionOfInterest getRegionOfInterest() {
return m_roi;
public IterableInterval<Void> getRegionOfInterest() {
final ArrayList<RealPoint> vertices = new ArrayList<RealPoint>();
for (int i = 0; i < m_poly.npoints; i++) {
vertices.add(new RealPoint(m_poly.xpoints[i], m_poly.ypoints[i]));
}

return new net.imglib2.roi.geometric.Polygon(vertices).rasterize();
}

@Override
Expand All @@ -200,12 +198,6 @@ public void readExternal(final ObjectInput in) throws IOException, ClassNotFound
super.readExternal(in);
m_poly = (Polygon)in.readObject();
m_isClosed = in.readBoolean();

m_roi = new PolygonRegionOfInterest();

for (int n = 0; n < m_poly.npoints; n++) {
m_roi.addVertex(n, new RealPoint((double)m_poly.xpoints[n], (double)m_poly.ypoints[n]));
}
}

}
Loading

0 comments on commit 57c4f1f

Please sign in to comment.