00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef WBOUNDINGBOX_H
00026 #define WBOUNDINGBOX_H
00027
00028 #include <ostream>
00029
00030 #include <osg/BoundingBox>
00031
00032 #include "exceptions/WInvalidBoundingBox.h"
00033 #include "math/linearAlgebra/WLinearAlgebra.h"
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 template< class VT >
00045 class WBoundingBoxImpl : private osg::BoundingBoxImpl< VT >
00046 {
00047 public:
00048
00049
00050
00051 typedef typename osg::BoundingBoxImpl< VT >::vec_type vec_type;
00052
00053
00054
00055
00056 typedef typename osg::BoundingBoxImpl< VT >::value_type value_type;
00057
00058
00059
00060
00061 WBoundingBoxImpl();
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 WBoundingBoxImpl( value_type xmin, value_type ymin, value_type zmin, value_type xmax, value_type ymax, value_type zmax );
00074
00075
00076
00077
00078
00079
00080
00081 WBoundingBoxImpl( const vec_type& min, const vec_type& max );
00082
00083
00084
00085
00086 virtual ~WBoundingBoxImpl();
00087
00088
00089
00090
00091
00092
00093 void reset();
00094
00095 using osg::BoundingBoxImpl< VT >::valid;
00096 using osg::BoundingBoxImpl< VT >::set;
00097 using osg::BoundingBoxImpl< VT >::xMin;
00098 using osg::BoundingBoxImpl< VT >::yMin;
00099 using osg::BoundingBoxImpl< VT >::zMin;
00100 using osg::BoundingBoxImpl< VT >::xMax;
00101 using osg::BoundingBoxImpl< VT >::yMax;
00102 using osg::BoundingBoxImpl< VT >::zMax;
00103 using osg::BoundingBoxImpl< VT >::center;
00104 using osg::BoundingBoxImpl< VT >::radius;
00105
00106
00107
00108
00109
00110
00111
00112
00113 value_type radiusSquare() const;
00114
00115 using osg::BoundingBoxImpl< VT >::corner;
00116
00117
00118
00119
00120
00121
00122 osg::BoundingBox toOSGBB() const;
00123
00124 using osg::BoundingBoxImpl< VT >::expandBy;
00125
00126
00127
00128
00129
00130
00131 void expandBy( const WBoundingBoxImpl< VT > &bb );
00132
00133
00134
00135
00136
00137
00138
00139
00140 bool intersects( const WBoundingBoxImpl< VT > &bb ) const;
00141
00142
00143
00144
00145
00146
00147
00148
00149 value_type minDistance( const WBoundingBoxImpl< VT > &bb ) const;
00150
00151 using osg::BoundingBoxImpl< VT >::contains;
00152
00153
00154
00155
00156
00157
00158 const vec_type& getMin() const;
00159
00160
00161
00162
00163
00164
00165 const vec_type& getMax() const;
00166
00167 protected:
00168 private:
00169 };
00170
00171 template< class VT >
00172 inline WBoundingBoxImpl< VT >::WBoundingBoxImpl()
00173 : osg::BoundingBoxImpl< VT >()
00174 {
00175 }
00176
00177 template< class VT >
00178 inline WBoundingBoxImpl< VT >::WBoundingBoxImpl( value_type xmin, value_type ymin, value_type zmin, value_type xmax, value_type ymax, value_type zmax )
00179 : osg::BoundingBoxImpl< VT >( xmin, ymin, zmin, xmax, ymax, zmax )
00180 {
00181 }
00182
00183 template< class VT >
00184 inline WBoundingBoxImpl< VT >::WBoundingBoxImpl( const vec_type& min, const vec_type& max )
00185 : osg::BoundingBoxImpl< VT >( min, max )
00186 {
00187 }
00188
00189 template< class VT >
00190 inline WBoundingBoxImpl< VT >::~WBoundingBoxImpl()
00191 {
00192 }
00193
00194 template< class VT >
00195 inline void WBoundingBoxImpl< VT >::reset()
00196 {
00197 this->init();
00198 }
00199
00200 template< class VT >
00201 inline typename WBoundingBoxImpl< VT >::value_type WBoundingBoxImpl< VT >::radiusSquare() const
00202 {
00203 return this->raidus2();
00204 }
00205
00206 template< class VT >
00207 inline osg::BoundingBox WBoundingBoxImpl< VT >::toOSGBB() const
00208 {
00209 return osg::BoundingBox( osg::BoundingBoxImpl< VT >::_min, osg::BoundingBoxImpl< VT >::_max );
00210 }
00211
00212 template< class VT >
00213 inline void WBoundingBoxImpl< VT >::expandBy( const WBoundingBoxImpl< VT > &bb )
00214 {
00215 osg::BoundingBoxImpl< VT >::expandBy( bb );
00216 }
00217
00218 template< class VT >
00219 inline bool WBoundingBoxImpl< VT >::intersects( const WBoundingBoxImpl< VT > &bb ) const
00220 {
00221 return osg::BoundingBoxImpl< VT >::intersects( bb );
00222 }
00223
00224
00225
00226
00227 namespace
00228 {
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 inline double intervalDistance( double a0, double a1, double b0, double b1 )
00240 {
00241 if( a1 < b0 )
00242 {
00243 return b0 - a1;
00244 }
00245 else if( b1 < a0 )
00246 {
00247 return a0 - b1;
00248 }
00249 return 0.0;
00250 }
00251 }
00252
00253 template< class VT >
00254 inline typename WBoundingBoxImpl< VT >::value_type WBoundingBoxImpl< VT >::minDistance( const WBoundingBoxImpl< VT > &bb ) const
00255 {
00256
00257 if( !valid() || !bb.valid() )
00258 {
00259 throw WInvalidBoundingBox( "One of the both bounding boxes inside minDistance computation is not valid." );
00260 }
00261
00262 double dx = intervalDistance( xMin(), xMax(), bb.xMin(), bb.xMax() );
00263 double dy = intervalDistance( yMin(), yMax(), bb.yMin(), bb.yMax() );
00264 double dz = intervalDistance( zMin(), zMax(), bb.zMin(), bb.zMax() );
00265 if( dx == 0.0 && dy == 0.0 && dz == 0.0 )
00266 {
00267 return 0.0;
00268 }
00269 return std::sqrt( dx * dx + dy * dy + dz * dz );
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 template< class VT >
00281 inline std::ostream& operator<<( std::ostream& out, const WBoundingBoxImpl< VT >& bb )
00282 {
00283 out << std::scientific << std::setprecision( 16 );
00284 out << "AABB( min: " << bb.xMin() << ", " << bb.yMin() << ", " << bb.zMin();
00285 out << " max: " << bb.xMax() << ", " << bb.yMax() << ", " << bb.zMax() << " )";
00286 return out;
00287 }
00288
00289 template< class VT >
00290 inline const typename WBoundingBoxImpl< VT >::vec_type& WBoundingBoxImpl< VT >::getMin() const
00291 {
00292 return osg::BoundingBoxImpl< VT >::_min;
00293 }
00294
00295 template< class VT >
00296 inline const typename WBoundingBoxImpl< VT >::vec_type& WBoundingBoxImpl< VT >::getMax() const
00297 {
00298 return osg::BoundingBoxImpl< VT >::_max;
00299 }
00300
00301 typedef WBoundingBoxImpl< osg::Vec3 > WBoundingBox;
00302
00303 #endif // WBOUNDINGBOX_H