OpenSceneGraph/include/osg/BoundingSphere

151 lines
5.8 KiB
Plaintext
Raw Normal View History

2006-07-18 23:21:48 +08:00
/* -*-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.
*/
2001-01-11 00:32:10 +08:00
#ifndef OSG_BOUNDINGSPHERE
#define OSG_BOUNDINGSPHERE 1
#include <osg/Config>
2001-01-11 00:32:10 +08:00
#include <osg/Export>
#include <osg/Vec3f>
#include <osg/Vec3d>
2001-01-11 00:32:10 +08:00
namespace osg {
class BoundingBox;
2001-01-11 00:32:10 +08:00
/** General purpose bounding sphere class for enclosing nodes/objects/vertices.
* Bounds internal osg::Nodes in the scene, assists in view frustum culling,
* etc. Similar in function to BoundingBox, it's quicker for evaluating
* culling but generally will not cull as aggressively because it encloses a
* greater volume.
2001-01-11 00:32:10 +08:00
*/
class OSG_EXPORT BoundingSphere
2001-01-11 00:32:10 +08:00
{
public:
#ifdef OSG_USE_FLOAT_BOUNDINGSPHERE
typedef Vec3f vec_type;
typedef float value_type;
#else
typedef Vec3d vec_type;
typedef double value_type;
#endif
2001-01-11 00:32:10 +08:00
vec_type _center;
value_type _radius;
2001-01-11 00:32:10 +08:00
/** Construct a default bounding sphere with radius to -1.0f, representing an invalid/unset bounding sphere.*/
BoundingSphere() : _center(0.0,0.0,0.0),_radius(-1.0) {}
2001-01-11 00:32:10 +08:00
/** Creates a bounding sphere initialized to the given extents. */
BoundingSphere(const vec_type& center,value_type radius) : _center(center),_radius(radius) {}
/** Creates a bounding sphere initialized to the given extents. */
BoundingSphere(const BoundingSphere& bs) : _center(bs._center),_radius(bs._radius) {}
/** Creates a bounding sphere initialized to the given extents. */
BoundingSphere(const BoundingBox& bb) : _center(0.0,0.0,0.0),_radius(-1.0) { expandBy(bb); }
/** Clear the bounding sphere. Reset to default values. */
inline void init()
2001-01-11 00:32:10 +08:00
{
_center.set(0.0,0.0,0.0);
_radius = -1.0;
2001-01-11 00:32:10 +08:00
}
/** Returns true of the bounding sphere extents are valid, false
* otherwise. */
inline bool valid() const { return _radius>=0.0; }
/** Set the bounding sphere to the given center/radius using floats. */
inline void set(const vec_type& center,value_type radius)
{
_center = center;
_radius = radius;
}
2001-01-11 00:32:10 +08:00
/** Returns the center of the bounding sphere. */
inline vec_type& center() { return _center; }
/** Returns the const center of the bounding sphere. */
inline const vec_type& center() const { return _center; }
2001-01-11 00:32:10 +08:00
/** Returns the radius of the bounding sphere. */
inline value_type& radius() { return _radius; }
/** Returns the const radius of the bounding sphere. */
inline value_type radius() const { return _radius; }
2001-01-11 00:32:10 +08:00
/** Returns the squared length of the radius. Note, For performance
* reasons, the calling method is responsible for checking to make
* sure the sphere is valid. */
inline value_type radius2() const { return _radius*_radius; }
/** Expands the sphere to encompass the given point. Repositions the
* sphere center to minimize the radius increase. If the sphere is
* uninitialized, set its center to v and radius to zero. */
void expandBy(const Vec3f& v);
/** Expands the sphere to encompass the given point. Does not
* reposition the sphere center. If the sphere is
* uninitialized, set its center to v and radius to zero. */
void expandRadiusBy(const Vec3f& v);
/** Expands the sphere to encompass the given point. Repositions the
* sphere center to minimize the radius increase. If the sphere is
* uninitialized, set its center to v and radius to zero. */
void expandBy(const Vec3d& v);
2001-01-11 00:32:10 +08:00
/** Expands the sphere to encompass the given point. Does not
* reposition the sphere center. If the sphere is
* uninitialized, set its center to v and radius to zero. */
void expandRadiusBy(const Vec3d& v);
2001-01-11 00:32:10 +08:00
/** Expands the sphere to encompass the given sphere. Repositions the
* sphere center to minimize the radius increase. If the sphere is
* uninitialized, set its center and radius to match sh. */
2001-01-11 00:32:10 +08:00
void expandBy(const BoundingSphere& sh);
/** Expands the sphere to encompass the given sphere. Does not
* repositions the sphere center. If the sphere is
* uninitialized, set its center and radius to match sh. */
2001-01-11 00:32:10 +08:00
void expandRadiusBy(const BoundingSphere& sh);
/** Expands the sphere to encompass the given box. Repositions the
* sphere center to minimize the radius increase. */
void expandBy(const BoundingBox& bb);
/** Expands the sphere to encompass the given box. Does not
* repositions the sphere center. */
void expandRadiusBy(const BoundingBox& bb);
/** Returns true if v is within the sphere. */
inline bool contains(const vec_type& v) const
2001-01-11 00:32:10 +08:00
{
return valid() && ((v-_center).length2()<=radius2());
2001-01-11 00:32:10 +08:00
}
/** Returns true if there is a non-empty intersection with the given
* bounding sphere. */
inline bool intersects( const BoundingSphere& bs ) const
{
return valid() && bs.valid() &&
((_center - bs._center).length2() <= (_radius + bs._radius)*(_radius + bs._radius));
}
2001-01-11 00:32:10 +08:00
};
}
2001-01-11 00:32:10 +08:00
#endif