Go to the documentation of this file.00001
00009
00010
00011
00012 #pragma once
00013
00014
00015 #ifndef WIN32
00016 #pragma interface
00017 #endif // WIN32
00018
00019
00020 #include "mlist.h"
00021 #include "cube.h"
00022 #include "hash.h"
00023 #include "MarchingCubes.h"
00024 #include "mc_draw.h"
00025 #include "data_access.h"
00026
00027
00028
00029
00030
00032 class OptOctree
00033
00034 {
00035
00036 public:
00037 class geom_cell ;
00038 class cell_iterator ;
00039 class leaf_iterator ;
00040
00041
00042 protected:
00044 typedef Hash<real> HashField ;
00045
00047 HashField _hash ;
00048
00050 uint _level_dist[MAX_LEVEL+1] ;
00051
00053 Level _opt_level ;
00054
00056 real _max_field ;
00057
00059 MarchingCubes _mc ;
00060
00062 MC_Draw _mc_draw ;
00063
00064 private :
00066 typedef Hash<Level> HashVerts ;
00068 HashVerts _verts ;
00069
00070
00071
00072 public:
00074 OptOctree() { init() ; }
00075
00077 ~OptOctree() { clear() ; }
00078
00079
00081 void init() ;
00082
00084 void clear() { clear_octree() ; _mc.clean_all() ; }
00085
00087 void clear_octree() ;
00088
00090 void compute_opt_level() ;
00091
00093 bool check () ;
00094
00096 void stats() const ;
00097
00099 Level max_level() const { Level lv = MAX_LEVEL ; while( lv > 0 && !_level_dist[lv] ) --lv ; return lv ; }
00100
00102 Level opt_level() const { return _opt_level ; }
00103
00105 Level &opt_level() { return _opt_level ; }
00106
00108 real max_field() const { return _max_field ; }
00109
00111 MarchingCubes &mc() { return _mc ; }
00112
00114 MC_Draw &mc_draw() { return _mc_draw ; }
00115
00117 bool set_impl( data_access *ref = NULL ) ;
00118
00120 bool refine( data_access *ref = NULL ) ;
00121
00123 bool adapt( data_access *ref = NULL ) ;
00124
00126 bool draw_wire() ;
00127
00129 bool draw_centers() ;
00130
00132 typedef bool opt_dual_walker( OptOctree &fo, Key *keys ) ;
00133
00135 bool compute_primal_verts() ;
00136
00138 bool dual_cubes_walk( opt_dual_walker &walker ) ;
00139
00141 bool build_isosurface( data_access *ref = NULL ) ;
00142
00144 bool direct_draw_isosurface( data_access *ref = NULL ) ;
00145
00147 bool draw_dual() ;
00148
00150 bool dual_timing() ;
00151
00152
00153
00154 public:
00156 inline cell_iterator cells_begin() { return cell_iterator( *this ) ; }
00157
00159 inline leaf_iterator leaves_begin() { return leaf_iterator( *this ) ; }
00160
00161
00162
00163 public:
00165 bool find_leaf( Key k, Level o_lv, geom_cell &cell ) const ;
00166 bool find_leaf( Key k, geom_cell &cell ) const { return find_leaf( k, opt_level(), cell ) ; }
00167 bool find_leaf( real x, real y, real z, geom_cell &cell ) const { return find_leaf( cube2key( Cube(x,y,z,MAX_LEVEL) ), opt_level(), cell ) ; }
00168
00170 bool find_radius( real x, real y, real z, real r, List<geom_cell> &cells ) const ;
00171
00173 bool adjacent( Key k, List<geom_cell> &cells ) const ;
00174 bool adjacent( const geom_cell &cell, List<geom_cell> &cells ) const { return adjacent( cell.key(), cells ) ; }
00175
00177 bool is_leaf( const HashField::KeyData &kd ) const { return !is_inv( kd.data ) ; }
00178
00180 bool is_leaf( Key k, real &d ) const { const HashField::KeyData &kd = _hash[k] ; d = kd.data ; return is_leaf( kd ) ; }
00181
00183 bool is_leaf( Key k ) const { return is_leaf( _hash[k] ) ; }
00184
00186 bool node_exists( Key k, HashField::KeyData &kd ) const { kd = _hash[k] ; return kd.key != KEY_INV ; }
00187
00188
00189
00190 public:
00191
00193 void draw_plane ( real nx, real ny, real nz, real d ) ;
00194
00196 void draw_slice ( real nx, real ny, real nz, real d, float alpha ) ;
00197
00199 void draw_iso () { _mc.draw_surf() ; }
00200
00201
00202
00203 public :
00205 class geom_cell : public Cube
00206
00207 {
00208 friend class OptOctree ;
00209
00210 protected:
00211 Key _key ;
00212 real _field ;
00213
00214
00215
00216 public:
00218 geom_cell( Key key_ = KEY_INV, real field_ = R_INV ) : Cube(key2cube(key_)), _key(key_), _field(field_) {}
00219
00221 geom_cell( HashField::KeyData kd ) : Cube(key2cube(kd.key)), _key(kd.key), _field(kd.data) {}
00222
00224 ~geom_cell() {}
00225
00227 geom_cell( const geom_cell &i ) : Cube(i), _key(i._key), _field(i._field) {}
00228
00230 geom_cell &operator = ( const geom_cell &i )
00231 { Cube::operator=(i) ; _key = i._key ; _field = i._field ; return *this; }
00232
00233
00234
00235
00236 public :
00238 inline Key key() const { return _key ; }
00240 inline Key &key() { return _key ; }
00241
00243 inline real operator*() const { return _field ; }
00244
00245
00246
00247 public :
00249 inline bool operator ==( const geom_cell &i ) const { return key() == i.key() ; }
00250
00252 inline bool operator !=( const geom_cell &i ) const { return key() != i.key() ; }
00253
00255 inline bool is_leaf() const { return !is_inv(*(*this)) ; }
00256
00258 inline bool operator ()() const { return key() != KEY_INV ; }
00259
00260
00261
00262 public :
00264 inline bool sons( geom_cell *s , const HashField &hash )
00265 {
00266 if( is_leaf() ) return false ;
00267
00268 Key k = _key << 3 ;
00269 for( int i = 0 ; i < 8 ; ++i )
00270 {
00271 s[i]._key = k | i ;
00272 s[i]._field = hash[k|i].data ;
00273 (Cube&)s[i] = key2cube(s[i]._key) ;
00274 }
00275 return true ;
00276 }
00277 };
00278
00279 const geom_cell geom_root() const { const Key root_key = 1 ; return geom_cell( root_key, _hash[root_key].data ) ; }
00280 const geom_cell geom_key ( Key k ) const { return geom_cell( k, _hash[k].data ) ; }
00281
00282
00283
00284
00285
00286 public :
00288 class cell_iterator
00289
00290 {
00291 friend class OptOctree ;
00292
00293 protected:
00295 HashField::iterator _it ;
00296
00297
00298
00299 public:
00301 cell_iterator( OptOctree &o ) : _it( o._hash.begin() ) {}
00302
00304 ~cell_iterator() {}
00305
00307 cell_iterator( const cell_iterator &i ) : _it(i._it) {}
00308
00310 cell_iterator &operator = ( const cell_iterator &i )
00311 { _it = i._it; return *this; }
00312
00313
00314
00315 public :
00317 inline bool operator ==( const cell_iterator &i ) const { return _it == i._it ; }
00318
00320 inline bool operator !=( const cell_iterator &i ) const { return _it != i._it ; }
00321
00323 inline bool operator ()() const { return _it() ; }
00324
00326 inline cell_iterator &operator ++() { ++_it ; return *this ; }
00327
00328
00329
00330 public :
00331
00332 inline geom_cell top() const { return geom_cell( key(), *_it ) ; }
00333
00335 inline real &operator*() { return *_it ; }
00336
00338 inline Level lv() { return key_level(_it.key()) ; }
00339
00341 inline real sz() { return Cube(0,0,0,lv()).sz() ; }
00342
00344 inline Key key() const { return _it.key() ; }
00345
00347 inline bool is_leaf() const { return !is_inv(*_it) ; }
00348
00350 void draw_wire () const { top().draw_wire () ; }
00351
00352 };
00353
00354
00356 class leaf_iterator : public cell_iterator
00357
00358 {
00359 public :
00360 leaf_iterator( OptOctree &o ) : cell_iterator( o )
00361 { if( (*this)() && !this->is_leaf() ) ++(*this) ; }
00362
00363
00365 inline leaf_iterator &operator ++()
00366 {
00367 cell_iterator &it = *this ;
00368 do ++it ; while ( it() && !it.is_leaf() ) ;
00369 return *this ;
00370 }
00371 } ;
00372 } ;
00373
00374
00375
00376