]> Creatis software - clitk.git/blob - common/clitkMatrix.cxx
Merge branch 'master' of git.creatis.insa-lyon.fr:clitk
[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
21 //--------------------------------------------------------------------
22 namespace clitk {
23
24 //-------------------------------------------------------------------
25 std::string
26 Get4x4MatrixDoubleAsString(vtkMatrix4x4 *matrix,
27                            const int precision)
28 {
29   std::ostringstream strmatrix;
30
31   // Figure out the number of digits of the integer part of the largest absolute value
32   // for each column
33   unsigned width[4];
34   for (unsigned int j = 0; j < 4; j++){
35     double absmax = 0.;
36     for (unsigned int i = 0; i < 4; i++)
37       absmax = std::max(absmax, vnl_math_abs(matrix->GetElement(i, j)));
38     unsigned ndigits = (unsigned)std::max(0.,std::log10(absmax))+1;
39     width[j] = precision+ndigits+3;
40   }
41
42   // Output with correct width, aligned to the right
43   for (unsigned int i = 0; i < 4; i++) {
44     for (unsigned int j = 0; j < 4; j++) {
45       strmatrix.setf(ios::fixed,ios::floatfield);
46       strmatrix.precision(precision);
47       strmatrix.fill(' ');
48       strmatrix.width(width[j]);
49       strmatrix << std::right << matrix->GetElement(i, j);
50     }
51     strmatrix << std::endl;
52   }
53   std::string result = strmatrix.str().c_str();
54   return result;
55 }
56 //-------------------------------------------------------------------
57
58 //-------------------------------------------------------------------
59 std::string
60 Get4x4MatrixDoubleAsString(itk::Matrix<double, 4, 4> m,
61                            const int precision)
62 {
63   vtkSmartPointer<vtkMatrix4x4> matrix = vtkSmartPointer<vtkMatrix4x4>::New();
64   for (unsigned int j = 0; j < 4; j++)
65     for (unsigned int i = 0; i < 4; i++)
66       matrix->SetElement(j,i,m[j][i]);
67   return Get4x4MatrixDoubleAsString(matrix, precision);
68 }
69 //-------------------------------------------------------------------
70 }
71 //-------------------------------------------------------------------