]> Creatis software - clitk.git/blob - itk/clitkBinaryImageToMeshFilter.txx
Debug RTStruct conversion with empty struc
[clitk.git] / itk / clitkBinaryImageToMeshFilter.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
20 //--------------------------------------------------------------------
21 template <class ImageType>
22 clitk::BinaryImageToMeshFilter<ImageType>::
23 BinaryImageToMeshFilter():itk::Object()
24 {
25   m_ThresholdValue = 0.5; // (for image with 0=background and 1=foreground) 
26 }
27 //--------------------------------------------------------------------
28
29
30 //--------------------------------------------------------------------
31 template <class ImageType>
32 void
33 clitk::BinaryImageToMeshFilter<ImageType>::
34 Update()
35 {
36   // Convert itk image into vtk image
37   const ImageType * im = this->GetInput();
38   typedef itk::ImageToVTKImageFilter<ImageType> ConvertType;
39   typename ConvertType::Pointer convert = ConvertType::New();
40   convert->SetInput(im);
41   convert->Update();
42   vtkImageData * input_vtk = convert->GetOutput();
43   
44   // Get extend
45   vtkSmartPointer<vtkImageClip> clipper = vtkSmartPointer<vtkImageClip>::New();
46 #if VTK_MAJOR_VERSION <= 5
47   clipper->SetInput(input_vtk);
48 #else
49   clipper->SetInputData(input_vtk);
50 #endif
51   int* extent = input_vtk->GetExtent();
52
53   // Loop on slices
54   vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
55   uint n = input_vtk->GetDimensions()[2];
56   // std::vector<vtkSmartPointer<vtkPolyData> > contours;
57   for(uint i=0; i<n; i++) {
58     vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
59 #if VTK_MAJOR_VERSION <= 5
60     squares->SetInput(input_vtk);
61 #else
62     squares->SetInputData(input_vtk);
63 #endif
64     squares->SetImageRange(extent[0], extent[1], extent[2], extent[3], i, i);
65     squares->SetNumberOfContours(1);
66     squares->SetValue(0, m_ThresholdValue);
67     squares->Update();    
68     vtkSmartPointer<vtkPolyData> m = squares->GetOutput();
69
70     // Strip (needed)
71     vtkSmartPointer<vtkStripper> vs = vtkSmartPointer<vtkStripper>::New();
72 #if VTK_MAJOR_VERSION <= 5
73     vs->SetInput(squares->GetOutput());
74 #else
75     vs->SetInputData(squares->GetOutput());
76 #endif
77     vs->Update();
78     m = vs->GetOutput();
79
80     // Decimate
81     if (false) { // FIXME (do not work)
82       vtkSmartPointer<vtkDecimatePro> psurface = vtkDecimatePro::New();
83       psurface->SetInputConnection(squares->GetOutputPort());
84       psurface->SetTargetReduction(0.5);
85       psurface->Update();
86       m = psurface->GetOutput();
87     }
88
89     // only add if lines>0
90     if (m->GetNumberOfLines() > 0) {
91 #if VTK_MAJOR_VERSION <= 5
92       append->AddInput(m);//contours[i]);
93 #else
94       append->AddInputData(m);//contours[i]);
95 #endif
96     }
97   }
98   append->Update();
99   
100   m_OutputMesh = vtkSmartPointer<vtkPolyData>::New();
101   m_OutputMesh->DeepCopy(append->GetOutput());
102 }
103 //--------------------------------------------------------------------
104