| 1 | /** |
|---|
| 2 | @file AABox.h |
|---|
| 3 | |
|---|
| 4 | Axis-aligned box class |
|---|
| 5 | |
|---|
| 6 | @maintainer Morgan McGuire, matrix@graphics3d.com |
|---|
| 7 | |
|---|
| 8 | @created 2004-01-10 |
|---|
| 9 | @edited 2006-02-10 |
|---|
| 10 | |
|---|
| 11 | Copyright 2000-2006, Morgan McGuire. |
|---|
| 12 | All rights reserved. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | #ifndef G3D_AABOX_H |
|---|
| 16 | #define G3D_AABOX_H |
|---|
| 17 | |
|---|
| 18 | #include "G3D/platform.h" |
|---|
| 19 | #include "G3D/Vector3.h" |
|---|
| 20 | #include "G3D/debug.h" |
|---|
| 21 | #include "G3D/Array.h" |
|---|
| 22 | |
|---|
| 23 | namespace G3D { |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | An axis-aligned box. |
|---|
| 27 | */ |
|---|
| 28 | class AABox { |
|---|
| 29 | private: |
|---|
| 30 | |
|---|
| 31 | /** Optional argument placeholder */ |
|---|
| 32 | static int dummy; |
|---|
| 33 | |
|---|
| 34 | Vector3 lo; |
|---|
| 35 | Vector3 hi; |
|---|
| 36 | |
|---|
| 37 | public: |
|---|
| 38 | |
|---|
| 39 | /** Does not initialize the fields */ |
|---|
| 40 | inline AABox() {} |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | Constructs a zero-area AABox at v. |
|---|
| 44 | */ |
|---|
| 45 | inline AABox(const Vector3& v) { |
|---|
| 46 | lo = hi = v; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | /** Assumes that low is less than or equal to high along each dimension. |
|---|
| 50 | To have this automatically enforced, use |
|---|
| 51 | <code>AABox(low.min(high), low.max(high));</code> |
|---|
| 52 | */ |
|---|
| 53 | inline AABox(const Vector3& low, const Vector3& high) { |
|---|
| 54 | set(low, high); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | /** Assumes that low is less than or equal to high along each dimension. |
|---|
| 58 | */ |
|---|
| 59 | inline void set(const Vector3& low, const Vector3& high) { |
|---|
| 60 | debugAssert( |
|---|
| 61 | (low.x <= high.x) && |
|---|
| 62 | (low.y <= high.y) && |
|---|
| 63 | (low.z <= high.z)); |
|---|
| 64 | lo = low; |
|---|
| 65 | hi = high; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | inline const Vector3& low() const { |
|---|
| 70 | return lo; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | inline const Vector3& high() const { |
|---|
| 74 | return hi; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | The largest possible finite box. |
|---|
| 79 | */ |
|---|
| 80 | static inline const AABox& maxFinite() { |
|---|
| 81 | static const AABox b = AABox(Vector3::minFinite(), Vector3::maxFinite()); |
|---|
| 82 | return b; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | static inline const AABox& inf() { |
|---|
| 86 | static const AABox b = AABox(-Vector3::inf(), Vector3::inf()); |
|---|
| 87 | return b; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | static inline const AABox& zero() { |
|---|
| 91 | static const AABox b = AABox(Vector3::zero(), Vector3::zero()); |
|---|
| 92 | return b; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | Returns the centroid of the box. |
|---|
| 97 | */ |
|---|
| 98 | inline Vector3 center() const { |
|---|
| 99 | return (lo + hi) * 0.5; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | Distance from corner(0) to the next corner along axis a. |
|---|
| 104 | */ |
|---|
| 105 | inline double extent(int a) const { |
|---|
| 106 | debugAssert(a < 3); |
|---|
| 107 | return hi[a] - lo[a]; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | inline Vector3 extent() const { |
|---|
| 111 | return hi - lo; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | @deprecated Use culledBy(Array<Plane>&) |
|---|
| 116 | */ |
|---|
| 117 | bool culledBy( |
|---|
| 118 | const class Plane* plane, |
|---|
| 119 | int numPlanes, |
|---|
| 120 | int32& cullingPlaneIndex, |
|---|
| 121 | const uint32 testMask, |
|---|
| 122 | uint32& childMask) const; |
|---|
| 123 | |
|---|
| 124 | /** |
|---|
| 125 | @deprecated Use culledBy(Array<Plane>&) |
|---|
| 126 | */ |
|---|
| 127 | bool culledBy( |
|---|
| 128 | const class Plane* plane, |
|---|
| 129 | int numPlanes, |
|---|
| 130 | int32& cullingPlaneIndex = dummy, |
|---|
| 131 | const uint32 testMask = 0xFFFFFF) const; |
|---|
| 132 | |
|---|
| 133 | /** |
|---|
| 134 | Splits the box into two AABoxes along the specified axis. low contains |
|---|
| 135 | the part that was closer to negative infinity along axis, high contains |
|---|
| 136 | the other part. Either may have zero volume. |
|---|
| 137 | */ |
|---|
| 138 | void split(const Vector3::Axis& axis, float location, AABox& low, AABox& high) const; |
|---|
| 139 | |
|---|
| 140 | /** |
|---|
| 141 | Conservative culling test for up to 32 planes. |
|---|
| 142 | Returns true if there exists a <CODE>plane[p]</CODE> for |
|---|
| 143 | which the entire object is in the negative half space |
|---|
| 144 | (opposite the plane normal). |
|---|
| 145 | |
|---|
| 146 | <CODE>testMask</CODE> and <CODE>childMask</CODE> |
|---|
| 147 | are used for optimizing bounding volume hierarchies. |
|---|
| 148 | The version of this method that produces childMask |
|---|
| 149 | is slower than the version without; it should only |
|---|
| 150 | be used for parent nodes. |
|---|
| 151 | |
|---|
| 152 | @param cullingPlaneIndex The index of the first plane for which |
|---|
| 153 | the entire object is in the negative half-space. The function |
|---|
| 154 | exits early when one plane is found. -1 when the function |
|---|
| 155 | returns false (i.e. when no plane culls the whole object). |
|---|
| 156 | |
|---|
| 157 | @param testMask If bit <I>p</I> is 0, the |
|---|
| 158 | bounding volume automatically passes the culling test for |
|---|
| 159 | <CODE>plane[p]</CODE> (i.e. it is known that the volume |
|---|
| 160 | is entirely within the positive half space). The function |
|---|
| 161 | must return false if testMask is 0 and test all planes |
|---|
| 162 | when testMask is -1 (0xFFFFFFFF). |
|---|
| 163 | |
|---|
| 164 | @param childMask Test mask for the children of this volume. |
|---|
| 165 | |
|---|
| 166 | */ |
|---|
| 167 | bool culledBy( |
|---|
| 168 | const Array<Plane>& plane, |
|---|
| 169 | int32& cullingPlaneIndex, |
|---|
| 170 | const uint32 testMask, |
|---|
| 171 | uint32& childMask) const; |
|---|
| 172 | |
|---|
| 173 | /** |
|---|
| 174 | Conservative culling test that does not produce a mask for children. |
|---|
| 175 | */ |
|---|
| 176 | bool culledBy( |
|---|
| 177 | const Array<Plane>& plane, |
|---|
| 178 | int32& cullingPlaneIndex = dummy, |
|---|
| 179 | const uint32 testMask = -1) const; |
|---|
| 180 | |
|---|
| 181 | inline bool contains( |
|---|
| 182 | const Vector3& point) const { |
|---|
| 183 | return |
|---|
| 184 | (point.x >= lo.x) && |
|---|
| 185 | (point.y >= lo.y) && |
|---|
| 186 | (point.z >= lo.z) && |
|---|
| 187 | (point.x <= hi.x) && |
|---|
| 188 | (point.y <= hi.y) && |
|---|
| 189 | (point.z <= hi.z); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | /** @deprecated */ |
|---|
| 193 | inline float surfaceArea() const { |
|---|
| 194 | Vector3 diag = hi - lo; |
|---|
| 195 | return 2.0f * (diag.x * diag.y + diag.y * diag.z + diag.x * diag.z); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | inline float area() const { |
|---|
| 199 | return surfaceArea(); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | inline float volume() const { |
|---|
| 203 | Vector3 diag = hi - lo; |
|---|
| 204 | return diag.x * diag.y * diag.z; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | Vector3 randomInteriorPoint() const; |
|---|
| 208 | |
|---|
| 209 | Vector3 randomSurfacePoint() const; |
|---|
| 210 | |
|---|
| 211 | /** @deprecated use Box constructor */ |
|---|
| 212 | class Box toBox() const; |
|---|
| 213 | |
|---|
| 214 | /** Returns true if there is any overlap */ |
|---|
| 215 | bool intersects(const AABox& other) const; |
|---|
| 216 | |
|---|
| 217 | /** Returns true if there is any overlap. |
|---|
| 218 | @cite Jim Arvo's algorithm from Graphics Gems II*/ |
|---|
| 219 | bool intersects(const class Sphere& other) const; |
|---|
| 220 | |
|---|
| 221 | /** Return the intersection of the two boxes */ |
|---|
| 222 | AABox intersect(const AABox& other) const { |
|---|
| 223 | Vector3 H = hi.min(other.hi); |
|---|
| 224 | Vector3 L = lo.max(other.lo).min(H); |
|---|
| 225 | return AABox(L, H); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | inline unsigned int hashCode() const { |
|---|
| 229 | return lo.hashCode() + hi.hashCode(); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | inline bool operator==(const AABox& b) const { |
|---|
| 233 | return (lo == b.lo) && (hi == b.hi); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | inline bool operator!=(const AABox& b) const { |
|---|
| 237 | return !((lo == b.lo) && (hi == b.hi)); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | void getBounds(AABox& out) const { |
|---|
| 241 | out = *this; |
|---|
| 242 | } |
|---|
| 243 | }; |
|---|
| 244 | |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | /** |
|---|
| 248 | Hashing function for use with Table. |
|---|
| 249 | */ |
|---|
| 250 | inline unsigned int hashCode(const G3D::AABox& b) { |
|---|
| 251 | return b.hashCode(); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | #endif |
|---|