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 MemOctree
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
00065
00066 public:
00068 MemOctree() { init() ; }
00069
00071 ~MemOctree() { clear() ; }
00072
00073
00075 void init() ;
00076
00078 void clear() { clear_octree() ; _mc.clean_all() ; }
00079
00081 void clear_octree() ;
00082
00084 void compute_opt_level() ;
00085
00087 bool check () ;
00088
00090 void stats() const ;
00091
00093 Level max_level() const { Level lv = MAX_LEVEL ; while( lv > 0 && !_level_dist[lv] ) --lv ; return lv ; }
00094
00096 Level opt_level() const { return _opt_level ; }
00097
00099 Level &opt_level() { return _opt_level ; }
00100
00102 real max_field() const { return _max_field ; }
00103
00105 MarchingCubes &mc() { return _mc ; }
00106
00108 MC_Draw &mc_draw() { return _mc_draw ; }
00109
00111 bool set_impl( data_access *ref = NULL ) ;
00112
00114 bool refine( data_access *ref = NULL ) ;
00115
00117 bool adapt( data_access *ref = NULL ) ;
00118
00120 bool draw_wire() ;
00121
00123 bool draw_centers() ;
00124
00126 typedef bool mem_dual_walker( MemOctree &fo, Key *keys ) ;
00127
00129 bool dual_cubes_walk( mem_dual_walker &walker ) ;
00130
00132 bool build_isosurface( data_access *ref = NULL ) ;
00133
00135 bool direct_draw_isosurface( data_access *ref = NULL ) ;
00136
00138 bool draw_dual() ;
00139
00141 bool dual_timing() ;
00142
00143
00144
00145 public:
00147 inline cell_iterator cells_begin() { return cell_iterator( *this ) ; }
00148
00150 inline leaf_iterator leaves_begin() { return leaf_iterator( *this ) ; }
00151
00152
00153
00154 public:
00156 bool find_leaf( Key k, Level o_lv, geom_cell &cell ) const ;
00157 bool find_leaf( Key k, geom_cell &cell ) const { return find_leaf( k, opt_level(), cell ) ; }
00158 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 ) ; }
00159
00161 bool find_radius( real x, real y, real z, real r, List<geom_cell> &cells ) const ;
00162
00164 bool adjacent( Key k, List<geom_cell> &cells ) const ;
00165 bool adjacent( const geom_cell &cell, List<geom_cell> &cells ) const { return adjacent( cell.key(), cells ) ; }
00166
00168 bool is_leaf( const HashField::KeyData &kd ) const { return !is_inv( kd.data ) ; }
00169
00171 bool is_leaf( Key k, real &d ) const { const HashField::KeyData &kd = _hash[k] ; d = kd.data ; return is_leaf( kd ) ; }
00172
00174 bool is_leaf( Key k ) const { return is_leaf( _hash[k] ) ; }
00175
00177 bool node_exists( Key k, HashField::KeyData &kd ) const { kd = _hash[k] ; return kd.key != KEY_INV ; }
00178
00179
00180
00181 public:
00182
00184 void draw_plane ( real nx, real ny, real nz, real d ) ;
00185
00187 void draw_slice ( real nx, real ny, real nz, real d, float alpha ) ;
00188
00190 void draw_iso () { _mc.draw_surf() ; }
00191
00192
00193
00194 public :
00196 class geom_cell : public Cube
00197
00198 {
00199 friend class MemOctree ;
00200
00201 protected:
00202 Key _key ;
00203 real _field ;
00204
00205
00206
00207 public:
00209 geom_cell( Key key_ = KEY_INV, real field_ = R_INV ) : Cube(key2cube(key_)), _key(key_), _field(field_) {}
00210
00212 geom_cell( HashField::KeyData kd ) : Cube(key2cube(kd.key)), _key(kd.key), _field(kd.data) {}
00213
00215 ~geom_cell() {}
00216
00218 geom_cell( const geom_cell &i ) : Cube(i), _key(i._key), _field(i._field) {}
00219
00221 geom_cell &operator = ( const geom_cell &i )
00222 { Cube::operator=(i) ; _key = i._key ; _field = i._field ; return *this; }
00223
00224
00225
00226
00227 public :
00229 inline Key key() const { return _key ; }
00231 inline Key &key() { return _key ; }
00232
00234 inline real operator*() const { return _field ; }
00235
00236
00237
00238 public :
00240 inline bool operator ==( const geom_cell &i ) const { return key() == i.key() ; }
00241
00243 inline bool operator !=( const geom_cell &i ) const { return key() != i.key() ; }
00244
00246 inline bool is_leaf() const { return !is_inv(*(*this)) ; }
00247
00249 inline bool operator ()() const { return key() != KEY_INV ; }
00250
00251
00252
00253 public :
00255 inline bool sons( geom_cell *s , const HashField &hash )
00256 {
00257 if( is_leaf() ) return false ;
00258
00259 Key k = _key << 3 ;
00260 for( int i = 0 ; i < 8 ; ++i )
00261 {
00262 s[i]._key = k | i ;
00263 s[i]._field = hash[k|i].data ;
00264 (Cube&)s[i] = key2cube(s[i]._key) ;
00265 }
00266 return true ;
00267 }
00268 };
00269
00270 const geom_cell geom_root() const { const Key root_key = 1 ; return geom_cell( root_key, _hash[root_key].data ) ; }
00271 const geom_cell geom_key ( Key k ) const { return geom_cell( k, _hash[k].data ) ; }
00272
00273
00274
00275
00276
00277 public :
00279 class cell_iterator
00280
00281 {
00282 friend class MemOctree ;
00283
00284 protected:
00286 HashField::iterator _it ;
00287
00288
00289
00290 public:
00292 cell_iterator( MemOctree &o ) : _it( o._hash.begin() ) {}
00293
00295 ~cell_iterator() {}
00296
00298 cell_iterator( const cell_iterator &i ) : _it(i._it) {}
00299
00301 cell_iterator &operator = ( const cell_iterator &i )
00302 { _it = i._it; return *this; }
00303
00304
00305
00306 public :
00308 inline bool operator ==( const cell_iterator &i ) const { return _it == i._it ; }
00309
00311 inline bool operator !=( const cell_iterator &i ) const { return _it != i._it ; }
00312
00314 inline bool operator ()() const { return _it() ; }
00315
00317 inline cell_iterator &operator ++() { ++_it ; return *this ; }
00318
00319
00320
00321 public :
00322
00323 inline geom_cell top() const { return geom_cell( key(), *_it ) ; }
00324
00326 inline real &operator*() { return *_it ; }
00327
00329 inline Level lv() { return key_level(_it.key()) ; }
00330
00332 inline real sz() { return Cube(0,0,0,lv()).sz() ; }
00333
00335 inline Key key() const { return _it.key() ; }
00336
00338 inline bool is_leaf() const { return !is_inv(*_it) ; }
00339
00341 void draw_wire () const { top().draw_wire () ; }
00342
00343 };
00344
00345
00347 class leaf_iterator : public cell_iterator
00348
00349 {
00350 public :
00351 leaf_iterator( MemOctree &o ) : cell_iterator( o )
00352 { if( (*this)() && !this->is_leaf() ) ++(*this) ; }
00353
00354
00356 inline leaf_iterator &operator ++()
00357 {
00358 cell_iterator &it = *this ;
00359 do ++it ; while ( it() && !it.is_leaf() ) ;
00360 return *this ;
00361 }
00362 } ;
00363 } ;
00364
00365
00366
00367