7 #include "PlusConfigure.h" 10 #include "igtlPositionMessage.h" 11 #include "igtlTrackingDataMessage.h" 12 #include "igtlTransformMessage.h" 13 #include "vtkMatrix4x4.h" 14 #include "vtkObjectFactory.h" 24 : UseLastTransformsOnReceiveTimeout(false)
45 LOG_TRACE(
"vtkPlusOpenIGTLinkTracker::Disconnect");
49 auto stpMsg = igtl::StopTrackingDataMessage::New();
50 stpMsg->SetDeviceName(
"");
55 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
57 (retValue = this->
ClientSocket->Send(stpMsg->GetBufferPointer(), stpMsg->GetBufferSize())) != 0,
63 LOG_ERROR(
"Failed to send STP_TDATA message to server!");
74 LOG_TRACE(
"vtkPlusOpenIGTLinkTracker::InternalUpdate");
78 LOG_ERROR(
"called Update() when not tracking");
95 LOG_TRACE(
"vtkPlusOpenIGTLinkTracker::InternalUpdateTData");
97 igtl::MessageBase::Pointer bodyMsg;
98 igtl::MessageHeader::Pointer headerMsg;
104 if (headerMsg.IsNull())
110 LOG_TRACE(
"No OpenIGTLink message has been received in device " << this->
GetDeviceId());
112 double unfilteredTimestamp = vtkIGSIOAccurateTimer::GetSystemTime();
119 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
123 double unfilteredTimestamp = vtkIGSIOAccurateTimer::GetSystemTime();
134 if (
typeid(*bodyMsg) ==
typeid(igtl::TrackingDataMessage))
141 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
142 this->
ClientSocket->Skip(headerMsg->GetBodySizeToRead(), 0);
146 igtl::TrackingDataMessage::Pointer tdataMsg = dynamic_cast<igtl::TrackingDataMessage*>(bodyMsg.GetPointer());
147 tdataMsg->SetMessageHeader(headerMsg);
148 tdataMsg->AllocateBuffer();
152 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
153 this->
ClientSocket->Receive(tdataMsg->GetBufferBodyPointer(), tdataMsg->GetBufferBodySize(), timeout);
156 if (!(c & igtl::MessageHeader::UNPACK_BODY))
158 LOG_ERROR(
"Couldn't receive TDATA message from server!");
163 double unfilteredTimestamp = vtkIGSIOAccurateTimer::GetSystemTime();
164 double filteredTimestamp = unfilteredTimestamp;
167 std::set<std::string> identifiedToolSourceIds;
168 for (
int i = 0;
i < tdataMsg->GetNumberOfTrackingDataElements(); ++
i)
170 auto tdataElem = igtl::TrackingDataElement::New();
171 tdataMsg->GetTrackingDataElement(
i, tdataElem);
173 igtl::Matrix4x4 igtlMatrix;
174 tdataElem->GetMatrix(igtlMatrix);
175 vtkSmartPointer<vtkMatrix4x4> toolMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
177 for (
int r = 0; r < 4; r++)
179 for (
int c = 0; c < 4; c++)
181 toolMatrix->SetElement(r, c, igtlMatrix[r][c]);
186 auto igtlTimestamp = igtl::TimeStamp::New();
187 tdataMsg->GetTimeStamp(igtlTimestamp);
190 std::string igtlTransformName = tdataElem->GetName();
193 igsioTransformName transformName;
194 if (igtlTransformName.find(
"To") != std::string::npos)
197 transformName = igtlTransformName;
207 identifiedToolSourceIds.insert(transformName.GetTransformName());
211 LOG_INFO(
"ToolTimeStampedUpdate failed for tool: " << transformName.From() <<
" with timestamp: " << std::fixed << unfilteredTimestamp);
216 vtkSmartPointer<vtkMatrix4x4> toolMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
217 toolMatrix->Identity();
220 if (identifiedToolSourceIds.find(it->second->GetId()) != identifiedToolSourceIds.end())
223 LOG_TRACE(
"Tool " << it->second->GetId() <<
": found");
226 LOG_TRACE(
"Tool " << it->second->GetId() <<
": not found");
235 LOG_TRACE(
"vtkPlusOpenIGTLinkTracker::InternalUpdateGeneral");
237 double unfilteredTimestamp = vtkIGSIOAccurateTimer::GetSystemTime();
239 double maxAllocatedProcessingTime = 2.0;
246 igtl::MessageHeader::Pointer headerMsg;
248 bool moreMessagesPossible =
true;
249 while (moreMessagesPossible)
256 if (vtkIGSIOAccurateTimer::GetSystemTime() - unfilteredTimestamp > maxAllocatedProcessingTime)
278 double unfilteredTimestamp = vtkIGSIOAccurateTimer::GetSystemTime();
289 igtl::MessageHeader::Pointer headerMsg;
291 if (headerMsg.IsNull())
294 moreMessagesPossible =
false;
298 moreMessagesPossible =
true;
304 double unfilteredTimestampUtc = 0;
305 vtkSmartPointer<vtkMatrix4x4> toolMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
306 std::string igtlTransformName;
307 ToolStatus toolStatus(TOOL_UNKNOWN);
309 igtl::MessageBase::Pointer bodyMsg = this->
MessageFactory->CreateReceiveMessage(headerMsg);
310 if (
typeid(*bodyMsg) ==
typeid(igtl::TransformMessage))
314 LOG_ERROR(
"Couldn't receive transform message from server!");
318 else if (
typeid(*bodyMsg) ==
typeid(igtl::PositionMessage))
322 LOG_ERROR(
"Couldn't receive position message from server!");
329 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
330 this->
ClientSocket->Skip(headerMsg->GetBodySizeToRead(), 0);
335 igsioTransformName transformName;
336 if (transformName.SetTransformName(igtlTransformName.c_str()) !=
PLUS_SUCCESS)
338 LOG_ERROR(
"Failed to update tracker tool - unrecognized transform name: " << igtlTransformName);
342 double unfilteredTimestamp = 0;
347 unfilteredTimestamp = vtkIGSIOAccurateTimer::GetSystemTimeFromUniversalTime(unfilteredTimestampUtc);
351 unfilteredTimestamp = vtkIGSIOAccurateTimer::GetSystemTime();
356 double filteredTimestamp = unfilteredTimestamp;
362 LOG_INFO(
"ToolTimeStampedUpdate failed for tool: " << transformName.GetTransformName() <<
" with timestamp: " << std::fixed << unfilteredTimestamp);
383 auto sttMsg = igtl::StartTrackingDataMessage::New();
384 sttMsg->SetDeviceName(
"");
385 sttMsg->SetResolution(50);
391 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
393 (retValue = this->
ClientSocket->Send(sttMsg->GetBufferPointer(), sttMsg->GetBufferSize())) != 0,
399 LOG_ERROR(
"Failed to send STT_TDATA message to server!");
414 vtkSmartPointer<vtkMatrix4x4> toolMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
416 if (it->second->GetNumberOfItems() > 0)
420 if (it->second->GetStreamBufferItem(latestItemUid, &item) ==
ITEM_OK)
429 LOG_WARNING(
"Failed to get matrix from buffer item with UID: " << latestItemUid);
435 LOG_WARNING(
"Failed to get buffer item with UID: " << latestItemUid);
441 LOG_INFO(
"ToolTimeStampedUpdate failed for tool: " << it->second->GetId() <<
" with timestamp: " << std::fixed << unfilteredTimestamp);
456 double itemArrivalDelayToleranceSec = 3.0;
465 vtkSmartPointer<vtkMatrix4x4> toolMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
467 if (it->second->GetNumberOfItems() > 0)
471 if (it->second->GetStreamBufferItem(latestItemUid, &item) ==
ITEM_OK)
473 if (item.
GetUnfilteredTimestamp(this->GetLocalTimeOffsetSec()) + itemArrivalDelayToleranceSec >= unfilteredTimestamp)
480 LOG_WARNING(
"Failed to get matrix from buffer item with UID: " << latestItemUid);
486 LOG_WARNING(
"Failed to get buffer item with UID: " << latestItemUid);
491 LOG_TRACE(
"No update was received for tool: " << it->second->GetId() <<
", assume that it does not provide valid transform anymore");
494 LOG_INFO(
"ToolTimeStampedUpdate failed for tool: " << it->second->GetId() <<
" with timestamp: " << std::fixed << unfilteredTimestamp);
525 return (igsioCommon::IsEqualInsensitive(this->
MessageType,
"TDATA"));
DataSourceContainer::const_iterator DataSourceContainerConstIterator
std::string ToolReferenceFrameName
vtkStandardNewMacro(vtkPlusOpenIGTLinkTracker)
void ReceiveMessageHeaderWithErrorHandling(igtl::MessageHeader::Pointer &headerMsg)
bool UseLastTransformsOnReceiveTimeout
virtual PlusStatus InternalDisconnect()
static PlusStatus UnpackTransformMessage(igtl::MessageHeader::Pointer headerMsg, igtl::Socket *socket, vtkMatrix4x4 *transformMatrix, ToolStatus &toolStatus, std::string &transformName, double ×tamp, int crccheck)
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_WRITING(deviceConfig, rootConfigElement)
virtual PlusStatus ToolTimeStampedUpdateWithoutFiltering(const std::string &aToolSourceId, vtkMatrix4x4 *matrix, ToolStatus status, double unfilteredtimestamp, double filteredtimestamp, const igsioFieldMapType *customFields=NULL)
PlusStatus StoreMostRecentTransformValues(double unfilteredTimestamp)
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *config)
vtkPlusOpenIGTLinkTracker()
vtkSmartPointer< vtkPlusIgtlMessageFactory > MessageFactory
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *config)
virtual std::string GetDeviceId() const
virtual PlusStatus InternalDisconnect()
vtkSmartPointer< vtkIGSIORecursiveCriticalSection > SocketMutex
PlusStatus InternalUpdateGeneral()
static PlusStatus UnpackPositionMessage(igtl::MessageHeader::Pointer headerMsg, igtl::Socket *socket, vtkMatrix4x4 *transformMatrix, std::string &transformName, ToolStatus &toolStatus, double ×tamp, int crccheck)
virtual double GetAcquisitionRate() const
int NumberOfRetryAttempts
virtual void PrintSelf(ostream &os, vtkIndent indent)
virtual PlusStatus SendRequestedMessageTypes()
PlusStatus StoreInvalidTransforms(double unfilteredTimestamp)
PlusStatus ProcessTransformMessageGeneral(bool &moreMessagesPossible)
PlusStatus InternalUpdateTData()
PlusStatus InternalUpdate()
virtual ~vtkPlusOpenIGTLinkTracker()
PlusStatus GetMatrix(vtkMatrix4x4 *outputMatrix)
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_READING(deviceConfig, rootConfigElement)
bool UseReceivedTimestamps
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
double GetUnfilteredTimestamp(double localTimeOffsetSec)
virtual bool IsTDataMessageType()
igtl::ClientSocket::Pointer ClientSocket
DataSourceContainerConstIterator GetToolIteratorBegin() const
OpenIGTLink tracker client.
virtual PlusStatus SendRequestedMessageTypes()
void SetToolReferenceFrameName(const std::string &frameName)
unsigned long long BufferItemUidType
DataSourceContainerConstIterator GetToolIteratorEnd() const
double DelayBetweenRetryAttemptsSec
int IgtlMessageCrcCheckEnabled