mOrientedBox.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "math/mOrientedBox.h"
  24. #include "math/mMatrix.h"
  25. //-----------------------------------------------------------------------------
  26. bool OrientedBox3F::isContained( const Point3F& point ) const
  27. {
  28. Point3F distToCenter = point - getCenter();
  29. for( U32 i = 0; i < 3; ++ i )
  30. {
  31. F32 coeff = mDot( distToCenter, getAxis( i ) );
  32. if( mFabs( coeff ) > getHalfExtents()[ i ] )
  33. return false;
  34. }
  35. return true;
  36. }
  37. //-----------------------------------------------------------------------------
  38. void OrientedBox3F::set( const MatrixF& transform, const Point3F& extents )
  39. {
  40. mCenter = transform.getPosition();
  41. mAxes[ RightVector ] = transform.getRightVector();
  42. mAxes[ ForwardVector ] = transform.getForwardVector();
  43. mAxes[ UpVector ] = transform.getUpVector();
  44. mHalfExtents = extents * 0.5f;
  45. _initPoints();
  46. }
  47. //-----------------------------------------------------------------------------
  48. void OrientedBox3F::set( const MatrixF& transform, const Box3F& aabb )
  49. {
  50. mCenter = aabb.getCenter();
  51. transform.mulP( mCenter );
  52. mAxes[ RightVector ] = transform.getRightVector();
  53. mAxes[ ForwardVector ] = transform.getForwardVector();
  54. mAxes[ UpVector ] = transform.getUpVector();
  55. mHalfExtents[ 0 ] = aabb.len_x() / 2.f;
  56. mHalfExtents[ 1 ] = aabb.len_y() / 2.f;
  57. mHalfExtents[ 2 ] = aabb.len_z() / 2.f;
  58. _initPoints();
  59. }
  60. //-----------------------------------------------------------------------------
  61. void OrientedBox3F::_initPoints()
  62. {
  63. const Point3F right = mAxes[ RightVector ] * mHalfExtents.x;
  64. const Point3F forward = mAxes[ ForwardVector ] * mHalfExtents.y;
  65. const Point3F up = mAxes[ UpVector ] * mHalfExtents.z;
  66. mPoints[ NearBottomLeft ] = mCenter - forward - right - up;
  67. mPoints[ NearBottomRight ] = mCenter - forward + right - up;
  68. mPoints[ NearTopLeft ] = mCenter - forward - right + up;
  69. mPoints[ NearTopRight ] = mCenter - forward + right + up;
  70. mPoints[ FarBottomLeft ] = mCenter + forward - right - up;
  71. mPoints[ FarBottomRight ] = mCenter + forward + right - up;
  72. mPoints[ FarTopLeft ] = mCenter + forward - right + up;
  73. mPoints[ FarTopRight ] = mCenter + forward + right + up;
  74. }