TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
AABox.h
Go to the documentation of this file.
1 
15 #ifndef G3D_AABox_h
16 #define G3D_AABox_h
17 
18 #include "G3D/platform.h"
19 #include "G3D/debug.h"
20 #include "G3D/Array.h"
21 #include "G3D/Plane.h"
22 #include "G3D/Sphere.h"
23 #include "G3D/Vector3.h"
24 
25 namespace G3D {
26 
27 class Any;
28 
32 class AABox {
33 private:
34  friend class Intersect;
35 
37  static int dummy;
38 
41 
44 
45 public:
46 
48  AABox() : lo(fnan(), fnan(), fnan()), hi(fnan(), fnan(), fnan()) {}
49 
53  explicit AABox(const Point3& v) {
54  lo = hi = v;
55  }
56 
63  explicit AABox(const class Any& a);
64 
65  Any toAny() const;
66 
67  bool isEmpty() const {
68  return lo.isNaN();
69  }
70 
75  AABox(const Point3& low, const Point3& high) {
76  set(low, high);
77  }
78 
79  AABox operator*(float f) const {
80  if (f < 0) {
81  return AABox(hi * f, lo * f);
82  } else {
83  return AABox(lo * f, hi * f);
84  }
85  }
86 
87  AABox operator/(float f) const {
88  return *this * (1.0f / f);
89  }
90 
93  inline void set(const Point3& low, const Point3& high) {
95  (low.x <= high.x) &&
96  (low.y <= high.y) &&
97  (low.z <= high.z));
98  debugAssert(! low.isNaN() && ! high.isNaN());
99  lo = low;
100  hi = high;
101  }
102 
106  inline void merge(const AABox& a) {
107  if (isEmpty()) {
108  lo = a.lo;
109  hi = a.hi;
110  } else if (! a.isEmpty()) {
111  lo = lo.min(a.lo);
112  hi = hi.max(a.hi);
113  }
114  }
115 
116  inline void merge(const Point3& a) {
117  if (isEmpty()) {
118  lo = hi = a;
119  } else {
120  lo = lo.min(a);
121  hi = hi.max(a);
122  }
123  }
124 
125  void merge(const class Box& b);
126 
127  void serialize(class BinaryOutput& b) const;
128 
129  void deserialize(class BinaryInput& b);
130 
131  inline bool isFinite() const {
132  return isEmpty() || (lo.isFinite() && hi.isFinite());
133  }
134 
136  inline const Point3& low() const {
137  return lo;
138  }
139 
141  inline const Point3& high() const {
142  return hi;
143  }
144 
148  static const AABox& maxFinite();
149 
152  static const AABox& large();
153 
154  static const AABox& inf();
155 
156  static const AABox& zero();
157 
158  static const AABox& empty();
159 
163  inline Point3 center() const {
164  return (lo + hi) * 0.5;
165  }
166 
167  Point3 corner(int index) const;
168 
172  inline float extent(int a) const {
173  if (isEmpty()) {
174  return 0.0f;
175  }
176  debugAssert(a < 3);
177  return hi[a] - lo[a];
178  }
179 
180 
181  inline Vector3 extent() const {
182  if (isEmpty()) {
183  return Vector3::zero();
184  }
185  return hi - lo;
186  }
187 
188 
194  void split(const Vector3::Axis& axis, float location, AABox& low, AABox& high) const;
195 
223  bool culledBy
224  (const Array<Plane>& plane,
225  int32& cullingPlaneIndex,
226  const uint32 testMask,
227  uint32& childMask) const;
228 
232  bool culledBy
233  (const Array<Plane>& plane,
234  int32& cullingPlaneIndex = dummy,
235  const uint32 testMask = 0xFFFFFFFF) const;
236 
238  inline bool contains(const AABox& other) const {
239  return
240  (other.hi.x <= hi.x) &&
241  (other.hi.y <= hi.y) &&
242  (other.hi.z <= hi.z) &&
243  (other.lo.x >= lo.x) &&
244  (other.lo.y >= lo.y) &&
245  (other.lo.z >= lo.z);
246  }
247 
248  inline bool contains(const Point3& point) const {
249  return
250  (point.x >= lo.x) &&
251  (point.y >= lo.y) &&
252  (point.z >= lo.z) &&
253  (point.x <= hi.x) &&
254  (point.y <= hi.y) &&
255  (point.z <= hi.z);
256  }
257 
258  inline float area() const {
259  if (isEmpty()) { return 0; }
260  Vector3 diag = hi - lo;
261  return 2.0f * (diag.x * diag.y + diag.y * diag.z + diag.x * diag.z);
262  }
263 
264  inline float volume() const {
265  if (isEmpty()) { return 0; }
266  Vector3 diag = hi - lo;
267  return diag.x * diag.y * diag.z;
268  }
269 
270  Point3 randomInteriorPoint() const;
271 
272  Point3 randomSurfacePoint() const;
273 
275  bool intersects(const AABox& other) const;
276 
279  bool intersects(const Sphere& other) const;
280 
282  AABox intersect(const AABox& other) const {
283  if (isEmpty() || other.isEmpty()) {
284  return empty();
285  }
286 
287  const Point3& H = hi.min(other.hi);
288  const Point3& L = lo.max(other.lo).min(H);
289 
290  if (H.x < L.x && H.y < L.y && H.z < L.z) {
291  return empty();
292  } else {
293  return AABox(L, H);
294  }
295  }
296 
297  inline size_t hashCode() const {
298  return lo.hashCode() + hi.hashCode();
299  }
300 
301  inline bool operator==(const AABox& b) const {
302  if (isEmpty() && b.isEmpty()) {
303  return true;
304  } else {
305  return (lo == b.lo) && (hi == b.hi);
306  }
307  }
308 
309  inline bool operator!=(const AABox& b) const {
310  if (isEmpty()) {
311  return b.isEmpty();
312  } else {
313  return !((lo == b.lo) && (hi == b.hi));
314  }
315  }
316 
317  inline AABox operator+(const Vector3& v) const {
318  AABox out;
319  out.lo = lo + v;
320  out.hi = hi + v;
321  return out;
322  }
323 
324  inline AABox operator-(const Vector3& v) const {
325  AABox out;
326  out.lo = lo - v;
327  out.hi = hi - v;
328  return out;
329  }
330 
331  void getBounds(AABox& out) const {
332  out = *this;
333  }
334 
335  void getBounds(Sphere& out) const;
336 };
337 
338 }
339 
340 template <> struct HashTrait<G3D::AABox> {
341  static size_t hashCode(const G3D::AABox& key) { return key.hashCode(); }
342 };
343 
344 
345 
346 #endif
bool intersects(const AABox &other) const
Definition: AABox.cpp:175
void deserialize(class BinaryInput &b)
Definition: AABox.cpp:99
float x
Definition: Vector3.h:62
void merge(const AABox &a)
Definition: AABox.h:106
float fnan()
Definition: g3dmath.cpp:82
AABox operator*(float f) const
Definition: AABox.h:79
size_t hashCode() const
Definition: AABox.h:297
AABox intersect(const AABox &other) const
Definition: AABox.h:282
AABox(const Point3 &v)
Definition: AABox.h:53
Definition: BinaryInput.h:69
bool contains(const AABox &other) const
Definition: AABox.h:238
Vector3 __fastcall max(const Vector3 &v) const
Definition: Vector3.h:794
bool isFinite() const
Definition: AABox.h:131
Definition: Intersect.h:27
float area() const
Definition: AABox.h:258
Definition: HashTrait.h:105
Point3 hi
Definition: AABox.h:43
Definition: AABox.h:25
Dynamic 1D array tuned for performance.
Definition: Array.h:95
AABox(const Point3 &low, const Point3 &high)
Definition: AABox.h:75
static const AABox & maxFinite()
Definition: AABox.cpp:67
const Point3 & low() const
Definition: AABox.h:136
static const AABox & inf()
Definition: AABox.cpp:81
Axis
Definition: Vector3.h:122
Any toAny() const
Definition: AABox.cpp:44
Vector3 __fastcall min(const Vector3 &v) const
Definition: Vector3.h:789
An arbitrary (oriented) 3D box, useful as a bounding box.
Definition: Box.h:35
float y
Definition: Vector3.h:62
void set(const Point3 &low, const Point3 &high)
Definition: AABox.h:93
Definition: Vector3.h:58
Point3 randomSurfacePoint() const
Definition: AABox.cpp:129
bool operator!=(const AABox &b) const
Definition: AABox.h:309
static const Vector3 & zero()
Definition: Vector3.cpp:119
#define H(x, y, z)
Definition: Sphere.h:24
Easy loading and saving of human-readable configuration files.
Definition: Any.h:184
static const AABox & empty()
Definition: AABox.cpp:61
size_t hashCode() const
Definition: Vector3.cpp:155
bool culledBy(const Array< Plane > &plane, int32 &cullingPlaneIndex, const uint32 testMask, uint32 &childMask) const
AABox operator/(float f) const
Definition: AABox.h:87
Vector3 extent() const
Definition: AABox.h:181
#define debugAssert(exp)
Definition: debugAssert.h:160
void getBounds(AABox &out) const
Definition: AABox.h:331
Point3 center() const
Definition: AABox.h:163
void serialize(class BinaryOutput &b) const
Definition: AABox.cpp:93
Point3 randomInteriorPoint() const
Definition: AABox.cpp:167
Definition: AABox.h:32
bool isFinite() const
Definition: Vector3.h:652
static const AABox & zero()
Definition: AABox.cpp:87
static int dummy
Definition: AABox.h:37
static const AABox & large()
Definition: AABox.cpp:74
float extent(int a) const
Definition: AABox.h:172
float z
Definition: Vector3.h:62
AABox operator-(const Vector3 &v) const
Definition: AABox.h:324
AABox operator+(const Vector3 &v) const
Definition: AABox.h:317
Definition: BinaryOutput.h:52
AABox()
Definition: AABox.h:48
int32_t int32
Definition: g3dmath.h:167
bool contains(const Point3 &point) const
Definition: AABox.h:248
void split(const Vector3::Axis &axis, float location, AABox &low, AABox &high) const
Definition: AABox.cpp:112
static size_t hashCode(const G3D::AABox &key)
Definition: AABox.h:341
uint32_t uint32
Definition: g3dmath.h:168
const Point3 & high() const
Definition: AABox.h:141
bool operator==(const AABox &b) const
Definition: AABox.h:301
void merge(const Point3 &a)
Definition: AABox.h:116
bool isNaN() const
Definition: Vector3.cpp:83
Point3 lo
Definition: AABox.h:40
bool isEmpty() const
Definition: AABox.h:67
float volume() const
Definition: AABox.h:264
Point3 corner(int index) const
Definition: AABox.cpp:361