]> Creatis software - clitk.git/blob - common/vvFromITK.h
Merge branch 'master' of /home/romulo/creatis/clitk3-git-shared/clitk3
[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://www.centreleonberard.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     typedef itk::Image< PixelType, Dim > InputImageType;
36
37     if (Dim == 4 || time_sequence) //The time sequence case: create a series of VTK images
38     {
39         typedef itk::Image< PixelType,  Dim - 1 >    ItkImageType;
40         typedef itk::ExtractImageFilter<InputImageType, ItkImageType> FilterType;
41
42         //extract the 3D slices and put them in a std::vector<vtkImageData*>
43         input->UpdateOutputInformation();
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->SetDirectionCollapseToSubmatrix();
58             filter->SetExtractionRegion(extractedRegion);
59             filter->SetInput(input);
60             filter->ReleaseDataFlagOn();
61             vv_image->AddItkImage<ItkImageType>(filter->GetOutput());
62         }
63         vv_image->SetTimeSpacing(input->GetSpacing()[Dim-1]);
64         vv_image->SetTimeOrigin(input->GetOrigin()[Dim-1]);
65     }
66     else //Dim == 1,2,3 and not time_sequence
67     {
68         vv_image->AddItkImage<InputImageType>(input);
69     }
70     return vv_image;
71 }
72 //------------------------------------------------------------------------------
73
74 #endif //vvImageFromITK