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)
4 #include "bbcreaVtkCleanMeshWithPatch.h"
5 #include "bbcreaVtkPackage.h"
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>
19 // #include <vtkImprintFilter.h>
25 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,CleanMeshWithPatch)
26 BBTK_BLACK_BOX_IMPLEMENTATION(CleanMeshWithPatch,bbtk::AtomicBlackBox);
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)
30 void CleanMeshWithPatch::Process()
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
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;
46 std::vector<int> lstIndexs = bbGetInputLstIndexs();
47 if (( lstIndexs.size()>=3 ) && (bbGetInputMesh()!=NULL) ) {
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++)
65 for (i=iGeneral ; i<iGeneral+lstIndexs[iGroup] ; i++)
67 perimeterX.push_back( lstX[i] );
68 perimeterY.push_back( lstY[i] );
69 perimeterZ.push_back( lstZ[i] );
72 if ((iGroup!=0) && (iGroup!=sizeGroups-1))
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] );
82 if (iGroup==sizeGroups-1)
84 for (i=iGeneral+lstIndexs[iGroup]-1 ; i>=iGeneral ; i--)
86 perimeterX.push_back( lstX[i] );
87 perimeterY.push_back( lstY[i] );
88 perimeterZ.push_back( lstZ[i] );
91 iGeneral=iGeneral+lstIndexs[iGroup];
94 for (i=0; i<size ; i++)
96 perimeterX.push_back( tmpX[i] );
97 perimeterY.push_back( tmpY[i] );
98 perimeterZ.push_back( tmpZ[i] );
100 // Step 2. ----Get Ids in Mesh of perimeter CtrlPoints-----
101 std::vector<double> spc;
102 if (bbGetInputSpacing().size()<3)
108 spc = bbGetInputSpacing();
110 vtkStaticPointLocator *pointLocator;
111 pointLocator = vtkStaticPointLocator::New();
112 pointLocator->SetDataSet( bbGetInputMesh() );
113 pointLocator->BuildLocator();
115 size=perimeterX.size();
116 std::vector<long int> lstIdsCtrlPoints;
119 id = pointLocator->FindClosestPoint( perimeterX[i]*spc[0] ,perimeterY[i]*spc[1] , perimeterZ[i]*spc[2] ) ;
120 lstIdsCtrlPoints.push_back( id );
122 // Step 3 Get geodesic path from segments
124 std::vector<long int> lstIdsGeodesicPerimter;
125 vtkAppendPolyData* append = vtkAppendPolyData::New();
128 id1=lstIdsCtrlPoints[i];
131 id2 = lstIdsCtrlPoints[i+1];
133 id2 = lstIdsCtrlPoints[0];
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() );
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++)
148 lstIdsGeodesicPerimter.push_back( IdsList->GetId(iIdLst) );
151 pathFilter->Delete();
154 // bbSetOutputOut( append->GetOutput() );
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++)
163 scalarsArray->SetValue(iMPs,15);
165 long int iIdLst,sizeIdLst = lstIdsGeodesicPerimter.size();
166 for (iIdLst=0; iIdLst<sizeIdLst; iIdLst++)
168 scalarsArray->SetValue( lstIdsGeodesicPerimter[iIdLst] ,100 );
170 scalarsArray->SetName("scalarsPerimeterPatch");
171 bbGetInputMesh()->GetPointData()->AddArray( scalarsArray );
172 bbGetInputMesh()->GetPointData()->SetActiveScalars( "scalarsPerimeterPatch" );
173 bbGetInputMesh()->GetCellData()->SetActiveScalars( "scalarsPerimeterPatch" );
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() );
188 vtkAppendPolyData* append2 = vtkAppendPolyData::New();
189 append2->AddInputData( connectivity->GetOutput() );
190 append2->AddInputData( bbGetInputPatch() );
194 vtkPolyDataNormals *normals =vtkPolyDataNormals::New();
195 normals->SetInputConnection( append2->GetOutputPort() );
196 normals->ConsistencyOn();
197 normals->SplittingOff();
199 bbSetOutputOut( normals->GetOutput() );
201 // vtkFillHolesFilter *fillHoles = vtkFillHolesFilter::New();
202 // fillHoles->SetInputData( normals->GetOutput() );
203 // fillHoles->SetHoleSize(1000);
204 // fillHoles->Update();
205 // bbSetOutputOut( fillHoles->GetOutput() );
209 // Step 4. -- vtkImprintFilter ---
210 vtkImprintFilter *imp = vtkImprintFilter::New();
211 // imp->SetTargetData( append->GetOutput() );
212 // imp->SetImprintData( bbGetInputMesh() );
214 // imp->SetTargetData( bbGetInputMesh() );
215 // imp->SetImprintData( append->GetOutput() );
217 // imp->SetTargetData( bbGetInputMesh() );
218 // imp->SetImprintData( bbGetInputPatch() );
220 imp->SetTargetData( bbGetInputMesh() );
221 imp->SetImprintData( bbGetInputPatch() );
223 // imp->SetOutputTypeToProjectedImprint();
224 imp->SetOutputTypeToTargetCells();
225 // imp->SetOutputTypeToImprintedCells();
226 // imp->SetOutputTypeToImprintedRegion();
227 // imp->SetOutputTypeToMergedImprint();
229 imp->SetTolerance(500);
231 bbSetOutputOut( imp->GetOutput() );
234 } // if LstIndexs size >=3
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)
240 void CleanMeshWithPatch::bbUserSetDefaultValues()
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);
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)
252 void CleanMeshWithPatch::bbUserInitializeProcessing()
254 // THE INITIALIZATION METHOD BODY :
256 // but this is where you should allocate the internal/output pointers
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)
263 void CleanMeshWithPatch::bbUserFinalizeProcessing()
265 // THE FINALIZATION METHOD BODY :
267 // but this is where you should desallocate the internal/output pointers
271 } // EO namespace bbcreaVtk