VRUT::AABB Class Reference

AABB class. More...

#include <bvolumes.h>

List of all members.

Public Member Functions

 AABB ()
 Class constructor.
 AABB (const AABB &aabb)
 Copy constructor.
 ~AABB ()
 Class destructor.
VECTOR3 Center () const
bool PointIsIn (const VECTOR3 &p) const
 Check if point is in AABB.
void ComputeNPvertices (const PLANE *plane, VECTOR3 *nvert, VECTOR3 *pvert) const
void ComputeNPvertices (const VECTOR3 *normal, VECTOR3 *nvert, VECTOR3 *pvert) const
float GetDistFromPointNormal (const VECTOR3 *pos, const VECTOR3 *dir) const
float GetSqrDistFromPointNormal (const VECTOR3 *pos, const VECTOR3 *dir) const
AABBoperator= (const AABB &aabb)
 Prevent copying NULL pointers - when debugging.
AABB operator+ (const AABB &aabb) const
 AABB union.
AABBMerge (const AABB &aabb)
 AABB union.
AABB Transform (const MATRIX *transMat) const
 Build box from AABB, transform it and get its new AABB.
bool Intersects (const Ray &r, float t0=0.0f, float t1=1e9f, float *dist=NULL) const
wxString ToString () const
 Get string with bounds.

Public Attributes

VECTOR3 MinBound
 Minimum point of AABB.
VECTOR3 MaxBound
 Maximum point of AABB.


Detailed Description

AABB class.

Definition at line 22 of file bvolumes.h.


Constructor & Destructor Documentation

VRUT::AABB::AABB (  )  [inline]

Class constructor.

Definition at line 32 of file bvolumes.h.

00032                      : MinBound(0,0,0), MaxBound(0,0,0)
00033               {
00034               }

VRUT::AABB::AABB ( const AABB aabb  )  [inline]

Copy constructor.

Definition at line 36 of file bvolumes.h.

00036 : MinBound(aabb.MinBound), MaxBound(aabb.MaxBound)      {}

VRUT::AABB::~AABB (  )  [inline]

Class destructor.

Definition at line 38 of file bvolumes.h.

00039               {
00040               }


Member Function Documentation

VECTOR3 VRUT::AABB::Center (  )  const [inline]

Definition at line 41 of file bvolumes.h.

00041 { return 0.5f*(MinBound + MaxBound); }

bool VRUT::AABB::PointIsIn ( const VECTOR3 p  )  const [inline]

Check if point is in AABB.

Definition at line 43 of file bvolumes.h.

00044               {
00045                      const float * pt(p._v);
00046                      return *pt >= MinBound.x && *pt++ <= MaxBound.x
00047                             && *pt >= MinBound.y && *pt++ <= MaxBound.y
00048                             && *pt >= MinBound.z && *pt <= MaxBound.z;
00049               }

void VRUT::AABB::ComputeNPvertices ( const PLANE plane,
VECTOR3 nvert,
VECTOR3 pvert 
) const [inline]

Get n-vertex (closest) and p-vertex (farthest) of AABB for given plane

Parameters:
[in] plane Given plane
[out] nvert Will be filled with n-vertex
[out] pvert Will be filled with p-vertex

Definition at line 55 of file bvolumes.h.

00056               {
00057                      VECTOR3 normal(plane->_p);
00058                      ComputeNPvertices(&normal, nvert, pvert);
00059               }

void VRUT::AABB::ComputeNPvertices ( const VECTOR3 normal,
VECTOR3 nvert,
VECTOR3 pvert 
) const [inline]

Get n-vertex (closest) and p-vertex (farthest) of AABB for given plane normal

Parameters:
[in] normal Given plane normal
[out] nvert Will be filled with n-vertex
[out] pvert Will be filled with p-vertex

Definition at line 65 of file bvolumes.h.

00066               {
00067                      for (int i = 0; i < 3; i++)
00068                      {
00069                             bool pos = normal->_v[i] >= 0.0f;
00070                             if (nvert)    nvert->_v[i] = ( pos ? MinBound[i] : MaxBound[i] );
00071                             if (pvert)    pvert->_v[i] = ( pos ? MaxBound[i] : MinBound[i] );
00072                      }
00073               }

