19 #include "mitkIGTConfig.h"
21 #include "mitkIGTHardwareException.h"
23 #include <itksys/SystemTools.hxx>
25 #include <itkMutexLockHolder.h>
26 #include <itkCommand.h>
53 return m_OpenIGTLinkClient->GetPortNumber();
58 return m_OpenIGTLinkClient->GetHostname();
63 m_OpenIGTLinkClient->SetPortNumber(portNumber);
68 m_OpenIGTLinkClient->SetHostname(hostname);
80 if (this->InternalAddTool(t) ==
false)
82 return t.GetPointer();
87 m_AllTools.push_back(tool);
93 if (m_OpenIGTLinkClient->GetPortNumber() == -1)
95 MITK_WARN <<
"Connection not initialized, aborting (invalid port number).";
101 m_IGTLDeviceSource->Connect();
102 m_IGTLDeviceSource->StartCommunication();
104 catch (std::runtime_error &e)
106 MITK_WARN <<
"Open IGT Link device retruned an error while trying to connect: " << e.what();
112 std::string message =
"STT_TDATA";
115 ((igtl::StartTrackingDataMessage*)sttMsg.GetPointer())->SetResolution(m_UpdateRate);
116 m_OpenIGTLinkClient->SendMessage(sttMsg);
120 std::chrono::high_resolution_clock::time_point time = std::chrono::high_resolution_clock::now();
121 std::chrono::milliseconds d = std::chrono::milliseconds(waitingTime);
123 while (!(receivedMessage.IsNotNull() && receivedMessage->IsDataValid()))
125 m_IGTLDeviceSource->Update();
126 receivedMessage = m_IGTLDeviceSource->GetOutput();
128 if ((time + d) < std::chrono::high_resolution_clock::now())
131 std::this_thread::sleep_for(std::chrono::milliseconds(100));
136 if (receivedMessage.IsNull())
138 MITK_WARN <<
"No message was received. Is there really a server?";
141 else if (!receivedMessage->IsDataValid())
143 MITK_WARN <<
"Received invalid message.";
147 const char* msgType = receivedMessage->GetIGTLMessageType();
149 mitk::OpenIGTLinkTrackingDevice::TrackingMessageType type = GetMessageTypeFromString(msgType);
154 return DiscoverToolsFromTData(dynamic_cast<igtl::TrackingDataMessage*>(receivedMessage->GetMessage().GetPointer()));
156 return DiscoverToolsFromQTData(dynamic_cast<igtl::QuaternionTrackingDataMessage*>(receivedMessage->GetMessage().GetPointer()));
158 return DiscoverToolsFromTransform();
160 MITK_INFO <<
"Server does not send tracking data. Received data is not of a compatible type. Received type: " << msgType;
167 MITK_INFO <<
"Start discovering tools by TDATA messages";
168 if (tdMsg ==
nullptr)
170 MITK_WARN <<
"Message was not a TrackingDataMessage, aborting!";
174 int numberOfTools = tdMsg->GetNumberOfTrackingDataElements();
175 MITK_INFO <<
"Found " << numberOfTools <<
" tools";
176 for (
int i = 0; i < numberOfTools; i++)
179 tdMsg->GetTrackingDataElement(i, currentTrackingData);
180 std::string name = currentTrackingData->GetName();
181 AddNewToolForName(name, i);
184 m_IGTLDeviceSource->StopCommunication();
191 MITK_INFO <<
"Start discovering tools by QTDATA messages";
194 MITK_WARN <<
"Message was not a QuaternionTrackingDataMessage, aborting!";
197 int numberOfTools = msg->GetNumberOfQuaternionTrackingDataElements();
198 MITK_INFO <<
"Found " << numberOfTools <<
" tools";
199 for (
int i = 0; i < numberOfTools; i++)
202 msg->GetQuaternionTrackingDataElement(i, currentTrackingData);
203 std::string name = currentTrackingData->GetName();
204 AddNewToolForName(name, i);
206 m_IGTLDeviceSource->StopCommunication();
211 void mitk::OpenIGTLinkTrackingDevice::AddNewToolForName(std::string name,
int i)
216 std::stringstream defaultName;
217 defaultName <<
"OpenIGTLinkTool#" << i;
218 name = defaultName.str();
220 MITK_INFO <<
"Added tool " << name <<
" to tracking device.";
221 newTool->SetToolName(name);
222 InternalAddTool(newTool);
225 bool mitk::OpenIGTLinkTrackingDevice::DiscoverToolsFromTransform()
227 MITK_INFO <<
"Start discovering tools by TRANSFORM messages";
228 std::map<std::string, int> toolNameMap;
229 bool condition =
false;
233 std::this_thread::sleep_for(std::chrono::milliseconds(20));
234 m_IGTLDeviceSource->Update();
236 if (msg ==
nullptr || msg.IsNull())
238 MITK_INFO <<
"Received message could not be casted to TransformMessage. Skipping..";
243 int count = toolNameMap[msg->GetDeviceName()];
246 MITK_WARN <<
"ADDED NEW TOOL TO TOOLCHAIN: " << msg->GetDeviceName() <<
" - 1";
247 toolNameMap[msg->GetDeviceName()] = 1;
251 toolNameMap[msg->GetDeviceName()]++;
252 MITK_WARN <<
"INCREMENTED TOOL COUNT IN TOOLCHAIN: " << msg->GetDeviceName() <<
" - " << toolNameMap[msg->GetDeviceName()];
255 for (std::map<std::string, int>::iterator it = toolNameMap.begin(); it != toolNameMap.end(); ++it)
266 for (std::map<std::string, int>::iterator it = toolNameMap.begin(); it != toolNameMap.end(); ++it)
268 AddNewToolForName(it->first, i++);
277 if (this->GetState() != Tracking)
279 MITK_ERROR <<
"Method was called in the wrong state, something went wrong!";
283 m_IGTLMsgToNavDataFilter->Update();
286 const char* name = currentNavData->GetName();
289 for (
int i = 0; i < m_AllTools.size(); i++)
291 if (strcmp(m_AllTools.at(i)->GetToolName(), name) == 0)
293 m_AllTools.at(i)->SetDataValid(currentNavData->IsDataValid());
294 m_AllTools.at(i)->SetPosition(currentNavData->GetPosition());
295 m_AllTools.at(i)->SetOrientation(currentNavData->GetOrientation());
296 m_AllTools.at(i)->SetIGTTimeStamp(currentNavData->GetIGTTimeStamp());
304 if (this->GetState() != Ready)
306 MITK_WARN <<
"Cannot start tracking, device is not ready!";
312 m_IGTLDeviceSource->StartCommunication();
316 std::string message =
"STT_TDATA";
319 catch (std::runtime_error &e)
321 MITK_WARN <<
"Open IGT Link device retruned an error while starting communication: " << e.what();
327 m_IGTLMsgToNavDataFilter->SetNumberOfExpectedOutputs(this->GetToolCount());
328 m_IGTLMsgToNavDataFilter->ConnectTo(m_IGTLDeviceSource);
331 typedef itk::SimpleMemberCommand< mitk::OpenIGTLinkTrackingDevice > CurCommandType;
334 m_MessageReceivedObserverTag = m_OpenIGTLinkClient->AddObserver(mitk::MessageReceivedEvent(), messageReceivedCommand);
336 this->SetState(Tracking);
343 if (this->GetState() != Tracking)
345 MITK_WARN <<
"Cannot open connection, device is already connected!";
349 m_OpenIGTLinkClient->RemoveObserver(m_MessageReceivedObserverTag);
353 m_IGTLDeviceSource->StopCommunication();
355 catch (std::runtime_error &e)
357 MITK_WARN <<
"Open IGT Link device retruned an error while stopping communication: " << e.what();
360 this->SetState(Ready);
366 return (
unsigned int)this->m_AllTools.size();
371 if (toolNumber >= this->GetToolCount())
374 return this->m_AllTools[toolNumber];
380 if (this->GetState() !=
Setup)
382 MITK_WARN <<
"Cannot open connection, device is already connected!";
388 m_IGTLDeviceSource->Connect();
390 catch (std::runtime_error &e)
392 MITK_WARN <<
"Open IGT Link device retruned an error while trying to connect: " << e.what();
395 this->SetState(Ready);
402 if (this->GetState() != Ready)
404 MITK_WARN <<
"Cannot close connection, device is in the wrong state!";
410 m_IGTLDeviceSource->Disconnect();
412 catch (std::runtime_error &e)
414 MITK_WARN <<
"Open IGT Link device retruned an error while trying to disconnect: " << e.what();
418 this->SetState(
Setup);
425 return this->m_AllTools;
428 mitk::OpenIGTLinkTrackingDevice::TrackingMessageType mitk::OpenIGTLinkTrackingDevice::GetMessageTypeFromString(
const char* messageTypeString)
430 if (strcmp(messageTypeString,
"TDATA") == 0)
432 return mitk::OpenIGTLinkTrackingDevice::TrackingMessageType::TDATA;
434 else if (strcmp(messageTypeString,
"QTDATA") == 0)
436 return mitk::OpenIGTLinkTrackingDevice::TrackingMessageType::QTDATA;
438 else if (strcmp(messageTypeString,
"TRANSFORM") == 0)
440 return mitk::OpenIGTLinkTrackingDevice::TrackingMessageType::TRANSFORM;
444 return mitk::OpenIGTLinkTrackingDevice::TrackingMessageType::UNKNOWN;
std::string GetHostname()
itk::SmartPointer< Self > Pointer
TrackingTool * GetTool(unsigned int toolNumber) const
void SetHostname(std::string hostname)
bool DiscoverTools(int WaitingTime=10000)
Discover the tools available from the connected OpenIGTLink device and adds these tools to this track...
DataCollection - Class to facilitate loading/accessing structured data.
OpenIGTLinkTrackingDevice()
Returns the update rate of the device in fps.
itk::MutexLockHolder< itk::FastMutexLock > MutexLockHolder
mitk::TrackingTool * AddTool(const char *toolName, const char *fileName)
Create a new OpenIGTLink tool with toolName and fileName and add it to the list of tools...
virtual unsigned int GetToolCount() const
void SetPortNumber(int portNumber)
virtual bool StopTracking()
Stops the tracking.
virtual bool CloseConnection()
Closes the connection and clears all resources.
Interface for all Tracking Devices.
TrackingDeviceData m_Data
current device Data
mitk::IGTLTransformDeviceSource::Pointer m_IGTLDeviceSource
mitk::IGTLClient::Pointer m_OpenIGTLinkClient
std::vector< OpenIGTLinkTrackingTool::Pointer > GetAllTools()
~OpenIGTLinkTrackingDevice()
virtual bool OpenConnection()
Opens the connection to the device. This have to be done before the tracking is started.
bool InternalAddTool(OpenIGTLinkTrackingTool::Pointer tool)
Adds a tool to the tracking device.
virtual bool StartTracking()
Starts the tracking.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.