/******************************************************************/ /* BBox.h */ /* ----------------------- */ /* */ /* The file defines a 'bounding box' class. Essentially, this */ /* stores an axis-aligned bounding volume defined by two points */ /* */ /* Chris Wyman (10/26/2006) */ /******************************************************************/ #ifndef BBOX_H #define BBOX_H #include "DataTypes/Point.h" #include "Definitions.h" class BBox { private: Point pts[2]; public: // Default constructor, which defines an invalid bounding box (i.e., the max point is // less than the min point). The reason for this is to allow any point that is added // to the volume to become both the minimum and the maximum BBox() { pts[0] = Point(MAXFLOAT, MAXFLOAT, MAXFLOAT); pts[1] = Point(-MAXFLOAT,-MAXFLOAT,-MAXFLOAT); } // Constructor that creates a bounding volume from two points (the min and max values) BBox( const Point &minVal, const Point &maxVal ) { pts[0]=minVal; pts[1]=maxVal; } // Constructor that creates one bounding volume by copying another BBox( const BBox &bbox ) { pts[0] = bbox.pts[0]; pts[1] = bbox.pts[1]; } // Destructor ~BBox() {} // Accessor functions to access the min and max functions, as well as index into // the 2-point array. (This last case is useful for optimizations at times) inline const Point &GetMin( void ) const { return pts[0]; } inline const Point &GetMax( void ) const { return pts[1]; } inline const Point &GetPoint( int i ) const { return pts[i]; } // If you use a BBox inside a loop, instead of initializing a new BBox every // loop iteration consider using the same one and calling Reset() every iteration. // After Reset() the BBox is in exactly the same state as after the default constructor. inline void Reset( void ) { pts[0] = Point(MAXFLOAT, MAXFLOAT, MAXFLOAT); pts[1] = Point(-MAXFLOAT,-MAXFLOAT,-MAXFLOAT); } // Expand adds a point to the bounding volume. inline void Expand( const Point &pt ) { pts[0] = Point( MIN(pts[0].X(), pt.X()), MIN(pts[0].Y(), pt.Y()), MIN(pts[0].Z(), pt.Z()) ); pts[1] = Point( MAX(pts[1].X(), pt.X()), MAX(pts[1].Y(), pt.Y()), MAX(pts[1].Z(), pt.Z()) ); } // Expand can also take another bounding box inline void Expand( const BBox &bbox ) { pts[0] = Point( MIN(pts[0].X(), bbox.pts[0].X()), MIN(pts[0].Y(), bbox.pts[0].Y()), MIN(pts[0].Z(), bbox.pts[0].Z()) ); pts[1] = Point( MAX(pts[1].X(), bbox.pts[1].X()), MAX(pts[1].Y(), bbox.pts[1].Y()), MAX(pts[1].Z(), bbox.pts[1].Z()) ); } }; #endif