]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/kernel/vtkSTLExtractor.cpp
2345 creaMaracasVisu Feature Test Phase Normal Ruler01XY
[creaMaracasVisu.git] / lib / maracasVisuLib / src / kernel / vtkSTLExtractor.cpp
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26
27 #include "vtkSTLExtractor.h"
28 #include <vtkImageGaussianSmooth.h>
29 #include <vtkMarchingCubes.h>
30 #include <vtkCleanPolyData.h>
31 #include <vtkTriangleFilter.h>
32 #include <vtkImageContinuousDilate3D.h>
33
34
35 vtkSTLExtractor::vtkSTLExtractor(){
36         innerSurface = vtkPolyData::New();
37         outerSurface = vtkPolyData::New();
38         marchingCubesLevel = 128.0;
39         sigmaLevel = 100.0;
40 }
41
42 vtkSTLExtractor::~vtkSTLExtractor(){
43         innerSurface->Delete();
44         outerSurface->Delete();
45 }
46
47 void vtkSTLExtractor::setVolume(vtkImageData *volume){
48         this->volume = volume;
49 }
50
51
52 void vtkSTLExtractor::setMarchingCubesLevel(double level){
53         this->marchingCubesLevel = level;
54 }
55
56 void vtkSTLExtractor::setSigmaLevel(double level){
57         this->sigmaLevel = level;
58 }
59
60
61 double vtkSTLExtractor::getMarchingCubesLevel(){
62         return marchingCubesLevel;
63 }
64
65 double vtkSTLExtractor::getSigmaLevel(){
66         return sigmaLevel;
67 }
68
69
70 vtkPolyData* vtkSTLExtractor::getInnerSurface(){
71         return innerSurface;
72 }
73
74
75 vtkPolyData* vtkSTLExtractor::getOuterSurface(){
76         return outerSurface;
77 }
78
79
80
81 void vtkSTLExtractor::calculate(){
82
83         // ------------------------------------------------------------------------
84                 //  1. CALCULATING THE INNER SURFACE
85                 // ------------------------------------------------------------------------
86             vtkImageGaussianSmooth *gaussFilter = vtkImageGaussianSmooth::New();
87         gaussFilter->SetInput(volume);
88         gaussFilter->SetDimensionality(3);
89         gaussFilter->SetStandardDeviation(sigmaLevel);
90     
91         vtkMarchingCubes *cubesFilter = vtkMarchingCubes::New();
92         cubesFilter->SetInput(gaussFilter->GetOutput());
93         cubesFilter->SetValue(0,marchingCubesLevel);
94                 cubesFilter->ComputeGradientsOn ();
95         cubesFilter->ComputeScalarsOn ();
96                 cubesFilter->SetNumberOfContours( 1 );
97             
98                 // Unir puntos duplicados y remover primitivas degeneradas
99                 vtkCleanPolyData *cleanFilter = vtkCleanPolyData::New();
100         cleanFilter->SetInput ( cubesFilter->GetOutput() );
101                 
102                 // crea poligonos triangulares
103         vtkTriangleFilter *triangleFilter = vtkTriangleFilter::New();
104         triangleFilter->SetInput( cleanFilter->GetOutput() );
105         triangleFilter->Update();
106
107                 innerSurface->DeepCopy(triangleFilter->GetOutput());
108
109
110                 // ------------------------------------------------------------------------
111                 //  2. CALCULATING THE OUTER SURFACE
112                 // ------------------------------------------------------------------------
113                 vtkImageContinuousDilate3D *dilateFilter = vtkImageContinuousDilate3D ::New();
114         dilateFilter->SetInput (volume);
115                 dilateFilter->SetKernelSize (3, 3, 3);
116                 gaussFilter->SetInput( dilateFilter->GetOutput());
117         triangleFilter->Update();
118                 
119             outerSurface->DeepCopy(triangleFilter->GetOutput());
120
121                 // ------------------------------------------------------------------------
122                 //  3. CLEANING UP
123                 // ------------------------------------------------------------------------
124
125                 gaussFilter->Delete();
126                 cubesFilter->Delete();
127                 cleanFilter->Delete();
128             triangleFilter->Delete();
129         dilateFilter->Delete();
130 }