AxisAlignedBoundingBox Class Reference

#include <math.h>

Class Description

Represents a bounding box whose axes are aligned with the coordinate system.

The box is usually referred by two corners, the start corner means the one whose coordinates are the smallest, while the end corner means the opposite.

+ Examples:

Definition at line 838 of file math.h.

Public Member Functions

 AxisAlignedBoundingBox (void)
 Creates an empty bounding box. More...
 
 AxisAlignedBoundingBox (const Vector &vStart, const Vector &vEnd)
 Creates an object by specifying two corners of the box. More...
 
 AxisAlignedBoundingBox (const Vector &vCenter, float fSize)
 Creates an object by specifying the center of it and the distance of the sides from the center. More...
 
 AxisAlignedBoundingBox (const AxisAlignedBoundingBox &cA)
 Creates an object by cloning another one. More...
 
AxisAlignedBoundingBoxoperator= (const AxisAlignedBoundingBox &cBB)
 Assigns the value of a box object to another one. More...
 
AxisAlignedBoundingBoxoperator+= (const AxisAlignedBoundingBox &cBB)
 Extends the box to contain another one. More...
 
bool operator== (const AxisAlignedBoundingBox &cBB) const
 Returns true if two boxes are identical, false otherwise. More...
 
void Reset (void)
 Makes the box empty. More...
 
void Serialize (Stream &s)
 Writes/reads the object data to/from a stream. More...
 
bool operator!= (const AxisAlignedBoundingBox &cBox) const
 returns true if the two boxes are different, false otherwise. More...
 
void Extend (const Vector &cV) throw ()
 Extends the box to contain the passed point. More...
 
void Extend (const AxisAlignedBoundingBox &bb)
 Extends the box to contain another one. More...
 
void ExpandBy (float f) throw ()
 Expands the AABB by a specified amount in all directions. More...
 
void Transform (const class Matrix &mMatrix)
 Transforms the box with the passed-in matrix, and returns the smallest box which contains the result. More...
 
Vector operator[] (int iCornerIndex) const
 Returns the corners of the box. Valid indexes are 0-7. More...
 
float Size (void) const
 Returns the maximum edge length of the box. More...
 
float XSize (void) const
 Returns the length of the box along the X axis. More...
 
float YSize (void) const
 Returns the length of the box along the Y axis. More...
 
float ZSize (void) const
 Returns the length of the box along the Y axis. More...
 
float Volume (void) const
 Returns the volume of the box. More...
 
Vector Center (void) const
 Returns the center of the box. More...
 
bool IsPartOf (const AxisAlignedBoundingBox &cBB) const
 Returns true if the passed-in box contains this box. More...
 
bool IsTouching (const AxisAlignedBoundingBox &cBB) const
 Returns true if the two boxes intersect at all. More...
 
bool IsTouching (const Vector &vStart, const Vector &vEnd, float &fPlace) const
 Returns true if the box intersects any part of a specified line. More...
 
bool IsContaining (const Vector &cV) const
 Returns true if the box contains the specified point. More...
 
bool IsContaining (const AxisAlignedBoundingBox &b) const
 Returns true if the box contains the specified box. More...
 
AxisAlignedBoundingBox Intersection (const AxisAlignedBoundingBox &cOther) const
 Returns the intersection between this box and cOther. More...
 
bool IsEmpty (void) const
 Returns true if the box is empy. More...
 
AxisAlignedBoundingBox operator* (float fFactor)
 Multiplies the box coordinates with a scalar value. More...
 

Static Public Member Functions

static AxisAlignedBoundingBox SphereBoundingBox (const Vector &cCenter, float fRadius, const Matrix &mTransform)
 Returns the bounding box of the given sphere, in the given coordinate space. More...
 

Public Attributes

Vector m_vStart
 
Vector m_vEnd
 

Constructor & Destructor Documentation

Creates an empty bounding box.

Definition at line 842 of file math.h.

