12 #include <vtkPointData.h> 13 #include <vtkPolyData.h> 14 #include <vtkPolyDataWriter.h> 15 #include <vtkCellArray.h> 16 #include <vtkMatrix4x4.h> 17 #include <vtkLinearTransform.h> 18 #include <vtkLandmarkTransform.h> 22 #include <boost/math/special_functions/fpclassify.hpp> 23 #include "vtkCardinalSpline.h" 33 mFixedPointSet = PointSetType::New();
34 mMovingPointSet = PointSetType::New();
35 mRegistration = RegistrationType::New();
36 mTransform = TransformType::New();
37 MetricType::Pointer metric = MetricType::New();
38 mOptimizer = OptimizerType::New();
39 mOptimizer->SetUseCostFunctionGradient(
false);
40 OptimizerType::ScalesType scales(mTransform->GetNumberOfParameters());
42 unsigned long numberOfIterations = 2000;
43 double gradientTolerance = 1e-4;
44 double valueTolerance = 1e-4;
45 double epsilonFunction = 1e-5;
48 mOptimizer->SetNumberOfIterations(numberOfIterations);
49 mOptimizer->SetValueTolerance(valueTolerance);
50 mOptimizer->SetGradientTolerance(gradientTolerance);
51 mOptimizer->SetEpsilonFunction(epsilonFunction);
54 mTransform->SetIdentity();
55 mRegistration->SetInitialTransformParameters(mTransform->GetParameters());
58 mRegistration->SetMetric(metric);
59 mRegistration->SetOptimizer(mOptimizer);
60 mRegistration->SetTransform(mTransform);
70 for (
unsigned i=0; i<positions.cols(); ++i)
72 retval->InsertNextPoint(positions(0,i), positions(1,i), positions(2,i));
80 OptimizerType::ScalesType scales(mTransform->GetNumberOfParameters());
81 const double translationScale = 40.0;
82 const double rotationScale = 0.3;
85 scales[0] = 1.0 / rotationScale;
89 scales[1] = 1.0 / rotationScale;
93 scales[2] = 1.0 / rotationScale;
97 scales[3] = 1.0 / translationScale;
101 scales[4] = 1.0 / translationScale;
105 scales[5] = 1.0 / translationScale;
109 mOptimizer->SetScales(scales);
110 mRegistration->SetOptimizer(mOptimizer);
112 std::cout <<
"Update scales: " 113 << mRegistration->GetOptimizer()->GetScales()
122 int numberOfInputPoints = centerline->GetNumberOfPoints();
123 int controlPointFactor = 10;
124 int numberOfControlPoints = numberOfInputPoints / controlPointFactor;
125 if (numberOfControlPoints < 10)
126 numberOfControlPoints = std::min(numberOfInputPoints,10);
128 int numberOfOutputPoints = std::max(numberOfInputPoints, 100);
135 if (numberOfControlPoints >= 2)
138 for(
int i=0; i<numberOfControlPoints; i++)
140 int indexP = (i*numberOfInputPoints)/numberOfControlPoints;
142 centerline->GetPoint(indexP,p);
143 int indexSpline = (i*numberOfOutputPoints)/numberOfControlPoints;
144 splineX->AddPoint( indexSpline, p[0] );
145 splineY->AddPoint( indexSpline, p[1] );
146 splineZ->AddPoint( indexSpline, p[2] );
152 centerline->GetPoint( numberOfInputPoints-1, p );
153 splineX->AddPoint( numberOfOutputPoints-1, p[0] );
154 splineY->AddPoint( numberOfOutputPoints-1, p[1] );
155 splineZ->AddPoint( numberOfOutputPoints-1, p[2] );
159 for(
int i=0; i<numberOfOutputPoints; i++)
161 double splineParameter = i;
162 newCenterline->InsertNextPoint(splineX->Evaluate(splineParameter),
163 splineY->Evaluate(splineParameter),
164 splineZ->Evaluate(splineParameter));
170 return newCenterline;
177 int N = centerline->GetNumberOfPoints();
178 Eigen::MatrixXd CLpoints(3,N);
179 for(vtkIdType i = 0; i < N; i++)
182 centerline->GetPoint(i,p);
184 pos(0) = p[0]; pos(1) = p[1]; pos(2) = p[2];
185 pos = rMd.coord(pos);
186 processedPositions->InsertNextPoint(pos[0],pos[1],pos[2]);
192 return processedPositions;
199 for(TimedTransformMap::iterator iter=trackingData_prMt.begin();
200 iter!=trackingData_prMt.end();++iter)
203 Vector3D pos = prMt.matrix().block<3,1> (0, 3);
204 pos = rMpr.coord(pos);
205 loggedPositions->InsertNextPoint(pos[0],pos[1],pos[2]);
208 return loggedPositions;
213 mFixedPointSet->Initialize();
218 for(vtkIdType n=0;n<vtkPoints->GetNumberOfPoints();n++)
220 double temp_Point[3];
221 vtkPoints->GetPoint(n, temp_Point);
222 point[0] = temp_Point[0];
223 point[1] = temp_Point[1];
224 point[2] = temp_Point[2];
225 Vector3D vPoint(temp_Point[0], temp_Point[1], temp_Point[2]);
226 itkPoints->InsertElement(n,point);
228 mFixedPointSet->SetPoints(itkPoints);
233 mMovingPointSet->Initialize();
237 for(vtkIdType n=0;n<vtkPoints->GetNumberOfPoints();n++)
239 double temp_Point[3];
240 vtkPoints->GetPoint(n, temp_Point);
241 Vector3D vPoint(temp_Point[0], temp_Point[1], temp_Point[2]);
242 point[0] = temp_Point[0];
243 point[1] = temp_Point[1];
244 point[2] = temp_Point[2];
245 itkPoints->InsertElement(n,point);
247 mMovingPointSet->SetPoints(itkPoints);
252 mRegistration->SetFixedPointSet(mFixedPointSet);
253 mRegistration->SetMovingPointSet(mMovingPointSet);
255 typedef itk::Matrix<double,3,3> MatrixType;
256 typedef TransformType::OffsetType TranslateVector;
257 MatrixType initMatrix;
258 TranslateVector initTranslation;
261 for(
int i=0; i<3; i++)
262 for(
int j=0;j<3;j++) {
263 double element = init_transform(i,j);
264 initMatrix(i,j) = element;
268 initTranslation[0] = init_transform(0,3);
269 initTranslation[1] = init_transform(1,3);
270 initTranslation[2] = init_transform(2,3);
272 mTransform->SetMatrix(initMatrix, 1e-5);
273 mTransform->SetTranslation(initTranslation);
275 mRegistration->SetInitialTransformParameters(mTransform->GetParameters());
279 mRegistration->Update();
281 catch (itk::ExceptionObject &exp)
283 std::cout <<
"CenterlineRegMethod - Exception caught ! " << std::endl;
284 std::cout << exp << std::endl;
287 RegistrationType::ParametersType finalParameters =
288 mRegistration->GetLastTransformParameters();
290 TransformType::MatrixType regMatrix = mTransform->GetMatrix();
292 for(
int i=0; i<3; i++)
293 for(
int j=0;j<3;j++) {
294 double element = regMatrix(i,j);
295 mResultTransform(i,j) = element;
298 mResultTransform(0,3) = finalParameters(3);
299 mResultTransform(1,3) = finalParameters(4);
300 mResultTransform(2,3) = finalParameters(5);
302 mRegistrationUpdated =
true;
318 std::cout <<
"rMpr : " << std::endl;
319 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.