1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #ifndef VVIMAGEREADER_CXX
19 #define VVIMAGEREADER_CXX
21 #include <QApplication>
22 #include <itkImageFileReader.h>
23 #include "vvImageReader.h"
24 #include "vvImageReader.txx"
25 #include "clitkTransformUtilities.h"
27 //------------------------------------------------------------------------------
28 vvImageReader::vvImageReader()
31 mInputFilenames.resize(0);
33 mType = UNDEFINEDIMAGETYPE;
36 //------------------------------------------------------------------------------
39 //------------------------------------------------------------------------------
40 vvImageReader::~vvImageReader() { }
41 //------------------------------------------------------------------------------
44 //------------------------------------------------------------------------------
45 void vvImageReader::Update()
49 //------------------------------------------------------------------------------
52 //------------------------------------------------------------------------------
53 void vvImageReader::Update(LoadedImageType type)
55 itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(mInputFilenames[0].c_str(), itk::ImageIOFactory::ReadMode);
57 mLastError="Unable to read file.";
59 reader->SetFileName(mInputFilenames[0]);
60 reader->ReadImageInformation();
61 if (mInputFilenames.size() > 1)
62 Update(reader->GetNumberOfDimensions()+1,reader->GetComponentTypeAsString(reader->GetComponentType()),type);
64 Update(reader->GetNumberOfDimensions(),reader->GetComponentTypeAsString(reader->GetComponentType()),type);
67 //------------------------------------------------------------------------------
70 //------------------------------------------------------------------------------
71 void vvImageReader::Update(int dim,std::string inputPixelType, LoadedImageType type)
73 //CALL_FOR_ALL_DIMS(dim,UpdateWithDim,inputPixelType);
76 mInputPixelType=inputPixelType;
77 this->start(); //Start heavy read operation in a separate thread
78 while (this->isRunning()) {
79 qApp->processEvents();
83 //------------------------------------------------------------------------------
86 //------------------------------------------------------------------------------
87 void vvImageReader::run()
91 UpdateWithDim<2>(mInputPixelType);
94 UpdateWithDim<3>(mInputPixelType);
97 UpdateWithDim<4>(mInputPixelType);
100 std::cerr << "dimension unknown in Update ! " << std::endl;
103 //------------------------------------------------------------------------------
106 //------------------------------------------------------------------------------
107 void vvImageReader::SetInputFilename(const std::string & filename)
109 mInputFilenames.resize(0);
110 mInputFilenames.push_back(filename);
112 //------------------------------------------------------------------------------
115 //------------------------------------------------------------------------------
116 void vvImageReader::SetInputFilenames(const std::vector<std::string> & filenames)
118 mInputFilenames = filenames;
120 //------------------------------------------------------------------------------
123 //------------------------------------------------------------------------------
124 //Read transformation in NKI format (Xdr, transposed, cm)
125 void vvImageReader::ReadNkiImageTransform()
128 std::string filename = mInputFilenames[0]+".MACHINEORIENTATION";
129 if(itksys::SystemTools::FileExists(filename.c_str())){
130 typedef itk::ImageFileReader< itk::Image< double, 2 > > MatrixReaderType;
131 MatrixReaderType::Pointer readerTransfo = MatrixReaderType::New();
132 readerTransfo->SetFileName(filename);
135 readerTransfo->Update();
136 } catch( itk::ExceptionObject & err ) {
138 std::cerr << "Cannot read " << filename << std::endl
139 << "The error is: " << err << std::endl;
143 //Transpose matrix (NKI format)
144 for(int j=0; j<4; j++)
145 for(int i=0; i<4; i++)
146 mImage->GetTransform()->GetMatrix()->SetElement(j,i,readerTransfo->GetOutput()->GetBufferPointer()[4*i+j]);
149 for(int i=0; i<3; i++)
150 mImage->GetTransform()->GetMatrix()->SetElement(i,3,10*mImage->GetTransform()->GetMatrix()->GetElement(i,3));
152 mImage->GetTransform()->Inverse();
156 //------------------------------------------------------------------------------
159 //------------------------------------------------------------------------------
160 //Read transformation in ASCII format
161 void vvImageReader::ReadMatImageTransform()
163 std::string filename(mInputFilenames[0]+".mat");
164 std::ifstream f(filename.c_str());
168 itk::Matrix<double, 4, 4> itkMat = clitk::ReadMatrix3D(filename);
170 vtkSmartPointer<vtkMatrix4x4> matrix = vtkSmartPointer<vtkMatrix4x4>::New();
172 for(int j=0; j<4; j++)
173 for(int i=0; i<4; i++)
174 matrix->SetElement(j,i,itkMat[j][i]);
177 // For some reason, the transformation matrix given in the MHD
178 // file is inverted wrt the transformation given in the MAT file.
179 // We don't really know where the inversion takes place inside
180 // VTK or ITK. For what we could see in VV, the transformation
181 // given in the MHD file seems "more correct" than that given in
182 // the MAT file. For this reason, we invert the matrix read from
183 // the MAT file before concatenating it to the current transformation.
184 // Still, it would be nice to find out what happens exactly between
187 vtkSmartPointer<vtkMatrix4x4> inv_matrix = vtkSmartPointer<vtkMatrix4x4>::New();
188 if (matrix->Determinant() == 0)
190 vtkGenericWarningMacro("Matrix in " << filename.c_str() << " cannot be inverted (determinant = 0)");
193 matrix->Invert(*matrix, *inv_matrix);
195 mImage->GetTransform()->PostMultiply();
196 mImage->GetTransform()->Concatenate(inv_matrix);
197 mImage->GetTransform()->Update();
200 //------------------------------------------------------------------------------