Medical Imaging Interaction Toolkit  2018.4.99-07c45cb1
Medical Imaging Interaction Toolkit
mitkTrackingDeviceSourceConfigurator.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 "mitkNDITrackingDevice.h"
20 
21 #include <mitkIGTException.h>
22 #include <mitkIGTHardwareException.h>
23 
24 #include <usGetModuleContext.h>
25 #include <usModule.h>
26 #include <usServiceProperties.h>
27 #include <usModuleContext.h>
29 
30 
31 mitk::TrackingDeviceSourceConfigurator::TrackingDeviceSourceConfigurator(mitk::NavigationToolStorage::Pointer NavigationTools, mitk::TrackingDevice::Pointer TrackingDevice)
32 {
33 //make a copy of the navigation tool storage because we will modify the storage
34 if (NavigationTools.IsNotNull())
35  {
37  for (unsigned int i=0; i<NavigationTools->GetToolCount(); i++)
38  {
39  m_NavigationTools->AddTool(NavigationTools->GetTool(i));
40  }
41  }
42 
43 m_TrackingDevice = TrackingDevice;
44 m_ToolCorrespondencesInToolStorage = std::vector<int>();
45 m_ErrorMessage = "";
46 }
47 
49 {
50 return m_NavigationTools;
51 }
52 
53 
55 {
56 }
57 
59 {
60 if (m_NavigationTools.IsNull())
61  {
62  m_ErrorMessage = "NavigationToolStorage is nullptr!";
63  return false;
64  }
65 else if (m_TrackingDevice.IsNull())
66  {
67  m_ErrorMessage = "TrackingDevice is nullptr!";
68  return false;
69  }
70 else
71  {
72  for (unsigned int i=0; i<m_NavigationTools->GetToolCount(); i++)
73  {
74  if (m_NavigationTools->GetTool(i)->GetTrackingDeviceType() != m_TrackingDevice->GetType())
75  {
76  m_ErrorMessage = "At least one tool is not of the same type like the tracking device.";
77  return false;
78  }
79  }
80  //TODO in case of Aurora: check if the tools are automatically detected by comparing the serial number
81  return true;
82  }
83 }
84 
86 {
87 mitk::NavigationDataObjectVisualizationFilter::Pointer dummy; //this dummy is lost directly after creating the device
88 return this->CreateTrackingDeviceSource(dummy);
89 }
90 
91 mitk::TrackingDeviceSource::Pointer mitk::TrackingDeviceSourceConfigurator::CreateTrackingDeviceSource(mitk::NavigationDataObjectVisualizationFilter::Pointer &visualizationFilter)
92 {
93  if (!this->IsCreateTrackingDeviceSourcePossible()) {MITK_WARN << "Cannot create tracking decive: " << m_ErrorMessage; return nullptr;}
94 
95  mitk::TrackingDeviceSource::Pointer returnValue;
96 
97  us::ModuleContext* context = us::GetModuleContext();
98 
99  std::vector<us::ServiceReference<mitk::TrackingDeviceTypeCollection> > refs = context->GetServiceReferences<mitk::TrackingDeviceTypeCollection>();
100 
101  if (refs.empty())
102  {
103  MITK_ERROR << "No tracking device service found!";
104  }
105 
106  mitk::TrackingDeviceTypeCollection* deviceTypeCollection = context->GetService<mitk::TrackingDeviceTypeCollection>(refs.front());
107 
108  //create tracking device source
109  returnValue = deviceTypeCollection->GetTrackingDeviceTypeInformation(m_TrackingDevice->GetType())->
111 
112  //TODO: insert other tracking systems?
113  if (returnValue.IsNull()) {MITK_WARN << "Cannot create tracking decive: " << m_ErrorMessage; return nullptr;}
114 
115  //create visualization filter
116  visualizationFilter = CreateNavigationDataObjectVisualizationFilter(returnValue,m_NavigationTools);
117  if (visualizationFilter.IsNull()) {MITK_WARN << "Cannot create tracking decive: " << m_ErrorMessage; return nullptr;}
118 
119  return returnValue;
120 }
121 
123 {
124  return this->m_ErrorMessage;
125 }
126 
127 //############################ internal help methods ########################################
128 
129 mitk::NavigationDataObjectVisualizationFilter::Pointer mitk::TrackingDeviceSourceConfigurator::CreateNavigationDataObjectVisualizationFilter(mitk::TrackingDeviceSource::Pointer trackingDeviceSource, mitk::NavigationToolStorage::Pointer navigationTools)
130  {
131  mitk::NavigationDataObjectVisualizationFilter::Pointer returnValue = mitk::NavigationDataObjectVisualizationFilter::New();
132  for (unsigned int i=0; i<trackingDeviceSource->GetNumberOfIndexedOutputs(); i++)
133  {
134  // Note: If all tools have the same name only the first tool will always be returned and
135  // the others won't be updated during rendering.This could potentially lead to inconstencies
136  mitk::NavigationTool::Pointer currentTool = navigationTools->GetToolByName(trackingDeviceSource->GetOutput(i)->GetName());
137  if (currentTool.IsNull())
138  {
139  this->m_ErrorMessage = "Error: did not find corresponding tool in tracking device after initialization.";
140  return nullptr;
141  }
142  returnValue->SetInput(i,trackingDeviceSource->GetOutput(i));
143  returnValue->SetRepresentationObject(i,currentTool->GetDataNode()->GetData());
144  }
145  return returnValue;
146  }
147 
149  {
150  if (outputID < m_ToolCorrespondencesInToolStorage.size()) return m_ToolCorrespondencesInToolStorage.at(outputID);
151  else return -1;
152  }
153 
155  {
156  if (outputID < m_ToolCorrespondencesInToolStorage.size()) return m_NavigationTools->GetTool(m_ToolCorrespondencesInToolStorage.at(outputID))->GetIdentifier();
157  else return "";
158  }
159 
161  {
163  }
164 
166  {
167  std::vector<std::string> returnValue = std::vector<std::string>();
168  for (unsigned int i=0; i<m_ToolCorrespondencesInToolStorage.size(); i++)
169  {returnValue.push_back(m_NavigationTools->GetTool(m_ToolCorrespondencesInToolStorage.at(i))->GetIdentifier());}
170  return returnValue;
171  }
mitk::TrackingDeviceSource::Pointer CreateTrackingDeviceSource()
#define MITK_ERROR
Definition: mitkLogMacros.h:20
std::string GetToolIdentifierInToolStorage(unsigned int outputID)
This class is a collection for information of all Tracking Device Types (derived from abstract Tracki...
mitk::NavigationToolStorage::Pointer GetUpdatedNavigationToolStorage()
TrackingDeviceTypeInformation * GetTrackingDeviceTypeInformation(TrackingDeviceType type)
#define MITK_WARN
Definition: mitkLogMacros.h:19
TrackingDeviceSourceConfigurator(mitk::NavigationToolStorage::Pointer NavigationTools, mitk::TrackingDevice::Pointer TrackingDevice)
mitk::NavigationDataObjectVisualizationFilter::Pointer CreateNavigationDataObjectVisualizationFilter(mitk::TrackingDeviceSource::Pointer trackingDeviceSource, mitk::NavigationToolStorage::Pointer navigationTools)
Interface for all Tracking Devices.
static Pointer New()
Constructs a NavigationToolStorage without reference to a DataStorage. The Data Nodes of tools have t...
mitk::NavigationToolStorage::Pointer m_NavigationTools
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.