]> Creatis software - clitk.git/blob - common/vvFromITK.h
Implemented new vvImage scheme: the itk to vtk image converter is now kept as a class...
[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
21 // vv
22 #include "vvImage.h"
23
24 // itk
25 #include <itkExtractImageFilter.h>
26
27 //------------------------------------------------------------------------------
28 /**Converts the itk image to vv, handling the 4D problem
29  * The time_sequence boolean specifies that the image is to be interpreted as a time sequence,
30  * even if its dim is < 4. */
31 template<unsigned int Dim, class PixelType> vvImage::Pointer vvImageFromITK(typename itk::Image<PixelType,Dim>::Pointer input, bool time_sequence=false)
32 {
33     assert(Dim < 5 && Dim > 0); // We don't handle anything higher than 4-dimensional (for the moment :-p)
34     vvImage::Pointer vv_image=vvImage::New();
35     vv_image->Init(); //Delete any existing images
36     typedef itk::Image< PixelType, Dim > InputImageType;
37
38     if (Dim == 4 || time_sequence) //The time sequence case: create a series of VTK images
39     {
40         typedef itk::Image< PixelType,  Dim - 1 >    ItkImageType;
41         typedef itk::ExtractImageFilter<InputImageType, ItkImageType> FilterType;
42
43         //extract the 3D slices and put them in a std::vector<vtkImageData*>
44         typename InputImageType::RegionType inputRegion = input->GetLargestPossibleRegion();
45         typename InputImageType::SizeType inputSize = inputRegion.GetSize();
46         typename InputImageType::IndexType start = inputRegion.GetIndex();
47         typename InputImageType::SizeType extractedRegionSize = inputSize;
48         typename InputImageType::RegionType extractedRegion;
49         extractedRegionSize[Dim - 1] = 0;
50         extractedRegion.SetSize(extractedRegionSize);
51
52         for (unsigned int i = 0; i < inputSize[Dim - 1]; i++) {
53             start[Dim - 1] = i;
54             extractedRegion.SetIndex(start);
55
56             typename FilterType::Pointer filter = FilterType::New();
57             filter->SetExtractionRegion(extractedRegion);
58             filter->SetInput(input);
59             filter->ReleaseDataFlagOn();
60
61             try {
62                 filter->Update();
63             }
64             catch ( itk::ExceptionObject & err ) {
65                 std::cerr << "Error while setting vvImage from ITK (Dim==4) [Extract phase]"
66                           << " " << err << std::endl;
67                 return vv_image;
68             }
69             vv_image->AddItkImage<ItkImageType>(filter->GetOutput());
70         }
71         vv_image->SetTimeSpacing(input->GetSpacing()[Dim-1]);
72         vv_image->SetTimeOrigin(input->GetOrigin()[Dim-1]);
73     }
74     else //Dim == 1,2,3 and not time_sequence
75     {
76         vv_image->AddItkImage<InputImageType>(input);
77     }
78     return vv_image;
79 }
80 //------------------------------------------------------------------------------
81
82 #endif //vvImageFromITK