MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkTrackingDeviceSource.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found 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
26
28{
29 if (m_TrackingDevice.IsNotNull())
30 {
31 if (m_TrackingDevice->GetState() == mitk::TrackingDevice::Tracking)
32 {
33 this->StopTracking();
34 }
35 if (m_TrackingDevice->GetState() == mitk::TrackingDevice::Ready)
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);
87 nd->SetIGTTimeStamp(t->GetIGTTimeStamp());
88
89 //for backward compatibility: check if the timestamp was set, if not create a default timestamp
90 if (nd->GetIGTTimeStamp()==0) nd->SetIGTTimeStamp(mitk::IGTTimeStamp::GetInstance()->GetElapsed());
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");
160 if (m_TrackingDevice->GetState() == mitk::TrackingDevice::Tracking)
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
203 return (m_TrackingDevice->GetState() == mitk::TrackingDevice::Ready) || (m_TrackingDevice->GetState() == mitk::TrackingDevice::Tracking);
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.
An object of this class represents an exception of the MITK-IGT module which are releated to the hard...
static IGTTimeStamp * GetInstance()
returns a pointer to the current instance of mitkTimeStamp
void SetPositionAccuracy(mitk::ScalarType error)
void SetOrientationAccuracy(mitk::ScalarType error)
mitk::Quaternion OrientationType
Type that holds the orientation part of the tracking data.
mitk::Point3D PositionType
Type that holds the position part of the tracking data.
void StartTracking()
starts tracking. This needs to be called before Update() or GetOutput()->Update()....
void CreateOutputs()
Create the necessary outputs for the TrackingTool objects in m_TrackingDevice.
void UpdateOutputInformation() override
Used for pipeline update.
virtual bool IsTracking()
returns true if tracking is in progress
virtual void SetTrackingDevice(mitk::TrackingDevice *td)
sets the tracking device that will be used as a source for tracking data
void GenerateData() override
filter execute method
void Connect()
Establishes a connection to the tracking device. If there is already a connection the method does not...
void Disconnect()
Closes the connection to the tracking device.
virtual bool IsConnected()
returns true if a connection to the tracking device is established
Interface for all Tracking Devices.
TrackingDeviceData GetData() const
return device data
Interface for all Tracking Tools.
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 GetOrientation(Quaternion &orientation) const
returns the current orientation of the tool as a quaternion in a mitk::Point4D (in the tracking devic...
virtual bool IsEnabled() const
returns whether the tool is enabled or disabled
virtual float GetTrackingError() const
returns one value that corresponds to the overall tracking error.
virtual bool IsDataValid() const
returns true if the current position data is valid (no error during tracking, tracking error below th...
IGT Exceptions.