8 #include "PlusConfigure.h" 9 #include "igsioTrackedFrame.h" 10 #include "igsioVideoFrame.h" 12 #include "vtkIGSIOTrackedFrameList.h" 13 #include "vtkIGSIOTransformRepository.h" 14 #include <vtkIGSIOFrameConverter.h> 17 #include <vtkImageData.h> 18 #include <vtkMatrix4x4.h> 19 #include <vtkObjectFactory.h> 20 #include <vtkTransform.h> 24 #include <igtl_tdata.h> 27 #include <igtlioImageConverter.h> 28 #include <igtlioPolyDataConverter.h> 29 #include <igtlioTransformConverter.h> 30 #include <igtlioConverterUtilities.h> 31 #if defined(OpenIGTLink_ENABLE_VIDEOSTREAMING) 32 #include <igtlioVideoConverter.h> 52 this->Superclass::PrintSelf(os, indent);
57 vtkIGSIOTransformRepository* transformRepository,
58 igsioTransformName& transformName)
60 igtl::IdentityMatrix(igtlMatrix);
62 if (transformRepository == NULL || !transformName.IsValid())
64 LOG_ERROR(
"GetIgtlMatrix failed: Transform repository or transform name is invalid");
68 ToolStatus status(TOOL_INVALID);
69 vtkSmartPointer<vtkMatrix4x4> vtkMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
70 if (transformRepository->GetTransform(transformName, vtkMatrix, &status) !=
PLUS_SUCCESS)
72 LOG_ERROR(
"Failed to get transform from transform repository (" << transformName.From() <<
" to " << transformName.To() <<
")");
76 if (status != TOOL_OK)
78 LOG_DEBUG(
"Skipped transformation matrix - Invalid transform in the transform repository (" << transformName.From() <<
" to " << transformName.To() <<
")");
83 igtlioTransformConverter::VTKToIGTLTransform(*vtkMatrix, igtlMatrix);
90 igsioTrackedFrame& trackedFrame,
91 vtkSmartPointer<vtkMatrix4x4> embeddedImageTransform,
92 const std::vector<igsioTransformName>& requestedTransforms)
94 if (trackedFrameMessage.IsNull())
96 LOG_ERROR(
"Failed to pack tracked frame message - input tracked frame message is NULL");
100 PlusStatus status = trackedFrameMessage->SetTrackedFrame(trackedFrame, requestedTransforms);
105 status = trackedFrameMessage->SetEmbeddedImageTransform(embeddedImageTransform);
111 trackedFrameMessage->Pack();
118 igtl::Socket* socket,
119 igsioTrackedFrame& trackedFrame,
120 const igsioTransformName& embeddedTransformName,
123 if (headerMsg.IsNull())
125 LOG_ERROR(
"Unable to unpack tracked frame message - header message is NULL!");
131 LOG_ERROR(
"Unable to unpack tracked frame message - socket is NULL!");
135 igtl::PlusTrackedFrameMessage::Pointer trackedFrameMsg = dynamic_cast<igtl::PlusTrackedFrameMessage*>(headerMsg.GetPointer());
136 if (trackedFrameMsg.IsNull())
138 trackedFrameMsg = igtl::PlusTrackedFrameMessage::New();
140 trackedFrameMsg->SetMessageHeader(headerMsg);
141 trackedFrameMsg->AllocateBuffer();
144 socket->Receive(trackedFrameMsg->GetBufferBodyPointer(), trackedFrameMsg->GetBufferBodySize(), timeout);
146 int c = trackedFrameMsg->Unpack(crccheck);
147 if (!(c & igtl::MessageHeader::UNPACK_BODY))
149 LOG_ERROR(
"Couldn't receive tracked frame message from server!");
154 trackedFrame = trackedFrameMsg->GetTrackedFrame();
156 if (embeddedTransformName.IsValid())
159 trackedFrame.SetFrameTransform(embeddedTransformName, trackedFrameMsg->GetEmbeddedImageTransform());
168 if (usMessage.IsNull())
170 LOG_ERROR(
"Failed to pack US message - input US message is NULL");
174 PlusStatus status = usMessage->SetTrackedFrame(trackedFrame);
182 igtl::Socket* socket,
183 igsioTrackedFrame& trackedFrame,
186 if (headerMsg.IsNull())
188 LOG_ERROR(
"Unable to unpack US message - header message is NULL!");
194 LOG_ERROR(
"Unable to unpack US message - socket is NULL!");
198 auto usMsg = igtl::PlusUsMessage::New();
199 usMsg->SetMessageHeader(headerMsg);
200 usMsg->AllocateBuffer();
203 socket->Receive(usMsg->GetBufferBodyPointer(), usMsg->GetBufferBodySize(), timeout);
205 int c = usMsg->Unpack(crccheck);
206 if (!(c & igtl::MessageHeader::UNPACK_BODY))
208 LOG_ERROR(
"Couldn't receive US message from server!");
213 trackedFrame = usMsg->GetTrackedFrame();
220 igsioTrackedFrame& trackedFrame,
221 const vtkMatrix4x4& matrix,
222 vtkIGSIOFrameConverter* frameConverter)
224 if (imageMessage.IsNull())
226 LOG_ERROR(
"Failed to pack image message - input image message is NULL");
230 if (!trackedFrame.GetImageData()->IsImageValid())
232 LOG_WARNING(
"Unable to send image message - image data is NOT valid!");
236 vtkSmartPointer<vtkIGSIOFrameConverter> converter = frameConverter;
239 converter = vtkSmartPointer<vtkIGSIOFrameConverter>::New();
242 double timestamp = trackedFrame.GetTimestamp();
243 vtkSmartPointer<vtkImageData> frameImage = converter->GetImageData(trackedFrame.GetImageData());
245 auto igtlFrameTime = igtl::TimeStamp::New();
248 int imageSizePixels[3] = { 0 };
249 int subSizePixels[3] = { 0 };
250 int subOffset[3] = { 0 };
251 double imageSpacingMm[3] = { 0 };
252 double imageOriginMm[3] = { 0 };
253 int scalarType = PlusCommon::GetIGTLScalarPixelTypeFromVTK(trackedFrame.GetImageData()->GetVTKScalarPixelType());
254 unsigned int numScalarComponents(1);
255 if (trackedFrame.GetImageData()->GetNumberOfScalarComponents(numScalarComponents) ==
PLUS_FAIL)
257 LOG_ERROR(
"Unable to retrieve number of scalar components.");
261 frameImage->GetDimensions(imageSizePixels);
262 frameImage->GetSpacing(imageSpacingMm);
263 frameImage->GetOrigin(imageOriginMm);
264 frameImage->GetDimensions(subSizePixels);
266 float spacingFloat[3] = { 0 };
267 for (
int i = 0;
i < 3; ++
i)
269 spacingFloat[
i ] = (float)imageSpacingMm[
i ];
272 imageMessage->SetDimensions(imageSizePixels);
273 imageMessage->SetSpacing(spacingFloat);
274 imageMessage->SetNumComponents(numScalarComponents);
275 imageMessage->SetScalarType(scalarType);
276 imageMessage->SetEndian(igtl_is_little_endian() ? igtl::ImageMessage::ENDIAN_LITTLE : igtl::ImageMessage::ENDIAN_BIG);
277 imageMessage->SetSubVolume(subSizePixels, subOffset);
278 imageMessage->AllocateScalars();
280 unsigned char* igtlImagePointer = (
unsigned char*)(imageMessage->GetScalarPointer());
281 unsigned char* vtkImagePointer = (
unsigned char*)(frameImage->GetScalarPointer());
283 memcpy(igtlImagePointer, vtkImagePointer, imageMessage->GetImageSize());
286 if (igtlioImageConverter::VTKTransformToIGTLImage(matrix, imageSizePixels, imageSpacingMm, imageOriginMm, imageMessage) != 1)
288 LOG_ERROR(
"Failed to pack image message - unable to compute IJKToRAS transform");
292 imageMessage->SetTimeStamp(igtlFrameTime);
293 imageMessage->Pack();
301 const vtkMatrix4x4& imageToReferenceTransform,
304 if (imageMessage.IsNull())
306 LOG_ERROR(
"Failed to pack image message - input image message is NULL");
310 int imageSizePixels[3] = { 0 };
311 image->GetDimensions(imageSizePixels);
312 imageMessage->SetDimensions(imageSizePixels);
314 int subSizePixels[3] = { 0 };
315 image->GetDimensions(subSizePixels);
316 int subOffset[3] = { 0 };
317 imageMessage->SetSubVolume(subSizePixels, subOffset);
319 double imageSpacingMm[3] = { 0 };
320 image->GetSpacing(imageSpacingMm);
321 float spacingFloat[3] = { 0 };
322 for (
int i = 0;
i < 3; ++
i)
324 spacingFloat[
i] = (float)imageSpacingMm[
i];
326 imageMessage->SetSpacing(spacingFloat);
328 double imageOriginMm[3] = { 0 };
329 image->GetOrigin(imageOriginMm);
332 int scalarType = PlusCommon::GetIGTLScalarPixelTypeFromVTK(image->GetScalarType());
333 imageMessage->SetScalarType(scalarType);
334 imageMessage->SetEndian(igtl_is_little_endian() ? igtl::ImageMessage::ENDIAN_LITTLE : igtl::ImageMessage::ENDIAN_BIG);
335 imageMessage->AllocateScalars();
337 unsigned char* igtlImagePointer = (
unsigned char*)(imageMessage->GetScalarPointer());
338 unsigned char* vtkImagePointer = (
unsigned char*)(image->GetScalarPointer());
340 memcpy(igtlImagePointer, vtkImagePointer, imageMessage->GetImageSize());
342 if (igtlioImageConverter::VTKTransformToIGTLImage(imageToReferenceTransform, imageSizePixels, imageSpacingMm, imageOriginMm, imageMessage) != 1)
344 LOG_ERROR(
"Failed to pack image message - unable to compute IJKToRAS transform");
348 auto igtlTime = igtl::TimeStamp::New();
350 imageMessage->SetTimeStamp(igtlTime);
352 imageMessage->Pack();
360 igtl::Socket* socket,
361 igsioTrackedFrame& trackedFrame,
362 const igsioTransformName& embeddedTransformName,
365 if (headerMsg.IsNull())
367 LOG_ERROR(
"Unable to unpack image message - header message is NULL!");
373 LOG_ERROR(
"Unable to unpack image message - socket is NULL!");
378 igtl::ImageMessage::Pointer imgMsg = dynamic_cast<igtl::ImageMessage*>(headerMsg.GetPointer());
381 imgMsg = igtl::ImageMessage::New();
383 imgMsg->SetMessageHeader(headerMsg);
384 imgMsg->AllocateBuffer();
387 socket->Receive(imgMsg->GetBufferBodyPointer(), imgMsg->GetBufferBodySize(), timeout);
389 int c = imgMsg->Unpack(crccheck);
390 if (!(c & igtl::MessageHeader::UNPACK_BODY))
392 LOG_ERROR(
"Couldn't receive image message from server!");
397 auto igtlTimestamp = igtl::TimeStamp::New();
398 imgMsg->GetTimeStamp(igtlTimestamp);
400 int imgSize[3] = {0};
401 imgMsg->GetDimensions(imgSize);
403 if (imgSize[0] < 0 || imgSize[1] < 0 || imgSize[2] < 0)
405 LOG_ERROR(
"Image with negative dimension. Aborting.");
408 FrameSizeType imageSize = {static_cast<unsigned int>(imgSize[0]), static_cast<unsigned int>(imgSize[1]), static_cast<unsigned int>(imgSize[2]) };
412 igsioVideoFrame frame;
413 if (frame.AllocateFrame(imageSize, pixelType, imgMsg->GetNumComponents()) !=
PLUS_SUCCESS)
415 LOG_ERROR(
"Failed to allocate image data for tracked frame!");
420 if (imgMsg->GetScalarType() == igtl::ImageMessage::TYPE_INT8)
422 frame.SetImageType((imgMsg->GetNumComponents() == igtl::ImageMessage::DTYPE_VECTOR) ? US_IMG_RGB_COLOR : US_IMG_BRIGHTNESS);
426 memcpy(frame.GetScalarPointer(), imgMsg->GetScalarPointer(), frame.GetFrameSizeInBytes());
428 trackedFrame.SetImageData(frame);
429 trackedFrame.SetTimestamp(igtlTimestamp->GetTimeStamp());
431 if (embeddedTransformName.IsValid())
433 vtkSmartPointer<vtkMatrix4x4> vtkMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
434 if (igtlioImageConverter::IGTLImageToVTKTransform(imgMsg, vtkMatrix) != 1)
436 LOG_ERROR(
"Failed to unpack image message - unable to extract IJKToRAS transform");
439 trackedFrame.SetFrameTransform(embeddedTransformName, vtkMatrix);
447 igsioCommon::ImageMetaDataList& imageMetaDataList)
449 if (imageMetaMessage.IsNull())
451 LOG_ERROR(
"Failed to pack image message - input image message is NULL");
454 for (igsioCommon::ImageMetaDataList::iterator it = imageMetaDataList.begin(); it != imageMetaDataList.end(); it++)
456 auto imageMetaElement = igtl::ImageMetaElement::New();
457 auto timeStamp = igtl::TimeStamp::New();
458 if (!imageMetaElement->SetName(it->Description.c_str()))
460 LOG_ERROR(
"vtkPlusIgtlMessageCommon::PackImageMetaMessage failed: image name is too long " << it->Id);
463 if (!imageMetaElement->SetDeviceName(it->Id.c_str()))
465 LOG_ERROR(
"vtkPlusIgtlMessageCommon::PackImageMetaMessage failed: device name is too long " << it->Id);
468 imageMetaElement->SetModality(it->Modality.c_str());
469 if (!imageMetaElement->SetPatientName(it->PatientName.c_str()))
471 LOG_ERROR(
"vtkPlusIgtlMessageCommon::PackImageMetaMessage failed: patient name is too long " << it->Id);
474 if (!imageMetaElement->SetPatientID(it->PatientId.c_str()))
476 LOG_ERROR(
"vtkPlusIgtlMessageCommon::PackImageMetaMessage failed: patient id is too long " << it->Id);
479 timeStamp->SetTime(it->TimeStampUtc);
480 imageMetaElement->SetTimeStamp(timeStamp);
481 imageMetaElement->SetSize(it->Size[0], it->Size[1], it->Size[2]);
482 imageMetaElement->SetScalarType(it->ScalarType);
483 imageMetaMessage->AddImageMetaElement(imageMetaElement);
485 imageMetaMessage->Pack();
490 #if defined(OpenIGTLink_ENABLE_VIDEOSTREAMING) 491 PlusStatus vtkPlusIgtlMessageCommon::PackVideoMessage(igtl::VideoMessage::Pointer videoMessage,
492 igsioTrackedFrame& trackedFrame,
493 vtkMatrix4x4& matrix,
494 vtkIGSIOFrameConverter* frameConverter,
496 std::map<std::string, std::string> parameters)
498 if (videoMessage.IsNull())
500 LOG_ERROR(
"Failed to pack image message - input image message is NULL");
504 if (!trackedFrame.GetImageData()->IsImageValid())
506 LOG_WARNING(
"Unable to send image message - image data is NOT valid!");
510 vtkSmartPointer<vtkStreamingVolumeFrame> frame = trackedFrame.GetImageData()->GetEncodedFrame();
511 std::string codecFourCC = fourCC;
512 if (codecFourCC.empty() && frame)
514 codecFourCC = frame->GetCodecFourCC();
516 if (codecFourCC.empty())
518 LOG_ERROR(
"Unknown frame encoding!");
522 frame = frameConverter->GetEncodedFrame(trackedFrame.GetImageData(), codecFourCC, parameters);
525 LOG_ERROR(
"Could not encode frame!");
529 vtkSmartPointer<vtkUnsignedCharArray> frameData = frame->GetFrameData();
530 int frameType = frame->GetFrameType();
531 unsigned int frameSize = frameData->GetSize() * frameData->GetElementComponentSize();
532 codecFourCC = frame->GetCodecFourCC();
533 int endian = (igtl_is_little_endian() == 1 ? IGTL_VIDEO_ENDIAN_LITTLE : IGTL_VIDEO_ENDIAN_BIG);
534 int dimensions[3] = { 0, 0, 0 };
535 frame->GetDimensions(dimensions);
536 double spacing[3] = { 1.0, 1.0, 1.0 };
537 int encodedFrameType = FrameTypeUnKnown;
538 if (frameType == vtkStreamingVolumeFrame::IFrame)
540 encodedFrameType = FrameTypeKey;
542 if (frame->GetNumberOfComponents() == 1)
544 encodedFrameType = encodedFrameType << 8;
547 igtl::Matrix4x4 videoMatrix;
548 igtl::IdentityMatrix(videoMatrix);
549 igtlioConverterUtilities::VTKTransformToIGTLTransform(&matrix, frame->GetDimensions(), spacing, videoMatrix);
551 double timestamp = trackedFrame.GetTimestamp();
552 auto igtlFrameTime = igtl::TimeStamp::New();
555 videoMessage->SetCodecType(codecFourCC.c_str());
556 videoMessage->SetEndian(endian);
557 videoMessage->SetSpacing(spacing[0], spacing[1], spacing[2]);
558 videoMessage->SetWidth(dimensions[0]);
559 videoMessage->SetHeight(dimensions[1]);
560 videoMessage->SetAdditionalZDimension(dimensions[2]);
561 videoMessage->SetMatrix(videoMatrix);
562 videoMessage->SetFrameType(encodedFrameType);
563 videoMessage->SetTimeStamp(igtlFrameTime);
564 videoMessage->SetBitStreamSize(frameSize);
565 videoMessage->AllocateScalars();
566 memcpy(videoMessage->GetPackFragmentPointer(2), frameData->GetPointer(0), frameSize);
567 videoMessage->Pack();
575 igsioTransformName& transformName,
576 igtl::Matrix4x4& igtlMatrix,
580 if (transformMessage.IsNull())
582 LOG_ERROR(
"Failed to pack transform message - input transform message is NULL");
586 auto igtlTime = igtl::TimeStamp::New();
589 std::string strTransformName;
590 transformName.GetTransformName(strTransformName);
592 transformMessage->SetMetaDataElement(
"TransformValid", status == TOOL_OK);
593 transformMessage->SetMetaDataElement(
"TransformStatus", IANA_TYPE_US_ASCII, igsioCommon::ConvertToolStatusToString(status));
594 transformMessage->SetMatrix(igtlMatrix);
595 transformMessage->SetTimeStamp(igtlTime);
596 if (strTransformName.length() > IGTL_TDATA_LEN_NAME)
598 std::string shortenedName = strTransformName.substr(0, IGTL_TDATA_LEN_NAME);
599 transformMessage->SetDeviceName(shortenedName.c_str());
600 transformMessage->SetMetaDataElement(
"IGTL_DEVICE_NAME", IANA_TYPE_US_ASCII, strTransformName);
604 transformMessage->SetDeviceName(strTransformName.c_str());
606 transformMessage->Pack();
613 vtkSmartPointer<vtkPolyData> polyData,
616 if (polydataMessage.IsNull())
618 LOG_ERROR(
"Failed to pack poly data message - input polydata message is NULL");
622 auto igtlTime = igtl::TimeStamp::New();
625 igtlioPolyDataConverter::VTKPolyDataToIGTL(polyData, polydataMessage);
626 polydataMessage->SetTimeStamp(igtlTime);
627 polydataMessage->Pack();
634 const std::vector<igsioTransformName>& names,
635 const vtkIGSIOTransformRepository& repository,
638 if (trackingDataMessage.IsNull())
640 LOG_ERROR(
"Failed to pack tracking data message - input tracking data message is NULL");
644 auto igtlTime = igtl::TimeStamp::New();
648 for (
auto it = names.begin(); it != names.end(); ++it)
650 if (it->GetTransformName().empty())
652 LOG_ERROR(
"Unable to pack transform element in TDATA message. Skipping.");
656 vtkNew<vtkMatrix4x4> vtkMat;
657 ToolStatus status(TOOL_INVALID);
658 if (repository.GetTransform(*it, vtkMat.GetPointer(), &status) !=
PLUS_SUCCESS)
660 LOG_ERROR(
"Transform " << it->From() <<
"To" << it->To() <<
" not found in repository.");
663 igtl::Matrix4x4 matrix;
664 if (igtlioTransformConverter::VTKToIGTLTransform(*vtkMat.GetPointer(), matrix) != 1)
666 LOG_ERROR(
"Unable to convert from VTK to IGTL transform.");
670 auto trackElement = igtl::TrackingDataElement::New();
671 std::string shortenedName = it->GetTransformName().substr(0, IGTL_TDATA_LEN_NAME);
672 trackElement->SetName(shortenedName.c_str());
673 trackElement->SetType(igtl::TrackingDataElement::TYPE_6D);
674 trackElement->SetMatrix(matrix);
675 trackingDataMessage->AddTrackingDataElement(trackElement);
676 trackingDataMessage->SetMetaDataElement(it->GetTransformName() +
"Status", IANA_TYPE_US_ASCII, igsioCommon::ConvertToolStatusToString(status));
677 trackingDataMessage->SetMetaDataElement(it->GetTransformName() +
"Index", IANA_TYPE_US_ASCII, igsioCommon::ToString(
i));
681 trackingDataMessage->SetDeviceName(
"TDATA_" + igsioCommon::ToString(trackingDataMessage->GetNumberOfTrackingDataElements()) +
"Elem");
682 trackingDataMessage->SetTimeStamp(igtlTime);
683 trackingDataMessage->Pack();
690 igtl::Socket* socket,
691 std::vector<igsioTransformName>& names,
692 vtkIGSIOTransformRepository& repository,
696 if (headerMsg.IsNull())
698 LOG_ERROR(
"Unable to unpack transform message - header message is NULL!");
704 LOG_ERROR(
"Unable to unpack transform message - socket is NULL!");
708 auto tdMsg = igtl::TrackingDataMessage::New();
709 tdMsg->SetMessageHeader(headerMsg);
713 socket->Receive(tdMsg->GetBufferBodyPointer(), tdMsg->GetBufferBodySize(), timeout);
715 int c = tdMsg->Unpack(crccheck);
716 if (!(c & igtl::MessageHeader::UNPACK_BODY))
718 LOG_ERROR(
"Couldn't receive tracking data message from server!");
723 int numberOfTools = tdMsg->GetNumberOfTrackingDataElements();
725 if (numberOfTools > 1)
727 LOG_INFO(
"Found " << numberOfTools <<
" tools");
730 for (
int i = 0;
i < numberOfTools; ++
i)
732 igtl::TrackingDataElement::Pointer currentTrackingData;
733 tdMsg->GetTrackingDataElement(
i, currentTrackingData);
734 std::string name = currentTrackingData->GetName();
736 igtl::Matrix4x4 igtlMatrix;
737 currentTrackingData->GetMatrix(igtlMatrix);
739 ToolStatus status(TOOL_UNKNOWN);
740 std::string statusStr;
741 if (tdMsg->GetMetaDataElement(std::string(currentTrackingData->GetName()) +
"Status", statusStr))
743 status = igsioCommon::ConvertStringToToolStatus(statusStr);
747 LOG_WARNING(
"Incoming TDATA message element " << currentTrackingData->GetName() <<
" is without a tool status. Defaulting to TOOL_OK.");
750 vtkSmartPointer<vtkMatrix4x4> mat = vtkSmartPointer<vtkMatrix4x4>::New();
751 if (igtlioTransformConverter::IGTLToVTKTransform(igtlMatrix, mat) != 1)
753 LOG_ERROR(
"Unable to unpack transform message - cannot convert from IGTL to VTK");
756 igsioTransformName transName(name);
757 if (!transName.IsValid())
759 LOG_ERROR(
"Invalid transform name sent via TDATA message. Skipping.");
762 names.push_back(name);
763 repository.SetTransform(transName, mat, status);
767 auto igtlTimestamp = igtl::TimeStamp::New();
768 tdMsg->GetTimeStamp(igtlTimestamp);
769 timestamp = igtlTimestamp->GetTimeStamp();
776 igtl::Socket* socket,
777 vtkMatrix4x4* transformMatrix,
778 ToolStatus& toolStatus,
779 std::string& transformName,
783 if (headerMsg.IsNull())
785 LOG_ERROR(
"Unable to unpack transform message - header message is NULL!");
791 LOG_ERROR(
"Unable to unpack transform message - socket is NULL!");
795 if (transformMatrix == NULL)
797 LOG_ERROR(
"Unable to unpack transform message - matrix is NULL!");
801 igtl::TransformMessage::Pointer transMsg = dynamic_cast<igtl::TransformMessage*>(headerMsg.GetPointer());
802 if (transMsg.IsNull())
804 transMsg = igtl::TransformMessage::New();
806 transMsg->SetMessageHeader(headerMsg);
807 transMsg->AllocateBuffer();
810 socket->Receive(transMsg->GetBufferBodyPointer(), transMsg->GetBufferBodySize(), timeout);
812 int c = transMsg->Unpack(crccheck);
813 if (!(c & igtl::MessageHeader::UNPACK_BODY))
815 LOG_ERROR(
"Couldn't receive transform message from server!");
819 igtl::Matrix4x4 igtlMatrix;
820 igtl::IdentityMatrix(igtlMatrix);
821 transMsg->GetMatrix(igtlMatrix);
824 vtkSmartPointer<vtkMatrix4x4> mat = vtkSmartPointer<vtkMatrix4x4>::New();
825 if (igtlioTransformConverter::IGTLToVTKTransform(igtlMatrix, transformMatrix) != 1)
827 LOG_ERROR(
"Unable to unpack transform message - cannot convert from IGTL to VTK");
832 auto igtlTimestamp = igtl::TimeStamp::New();
833 transMsg->GetTimeStamp(igtlTimestamp);
834 timestamp = igtlTimestamp->GetTimeStamp();
837 std::string statusStr;
838 toolStatus = TOOL_UNKNOWN;
839 if (transMsg->GetMetaDataElement(
"TransformStatus", statusStr))
841 toolStatus = igsioCommon::ConvertStringToToolStatus(statusStr);
845 LOG_WARNING(
"Incoming TRANSFORM message was received without a tool status. Defaulting to TOOL_OK.");
846 toolStatus = TOOL_OK;
850 transformName = transMsg->GetDeviceName();
857 igsioTransformName& transformName,
863 if (positionMessage.IsNull())
865 LOG_ERROR(
"Failed to pack position message - input position message is NULL");
869 auto igtlTime = igtl::TimeStamp::New();
872 std::string strTransformName;
873 transformName.GetTransformName(strTransformName);
875 positionMessage->SetPosition(
position);
876 positionMessage->SetQuaternion(quaternion);
877 positionMessage->SetMetaDataElement(
"TransformStatus", IANA_TYPE_US_ASCII, igsioCommon::ConvertToolStatusToString(status));
878 positionMessage->SetTimeStamp(igtlTime);
879 positionMessage->SetDeviceName(strTransformName.c_str());
880 positionMessage->Pack();
887 igtl::Socket* socket,
888 vtkMatrix4x4* transformMatrix,
889 std::string& transformName,
890 ToolStatus& toolStatus,
894 if (headerMsg.IsNull())
896 LOG_ERROR(
"Unable to unpack position message - header message is NULL!");
902 LOG_ERROR(
"Unable to unpack position message - socket is NULL!");
906 if (transformMatrix == NULL)
908 LOG_ERROR(
"Unable to unpack position message - transformMatrix is NULL!");
912 igtl::PositionMessage::Pointer posMsg = dynamic_cast<igtl::PositionMessage*>(headerMsg.GetPointer());
915 posMsg = igtl::PositionMessage::New();
917 posMsg->SetMessageHeader(headerMsg);
918 posMsg->AllocateBuffer();
921 socket->Receive(posMsg->GetBufferBodyPointer(), posMsg->GetBufferBodySize(), timeout);
924 int c = posMsg->Unpack(crccheck);
925 if (!(c & igtl::MessageHeader::UNPACK_BODY))
927 LOG_ERROR(
"Couldn't receive position message from server!");
935 float quaternion[4] = {0, 0, 0, 1};
936 posMsg->GetQuaternion(quaternion);
939 igtl::Matrix4x4 igtlMatrix;
940 igtl::IdentityMatrix(igtlMatrix);
941 igtl::QuaternionToMatrix(quaternion, igtlMatrix);
944 transformMatrix->SetElement(0, 3, igtlMatrix[0][3]);
945 transformMatrix->SetElement(1, 3, igtlMatrix[1][3]);
946 transformMatrix->SetElement(2, 3, igtlMatrix[2][3]);
949 std::string statusStr;
950 toolStatus = TOOL_UNKNOWN;
951 if (posMsg->GetMetaDataElement(
"Status", statusStr))
953 toolStatus = igsioCommon::ConvertStringToToolStatus(statusStr);
957 LOG_WARNING(
"Incoming POSITION message was received without a tool status. Defaulting to TOOL_OK.");
958 toolStatus = TOOL_OK;
962 auto igtlTimestamp = igtl::TimeStamp::New();
963 posMsg->GetTimeStamp(igtlTimestamp);
964 timestamp = igtlTimestamp->GetTimeStamp();
967 transformName = posMsg->GetDeviceName();
974 const char* stringName,
975 const char* stringValue,
978 if (stringMessage.IsNull())
980 LOG_ERROR(
"Failed to pack string message - input string message is NULL");
984 auto igtlTime = igtl::TimeStamp::New();
987 stringMessage->SetString(stringValue);
988 stringMessage->SetTimeStamp(igtlTime);
989 stringMessage->SetDeviceName(stringName);
990 stringMessage->Pack();
static PlusStatus PackImageMessage(igtl::ImageMessage::Pointer imageMessage, igsioTrackedFrame &trackedFrame, const vtkMatrix4x4 &imageToReferenceTransform, vtkIGSIOFrameConverter *frameConverter=NULL)
Helper class for OpenIGTLink message generation.
static PlusStatus GetIgtlMatrix(igtl::Matrix4x4 &igtlMatrix, vtkIGSIOTransformRepository *transformRepository, igsioTransformName &transformName)
static PlusStatus UnpackTransformMessage(igtl::MessageHeader::Pointer headerMsg, igtl::Socket *socket, vtkMatrix4x4 *transformMatrix, ToolStatus &toolStatus, std::string &transformName, double ×tamp, int crccheck)
vtkPlusIgtlMessageCommon()
static PlusStatus UnpackPositionMessage(igtl::MessageHeader::Pointer headerMsg, igtl::Socket *socket, vtkMatrix4x4 *transformMatrix, std::string &transformName, ToolStatus &toolStatus, double ×tamp, int crccheck)
static PlusStatus PackTransformMessage(igtl::TransformMessage::Pointer transformMessage, igsioTransformName &transformName, igtl::Matrix4x4 &igtlMatrix, ToolStatus status, double timestamp)
static PlusStatus UnpackTrackedFrameMessage(igtl::MessageHeader::Pointer headerMsg, igtl::Socket *socket, igsioTrackedFrame &trackedFrame, const igsioTransformName &embeddedTransformName, int crccheck)
static PlusStatus PackPositionMessage(igtl::PositionMessage::Pointer positionMessage, igsioTransformName &transformName, ToolStatus status, float position[3], float quaternion[4], double timestamp)
static PlusStatus PackImageMetaMessage(igtl::ImageMetaMessage::Pointer imageMetaMessage, igsioCommon::ImageMetaDataList &imageMetaDataList)
static PlusStatus PackUsMessage(igtl::PlusUsMessage::Pointer usMessage, igsioTrackedFrame &trackedFrame)
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
vtkStandardNewMacro(vtkPlusIgtlMessageCommon)
static PlusStatus PackStringMessage(igtl::StringMessage::Pointer stringMessage, const char *stringName, const char *stringValue, double timestamp)
static PlusStatus PackTrackedFrameMessage(igtl::PlusTrackedFrameMessage::Pointer trackedFrameMessage, igsioTrackedFrame &trackedFrame, vtkSmartPointer< vtkMatrix4x4 > embeddedImageTransform, const std::vector< igsioTransformName > &requestedTransforms)
static PlusStatus UnpackImageMessage(igtl::MessageHeader::Pointer headerMsg, igtl::Socket *socket, igsioTrackedFrame &trackedFrame, const igsioTransformName &embeddedTransformName, int crccheck)
static PlusStatus UnpackUsMessage(igtl::MessageHeader::Pointer headerMsg, igtl::Socket *socket, igsioTrackedFrame &trackedFrame, int crccheck)
static PlusStatus UnpackTrackingDataMessage(igtl::MessageHeader::Pointer headerMsg, igtl::Socket *socket, std::vector< igsioTransformName > &names, vtkIGSIOTransformRepository &repository, double ×tamp, int crccheck)
static PlusStatus PackTrackingDataMessage(igtl::TrackingDataMessage::Pointer tdataMessage, const std::vector< igsioTransformName > &names, const vtkIGSIOTransformRepository &repository, double timestamp)
virtual ~vtkPlusIgtlMessageCommon()
static PlusStatus PackPolyDataMessage(igtl::PolyDataMessage::Pointer polydataMessage, vtkSmartPointer< vtkPolyData > polyData, double timestamp)