]> Creatis software - creaVtk.git/blob - lib/creaVtk/creaVtkFromVtk4DToItk4D.cpp~
a54c7d15816d4933eca2d10d045afc69eb457d09
[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 #include "itkImageLinearConstIteratorWithIndex.h"
31 #include "itkImageRegionConstIterator.h"
32 #include "itkImageRegionIterator.h"
33 #include "itkImageFileReader.h"
34 #include <vector>
35
36 template <class TImage>
37 creaVtkFromVtk4DToItk4D<TImage>::creaVtkFromVtk4DToItk4D()
38 {
39 }
40
41 template <class TImage>
42 creaVtkFromVtk4DToItk4D<TImage>::~creaVtkFromVtk4DToItk4D()
43 {
44 }
45
46 template <class TImage>
47 void creaVtkFromVtk4DToItk4D<TImage>::SetImage4D(TImage *image)
48 {
49    _image4D = image;
50 }
51
52 template <class TImage>
53 void creaVtkFromVtk4DToItk4D<TImage>::SetImage4Dvtk(std::vector<vtkImageData*> image)
54 {
55         image4Dvtk = image;
56 }
57
58 template <class TImage>
59 std::vector<itk::Image< unsigned char > >  creaVtkFromVtk4DToItk4D<TImage>::Process()
60 {
61         /*      
62         int dir;        
63         double pv[65];
64         int count = 0;
65         typedef itk::ImageLinearConstIteratorWithIndex< TImage > ConstIteratorType;     
66         const unsigned int Dimension = 4;
67   typedef unsigned char PixelType;
68   typedef itk::Image< PixelType, Dimension > ImageType;
69         
70         ConstIteratorType inputIt( _image4D, inputRegion );     
71         inputIt.SetDirection(3);        
72         inputIt.GoToBegin();
73
74   ImageType::RegionType outputRegion;
75   ImageType::RegionType::IndexType outputStart;
76   outputStart[0] = 0;
77   outputStart[1] = 0;
78   outputRegion.SetSize( size );
79   outputRegion.SetIndex( outputStart );
80
81         while( !inputIt.IsAtEnd() )
82         {
83                         inputIt.GoToBeginOfLine();
84                         count = 0;
85                         while( !inputIt.IsAtEndOfLine() )
86                         {       
87                                 pv[count] = inputIt.Get();
88                                 ++inputIt;
89                                 ++count;                                
90                         }
91         }
92 */
93         const unsigned int Dimension = 4;
94   typedef unsigned char PixelType;
95   typedef itk::Image< PixelType, Dimension > ImageType;
96   typedef itk::ImageRegionConstIterator< ImageType > ConstIteratorType;
97   typedef itk::ImageRegionIterator< ImageType> IteratorType;
98         
99   //typedef itk::ImageFileReader< ImageType > ReaderType;
100   //typedef itk::ImageFileWriter< ImageType > WriterType;
101   
102   ImageType::RegionType inputRegion;
103   ImageType::RegionType::IndexType inputStart;
104   ImageType::RegionType::SizeType  size;
105   inputStart[0] = ::atoi( "20" );
106   inputStart[1] = ::atoi( "70" );
107   size[0]  = ::atoi( "210" );
108   size[1]  = ::atoi( "140" );
109   inputRegion.SetSize( size );
110   inputRegion.SetIndex( inputStart );
111
112   ImageType::RegionType outputRegion;
113   ImageType::RegionType::IndexType outputStart;
114   outputStart[0] = 0;
115   outputStart[1] = 0;
116         outputStart[2] = 0;
117   outputStart[3] = 0;
118   outputRegion.SetSize( size );
119   outputRegion.SetIndex( outputStart );
120
121         /*
122   ReaderType::Pointer reader = ReaderType::New();
123         reader->SetFileName( "foo.vtk" );
124   try
125     {
126     reader->Update();
127     }
128         catch ( itk::ExceptionObject &err)
129     {
130     std::cerr << "ExceptionObject caught !" << std::endl;
131     std::cerr << err << std::endl;
132     return -1;
133     }
134
135   if ( ! reader->GetOutput()->GetRequestedRegion().IsInside( inputRegion ) )
136     {
137     std::cerr << "Error" << std::endl;
138     std::cerr << "The region " << inputRegion << "is not contained within the input image region "
139               << reader->GetOutput()->GetRequestedRegion() << std::endl;
140     return -1;
141     }*/
142         std::vector<ImageType::Pointer> outputImage4D;
143         for(int k = 0 ; k < image4Dvtk.size() ; k++){
144                         ImageType::Pointer outputImage = ImageType::New();
145                         outputImage->SetRegions( outputRegion );
146                         const ImageType::SpacingType& spacing = image4Dvtk[k]->GetSpacing();
147                         const ImageType::PointType& inputOrigin = image4Dvtk[k]->GetOrigin();
148                         double outputOrigin[ Dimension ];
149                         for(unsigned int i=0; i< Dimension; i++)
150                                 {
151                                 outputOrigin[i] = inputOrigin[i] + spacing[i] * inputStart[i];
152                                 }
153                         outputImage->SetSpacing( spacing );
154                         outputImage->SetOrigin(  outputOrigin );
155                         outputImage->Allocate();
156
157                         ConstIteratorType inputIt( image4Dvtk[k], inputRegion  );
158                         IteratorType outputIt( outputImage, outputRegion );
159                         inputIt.GoToBegin();
160                         outputIt.GoToBegin();
161                         
162                         while( !outputIt.IsAtEnd() )
163                                 {
164                                 outputIt.Set( inputIt.Get() );
165                                 ++inputIt;
166                                 ++outputIt;
167                                 }
168                         outputImage4D[k]=outputImage;
169         }
170         return outputImage4D;
171 }
172 //---------------------------------------------
173 //Method template
174 //---------------------------------------------
175 /*
176 void creaVtkFromVtk4DToItk4D::FunctionName(int& parameterA)
177 {
178   parameterA = 2 * parameterA;
179   return;
180 }
181 */