CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
cmlabs::Box Class Reference

Axis-aligned rectangle defined by an upper-left corner and a Size, with overlap/containment math. More...

#include <MathClasses.h>

Collaboration diagram for cmlabs::Box:
[legend]

Public Member Functions

 Box ()
 Empty box at the origin.
 Box (PointFloat upperleft, Size boxsize, double linewidth=0)
 From corner + size.
 Box (PointFloat upperleft, PointFloat lowerright, double linewidth=0)
 From two opposite corners.
 Box (double x, double y, double w, double h, double linewidth=0)
 From scalar corner and dimensions.
virtual ~Box ()
double getUpperY () const
double getLowerY () const
double getLeftX () const
double getRightX () const
double getArea () const
double getCMX () const
double getCMY () const
PointFloat getCM () const
PointFloat getUpperLeft () const
PointFloat getUpperRight () const
PointFloat getLowerLeft () const
PointFloat getLowerRight () const
double getLineWidth () const
Size getSize () const
double getWidth () const
double getHeight () const
Box operator- (const Point &p) const
 Box translated by -p.
Box operator- (const PointFloat &p) const
 Box translated by -p.
Box operator+ (const Point &p) const
 Box translated by +p.
Box operator+ (const PointFloat &p) const
 Box translated by +p.
bool move (double dx, double dy)
 Translate in place.
bool moveTo (double x, double y)
 Move the upper-left corner to (x,y).
bool set (double x, double y, double w, double h, double linewidth=0)
 Reset all geometry.
bool setUpperLeft (const PointFloat &point)
 Set the corner.
bool setSize (const Size &boxsize)
 Set the extent.
bool setLineWidth (double width)
 Set the stroke width.
bool grow (double dw, double dh)
 Enlarge by (dw,dh), keeping the upper-left corner.
bool isPointWithin (const PointFloat &point) const
bool isPointWithin (int x, int y) const
PointFloat getCentreMass () const
bool hasZeroSize () const
bool equals (const Box &otherbox) const
 Exact geometric equality.
bool equals (const Box &otherbox, double maxerror) const
 Approximate equality within maxerror per component.
Box getBoundingBox (const Box &otherbox) const
bool growToBoundingBox (const Box &otherbox)
 Enlarge in place to include otherbox.
bool growToIncludePoint (const Point &point, int padX=0, int padY=0)
 Enlarge to include point plus padding.
Box getOverlapBox (const Box &otherbox) const
double percentOverlap (const Box &otherbox) const
 Overlap between the two boxes as a fraction of the larger area.
Box getDoubleSizeSameCenter ()
bool constrainTo (const Box &box)
 Clip this box in place to lie within box.
bool constrainTo (double x, double y, double w, double h)
 Clip in place to the given rectangle.
Box getConstrainedCopy (const Box &box)
Box getConstrainedCopy (double x, double y, double w, double h)
std::string print ()
 Human-readable representation for debugging.

Public Attributes

PointFloat upperLeft
 Upper-left corner.
Size size
 Extent.
double lineWidth
 Stroke width (drawing hint).
std::string name
 Optional label.
std::string comment
 Optional free-form comment.
double orientation
 Optional rotation metadata (radians); not applied by the geometric queries.

Detailed Description

Axis-aligned rectangle defined by an upper-left corner and a Size, with overlap/containment math.

Widely used for image regions and tracked object bounding boxes. Y grows downward (screen convention): getUpperY() < getLowerY(), and "upper left" is the minimum-x/minimum-y corner. All geometric queries treat the box as axis-aligned; the orientation field is metadata only. Optional metadata (name, comment, orientation) travels with the box.

using namespace cmlabs;
Box a(10, 10, 100, 50); // x, y, w, h
Box b(60, 20, 100, 50);
Box overlap = a.getOverlapBox(b); // zero-sized when disjoint
double frac = a.percentOverlap(b); // fraction [0,1], NOT 0-100
Box bounds = a.getBoundingBox(b); // smallest box containing both
a.constrainTo(0, 0, 640, 480); // clip in place to the image area
Box()
Empty box at the origin.

Definition at line 557 of file MathClasses.h.

Constructor & Destructor Documentation

◆ Box() [1/4]

◆ Box() [2/4]

cmlabs::Box::Box ( PointFloat upperleft,
Size boxsize,
double linewidth = 0 )

From corner + size.

Definition at line 512 of file MathClasses.cpp.

References lineWidth, orientation, size, and upperLeft.

◆ Box() [3/4]

cmlabs::Box::Box ( PointFloat upperleft,
PointFloat lowerright,
double linewidth = 0 )

