BoundingSphere
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_BOUNDINGSPHERE
#define OSG_BOUNDINGSPHERE 1
#include <osg/Config>
#include <osg/Export>
#include <osg/Vec3f>
#include <osg/Vec3d>
namespace osg {
template<typename VT>
class BoundingBoxImpl;
template<typename VT>
class BoundingSphereImpl
{
public:
typedef VT vec_type;
typedef typename VT::value_type value_type;
vec_type _center;
value_type _radius;
BoundingSphereImpl() : _center(0.0,0.0,0.0),_radius(-1.0) {}
BoundingSphereImpl(const vec_type& cntr, value_type rad) : _center(cntr),_radius(rad) {}
BoundingSphereImpl(const BoundingSphereImpl& bs) : _center(bs._center),_radius(bs._radius) {}
BoundingSphereImpl(const BoundingBoxImpl<VT>& bb) : _center(0.0,0.0,0.0),_radius(-1.0) { expandBy(bb); }
BoundingSphereImpl& operator= (const BoundingSphereImpl &o){
_center = o._center;
_radius = o._radius;
return *this;
}
inline void init()
{
_center.set(0.0,0.0,0.0);
_radius = -1.0;
}
inline bool valid() const { return _radius>=0.0; }
inline bool operator == (const BoundingSphereImpl& rhs) const { return _center==rhs._center && _radius==rhs._radius; }
inline bool operator != (const BoundingSphereImpl& rhs) const { return _center!=rhs._center || _radius!=rhs._radius; }
inline void set(const vec_type& center,value_type radius)
{
_center = center;
_radius = radius;
}
inline vec_type& center() { return _center; }
inline const vec_type& center() const { return _center; }
inline value_type& radius() { return _radius; }
inline value_type radius() const { return _radius; }
inline value_type radius2() const { return _radius*_radius; }
template<typename vector_type>
void expandBy(const vector_type& v);
template<typename vector_type>
void expandRadiusBy(const vector_type& v);
void expandBy(const BoundingSphereImpl& sh);
void expandRadiusBy(const BoundingSphereImpl& sh);
template<typename BBT>
void expandBy(const BoundingBoxImpl<BBT>& bb);
template<typename BBT>
void expandRadiusBy(const BoundingBoxImpl<BBT>& bb);
inline bool contains(const vec_type& v) const
{
return valid() && ((v-_center).length2()<=radius2());
}
inline bool intersects( const BoundingSphereImpl& bs ) const
{
return valid() && bs.valid() &&
((_center - bs._center).length2() <= (_radius + bs._radius)*(_radius + bs._radius));
}
};
template<typename VT>
template<typename vector_type>
void BoundingSphereImpl<VT>::expandBy(const vector_type& v)
{
if (valid())
{
vec_type dv = vec_type(v)-_center;
value_type r = dv.length();
if (r>_radius)
{
value_type dr = (r-_radius)*0.5;
_center += dv*(dr/r);
_radius += dr;
} // else do nothing as vertex is within sphere.
}
else
{
_center = v;
_radius = 0.0;
}
}
template<typename VT>
template<typename vector_type>
void BoundingSphereImpl<VT>::expandRadiusBy(const vector_type& v)
{
if (valid())
{
value_type r = (vec_type(v)-_center).length();
if (r>_radius) _radius = r;
// else do nothing as vertex is within sphere.
}
else
{
_center = v;
_radius = 0.0;
}
}
template<typename VT>
void BoundingSphereImpl<VT>::expandBy(const BoundingSphereImpl& sh)
{
// ignore operation if incoming BoundingSphere is invalid.
if (!sh.valid()) return;
// This sphere is not set so use the inbound sphere
if (!valid())
{
_center = sh._center;
_radius = sh._radius;
return;
}
// Calculate d == The distance between the sphere centers
double d = ( _center - sh.center() ).length();
// New sphere is already inside this one
if ( d + sh.radius() <= _radius )
{
return;
}
// New sphere completely contains this one
if ( d + _radius <= sh.radius() )
{
_center = sh._center;
_radius = sh._radius;
return;
}
// Build a new sphere that completely contains the other two:
//
// The center point lies halfway along the line between the furthest
// points on the edges of the two spheres.
//
// Computing those two points is ugly - so we'll use similar triangles
double new_radius = (_radius + d + sh.radius() ) * 0.5;
double ratio = ( new_radius - _radius ) / d ;
_center[0] += ( sh.center()[0] - _center[0] ) * ratio;
_center[1] += ( sh.center()[1] - _center[1] ) * ratio;
_center[2] += ( sh.center()[2] - _center[2] ) * ratio;
_radius = new_radius;
}
template<typename VT>
void BoundingSphereImpl<VT>::expandRadiusBy(const BoundingSphereImpl& sh)
{
if (sh.valid())
{
if (valid())
{
value_type r = (sh._center-_center).length()+sh._radius;
if (r>_radius) _radius = r;
// else do nothing as vertex is within sphere.
}
else
{
_center = sh._center;
_radius = sh._radius;
}
}
}
template<typename VT>
template<typename BBT>
void BoundingSphereImpl<VT>::expandBy(const BoundingBoxImpl<BBT>& bb)
{
if (bb.valid())
{
if (valid())
{
BoundingBoxImpl<vec_type> newbb(bb);
for(unsigned int c=0;c<8;++c)
{
vec_type v = bb.corner(c)-_center; // get the direction vector from corner
v.normalize(); // normalise it.
v *= -_radius; // move the vector in the opposite direction distance radius.
v += _center; // move to absolute position.
newbb.expandBy(v); // add it into the new bounding box.
}
_center = newbb.center();
_radius = newbb.radius();
}
else
{
_center = bb.center();
_radius = bb.radius();
}
}
}
template<typename VT>
template<typename BBT>
void BoundingSphereImpl<VT>::expandRadiusBy(const BoundingBoxImpl<BBT>& bb)
{
if (bb.valid())
{
if (valid())
{
for(unsigned int c=0;c<8;++c)
{
expandRadiusBy(bb.corner(c));
}
}
else
{
_center = bb.center();
_radius = bb.radius();
}
}
}
typedef BoundingSphereImpl<Vec3f> BoundingSpheref;
typedef BoundingSphereImpl<Vec3d> BoundingSphered;
#ifdef OSG_USE_FLOAT_BOUNDINGSPHERE
typedef BoundingSpheref BoundingSphere;
#else
typedef BoundingSphered BoundingSphere;
#endif
}
#endif
Vec3d
Vec3f
osg::BoundingBoxImpl
Definition BoundingBox:34
osg::BoundingBoxImpl::center
const vec_type center() const
Definition BoundingBox:126
osg::BoundingBoxImpl::expandBy
void expandBy(const vec_type &v)
Definition BoundingBox:156
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::radius
value_type radius() const
Definition BoundingBox:132
osg::BoundingSphereImpl
Definition BoundingSphere:35
osg::BoundingSphereImpl::operator!=
bool operator!=(const BoundingSphereImpl &rhs) const
Definition BoundingSphere:73
osg::BoundingSphereImpl::init
void init()
Definition BoundingSphere:62
osg::BoundingSphereImpl::BoundingSphereImpl
BoundingSphereImpl(const BoundingSphereImpl &bs)
Definition BoundingSphere:50
osg::BoundingSphereImpl::radius2
value_type radius2() const
Definition BoundingSphere:96
osg::BoundingSphereImpl< Vec3f >::center
vec_type & center()
Definition BoundingSphere:83
osg::BoundingSphereImpl::intersects
bool intersects(const BoundingSphereImpl &bs) const
Definition BoundingSphere:139
osg::BoundingSphereImpl::operator==
bool operator==(const BoundingSphereImpl &rhs) const
Definition BoundingSphere:72
osg::BoundingSphereImpl::contains
bool contains(const vec_type &v) const
Definition BoundingSphere:131
osg::BoundingSphereImpl::expandBy
void expandBy(const vector_type &v)
Definition BoundingSphere:149
osg::BoundingSphereImpl::expandRadiusBy
void expandRadiusBy(const BoundingBoxImpl< BBT > &bb)
Definition BoundingSphere:289
osg::BoundingSphereImpl::center
const vec_type & center() const
Definition BoundingSphere:86
osg::BoundingSphereImpl::expandRadiusBy
void expandRadiusBy(const vector_type &v)
Definition BoundingSphere:171
osg::BoundingSphereImpl< Vec3f >::_radius
value_type _radius
Definition BoundingSphere:41
osg::BoundingSphereImpl< Vec3f >::_center
vec_type _center
Definition BoundingSphere:40
osg::BoundingSphereImpl::vec_type
VT vec_type
Definition BoundingSphere:37
osg::BoundingSphereImpl::value_type
VT::value_type value_type
Definition BoundingSphere:38
osg::BoundingSphereImpl::BoundingSphereImpl
BoundingSphereImpl(const BoundingBoxImpl< VT > &bb)
Definition BoundingSphere:53
osg::BoundingSphereImpl::expandBy
void expandBy(const BoundingBoxImpl< BBT > &bb)
Definition BoundingSphere:258
osg::BoundingSphereImpl::set
void set(const vec_type &center, value_type radius)
Definition BoundingSphere:76
osg::BoundingSphereImpl::operator=
BoundingSphereImpl & operator=(const BoundingSphereImpl &o)
Definition BoundingSphere:55
osg::BoundingSphereImpl::expandBy
void expandBy(const BoundingSphereImpl &sh)
Definition BoundingSphere:187
osg::BoundingSphereImpl::BoundingSphereImpl
BoundingSphereImpl(const vec_type &cntr, value_type rad)
Definition BoundingSphere:47
osg::BoundingSphereImpl::BoundingSphereImpl
BoundingSphereImpl()
Definition BoundingSphere:44
osg::BoundingSphereImpl::valid
bool valid() const
Definition BoundingSphere:70
osg::BoundingSphereImpl< Vec3f >::radius
value_type & radius()
Definition BoundingSphere:89
osg::BoundingSphereImpl::radius
value_type radius() const
Definition BoundingSphere:91
osg::BoundingSphereImpl::expandRadiusBy
void expandRadiusBy(const BoundingSphereImpl &sh)
Definition BoundingSphere:238
osg
Definition AlphaFunc:19
osg::BoundingSphere
BoundingSphered BoundingSphere
Definition BoundingSphere:314
osg::BoundingSpheref
BoundingSphereImpl< Vec3f > BoundingSpheref
Definition BoundingSphere:308
osg::BoundingSphered
BoundingSphereImpl< Vec3d > BoundingSphered
Definition BoundingSphere:309
Export