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