Kaim::Box2i Class Reference

Kaim::Box2i Class Reference

#include <box2i.h>

Class Description

This class represents a two-dimensional axis-aligned bounding box whose dimensions are stored using 32-bit integers.

This class may be used to store (X,Y) coordinates. For example, the CoordBox type (used during NavData generation) uses this class as follows:

minX=2 , maxX=8 , countX=7 (=8-2 +1)
minY=3 , maxY=6 , countY=4 (=6-3 +1)
/\Y
| | | | | | | | | |
7+---+---+---+---+---+---+---+---+---+--
| | | | | | | | | |
6+---+---#########################---+--
| | # | | | | | # |
5+---+---#---+---+---+---+---+---#---+--
| | # | | | | | # |
4+---+---#---+---+---+---+---+---#---+--
| | # | | | | | # |
3+---+---#########################---+--
| | | | | | | | | |
2+---+---+---+---+---+---+---+---+---+--
| | | | | | | | | |
1+---+---+---+---+---+---+---+---+---+--
| | | | | | | | | |
0+---+---+---+---+---+---+---+---+---+-> X
0 1 2 3 4 5 6 7 8 9

Alternatively, this class may be used to refer to elements within a regular grid according to their column and row. For example, the PixelBox, CellBox and TileBox types (used during NavData generation) use this class as follows:

minX=2 , maxX=7 , countX=6 (=7-2 +1)
minY=3 , maxY=5 , countY=3 (=5-3 +1)
/\Y
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+--
|0,6|1,6|2,6| | | | | | |
+---+---#########################---+--
|0,5|1,5#2,5|3,5|4,5|5,5|6,5|7,5# |
+---+---#---+---+---+---+---+---#---+--
|0,4|1,4#2,4|3,4|4,4|5,4|6,4|7,4# |
+---+---#---+---+---+---+---+---#---+--
|0,3|1,3#2,3|3,3|4,3|5,3|6,3|7,3# |
+---+---#########################---+--
|0,2|1,2|2,2| | | | | | |
+---+---+---+---+---+---+---+---+---+--
|0,1|1,1|2,1| | | | | | |
+---+---+---+---+---+---+---+---+---+--
|0,0|1,0|2,0| | | | | | |
+---+---+---+---+---+---+---+---+---+-> X

Each instance of this class also maintains a count of the number of elements (or grid cells) contained by the box along its X and Y axes, accessible through the CountX() and CountY() methods. For coordinate boxes, all coordinates along the edge of the box are counted, including the corners. For grid boxes, all grid cells that are contained within the box are counted. See the images above for examples.

+ Examples:

Friends

void SwapEndianness (Kaim::Endianness::Target e, Box2i &self)
 

Constructors

 Box2i ()
 
 Box2i (const Vec2i &min_, const Vec2i &max_)
 
 Box2i (KyInt32 min_x, KyInt32 min_y, KyInt32 max_x, KyInt32 max_y)
 

Main API Functions

bool IsValid () const
 
void MakeZero ()
 
void Set (const Vec2i &min_, const Vec2i &max_)
 
void Set (KyInt32 min_x, KyInt32 min_y, KyInt32 max_x, KyInt32 max_y)
 
void SetSafe (const Vec2i &min_, const Vec2i &max_)
 
void SetMin (const Vec2i &min_)
 
void SetMax (const Vec2i &max_)
 
const Vec2iMin () const
 
const Vec2iMax () const
 
Vec2i CornerSouthWest () const
 
Vec2i CornerNorthEast () const
 
Vec2i CornerSouthEast () const
 
Vec2i CornerNorthWest () const
 
const Vec2iCountXY () const
 
KyInt32 CountX () const
 
KyInt32 CountY () const
 
bool IsInside (const Vec2i &pos) const
 
bool FastIsInside (const Vec2i &pos) const
 
bool IsInsideStrictly (const Vec2i &pos) const
 
