From e14892179201c98b313764771c2456cf5cb29a3d Mon Sep 17 00:00:00 2001 From: Simon Rit Date: Thu, 25 Jul 2013 10:50:04 +0200 Subject: [PATCH] Move matrix to string conversion to common library --- common/CMakeLists.txt | 1 + common/clitkMatrix.cxx | 71 +++++++++++++++++++++++++ common/clitkMatrix.h | 38 +++++++++++++ tools/clitkElastixTransformToMatrix.cxx | 9 ++-- vv/vvMainWindow.cxx | 35 +----------- vv/vvMainWindow.h | 1 - vv/vvToolRigidReg.cxx | 5 +- 7 files changed, 118 insertions(+), 42 deletions(-) create mode 100644 common/clitkMatrix.cxx create mode 100644 common/clitkMatrix.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 95a1023..bef0f36 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -39,6 +39,7 @@ SET(clitkCommon_SRC clitkExceptionObject.cxx clitkFilterBase.cxx clitkMemoryUsage.cxx + clitkMatrix.cxx vvImage.cxx vvImageReader.cxx vvImageWriter.cxx diff --git a/common/clitkMatrix.cxx b/common/clitkMatrix.cxx new file mode 100644 index 0000000..c911f55 --- /dev/null +++ b/common/clitkMatrix.cxx @@ -0,0 +1,71 @@ +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv + + Authors belong to: + - University of LYON http://www.universite-lyon.fr/ + - Léon Bérard cancer center http://www.centreleonberard.fr + - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the copyright notices for more information. + + It is distributed under dual licence + + - BSD See included LICENSE.txt file + - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +===========================================================================**/ + +#include "clitkMatrix.h" + +//-------------------------------------------------------------------- +namespace clitk { + +//------------------------------------------------------------------- +std::string +Get4x4MatrixDoubleAsString(vtkMatrix4x4 *matrix, + const int precision) +{ + std::ostringstream strmatrix; + + // Figure out the number of digits of the integer part of the largest absolute value + // for each column + unsigned width[4]; + for (unsigned int j = 0; j < 4; j++){ + double absmax = 0.; + for (unsigned int i = 0; i < 4; i++) + absmax = std::max(absmax, vnl_math_abs(matrix->GetElement(i, j))); + unsigned ndigits = (unsigned)std::max(0.,std::log10(absmax))+1; + width[j] = precision+ndigits+3; + } + + // Output with correct width, aligned to the right + for (unsigned int i = 0; i < 4; i++) { + for (unsigned int j = 0; j < 4; j++) { + strmatrix.setf(ios::fixed,ios::floatfield); + strmatrix.precision(precision); + strmatrix.fill(' '); + strmatrix.width(width[j]); + strmatrix << std::right << matrix->GetElement(i, j); + } + strmatrix << std::endl; + } + std::string result = strmatrix.str().c_str(); + return result; +} +//------------------------------------------------------------------- + +//------------------------------------------------------------------- +std::string +Get4x4MatrixDoubleAsString(itk::Matrix m, + const int precision) +{ + vtkSmartPointer matrix = vtkSmartPointer::New(); + for (unsigned int j = 0; j < 4; j++) + for (unsigned int i = 0; i < 4; i++) + matrix->SetElement(j,i,m[j][i]); + return Get4x4MatrixDoubleAsString(matrix, precision); +} +//------------------------------------------------------------------- +} +//------------------------------------------------------------------- diff --git a/common/clitkMatrix.h b/common/clitkMatrix.h new file mode 100644 index 0000000..1e497ea --- /dev/null +++ b/common/clitkMatrix.h @@ -0,0 +1,38 @@ +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv + + Authors belong to: + - University of LYON http://www.universite-lyon.fr/ + - Léon Bérard cancer center http://www.centreleonberard.fr + - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the copyright notices for more information. + + It is distributed under dual licence + + - BSD See included LICENSE.txt file + - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +===========================================================================**/ + +#ifndef clitkMatrix_h +#define clitkMatrix_h + +#include +#include +#include + +//-------------------------------------------------------------------- +namespace clitk { +std::string +Get4x4MatrixDoubleAsString(vtkMatrix4x4 *matrix, + const int precision=3); + +std::string +Get4x4MatrixDoubleAsString(itk::Matrix m, + const int precision=3); +} +//------------------------------------------------------------------- + +#endif diff --git a/tools/clitkElastixTransformToMatrix.cxx b/tools/clitkElastixTransformToMatrix.cxx index f2197bd..7efc4e9 100644 --- a/tools/clitkElastixTransformToMatrix.cxx +++ b/tools/clitkElastixTransformToMatrix.cxx @@ -20,6 +20,7 @@ #include "clitkElastixTransformToMatrix_ggo.h" #include "clitkAffineTransformGenericFilter.h" #include "clitkElastix.h" +#include "clitkMatrix.h" //-------------------------------------------------------------------- int main(int argc, char * argv[]) @@ -37,12 +38,8 @@ int main(int argc, char * argv[]) // Print matrix std::ofstream os; clitk::openFileForWriting(os, args_info.output_arg); - for(unsigned int i=0; i<4; i++) { - for(unsigned int j=0; j<4; j++) - os << m[i][j] << " "; - os << std::endl; - } - os.close(); + os << clitk::Get4x4MatrixDoubleAsString(m, 16); + os.close(); return EXIT_SUCCESS; }// end main diff --git a/vv/vvMainWindow.cxx b/vv/vvMainWindow.cxx index 4210798..15e1d91 100644 --- a/vv/vvMainWindow.cxx +++ b/vv/vvMainWindow.cxx @@ -48,6 +48,7 @@ It is distributed under dual licence #include "vvSaveState.h" #include "vvReadState.h" #include "clitkConfiguration.h" +#include "clitkMatrix.h" // ITK include #include @@ -1145,7 +1146,7 @@ void vvMainWindow::ImageInfoChanged() infoPanel->setNPixel(QString::number(NPixel)+" ("+inputSizeInBytes+")"); transformation = imageSelected->GetTransform()[tSlice]->GetMatrix(); - infoPanel->setTransformation(Get4x4MatrixDoubleAsString(transformation)); + infoPanel->setTransformation(clitk::Get4x4MatrixDoubleAsString(transformation).c_str()); landmarksPanel->SetCurrentLandmarks(mSlicerManagers[index]->GetLandmarks(), mSlicerManagers[index]->GetTSlice()); @@ -1343,38 +1344,6 @@ QString vvMainWindow::GetSizeInBytes(unsigned long size) } //------------------------------------------------------------------------------ -//------------------------------------------------------------------------------ -QString vvMainWindow::Get4x4MatrixDoubleAsString(vtkSmartPointer matrix, const int precision) -{ - std::ostringstream strmatrix; - - // Figure out the number of digits of the integer part of the largest absolute value - // for each column - unsigned width[4]; - for (unsigned int j = 0; j < 4; j++){ - double absmax = 0.; - for (unsigned int i = 0; i < 4; i++) - absmax = std::max(absmax, vnl_math_abs(matrix->GetElement(i, j))); - unsigned ndigits = (unsigned)std::max(0.,std::log10(absmax))+1; - width[j] = precision+ndigits+3; - } - - // Output with correct width, aligned to the right - for (unsigned int i = 0; i < 4; i++) { - for (unsigned int j = 0; j < 4; j++) { - strmatrix.setf(ios::fixed,ios::floatfield); - strmatrix.precision(precision); - strmatrix.fill(' '); - strmatrix.width(width[j]); - strmatrix << std::right << matrix->GetElement(i, j); - } - strmatrix << std::endl; - } - QString result = strmatrix.str().c_str(); - return result; -} -//------------------------------------------------------------------------------ - //------------------------------------------------------------------------------ QString vvMainWindow::GetVectorDoubleAsString(std::vector vectorDouble) { diff --git a/vv/vvMainWindow.h b/vv/vvMainWindow.h index e5e3155..b093554 100644 --- a/vv/vvMainWindow.h +++ b/vv/vvMainWindow.h @@ -69,7 +69,6 @@ class vvMainWindow: public vvMainWindowBase, void SaveCurrentStateAs(const std::string& stateFile); void ReadSavedStateFile(const std::string& stateFile); void LinkAllImages(); - QString Get4x4MatrixDoubleAsString(vtkSmartPointer matrix, const int precision=3); virtual void UpdateCurrentSlicer(); virtual QTabWidget * GetTab(); diff --git a/vv/vvToolRigidReg.cxx b/vv/vvToolRigidReg.cxx index 508551d..2948165 100644 --- a/vv/vvToolRigidReg.cxx +++ b/vv/vvToolRigidReg.cxx @@ -30,6 +30,7 @@ // clitk #include "clitkTransformUtilities.h" +#include "clitkMatrix.h" // qt #include @@ -120,7 +121,7 @@ void vvToolRigidReg::InputIsSelected(vvSlicerManager *input) for(int i=0; i<4; i++) // TODO SR and BP: check on the list of transforms and not the first only mInitialMatrix->SetElement(i,j, mCurrentSlicerManager->GetImage()->GetTransform()[0]->GetMatrix()->GetElement(i,j)); - QString origTransformString = dynamic_cast(mMainWindow)->Get4x4MatrixDoubleAsString(mInitialMatrix); + QString origTransformString(clitk::Get4x4MatrixDoubleAsString(mInitialMatrix).c_str()); transformationLabel->setText(origTransformString); SetTransform(mInitialMatrix); @@ -298,7 +299,7 @@ void vvToolRigidReg::SaveFile() if (file.open(QFile::WriteOnly | QFile::Truncate)) { // TODO SR and BP: check on the list of transforms and not the first only vtkMatrix4x4* matrix = mCurrentSlicerManager->GetImage()->GetTransform()[0]->GetMatrix(); - QString matrixStr = dynamic_cast(mMainWindow)->Get4x4MatrixDoubleAsString(matrix,16); + QString matrixStr = clitk::Get4x4MatrixDoubleAsString(matrix,16).c_str(); QTextStream out(&file); out << matrixStr; } -- 2.47.1