15#include "mitkIGTConfig.h"
17#include "mitkIGTHardwareException.h"
18#include <itksys/SystemTools.hxx>
27 m_Device = mitk::PolhemusInterface::New();
42 for (
auto _tool : m_AllTools)
44 if (_tool->GetToolPort() == toolPort)
46 MITK_DEBUG <<
"There is already a tool connected to this port. Returning existing tool";
51 mitk::PolhemusTool::Pointer t = mitk::PolhemusTool::New();
52 t->SetToolName(toolName);
53 t->SetToolPort(toolPort);
54 if (this->InternalAddTool(t) ==
false)
56 return t.GetPointer();
61 m_AllTools.push_back(tool);
67 bool success = m_Device->StartTracking();
71 this->SetState(Tracking);
72 this->m_StopTrackingMutex.lock();
73 this->m_StopTracking =
false;
74 this->m_StopTrackingMutex.unlock();
75 if (m_Thread.joinable()) {m_Thread.detach();}
80 this->SetState(Ready);
88 m_Device->StopTracking();
89 return Superclass::StopTracking();
94 return (
unsigned int)this->m_AllTools.size();
99 if (toolNumber >= this->GetToolCount())
102 return this->m_AllTools[toolNumber];
108 if (m_Device.IsNull()) { m_Device = mitk::PolhemusInterface::New(); }
109 if (!m_Device->Connect())
111 MITK_ERROR <<
"Cannot connect Polhemus device!";
117 this->SetState(Ready);
120 std::vector<int> toolPorts = m_Device->GetToolPorts();
123 if (this->GetToolCount() != toolPorts.size())
125 MITK_ERROR <<
"Cannot connect device, number of tools in toolstorage doesn't match the number of tools connected to Polhemus device!";
131 for (
auto _tool : m_AllTools)
133 if (std::find(toolPorts.begin(), toolPorts.end(), _tool->GetToolPort()) == toolPorts.end())
135 MITK_ERROR <<
"Cannot connect device, tool " << _tool->GetToolPort() <<
" is not connected to its port.";
142 toolPorts.erase(std::find(toolPorts.begin(), toolPorts.end(), _tool->GetToolPort()));
146 m_Device->SetHemisphereTrackingEnabled(m_HemisphereTrackingEnabled);
153 bool returnValue =
true;
154 if (this->GetState() == Setup)
157 returnValue = m_Device->Disconnect();
159 this->SetState(Setup);
170 return this->m_AllTools;
178 std::lock_guard<std::mutex> trackingFinishedLockHolder(m_TrackingFinishedMutex);
180 bool localStopTracking;
181 this->m_StopTrackingMutex.lock();
182 localStopTracking = this->m_StopTracking;
183 this->m_StopTrackingMutex.unlock();
186 while ((this->GetState() == Tracking) && (localStopTracking ==
false))
188 std::vector<mitk::PolhemusInterface::trackingData> lastData = this->GetDevice()->GetLastFrame();
190 if (lastData.size() != m_AllTools.size())
192 MITK_WARN <<
"Tool count is corrupt. Hardware gives " << lastData.size() <<
" tools, MITK expects " << m_AllTools.size() <<
" tools. Aborting!";
196 std::vector<mitk::PolhemusTool::Pointer> allTools = this->GetAllTools();
197 for (
size_t i = 0; i < allTools.size(); i++)
199 mitk::PolhemusTool::Pointer currentTool = allTools.at(i);
201 const int distortionLevel = lastData.at(i).distortionLevel;
203 if (distortionLevel == 0)
205 currentTool->SetDataValid(
true);
209 currentTool->SetDataValid(
false);
212 currentTool->SetDistortionLevel(distortionLevel);
213 currentTool->SetPosition(lastData.at(i).pos);
214 currentTool->SetOrientation(lastData.at(i).rot);
219 this->m_StopTrackingMutex.lock();
220 localStopTracking = m_StopTracking;
221 this->m_StopTrackingMutex.unlock();
226 this->StopTracking();
243 std::vector<mitk::PolhemusInterface::trackingData> singeFrameData = this->m_Device->AutoDetectTools();
244 MITK_INFO <<
"Found " << singeFrameData.size() <<
" tools.";
245 mitk::NavigationToolStorage::Pointer returnValue = mitk::NavigationToolStorage::New();
248 mitk::NavigationTool::Pointer newTool = mitk::NavigationTool::New();
250 std::stringstream name;
251 name <<
"Sensor-" << ((int)t.
id);
252 newTool->GetDataNode()->SetName(name.str());
255 std::stringstream identifier;
256 identifier << ((int)t.
id);
257 newTool->SetIdentifier(identifier.str());
260 returnValue->AddTool(newTool);
272 m_HemisphereTrackingEnabled = _HemisphereTrackingEnabled;
273 this->m_Device->SetHemisphereTrackingEnabled(_HemisphereTrackingEnabled);
278 this->m_Device->ToggleHemisphere(_tool);
285 if (_hemisphere.GetNorm() != 0)
286 m_HemisphereTrackingEnabled =
false;
288 this->m_Device->SetHemisphere(_tool, _hemisphere);
293 return this->m_Device->GetHemisphere(_tool);
298 return this->m_Device->GetHemisphereTrackingEnabled(_tool);
303 return this->m_Device->AdjustHemisphere(_tool);
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 Start(itk::Object::Pointer device)
starts the time-acquisition
An object of this class represents the interface to Polhemus trackers. All variables with the name "t...
std::vector< PolhemusTool::Pointer > GetAllTools()
void SetHemisphere(int _tool, mitk::Vector3D _hemisphere)
mitk::Vector3D GetHemisphere(int _tool)
virtual bool AutoDetectToolsAvailable()
~PolhemusTrackingDevice()
TrackingTool * GetTool(unsigned int toolNumber) const override
void TrackTools()
This method tracks tools as long as the variable m_Mode is set to "Tracking". Tracking tools means gr...
virtual unsigned int GetToolCount() const override
void AdjustHemisphere(int _tool)
virtual bool StopTracking() override
Stops the tracking.
virtual bool OpenConnection() override
Opens the connection to the device. This have to be done before the tracking is started.
void SetHemisphereTrackingEnabled(bool _HemisphereTrackingEnabled)
void ToggleHemisphere(int _tool=-1)
PolhemusInterface * GetDevice()
bool InternalAddTool(PolhemusTool::Pointer tool)
Adds a tool to the tracking device.
virtual bool StartTracking() override
Starts the tracking.
void ThreadStartTracking()
virtual mitk::NavigationToolStorage::Pointer AutoDetectTools()
virtual bool CloseConnection() override
Closes the connection and clears all resources.
mitk::TrackingTool * AddTool(const char *toolName, int toolPort)
Create a new Polhemus tool with toolName and add it to the list of tools.
bool GetHemisphereTrackingEnabled(int _tool)
PolhemusInterface::Pointer m_Device
represents the interface to the tracking hardware
Interface for all Tracking Devices.
TrackingDeviceData m_Data
current device Data