An Implementation of the Marching Cubes Algorithm  1.0
grid.cpp
Go to the documentation of this file.
00001 
00025 #include "grid.hpp"        // Grid
00026 
00027 #include "mathmacros.hpp"  // is_zero()
00028 
00029 #include <sstream>         // std::stringstream
00030 
00031 
00048 namespace mc {
00049 
00050 
00051   using common::ExceptionObject ;
00052   using common::t3DVector ;
00053 
00054 
00055   // -----------------------------------------------------------------
00056   //
00057   // Public methods
00058   //
00059   // -----------------------------------------------------------------
00060 
00072   Grid::Grid( 
00073              unsigned sx ,
00074              unsigned sy , 
00075              unsigned sz , 
00076              const t3DVector& og ,
00077              const t3DVector& sp 
00078             ) 
00079   :
00080     _sizex( sx ) ,
00081     _sizey( sy ) ,
00082     _sizez( sz ) ,
00083     _orig(  og ) ,
00084     _spac(  sp )
00085   {
00086     /*
00087      * Allocate  memory for  the function  values associated  with the
00088      * grid vertices.
00089      */
00090     _f = ( double*** ) new double**[ _sizex ] ;
00091     for ( unsigned i = 0 ; i < _sizex ; i++ ) {
00092       _f[ i ] = ( double** ) new double*[ _sizey ] ;
00093       for ( unsigned j = 0 ; j < _sizey ; j++ ) {
00094         _f[ i ][ j ] = ( double* ) new double[ _sizez ] ;
00095       }
00096     }   
00097   }
00098 
00099 
00107   Grid::Grid( const Grid& g ) 
00108   :
00109     _sizex( g._sizex ) ,
00110     _sizey( g._sizey ) ,
00111     _sizez( g._sizez ) ,
00112     _orig(   g._orig ) ,
00113     _spac(   g._spac )
00114   {
00115     /*
00116      * Allocate  memory for  the function  values associated  with the
00117      * grid vertices.
00118      */
00119     _f = ( double*** ) new double**[ _sizex ] ;
00120 
00121     _f = ( double*** ) new double**[ _sizex ] ;
00122     for ( unsigned i = 0 ; i < _sizex ; i++ ) {
00123       _f[ i ] = ( double** ) new double*[ _sizey ] ;
00124       for ( unsigned j = 0 ; j < _sizey ; j++ ) {
00125         _f[ i ][ j ] = ( double* ) new double[ _sizez ] ;
00126       }
00127     }
00128  
00129     for ( unsigned i = 0 ; i < _sizex ; i++ ) {
00130       for ( unsigned j = 0 ; j < _sizey ; j++ ) {
00131         for ( unsigned k = 0 ; k < _sizez ; k++ ) {
00132           _f[ i ][ j ][ k ] = g._f[ i ][ j ][ k ] ;
00133         }
00134       }
00135     }
00136   }
00137 
00138 
00144   Grid::~Grid()
00145   {
00146     for ( unsigned i = 0 ; i < _sizex ; i++ ) {
00147       for ( unsigned j = 0 ; j < _sizey ; j++ ) {
00148         if ( _f[ i ][ j ] ) {
00149           delete[] _f[ i ][ j ] ;
00150         }
00151       }
00152       
00153       if ( _f[ i ] ) {
00154         delete[] _f[ i ] ;
00155       }
00156     }
00157   
00158     _f = 0 ;
00159   }
00160 
00161 
00171   double
00172   Grid::get_value(
00173                   unsigned i ,
00174                   unsigned j ,
00175                   unsigned k 
00176                  ) 
00177     const throw ( ExceptionObject )
00178   {
00179     if ( ( i > _sizex ) || ( j > _sizey ) || ( k > _sizez ) ) {
00180       std::stringstream ss (
00181                             std::stringstream::in | 
00182                             std::stringstream::out
00183                            ) ;
00184       ss << "Vertex index is out of range" ;
00185       throw ExceptionObject( __FILE__ , __LINE__ , ss.str() ) ;
00186     }
00187   
00188     return _f[ i ][ j ][ k ] ;
00189   }
00190 
00191 
00204   void
00205   Grid::set_value(
00206                   unsigned i ,
00207                   unsigned j ,
00208                   unsigned k ,
00209                   double f 
00210                  ) 
00211     throw ( ExceptionObject )
00212   {
00213     if ( ( i > _sizex ) || ( j > _sizey ) || ( k > _sizez ) ) {
00214       std::stringstream ss (
00215                             std::stringstream::in | 
00216                             std::stringstream::out
00217                            ) ;
00218       ss << "Vertex index is out of range" ;
00219       throw ExceptionObject( __FILE__ , __LINE__ , ss.str() ) ;
00220     }
00221   
00222     if ( common::is_zero( f ) ) {
00223       std::stringstream ss (
00224                             std::stringstream::in | 
00225                             std::stringstream::out
00226                            ) ;
00227       ss << "Vertex grid cannot be assigned the value 0" ;
00228       throw ExceptionObject( __FILE__ , __LINE__ , ss.str() ) ;
00229     }
00230   
00231     _f[ i ][ j ][ k ] = f ;
00232   }
00233 
00234 }         // end of namespace mc
00235  //end of group class.