]> Creatis software - clitk.git/blob - itk/clitkExtractSliceFilter.txx
fix previous commit, sorry
[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://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
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   DD("GetOutputSlices");
57   o.clear();
58   for(unsigned int i=0; i<this->GetNumberOfOutputs(); i++) {
59     o.push_back(this->GetOutput(i));
60     // writeImage<SliceType>(this->GetOutput(i), "extractB"+clitk::toString(i)+".mhd");
61   }
62 }
63 //--------------------------------------------------------------------
64   
65
66 //--------------------------------------------------------------------
67 template <class ImageType>
68 void 
69 clitk::ExtractSliceFilter<ImageType>::
70 GenerateOutputInformation() 
71
72   DD("GenerateOutputInformation");
73   ImagePointer input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
74   
75   // Create region to extract
76   m_region = input->GetLargestPossibleRegion();
77   m_size   = m_region.GetSize();
78   m_index  = m_region.GetIndex();
79   m_NumberOfSlices = m_region.GetSize()[GetDirection()];
80   DD(m_NumberOfSlices);
81 }
82 //--------------------------------------------------------------------
83
84
85 //--------------------------------------------------------------------
86 template <class ImageType>
87 void 
88 clitk::ExtractSliceFilter<ImageType>::
89 GenerateInputRequestedRegion() {
90   DD("GenerateInputRequestedRegion");
91   // Call default
92   Superclass::GenerateInputRequestedRegion();
93   // Get input pointers and set requested region to common region
94   ImagePointer input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
95   input->SetRequestedRegion(input->GetLargestPossibleRegion());
96 }
97 //--------------------------------------------------------------------
98
99 //--------------------------------------------------------------------
100 template <class ImageType>
101 void 
102 clitk::ExtractSliceFilter<ImageType>::
103 GenerateData() {
104   DD("GenerateData");
105
106   //--------------------------------------------------------------------
107   // Get input pointer
108   input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
109
110   //--------------------------------------------------------------------
111   // Create region to extract in 3D (and 2D = not used)
112   m_size[GetDirection()] = 0;
113   m_region.SetSize(m_size);
114   int start = m_index[GetDirection()];
115   DD(start);
116   this->SetNumberOfOutputs(m_NumberOfSlices);
117   /*
118     typename SliceType::RegionType regionSlice;
119     typename SliceType::SizeType   sizeSlice;
120     typename SliceType::IndexType  indexSlice;
121     int j=0;
122     for(int i=0; i<ImageDimension; i++) {
123     if (i != GetDirection()) {
124     sizeSlice[j] = m_size[i];
125     indexSlice[j] = m_index[j];
126     j++;
127     }
128     }
129     regionSlice.SetSize(sizeSlice);
130     regionSlice.SetIndex(indexSlice);
131     DD(regionSlice);
132   */
133
134   //--------------------------------------------------------------------
135   // loop ExtractImageFilter with region updated, push_back
136   typedef itk::ExtractImageFilter<ImageType, SliceType> ExtractImageFilterType;
137   typename ExtractImageFilterType::Pointer extract = ExtractImageFilterType::New();
138   extract->SetInput(input);
139   for(int i=0; i<m_NumberOfSlices; i++) {
140     extract = ExtractImageFilterType::New();
141     extract->SetInput(input);
142     // DD(i);
143     m_index[GetDirection()] = start + i;
144     m_region.SetIndex(m_index);
145     // DD(m_region);
146     extract->SetExtractionRegion(m_region);
147     extract->Update();
148     // DD("set output");
149     SetNthOutput(i, extract->GetOutput());
150     // writeImage<SliceType>(extract->GetOutput(), "extractA"+clitk::toString(i)+".mhd");
151   }
152
153   return;
154 }
155 //--------------------------------------------------------------------
156