From: Simon Rit Date: Mon, 27 Jun 2011 16:53:16 +0000 (+0200) Subject: transform_grid option was not working for flips X-Git-Tag: v1.3.0~303 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=47a8840921750ace679254bcbac524e7e71c6993;p=clitk.git transform_grid option was not working for flips --- 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;