]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkCleanMeshWithPatch.cxx
#3513 CleanMeshWithPatch
[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 <vtkImprintFilter.h>
11
12 namespace bbcreaVtk
13 {
14
15 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,CleanMeshWithPatch)
16 BBTK_BLACK_BOX_IMPLEMENTATION(CleanMeshWithPatch,bbtk::AtomicBlackBox);
17 //===== 
18 // 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)
19 //===== 
20 void CleanMeshWithPatch::Process()
21 {
22 // THE MAIN PROCESSING METHOD BODY
23 //   Here we simply set the input 'In' value to the output 'Out'
24 //   And print out the output value
25 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
26 //    void bbSet{Input|Output}NAME(const TYPE&)
27 //    const TYPE& bbGet{Input|Output}NAME() const 
28 //    Where :
29 //    * NAME is the name of the input/output
30 //      (the one provided in the attribute 'name' of the tag 'input')
31 //    * TYPE is the C++ type of the input/output
32 //      (the one provided in the attribute 'type' of the tag 'input')
33 //    bbSetOutputOut( bbGetInputIn() );
34 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
35   
36     printf("EED CleanMeshWithPatch::Process Start \n");
37     std::vector<int>    lstIndexs   = bbGetInputLstIndexs();
38     if (( lstIndexs.size()>=3 ) && (bbGetInputMesh()!=NULL) ) {
39         
40         // Step 1. ----Get perimeter CtrlPoints-----
41         std::vector<double> lstX        = bbGetInputLstX();
42         std::vector<double> lstY        = bbGetInputLstY();
43         std::vector<double> lstZ        = bbGetInputLstZ();
44         std::vector<double> perimeterX;
45         std::vector<double> perimeterY;
46         std::vector<double> perimeterZ;
47         std::vector<double> tmpX;
48         std::vector<double> tmpY;
49         std::vector<double> tmpZ;
50         int iGroup,sizeGroups = lstIndexs.size();
51         int i,iGeneral=0,size;
52         for (iGroup=0 ;  iGroup<sizeGroups ; iGroup++)
53         {
54             if (iGroup==0)
55             {
56                 for (i=iGeneral ;  i<iGeneral+lstIndexs[iGroup] ; i++)
57                 {
58                     perimeterX.push_back( lstX[i] );
59                     perimeterY.push_back( lstY[i] );
60                     perimeterZ.push_back( lstZ[i] );
61                 } // for first group
62             } // if iGroup == 0
63             if ((iGroup!=0) && (iGroup!=sizeGroups-1))
64             {
65                 i=iGeneral+(lstIndexs[iGroup]-1);
66                 perimeterX.push_back( lstX[i] );
67                 perimeterY.push_back( lstY[i] );
68                 perimeterZ.push_back( lstZ[i] );
69                 tmpX.insert( tmpX.begin() , lstX[iGeneral] );
70                 tmpY.insert( tmpY.begin() , lstY[iGeneral] );
71                 tmpZ.insert( tmpZ.begin() , lstZ[iGeneral] );
72             }
73             if (iGroup==sizeGroups-1)
74             {
75                 for (i=iGeneral+lstIndexs[iGroup]-1 ;  i>=iGeneral ; i--)
76                 {
77                     perimeterX.push_back( lstX[i] );
78                     perimeterY.push_back( lstY[i] );
79                     perimeterZ.push_back( lstZ[i] );
80                 } // for i  last group
81             } // if iGroup == 0
82             iGeneral=iGeneral+lstIndexs[iGroup];
83         } // for
84         size=tmpX.size();
85         for (i=0; i<size ; i++)
86         {
87             perimeterX.push_back( tmpX[i] );
88             perimeterY.push_back( tmpY[i] );
89             perimeterZ.push_back( tmpZ[i] );
90         } // for i tmpX
91         // Step 2. ----Get Ids in Mesh of perimeter CtrlPoints-----
92         std::vector<double>    spc;
93         if (bbGetInputSpacing().size()<3)
94         {
95             spc.push_back(1);
96             spc.push_back(1);
97             spc.push_back(1);
98         } else {
99             spc = bbGetInputSpacing();
100         }//
101         vtkStaticPointLocator   *pointLocator;
102         pointLocator   = vtkStaticPointLocator::New();
103         pointLocator->SetDataSet( bbGetInputMesh() );
104         pointLocator->BuildLocator();
105         long int id;
106         size=perimeterX.size();
107         std::vector<long int> lstIds;
108         for (i=0;i<size;i++)
109         {
110             id = pointLocator->FindClosestPoint( perimeterX[i]*spc[0] ,perimeterY[i]*spc[1] , perimeterZ[i]*spc[2] ) ;
111             lstIds.push_back( id );
112             printf("EED id=%ld    spc=%f %f %f   point=%f %f %f \n",id,  spc[0],spc[1],spc[2],  perimeterX[i],  perimeterY[i],   perimeterZ[i]  );
113         } // for i  perimeter
114         // Step 3 Get geodesic path from segments
115         long int id1,id2;
116         vtkAppendPolyData* append                   = vtkAppendPolyData::New();
117         vtkPoints               *points ;
118         for (i=0;i<size;i++)
119         {
120             id1=lstIds[i];
121             if (i<size-1)
122             {
123                 id2 = lstIds[i+1];
124             } else {
125                 id2 = lstIds[0];
126             }// if
127             vtkDijkstraGraphGeodesicPath *pathFilter    = vtkDijkstraGraphGeodesicPath::New();
128             pathFilter->SetInputData( bbGetInputMesh() );
129             pathFilter->SetStartVertex( id1 );
130             pathFilter->SetEndVertex( id2 );
131             pathFilter->Update();
132             append->AddInputData( pathFilter->GetOutput() );
133             append->Update();
134
135             points         =  pathFilter->GetOutput()->GetPoints();
136             printf("EED nrP=%d \n", points->GetNumberOfPoints()  );
137             pathFilter->Delete();
138 //            if (i==0) bbSetOutputOut(  pathFilter->GetOutput() );
139         } // for i  perimeter
140         append->Update();
141
142         // Step 4. -- vtkImprintFilter ---
143         vtkImprintFilter *imp = vtkImprintFilter::New();
144 //        imp->SetTargetData( append->GetOutput() );
145 //        imp->SetImprintData( bbGetInputMesh() );
146         
147 //        imp->SetTargetData( bbGetInputMesh() );
148 //        imp->SetImprintData( append->GetOutput() );
149
150 //        imp->SetTargetData( bbGetInputMesh() );
151 //        imp->SetImprintData( bbGetInputPatch() );
152         
153           imp->SetTargetData(  bbGetInputMesh() );
154           imp->SetImprintData( bbGetInputPatch()  );
155
156    //     imp->SetOutputTypeToProjectedImprint();
157         imp->SetOutputTypeToTargetCells();
158     //    imp->SetOutputTypeToImprintedCells();
159     //    imp->SetOutputTypeToImprintedRegion();
160     //    imp->SetOutputTypeToMergedImprint();
161         
162         imp->SetTolerance(500);
163         imp->Update();
164
165         bbSetOutputOut( imp->GetOutput() );
166         printf("EED CleanMeshWithPatch::Process size=%d \n", perimeterX.size());
167     } // if LstIndexs size >=3
168     printf("EED CleanMeshWithPatch::Process End \n");
169 }
170
171 //===== 
172 // 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)
173 //===== 
174 void CleanMeshWithPatch::bbUserSetDefaultValues()
175 {
176 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
177 //    Here we initialize the input 'In' to 0
178    bbSetInputMesh(NULL);
179    bbSetInputPatch(NULL);
180    bbSetOutputOut(NULL);
181 }
182
183 //===== 
184 // 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)
185 //===== 
186 void CleanMeshWithPatch::bbUserInitializeProcessing()
187 {
188 //  THE INITIALIZATION METHOD BODY :
189 //    Here does nothing 
190 //    but this is where you should allocate the internal/output pointers 
191 //    if any
192 }
193
194 //===== 
195 // 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)
196 //===== 
197 void CleanMeshWithPatch::bbUserFinalizeProcessing()
198 {
199 //  THE FINALIZATION METHOD BODY :
200 //    Here does nothing 
201 //    but this is where you should desallocate the internal/output pointers 
202 //    if any
203 }
204
205 } // EO namespace bbcreaVtk
206
207