NifTK  16.4.1 - 0798f20
CMIC's Translational Medical Imaging Platform
itkMatrixLinearCombinationFunctions.h
Go to the documentation of this file.
1 /*=============================================================================
2 
3  NifTK: A software platform for medical image computing.
4 
5  Copyright (c) University College London (UCL). All rights reserved.
6 
7  This software is distributed WITHOUT ANY WARRANTY; without even
8  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9  PURPOSE.
10 
11  See LICENSE.txt in the top level directory for details.
12 
13 =============================================================================*/
14 
15 #ifndef itkMatrixLinearCombinationFunctions_h
16 #define itkMatrixLinearCombinationFunctions_h
17 #include <algorithm>
18 #include <niftkConversionUtils.h>
19 
20 namespace itk
21 {
22 
23 template <typename TVnlMatrixFixed>
25 {
26 public:
30  static TVnlMatrixFixed ComputeMatrixSquareRoot(const TVnlMatrixFixed& matrix, typename TVnlMatrixFixed::element_type tolerance)
31  {
32  TVnlMatrixFixed X = matrix;
33  TVnlMatrixFixed Y;
34  Y.set_identity();
35 
36  while (fabs((X*X - matrix).array_inf_norm()) > tolerance)
37  {
38  TVnlMatrixFixed iX = vnl_matrix_inverse<typename TVnlMatrixFixed::element_type>(X).inverse();
39  TVnlMatrixFixed iY = vnl_matrix_inverse<typename TVnlMatrixFixed::element_type>(Y).inverse();
40 
41  X = 0.5*(X + iY);
42  Y = 0.5*(Y + iX);
43  //std::cout << X << std::endl;
44  }
45  return X;
46  }
47 
51  static TVnlMatrixFixed ComputeMatrixExponential(const TVnlMatrixFixed& matrix)
52  {
53  double j = std::max(0.0, 1.0+floor(log(matrix.array_inf_norm())/log(2.0)));
54  TVnlMatrixFixed A = matrix*pow(2.0, -j);
55  TVnlMatrixFixed D;
56  D.set_identity();
57  TVnlMatrixFixed N;
58  N.set_identity();
59  TVnlMatrixFixed X;
60  X.set_identity();
61  double c = 1.0;
62  int q = 6; // 6 said to be a good choice in the paper.
63 
64  for (int k = 1; k <= q; k++)
65  {
66  c = c*(q-k+1.0)/(k*(2.0*q-k+1.0));
67  X = A*X;
68  N = N + X*c;
69  D = D + X*(pow(-1.0, k)*c);
70  }
71 
72  X = vnl_matrix_inverse<typename TVnlMatrixFixed::element_type>(D).inverse()*N;
73 
74  for (int index = 0; index < niftk::Round(j); index++)
75  {
76  X = X * X;
77  }
78 
79  return X;
80  }
81 
85  static TVnlMatrixFixed ComputeMatrixLogarithm(const TVnlMatrixFixed& matrix, typename TVnlMatrixFixed::element_type tolerance)
86  {
87  int k = 0;
88  TVnlMatrixFixed A = matrix;
89  TVnlMatrixFixed I;
90  I.set_identity();
91 
92  while (fabs((A-I).array_inf_norm()) > 0.5)
93  {
95  k = k+1;
96  }
97  A = I - A;
98  TVnlMatrixFixed Z = A;
99  TVnlMatrixFixed X = A;
100  double i = 1.0;
101  while (Z.array_inf_norm() > tolerance)
102  {
103  Z = Z * A;
104  i = i+1.0;
105  X = X + Z/i;
106  }
107 
108  X = -X * pow(2.0, k);
109  return X;
110  }
111 };
112 
113 }
114 
115 
116 #ifndef ITK_MANUAL_INSTANTIATION
117 #include "itkMatrixLinearCombinationFunctions.txx"
118 #endif
119 
120 #endif
static TVnlMatrixFixed ComputeMatrixLogarithm(const TVnlMatrixFixed &matrix, typename TVnlMatrixFixed::element_type tolerance)
Compute the matrix logarithm according to "Linear combination of transformations", Marc Alex, Volume 21, Issue 3, ACM SIGGRAPH 2002.
Definition: itkMatrixLinearCombinationFunctions.h:85
const GLfloat * c
Definition: glew.h:14144
Definition: niftkITKAffineResampleImage.cxx:74
static TVnlMatrixFixed ComputeMatrixSquareRoot(const TVnlMatrixFixed &matrix, typename TVnlMatrixFixed::element_type tolerance)
Compute the square root of a matrix according to "Linear combination of transformations", Marc Alex, Volume 21, Issue 3, ACM SIGGRAPH 2002.
Definition: itkMatrixLinearCombinationFunctions.h:30
GLdouble GLdouble GLdouble GLdouble q
Definition: glew.h:1398
static TVnlMatrixFixed ComputeMatrixExponential(const TVnlMatrixFixed &matrix)
Compute the matrix exponential according to "Linear combination of transformations", Marc Alex, Volume 21, Issue 3, ACM SIGGRAPH 2002.
Definition: itkMatrixLinearCombinationFunctions.h:51
int Round(double d)
Definition: niftkConversionUtils.cxx:160
GLuint GLenum matrix
Definition: glew.h:12775
GLuint index
Definition: glew.h:1798
Definition: itkMatrixLinearCombinationFunctions.h:24