7 #include "PlusConfigure.h" 9 #include "igsioTrackedFrame.h" 10 #include "igsioVideoFrame.h" 11 #include "vtkImageData.h" 13 #include "vtkMatrix4x4.h" 14 #include "vtkObjectFactory.h" 17 #include "vtkIGSIOTrackedFrameList.h" 18 #include "vtkIGSIOTransformRepository.h" 19 #include "vtksys/SystemTools.hxx" 24 #include "igtlCommandMessage.h" 25 #include "igtlImageMessage.h" 29 #include "igtlPositionMessage.h" 30 #include "igtlStatusMessage.h" 31 #include "igtlTrackingDataMessage.h" 32 #include "igtlTransformMessage.h" 34 #if defined(OpenIGTLink_ENABLE_VIDEOSTREAMING) 35 #include "igtlVideoMessage.h" 36 #include "igtl_video.h" 37 #include "igtlI420Encoder.h" 38 #if defined(OpenIGTLink_USE_VP9) 39 #include "igtlVP9Encoder.h" 41 #if defined(OpenIGTLink_USE_H264) 42 #include "igtlH264Encoder.h" 52 : IgtlFactory(
igtl::MessageFactory::New())
68 this->Superclass::PrintSelf(os, indent);
75 return this->
IgtlFactory->GetMessageTypeNewPointer(messageTypeName);
81 os << indent <<
"Supported OpenIGTLink message types: " << std::endl;
82 std::vector<std::string> types;
84 for (std::vector<std::string>::iterator it = types.begin(); it != types.end(); ++it)
86 os << indent.GetNextIndent() <<
"- " << *it << std::endl;
93 return this->
IgtlFactory->CreateHeaderMessage(headerVersion);
99 if (aIgtlMessageHdr.IsNull())
101 LOG_ERROR(
"Null header sent to factory. Unable to produce a message.");
105 igtl::MessageBase::Pointer aMessageBase;
108 aMessageBase = this->
IgtlFactory->CreateReceiveMessage(aIgtlMessageHdr);
110 catch (std::invalid_argument& e)
112 LOG_ERROR(
"Unable to create message: " << e.what());
116 if (aMessageBase.IsNull())
118 LOG_ERROR(
"IGTL factory unable to produce message of type:" << aIgtlMessageHdr->GetMessageType());
128 igtl::MessageBase::Pointer aMessageBase;
131 aMessageBase = this->
IgtlFactory->CreateSendMessage(messageType, headerVersion);
133 catch (std::invalid_argument& e)
135 LOG_ERROR(
"Unable to create message: " << e.what());
143 bool packValidTransformsOnly, vtkIGSIOTransformRepository* transformRepository)
145 int numberOfErrors(0);
146 igtlMessages.clear();
148 if (transformRepository != NULL)
150 transformRepository->SetTransforms(trackedFrame);
153 for (std::vector<std::string>::const_iterator messageTypeIterator = clientInfo.
IgtlMessageTypes.begin(); messageTypeIterator != clientInfo.
IgtlMessageTypes.end(); ++ messageTypeIterator)
155 std::string messageType = (*messageTypeIterator);
156 igtl::MessageBase::Pointer igtlMessage;
161 catch (std::invalid_argument& e)
163 LOG_ERROR(
"Unable to create message: " << e.what());
167 if (igtlMessage.IsNull())
169 LOG_ERROR(
"Failed to pack IGT messages - unable to create instance from message type: " << messageType);
174 if (
typeid(*igtlMessage) ==
typeid(igtl::ImageMessage))
176 numberOfErrors +=
PackImageMessage(clientInfo, *transformRepository, messageType, igtlMessage, trackedFrame, igtlMessages, clientId);
178 #if defined(OpenIGTLink_ENABLE_VIDEOSTREAMING) 179 else if (
typeid(*igtlMessage) ==
typeid(igtl::VideoMessage))
181 numberOfErrors += PackVideoMessage(clientInfo, *transformRepository, messageType, igtlMessage, trackedFrame, igtlMessages, clientId);
184 else if (
typeid(*igtlMessage) ==
typeid(igtl::TransformMessage))
186 numberOfErrors +=
PackTransformMessage(clientInfo, *transformRepository, packValidTransformsOnly, igtlMessage, trackedFrame, igtlMessages);
188 else if (
typeid(*igtlMessage) ==
typeid(igtl::TrackingDataMessage))
190 numberOfErrors +=
PackTrackingDataMessage(clientInfo, trackedFrame, *transformRepository, packValidTransformsOnly, igtlMessage, igtlMessages);
192 else if (
typeid(*igtlMessage) ==
typeid(igtl::PositionMessage))
194 numberOfErrors +=
PackPositionMessage(clientInfo, *transformRepository, igtlMessage, trackedFrame, igtlMessages);
198 numberOfErrors +=
PackTrackedFrameMessage(igtlMessage, clientInfo, *transformRepository, trackedFrame, igtlMessages);
202 numberOfErrors +=
PackUsMessage(igtlMessage, trackedFrame, igtlMessages);
204 else if (
typeid(*igtlMessage) ==
typeid(igtl::StringMessage))
206 numberOfErrors +=
PackStringMessage(clientInfo, trackedFrame, igtlMessage, igtlMessages);
208 else if (
typeid(*igtlMessage) ==
typeid(igtl::CommandMessage))
214 LOG_WARNING(
"This message type (" << messageType <<
") is not supported!");
225 igtl::CommandMessage::Pointer commandMessage = dynamic_cast<igtl::CommandMessage*>(igtlMessage->Clone().GetPointer());
227 igtlMessages.push_back(commandMessage.GetPointer());
235 for (std::vector<std::string>::const_iterator stringNameIterator = clientInfo.
StringNames.begin(); stringNameIterator != clientInfo.
StringNames.end(); ++stringNameIterator)
237 std::string stringValue = trackedFrame.GetFrameField(*stringNameIterator);
238 if (stringValue.empty())
243 igtl::StringMessage::Pointer stringMessage = dynamic_cast<igtl::StringMessage*>(igtlMessage->Clone().GetPointer());
245 igtlMessages.push_back(stringMessage.GetPointer());
253 int numberOfErrors(0);
254 igtl::PlusUsMessage::Pointer usMessage = dynamic_cast<igtl::PlusUsMessage*>(igtlMessage->Clone().GetPointer());
257 LOG_ERROR(
"Failed to pack IGT messages - unable to pack US message");
259 return numberOfErrors;
261 igtlMessages.push_back(usMessage.GetPointer());
262 return numberOfErrors;
268 int numberOfErrors(0);
269 igtl::PlusTrackedFrameMessage::Pointer trackedFrameMessage = dynamic_cast<igtl::PlusTrackedFrameMessage*>(igtlMessage->Clone().GetPointer());
273 ToolStatus status(TOOL_INVALID);
274 vtkSmartPointer<vtkMatrix4x4> matrix(vtkSmartPointer<vtkMatrix4x4>::New());
275 transformRepository.GetTransform(*nameIter, matrix, &status);
276 trackedFrame.SetFrameTransform(*nameIter, matrix);
277 trackedFrame.SetFrameTransformStatus(*nameIter, status);
280 vtkSmartPointer<vtkMatrix4x4> imageMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
281 imageMatrix->Identity();
284 ToolStatus status(TOOL_INVALID);
285 if (transformRepository.GetTransform(igsioTransformName(clientInfo.
ImageStreams[0].Name, clientInfo.
ImageStreams[0].EmbeddedTransformToFrame), imageMatrix, &status) !=
PLUS_SUCCESS)
287 LOG_ERROR(
"Unable to retrieve embedded image transform: " << clientInfo.
ImageStreams[0].Name <<
"To" << clientInfo.
ImageStreams[0].EmbeddedTransformToFrame <<
".");
289 return numberOfErrors;
294 LOG_ERROR(
"Failed to pack IGT messages - unable to pack tracked frame message");
296 return numberOfErrors;
298 igtlMessages.push_back(trackedFrameMessage.GetPointer());
299 return numberOfErrors;
305 for (std::vector<igsioTransformName>::const_iterator transformNameIterator = clientInfo.
TransformNames.begin(); transformNameIterator != clientInfo.
TransformNames.end(); ++transformNameIterator)
313 igsioTransformName transformName = (*transformNameIterator);
314 igtl::Matrix4x4 igtlMatrix;
318 vtkNew<vtkMatrix4x4> temp;
319 transformRepository.GetTransform(transformName, temp.GetPointer(), &status);
321 float position[3] = { igtlMatrix[0][3], igtlMatrix[1][3], igtlMatrix[2][3] };
322 float quaternion[4] = { 0, 0, 0, 1 };
323 igtl::MatrixToQuaternion(igtlMatrix, quaternion);
325 igtl::PositionMessage::Pointer positionMessage = dynamic_cast<igtl::PositionMessage*>(igtlMessage->Clone().GetPointer());
327 igtlMessages.push_back(positionMessage.GetPointer());
338 std::vector<igsioTransformName> names;
340 std::map<std::string, vtkSmartPointer<vtkMatrix4x4> > transforms;
341 for (std::vector<igsioTransformName>::const_iterator transformNameIterator = clientInfo.
TransformNames.begin(); transformNameIterator != clientInfo.
TransformNames.end(); ++transformNameIterator)
343 igsioTransformName transformName = (*transformNameIterator);
345 ToolStatus status(TOOL_INVALID);
346 vtkSmartPointer<vtkMatrix4x4> mat = vtkSmartPointer<vtkMatrix4x4>::New();
347 transformRepository.GetTransform(transformName, mat, &status);
349 if (status != TOOL_OK && packValidTransformsOnly)
351 LOG_TRACE(
"Attempted to send invalid transform over IGT Link when server has prevented sending.");
355 names.push_back(transformName);
358 igtl::TrackingDataMessage::Pointer trackingDataMessage = dynamic_cast<igtl::TrackingDataMessage*>(igtlMessage->Clone().GetPointer());
360 igtlMessages.push_back(trackingDataMessage.GetPointer());
368 for (std::vector<igsioTransformName>::const_iterator transformNameIterator = clientInfo.
TransformNames.begin(); transformNameIterator != clientInfo.
TransformNames.end(); ++transformNameIterator)
370 igsioTransformName transformName = (*transformNameIterator);
371 ToolStatus status(TOOL_UNKNOWN);
372 vtkNew<vtkMatrix4x4> temp;
373 transformRepository.GetTransform(transformName, temp.GetPointer(), &status);
375 if (status != TOOL_OK && packValidTransformsOnly)
377 LOG_TRACE(
"Attempted to send invalid transform over IGT Link when server has prevented sending.");
381 igtl::Matrix4x4 igtlMatrix;
383 igtl::TransformMessage::Pointer transformMessage = dynamic_cast<igtl::TransformMessage*>(igtlMessage->Clone().GetPointer());
384 igsioFieldMapType frameFields = trackedFrame.GetFrameFields();
385 for (igsioFieldMapType::iterator
iter = frameFields.begin();
iter != frameFields.end(); ++
iter)
387 if (
iter->first.find(transformName.GetTransformName()) == 0)
390 if ((
iter->second.first & igsioFrameFieldFlags::FRAMEFIELD_FORCE_SERVER_SEND) > 0)
392 std::string stripped =
iter->first.substr(transformName.GetTransformName().length());
393 transformMessage->SetMetaDataElement(stripped, IANA_TYPE_US_ASCII,
iter->second.second);
398 igtlMessages.push_back(transformMessage.GetPointer());
407 int numberOfErrors = 0;
408 for (std::vector<PlusIgtlClientInfo::ImageStream>::const_iterator imageStreamIterator = clientInfo.
ImageStreams.begin(); imageStreamIterator != clientInfo.
ImageStreams.end(); ++imageStreamIterator)
415 vtkSmartPointer<vtkMatrix4x4> matrix = vtkSmartPointer<vtkMatrix4x4>::New();
417 if (transformRepository.GetTransform(imageTransformName, matrix.Get(), &status) !=
PLUS_SUCCESS)
419 LOG_WARNING(
"Failed to create " << messageType <<
" message: cannot get image transform. ToolStatus: " << status);
424 std::string
deviceName = imageTransformName.From() + std::string(
"_") + imageTransformName.To();
426 igtl::ImageMessage::Pointer imageMessage = dynamic_cast<igtl::ImageMessage*>(igtlMessage->Clone().GetPointer());
427 if (trackedFrame.IsFrameFieldDefined(igsioTrackedFrame::FIELD_FRIENDLY_DEVICE_NAME))
431 deviceName = trackedFrame.GetFrameField(igsioTrackedFrame::FIELD_FRIENDLY_DEVICE_NAME);
433 imageMessage->SetDeviceName(
deviceName.c_str());
436 std::vector<std::string> frameFields;
437 trackedFrame.GetFrameFieldNameList(frameFields);
438 for (std::vector<std::string>::const_iterator stringNameIterator = frameFields.begin(); stringNameIterator != frameFields.end(); ++stringNameIterator)
440 if (trackedFrame.GetFrameField(*stringNameIterator).empty())
443 LOG_WARNING(
"No metadata value for: " << *stringNameIterator)
446 imageMessage->SetMetaDataElement(*stringNameIterator, IANA_TYPE_US_ASCII, trackedFrame.GetFrameField(*stringNameIterator));
451 LOG_ERROR(
"Failed to create " << messageType <<
" message - unable to pack image message");
455 igtlMessages.push_back(imageMessage.GetPointer());
457 return numberOfErrors;
460 #if defined(OpenIGTLink_ENABLE_VIDEOSTREAMING) 462 int vtkPlusIgtlMessageFactory::PackVideoMessage(
const PlusIgtlClientInfo& clientInfo, vtkIGSIOTransformRepository& transformRepository,
const std::string& messageType, igtl::MessageBase::Pointer igtlMessage, igsioTrackedFrame& trackedFrame, std::vector<igtl::MessageBase::Pointer>& igtlMessages,
int clientId)
464 int numberOfErrors = 0;
465 for (std::vector<PlusIgtlClientInfo::VideoStream>::const_iterator videoStreamIterator = clientInfo.
VideoStreams.begin(); videoStreamIterator != clientInfo.
VideoStreams.end(); ++videoStreamIterator)
472 vtkSmartPointer<vtkMatrix4x4> matrix = vtkSmartPointer<vtkMatrix4x4>::New();
473 if (transformRepository.GetTransform(imageTransformName, matrix.Get()) !=
PLUS_SUCCESS)
475 LOG_WARNING(
"Failed to create " << messageType <<
" message: cannot get image transform");
480 std::string
deviceName = imageTransformName.From() + std::string(
"_") + imageTransformName.To();
482 igtl::VideoMessage::Pointer videoMessage = dynamic_cast<igtl::VideoMessage*>(igtlMessage->Clone().GetPointer());
483 if (trackedFrame.IsFrameFieldDefined(igsioTrackedFrame::FIELD_FRIENDLY_DEVICE_NAME))
487 deviceName = trackedFrame.GetFrameField(igsioTrackedFrame::FIELD_FRIENDLY_DEVICE_NAME);
489 videoMessage->SetDeviceName(
deviceName.c_str());
492 std::vector<std::string> frameFields;
493 trackedFrame.GetFrameFieldNameList(frameFields);
494 for (std::vector<std::string>::const_iterator stringNameIterator = frameFields.begin(); stringNameIterator != frameFields.end(); ++stringNameIterator)
496 if (trackedFrame.GetFrameField(*stringNameIterator).empty())
499 LOG_WARNING(
"No metadata value for: " << *stringNameIterator);
502 videoMessage->SetMetaDataElement(*stringNameIterator, IANA_TYPE_US_ASCII, trackedFrame.GetFrameField(*stringNameIterator));
504 videoMessage = igtl::VideoMessage::New();
505 videoMessage->SetDeviceName(
deviceName.c_str());
506 std::map<std::string, std::string> parameters;
520 LOG_ERROR(
"Failed to create " << messageType <<
" message - unable to pack image message");
524 igtlMessages.push_back(videoMessage.GetPointer());
526 return numberOfErrors;
static PlusStatus PackImageMessage(igtl::ImageMessage::Pointer imageMessage, igsioTrackedFrame &trackedFrame, const vtkMatrix4x4 &imageToReferenceTransform, vtkIGSIOFrameConverter *frameConverter=NULL)
double GetLastTDATASentTimeStamp() const
igtl::MessageBase::Pointer CreateReceiveMessage(igtl::MessageHeader::Pointer headerMsg) const
virtual ~vtkPlusIgtlMessageFactory()
virtual void PrintAvailableMessageTypes(ostream &os, vtkIndent indent)
static PlusStatus GetIgtlMatrix(igtl::Matrix4x4 &igtlMatrix, vtkIGSIOTransformRepository *transformRepository, igsioTransformName &transformName)
vtkStandardNewMacro(vtkPlusIgtlMessageFactory)
std::string EmbeddedTransformToFrame
std::vector< ImageStream > ImageStreams
std::vector< std::string > IgtlMessageTypes
int PackPositionMessage(const PlusIgtlClientInfo &clientInfo, vtkIGSIOTransformRepository &transformRepository, igtl::MessageBase::Pointer igtlMessage, igsioTrackedFrame &trackedFrame, std::vector< igtl::MessageBase::Pointer > &igtlMessages)
std::vector< std::string > StringNames
vtkSmartPointer< vtkIGSIOFrameConverter > FrameConverter
int PackTrackingDataMessage(const PlusIgtlClientInfo &clientInfo, igsioTrackedFrame &trackedFrame, vtkIGSIOTransformRepository &transformRepository, bool packValidTransformsOnly, igtl::MessageBase::Pointer igtlMessage, std::vector< igtl::MessageBase::Pointer > &igtlMessages)
std::vector< igsioTransformName > TransformNames
IGTL message helper class for tracked frame messages.
static PlusStatus PackTransformMessage(igtl::TransformMessage::Pointer transformMessage, igsioTransformName &transformName, igtl::Matrix4x4 &igtlMatrix, ToolStatus status, double timestamp)
static PlusStatus PackPositionMessage(igtl::PositionMessage::Pointer positionMessage, igsioTransformName &transformName, ToolStatus status, float position[3], float quaternion[4], double timestamp)
bool GetTDATARequested() const
static PlusStatus PackUsMessage(igtl::PlusUsMessage::Pointer usMessage, igsioTrackedFrame &trackedFrame)
int PackStringMessage(const PlusIgtlClientInfo &clientInfo, igsioTrackedFrame &trackedFrame, igtl::MessageBase::Pointer igtlMessage, std::vector< igtl::MessageBase::Pointer > &igtlMessages)
int PackTransformMessage(const PlusIgtlClientInfo &clientInfo, vtkIGSIOTransformRepository &transformRepository, bool packValidTransformsOnly, igtl::MessageBase::Pointer igtlMessage, igsioTrackedFrame &trackedFrame, std::vector< igtl::MessageBase::Pointer > &igtlMessages)
EncodingParameters EncodeVideoParameters
int PackCommandMessage(igtl::MessageBase::Pointer igtlMessage, std::vector< igtl::MessageBase::Pointer > &igtlMessages)
vtkPlusIgtlMessageFactory()
virtual vtkPlusIgtlMessageFactory::PointerToMessageBaseNew GetMessageTypeNewPointer(const std::string &messageTypeName)
int GetTDATAResolution() const
IGTL message helper class for sending USMessage device messages as IMAGE type message from tracked fr...
igtl::MessageFactory::Pointer IgtlFactory
std::vector< VideoStream > VideoStreams
Factory class of supported OpenIGTLink message types.
int GetClientHeaderVersion() const
int PackTrackedFrameMessage(igtl::MessageBase::Pointer igtlMessage, const PlusIgtlClientInfo &clientInfo, vtkIGSIOTransformRepository &transformRepository, igsioTrackedFrame &trackedFrame, std::vector< igtl::MessageBase::Pointer > &igtlMessages)
static PlusStatus PackStringMessage(igtl::StringMessage::Pointer stringMessage, const char *stringName, const char *stringValue, double timestamp)
igtl::MessageBase::Pointer(* PointerToMessageBaseNew)()
static PlusStatus PackTrackedFrameMessage(igtl::PlusTrackedFrameMessage::Pointer trackedFrameMessage, igsioTrackedFrame &trackedFrame, vtkSmartPointer< vtkMatrix4x4 > embeddedImageTransform, const std::vector< igsioTransformName > &requestedTransforms)
igtl::MessageBase::Pointer CreateSendMessage(const std::string &messageType, int headerVersion) const
igtl::MessageHeader::Pointer CreateHeaderMessage(int headerVersion) const
int PackImageMessage(const PlusIgtlClientInfo &clientInfo, vtkIGSIOTransformRepository &transformRepository, const std::string &messageType, igtl::MessageBase::Pointer igtlMessage, igsioTrackedFrame &trackedFrame, std::vector< igtl::MessageBase::Pointer > &igtlMessages, int clientId)
static PlusStatus PackTrackingDataMessage(igtl::TrackingDataMessage::Pointer tdataMessage, const std::vector< igsioTransformName > &names, const vtkIGSIOTransformRepository &repository, double timestamp)
This class provides client information for vtkPlusOpenIGTLinkServer.
vtkSmartPointer< vtkIGSIOFrameConverter > FrameConverter
int PackUsMessage(igtl::MessageBase::Pointer igtlMessage, igsioTrackedFrame &trackedFrame, std::vector< igtl::MessageBase::Pointer > &igtlMessages)
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
PlusStatus PackMessages(int clientId, const PlusIgtlClientInfo &clientInfo, std::vector< igtl::MessageBase::Pointer > &igtMessages, igsioTrackedFrame &trackedFrame, bool packValidTransformsOnly, vtkIGSIOTransformRepository *transformRepository=NULL)
std::string EmbeddedTransformToFrame