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>
23 #include <vtkVersion.h>
25 #include "itkVTKImageImport.h"
26 #include "vtkImageExport.h"
27 #include "vtkImageData.h"
31 //--------------------------------------------------------------------
32 template <class ImageType>
33 clitk::MeshToBinaryImageFilter<ImageType>::
34 MeshToBinaryImageFilter() : itk::ImageSource<ImageType>(), m_Extrude(true)
37 //--------------------------------------------------------------------
40 //--------------------------------------------------------------------
41 template <class ImageType>
43 clitk::MeshToBinaryImageFilter<ImageType>::
44 GenerateOutputInformation()
46 ImagePointer output = this->GetOutput(0);
48 // Set the region to output
49 typename ImageType::RegionType m_Region = m_LikeImage->GetLargestPossibleRegion();
50 typename ImageType::SizeType size = m_Region.GetSize();
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);
61 //--------------------------------------------------------------------
64 //--------------------------------------------------------------------
65 template <class ImageType>
67 clitk::MeshToBinaryImageFilter<ImageType>::
71 vtkSmartPointer<vtkImageData> binary_image=vtkSmartPointer<vtkImageData>::New();
72 #if VTK_MAJOR_VERSION <= 5
73 binary_image->SetScalarTypeToUnsignedChar();
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);
85 /// Put the origin on a voxel to avoid small skips
86 binary_image->SetOrigin(samp_origin[0], samp_origin[1], samp_origin[2]);
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]
95 binary_image->AllocateScalars();
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);
107 /// Put the origin on a voxel to avoid small skips
108 binary_image->SetOrigin(samp_origin[0], samp_origin[1], samp_origin[2]);
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]
117 binary_image->AllocateScalars(VTK_UNSIGNED_CHAR, 1);
119 memset(binary_image->GetScalarPointer(),0,
120 binary_image->GetDimensions()[0]*binary_image->GetDimensions()[1]*binary_image->GetDimensions()[2]*sizeof(unsigned char));
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);
132 vtkSmartPointer<vtkLinearExtrusionFilter> extrude=vtkSmartPointer<vtkLinearExtrusionFilter>::New();
133 #if VTK_MAJOR_VERSION <= 5
134 extrude->SetInput(m_Mesh);
136 extrude->SetInputData(m_Mesh);
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());
143 sts->SetInputConnection(extrude->GetOutputPort());
146 // When extrude ScaleFactor indicate the extrusion scaling (default = 1)
148 extrude->SetScaleFactor(m_LikeImage->GetSpacing()[2]/2.0);
149 DD(extrude->GetScaleFactor());
150 DD(extrude->GetCapping());
154 #if VTK_MAJOR_VERSION <= 5
155 sts->SetInput(m_Mesh);
157 sts->SetInputData(m_Mesh);
161 vtkSmartPointer<vtkImageStencil> stencil=vtkSmartPointer<vtkImageStencil>::New();
162 #if VTK_MAJOR_VERSION <= 5
163 stencil->SetStencil(sts->GetOutput());
164 stencil->SetInput(binary_image);
166 stencil->SetStencilData(sts->GetOutput());
167 stencil->SetInputData(binary_image);
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();
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());
188 #if VTK_MAJOR_VERSION <= 5
189 m_Exporter->SetInput( stencil->GetOutput() );
191 m_Exporter->SetInputData( stencil->GetOutput() );
193 m_Importer->Update();
195 // writeImage<ImageType>(m_Importer->GetOutput(), "f.mhd");
197 this->SetNthOutput(0, m_Importer->GetOutput());
201 //--------------------------------------------------------------------