void Clear ()
 
Box2ioperator*= (KyInt32 s)
 
Box2ioperator/= (KyInt32 s)
 
Box2ioperator+= (const Vec2i &v)
 
Box2ioperator-= (const Vec2i &v)
 
bool operator== (const Box2i &other) const
 
bool operator!= (const Box2i &other) const
 
KyInt32 GetColumnMajorIndex (const Vec2i &pos) const
 
KyInt32 GetRowMajorIndex (const Vec2i &pos) const
 
KyInt32 GetColumnMajorIndexFromLocalPos (const Vec2i &localPos) const
 
KyInt32 GetRowMajorIndexFromLocalPos (const Vec2i &localPos) const
 
void ExpandByVec2 (const Vec2i &pos)
 
void ExpandByVec3 (const Vec3i &pos)
 
void ExpandByTriangle (const Triangle3i &triangle)
 
void ExpandByBox2 (const Box2i &box)
 
void Enlarge (KyInt32 enlargement)
 
void GetEnlarged (KyInt32 enlargement, Box2i &enlarged) const
 
bool IntersectWith (const Box2i &box)
 
bool SetAsIntersection (const Box2i &box_1, const Box2i &box_2)
 

Constructor & Destructor Documentation

Kaim::Box2i::Box2i ( )
inline

Creates a new Box2i with invalid extents: you must call Set() before using it.

Kaim::Box2i::Box2i ( const Vec2i min_,
const Vec2i max_ 
)
inline

Creates a new Box2i with the provided extents.

Parameters
min_The minima of the bounding box.
max_The maxima of the bounding box.
Kaim::Box2i::Box2i ( KyInt32  min_x,
KyInt32  min_y,
KyInt32  max_x,
KyInt32  max_y 
)
inline

Creates a new Box2i with the provided extents.

Parameters
min_xThe minimum position of the bounding box on the X axis.
min_yThe minimum position of the bounding box on the Y axis.
max_xThe maximum position of the bounding box on the X axis.
max_yThe maximum position of the bounding box on the Y axis.

Member Function Documentation

void Kaim::Box2i::Clear ( )
inline

Clears all information maintained by this object.

This method sets the extents of the box to invalid values; you must follow this method with a call to Set() before you can use the box.

Vec2i Kaim::Box2i::CornerNorthEast ( ) const
inline

Retrieves the coordinates of the North-East corner of the bounding box.

Since North is considered to be the positive direction of the Y axis and East is considered to be the positive direction of the Y axis, this corner is equivalent to the maxima.

Vec2i Kaim::Box2i::CornerNorthWest ( ) const
inline

Retrieves the coordinates of the North-West corner of the bounding box.

Since North is considered to be the positive direction of the Y axis and East is considered to be the positive direction of the Y axis, this corner has the maximum value on the Y axis and the minimum value on the X axis.

Vec2i Kaim::Box2i::CornerSouthEast ( ) const
inline

Retrieves the coordinates of the South-East corner of the bounding box.

Since North is considered to be the positive direction of the Y axis and East is considered to be the positive direction of the Y axis, this corner has the minimum value on the Y axis and the maximum value on the X axis.

Vec2i Kaim::Box2i::CornerSouthWest ( ) const
inline

Retrieves the coordinates of the South-West corner of the bounding box.

Since North is considered to be the positive direction of the Y axis and East is considered to be the positive direction of the Y axis, this corner is equivalent to the minima.

KyInt32 Kaim::Box2i::CountX ( ) const
inline

Retrieves the number of grid cells contained within this box along its X axis.

const Vec2i& Kaim::Box2i::CountXY ( ) const
inline

Retrieves the number of grid cells contained within this box along its X and Y axes.

KyInt32 Kaim::Box2i::CountY ( ) const
inline

Retrieves the number of grid cells contained within this box along its Y axis.

