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 "bbitkvtkGeodesicMeshDeformation.h"
5 #include "bbitkvtkPackage.h"
6 #include <vtkCellArrayIterator.h>
10 BBTK_ADD_BLACK_BOX_TO_PACKAGE(itkvtk,GeodesicMeshDeformation)
11 BBTK_BLACK_BOX_IMPLEMENTATION(GeodesicMeshDeformation,bbtk::AtomicBlackBox);
13 // 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)
15 void GeodesicMeshDeformation::Process()
17 // THE MAIN PROCESSING METHOD BODY
18 // Here we simply set the input 'In' value to the output 'Out'
19 // And print out the output value
20 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
21 // void bbSet{Input|Output}NAME(const TYPE&)
22 // const TYPE& bbGet{Input|Output}NAME() const
24 // * NAME is the name of the input/output
25 // (the one provided in the attribute 'name' of the tag 'input')
26 // * TYPE is the C++ type of the input/output
27 // (the one provided in the attribute 'type' of the tag 'input')
28 std::vector<double> lstCenter = bbGetInputCenter();
29 double s = bbGetInputS();
31 bool pdChanged = false;
32 using MeshType = itk::QuadEdgeMesh<double, 3>;
33 std::vector<double> deformInfo;
34 bbSetOutputOut(deformInfo);
36 //Set up QuadEdge and filter every time polydata changes
37 if ((bbGetInputIn() != polydata) && (bbGetInputActive()==true) && (bbGetInputIn() != NULL))
41 if(lstCenter.size() != NULL)
43 backLstCenter[0] = lstCenter[0];
44 backLstCenter[1] = lstCenter[1];
45 backLstCenter[2] = lstCenter[2];
50 using PointType = MeshType::PointType;
52 polydata = bbGetInputIn();
54 //points = points of input polydata
55 vtkPoints* points=bbGetInputIn()->GetPoints();
57 quadEdgeMesh = MeshType::New();
58 double currentPoint[3] = {};
61 int numberOfPoints = points->GetNumberOfPoints();;
62 for(int pointId = 0; pointId < numberOfPoints; pointId++){
63 points->GetPoint(pointId, currentPoint);
64 p[0] = currentPoint[0];
65 p[1] = currentPoint[1];
66 p[2] = currentPoint[2];
67 quadEdgeMesh->SetPoint(pointId, p);
68 //point data must be 1 on each point so that fast marching can calculate the distance.
69 quadEdgeMesh->SetPointData(pointId, 1.);
72 //add cells to QuadEdge mesh
73 auto cellIterator = vtk::TakeSmartPointer(bbGetInputIn()->GetPolys()->NewIterator());
75 for(cellIterator->GoToFirstCell(); !cellIterator->IsDoneWithTraversal(); cellIterator->GoToNextCell()){
76 cell = cellIterator->GetCurrentCell();
77 quadEdgeMesh->AddFaceTriangle(cell->GetId(0), cell->GetId(1), cell->GetId(2));
80 using FastMarchingType = itk::FastMarchingQuadEdgeMeshFilterResultsBase<MeshType, MeshType>;
81 fmmFilter = FastMarchingType::New();
82 fmmFilter->SetInput(quadEdgeMesh);
84 //Filter and starting trial point removed because the range of point ids is not known when polydatas are split.
86 //using NodePairType = FastMarchingType::NodePairType;
87 //using NodePairContainerType = FastMarchingType::NodePairContainerType;
89 //auto trial = NodePairContainerType::New();
90 //NodePairType nodePair(0, 0.);
91 //trial->push_back(nodePair);
93 //fmmFilter->SetTrialPoints(trial);
95 EdgeIdBack = bbGetInputEdgeId();
97 using CriterionType = itk::FastMarchingThresholdStoppingCriterion<MeshType, MeshType>;
98 auto criterion = CriterionType::New();
100 criterion->SetThreshold(1);
101 fmmFilter->SetStoppingCriterion(criterion);
102 fmmFilter->SetCollectPoints(true);
103 fmmFilter->SetReleaseDataBeforeUpdateFlag(false);
104 //fmmFilter->Update();
106 //printf("PG GeodesicMeshDeformation::Process Filter Created \n");
109 if (bbGetInputTypeIn()==0) // direction
111 if (bbGetInputDirection().size()==3)
113 ok = !( (bbGetInputDirection()[0]==0) && (bbGetInputDirection()[1]==0) && (bbGetInputDirection()[2]==0) );
117 if (bbGetInputTypeIn()==1) // center
119 ok = ( lstCenter.size()==3 );
122 if ( (bbGetInputIn()!=NULL) && (ok==true) && (bbGetInputEdgeId()>=0) && (bbGetInputActive()==true) )
124 vtkPoints* points=bbGetInputIn()->GetPoints();
125 long i,size=points->GetNumberOfPoints();
126 double p[3]; // point
127 double pb[3]; // point base
128 double np[3]; // new point
134 double displcement_x = 0;
135 double displcement_y = 0;
136 double displcement_z = 0;
138 if (bbGetInputTypeIn()==0) // Direction
140 displcement_x = bbGetInputDirection()[0];
141 displcement_y = bbGetInputDirection()[1];
142 displcement_z = bbGetInputDirection()[2];
143 } // if TypeIn 0 Direction
145 //printf(" EED GeodesicMeshDeformation::Process %ld %ld - %f %f %f \n", EdgeIdBack, bbGetInputEdgeId() , lstCenter[0],lstCenter[1],lstCenter[2] );
147 if (bbGetInputTypeIn()==1) // Center
149 if (EdgeIdBack==bbGetInputEdgeId() )
151 displcement_x = (lstCenter[0]-backLstCenter[0])/1.0;
152 displcement_y = (lstCenter[1]-backLstCenter[1])/1.0;
153 displcement_z = (lstCenter[2]-backLstCenter[2])/1.0;
154 } // if EdgeIdBack!=bbGetInputEdgeId()
155 backLstCenter[0] = lstCenter[0];
156 backLstCenter[1] = lstCenter[1];
157 backLstCenter[2] = lstCenter[2];
159 } // if TypeIn 1 Center
160 points->GetPoint( bbGetInputEdgeId() , pb );
161 if (EdgeIdBack!=bbGetInputEdgeId() || pdChanged)
162 {printf("PG GeodesicMeshDeformation::Process ENTERED \n");
163 EdgeIdBack = bbGetInputEdgeId();
165 backLstCenter[0] = lstCenter[0];
166 backLstCenter[1] = lstCenter[1];
167 backLstCenter[2] = lstCenter[2];
169 using NodePairType = FastMarchingType::NodePairType;
170 using NodePairContainerType = FastMarchingType::NodePairContainerType;
172 auto processedPoints = NodePairContainerType::New();
173 fmmFilter->SetProcessedPoints(processedPoints);
175 auto trial = NodePairContainerType::New();
177 //Seed point for fast marching with distance 0
178 NodePairType nodePair(bbGetInputEdgeId(), 0.);
179 trial->push_back(nodePair);
181 fmmFilter->SetTrialPoints(trial);
183 //threshold distance for fast marching method
186 using CriterionType = itk::FastMarchingThresholdStoppingCriterion<MeshType, MeshType>;
187 auto criterion = CriterionType::New();
189 criterion->SetThreshold(s*4);
190 fmmFilter->SetStoppingCriterion(criterion);
196 if ( !((displcement_x==0) &&(displcement_y==0) && (displcement_z==0)) )
198 if(fmmFilter != NULL)
200 MeshType::PointDataContainer::Pointer pointData = fmmFilter->GetOutput()->GetPointData();
203 FastMarchingType::NodePairContainerType::Iterator BegProcessedIt = fmmFilter->GetProcessedPoints()->Begin();
204 FastMarchingType::NodePairContainerType::Iterator EndProcessedIt = fmmFilter->GetProcessedPoints()->End();
206 while (BegProcessedIt != EndProcessedIt)
209 points->GetPoint(BegProcessedIt.Value().GetNode(), pPD);
210 double weight = exp(-BegProcessedIt.Value().GetValue() * 1. / s);
212 np[0] = pPD[0]+ weight*displcement_x;
213 np[1] = pPD[1]+ weight*displcement_y;
214 np[2] = pPD[2]+ weight*displcement_z;
215 points->SetPoint(BegProcessedIt.Value().GetNode(), np);
219 std::vector<double> info{pb[0],pb[1],pb[2], (double) bbGetInputEdgeId(), s};
220 bbSetOutputOut(info);
221 cout << "info updated" << endl;
223 bbGetInputIn()->Modified();
224 }// if ffmFilter != NULL
225 } // if distance_x y z != 0
226 } // In != NULL ok active
229 // 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)
231 void GeodesicMeshDeformation::bbUserSetDefaultValues()
234 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
235 // Here we initialize the input 'In' to 0
236 bbSetInputActive(true);
239 std::vector<double> direction;
240 direction.push_back(1);
241 direction.push_back(0);
242 direction.push_back(0);
243 bbSetInputDirection(direction);
245 bbSetInputEdgeId(EdgeIdBack);
247 std::vector<double> OutputVect;
248 bbSetOutputOut(OutputVect);
250 backLstCenter.push_back(0);
251 backLstCenter.push_back(0);
252 backLstCenter.push_back(0);
260 // 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 void GeodesicMeshDeformation::bbUserInitializeProcessing()
265 // THE INITIALIZATION METHOD BODY :
267 // but this is where you should allocate the internal/output pointers
273 // 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)
275 void GeodesicMeshDeformation::bbUserFinalizeProcessing()
278 // THE FINALIZATION METHOD BODY :
280 // but this is where you should desallocate the internal/output pointers
285 // EO namespace bbitkvtk