#include <iostream>
#include <fstream>
{
public:
using Self = CommandIterationUpdate;
protected:
CommandIterationUpdate() = default;
public:
using OptimizerPointer = const OptimizerType *;
void
{
}
void
{
auto optimizer = dynamic_cast<OptimizerPointer>(object);
if (optimizer == nullptr)
{
itkExceptionMacro("Could not cast optimizer.");
}
if (!itk::IterationEvent().CheckEvent(&event))
{
return;
}
std::cout << "Value = " << optimizer->GetCachedValue() << std::endl;
std::cout << "Position = " << optimizer->GetCachedCurrentPosition();
std::cout << std::endl << std::endl;
}
};
int
main(int argc, char * argv[])
{
if (argc < 3)
{
std::cerr << "Arguments Missing. " << std::endl;
std::cerr
<< "Usage: IterativeClosestPoint2 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());
constexpr double translationScale = 1000.0;
constexpr double rotationScale = 1.0;
scales[0] = 1.0 / rotationScale;
scales[1] = 1.0 / rotationScale;
scales[2] = 1.0 / rotationScale;
scales[3] = 1.0 / translationScale;
scales[4] = 1.0 / translationScale;
scales[5] = 1.0 / translationScale;
unsigned long numberOfIterations = 2000;
double gradientTolerance = 1
e-4;
double valueTolerance = 1
e-4;
double epsilonFunction = 1
e-5;
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);
optimizer->AddObserver(itk::IterationEvent(), observer);
try
{
registration->Update();
}
catch (
const itk::ExceptionObject &
e)
{
std::cerr <<
e << std::endl;
return EXIT_FAILURE;
}
std::cout << "Solution = " << transform->GetParameters() << std::endl;
std::cout << "Stopping condition: "
<< optimizer->GetStopConditionDescription() << std::endl;
return EXIT_SUCCESS;
}