]> Creatis software - clitk.git/blob - common/vvFromITK.h
Initial revision
[clitk.git] / common / vvFromITK.h
1 /*=========================================================================
2
3  Program:   vv
4  Module:    $RCSfile: vvFromITK.h,v $
5  Language:  C++
6  Date:      $Date: 2010/01/06 13:32:01 $
7  Version:   $Revision: 1.1 $
8  Author :   Joel Schaerer (joel.schaerer@insa-lyon.fr)
9
10 Copyright (C) 2008
11 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
12 CREATIS-LRMN http://www.creatis.insa-lyon.fr
13
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, version 3 of the License.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
26 =========================================================================*/
27
28 #ifndef vvImageFromITK_h
29 #define vvImageFromITK_h
30
31 #include "vvImage.h"
32 #include <itkImage.h>
33 #include <itkExtractImageFilter.h>
34 #include "itkImageToVTKImageFilter.h"
35
36 /**Converts the itk image to vv, handling the 4D problem
37  * The time_sequence boolean specifies that the image is to be interpreted as a time sequence,
38  * even if its dim is < 4. */
39 template<unsigned int Dim, class PixelType> vvImage::Pointer vvImageFromITK(typename itk::Image<PixelType,Dim>::Pointer input, bool time_sequence=false)
40 {
41     assert(Dim < 5 && Dim > 0); // We don't handle anything higher than 4-dimensional (for the moment :-p)
42     vvImage::Pointer vv_image=vvImage::New();
43     vv_image->Init(); //Delete any existing images
44     typedef itk::Image< PixelType, Dim > InputImageType;
45
46     if (Dim == 4 || time_sequence) //The time sequence case: create a series of VTK images
47     {
48         typedef itk::Image< PixelType,  Dim - 1 >    ConnectorImageType;
49         typedef itk::ImageToVTKImageFilter <ConnectorImageType> ConnectorType;
50         typedef itk::ExtractImageFilter<InputImageType,ConnectorImageType> FilterType;
51
52         typename FilterType::Pointer filter = FilterType::New();
53         typename ConnectorType::Pointer connector = ConnectorType::New();
54
55         //extract the 3D slices and put them in a std::vector<vtkImageData*>
56         typename InputImageType::RegionType inputRegion = input->GetLargestPossibleRegion();
57         typename InputImageType::SizeType inputSize = inputRegion.GetSize();
58
59         typename InputImageType::SizeType extractedRegionSize = inputSize;
60         typename InputImageType::RegionType extractedRegion;
61         extractedRegionSize[Dim - 1] = 0;
62         extractedRegion.SetSize(extractedRegionSize);
63
64         filter->SetInput(input);
65         connector->SetInput(filter->GetOutput());
66
67         typename InputImageType::IndexType start = inputRegion.GetIndex();
68
69         for (unsigned int i = 0; i < inputSize[Dim - 1]; i++) {
70             start[Dim - 1] = i;
71             extractedRegion.SetIndex(start);
72             filter->SetExtractionRegion(extractedRegion);
73             try {
74                 filter->Update();
75             }
76             catch ( itk::ExceptionObject & err ) {
77                 std::cerr << "Error while setting vvImage from ITK (Dim==4) [Extract phase]"
78                           << " " << err << std::endl;
79                 return vv_image;
80             }
81             try {
82                 connector->Update();
83             }
84             catch ( itk::ExceptionObject & err ) {
85                 std::cerr << "Error while setting vvImage from ITK (Dim==4) [Connect phase]"
86                           << " " << err << std::endl;
87                 return vv_image;
88             }
89             vtkImageData *image = vtkImageData::New();
90             image->DeepCopy(connector->GetOutput());
91             vv_image->AddImage(image);
92         }
93     }
94     else //Dim == 1,2,3 and not time_sequence
95     {
96         typedef itk::Image< PixelType,  Dim >    ConnectorImageType;
97         typedef itk::ImageToVTKImageFilter <ConnectorImageType> ConnectorType;
98         typename ConnectorType::Pointer connector = ConnectorType::New();
99         connector->SetInput(input);
100
101         try {
102             connector->Update();
103         }
104         catch ( itk::ExceptionObject & err ) {
105             std::cerr << "Error while setting vvImage from ITK (Dim==3)"
106                       << " " << err << std::endl;
107             return vv_image;
108         }
109         vtkImageData *image = vtkImageData::New();
110         image->DeepCopy(connector->GetOutput());
111         vv_image->AddImage(image);
112     }
113     return vv_image;
114 }
115
116 #endif //vvImageFromITK