[1] | 1 | /* |
---|
| 2 | ----------------------------------------------------------------------------- |
---|
| 3 | This source file is part of OGRE |
---|
| 4 | (Object-oriented Graphics Rendering Engine) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
| 8 | Also see acknowledgements in Readme.html |
---|
| 9 | |
---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
| 13 | version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
| 18 | |
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
| 23 | |
---|
| 24 | You may alternatively use this source under the terms of a specific version of |
---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
| 26 | Torus Knot Software Ltd. |
---|
| 27 | ----------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | #ifndef __AxisAlignedBox_H_ |
---|
| 30 | #define __AxisAlignedBox_H_ |
---|
| 31 | |
---|
| 32 | // Precompiler options |
---|
| 33 | #include "OgrePrerequisites.h" |
---|
| 34 | |
---|
| 35 | #include "OgreVector3.h" |
---|
| 36 | #include "OgreMatrix4.h" |
---|
| 37 | |
---|
| 38 | namespace Ogre { |
---|
| 39 | |
---|
| 40 | /** A 3D box aligned with the x/y/z axes. |
---|
| 41 | @remarks |
---|
| 42 | This class represents a simple box which is aligned with the |
---|
| 43 | axes. Internally it only stores 2 points as the extremeties of |
---|
| 44 | the box, one which is the minima of all 3 axes, and the other |
---|
| 45 | which is the maxima of all 3 axes. This class is typically used |
---|
| 46 | for an axis-aligned bounding box (AABB) for collision and |
---|
| 47 | visibility determination. |
---|
| 48 | */ |
---|
| 49 | class _OgreExport AxisAlignedBox |
---|
| 50 | { |
---|
| 51 | protected: |
---|
| 52 | enum Extent |
---|
| 53 | { |
---|
| 54 | EXTENT_NULL, |
---|
| 55 | EXTENT_FINITE, |
---|
| 56 | EXTENT_INFINITE |
---|
| 57 | }; |
---|
| 58 | |
---|
| 59 | Vector3 mMinimum; |
---|
| 60 | Vector3 mMaximum; |
---|
| 61 | Extent mExtent; |
---|
| 62 | mutable Vector3* mpCorners; |
---|
| 63 | |
---|
| 64 | public: |
---|
| 65 | /* |
---|
| 66 | 1-----2 |
---|
| 67 | /| /| |
---|
| 68 | / | / | |
---|
| 69 | 5-----4 | |
---|
| 70 | | 0--|--3 |
---|
| 71 | | / | / |
---|
| 72 | |/ |/ |
---|
| 73 | 6-----7 |
---|
| 74 | */ |
---|
| 75 | typedef enum { |
---|
| 76 | FAR_LEFT_BOTTOM = 0, |
---|
| 77 | FAR_LEFT_TOP = 1, |
---|
| 78 | FAR_RIGHT_TOP = 2, |
---|
| 79 | FAR_RIGHT_BOTTOM = 3, |
---|
| 80 | NEAR_RIGHT_BOTTOM = 7, |
---|
| 81 | NEAR_LEFT_BOTTOM = 6, |
---|
| 82 | NEAR_LEFT_TOP = 5, |
---|
| 83 | NEAR_RIGHT_TOP = 4 |
---|
| 84 | } CornerEnum; |
---|
| 85 | inline AxisAlignedBox() : mpCorners(0) |
---|
| 86 | { |
---|
| 87 | // Default to a null box |
---|
| 88 | setMinimum( -0.5, -0.5, -0.5 ); |
---|
| 89 | setMaximum( 0.5, 0.5, 0.5 ); |
---|
| 90 | mExtent = EXTENT_NULL; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | inline AxisAlignedBox(const AxisAlignedBox & rkBox) : mpCorners(0) |
---|
| 94 | { |
---|
| 95 | if (rkBox.isNull()) |
---|
| 96 | setNull(); |
---|
| 97 | else if (rkBox.isInfinite()) |
---|
| 98 | setInfinite(); |
---|
| 99 | else |
---|
| 100 | setExtents( rkBox.mMinimum, rkBox.mMaximum ); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | inline AxisAlignedBox( const Vector3& min, const Vector3& max ) : mpCorners(0) |
---|
| 104 | { |
---|
| 105 | setExtents( min, max ); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | inline AxisAlignedBox( |
---|
| 109 | Real mx, Real my, Real mz, |
---|
| 110 | Real Mx, Real My, Real Mz ) : mpCorners(0) |
---|
| 111 | { |
---|
| 112 | setExtents( mx, my, mz, Mx, My, Mz ); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | AxisAlignedBox& operator=(const AxisAlignedBox& rhs) |
---|
| 116 | { |
---|
| 117 | // Specifically override to avoid copying mpCorners |
---|
| 118 | if (rhs.isNull()) |
---|
| 119 | setNull(); |
---|
| 120 | else if (rhs.isInfinite()) |
---|
| 121 | setInfinite(); |
---|
| 122 | else |
---|
| 123 | setExtents(rhs.mMinimum, rhs.mMaximum); |
---|
| 124 | |
---|
| 125 | return *this; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | ~AxisAlignedBox() |
---|
| 129 | { |
---|
| 130 | if (mpCorners) |
---|
| 131 | delete [] mpCorners; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | /** Gets the minimum corner of the box. |
---|
| 136 | */ |
---|
| 137 | inline const Vector3& getMinimum(void) const |
---|
| 138 | { |
---|
| 139 | return mMinimum; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** Gets a modifiable version of the minimum |
---|
| 143 | corner of the box. |
---|
| 144 | */ |
---|
| 145 | inline Vector3& getMinimum(void) |
---|
| 146 | { |
---|
| 147 | return mMinimum; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** Gets the maximum corner of the box. |
---|
| 151 | */ |
---|
| 152 | inline const Vector3& getMaximum(void) const |
---|
| 153 | { |
---|
| 154 | return mMaximum; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /** Gets a modifiable version of the maximum |
---|
| 158 | corner of the box. |
---|
| 159 | */ |
---|
| 160 | inline Vector3& getMaximum(void) |
---|
| 161 | { |
---|
| 162 | return mMaximum; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | /** Sets the minimum corner of the box. |
---|
| 167 | */ |
---|
| 168 | inline void setMinimum( const Vector3& vec ) |
---|
| 169 | { |
---|
| 170 | mExtent = EXTENT_FINITE; |
---|
| 171 | mMinimum = vec; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | inline void setMinimum( Real x, Real y, Real z ) |
---|
| 175 | { |
---|
| 176 | mExtent = EXTENT_FINITE; |
---|
| 177 | mMinimum.x = x; |
---|
| 178 | mMinimum.y = y; |
---|
| 179 | mMinimum.z = z; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /** Changes one of the components of the minimum corner of the box |
---|
| 183 | used to resize only one dimension of the box |
---|
| 184 | */ |
---|
| 185 | inline void setMinimumX(Real x) |
---|
| 186 | { |
---|
| 187 | mMinimum.x = x; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | inline void setMinimumY(Real y) |
---|
| 191 | { |
---|
| 192 | mMinimum.y = y; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | inline void setMinimumZ(Real z) |
---|
| 196 | { |
---|
| 197 | mMinimum.z = z; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | /** Sets the maximum corner of the box. |
---|
| 201 | */ |
---|
| 202 | inline void setMaximum( const Vector3& vec ) |
---|
| 203 | { |
---|
| 204 | mExtent = EXTENT_FINITE; |
---|
| 205 | mMaximum = vec; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | inline void setMaximum( Real x, Real y, Real z ) |
---|
| 209 | { |
---|
| 210 | mExtent = EXTENT_FINITE; |
---|
| 211 | mMaximum.x = x; |
---|
| 212 | mMaximum.y = y; |
---|
| 213 | mMaximum.z = z; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | /** Changes one of the components of the maximum corner of the box |
---|
| 217 | used to resize only one dimension of the box |
---|
| 218 | */ |
---|
| 219 | inline void setMaximumX( Real x ) |
---|
| 220 | { |
---|
| 221 | mMaximum.x = x; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | inline void setMaximumY( Real y ) |
---|
| 225 | { |
---|
| 226 | mMaximum.y = y; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | inline void setMaximumZ( Real z ) |
---|
| 230 | { |
---|
| 231 | mMaximum.z = z; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | /** Sets both minimum and maximum extents at once. |
---|
| 235 | */ |
---|
| 236 | inline void setExtents( const Vector3& min, const Vector3& max ) |
---|
| 237 | { |
---|
| 238 | assert( (min.x <= max.x && min.y <= max.y && min.z <= max.z) && |
---|
| 239 | "The minimum corner of the box must be less than or equal to maximum corner" ); |
---|
| 240 | |
---|
| 241 | mExtent = EXTENT_FINITE; |
---|
| 242 | mMinimum = min; |
---|
| 243 | mMaximum = max; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | inline void setExtents( |
---|
| 247 | Real mx, Real my, Real mz, |
---|
| 248 | Real Mx, Real My, Real Mz ) |
---|
| 249 | { |
---|
| 250 | assert( (mx <= Mx && my <= My && mz <= Mz) && |
---|
| 251 | "The minimum corner of the box must be less than or equal to maximum corner" ); |
---|
| 252 | |
---|
| 253 | mExtent = EXTENT_FINITE; |
---|
| 254 | |
---|
| 255 | mMinimum.x = mx; |
---|
| 256 | mMinimum.y = my; |
---|
| 257 | mMinimum.z = mz; |
---|
| 258 | |
---|
| 259 | mMaximum.x = Mx; |
---|
| 260 | mMaximum.y = My; |
---|
| 261 | mMaximum.z = Mz; |
---|
| 262 | |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | /** Returns a pointer to an array of 8 corner points, useful for |
---|
| 266 | collision vs. non-aligned objects. |
---|
| 267 | @remarks |
---|
| 268 | If the order of these corners is important, they are as |
---|
| 269 | follows: The 4 points of the minimum Z face (note that |
---|
| 270 | because Ogre uses right-handed coordinates, the minimum Z is |
---|
| 271 | at the 'back' of the box) starting with the minimum point of |
---|
| 272 | all, then anticlockwise around this face (if you are looking |
---|
| 273 | onto the face from outside the box). Then the 4 points of the |
---|
| 274 | maximum Z face, starting with maximum point of all, then |
---|
| 275 | anticlockwise around this face (looking onto the face from |
---|
| 276 | outside the box). Like this: |
---|
| 277 | <pre> |
---|
| 278 | 1-----2 |
---|
| 279 | /| /| |
---|
| 280 | / | / | |
---|
| 281 | 5-----4 | |
---|
| 282 | | 0--|--3 |
---|
| 283 | | / | / |
---|
| 284 | |/ |/ |
---|
| 285 | 6-----7 |
---|
| 286 | </pre> |
---|
| 287 | @remarks as this implementation uses a static member, make sure to use your own copy ! |
---|
| 288 | */ |
---|
| 289 | inline const Vector3* getAllCorners(void) const |
---|
| 290 | { |
---|
| 291 | assert( (mExtent == EXTENT_FINITE) && "Can't get corners of a null or infinite AAB" ); |
---|
| 292 | |
---|
| 293 | // The order of these items is, using right-handed co-ordinates: |
---|
| 294 | // Minimum Z face, starting with Min(all), then anticlockwise |
---|
| 295 | // around face (looking onto the face) |
---|
| 296 | // Maximum Z face, starting with Max(all), then anticlockwise |
---|
| 297 | // around face (looking onto the face) |
---|
| 298 | // Only for optimization/compatibility. |
---|
| 299 | if (!mpCorners) |
---|
| 300 | mpCorners = new Vector3[8]; |
---|
| 301 | |
---|
| 302 | mpCorners[0] = mMinimum; |
---|
| 303 | mpCorners[1].x = mMinimum.x; mpCorners[1].y = mMaximum.y; mpCorners[1].z = mMinimum.z; |
---|
| 304 | mpCorners[2].x = mMaximum.x; mpCorners[2].y = mMaximum.y; mpCorners[2].z = mMinimum.z; |
---|
| 305 | mpCorners[3].x = mMaximum.x; mpCorners[3].y = mMinimum.y; mpCorners[3].z = mMinimum.z; |
---|
| 306 | |
---|
| 307 | mpCorners[4] = mMaximum; |
---|
| 308 | mpCorners[5].x = mMinimum.x; mpCorners[5].y = mMaximum.y; mpCorners[5].z = mMaximum.z; |
---|
| 309 | mpCorners[6].x = mMinimum.x; mpCorners[6].y = mMinimum.y; mpCorners[6].z = mMaximum.z; |
---|
| 310 | mpCorners[7].x = mMaximum.x; mpCorners[7].y = mMinimum.y; mpCorners[7].z = mMaximum.z; |
---|
| 311 | |
---|
| 312 | return mpCorners; |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | /** gets the position of one of the corners |
---|
| 316 | */ |
---|
| 317 | Vector3 getCorner(CornerEnum cornerToGet) const |
---|
| 318 | { |
---|
| 319 | switch(cornerToGet) |
---|
| 320 | { |
---|
| 321 | case FAR_LEFT_BOTTOM: |
---|
| 322 | return mMinimum; |
---|
| 323 | case FAR_LEFT_TOP: |
---|
| 324 | return Vector3(mMinimum.x, mMaximum.y, mMinimum.z); |
---|
| 325 | case FAR_RIGHT_TOP: |
---|
| 326 | return Vector3(mMaximum.x, mMaximum.y, mMinimum.z); |
---|
| 327 | case FAR_RIGHT_BOTTOM: |
---|
| 328 | return Vector3(mMaximum.x, mMinimum.y, mMinimum.z); |
---|
| 329 | case NEAR_RIGHT_BOTTOM: |
---|
| 330 | return Vector3(mMaximum.x, mMinimum.y, mMaximum.z); |
---|
| 331 | case NEAR_LEFT_BOTTOM: |
---|
| 332 | return Vector3(mMinimum.x, mMinimum.y, mMaximum.z); |
---|
| 333 | case NEAR_LEFT_TOP: |
---|
| 334 | return Vector3(mMinimum.x, mMaximum.y, mMaximum.z); |
---|
| 335 | case NEAR_RIGHT_TOP: |
---|
| 336 | return mMaximum; |
---|
| 337 | default: |
---|
| 338 | return Vector3(); |
---|
| 339 | } |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | _OgreExport friend std::ostream& operator<<( std::ostream& o, const AxisAlignedBox aab ) |
---|
| 343 | { |
---|
| 344 | switch (aab.mExtent) |
---|
| 345 | { |
---|
| 346 | case EXTENT_NULL: |
---|
| 347 | o << "AxisAlignedBox(null)"; |
---|
| 348 | return o; |
---|
| 349 | |
---|
| 350 | case EXTENT_FINITE: |
---|
| 351 | o << "AxisAlignedBox(min=" << aab.mMinimum << ", max=" << aab.mMaximum << ")"; |
---|
| 352 | return o; |
---|
| 353 | |
---|
| 354 | case EXTENT_INFINITE: |
---|
| 355 | o << "AxisAlignedBox(infinite)"; |
---|
| 356 | return o; |
---|
| 357 | |
---|
| 358 | default: // shut up compiler |
---|
| 359 | assert( false && "Never reached" ); |
---|
| 360 | return o; |
---|
| 361 | } |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | /** Merges the passed in box into the current box. The result is the |
---|
| 365 | box which encompasses both. |
---|
| 366 | */ |
---|
| 367 | void merge( const AxisAlignedBox& rhs ) |
---|
| 368 | { |
---|
| 369 | // Do nothing if rhs null, or this is infinite |
---|
| 370 | if ((rhs.mExtent == EXTENT_NULL) || (mExtent == EXTENT_INFINITE)) |
---|
| 371 | { |
---|
| 372 | return; |
---|
| 373 | } |
---|
| 374 | // Otherwise if rhs is infinite, make this infinite, too |
---|
| 375 | else if (rhs.mExtent == EXTENT_INFINITE) |
---|
| 376 | { |
---|
| 377 | mExtent = EXTENT_INFINITE; |
---|
| 378 | } |
---|
| 379 | // Otherwise if current null, just take rhs |
---|
| 380 | else if (mExtent == EXTENT_NULL) |
---|
| 381 | { |
---|
| 382 | setExtents(rhs.mMinimum, rhs.mMaximum); |
---|
| 383 | } |
---|
| 384 | // Otherwise merge |
---|
| 385 | else |
---|
| 386 | { |
---|
| 387 | Vector3 min = mMinimum; |
---|
| 388 | Vector3 max = mMaximum; |
---|
| 389 | max.makeCeil(rhs.mMaximum); |
---|
| 390 | min.makeFloor(rhs.mMinimum); |
---|
| 391 | |
---|
| 392 | setExtents(min, max); |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | /** Extends the box to encompass the specified point (if needed). |
---|
| 398 | */ |
---|
| 399 | inline void merge( const Vector3& point ) |
---|
| 400 | { |
---|
| 401 | switch (mExtent) |
---|
| 402 | { |
---|
| 403 | case EXTENT_NULL: // if null, use this point |
---|
| 404 | setExtents(point, point); |
---|
| 405 | return; |
---|
| 406 | |
---|
| 407 | case EXTENT_FINITE: |
---|
| 408 | mMaximum.makeCeil(point); |
---|
| 409 | mMinimum.makeFloor(point); |
---|
| 410 | return; |
---|
| 411 | |
---|
| 412 | case EXTENT_INFINITE: // if infinite, makes no difference |
---|
| 413 | return; |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | assert( false && "Never reached" ); |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | /** Transforms the box according to the matrix supplied. |
---|
| 420 | @remarks |
---|
| 421 | By calling this method you get the axis-aligned box which |
---|
| 422 | surrounds the transformed version of this box. Therefore each |
---|
| 423 | corner of the box is transformed by the matrix, then the |
---|
| 424 | extents are mapped back onto the axes to produce another |
---|
| 425 | AABB. Useful when you have a local AABB for an object which |
---|
| 426 | is then transformed. |
---|
| 427 | */ |
---|
| 428 | inline void transform( const Matrix4& matrix ) |
---|
| 429 | { |
---|
| 430 | // Do nothing if current null or infinite |
---|
| 431 | if( mExtent != EXTENT_FINITE ) |
---|
| 432 | return; |
---|
| 433 | |
---|
| 434 | Vector3 oldMin, oldMax, currentCorner; |
---|
| 435 | |
---|
| 436 | // Getting the old values so that we can use the existing merge method. |
---|
| 437 | oldMin = mMinimum; |
---|
| 438 | oldMax = mMaximum; |
---|
| 439 | |
---|
| 440 | // reset |
---|
| 441 | setNull(); |
---|
| 442 | |
---|
| 443 | // We sequentially compute the corners in the following order : |
---|
| 444 | // 0, 6, 5, 1, 2, 4 ,7 , 3 |
---|
| 445 | // This sequence allows us to only change one member at a time to get at all corners. |
---|
| 446 | |
---|
| 447 | // For each one, we transform it using the matrix |
---|
| 448 | // Which gives the resulting point and merge the resulting point. |
---|
| 449 | |
---|
| 450 | // First corner |
---|
| 451 | // min min min |
---|
| 452 | currentCorner = oldMin; |
---|
| 453 | merge( matrix * currentCorner ); |
---|
| 454 | |
---|
| 455 | // min,min,max |
---|
| 456 | currentCorner.z = oldMax.z; |
---|
| 457 | merge( matrix * currentCorner ); |
---|
| 458 | |
---|
| 459 | // min max max |
---|
| 460 | currentCorner.y = oldMax.y; |
---|
| 461 | merge( matrix * currentCorner ); |
---|
| 462 | |
---|
| 463 | // min max min |
---|
| 464 | currentCorner.z = oldMin.z; |
---|
| 465 | merge( matrix * currentCorner ); |
---|
| 466 | |
---|
| 467 | // max max min |
---|
| 468 | currentCorner.x = oldMax.x; |
---|
| 469 | merge( matrix * currentCorner ); |
---|
| 470 | |
---|
| 471 | // max max max |
---|
| 472 | currentCorner.z = oldMax.z; |
---|
| 473 | merge( matrix * currentCorner ); |
---|
| 474 | |
---|
| 475 | // max min max |
---|
| 476 | currentCorner.y = oldMin.y; |
---|
| 477 | merge( matrix * currentCorner ); |
---|
| 478 | |
---|
| 479 | // max min min |
---|
| 480 | currentCorner.z = oldMin.z; |
---|
| 481 | merge( matrix * currentCorner ); |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | /** Transforms the box according to the affine matrix supplied. |
---|
| 485 | @remarks |
---|
| 486 | By calling this method you get the axis-aligned box which |
---|
| 487 | surrounds the transformed version of this box. Therefore each |
---|
| 488 | corner of the box is transformed by the matrix, then the |
---|
| 489 | extents are mapped back onto the axes to produce another |
---|
| 490 | AABB. Useful when you have a local AABB for an object which |
---|
| 491 | is then transformed. |
---|
| 492 | @note |
---|
| 493 | The matrix must be an affine matrix. @see Matrix4::isAffine. |
---|
| 494 | */ |
---|
| 495 | void transformAffine(const Matrix4& m) |
---|
| 496 | { |
---|
| 497 | assert(m.isAffine()); |
---|
| 498 | |
---|
| 499 | // Do nothing if current null or infinite |
---|
| 500 | if ( mExtent != EXTENT_FINITE ) |
---|
| 501 | return; |
---|
| 502 | |
---|
| 503 | Vector3 centre = getCenter(); |
---|
| 504 | Vector3 halfSize = getHalfSize(); |
---|
| 505 | |
---|
| 506 | Vector3 newCentre = m.transformAffine(centre); |
---|
| 507 | Vector3 newHalfSize( |
---|
| 508 | Math::Abs(m[0][0]) * halfSize.x + Math::Abs(m[0][1]) * halfSize.y + Math::Abs(m[0][2]) * halfSize.z, |
---|
| 509 | Math::Abs(m[1][0]) * halfSize.x + Math::Abs(m[1][1]) * halfSize.y + Math::Abs(m[1][2]) * halfSize.z, |
---|
| 510 | Math::Abs(m[2][0]) * halfSize.x + Math::Abs(m[2][1]) * halfSize.y + Math::Abs(m[2][2]) * halfSize.z); |
---|
| 511 | |
---|
| 512 | setExtents(newCentre - newHalfSize, newCentre + newHalfSize); |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | /** Sets the box to a 'null' value i.e. not a box. |
---|
| 516 | */ |
---|
| 517 | inline void setNull() |
---|
| 518 | { |
---|
| 519 | mExtent = EXTENT_NULL; |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | /** Returns true if the box is null i.e. empty. |
---|
| 523 | */ |
---|
| 524 | inline bool isNull(void) const |
---|
| 525 | { |
---|
| 526 | return (mExtent == EXTENT_NULL); |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | /** Returns true if the box is finite. |
---|
| 530 | */ |
---|
| 531 | bool isFinite(void) const |
---|
| 532 | { |
---|
| 533 | return (mExtent == EXTENT_FINITE); |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | /** Sets the box to 'infinite' |
---|
| 537 | */ |
---|
| 538 | inline void setInfinite() |
---|
| 539 | { |
---|
| 540 | mExtent = EXTENT_INFINITE; |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | /** Returns true if the box is infinite. |
---|
| 544 | */ |
---|
| 545 | bool isInfinite(void) const |
---|
| 546 | { |
---|
| 547 | return (mExtent == EXTENT_INFINITE); |
---|
| 548 | } |
---|
| 549 | |
---|
| 550 | /** Returns whether or not this box intersects another. */ |
---|
| 551 | inline bool intersects(const AxisAlignedBox& b2) const |
---|
| 552 | { |
---|
| 553 | // Early-fail for nulls |
---|
| 554 | if (this->isNull() || b2.isNull()) |
---|
| 555 | return false; |
---|
| 556 | |
---|
| 557 | // Early-success for infinites |
---|
| 558 | if (this->isInfinite() || b2.isInfinite()) |
---|
| 559 | return true; |
---|
| 560 | |
---|
| 561 | // Use up to 6 separating planes |
---|
| 562 | if (mMaximum.x < b2.mMinimum.x) |
---|
| 563 | return false; |
---|
| 564 | if (mMaximum.y < b2.mMinimum.y) |
---|
| 565 | return false; |
---|
| 566 | if (mMaximum.z < b2.mMinimum.z) |
---|
| 567 | return false; |
---|
| 568 | |
---|
| 569 | if (mMinimum.x > b2.mMaximum.x) |
---|
| 570 | return false; |
---|
| 571 | if (mMinimum.y > b2.mMaximum.y) |
---|
| 572 | return false; |
---|
| 573 | if (mMinimum.z > b2.mMaximum.z) |
---|
| 574 | return false; |
---|
| 575 | |
---|
| 576 | // otherwise, must be intersecting |
---|
| 577 | return true; |
---|
| 578 | |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | /// Calculate the area of intersection of this box and another |
---|
| 582 | inline AxisAlignedBox intersection(const AxisAlignedBox& b2) const |
---|
| 583 | { |
---|
| 584 | if (this->isNull() || b2.isNull()) |
---|
| 585 | { |
---|
| 586 | return AxisAlignedBox(); |
---|
| 587 | } |
---|
| 588 | else if (this->isInfinite()) |
---|
| 589 | { |
---|
| 590 | return b2; |
---|
| 591 | } |
---|
| 592 | else if (b2.isInfinite()) |
---|
| 593 | { |
---|
| 594 | return *this; |
---|
| 595 | } |
---|
| 596 | |
---|
| 597 | Vector3 intMin = mMinimum; |
---|
| 598 | Vector3 intMax = mMaximum; |
---|
| 599 | |
---|
| 600 | intMin.makeCeil(b2.getMinimum()); |
---|
| 601 | intMax.makeFloor(b2.getMaximum()); |
---|
| 602 | |
---|
| 603 | // Check intersection isn't null |
---|
| 604 | if (intMin.x < intMax.x && |
---|
| 605 | intMin.y < intMax.y && |
---|
| 606 | intMin.z < intMax.z) |
---|
| 607 | { |
---|
| 608 | return AxisAlignedBox(intMin, intMax); |
---|
| 609 | } |
---|
| 610 | |
---|
| 611 | return AxisAlignedBox(); |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | /// Calculate the volume of this box |
---|
| 615 | Real volume(void) const |
---|
| 616 | { |
---|
| 617 | switch (mExtent) |
---|
| 618 | { |
---|
| 619 | case EXTENT_NULL: |
---|
| 620 | return 0.0f; |
---|
| 621 | |
---|
| 622 | case EXTENT_FINITE: |
---|
| 623 | { |
---|
| 624 | Vector3 diff = mMaximum - mMinimum; |
---|
| 625 | return diff.x * diff.y * diff.z; |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | case EXTENT_INFINITE: |
---|
| 629 | return Math::POS_INFINITY; |
---|
| 630 | |
---|
| 631 | default: // shut up compiler |
---|
| 632 | assert( false && "Never reached" ); |
---|
| 633 | return 0.0f; |
---|
| 634 | } |
---|
| 635 | } |
---|
| 636 | |
---|
| 637 | /** Scales the AABB by the vector given. */ |
---|
| 638 | inline void scale(const Vector3& s) |
---|
| 639 | { |
---|
| 640 | // Do nothing if current null or infinite |
---|
| 641 | if (mExtent != EXTENT_FINITE) |
---|
| 642 | return; |
---|
| 643 | |
---|
| 644 | // NB assumes centered on origin |
---|
| 645 | Vector3 min = mMinimum * s; |
---|
| 646 | Vector3 max = mMaximum * s; |
---|
| 647 | setExtents(min, max); |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | /** Tests whether this box intersects a sphere. */ |
---|
| 651 | bool intersects(const Sphere& s) const |
---|
| 652 | { |
---|
| 653 | return Math::intersects(s, *this); |
---|
| 654 | } |
---|
| 655 | /** Tests whether this box intersects a plane. */ |
---|
| 656 | bool intersects(const Plane& p) const |
---|
| 657 | { |
---|
| 658 | return Math::intersects(p, *this); |
---|
| 659 | } |
---|
| 660 | /** Tests whether the vector point is within this box. */ |
---|
| 661 | bool intersects(const Vector3& v) const |
---|
| 662 | { |
---|
| 663 | switch (mExtent) |
---|
| 664 | { |
---|
| 665 | case EXTENT_NULL: |
---|
| 666 | return false; |
---|
| 667 | |
---|
| 668 | case EXTENT_FINITE: |
---|
| 669 | return(v.x >= mMinimum.x && v.x <= mMaximum.x && |
---|
| 670 | v.y >= mMinimum.y && v.y <= mMaximum.y && |
---|
| 671 | v.z >= mMinimum.z && v.z <= mMaximum.z); |
---|
| 672 | |
---|
| 673 | case EXTENT_INFINITE: |
---|
| 674 | return true; |
---|
| 675 | |
---|
| 676 | default: // shut up compiler |
---|
| 677 | assert( false && "Never reached" ); |
---|
| 678 | return false; |
---|
| 679 | } |
---|
| 680 | } |
---|
| 681 | /// Gets the centre of the box |
---|
| 682 | Vector3 getCenter(void) const |
---|
| 683 | { |
---|
| 684 | assert( (mExtent == EXTENT_FINITE) && "Can't get center of a null or infinite AAB" ); |
---|
| 685 | |
---|
| 686 | return Vector3( |
---|
| 687 | (mMaximum.x + mMinimum.x) * 0.5, |
---|
| 688 | (mMaximum.y + mMinimum.y) * 0.5, |
---|
| 689 | (mMaximum.z + mMinimum.z) * 0.5); |
---|
| 690 | } |
---|
| 691 | /// Gets the size of the box |
---|
| 692 | Vector3 getSize(void) const |
---|
| 693 | { |
---|
| 694 | switch (mExtent) |
---|
| 695 | { |
---|
| 696 | case EXTENT_NULL: |
---|
| 697 | return Vector3::ZERO; |
---|
| 698 | |
---|
| 699 | case EXTENT_FINITE: |
---|
| 700 | return mMaximum - mMinimum; |
---|
| 701 | |
---|
| 702 | case EXTENT_INFINITE: |
---|
| 703 | return Vector3( |
---|
| 704 | Math::POS_INFINITY, |
---|
| 705 | Math::POS_INFINITY, |
---|
| 706 | Math::POS_INFINITY); |
---|
| 707 | |
---|
| 708 | default: // shut up compiler |
---|
| 709 | assert( false && "Never reached" ); |
---|
| 710 | return Vector3::ZERO; |
---|
| 711 | } |
---|
| 712 | } |
---|
| 713 | /// Gets the half-size of the box |
---|
| 714 | Vector3 getHalfSize(void) const |
---|
| 715 | { |
---|
| 716 | switch (mExtent) |
---|
| 717 | { |
---|
| 718 | case EXTENT_NULL: |
---|
| 719 | return Vector3::ZERO; |
---|
| 720 | |
---|
| 721 | case EXTENT_FINITE: |
---|
| 722 | return (mMaximum - mMinimum) * 0.5; |
---|
| 723 | |
---|
| 724 | case EXTENT_INFINITE: |
---|
| 725 | return Vector3( |
---|
| 726 | Math::POS_INFINITY, |
---|
| 727 | Math::POS_INFINITY, |
---|
| 728 | Math::POS_INFINITY); |
---|
| 729 | |
---|
| 730 | default: // shut up compiler |
---|
| 731 | assert( false && "Never reached" ); |
---|
| 732 | return Vector3::ZERO; |
---|
| 733 | } |
---|
| 734 | } |
---|
| 735 | |
---|
| 736 | /** Tests whether the given point contained by this box. |
---|
| 737 | */ |
---|
| 738 | bool contains(const Vector3& v) const |
---|
| 739 | { |
---|
| 740 | if (isNull()) |
---|
| 741 | return false; |
---|
| 742 | if (isInfinite()) |
---|
| 743 | return true; |
---|
| 744 | |
---|
| 745 | return mMinimum.x <= v.x && v.x <= mMaximum.x && |
---|
| 746 | mMinimum.y <= v.y && v.y <= mMaximum.y && |
---|
| 747 | mMinimum.z <= v.z && v.z <= mMaximum.z; |
---|
| 748 | } |
---|
| 749 | |
---|
| 750 | /** Tests whether another box contained by this box. |
---|
| 751 | */ |
---|
| 752 | bool contains(const AxisAlignedBox& other) const |
---|
| 753 | { |
---|
| 754 | if (other.isNull() || this->isInfinite()) |
---|
| 755 | return true; |
---|
| 756 | |
---|
| 757 | if (this->isNull() || other.isInfinite()) |
---|
| 758 | return false; |
---|
| 759 | |
---|
| 760 | return this->mMinimum.x <= other.mMinimum.x && |
---|
| 761 | this->mMinimum.y <= other.mMinimum.y && |
---|
| 762 | this->mMinimum.z <= other.mMinimum.z && |
---|
| 763 | other.mMaximum.x <= this->mMaximum.x && |
---|
| 764 | other.mMaximum.y <= this->mMaximum.y && |
---|
| 765 | other.mMaximum.z <= this->mMaximum.z; |
---|
| 766 | } |
---|
| 767 | |
---|
| 768 | /** Tests 2 boxes for equality. |
---|
| 769 | */ |
---|
| 770 | bool operator== (const AxisAlignedBox& rhs) const |
---|
| 771 | { |
---|
| 772 | if (this->mExtent != rhs.mExtent) |
---|
| 773 | return false; |
---|
| 774 | |
---|
| 775 | if (!this->isFinite()) |
---|
| 776 | return true; |
---|
| 777 | |
---|
| 778 | return this->mMinimum == rhs.mMinimum && |
---|
| 779 | this->mMaximum == rhs.mMaximum; |
---|
| 780 | } |
---|
| 781 | |
---|
| 782 | /** Tests 2 boxes for inequality. |
---|
| 783 | */ |
---|
| 784 | bool operator!= (const AxisAlignedBox& rhs) const |
---|
| 785 | { |
---|
| 786 | return !(*this == rhs); |
---|
| 787 | } |
---|
| 788 | |
---|
| 789 | }; |
---|
| 790 | |
---|
| 791 | } // namespace Ogre |
---|
| 792 | |
---|
| 793 | #endif |
---|