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