Rectangle Class Reference

A basic rectangle class. More...

#include <Image/Rectangle.H>

List of all members.

Public Member Functions

 Rectangle (const Point2D< int > &topleft, const Dims &dims)
 Build from a top-left corner and some dims.
 Rectangle (const Rectangle &rect)
 Copy constructor.
 Rectangle ()
 Uninitialized constructor (useful for arrays).
Rectangle getOverlap (const Rectangle &r2) const
 get overlap between this rectangle and passed in rectangle
double getOverlapRatio (const Rectangle &r2) const
 compute overlap as area of intersection / area of union
bool contains (const Point2D< int > &p) const
 check if point p is within rectangle
bool contains (const Rectangle &r) const
 check if this rect contains another
int top () const
 get (inner) top coordinate
int bottomI () const
 get inner bottom coordinate (=top+height-1)
int bottomO () const
 get outer bottom coordinate (=top+height)
int left () const
 get (inner) left coordinate
int rightI () const
 get inner right coordinate (=left+width-1)
int rightO () const
 get outer right coordinate (=left+width)
Point2D< int > topLeft () const
 Get (outer) top left corner point.
Point2D< int > topRight () const
 Get (outer) top right corner point.
Point2D< int > bottomLeft () const
 Get (outer) bottom left corner point.
Point2D< int > bottomRight () const
 Get (outer) bottom right corner point.
Dims dims () const
 Get the rectangle's width, height.
int width () const
 Access some metric info.
int height () const
 Access some metric info.
int area () const
 Access some metric info.
float aspect () const
 Access some metric info.
Point2D< int > center () const
 The center of the rectangle.
bool isValid () const
 whether the rectangle is valid

Static Public Member Functions

static Rectangle tlbrI (int tt, int ll, int bb, int rr)
 Build from 4 coordinates (top, left, inner-bottom, inner-right).
static Rectangle tlbrO (int tt, int ll, int bb, int rr)
 Build from 4 coordinates (top, left, outer-bottom, outer-right).
static Rectangle centerDims (const Point2D< int > &center, const Dims &dims)
 Build from center loc and dims.

Detailed Description

A basic rectangle class.

This is a shorthand class to represent a 2D rectangle, as defined by integer top, left, bottom and right coordinates. The goal of this object is not to have to check if left < right, etc... for each function using a Rectangle. The object checks for that at its creation, and is always internally coherent. Image convention: top < bottom, left < right.

There is often some ambiguity about how to represent the left-right and top-bottom ranges involved in a rectangle. Imagine a pixel grid like this:

+---+---+---+---+ | | | | | | | | | | +---+---+---+---+ | | | | | | | | | | +---+---+---+---+ | | | | | | | | | | +---+---+---+---+ | | | | | | | | | | +---+---+---+---+

If we want a Rectangle that covers this whole 4x4 grid, how should we represent the coordinates? Clearly the upper left corner is (0,0), but what about the bottom right corner? One answer would be that the lower right corner is (4,4), in which we imagine the coordinate numbers falling between the cracks of the pixels:

. 0...1...2...3...4 0 +---+---+---+---+ . | | | | | . | | | | | 1 +---+---+---+---+ . | | | | | . | | | | | 2 +---+---+---+---+ . | | | | | . | | | | | 3 +---+---+---+---+ . | | | | | . | | | | | 4 +---+---+---+---+

Another answer is that the lower right corner should be labeled as (3,3), if we envision the coordinate numbers labeling the pixel centers, and this approach is more natural if we want to draw a line just inside the border of the rectangle as shown by the '*' here:

. ..0...1...2...3.. . +---+---+---+---+ 0 |***|***|***|***| . |***|***|***|***| . +---+---+---+---+ 1 |***| | |***| . |***| | |***| . +---+---+---+---+ 2 |***| | |***| . |***| | |***| . +---+---+---+---+ 3 |***|***|***|***| . |***|***|***|***| . +---+---+---+---+

In the Rectangle class we support both approaches; the (4,4) coords are called 'outer' coordinates and corresponding functions have a 'O' suffix (bottomO(), right(), and tlbrO()), while the (3,3) coords are called 'inner' coordinates and corresponding functions have an 'I' suffix (bottomI(), rightI(), and tlbrI()).

The internal representation of Rectangle is based on outer coordinates, but this has no effect on users of the class who are free to user whichever of inner or outer coords are more convenient for their purposes.

Outer coordinates are more natural for uniformly scaling a rectangle up or down by a factor. For example, if we want to scale the 4x4 rect up by a factor of 2 giving an 8x8 rect, then in outer coords we just do newright=right*2=4*2=8 and newbottom=bottom*2=4*2=8. But with inner coords we can't just multiply by 2: the inner bottom-right coords are (3,3), and if we scale those by 2 we get (6,6) which represents a 7x7 rectangle rather than the correct 8x8 result.

