]> Creatis software - clitk.git/blob - itk/clitkExtractSliceFilter.txx
First version to convert image to dicomrtstruct
[clitk.git] / itk / clitkExtractSliceFilter.txx
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
19 // clitk
20 #include "clitkCommon.h"
21
22 // itk
23 #include <itkExtractImageFilter.h>
24
25 //--------------------------------------------------------------------
26 template <class ImageType>
27 clitk::ExtractSliceFilter<ImageType>::
28 ExtractSliceFilter():
29   clitk::FilterBase(),
30   Superclass()
31 {
32   this->SetNumberOfRequiredInputs(1);
33   SetDirection(2);
34 }
35 //--------------------------------------------------------------------
36
37
38 //--------------------------------------------------------------------
39 template <class ImageType>
40 void 
41 clitk::ExtractSliceFilter<ImageType>::
42 SetInput(const ImageType * image) 
43 {
44   // Process object is not const-correct so the const casting is required.
45   this->SetNthInput(0, const_cast<ImageType *>(image));
46 }
47 //--------------------------------------------------------------------
48   
49
50 //--------------------------------------------------------------------
51 template <class ImageType>
52 void
53 clitk::ExtractSliceFilter<ImageType>::
54 GetOutputSlices(std::vector<typename SliceType::Pointer> & o)
55 {
56   o.clear();
57   for(unsigned int i=0; i<this->GetNumberOfOutputs(); i++) {
58     o.push_back(this->GetOutput(i));
59   }
60 }
61 //--------------------------------------------------------------------
62   
63
64 //--------------------------------------------------------------------
65 template <class ImageType>
66 void 
67 clitk::ExtractSliceFilter<ImageType>::
68 GenerateOutputInformation() 
69
70   ImagePointer input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
71   
72   // Create region to extract
73   m_region = input->GetLargestPossibleRegion();
74   m_size   = m_region.GetSize();
75   m_index  = m_region.GetIndex();
76   m_NumberOfSlices = m_region.GetSize()[GetDirection()];
77 }
78 //--------------------------------------------------------------------
79
80
81 //--------------------------------------------------------------------
82 template <class ImageType>
83 void 
84 clitk::ExtractSliceFilter<ImageType>::
85 GenerateInputRequestedRegion() {
86   // Call default
87   Superclass::GenerateInputRequestedRegion();
88   // Get input pointers and set requested region to common region
89   ImagePointer input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
90   input->SetRequestedRegion(input->GetLargestPossibleRegion());
91 }
92 //--------------------------------------------------------------------
93
94 //--------------------------------------------------------------------
95 template <class ImageType>
96 void 
97 clitk::ExtractSliceFilter<ImageType>::
98 GenerateData() {
99   //--------------------------------------------------------------------
100   // Get input pointer
101   input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
102
103   //--------------------------------------------------------------------
104   // Create region to extract in 3D (and 2D = not used)
105   m_size[GetDirection()] = 0;
106   m_region.SetSize(m_size);
107   int start = m_index[GetDirection()];
108 #if ITK_VERSION_MAJOR >= 4
109   this->SetNumberOfIndexedInputs(m_NumberOfSlices);
110 #else
111   this->SetNumberOfOutputs(m_NumberOfSlices);
112 #endif
113
114   //--------------------------------------------------------------------
115   // loop ExtractImageFilter with region updated, push_back
116   typedef itk::ExtractImageFilter<ImageType, SliceType> ExtractImageFilterType;
117   typename ExtractImageFilterType::Pointer extract = ExtractImageFilterType::New();
118   extract->SetInput(input);
119   for(int i=0; i<m_NumberOfSlices; i++) {
120     extract = ExtractImageFilterType::New();
121     extract->SetInput(input);
122     m_index[GetDirection()] = start + i;
123     m_region.SetIndex(m_index);
124     extract->SetExtractionRegion(m_region);
125 #if ITK_VERSION_MAJOR == 4
126     extract->SetDirectionCollapseToSubmatrix();
127 #endif
128     extract->Update();
129     this->SetNthOutput(i, extract->GetOutput());
130   }
131   return;
132 }
133 //--------------------------------------------------------------------
134