#include <fstream>
template <typename TFilter>
class itkJensenHavrdaCharvatTsallisPointSetMetricRegistrationTestCommandIterationUpdate :
public itk::Command
{
public:
using Self = itkJensenHavrdaCharvatTsallisPointSetMetricRegistrationTestCommandIterationUpdate;
protected:
itkJensenHavrdaCharvatTsallisPointSetMetricRegistrationTestCommandIterationUpdate() = default;
public:
void
{
}
void
{
if (typeid(event) != typeid(itk::IterationEvent))
{
return;
}
const auto * optimizer = dynamic_cast<const TFilter *>(object);
if (!optimizer)
{
itkGenericExceptionMacro("Error dynamic_cast failed");
}
std::cout << "It: " << optimizer->GetCurrentIteration() << " metric value: " << optimizer->GetCurrentMetricValue();
std::cout << std::endl;
}
};
int
main(int argc, char * argv[])
{
unsigned int numberOfIterations = 10;
if (argc > 1)
{
numberOfIterations = std::stoi(argv[1]);
}
fixedPoints->Initialize();
movingPoints->Initialize();
{
offset[d] = 2.0;
}
unsigned long count = 0;
{
float radius = 100.0;
fixedPoint[0] = radius * std::cos(theta);
fixedPoint[1] = radius * std::sin(theta);
{
fixedPoint[2] = radius * std::sin(theta);
}
fixedPoints->SetPoint(count, fixedPoint);
movingPoint[0] = fixedPoint[0] + offset[0];
movingPoint[1] = fixedPoint[1] + offset[1];
{
movingPoint[2] = fixedPoint[2] + offset[2];
}
movingPoints->SetPoint(count, movingPoint);
count++;
}
transform->SetIdentity();
metric->SetFixedPointSet(fixedPoints);
metric->SetMovingPointSet(movingPoints);
metric->SetPointSetSigma(1.0);
metric->SetKernelSigma(10.0);
metric->SetUseAnisotropicCovariances(false);
metric->SetCovarianceKNeighborhood(5);
metric->SetEvaluationKNeighborhood(10);
metric->SetMovingTransform(transform);
metric->SetAlpha(1.1);
metric->Initialize();
using RegistrationParameterScalesFromShiftType =
shiftScaleEstimator->SetMetric(metric);
shiftScaleEstimator->SetVirtualDomainPointSet(metric->GetVirtualTransformedPointSet());
optimizer->SetMetric(metric);
optimizer->SetNumberOfIterations(numberOfIterations);
optimizer->SetScalesEstimator(shiftScaleEstimator);
optimizer->SetMaximumStepSizeInPhysicalUnits(3.0);
using CommandType = itkJensenHavrdaCharvatTsallisPointSetMetricRegistrationTestCommandIterationUpdate<OptimizerType>;
optimizer->AddObserver(itk::IterationEvent(), observer);
optimizer->SetMinimumConvergenceValue(0.0);
optimizer->SetConvergenceWindowSize(10);
optimizer->StartOptimization();
std::cout << "numberOfIterations: " << numberOfIterations << std::endl;
std::cout << "Moving-source final value: " << optimizer->GetCurrentMetricValue() << std::endl;
std::cout << "Moving-source final position: " << optimizer->GetCurrentPosition() << std::endl;
std::cout << "Optimizer scales: " << optimizer->GetScales() << std::endl;
std::cout << "Optimizer learning rate: " << optimizer->GetLearningRate() << std::endl;
std::cout << "Fixed\tMoving\tMovingTransformed\tFixedTransformed\tDiff" << std::endl;
bool passed = true;
PointType::ValueType tolerance = 1
e-2;
AffineTransformType::InverseTransformBasePointer movingInverse = metric->GetMovingTransform()->GetInverseTransform();
AffineTransformType::InverseTransformBasePointer fixedInverse = metric->GetFixedTransform()->GetInverseTransform();
for (unsigned int n = 0; n < metric->GetNumberOfComponents(); ++n)
{
PointType transformedMovingPoint = movingInverse->TransformPoint(movingPoints->GetPoint(n));
PointType transformedFixedPoint = fixedInverse->TransformPoint(fixedPoints->GetPoint(n));
difference[0] = transformedMovingPoint[0] - transformedFixedPoint[0];
difference[1] = transformedMovingPoint[1] - transformedFixedPoint[1];
std::cout << fixedPoints->GetPoint(n) << "\t" << movingPoints->GetPoint(n) << "\t" << transformedMovingPoint << "\t"
<< transformedFixedPoint << "\t" << difference << std::endl;
if (fabs(difference[0]) > tolerance || fabs(difference[1]) > tolerance)
{
passed = false;
}
}
if (!passed)
{
std::cerr << "Results do not match truth within tolerance." << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}