From 58d0dc73fae9ce19162706d3da1392712a9e73c5 Mon Sep 17 00:00:00 2001 From: Simon Rit Date: Wed, 8 Feb 2012 19:52:16 +0100 Subject: [PATCH] Modified the string output of 4x4 matrix, now public for use in other parts of the code --- vv/vvMainWindow.cxx | 23 ++++++++++++++++------- vv/vvMainWindow.h | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/vv/vvMainWindow.cxx b/vv/vvMainWindow.cxx index b102e62..d210d11 100644 --- a/vv/vvMainWindow.cxx +++ b/vv/vvMainWindow.cxx @@ -1279,20 +1279,29 @@ QString vvMainWindow::GetSizeInBytes(unsigned long size) //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -QString vvMainWindow::Get4x4MatrixDoubleAsString(vtkSmartPointer matrix) +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.flags(ios::showpos); - strmatrix.width(10); - strmatrix.precision(3); strmatrix.setf(ios::fixed,ios::floatfield); + strmatrix.precision(precision); strmatrix.fill(' '); - strmatrix << std::left << matrix->GetElement(i, j); - //strmatrix.width(10); - strmatrix << " "; + strmatrix.width(width[j]); + strmatrix << std::right << matrix->GetElement(i, j); } strmatrix << std::endl; } diff --git a/vv/vvMainWindow.h b/vv/vvMainWindow.h index 23ee6f4..1650571 100644 --- a/vv/vvMainWindow.h +++ b/vv/vvMainWindow.h @@ -64,6 +64,7 @@ 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(); @@ -194,7 +195,6 @@ private: QString GetSizeInBytes(unsigned long size); QString GetVectorDoubleAsString(std::vector vectorDouble); QString GetVectorIntAsString(std::vector vectorInt); - QString Get4x4MatrixDoubleAsString(vtkSmartPointer matrix); int GetSlicerIndexFromItem(QTreeWidgetItem* item); QTreeWidgetItem* GetItemFromSlicerManager(vvSlicerManager* sm); void SaveScreenshot(QVTKWidget *widget); -- 2.45.1