void Kaim::Box2i::Enlarge ( KyInt32  enlargement)
inline

Enlarges the extents of the bounding box by the specified amount in all directions.

void Kaim::Box2i::ExpandByBox2 ( const Box2i box)
inline

Enlarges the extents of the bounding box to include the area covered by the specified bounding box.

If the bounding box already encompasses the area of box, the bounding box is not modified.

void Kaim::Box2i::ExpandByTriangle ( const Triangle3i triangle)
inline

Enlarges the extents of the bounding box to include the (X,Y) extents of the specified three-dimensional triangle.

If the (X,Y) extents of the triangle are already contained within the bounding box, the box is not modified.

void Kaim::Box2i::ExpandByVec2 ( const Vec2i pos)
inline

Enlarges the extents of the bounding box to include the specified two-dimensional point.

If the point is already contained within the bounding box, the box is not modified.

void Kaim::Box2i::ExpandByVec3 ( const Vec3i pos)
inline

Enlarges the extents of the bounding box to include the (X,Y) coordinates of the specified three-dimensional point.

If the (X,Y) coordinates of the point are already contained within the bounding box, the box is not modified.

bool Kaim::Box2i::FastIsInside ( const Vec2i pos) const
inline

Returns true if the specified position is contained within the extents of the bounding box or if the specified position is located on the edge of the bounding box.

Warning: this method can cause overflow during computation. It should not be used with integers whose values take up 31 bits.

KyInt32 Kaim::Box2i::GetColumnMajorIndex ( const Vec2i pos) const
inline

Retrieves the index of the grid cell at the specified (X,Y) position, counting cells column-by-column.

This indexes the cells in the box according to the following pattern:

/\Y
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+--
| | | | | | | | | |
+---+---#########################---+--
| | # 2 | 5 | 8 | 11| 14| 17# |
+---+---#---+---+---+---+---+---#---+--
| | # 1 | 4 | 7 | 10| 13| 16# |
+---+---#---+---+---+---+---+---#---+--
| | # 0 | 3 | 6 | 9 | 12| 15# |
+---+---#########################---+--
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+-> X
KyInt32 Kaim::Box2i::GetColumnMajorIndexFromLocalPos ( const Vec2i localPos) const
inline

Retrieves the index of the grid cell at the specified (X,Y) position, counting cells column-by-column.

The X and Y coordinates within localPos should be relative to the minima of the box.

See also GetColumnMajorIndex() for information on the index value returned.

void Kaim::Box2i::GetEnlarged ( KyInt32  enlargement,
Box2i enlarged 
) const
inline

Enlarges the extents of the bounding box by the specified amount in all directions, and stores the new dimensions in the enlarged parameter.

Parameters
enlargementThe amount to enlarge the box in each direction.
[out]enlargedStores the enlarged bounding box.
KyInt32 Kaim::Box2i::GetRowMajorIndex ( const Vec2i pos) const
inline

Retrieves the index of the grid cell at the specified (X,Y) position, counting cells row-by-row.

This indexes the cells in the box according to the following pattern:

/\Y
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+--
| | | | | | | | | |
+---+---#########################---+--
| | # 12| 13| 14| 15| 16| 17# |
+---+---#---+---+---+---+---+---#---+--
| | # 6 | 7 | 8 | 9 | 10| 11# |
+---+---#---+---+---+---+---+---#---+--
| | # 0 | 1 | 2 | 3 | 4 | 5 # |
+---+---#########################---+--
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+-> X
KyInt32 Kaim::Box2i::GetRowMajorIndexFromLocalPos ( const Vec2i localPos) const
inline

Retrieves the index of the grid cell at the specified (X,Y) position, counting cells row-by-row.

The X and Y coordinates within localPos should be relative to the minima of the box.

See also GetRowMajorIndex() for information on the index value returned.

bool Kaim::Box2i::IntersectWith ( const Box2i box)
inline

