Medical Imaging Interaction Toolkit  2018.4.99-a3d2e8fb
Medical Imaging Interaction Toolkit
QmitkPolhemusTrackerWidget.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 
14 
15 #include <QFileDialog>
16 #include <QScrollBar>
17 #include <QMessageBox>
18 
19 #include <itksys/SystemTools.hxx>
20 #include <Poco/Path.h>
21 #include <QSettings>
22 
23 #include <mitkBaseRenderer.h>
24 #include <mitkCameraController.h>
25 #include "vtkRenderer.h"
26 #include "vtkCamera.h"
27 
28 const std::string QmitkPolhemusTrackerWidget::VIEW_ID = "org.mitk.views.PolhemusTrackerWidget";
29 
30 QmitkPolhemusTrackerWidget::QmitkPolhemusTrackerWidget(QWidget* parent, Qt::WindowFlags f)
32  , m_Controls(nullptr)
33 {
34 }
36 {
38  CreateQtPartControl(this);
39 
40  SetAdvancedSettingsEnabled(false);
41  on_m_AdvancedSettings_clicked(); //hide advanced settings on setup
42 }
43 
45 {
46  delete m_Controls;
47 }
48 
49 void QmitkPolhemusTrackerWidget::CreateQtPartControl(QWidget *parent)
50 {
51  if (!m_Controls)
52  {
53  // create GUI widgets
54  m_Controls = new Ui::QmitkPolhemusTrackerWidget;
55  m_Controls->setupUi(parent);
56  }
57 }
58 
60 {
61  this->m_TrackingDevice = nullptr;
62  MITK_DEBUG<<"Resetting Polhemus Tracking Device, because tool storage changed.";
63 }
64 
65 void QmitkPolhemusTrackerWidget::CreateConnections()
66 {
67  if (m_Controls)
68  {
69  connect((QObject*)(m_Controls->m_hemisphereTracking), SIGNAL(clicked()), this, SLOT(on_m_hemisphereTracking_clicked()));
70  connect((QObject*)(m_Controls->m_ToggleHemisphere), SIGNAL(clicked()), this, SLOT(on_m_ToggleHemisphere_clicked()));
71  connect((QObject*)(m_Controls->m_SetHemisphere), SIGNAL(clicked()), this, SLOT(on_m_SetHemisphere_clicked()));
72  connect((QObject*)(m_Controls->m_GetHemisphere), SIGNAL(clicked()), this, SLOT(on_m_GetHemisphere_clicked()));
73  connect((QObject*)(m_Controls->m_AdjustHemisphere), SIGNAL(clicked()), this, SLOT(on_m_AdjustHemisphere_clicked()));
74  connect((QObject*)(m_Controls->m_AdvancedSettings), SIGNAL(clicked()), this, SLOT(on_m_AdvancedSettings_clicked()));
75  connect((QObject*)(m_Controls->m_ToggleToolTipCalibration), SIGNAL(clicked()), this, SLOT(on_m_ToggleToolTipCalibration_clicked()));
76  }
77 }
78 
79 mitk::TrackingDevice::Pointer QmitkPolhemusTrackerWidget::GetTrackingDevice()
80 {
81  if (m_TrackingDevice.IsNull())
82  {
84  m_TrackingDevice->SetHemisphereTrackingEnabled(m_Controls->m_hemisphereTracking->isChecked());
85  }
86 
87  return static_cast<mitk::TrackingDevice::Pointer>(m_TrackingDevice);
88 }
89 
91 {
92  QmitkPolhemusTrackerWidget* clonedWidget = new QmitkPolhemusTrackerWidget(parent);
93  clonedWidget->Initialize();
94  return clonedWidget;
95 }
96 
98 {
99  m_TrackingDevice->SetHemisphereTrackingEnabled(m_Controls->m_hemisphereTracking->isChecked());
100 }
101 
103 {
104  // Index 0 == All Tools == -1 for Polhemus interface; Index 2 == Tool 2 == 1 for Polhemus; etc...
105  m_TrackingDevice->ToggleHemisphere(GetSelectedToolIndex());
106 
107  MITK_INFO << "Toggle Hemisphere for tool " << m_Controls->m_ToolSelection->currentText().toStdString();
108 }
109 
111 {
112  mitk::Vector3D _hemisphere;
113  mitk::FillVector3D(_hemisphere, m_Controls->m_Hemisphere_X->value(), m_Controls->m_Hemisphere_Y->value(), m_Controls->m_Hemisphere_Z->value());
114  m_TrackingDevice->SetHemisphere(GetSelectedToolIndex(), _hemisphere);
115 
116  //If you set a hemisphere vector which is unequal (0|0|0), this means, that there is no hemisphere tracking any more
117  //disable the checkbox in case it was on before, so that it can be reactivated...
118  if (_hemisphere.GetNorm() != 0)
119  m_Controls->m_hemisphereTracking->setChecked(false);
120 
121  MITK_INFO << "Hemisphere set for tool " << m_Controls->m_ToolSelection->currentText().toStdString();
122 }
123 
125 {
126  mitk::Vector3D _hemisphere = m_TrackingDevice->GetHemisphere(GetSelectedToolIndex());
127  m_Controls->m_Hemisphere_X->setValue(_hemisphere[0]);
128  m_Controls->m_Hemisphere_Y->setValue(_hemisphere[1]);
129  m_Controls->m_Hemisphere_Z->setValue(_hemisphere[2]);
130 
131  QString label;
132 
133  if (m_TrackingDevice->GetHemisphereTrackingEnabled(GetSelectedToolIndex()))
134  {
135  label = "HemisphereTracking is ON for tool ";
136  label.append(m_Controls->m_ToolSelection->currentText());
137  }
138  else if (GetSelectedToolIndex() == -1)
139  {
140  label = "HemisphereTracking is OFF for at least one tool.";
141  }
142  else
143  {
144  label = "HemisphereTracking is OFF for tool ";
145  label.append(m_Controls->m_ToolSelection->currentText());
146  }
147 
148  m_Controls->m_StatusLabelHemisphereTracking->setText(label);
149 
150  MITK_INFO << "Updated SpinBox for Hemisphere of tool " << m_Controls->m_ToolSelection->currentText().toStdString();
151 }
152 
154 {
155  int _tool = GetSelectedToolIndex();
156  QMessageBox msgBox;
157  QString _text;
158  if (_tool == -1)
159  {
160  _text.append("Adjusting hemisphere for all tools.");
161  msgBox.setText(_text);
162  _text.clear();
163  _text = tr("Please make sure, that the entire tools (including tool tip AND sensor) are placed in the positive x hemisphere. Press 'Adjust hemisphere' if you are ready.");
164  msgBox.setInformativeText(_text);
165  }
166  else
167  {
168  _text.append("Adjusting hemisphere for tool '");
169  _text.append(m_Controls->m_ToolSelection->currentText());
170  _text.append(tr("' at port %2.").arg(_tool));
171  msgBox.setText(_text);
172  _text.clear();
173  _text = tr("Please make sure, that the entire tool (including tool tip AND sensor) is placed in the positive x hemisphere. Press 'Adjust hemisphere' if you are ready.");
174  msgBox.setInformativeText(_text);
175  }
176 
177  QPushButton *adjustButton = msgBox.addButton(tr("Adjust hemisphere"), QMessageBox::ActionRole);
178  QPushButton *cancelButton = msgBox.addButton(QMessageBox::Cancel);
179  msgBox.exec();
180  if (msgBox.clickedButton() == adjustButton) {
181  // adjust
182  m_TrackingDevice->AdjustHemisphere(_tool);
183  MITK_INFO << "Adjusting Hemisphere for tool " << m_Controls->m_ToolSelection->currentText().toStdString();
184  }
185  else if (msgBox.clickedButton() == cancelButton) {
186  // abort
187  MITK_INFO << "Cancel 'Adjust hemisphere'. No harm done...";
188  }
189 }
190 
192 {
193  if (m_Controls->m_ToolSelection->currentIndex() != 0)
194  {
195  mitk::PolhemusTool* _tool = dynamic_cast<mitk::PolhemusTool*> (this->m_TrackingDevice->GetToolByName(m_Controls->m_ToolSelection->currentText().toStdString()));
196  mitk::Point3D tip = _tool->GetToolTipPosition().GetVectorFromOrigin()*(-1.);
197  mitk::Quaternion quat = _tool->GetToolAxisOrientation().inverse();
198  _tool->SetToolTipPosition(tip, quat);
199  }
200  else
201  {
202  for (int i = 0; i < m_TrackingDevice->GetToolCount(); ++i)
203  {
204  mitk::PolhemusTool* _tool = dynamic_cast<mitk::PolhemusTool*> (this->m_TrackingDevice->GetTool(i));
205  mitk::Point3D tip = _tool->GetToolTipPosition().GetVectorFromOrigin()*(-1.);
206  mitk::Quaternion quat = _tool->GetToolAxisOrientation().inverse();
207  _tool->SetToolTipPosition(tip, quat);
208  }
209  }
210 }
211 
213 {
214  if (!_success)
215  {
216  this->m_TrackingDevice = nullptr;
217  return;
218  }
219 
220  SetAdvancedSettingsEnabled(true);
221 
222  if (m_TrackingDevice->GetToolCount() != m_Controls->m_ToolSelection->count())
223  {
224  m_Controls->m_ToolSelection->clear();
225 
226  m_Controls->m_ToolSelection->addItem("All Tools");
227 
228  for (int i = 0; i < m_TrackingDevice->GetToolCount(); ++i)
229  {
230  m_Controls->m_ToolSelection->addItem(m_TrackingDevice->GetTool(i)->GetToolName());
231  }
232  }
233 }
234 
236 {
237  if (!_success)
238  return;
239  //Rotate mitk standard multi widget, so that the view matches the sensor. Positive x == right, y == front, z == down;
241  mitk::BaseRenderer::GetInstance(mitk::BaseRenderer::GetRenderWindowByName("stdmulti.widget3"))->GetVtkRenderer()->GetActiveCamera()->SetViewUp(0, 0, -1);
242 
243 }
244 
246 {
247  if (!_success)
248  return;
249  SetAdvancedSettingsEnabled(false);
250 }
251 
252 void QmitkPolhemusTrackerWidget::SetAdvancedSettingsEnabled(bool _enable)
253 {
254  m_Controls->m_ToolSelection->setEnabled(_enable);
255  m_Controls->label_toolsToChange->setEnabled(_enable);
256  m_Controls->label_UpdateOnRequest->setEnabled(_enable);
257  m_Controls->m_GetHemisphere->setEnabled(_enable);
258  m_Controls->m_Hemisphere_X->setEnabled(_enable);
259  m_Controls->m_Hemisphere_Y->setEnabled(_enable);
260  m_Controls->m_Hemisphere_Z->setEnabled(_enable);
261  m_Controls->m_SetHemisphere->setEnabled(_enable);
262  m_Controls->m_ToggleHemisphere->setEnabled(_enable);
263  m_Controls->m_AdjustHemisphere->setEnabled(_enable);
264  m_Controls->m_ToggleToolTipCalibration->setEnabled(_enable);
265 }
266 
268 {
269  bool _enable = m_Controls->m_AdvancedSettings->isChecked();
270  m_Controls->m_ToolSelection->setVisible(_enable);
271  m_Controls->label_toolsToChange->setVisible(_enable);
272  m_Controls->label_UpdateOnRequest->setVisible(_enable);
273  m_Controls->m_GetHemisphere->setVisible(_enable);
274  m_Controls->m_Hemisphere_X->setVisible(_enable);
275  m_Controls->m_Hemisphere_Y->setVisible(_enable);
276  m_Controls->m_Hemisphere_Z->setVisible(_enable);
277  m_Controls->m_SetHemisphere->setVisible(_enable);
278  m_Controls->m_ToggleHemisphere->setVisible(_enable);
279  m_Controls->m_AdjustHemisphere->setVisible(_enable);
280  m_Controls->m_ToggleToolTipCalibration->setVisible(_enable);
281  m_Controls->m_StatusLabelHemisphereTracking->setVisible(_enable);
282 }
283 
284 int QmitkPolhemusTrackerWidget::GetSelectedToolIndex()
285 {
286  // Index 0 == All Tools == -1 for Polhemus interface; Index 1 == Tool 1 == 1 for Polhemus Interface; etc...
287  int _index = m_Controls->m_ToolSelection->currentIndex() - 1;
288  if (_index != -1)
289  {
290  //we need to find the internal Polhemus index for this tool. This is stored in the identifier of a navigation tool or as Port in PolhemusTool.
291  mitk::PolhemusTool* _tool = dynamic_cast<mitk::PolhemusTool*>(m_TrackingDevice->GetToolByName(m_Controls->m_ToolSelection->currentText().toStdString()));
292  _index = _tool->GetToolPort();
293  }
294  return _index;
295 }
virtual void Initialize()
Subclass must implement this method to return a pointer to a copy of the object. Please don&#39;t forget ...
static BaseRenderer * GetInstance(vtkRenderWindow *renWin)
static vtkRenderWindow * GetRenderWindowByName(const std::string &name)
#define MITK_INFO
Definition: mitkLogMacros.h:18
virtual void OnDisconnected(bool _success)
This function is called, when in the TrackingToolboxView "Disconnect" was clicked and the device is s...
vtkRenderer * GetVtkRenderer() const
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
static const std::string VIEW_ID
virtual void OnConnected(bool _success)
This function is called, when in the TrackingToolboxView "Connect" was clicked and the device is succ...
void FillVector3D(Tout &out, mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
Definition: mitkArray.h:106
mitk::PolhemusTrackingDevice::Pointer m_TrackingDevice
Ui::QmitkPolhemusTrackerWidget * m_Controls
virtual void SetToolTipPosition(Point3D toolTipPosition, Quaternion orientation, ScalarType eps=0.0)
defines a tool tip for this tool in tool coordinates. GetPosition() and GetOrientation() return the d...
QmitkPolhemusTrackerWidget(QWidget *parent=0, Qt::WindowFlags f=0)
Quaternion GetToolAxisOrientation() const
returns the transformation of the tool axis with respect to the MITK-IGT main tool axis (0...
virtual CameraController * GetCameraController()
Implementation of a configuration widget for Polhemus Tracking Devices.
virtual mitk::TrackingDevice::Pointer GetTrackingDevice()
vnl_quaternion< ScalarType > Quaternion
Point3D GetToolTipPosition() const
returns the tool tip in tool coordinates, which where set by SetToolTip
virtual QmitkPolhemusTrackerWidget * Clone(QWidget *parent) const
Subclass must implement this method to return a pointer to a copy of the object. Please don&#39;t forget ...
An object of this class represents a tool of a Polhemus tracking device. A tool has to be added to a ...
virtual int GetToolPort()
Sets the port of the tool. (e.g. 1 for port "SENS 1" etc.)
virtual void OnToolStorageChanged()
This function is called, when anything in the ToolStorage changed, e.g. AddTool or EditTool...
PERSISTENCE_GET_SERVICE_METHOD_MACRO void InitializeSuperclassWidget()
Abstract class to configure a tracking device. Inherited widgets should be registered in the Microser...
virtual void OnStartTracking(bool _success)
This function is called, when in the TrackingToolboxView "Start Tracking" was clicked and the device ...