]> Creatis software - clitk.git/blob - itk/clitkMeshToBinaryImageFilter.txx
merge cvs -> git
[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://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 #include <vtkPolyDataToImageStencil.h>
20 #include <vtkLinearExtrusionFilter.h>
21 #include <vtkImageStencil.h>
22 #include <vtkMetaImageWriter.h>
23
24 #include "itkVTKImageImport.h"
25 #include "vtkImageExport.h"
26 #include "vtkImageData.h"
27
28
29
30 //--------------------------------------------------------------------
31 template <class ImageType>
32 clitk::MeshToBinaryImageFilter<ImageType>::
33 MeshToBinaryImageFilter():itk::ImageSource<ImageType>()
34 {
35 }
36 //--------------------------------------------------------------------
37
38
39 //--------------------------------------------------------------------
40 template <class ImageType>
41 void 
42 clitk::MeshToBinaryImageFilter<ImageType>::
43 GenerateOutputInformation() 
44 {
45   ImagePointer output = this->GetOutput(0);
46   
47   // Set the region to output
48   typename ImageType::RegionType m_Region = m_LikeImage->GetLargestPossibleRegion();
49   typename ImageType::SizeType size = m_Region.GetSize();
50   size[0]++;
51   size[1]++;
52   size[2]++;
53   m_Region.SetSize(size);  
54   output->SetLargestPossibleRegion(m_Region);
55   output->SetRequestedRegion(m_Region);
56   output->SetBufferedRegion(m_Region);
57   output->SetRegions(m_Region);
58   output->Allocate();
59 }
60 //--------------------------------------------------------------------
61
62
63 //--------------------------------------------------------------------
64 template <class ImageType>
65 void 
66 clitk::MeshToBinaryImageFilter<ImageType>::
67 GenerateData() 
68 {
69   // GO
70   vtkSmartPointer<vtkImageData> binary_image=vtkSmartPointer<vtkImageData>::New();
71   binary_image->SetScalarTypeToUnsignedChar();
72
73   // Set spacing
74   PointType samp_origin = m_LikeImage->GetOrigin();
75   SpacingType spacing=m_LikeImage->GetSpacing();
76   double * spacing2 = new double[3];
77   spacing2[0] = spacing[0];
78   spacing2[1] = spacing[1];
79   spacing2[2] = spacing[2];
80   binary_image->SetSpacing(spacing2);
81
82   // Set origin
83   /// Put the origin on a voxel to avoid small skips
84   binary_image->SetOrigin(samp_origin[0], samp_origin[1], samp_origin[2]);
85
86   // Compute image bounds
87   binary_image->SetExtent(0,m_LikeImage->GetLargestPossibleRegion().GetSize()[0],
88                           0,m_LikeImage->GetLargestPossibleRegion().GetSize()[1],
89                           0,m_LikeImage->GetLargestPossibleRegion().GetSize()[2] 
90                           );
91   
92   // Allocate data
93   binary_image->AllocateScalars();
94   memset(binary_image->GetScalarPointer(),0,
95          binary_image->GetDimensions()[0]*binary_image->GetDimensions()[1]*binary_image->GetDimensions()[2]*sizeof(unsigned char));
96
97   // Image stencil 
98   vtkSmartPointer<vtkPolyDataToImageStencil> sts=vtkSmartPointer<vtkPolyDataToImageStencil>::New();
99   //The following line is extremely important
100   //http://www.nabble.com/Bug-in-vtkPolyDataToImageStencil--td23368312.html#a23370933
101   sts->SetTolerance(0);
102   sts->SetInformationInput(binary_image);
103     
104   // Extrusion
105   vtkSmartPointer<vtkLinearExtrusionFilter> extrude=vtkSmartPointer<vtkLinearExtrusionFilter>::New();
106   extrude->SetInput(m_Mesh);
107   // We extrude in the -slice_spacing direction to respect the FOCAL convention 
108   extrude->SetVector(0, 0, -m_LikeImage->GetSpacing()[2]);
109   sts->SetInput(extrude->GetOutput());
110
111   // Stencil
112   vtkSmartPointer<vtkImageStencil> stencil=vtkSmartPointer<vtkImageStencil>::New();
113   stencil->SetStencil(sts->GetOutput());
114   stencil->SetInput(binary_image);
115
116   // Convert VTK to ITK
117   vtkImageExport * m_Exporter = vtkImageExport::New();
118   typedef itk::VTKImageImport< ImageType >   ImporterFilterType;
119   typename ImporterFilterType::Pointer m_Importer = ImporterFilterType::New();
120
121   m_Importer->SetUpdateInformationCallback( m_Exporter->GetUpdateInformationCallback());
122   m_Importer->SetPipelineModifiedCallback( m_Exporter->GetPipelineModifiedCallback());
123   m_Importer->SetWholeExtentCallback( m_Exporter->GetWholeExtentCallback());
124   m_Importer->SetSpacingCallback( m_Exporter->GetSpacingCallback());
125   m_Importer->SetOriginCallback( m_Exporter->GetOriginCallback());
126   m_Importer->SetScalarTypeCallback( m_Exporter->GetScalarTypeCallback());
127   m_Importer->SetNumberOfComponentsCallback( m_Exporter->GetNumberOfComponentsCallback());
128   m_Importer->SetPropagateUpdateExtentCallback( m_Exporter->GetPropagateUpdateExtentCallback());
129   m_Importer->SetUpdateDataCallback( m_Exporter->GetUpdateDataCallback());
130   m_Importer->SetDataExtentCallback( m_Exporter->GetDataExtentCallback());
131   m_Importer->SetBufferPointerCallback( m_Exporter->GetBufferPointerCallback());
132   m_Importer->SetCallbackUserData( m_Exporter->GetCallbackUserData());
133
134   m_Exporter->SetInput( stencil->GetOutput() );
135   m_Importer->Update();
136
137   writeImage<ImageType>(m_Importer->GetOutput(), "f.mhd");
138
139   this->SetNthOutput(0, m_Importer->GetOutput());
140
141   return;
142 }
143 //--------------------------------------------------------------------
144