842 { Reset(); };
void Reset(void)
Makes the box empty.
Definition: math.h:885
AxisAlignedBoundingBox ( const Vector vStart,
const Vector vEnd 
)
inline

Creates an object by specifying two corners of the box.

Definition at line 845 of file math.h.

845  :
846  m_vStart( vStart ),
847  m_vEnd( vEnd ) {};
AxisAlignedBoundingBox ( const Vector vCenter,
float  fSize 
)
inline

Creates an object by specifying the center of it and the distance of the sides from the center.

This will be a cube.

Definition at line 851 of file math.h.

852  {
853  Vector v( fSize, fSize, fSize );
854  m_vStart = vCenter-v;
855  m_vEnd = vCenter+v;
856  };
const GLdouble * v
Definition: GLee.h:1174

Creates an object by cloning another one.

Definition at line 859 of file math.h.

859  :
860  m_vStart( cA.m_vStart ),
861  m_vEnd( cA.m_vEnd ) {};

Member Function Documentation

AxisAlignedBoundingBox& operator= ( const AxisAlignedBoundingBox cBB)
inline

Assigns the value of a box object to another one.

Definition at line 864 of file math.h.

865  {
866  m_vStart = cBB.m_vStart;
867  m_vEnd = cBB.m_vEnd;
868  return *this;
869  };
AxisAlignedBoundingBox& operator+= ( const AxisAlignedBoundingBox cBB)
inline

Extends the box to contain another one.

Definition at line 872 of file math.h.

873  {
874  for ( int i = 0; i < 8; i++ ) Extend( cBB[i] );
875  return *this;
876  };
void Extend(const Vector &cV)
Extends the box to contain the passed point.
Definition: math.h:901
bool operator== ( const AxisAlignedBoundingBox cBB) const
inline

Returns true if two boxes are identical, false otherwise.

Definition at line 879 of file math.h.

880  {
881  return m_vStart == cBB.m_vStart && m_vEnd == cBB.m_vEnd;
882  };
void Reset ( void  )
inline

Makes the box empty.

Definition at line 885 of file math.h.

886  {
887  m_vStart.Set( 0, 0, 0 );
888  m_vEnd.Set( -1, -1, -1 );
889  };
Vector & Set(float fX, float fY, float fZ)
Sets the values of the vector elements and returns the vector.
Definition: math.h:61
void Serialize ( Stream s)

Writes/reads the object data to/from a stream.

bool operator!= ( const AxisAlignedBoundingBox cBox) const
inline

returns true if the two boxes are different, false otherwise.

Definition at line 896 of file math.h.

897  { return !(operator ==(cBox)); };
bool operator==(const AxisAlignedBoundingBox &cBB) const
Returns true if two boxes are identical, false otherwise.
Definition: math.h:879
void Extend ( const Vector cV)
throw (
)
inline

Extends the box to contain the passed point.

Definition at line 901 of file math.h.

902  {
903  if ( IsEmpty() ) {
904  m_vStart = m_vEnd = cV;
905  } else {
906  if ( m_vStart.m_fX > cV.m_fX ) m_vStart.m_fX = cV.m_fX;
907  if ( m_vStart.m_fY > cV.m_fY ) m_vStart.m_fY = cV.m_fY;
908  if ( m_vStart.m_fZ > cV.m_fZ ) m_vStart.m_fZ = cV.m_fZ;
909  if ( m_vEnd.m_fX < cV.m_fX ) m_vEnd.m_fX = cV.m_fX;
910  if ( m_vEnd.m_fY < cV.m_fY ) m_vEnd.m_fY = cV.m_fY;
911  if ( m_vEnd.m_fZ < cV.m_fZ ) m_vEnd.m_fZ = cV.m_fZ;
912  }
913  }
float m_fZ
Definition: math.h:339
float m_fY
Definition: math.h:339
float m_fX
Definition: math.h:339
bool IsEmpty(void) const
Returns true if the box is empy.
Definition: math.h:1024
void Extend ( const AxisAlignedBoundingBox bb)
inline

Extends the box to contain another one.

