]> Creatis software - clitk.git/blobdiff - vv/vvImageReader.cxx
Delete widget first, the image data after
[clitk.git] / vv / vvImageReader.cxx
index 0076bf7663767d212d2306b7c52fe8c124fa012b..25e235a08edb3b713b347b5a5a1147aaed7fb061 100644 (file)
@@ -167,14 +167,33 @@ void vvImageReader::ReadMatImageTransform()
     
     itk::Matrix<double, 4, 4> itkMat = clitk::ReadMatrix3D(filename);
     
-    vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
-    transform->Identity();
+    vtkSmartPointer<vtkMatrix4x4> matrix = vtkSmartPointer<vtkMatrix4x4>::New();
+    matrix->Identity();
     for(int j=0; j<4; j++)
       for(int i=0; i<4; i++)
-        transform->GetMatrix()->SetElement(j,i,itkMat[j][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(transform->GetMatrix());
+    mImage->GetTransform()->Concatenate(inv_matrix);
     mImage->GetTransform()->Update();
   }
 }