15 #include "mitkIGTConfig.h" 17 #include "mitkIGTHardwareException.h" 19 #include <itksys/SystemTools.hxx> 21 #include <itkMutexLockHolder.h> 22 #include <itkCommand.h> 24 #include <vtkConeSource.h> 65 MITK_WARN <<
"Connection not initialized, aborting (invalid port number).";
75 catch (std::runtime_error &e)
77 MITK_WARN <<
"AutoDetection: Open IGT Link device retruned an error while trying to connect: " << e.what();
85 const char* msgType = receivedMessage->GetIGTLMessageType();
87 if (std::string(msgType).empty())
89 MITK_INFO <<
"Did not receive a message. Do you have to start the stream manually at the server?";
90 MITK_INFO <<
"Waiting for 10 seconds ...";
92 msgType = receivedMessage->GetIGTLMessageType();
94 MITK_INFO <<
"################# got message type: " << msgType;
95 mitk::OpenIGTLinkTrackingDevice::TrackingMessageType type = GetMessageTypeFromString(msgType);
111 returnValue = DiscoverToolsAndConvertToNavigationTools(type);
119 catch (std::runtime_error &e)
121 MITK_WARN <<
"AutoDetection: Open IGT Link device retruned an error while trying to disconnect: " << e.what();
129 mitk::NavigationToolStorage::Pointer mitk::OpenIGTLinkTrackingDevice::DiscoverToolsAndConvertToNavigationTools(mitk::OpenIGTLinkTrackingDevice::TrackingMessageType type,
int NumberOfMessagesToWait)
131 MITK_INFO <<
"Start discovering tools by " << type <<
" messages";
133 std::map<std::string, int> toolNameMap;
135 for (
int j = 0; j<NumberOfMessagesToWait; j++)
137 std::this_thread::sleep_for(std::chrono::milliseconds(20));
143 igtl::TransformMessage::Pointer msg =
dynamic_cast<igtl::TransformMessage*
>(
m_IGTLDeviceSource->GetOutput()->GetMessage().GetPointer());
144 if (msg ==
nullptr || msg.IsNull()) {
MITK_INFO <<
"Received message is invalid / null. Skipping..";
continue; }
145 int count = toolNameMap[msg->GetDeviceName()];
146 if (count == 0) { toolNameMap[msg->GetDeviceName()] = 1; }
147 else { toolNameMap[msg->GetDeviceName()]++; }
152 igtl::TrackingDataMessage::Pointer msg =
dynamic_cast<igtl::TrackingDataMessage*
>(
m_IGTLDeviceSource->GetOutput()->GetMessage().GetPointer());
153 if (msg ==
nullptr || msg.IsNull()) {
MITK_INFO <<
"Received message is invalid / null. Skipping..";
continue; }
154 for (
int k = 0;
k < msg->GetNumberOfTrackingDataElements();
k++)
156 igtl::TrackingDataElement::Pointer tde;
157 msg->GetTrackingDataElement(
k, tde);
160 int count = toolNameMap[tde->GetName()];
161 if (count == 0) { toolNameMap[tde->GetName()] = 1; }
162 else { toolNameMap[tde->GetName()]++; }
168 MITK_WARN <<
"Only TRANSFORM and TDATA is currently supported, skipping!";
174 for (std::map<std::string, int>::iterator it = toolNameMap.begin(); it != toolNameMap.end(); ++it)
176 MITK_INFO <<
"Found tool: " << it->first;
178 std::stringstream name;
181 std::stringstream identifier;
182 identifier <<
"AutoDetectedTool-" << i;
185 mitk::NavigationTool::Pointer newTool = ConstructDefaultOpenIGTLinkTool(name.str(), identifier.str());
187 returnValue->AddTool(newTool);
215 mitk::OpenIGTLinkTrackingTool::Pointer t;
219 return t.GetPointer();
232 MITK_WARN <<
"Connection not initialized, aborting (invalid port number).";
241 catch (std::runtime_error &e)
243 MITK_WARN <<
"Open IGT Link device retruned an error while trying to connect: " << e.what();
247 mitk::IGTLMessage::Pointer receivedMessage =
ReceiveMessage(waitingTime);
251 if (receivedMessage.IsNull())
253 MITK_WARN <<
"No message was received. Is there really a server?";
256 else if (!receivedMessage->IsDataValid())
258 MITK_WARN <<
"Received invalid message.";
262 const char* msgType = receivedMessage->GetIGTLMessageType();
264 mitk::OpenIGTLinkTrackingDevice::TrackingMessageType type = GetMessageTypeFromString(msgType);
266 mitk::NavigationToolStorage::Pointer foundTools = this->DiscoverToolsAndConvertToNavigationTools(type);
267 if (foundTools.IsNull() || (foundTools->GetToolCount() == 0)) {
return false; }
268 for (
unsigned int i = 0; i < foundTools->GetToolCount(); i++) { AddNewToolForName(foundTools->GetTool(i)->GetToolName(), i); }
269 MITK_INFO <<
"Found tools: " << foundTools->GetToolCount();
275 mitk::IGTLMessage::Pointer receivedMessage;
278 std::string message[2] = {
"STT_QTDATA",
"STT_TDATA"};
280 for (
int i = 0; i < 2; i++)
282 igtl::MessageBase::Pointer sttMsg = msgFactory->CreateInstance(message[i]);
284 ((igtl::StartTrackingDataMessage*)sttMsg.GetPointer())->SetResolution(
m_UpdateRate);
288 std::chrono::high_resolution_clock::time_point time = std::chrono::high_resolution_clock::now();
289 std::chrono::milliseconds d = std::chrono::milliseconds(waitingTime);
291 while (!(receivedMessage.IsNotNull() && receivedMessage->IsDataValid()))
296 if ((time + d) < std::chrono::high_resolution_clock::now())
299 std::this_thread::sleep_for(std::chrono::milliseconds(100));
301 return receivedMessage;
304 void mitk::OpenIGTLinkTrackingDevice::AddNewToolForName(std::string name,
int i)
309 std::stringstream defaultName;
310 defaultName <<
"OpenIGTLinkTool#" << i;
311 name = defaultName.str();
313 MITK_INFO <<
"Added tool " << name <<
" to tracking device.";
314 newTool->SetToolName(name);
318 mitk::NavigationTool::Pointer mitk::OpenIGTLinkTrackingDevice::ConstructDefaultOpenIGTLinkTool(std::string name, std::string identifier)
321 newTool->GetDataNode()->SetName(name);
322 newTool->SetIdentifier(identifier);
334 MITK_ERROR <<
"Method was called in the wrong state, something went wrong!";
343 const char* name = currentNavData->GetName();
344 for (std::size_t i = 0; i <
m_AllTools.size(); i++)
346 if (strcmp(
m_AllTools.at(i)->GetToolName(), name) == 0)
348 m_AllTools.at(i)->SetDataValid(currentNavData->IsDataValid());
349 m_AllTools.at(i)->SetPosition(currentNavData->GetPosition());
350 m_AllTools.at(i)->SetOrientation(currentNavData->GetOrientation());
351 m_AllTools.at(i)->SetIGTTimeStamp(currentNavData->GetIGTTimeStamp());
362 MITK_WARN <<
"Cannot start tracking, device is not ready!";
372 std::string message =
"STT_TDATA";
375 catch (std::runtime_error &e)
377 MITK_WARN <<
"Open IGT Link device retruned an error while starting communication: " << e.what();
387 typedef itk::SimpleMemberCommand< mitk::OpenIGTLinkTrackingDevice > CurCommandType;
388 CurCommandType::Pointer messageReceivedCommand = CurCommandType::New();
402 MITK_WARN <<
"Cannot open connection, device is already connected!";
412 catch (std::runtime_error &e)
414 MITK_WARN <<
"Open IGT Link device retruned an error while stopping communication: " << e.what();
440 MITK_WARN <<
"Cannot open connection, device is already connected!";
448 catch (std::runtime_error &e)
450 MITK_WARN <<
"Open IGT Link device retruned an error while trying to connect: " << e.what();
462 MITK_WARN <<
"Cannot close connection, device is in the wrong state!";
470 catch (std::runtime_error &e)
472 MITK_WARN <<
"Open IGT Link device retruned an error while trying to disconnect: " << e.what();
486 mitk::OpenIGTLinkTrackingDevice::TrackingMessageType mitk::OpenIGTLinkTrackingDevice::GetMessageTypeFromString(
const char* messageTypeString)
488 if (strcmp(messageTypeString,
"TDATA") == 0)
490 return mitk::OpenIGTLinkTrackingDevice::TrackingMessageType::TDATA;
492 else if (strcmp(messageTypeString,
"QTDATA") == 0)
494 return mitk::OpenIGTLinkTrackingDevice::TrackingMessageType::QTDATA;
496 else if (strcmp(messageTypeString,
"TRANSFORM") == 0)
498 return mitk::OpenIGTLinkTrackingDevice::TrackingMessageType::TRANSFORM;
502 return mitk::OpenIGTLinkTrackingDevice::TrackingMessageType::UNKNOWN;
TrackingTool * GetTool(unsigned int toolNumber) const override
unsigned int GetToolCount() const override
std::string GetHostname()
bool IsDeviceInstalled() override
bool AutoDetectToolsAvailable() override
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...
bool StopTracking() override
Stops the tracking.
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...
mitk::NavigationToolStorage::Pointer AutoDetectTools() override
TrackingDeviceState GetState() const
return current object state (Setup, Ready or Tracking)
~OpenIGTLinkTrackingDevice() override
bool OpenConnection() override
Opens the connection to the device. This have to be done before the tracking is started.
void SetPortNumber(int portNumber)
std::vector< OpenIGTLinkTrackingTool::Pointer > m_AllTools
vector holding all tools
mitk::IGTLMessage::Pointer ReceiveMessage(int waitingTime)
int m_UpdateRate
holds the update rate in FPS (will be set automatically when the OpenIGTLink connection is establishe...
unsigned long m_MessageReceivedObserverTag
Interface for all Tracking Devices.
mitk::IGTLMessageToNavigationDataFilter::Pointer m_IGTLMsgToNavDataFilter
mitk::IGTLTrackingDataDeviceSource::Pointer m_IGTLDeviceSource
bool StartTracking() override
Starts the tracking.
TrackingDeviceData m_Data
current device Data
mitk::IGTLClient::Pointer m_OpenIGTLinkClient
bool CloseConnection() override
Closes the connection and clears all resources.
std::vector< OpenIGTLinkTrackingTool::Pointer > GetAllTools()
void SetState(TrackingDeviceState state)
change object state
bool InternalAddTool(OpenIGTLinkTrackingTool::Pointer tool)
Adds a tool to the tracking device.