1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
19 #include <vtkPolyDataToImageStencil.h>
20 #include <vtkLinearExtrusionFilter.h>
21 #include <vtkImageStencil.h>
22 #include <vtkMetaImageWriter.h>
24 #include "itkVTKImageImport.h"
25 #include "vtkImageExport.h"
26 #include "vtkImageData.h"
30 //--------------------------------------------------------------------
31 template <class ImageType>
32 clitk::MeshToBinaryImageFilter<ImageType>::
33 MeshToBinaryImageFilter() : itk::ImageSource<ImageType>(), m_Extrude(true)
36 //--------------------------------------------------------------------
39 //--------------------------------------------------------------------
40 template <class ImageType>
42 clitk::MeshToBinaryImageFilter<ImageType>::
43 GenerateOutputInformation()
45 ImagePointer output = this->GetOutput(0);
47 // Set the region to output
48 typename ImageType::RegionType m_Region = m_LikeImage->GetLargestPossibleRegion();
49 typename ImageType::SizeType size = m_Region.GetSize();
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);
60 //--------------------------------------------------------------------
63 //--------------------------------------------------------------------
64 template <class ImageType>
66 clitk::MeshToBinaryImageFilter<ImageType>::
70 vtkSmartPointer<vtkImageData> binary_image=vtkSmartPointer<vtkImageData>::New();
71 binary_image->SetScalarTypeToUnsignedChar();
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);
83 /// Put the origin on a voxel to avoid small skips
84 binary_image->SetOrigin(samp_origin[0], samp_origin[1], samp_origin[2]);
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]
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));
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);
107 vtkSmartPointer<vtkLinearExtrusionFilter> extrude=vtkSmartPointer<vtkLinearExtrusionFilter>::New();
108 extrude->SetInput(m_Mesh);
109 // We extrude in the -slice_spacing direction to respect the FOCAL convention
110 extrude->SetVector(0, 0, -m_LikeImage->GetSpacing()[2]);
111 sts->SetInput(extrude->GetOutput());
114 sts->SetInput(m_Mesh);
117 vtkSmartPointer<vtkImageStencil> stencil=vtkSmartPointer<vtkImageStencil>::New();
118 stencil->SetStencil(sts->GetOutput());
119 stencil->SetInput(binary_image);
121 // Convert VTK to ITK
122 vtkImageExport * m_Exporter = vtkImageExport::New();
123 typedef itk::VTKImageImport< ImageType > ImporterFilterType;
124 typename ImporterFilterType::Pointer m_Importer = ImporterFilterType::New();
126 m_Importer->SetUpdateInformationCallback( m_Exporter->GetUpdateInformationCallback());
127 m_Importer->SetPipelineModifiedCallback( m_Exporter->GetPipelineModifiedCallback());
128 m_Importer->SetWholeExtentCallback( m_Exporter->GetWholeExtentCallback());
129 m_Importer->SetSpacingCallback( m_Exporter->GetSpacingCallback());
130 m_Importer->SetOriginCallback( m_Exporter->GetOriginCallback());
131 m_Importer->SetScalarTypeCallback( m_Exporter->GetScalarTypeCallback());
132 m_Importer->SetNumberOfComponentsCallback( m_Exporter->GetNumberOfComponentsCallback());
133 m_Importer->SetPropagateUpdateExtentCallback( m_Exporter->GetPropagateUpdateExtentCallback());
134 m_Importer->SetUpdateDataCallback( m_Exporter->GetUpdateDataCallback());
135 m_Importer->SetDataExtentCallback( m_Exporter->GetDataExtentCallback());
136 m_Importer->SetBufferPointerCallback( m_Exporter->GetBufferPointerCallback());
137 m_Importer->SetCallbackUserData( m_Exporter->GetCallbackUserData());
139 m_Exporter->SetInput( stencil->GetOutput() );
140 m_Importer->Update();
142 // writeImage<ImageType>(m_Importer->GetOutput(), "f.mhd");
144 this->SetNthOutput(0, m_Importer->GetOutput());
148 //--------------------------------------------------------------------