]> Creatis software - clitk.git/blobdiff - tools/clitkAffineTransformGenericFilter.txx
Add Direction options in clitkAffineTransform
[clitk.git] / tools / clitkAffineTransformGenericFilter.txx
index 621dfd0eae1b1796bf8decefc988ca282650fd07..15baf2d7a41c2b99068bc52b331634545cd993c4 100644 (file)
@@ -22,6 +22,7 @@
 #include <istream>
 #include <iterator>
 #include <itkCenteredEuler3DTransform.h>
+#include "clitkElastix.h"
 
 namespace clitk
 {
@@ -52,12 +53,12 @@ namespace clitk
     // Call UpdateWithDim
     if(Dimension==2) UpdateWithDim<2>(PixelType, Components);
     else 
-    if(Dimension==3) UpdateWithDim<3>(PixelType, Components);
-    else if (Dimension==4)UpdateWithDim<4>(PixelType, Components);
-    else {
-      std::cout<<"Error, Only for 2, 3 or 4  Dimensions!!!"<<std::endl ;
-      return;
-    }
+      if(Dimension==3) UpdateWithDim<3>(PixelType, Components);
+      else if (Dimension==4)UpdateWithDim<4>(PixelType, Components);
+      else {
+        std::cout<<"Error, Only for 2, 3 or 4  Dimensions!!!"<<std::endl ;
+        return;
+      }
   }
   //-------------------------------------------------------------------
  
@@ -77,10 +78,10 @@ namespace clitk
         if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
         UpdateWithDimAndPixelType<Dimension, signed short>();
       }
-      //    else if(PixelType == "unsigned_short"){
-      //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
-      //       UpdateWithDimAndPixelType<Dimension, unsigned short>();
-      //     }
+      else if(PixelType == "unsigned_short"){
+        if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
+        UpdateWithDimAndPixelType<Dimension, unsigned short>();
+      }
 
       else if (PixelType == "unsigned_char") {
         if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
@@ -138,7 +139,7 @@ namespace clitk
       {
         if (m_ArgsInfo.matrix_given)
           {
-            std::cerr << "You must use either rotate/translate or matrix options" << std::cout;
+            std::cerr << "You must use either rotate/translate or matrix options" << std::endl;
             return;
           }
         itk::Array<double> transformParameters(2 * Dimension);
@@ -181,7 +182,8 @@ namespace clitk
           }
         else {
           if (m_ArgsInfo.elastix_given) {
-            matrix = createMatrixFromElastixFile<Dimension,PixelType>(m_ArgsInfo.elastix_arg);
+            std::string filename(m_ArgsInfo.elastix_arg);
+            matrix = createMatrixFromElastixFile<Dimension>(filename, m_Verbose);
           }
           else 
             matrix.SetIdentity();
@@ -210,11 +212,12 @@ namespace clitk
       likeReader->SetFileName(m_ArgsInfo.like_arg);
       likeReader->Update();
       resampler->SetOutputParametersFromImage(likeReader->GetOutput());
+      resampler->SetOutputDirection(likeReader->GetOutput()->GetDirection());
     } 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;
@@ -278,17 +281,50 @@ namespace clitk
           outputOrigin[i]=m_ArgsInfo.origin_arg[i];
       } else outputOrigin=input->GetOrigin();
 
+      //Direction
+      typename OutputImageType::DirectionType outputDirection;
+      if (m_ArgsInfo.direction_given) {
+        for(unsigned int j=0; j< Dimension; j++)
+            for(unsigned int i=0; i< Dimension; i++)
+                outputDirection[j][i]=m_ArgsInfo.direction_arg[i+Dimension*j];
+      } else outputDirection=input->GetDirection();
+
       // Set
       resampler->SetSize( outputSize );
       resampler->SetOutputSpacing( outputSpacing );
       resampler->SetOutputOrigin(  outputOrigin );
