00001
00015 #ifndef G3D_AABOX_H
00016 #define G3D_AABOX_H
00017
00018 #include "G3D/platform.h"
00019 #include "G3D/Vector3.h"
00020 #include "G3D/debug.h"
00021 #include "G3D/Array.h"
00022
00023 namespace G3D {
00024
00028 class AABox {
00029 private:
00030
00032 static int dummy;
00033
00034 Vector3 lo;
00035 Vector3 hi;
00036
00037 public:
00038
00040 inline AABox() {}
00041
00045 inline AABox(const Vector3& v) {
00046 lo = hi = v;
00047 }
00048
00053 inline AABox(const Vector3& low, const Vector3& high) {
00054 set(low, high);
00055 }
00056
00059 inline void set(const Vector3& low, const Vector3& high) {
00060 debugAssert(
00061 (low.x <= high.x) &&
00062 (low.y <= high.y) &&
00063 (low.z <= high.z));
00064 lo = low;
00065 hi = high;
00066 }
00067
00068 void serialize(class BinaryOutput& b) const;
00069
00070 void deserialize(class BinaryInput& b);
00071
00072 inline const Vector3& low() const {
00073 return lo;
00074 }
00075
00076 inline const Vector3& high() const {
00077 return hi;
00078 }
00079
00083 static inline const AABox& maxFinite() {
00084 static const AABox b = AABox(Vector3::minFinite(), Vector3::maxFinite());
00085 return b;
00086 }
00087
00088 static inline const AABox& inf() {
00089 static const AABox b = AABox(-Vector3::inf(), Vector3::inf());
00090 return b;
00091 }
00092
00093 static inline const AABox& zero() {
00094 static const AABox b = AABox(Vector3::zero(), Vector3::zero());
00095 return b;
00096 }
00097
00101 inline Vector3 center() const {
00102 return (lo + hi) * 0.5;
00103 }
00104
00108 inline double extent(int a) const {
00109 debugAssert(a < 3);
00110 return hi[a] - lo[a];
00111 }
00112
00113 inline Vector3 extent() const {
00114 return hi - lo;
00115 }
00116
00120 bool culledBy(
00121 const class Plane* plane,
00122 int numPlanes,
00123 int32& cullingPlaneIndex,
00124 const uint32 testMask,
00125 uint32& childMask) const;
00126
00130 bool culledBy(
00131 const class Plane* plane,
00132 int numPlanes,
00133 int32& cullingPlaneIndex = dummy,
00134 const uint32 testMask = 0xFFFFFF) const;
00135
00141 void split(const Vector3::Axis& axis, float location, AABox& low, AABox& high) const;
00142
00170 bool culledBy(
00171 const Array<Plane>& plane,
00172 int32& cullingPlaneIndex,
00173 const uint32 testMask,
00174 uint32& childMask) const;
00175
00179 bool culledBy(
00180 const Array<Plane>& plane,
00181 int32& cullingPlaneIndex = dummy,
00182 const uint32 testMask = -1) const;
00183
00184 inline bool contains(
00185 const Vector3& point) const {
00186 return
00187 (point.x >= lo.x) &&
00188 (point.y >= lo.y) &&
00189 (point.z >= lo.z) &&
00190 (point.x <= hi.x) &&
00191 (point.y <= hi.y) &&
00192 (point.z <= hi.z);
00193 }
00194
00196 inline float surfaceArea() const {
00197 Vector3 diag = hi - lo;
00198 return 2.0f * (diag.x * diag.y + diag.y * diag.z + diag.x * diag.z);
00199 }
00200
00201 inline float area() const {
00202 return surfaceArea();
00203 }
00204
00205 inline float volume() const {
00206 Vector3 diag = hi - lo;
00207 return diag.x * diag.y * diag.z;
00208 }
00209
00210 Vector3 randomInteriorPoint() const;
00211
00212 Vector3 randomSurfacePoint() const;
00213
00215 class Box toBox() const;
00216
00218 bool intersects(const AABox& other) const;
00219
00222 bool intersects(const class Sphere& other) const;
00223
00225 AABox intersect(const AABox& other) const {
00226 Vector3 H = hi.min(other.hi);
00227 Vector3 L = lo.max(other.lo).min(H);
00228 return AABox(L, H);
00229 }
00230
00231 inline unsigned int hashCode() const {
00232 return lo.hashCode() + hi.hashCode();
00233 }
00234
00235 inline bool operator==(const AABox& b) const {
00236 return (lo == b.lo) && (hi == b.hi);
00237 }
00238
00239 inline bool operator!=(const AABox& b) const {
00240 return !((lo == b.lo) && (hi == b.hi));
00241 }
00242
00243 void getBounds(AABox& out) const {
00244 out = *this;
00245 }
00246 };
00247
00248 }
00249
00253 inline unsigned int hashCode(const G3D::AABox& b) {
00254 return b.hashCode();
00255 }
00256
00257
00258 #endif