mOrientedBox.h

Engine/source/math/mOrientedBox.h

More...

Classes:

class

An oriented bounding box (OBB) described by a center point, three normalizes axis vectors, and half-extents along each of the axes.

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#ifndef _MORIENTEDBOX_H_
 25#define _MORIENTEDBOX_H_
 26
 27#ifndef _MBOXBASE_H_
 28#include "math/mBoxBase.h"
 29#endif
 30
 31#ifndef _MPOINT3_H_
 32#include "math/mPoint3.h"
 33#endif
 34
 35
 36class MatrixF;
 37class Box3F;
 38
 39
 40/// An oriented bounding box (OBB) described by a center point, three normalizes axis
 41/// vectors, and half-extents along each of the axes.
 42class OrientedBox3F : public BoxBase
 43{
 44   public:
 45
 46      enum Axis
 47      {
 48         RightVector,
 49         ForwardVector,
 50         UpVector
 51      };
 52
 53   protected:
 54
 55      /// Center point.
 56      Point3F mCenter;
 57
 58      /// Normalized axis vectors.
 59      Point3F mAxes[ 3 ];
 60
 61      /// Box half-extents along each axis.
 62      Point3F mHalfExtents;
 63
 64      /// Corner points.
 65      Point3F mPoints[ NUM_POINTS ];
 66
 67      void _initPoints();
 68
 69   public:
 70
 71      OrientedBox3F() {}
 72      OrientedBox3F( const MatrixF& transform, const Point3F& extents ) { set( transform, extents ); }
 73      OrientedBox3F( const MatrixF& transform, const Box3F& aabb ) { set( transform, aabb ); }
 74
 75      /// Return the center point of the bounding box.
 76      const Point3F& getCenter() const { return mCenter; }
 77
 78      /// Return the normalized axis vector for the given world-space axis. 
 79      const Point3F& getAxis( U32 i ) const
 80      {
 81         AssertFatal( i < 3, "OrientedBox3F::getAxis - Index out of range" );
 82         return mAxes[ i ];
 83      }
 84
 85      /// Return the half-extents along each axis.
 86      ///
 87      /// Since the OBBs are symmetrical across each axis, we store half-extents
 88      /// instead of full extents as usually half-extents are needed in the computations.
 89      const Point3F& getHalfExtents() const { return mHalfExtents; }
 90
 91      /// Return true if the given point is contained in the OBB.
 92      bool isContained( const Point3F& point ) const;
 93
 94      /// Return the corner points of the box.
 95      const Point3F* getPoints() const { return mPoints; }
 96
 97      /// Return the array of corner points for the box.
 98      operator const Point3F*() const { return getPoints(); }
 99
100      /// Compute the OBB values from the given transform and extents.
101      ///
102      /// @param transform World->object space transform.
103      /// @param extents Box extent on each axis.
104      void set( const MatrixF& transform, const Point3F& extents );
105
106      /// Compute the OBB from an AABB in the given transform space.
107      ///
108      /// @param transform Transform space for the AABB.
109      /// @param aabb An axis-aligned bounding box in the given transform space.
110      void set( const MatrixF& transform, const Box3F& aabb );
111};
112
113#endif // !_MORIENTEDBOX_H_
114