]> Creatis software - clitk.git/blob - common/clitkMatrix.cxx
2c09f311dd523416720c3496e890de46eaf1397b
[clitk.git] / common / clitkMatrix.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18
19 #include "clitkMatrix.h"
20 #include <algorithm>
21
22 //--------------------------------------------------------------------
23 namespace clitk {
24
25 //-------------------------------------------------------------------
26 std::string
27 Get4x4MatrixDoubleAsString(vtkMatrix4x4 *matrix,
28                            const int precision)
29 {
30   std::ostringstream strmatrix;
31
32   // Figure out the number of digits of the integer part of the largest absolute value
33   // for each column
34   unsigned width[4];
35   for (unsigned int j = 0; j < 4; j++){
36     double absmax = 0.;
37     for (unsigned int i = 0; i < 4; i++)
38       absmax = std::max(absmax, vnl_math_abs(matrix->GetElement(i, j)));
39     unsigned ndigits = (unsigned)std::max(0.,std::log10(absmax))+1;
40     width[j] = precision+ndigits+3;
41   }
42
43   // Output with correct width, aligned to the right
44   for (unsigned int i = 0; i < 4; i++) {
45     for (unsigned int j = 0; j < 4; j++) {
46       strmatrix.setf(ios::fixed,ios::floatfield);
47       strmatrix.precision(precision);
48       strmatrix.fill(' ');
49       strmatrix.width(width[j]);
50       strmatrix << std::right << matrix->GetElement(i, j);
51     }
52     strmatrix << std::endl;
53   }
54   std::string result = strmatrix.str().c_str();
55   return result;
56 }
57 //-------------------------------------------------------------------
58
59 //-------------------------------------------------------------------
60 std::string
61 Get4x4MatrixDoubleAsString(itk::Matrix<double, 4, 4> m,
62                            const int precision)
63 {
64   vtkSmartPointer<vtkMatrix4x4> matrix = vtkSmartPointer<vtkMatrix4x4>::New();
65   for (unsigned int j = 0; j < 4; j++)
66     for (unsigned int i = 0; i < 4; i++)
67       matrix->SetElement(j,i,m[j][i]);
68   return Get4x4MatrixDoubleAsString(matrix, precision);
69 }
70 //-------------------------------------------------------------------
71 }
72 //-------------------------------------------------------------------