From two opposite corners.

Definition at line 530 of file MathClasses.cpp.

References cmlabs::PointFloat::getX(), cmlabs::PointFloat::getY(), lineWidth, orientation, size, and upperLeft.

◆ Box() [4/4]

cmlabs::Box::Box ( double x,
double y,
double w,
double h,
double linewidth = 0 )

From scalar corner and dimensions.

Definition at line 494 of file MathClasses.cpp.

References lineWidth, orientation, size, and upperLeft.

◆ ~Box()

cmlabs::Box::~Box ( )
virtual

Definition at line 548 of file MathClasses.cpp.

Member Function Documentation

◆ constrainTo() [1/2]

bool cmlabs::Box::constrainTo ( const Box & box)

Clip this box in place to lie within box.

Returns
true when still valid.

Definition at line 670 of file MathClasses.cpp.

References Box(), constrainTo(), cmlabs::Size::h, size, upperLeft, cmlabs::Size::w, cmlabs::PointFloat::x, and cmlabs::PointFloat::y.

Referenced by constrainTo(), getConstrainedCopy(), and getConstrainedCopy().

◆ constrainTo() [2/2]

bool cmlabs::Box::constrainTo ( double x,
double y,
double w,
double h )

Clip in place to the given rectangle.

Returns
true when still valid.

Definition at line 674 of file MathClasses.cpp.

References size, and upperLeft.

◆ equals() [1/2]

bool cmlabs::Box::equals ( const Box & otherbox) const

Exact geometric equality.

Definition at line 762 of file MathClasses.cpp.

References Box(), orientation, size, and upperLeft.

◆ equals() [2/2]

bool cmlabs::Box::equals ( const Box & otherbox,
double maxerror ) const

Approximate equality within maxerror per component.

Definition at line 769 of file MathClasses.cpp.

References Box(), and percentOverlap().

◆ getArea()

double cmlabs::Box::getArea ( ) const
Returns
w*h.

Definition at line 605 of file MathClasses.cpp.

References size.

Referenced by cmlabs::MathClasses_UnitTest().

◆ getBoundingBox()

Box cmlabs::Box::getBoundingBox ( const Box & otherbox) const
Returns
Smallest box containing both boxes.

Definition at line 804 of file MathClasses.cpp.

References Box(), getLeftX(), getLowerY(), getRightX(), and getUpperY().

Referenced by growToBoundingBox(), and cmlabs::MathClasses_UnitTest().

◆ getCentreMass()

PointFloat cmlabs::Box::getCentreMass ( ) const
Returns
Center of mass (same as getCM()).

Definition at line 754 of file MathClasses.cpp.

References size, and upperLeft.

◆ getCM()

PointFloat cmlabs::Box::getCM ( ) const
Returns
Center of mass.

Definition at line 560 of file MathClasses.cpp.

References size, and upperLeft.

Referenced by getCMX(), getCMY(), and cmlabs::MathClasses_UnitTest().

◆ getCMX()

double cmlabs::Box::getCMX ( ) const
Returns
Center-of-mass x.

Definition at line 552 of file MathClasses.cpp.

References getCM(), and cmlabs::PointFloat::getX().

◆ getCMY()

double cmlabs::Box::getCMY ( ) const
Returns
Center-of-mass y.

Definition at line 556 of file MathClasses.cpp.

References getCM(), and cmlabs::PointFloat::getY().

◆ getConstrainedCopy() [1/2]

Box cmlabs::Box::getConstrainedCopy ( const Box & box)
Returns
Clipped copy within box.

Definition at line 658 of file MathClasses.cpp.

References Box(), and constrainTo().

◆ getConstrainedCopy() [2/2]

Box cmlabs::Box::getConstrainedCopy ( double x,
double y,
double w,
double h )
Returns
Clipped copy within the given rectangle.

Definition at line 664 of file MathClasses.cpp.

References Box(), and constrainTo().

◆ getDoubleSizeSameCenter()

Box cmlabs::Box::getDoubleSizeSameCenter ( )
Returns
Box with doubled dimensions and the same center.

Definition at line 686 of file MathClasses.cpp.

References Box(), cmlabs::Size::h, size, upperLeft, cmlabs::Size::w, cmlabs::PointFloat::x, and cmlabs::PointFloat::y.

◆ getHeight()

double cmlabs::Box::getHeight ( ) const
Returns
Height.

Definition at line 613 of file MathClasses.cpp.

References size.

