My Project
ShapeAABB.h
1 #pragma once
2 
3 
4 namespace ParaEngine
5 {
6  class CShapeBox;
7  class CShapeSphere;
8  class CShapeOBB;
9 
11  class CShapeAABB
12  {
13  public:
14  CShapeAABB() : mCenter(0.f, 0.f, 0.f), mExtents(-1e33f, -1e33f, -1e33f){};
15  CShapeAABB(const CShapeBox& box);
16  CShapeAABB(const Vector3& center, const Vector3& extents);
17  CShapeAABB(const CShapeAABB* boxes, int nSize);
18  CShapeAABB( const Vector3* points, int nSize);
19  ~CShapeAABB(void);
20 
26  void SetMinMax(const Vector3& min, const Vector3& max) { mCenter = (max + min)*0.5f; mExtents = (max - min)*0.5f; }
27 
33  void SetCenterExtents(const Vector3& c, const Vector3& e) { mCenter = c; mExtents = e; }
34 
38  void SetEmpty();
39 
41  void SetInvalid();
42 
46  void SetPointAABB(const Vector3& pt) { mCenter = pt; mExtents=Vector3(0,0,0);}
47 
52  float GetSize() const;
53 
54 
57  Vector3 Point(int i) const { return Vector3( (i&1)?(mCenter.x-mExtents.x):(mCenter.x+mExtents.x), (i&2)?(mCenter.y-mExtents.y):(mCenter.y+mExtents.y), (i&4)?(mCenter.z-mExtents.z):(mCenter.z+mExtents.z)); }
58 
63  void Extend(const Vector3& p);
64 
66  void Extend(const CShapeOBB& p);
67 
69  void ExtendByPointList( const Vector3* points, int nSize);
70 
72  void ExtendByAABBList( const CShapeAABB* boxes, int nSize );
73 
74  // Data access
75 
77  void GetMin(Vector3& min) const { min = mCenter - mExtents; }
79  void GetMax(Vector3& max) const { max = mCenter + mExtents; }
80 
82  Vector3 GetMin() const { return mCenter - mExtents; }
84  Vector3 GetMax() const { return mCenter + mExtents; }
85 
87  float GetMin(DWORD axis) const { return mCenter[axis] - mExtents[axis]; }
89  float GetMax(DWORD axis) const { return mCenter[axis] + mExtents[axis]; }
90 
92  void GetCenter(Vector3& center) const { center = mCenter; }
94  void GetExtents(Vector3& extents) const { extents = mExtents; }
95 
97  void SetCenter(const Vector3& center) { mCenter = center ; }
99  void SetExtents(const Vector3& extents) { mExtents = extents; }
100 
102  float GetCenter(DWORD axis) const { return mCenter[axis]; }
103  inline Vector3& GetCenter() { return mCenter; }
105  float GetExtents(DWORD axis) const { return mExtents[axis]; }
106  inline Vector3& GetExtents() { return mExtents; }
107 
109  void GetDiagonal(Vector3& diagonal) const { diagonal = mExtents * 2.0f; }
110  float GetWidth() const { return mExtents.x * 2.0f; }
111  float GetHeight() const { return mExtents.y * 2.0f; }
112  float GetDepth() const { return mExtents.z * 2.0f; }
113 
114  float GetRadius();
115 
116  void SetWidth(float fWidth);
117  void SetHeight(float fHeight);
118  void SetDepth(float fDepth);
119 
121  float GetVolume() const { return mExtents.x * mExtents.y * mExtents.z * 8.0f; }
122 
128  BOOL Intersect(const CShapeAABB& a) const;
129 
135  bool GomezIntersect(const CShapeAABB& a);
136 
143  BOOL Intersect(const CShapeAABB& a, DWORD axis) const;
144 
149  bool Intersect(float* hitDist, const Vector3* origPt, const Vector3* dir, int* pHitSide = NULL);
150 
155  bool IntersectOutside(float* hitDist, const Vector3* origPt, const Vector3* dir, int* pHitSide = NULL);
156 
162  void Rotate(const Matrix4& mtx, CShapeAABB& aabb) const;
163 
168  BOOL IsValid() const;
169 
171  CShapeAABB& operator*=(float s) { mExtents*=s; return *this; }
172 
174  CShapeAABB& operator/=(float s) { mExtents/=s; return *this; }
175 
178  {
179  mCenter+=trans;
180  return *this;
181  }
182  private:
183 
184  Vector3 mCenter;
185  Vector3 mExtents;
186 
187  };
188 
190  class CShapeBox
191  {
192  public:
193  Vector3 m_Max;
194  Vector3 m_Min;
195 
196  CShapeBox(): m_Min(1e33f, 1e33f, 1e33f), m_Max(-1e33f, -1e33f, -1e33f) { }
197  CShapeBox( const CShapeBox& other ): m_Min(other.m_Min), m_Max(other.m_Max) { }
198  CShapeBox( const Vector3& vMin, const Vector3& vMax): m_Min(vMin), m_Max(vMax) { }
199  CShapeBox(const CShapeBox* boxes, int nSize);
200  CShapeBox( const Vector3* points, int nSize);
201  CShapeBox(const CShapeAABB& other);
203  inline const Vector3& GetMin() const { return m_Min; }
205  inline const Vector3& GetMax() const { return m_Max; }
206 
212  inline void SetMinMax(const Vector3& min, const Vector3& max) { m_Min = min; m_Max = max;}
213 
219  void SetCenterExtents(const Vector3& c, const Vector3& e);
220 
224  void SetEmpty();
226  bool IsValid();
227 
231  void SetPointBox(const Vector3& pt) { m_Min = m_Max = pt;}
232 
233  float GetWidth() const { return m_Max.x - m_Min.x; }
234  float GetHeight() const { return m_Max.y - m_Min.y; }
235  float GetDepth() const { return m_Max.z - m_Min.z; }
236 
237  inline void GetCenter( Vector3* vec) const { *vec = 0.5f*(m_Min+m_Max); }
238 
239  inline Vector3 GetCenter() const { return 0.5f*(m_Min+m_Max); }
240  inline Vector3 GetExtent() const { return 0.5f*(m_Max-m_Min); }
241 
243  void Translate(const Vector3& offset);
244 
246  void Extend( const Vector3& vec );
247 
249  void Extend( const Vector3* points, int nSize);
250 
252  void Extend( const CShapeBox* boxes, int nSize );
253 
255  void Extend(const CShapeOBB& p);
256 
257  bool Intersect( float* hitDist, const Vector3* origPt, const Vector3* dir ) const;
258 
260  bool Intersect(const CShapeSphere& sphere) const;
261 
267  float DistanceToPoint(const Vector3& p, Vector3* where) const;
268 
269  Vector3 Point(int i) const { return Vector3( (i&1)?m_Min.x:m_Max.x, (i&2)?m_Min.y:m_Max.y, (i&4)?m_Min.z:m_Max.z ); }
270 
271  };
272 
273 }
274 
const Vector3 & GetMin() const
Get min point of the box.
Definition: ShapeAABB.h:203
bool GomezIntersect(const CShapeAABB &a)
The standard intersection method.
Definition: ShapeAABB.cpp:58
void Rotate(const Matrix4 &mtx, CShapeAABB &aabb) const
Recomputes the CShapeAABB after an arbitrary transform by a 4x4 matrix.
Definition: ShapeAABB.cpp:80
float GetCenter(DWORD axis) const
Get component of the box's center along a given axis.
Definition: ShapeAABB.h:102
float GetMax(DWORD axis) const
Get component of the box's max point along a given axis.
Definition: ShapeAABB.h:89
void ExtendByAABBList(const CShapeAABB *boxes, int nSize)
Extends the AABB.
Definition: ShapeAABB.cpp:120
void SetExtents(const Vector3 &extents)
Get box extents.
Definition: ShapeAABB.h:99
different physics engine has different winding order.
Definition: EventBinding.h:32
void SetMinMax(const Vector3 &min, const Vector3 &max)
Setups an AABB from min & max vectors.
Definition: ShapeAABB.h:26
void SetMinMax(const Vector3 &min, const Vector3 &max)
Setups a box from min & max vectors.
Definition: ShapeAABB.h:212
float GetVolume() const
Volume.
Definition: ShapeAABB.h:121
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
Vector3 GetMax() const
Get max point of the box.
Definition: ShapeAABB.h:84
void SetCenter(const Vector3 &center)
Get box center.
Definition: ShapeAABB.h:97
Definition: other.hpp:41
Vector3 GetMin() const
Get min point of the box.
Definition: ShapeAABB.h:82
void GetExtents(Vector3 &extents) const
Get box extents.
Definition: ShapeAABB.h:94
void GetDiagonal(Vector3 &diagonal) const
Get box diagonal.
Definition: ShapeAABB.h:109
void SetInvalid()
negative extents
Definition: ShapeAABB.cpp:115
Definition: ShapeSphere.h:6
AABB-related code.
Definition: ShapeAABB.h:11
float GetExtents(DWORD axis) const
Get component of the box's extents along a given axis.
Definition: ShapeAABB.h:105
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: ParaMatrix4.h:23
float GetSize() const
Gets the size of the AABB.
Definition: ShapeAABB.cpp:45
CShapeAABB & operator*=(float s)
Operator for CShapeAABB *= float. Scales the extents, keeps same center.
Definition: ShapeAABB.h:171
void GetMax(Vector3 &max) const
Get max point of the box.
Definition: ShapeAABB.h:79
void SetPointBox(const Vector3 &pt)
Setups a point box.
Definition: ShapeAABB.h:231
void GetMin(Vector3 &min) const
Get min point of the box.
Definition: ShapeAABB.h:77
BOOL IsValid() const
Checks the CShapeAABB is valid.
Definition: ShapeAABB.cpp:100
AABB with orientation by a matrix.
Definition: ShapeOBB.h:11
void ExtendByPointList(const Vector3 *points, int nSize)
Extends the AABB.
Definition: ShapeAABB.cpp:109
Vector3 Point(int i) const
get the i of the 8 corner points
Definition: ShapeAABB.h:57
void SetEmpty()
Setups an empty AABB.
Definition: ShapeAABB.cpp:74
void Extend(const Vector3 &p)
Extends the AABB.
Definition: ShapeAABB.cpp:132
void GetCenter(Vector3 &center) const
Get box center.
Definition: ShapeAABB.h:92
void SetPointAABB(const Vector3 &pt)
Setups a point AABB.
Definition: ShapeAABB.h:46
float GetMin(DWORD axis) const
Get component of the box's min point along a given axis.
Definition: ShapeAABB.h:87
void SetCenterExtents(const Vector3 &c, const Vector3 &e)
Setups an AABB from center & extents vectors.
Definition: ShapeAABB.h:33
CShapeAABB & operator/=(float s)
Operator for CShapeAABB /= float. Scales the extents, keeps same center.
Definition: ShapeAABB.h:174
CShapeAABB & operator+=(const Vector3 &trans)
Operator for CShapeAABB += Vector3. Translates the box.
Definition: ShapeAABB.h:177
bool IntersectOutside(float *hitDist, const Vector3 *origPt, const Vector3 *dir, int *pHitSide=NULL)
ray AABB intersection
Definition: ShapeAABB.cpp:176
a min max box.
Definition: ShapeAABB.h:190
const Vector3 & GetMax() const
Get max point of the box.
Definition: ShapeAABB.h:205
BOOL Intersect(const CShapeAABB &a) const
Computes the intersection between two AABBs.
Definition: ShapeAABB.cpp:50