#include <fstream>
{
public:
typedef CommandProgressUpdate
Self;
itkNewMacro( Self );
protected:
CommandProgressUpdate() {};
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;
}
const unsigned int ImageDimension = 2;
typedef unsigned char PixelType;
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;
FilterType::Pointer resampler = FilterType::New();
MovingImageType, double > InterpolatorType;
InterpolatorType::Pointer interpolator = InterpolatorType::New();
resampler->SetInterpolator( interpolator );
FixedImageType::SpacingType fixedSpacing = fixedImage->GetSpacing();
FixedImageType::PointType fixedOrigin = fixedImage->GetOrigin();
FixedImageType::DirectionType fixedDirection = fixedImage->GetDirection();
resampler->SetOutputSpacing( fixedSpacing );
resampler->SetOutputOrigin( fixedOrigin );
resampler->SetOutputDirection( fixedDirection );
FixedImageType::RegionType fixedRegion = fixedImage->GetBufferedRegion();
FixedImageType::SizeType fixedSize = fixedRegion.GetSize();
resampler->SetSize( fixedSize );
resampler->SetOutputStartIndex( fixedRegion.GetIndex() );
resampler->SetInput( movingReader->GetOutput() );
movingWriter->SetInput( resampler->GetOutput() );
const unsigned int SpaceDimension = ImageDimension;
const unsigned int SplineOrder = 3;
typedef double CoordinateRepType;
CoordinateRepType,
SpaceDimension,
SplineOrder > TransformType;
TransformType::Pointer bsplineTransform = TransformType::New();
const 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 );
typedef TransformType::ParametersType 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;
DisplacementFieldType::IndexType index;
VectorType displacement;
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;
}