]> Creatis software - clitk.git/blob - itk/clitkMeshToBinaryImageFilter.txx
Remove sonarQube
[clitk.git] / itk / clitkMeshToBinaryImageFilter.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 #include <vtkPolyDataToImageStencil.h>
20 #include <vtkLinearExtrusionFilter.h>
21 #include <vtkImageStencil.h>
22 #include <vtkMetaImageWriter.h>
23 #include <vtkVersion.h>
24
25 #include "itkVTKImageImport.h"
26 #include "vtkImageExport.h"
27 #include "vtkImageData.h"
28
29
30
31 //--------------------------------------------------------------------
32 template <class ImageType>
33 clitk::MeshToBinaryImageFilter<ImageType>::
34 MeshToBinaryImageFilter() : itk::ImageSource<ImageType>(), m_Extrude(true)
35 {
36 }
37 //--------------------------------------------------------------------
38
39
40 //--------------------------------------------------------------------
41 template <class ImageType>
42 void 
43 clitk::MeshToBinaryImageFilter<ImageType>::
44 GenerateOutputInformation() 
45 {
46   ImagePointer output = this->GetOutput(0);
47   
48   // Set the region to output
49   typename ImageType::RegionType m_Region = m_LikeImage->GetLargestPossibleRegion();
50   typename ImageType::SizeType size = m_Region.GetSize();
51   size[0]++;
52   size[1]++;
53   size[2]++;
54   m_Region.SetSize(size);  
55   output->SetLargestPossibleRegion(m_Region);
56   output->SetRequestedRegion(m_Region);
57   output->SetBufferedRegion(m_Region);
58   output->SetRegions(m_Region);
59   output->Allocate();
60 }
61 //--------------------------------------------------------------------
62
63
64 //--------------------------------------------------------------------
65 template <class ImageType>
66 void 
67 clitk::MeshToBinaryImageFilter<ImageType>::
68 GenerateData() 
69 {
70   // GO
71   vtkSmartPointer<vtkImageData> binary_image=vtkSmartPointer<vtkImageData>::New();
72 #if VTK_MAJOR_VERSION <= 5
73   binary_image->SetScalarTypeToUnsignedChar();
74
75   // Set spacing
76   PointType samp_origin = m_LikeImage->GetOrigin();
77   SpacingType spacing=m_LikeImage->GetSpacing();
78   double * spacing2 = new double[3];
79   spacing2[0] = spacing[0];
80   spacing2[1] = spacing[1];
81   spacing2[2] = spacing[2];
82   binary_image->SetSpacing(spacing2);
83
84   // Set origin
85   /// Put the origin on a voxel to avoid small skips
86   binary_image->SetOrigin(samp_origin[0], samp_origin[1], samp_origin[2]);
87
88   // Compute image bounds
89   binary_image->SetExtent(0,m_LikeImage->GetLargestPossibleRegion().GetSize()[0],
90                           0,m_LikeImage->GetLargestPossibleRegion().GetSize()[1],
91                           0,m_LikeImage->GetLargestPossibleRegion().GetSize()[2] 
92                           );
93   
94   // Allocate data
95   binary_image->AllocateScalars();
96 #else
97   // Set spacing
98   PointType samp_origin = m_LikeImage->GetOrigin();
99   SpacingType spacing=m_LikeImage->GetSpacing();
100   double * spacing2 = new double[3];
101   spacing2[0] = spacing[0];
102   spacing2[1] = spacing[1];
103   spacing2[2] = spacing[2];
104   binary_image->SetSpacing(spacing2);
105
106   // Set origin
107   /// Put the origin on a voxel to avoid small skips
108   binary_image->SetOrigin(samp_origin[0], samp_origin[1], samp_origin[2]);
109
110   // Compute image bounds
111   binary_image->SetExtent(0,m_LikeImage->GetLargestPossibleRegion().GetSize()[0],
112                           0,m_LikeImage->GetLargestPossibleRegion().GetSize()[1],
113                           0,m_LikeImage->GetLargestPossibleRegion().GetSize()[2] 
114                           );
115   
116   // Allocate data
117   binary_image->AllocateScalars(VTK_UNSIGNED_CHAR, 1);
118 #endif
119   memset(binary_image->GetScalarPointer(),0,
120          binary_image->GetDimensions()[0]*binary_image->GetDimensions()[1]*binary_image->GetDimensions()[2]*sizeof(unsigned char));
121
122   // Image stencil 
123   vtkSmartPointer<vtkPolyDataToImageStencil> sts=vtkSmartPointer<vtkPolyDataToImageStencil>::New();
124   //The following line is extremely important
125   //http://www.nabble.com/Bug-in-vtkPolyDataToImageStencil--td23368312.html#a23370933
126   sts->SetTolerance(0);
127   sts->SetInformationInput(binary_image);
128     
129   // Extrusion
130   if (m_Extrude)
131   {
132     vtkSmartPointer<vtkLinearExtrusionFilter> extrude=vtkSmartPointer<vtkLinearExtrusionFilter>::New();
133 #if VTK_MAJOR_VERSION <= 5
134     extrude->SetInput(m_Mesh);
135 #else
136     extrude->SetInputData(m_Mesh);
137 #endif
138     // We extrude in the -slice_spacing direction to respect the FOCAL convention 
139     extrude->SetVector(0, 0, -m_LikeImage->GetSpacing()[2]);
140 #if VTK_MAJOR_VERSION <= 5
141     sts->SetInput(extrude->GetOutput());
142 #else
143     sts->SetInputConnection(extrude->GetOutputPort());
144 #endif
145     
146     // When extrude ScaleFactor indicate the extrusion scaling (default = 1)
147     /*
148       extrude->SetScaleFactor(m_LikeImage->GetSpacing()[2]/2.0);
149       DD(extrude->GetScaleFactor());
150       DD(extrude->GetCapping());
151     */ 
152   }
153   else
154 #if VTK_MAJOR_VERSION <= 5
155     sts->SetInput(m_Mesh);
156 #else
157     sts->SetInputData(m_Mesh);
158 #endif
159
160   // Stencil
161   vtkSmartPointer<vtkImageStencil> stencil=vtkSmartPointer<vtkImageStencil>::New();
162 #if VTK_MAJOR_VERSION <= 5
163   stencil->SetStencil(sts->GetOutput());
164   stencil->SetInput(binary_image);
165 #else
166   stencil->SetStencilData(sts->GetOutput());
167   stencil->SetInputData(binary_image);
168 #endif
169
170   // Convert VTK to ITK
171   vtkImageExport * m_Exporter = vtkImageExport::New();
172   typedef itk::VTKImageImport< ImageType >   ImporterFilterType;
173   typename ImporterFilterType::Pointer m_Importer = ImporterFilterType::New();
174
175   m_Importer->SetUpdateInformationCallback( m_Exporter->GetUpdateInformationCallback());
176   m_Importer->SetPipelineModifiedCallback( m_Exporter->GetPipelineModifiedCallback());
177   m_Importer->SetWholeExtentCallback( m_Exporter->GetWholeExtentCallback());
178   m_Importer->SetSpacingCallback( m_Exporter->GetSpacingCallback());
179   m_Importer->SetOriginCallback( m_Exporter->GetOriginCallback());
180   m_Importer->SetScalarTypeCallback( m_Exporter->GetScalarTypeCallback());
181   m_Importer->SetNumberOfComponentsCallback( m_Exporter->GetNumberOfComponentsCallback());
182   m_Importer->SetPropagateUpdateExtentCallback( m_Exporter->GetPropagateUpdateExtentCallback());
183   m_Importer->SetUpdateDataCallback( m_Exporter->GetUpdateDataCallback());
184   m_Importer->SetDataExtentCallback( m_Exporter->GetDataExtentCallback());
185   m_Importer->SetBufferPointerCallback( m_Exporter->GetBufferPointerCallback());
186   m_Importer->SetCallbackUserData( m_Exporter->GetCallbackUserData());
187
188 #if VTK_MAJOR_VERSION <= 5
189   m_Exporter->SetInput( stencil->GetOutput() );
190 #else
191   m_Exporter->SetInputData( stencil->GetOutput() );
192 #endif
193   m_Importer->Update();
194
195   // writeImage<ImageType>(m_Importer->GetOutput(), "f.mhd");
196
197   this->SetNthOutput(0, m_Importer->GetOutput());
198
199   return;
200 }
201 //--------------------------------------------------------------------
202