]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/kernel/marDynData.cpp
#3109 creaMaracasVisu Bug New Normal - branch vtk7itk4 compilation with vtk7
[creaMaracasVisu.git] / lib / maracasVisuLib / src / kernel / marDynData.cpp
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 /*=========================================================================
27
28   Program:   wxMaracas
29   Module:    $RCSfile: marDynData.cpp,v $
30   Language:  C++
31   Date:      $Date: 2012/11/15 14:16:12 $
32   Version:   $Revision: 1.4 $
33
34   Copyright: (c) 2002, 2003
35   License:
36   
37      This software is distributed WITHOUT ANY WARRANTY; without even 
38      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
39      PURPOSE.  See the above copyright notice for more information.
40
41 =========================================================================*/
42
43 #include <vtkImageResample.h>
44 #include <vtkExtractVOI.h>
45 #include <vtkImageData.h>
46 #include <vtkImageChangeInformation.h>
47 #include <vtkImageShiftScale.h> 
48 #include <vtkImageThreshold.h> 
49 #include "marDynData.h"
50
51 // -------------------------------------------------------------------------
52 marDynData::marDynData( marParameters* p ) : marObject( p ), _volume( NULL )
53 {
54         _marimagedata = new marImageData(NULL); 
55 }
56
57 // -------------------------------------------------------------------------
58 marDynData::~marDynData() 
59 {
60 //      _marimagedata->SetImageData(NULL);
61         delete _marimagedata;
62         reset( );
63 }
64 // -------------------------------------------------------------------------
65
66 void marDynData::loadData( kVolume* vol, int* voi )
67  {
68    int ext[6];
69    reset( );
70  
71    vtkExtractVOI *crop = vtkExtractVOI::New();
72 //EED 2017-01-01 Migration VTK7
73 #if VTK_MAJOR_VERSION <= 5
74    crop->SetInput( vol->castVtk( ) );
75 #else
76    crop->SetInputData( vol->castVtk( ) );
77 #endif
78
79    crop->SetVOI( voi );
80    //crop->SetSampleRate( getParameters( )->getVoxelSize( ), getParameters( )->getVoxelSize( ), getParameters( )->getVoxelSize( ));
81    
82    float RescalaSlope           =  getParameters()->getRescaleSlope();
83    float RescalaIntercept       =  getParameters()->getRescaleIntercept();
84
85 //EED
86 //      RescalaSlope            = 1;
87 //      RescalaIntercept        = 0;
88
89    vtkImageShiftScale *scale = vtkImageShiftScale::New();
90 //EED 2017-01-01 Migration VTK7
91 #if VTK_MAJOR_VERSION <= 5
92    scale->SetInput( crop->GetOutput( ) );
93 #else
94    scale->SetInputData( crop->GetOutput( ) );
95 #endif
96    scale->SetScale(RescalaSlope);
97    scale->SetShift(0);
98    scale->SetOutputScalarTypeToShort();
99    scale->Update();
100    vtkImageShiftScale *shift = vtkImageShiftScale::New();
101 //EED 2017-01-01 Migration VTK7
102 #if VTK_MAJOR_VERSION <= 5
103    shift->SetInput( scale->GetOutput( ) );
104 #else
105    shift->SetInputData( scale->GetOutput( ) );
106 #endif
107    shift->SetScale(1); 
108    shift->SetShift(RescalaIntercept);
109    shift->SetOutputScalarTypeToShort();
110    shift->Update();
111
112
113    vtkImageThreshold *threshold = vtkImageThreshold::New();
114 //EED 2017-01-01 Migration VTK7
115 #if VTK_MAJOR_VERSION <= 5
116    threshold->SetInput( shift->GetOutput( ) );
117 #else
118    threshold->SetInputData( shift->GetOutput( ) );
119 #endif
120    threshold->ThresholdByUpper (-10000); 
121    threshold->ThresholdByLower (-1); 
122    threshold->SetInValue(0);   
123    threshold->ReplaceOutOff ();
124    threshold->ReplaceInOn ();
125    threshold->SetOutputScalarTypeToUnsignedShort();
126    
127    vtkImageResample* ir = vtkImageResample::New( );
128 //EED 2017-01-01 Migration VTK7
129 #if VTK_MAJOR_VERSION <= 5
130    ir->SetInput( threshold->GetOutput( ) );
131 #else
132    ir->SetInputData( threshold->GetOutput( ) );
133 #endif
134    ir->SetDimensionality( 3 );
135    double voxelSize= getParameters( )->getVoxelSize( );
136    ir->SetAxisOutputSpacing( 0, voxelSize );
137    ir->SetAxisOutputSpacing( 1, voxelSize );
138    ir->SetAxisOutputSpacing( 2, voxelSize );
139
140
141 //      ir->SetAxisMagnificationFactor(0,1/getParameters( )->getVoxelSize( )); 
142
143 //EEDx1
144    ir->InterpolateOn( );
145 //   ir->InterpolateOff( );
146
147
148    ir->Update( );    //important
149    ir->GetOutput()->GetExtent( ext );
150  
151   /**
152    * Before we were using kgfoCrop which assume extent start at 0,0,0
153    */
154    vtkImageChangeInformation* change = vtkImageChangeInformation::New();
155 //EED 2017-01-01 Migration VTK7
156 #if VTK_MAJOR_VERSION <= 5
157    change->SetInput( ir->GetOutput() );
158 #else
159    change->SetInputData( ir->GetOutput() );
160 #endif
161
162    change->SetExtentTranslation( -ext[0], -ext[2], -ext[4] );
163    change->SetOutputSpacing ( voxelSize , voxelSize , voxelSize );
164    change->Update();    //important
165  
166    _volume = new kVolume( change->GetOutput( ) );
167
168         _marimagedata->AddImageData( _volume->castVtk() );
169         double spc[3];
170         vol->castVtk()->GetSpacing(spc);
171         _marimagedata->SetSpcOriginal( spc );
172         _marimagedata->SetVoiOriginal(voi);
173
174
175    crop         -> Delete();
176    ir           -> Delete();
177    change       -> Delete();
178    shift        -> Delete();
179    scale        -> Delete();
180    threshold-> Delete();
181  }
182
183
184 // -------------------------------------------------------------------------
185 void marDynData::reset( )
186 {
187   //Efface car _volume est juste un pointeur ver _volume de marGdcmDicom qui
188   //est detruit par le reset correspondant
189 // PS ->   #ifndef DXMM
190 // PS ->   if( _volume ) delete _volume;
191 // PS ->   #endif
192   _volume = NULL;
193 }
194
195 // -------------------------------------------------------------------------
196 void marDynData::copyFrom( const marObject& from )
197 {
198   reset( );
199   _volume = new kVolume( *( ( ( marDynData& )from )._volume ) );
200 }
201
202 // -------------------------------------------------------------------------
203 bool marDynData::save( std::ofstream& os )
204 {
205     void* data = _volume->getData1D( );
206     uint32_t dims[ 3 ];
207     ulong dSize = _volume->getRawSizeInBytes( );  // How many bytes for a 'ulong'? // JPR
208     double sizes[ 3 ];
209     kVolume::Type type = _volume->getType( );
210
211     dims[ 0 ] = _volume->getXdim( );
212     dims[ 1 ] = _volume->getYdim( );
213     dims[ 2 ] = _volume->getZdim( );
214     sizes[ 0 ] = _volume->getXsize( );
215     sizes[ 1 ] = _volume->getYsize( );
216     sizes[ 2 ] = _volume->getZsize( );
217
218     os.write( ( const char* )dims, 3 * sizeof( uint32_t ) );
219     os.write( ( const char* )sizes, 3 * sizeof( double ) );
220     os.write( ( const char* )&type, sizeof( kVolume::Type ) );
221     os.write( ( const char* )&dSize, sizeof( ulong ) );
222     os.write( ( const char* )data, dSize );
223
224     return( true );
225 }
226
227 // -------------------------------------------------------------------------
228 bool marDynData::load( std::ifstream& is )
229 {
230     uint8_t* data;
231     uint32_t dims[ 3 ];
232     ulong dSize;
233     double sizes[ 3 ];
234     kVolume::Type type;
235
236     reset( );
237
238     is.read( ( char* )dims, 3 * sizeof( uint32_t ) );
239     is.read( ( char* )sizes, 3 * sizeof( double ) );
240     is.read( ( char* )&type, sizeof( kVolume::Type ) );
241     is.read( ( char* )&dSize, sizeof( ulong ) );
242     data = new uint8_t[ dSize ];
243     is.read( ( char* )data, dSize );
244         
245     _volume = new kVolume( type, dims, sizes, ( void* )data );
246         
247     //cleaning
248     delete[] data;
249
250     return( true );
251 }
252
253 // ----------------------------------------------------------------------------------------------
254 marImageData *marDynData::GetMarImageData()
255 {
256         return _marimagedata;
257 }
258 // ----------------------------------------------------------------------------------------------
259
260 // eof - dynData.cxx