volume.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   wxMaracas
00004   Module:    $RCSfile: volume.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2010/03/15 14:12:30 $
00007   Version:   $Revision: 1.6 $
00008 
00009   Copyright: (c) 2002, 2003
00010   License:
00011   
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notice for more information.
00015 
00016 =========================================================================*/
00017 
00018 // PS -> //#include <gsl/gsl_math.h>//PS
00019 #include <memory.h>
00020 #include "volume.hxx"
00021 
00022 #ifdef KGFO_USE_VTK
00023 
00024 #include <vtkCharArray.h>
00025 #include <vtkDataArray.h>
00026 #include <vtkDoubleArray.h>
00027 #include <vtkFloatArray.h>
00028 #include <vtkIntArray.h>
00029 #include <vtkPointData.h>
00030 #include <vtkShortArray.h>
00031 #include <vtkUnsignedCharArray.h>
00032 #include <vtkUnsignedIntArray.h>
00033 #include <vtkUnsignedShortArray.h>
00034 
00035 // -------------------------------------------------------------------------
00036 const vtkIdType kVolume::VTKTypes[] = { VTK_CHAR, VTK_FLOAT, VTK_DOUBLE,
00037                                         VTK_INT, VTK_SHORT, VTK_UNSIGNED_CHAR,
00038                                         VTK_UNSIGNED_INT, VTK_UNSIGNED_SHORT };
00039 
00040 #endif // KGFO_USE_VTK
00041 
00042 //#ifdef KGFO_USE_IDO
00043 
00044 // -------------------------------------------------------------------------
00045 //const int kVolume::IDOTypes[] = { VOL_CHAR, VOL_FLOAT, VOL_DOUBLE, VOL_LONG,
00046 //                                VOL_SHORT, VOL_UCHAR, VOL_ULONG,
00047 //                                VOL_USHORT };
00048 
00049 //#endif // KGFO_USE_IDO
00050 
00051 // -------------------------------------------------------------------------
00052 const void* kVolume::BLANK   = ( void* ) 0;
00053 const void* kVolume::NOALLOC = ( void* )-1;
00054 const int kVolume::SIZETypes[] = { sizeof( char ), sizeof( float ), 
00055                                    sizeof( double ), sizeof( int ),
00056                                    sizeof( short ), sizeof( uchar ),
00057                                    sizeof( uint ), sizeof( ushort ) };
00058 
00059 // ---------------------------------------------------------------------------
00060 template< class FROM, class TO >
00061     static void convertCastT( FROM* src, TO* dest, ulong size )
00062 {
00063     FROM* end = src + size;
00064     for( ; src < end; src++, dest++ ) *dest = ( TO )*src;
00065 }
00066 
00067 
00068 // ---------------------------------------------------------------------------
00069 template< class FROM, class TO >
00070     static void convertScaleT( FROM *src, TO *dest, ulong size,
00071                                double smin, double tmin, double slope )
00072 {
00073     FROM* end = src + size;
00074     for( ; src < end; src++, dest++ )
00075         *dest = ( TO )( ( double( *src ) - smin ) * slope + tmin );
00076 }
00077 
00078 // ---------------------------------------------------------------------------
00079 template< class TYPE >
00080     static void getMinMaxT( TYPE *src, ulong size,
00081                             double& min, double& max )
00082 {
00083     TYPE* end = src + size;
00084     TYPE m, M;
00085     bool st;
00086         
00087     m = ( TYPE )0;
00088     M = ( TYPE )0;
00089     for( st = true; src < end; src++, st = false ) {
00090                 
00091         if( *src < m || st ) m = *src;
00092         if( *src > M || st ) M = *src;
00093                 
00094     } // rof
00095         
00096     min = ( double )m;
00097     max = ( double )M;
00098 }
00099 
00100 // -------------------------------------------------------------------------
00101 kVolume::kVolume( )
00102     : _type( UCHAR ), 
00103       _creator( SELF ),
00104       _raw( NULL ), 
00105       _columns( NULL ),
00106       _images( NULL )
00107 #ifdef KGFO_USE_VTK
00108                   ,
00109       _vtk( NULL )      
00110 #endif // KGFO_USE_VTK      
00111 {
00112     _dims[ CX ] = 1; _dims[ CY ] = 1; _dims[ CZ ] = 1;
00113     _sizes[ CX ] = 1; _sizes[ CY ] = 1; _sizes[ CZ ] = 1;
00114     allocate( );
00115     buildIndex( );
00116 }
00117 
00118 // -------------------------------------------------------------------------
00119 kVolume::kVolume( Type type,
00120                   uint xdim, uint ydim, uint zdim,
00121                   double xsize, double ysize, double zsize,
00122                   void* data )
00123     : _type( type ), 
00124       _creator( SELF ),
00125       _raw( NULL ), 
00126       _columns( NULL ),
00127       _images( NULL )
00128 #ifdef KGFO_USE_VTK
00129                     ,
00130       _vtk( NULL )
00131 #endif // KGFO_USE_VTK      
00132 {
00133     _dims[ CX ] = xdim; _dims[ CY ] = ydim; _dims[ CZ ] = zdim;
00134     _sizes[ CX ] = xsize; _sizes[ CY ] = ysize; _sizes[ CZ ] = zsize;
00135     if( data != NOALLOC ) {
00136         
00137         if( data != BLANK )
00138             _raw = data;
00139         else
00140             allocate( );
00141         buildIndex( );
00142                         
00143     } // fi
00144 }
00145 
00146 // -------------------------------------------------------------------------
00147 kVolume::kVolume( Type type,
00148                   const uint *dims,
00149                   const double *sizes,
00150                   void* data )
00151     : _type( type ), 
00152       _creator( SELF ),
00153       _raw( NULL ), _columns( NULL ),
00154       _images( NULL )
00155 #ifdef KGFO_USE_VTK
00156                    ,
00157       _vtk( NULL )
00158 #endif // KGFO_USE_VTK      
00159       
00160 {
00161     memcpy( _dims, dims, 3 * sizeof( uint ) );
00162     memcpy( _sizes, sizes, 3 * sizeof( double ) );
00163     if( data != NOALLOC ) {
00164         
00165         if( data != BLANK )
00166             _raw = data;
00167         else
00168             allocate( );
00169         buildIndex( );
00170                         
00171     } // fi
00172 }
00173 
00174 // -------------------------------------------------------------------------
00175 kVolume::kVolume( const kVolume& org )
00176     : _type( UCHAR ), 
00177       _creator( SELF ),
00178       _raw( NULL ), 
00179       _columns( NULL ),
00180       _images( NULL )
00181 #ifdef KGFO_USE_VTK
00182                     ,
00183       _vtk( NULL )
00184 #endif // KGFO_USE_VTK      
00185   
00186 {
00187     copyFrom( org );
00188 }
00189 
00190 // -------------------------------------------------------------------------
00191 kVolume& kVolume::operator=( const kVolume& org )
00192 {
00193     copyFrom( org );
00194     return( *this );
00195 }
00196 
00197 // -------------------------------------------------------------------------
00198 void kVolume::copyFrom( const kVolume& org )
00199 {
00200     _type = org._type;
00201     _creator = SELF;
00202     _raw = 0;
00203     _columns = 0;
00204     _images = 0;
00205 
00206     memcpy( _dims, org._dims, 3 * sizeof( uint ) );
00207     memcpy( _sizes, org._sizes, 3 * sizeof( double ) );
00208     if( org._raw ) {
00209 
00210         allocate( );
00211         buildIndex( );
00212         memcpy( _raw, org._raw, getRawSizeInBytes( ) );
00213 
00214     } // fi
00215 }
00216 
00217 // -------------------------------------------------------------------------
00218 bool kVolume::sameDimension( const kVolume& comp )
00219 {
00220     return( ( _dims[ CX ] == comp._dims[ CX ] &&
00221               _dims[ CY ] == comp._dims[ CY ] &&
00222               _dims[ CZ ] == comp._dims[ CZ ] ) );
00223 }
00224 
00225 // -------------------------------------------------------------------------
00226 double kVolume::getPixel( uint x, uint y, uint z ) const
00227 {
00228     double p;
00229 
00230     switch( _type ) {
00231                 
00232     case CHAR:   p = ( double )(   ( char*** )_images )[ z ][ y ][ x ]; break;
00233     case FLOAT:  p = ( double )(  ( float*** )_images )[ z ][ y ][ x ]; break;
00234     case DOUBLE: p = ( double )( ( double*** )_images )[ z ][ y ][ x ]; break;
00235     case INT:    p = ( double )(    ( int*** )_images )[ z ][ y ][ x ]; break;
00236     case SHORT:  p = ( double )(  ( short*** )_images )[ z ][ y ][ x ]; break;
00237     case UCHAR:  p = ( double )(  ( uchar*** )_images )[ z ][ y ][ x ]; break;
00238     case UINT:   p = ( double )(   ( uint*** )_images )[ z ][ y ][ x ]; break;
00239     case USHORT: p = ( double )( ( ushort*** )_images )[ z ][ y ][ x ]; break;
00240     default: p = 0.0; break;
00241 
00242     } // fswitch
00243 
00244     return( p );
00245 }
00246 
00247 // -------------------------------------------------------------------------
00248 void kVolume::setPixel( double v, uint x, uint y, uint z )
00249 {
00250 
00251     switch( _type ) {
00252                 
00253     case CHAR:     ( ( char*** )_images )[ z ][ y ][ x ] = ( char )v; break;
00254     case FLOAT:   ( ( float*** )_images )[ z ][ y ][ x ] = ( float )v; break;
00255     case DOUBLE: ( ( double*** )_images )[ z ][ y ][ x ] = ( double )v; break;
00256     case INT:       ( ( int*** )_images )[ z ][ y ][ x ] = ( int )v; break;
00257     case SHORT:   ( ( short*** )_images )[ z ][ y ][ x ] = ( short )v; break;
00258     case UCHAR:   ( ( uchar*** )_images )[ z ][ y ][ x ] = ( uchar )v; break;
00259     case UINT:     ( ( uint*** )_images )[ z ][ y ][ x ] = ( uint )v; break;
00260     case USHORT: ( ( ushort*** )_images )[ z ][ y ][ x ] = ( ushort )v; break;
00261     default: break;
00262 
00263     } // fswitch
00264 }
00265 
00266 // -------------------------------------------------------------------------
00267 void kVolume::convertCast( Type type )
00268 {
00269     if( _type != type ) {
00270 
00271         void* buffer;
00272         ulong size = getRawSize( );
00273 
00274         buffer = ( void* )new uchar[ size * SIZETypes[ type ] ];
00275 
00276         switch( _type ) {
00277 
00278         case CHAR:
00279             switch( type ) {
00280 
00281             case SHORT:  convertCastT( ( char* )_raw, ( short* )buffer, size ); break;
00282             case INT:    convertCastT( ( char* )_raw, ( int* )buffer, size ); break;
00283             case USHORT: convertCastT( ( char* )_raw, ( ushort* )buffer, size ); break;
00284             case UINT:   convertCastT( ( char* )_raw, ( uint* )buffer, size ); break;
00285             case FLOAT:  convertCastT( ( char* )_raw, ( float* )buffer, size ); break;
00286             case DOUBLE: convertCastT( ( char* )_raw, ( double* )buffer, size ); break;
00287             case UCHAR:  convertCastT( ( char* )_raw, ( uchar* )buffer, size ); break;
00288             default : break;
00289 
00290             } // fswitch
00291             break;
00292 
00293         case SHORT:
00294             switch( type ) {
00295 
00296             case CHAR:   convertCastT( ( short* )_raw, ( char* )buffer, size ); break;
00297             case INT:    convertCastT( ( short* )_raw, ( int* )buffer, size ); break;
00298             case USHORT: convertCastT( ( short* )_raw, ( ushort* )buffer, size ); break;
00299             case UINT:   convertCastT( ( short* )_raw, ( uint* )buffer, size ); break;
00300             case FLOAT:  convertCastT( ( short* )_raw, ( float* )buffer, size ); break;
00301             case DOUBLE: convertCastT( ( short* )_raw, ( double* )buffer, size ); break;
00302             case UCHAR:  convertCastT( ( short* )_raw, ( uchar* )buffer, size ); break;
00303             default : break;
00304             } // fswitch
00305             break;
00306 
00307         case INT:
00308             switch( type ) {
00309 
00310             case CHAR:   convertCastT( ( int* )_raw, ( char* )buffer, size ); break;
00311             case SHORT:  convertCastT( ( int* )_raw, ( short* )buffer, size ); break;
00312             case USHORT: convertCastT( ( int* )_raw, ( ushort* )buffer, size ); break;
00313             case UINT:   convertCastT( ( int* )_raw, ( uint* )buffer, size ); break;
00314             case FLOAT:  convertCastT( ( int* )_raw, ( float* )buffer, size ); break;
00315             case DOUBLE: convertCastT( ( int* )_raw, ( double* )buffer, size ); break;
00316             case UCHAR:  convertCastT( ( int* )_raw, ( uchar* )buffer, size ); break;
00317             default : break;
00318             } // fswitch
00319             break;
00320 
00321         case USHORT:
00322             switch( type ) {
00323 
00324             case CHAR:   convertCastT( ( ushort* )_raw, ( char* )buffer, size ); break;
00325             case SHORT:  convertCastT( ( ushort* )_raw, ( short* )buffer, size ); break;
00326             case INT:    convertCastT( ( ushort* )_raw, ( int* )buffer, size ); break;
00327             case UINT:   convertCastT( ( ushort* )_raw, ( uint* )buffer, size ); break;
00328             case FLOAT:  convertCastT( ( ushort* )_raw, ( float* )buffer, size ); break;
00329             case DOUBLE: convertCastT( ( ushort* )_raw, ( double* )buffer, size ); break;
00330             case UCHAR:  convertCastT( ( ushort* )_raw, ( uchar* )buffer, size ); break;
00331             default : break;
00332             } // fswitch
00333             break;
00334 
00335         case UINT:
00336             switch( type ) {
00337 
00338             case CHAR:   convertCastT( ( uint* )_raw, ( char* )buffer, size ); break;
00339             case SHORT:  convertCastT( ( uint* )_raw, ( short* )buffer, size ); break;
00340             case INT:    convertCastT( ( uint* )_raw, ( int* )buffer, size ); break;
00341             case USHORT: convertCastT( ( uint* )_raw, ( ushort* )buffer, size ); break;
00342             case FLOAT:  convertCastT( ( uint* )_raw, ( float* )buffer, size ); break;
00343             case DOUBLE: convertCastT( ( uint* )_raw, ( double* )buffer, size ); break;
00344             case UCHAR:  convertCastT( ( uint* )_raw, ( uchar* )buffer, size ); break;
00345             default : break;
00346             } // fswitch
00347             break;
00348 
00349         case FLOAT:
00350             switch( type ) {
00351 
00352             case CHAR:   convertCastT( ( float* )_raw, ( char* )buffer, size ); break;
00353             case SHORT:  convertCastT( ( float* )_raw, ( short* )buffer, size ); break;
00354             case INT:    convertCastT( ( float* )_raw, ( int* )buffer, size ); break;
00355             case USHORT: convertCastT( ( float* )_raw, ( ushort* )buffer, size ); break;
00356             case UINT:   convertCastT( ( float* )_raw, ( uint* )buffer, size ); break;
00357             case DOUBLE: convertCastT( ( float* )_raw, ( double* )buffer, size ); break;
00358             case UCHAR:  convertCastT( ( float* )_raw, ( uchar* )buffer, size ); break;
00359             default : break;
00360             } // fswitch
00361             break;
00362 
00363         case DOUBLE:
00364             switch( type ) {
00365 
00366             case CHAR:   convertCastT( ( double* )_raw, ( char* )buffer, size ); break;
00367             case SHORT:  convertCastT( ( double* )_raw, ( short* )buffer, size ); break;
00368             case INT:    convertCastT( ( double* )_raw, ( int* )buffer, size ); break;
00369             case USHORT: convertCastT( ( double* )_raw, ( ushort* )buffer, size ); break;
00370             case UINT:   convertCastT( ( double* )_raw, ( uint* )buffer, size ); break;
00371             case FLOAT:  convertCastT( ( double* )_raw, ( float* )buffer, size ); break;
00372             case UCHAR:  convertCastT( ( double* )_raw, ( uchar* )buffer, size ); break;
00373             default : break;
00374             } // fswitch
00375             break;
00376 
00377         case UCHAR:
00378             switch( type ) {
00379 
00380             case CHAR:   convertCastT( ( uchar* )_raw, ( char* )buffer, size ); break;
00381             case SHORT:  convertCastT( ( uchar* )_raw, ( short* )buffer, size ); break;
00382             case INT:    convertCastT( ( uchar* )_raw, ( int* )buffer, size ); break;
00383             case USHORT: convertCastT( ( uchar* )_raw, ( ushort* )buffer, size ); break;
00384             case UINT:   convertCastT( ( uchar* )_raw, ( uint* )buffer, size ); break;
00385             case FLOAT:  convertCastT( ( uchar* )_raw, ( float* )buffer, size ); break;
00386             case DOUBLE: convertCastT( ( uchar* )_raw, ( double* )buffer, size ); break;
00387             default : break;
00388             } // fswitch
00389             break;
00390      
00391         } // fswitch
00392 
00393         _type = type;
00394         deallocate( );
00395         _creator = SELF;
00396         _raw = buffer;
00397         buildIndex( );
00398 
00399     } // fi
00400 }
00401 
00402 // -------------------------------------------------------------------------
00403 void kVolume::convertScale( Type type, double min, double max )
00404 {
00405     double tmin, tmax, smin, smax;
00406 
00407 // PS ->     //tmin = GSL_MIN( min, max ); //PS
00408         tmin= ((min<max)?min:max);
00409 // PS ->     //tmax = GSL_MAX( min, max ); //PS
00410         tmax= ((min>max)?min:max);
00411 
00412     getMinMax( smin, smax );
00413         
00414     // compute scaling slope
00415     double a = ( tmax - tmin ) / ( smax - smin );
00416 
00417     void* buffer;
00418     ulong size = getRawSize( );
00419 
00420     buffer = ( void* )new uchar[ size * SIZETypes[ type ] ];
00421 
00422     switch( _type ) {
00423 
00424     case CHAR:
00425         switch( type ) {
00426 
00427         case CHAR:   convertScaleT( ( char* )_raw, ( char* )buffer, size, smin, tmin, a ); break;
00428         case SHORT:  convertScaleT( ( char* )_raw, ( short* )buffer, size, smin, tmin, a ); break;
00429         case INT:    convertScaleT( ( char* )_raw, ( int* )buffer, size, smin, tmin, a ); break;
00430         case USHORT: convertScaleT( ( char* )_raw, ( ushort* )buffer, size, smin, tmin, a ); break;
00431         case UINT:   convertScaleT( ( char* )_raw, ( uint* )buffer, size, smin, tmin, a ); break;
00432         case FLOAT:  convertScaleT( ( char* )_raw, ( float* )buffer, size, smin, tmin, a ); break;
00433         case DOUBLE: convertScaleT( ( char* )_raw, ( double* )buffer, size, smin, tmin, a ); break;
00434         case UCHAR:  convertScaleT( ( char* )_raw, ( uchar* )buffer, size, smin, tmin, a ); break;
00435 
00436         } // fswitch
00437         break;
00438     case SHORT:
00439         switch( type ) {
00440 
00441         case CHAR:   convertScaleT( ( short* )_raw, ( char* )buffer, size, smin, tmin, a ); break;
00442         case SHORT:  convertScaleT( ( short* )_raw, ( short* )buffer, size, smin, tmin, a ); break;
00443         case INT:    convertScaleT( ( short* )_raw, ( int* )buffer, size, smin, tmin, a ); break;
00444         case USHORT: convertScaleT( ( short* )_raw, ( ushort* )buffer, size, smin, tmin, a ); break;
00445         case UINT:   convertScaleT( ( short* )_raw, ( uint* )buffer, size, smin, tmin, a ); break;
00446         case FLOAT:  convertScaleT( ( short* )_raw, ( float* )buffer, size, smin, tmin, a ); break;
00447         case DOUBLE: convertScaleT( ( short* )_raw, ( double* )buffer, size, smin, tmin, a ); break;
00448         case UCHAR:  convertScaleT( ( short* )_raw, ( uchar* )buffer, size, smin, tmin, a ); break;
00449 
00450         } // fswitch
00451         break;
00452     case INT:
00453         switch( type ) {
00454 
00455         case CHAR:   convertScaleT( ( int* )_raw, ( char* )buffer, size, smin, tmin, a ); break;
00456         case SHORT:  convertScaleT( ( int* )_raw, ( short* )buffer, size, smin, tmin, a ); break;
00457         case INT:    convertScaleT( ( int* )_raw, ( int* )buffer, size, smin, tmin, a ); break;
00458         case USHORT: convertScaleT( ( int* )_raw, ( ushort* )buffer, size, smin, tmin, a ); break;
00459         case UINT:   convertScaleT( ( int* )_raw, ( uint* )buffer, size, smin, tmin, a ); break;
00460         case FLOAT:  convertScaleT( ( int* )_raw, ( float* )buffer, size, smin, tmin, a ); break;
00461         case DOUBLE: convertScaleT( ( int* )_raw, ( double* )buffer, size, smin, tmin, a ); break;
00462         case UCHAR:  convertScaleT( ( int* )_raw, ( uchar* )buffer, size, smin, tmin, a ); break;
00463 
00464         } // fswitch
00465         break;
00466     case USHORT:
00467         switch( type ) {
00468 
00469         case CHAR:   convertScaleT( ( ushort* )_raw, ( char* )buffer, size, smin, tmin, a ); break;
00470         case SHORT:  convertScaleT( ( ushort* )_raw, ( short* )buffer, size, smin, tmin, a ); break;
00471         case INT:    convertScaleT( ( ushort* )_raw, ( int* )buffer, size, smin, tmin, a ); break;
00472         case USHORT: convertScaleT( ( ushort* )_raw, ( ushort* )buffer, size, smin, tmin, a ); break;
00473         case UINT:   convertScaleT( ( ushort* )_raw, ( uint* )buffer, size, smin, tmin, a ); break;
00474         case FLOAT:  convertScaleT( ( ushort* )_raw, ( float* )buffer, size, smin, tmin, a ); break;
00475         case DOUBLE: convertScaleT( ( ushort* )_raw, ( double* )buffer, size, smin, tmin, a ); break;
00476         case UCHAR:  convertScaleT( ( ushort* )_raw, ( uchar* )buffer, size, smin, tmin, a ); break;
00477 
00478         } // fswitch
00479         break;
00480     case UINT:
00481         switch( type ) {
00482 
00483         case CHAR:   convertScaleT( ( uint* )_raw, ( char* )buffer, size, smin, tmin, a ); break;
00484         case SHORT:  convertScaleT( ( uint* )_raw, ( short* )buffer, size, smin, tmin, a ); break;
00485         case INT:    convertScaleT( ( uint* )_raw, ( int* )buffer, size, smin, tmin, a ); break;
00486         case USHORT: convertScaleT( ( uint* )_raw, ( ushort* )buffer, size, smin, tmin, a ); break;
00487         case UINT:   convertScaleT( ( uint* )_raw, ( uint* )buffer, size, smin, tmin, a ); break;
00488         case FLOAT:  convertScaleT( ( uint* )_raw, ( float* )buffer, size, smin, tmin, a ); break;
00489         case DOUBLE: convertScaleT( ( uint* )_raw, ( double* )buffer, size, smin, tmin, a ); break;
00490         case UCHAR:  convertScaleT( ( uint* )_raw, ( uchar* )buffer, size, smin, tmin, a ); break;
00491 
00492         } // fswitch
00493         break;
00494     case UCHAR:
00495         switch( type ) {
00496 
00497         case CHAR:   convertScaleT( ( uchar* )_raw, ( char* )buffer, size, smin, tmin, a ); break;
00498         case SHORT:  convertScaleT( ( uchar* )_raw, ( short* )buffer, size, smin, tmin, a ); break;
00499         case INT:    convertScaleT( ( uchar* )_raw, ( int* )buffer, size, smin, tmin, a ); break;
00500         case USHORT: convertScaleT( ( uchar* )_raw, ( ushort* )buffer, size, smin, tmin, a ); break;
00501         case UINT:   convertScaleT( ( uchar* )_raw, ( uint* )buffer, size, smin, tmin, a ); break;
00502         case FLOAT:  convertScaleT( ( uchar* )_raw, ( float* )buffer, size, smin, tmin, a ); break;
00503         case DOUBLE: convertScaleT( ( uchar* )_raw, ( double* )buffer, size, smin, tmin, a ); break;
00504         case UCHAR:  convertScaleT( ( uchar* )_raw, ( uchar* )buffer, size, smin, tmin, a ); break;
00505 
00506         } // fswitch
00507         break;
00508     case DOUBLE:
00509         switch( type ) {
00510 
00511         case CHAR:   convertScaleT( ( double* )_raw, ( char* )buffer, size, smin, tmin, a ); break;
00512         case SHORT:  convertScaleT( ( double* )_raw, ( short* )buffer, size, smin, tmin, a ); break;
00513         case INT:    convertScaleT( ( double* )_raw, ( int* )buffer, size, smin, tmin, a ); break;
00514         case USHORT: convertScaleT( ( double* )_raw, ( ushort* )buffer, size, smin, tmin, a ); break;
00515         case UINT:   convertScaleT( ( double* )_raw, ( uint* )buffer, size, smin, tmin, a ); break;
00516         case FLOAT:  convertScaleT( ( double* )_raw, ( float* )buffer, size, smin, tmin, a ); break;
00517         case DOUBLE: convertScaleT( ( double* )_raw, ( double* )buffer, size, smin, tmin, a ); break;
00518         case UCHAR:  convertScaleT( ( uchar* )_raw, ( double* )buffer, size, smin, tmin, a ); break;
00519 
00520         } // fswitch
00521         break;
00522     case FLOAT:
00523         switch( type ) {
00524 
00525         case CHAR:   convertScaleT( ( float* )_raw, ( char* )buffer, size, smin, tmin, a ); break;
00526         case SHORT:  convertScaleT( ( float* )_raw, ( short* )buffer, size, smin, tmin, a ); break;
00527         case INT:    convertScaleT( ( float* )_raw, ( int* )buffer, size, smin, tmin, a ); break;
00528         case USHORT: convertScaleT( ( float* )_raw, ( ushort* )buffer, size, smin, tmin, a ); break;
00529         case UINT:   convertScaleT( ( float* )_raw, ( uint* )buffer, size, smin, tmin, a ); break;
00530         case FLOAT:  convertScaleT( ( float* )_raw, ( float* )buffer, size, smin, tmin, a ); break;
00531         case DOUBLE: convertScaleT( ( float* )_raw, ( double* )buffer, size, smin, tmin, a ); break;
00532         case UCHAR:  convertScaleT( ( float* )_raw, ( double* )buffer, size, smin, tmin, a ); break;
00533 
00534         } // fswitch
00535         break;
00536 
00537     } // fswitch
00538 
00539     _type = type;
00540     deallocate( );
00541     _creator = SELF;
00542     _raw = buffer;
00543     buildIndex( );
00544 }
00545 
00546 // -------------------------------------------------------------------------
00547 void kVolume::getMinMax( double& min, double& max ) const
00548 {
00549     ulong size = getRawSize( );
00550 
00551     switch( _type ) {
00552                 
00553     case CHAR:   getMinMaxT( ( char* )_raw, size, min, max ); break;
00554     case FLOAT:  getMinMaxT( ( float* )_raw, size, min, max ); break;
00555     case DOUBLE: getMinMaxT( ( double* )_raw, size, min, max ); break;
00556     case INT:    getMinMaxT( ( int* )_raw, size, min, max ); break;
00557     case SHORT:  getMinMaxT( ( short* )_raw, size, min, max ); break;
00558     case UCHAR:  getMinMaxT( ( uchar* )_raw, size, min, max ); break;
00559     case UINT:   getMinMaxT( ( uint* )_raw, size, min, max ); break;
00560     case USHORT: getMinMaxT( ( ushort* )_raw, size, min, max ); break;
00561 
00562     } // fswitch
00563 }
00564 
00565 // -------------------------------------------------------------------------
00566 double kVolume::getMin( ) const
00567 {
00568     double m, M;
00569 
00570     getMinMax( m, M );
00571     return( m );
00572 }
00573 
00574 // -------------------------------------------------------------------------
00575 double kVolume::getMax( ) const
00576 {
00577     double m, M;
00578 
00579     getMinMax( m, M );
00580     return( M );
00581 }
00582 
00583 // -------------------------------------------------------------------------
00584 /*double kVolume::GetMaxIntSphere( double* p, double r )
00585 {
00586     int minX, minY, minZ, maxX, maxY, maxZ;
00587     gslobj_vector vP( p, 3 ), v( 3 );
00588     double maxint, tmp;
00589     bool start;
00590         
00591     minX = int( floor( p[ 0 ] - r ) );
00592     minY = int( floor( p[ 1 ] - r ) );
00593     minZ = int( floor( p[ 2 ] - r ) );
00594     maxX = int( ceil( p[ 0 ] + r ) );
00595     maxY = int( ceil( p[ 1 ] + r ) );
00596     maxZ = int( ceil( p[ 2 ] + r ) );
00597         
00598     minX = GSL_MAX( minX, 0 );
00599     minY = GSL_MAX( minY, 0 );
00600     minZ = GSL_MAX( minZ, 0 );
00601         
00602     maxX = GSL_MIN( maxX, this->getXdim( ) - 1 );
00603     maxY = GSL_MIN( maxY, this->getYdim( ) - 1 );
00604     maxZ = GSL_MIN( maxZ, this->getZdim( ) - 1 );
00605 
00606     maxint = 0.0;
00607     start = true;
00608     for( v( 0 ) = minX; v( 0 ) <= maxX; v( 0 )++ )
00609       for( v( 1 ) = minY; v( 1 ) <= maxY; v( 1 )++ )
00610         for( v( 2 ) = minZ; v( 2 ) <= maxZ; v( 2 )++ )
00611           if( ( v - vP ).norm2( ) <= r ) {
00612             tmp = this->getPixel( ( uint )v(0), ( uint )v(1), ( uint )v(2));
00613             maxint = ( tmp > maxint || start )? tmp: maxint;
00614             start = false;
00615           } // fi
00616 
00617     return( maxint );
00618 }*/
00619 // -------------------------------------------------------------------------
00620 unsigned short kVolume::GetMaxIntSphere2( double* p, double r )
00621 {
00626     int minX, minY, minZ, maxX, maxY, maxZ;
00627     unsigned int v[3];
00628     unsigned short maxint = 0, tmp;
00629 
00630     minX = int( floor( p[ 0 ] - r ) );
00631     minY = int( floor( p[ 1 ] - r ) );
00632     minZ = int( floor( p[ 2 ] - r ) );
00633     maxX = int( ceil( p[ 0 ] + r ) );
00634     maxY = int( ceil( p[ 1 ] + r ) );
00635     maxZ = int( ceil( p[ 2 ] + r ) );
00636 
00637 // PS ->     //minX = GSL_MAX( minX, 0 );//PS
00638 // PS ->     //minY = GSL_MAX( minY, 0 );//PS
00639 // PS ->     //minZ = GSL_MAX( minZ, 0 );//PS
00640         minX=((minX>0)?minX:0);
00641         minY=((minY>0)?minY:0);
00642         minZ=((minZ>0)?minZ:0);
00643         
00644 // PS ->     //maxX = GSL_MIN( maxX, this->getXdim( ) - 1 );//PS
00645 // PS ->     //maxY = GSL_MIN( maxY, this->getYdim( ) - 1 );//PS
00646 // PS ->     //maxZ = GSL_MIN( maxZ, this->getZdim( ) - 1 );//PS
00647         int xDim=this->getXdim( ) - 1;
00648         maxX= (maxX<xDim?maxX:xDim);
00649 
00650         int yDim=this->getYdim( ) - 1;
00651         maxY= (maxY<yDim?maxY:yDim);
00652 
00653         int zDim=this->getZdim( ) - 1;
00654         maxZ= (maxZ<zDim?maxZ:zDim);
00655 
00656     double r2 = r*r;  //need to do comparison in double ... don't know why...
00657     for( v[0] = minX; v[0] <= maxX; v[0]++ )
00658       for( v[1] = minY; v[1] <= maxY; v[1]++ )
00659         for( v[2] = minZ; v[2] <= maxZ; v[2]++ )
00660           if( ((v[0]-p[0])*(v[0]-p[0])+(v[1]-p[1])*(v[1]-p[1])+(v[2]-p[2])*(v[2]-p[2])) <= r2 )
00661           {
00662             tmp = (unsigned short)this->getPixel( v[0], v[1], v[2] );
00663             maxint = ( tmp > maxint ) ? tmp : maxint;
00664           }
00665 
00666     return( maxint );
00667 }
00668 
00669 // -------------------------------------------------------------------------
00670 void kVolume::allocate( )
00671 {
00672     ulong size = getRawSizeInBytes( );
00673 
00674     if( _creator == SELF ) {
00675 
00676         _raw = ( void* )new uchar[ size ];
00677         memset( _raw, 0, size );
00678 
00679     } // fi
00680 }
00681 
00682 // -------------------------------------------------------------------------
00683 void kVolume::buildIndex( )
00684 {
00685     ulong size;
00686 
00687     size = ( _dims[ CZ ] * sizeof( uchar** ) ) +
00688         ( _dims[ CZ ] * _dims[ CY ] * sizeof( void* ) );
00689                 
00690         _images = ( void*** )new uchar[ size ];
00691                 
00692         _columns = ( void** )( _images + _dims[ CZ ] );
00693         void** plane = _columns;
00694         for( uint z = 0; z < _dims[ CZ ]; z++ ) {
00695                         
00696             _images[ z ] = plane;
00697             plane += _dims[ CY ];
00698                         
00699         } // rof
00700         void* line = _raw;
00701         for( uint y = 0; y < _dims[ CZ ] * _dims[ CY ]; y++ ) {
00702                         
00703             _columns[ y ] = line;
00704             line = ( void* )( ( uchar* ) line +
00705                               _dims[ CX ] * SIZETypes[ _type ] );
00706                         
00707         } // rof
00708         
00709 #ifdef KGFO_USE_VTK
00710         
00711     vtkCharArray* carray;
00712     vtkDoubleArray* darray;
00713     vtkFloatArray* farray;
00714     vtkIntArray* iarray;
00715     vtkShortArray* sarray;
00716     vtkUnsignedCharArray* ucarray;
00717     vtkUnsignedIntArray* uiarray;
00718     vtkUnsignedShortArray* usarray;
00719         
00720     size = _dims[ CX ] * _dims[ CY ] * _dims[ CZ ];
00721 
00722     if( _creator == SELF || _creator == IDO ) {
00723           
00724         _vtk = vtkImageData::New( );
00725         _vtk->SetDimensions( _dims[ CX ], _dims[ CY ], _dims[ CZ ] );
00726         _vtk->SetSpacing( _sizes[ CX ], _sizes[ CY ], _sizes[ CZ ] );
00727 
00728         switch( _type ) {
00729 
00730         case CHAR:
00731             carray = vtkCharArray::New( );
00732             carray->SetNumberOfComponents( 1 );
00733             carray->SetArray( ( char* )( _raw ), size, 1 );
00734             _vtk->SetScalarType( VTK_CHAR );
00735             _vtk->GetPointData( )->SetScalars( carray );
00736             carray->Delete( );
00737             break;
00738 
00739         case UCHAR:
00740             ucarray = vtkUnsignedCharArray::New( );
00741             ucarray->SetNumberOfComponents( 1 );
00742             ucarray->SetArray( ( uchar* )( _raw ), size, 1 );
00743             _vtk->SetScalarType( VTK_UNSIGNED_CHAR );
00744             _vtk->GetPointData( )->SetScalars( ucarray );
00745             ucarray->Delete( );
00746             break;
00747 
00748         case SHORT:
00749             sarray = vtkShortArray::New( );
00750             sarray->SetNumberOfComponents( 1 );
00751             sarray->SetArray( ( short* )( _raw ), size, 1 );
00752             _vtk->SetScalarType( VTK_SHORT );
00753             _vtk->GetPointData( )->SetScalars( sarray );
00754             sarray->Delete( );
00755             break;
00756 
00757         case INT:
00758             iarray = vtkIntArray::New( );
00759             iarray->SetNumberOfComponents( 1 );
00760             iarray->SetArray( ( int* )( _raw ), size, 1 );
00761             _vtk->SetScalarType( VTK_INT );
00762             _vtk->GetPointData( )->SetScalars( iarray );
00763             iarray->Delete( );
00764             break;
00765 
00766         case USHORT:
00767             usarray = vtkUnsignedShortArray::New( );
00768             usarray->SetNumberOfComponents( 1 );
00769             usarray->SetArray( ( ushort* )( _raw ), size, 1 );
00770             _vtk->SetScalarType( VTK_UNSIGNED_SHORT );
00771             _vtk->GetPointData( )->SetScalars( usarray );
00772             usarray->Delete( );
00773             break;
00774 
00775         case UINT:
00776             uiarray = vtkUnsignedIntArray::New( );
00777             uiarray->SetNumberOfComponents( 1 );
00778             uiarray->SetArray( ( uint* )( _raw ), size, 1 );
00779             _vtk->SetScalarType( VTK_UNSIGNED_INT );
00780             _vtk->GetPointData( )->SetScalars( uiarray );
00781             uiarray->Delete( );
00782             break;
00783 
00784         case FLOAT:
00785             farray = vtkFloatArray::New( );
00786             farray->SetNumberOfComponents( 1 );
00787             farray->SetArray( ( float* )( _raw ), size, 1 );
00788             _vtk->SetScalarType( VTK_FLOAT );
00789             _vtk->GetPointData( )->SetScalars( farray );
00790             farray->Delete( );
00791             break;
00792                   
00793         case DOUBLE:
00794             darray = vtkDoubleArray::New( );
00795             darray->SetNumberOfComponents( 1 );
00796             darray->SetArray( ( double* )( _raw ), size, 1 );
00797             _vtk->SetScalarType( VTK_DOUBLE );
00798             _vtk->GetPointData( )->SetScalars( darray );
00799             darray->Delete( );
00800             break;
00801                   
00802         } // fswitch
00803           
00804     } // fi
00805 
00806 #endif // KGFO_USE_VTK
00807 }
00808 
00809 // -------------------------------------------------------------------------
00810 void kVolume::deallocate( )
00811 {
00812 #ifdef KGFO_USE_VTK
00813     if( _vtk ) _vtk->Delete();
00814     _vtk = NULL;
00815 #endif // KGFO_USE_VTK
00816     delete[] ( uchar* )_images;
00817     if( _raw && _creator == SELF )
00818 
00819 //EED purify 12/sept/2006
00820 //      delete[] ( uchar* )_raw;
00821 
00822     free ( _raw );
00823 
00824     _creator = SELF;
00825     _raw     = NULL;
00826     _columns = NULL;
00827     _images  = NULL;
00828 }
00829 
00830 #ifdef KGFO_USE_VTK
00831 
00832 // -------------------------------------------------------------------------
00833 kVolume::kVolume( vtkImageData* org )
00834     :
00835       _creator( VTK ),
00836       _raw( 0 ), 
00837       _columns( 0 ), 
00838       _images( 0 ),
00839       _vtk( 0 )
00840 {
00841     //int i, j, k, y;
00842     int itmp[ 3 ];
00843     double ftmp[ 3 ];
00844     //double v;
00845 
00846     switch( org->GetScalarType( ) ) {
00847 
00848     case VTK_CHAR:           _type = CHAR;   break;
00849     case VTK_UNSIGNED_CHAR:  _type = UCHAR;  break;
00850     case VTK_SHORT:          _type = SHORT;  break;
00851     case VTK_INT:            _type = INT;    break;
00852     case VTK_UNSIGNED_SHORT: _type = USHORT; break;
00853     case VTK_UNSIGNED_INT:   _type = UINT;   break;
00854     case VTK_FLOAT:          _type = FLOAT;  break;
00855     case VTK_DOUBLE:         _type = DOUBLE; break;
00856     default: break;
00857                 
00858     } // fswitch
00859         
00860     org->GetDimensions( itmp );
00861     _dims[ CX ] = ( uint )itmp[ 0 ];
00862     _dims[ CY ] = ( uint )itmp[ 1 ];
00863     _dims[ CZ ] = ( uint )itmp[ 2 ];
00864     org->GetSpacing( ftmp );
00865     _sizes[ CX ] = ( double )ftmp[ 0 ];
00866     _sizes[ CY ] = ( double )ftmp[ 1 ];
00867     _sizes[ CZ ] = ( double )ftmp[ 2 ];
00868 
00869     _raw = org->GetPointData( )->GetScalars( )->GetVoidPointer( 0 );
00870     //_vtk = org;
00871     _vtk = vtkImageData::New();
00872     _vtk->ShallowCopy( org );
00873     buildIndex( );
00874 }
00875 
00876 // -------------------------------------------------------------------------
00877 kVolume& kVolume::operator=( vtkImageData* org )
00878 {
00879     copyFrom( org );
00880     return( *this );
00881 }
00882 
00883 // -------------------------------------------------------------------------
00884 void kVolume::copyFrom( vtkImageData* org )
00885 {
00886     int i, j, k;//, y;
00887     int itmp[ 3 ];
00888     int ext[ 6 ];
00889     double ftmp[ 3 ];
00890     double v;
00891 
00892     deallocate( );
00893         
00894 //#ifdef KGFO_USE_IDO
00895 
00896 //    _privateIdo = NULL;
00897 
00898 //#endif // KGFO_USE_IDO
00899 
00900     _vtk     = NULL;
00901     _raw     = NULL;
00902     _columns = NULL;
00903     _images  = NULL;
00904     _creator = SELF;
00905 
00906     switch( org->GetScalarType( ) ) {
00907 
00908     case VTK_CHAR:           _type = CHAR;   break;
00909     case VTK_UNSIGNED_CHAR:  _type = UCHAR;  break;
00910     case VTK_SHORT:          _type = SHORT;  break;
00911     case VTK_INT:            _type = INT;    break;
00912     case VTK_UNSIGNED_SHORT: _type = USHORT; break;
00913     case VTK_UNSIGNED_INT:   _type = UINT;   break;
00914     case VTK_FLOAT:          _type = FLOAT;  break;
00915     case VTK_DOUBLE:         _type = DOUBLE; break;
00916     default: break;
00917                 
00918     } // fswitch
00919         
00920     org->GetDimensions( itmp );
00921     _dims[ CX ] = ( uint )itmp[ 0 ];
00922     _dims[ CY ] = ( uint )itmp[ 1 ];
00923     _dims[ CZ ] = ( uint )itmp[ 2 ];
00924     org->GetSpacing( ftmp );
00925     _sizes[ CX ] = ( double )ftmp[ 0 ];
00926     _sizes[ CY ] = ( double )ftmp[ 1 ];
00927     _sizes[ CZ ] = ( double )ftmp[ 2 ];
00928 
00929     allocate( );
00930     buildIndex( );
00931 
00932     // This avoids vtk extent crap conversion...
00933     org->GetExtent( ext );
00934     for( i = ext[ 0 ]; i <= ext[ 1 ]; i++ ) {
00935         for( j = ext[ 2 ]; j <= ext[ 3 ]; j++ ) {
00936             for( k = ext[ 4 ]; k <= ext[ 5 ]; k++ ) {           
00937                 v = org->GetScalarComponentAsDouble( i, j, k, 0 );
00938                 setPixel( v, i - ext[ 0 ], j - ext[ 2 ], k - ext[ 4 ] );                
00939             } // rof
00940         } // rof
00941     } // rof
00942 }
00943 
00944 #endif // KGFO_USE_VTK
00945 
00946 
00947 // eof - volume.cxx

Generated on 18 Mar 2010 for creaMaracasVisu_lib by  doxygen 1.6.1