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);
35 //std::vector<double> displacementVector;
37 //Set up QuadEdge and filter every time polydata changes
38 if ((bbGetInputIn() != polydata) && (bbGetInputActive()==true) && (bbGetInputIn() != NULL))
42 if(lstCenter.size() != NULL)
44 backLstCenter[0] = lstCenter[0];
45 backLstCenter[1] = lstCenter[1];
46 backLstCenter[2] = lstCenter[2];
51 using PointType = MeshType::PointType;
53 polydata = bbGetInputIn();
55 //points = points of input polydata
56 vtkPoints* points=bbGetInputIn()->GetPoints();
58 quadEdgeMesh = MeshType::New();
59 double currentPoint[3] = {};
62 int numberOfPoints = points->GetNumberOfPoints();;
63 for(int pointId = 0; pointId < numberOfPoints; pointId++){
64 points->GetPoint(pointId, currentPoint);
65 p[0] = currentPoint[0];
66 p[1] = currentPoint[1];
67 p[2] = currentPoint[2];
68 quadEdgeMesh->SetPoint(pointId, p);
69 //point data must be 1 on each point so that fast marching can calculate the distance.
70 quadEdgeMesh->SetPointData(pointId, 1.);
73 //add cells to QuadEdge mesh
74 auto cellIterator = vtk::TakeSmartPointer(bbGetInputIn()->GetPolys()->NewIterator());
76 for(cellIterator->GoToFirstCell(); !cellIterator->IsDoneWithTraversal(); cellIterator->GoToNextCell()){
77 cell = cellIterator->GetCurrentCell();
78 quadEdgeMesh->AddFaceTriangle(cell->GetId(0), cell->GetId(1), cell->GetId(2));
81 using FastMarchingType = itk::FastMarchingQuadEdgeMeshFilterResultsBase<MeshType, MeshType>;
82 fmmFilter = FastMarchingType::New();
83 fmmFilter->SetInput(quadEdgeMesh);
85 //Filter and starting trial point removed because the range of point ids is not known when polydatas are split.
87 //using NodePairType = FastMarchingType::NodePairType;
88 //using NodePairContainerType = FastMarchingType::NodePairContainerType;
90 //auto trial = NodePairContainerType::New();
91 //NodePairType nodePair(0, 0.);
92 //trial->push_back(nodePair);
94 //fmmFilter->SetTrialPoints(trial);
96 EdgeIdBack = bbGetInputEdgeId();
98 using CriterionType = itk::FastMarchingThresholdStoppingCriterion<MeshType, MeshType>;
99 auto criterion = CriterionType::New();
101 criterion->SetThreshold(1);
102 fmmFilter->SetStoppingCriterion(criterion);
103 fmmFilter->SetCollectPoints(true);
104 fmmFilter->SetReleaseDataBeforeUpdateFlag(false);
105 //fmmFilter->Update();
107 //printf("PG GeodesicMeshDeformation::Process Filter Created \n");
110 if (bbGetInputTypeIn()==0) // direction
112 if (bbGetInputDirection().size()==3)
114 ok = !( (bbGetInputDirection()[0]==0) && (bbGetInputDirection()[1]==0) && (bbGetInputDirection()[2]==0) );
118 if (bbGetInputTypeIn()==1) // center
120 ok = ( lstCenter.size()==3 );
123 if ( (bbGetInputIn()!=NULL) && (ok==true) && (bbGetInputEdgeId()>=0) && (bbGetInputActive()==true) )
125 vtkPoints* points=bbGetInputIn()->GetPoints();
126 long i,size=points->GetNumberOfPoints();
127 double p[3]; // point
128 double pb[3]; // point base
129 double np[3]; // new point
135 double displcement_x = 0;
136 double displcement_y = 0;
137 double displcement_z = 0;
139 if (bbGetInputTypeIn()==0) // Direction
141 displcement_x = bbGetInputDirection()[0];
142 displcement_y = bbGetInputDirection()[1];
143 displcement_z = bbGetInputDirection()[2];
144 } // if TypeIn 0 Direction
146 //printf(" EED GeodesicMeshDeformation::Process %ld %ld - %f %f %f \n", EdgeIdBack, bbGetInputEdgeId() , lstCenter[0],lstCenter[1],lstCenter[2] );
148 if (bbGetInputTypeIn()==1) // Center
150 if (EdgeIdBack==bbGetInputEdgeId() )
152 displcement_x = (lstCenter[0]-backLstCenter[0])/1.0;
153 displcement_y = (lstCenter[1]-backLstCenter[1])/1.0;
154 displcement_z = (lstCenter[2]-backLstCenter[2])/1.0;
155 } // if EdgeIdBack!=bbGetInputEdgeId()
156 backLstCenter[0] = lstCenter[0];
157 backLstCenter[1] = lstCenter[1];
158 backLstCenter[2] = lstCenter[2];
160 } // if TypeIn 1 Center
161 points->GetPoint( bbGetInputEdgeId() , pb );
162 if (EdgeIdBack!=bbGetInputEdgeId() || pdChanged)
163 {printf("PG GeodesicMeshDeformation::Process ENTERED \n");
164 EdgeIdBack = bbGetInputEdgeId();
166 backLstCenter[0] = lstCenter[0];
167 backLstCenter[1] = lstCenter[1];
168 backLstCenter[2] = lstCenter[2];
170 using NodePairType = FastMarchingType::NodePairType;
171 using NodePairContainerType = FastMarchingType::NodePairContainerType;
173 auto processedPoints = NodePairContainerType::New();
174 fmmFilter->SetProcessedPoints(processedPoints);
176 auto trial = NodePairContainerType::New();
178 //Seed point for fast marching with distance 0
179 NodePairType nodePair(bbGetInputEdgeId(), 0.);
180 trial->push_back(nodePair);
182 fmmFilter->SetTrialPoints(trial);
184 //threshold distance for fast marching method
187 using CriterionType = itk::FastMarchingThresholdStoppingCriterion<MeshType, MeshType>;
188 auto criterion = CriterionType::New();
190 criterion->SetThreshold(s*4);
191 fmmFilter->SetStoppingCriterion(criterion);
197 if ( !((displcement_x==0) &&(displcement_y==0) && (displcement_z==0)) )
199 if(fmmFilter != NULL)
201 MeshType::PointDataContainer::Pointer pointData = fmmFilter->GetOutput()->GetPointData();
204 FastMarchingType::NodePairContainerType::Iterator BegProcessedIt = fmmFilter->GetProcessedPoints()->Begin();
205 FastMarchingType::NodePairContainerType::Iterator EndProcessedIt = fmmFilter->GetProcessedPoints()->End();
207 while (BegProcessedIt != EndProcessedIt)
210 points->GetPoint(BegProcessedIt.Value().GetNode(), pPD);
211 double weight = exp(-BegProcessedIt.Value().GetValue() * 1. / s);
213 np[0] = pPD[0]+ weight*displcement_x;
214 np[1] = pPD[1]+ weight*displcement_y;
215 np[2] = pPD[2]+ weight*displcement_z;
216 points->SetPoint(BegProcessedIt.Value().GetNode(), np);
220 //double directionMoved[3] = {lstCenter[0]-displcement_x, lstCenter[1]-displcement_y, lstCenter[2]-displcement_z};
221 //vtkMath::Normalize(directionMoved);
222 std::vector<double> info{lstCenter[0],lstCenter[1],lstCenter[2], bbGetInputDirection()[0] , bbGetInputDirection()[1], bbGetInputDirection()[2],(double) bbGetInputEdgeId(), s};
223 bbSetOutputOut(info);
224 cout << "info updated" << endl;
226 bbGetInputIn()->Modified();
227 }// if ffmFilter != NULL
228 } // if distance_x y z != 0
229 } // In != NULL ok active
232 // 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)
234 void GeodesicMeshDeformation::bbUserSetDefaultValues()
237 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
238 // Here we initialize the input 'In' to 0
239 bbSetInputActive(true);
242 std::vector<double> direction;
243 direction.push_back(1);
244 direction.push_back(0);
245 direction.push_back(0);
246 bbSetInputDirection(direction);
248 bbSetInputEdgeId(EdgeIdBack);
250 std::vector<double> OutputVect;
251 bbSetOutputOut(OutputVect);
253 backLstCenter.push_back(0);
254 backLstCenter.push_back(0);
255 backLstCenter.push_back(0);
263 // 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)
265 void GeodesicMeshDeformation::bbUserInitializeProcessing()
268 // THE INITIALIZATION METHOD BODY :
270 // but this is where you should allocate the internal/output pointers
276 // 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)
278 void GeodesicMeshDeformation::bbUserFinalizeProcessing()
281 // THE FINALIZATION METHOD BODY :
283 // but this is where you should desallocate the internal/output pointers
288 // EO namespace bbitkvtk