Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPivotCalibration.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkPivotCalibration.h"
14 #include "vnl/algo/vnl_svd.h"
15 #include "vnl/vnl_matrix.h"
16 #include "vnl/vnl_vector.h"
17 #include <vtkMatrix4x4.h>
18 
19 mitk::PivotCalibration::PivotCalibration() : m_NavigationDatas(std::vector<mitk::NavigationData::Pointer>()), m_ResultPivotPoint(mitk::Point3D(0.0))
20 {
21 
22 
23 }
24 
26 {
27 
28 }
29 
30 void mitk::PivotCalibration::AddNavigationData(mitk::NavigationData::Pointer data)
31 {
32  m_NavigationDatas.push_back(data);
33 }
34 
36 {
37  return ComputePivotPoint();
38 }
39 
41 {
42  double defaultThreshold = 1e-1;
43 
44  std::vector<mitk::NavigationData::Pointer> _CheckedTransforms;
45  for (size_t i = 0; i < m_NavigationDatas.size(); ++i)
46  {
47  if (!m_NavigationDatas.at(i)->IsDataValid())
48  {
49  MITK_WARN << "Skipping invalid transform " << i << ".";
50  continue;
51  }
52  _CheckedTransforms.push_back(m_NavigationDatas.at(i));
53  }
54 
55  if (_CheckedTransforms.empty())
56  {
57  MITK_WARN << "Checked Transforms are empty";
58  return false;
59  }
60 
61  unsigned int rows = 3 * _CheckedTransforms.size();
62  unsigned int columns = 6;
63 
64  vnl_matrix< double > A(rows, columns), minusI(3, 3, 0), R(3, 3);
65  vnl_vector< double > b(rows), x(columns), t(3);
66 
67  minusI(0, 0) = -1;
68  minusI(1, 1) = -1;
69  minusI(2, 2) = -1;
70 
71  //do the computation and set the internal variables
72  unsigned int currentRow = 0;
73 
74 
75  for (size_t i = 0; i < _CheckedTransforms.size(); ++i)
76  {
77  t = _CheckedTransforms.at(i)->GetPosition().GetVnlVector();// t = the current position of the tracked sensor
78  t *= -1;
79  b.update(t, currentRow); //b = combines the position for each collected transform in one column vector
80  R = _CheckedTransforms.at(i)->GetOrientation().rotation_matrix_transpose().transpose(); // R = the current rotation of the tracked sensor, *rotation_matrix_transpose().transpose() is used to obtain original matrix
81  A.update(R, currentRow, 0); //A = the matrix which stores the rotations for each collected transform and -I
82  A.update(minusI, currentRow, 3);
83  currentRow += 3;
84  }
85  vnl_svd<double> svdA(A); //The singular value decomposition of matrix A
86  svdA.zero_out_absolute(defaultThreshold);
87 
88  //there is a solution only if rank(A)=6 (columns are linearly
89  //independent)
90  if (svdA.rank() < 6)
91  {
92  MITK_WARN << "svdA.rank() < 6";
93  return false;
94  }
95  else
96  {
97  x = svdA.solve(b); //x = the resulting pivot point
98 
99  m_ResultRMSError = (A * x - b).rms(); //the root mean sqaure error of the computation
100 
101  //sets the Pivot Point
102  m_ResultPivotPoint[0] = x[0];
103  m_ResultPivotPoint[1] = x[1];
104  m_ResultPivotPoint[2] = x[2];
105 
106 
107  }
108  return true;
109 
110 }
Navigation Data.
STL namespace.
DataCollection - Class to facilitate loading/accessing structured data.
std::vector< mitk::NavigationData::Pointer > m_NavigationDatas
void AddNavigationData(mitk::NavigationData::Pointer data)
#define MITK_WARN
Definition: mitkLogMacros.h:19
bool ComputePivotResult()
Computes the pivot point and rotation/axis on the given navigation datas. You can get the results aft...