2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2003-01-22 00:45:36 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2003-01-22 00:45:36 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2003-01-22 00:45:36 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2001-10-04 23:12:57 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
#ifndef OSG_BOUNDINGBOX
|
|
|
|
#define OSG_BOUNDINGBOX 1
|
|
|
|
|
2008-06-20 23:50:53 +08:00
|
|
|
#include <osg/Config>
|
2001-01-11 00:32:10 +08:00
|
|
|
#include <osg/Export>
|
|
|
|
#include <osg/Vec3>
|
2008-04-04 02:36:50 +08:00
|
|
|
#include <osg/Vec3d>
|
2001-01-11 00:32:10 +08:00
|
|
|
#include <float.h>
|
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
2008-11-27 00:39:52 +08:00
|
|
|
template<typename VT>
|
|
|
|
class BoundingSphereImpl;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
/** General purpose axis-aligned bounding box class for enclosing objects/vertices.
|
2004-09-01 16:15:36 +08:00
|
|
|
* Bounds leaf objects in a scene such as osg::Drawable objects. Used for frustum
|
|
|
|
* culling etc.
|
2001-01-11 00:32:10 +08:00
|
|
|
*/
|
2008-11-27 00:39:52 +08:00
|
|
|
template<typename VT>
|
|
|
|
class BoundingBoxImpl
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
public:
|
2008-11-27 00:39:52 +08:00
|
|
|
typedef VT vec_type;
|
|
|
|
typedef typename VT::value_type value_type;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-12-05 04:08:41 +08:00
|
|
|
/** Minimum extent. (Smallest X, Y, and Z values of all coordinates.) */
|
2008-04-04 02:36:50 +08:00
|
|
|
vec_type _min;
|
2005-12-05 04:08:41 +08:00
|
|
|
/** Maximum extent. (Greatest X, Y, and Z values of all coordinates.) */
|
2008-04-04 02:36:50 +08:00
|
|
|
vec_type _max;
|
2004-09-01 16:15:36 +08:00
|
|
|
|
|
|
|
/** Creates an uninitialized bounding box. */
|
2008-11-27 00:39:52 +08:00
|
|
|
inline BoundingBoxImpl() :
|
|
|
|
_min(FLT_MAX,
|
|
|
|
FLT_MAX,
|
|
|
|
FLT_MAX),
|
|
|
|
_max(-FLT_MAX,
|
|
|
|
-FLT_MAX,
|
|
|
|
-FLT_MAX)
|
|
|
|
{}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2014-04-07 23:04:34 +08:00
|
|
|
template<typename BT>
|
|
|
|
inline BoundingBoxImpl(const BoundingBoxImpl<BT>& bb) :
|
|
|
|
_min(bb._min),
|
|
|
|
_max(bb._max)
|
|
|
|
{}
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Creates a bounding box initialized to the given extents. */
|
2008-11-27 00:39:52 +08:00
|
|
|
inline BoundingBoxImpl(value_type xmin, value_type ymin, value_type zmin,
|
2008-04-04 02:36:50 +08:00
|
|
|
value_type xmax, value_type ymax, value_type zmax) :
|
|
|
|
_min(xmin,ymin,zmin),
|
|
|
|
_max(xmax,ymax,zmax) {}
|
2001-11-06 18:34:51 +08:00
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Creates a bounding box initialized to the given extents. */
|
2012-03-22 01:36:20 +08:00
|
|
|
inline BoundingBoxImpl(const vec_type& min,const vec_type& max) :
|
2001-11-06 18:34:51 +08:00
|
|
|
_min(min),
|
|
|
|
_max(max) {}
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Clear the bounding box. Erases existing minimum and maximum extents. */
|
2001-09-20 05:08:56 +08:00
|
|
|
inline void init()
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2008-11-27 00:39:52 +08:00
|
|
|
_min.set(FLT_MAX,
|
|
|
|
FLT_MAX,
|
|
|
|
FLT_MAX);
|
|
|
|
_max.set(-FLT_MAX,
|
|
|
|
-FLT_MAX,
|
|
|
|
-FLT_MAX);
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2014-02-11 00:44:13 +08:00
|
|
|
inline bool operator == (const BoundingBoxImpl& rhs) const { return _min==rhs._min && _max==rhs._max; }
|
|
|
|
inline bool operator != (const BoundingBoxImpl& rhs) const { return _min!=rhs._min || _max!=rhs._max; }
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** Returns true if the bounding box extents are valid, false otherwise. */
|
2002-09-02 20:31:35 +08:00
|
|
|
inline bool valid() const
|
2002-06-20 00:06:03 +08:00
|
|
|
{
|
2003-11-25 18:56:42 +08:00
|
|
|
return _max.x()>=_min.x() && _max.y()>=_min.y() && _max.z()>=_min.z();
|
2002-06-20 00:06:03 +08:00
|
|
|
}
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Sets the bounding box extents. */
|
2008-04-04 02:36:50 +08:00
|
|
|
inline void set (value_type xmin, value_type ymin, value_type zmin,
|
|
|
|
value_type xmax, value_type ymax, value_type zmax)
|
2001-11-06 18:34:51 +08:00
|
|
|
{
|
|
|
|
_min.set(xmin,ymin,zmin);
|
|
|
|
_max.set(xmax,ymax,zmax);
|
|
|
|
}
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Sets the bounding box extents. */
|
2008-04-04 02:36:50 +08:00
|
|
|
inline void set(const vec_type& min,const vec_type& max)
|
2001-11-06 18:34:51 +08:00
|
|
|
{
|
|
|
|
_min = min;
|
|
|
|
_max = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-04 02:36:50 +08:00
|
|
|
inline value_type& xMin() { return _min.x(); }
|
|
|
|
inline value_type xMin() const { return _min.x(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-04-04 02:36:50 +08:00
|
|
|
inline value_type& yMin() { return _min.y(); }
|
|
|
|
inline value_type yMin() const { return _min.y(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-04-04 02:36:50 +08:00
|
|
|
inline value_type& zMin() { return _min.z(); }
|
|
|
|
inline value_type zMin() const { return _min.z(); }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2008-04-04 02:36:50 +08:00
|
|
|
inline value_type& xMax() { return _max.x(); }
|
|
|
|
inline value_type xMax() const { return _max.x(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-04-04 02:36:50 +08:00
|
|
|
inline value_type& yMax() { return _max.y(); }
|
|
|
|
inline value_type yMax() const { return _max.y(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-04-04 02:36:50 +08:00
|
|
|
inline value_type& zMax() { return _max.z(); }
|
|
|
|
inline value_type zMax() const { return _max.z(); }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Calculates and returns the bounding box center. */
|
2008-04-04 02:36:50 +08:00
|
|
|
inline const vec_type center() const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2008-04-04 02:36:50 +08:00
|
|
|
return (_min+_max)*0.5;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Calculates and returns the bounding box radius. */
|
2008-04-04 02:36:50 +08:00
|
|
|
inline value_type radius() const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2008-04-04 02:36:50 +08:00
|
|
|
return sqrt(radius2());
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Calculates and returns the squared length of the bounding box radius.
|
|
|
|
* Note, radius2() is faster to calculate than radius(). */
|
2008-04-04 02:36:50 +08:00
|
|
|
inline value_type radius2() const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2008-04-04 02:36:50 +08:00
|
|
|
return 0.25*((_max-_min).length2());
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Returns a specific corner of the bounding box.
|
|
|
|
* pos specifies the corner as a number between 0 and 7.
|
|
|
|
* Each bit selects an axis, X, Y, or Z from least- to
|
|
|
|
* most-significant. Unset bits select the minimum value
|
|
|
|
* for that axis, and set bits select the maximum. */
|
2008-04-04 02:36:50 +08:00
|
|
|
inline const vec_type corner(unsigned int pos) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2008-04-04 02:36:50 +08:00
|
|
|
return vec_type(pos&1?_max.x():_min.x(),pos&2?_max.y():_min.y(),pos&4?_max.z():_min.z());
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Expands the bounding box to include the given coordinate.
|
|
|
|
* If the box is uninitialized, set its min and max extents to v. */
|
2008-04-04 02:36:50 +08:00
|
|
|
inline void expandBy(const vec_type& v)
|
2002-07-19 06:35:54 +08:00
|
|
|
{
|
|
|
|
if(v.x()<_min.x()) _min.x() = v.x();
|
|
|
|
if(v.x()>_max.x()) _max.x() = v.x();
|
|
|
|
|
|
|
|
if(v.y()<_min.y()) _min.y() = v.y();
|
|
|
|
if(v.y()>_max.y()) _max.y() = v.y();
|
|
|
|
|
|
|
|
if(v.z()<_min.z()) _min.z() = v.z();
|
|
|
|
if(v.z()>_max.z()) _max.z() = v.z();
|
|
|
|
}
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Expands the bounding box to include the given coordinate.
|
|
|
|
* If the box is uninitialized, set its min and max extents to
|
|
|
|
* Vec3(x,y,z). */
|
2008-04-04 02:36:50 +08:00
|
|
|
inline void expandBy(value_type x,value_type y,value_type z)
|
2002-07-19 06:35:54 +08:00
|
|
|
{
|
|
|
|
if(x<_min.x()) _min.x() = x;
|
|
|
|
if(x>_max.x()) _max.x() = x;
|
|
|
|
|
|
|
|
if(y<_min.y()) _min.y() = y;
|
|
|
|
if(y>_max.y()) _max.y() = y;
|
|
|
|
|
2002-07-19 23:49:43 +08:00
|
|
|
if(z<_min.z()) _min.z() = z;
|
|
|
|
if(z>_max.z()) _max.z() = z;
|
2002-07-19 06:35:54 +08:00
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Expands this bounding box to include the given bounding box.
|
|
|
|
* If this box is uninitialized, set it equal to bb. */
|
2009-02-11 18:41:55 +08:00
|
|
|
void expandBy(const BoundingBoxImpl& bb)
|
|
|
|
{
|
|
|
|
if (!bb.valid()) return;
|
|
|
|
|
|
|
|
if(bb._min.x()<_min.x()) _min.x() = bb._min.x();
|
|
|
|
if(bb._max.x()>_max.x()) _max.x() = bb._max.x();
|
|
|
|
|
|
|
|
if(bb._min.y()<_min.y()) _min.y() = bb._min.y();
|
|
|
|
if(bb._max.y()>_max.y()) _max.y() = bb._max.y();
|
|
|
|
|
|
|
|
if(bb._min.z()<_min.z()) _min.z() = bb._min.z();
|
|
|
|
if(bb._max.z()>_max.z()) _max.z() = bb._max.z();
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Expands this bounding box to include the given sphere.
|
|
|
|
* If this box is uninitialized, set it to include sh. */
|
2014-04-07 23:04:34 +08:00
|
|
|
template<typename BST>
|
|
|
|
void expandBy(const BoundingSphereImpl<BST>& sh)
|
2009-02-11 18:41:55 +08:00
|
|
|
{
|
|
|
|
if (!sh.valid()) return;
|
|
|
|
|
|
|
|
if(sh._center.x()-sh._radius<_min.x()) _min.x() = sh._center.x()-sh._radius;
|
|
|
|
if(sh._center.x()+sh._radius>_max.x()) _max.x() = sh._center.x()+sh._radius;
|
|
|
|
|
|
|
|
if(sh._center.y()-sh._radius<_min.y()) _min.y() = sh._center.y()-sh._radius;
|
|
|
|
if(sh._center.y()+sh._radius>_max.y()) _max.y() = sh._center.y()+sh._radius;
|
|
|
|
|
|
|
|
if(sh._center.z()-sh._radius<_min.z()) _min.z() = sh._center.z()-sh._radius;
|
|
|
|
if(sh._center.z()+sh._radius>_max.z()) _max.z() = sh._center.z()+sh._radius;
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-11-25 18:56:42 +08:00
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
/** Returns the intersection of this bounding box and the specified bounding box. */
|
2008-11-27 00:39:52 +08:00
|
|
|
BoundingBoxImpl intersect(const BoundingBoxImpl& bb) const
|
|
|
|
{ return BoundingBoxImpl(osg::maximum(xMin(),bb.xMin()),osg::maximum(yMin(),bb.yMin()),osg::maximum(zMin(),bb.zMin()),
|
2003-11-25 18:56:42 +08:00
|
|
|
osg::minimum(xMax(),bb.xMax()),osg::minimum(yMax(),bb.yMax()),osg::minimum(zMax(),bb.zMax()));
|
|
|
|
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return true if this bounding box intersects the specified bounding box. */
|
2008-11-27 00:39:52 +08:00
|
|
|
bool intersects(const BoundingBoxImpl& bb) const
|
2003-11-27 23:13:55 +08:00
|
|
|
{ return osg::maximum(xMin(),bb.xMin()) <= osg::minimum(xMax(),bb.xMax()) &&
|
|
|
|
osg::maximum(yMin(),bb.yMin()) <= osg::minimum(yMax(),bb.yMax()) &&
|
|
|
|
osg::maximum(zMin(),bb.zMin()) <= osg::minimum(zMax(),bb.zMax());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Returns true if this bounding box contains the specified coordinate. */
|
2008-04-04 02:36:50 +08:00
|
|
|
inline bool contains(const vec_type& v) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2012-03-22 01:36:20 +08:00
|
|
|
return valid() &&
|
2001-01-11 00:32:10 +08:00
|
|
|
(v.x()>=_min.x() && v.x()<=_max.x()) &&
|
|
|
|
(v.y()>=_min.y() && v.y()<=_max.y()) &&
|
|
|
|
(v.z()>=_min.z() && v.z()<=_max.z());
|
|
|
|
}
|
2013-11-18 20:45:04 +08:00
|
|
|
|
|
|
|
/** Returns true if this bounding box contains the specified coordinate allowing for specific epsilon. */
|
|
|
|
inline bool contains(const vec_type& v, value_type epsilon) const
|
|
|
|
{
|
|
|
|
return valid() &&
|
|
|
|
((v.x()+epsilon)>=_min.x() && (v.x()-epsilon)<=_max.x()) &&
|
|
|
|
((v.y()+epsilon)>=_min.y() && (v.y()-epsilon)<=_max.y()) &&
|
|
|
|
((v.z()+epsilon)>=_min.z() && (v.z()-epsilon)<=_max.z());
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
};
|
|
|
|
|
2008-11-27 00:39:52 +08:00
|
|
|
typedef BoundingBoxImpl<Vec3f> BoundingBoxf;
|
|
|
|
typedef BoundingBoxImpl<Vec3d> BoundingBoxd;
|
|
|
|
|
|
|
|
#ifdef OSG_USE_FLOAT_BOUNDINGBOX
|
|
|
|
typedef BoundingBoxf BoundingBox;
|
|
|
|
#else
|
|
|
|
typedef BoundingBoxd BoundingBox;
|
|
|
|
#endif
|
|
|
|
|
2002-02-03 20:33:41 +08:00
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
#endif
|