HISTORICAL NOTE (2007-Mar-20): The Rectangle class previously used only inner coordinates both for its implementation and in its public interface. This lead to clunky code in some places where many -1 or +1 offsets were needed to handle rectangle coordinates.

Definition at line 143 of file Rectangle.H.


Constructor & Destructor Documentation

Rectangle::Rectangle ( const Point2D< int > &  topleft,
const Dims dims 
) [inline]

Build from a top-left corner and some dims.

Definition at line 395 of file Rectangle.H.

Rectangle::Rectangle ( const Rectangle rect  )  [inline]

Copy constructor.

Definition at line 402 of file Rectangle.H.

Rectangle::Rectangle (  )  [inline]

Uninitialized constructor (useful for arrays).

Definition at line 407 of file Rectangle.H.

Referenced by centerDims(), CudaFramework::drawRectangle_centrepoint(), CudaFramework::drawRectangle_topleftpoint(), getOverlap(), tlbrI(), and tlbrO().


Member Function Documentation

int Rectangle::area (  )  const [inline]

Access some metric info.

Definition at line 451 of file Rectangle.H.

References height(), and width().

Referenced by getOverlapRatio(), and SDLdisplay::start2().

float Rectangle::aspect (  )  const [inline]

Access some metric info.

Definition at line 457 of file Rectangle.H.

References height(), and width().

int Rectangle::bottomI (  )  const [inline]
Point2D< int > Rectangle::bottomLeft (  )  const [inline]

Get (outer) bottom left corner point.

Definition at line 509 of file Rectangle.H.

References ASSERT.

Referenced by InferoTemporalSIFT::attentionShift().

int Rectangle::bottomO (  )  const [inline]
Point2D< int > Rectangle::bottomRight (  )  const [inline]

Get (outer) bottom right corner point.

Definition at line 512 of file Rectangle.H.

References ASSERT.

Referenced by InferoTemporalSIFT::attentionShift().

Point2D< int > Rectangle::center (  )  const [inline]

The center of the rectangle.

Definition at line 463 of file Rectangle.H.

Referenced by InferoTemporalSIFT::attentionShift().

Rectangle Rectangle::centerDims ( const Point2D< int > &  center,
const Dims dims 
) [inline, static]

Build from center loc and dims.

Definition at line 391 of file Rectangle.H.

References Dims::h(), Point2D< T >::i, Rectangle(), and Dims::w().

Referenced by InferoTemporalSIFT::attentionShift().

bool Rectangle::contains ( const Rectangle r  )  const [inline]

check if this rect contains another

The boundaries are allowed to coincide, such that by this definition a rectangle "contains" itself.

Definition at line 476 of file Rectangle.H.

bool Rectangle::contains ( const Point2D< int > &  p  )  const [inline]

check if point p is within rectangle

CAUTION: boundaries are included.

Definition at line 470 of file Rectangle.H.

References Point2D< T >::i.

Referenced by constrainRect(), and InferotemporalCortexI::evolve().

Dims Rectangle::dims (  )  const [inline]

Get the rectangle's width, height.

Definition at line 515 of file Rectangle.H.

References ASSERT.

Referenced by InputFrameSeries::peekFrameSpec().

Rectangle Rectangle::getOverlap ( const Rectangle r2  )  const [inline]
double Rectangle::getOverlapRatio ( const Rectangle r2  )  const [inline]

compute overlap as area of intersection / area of union

Definition at line 434 of file Rectangle.H.

References area(), getOverlap(), and isValid().

int Rectangle::height (  )  const [inline]
bool Rectangle::isValid (  )  const [inline]
int Rectangle::left (  )  const [inline]
int Rectangle::rightI (  )  const [inline]
int Rectangle::rightO (  )  const [inline]
Rectangle Rectangle::tlbrI ( int  tt,
int  ll,
int  bb,
int  rr 
) [inline, static]
Rectangle Rectangle::tlbrO ( int  tt,
int  ll,
int  bb,
int  rr 
) [inline, static]
int Rectangle::top (  )  const [inline]
Point2D< int > Rectangle::topLeft (  )  const [inline]

Get (outer) top left corner point.

Definition at line 503 of file Rectangle.H.

References ASSERT.

Referenced by InferoTemporalSIFT::attentionShift(), and ObjDetChannel::doInput().

Point2D< int > Rectangle::topRight (  )  const [inline]

Get (outer) top right corner point.

Definition at line 506 of file Rectangle.H.

References ASSERT.

Referenced by InferoTemporalSIFT::attentionShift().

int Rectangle::width (  )  const [inline]

The documentation for this class was generated from the following file:
Generated on Sun May 8 08:24:11 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3