]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkCleanMeshWithPatch.cxx
f7171091cddf2d048f624819c87e46b5784e4f57
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkCleanMeshWithPatch.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4 #include "bbcreaVtkCleanMeshWithPatch.h"
5 #include "bbcreaVtkPackage.h"
6
7 #include <vtkAppendPolyData.h>
8 #include <vtkStaticPointLocator.h>
9 #include <vtkDijkstraGraphGeodesicPath.h>
10 #include <vtkIdList.h>
11 #include <vtkCharArray.h>
12 #include <vtkPolyDataConnectivityFilter.h>
13 #include <vtkPointData.h>
14 #include <vtkCellData.h>
15 #include <vtkFillHolesFilter.h>
16 #include <vtkPolyDataNormals.h>
17
18
19 // #include <vtkImprintFilter.h>
20
21
22 namespace bbcreaVtk
23 {
24
25 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,CleanMeshWithPatch)
26 BBTK_BLACK_BOX_IMPLEMENTATION(CleanMeshWithPatch,bbtk::AtomicBlackBox);
27 //===== 
28 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
29 //===== 
30 void CleanMeshWithPatch::Process()
31 {
32 // THE MAIN PROCESSING METHOD BODY
33 //   Here we simply set the input 'In' value to the output 'Out'
34 //   And print out the output value
35 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
36 //    void bbSet{Input|Output}NAME(const TYPE&)
37 //    const TYPE& bbGet{Input|Output}NAME() const 
38 //    Where :
39 //    * NAME is the name of the input/output
40 //      (the one provided in the attribute 'name' of the tag 'input')
41 //    * TYPE is the C++ type of the input/output
42 //      (the one provided in the attribute 'type' of the tag 'input')
43 //    bbSetOutputOut( bbGetInputIn() );
44 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
45   
46     std::vector<int>    lstIndexs   = bbGetInputLstIndexs();
47     if (( lstIndexs.size()>=3 ) && (bbGetInputMesh()!=NULL) ) {
48         
49         // Step 1. ----Get perimeter CtrlPoints-----
50         std::vector<double> lstX        = bbGetInputLstX();
51         std::vector<double> lstY        = bbGetInputLstY();
52         std::vector<double> lstZ        = bbGetInputLstZ();
53         std::vector<double> perimeterX;
54         std::vector<double> perimeterY;
55         std::vector<double> perimeterZ;
56         std::vector<double> tmpX;
57         std::vector<double> tmpY;
58         std::vector<double> tmpZ;
59         int iGroup,sizeGroups = lstIndexs.size();
60         int i,iGeneral=0,size;
61         for (iGroup=0 ;  iGroup<sizeGroups ; iGroup++)
62         {
63             if (iGroup==0)
64             {
65                 for (i=iGeneral ;  i<iGeneral+lstIndexs[iGroup] ; i++)
66                 {
67                     perimeterX.push_back( lstX[i] );
68                     perimeterY.push_back( lstY[i] );
69                     perimeterZ.push_back( lstZ[i] );
70                 } // for first group
71             } // if iGroup == 0
72             if ((iGroup!=0) && (iGroup!=sizeGroups-1))
73             {
74                 i=iGeneral+(lstIndexs[iGroup]-1);
75                 perimeterX.push_back( lstX[i] );
76                 perimeterY.push_back( lstY[i] );
77                 perimeterZ.push_back( lstZ[i] );
78                 tmpX.insert( tmpX.begin() , lstX[iGeneral] );
79                 tmpY.insert( tmpY.begin() , lstY[iGeneral] );
80                 tmpZ.insert( tmpZ.begin() , lstZ[iGeneral] );
81             }
82             if (iGroup==sizeGroups-1)
83             {
84                 for (i=iGeneral+lstIndexs[iGroup]-1 ;  i>=iGeneral ; i--)
85                 {
86                     perimeterX.push_back( lstX[i] );
87                     perimeterY.push_back( lstY[i] );
88                     perimeterZ.push_back( lstZ[i] );
89                 } // for i  last group
90             } // if iGroup == 0
91             iGeneral=iGeneral+lstIndexs[iGroup];
92         } // for
93         size=tmpX.size();
94         for (i=0; i<size ; i++)
95         {
96             perimeterX.push_back( tmpX[i] );
97             perimeterY.push_back( tmpY[i] );
98             perimeterZ.push_back( tmpZ[i] );
99         } // for i tmpX
100         // Step 2. ----Get Ids in Mesh of perimeter CtrlPoints-----
101         std::vector<double>    spc;
102         if (bbGetInputSpacing().size()<3)
103         {
104             spc.push_back(1);
105             spc.push_back(1);
106             spc.push_back(1);
107         } else {
108             spc = bbGetInputSpacing();
109         }//
110         vtkStaticPointLocator   *pointLocator;
111         pointLocator   = vtkStaticPointLocator::New();
112         pointLocator->SetDataSet( bbGetInputMesh() );
113         pointLocator->BuildLocator();
114         long int id;
115         size=perimeterX.size();
116         std::vector<long int> lstIdsCtrlPoints;
117         for (i=0;i<size;i++)
118         {
119             id = pointLocator->FindClosestPoint( perimeterX[i]*spc[0] ,perimeterY[i]*spc[1] , perimeterZ[i]*spc[2] ) ;
120             lstIdsCtrlPoints.push_back( id );
121         } // for i  perimeter
122         // Step 3 Get geodesic path from segments
123         long int id1,id2;
124         std::vector<long int> lstIdsGeodesicPerimter;
125         vtkAppendPolyData* append = vtkAppendPolyData::New();
126         for (i=0;i<size;i++)
127         {
128             id1=lstIdsCtrlPoints[i];
129             if (i<size-1)
130             {
131                 id2 = lstIdsCtrlPoints[i+1];
132             } else {
133                 id2 = lstIdsCtrlPoints[0];
134             }// if
135             vtkDijkstraGraphGeodesicPath *pathFilter    = vtkDijkstraGraphGeodesicPath::New();
136             pathFilter->SetInputData( bbGetInputMesh() );
137             pathFilter->SetStartVertex( id1 );
138             pathFilter->SetEndVertex( id2 );
139             pathFilter->Update();
140             append->AddInputData( pathFilter->GetOutput() );
141             append->Update();
142             
143             // Step 3.1 Puts ids of perimeter Subsegment in vector  lstIdsGeodesicPerimter
144             vtkIdList   *IdsList            = pathFilter->GetIdList();
145             long int    iIdLst,sizeIdLst    = IdsList->GetNumberOfIds();
146             for (iIdLst=0; iIdLst<sizeIdLst; iIdLst++)
147             {
148                 lstIdsGeodesicPerimter.push_back( IdsList->GetId(iIdLst) );
149             }// for iIdLst
150             
151             pathFilter->Delete();
152         } // for i  perimeter
153         append->Update();
154 //        bbSetOutputOut( append->GetOutput() );
155
156         // Step 4. -- Define scalars ---
157         vtkCharArray    *scalarsArray   = vtkCharArray::New();
158         vtkPoints       *points         = bbGetInputMesh()->GetPoints();
159         long int        iMPs,sizeMPs    = points->GetNumberOfPoints();
160         scalarsArray->SetNumberOfValues(sizeMPs);
161         for (iMPs=0; iMPs<sizeMPs; iMPs++)
162         {
163             scalarsArray->SetValue(iMPs,15);
164         } // for iMPs
165         long int    iIdLst,sizeIdLst    = lstIdsGeodesicPerimter.size();
166         for (iIdLst=0; iIdLst<sizeIdLst; iIdLst++)
167         {
168             scalarsArray->SetValue( lstIdsGeodesicPerimter[iIdLst]  ,100 );
169         }// for iIdLst
170         scalarsArray->SetName("scalarsPerimeterPatch");
171         bbGetInputMesh()->GetPointData()->AddArray( scalarsArray );
172         bbGetInputMesh()->GetPointData()->SetActiveScalars( "scalarsPerimeterPatch" );
173         bbGetInputMesh()->GetCellData()->SetActiveScalars( "scalarsPerimeterPatch" );
174
175         // Step 5.  -- Connectivity Filter
176         vtkPolyDataConnectivityFilter *connectivity = vtkPolyDataConnectivityFilter::New();
177         connectivity->SetInputData( bbGetInputMesh() );
178         connectivity->SetExtractionModeToCellSeededRegions();
179         connectivity->InitializeSeedList();
180         connectivity->AddSeed(0);
181         connectivity->SetScalarConnectivity(true);
182         connectivity->SetScalarRange(10,20);
183         connectivity->SetFullScalarConnectivity(true);
184         connectivity->Update();
185 //        bbSetOutputOut( connectivity->GetOutput() );
186
187         
188         vtkAppendPolyData* append2 = vtkAppendPolyData::New();
189         append2->AddInputData( connectivity->GetOutput()  );
190         append2->AddInputData( bbGetInputPatch()  );
191         append2->Update();
192
193         
194         vtkPolyDataNormals *normals =vtkPolyDataNormals::New();
195         normals->SetInputConnection( append2->GetOutputPort() );
196         normals->ConsistencyOn();
197         normals->SplittingOff();
198         normals->Update();
199         bbSetOutputOut( normals->GetOutput() );
200
201 //        vtkFillHolesFilter *fillHoles = vtkFillHolesFilter::New();
202 //        fillHoles->SetInputData( normals->GetOutput() );
203 //        fillHoles->SetHoleSize(1000);
204 //        fillHoles->Update();
205 //        bbSetOutputOut( fillHoles->GetOutput() );
206
207         
208         /*
209         // Step 4. -- vtkImprintFilter ---
210         vtkImprintFilter *imp = vtkImprintFilter::New();
211 //        imp->SetTargetData( append->GetOutput() );
212 //        imp->SetImprintData( bbGetInputMesh() );
213         
214 //        imp->SetTargetData( bbGetInputMesh() );
215 //        imp->SetImprintData( append->GetOutput() );
216
217 //        imp->SetTargetData( bbGetInputMesh() );
218 //        imp->SetImprintData( bbGetInputPatch() );
219         
220           imp->SetTargetData(  bbGetInputMesh() );
221           imp->SetImprintData( bbGetInputPatch()  );
222
223    //     imp->SetOutputTypeToProjectedImprint();
224         imp->SetOutputTypeToTargetCells();
225     //    imp->SetOutputTypeToImprintedCells();
226     //    imp->SetOutputTypeToImprintedRegion();
227     //    imp->SetOutputTypeToMergedImprint();
228         
229         imp->SetTolerance(500);
230         imp->Update();
231          bbSetOutputOut( imp->GetOutput() );
232 */
233         
234     } // if LstIndexs size >=3
235 }
236
237 //===== 
238 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
239 //===== 
240 void CleanMeshWithPatch::bbUserSetDefaultValues()
241 {
242 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
243 //    Here we initialize the input 'In' to 0
244    bbSetInputMesh(NULL);
245    bbSetInputPatch(NULL);
246    bbSetOutputOut(NULL);
247 }
248
249 //===== 
250 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
251 //===== 
252 void CleanMeshWithPatch::bbUserInitializeProcessing()
253 {
254 //  THE INITIALIZATION METHOD BODY :
255 //    Here does nothing 
256 //    but this is where you should allocate the internal/output pointers 
257 //    if any
258 }
259
260 //===== 
261 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
262 //===== 
263 void CleanMeshWithPatch::bbUserFinalizeProcessing()
264 {
265 //  THE FINALIZATION METHOD BODY :
266 //    Here does nothing 
267 //    but this is where you should desallocate the internal/output pointers 
268 //    if any
269 }
270
271 } // EO namespace bbcreaVtk
272
273