]> Creatis software - clitk.git/blob - itk/clitkBinaryImageToMeshFilter.txx
Merge branch 'master' of git.creatis.insa-lyon.fr:clitk
[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   clipper->SetInput(input_vtk);
47   int* extent = input_vtk->GetExtent();
48
49   // Loop on slices
50   vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
51   uint n = input_vtk->GetDimensions()[2];
52   // std::vector<vtkSmartPointer<vtkPolyData> > contours;
53   for(uint i=0; i<n; i++) {
54     vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
55     squares->SetInput(input_vtk);
56     squares->SetImageRange(extent[0], extent[1], extent[2], extent[3], i, i);
57     squares->SetNumberOfContours(1);
58     squares->SetValue(0, m_ThresholdValue);
59     squares->Update();    
60     vtkSmartPointer<vtkPolyData> m = squares->GetOutput();
61
62     // Strip (needed)
63     vtkSmartPointer<vtkStripper> vs = vtkSmartPointer<vtkStripper>::New();
64     vs->SetInput(squares->GetOutput());
65     vs->Update();
66     m = vs->GetOutput();
67
68     // Decimate
69     if (false) { // FIXME (do not work)
70       vtkSmartPointer<vtkDecimatePro> psurface = vtkDecimatePro::New();
71       psurface->SetInputConnection(squares->GetOutputPort());
72       psurface->SetTargetReduction(0.5);
73       psurface->Update();
74       m = psurface->GetOutput();
75     }
76
77     // only add if lines>0
78     if (m->GetNumberOfLines() > 0) {
79       append->AddInput(m);//contours[i]);
80     }
81   }
82   append->Update();
83   
84   m_OutputMesh = vtkSmartPointer<vtkPolyData>::New();
85   m_OutputMesh->DeepCopy(append->GetOutput());
86 }
87 //--------------------------------------------------------------------
88