From 47a8840921750ace679254bcbac524e7e71c6993 Mon Sep 17 00:00:00 2001 From: Simon Rit Date: Mon, 27 Jun 2011 18:53:16 +0200 Subject: [PATCH] transform_grid option was not working for flips --- tools/clitkAffineTransformGenericFilter.txx | 33 ++++++++++++++------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/tools/clitkAffineTransformGenericFilter.txx b/tools/clitkAffineTransformGenericFilter.txx index c758cb2..ef81fe2 100644 --- a/tools/clitkAffineTransformGenericFilter.txx +++ b/tools/clitkAffineTransformGenericFilter.txx @@ -168,7 +168,20 @@ AffineTransformGenericFilter::UpdateWithDimAndPixelType() typename itk::Matrix invRotMatrix( clitk::GetRotationalPartMatrix(invMatrix) ); typename itk::Vector invTrans = clitk::GetTranslationPartMatrix(invMatrix); - // Size is converted to double, transformed and converted back to size type + // Spacing is influenced by affine transform matrix and input direction + typename InputImageType::SpacingType outputSpacing; + outputSpacing = invRotMatrix * + input->GetDirection() * + input->GetSpacing(); + + // Origin is influenced by translation but not by input direction + typename InputImageType::PointType outputOrigin; + outputOrigin = invRotMatrix * + input->GetOrigin() + + invTrans; + + // Size is influenced by affine transform matrix and input direction + // Size is converted to double, transformed and converted back to size type. vnl_vector vnlOutputSize(Dimension); for(unsigned int i=0; i< Dimension; i++) { vnlOutputSize[i] = input->GetLargestPossibleRegion().GetSize()[i]; @@ -178,18 +191,18 @@ AffineTransformGenericFilter::UpdateWithDimAndPixelType() vnlOutputSize; typename OutputImageType::SizeType outputSize; for(unsigned int i=0; i< Dimension; i++) { + // If the size is negative, we have a flip and we must modify + // the origin and the spacing accordingly. + if(vnlOutputSize[i]<0.) { + vnlOutputSize[i] *= -1.; + outputOrigin[i] = outputOrigin[i] + outputSpacing[i] * (vnlOutputSize[i]-1); + outputSpacing[i] *= -1.; + } outputSize[i] = lrint(vnlOutputSize[i]); } resampler->SetSize( outputSize ); - - // Spacing can be dictly computed in the same way - resampler->SetOutputSpacing ( invRotMatrix * - input->GetDirection() * - input->GetSpacing() ); - // Origin is influenced by translation but not by direction - resampler->SetOutputOrigin ( invRotMatrix * - input->GetOrigin() + - invTrans ); + resampler->SetOutputSpacing( outputSpacing ); + resampler->SetOutputOrigin( outputOrigin ); } else { //Size typename OutputImageType::SizeType outputSize; -- 2.47.1