#include <fstream>
{
public:
using Self = CommandProgressUpdate;
itkNewMacro(Self);
protected:
CommandProgressUpdate() = default;
public:
void
{
}
void
{
const auto * filter = static_cast<const itk::ProcessObject *>(object);
if (!itk::ProgressEvent().CheckEvent(&event))
{
return;
}
std::cout << filter->GetProgress() << std::endl;
}
};
int
main(int argc, char * argv[])
{
if (argc < 5)
{
std::cerr << "Missing Parameters " << std::endl;
std::cerr << "Usage: " << argv[0];
std::cerr << " coefficientsFile fixedImage ";
std::cerr << "movingImage deformedMovingImage" << std::endl;
std::cerr << "[deformationField]" << std::endl;
return EXIT_FAILURE;
}
constexpr unsigned int ImageDimension = 3;
using PixelType = unsigned char;
FixedReaderType::Pointer fixedReader = FixedReaderType::New();
fixedReader->SetFileName(argv[2]);
try
{
fixedReader->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Exception thrown " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
MovingReaderType::Pointer movingReader = MovingReaderType::New();
MovingWriterType::Pointer movingWriter = MovingWriterType::New();
movingReader->SetFileName(argv[3]);
movingWriter->SetFileName(argv[4]);
FixedImageType::ConstPointer fixedImage = fixedReader->GetOutput();
using FilterType =
FilterType::Pointer resampler = FilterType::New();
using InterpolatorType =
InterpolatorType::Pointer interpolator = InterpolatorType::New();
resampler->SetInterpolator(interpolator);
FixedImageType::SpacingType fixedSpacing = fixedImage->GetSpacing();
resampler->SetOutputSpacing(fixedSpacing);
resampler->SetOutputOrigin(fixedOrigin);
resampler->SetOutputDirection(fixedDirection);
resampler->SetSize(fixedSize);
resampler->SetOutputStartIndex(fixedRegion.GetIndex());
resampler->SetInput(movingReader->GetOutput());
movingWriter->SetInput(resampler->GetOutput());
const unsigned int SpaceDimension = ImageDimension;
constexpr unsigned int SplineOrder = 3;
using CoordinateRepType = double;
using TransformType =
TransformType::Pointer bsplineTransform = TransformType::New();
constexpr unsigned int numberOfGridNodes = 8;
TransformType::PhysicalDimensionsType fixedPhysicalDimensions;
TransformType::MeshSizeType meshSize;
for (unsigned int i = 0; i < SpaceDimension; i++)
{
fixedPhysicalDimensions[i] =
fixedSpacing[i] * static_cast<double>(fixedSize[i] - 1);
}
meshSize.Fill(numberOfGridNodes - SplineOrder);
bsplineTransform->SetTransformDomainOrigin(fixedOrigin);
bsplineTransform->SetTransformDomainPhysicalDimensions(
fixedPhysicalDimensions);
bsplineTransform->SetTransformDomainMeshSize(meshSize);
bsplineTransform->SetTransformDomainDirection(fixedDirection);
using ParametersType = TransformType::ParametersType;
const unsigned int numberOfParameters =
bsplineTransform->GetNumberOfParameters();
const unsigned int numberOfNodes = numberOfParameters / SpaceDimension;
ParametersType parameters(numberOfParameters);
std::ifstream infile;
infile.open(argv[1]);
for (unsigned int n = 0; n < numberOfNodes; n++)
{
infile >> parameters[n];
infile >> parameters[n + numberOfNodes];
infile >> parameters[n + numberOfNodes * 2];
}
infile.close();
bsplineTransform->SetParameters(parameters);
CommandProgressUpdate::Pointer observer = CommandProgressUpdate::New();
resampler->AddObserver(itk::ProgressEvent(), observer);
resampler->SetTransform(bsplineTransform);
try
{
movingWriter->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Exception thrown " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
DisplacementFieldType::Pointer field = DisplacementFieldType::New();
field->SetRegions(fixedRegion);
field->SetOrigin(fixedOrigin);
field->SetSpacing(fixedSpacing);
field->SetDirection(fixedDirection);
field->Allocate();
FieldIterator fi(field, fixedRegion);
fi.GoToBegin();
TransformType::InputPointType fixedPoint;
TransformType::OutputPointType movingPoint;
while (!fi.IsAtEnd())
{
index = fi.GetIndex();
field->TransformIndexToPhysicalPoint(index, fixedPoint);
movingPoint = bsplineTransform->TransformPoint(fixedPoint);
displacement = movingPoint - fixedPoint;
fi.Set(displacement);
++fi;
}
FieldWriterType::Pointer fieldWriter = FieldWriterType::New();
fieldWriter->SetInput(field);
if (argc >= 6)
{
fieldWriter->SetFileName(argv[5]);
try
{
fieldWriter->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Exception thrown " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
}
if (argc >= 7)
{
fieldWriter->SetFileName(argv[6]);
try
{
TransformWriterType::Pointer transformWriter =
TransformWriterType::New();
transformWriter->AddTransform(bsplineTransform);
transformWriter->SetFileName(argv[6]);
transformWriter->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Exception thrown " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}