Resizes this bounding box to contain only the grid cells that are common to both it and box.

This has the following results:

  • If box is entirely contained within this bounding box, this bounding box is resized to match the extents of box.
  • If the edges of this bounding box cross the edges of box, the extents of this bounding box are resized to include only the area common to both bounding boxes.
  • If this bounding box is entirely contained within the extents of box, this bounding box is not altered.
  • If this bounding box does not intersect box at all, the extents of this bounding box are set to invalid values and this method returns false.
    Returns
    true if the boxes intersect, or false if they do not. If this method returns false, the extents of this bounding box will be set to invalid values. You must call Set() before you can use the box again.
bool Kaim::Box2i::IsInside ( const Vec2i pos) const
inline

Returns true if the specified position is contained within the extents of the bounding box or if the specified position is located on the edge of the bounding box.

bool Kaim::Box2i::IsInsideStrictly ( const Vec2i pos) const
inline

Returns true if the specified position is contained entirely within the extents of the bounding box.

bool Kaim::Box2i::IsValid ( ) const
inline

Indicates whether or not the extents of the bounding box are valid.

void Kaim::Box2i::MakeZero ( )
inline

Sets both the minima and maxima of the bounding box to (0,0).

const Vec2i& Kaim::Box2i::Max ( ) const
inline

Retrieves the maxima of the bounding box.

const Vec2i& Kaim::Box2i::Min ( ) const
inline

Retrieves the minima of the bounding box.

Box2i& Kaim::Box2i::operator*= ( KyInt32  s)
inline

Multiplies both the minima and maxima of the box by the specified value.

Box2i& Kaim::Box2i::operator+= ( const Vec2i v)
inline

Adds the specified two-dimensional vector to both the minima and maxima of the box.

Box2i& Kaim::Box2i::operator-= ( const Vec2i v)
inline

Subtracts the specified two-dimensional vector from both the minima and maxima of the box.

Box2i& Kaim::Box2i::operator/= ( KyInt32  s)
inline

Divides both the minima and maxima of the box by the specified value.

bool Kaim::Box2i::operator== ( const Box2i other) const
inline

Check equality.

void Kaim::Box2i::Set ( const Vec2i min_,
const Vec2i max_ 
)
inline

Sets the extents of the bounding box to the specified values.

Parameters
min_The minima of the bounding box.
max_The maxima of the bounding box.
+ Examples:
bool Kaim::Box2i::SetAsIntersection ( const Box2i box_1,
const Box2i box_2 
)
inline

Resizes this bounding box to contain only the grid cells that are common to both box_1 and box_2.

This has the following results:

  • If one box is entirely contained within the other, this bounding box is set to match the extents of the smaller box.
  • If the edges of the two boxes cross, the extents of this bounding box are resized to include only the area common to both box_1 and box_2.
  • If box_1 and box_2 do not intersect at all, the extents of this bounding box are set to invalid values and this method returns false.
    Returns
    true if box_1 and box_2 intersect, or false if they do not. If this method returns false, the extents of this bounding box will be set to invalid values. You must call Set() before you can use the box again.
void Kaim::Box2i::SetMax ( const Vec2i max_)
inline

Sets the maxima of the bounding box to the specified coordinates.

void Kaim::Box2i::SetMin ( const Vec2i min_)
inline

Sets the minima of the bounding box to the specified coordinates.

void Kaim::Box2i::SetSafe ( const Vec2i min_,
const Vec2i max_ 
)
inline

Sets the extents of the bounding box to the specified values.

Parameters
min_The minima of the bounding box.
max_The maxima of the bounding box.

Friends And Related Function Documentation

void SwapEndianness ( Kaim::Endianness::Target  e,
Box2i self 
)
friend

Swaps the endianness of the data in the specified object.

This function allows this type of object to be serialized through the blob framework.

The first parameter specifies the target endianness, and the second parameter provides the object whose data is to be swapped.


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