33 #include <vtkPointData.h> 34 #include <vtkPolyData.h> 35 #include <vtkPolyDataWriter.h> 36 #include <vtkCellArray.h> 37 #include <vtkMatrix4x4.h> 38 #include <vtkLinearTransform.h> 39 #include <vtkLandmarkTransform.h> 43 #include <boost/math/special_functions/fpclassify.hpp> 44 #include "vtkCardinalSpline.h" 54 mFixedPointSet = PointSetType::New();
55 mMovingPointSet = PointSetType::New();
56 mRegistration = RegistrationType::New();
57 mTransform = TransformType::New();
58 MetricType::Pointer metric = MetricType::New();
59 mOptimizer = OptimizerType::New();
60 mOptimizer->SetUseCostFunctionGradient(
false);
61 OptimizerType::ScalesType scales(mTransform->GetNumberOfParameters());
63 unsigned long numberOfIterations = 2000;
64 double gradientTolerance = 1e-4;
65 double valueTolerance = 1e-4;
66 double epsilonFunction = 1e-5;
69 mOptimizer->SetNumberOfIterations(numberOfIterations);
70 mOptimizer->SetValueTolerance(valueTolerance);
71 mOptimizer->SetGradientTolerance(gradientTolerance);
72 mOptimizer->SetEpsilonFunction(epsilonFunction);
75 mTransform->SetIdentity();
76 mRegistration->SetInitialTransformParameters(mTransform->GetParameters());
79 mRegistration->SetMetric(metric);
80 mRegistration->SetOptimizer(mOptimizer);
81 mRegistration->SetTransform(mTransform);
91 for (
unsigned i=0; i<positions.cols(); ++i)
93 retval->InsertNextPoint(positions(0,i), positions(1,i), positions(2,i));
101 OptimizerType::ScalesType scales(mTransform->GetNumberOfParameters());
102 const double translationScale = 40.0;
103 const double rotationScale = 0.3;
106 scales[0] = 1.0 / rotationScale;
110 scales[1] = 1.0 / rotationScale;
114 scales[2] = 1.0 / rotationScale;
118 scales[3] = 1.0 / translationScale;
122 scales[4] = 1.0 / translationScale;
126 scales[5] = 1.0 / translationScale;
130 mOptimizer->SetScales(scales);
131 mRegistration->SetOptimizer(mOptimizer);
133 std::cout <<
"Update scales: " 134 << mRegistration->GetOptimizer()->GetScales()
143 int numberOfInputPoints = centerline->GetNumberOfPoints();
144 int controlPointFactor = 10;
145 int numberOfControlPoints = numberOfInputPoints / controlPointFactor;
146 if (numberOfControlPoints < 10)
147 numberOfControlPoints = std::min(numberOfInputPoints,10);
149 int numberOfOutputPoints = std::max(numberOfInputPoints, 100);
156 if (numberOfControlPoints >= 2)
159 for(
int i=0; i<numberOfControlPoints; i++)
161 int indexP = (i*numberOfInputPoints)/numberOfControlPoints;
163 centerline->GetPoint(indexP,p);
164 int indexSpline = (i*numberOfOutputPoints)/numberOfControlPoints;
165 splineX->AddPoint( indexSpline, p[0] );
166 splineY->AddPoint( indexSpline, p[1] );
167 splineZ->AddPoint( indexSpline, p[2] );
173 centerline->GetPoint( numberOfInputPoints-1, p );
174 splineX->AddPoint( numberOfOutputPoints-1, p[0] );
175 splineY->AddPoint( numberOfOutputPoints-1, p[1] );
176 splineZ->AddPoint( numberOfOutputPoints-1, p[2] );
180 for(
int i=0; i<numberOfOutputPoints; i++)
182 double splineParameter = i;
183 newCenterline->InsertNextPoint(splineX->Evaluate(splineParameter),
184 splineY->Evaluate(splineParameter),
185 splineZ->Evaluate(splineParameter));
191 return newCenterline;
198 int N = centerline->GetNumberOfPoints();
199 Eigen::MatrixXd CLpoints(3,N);
200 for(vtkIdType i = 0; i < N; i++)
203 centerline->GetPoint(i,p);
205 pos(0) = p[0]; pos(1) = p[1]; pos(2) = p[2];
206 pos = rMd.coord(pos);
207 processedPositions->InsertNextPoint(pos[0],pos[1],pos[2]);
213 return processedPositions;
220 for(TimedTransformMap::iterator iter=trackingData_prMt.begin();
221 iter!=trackingData_prMt.end();++iter)
224 Vector3D pos = prMt.matrix().block<3,1> (0, 3);
225 pos = rMpr.coord(pos);
226 loggedPositions->InsertNextPoint(pos[0],pos[1],pos[2]);
229 return loggedPositions;
234 mFixedPointSet->Initialize();
239 for(vtkIdType n=0;n<vtkPoints->GetNumberOfPoints();n++)
241 double temp_Point[3];
242 vtkPoints->GetPoint(n, temp_Point);
243 point[0] = temp_Point[0];
244 point[1] = temp_Point[1];
245 point[2] = temp_Point[2];
246 Vector3D vPoint(temp_Point[0], temp_Point[1], temp_Point[2]);
247 itkPoints->InsertElement(n,point);
249 mFixedPointSet->SetPoints(itkPoints);
254 mMovingPointSet->Initialize();
258 for(vtkIdType n=0;n<vtkPoints->GetNumberOfPoints();n++)
260 double temp_Point[3];
261 vtkPoints->GetPoint(n, temp_Point);
262 Vector3D vPoint(temp_Point[0], temp_Point[1], temp_Point[2]);
263 point[0] = temp_Point[0];
264 point[1] = temp_Point[1];
265 point[2] = temp_Point[2];
266 itkPoints->InsertElement(n,point);
268 mMovingPointSet->SetPoints(itkPoints);
273 mRegistration->SetFixedPointSet(mFixedPointSet);
274 mRegistration->SetMovingPointSet(mMovingPointSet);
276 typedef itk::Matrix<double,3,3> MatrixType;
277 typedef TransformType::OffsetType TranslateVector;
278 MatrixType initMatrix;
279 TranslateVector initTranslation;
282 for(
int i=0; i<3; i++)
283 for(
int j=0;j<3;j++) {
284 double element = init_transform(i,j);
285 initMatrix(i,j) = element;
289 initTranslation[0] = init_transform(0,3);
290 initTranslation[1] = init_transform(1,3);
291 initTranslation[2] = init_transform(2,3);
293 mTransform->SetMatrix(initMatrix, 1e-5);
294 mTransform->SetTranslation(initTranslation);
296 mRegistration->SetInitialTransformParameters(mTransform->GetParameters());
300 mRegistration->Update();
302 catch (itk::ExceptionObject &exp)
304 std::cout <<
"CenterlineRegMethod - Exception caught ! " << std::endl;
305 std::cout << exp << std::endl;
308 RegistrationType::ParametersType finalParameters =
309 mRegistration->GetLastTransformParameters();
311 TransformType::MatrixType regMatrix = mTransform->GetMatrix();
313 for(
int i=0; i<3; i++)
314 for(
int j=0;j<3;j++) {
315 double element = regMatrix(i,j);
316 mResultTransform(i,j) = element;
319 mResultTransform(0,3) = finalParameters(3);
320 mResultTransform(1,3) = finalParameters(4);
321 mResultTransform(2,3) = finalParameters(5);
323 mRegistrationUpdated =
true;
339 std::cout <<
"rMpr : " << std::endl;
340 std::cout << rMpr << std::endl;
vtkPointsPtr processCenterline(vtkPolyDataPtr centerline, Transform3D rMd)
vtkPointsPtr ConvertTrackingDataToVTK(TimedTransformMap trackingData_prMt, Transform3D rMpr)
PointSetType::PointType PointType
virtual ~CenterlineRegistration()
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
void SetMovingPoints(vtkPointsPtr points)
vtkSmartPointer< vtkPoints > vtkPointsPtr
Transform3D runCenterlineRegistration(vtkPolyDataPtr centerline, Transform3D rMd, TimedTransformMap trackingData_prMt, Transform3D old_rMpr)
vtkSmartPointer< class vtkCardinalSpline > vtkCardinalSplinePtr
vtkPointsPtr smoothPositions(vtkPointsPtr centerline)
vtkPointsPtr convertTovtkPoints(Eigen::MatrixXd positions)
vtkSmartPointer< class vtkCardinalSpline > vtkCardinalSplinePtr
Transform3D FullRegisterMoving(Transform3D init_transform)
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
PointSetType::PointsContainerPointer PointsContainerPtr
void UpdateScales(bool xRot, bool yRot, bool zRot, bool xTrans, bool yTrans, bool zTrans)
void SetFixedPoints(vtkPointsPtr points)
std::map< double, Transform3D > TimedTransformMap
Namespace for all CustusX production code.