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

Generated on 20 Oct 2010 for creaMaracasVisu_lib by  doxygen 1.6.1