]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/kernel/marDynData.cpp
8ef4b5f483a69ea248de28a2f66024f7303f18c3
[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    crop->SetInput( vol->castVtk( ) );
73
74    crop->SetVOI( voi );
75    //crop->SetSampleRate( getParameters( )->getVoxelSize( ), getParameters( )->getVoxelSize( ), getParameters( )->getVoxelSize( ));
76    
77    float RescalaSlope           =  getParameters()->getRescaleSlope();
78    float RescalaIntercept       =  getParameters()->getRescaleIntercept();
79
80 //EED
81 //      RescalaSlope            = 1;
82 //      RescalaIntercept        = 0;
83
84    vtkImageShiftScale *scale = vtkImageShiftScale::New();
85    scale->SetInput( crop->GetOutput( ) );
86    scale->SetScale(RescalaSlope);
87    scale->SetShift(0);
88    scale->SetOutputScalarTypeToShort();
89    scale->Update();
90    vtkImageShiftScale *shift = vtkImageShiftScale::New();
91    shift->SetInput( scale->GetOutput( ) );
92    shift->SetScale(1); 
93    shift->SetShift(RescalaIntercept);
94    shift->SetOutputScalarTypeToShort();
95    shift->Update();
96
97
98    vtkImageThreshold *threshold = vtkImageThreshold::New();
99    threshold->SetInput( shift->GetOutput( ) );
100    threshold->ThresholdByUpper (-10000); 
101    threshold->ThresholdByLower (-1); 
102    threshold->SetInValue(0);   
103    threshold->ReplaceOutOff ();
104    threshold->ReplaceInOn ();
105    threshold->SetOutputScalarTypeToUnsignedShort();
106    
107    vtkImageResample* ir = vtkImageResample::New( );
108    ir->SetInput( threshold->GetOutput( ) );
109    ir->SetDimensionality( 3 );
110    double voxelSize= getParameters( )->getVoxelSize( );
111    ir->SetAxisOutputSpacing( 0, voxelSize );
112    ir->SetAxisOutputSpacing( 1, voxelSize );
113    ir->SetAxisOutputSpacing( 2, voxelSize );
114
115
116 //      ir->SetAxisMagnificationFactor(0,1/getParameters( )->getVoxelSize( )); 
117
118 //EEDx1
119    ir->InterpolateOn( );
120 //   ir->InterpolateOff( );
121
122
123    ir->Update( );    //important
124    ir->GetOutput()->GetExtent( ext );
125  
126   /**
127    * Before we were using kgfoCrop which assume extent start at 0,0,0
128    */
129    vtkImageChangeInformation* change = vtkImageChangeInformation::New();
130    change->SetInput( ir->GetOutput() );
131    change->SetExtentTranslation( -ext[0], -ext[2], -ext[4] );
132    change->SetOutputSpacing ( voxelSize , voxelSize , voxelSize );
133    change->Update();    //important
134  
135    _volume = new kVolume( change->GetOutput( ) );
136
137         _marimagedata->AddImageData( _volume->castVtk() );
138         double spc[3];
139         vol->castVtk()->GetSpacing(spc);
140         _marimagedata->SetSpcOriginal( spc );
141         _marimagedata->SetVoiOriginal(voi);
142
143
144    crop         -> Delete();
145    ir           -> Delete();
146    change       -> Delete();
147    shift        -> Delete();
148    scale        -> Delete();
149    threshold-> Delete();
150  }
151
152
153 // -------------------------------------------------------------------------
154 void marDynData::reset( )
155 {
156   //Efface car _volume est juste un pointeur ver _volume de marGdcmDicom qui
157   //est detruit par le reset correspondant
158 // PS ->   #ifndef DXMM
159 // PS ->   if( _volume ) delete _volume;
160 // PS ->   #endif
161   _volume = NULL;
162 }
163
164 // -------------------------------------------------------------------------
165 void marDynData::copyFrom( const marObject& from )
166 {
167   reset( );
168   _volume = new kVolume( *( ( ( marDynData& )from )._volume ) );
169 }
170
171 // -------------------------------------------------------------------------
172 bool marDynData::save( std::ofstream& os )
173 {
174     void* data = _volume->getData1D( );
175     uint32_t dims[ 3 ];
176     ulong dSize = _volume->getRawSizeInBytes( );  // How many bytes for a 'ulong'? // JPR
177     double sizes[ 3 ];
178     kVolume::Type type = _volume->getType( );
179
180     dims[ 0 ] = _volume->getXdim( );
181     dims[ 1 ] = _volume->getYdim( );
182     dims[ 2 ] = _volume->getZdim( );
183     sizes[ 0 ] = _volume->getXsize( );
184     sizes[ 1 ] = _volume->getYsize( );
185     sizes[ 2 ] = _volume->getZsize( );
186
187     os.write( ( const char* )dims, 3 * sizeof( uint32_t ) );
188     os.write( ( const char* )sizes, 3 * sizeof( double ) );
189     os.write( ( const char* )&type, sizeof( kVolume::Type ) );
190     os.write( ( const char* )&dSize, sizeof( ulong ) );
191     os.write( ( const char* )data, dSize );
192
193     return( true );
194 }
195
196 // -------------------------------------------------------------------------
197 bool marDynData::load( std::ifstream& is )
198 {
199     uint8_t* data;
200     uint32_t dims[ 3 ];
201     ulong dSize;
202     double sizes[ 3 ];
203     kVolume::Type type;
204
205     reset( );
206
207     is.read( ( char* )dims, 3 * sizeof( uint32_t ) );
208     is.read( ( char* )sizes, 3 * sizeof( double ) );
209     is.read( ( char* )&type, sizeof( kVolume::Type ) );
210     is.read( ( char* )&dSize, sizeof( ulong ) );
211     data = new uint8_t[ dSize ];
212     is.read( ( char* )data, dSize );
213         
214     _volume = new kVolume( type, dims, sizes, ( void* )data );
215         
216     //cleaning
217     delete[] data;
218
219     return( true );
220 }
221
222 // ----------------------------------------------------------------------------------------------
223 marImageData *marDynData::GetMarImageData()
224 {
225         return _marimagedata;
226 }
227 // ----------------------------------------------------------------------------------------------
228
229 // eof - dynData.cxx