+      resampler->SetOutputDirection( outputDirection );
+
+    }
+
+    if (m_ArgsInfo.spacinglike_given) {
+      typename InputReaderType::Pointer likeReader=InputReaderType::New();
+      likeReader->SetFileName(m_ArgsInfo.spacinglike_arg);
+      likeReader->Update(); 
+
+      // set the support like the image 
+      if (m_ArgsInfo.like_given) {
+        typename OutputImageType::SizeType outputSize;
+        outputSize[0] = ceil(resampler->GetSize()[0]*resampler->GetOutputSpacing()[0]
+                             /likeReader->GetOutput()->GetSpacing()[0]);
+        outputSize[1] = ceil(resampler->GetSize()[1]*resampler->GetOutputSpacing()[1]
+                             /likeReader->GetOutput()->GetSpacing()[1]);
+        outputSize[2] = ceil(resampler->GetSize()[2]*resampler->GetOutputSpacing()[2]
+                             /likeReader->GetOutput()->GetSpacing()[2]);
+        if (m_ArgsInfo.verbose_flag) {
+          std::cout << "Compute the number of pixels such as the support is like " << m_ArgsInfo.like_arg << std::endl;
+        }
+        resampler->SetSize( outputSize );
+      }
 
+      resampler->SetOutputSpacing( likeReader->GetOutput()->GetSpacing() );      
     }
 
     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;
+      std::cout << "Setting the output direction to " << resampler->GetOutputDirection() << "..." << std::endl;
     }
 
     resampler->SetInput( input );
@@ -343,7 +379,7 @@ namespace clitk
       {
         if (m_ArgsInfo.matrix_given)
           {
-            std::cerr << "You must use either rotate/translate or matrix options" << std::cout;
+            std::cerr << "You must use either rotate/translate or matrix options" << std::endl;
             return;
           }
         itk::Array<double> transformParameters(2 * Dimension);
@@ -412,6 +448,7 @@ namespace clitk
       resampler->SetSize( likeReader->GetOutput()->GetLargestPossibleRegion().GetSize() );
       resampler->SetOutputSpacing( likeReader->GetOutput()->GetSpacing() );
       resampler->SetOutputOrigin(  likeReader->GetOutput()->GetOrigin() );
+      resampler->SetOutputDirection( likeReader->GetOutput()->GetDirection() );
     } else {
       //Size
       typename OutputImageType::SizeType outputSize;
@@ -437,10 +474,20 @@ namespace clitk
       } else outputOrigin=input->GetOrigin();
       std::cout<<"Setting the origin to "<<outputOrigin<<"..."<<std::endl;
 
+      //Direction
+      typename OutputImageType::DirectionType outputDirection;
+      if (m_ArgsInfo.direction_given) {
+        for(unsigned int j=0; j< Dimension; j++)
+            for(unsigned int i=0; i< Dimension; i++)
+                outputDirection[j][i]=m_ArgsInfo.direction_arg[i+Dimension*j];
+      } else outputDirection=input->GetDirection();
+      std::cout<<"Setting the direction to "<<outputDirection<<"..."<<std::endl;
+
       // Set
       resampler->SetSize( outputSize );
       resampler->SetOutputSpacing( outputSpacing );
       resampler->SetOutputOrigin(  outputOrigin );
+      resampler->SetOutputDirection( outputDirection );
 
     }
 
@@ -466,121 +513,6 @@ namespace clitk
 
   }
   //-------------------------------------------------------------------
