]> Creatis software - clitk.git/blob - common/vvToITK.h
added the new headers
[clitk.git] / common / vvToITK.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 vvImageToITK_h
19 #define vvImageToITK_h
20 #include <vector>
21 #include "vvImage.h"
22 #include <itkImage.h>
23 #include <itkJoinSeriesImageFilter.h>
24 #include "itkVTKImageToImageFilter.h"
25
26 ///Converts the vv image to itk, handling the 4D problem
27 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
28 {
29     assert(Dim < 5 && Dim > 1); // We don't handle anything higher than 4-dimensional (for the moment :-p)
30     assert(vv_image->GetVTKImages().size() > 0); //we assume there is something to convert
31     typedef itk::Image< PixelType, Dim > OutputImageType;
32     std::vector<typename itk::Image<PixelType,Dim>::ConstPointer> result;
33
34     typedef itk::Image< PixelType, Dim >    ConnectorImageType;
35     typedef itk::VTKImageToImageFilter<ConnectorImageType> ConnectorType;
36     for (unsigned int i = 0; i < vv_image->GetVTKImages().size(); i++)
37     {
38         typename ConnectorType::Pointer connector = ConnectorType::New();
39         connector->SetInput(vv_image->GetVTKImages()[i]);
40         connector->Update();
41         result.push_back(connector->GetOutput());
42     }
43     return result;
44 }
45
46 ///Converts the vv image to itk, handling the 4D problem
47 template<class ImageType> typename ImageType::ConstPointer vvImageToITK(vvImage::Pointer vv_image) ///Converts the vv image to itk, handling the 4D problem
48 {
49     const unsigned int Dim=ImageType::ImageDimension;
50     assert(Dim < 5 && Dim > 0); // We don't handle anything higher than 4-dimensional (for the moment :-p)
51     typedef ImageType OutputImageType;
52
53     if (Dim==4)
54     {
55         typedef itk::Image< typename ImageType::PixelType,  3 >    ConnectorImageType;
56         typedef itk::VTKImageToImageFilter<ConnectorImageType> ConnectorType;
57         typedef itk::JoinSeriesImageFilter<ConnectorImageType,OutputImageType> FilterType;
58
59
60         typename FilterType::Pointer filter = FilterType::New();
61         filter->SetOrigin(vv_image->GetOrigin()[3]);
62         filter->SetSpacing(vv_image->GetSpacing()[3]);
63
64         for (int i = 0; i < vv_image->GetSize()[3]; i++)
65         {
66             typename ConnectorType::Pointer connector = ConnectorType::New();
67             connector->SetInput(vv_image->GetVTKImages()[i]);
68             connector->Update();
69             filter->PushBackInput(connector->GetOutput());
70         }
71         filter->Update();
72         return filter->GetOutput();
73     }
74     else //Dim == 1,2,3
75     {
76         assert(!vv_image->IsTimeSequence()); //This case isn't implemented
77         typedef ImageType  ConnectorImageType;
78         typedef itk::VTKImageToImageFilter <ConnectorImageType> ConnectorType;
79         typename ConnectorType::Pointer connector = ConnectorType::New();
80         connector->SetInput(vv_image->GetVTKImages()[0]);
81
82         connector->Update();
83         return connector->GetOutput();
84     }
85 }
86
87 ///Converts a single time frame of a vv image to itk.
88 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
89 {
90     assert(Dim < 4 && Dim > 0);
91     typedef itk::Image< PixelType, Dim > OutputImageType;
92     typedef itk::Image< PixelType,  Dim >    ConnectorImageType;
93     typedef itk::VTKImageToImageFilter <ConnectorImageType> ConnectorType;
94     typename ConnectorType::Pointer connector = ConnectorType::New();
95     connector->SetInput(vv_image->GetVTKImages()[frame]);
96     connector->Update();
97     return connector->GetOutput();
98 }
99
100 #endif