ROIGeometry extends ROI { @Override public LinkedList getAsRectangleList(int x, int y, int width, int height) { Rectangle rect = new Rectangle(x, y, width, height); if (!intersects(rect)) { return null; } else if (theGeom.getGeometry().isRectangle()) { Envelope env = theGeom.getGeometry().getEnvelopeInternal(); Envelope intersection = env.intersection(new Envelope(x, x + width, y, y + width)); int rx = (int) Math.round(intersection.getMinX()); int ry = (int) Math.round(intersection.getMinY()); int rw = (int) Math.round(intersection.getMaxX() - rx); int rh = (int) Math.round(intersection.getMaxY() - ry); LinkedList result = new LinkedList(); result.add(new Rectangle(rx, ry, rw, rh)); return result; } else { ROI roiImage = new ROI(getAsImage()); return roiImage.getAsRectangleList(x, y, width, height); } } ROIGeometry(Geometry geom);  ROIGeometry(Rectangle rect);  ROIGeometry(Geometry geom, final boolean useFixedPrecision);  ROIGeometry(Geometry geom, final boolean antiAliasing, final boolean useFixedPrecision);  ROIGeometry(Geometry geom, final RenderingHints hints);  ROIGeometry(Geometry geom, final boolean antiAliasing, 
            final boolean useFixedPrecision, final RenderingHints hints); @Override ROI add(ROI roi); @Override boolean contains(Point p); @Override boolean contains(Point2D p); @Override boolean contains(int x, int y); @Override boolean contains(double x, double y); @Override boolean contains(Rectangle rect); @Override boolean contains(Rectangle2D rect); @Override boolean contains(int x, int y, int w, int h); @Override boolean contains(double x, double y, double w, double h); @Override ROI exclusiveOr(ROI roi); @Override int[][] getAsBitmask(int x, int y, int width, int height, int[][] mask); @Override PlanarImage getAsImage(); @Override LinkedList getAsRectangleList(int x, int y, int width, int height); @Override Shape getAsShape(); Geometry getAsGeometry(); @Override Rectangle getBounds(); @Override Rectangle2D getBounds2D(); @Override int getThreshold(); @Override ROI intersect(ROI roi); @Override boolean intersects(Rectangle rect); @Override boolean intersects(Rectangle2D rect); @Override boolean intersects(int x, int y, int w, int h); @Override boolean intersects(double x, double y, double w, double h); @Override ROI performImageOp(RenderedImageFactory RIF, ParameterBlock paramBlock, int sourceIndex, RenderingHints renderHints); @Override ROI performImageOp(String name, ParameterBlock paramBlock, int sourceIndex, RenderingHints renderHints); @Override void setThreshold(int threshold); @Override ROI subtract(ROI roi); @Override ROI transform(AffineTransform at, Interpolation interp); @Override ROI transform(AffineTransform at); @Override String toString(); static final boolean DEFAULT_ROIGEOMETRY_ANTIALISING; static final boolean DEFAULT_ROIGEOMETRY_USEFIXEDPRECISION; }
@Test public void testRectangleListSimple() throws ParseException { ROI leftRight = createLeftRightROIGeometry(); LinkedList rectangles = leftRight.getAsRectangleList(-50, -50, 500, 500); assertSingleLeftRectangle(rectangles, 0, 0, 128, 256); rectangles = leftRight.getAsRectangleList(0, 0, 256, 256); assertSingleLeftRectangle(rectangles, 0, 0, 128, 256); rectangles = leftRight.getAsRectangleList(64, 64, 64, 64); assertSingleLeftRectangle(rectangles, 64, 64, 64, 64); rectangles = leftRight.getAsRectangleList(130, 0, 64, 64); assertNull(rectangles); }
@Test public void testRectangleListBowTie() throws ParseException { ROI bowTie = createBowTieROIGeometry(); LinkedList rectangles = bowTie.getAsRectangleList(-50, -50, 500, 500); assertEquals(254, rectangles.size()); int expectedWidth = 1; int y = 0; for (int i = 0; i < 254; i++) { boolean even = (i % 2) == 0; int x = even ? 0 : 256 - expectedWidth; Rectangle r = (Rectangle) rectangles.get(i); assertEquals(x, r.x); assertEquals(y, r.y); assertEquals(expectedWidth, r.width); if(i == 126 || i == 127 ) { assertEquals(2, r.height); } else { assertEquals(1, r.height); } if(!even) { if(i < 126) { expectedWidth += 2; } else { expectedWidth -= 2; } if(i == 127) { y += 2; } else { y++; } } } }