2002-07-17 04:07:32 +08:00
|
|
|
//C++ header - Open Scene Graph - Copyright (C) 1998-2002 Robert Osfield
|
2001-10-04 23:12:57 +08:00
|
|
|
//Distributed under the terms of the GNU Library General Public License (LGPL)
|
|
|
|
//as published by the Free Software Foundation.
|
|
|
|
|
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.
|
|
|
|
Used to bound internal osg::Node's in the scene,
|
2001-09-29 04:10:41 +08:00
|
|
|
to assist in view frustum culling etc. Similar in function to BoundingBox
|
2001-01-11 00:32:10 +08:00
|
|
|
but is quicker for evaluating culling, but generally encloses a greater volume
|
|
|
|
than a BoundingBox so will not cull so aggressively.
|
|
|
|
*/
|
|
|
|
class SG_EXPORT BoundingSphere
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
Vec3 _center;
|
|
|
|
float _radius;
|
|
|
|
|
|
|
|
/** construct to invalid values to represent an unset bounding sphere.*/
|
|
|
|
BoundingSphere() : _center(0.0f,0.0f,0.0f),_radius(-1.0f) {}
|
|
|
|
|
2001-11-06 18:34:51 +08:00
|
|
|
/** construct to specified bounding sphere.*/
|
|
|
|
BoundingSphere(const Vec3& center,float radius) : _center(center),_radius(radius) {}
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/** initialize to invalid values to represent an unset bounding sphere.*/
|
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;
|
|
|
|
}
|
|
|
|
|
2002-07-12 22:25:10 +08:00
|
|
|
#ifdef USE_DEPRECATED_API
|
2002-07-11 22:32:21 +08:00
|
|
|
/** deprecated, use valid() instead.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const bool isValid() const { return _radius>=0.0f; }
|
2002-07-12 22:25:10 +08:00
|
|
|
#endif
|
2002-06-20 00:06:03 +08:00
|
|
|
/** return true if the bounding sphere contains valid values,
|
|
|
|
false if the bounding sphere is effectively unset.*/
|
|
|
|
inline const bool valid() const { return _radius>=0.0f; }
|
|
|
|
|
2001-11-06 18:34:51 +08:00
|
|
|
/** set bounding sphere.*/
|
|
|
|
inline void set(const Vec3& center,float radius)
|
|
|
|
{
|
|
|
|
_center = center;
|
|
|
|
_radius = radius;
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
/** return the center of the bounding sphere.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline Vec3& center() { return _center; }
|
|
|
|
/** return the const center of the bounding sphere.*/
|
|
|
|
inline const Vec3& center() const { return _center; }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
/** return the radius of the bounding sphere.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline float& radius() { return _radius; }
|
|
|
|
|
|
|
|
/** return the const radius of the bounding sphere.*/
|
|
|
|
inline const float radius() const { return _radius; }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
/** return the radius squared.
|
|
|
|
Note, for performance reasons, assumes the calling method has ensured
|
|
|
|
that the sphere is valid before calling radius2(), i.e. has _radius>=0.0,
|
2001-09-29 04:10:41 +08:00
|
|
|
as it does not check th validity of sphere and will erroneously return a positive value.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const float radius2() const { return _radius*_radius; }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-29 04:10:41 +08:00
|
|
|
/** If the vertex is out-with the sphere expand to encompass vertex.
|
2001-01-11 00:32:10 +08:00
|
|
|
Calculates the combination of movement of center and radius which
|
|
|
|
minimizes the radius increase. If this sphere is empty then
|
2001-09-29 04:10:41 +08:00
|
|
|
move the center to v and set radius to 0.*/
|
2001-01-11 00:32:10 +08:00
|
|
|
void expandBy(const Vec3& v);
|
|
|
|
|
|
|
|
/** If the vertex is outwith the sphere expand radius to ecompass vertex.
|
|
|
|
Unlike update, does not move the center, just increasing the radius.
|
|
|
|
If this sphere is empty then move the centrer to v and set radius to 0 */
|
|
|
|
void expandRadiusBy(const Vec3& v);
|
|
|
|
|
|
|
|
/** If incomming sphere is outwith the sphere expand to ecompass incomming sphere.
|
|
|
|
calculates the combination of movement of center and radius which
|
|
|
|
minimizes the radius increase. If this sphere is empty then
|
|
|
|
move the centrer to v and set radius to 0.*/
|
|
|
|
void expandBy(const BoundingSphere& sh);
|
|
|
|
|
|
|
|
/** If incomming sphere is outwith the sphere expand radius to ecompass incomming sphere.
|
|
|
|
Unlike update, does not move the center, just increasing the radius.
|
|
|
|
If this sphere is empty then move the centrer to v and set radius to 0. */
|
|
|
|
void expandRadiusBy(const BoundingSphere& sh);
|
|
|
|
|
2002-06-20 00:06:03 +08:00
|
|
|
/** If incomming box is outwith the sphere expand to ecompass incomming box.
|
|
|
|
calculates the combination of movement of center and radius which
|
|
|
|
minimizes the radius increase. If this boz is empty then
|
|
|
|
move the centrer to v and set radius to 0.*/
|
|
|
|
void expandBy(const BoundingBox& bb);
|
|
|
|
|
|
|
|
/** If incomming box is outwith the sphere expand radius to ecompass incomming box.
|
|
|
|
Unlike update, does not move the center, just increasing the radius.
|
|
|
|
If this sphere is empty then move the centrer to v and set radius to 0. */
|
|
|
|
void expandRadiusBy(const BoundingBox& bb);
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/** return true is vertex v is within the sphere.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const 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
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
/** return true if bounding sphere's intersect each other.*/
|
|
|
|
inline const bool intersects( const BoundingSphere& bs ) const
|
|
|
|
{
|
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
|