Definition at line 916 of file math.h.

917  {
918  if ( !bb.IsEmpty() ) {
919  Extend( bb.m_vStart );
920  Extend( bb.m_vEnd );
921  };
922  };
void Extend(const Vector &cV)
Extends the box to contain the passed point.
Definition: math.h:901
void ExpandBy ( float  f)
throw (
)
inline

Expands the AABB by a specified amount in all directions.

A negative value will shrink the box by that amount.

Definition at line 926 of file math.h.

927  {
928  m_vStart -= f;
929  m_vEnd += f;
930  }
GLclampf f
Definition: GLee.h:9303
void Transform ( const class Matrix mMatrix)

Transforms the box with the passed-in matrix, and returns the smallest box which contains the result.

This is usually larger than the original box, because the transformation might rotate the box out of axis alignment.

Vector operator[] ( int  iCornerIndex) const

Returns the corners of the box. Valid indexes are 0-7.

float Size ( void  ) const
inline

Returns the maximum edge length of the box.

Definition at line 942 of file math.h.

942 { return Max( Max( XSize(), YSize() ), ZSize() ); };
float YSize(void) const
Returns the length of the box along the Y axis.
Definition: math.h:948
float XSize(void) const
Returns the length of the box along the X axis.
Definition: math.h:945
type Max(type a, type b)
Definition: mudbox.h:186
float ZSize(void) const
Returns the length of the box along the Y axis.
Definition: math.h:951
float XSize ( void  ) const
inline

Returns the length of the box along the X axis.

Definition at line 945 of file math.h.

float YSize ( void  ) const
inline

Returns the length of the box along the Y axis.

Definition at line 948 of file math.h.

float ZSize ( void  ) const
inline

Returns the length of the box along the Y axis.

Definition at line 951 of file math.h.

float Volume ( void  ) const
inline

Returns the volume of the box.

Definition at line 954 of file math.h.

954 { return XSize()*YSize()*ZSize(); };
float YSize(void) const
Returns the length of the box along the Y axis.
Definition: math.h:948
float XSize(void) const
Returns the length of the box along the X axis.
Definition: math.h:945
float ZSize(void) const
Returns the length of the box along the Y axis.
Definition: math.h:951
Vector Center ( void  ) const
inline

Returns the center of the box.

Definition at line 957 of file math.h.

957 { return (m_vStart+m_vEnd)*0.5f; };
bool IsPartOf ( const AxisAlignedBoundingBox cBB) const
inline

Returns true if the passed-in box contains this box.

Definition at line 960 of file math.h.

961  {
962  return (
963  m_vStart.m_fX >= cBB.m_vStart.m_fX && m_vEnd.m_fX <= cBB.m_vEnd.m_fX &&
964  m_vStart.m_fY >= cBB.m_vStart.m_fY && m_vEnd.m_fY <= cBB.m_vEnd.m_fY &&
965  m_vStart.m_fZ >= cBB.m_vStart.m_fZ && m_vEnd.m_fZ <= cBB.m_vEnd.m_fZ );
966  };
float m_fZ
Definition: math.h:339
float m_fY
Definition: math.h:339
float m_fX
Definition: math.h:339
bool IsTouching ( const AxisAlignedBoundingBox cBB) const
inline

Returns true if the two boxes intersect at all.

Definition at line 969 of file math.h.

970  {
971  return (
972  m_vStart.m_fX < cBB.m_vEnd.m_fX && m_vEnd.m_fX > cBB.m_vStart.m_fX &&
973  m_vStart.m_fY < cBB.m_vEnd.m_fY && m_vEnd.m_fY > cBB.m_vStart.m_fY &&
974  m_vStart.m_fZ < cBB.m_vEnd.m_fZ && m_vEnd.m_fZ > cBB.m_vStart.m_fZ );
975  };
float m_fZ
Definition: math.h:339
float m_fY
Definition: math.h:339
float m_fX
Definition: math.h:339
bool IsTouching ( const Vector vStart,
const Vector vEnd,
float &  fPlace 
) const

