]> Creatis software - creaVtk.git/blob - lib/creaVtk/creaVtkFromVtk4DToItk4D.cpp
2178 creaVtk Bug New High Error in creaVtkFromVtk4DToItk4 box output.
[creaVtk.git] / lib / creaVtk / creaVtkFromVtk4DToItk4D.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 #                        pour la Sante)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and
11 #  abiding by the rules of distribution of free software. You can  use,
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B
13 #  license as circulated by CEA, CNRS and INRIA at the following URL
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability.
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27
28 #include "creaVtkFromVtk4DToItk4D.h"
29 //#include "itkImage.h"
30
31 //#include <itkImageRegionConstIteratorWithIndex.h>
32
33
34 //#include "itkImageRegionConstIterator.h"
35 #include "itkImageRegionIterator.h"
36 // #include "itkImageFileReader.h"
37 #include <vector>
38
39 //template <class TImage>
40 //creaVtkFromVtk4DToItk4D<TImage>::creaVtkFromVtk4DToItk4D()
41 creaVtkFromVtk4DToItk4D::creaVtkFromVtk4DToItk4D()
42 {
43 }
44
45 //EED template <class TImage>
46 //EED creaVtkFromVtk4DToItk4D<TImage>::~creaVtkFromVtk4DToItk4D()
47 creaVtkFromVtk4DToItk4D::~creaVtkFromVtk4DToItk4D()
48 {
49 }
50
51 //EED template <class TImage>
52 //EED void creaVtkFromVtk4DToItk4D<TImage>::SetImage4D(TImage *image)
53 //EED {
54 //EED   _image4D = image;
55 //EED }
56
57
58 Image4DType::Pointer creaVtkFromVtk4DToItk4D::GetResult()
59 {
60    return _image4D;
61 }
62
63
64 //EED template <class TImage>
65 //EED void creaVtkFromVtk4DToItk4D<TImage>::SetImage4Dvtk( std::vector<vtkImageData*> image)
66 void creaVtkFromVtk4DToItk4D::SetImage4Dvtk( std::vector<vtkImageData*> image)
67 {
68         _image4Dvtk = image;
69 }
70
71 //EED template <class TImage>
72 //EED std::vector<itk::Image< unsigned char > >  creaVtkFromVtk4DToItk4D<TImage>::Process()
73 void creaVtkFromVtk4DToItk4D::Process()
74 {
75         /*      
76         int dir;        
77         double pv[65];
78         int count = 0;
79         typedef itk::ImageLinearConstIteratorWithIndex< TImage > ConstIteratorType;     
80         const unsigned int Dimension = 4;
81   typedef unsigned char PixelType;
82   typedef itk::Image< PixelType, Dimension > ImageType;
83         
84         ConstIteratorType inputIt( _image4D, inputRegion );     
85         inputIt.SetDirection(3);        
86         inputIt.GoToBegin();
87
88   ImageType::RegionType outputRegion;
89   ImageType::RegionType::IndexType outputStart;
90   outputStart[0] = 0;
91   outputStart[1] = 0;
92   outputRegion.SetSize( size );
93   outputRegion.SetIndex( outputStart );
94
95         while( !inputIt.IsAtEnd() )
96         {
97                         inputIt.GoToBeginOfLine();
98                         count = 0;
99                         while( !inputIt.IsAtEndOfLine() )
100                         {       
101                                 pv[count] = inputIt.Get();
102                                 ++inputIt;
103                                 ++count;                                
104                         }
105         }
106 */
107
108
109 /* EED2
110   const unsigned int Dimension = 4;
111   typedef unsigned char PixelType;
112   typedef itk::Image< PixelType, Dimension > ImageType;
113   typedef itk::ImageRegionConstIterator< ImageType > ConstIteratorType;
114   typedef itk::ImageRegionIterator< ImageType> IteratorType;
115         
116   //typedef itk::ImageFileReader< ImageType > ReaderType;
117   //typedef itk::ImageFileWriter< ImageType > WriterType;
118   
119   ImageType::RegionType inputRegion;
120   ImageType::RegionType::IndexType inputStart;
121   ImageType::RegionType::SizeType  size;
122   inputStart[0] = ::atoi( "20" );
123   inputStart[1] = ::atoi( "70" );
124   size[0]  = ::atoi( "210" );
125   size[1]  = ::atoi( "140" );
126   inputRegion.SetSize( size );
127   inputRegion.SetIndex( inputStart );
128
129   ImageType::RegionType outputRegion;
130   ImageType::RegionType::IndexType outputStart;
131   outputStart[0] = 0;
132   outputStart[1] = 0;
133         outputStart[2] = 0;
134   outputStart[3] = 0;
135   outputRegion.SetSize( size );
136   outputRegion.SetIndex( outputStart );
137 */
138
139         /*
140   ReaderType::Pointer reader = ReaderType::New();
141         reader->SetFileName( "foo.vtk" );
142   try
143     {
144     reader->Update();
145     }
146         catch ( itk::ExceptionObject &err)
147     {
148     std::cerr << "ExceptionObject caught !" << std::endl;
149     std::cerr << err << std::endl;
150     return -1;
151     }
152
153   if ( ! reader->GetOutput()->GetRequestedRegion().IsInside( inputRegion ) )
154     {
155     std::cerr << "Error" << std::endl;
156     std::cerr << "The region " << inputRegion << "is not contained within the input image region "
157               << reader->GetOutput()->GetRequestedRegion() << std::endl;
158     return -1;
159     }*/
160
161
162
163 /* EED
164         std::vector<ImageType::Pointer> outputImage4D;
165         for(int k = 0 ; k < _image4Dvtk.size() ; k++)
166         {
167                         ImageType::Pointer outputImage = ImageType::New();
168                         outputImage->SetRegions( outputRegion );
169                         const ImageType::SpacingType& spacing = _image4Dvtk[k]->GetSpacing();
170                         const ImageType::PointType& inputOrigin = _image4Dvtk[k]->GetOrigin();
171                         double outputOrigin[ Dimension ];
172                         for(unsigned int i=0; i< Dimension; i++)
173                         {
174                                 outputOrigin[i] = inputOrigin[i] + spacing[i] * inputStart[i];
175                         } // for
176                         outputImage->SetSpacing( spacing );
177                         outputImage->SetOrigin(  outputOrigin );
178                         outputImage->Allocate();
179
180                         ConstIteratorType inputIt( _image4Dvtk[k], inputRegion  );
181                         IteratorType outputIt( outputImage, outputRegion );
182                         inputIt.GoToBegin();
183                         outputIt.GoToBegin();
184                         
185                         while( !outputIt.IsAtEnd() )
186                         {
187                                 outputIt.Set( inputIt.Get() );
188                                 ++inputIt;
189                                 ++outputIt;
190                         } // while 
191 //EED                   outputImage4D[k]=outputImage;
192         } // for k
193 //EED   return outputImage4D;
194
195 */ 
196
197
198
199 // http://www.itk.org/pipermail/insight-users/2003-December/005796.html
200 // http://www.itk.org/Wiki/ITK/Examples/ImageProcessing/InvertIntensityImageFilter
201
202
203   int ext[6];
204   int dimX, dimY, dimZ;
205   long int sizeimage;
206   unsigned int k, i;
207
208   _image4Dvtk[0]->GetWholeExtent(ext);
209   dimX          = ext[1]-ext[0]+1;
210   dimY          = ext[3]-ext[2]+1;
211   dimZ          = ext[5]-ext[4]+1;
212   sizeimage     = dimX*dimY*dimZ;
213  
214
215
216   typedef itk::ImageRegionIterator< Image4DType >  Iterator4D;  
217 //  typedef itk::ImageRegionConstIterator< Image4DType >  Iterator4D;  
218 //  typedef itk::ImageRegionConstIteratorWithIndex< Image4DType >  Iterator4D;  
219
220
221   _image4D = Image4DType::New();
222
223   Image4DType::RegionType region;
224   Image4DType::IndexType start;
225   start[0] = 0;
226   start[1] = 0;
227   start[2] = 0;
228   start[3] = 0;
229  
230   Image4DType::SizeType size;
231   unsigned int NumRows          = dimX;
232   unsigned int NumCols          = dimY;
233   unsigned int NumSlices        = dimZ;
234   unsigned int NumVolumes       = _image4Dvtk.size();
235   size[0] = NumRows;
236   size[1] = NumCols;
237   size[2] = NumSlices;
238   size[3] = NumVolumes;
239  
240   region.SetSize(size);
241   region.SetIndex(start);
242  
243   _image4D->SetRegions(region);
244   _image4D->Allocate();
245
246     
247 //    Iterator4D it4( image4D, image4D->GetBufferedRegion() );
248     Iterator4D it4( _image4D, _image4D->GetLargestPossibleRegion() );
249     it4.GoToBegin();
250         for(k = 0 ; k < _image4Dvtk.size() ; k++)
251         {
252                 unsigned short * ptrVtkImage;
253                 ptrVtkImage = (unsigned short *)_image4Dvtk[k]->GetScalarPointer();
254                 for (i=0; i< sizeimage;i++)
255                 {
256                         it4.Set( *ptrVtkImage );
257 //                      *it4 = *ptrVtkImage;
258 //                      unsigned short q= it4.Get();
259                         ++it4;
260                         ptrVtkImage++;
261                 }
262         }
263 }
264 //---------------------------------------------
265 //Method template
266 //---------------------------------------------
267 /*
268 void creaVtkFromVtk4DToItk4D::FunctionName(int& parameterA)
269 {
270   parameterA = 2 * parameterA;
271   return;
272 }
273 */