float VRUT::AABB::GetDistFromPointNormal ( const VECTOR3 pos,
const VECTOR3 dir 
) const [inline]

Get distance of AABB from plane defined by point and normal

Parameters:
[in] pos Point on plane
[in] dir Plane normal
Returns:
Distance from plane

Definition at line 79 of file bvolumes.h.

00080               {
00081                      VECTOR3 nvert;
00082                      ComputeNPvertices(dir, &nvert, (VECTOR3 *)NULL);
00083                      return (nvert - *pos).Length();
00084               }

float VRUT::AABB::GetSqrDistFromPointNormal ( const VECTOR3 pos,
const VECTOR3 dir 
) const [inline]

Get square distance (faster than normal) of AABB from plane defined by point and normal

Parameters:
[in] pos Point on plane
[in] dir Plane normal
Returns:
Distance from plane

Definition at line 90 of file bvolumes.h.

00091               {
00092                      VECTOR3 nvert;
00093                      ComputeNPvertices(dir, &nvert, (VECTOR3 *)NULL);
00094                      return (nvert - *pos).LengthSq();
00095               }

AABB& VRUT::AABB::operator= ( const AABB aabb  )  [inline]

Prevent copying NULL pointers - when debugging.

Definition at line 98 of file bvolumes.h.

00099               {
00100                      MinBound = aabb.MinBound;
00101                      MaxBound = aabb.MaxBound;
00102                      return *this;
00103               }

AABB VRUT::AABB::operator+ ( const AABB aabb  )  const [inline]

AABB union.

Definition at line 106 of file bvolumes.h.

00107               {
00108                      AABB ret;
00109                      ret.MinBound = MinBound;
00110                      ret.MaxBound = MaxBound;
00111                      ret.Merge(aabb);
00112                      return ret;
00113               }

AABB& VRUT::AABB::Merge ( const AABB aabb  )  [inline]

AABB union.

Definition at line 116 of file bvolumes.h.

00117               {
00118                      float * minB(MinBound._v);
00119                      float * maxB(MaxBound._v);
00120                      const float * minB2(aabb.MinBound._v);
00121                      const float * maxB2(aabb.MaxBound._v);
00122 
00123                      for (unsigned i = 0; i < 3; i++)
00124                      {
00125                             if (minB2[i] < minB[i])
00126                                    minB[i] = minB2[i];
00127                             if (maxB2[i] > maxB[i])
00128                                    maxB[i] = maxB2[i];
00129                      }
00130                      return *this;
00131               }

AABB VRUT::AABB::Transform ( const MATRIX transMat  )  const [inline]

Build box from AABB, transform it and get its new AABB.

Get transformed box

Get AABB for transformed aabb

Definition at line 134 of file bvolumes.h.

00135               {
00137                      VECTOR3 transedAabb[8];
00138                      transedAabb[0] = MinBound;
00139                      transedAabb[1] = VECTOR3(MaxBound.x, MinBound.y, MinBound.z);
00140                      transedAabb[2] = VECTOR3(MinBound.x, MaxBound.y, MinBound.z);
00141                      transedAabb[3] = VECTOR3(MinBound.x, MinBound.y, MaxBound.z);
00142                      for (int i = 0; i < 4; i++)
00143                             transedAabb[i] = transMat->TransformCoord(transedAabb[i]);
00144 
00145                      VECTOR3 yaxis = transedAabb[2] - transedAabb[0];
00146                      transedAabb[4] = transedAabb[1] + yaxis;
00147                      transedAabb[5] = transedAabb[3] + yaxis;
00148                      transedAabb[6] = transedAabb[1] + transedAabb[3] - transedAabb[0];
00149                      transedAabb[7] = transedAabb[6] + yaxis;
00151                      AABB ret;
00152                      VECTOR3 * minB(&ret.MinBound);
00153                      VECTOR3 * maxB(&ret.MaxBound);
00154                      *minB = *maxB = transedAabb[0];
00155                      for (int i = 1; i < 8; i++)
00156                      {
00157                             const float * pos = transedAabb[i]._v;
00158                             minB->x = __min(minB->x, *pos);
00159                             maxB->x = __max(maxB->x, *pos); pos++;
00160                             minB->y = __min(minB->y, *pos);
00161                             maxB->y = __max(maxB->y, *pos); pos++;
00162                             minB->z = __min(minB->z, *pos);
00163                             maxB->z = __max(maxB->z, *pos);
00164                      }
00165                      return ret;
00166               }

