]> Creatis software - clitk.git/blob - common/vvFromITK.h
Merge branch 'master' into extentSimon
[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
32 template<unsigned int Dim, class PixelType>
33 static inline void ReadTimeSequence (vvImage::Pointer& vv_image, typename itk::Image<PixelType,Dim>::Pointer input, bool time_sequence=false)
34 {
35   typedef itk::Image< PixelType, Dim > InputImageType;
36   typedef itk::Image< PixelType,  Dim - 1 >    ItkImageType;
37   typedef itk::ExtractImageFilter<InputImageType, ItkImageType> FilterType;
38
39   //extract the 3D slices and put them in a std::vector<vtkImageData*>
40   input->UpdateOutputInformation();
41   typename InputImageType::RegionType inputRegion = input->GetLargestPossibleRegion();
42   typename InputImageType::SizeType inputSize = inputRegion.GetSize();
43   typename InputImageType::IndexType start = inputRegion.GetIndex();
44   typename InputImageType::SizeType extractedRegionSize = inputSize;
45   typename InputImageType::RegionType extractedRegion;
46   extractedRegionSize[Dim - 1] = 0;
47   extractedRegion.SetSize(extractedRegionSize);
48
49   for (unsigned int i = 0; i < inputSize[Dim - 1]; i++) {
50     start[Dim - 1] = i;
51     extractedRegion.SetIndex(start);
52
53     typename FilterType::Pointer filter = FilterType::New();
54     filter->SetDirectionCollapseToSubmatrix();
55     filter->SetExtractionRegion(extractedRegion);
56     filter->SetInput(input);
57     filter->ReleaseDataFlagOn();
58     vv_image->AddItkImage<ItkImageType>(filter->GetOutput());
59     vv_image->ComputeScalarRangeBase<PixelType, Dim-1>(filter->GetOutput());
60   }
61   vv_image->SetTimeSpacing(input->GetSpacing()[Dim-1]);
62   vv_image->SetTimeOrigin(input->GetOrigin()[Dim-1]);
63 }
64
65 template<unsigned int Dim, class PixelType>
66 struct vvImageFromITK_Impl
67 {
68   static vvImage::Pointer Do (typename itk::Image<PixelType,Dim>::Pointer input, bool time_sequence=false)
69   {
70     vvImage::Pointer vv_image=vvImage::New();
71     typedef itk::Image< PixelType, Dim > InputImageType;
72
73     if (time_sequence) //The time sequence case: create a series of VTK images
74       ReadTimeSequence<Dim,PixelType>(vv_image, input, time_sequence);
75     else { //Dim == 1,2,3 and not time_sequence
76       vv_image->AddItkImage<InputImageType>(input);
77       vv_image->ComputeScalarRangeBase<PixelType, Dim>(input);
78     }
79     return vv_image;
80   }
81 };
82
83 template<class PixelType>
84 struct vvImageFromITK_Impl<4u, PixelType>
85 {
86   static vvImage::Pointer Do (typename itk::Image<PixelType,4u>::Pointer input, bool time_sequence=false)
87   {
88     vvImage::Pointer vv_image = vvImage::New();
89     ReadTimeSequence<4u,PixelType>(vv_image, input, time_sequence);
90     return vv_image;
91   }
92 };
93
94 template<unsigned int Dim, class PixelType> vvImage::Pointer vvImageFromITK(typename itk::Image<PixelType,Dim>::Pointer input, bool time_sequence=false)
95 {
96     assert(Dim < 5 && Dim > 0); // We don't handle anything higher than 4-dimensional (for the moment :-p)
97     return vvImageFromITK_Impl<Dim, PixelType>::Do(input, time_sequence);
98 }
99 //------------------------------------------------------------------------------
100
101 #endif //vvImageFromITK