-
- if (bRead)
- { double mat[16];
-
- //Transpose matrix (NKI format)
- for(int j=0; j<4; j++)
- for(int i=0; i<4; i++)
- mat[4*j+i]=readerTransfo->GetOutput()->GetBufferPointer()[4*i+j];
-
- //From cm to mm
- for(int i=0; i<3; i++)
- mat[4*i+3]*=10;
-
- //Set Transformation
- vtkSmartPointer<vtkTransform> pt = vtkSmartPointer<vtkTransform>::New();
- pt->SetMatrix( mat );
- pt->Inverse();
-DD(*pt);
- mImage->SetTransform( pt );
+ }
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+//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)");