]> Creatis software - clitk.git/blob - tools/clitkExtrudeGenericFilter.txx
96c7dc1278a2dfcd268ecdb676095fd683b02f0b
[clitk.git] / tools / clitkExtrudeGenericFilter.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 #ifndef clitkExtrudeGenericFilter_txx
19 #define clitkExtrudeGenericFilter_txx
20
21 // itk include
22 #include <clitkCommon.h>
23
24 namespace clitk
25 {
26
27 //--------------------------------------------------------------------
28 template<class args_info_type>
29 ExtrudeGenericFilter<args_info_type>::ExtrudeGenericFilter():
30   ImageToImageGenericFilter<Self>("Extrude")
31 {
32   InitializeImageType<2>();
33   InitializeImageType<3>();
34 }
35 //--------------------------------------------------------------------
36
37
38 //--------------------------------------------------------------------
39 template<class args_info_type>
40 template<unsigned int Dim>
41 void ExtrudeGenericFilter<args_info_type>::InitializeImageType()
42 {
43   ADD_DEFAULT_IMAGE_TYPES(Dim);
44 }
45 //--------------------------------------------------------------------
46
47
48 //--------------------------------------------------------------------
49 template<class args_info_type>
50 void ExtrudeGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a)
51 {
52   mArgsInfo=a;
53   this->SetIOVerbose(mArgsInfo.verbose_flag);
54
55   if (mArgsInfo.input_given) {
56     this->SetInputFilename(mArgsInfo.input_arg);
57   }
58   if (mArgsInfo.output_given) {
59     this->SetOutputFilename(mArgsInfo.output_arg);
60   }
61 }
62 //--------------------------------------------------------------------
63
64 //--------------------------------------------------------------------
65 // Update with the number of dimensions and the pixeltype
66 //--------------------------------------------------------------------
67 template<class args_info_type>
68 template<class InputImageType>
69 void
70 ExtrudeGenericFilter<args_info_type>::UpdateWithInputImageType()
71 {
72
73   // Reading input
74   typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
75
76   // Main filter
77   typedef typename InputImageType::PixelType PixelType;
78   const int Dim = InputImageType::ImageDimension;
79   typedef itk::Image<PixelType,Dim> ImageType;
80   typedef itk::Image<PixelType,Dim+1> OutputImageType;
81
82   //Create the output
83   typename OutputImageType::IndexType start;
84   typename OutputImageType::SizeType size;
85   typename OutputImageType::PointType origin;
86   typename OutputImageType::SpacingType spacing;
87   typename OutputImageType::DirectionType direction;
88   typename OutputImageType::Pointer output = OutputImageType::New();
89
90   start.Fill(0);
91
92   int extrusionSize(1);
93   if (mArgsInfo.size_given) {
94     if (mArgsInfo.size_arg > 0)
95       extrusionSize = mArgsInfo.size_arg;
96     else {
97       std::cerr << "The size has to be > 0" << std::endl;
98       return;
99     }
100   }
101
102   for (unsigned int i=0; i<Dim; ++i)
103     size[i] = input->GetLargestPossibleRegion().GetSize()[i];
104   size[Dim] = extrusionSize;
105
106   for (unsigned int i=0; i<Dim; ++i)
107     origin[i] = input->GetOrigin()[i];
108   if (mArgsInfo.origin_given)
109     origin[Dim] = mArgsInfo.origin_arg;
110   else
111     origin[Dim] = 0;
112
113   for (unsigned int i=0; i<Dim; ++i)
114     spacing[i] = input->GetSpacing()[i];
115   if (mArgsInfo.spacing_given)
116     spacing[Dim] = mArgsInfo.spacing_arg;
117   else
118     spacing[Dim] = 1;
119
120   for (unsigned int i=0; i<Dim; ++i) {
121     for (unsigned int j=0; j<Dim; ++j)
122       direction[i][j] = input->GetDirection()[i][j];
123     direction[i][Dim] = 0;
124   }
125   for (unsigned int i=0; i<Dim; ++i)
126     direction[Dim][i] = 0;
127   direction[Dim][Dim] = 1;
128
129   typename OutputImageType::RegionType region(start, size);
130   output->SetRegions(region);
131   output->Allocate();
132   output->FillBuffer(0);
133   output->SetOrigin(origin);
134   output->SetSpacing(spacing);
135   output->SetDirection(direction);
136
137   itk::ImageRegionIteratorWithIndex<InputImageType> inputIterator(input, input->GetLargestPossibleRegion());
138   while(!inputIterator.IsAtEnd()) {
139     typename OutputImageType::IndexType pixelIndex;
140     for (unsigned int i=0; i<Dim; ++i)
141       pixelIndex[i] = inputIterator.GetIndex()[i];
142     for (unsigned int i=0; i<extrusionSize; ++i) {
143       pixelIndex[Dim] = i;
144       output->SetPixel(pixelIndex, inputIterator.Get());
145     }
146     ++inputIterator;
147   }
148
149   this->template SetNextOutput<OutputImageType>(output);
150
151 }
152 //--------------------------------------------------------------------
153
154
155 }//end clitk
156
157 #endif //#define clitkExtrudeGenericFilter_txx