Changeset 9268
- Timestamp:
- 11/26/08 17:39:52 (3 years ago)
- Location:
- OpenSceneGraph/trunk
- Files:
-
- 2 removed
- 3 modified
-
include/osg/BoundingBox (modified) (8 diffs)
-
include/osg/BoundingSphere (modified) (4 diffs)
-
src/osg/BoundingBox.cpp (deleted)
-
src/osg/BoundingSphere.cpp (deleted)
-
src/osg/CMakeLists.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/include/osg/BoundingBox
r8477 r9268 23 23 namespace osg { 24 24 25 class BoundingSphere; 25 template<typename VT> 26 class BoundingSphereImpl; 26 27 27 28 /** General purpose axis-aligned bounding box class for enclosing objects/vertices. … … 29 30 * culling etc. 30 31 */ 31 class OSG_EXPORT BoundingBox 32 template<typename VT> 33 class BoundingBoxImpl 32 34 { 33 35 public: 34 35 #ifdef OSG_USE_FLOAT_BOUNDINGBOX 36 typedef Vec3f vec_type; 37 typedef float value_type; 38 #else 39 typedef Vec3d vec_type; 40 typedef double value_type; 41 #endif 42 36 typedef VT vec_type; 37 typedef typename VT::value_type value_type; 43 38 44 39 /** Minimum extent. (Smallest X, Y, and Z values of all coordinates.) */ … … 48 43 49 44 /** Creates an uninitialized bounding box. */ 50 inline BoundingBox() : _min(FLT_MAX,FLT_MAX,FLT_MAX), 51 _max(-FLT_MAX,-FLT_MAX,-FLT_MAX) {} 45 inline BoundingBoxImpl() : 46 _min(FLT_MAX, 47 FLT_MAX, 48 FLT_MAX), 49 _max(-FLT_MAX, 50 -FLT_MAX, 51 -FLT_MAX) 52 {} 52 53 53 54 /** Creates a bounding box initialized to the given extents. */ 54 inline BoundingBox (value_type xmin, value_type ymin, value_type zmin,55 inline BoundingBoxImpl(value_type xmin, value_type ymin, value_type zmin, 55 56 value_type xmax, value_type ymax, value_type zmax) : 56 57 _min(xmin,ymin,zmin), … … 58 59 59 60 /** Creates a bounding box initialized to the given extents. */ 60 inline BoundingBox (const vec_type& min,const vec_type& max) :61 inline BoundingBoxImpl(const vec_type& min,const vec_type& max) : 61 62 _min(min), 62 63 _max(max) {} … … 65 66 inline void init() 66 67 { 67 _min.set(FLT_MAX,FLT_MAX,FLT_MAX); 68 _max.set(-FLT_MAX,-FLT_MAX,-FLT_MAX); 68 _min.set(FLT_MAX, 69 FLT_MAX, 70 FLT_MAX); 71 _max.set(-FLT_MAX, 72 -FLT_MAX, 73 -FLT_MAX); 69 74 } 70 75 … … 169 174 /** Expands this bounding box to include the given bounding box. 170 175 * If this box is uninitialized, set it equal to bb. */ 171 void expandBy(const BoundingBox & bb);176 void expandBy(const BoundingBoxImpl& bb); 172 177 173 178 /** Expands this bounding box to include the given sphere. 174 179 * If this box is uninitialized, set it to include sh. */ 175 void expandBy(const BoundingSphere & sh);180 void expandBy(const BoundingSphereImpl<VT>& sh); 176 181 177 182 178 183 /** Returns the intersection of this bounding box and the specified bounding box. */ 179 BoundingBox intersect(const BoundingBox& bb) const180 { return osg::BoundingBox(osg::maximum(xMin(),bb.xMin()),osg::maximum(yMin(),bb.yMin()),osg::maximum(zMin(),bb.zMin()),184 BoundingBoxImpl intersect(const BoundingBoxImpl& bb) const 185 { return BoundingBoxImpl(osg::maximum(xMin(),bb.xMin()),osg::maximum(yMin(),bb.yMin()),osg::maximum(zMin(),bb.zMin()), 181 186 osg::minimum(xMax(),bb.xMax()),osg::minimum(yMax(),bb.yMax()),osg::minimum(zMax(),bb.zMax())); 182 187 … … 184 189 185 190 /** Return true if this bounding box intersects the specified bounding box. */ 186 bool intersects(const BoundingBox & bb) const191 bool intersects(const BoundingBoxImpl& bb) const 187 192 { return osg::maximum(xMin(),bb.xMin()) <= osg::minimum(xMax(),bb.xMax()) && 188 193 osg::maximum(yMin(),bb.yMin()) <= osg::minimum(yMax(),bb.yMax()) && … … 201 206 }; 202 207 208 template<typename VT> 209 void BoundingBoxImpl<VT>::expandBy(const BoundingBoxImpl<VT>& bb) 210 { 211 if (!bb.valid()) return; 212 213 if(bb._min.x()<_min.x()) _min.x() = bb._min.x(); 214 if(bb._max.x()>_max.x()) _max.x() = bb._max.x(); 215 216 if(bb._min.y()<_min.y()) _min.y() = bb._min.y(); 217 if(bb._max.y()>_max.y()) _max.y() = bb._max.y(); 218 219 if(bb._min.z()<_min.z()) _min.z() = bb._min.z(); 220 if(bb._max.z()>_max.z()) _max.z() = bb._max.z(); 203 221 } 204 222 223 template<typename VT> 224 void BoundingBoxImpl<VT>::expandBy(const BoundingSphereImpl<VT>& sh) 225 { 226 if (!sh.valid()) return; 227 228 if(sh._center.x()-sh._radius<_min.x()) _min.x() = sh._center.x()-sh._radius; 229 if(sh._center.x()+sh._radius>_max.x()) _max.x() = sh._center.x()+sh._radius; 230 231 if(sh._center.y()-sh._radius<_min.y()) _min.y() = sh._center.y()-sh._radius; 232 if(sh._center.y()+sh._radius>_max.y()) _max.y() = sh._center.y()+sh._radius; 233 234 if(sh._center.z()-sh._radius<_min.z()) _min.z() = sh._center.z()-sh._radius; 235 if(sh._center.z()+sh._radius>_max.z()) _max.z() = sh._center.z()+sh._radius; 236 } 237 238 typedef BoundingBoxImpl<Vec3f> BoundingBoxf; 239 typedef BoundingBoxImpl<Vec3d> BoundingBoxd; 240 241 #ifdef OSG_USE_FLOAT_BOUNDINGBOX 242 typedef BoundingBoxf BoundingBox; 243 #else 244 typedef BoundingBoxd BoundingBox; 205 245 #endif 246 247 } 248 249 #endif -
OpenSceneGraph/trunk/include/osg/BoundingSphere
r8477 r9268 22 22 namespace osg { 23 23 24 class BoundingBox; 24 template<typename VT> 25 class BoundingBoxImpl; 25 26 26 27 /** General purpose bounding sphere class for enclosing nodes/objects/vertices. … … 30 31 * greater volume. 31 32 */ 32 class OSG_EXPORT BoundingSphere 33 template<typename VT> 34 class BoundingSphereImpl 33 35 { 34 36 public: 35 36 #ifdef OSG_USE_FLOAT_BOUNDINGSPHERE 37 typedef Vec3f vec_type; 38 typedef float value_type; 39 #else 40 typedef Vec3d vec_type; 41 typedef double value_type; 42 #endif 43 37 typedef VT vec_type; 38 typedef typename vec_type::value_type value_type; 39 44 40 vec_type _center; 45 41 value_type _radius; 46 42 47 43 /** Construct a default bounding sphere with radius to -1.0f, representing an invalid/unset bounding sphere.*/ 48 BoundingSphere () : _center(0.0,0.0,0.0),_radius(-1.0) {}44 BoundingSphereImpl() : _center(0.0,0.0,0.0),_radius(-1.0) {} 49 45 50 46 /** Creates a bounding sphere initialized to the given extents. */ 51 BoundingSphere (const vec_type& center,value_type radius) : _center(center),_radius(radius) {}47 BoundingSphereImpl(const vec_type& center,value_type radius) : _center(center),_radius(radius) {} 52 48 53 49 /** Creates a bounding sphere initialized to the given extents. */ 54 BoundingSphere (const BoundingSphere& bs) : _center(bs._center),_radius(bs._radius) {}50 BoundingSphereImpl(const BoundingSphereImpl& bs) : _center(bs._center),_radius(bs._radius) {} 55 51 56 52 /** Creates a bounding sphere initialized to the given extents. */ 57 BoundingSphere (const BoundingBox& bb) : _center(0.0,0.0,0.0),_radius(-1.0) { expandBy(bb); }53 BoundingSphereImpl(const BoundingBoxImpl<VT>& bb) : _center(0.0,0.0,0.0),_radius(-1.0) { expandBy(bb); } 58 54 59 55 /** Clear the bounding sphere. Reset to default values. */ … … 94 90 * sphere center to minimize the radius increase. If the sphere is 95 91 * uninitialized, set its center to v and radius to zero. */ 96 void expandBy(const Vec3f& v); 92 template<typename vector_type> 93 void expandBy(const vector_type& v); 97 94 98 95 /** Expands the sphere to encompass the given point. Does not 99 96 * reposition the sphere center. If the sphere is 100 97 * uninitialized, set its center to v and radius to zero. */ 101 void expandRadiusBy(const Vec3f& v); 102 103 /** Expands the sphere to encompass the given point. Repositions the 104 * sphere center to minimize the radius increase. If the sphere is 105 * uninitialized, set its center to v and radius to zero. */ 106 void expandBy(const Vec3d& v); 107 108 /** Expands the sphere to encompass the given point. Does not 109 * reposition the sphere center. If the sphere is 110 * uninitialized, set its center to v and radius to zero. */ 111 void expandRadiusBy(const Vec3d& v); 98 template<typename vector_type> 99 void expandRadiusBy(const vector_type& v); 112 100 113 101 /** Expands the sphere to encompass the given sphere. Repositions the 114 102 * sphere center to minimize the radius increase. If the sphere is 115 103 * uninitialized, set its center and radius to match sh. */ 116 void expandBy(const BoundingSphere & sh);104 void expandBy(const BoundingSphereImpl& sh); 117 105 118 106 /** Expands the sphere to encompass the given sphere. Does not 119 107 * repositions the sphere center. If the sphere is 120 108 * uninitialized, set its center and radius to match sh. */ 121 void expandRadiusBy(const BoundingSphere & sh);109 void expandRadiusBy(const BoundingSphereImpl& sh); 122 110 123 111 /** Expands the sphere to encompass the given box. Repositions the 124 112 * sphere center to minimize the radius increase. */ 125 void expandBy(const BoundingBox & bb);113 void expandBy(const BoundingBoxImpl<VT>& bb); 126 114 127 115 /** Expands the sphere to encompass the given box. Does not 128 116 * repositions the sphere center. */ 129 void expandRadiusBy(const BoundingBox & bb);117 void expandRadiusBy(const BoundingBoxImpl<VT>& bb); 130 118 131 119 /** Returns true if v is within the sphere. */ … … 138 126 /** Returns true if there is a non-empty intersection with the given 139 127 * bounding sphere. */ 140 inline bool intersects( const BoundingSphere & bs ) const128 inline bool intersects( const BoundingSphereImpl& bs ) const 141 129 { 142 130 return valid() && bs.valid() && 143 131 ((_center - bs._center).length2() <= (_radius + bs._radius)*(_radius + bs._radius)); 144 132 } 145 133 146 134 }; 147 135 148 } 149 136 137 template<typename VT> 138 template<typename vector_type> 139 void BoundingSphereImpl<VT>::expandBy(const vector_type& v) 140 { 141 if (valid()) 142 { 143 vec_type dv = v-_center; 144 value_type r = dv.length(); 145 if (r>_radius) 146 { 147 value_type dr = (r-_radius)*0.5; 148 _center += dv*(dr/r); 149 _radius += dr; 150 } // else do nothing as vertex is within sphere. 151 } 152 else 153 { 154 _center = v; 155 _radius = 0.0; 156 } 157 } 158 159 template<typename VT> 160 template<typename vector_type> 161 void BoundingSphereImpl<VT>::expandRadiusBy(const vector_type& v) 162 { 163 if (valid()) 164 { 165 value_type r = (v-_center).length(); 166 if (r>_radius) _radius = r; 167 // else do nothing as vertex is within sphere. 168 } 169 else 170 { 171 _center = v; 172 _radius = 0.0; 173 } 174 } 175 176 template<typename VT> 177 void BoundingSphereImpl<VT>::expandBy(const BoundingSphereImpl& sh) 178 { 179 // ignore operation if incomming BoundingSphere is invalid. 180 if (!sh.valid()) return; 181 182 // This sphere is not set so use the inbound sphere 183 if (!valid()) 184 { 185 _center = sh._center; 186 _radius = sh._radius; 187 188 return; 189 } 190 191 192 // Calculate d == The distance between the sphere centers 193 double d = ( _center - sh.center() ).length(); 194 195 // New sphere is already inside this one 196 if ( d + sh.radius() <= _radius ) 197 { 198 return; 199 } 200 201 // New sphere completely contains this one 202 if ( d + _radius <= sh.radius() ) 203 { 204 _center = sh._center; 205 _radius = sh._radius; 206 return; 207 } 208 209 210 // Build a new sphere that completely contains the other two: 211 // 212 // The center point lies halfway along the line between the furthest 213 // points on the edges of the two spheres. 214 // 215 // Computing those two points is ugly - so we'll use similar triangles 216 double new_radius = (_radius + d + sh.radius() ) * 0.5; 217 double ratio = ( new_radius - _radius ) / d ; 218 219 _center[0] += ( sh.center()[0] - _center[0] ) * ratio; 220 _center[1] += ( sh.center()[1] - _center[1] ) * ratio; 221 _center[2] += ( sh.center()[2] - _center[2] ) * ratio; 222 223 _radius = new_radius; 224 225 } 226 227 template<typename VT> 228 void BoundingSphereImpl<VT>::expandRadiusBy(const BoundingSphereImpl& sh) 229 { 230 if (sh.valid()) 231 { 232 if (valid()) 233 { 234 value_type r = (sh._center-_center).length()+sh._radius; 235 if (r>_radius) _radius = r; 236 // else do nothing as vertex is within sphere. 237 } 238 else 239 { 240 _center = sh._center; 241 _radius = sh._radius; 242 } 243 } 244 } 245 246 template<typename VT> 247 void BoundingSphereImpl<VT>::expandBy(const BoundingBoxImpl<VT>& bb) 248 { 249 if (bb.valid()) 250 { 251 if (valid()) 252 { 253 BoundingBoxImpl<vec_type> newbb(bb); 254 255 for(unsigned int c=0;c<8;++c) 256 { 257 vec_type v = bb.corner(c)-_center; // get the direction vector from corner 258 v.normalize(); // normalise it. 259 v *= -_radius; // move the vector in the opposite direction distance radius. 260 v += _center; // move to absolute position. 261 newbb.expandBy(v); // add it into the new bounding box. 262 } 263 264 _center = newbb.center(); 265 _radius = newbb.radius(); 266 267 } 268 else 269 { 270 _center = bb.center(); 271 _radius = bb.radius(); 272 } 273 } 274 } 275 276 template<typename VT> 277 void BoundingSphereImpl<VT>::expandRadiusBy(const BoundingBoxImpl<VT>& bb) 278 { 279 if (bb.valid()) 280 { 281 if (valid()) 282 { 283 for(unsigned int c=0;c<8;++c) 284 { 285 expandRadiusBy(bb.corner(c)); 286 } 287 } 288 else 289 { 290 _center = bb.center(); 291 _radius = bb.radius(); 292 } 293 } 294 } 295 296 typedef BoundingSphereImpl<Vec3f> BoundingSpheref; 297 typedef BoundingSphereImpl<Vec3d> BoundingSphered; 298 299 #ifdef OSG_USE_FLOAT_BOUNDINGSPHERE 300 typedef BoundingSpheref BoundingSphere; 301 #else 302 typedef BoundingSphered BoundingSphere; 150 303 #endif 304 } 305 306 #endif -
OpenSceneGraph/trunk/src/osg/CMakeLists.txt
r9218 r9268 198 198 BlendEquation.cpp 199 199 BlendFunc.cpp 200 BoundingBox.cpp201 BoundingSphere.cpp202 200 BufferObject.cpp 203 201 Camera.cpp
