marDynData.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   wxMaracas
00004   Module:    $RCSfile: marDynData.cpp,v $
00005   Language:  C++
00006   Date:      $Date: 2009/05/14 13:54:43 $
00007   Version:   $Revision: 1.2 $
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 #include <vtkImageResample.h>
00019 #include <vtkExtractVOI.h>
00020 #include <vtkImageData.h>
00021 #include <vtkImageChangeInformation.h>
00022 #include <vtkImageShiftScale.h> 
00023 #include <vtkImageThreshold.h> 
00024 #include "marDynData.h"
00025 
00026 // -------------------------------------------------------------------------
00027 marDynData::marDynData( marParameters* p ) : marObject( p ), _volume( NULL )
00028 {
00029         _marimagedata = new marImageData(NULL); 
00030 }
00031 
00032 // -------------------------------------------------------------------------
00033 marDynData::~marDynData() 
00034 {
00035 //      _marimagedata->SetImageData(NULL);
00036         delete _marimagedata;
00037         reset( );
00038 }
00039 // -------------------------------------------------------------------------
00040 
00041 void marDynData::loadData( kVolume* vol, int* voi )
00042  {
00043    int ext[6];
00044    reset( );
00045  
00046    vtkExtractVOI *crop = vtkExtractVOI::New();
00047    crop->SetInput( vol->castVtk( ) );
00048 
00049    crop->SetVOI( voi );
00050    //crop->SetSampleRate( getParameters( )->getVoxelSize( ), getParameters( )->getVoxelSize( ), getParameters( )->getVoxelSize( ));
00051    
00052    float RescalaSlope           =  getParameters()->getRescaleSlope();
00053    float RescalaIntercept       =  getParameters()->getRescaleIntercept();
00054 
00055 //EED
00056 //      RescalaSlope            = 1;
00057 //      RescalaIntercept        = 0;
00058 
00059    vtkImageShiftScale *scale = vtkImageShiftScale::New();
00060    scale->SetInput( crop->GetOutput( ) );
00061    scale->SetScale(RescalaSlope);
00062    scale->SetShift(0);
00063    scale->SetOutputScalarTypeToShort();
00064    scale->Update();
00065    vtkImageShiftScale *shift = vtkImageShiftScale::New();
00066    shift->SetInput( scale->GetOutput( ) );
00067    shift->SetScale(1); 
00068    shift->SetShift(RescalaIntercept);
00069    shift->SetOutputScalarTypeToShort();
00070    shift->Update();
00071 
00072 
00073    vtkImageThreshold *threshold = vtkImageThreshold::New();
00074    threshold->SetInput( shift->GetOutput( ) );
00075    threshold->ThresholdByUpper (-10000); 
00076    threshold->ThresholdByLower (-1); 
00077    threshold->SetInValue(0);   
00078    threshold->ReplaceOutOff ();
00079    threshold->ReplaceInOn ();
00080    threshold->SetOutputScalarTypeToUnsignedShort();
00081    
00082    vtkImageResample* ir = vtkImageResample::New( );
00083    ir->SetInput( threshold->GetOutput( ) );
00084    ir->SetDimensionality( 3 );
00085    double voxelSize= getParameters( )->getVoxelSize( );
00086    ir->SetAxisOutputSpacing( 0, voxelSize );
00087    ir->SetAxisOutputSpacing( 1, voxelSize );
00088    ir->SetAxisOutputSpacing( 2, voxelSize );
00089 
00090 
00091 //      ir->SetAxisMagnificationFactor(0,1/getParameters( )->getVoxelSize( )); 
00092 
00093 //EEDx1
00094    ir->InterpolateOn( );
00095 //   ir->InterpolateOff( );
00096 
00097 
00098    ir->Update( );    //important
00099    ir->GetOutput()->GetExtent( ext );
00100  
00104    vtkImageChangeInformation* change = vtkImageChangeInformation::New();
00105    change->SetInput( ir->GetOutput() );
00106    change->SetExtentTranslation( -ext[0], -ext[2], -ext[4] );
00107    change->SetOutputSpacing ( voxelSize , voxelSize , voxelSize );
00108    change->Update();    //important
00109  
00110    _volume = new kVolume( change->GetOutput( ) );
00111 
00112         _marimagedata->AddImageData( _volume->castVtk() );
00113         double spc[3];
00114         vol->castVtk()->GetSpacing(spc);
00115         _marimagedata->SetSpcOriginal( spc );
00116         _marimagedata->SetVoiOriginal(voi);
00117 
00118 
00119    crop         -> Delete();
00120    ir           -> Delete();
00121    change       -> Delete();
00122    shift        -> Delete();
00123    scale        -> Delete();
00124    threshold-> Delete();
00125  }
00126 
00127 
00128 // -------------------------------------------------------------------------
00129 void marDynData::reset( )
00130 {
00131   //Efface car _volume est juste un pointeur ver _volume de marGdcmDicom qui
00132   //est detruit par le reset correspondant
00133 // PS ->   #ifndef DXMM
00134 // PS ->   if( _volume ) delete _volume;
00135 // PS ->   #endif
00136   _volume = NULL;
00137 }
00138 
00139 // -------------------------------------------------------------------------
00140 void marDynData::copyFrom( const marObject& from )
00141 {
00142   reset( );
00143   _volume = new kVolume( *( ( ( marDynData& )from )._volume ) );
00144 }
00145 
00146 // -------------------------------------------------------------------------
00147 bool marDynData::save( std::ofstream& os )
00148 {
00149     void* data = _volume->getData1D( );
00150     uint dims[ 3 ];
00151     ulong dSize = _volume->getRawSizeInBytes( );
00152     double sizes[ 3 ];
00153     kVolume::Type type = _volume->getType( );
00154 
00155     dims[ 0 ] = _volume->getXdim( );
00156     dims[ 1 ] = _volume->getYdim( );
00157     dims[ 2 ] = _volume->getZdim( );
00158     sizes[ 0 ] = _volume->getXsize( );
00159     sizes[ 1 ] = _volume->getYsize( );
00160     sizes[ 2 ] = _volume->getZsize( );
00161 
00162     os.write( ( const char* )dims, 3 * sizeof( uint ) );
00163     os.write( ( const char* )sizes, 3 * sizeof( double ) );
00164     os.write( ( const char* )&type, sizeof( kVolume::Type ) );
00165     os.write( ( const char* )&dSize, sizeof( ulong ) );
00166     os.write( ( const char* )data, dSize );
00167 
00168     return( true );
00169 }
00170 
00171 // -------------------------------------------------------------------------
00172 bool marDynData::load( std::ifstream& is )
00173 {
00174     uchar* data;
00175     uint dims[ 3 ];
00176     ulong dSize;
00177     double sizes[ 3 ];
00178     kVolume::Type type;
00179 
00180     reset( );
00181 
00182     is.read( ( char* )dims, 3 * sizeof( uint ) );
00183     is.read( ( char* )sizes, 3 * sizeof( double ) );
00184     is.read( ( char* )&type, sizeof( kVolume::Type ) );
00185     is.read( ( char* )&dSize, sizeof( ulong ) );
00186     data = new uchar[ dSize ];
00187     is.read( ( char* )data, dSize );
00188         
00189     _volume = new kVolume( type, dims, sizes, ( void* )data );
00190         
00191     //cleaning
00192     delete[] data;
00193 
00194     return( true );
00195 }
00196 
00197 // ----------------------------------------------------------------------------------------------
00198 marImageData *marDynData::GetMarImageData()
00199 {
00200         return _marimagedata;
00201 }
00202 // ----------------------------------------------------------------------------------------------
00203 
00204 // eof - dynData.cxx

Generated on 18 Mar 2010 for creaMaracasVisu_lib by  doxygen 1.6.1