Referenced by cmlabs::Bitmap::drawBitmap(), cmlabs::Bitmap::fillBox(), grow(), cmlabs::MathClasses_UnitTest(), move(), and moveTo().

◆ getLeftX()

double cmlabs::Box::getLeftX ( ) const

◆ getLineWidth()

double cmlabs::Box::getLineWidth ( ) const
Returns
Stroke width.

Definition at line 597 of file MathClasses.cpp.

References lineWidth.

Referenced by cmlabs::Bitmap::drawBox().

◆ getLowerLeft()

PointFloat cmlabs::Box::getLowerLeft ( ) const
Returns
Lower-left corner.

Definition at line 572 of file MathClasses.cpp.

References size, and upperLeft.

Referenced by cmlabs::Bitmap::drawBox().

◆ getLowerRight()

PointFloat cmlabs::Box::getLowerRight ( ) const
Returns
Lower-right corner.

Definition at line 576 of file MathClasses.cpp.

References size, and upperLeft.

Referenced by cmlabs::Bitmap::drawBox(), and print().

◆ getLowerY()

double cmlabs::Box::getLowerY ( ) const
Returns
Bottom edge y.

Definition at line 584 of file MathClasses.cpp.

References size, and upperLeft.

Referenced by getBoundingBox(), getOverlapBox(), isPointWithin(), and cmlabs::MathClasses_UnitTest().

◆ getOverlapBox()

Box cmlabs::Box::getOverlapBox ( const Box & otherbox) const
Returns
Intersection box (zero-sized when disjoint).

Definition at line 831 of file MathClasses.cpp.

References Box(), getLeftX(), getLowerY(), getRightX(), and getUpperY().

Referenced by cmlabs::MathClasses_UnitTest(), and percentOverlap().

◆ getRightX()

double cmlabs::Box::getRightX ( ) const
Returns
Right edge x.

Definition at line 592 of file MathClasses.cpp.

References size, and upperLeft.

Referenced by getBoundingBox(), getOverlapBox(), isPointWithin(), and cmlabs::MathClasses_UnitTest().

◆ getSize()

Size cmlabs::Box::getSize ( ) const
Returns
Extent.

Definition at line 601 of file MathClasses.cpp.

References size.

Referenced by cmlabs::MathClasses_UnitTest(), and percentOverlap().

◆ getUpperLeft()

PointFloat cmlabs::Box::getUpperLeft ( ) const
Returns
Upper-left corner.

Definition at line 564 of file MathClasses.cpp.

References upperLeft.

Referenced by cmlabs::Bitmap::drawBox(), and print().

◆ getUpperRight()

PointFloat cmlabs::Box::getUpperRight ( ) const
Returns
Upper-right corner.

Definition at line 568 of file MathClasses.cpp.

References size, and upperLeft.

Referenced by cmlabs::Bitmap::drawBox().

◆ getUpperY()

double cmlabs::Box::getUpperY ( ) const

◆ getWidth()

double cmlabs::Box::getWidth ( ) const
Returns
Width.

Definition at line 609 of file MathClasses.cpp.

References size.

Referenced by cmlabs::Bitmap::drawBitmap(), cmlabs::Bitmap::fillBox(), grow(), cmlabs::MathClasses_UnitTest(), move(), and moveTo().

◆ grow()

bool cmlabs::Box::grow ( double dw,
double dh )

Enlarge by (dw,dh), keeping the upper-left corner.

Returns
true.

Definition at line 650 of file MathClasses.cpp.

References getHeight(), getWidth(), and setSize().

Referenced by cmlabs::MathClasses_UnitTest().

◆ growToBoundingBox()

bool cmlabs::Box::growToBoundingBox ( const Box & otherbox)

Enlarge in place to include otherbox.

Returns
true.

Definition at line 775 of file MathClasses.cpp.

References Box(), getBoundingBox(), hasZeroSize(), size, and upperLeft.

◆ growToIncludePoint()

bool cmlabs::Box::growToIncludePoint ( const Point & point,
int padX = 0,
int padY = 0 )

Enlarge to include point plus padding.

Returns
true.

Definition at line 784 of file MathClasses.cpp.

References size, upperLeft, cmlabs::Point::x, and cmlabs::Point::y.

◆ hasZeroSize()

bool cmlabs::Box::hasZeroSize ( ) const
Returns
true when width or height is zero.

Definition at line 758 of file MathClasses.cpp.

References size.

Referenced by growToBoundingBox().

◆ isPointWithin() [1/2]

bool cmlabs::Box::isPointWithin ( const PointFloat & point) const
Returns
true when point lies inside (inclusive).