-  
-  
-  //-------------------------------------------------------------------
-  template<class args_info_type>
-  template<unsigned int Dimension, class PixelType>
-  typename itk::Matrix<double, Dimension+1, Dimension+1>
-   AffineTransformGenericFilter<args_info_type>::createMatrixFromElastixFile(std::string filename)
-  {
-    if (Dimension != 3) {
-      FATAL("Only 3D yet" << std::endl);
-    }
-    typename itk::Matrix<double, Dimension+1, Dimension+1> matrix;
-
-    // Open file
-    std::ifstream is;
-    clitk::openFileForReading(is, filename);
-
-    // Check Transform
-    std::string s; 
-    bool b = GetElastixValueFromTag(is, "Transform ", s);
-    if (!b) {
-      FATAL("Error must read 'Transform' in " << filename << std::endl);
-    }
-    if (s != "EulerTransform") {
-      FATAL("Sorry only 'EulerTransform'" << std::endl);
-    }
-
-    // FIXME check
-    //    (InitialTransformParametersFileName "NoInitialTransform")
-
-    // Get CenterOfRotationPoint
-    GetElastixValueFromTag(is, "CenterOfRotationPoint ", s); // space is needed
-    if (!b) {
-      FATAL("Error must read 'CenterOfRotationPoint' in " << filename << std::endl);
-    }
-    std::vector<std::string> cor; 
-    GetValuesFromValue(s, cor);
-
-    // Get Transformparameters
-    GetElastixValueFromTag(is, "TransformParameters ", s); // space is needed
-    if (!b) {
-      FATAL("Error must read 'TransformParameters' in " << filename << std::endl);
-    }
-    std::vector<std::string> results; 
-    GetValuesFromValue(s, results);
-    
-    // construct a stream from the string
-    itk::CenteredEuler3DTransform<double>::Pointer mat = itk::CenteredEuler3DTransform<double>::New();
-    itk::CenteredEuler3DTransform<double>::ParametersType p;
-    p.SetSize(9);
-    for(uint i=0; i<3; i++)
-      p[i] = atof(results[i].c_str()); // Rotation
-    for(uint i=0; i<3; i++)
-      p[i+3] = atof(cor[i].c_str()); // Centre of rotation
-    for(uint i=0; i<3; i++)
-      p[i+6] = atof(results[i+3].c_str()); // Translation
-    mat->SetParameters(p);
-    
-    if (m_Verbose) {
-      std::cout << "Rotation      (deg) : " << rad2deg(p[0]) << " " << rad2deg(p[1]) << " " << rad2deg(p[2]) << std::endl;
-      std::cout << "Translation   (phy) : " << p[3] << " " << p[4] << " " << p[5] << std::endl;
-      std::cout << "Center of rot (phy) : " << p[6] << " " << p[7] << " " << p[8] << std::endl;
-    }
-
-    for(uint i=0; i<3; i++)
-      for(uint j=0; j<3; j++)
-        matrix[i][j] = mat->GetMatrix()[i][j];
-    // Offset is -Rc + t + c
-    matrix[0][3] = mat->GetOffset()[0];
-    matrix[1][3] = mat->GetOffset()[1];
-    matrix[2][3] = mat->GetOffset()[2];
-    matrix[3][3] = 1;
-    
-    return matrix;
-  }
-
-  //-------------------------------------------------------------------
-  template<class args_info_type>
-  bool
-  AffineTransformGenericFilter<args_info_type>::GetElastixValueFromTag(std::ifstream & is, 
-                                                                       std::string tag, 
-                                                                       std::string & value)
-  {
-    std::string line;
-    is.seekg (0, is.beg);
-    while(std::getline(is, line))   {
-      unsigned pos = line.find(tag);
-      if (pos<line.size()) {
-        value=line.substr(pos+tag.size(),line.size()-2);// remove last ')'
-        value.erase (std::remove (value.begin(), value.end(), '"'), value.end());
-        value.erase (std::remove (value.begin(), value.end(), ')'), value.end());
-        return true;
-      }
-   }
-    return false;
-  }
-  //-------------------------------------------------------------------
-
-
-  //-------------------------------------------------------------------
-  template<class args_info_type>
-  void
-  AffineTransformGenericFilter<args_info_type>::GetValuesFromValue(const std::string & s, 
-                                                                   std::vector<std::string> & values)
-  {
-    std::stringstream strstr(s);
-    std::istream_iterator<std::string> it(strstr);
-    std::istream_iterator<std::string> end;
-    std::vector<std::string> results(it, end);
-    values.clear();
-    values.resize(results.size());
-    for(uint i=0; i<results.size(); i++) values[i] = results[i];
-  }
-  //-------------------------------------------------------------------
-
 
 } //end clitk