/usr/include/osg/BoundingSphere is in libopenscenegraph-dev 3.2.3+dfsg1-2+b4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | /* -*-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;
/** 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.
*/
template<typename VT>
class BoundingSphereImpl
{
public:
typedef VT vec_type;
typedef typename VT::value_type value_type;
vec_type _center;
value_type _radius;
/** Construct a default bounding sphere with radius to -1.0f, representing an invalid/unset bounding sphere.*/
BoundingSphereImpl() : _center(0.0,0.0,0.0),_radius(-1.0) {}
/** Creates a bounding sphere initialized to the given extents. */
BoundingSphereImpl(const vec_type& center, value_type radius) : _center(center),_radius(radius) {}
/** Creates a bounding sphere initialized to the given extents. */
BoundingSphereImpl(const BoundingSphereImpl& bs) : _center(bs._center),_radius(bs._radius) {}
/** Creates a bounding sphere initialized to the given extents. */
BoundingSphereImpl(const BoundingBoxImpl<VT>& bb) : _center(0.0,0.0,0.0),_radius(-1.0) { expandBy(bb); }
/** Clear the bounding sphere. Reset to default values. */
inline void init()
{
_center.set(0.0,0.0,0.0);
_radius = -1.0;
}
/** Returns true of the bounding sphere extents are valid, false
* otherwise. */
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; }
/** 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;
}
/** 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; }
/** 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; }
/** 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. */
template<typename vector_type>
void expandBy(const vector_type& 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. */
template<typename vector_type>
void expandRadiusBy(const vector_type& v);
/** 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. */
void expandBy(const BoundingSphereImpl& 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. */
void expandRadiusBy(const BoundingSphereImpl& sh);
/** Expands the sphere to encompass the given box. Repositions the
* sphere center to minimize the radius increase. */
template<typename BBT>
void expandBy(const BoundingBoxImpl<BBT>& bb);
/** Expands the sphere to encompass the given box. Does not
* repositions the sphere center. */
template<typename BBT>
void expandRadiusBy(const BoundingBoxImpl<BBT>& bb);
/** Returns true if v is within the sphere. */
inline bool contains(const vec_type& v) const
{
return valid() && ((v-_center).length2()<=radius2());
}
/** Returns true if there is a non-empty intersection with the given
* bounding sphere. */
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 incomming 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
|