BoundingBox
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* 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
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_BOUNDINGBOX
#define OSG_BOUNDINGBOX 1
#include <osg/Config>
#include <osg/Export>
#include <osg/Vec3>
#include <osg/Vec3d>
#include <float.h>
namespace osg {
template<typename VT>
class BoundingSphereImpl;
template<typename VT>
class BoundingBoxImpl
{
public:
typedef VT vec_type;
typedef typename VT::value_type value_type;
vec_type _min;
vec_type _max;
inline BoundingBoxImpl() :
_min(FLT_MAX,
FLT_MAX,
FLT_MAX),
_max(-FLT_MAX,
-FLT_MAX,
-FLT_MAX)
{}
template<typename BT>
inline BoundingBoxImpl(const BoundingBoxImpl<BT>& bb) :
_min(bb._min),
_max(bb._max)
{}
inline BoundingBoxImpl(value_type xmin, value_type ymin, value_type zmin,
value_type xmax, value_type ymax, value_type zmax) :
_min(xmin,ymin,zmin),
_max(xmax,ymax,zmax) {}
inline BoundingBoxImpl(const vec_type& min,const vec_type& max) :
_min(min),
_max(max) {}
inline void init()
{
_min.set(FLT_MAX,
FLT_MAX,
FLT_MAX);
_max.set(-FLT_MAX,
-FLT_MAX,
-FLT_MAX);
}
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; }
inline bool valid() const
{
return _max.x()>=_min.x() && _max.y()>=_min.y() && _max.z()>=_min.z();
}
inline void set (value_type xmin, value_type ymin, value_type zmin,
value_type xmax, value_type ymax, value_type zmax)
{
_min.set(xmin,ymin,zmin);
_max.set(xmax,ymax,zmax);
}
inline void set(const vec_type& min,const vec_type& max)
{
_min = min;
_max = max;
}
inline value_type& xMin() { return _min.x(); }
inline value_type xMin() const { return _min.x(); }
inline value_type& yMin() { return _min.y(); }
inline value_type yMin() const { return _min.y(); }
inline value_type& zMin() { return _min.z(); }
inline value_type zMin() const { return _min.z(); }
inline value_type& xMax() { return _max.x(); }
inline value_type xMax() const { return _max.x(); }
inline value_type& yMax() { return _max.y(); }
inline value_type yMax() const { return _max.y(); }
inline value_type& zMax() { return _max.z(); }
inline value_type zMax() const { return _max.z(); }
inline const vec_type center() const
{
return (_min+_max)*0.5;
}
inline value_type radius() const
{
return sqrt(radius2());
}
inline value_type radius2() const
{
return 0.25*((_max-_min).length2());
}
inline const vec_type corner(unsigned int pos) const
{
return vec_type(pos&1?_max.x():_min.x(),pos&2?_max.y():_min.y(),pos&4?_max.z():_min.z());
}
inline void expandBy(const vec_type& v)
{
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();
}
inline void expandBy(value_type x,value_type y,value_type z)
{
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;
if(z<_min.z()) _min.z() = z;
if(z>_max.z()) _max.z() = z;
}
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();
}
template<typename BST>
void expandBy(const BoundingSphereImpl<BST>& sh)
{
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;
}
BoundingBoxImpl intersect(const BoundingBoxImpl& bb) const
{ return BoundingBoxImpl(osg::maximum(xMin(),bb.xMin()),osg::maximum(yMin(),bb.yMin()),osg::maximum(zMin(),bb.zMin()),
osg::minimum(xMax(),bb.xMax()),osg::minimum(yMax(),bb.yMax()),osg::minimum(zMax(),bb.zMax()));
}
bool intersects(const BoundingBoxImpl& bb) const
{ 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());
}
inline bool contains(const vec_type& v) const
{
return valid() &&
(v.x()>=_min.x() && v.x()<=_max.x()) &&
(v.y()>=_min.y() && v.y()<=_max.y()) &&
(v.z()>=_min.z() && v.z()<=_max.z());
}
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());
}
};
typedef BoundingBoxImpl<Vec3f> BoundingBoxf;
typedef BoundingBoxImpl<Vec3d> BoundingBoxd;
#ifdef OSG_USE_FLOAT_BOUNDINGBOX
typedef BoundingBoxf BoundingBox;
#else
typedef BoundingBoxd BoundingBox;
#endif
}
#endif
Vec3
Vec3d
osg::BoundingBoxImpl
Definition BoundingBox:34
osg::BoundingBoxImpl::zMax
value_type zMax() const
Definition BoundingBox:123
osg::BoundingBoxImpl::center
const vec_type center() const
Definition BoundingBox:126
osg::BoundingBoxImpl::value_type
VT::value_type value_type
Definition BoundingBox:37
osg::BoundingBoxImpl::intersects
bool intersects(const BoundingBoxImpl &bb) const
Definition BoundingBox:225
osg::BoundingBoxImpl::xMax
value_type & xMax()
Definition BoundingBox:116
osg::BoundingBoxImpl::yMin
value_type & yMin()
Definition BoundingBox:110
osg::BoundingBoxImpl::BoundingBoxImpl
BoundingBoxImpl(const BoundingBoxImpl< BT > &bb)
Definition BoundingBox:55
osg::BoundingBoxImpl::xMin
value_type & xMin()
Definition BoundingBox:107
osg::BoundingBoxImpl::zMin
value_type & zMin()
Definition BoundingBox:113
osg::BoundingBoxImpl::xMax
value_type xMax() const
Definition BoundingBox:117
osg::BoundingBoxImpl::expandBy
void expandBy(value_type x, value_type y, value_type z)
Definition BoundingBox:171
osg::BoundingBoxImpl::BoundingBoxImpl
BoundingBoxImpl(value_type xmin, value_type ymin, value_type zmin, value_type xmax, value_type ymax, value_type zmax)
Definition BoundingBox:61
osg::BoundingBoxImpl< Vec3f >::_max
vec_type _max
Definition BoundingBox:42
osg::BoundingBoxImpl::contains
bool contains(const vec_type &v, value_type epsilon) const
Definition BoundingBox:242
osg::BoundingBoxImpl::expandBy
void expandBy(const vec_type &v)
Definition BoundingBox:156
osg::BoundingBoxImpl::operator==
bool operator==(const BoundingBoxImpl &rhs) const
Definition BoundingBox:82
osg::BoundingBoxImpl::zMax
value_type & zMax()
Definition BoundingBox:122
osg::BoundingBoxImpl::xMin
value_type xMin() const
Definition BoundingBox:108
osg::BoundingBoxImpl::set
void set(value_type xmin, value_type ymin, value_type zmin, value_type xmax, value_type ymax, value_type zmax)
Definition BoundingBox:92
osg::BoundingBoxImpl::intersect
BoundingBoxImpl intersect(const BoundingBoxImpl &bb) const
Definition BoundingBox:218
osg::BoundingBoxImpl::corner
const vec_type corner(unsigned int pos) const
Definition BoundingBox:149
osg::BoundingBoxImpl::valid
bool valid() const
Definition BoundingBox:86
osg::BoundingBoxImpl< Vec3f >::_min
vec_type _min
Definition BoundingBox:40
osg::BoundingBoxImpl::expandBy
void expandBy(const BoundingBoxImpl &bb)
Definition BoundingBox:185
osg::BoundingBoxImpl::expandBy
void expandBy(const BoundingSphereImpl< BST > &sh)
Definition BoundingBox:202
osg::BoundingBoxImpl::operator!=
bool operator!=(const BoundingBoxImpl &rhs) const
Definition BoundingBox:83
osg::BoundingBoxImpl::BoundingBoxImpl
BoundingBoxImpl()
Definition BoundingBox:45
osg::BoundingBoxImpl::contains
bool contains(const vec_type &v) const
Definition BoundingBox:233
osg::BoundingBoxImpl::set
void set(const vec_type &min, const vec_type &max)
Definition BoundingBox:100
osg::BoundingBoxImpl::vec_type
VT vec_type
Definition BoundingBox:36
osg::BoundingBoxImpl::yMax
value_type yMax() const
Definition BoundingBox:120
osg::BoundingBoxImpl::zMin
value_type zMin() const
Definition BoundingBox:114
osg::BoundingBoxImpl::init
void init()
Definition BoundingBox:72
osg::BoundingBoxImpl::radius2
value_type radius2() const
Definition BoundingBox:139
osg::BoundingBoxImpl::yMax
value_type & yMax()
Definition BoundingBox:119
osg::BoundingBoxImpl::radius
value_type radius() const
Definition BoundingBox:132
osg::BoundingBoxImpl::yMin
value_type yMin() const
Definition BoundingBox:111
osg::BoundingBoxImpl::BoundingBoxImpl
BoundingBoxImpl(const vec_type &min, const vec_type &max)
Definition BoundingBox:67
osg::BoundingSphereImpl
Definition BoundingSphere:35
osg::BoundingSphereImpl::_radius
value_type _radius
Definition BoundingSphere:41
osg::BoundingSphereImpl::_center
vec_type _center
Definition BoundingSphere:40
osg::BoundingSphereImpl::valid
bool valid() const
Definition BoundingSphere:70
osg::Vec3f::z
value_type & z()
Definition Vec3f:82
osg::Vec3f::y
value_type & y()
Definition Vec3f:81
osg::Vec3f::x
value_type & x()
Definition Vec3f:80
osg
Definition AlphaFunc:19
osg::BoundingBoxd
BoundingBoxImpl< Vec3d > BoundingBoxd
Definition BoundingBox:252
osg::BoundingBox
BoundingBoxd BoundingBox
Definition BoundingBox:257
osg::BoundingBoxf
BoundingBoxImpl< Vec3f > BoundingBoxf
Definition BoundingBox:251
osg::maximum
T maximum(T lhs, T rhs)
Definition Math:85
osg::minimum
T minimum(T lhs, T rhs)
Definition Math:78
Export