]> Creatis software - clitk.git/blobdiff - vv/vvImageReader.cxx
Delete widget first, the image data after
[clitk.git] / vv / vvImageReader.cxx
index 7cc0e569856b160276385b1f7c81d7e9ecba03ec..25e235a08edb3b713b347b5a5a1147aaed7fb061 100644 (file)
@@ -1,7 +1,7 @@
 /*=========================================================================
   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
 
-  Authors belong to: 
+  Authors belong to:
   - University of LYON              http://www.universite-lyon.fr/
   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
@@ -22,6 +22,7 @@
 #include <itkImageFileReader.h>
 #include "vvImageReader.h"
 #include "vvImageReader.txx"
+#include "clitkTransformUtilities.h"
 
 //------------------------------------------------------------------------------
 vvImageReader::vvImageReader()
@@ -30,6 +31,7 @@ vvImageReader::vvImageReader()
   mInputFilenames.resize(0);
   mLastError = "";
   mType = UNDEFINEDIMAGETYPE;
+  mSlice = 0;
 }
 //------------------------------------------------------------------------------
 
@@ -53,8 +55,7 @@ void vvImageReader::Update(LoadedImageType type)
   itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(mInputFilenames[0].c_str(), itk::ImageIOFactory::ReadMode);
   if (!reader) {
     mLastError="Unable to read file.";
-  }
-  else {
+  } else {
     reader->SetFileName(mInputFilenames[0]);
     reader->ReadImageInformation();
     if (mInputFilenames.size() > 1)
@@ -67,17 +68,17 @@ void vvImageReader::Update(LoadedImageType type)
 
 
 //------------------------------------------------------------------------------
-void vvImageReader::Update(int dim,std::string inputPixelType, LoadedImageType type) {
+void vvImageReader::Update(int dim,std::string inputPixelType, LoadedImageType type)
+{
   //CALL_FOR_ALL_DIMS(dim,UpdateWithDim,inputPixelType);
   mType = type;
   mDim = dim;
   mInputPixelType=inputPixelType;
   this->start(); //Start heavy read operation in a separate thread
-  while (this->isRunning())
-    {
-      qApp->processEvents();
-      this->wait(50);
-    }
+  while (this->isRunning()) {
+    qApp->processEvents();
+    this->wait(50);
+  }
 }
 //------------------------------------------------------------------------------
 
@@ -112,10 +113,90 @@ void vvImageReader::SetInputFilename(const std::string & filename)
 
 
 //------------------------------------------------------------------------------
-void vvImageReader::SetInputFilenames(const std::vector<std::string> & filenames) {
+void vvImageReader::SetInputFilenames(const std::vector<std::string> & filenames)
+{
   mInputFilenames = filenames;
 }
 //------------------------------------------------------------------------------
 
+
+//------------------------------------------------------------------------------
+//Read transformation in NKI format (Xdr, transposed, cm)
+void vvImageReader::ReadNkiImageTransform()
+{
+  bool bRead=false;
+  std::string filename = mInputFilenames[0]+".MACHINEORIENTATION";
+  if(itksys::SystemTools::FileExists(filename.c_str())){
+    typedef itk::ImageFileReader< itk::Image< double, 2 > > MatrixReaderType;
+    MatrixReaderType::Pointer readerTransfo = MatrixReaderType::New();
+    readerTransfo->SetFileName(filename);
+    try {
+      bRead = true;
+      readerTransfo->Update();
+    } catch( itk::ExceptionObject & err ) {
+      bRead=false;
+      std::cerr << "Cannot read " << filename << std::endl
+                << "The error is: " << err << std::endl;
+    }
+
+    if (bRead) {
+      //Transpose matrix (NKI format)
+      for(int j=0; j<4; j++)
+        for(int i=0; i<4; i++)
+          mImage->GetTransform()->GetMatrix()->SetElement(j,i,readerTransfo->GetOutput()->GetBufferPointer()[4*i+j]);
+
+      //From cm to mm
+      for(int i=0; i<3; i++)
+        mImage->GetTransform()->GetMatrix()->SetElement(i,3,10*mImage->GetTransform()->GetMatrix()->GetElement(i,3));
+
+      mImage->GetTransform()->Inverse();
+    }
+  }
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+//Read transformation in ASCII format
+void vvImageReader::ReadMatImageTransform()
+{
+  std::string filename(mInputFilenames[0]+".mat");
+  std::ifstream f(filename.c_str());
+  if(f.is_open()) {
+    f.close();
+    
+    itk::Matrix<double, 4, 4> itkMat = clitk::ReadMatrix3D(filename);
+    
+    vtkSmartPointer<vtkMatrix4x4> matrix = vtkSmartPointer<vtkMatrix4x4>::New();
+    matrix->Identity();
+    for(int j=0; j<4; j++)
+      for(int i=0; i<4; i++)
+        matrix->SetElement(j,i,itkMat[j][i]);
+
+    // RP: 14/03/2011
+    //  For some reason, the transformation matrix given in the MHD
+    // file is inverted wrt the transformation given in the MAT file.
+    // We don't really know where the inversion takes place inside
+    // VTK or ITK. For what we could see in VV, the transformation
+    // given in the MHD file seems "more correct" than that given in 
+    // the MAT file. For this reason, we invert the matrix read from
+    // the MAT file before concatenating it to the current transformation.
+    // Still, it would be nice to find out what happens exactly between
+    // VTK and ITK...
+    //
+    vtkSmartPointer<vtkMatrix4x4> inv_matrix = vtkSmartPointer<vtkMatrix4x4>::New();
+    if (matrix->Determinant() == 0)
+    {
+      vtkGenericWarningMacro("Matrix in " << filename.c_str() << " cannot be inverted (determinant = 0)");
+    }
+    else
+      matrix->Invert(*matrix, *inv_matrix);
+    
+    mImage->GetTransform()->PostMultiply();
+    mImage->GetTransform()->Concatenate(inv_matrix);
+    mImage->GetTransform()->Update();
+  }
+}
+//------------------------------------------------------------------------------
 #endif