edu.umd.cfar.lamp.viper.geometry
Class BoundingBox

java.lang.Object
  extended byedu.umd.cfar.lamp.viper.geometry.PolyList
      extended byedu.umd.cfar.lamp.viper.geometry.ConvexPolygon
          extended byedu.umd.cfar.lamp.viper.geometry.BoundingBox
All Implemented Interfaces:
BoxInformation, java.lang.Cloneable, HasCentroid, Moveable

public class BoundingBox
extends ConvexPolygon
implements BoxInformation, Moveable, HasCentroid

This class reprsents a rectangle aligned with the coordinate axes or a set of such rectangles.


Field Summary
 
Fields inherited from class edu.umd.cfar.lamp.viper.geometry.PolyList
area, bbox, CARVE_POLYS, composed, originals, polys
 
Fields inherited from interface edu.umd.cfar.lamp.viper.geometry.Moveable
EAST, NORTH, NORTHEAST, NORTHWEST, SOUTH, SOUTHEAST, SOUTHWEST, WEST
 
Constructor Summary
BoundingBox()
          Construct the empty bounding box.
BoundingBox(double leftEdge, double bottomEdge, double width, double height)
          Creates a bounding box in double coordinates.
BoundingBox(int[] dimensions)
          Construct a bounding box with the given origin, width and height.
BoundingBox(int leftEdge, int bottomEdge, int width, int height)
          Construct a rectangle with the given parameters.
BoundingBox(java.awt.Rectangle dimensions)
          Creates a new bounding box from the given java.awt.Rectangle.
BoundingBox(java.lang.String S)
          Creates a new Box from a string representation.
 
Method Summary
 Rational area()
          Calculates the area of the box(es)
 java.lang.Object clone()
          Copies the box(es).
 boolean contains(Pnt point)
          Tests to see if any of the boxes contains the specified point
 BoundingBox copy()
          Like clone, but returns the right type.
 boolean equals(java.lang.Object o)
          Checks to see if the shapes are equal.
 void extendToContain(BoundingBox other)
          Unions this with the specified box(es) and sets this to that union.
 Pnt getCentroid()
          Gets the center of the box.
 int getHeight()
          Gets the height of the box.
 Pnt getNearIntersection(Pnt q1)
          Of the two points on the line between this box's centroid and q1 that instersect this box, this returns the one closer to q1.
 java.util.Iterator getPolys()
          The getPolys iterator returns a set of non-overlapping polygons that, together, cover the same region as this set of boxes.
 java.awt.Rectangle getRectangle()
          Gets the original rectangle
 int getRotation()
          Gets the rotation
 int getWidth()
          Gets the width of the box.
 int getX()
          Gets the x-coordinate of the box's origin.
 int getY()
          Gets the y-coordinate of the box's origin.
protected  void initBbox()
          Creates a BoundingBox around this, for use by getBoundingBox, etc.
static BoundingBox intersection(BoundingBox A, BoundingBox B)
          This creates a new box that represents the area shared by two boxes.
 boolean intersects(BoundingBox other)
          Tests whether this box intersects the specified box.
 Moveable move(int direction, int distance)
          Gets a copy of the box, shifted by the given amount in the specified direction.
 void set(int x, int y, int width, int height)
          Sets this to refer to only the given bounding box.
 BoundingBox setTo(BoundingBox other)
          Like an overloaded '='.
 Moveable shift(int x, int y)
          Shifts the moveable by the given amount.
 java.lang.String toString()
          Gets a parsable, human readable version of the box(es).
static BoundingBox union(BoundingBox A, BoundingBox B)
          Gets a set of boxes which covers all and only the pixels covered by A and B.
static BoundingBox valueOf(java.lang.String S)
          Creates a new Box from a string representation.
 
Methods inherited from class edu.umd.cfar.lamp.viper.geometry.ConvexPolygon
add, addVertex, clearPolygon, clip, contains, getIntersection, intersection, isInside, subtract, toStringListOfPoints
 
