My Project
ParaAxisAlignedBox.h
1 #pragma once
2 #include "ParaVector3.h"
3 #include "ParaMatrix4.h"
4 
5 namespace ParaEngine {
6 
17  {
18  public:
19  enum Extent
20  {
21  EXTENT_NULL,
22  EXTENT_FINITE,
23  EXTENT_INFINITE
24  };
25  protected:
26 
27  Vector3 mMinimum;
28  Vector3 mMaximum;
29  Extent mExtent;
30  mutable Vector3* mpCorners;
31 
32  public:
33  /*
34  1-----2
35  /| /|
36  / | / |
37  5-----4 |
38  | 0--|--3
39  | / | /
40  |/ |/
41  6-----7
42  */
43  typedef enum {
44  FAR_LEFT_BOTTOM = 0,
45  FAR_LEFT_TOP = 1,
46  FAR_RIGHT_TOP = 2,
47  FAR_RIGHT_BOTTOM = 3,
48  NEAR_RIGHT_BOTTOM = 7,
49  NEAR_LEFT_BOTTOM = 6,
50  NEAR_LEFT_TOP = 5,
51  NEAR_RIGHT_TOP = 4
52  } CornerEnum;
53  inline AxisAlignedBox() : mpCorners(0)
54  {
55  // Default to a null box
56  setMinimum( -0.5, -0.5, -0.5 );
57  setMaximum( 0.5, 0.5, 0.5 );
58  mExtent = EXTENT_NULL;
59  }
60  inline AxisAlignedBox(Extent e) : mpCorners(0)
61  {
62  setMinimum( -0.5, -0.5, -0.5 );
63  setMaximum( 0.5, 0.5, 0.5 );
64  mExtent = e;
65  }
66 
67  inline AxisAlignedBox(const AxisAlignedBox & rkBox) : mpCorners(0)
68  {
69  if (rkBox.isNull())
70  setNull();
71  else if (rkBox.isInfinite())
72  setInfinite();
73  else
74  setExtents( rkBox.mMinimum, rkBox.mMaximum );
75  }
76 
77  inline AxisAlignedBox( const Vector3& min, const Vector3& max ) : mpCorners(0)
78  {
79  setExtents( min, max );
80  }
81 
82  inline AxisAlignedBox(
83  float mx, float my, float mz,
84  float Mx, float My, float Mz ) : mpCorners(0)
85  {
86  setExtents( mx, my, mz, Mx, My, Mz );
87  }
88 
89  AxisAlignedBox& operator=(const AxisAlignedBox& rhs)
90  {
91  // Specifically override to avoid copying mpCorners
92  if (rhs.isNull())
93  setNull();
94  else if (rhs.isInfinite())
95  setInfinite();
96  else
97  setExtents(rhs.mMinimum, rhs.mMaximum);
98 
99  return *this;
100  }
101 
102  ~AxisAlignedBox()
103  {
104  if (mpCorners)
105  delete [](mpCorners);
106  }
107 
108 
111  inline const Vector3& getMinimum(void) const
112  {
113  return mMinimum;
114  }
115 
119  inline Vector3& getMinimum(void)
120  {
121  return mMinimum;
122  }
123 
126  inline const Vector3& getMaximum(void) const
127  {
128  return mMaximum;
129  }
130 
134  inline Vector3& getMaximum(void)
135  {
136  return mMaximum;
137  }
138 
139 
142  inline void setMinimum( const Vector3& vec )
143  {
144  mExtent = EXTENT_FINITE;
145  mMinimum = vec;
146  }
147 
148  inline void setMinimum( float x, float y, float z )
149  {
150  mExtent = EXTENT_FINITE;
151  mMinimum.x = x;
152  mMinimum.y = y;
153  mMinimum.z = z;
154  }
155 
159  void setMinimumX(float x);
160 
161  void setMinimumY(float y);
162 
163  void setMinimumZ(float z);
164 
167  inline void setMaximum( const Vector3& vec )
168  {
169  mExtent = EXTENT_FINITE;
170  mMaximum = vec;
171  }
172 
173  inline void setMaximum( float x, float y, float z )
174  {
175  mExtent = EXTENT_FINITE;
176  mMaximum.x = x;
177  mMaximum.y = y;
178  mMaximum.z = z;
179  }
180 
184  void setMaximumX( float x );
185 
186  void setMaximumY( float y );
187 
188  void setMaximumZ( float z );
189 
192  void setExtents( const Vector3& min, const Vector3& max );
193 
194  void setExtents(
195  float mx, float my, float mz,
196  float Mx, float My, float Mz );
197 
222  const Vector3* getAllCorners(void) const;
223 
226  Vector3 getCorner(CornerEnum cornerToGet) const;
227 
228  friend std::ostream& operator<<( std::ostream& o, const AxisAlignedBox aab )
229  {
230  switch (aab.mExtent)
231  {
232  case EXTENT_NULL:
233  o << "AxisAlignedBox(null)";
234  return o;
235 
236  case EXTENT_FINITE:
237  o << "AxisAlignedBox(min=" << aab.mMinimum << ", max=" << aab.mMaximum << ")";
238  return o;
239 
240  case EXTENT_INFINITE:
241  o << "AxisAlignedBox(infinite)";
242  return o;
243 
244  default: // shut up compiler
245  PE_ASSERT( false && "Never reached" );
246  return o;
247  }
248  }
249 
253  void merge( const AxisAlignedBox& rhs );
254 
257  void merge( const Vector3& point );
258 
268  void transform( const Matrix4& matrix );
269 
281  void transformAffine(const Matrix4& m);
282 
285  void setNull();
286 
289  bool isNull(void) const;
290 
293  bool isFinite(void) const;
294 
297  void setInfinite();
298 
301  bool isInfinite(void) const;
302 
304  bool intersects(const AxisAlignedBox& b2) const;
305 
307  AxisAlignedBox intersection(const AxisAlignedBox& b2) const;
308 
310  float volume(void) const;
311 
313  void scale(const Vector3& s);
314 
316  bool intersects(const Sphere& s) const;
318  bool intersects(const Plane& p) const;
320  bool intersects(const Vector3& v) const;
322  Vector3 getCenter(void) const;
324  Vector3 getSize(void) const;
326  Vector3 getHalfSize(void) const;
327 
330  bool contains(const Vector3& v) const;
331 
334  bool contains(const AxisAlignedBox& other) const;
335 
338  bool operator== (const AxisAlignedBox& rhs) const;
339 
342  bool operator!= (const AxisAlignedBox& rhs) const;
343 
344  // special values
345  static const AxisAlignedBox BOX_NULL;
346  static const AxisAlignedBox BOX_INFINITE;
347 
348 
349  };
350 
351 } // namespace ParaEngine
352 
Vector3 & getMinimum(void)
Gets a modifiable version of the minimum corner of the box.
Definition: ParaAxisAlignedBox.h:119
bool operator==(const AxisAlignedBox &rhs) const
Tests 2 boxes for equality.
Definition: ParaAxisAlignedBox.cpp:21
void merge(const AxisAlignedBox &rhs)
Merges the passed in box into the current box.
Definition: ParaAxisAlignedBox.cpp:363
float volume(void) const
Calculate the volume of this box.
Definition: ParaAxisAlignedBox.cpp:188
bool isInfinite(void) const
Returns true if the box is infinite.
Definition: ParaAxisAlignedBox.cpp:242
different physics engine has different winding order.
Definition: EventBinding.h:32
A sphere primitive, mostly used for bounds checking.
Definition: ParaSphere.h:46
void setExtents(const Vector3 &min, const Vector3 &max)
Sets both minimum and maximum extents at once.
Definition: ParaAxisAlignedBox.cpp:459
Vector3 getCenter(void) const
Gets the centre of the box.
Definition: ParaAxisAlignedBox.cpp:105
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
Vector3 & getMaximum(void)
Gets a modifiable version of the maximum corner of the box.
Definition: ParaAxisAlignedBox.h:134
Definition: other.hpp:41
const Vector3 * getAllCorners(void) const
Returns a pointer to an array of 8 corner points, useful for collision vs.
Definition: ParaAxisAlignedBox.cpp:417
void transformAffine(const Matrix4 &m)
Transforms the box according to the affine matrix supplied.
Definition: ParaAxisAlignedBox.cpp:267
Vector3 getCorner(CornerEnum cornerToGet) const
gets the position of one of the corners
Definition: ParaAxisAlignedBox.cpp:392
bool intersects(const AxisAlignedBox &b2) const
Returns whether or not this box intersects another.
Definition: ParaAxisAlignedBox.cpp:146
void setMaximumX(float x)
Changes one of the components of the maximum corner of the box used to resize only one dimension of t...
Definition: ParaAxisAlignedBox.cpp:479
bool contains(const Vector3 &v) const
Tests whether the given point contained by this box.
Definition: ParaAxisAlignedBox.cpp:49
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: ParaMatrix4.h:23
Defines a plane in 3D space.
Definition: ParaPlane.h:23
void setMaximum(const Vector3 &vec)
Sets the maximum corner of the box.
Definition: ParaAxisAlignedBox.h:167
AxisAlignedBox intersection(const AxisAlignedBox &b2) const
Calculate the area of intersection of this box and another.
Definition: ParaAxisAlignedBox.cpp:210
void scale(const Vector3 &s)
Scales the AABB by the vector given.
Definition: ParaAxisAlignedBox.cpp:176
bool isNull(void) const
Returns true if the box is null i.e.
Definition: ParaAxisAlignedBox.cpp:257
void setNull()
Sets the box to a &#39;null&#39; value i.e.
Definition: ParaAxisAlignedBox.cpp:262
const Vector3 & getMaximum(void) const
Gets the maximum corner of the box.
Definition: ParaAxisAlignedBox.h:126
void setMinimum(const Vector3 &vec)
Sets the minimum corner of the box.
Definition: ParaAxisAlignedBox.h:142
void transform(const Matrix4 &matrix)
Transforms the box according to the matrix supplied.
Definition: ParaAxisAlignedBox.cpp:287
bool operator!=(const AxisAlignedBox &rhs) const
Tests 2 boxes for inequality.
Definition: ParaAxisAlignedBox.cpp:16
Vector3 getSize(void) const
Gets the size of the box.
Definition: ParaAxisAlignedBox.cpp:83
A 3D box aligned with the x/y/z axes.
Definition: ParaAxisAlignedBox.h:16
bool isFinite(void) const
Returns true if the box is finite.
Definition: ParaAxisAlignedBox.cpp:252
Vector3 getHalfSize(void) const
Gets the half-size of the box.
Definition: ParaAxisAlignedBox.cpp:61
const Vector3 & getMinimum(void) const
Gets the minimum corner of the box.
Definition: ParaAxisAlignedBox.h:111
void setMinimumX(float x)
Changes one of the components of the minimum corner of the box used to resize only one dimension of t...
Definition: ParaAxisAlignedBox.cpp:494
void setInfinite()
Sets the box to &#39;infinite&#39;.
Definition: ParaAxisAlignedBox.cpp:247