001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package plugins.kernel.roi.roi2d;
020
021import icy.resource.ResourceUtil;
022import icy.roi.ROI;
023import icy.type.point.Point5D;
024
025import java.awt.geom.Point2D;
026import java.awt.geom.Rectangle2D;
027
028/**
029 * @author Stephane
030 */
031public class ROI2DRectangle extends ROI2DRectShape
032{
033    /**
034     * @deprecated
035     */
036    @Deprecated
037    public ROI2DRectangle(Point2D topLeft, Point2D bottomRight, boolean cm)
038    {
039        this(topLeft, bottomRight);
040    }
041
042    public ROI2DRectangle(Point2D topLeft, Point2D bottomRight)
043    {
044        super(new Rectangle2D.Double(), topLeft, bottomRight);
045
046        // set icon (default name is defined by getDefaultName()) 
047        setIcon(ResourceUtil.ICON_ROI_RECTANGLE);
048    }
049
050    public ROI2DRectangle(double xmin, double ymin, double xmax, double ymax)
051    {
052        this(new Point2D.Double(xmin, ymin), new Point2D.Double(xmax, ymax));
053    }
054
055    /**
056     * @deprecated
057     */
058    @Deprecated
059    public ROI2DRectangle(Rectangle2D rectangle, boolean cm)
060    {
061        this(rectangle);
062    }
063
064    public ROI2DRectangle(Rectangle2D rectangle)
065    {
066        this(new Point2D.Double(rectangle.getMinX(), rectangle.getMinY()), new Point2D.Double(rectangle.getMaxX(),
067                rectangle.getMaxY()));
068    }
069
070    /**
071     * @deprecated
072     */
073    @Deprecated
074    public ROI2DRectangle(Point2D pt, boolean cm)
075    {
076        this(pt);
077    }
078
079    public ROI2DRectangle(Point2D pt)
080    {
081        this(new Point2D.Double(pt.getX(), pt.getY()), pt);
082    }
083
084    /**
085     * Generic constructor for interactive mode
086     */
087    public ROI2DRectangle(Point5D pt)
088    {
089        this(pt.toPoint2D());
090    }
091
092    public ROI2DRectangle()
093    {
094        this(new Point2D.Double(), new Point2D.Double());
095    }
096    
097    @Override
098    public String getDefaultName()
099    {
100        return "Rectangle2D";
101    }
102
103    public Rectangle2D getRectangle()
104    {
105        return (Rectangle2D) shape;
106    }
107
108    public void setRectangle(Rectangle2D rectangle)
109    {
110        setBounds2D(rectangle);
111    }
112
113    @Override
114    public boolean contains(ROI roi)
115    {
116        // special case of ROI2DPoint
117        if (roi instanceof ROI2DPoint)
118            return onSamePos(((ROI2DPoint) roi), true) && contains(((ROI2DPoint) roi).getPoint());
119        // special case of ROI2DLine
120        if (roi instanceof ROI2DLine)
121            return onSamePos(((ROI2DLine) roi), true) && contains(((ROI2DLine) roi).getBounds2D());
122        // special case of ROI2DRectangle
123        if (roi instanceof ROI2DRectangle)
124            return onSamePos(((ROI2DRectangle) roi), true) && contains(((ROI2DRectangle) roi).getRectangle());
125
126        return super.contains(roi);
127    }
128
129    @Override
130    public boolean intersects(ROI roi)
131    {
132        // special case of ROI2DPoint
133        if (roi instanceof ROI2DPoint)
134            return onSamePos(((ROI2DPoint) roi), false) && contains(((ROI2DPoint) roi).getPoint());
135        // special case of ROI2DLine
136        if (roi instanceof ROI2DLine)
137            return onSamePos(((ROI2DLine) roi), false) && ((ROI2DLine) roi).getLine().intersects(getRectangle());
138        // special case of ROI2DRectangle
139        if (roi instanceof ROI2DRectangle)
140            return onSamePos(((ROI2DRectangle) roi), false)
141                    && ((ROI2DRectangle) roi).getRectangle().intersects(getRectangle());
142
143        return super.intersects(roi);
144    }
145
146    @Override
147    public double computeNumberOfPoints()
148    {
149        final Rectangle2D r = getRectangle();
150        return r.getWidth() * r.getHeight();
151    }
152}