]> Creatis software - clitk.git/blob - itk/clitkBinaryImageToMeshFilter.txx
First version to convert image to dicomrtstruct
[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()//:ProcessObject()
24 {
25   DD("BinaryImageToMeshFilter constructor");
26   // this->ProcessObject::SetNumberOfRequiredInputs(1);
27   // this->ProcessObject::SetNumberOfRequiredOutputs(1);
28
29   // //m_OutputMesh = vtkPolyData::New();
30
31   // this->ProcessObject::SetNumberOfRequiredOutputs(1);
32   // itk::DataObject::Pointer a = itk::DataObject::New();
33   // this->ProcessObject::SetNthOutput( 0, a);
34 }
35 //--------------------------------------------------------------------
36
37
38 //--------------------------------------------------------------------
39 /*template <class ImageType>
40   void 
41   clitk::BinaryImageToMeshFilter<ImageType>::
42   GenerateOutputInformation() 
43   {
44   DD("GenerateOutputInformation");
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::BinaryImageToMeshFilter<ImageType>::
67 // SetInput(unsigned int idx, const ImageType *input)
68 // {
69 //   DD(idx);
70 //   // process object is not const-correct, the const_cast
71 //   // is required here.
72 //   this->ProcessObject::SetNthInput( idx,
73 //                                     const_cast< ImageType * >( input ) );
74 //   DD("end");
75 // }
76
77 // template <class ImageType>
78 // const ImageType *
79 // clitk::BinaryImageToMeshFilter<ImageType>::
80 // GetInput(unsigned int idx)
81 // {
82 //   DD("GetInput");
83 //   DD(idx);
84 //   return dynamic_cast< const ImageType * >
85 //          ( this->ProcessObject::GetInput(idx) );
86 // }
87
88
89 //--------------------------------------------------------------------
90 template <class ImageType>
91 void
92 clitk::BinaryImageToMeshFilter<ImageType>::
93 Update()
94 //GenerateOutputInformation()
95 {
96   DD("Update");
97
98   // Convert itk image into vtk image
99   const ImageType * im = this->GetInput();
100   typedef itk::ImageToVTKImageFilter<ImageType> ConvertType;
101   typename ConvertType::Pointer convert = ConvertType::New();
102   convert->SetInput(im);
103   convert->Update();
104   vtkImageData * input_vtk = convert->GetOutput();
105   
106   // Get extend
107   vtkSmartPointer<vtkImageClip> clipper = vtkSmartPointer<vtkImageClip>::New();
108   clipper->SetInput(input_vtk);
109   int* extent = input_vtk->GetExtent();
110   DDV(extent, 6);
111
112   // Loop on slices
113   vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
114   uint n = input_vtk->GetDimensions()[2];
115   DD(n);
116   std::vector<vtkSmartPointer<vtkPolyData> > contours;
117   for(uint i=0; i<n; i++) {
118     DD(i);
119     // FIXME     vtkDiscreteMarchingCubes INSTEAD ?
120     vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
121     squares->SetInput(input_vtk);
122     squares->SetImageRange(extent[0], extent[1], extent[2], extent[3], i, i);
123     squares->SetNumberOfContours(1);
124     squares->SetValue(0, 0.5); // FIXME background (?)
125     squares->Update();
126     // DD(squares->GetNumberOfContours());
127     
128     vtkSmartPointer<vtkPolyData> m = squares->GetOutput();
129
130     DD(m->GetMaxCellSize());
131     DD(m->GetNumberOfVerts());
132     DD(m->GetNumberOfLines());
133     DD(m->GetNumberOfPolys());
134     DD(m->GetNumberOfStrips());
135
136     // Decimate
137     if (false) { // FIXME
138       vtkSmartPointer<vtkDecimatePro> psurface = vtkDecimatePro::New();
139       psurface->SetInputConnection(squares->GetOutputPort());
140       psurface->SetTargetReduction(0.5);
141       psurface->Update();
142       m = psurface->GetOutput();
143     }
144     if (true) {
145       vtkSmartPointer<vtkStripper> vs = vtkSmartPointer<vtkStripper>::New();
146       vs->SetInput(squares->GetOutput());
147       vs->Update();
148       m = vs->GetOutput();
149     }
150     
151     //vtkSmartPointer<vtkPolyData> m = squares->GetOutput();
152     contours.push_back(m);
153     DD(m->GetMaxCellSize());
154     DD(m->GetNumberOfVerts());
155     DD(m->GetNumberOfLines());
156     DD(m->GetNumberOfPolys());
157     DD(m->GetNumberOfStrips());
158     // m->Print(std::cout);
159
160     // FIXME : only add if lines>0
161     if (m->GetNumberOfLines() > 0) {
162       append->AddInput(contours[i]);
163     }
164   }
165   DD("done");
166  
167   DD("now append");
168   // for(unsigned int i=0; i<n; i++) {
169   //   append->AddInput(contours[i]);
170   // }
171   append->Update();
172   
173   DD(" copy");
174   m_OutputMesh = vtkSmartPointer<vtkPolyData>::New();
175   m_OutputMesh->DeepCopy(append->GetOutput());
176
177   /* // NO (3D)
178   vtkSmartPointer<vtkContourFilter> pcontour = vtkContourFilter::New();
179   pcontour->SetValue(0, 0.5);
180   pcontour->SetInput(input_vtk);
181   pcontour->Update();
182   // vtkAlgorithmOutput *data = pcontour->GetOutputPort();
183   // vtkSmartPointer<vtkPolyDataMapper> skinMapper = vtkPolyDataMapper::New();
184   // skinMapper->SetInputConnection(data); //psurface->GetOutputPort()
185   // skinMapper->ScalarVisibilityOff();
186   m_OutputMesh = pcontour->GetOutput();//data
187   */
188
189   DD("end");
190   int a=12;
191   DD("erelre");
192 }
193 //--------------------------------------------------------------------
194