bool VRUT::AABB::Intersects ( const Ray r,
float  t0 = 0.0f,
float  t1 = 1e9f,
float *  dist = NULL 
) const [inline]

Check if ray intersects AABB in given interval

Parameters:
[in] r Ray to be checked
[in] t0 Minimum distance from ray origin
[in] t1 Maximum distance from ray origin
[out] dist Distance from ray origin where ray touches AABB if intersected
Returns:
true if intersected

Definition at line 174 of file bvolumes.h.

00175               {
00176                      float tmin = -1e9f;
00177                      float tmax = 1e9f;
00178                      if (r.direction.x != 0.0f)
00179                      {
00180                             float invDirX = 1.0f/r.direction.x;
00181                             if (invDirX < 0.0f)
00182                             {
00183                                    tmin = (MaxBound.x - r.origin.x) * invDirX;
00184                                    tmax = (MinBound.x - r.origin.x) * invDirX;
00185                             }
00186                             else
00187                             {
00188                                    tmin = (MinBound.x - r.origin.x) * invDirX;
00189                                    tmax = (MaxBound.x - r.origin.x) * invDirX;
00190                             }
00191                      }
00192                      else if (r.origin.x < MinBound.x || r.origin.x > MaxBound.x)
00193                             return false;
00194 
00195                      if (r.direction.y != 0.0f)
00196                      {
00197                             float tymin, tymax;
00198                             float invDirY = 1.0f/r.direction.y;
00199                             if (invDirY < 0.0f)
00200                             {
00201                                    tymin = (MaxBound.y - r.origin.y) * invDirY;
00202                                    tymax = (MinBound.y - r.origin.y) * invDirY;
00203                             }
00204                             else
00205                             {
00206                                    tymin = (MinBound.y - r.origin.y) * invDirY;
00207                                    tymax = (MaxBound.y - r.origin.y) * invDirY;
00208                             }
00209                             if ( (tmin > tymax) || (tymin > tmax) )
00210                                    return false;
00211                             if (tymin > tmin)
00212                                    tmin = tymin;
00213                             if (tymax < tmax)
00214                                    tmax = tymax;
00215                      }
00216                      else if (r.origin.y < MinBound.y || r.origin.y > MaxBound.y)
00217                             return false;
00218 
00219                      if (r.direction.z != 0.0f)
00220                      {
00221                             float tzmin, tzmax;
00222                             float invDirZ = 1.0f/r.direction.z;
00223                             if (invDirZ < 0.0f)
00224                             {
00225                                    tzmin = (MaxBound.z - r.origin.z) * invDirZ;
00226                                    tzmax = (MinBound.z - r.origin.z) * invDirZ;
00227                             }
00228                             else
00229                             {
00230                                    tzmin = (MinBound.z - r.origin.z) * invDirZ;
00231                                    tzmax = (MaxBound.z - r.origin.z) * invDirZ;
00232                             }
00233                             if ( (tmin > tzmax) || (tzmin > tmax) )
00234                                    return false;
00235                             if (tzmin > tmin)
00236                                    tmin = tzmin;
00237                             if (tzmax < tmax)
00238                                    tmax = tzmax;
00239                      }
00240                      else if (r.origin.z < MinBound.z || r.origin.z > MaxBound.z)
00241                             return false;
00242 
00243                      bool ret = ( (tmin < t1) && (tmax > t0) );
00244                      if (ret && dist)
00245                             *dist = tmin;
00246 
00247                      return ret;
00248               }

wxString VRUT::AABB::ToString (  )  const [inline]

Get string with bounds.

Definition at line 251 of file bvolumes.h.

00252               {
00253                      return wxString::Format(wxT("MinBound: %s\nMaxBound: %s"), MinBound.ToString().c_str(), MaxBound.ToString().c_str());
00254               }


Member Data Documentation

Minimum point of AABB.

Definition at line 27 of file bvolumes.h.

Maximum point of AABB.

Definition at line 29 of file bvolumes.h.


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

Generated on Tue Mar 10 14:41:41 2009 for VRUT by  doxygen 1.5.5