1 /*=========================================================================
4 Module: $RCSfile: marDynData.cpp,v $
6 Date: $Date: 2009/05/14 13:54:43 $
7 Version: $Revision: 1.2 $
9 Copyright: (c) 2002, 2003
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notice for more information.
16 =========================================================================*/
18 #include <vtkImageResample.h>
19 #include <vtkExtractVOI.h>
20 #include <vtkImageData.h>
21 #include <vtkImageChangeInformation.h>
22 #include <vtkImageShiftScale.h>
23 #include <vtkImageThreshold.h>
24 #include "marDynData.h"
26 // -------------------------------------------------------------------------
27 marDynData::marDynData( marParameters* p ) : marObject( p ), _volume( NULL )
29 _marimagedata = new marImageData(NULL);
32 // -------------------------------------------------------------------------
33 marDynData::~marDynData()
35 // _marimagedata->SetImageData(NULL);
39 // -------------------------------------------------------------------------
41 void marDynData::loadData( kVolume* vol, int* voi )
46 vtkExtractVOI *crop = vtkExtractVOI::New();
47 crop->SetInput( vol->castVtk( ) );
50 //crop->SetSampleRate( getParameters( )->getVoxelSize( ), getParameters( )->getVoxelSize( ), getParameters( )->getVoxelSize( ));
52 float RescalaSlope = getParameters()->getRescaleSlope();
53 float RescalaIntercept = getParameters()->getRescaleIntercept();
57 // RescalaIntercept = 0;
59 vtkImageShiftScale *scale = vtkImageShiftScale::New();
60 scale->SetInput( crop->GetOutput( ) );
61 scale->SetScale(RescalaSlope);
63 scale->SetOutputScalarTypeToShort();
65 vtkImageShiftScale *shift = vtkImageShiftScale::New();
66 shift->SetInput( scale->GetOutput( ) );
68 shift->SetShift(RescalaIntercept);
69 shift->SetOutputScalarTypeToShort();
73 vtkImageThreshold *threshold = vtkImageThreshold::New();
74 threshold->SetInput( shift->GetOutput( ) );
75 threshold->ThresholdByUpper (-10000);
76 threshold->ThresholdByLower (-1);
77 threshold->SetInValue(0);
78 threshold->ReplaceOutOff ();
79 threshold->ReplaceInOn ();
80 threshold->SetOutputScalarTypeToUnsignedShort();
82 vtkImageResample* ir = vtkImageResample::New( );
83 ir->SetInput( threshold->GetOutput( ) );
84 ir->SetDimensionality( 3 );
85 double voxelSize= getParameters( )->getVoxelSize( );
86 ir->SetAxisOutputSpacing( 0, voxelSize );
87 ir->SetAxisOutputSpacing( 1, voxelSize );
88 ir->SetAxisOutputSpacing( 2, voxelSize );
91 // ir->SetAxisMagnificationFactor(0,1/getParameters( )->getVoxelSize( ));
95 // ir->InterpolateOff( );
98 ir->Update( ); //important
99 ir->GetOutput()->GetExtent( ext );
102 * Before we were using kgfoCrop which assume extent start at 0,0,0
104 vtkImageChangeInformation* change = vtkImageChangeInformation::New();
105 change->SetInput( ir->GetOutput() );
106 change->SetExtentTranslation( -ext[0], -ext[2], -ext[4] );
107 change->SetOutputSpacing ( voxelSize , voxelSize , voxelSize );
108 change->Update(); //important
110 _volume = new kVolume( change->GetOutput( ) );
112 _marimagedata->AddImageData( _volume->castVtk() );
114 vol->castVtk()->GetSpacing(spc);
115 _marimagedata->SetSpcOriginal( spc );
116 _marimagedata->SetVoiOriginal(voi);
124 threshold-> Delete();
128 // -------------------------------------------------------------------------
129 void marDynData::reset( )
131 //Efface car _volume est juste un pointeur ver _volume de marGdcmDicom qui
132 //est detruit par le reset correspondant
133 // PS -> #ifndef DXMM
134 // PS -> if( _volume ) delete _volume;
139 // -------------------------------------------------------------------------
140 void marDynData::copyFrom( const marObject& from )
143 _volume = new kVolume( *( ( ( marDynData& )from )._volume ) );
146 // -------------------------------------------------------------------------
147 bool marDynData::save( std::ofstream& os )
149 void* data = _volume->getData1D( );
151 ulong dSize = _volume->getRawSizeInBytes( );
153 kVolume::Type type = _volume->getType( );
155 dims[ 0 ] = _volume->getXdim( );
156 dims[ 1 ] = _volume->getYdim( );
157 dims[ 2 ] = _volume->getZdim( );
158 sizes[ 0 ] = _volume->getXsize( );
159 sizes[ 1 ] = _volume->getYsize( );
160 sizes[ 2 ] = _volume->getZsize( );
162 os.write( ( const char* )dims, 3 * sizeof( uint ) );
163 os.write( ( const char* )sizes, 3 * sizeof( double ) );
164 os.write( ( const char* )&type, sizeof( kVolume::Type ) );
165 os.write( ( const char* )&dSize, sizeof( ulong ) );
166 os.write( ( const char* )data, dSize );
171 // -------------------------------------------------------------------------
172 bool marDynData::load( std::ifstream& is )
182 is.read( ( char* )dims, 3 * sizeof( uint ) );
183 is.read( ( char* )sizes, 3 * sizeof( double ) );
184 is.read( ( char* )&type, sizeof( kVolume::Type ) );
185 is.read( ( char* )&dSize, sizeof( ulong ) );
186 data = new uchar[ dSize ];
187 is.read( ( char* )data, dSize );
189 _volume = new kVolume( type, dims, sizes, ( void* )data );
197 // ----------------------------------------------------------------------------------------------
198 marImageData *marDynData::GetMarImageData()
200 return _marimagedata;
202 // ----------------------------------------------------------------------------------------------