#include <fstream>
{
public:
using Self = CommandProgressUpdate;
itkNewMacro( Self );
protected:
CommandProgressUpdate() = default;
public:
{
}
{
if( ! itk::ProgressEvent().CheckEvent( &event ) )
{
return;
}
}
};
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] [transformFile]" << std::endl;
return EXIT_FAILURE;
}
constexpr unsigned int ImageDimension = 2;
using PixelType = unsigned char;
FixedReaderType::Pointer fixedReader = FixedReaderType::New();
fixedReader->SetFileName( argv[2] );
try
{
fixedReader->Update();
}
{
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();
FixedImageType >;
FilterType::Pointer resampler = FilterType::New();
MovingImageType, double >;
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;
CoordinateRepType,
SpaceDimension,
SplineOrder >;
TransformType::Pointer bsplineTransform = TransformType::New();
constexpr unsigned int numberOfGridNodes = 7;
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.close();
bsplineTransform->SetParameters( parameters );
CommandProgressUpdate::Pointer observer = CommandProgressUpdate::New();
resampler->AddObserver( itk::ProgressEvent(), observer );
resampler->SetTransform( bsplineTransform );
try
{
movingWriter->Update();
}
{
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();
}
{
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();
}
{
std::cerr << "Exception thrown " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}