Methods inherited from class edu.umd.cfar.lamp.viper.geometry.PolyList
addPoly, clearPolyList, getBoundingBox, getConvexPolygonArray, getFragmentationCount, getOriginals, getPoints, hashCode, intersection, intersects, union
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BoundingBox

public BoundingBox()
Construct the empty bounding box.


BoundingBox

public BoundingBox(int[] dimensions)
Construct a bounding box with the given origin, width and height.

Parameters:
dimensions - int array in the form {x, y, width, height}

BoundingBox

public BoundingBox(int leftEdge,
                   int bottomEdge,
                   int width,
                   int height)
Construct a rectangle with the given parameters. Note that in screen coordinates, the origin is the top left edge.

Parameters:
leftEdge - the distance of the box from the y axis
bottomEdge - the distance of the box from the x axis (in screen coordinates, a more appropriate name would be topEdge
width - the width of the box
height - the height of the box

BoundingBox

public BoundingBox(java.awt.Rectangle dimensions)
Creates a new bounding box from the given java.awt.Rectangle.

Parameters:
dimensions - the box to use

BoundingBox

public BoundingBox(double leftEdge,
                   double bottomEdge,
                   double width,
                   double height)
Creates a bounding box in double coordinates. Just casts the parameters to ints, for now.

Parameters:
leftEdge - the left edge
bottomEdge - the bottom edge (top in screen coordinates)
width - the width of the box
height - the height of the box

BoundingBox

public BoundingBox(java.lang.String S)
            throws BadDataException
Creates a new Box from a string representation.

Parameters:
S - a string representation, a series of 4 numbers representing the bottom left corner, width, and height
Throws:
BadDataException - if the String is malformed
Method Detail

intersection

public static BoundingBox intersection(BoundingBox A,
                                       BoundingBox B)
This creates a new box that represents the area shared by two boxes.

Parameters:
A - a box to intersect
B - a box to intersect
Returns:
a new bbox consisting solely of the shared region.

intersects

public boolean intersects(BoundingBox other)
Tests whether this box intersects the specified box.

Parameters:
other - the other box to test against
Returns:
true iff a pixel is shared between the two

valueOf

public static BoundingBox valueOf(java.lang.String S)
Creates a new Box from a string representation.

Parameters:
S - a string representation, a series of 4 numbers representing the bottom left corner, width, and height
Returns:
a BoundingBox represented by the string
Throws:
BadAttributeDataException - if the String is malformed

setTo

public BoundingBox setTo(BoundingBox other)
Like an overloaded '='. This recycles as much of the data structure as possible.

Parameters:
other - the box(es) to union with this bbox set
Returns:
this

set

public void set(int x,
                int y,
                int width,
                int height)
Sets this to refer to only the given bounding box.

Parameters:
x - the x coordinate of the box origin point
y - the y coordinate of the box origin point
width - the width of the box
height - the height of the box

copy

public BoundingBox copy()
Like clone, but returns the right type.

Returns:
an exact copy of this

clone

public java.lang.Object clone()
Copies the box(es).

Specified by:
clone in interface BoxInformation
Overrides:
clone in class ConvexPolygon
Returns:
a new copy of the data

contains

public boolean contains(Pnt point)
Tests to see if any of the boxes contains the specified point

Specified by:
contains in interface BoxInformation
Overrides:
contains in class ConvexPolygon
Parameters:
point - the point to check for
Returns:
true iff at least one of the boxes in this set contains point

initBbox

protected void initBbox()
Description copied from class: PolyList
Creates a BoundingBox around this, for use by getBoundingBox, etc. This is called whenever the bbox is requested and it is currently set to null; the bbox field therefore acts as a dirty bit.

Overrides:
initBbox in class ConvexPolygon

toString

public java.lang.String toString()
Gets a parsable, human readable version of the box(es). If a single box, it returns a space delimited string containing four integers. If more, it returns a list of such four-lists, each surrounded in parentheses, with the whole list in brackets. Note that this list will be of non-overlapping boxes that, together, cover the same area as the set of boxes that this loosely represents.

Overrides:
toString in class ConvexPolygon
Returns:
a String version of the box(es)

area

public Rational area()
Calculates the area of the box(es)

Overrides:
area in class ConvexPolygon
Returns:
the total number of pixels covered by the box(es)

equals

public boolean equals(java.lang.Object o)
Checks to see if the shapes are equal.

Overrides:
equals in class ConvexPolygon
Parameters:
o - the shape to check. Works for most children of PolyList.
Returns:
true if the given shape covers the same pixels as this set of boxes

union

public static BoundingBox union(BoundingBox A,
                                BoundingBox B)
Gets a set of boxes which covers all and only the pixels covered by A and B.

Parameters:
A - a set of boxes to union with
B - a set of boxes to union with
Returns:
a set of boxes corresponding to the region shared by A and B

extendToContain

public void extendToContain(BoundingBox other)
Unions this with the specified box(es) and sets this to that union.

Parameters:
other - The box(es) to add to this set

getPolys

public java.util.Iterator getPolys()
The getPolys iterator returns a set of non-overlapping polygons that, together, cover the same region as this set of boxes.

Overrides:
getPolys in class ConvexPolygon
Returns:
a set of convex polygons that tile this region

getX

public int getX()
Gets the x-coordinate of the box's origin.

Specified by:
getX in interface BoxInformation
Returns:
the x-coordinate of the box's origin.
Throws:
java.lang.ArithmeticException - if the set of boxes is not a singleton

getY

public int getY()
Gets the y-coordinate of the box's origin.

Specified by:
getY in interface BoxInformation
Returns:
the y-coordinate of the box's origin.
Throws:
java.lang.ArithmeticException - if the set of boxes is not a singleton

getWidth

public int getWidth()
Gets the width of the box.

Specified by:
getWidth in interface BoxInformation
Returns:
the width of the box.
Throws:
java.lang.ArithmeticException - if the set of boxes is not a singleton

getHeight

public int getHeight()
Gets the height of the box.

Specified by:
getHeight in interface BoxInformation
Returns:
the height of the box.
Throws:
java.lang.ArithmeticException - if the set of boxes is not a singleton

getRectangle

public java.awt.Rectangle getRectangle()
Gets the original rectangle

Returns:
the box.

getRotation

public int getRotation()
Gets the rotation

Specified by:
getRotation in interface BoxInformation
Returns:
the integer zero

getCentroid

public Pnt getCentroid()
Gets the center of the box.

Specified by:
getCentroid in interface BoxInformation
Overrides:
getCentroid in class ConvexPolygon
Returns:
the center point of the box as a pair of rational numbers
Throws:
java.lang.ArithmeticException - if the set of boxes is not a singleton

getNearIntersection

public Pnt getNearIntersection(Pnt q1)
Of the two points on the line between this box's centroid and q1 that instersect this box, this returns the one closer to q1.

Specified by:
getNearIntersection in interface BoxInformation
Overrides:
getNearIntersection in class ConvexPolygon
Parameters:
q1 - a point different from the centroid.
Returns:
point on the ray from centriod through q1 that is on the box border

move

public Moveable move(int direction,
                     int distance)
Gets a copy of the box, shifted by the given amount in the specified direction.

Specified by:
move in interface Moveable
Parameters:
direction - the direction to remove the box, e.g. Moveable.NORTH
distance - the distance to move the box
Returns:
a new box, that is a copy of this box, shifted as specified

shift

public Moveable shift(int x,
                      int y)
Description copied from interface: Moveable
Shifts the moveable by the given amount.

Specified by:
shift in interface Moveable
Parameters:
x -
y -
Returns:
See Also:
Moveable.shift(int, int)