Returns true if the box intersects any part of a specified line.

If there is an intersection, then a point guaranteed to be inside the box is returned in the third argument (fPlace). That argument represents a location along the line from vStart to vEnd. (Location = vStart + fPlace*vEnd)

Parameters
[in]vStartA point (represented as a Vector) defining the start of the line
[in]vEndA point (represented as a Vector) defining the end of the line
[out]fPlaceA number between 0.0 and 1.0, defining a position along the line that is guaranteed to be inside the box (if true was returned).
bool IsContaining ( const Vector cV) const
inline

Returns true if the box contains the specified point.

Definition at line 992 of file math.h.

993  {
994  return (
995  m_vStart.m_fX <= cV.m_fX && m_vEnd.m_fX >= cV.m_fX &&
996  m_vStart.m_fY <= cV.m_fY && m_vEnd.m_fY >= cV.m_fY &&
997  m_vStart.m_fZ <= cV.m_fZ && m_vEnd.m_fZ >= cV.m_fZ );
998  };
float m_fZ
Definition: math.h:339
float m_fY
Definition: math.h:339
float m_fX
Definition: math.h:339
bool IsContaining ( const AxisAlignedBoundingBox b) const
inline

Returns true if the box contains the specified box.

Definition at line 1002 of file math.h.

1003  { return IsContaining( b.m_vStart ) && IsContaining( b.m_vEnd ); };
GLubyte GLubyte b
Definition: GLee.h:5404
bool IsContaining(const Vector &cV) const
Returns true if the box contains the specified point.
Definition: math.h:992
AxisAlignedBoundingBox Intersection ( const AxisAlignedBoundingBox cOther) const
inline

Returns the intersection between this box and cOther.

Returned bounding box will be empty if this box does not intersect cOther.

Definition at line 1007 of file math.h.

1008  {
1009  if( !IsTouching( cOther ) ) return AxisAlignedBoundingBox();
1010 
1011  return AxisAlignedBoundingBox(
1012  Vector(
1013  Max( cOther.m_vStart.x, m_vStart.x ),
1014  Max( cOther.m_vStart.y, m_vStart.y ),
1015  Max( cOther.m_vStart.z, m_vStart.z ) ) ,
1016  Vector(
1017  Min( cOther.m_vEnd.x, m_vEnd.x ),
1018  Min( cOther.m_vEnd.y, m_vEnd.y ),
1019  Min( cOther.m_vEnd.z, m_vEnd.z ) ) );
1020  }
float z
Definition: math.h:340
AxisAlignedBoundingBox(void)
Creates an empty bounding box.
Definition: math.h:842
type Min(type a, type b)
Definition: mudbox.h:184
bool IsTouching(const AxisAlignedBoundingBox &cBB) const
Returns true if the two boxes intersect at all.
Definition: math.h:969
float y
Definition: math.h:340
type Max(type a, type b)
Definition: mudbox.h:186
float x
Definition: math.h:340
bool IsEmpty ( void  ) const
inline

Returns true if the box is empy.

Note that when the start and end corners are equal then the box is not treated as empty.

Definition at line 1024 of file math.h.

1024 { return m_vStart.x > m_vEnd.x; };
float x
Definition: math.h:340
AxisAlignedBoundingBox operator* ( float  fFactor)
inline

Multiplies the box coordinates with a scalar value.

Definition at line 1027 of file math.h.

1028  {
1029  return AxisAlignedBoundingBox( m_vStart*fFactor, m_vEnd*fFactor );
1030  };
AxisAlignedBoundingBox(void)
Creates an empty bounding box.
Definition: math.h:842
static AxisAlignedBoundingBox SphereBoundingBox ( const Vector cCenter,
float  fRadius,
const Matrix mTransform 
)
static

Returns the bounding box of the given sphere, in the given coordinate space.

Member Data Documentation

Vector m_vStart

Definition at line 1035 of file math.h.

Vector m_vEnd

Definition at line 1036 of file math.h.


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