+ //Filter
+ typedef itk::ResampleImageFilter< InputImageType,OutputImageType > ResampleFilterType;
+ typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
+
+ // Matrix
+ typename itk::Matrix<double, Dimension+1, Dimension+1> matrix;
+ 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::endl;
+ return;
+ }
+ itk::Array<double> 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<double, 4, 4> 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<Dimension>(transformParameters);
+ }
+ else
+ {
+ if (m_ArgsInfo.matrix_given)
+ {
+ matrix= clitk::ReadMatrix<Dimension>(m_ArgsInfo.matrix_arg);
+ if (m_Verbose) std::cout << "Reading the matrix..." << std::endl;
+ }
+ else {
+ if (m_ArgsInfo.elastix_given) {
+ std::string filename(m_ArgsInfo.elastix_arg);
+ matrix = createMatrixFromElastixFile<Dimension>(filename, m_Verbose);
+ }
+ else
+ matrix.SetIdentity();
+ }
+ }
+ if (m_Verbose)
+ std::cout << "Using the following matrix:" << std::endl
+ << matrix << std::endl;
+ typename itk::Matrix<double, Dimension, Dimension> rotationMatrix = clitk::GetRotationalPartMatrix(matrix);
+ typename itk::Vector<double, Dimension> translationPart = clitk::GetTranslationPartMatrix(matrix);
+
+ // Transform
+ typedef itk::AffineTransform<double, Dimension> AffineTransformType;
+ typename AffineTransformType::Pointer affineTransform=AffineTransformType::New();
+ affineTransform->SetMatrix(rotationMatrix);
+ affineTransform->SetTranslation(translationPart);
+
+ // Interp
+ typedef clitk::GenericInterpolator<args_info_type, InputImageType, double> GenericInterpolatorType;
+ typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
+ genericInterpolator->SetArgsInfo(m_ArgsInfo);
+
+ // Properties
+ if (m_ArgsInfo.like_given) {
+ typename InputReaderType::Pointer likeReader=InputReaderType::New();
+ likeReader->SetFileName(m_ArgsInfo.like_arg);
+ likeReader->Update();
+ resampler->SetOutputParametersFromImage(likeReader->GetOutput());
+ resampler->SetOutputDirection(likeReader->GetOutput()->GetDirection());
+ if (autoGaussEnabled) { // Automated sigma when downsample
+ for(unsigned int i=0; i<Dimension; i++) {
+ if (likeReader->GetOutput()->GetSpacing()[i] > input->GetSpacing()[i]) { // downsample
+ gaussianSigma[i] = 0.5*likeReader->GetOutput()->GetSpacing()[i];// / inputSpacing[i]);
+ }
+ else gaussianSigma[i] = 0; // will be ignore after
+ }
+ }
+ } else if(m_ArgsInfo.transform_grid_flag) {
+ typename itk::Matrix<double, Dimension+1, Dimension+1> invMatrix( matrix.GetInverse() );
+ typename itk::Matrix<double, Dimension, Dimension> invRotMatrix( clitk::GetRotationalPartMatrix(invMatrix) );
+ typename itk::Vector<double,Dimension> invTrans = clitk::GetTranslationPartMatrix(invMatrix);
+
+ // Display warning
+ if (m_ArgsInfo.spacing_given)
+ std::cout << "Warning --spacing ignored (because --transform_grid_flag)" << std::endl;
+ if (m_ArgsInfo.origin_given)
+ std::cout << "Warning --origin ignored (because --transform_grid_flag)" << std::endl;
+
+ // 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.
+ // Determine the bounding box tranforming all corners
+ vnl_vector<double> vnlOutputSize(Dimension), vnlOutputmmSize(Dimension), vnlOutputOffset(Dimension);
+ typename InputImageType::SpacingType outputSpacing;
+ for(unsigned int i=0; i< Dimension; i++) {
+ vnlOutputSize[i] = input->GetLargestPossibleRegion().GetSize()[i];
+ vnlOutputmmSize[i] = input->GetLargestPossibleRegion().GetSize()[i]*input->GetSpacing()[i];
+ vnlOutputOffset[i] = input->GetLargestPossibleRegion().GetSize()[i]*input->GetSpacing()[i];
+ }
+ vnlOutputSize = ComputeSize(vnlOutputSize, invRotMatrix.GetVnlMatrix() * input->GetDirection().GetVnlMatrix(), 0);
+ vnlOutputmmSize = ComputeSize(vnlOutputmmSize, invRotMatrix.GetVnlMatrix() * input->GetDirection().GetVnlMatrix(), 0);
+ vnlOutputOffset = ComputeSize(vnlOutputOffset, invRotMatrix.GetVnlMatrix() * input->GetDirection().GetVnlMatrix(), 1);
+ for(unsigned int i=0; i< Dimension; i++) {
+ outputSpacing[i] = vnlOutputmmSize[i]/lrint(vnlOutputSize[i]);
+ outputOrigin[i] += vnlOutputOffset[i];
+ }
+ if (autoGaussEnabled) { // Automated sigma when downsample
+ for(unsigned int i=0; i<Dimension; i++) {
+ if (outputSpacing[i] > input->GetSpacing()[i]) { // downsample
+ gaussianSigma[i] = 0.5*outputSpacing[i];// / inputSpacing[i]);
+ }
+ else gaussianSigma[i] = 0; // will be ignore after
+ }
+ }