Medical Imaging Interaction Toolkit  2018.4.99-07c45cb1
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 (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 "mitkTrackingDevice.h"
16 #include "mitkTrackingTool.h"
17 
18 #include "mitkIGTTimeStamp.h"
19 #include "mitkIGTException.h"
20 #include "mitkIGTHardwareException.h"
21 
23  : mitk::NavigationDataSource(), m_TrackingDevice(nullptr)
24 {
25 }
26 
28 {
29  if (m_TrackingDevice.IsNotNull())
30  {
32  {
33  this->StopTracking();
34  }
36  {
37  this->Disconnect();
38  }
39  m_TrackingDevice = nullptr;
40  }
41 }
42 
44 {
45  if (m_IsFrozen) {return;} //no update at all if device is frozen
46  else if (m_TrackingDevice.IsNull()) {return;}
47 
48  if (m_TrackingDevice->GetToolCount() < 1)
49  return;
50 
51  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?
52  {
53  //check this: TODO:
55  //this->CreateOutputs();
56 
57  std::stringstream ss;
58  ss << "mitk::TrackingDeviceSource: not enough outputs available for all tools. "
59  << this->GetNumberOfOutputs() << " outputs available, but "
60  << m_TrackingDevice->GetToolCount() << " tools available in the tracking device.";
61  throw std::out_of_range(ss.str());
62  }
63  /* update outputs with tracking data from tools */
64  unsigned int toolCount = m_TrackingDevice->GetToolCount();
65  for (unsigned int i = 0; i < toolCount; ++i)
66  {
67  mitk::NavigationData* nd = this->GetOutput(i);
68  assert(nd);
69  mitk::TrackingTool* t = m_TrackingDevice->GetTool(i);
70  assert(t);
71 
72  if ((t->IsEnabled() == false) || (t->IsDataValid() == false))
73  {
74  nd->SetDataValid(false);
75  continue;
76  }
77  nd->SetDataValid(true);
79  t->GetPosition(p);
80  nd->SetPosition(p);
81 
83  t->GetOrientation(o);
84  nd->SetOrientation(o);
88 
89  //for backward compatibility: check if the timestamp was set, if not create a default timestamp
91  }
92 }
93 
95 {
96  MITK_DEBUG << "Setting TrackingDevice to " << td;
97  if (this->m_TrackingDevice.GetPointer() != td)
98  {
99  this->m_TrackingDevice = td;
100  this->CreateOutputs();
101  std::stringstream name; // create a human readable name for the source
102  name << td->GetData().Model << " Tracking Source";
103  this->SetName(name.str());
104  }
105 }
106 
108  //if outputs are set then delete them
109  if (this->GetNumberOfOutputs() > 0)
110  {
111  for (int numOP = this->GetNumberOfOutputs() -1; numOP >= 0; numOP--)
112  this->RemoveOutput(numOP);
113  this->Modified();
114  }
115 
116  //fill the outputs if a valid tracking device is set
117  if (m_TrackingDevice.IsNull())
118  return;
119 
120  this->SetNumberOfIndexedOutputs(m_TrackingDevice->GetToolCount()); // create outputs for all tools
121  unsigned int numberOfOutputs = this->GetNumberOfIndexedOutputs();
122  MITK_DEBUG << "Number of tools at start of method CreateOutputs(): " << m_TrackingDevice->GetToolCount();
123  MITK_DEBUG << "Number of outputs at start of method CreateOutputs(): " << numberOfOutputs;
124  for (unsigned int idx = 0; idx < m_TrackingDevice->GetToolCount(); ++idx)
125  {
126  if (this->GetOutput(idx) == nullptr)
127  {
128  DataObjectPointer newOutput = this->MakeOutput(idx);
129  static_cast<mitk::NavigationData*>(newOutput.GetPointer())->SetName(m_TrackingDevice->GetTool(idx)->GetToolName()); // set NavigationData name to ToolName
130  this->SetNthOutput(idx, newOutput);
131  this->Modified();
132  }
133  }
134 }
135 
137 {
138  if (m_TrackingDevice.IsNull())
139  throw std::invalid_argument("mitk::TrackingDeviceSource: No tracking device set");
140  if (this->IsConnected())
141  return;
142  try
143  {
144  //Try to open the connection. If it didn't work (fals is returned from OpenConnection by the tracking device), throw an exception.
145  if (!m_TrackingDevice->OpenConnection())
146  {
147  mitkThrowException(mitk::IGTHardwareException) << "Could not open connection.";
148  }
149  }
150  catch (mitk::IGTException &e)
151  {
152  throw std::runtime_error(std::string("mitk::TrackingDeviceSource: Could not open connection to tracking device. Error: ") + e.GetDescription());
153  }
154 }
155 
157 {
158  if (m_TrackingDevice.IsNull())
159  throw std::invalid_argument("mitk::TrackingDeviceSource: No tracking device set");
161  return;
162  if (m_TrackingDevice->StartTracking() == false)
163  throw std::runtime_error("mitk::TrackingDeviceSource: Could not start tracking");
164 }
165 
167 {
168  if (m_TrackingDevice.IsNull())
169  throw std::invalid_argument("mitk::TrackingDeviceSource: No tracking device set");
170  if (m_TrackingDevice->CloseConnection() == false)
171  throw std::runtime_error("mitk::TrackingDeviceSource: Could not close connection to tracking device");
172 }
173 
175 {
176  if (m_TrackingDevice.IsNull())
177  throw std::invalid_argument("mitk::TrackingDeviceSource: No tracking device set");
178  if (m_TrackingDevice->StopTracking() == false)
179  throw std::runtime_error("mitk::TrackingDeviceSource: Could not stop tracking");
180 }
181 
183 {
184  if(this->GetTrackingDevice()->GetToolCount() != this->GetNumberOfIndexedOutputs())
185  this->CreateOutputs();
186 
187  this->Modified(); // make sure that we need to be updated
188  Superclass::UpdateOutputInformation();
189 }
190 
191 //unsigned int mitk::TrackingDeviceSource::GetToolCount()
192 //{
193 // if (m_TrackingDevice)
194 // return m_TrackingDevice->GetToolCount();
195 // return 0;
196 //}
197 
199 {
200  if (m_TrackingDevice.IsNull())
201  return false;
202 
204 }
205 
207 {
208  if (m_TrackingDevice.IsNull())
209  return false;
210 
211  return m_TrackingDevice->GetState() == mitk::TrackingDevice::Tracking;
212 }
An object of this class represents an exception of the MITK-IGT module which are releated to the hard...
void GenerateData() override
filter execute method
Interface for all Tracking Tools.
void SetOrientationAccuracy(mitk::ScalarType error)
NavigationData * GetOutput(void)
return the output (output with id 0) of the filter
virtual void SetTrackingDevice(mitk::TrackingDevice *td)
sets the tracking device that will be used as a source for tracking data
Navigation Data.
virtual float GetTrackingError() const
returns one value that corresponds to the overall tracking error.
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:22
void CreateOutputs()
Create the necessary outputs for the TrackingTool objects in m_TrackingDevice.
DataCollection - Class to facilitate loading/accessing structured data.
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 in milliseconds
virtual const mitk::TrackingDevice * GetTrackingDevice()
returns the tracking device that is used by this filter
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.
mitk::TrackingDevice::Pointer m_TrackingDevice
the tracking device that is used as a source for this filter object
virtual TimeStampType GetIGTTimeStamp() const
gets the IGT timestamp of the NavigationData object in milliseconds Please note, that there is also t...
virtual void GetOrientation(Quaternion &orientation) const
returns the current orientation of the tool as a quaternion in a mitk::Point4D (in the tracking devic...
itk::DataObject::Pointer MakeOutput(DataObjectPointerArraySizeType idx) override
virtual void SetOrientation(OrientationType _arg)
sets the orientation of the NavigationData object
Interface for all Tracking Devices.
static IGTTimeStamp * GetInstance()
returns a pointer to the current instance of mitkTimeStamp
virtual bool IsConnected()
returns true if a connection to the tracking device is established
virtual bool IsDataValid() const
returns true if the current position data is valid (no error during tracking, tracking error below th...
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
#define mitkThrowException(classname)
void Connect()
Establishes a connection to the tracking device. If there is already a connection the method does not...
virtual bool IsEnabled() const
returns whether the tool is enabled or disabled
void UpdateOutputInformation() override
Used for pipeline update.
virtual void GetPosition(Point3D &position) const
returns the current position of the tool as an array of three floats (in the tracking device coordina...
virtual void SetName(std::string _arg)
Sets the human readable name of this source. There is also a default name, but you can use this metho...
void SetPositionAccuracy(mitk::ScalarType error)
virtual double GetIGTTimeStamp() const
Gets the IGT timestamp of the tracking tool object (time in milliseconds). Returns 0 if the timestamp...