1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
14#ifndef OSG_BOUNDINGSPHERE
15#define OSG_BOUNDINGSPHERE 1
27/** General purpose bounding sphere class for enclosing nodes/objects/vertices.
28 * Bounds internal osg::Nodes in the scene, assists in view frustum culling,
29 * etc. Similar in function to BoundingBox, it's quicker for evaluating
30 * culling but generally will not cull as aggressively because it encloses a
34class BoundingSphereImpl
38 typedef typename VT::value_type value_type;
43 /** Construct a default bounding sphere with radius to -1.0f, representing an invalid/unset bounding sphere.*/
44 BoundingSphereImpl() : _center(0.0,0.0,0.0),_radius(-1.0) {}
46 /** Creates a bounding sphere initialized to the given extents. */
47 BoundingSphereImpl(const vec_type& cntr, value_type rad) : _center(cntr),_radius(rad) {}
49 /** Creates a bounding sphere initialized to the given extents. */
50 BoundingSphereImpl(const BoundingSphereImpl& bs) : _center(bs._center),_radius(bs._radius) {}
52 /** Creates a bounding sphere initialized to the given extents. */
53 BoundingSphereImpl(const BoundingBoxImpl<VT>& bb) : _center(0.0,0.0,0.0),_radius(-1.0) { expandBy(bb); }
55 BoundingSphereImpl& operator= (const BoundingSphereImpl &o){
61 /** Clear the bounding sphere. Reset to default values. */
64 _center.set(0.0,0.0,0.0);
68 /** Returns true of the bounding sphere extents are valid, false
70 inline bool valid() const { return _radius>=0.0; }
72 inline bool operator == (const BoundingSphereImpl& rhs) const { return _center==rhs._center && _radius==rhs._radius; }
73 inline bool operator != (const BoundingSphereImpl& rhs) const { return _center!=rhs._center || _radius!=rhs._radius; }
75 /** Set the bounding sphere to the given center/radius using floats. */
76 inline void set(const vec_type& center,value_type radius)
82 /** Returns the center of the bounding sphere. */
83 inline vec_type& center() { return _center; }
85 /** Returns the const center of the bounding sphere. */
86 inline const vec_type& center() const { return _center; }
88 /** Returns the radius of the bounding sphere. */
89 inline value_type& radius() { return _radius; }
90 /** Returns the const radius of the bounding sphere. */
91 inline value_type radius() const { return _radius; }
93 /** Returns the squared length of the radius. Note, For performance
94 * reasons, the calling method is responsible for checking to make
95 * sure the sphere is valid. */
96 inline value_type radius2() const { return _radius*_radius; }
98 /** Expands the sphere to encompass the given point. Repositions the
99 * sphere center to minimize the radius increase. If the sphere is
100 * uninitialized, set its center to v and radius to zero. */
101 template<typename vector_type>
102 void expandBy(const vector_type& v);
104 /** Expands the sphere to encompass the given point. Does not
105 * reposition the sphere center. If the sphere is
106 * uninitialized, set its center to v and radius to zero. */
107 template<typename vector_type>
108 void expandRadiusBy(const vector_type& v);
110 /** Expands the sphere to encompass the given sphere. Repositions the
111 * sphere center to minimize the radius increase. If the sphere is
112 * uninitialized, set its center and radius to match sh. */
113 void expandBy(const BoundingSphereImpl& sh);
115 /** Expands the sphere to encompass the given sphere. Does not
116 * repositions the sphere center. If the sphere is
117 * uninitialized, set its center and radius to match sh. */
118 void expandRadiusBy(const BoundingSphereImpl& sh);
120 /** Expands the sphere to encompass the given box. Repositions the
121 * sphere center to minimize the radius increase. */
122 template<typename BBT>
123 void expandBy(const BoundingBoxImpl<BBT>& bb);
125 /** Expands the sphere to encompass the given box. Does not
126 * repositions the sphere center. */
127 template<typename BBT>
128 void expandRadiusBy(const BoundingBoxImpl<BBT>& bb);
130 /** Returns true if v is within the sphere. */
131 inline bool contains(const vec_type& v) const
133 return valid() && ((v-_center).length2()<=radius2());
137 /** Returns true if there is a non-empty intersection with the given
138 * bounding sphere. */
139 inline bool intersects( const BoundingSphereImpl& bs ) const
141 return valid() && bs.valid() &&
142 ((_center - bs._center).length2() <= (_radius + bs._radius)*(_radius + bs._radius));
148template<typename vector_type>
149void BoundingSphereImpl<VT>::expandBy(const vector_type& v)
153 vec_type dv = vec_type(v)-_center;
154 value_type r = dv.length();
157 value_type dr = (r-_radius)*0.5;
158 _center += dv*(dr/r);
160 } // else do nothing as vertex is within sphere.
170template<typename vector_type>
171void BoundingSphereImpl<VT>::expandRadiusBy(const vector_type& v)
175 value_type r = (vec_type(v)-_center).length();
176 if (r>_radius) _radius = r;
177 // else do nothing as vertex is within sphere.
187void BoundingSphereImpl<VT>::expandBy(const BoundingSphereImpl& sh)
189 // ignore operation if incoming BoundingSphere is invalid.
190 if (!sh.valid()) return;
192 // This sphere is not set so use the inbound sphere
195 _center = sh._center;
196 _radius = sh._radius;
202 // Calculate d == The distance between the sphere centers
203 double d = ( _center - sh.center() ).length();
205 // New sphere is already inside this one
206 if ( d + sh.radius() <= _radius )
211 // New sphere completely contains this one
212 if ( d + _radius <= sh.radius() )
214 _center = sh._center;
215 _radius = sh._radius;
220 // Build a new sphere that completely contains the other two:
222 // The center point lies halfway along the line between the furthest
223 // points on the edges of the two spheres.
225 // Computing those two points is ugly - so we'll use similar triangles
226 double new_radius = (_radius + d + sh.radius() ) * 0.5;
227 double ratio = ( new_radius - _radius ) / d ;
229 _center[0] += ( sh.center()[0] - _center[0] ) * ratio;
230 _center[1] += ( sh.center()[1] - _center[1] ) * ratio;
231 _center[2] += ( sh.center()[2] - _center[2] ) * ratio;
233 _radius = new_radius;
238void BoundingSphereImpl<VT>::expandRadiusBy(const BoundingSphereImpl& sh)
244 value_type r = (sh._center-_center).length()+sh._radius;
245 if (r>_radius) _radius = r;
246 // else do nothing as vertex is within sphere.
250 _center = sh._center;
251 _radius = sh._radius;
257template<typename BBT>
258void BoundingSphereImpl<VT>::expandBy(const BoundingBoxImpl<BBT>& bb)
264 BoundingBoxImpl<vec_type> newbb(bb);
266 for(unsigned int c=0;c<8;++c)
268 vec_type v = bb.corner(c)-_center; // get the direction vector from corner
269 v.normalize(); // normalise it.
270 v *= -_radius; // move the vector in the opposite direction distance radius.
271 v += _center; // move to absolute position.
272 newbb.expandBy(v); // add it into the new bounding box.
275 _center = newbb.center();
276 _radius = newbb.radius();
281 _center = bb.center();
282 _radius = bb.radius();
288template<typename BBT>
289void BoundingSphereImpl<VT>::expandRadiusBy(const BoundingBoxImpl<BBT>& bb)
295 for(unsigned int c=0;c<8;++c)
297 expandRadiusBy(bb.corner(c));
302 _center = bb.center();
303 _radius = bb.radius();
308typedef BoundingSphereImpl<Vec3f> BoundingSpheref;
309typedef BoundingSphereImpl<Vec3d> BoundingSphered;
311#ifdef OSG_USE_FLOAT_BOUNDINGSPHERE
312 typedef BoundingSpheref BoundingSphere;
314 typedef BoundingSphered BoundingSphere;