#include <iostream>
#include <fstream>
int
main(int argc, char * argv[])
{
if (argc < 3)
{
std::cerr << "Arguments Missing. " << std::endl;
std::cerr
<< "Usage: IterativeClosestPoint3 fixedPointsFile movingPointsFile "
<< std::endl;
return EXIT_FAILURE;
}
using PointsContainer = PointSetType::PointsContainer;
std::ifstream fixedFile;
fixedFile.open(argv[1]);
if (fixedFile.fail())
{
std::cerr << "Error opening points file with name : " << std::endl;
std::cerr << argv[1] << std::endl;
return EXIT_FAILURE;
}
unsigned int pointId = 0;
fixedFile >> fixedPoint;
while (!fixedFile.eof())
{
fixedPointContainer->InsertElement(pointId, fixedPoint);
fixedFile >> fixedPoint;
pointId++;
}
fixedPointSet->SetPoints(fixedPointContainer);
std::cout << "Number of fixed Points = "
<< fixedPointSet->GetNumberOfPoints() << std::endl;
std::ifstream movingFile;
movingFile.open(argv[2]);
if (movingFile.fail())
{
std::cerr << "Error opening points file with name : " << std::endl;
std::cerr << argv[2] << std::endl;
return EXIT_FAILURE;
}
pointId = 0;
movingFile >> movingPoint;
while (!movingFile.eof())
{
movingPointContainer->InsertElement(pointId, movingPoint);
movingFile >> movingPoint;
pointId++;
}
movingPointSet->SetPoints(movingPointContainer);
std::cout << "Number of moving Points = "
<< movingPointSet->GetNumberOfPoints() << std::endl;
using MetricType =
optimizer->SetUseCostFunctionGradient(false);
using RegistrationType =
OptimizerType::ScalesType scales(transform->GetNumberOfParameters());
scales.Fill(0.01);
constexpr unsigned long numberOfIterations = 100;
const double gradientTolerance = 1
e-5;
const double valueTolerance = 1
e-5;
const double epsilonFunction = 1
e-6;
optimizer->SetScales(scales);
optimizer->SetNumberOfIterations(numberOfIterations);
optimizer->SetValueTolerance(valueTolerance);
optimizer->SetGradientTolerance(gradientTolerance);
optimizer->SetEpsilonFunction(epsilonFunction);
transform->SetIdentity();
registration->SetInitialTransformParameters(transform->GetParameters());
registration->SetMetric(metric);
registration->SetOptimizer(optimizer);
registration->SetTransform(transform);
registration->SetFixedPointSet(fixedPointSet);
registration->SetMovingPointSet(movingPointSet);
using PointsToImageFilterType =
pointsToImageFilter->SetInput(fixedPointSet);
BinaryImageType::SpacingType spacing;
spacing.Fill(1.0);
pointsToImageFilter->SetSpacing(spacing);
pointsToImageFilter->SetOrigin(origin);
pointsToImageFilter->Update();
using DistanceFilterType =
distanceFilter->SetInput(binaryImage);
distanceFilter->Update();
metric->SetDistanceMap(distanceFilter->GetOutput());
try
{
registration->Update();
}
catch (
const itk::ExceptionObject &
e)
{
std::cerr <<
e << std::endl;
return EXIT_FAILURE;
}
std::cout << "Solution = " << transform->GetParameters() << std::endl;
return EXIT_SUCCESS;
}