Definition at line 742 of file MathClasses.cpp.

References cmlabs::PointFloat::getX(), and cmlabs::PointFloat::getY().

Referenced by cmlabs::MathClasses_UnitTest().

◆ isPointWithin() [2/2]

bool cmlabs::Box::isPointWithin ( int x,
int y ) const
Returns
true when (x,y) lies inside (inclusive).

Definition at line 730 of file MathClasses.cpp.

References getLeftX(), getLowerY(), getRightX(), and getUpperY().

◆ move()

bool cmlabs::Box::move ( double dx,
double dy )

Translate in place.

Returns
true.

Definition at line 642 of file MathClasses.cpp.

References getHeight(), getLeftX(), getUpperY(), getWidth(), and set().

Referenced by cmlabs::MathClasses_UnitTest().

◆ moveTo()

bool cmlabs::Box::moveTo ( double x,
double y )

Move the upper-left corner to (x,y).

Returns
true.

Definition at line 646 of file MathClasses.cpp.

References getHeight(), getWidth(), and set().

◆ operator+() [1/2]

Box cmlabs::Box::operator+ ( const Point & p) const

Box translated by +p.

Definition at line 630 of file MathClasses.cpp.

References Box(), and upperLeft.

◆ operator+() [2/2]

Box cmlabs::Box::operator+ ( const PointFloat & p) const

Box translated by +p.

Definition at line 636 of file MathClasses.cpp.

References Box(), and upperLeft.

◆ operator-() [1/2]

Box cmlabs::Box::operator- ( const Point & p) const

Box translated by -p.

Definition at line 618 of file MathClasses.cpp.

References Box(), and upperLeft.

◆ operator-() [2/2]

Box cmlabs::Box::operator- ( const PointFloat & p) const

Box translated by -p.

Definition at line 624 of file MathClasses.cpp.

References Box(), and upperLeft.

◆ percentOverlap()

double cmlabs::Box::percentOverlap ( const Box & otherbox) const

Overlap between the two boxes as a fraction of the larger area.

Computed as overlapArea/myArea when this box is the larger, otherwise the ratio is inverted so the result stays in [0,1].

Returns
Overlap fraction in [0,1] (0 when disjoint).
Note
Despite the name, the value is a fraction (0..1), not a percentage (0..100).

Definition at line 872 of file MathClasses.cpp.

References Box(), cmlabs::Size::getArea(), getOverlapBox(), getSize(), cmlabs::Size::isNonZero(), and size.

Referenced by equals(), and cmlabs::MathClasses_UnitTest().

◆ print()

std::string cmlabs::Box::print ( )

Human-readable representation for debugging.

Definition at line 895 of file MathClasses.cpp.

References getLowerRight(), getUpperLeft(), lineWidth, print(), and cmlabs::utils::StringFormat().

Referenced by print().

◆ set()

bool cmlabs::Box::set ( double x,
double y,
double w,
double h,
double linewidth = 0 )

Reset all geometry.

Returns
true.

Definition at line 695 of file MathClasses.cpp.

References lineWidth, size, and upperLeft.

Referenced by move(), and moveTo().

◆ setLineWidth()

bool cmlabs::Box::setLineWidth ( double width)

Set the stroke width.

Returns
true.

Definition at line 724 of file MathClasses.cpp.

References lineWidth.

◆ setSize()

bool cmlabs::Box::setSize ( const Size & boxsize)

Set the extent.

Returns
true.

Definition at line 719 of file MathClasses.cpp.

References size.

Referenced by grow().

◆ setUpperLeft()

bool cmlabs::Box::setUpperLeft ( const PointFloat & point)

Set the corner.

Returns
true.

Definition at line 714 of file MathClasses.cpp.

References upperLeft.

Member Data Documentation

◆ comment

std::string cmlabs::Box::comment

Optional free-form comment.

Definition at line 570 of file MathClasses.h.

◆ lineWidth

double cmlabs::Box::lineWidth

Stroke width (drawing hint).

Definition at line 568 of file MathClasses.h.

Referenced by Box(), Box(), Box(), Box(), getLineWidth(), print(), set(), and setLineWidth().

◆ name

std::string cmlabs::Box::name

Optional label.

Definition at line 569 of file MathClasses.h.

◆ orientation

double cmlabs::Box::orientation

Optional rotation metadata (radians); not applied by the geometric queries.

Definition at line 571 of file MathClasses.h.

Referenced by Box(), Box(), Box(), Box(), and equals().

◆ size

◆ upperLeft


The documentation for this class was generated from the following files: