]> Creatis software - clitk.git/blob - common/vvToITK.h
again
[clitk.git] / common / vvToITK.h
1 /*=========================================================================
2
3  Program:   vv
4  Language:  C++
5  Author :   Joel Schaerer (joel.schaerer@insa-lyon.fr)
6
7 Copyright (C) 2008
8 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
9 CREATIS-LRMN http://www.creatis.insa-lyon.fr
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, version 3 of the License.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 =========================================================================*/
24 #ifndef vvImageToITK_h
25 #define vvImageToITK_h
26
27 #include <vector>
28 #include "vvImage.h"
29 #include <itkImage.h>
30 #include <itkJoinSeriesImageFilter.h>
31 #include "itkVTKImageToImageFilter.h"
32
33 ///Converts the vv image to itk, handling the 4D problem
34 template<unsigned int Dim, class PixelType> std::vector<typename itk::Image<PixelType,Dim>::ConstPointer> vvImageToITKImageVector(vvImage::Pointer vv_image) ///Converts the vv image to itk, handling the 4D problem
35 {
36     assert(Dim < 5 && Dim > 1); // We don't handle anything higher than 4-dimensional (for the moment :-p)
37     assert(vv_image->GetVTKImages().size() > 0); //we assume there is something to convert
38     typedef itk::Image< PixelType, Dim > OutputImageType;
39     std::vector<typename itk::Image<PixelType,Dim>::ConstPointer> result;
40
41     typedef itk::Image< PixelType, Dim >    ConnectorImageType;
42     typedef itk::VTKImageToImageFilter<ConnectorImageType> ConnectorType;
43     for (unsigned int i = 0; i < vv_image->GetVTKImages().size(); i++)
44     {
45         typename ConnectorType::Pointer connector = ConnectorType::New();
46         connector->SetInput(vv_image->GetVTKImages()[i]);
47         connector->Update();
48         result.push_back(connector->GetOutput());
49     }
50     return result;
51 }
52
53 ///Converts the vv image to itk, handling the 4D problem
54 template<class ImageType> typename ImageType::ConstPointer vvImageToITK(vvImage::Pointer vv_image) ///Converts the vv image to itk, handling the 4D problem
55 {
56     const unsigned int Dim=ImageType::ImageDimension;
57     assert(Dim < 5 && Dim > 0); // We don't handle anything higher than 4-dimensional (for the moment :-p)
58     typedef ImageType OutputImageType;
59
60     if (Dim==4)
61     {
62         typedef itk::Image< typename ImageType::PixelType,  3 >    ConnectorImageType;
63         typedef itk::VTKImageToImageFilter<ConnectorImageType> ConnectorType;
64         typedef itk::JoinSeriesImageFilter<ConnectorImageType,OutputImageType> FilterType;
65
66
67         typename FilterType::Pointer filter = FilterType::New();
68         filter->SetOrigin(vv_image->GetOrigin()[3]);
69         filter->SetSpacing(vv_image->GetSpacing()[3]);
70
71         for (int i = 0; i < vv_image->GetSize()[3]; i++)
72         {
73             typename ConnectorType::Pointer connector = ConnectorType::New();
74             connector->SetInput(vv_image->GetVTKImages()[i]);
75             connector->Update();
76             filter->PushBackInput(connector->GetOutput());
77         }
78         filter->Update();
79         return filter->GetOutput();
80     }
81     else //Dim == 1,2,3
82     {
83         assert(not vv_image->IsTimeSequence()); //This case isn't implemented
84         typedef ImageType  ConnectorImageType;
85         typedef itk::VTKImageToImageFilter <ConnectorImageType> ConnectorType;
86         typename ConnectorType::Pointer connector = ConnectorType::New();
87         connector->SetInput(vv_image->GetVTKImages()[0]);
88
89         connector->Update();
90         return connector->GetOutput();
91     }
92 }
93
94 ///Converts a single time frame of a vv image to itk.
95 template<unsigned int Dim, class PixelType> typename itk::Image<PixelType,Dim>::ConstPointer vvSingleFrameToITK(vvImage::Pointer vv_image,int frame) ///Converts the vv image to itk, handling the 4D problem
96 {
97     assert(Dim < 4 && Dim > 0);
98     typedef itk::Image< PixelType, Dim > OutputImageType;
99     typedef itk::Image< PixelType,  Dim >    ConnectorImageType;
100     typedef itk::VTKImageToImageFilter <ConnectorImageType> ConnectorType;
101     typename ConnectorType::Pointer connector = ConnectorType::New();
102     connector->SetInput(vv_image->GetVTKImages()[frame]);
103     connector->Update();
104     return connector->GetOutput();
105 }
106
107 #endif