2005-04-15 05:41:28 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
|
2003-01-22 00:45:36 +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
|
|
|
|
* (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-10-04 23:12:57 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
#ifndef OSG_BOUNDINGSPHERE
|
|
|
|
#define OSG_BOUNDINGSPHERE 1
|
|
|
|
|
|
|
|
#include <osg/Export>
|
|
|
|
#include <osg/Vec3>
|
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
2002-06-20 00:06:03 +08:00
|
|
|
class BoundingBox;
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/** General purpose bounding sphere class for enclosing nodes/objects/vertices.
|
2004-09-07 18:07:11 +08:00
|
|
|
* 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
|
|
|
*/
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT BoundingSphere
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
Vec3 _center;
|
|
|
|
float _radius;
|
|
|
|
|
2004-09-07 18:07:11 +08:00
|
|
|
/** Construct a default bounding sphere with radius to -1.0f, representing an invalid/unset bounding sphere.*/
|
2001-01-11 00:32:10 +08:00
|
|
|
BoundingSphere() : _center(0.0f,0.0f,0.0f),_radius(-1.0f) {}
|
|
|
|
|
2004-09-07 18:07:11 +08:00
|
|
|
/** Creates a bounding sphere initialized to the given extents. */
|
2001-11-06 18:34:51 +08:00
|
|
|
BoundingSphere(const Vec3& center,float radius) : _center(center),_radius(radius) {}
|
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
/** 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.0f,0.0f,0.0f),_radius(-1.0f) { expandBy(bb); }
|
|
|
|
|
2004-09-07 18:07:11 +08:00
|
|
|
/** Clear the bounding sphere. Reset to default values. */
|
2001-09-20 05:08:56 +08:00
|
|
|
inline void init()
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
_center.set(0.0f,0.0f,0.0f);
|
|
|
|
_radius = -1.0f;
|
|
|
|
}
|
|
|
|
|
2004-09-07 18:07:11 +08:00
|
|
|
/** Returns true of the bounding sphere extents are valid, false
|
|
|
|
* otherwise. */
|
2002-09-02 20:31:35 +08:00
|
|
|
inline bool valid() const { return _radius>=0.0f; }
|
2002-06-20 00:06:03 +08:00
|
|
|
|
2004-09-07 18:07:11 +08:00
|
|
|
/** Set the bounding sphere to the given center/radius. */
|
2001-11-06 18:34:51 +08:00
|
|
|
inline void set(const Vec3& center,float radius)
|
|
|
|
{
|
|
|
|
_center = center;
|
|
|
|
_radius = radius;
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2004-09-07 18:07:11 +08:00
|
|
|
/** Returns the center of the bounding sphere. */
|
2001-09-20 05:08:56 +08:00
|
|
|
inline Vec3& center() { return _center; }
|
2004-09-07 18:07:11 +08:00
|
|
|
/** Returns the const center of the bounding sphere. */
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const Vec3& center() const { return _center; }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2004-09-07 18:07:11 +08:00
|
|
|
/** Returns the radius of the bounding sphere. */
|
2001-09-20 05:08:56 +08:00
|
|
|
inline float& radius() { return _radius; }
|
2004-09-07 18:07:11 +08:00
|
|
|
/** Returns the const radius of the bounding sphere. */
|
2002-09-02 20:31:35 +08:00
|
|
|
inline float radius() const { return _radius; }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2004-09-07 18:07:11 +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. */
|
2002-09-02 20:31:35 +08:00
|
|
|
inline float radius2() const { return _radius*_radius; }
|
2004-09-07 18:07:11 +08:00
|
|
|
|
|
|
|
/** 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. */
|
2001-01-11 00:32:10 +08:00
|
|
|
void expandBy(const Vec3& v);
|
|
|
|
|
2004-09-07 18:07:11 +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. */
|
2001-01-11 00:32:10 +08:00
|
|
|
void expandRadiusBy(const Vec3& v);
|
|
|
|
|
2004-09-07 18:07:11 +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);
|
|
|
|
|
2004-09-07 18:07:11 +08:00
|
|
|
/** 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);
|
|
|
|
|
2004-09-07 18:07:11 +08:00
|
|
|
/** Expands the sphere to encompass the given box. Repositions the
|
|
|
|
* sphere center to minimize the radius increase. */
|
2002-06-20 00:06:03 +08:00
|
|
|
void expandBy(const BoundingBox& bb);
|
|
|
|
|
2004-09-07 18:07:11 +08:00
|
|
|
/** Expands the sphere to encompass the given box. Does not
|
|
|
|
* repositions the sphere center. */
|
2002-06-20 00:06:03 +08:00
|
|
|
void expandRadiusBy(const BoundingBox& bb);
|
|
|
|
|
2004-09-07 18:07:11 +08:00
|
|
|
/** Returns true if v is within the sphere. */
|
2002-09-02 20:31:35 +08:00
|
|
|
inline bool contains(const Vec3& v) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-07-11 22:32:21 +08:00
|
|
|
return valid() && ((v-_center).length2()<=radius2());
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2004-09-07 18:07:11 +08:00
|
|
|
/** Returns true if there is a non-empty intersection with the given
|
|
|
|
* bounding sphere. */
|
2002-09-02 20:31:35 +08:00
|
|
|
inline bool intersects( const BoundingSphere& bs ) const
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2002-07-11 22:32:21 +08:00
|
|
|
return valid() && bs.valid() &&
|
2001-09-20 05:08:56 +08:00
|
|
|
((_center - bs._center).length2() <= (_radius + bs._radius)*(_radius + bs._radius));
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
};
|
|
|
|
|
2002-02-03 20:33:41 +08:00
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
#endif
|