X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;ds=sidebyside;f=tools%2FclitkAffineTransformGenericFilter.txx;h=93cd16c91c2928455b4af569a450a985ecaa0a49;hb=bcd98a7cb45fd4c3b5a41bc8cefa89f5790aca27;hp=5a8cfa964e144132560fbc080da28ae903c67937;hpb=765020625fbc092d283e221e36c83e60a1844cb7;p=clitk.git diff --git a/tools/clitkAffineTransformGenericFilter.txx b/tools/clitkAffineTransformGenericFilter.txx index 5a8cfa9..93cd16c 100644 --- a/tools/clitkAffineTransformGenericFilter.txx +++ b/tools/clitkAffineTransformGenericFilter.txx @@ -135,16 +135,59 @@ AffineTransformGenericFilter::UpdateWithDimAndPixelType() // Matrix typename itk::Matrix matrix; - if (m_ArgsInfo.matrix_given) { - matrix= clitk::ReadMatrix(m_ArgsInfo.matrix_arg); - if (m_Verbose) std::cout<<"Reading the matrix..."< transformParameters(2 * Dimension); + transformParameters.Fill(0.0); + if (m_ArgsInfo.rotate_given) + { + if (Dimension == 2) + transformParameters[0] = m_ArgsInfo.rotate_arg[0]; + else + for (unsigned int i = 0; i < 3; i++) + transformParameters[i] = m_ArgsInfo.rotate_arg[i]; + } + if (m_ArgsInfo.translate_given) + { + int pos = 3; + if (Dimension == 2) + pos = 1; + for (unsigned int i = 0; i < Dimension && i < 3; i++) + transformParameters[pos++] = m_ArgsInfo.translate_arg[i]; + } + if (Dimension == 4) + { + matrix.SetIdentity(); + itk::Matrix tmp = GetForwardAffineMatrix3D(transformParameters); + for (unsigned int i = 0; i < 3; ++i) + for (unsigned int j = 0; j < 3; ++j) + matrix[i][j] = tmp[i][j]; + for (unsigned int i = 0; i < 3; ++i) + matrix[i][4] = tmp[i][3]; + } + else + matrix = GetForwardAffineMatrix(transformParameters); } - if (m_Verbose) std::cout<<"Using the following matrix:"< rotationMatrix=clitk::GetRotationalPartMatrix(matrix); - typename itk::Vector translationPart= clitk::GetTranslationPartMatrix(matrix); + else + { + if (m_ArgsInfo.matrix_given) + { + matrix= clitk::ReadMatrix(m_ArgsInfo.matrix_arg); + if (m_Verbose) std::cout << "Reading the matrix..." << std::endl; + } + else + matrix.SetIdentity(); + } + if (m_Verbose) + std::cout << "Using the following matrix:" << std::endl + << matrix << std::endl; + typename itk::Matrix rotationMatrix = clitk::GetRotationalPartMatrix(matrix); + typename itk::Vector translationPart = clitk::GetTranslationPartMatrix(matrix); // Transform typedef itk::AffineTransform AffineTransformType; @@ -163,6 +206,46 @@ AffineTransformGenericFilter::UpdateWithDimAndPixelType() likeReader->SetFileName(m_ArgsInfo.like_arg); likeReader->Update(); resampler->SetOutputParametersFromImage(likeReader->GetOutput()); + } else if(m_ArgsInfo.transform_grid_flag) { + typename itk::Matrix invMatrix( matrix.GetInverse() ); + typename itk::Matrix invRotMatrix( clitk::GetRotationalPartMatrix(invMatrix) ); + typename itk::Vector invTrans = clitk::GetTranslationPartMatrix(invMatrix); + + // 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]; + } + vnlOutputSize = invRotMatrix * + input->GetDirection().GetVnlMatrix() * + 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 ); + resampler->SetOutputSpacing( outputSpacing ); + resampler->SetOutputOrigin( outputOrigin ); } else { //Size typename OutputImageType::SizeType outputSize; @@ -170,7 +253,6 @@ AffineTransformGenericFilter::UpdateWithDimAndPixelType() for(unsigned int i=0; i< Dimension; i++) outputSize[i]=m_ArgsInfo.size_arg[i]; } else outputSize=input->GetLargestPossibleRegion().GetSize(); - std::cout<<"Setting the size to "<::UpdateWithDimAndPixelType() for(unsigned int i=0; i< Dimension; i++) outputSpacing[i]=m_ArgsInfo.spacing_arg[i]; } else outputSpacing=input->GetSpacing(); - std::cout<<"Setting the spacing to "<::UpdateWithDimAndPixelType() for(unsigned int i=0; i< Dimension; i++) outputOrigin[i]=m_ArgsInfo.origin_arg[i]; } else outputOrigin=input->GetOrigin(); - std::cout<<"Setting the origin to "<SetSize( outputSize ); @@ -195,6 +275,12 @@ AffineTransformGenericFilter::UpdateWithDimAndPixelType() } + if (m_ArgsInfo.verbose_flag) { + std::cout << "Setting the output size to " << resampler->GetSize() << "..." << std::endl; + std::cout << "Setting the output spacing to " << resampler->GetOutputSpacing() << "..." << std::endl; + std::cout << "Setting the output origin to " << resampler->GetOutputOrigin() << "..." << std::endl; + } + resampler->SetInput( input ); resampler->SetTransform( affineTransform ); resampler->SetInterpolator( genericInterpolator->GetInterpolatorPointer()); @@ -241,14 +327,59 @@ void AffineTransformGenericFilter::UpdateWithDimAndVectorType() // Matrix typename itk::Matrix matrix; - if (m_ArgsInfo.matrix_given) - matrix= clitk::ReadMatrix(m_ArgsInfo.matrix_arg); + if (m_ArgsInfo.rotate_given || m_ArgsInfo.translate_given) + { + if (m_ArgsInfo.matrix_given) + { + std::cerr << "You must use either rotate/translate or matrix options" << std::cout; + return; + } + itk::Array transformParameters(2 * Dimension); + transformParameters.Fill(0.0); + if (m_ArgsInfo.rotate_given) + { + if (Dimension == 2) + transformParameters[0] = m_ArgsInfo.rotate_arg[0]; + else + for (unsigned int i = 0; i < 3; i++) + transformParameters[i] = m_ArgsInfo.rotate_arg[i]; + } + if (m_ArgsInfo.translate_given) + { + int pos = 3; + if (Dimension == 2) + pos = 1; + for (unsigned int i = 0; i < Dimension && i < 3; i++) + transformParameters[pos++] = m_ArgsInfo.translate_arg[i]; + } + if (Dimension == 4) + { + matrix.SetIdentity(); + itk::Matrix tmp = GetForwardAffineMatrix3D(transformParameters); + for (unsigned int i = 0; i < 3; ++i) + for (unsigned int j = 0; j < 3; ++j) + matrix[i][j] = tmp[i][j]; + for (unsigned int i = 0; i < 3; ++i) + matrix[i][4] = tmp[i][3]; + } + else + matrix = GetForwardAffineMatrix(transformParameters); + } else - matrix.SetIdentity(); - if (m_Verbose) std::cout<<"Using the following matrix:"< rotationMatrix=clitk::GetRotationalPartMatrix(matrix); - typename itk::Vector translationPart= clitk::GetTranslationPartMatrix(matrix); + { + if (m_ArgsInfo.matrix_given) + { + matrix= clitk::ReadMatrix(m_ArgsInfo.matrix_arg); + if (m_Verbose) std::cout << "Reading the matrix..." << std::endl; + } + else + matrix.SetIdentity(); + } + if (m_Verbose) + std::cout << "Using the following matrix:" << std::endl + << matrix << std::endl; + typename itk::Matrix rotationMatrix = clitk::GetRotationalPartMatrix(matrix); + typename itk::Vector translationPart = clitk::GetTranslationPartMatrix(matrix); // Transform typedef itk::AffineTransform AffineTransformType;