]> Creatis software - clitk.git/blob - common/vvFromITK.h
added the new headers
[clitk.git] / common / vvFromITK.h
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #ifndef VVIMAGEFROMITK_H
19 #define VVIMAGEFROMITK_H
20 #include "vvImage.h"
21 //#include <itkImage.h>
22 #include <itkExtractImageFilter.h>
23 #include <itkImageToVTKImageFilter.h>
24
25 /**Converts the itk image to vv, handling the 4D problem
26  * The time_sequence boolean specifies that the image is to be interpreted as a time sequence,
27  * even if its dim is < 4. */
28 template<unsigned int Dim, class PixelType> vvImage::Pointer vvImageFromITK(typename itk::Image<PixelType,Dim>::Pointer input, bool time_sequence=false)
29 {
30     assert(Dim < 5 && Dim > 0); // We don't handle anything higher than 4-dimensional (for the moment :-p)
31     vvImage::Pointer vv_image=vvImage::New();
32     vv_image->Init(); //Delete any existing images
33     typedef itk::Image< PixelType, Dim > InputImageType;
34
35     if (Dim == 4 || time_sequence) //The time sequence case: create a series of VTK images
36     {
37         typedef itk::Image< PixelType,  Dim - 1 >    ConnectorImageType;
38         typedef itk::ImageToVTKImageFilter <ConnectorImageType> ConnectorType;
39         typedef itk::ExtractImageFilter<InputImageType,ConnectorImageType> FilterType;
40
41         typename FilterType::Pointer filter = FilterType::New();
42         typename ConnectorType::Pointer connector = ConnectorType::New();
43
44         //extract the 3D slices and put them in a std::vector<vtkImageData*>
45         typename InputImageType::RegionType inputRegion = input->GetLargestPossibleRegion();
46         typename InputImageType::SizeType inputSize = inputRegion.GetSize();
47
48         typename InputImageType::SizeType extractedRegionSize = inputSize;
49         typename InputImageType::RegionType extractedRegion;
50         extractedRegionSize[Dim - 1] = 0;
51         extractedRegion.SetSize(extractedRegionSize);
52
53         filter->SetInput(input);
54         connector->SetInput(filter->GetOutput());
55
56         typename InputImageType::IndexType start = inputRegion.GetIndex();
57
58         for (unsigned int i = 0; i < inputSize[Dim - 1]; i++) {
59             start[Dim - 1] = i;
60             extractedRegion.SetIndex(start);
61             filter->SetExtractionRegion(extractedRegion);
62             try {
63                 filter->Update();
64             }
65             catch ( itk::ExceptionObject & err ) {
66                 std::cerr << "Error while setting vvImage from ITK (Dim==4) [Extract phase]"
67                           << " " << err << std::endl;
68                 return vv_image;
69             }
70             try {
71                 connector->Update();
72             }
73             catch ( itk::ExceptionObject & err ) {
74                 std::cerr << "Error while setting vvImage from ITK (Dim==4) [Connect phase]"
75                           << " " << err << std::endl;
76                 return vv_image;
77             }
78             vtkImageData *image = vtkImageData::New();
79             image->DeepCopy(connector->GetOutput());
80             vv_image->AddImage(image);
81         }
82     }
83     else //Dim == 1,2,3 and not time_sequence
84     {
85         typedef itk::Image< PixelType,  Dim >    ConnectorImageType;
86         typedef itk::ImageToVTKImageFilter <ConnectorImageType> ConnectorType;
87         typename ConnectorType::Pointer connector = ConnectorType::New();
88         connector->SetInput(input);
89
90         try {
91             connector->Update();
92         }
93         catch ( itk::ExceptionObject & err ) {
94             std::cerr << "Error while setting vvImage from ITK (Dim==3)"
95                       << " " << err << std::endl;
96             return vv_image;
97         }
98         vtkImageData *image = vtkImageData::New();
99         image->DeepCopy(connector->GetOutput());
100         vv_image->AddImage(image);
101     }
102     return vv_image;
103 }
104
105 #endif //vvImageFromITK