From: Simon Rit Date: Thu, 4 Jul 2013 16:36:54 +0000 (+0200) Subject: Made .mat reading robust to non matrix files (e.g., Gate material files) X-Git-Tag: v1.4.0~169^2~5^2 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=2609379df5647933d2a260a03cfd7078f548aeeb;p=clitk.git Made .mat reading robust to non matrix files (e.g., Gate material files) --- diff --git a/common/clitkTransformUtilities.cxx b/common/clitkTransformUtilities.cxx index ef210d7..832d598 100644 --- a/common/clitkTransformUtilities.cxx +++ b/common/clitkTransformUtilities.cxx @@ -67,4 +67,32 @@ itk::Matrix GetRotationMatrix<3>(itk::Array rotationParame return GetRotationMatrix3D(rotationParameters); } +//-------------------------------------------------------------------- +itk::Matrix ReadMatrix3D(std::string fileName) +{ + // read input matrix + std::ifstream is; + openFileForReading(is, fileName); + std::vector nb; + double x; + skipComment(is); + is >> x; + while (is && !is.eof()) { + nb.push_back(x); + skipComment(is); + is >> x; + } + + if(nb.size() != 16) + itkGenericExceptionMacro(<< "Could not read 4x4 matrix in " << fileName); + + //copy it to the matrix + itk::Matrix matrix; + unsigned int index=0; + for (unsigned int i=0;i<4;i++) + for (unsigned int j=0;j<4;j++) + matrix[i][j]=nb[index++]; + return matrix; +} + } diff --git a/common/clitkTransformUtilities.h b/common/clitkTransformUtilities.h index cbc30b9..d3644b1 100644 --- a/common/clitkTransformUtilities.h +++ b/common/clitkTransformUtilities.h @@ -255,29 +255,7 @@ namespace clitk return matrix; } - inline itk::Matrix ReadMatrix3D(std::string fileName) - { - // read input matrix - std::ifstream is; - openFileForReading(is, fileName); - std::vector nb; - double x; - skipComment(is); - is >> x; - while (!is.eof()) { - nb.push_back(x); - skipComment(is); - is >> x; - } - - //copy it to the matrix - itk::Matrix matrix; - unsigned int index=0; - for (unsigned int i=0;i<4;i++) - for (unsigned int j=0;j<4;j++) - matrix[i][j]=nb[index++]; - return matrix; - } + itk::Matrix ReadMatrix3D(std::string fileName); inline vtkMatrix4x4* ReadVTKMatrix3D(std::string fileName) { // read input matrix diff --git a/common/vvImageReader.cxx b/common/vvImageReader.cxx index f4f1a90..ddc4ad3 100644 --- a/common/vvImageReader.cxx +++ b/common/vvImageReader.cxx @@ -161,7 +161,15 @@ void vvImageReader::ReadMatImageTransform() if(f.is_open()) { f.close(); - itk::Matrix itkMat = clitk::ReadMatrix3D(filename); + itk::Matrix itkMat; + itkMat.SetIdentity(); + try { + itkMat = clitk::ReadMatrix3D(filename); + } + catch (itk::ExceptionObject & err) { + itkWarningMacro(<< "Found " << filename + << " but this is not a 4x4 matrix so it is ignored."); + } vtkSmartPointer matrix = vtkSmartPointer::New(); matrix->Identity();