Medical Imaging Interaction Toolkit  2018.4.99-08619e4f
Medical Imaging Interaction Toolkit
mitkTrackingDevice.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 "mitkTrackingDevice.h"
14 #include "mitkIGTTimeStamp.h"
15 #include "mitkTrackingTool.h"
16 
17 #include <itkMutexLockHolder.h>
18 
19 #include <usModuleContext.h>
20 #include <usGetModuleContext.h>
21 
24 
26 
27 
29  m_State(mitk::TrackingDevice::Setup),
30  m_Data(mitk::UnspecifiedTrackingTypeInformation::GetDeviceDataUnspecified()),
31  m_StopTracking(false),
32  m_RotationMode(mitk::TrackingDevice::RotationStandard)
33 
34 {
35  m_StopTrackingMutex = itk::FastMutexLock::New();
36  m_StateMutex = itk::FastMutexLock::New();
37  m_TrackingFinishedMutex = itk::FastMutexLock::New();
38 }
39 
40 
42 {
43 }
44 
46 {
47 return true;
48 //this is the default for all tracking device
49 //If a device needs installation please reimplement
50 //this method in the subclass.
51 }
52 
54 {
55  return false;
56 }
57 
59 {
60  return true;
61 }
62 
63 mitk::NavigationToolStorage::Pointer mitk::TrackingDevice::AutoDetectTools()
64 {
66 }
67 
68 
70 {
72  return m_State;
73 }
74 
75 
77 {
78  itkDebugMacro("setting m_State to " << state);
79 
80  MutexLockHolder lock(*m_StateMutex); // lock and unlock the mutex
81  if (m_State == state)
82  {
83  return;
84  }
85  m_State = state;
86  this->Modified();
87 }
88 
90 {
91  MITK_WARN << "Rotation mode switching is not implemented for this device. Leaving it at mitk::TrackingDevice::RotationStandard";
92 }
93 
95  return m_Data.Line;
96 }
97 
99 
100  us::ModuleContext* context = us::GetModuleContext();
101 
102  std::vector<us::ServiceReference<mitk::TrackingDeviceTypeCollection> > refs = context->GetServiceReferences<mitk::TrackingDeviceTypeCollection>();
103  if (refs.empty())
104  {
105  MITK_ERROR << "No tracking device service found!";
106  }
107  mitk::TrackingDeviceTypeCollection* deviceTypeCollection = context->GetService<mitk::TrackingDeviceTypeCollection>(refs.front());
108 
109  m_Data = deviceTypeCollection->GetFirstCompatibleDeviceDataForLine(deviceType);
110 }
111 
113  return m_Data;
114 }
115 
116 
118  m_Data = data;
119 }
120 
121 
123 {
124  if (this->GetState() == Tracking) // Only if the object is in the correct state
125  {
126  m_StopTrackingMutex->Lock(); // m_StopTracking is used by two threads, so we have to ensure correct thread handling
127  m_StopTracking = true;
128  m_StopTrackingMutex->Unlock();
129  //we have to wait here that the other thread recognizes the STOP-command and executes it
130  m_TrackingFinishedMutex->Lock();
131  mitk::IGTTimeStamp::GetInstance()->Stop(this); // notify realtime clock
132  // StopTracking was called, thus the mode should be changed back
133  // to Ready now that the tracking loop has ended.
134  this->SetState(Ready);
135  m_TrackingFinishedMutex->Unlock();
136  }
137  return true;
138 }
139 
140 
142 {
143  unsigned int toolCount = this->GetToolCount();
144  for (unsigned int i = 0; i < toolCount; ++i)
145  if (name == this->GetTool(i)->GetToolName())
146  return this->GetTool(i);
147  return nullptr;
148 }
149 
151 {
152  return this->GetData().Line;
153 }
virtual unsigned int GetToolCount() const =0
Returns number of tracking tools.
Interface for all Tracking Tools.
itk::FastMutexLock::Pointer m_TrackingFinishedMutex
mutex to manage control flow of StopTracking()
itk::MutexLockHolder< itk::FastMutexLock > MutexLockHolder
#define MITK_ERROR
Definition: mitkLogMacros.h:20
TrackingDeviceData GetData() const
return device data
virtual bool AutoDetectToolsAvailable()
DataCollection - Class to facilitate loading/accessing structured data.
This class is a collection for information of all Tracking Device Types (derived from abstract Tracki...
TrackingDeviceType Line
std::string GetTrackingDeviceName()
Convenient Method to get the Name of the Tracking Device. This is identical with GetData().Line and can be used to compare with TrackingDeviceTypeInformation::GetTrackingDeviceName() to check if you have a specific device.
TrackingDeviceState GetState() const
return current object state (Setup, Ready or Tracking)
virtual void SetRotationMode(RotationMode r)
virtual bool AddSingleToolIsAvailable()
#define MITK_WARN
Definition: mitkLogMacros.h:19
itk::FastMutexLock::Pointer m_StateMutex
mutex to control access to m_State
TrackingDeviceData GetFirstCompatibleDeviceDataForLine(TrackingDeviceType type)
virtual bool StopTracking()
stop retrieving tracking data from the device. stop retrieving tracking data from the device...
bool m_StopTracking
signal stop to tracking thread
Interface for all Tracking Devices.
static IGTTimeStamp * GetInstance()
returns a pointer to the current instance of mitkTimeStamp
static Pointer New()
Constructs a NavigationToolStorage without reference to a DataStorage. The Data Nodes of tools have t...
virtual bool IsDeviceInstalled()
TrackingDeviceData m_Data
current device Data
std::string TrackingDeviceType
Type information for unspecified or invalid tracking devices. This is often used as default or for te...
itk::FastMutexLock::Pointer m_StopTrackingMutex
mutex to control access to m_StopTracking
void Stop(itk::Object::Pointer device)
stops the time-acqusition
void SetData(TrackingDeviceData data)
set device type
virtual TrackingTool * GetTool(unsigned int toolNumber) const =0
Return tool with index toolNumber.
static void Setup()
virtual mitk::NavigationToolStorage::Pointer AutoDetectTools()
void SetState(TrackingDeviceState state)
change object state
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
TrackingDeviceType GetType() const
Deprecated! Use the more specific getData or GetTrackingDeviceName instead. return device type identi...
virtual mitk::TrackingTool * GetToolByName(std::string name) const
Returns the tool with the given tool name.
void SetType(TrackingDeviceType type)
Deprecated! Use the more specific setDeviceData instead. set device type.