PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
igtlPlusTrackedFrameMessage.cxx
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2 Program: Plus
3 Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4 See License.txt for details.
5 =========================================================Plus=header=end*/
6 
7 #include "PlusConfigure.h"
9 #include "vtkImageData.h"
10 #include "vtkMatrix4x4.h"
12 
13 namespace igtl
14 {
15  //----------------------------------------------------------------------------
17  : MessageBase()
18  {
19  this->m_SendMessageType = "TRACKEDFRAME";
20  }
21 
22  //----------------------------------------------------------------------------
24  {
25  }
26 
27  //----------------------------------------------------------------------------
28  igtl::MessageBase::Pointer PlusTrackedFrameMessage::Clone()
29  {
30  igtl::MessageBase::Pointer clone;
31  {
32  vtkSmartPointer<vtkPlusIgtlMessageFactory> factory = vtkSmartPointer<vtkPlusIgtlMessageFactory>::New();
33  clone = dynamic_cast<igtl::MessageBase*>(factory->CreateSendMessage(this->GetMessageType(), this->GetHeaderVersion()).GetPointer());
34  }
35 
36  igtl::PlusTrackedFrameMessage::Pointer msg = dynamic_cast<igtl::PlusTrackedFrameMessage*>(clone.GetPointer());
37 
38  int bodySize = this->m_MessageSize - IGTL_HEADER_SIZE;
39  msg->InitBuffer();
40  msg->CopyHeader(this);
41  msg->AllocateBuffer(bodySize);
42  if (bodySize > 0)
43  {
44  msg->CopyBody(this);
45  }
46 
47  return clone;
48  }
49 
50  //----------------------------------------------------------------------------
51  PlusStatus PlusTrackedFrameMessage::SetTrackedFrame(const igsioTrackedFrame& trackedFrame, const std::vector<igsioTransformName>& requestedTransforms)
52  {
53  this->m_TrackedFrame = trackedFrame;
54 
55  if (this->m_TrackedFrame.GetTrackedFrameInXmlData(this->m_TrackedFrameXmlData, requestedTransforms) != PLUS_SUCCESS)
56  {
57  LOG_ERROR("Failed to pack Plus TrackedFrame message - unable to get tracked frame in xml data.");
58  return PLUS_FAIL;
59  }
60 
61  FrameSizeType frameSize = this->m_TrackedFrame.GetFrameSize();
62  if (frameSize[0] > static_cast<unsigned int>(std::numeric_limits<igtl_uint16>::max()) ||
63  frameSize[1] > static_cast<unsigned int>(std::numeric_limits<igtl_uint16>::max()) ||
64  frameSize[2] > static_cast<unsigned int>(std::numeric_limits<igtl_uint16>::max()))
65  {
66  LOG_ERROR("Frame size element is too large to be sent over OpenIGTLink. Cannot set tracked frame.");
67  return PLUS_FAIL;
68  }
69 
70  this->m_MessageHeader.m_FrameSize[0] = frameSize[0];
71  this->m_MessageHeader.m_FrameSize[1] = frameSize[1];
72  this->m_MessageHeader.m_FrameSize[2] = frameSize[2];
74  this->m_MessageHeader.m_ScalarType = PlusCommon::GetIGTLScalarPixelTypeFromVTK(this->m_TrackedFrame.GetImageData()->GetVTKScalarPixelType());
75 
76  unsigned int numberOfScalarComponents(1);
77  if (m_TrackedFrame.GetImageData()->GetNumberOfScalarComponents(numberOfScalarComponents) == PLUS_FAIL)
78  {
79  LOG_ERROR("Unable to retrieve number of scalar components.");
80  return PLUS_FAIL;
81  }
82  this->m_MessageHeader.m_NumberOfComponents = numberOfScalarComponents;
83  this->m_MessageHeader.m_ImageType = m_TrackedFrame.GetImageData()->GetImageType();
84  this->m_MessageHeader.m_ImageDataSizeInBytes = this->m_TrackedFrame.GetImageData()->GetFrameSizeInBytes();
85  this->m_MessageHeader.m_ImageOrientation = (igtl_uint16)this->m_TrackedFrame.GetImageData()->GetImageOrientation();
86 
87  return PLUS_SUCCESS;
88  }
89 
90  //----------------------------------------------------------------------------
92  {
93  return this->m_TrackedFrame;
94  }
95 
96  //----------------------------------------------------------------------------
97  PlusStatus PlusTrackedFrameMessage::SetEmbeddedImageTransform(vtkSmartPointer<vtkMatrix4x4> matrix)
98  {
99  for (int i = 0; i < 4; ++i)
100  {
101  for (int j = 0; j < 4; ++j)
102  {
103  m_MessageHeader.m_EmbeddedImageTransform[i][j] = matrix->GetElement(i, j);
104  }
105  }
106 
107  return PLUS_SUCCESS;
108  }
109 
110  //----------------------------------------------------------------------------
112  {
113  vtkSmartPointer<vtkMatrix4x4> mat(vtkSmartPointer<vtkMatrix4x4>::New());
114  for (int i = 0; i < 4; ++i)
115  {
116  for (int j = 0; j < 4; ++j)
117  {
118  mat->SetElement(i, j, m_MessageHeader.m_EmbeddedImageTransform[i][j]);
119  }
120  }
121  return mat;
122  }
123 
124  //----------------------------------------------------------------------------
126  {
130  }
131 
132  //----------------------------------------------------------------------------
134  {
135  AllocateBuffer();
136 
137  // Copy header
138  TrackedFrameHeader* header = (TrackedFrameHeader*)(this->m_Content);
139  header->m_ScalarType = this->m_MessageHeader.m_ScalarType;
141  header->m_ImageType = this->m_MessageHeader.m_ImageType;
142  header->m_FrameSize[0] = this->m_MessageHeader.m_FrameSize[0];
143  header->m_FrameSize[1] = this->m_MessageHeader.m_FrameSize[1];
144  header->m_FrameSize[2] = this->m_MessageHeader.m_FrameSize[2];
148  memcpy(header->m_EmbeddedImageTransform, this->m_MessageHeader.m_EmbeddedImageTransform, sizeof(igtl::Matrix4x4));
149 
150  // Copy xml data
151  char* xmlData = (char*)(this->m_Content + header->GetMessageHeaderSize());
152  strncpy(xmlData, this->m_TrackedFrameXmlData.c_str(), this->m_TrackedFrameXmlData.size());
154 
155  // Copy image data
156  void* imageData = (void*)(this->m_Content + header->GetMessageHeaderSize() + header->m_XmlDataSizeInBytes);
157  memcpy(imageData, this->m_TrackedFrame.GetImageData()->GetScalarPointer(), this->m_TrackedFrame.GetImageData()->GetFrameSizeInBytes());
158 
159  // Set timestamp
160  auto timestamp = igtl::TimeStamp::New();
161  timestamp->SetTime(this->m_TrackedFrame.GetTimestamp());
162  this->SetTimeStamp(timestamp);
163 
164  // Convert header endian
165  header->ConvertEndianness();
166 
167  return 1;
168  }
169 
170  //----------------------------------------------------------------------------
172  {
173  TrackedFrameHeader* header = (TrackedFrameHeader*)(this->m_Content);
174 
175  // Convert header endian
176  header->ConvertEndianness();
177 
178  // Copy header
179  this->m_MessageHeader.m_ScalarType = header->m_ScalarType;
181  this->m_MessageHeader.m_ImageType = header->m_ImageType;
182  this->m_MessageHeader.m_FrameSize[0] = header->m_FrameSize[0];
183  this->m_MessageHeader.m_FrameSize[1] = header->m_FrameSize[1];
184  this->m_MessageHeader.m_FrameSize[2] = header->m_FrameSize[2];
188  memcpy(this->m_MessageHeader.m_EmbeddedImageTransform, header->m_EmbeddedImageTransform, sizeof(igtl::Matrix4x4));
189 
190  // Copy xml data
191  char* xmlData = (char*)(this->m_Content + header->GetMessageHeaderSize());
192  this->m_TrackedFrameXmlData.assign(xmlData, header->m_XmlDataSizeInBytes);
193  if (this->m_TrackedFrame.SetTrackedFrameFromXmlData(this->m_TrackedFrameXmlData) != PLUS_SUCCESS)
194  {
195  LOG_ERROR("Failed to set tracked frame data from xml received in Plus TrackedFrame message");
196  return 0;
197  }
198 
199  // Copy image data
200  void* imageData = (void*)(this->m_Content + header->GetMessageHeaderSize() + header->m_XmlDataSizeInBytes);
201  FrameSizeType frameSize = { header->m_FrameSize[0], header->m_FrameSize[1], header->m_FrameSize[2] };
202  if (this->m_TrackedFrame.GetImageData()->AllocateFrame(frameSize, PlusCommon::GetVTKScalarPixelTypeFromIGTL(header->m_ScalarType), header->m_NumberOfComponents) != PLUS_SUCCESS)
203  {
204  LOG_ERROR("Failed to allocate memory for frame received in Plus TrackedFrame message");
205  return 0;
206  }
207 
208  // Carry the image type forward
209  m_TrackedFrame.GetImageData()->SetImageType((US_IMAGE_TYPE)header->m_ImageType);
210 
211  memcpy(this->m_TrackedFrame.GetImageData()->GetScalarPointer(), imageData, header->m_ImageDataSizeInBytes);
212  m_TrackedFrame.GetImageData()->GetImage()->Modified();
213 
214  // Set timestamp
215  auto timestamp = igtl::TimeStamp::New();
216  this->GetTimeStamp(timestamp);
217  this->m_TrackedFrame.SetTimestamp(timestamp->GetTimeStamp());
218 
219  return 1;
220  }
221 }
double * timestamp
Definition: phidget22.h:3432
igsioStatus PlusStatus
Definition: PlusCommon.h:40
for i
#define PLUS_FAIL
Definition: PlusCommon.h:43
PlusStatus SetTrackedFrame(const igsioTrackedFrame &trackedFrame, const std::vector< igsioTransformName > &requestedTransforms)
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
vtkSmartPointer< vtkMatrix4x4 > GetEmbeddedImageTransform()
PlusStatus SetEmbeddedImageTransform(vtkSmartPointer< vtkMatrix4x4 > matrix)
virtual igtl::MessageBase::Pointer Clone()