Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkTrackingDeviceSource.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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
19 #include "mitkTrackingDevice.h"
20 #include "mitkTrackingTool.h"
21 
22 #include "mitkIGTTimeStamp.h"
23 #include "mitkIGTException.h"
24 
26  : mitk::NavigationDataSource(), m_TrackingDevice(NULL)
27 {
28 }
29 
31 {
32  if (m_TrackingDevice.IsNotNull())
33  {
34  if (m_TrackingDevice->GetState() == mitk::TrackingDevice::Tracking)
35  {
36  this->StopTracking();
37  }
38  if (m_TrackingDevice->GetState() == mitk::TrackingDevice::Ready)
39  {
40  this->Disconnect();
41  }
42  m_TrackingDevice = NULL;
43  }
44 }
45 
47 {
48  if (m_IsFrozen) {return;} //no update at all if device is frozen
49  else if (m_TrackingDevice.IsNull()) {return;}
50 
51  if (m_TrackingDevice->GetToolCount() < 1)
52  return;
53 
54  if (this->GetNumberOfIndexedOutputs() != m_TrackingDevice->GetToolCount()) // mismatch between tools and outputs. What should we do? Were tools added to the tracking device after SetTrackingDevice() was called?
55  {
56  //check this: TODO:
58  //this->CreateOutputs();
59 
60  std::stringstream ss;
61  ss << "mitk::TrackingDeviceSource: not enough outputs available for all tools. "
62  << this->GetNumberOfOutputs() << " outputs available, but "
63  << m_TrackingDevice->GetToolCount() << " tools available in the tracking device.";
64  throw std::out_of_range(ss.str());
65  }
66  /* update outputs with tracking data from tools */
67  unsigned int toolCount = m_TrackingDevice->GetToolCount();
68  for (unsigned int i = 0; i < toolCount; ++i)
69  {
70  mitk::NavigationData* nd = this->GetOutput(i);
71  assert(nd);
72  mitk::TrackingTool* t = m_TrackingDevice->GetTool(i);
73  assert(t);
74 
75  if ((t->IsEnabled() == false) || (t->IsDataValid() == false))
76  {
77  nd->SetDataValid(false);
78  continue;
79  }
80  nd->SetDataValid(true);
82  t->GetPosition(p);
83  nd->SetPosition(p);
84 
86  t->GetOrientation(o);
87  nd->SetOrientation(o);
91 
92  //for backward compatibility: check if the timestamp was set, if not create a default timestamp
94  }
95 }
96 
98 {
99  MITK_DEBUG << "Setting TrackingDevice to " << td;
100  if (this->m_TrackingDevice.GetPointer() != td)
101  {
102  this->m_TrackingDevice = td;
103  this->CreateOutputs();
104  std::stringstream name; // create a human readable name for the source
105  name << td->GetData().Model << " Tracking Source";
106  this->SetName(name.str());
107  }
108 }
109 
111  //if outputs are set then delete them
112  if (this->GetNumberOfOutputs() > 0)
113  {
114  for (int numOP = this->GetNumberOfOutputs() -1; numOP >= 0; numOP--)
115  this->RemoveOutput(numOP);
116  this->Modified();
117  }
118 
119  //fill the outputs if a valid tracking device is set
120  if (m_TrackingDevice.IsNull())
121  return;
122 
123  this->SetNumberOfIndexedOutputs(m_TrackingDevice->GetToolCount()); // create outputs for all tools
124  unsigned int numberOfOutputs = this->GetNumberOfIndexedOutputs();
125  MITK_DEBUG << "Number of tools at start of method CreateOutputs(): " << m_TrackingDevice->GetToolCount();
126  MITK_DEBUG << "Number of outputs at start of method CreateOutputs(): " << numberOfOutputs;
127  for (unsigned int idx = 0; idx < m_TrackingDevice->GetToolCount(); ++idx)
128  {
129  if (this->GetOutput(idx) == NULL)
130  {
131  DataObjectPointer newOutput = this->MakeOutput(idx);
132  static_cast<mitk::NavigationData*>(newOutput.GetPointer())->SetName(m_TrackingDevice->GetTool(idx)->GetToolName()); // set NavigationData name to ToolName
133  this->SetNthOutput(idx, newOutput);
134  this->Modified();
135  }
136  }
137 }
138 
140 {
141  if (m_TrackingDevice.IsNull())
142  throw std::invalid_argument("mitk::TrackingDeviceSource: No tracking device set");
143  if (this->IsConnected())
144  return;
145  try {m_TrackingDevice->OpenConnection();}
146  catch (mitk::IGTException &e)
147  {
148  throw std::runtime_error(std::string("mitk::TrackingDeviceSource: Could not open connection to tracking device. Error: ") + e.GetDescription());
149  }
150 
151  /* NDI Aurora needs a connection to discover tools that are connected to it.
152  Therefore we need to create outputs for these tools now */
153  //if (m_TrackingDevice->GetType() == mitk::NDIAurora)
154  //this->CreateOutputs();
155 }
156 
158 {
159  if (m_TrackingDevice.IsNull())
160  throw std::invalid_argument("mitk::TrackingDeviceSource: No tracking device set");
161  if (m_TrackingDevice->GetState() == mitk::TrackingDevice::Tracking)
162  return;
163  if (m_TrackingDevice->StartTracking() == false)
164  throw std::runtime_error("mitk::TrackingDeviceSource: Could not start tracking");
165 }
166 
168 {
169  if (m_TrackingDevice.IsNull())
170  throw std::invalid_argument("mitk::TrackingDeviceSource: No tracking device set");
171  if (m_TrackingDevice->CloseConnection() == false)
172  throw std::runtime_error("mitk::TrackingDeviceSource: Could not close connection to tracking device");
173 }
174 
176 {
177  if (m_TrackingDevice.IsNull())
178  throw std::invalid_argument("mitk::TrackingDeviceSource: No tracking device set");
179  if (m_TrackingDevice->StopTracking() == false)
180  throw std::runtime_error("mitk::TrackingDeviceSource: Could not stop tracking");
181 }
182 
184 {
185  if(this->GetTrackingDevice()->GetToolCount() != this->GetNumberOfIndexedOutputs())
186  this->CreateOutputs();
187 
188  this->Modified(); // make sure that we need to be updated
189  Superclass::UpdateOutputInformation();
190 }
191 
192 //unsigned int mitk::TrackingDeviceSource::GetToolCount()
193 //{
194 // if (m_TrackingDevice)
195 // return m_TrackingDevice->GetToolCount();
196 // return 0;
197 //}
198 
200 {
201  if (m_TrackingDevice.IsNull())
202  return false;
203 
204  return (m_TrackingDevice->GetState() == mitk::TrackingDevice::Ready) || (m_TrackingDevice->GetState() == mitk::TrackingDevice::Tracking);
205 }
206 
208 {
209  if (m_TrackingDevice.IsNull())
210  return false;
211 
212  return m_TrackingDevice->GetState() == mitk::TrackingDevice::Tracking;
213 }
virtual void GenerateData() override
filter execute method
Interface for all Tracking Tools.
void SetOrientationAccuracy(mitk::ScalarType error)
virtual float GetTrackingError() const =0
returns one value that corresponds to the overall tracking error.
virtual void SetTrackingDevice(mitk::TrackingDevice *td)
sets the tracking device that will be used as a source for tracking data
virtual void GetOrientation(Quaternion &orientation) const =0
returns the current orientation of the tool as a quaternion in a mitk::Point4D (in the tracking devic...
Navigation Data.
double GetElapsed()
returns the time elapsed since calling Start() for the first time in milliseconds ...
An object of this class represents an exception of the MITK-IGT module.
#define MITK_DEBUG
Definition: mitkLogMacros.h:26
void CreateOutputs()
Create the necessary outputs for the TrackingTool objects in m_TrackingDevice.
DataCollection - Class to facilitate loading/accessing structured data.
virtual bool IsEnabled() const =0
returns whether the tool is enabled or disabled
virtual void SetDataValid(bool _arg)
sets the dataValid flag of the NavigationData object indicating if the object contains valid data ...
virtual void SetIGTTimeStamp(TimeStampType _arg)
sets the IGT timestamp of the NavigationData object
mitk::Quaternion OrientationType
Type that holds the orientation part of the tracking data.
void StartTracking()
starts tracking. This needs to be called before Update() or GetOutput()->Update(). If the device is already tracking the method does nothing.
virtual void SetOrientation(OrientationType _arg)
sets the orientation of the NavigationData object
Interface for all Tracking Devices.
virtual TimeStampType GetIGTTimeStamp() const
gets the IGT timestamp of the NavigationData object
static IGTTimeStamp * GetInstance()
returns a pointer to the current instance of mitkTimeStamp
virtual bool IsDataValid() const =0
returns true if the current position data is valid (no error during tracking, tracking error below th...
virtual bool IsConnected()
returns true if a connection to the tracking device is established
virtual bool IsTracking()
returns true if tracking is in progress
void Disconnect()
Closes the connection to the tracking device.
virtual void SetPosition(PositionType _arg)
sets the position of the NavigationData object
void Connect()
Establishes a connection to the tracking device. If there is already a connection the method does not...
virtual void GetPosition(Point3D &position) const =0
returns the current position of the tool as an array of three floats (in the tracking device coordina...
virtual void UpdateOutputInformation() override
Used for pipeline update.
virtual double GetIGTTimeStamp() const
Sets the IGT timestamp of the tracking tool object (time in milliseconds)
void SetPositionAccuracy(mitk::ScalarType error)