PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
igtlPlusUsMessage.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"
8 #include "igsioTrackedFrame.h"
9 #include "igtlPlusUsMessage.h"
10 #include "igtl_image.h"
11 #include "igtl_header.h"
12 #include "igtl_util.h"
14 
15 namespace igtl
16 {
17 
18  //----------------------------------------------------------------------------
20  {
21  size_t headersize = 13 * sizeof(igtl_uint32);
22  return headersize;
23  }
24 
25  //----------------------------------------------------------------------------
27  {
28  if (igtl_is_little_endian())
29  {
30  m_DataType = BYTE_SWAP_INT32(m_DataType);
31  m_TransmitFrequency = BYTE_SWAP_INT32(m_TransmitFrequency);
32  m_SamplingFrequency = BYTE_SWAP_INT32(m_SamplingFrequency);
33  m_DataRate = BYTE_SWAP_INT32(m_DataRate);
34  m_LineDensity = BYTE_SWAP_INT32(m_LineDensity);
35 
36  m_SteeringAngle = BYTE_SWAP_INT32(m_SteeringAngle);
37  m_ProbeID = BYTE_SWAP_INT32(m_ProbeID);
38  m_ExtensionAngle = BYTE_SWAP_INT32(m_ExtensionAngle);
39  m_Elements = BYTE_SWAP_INT32(m_Elements);
40  m_Pitch = BYTE_SWAP_INT32(m_Pitch);
41 
42  m_Radius = BYTE_SWAP_INT32(m_Radius);
43  m_ProbeAngle = BYTE_SWAP_INT32(m_ProbeAngle);
44  m_TxOffset = BYTE_SWAP_INT32(m_TxOffset);
45  }
46  }
47 
48  //----------------------------------------------------------------------------
50  : ImageMessage()
51  {
52  this->m_SendMessageType = "USMESSAGE";
53  }
54 
55  //----------------------------------------------------------------------------
57  {
58  }
59 
60  //----------------------------------------------------------------------------
61  igtl::MessageBase::Pointer PlusUsMessage::Clone()
62  {
63  igtl::MessageBase::Pointer clone;
64  {
65  vtkSmartPointer<vtkPlusIgtlMessageFactory> factory = vtkSmartPointer<vtkPlusIgtlMessageFactory>::New();
66  clone = dynamic_cast<igtl::MessageBase*>(factory->CreateSendMessage(this->GetMessageType(), this->GetHeaderVersion()).GetPointer());
67  }
68 
69  igtl::PlusUsMessage::Pointer msg = dynamic_cast<igtl::PlusUsMessage*>(clone.GetPointer());
70 
71  int bodySize = this->m_MessageSize - IGTL_HEADER_SIZE;
72  msg->InitBuffer();
73  msg->CopyHeader(this);
74  msg->AllocateBuffer(bodySize);
75  if (bodySize > 0)
76  {
77  msg->CopyBody(this);
78  }
79 
80 #if OpenIGTLink_HEADER_VERSION >= 2
81  msg->m_MetaDataHeader = this->m_MetaDataHeader;
82  msg->m_MetaDataMap = this->m_MetaDataMap;
83  msg->m_IsExtendedHeaderUnpacked = this->m_IsExtendedHeaderUnpacked;
84 #endif
85 
86  return clone;
87  }
88 
89  //----------------------------------------------------------------------------
90  igsioTrackedFrame& PlusUsMessage::GetTrackedFrame()
91  {
92  return this->m_TrackedFrame;
93  }
94 
95  //----------------------------------------------------------------------------
96  PlusStatus PlusUsMessage::SetTrackedFrame(const igsioTrackedFrame& trackedFrame)
97  {
98  this->m_TrackedFrame = trackedFrame;
99 
100  double timestamp = this->m_TrackedFrame.GetTimestamp();
101 
102  auto igtlFrameTime = igtl::TimeStamp::New();
103  igtlFrameTime->SetTime(timestamp);
104 
105  std::array<int, 3> offset = {0};
106  FrameSizeType imageSizePixels = {0};
107 
108  // NOTE: MUSiiC library expects the frame size in the format
109  // as Ultrasonix provide, not like Plus (Plus: if vector data switch width and
110  // height, because the image is not rasterized like a bitmap, but written rayline by rayline)
111  FrameSizeType size = this->m_TrackedFrame.GetFrameSize();
112  imageSizePixels[0] = size[1];
113  imageSizePixels[1] = size[0];
114  imageSizePixels[2] = 1;
115 
116  int scalarType = PlusCommon::GetIGTLScalarPixelTypeFromVTK(this->m_TrackedFrame.GetImageData()->GetVTKScalarPixelType());
117 
118  this->SetDimensions(static_cast<int>(imageSizePixels[0]), static_cast<int>(imageSizePixels[1]), static_cast<int>(imageSizePixels[2]));
119  this->SetSubVolume(static_cast<int>(imageSizePixels[0]), static_cast<int>(imageSizePixels[1]), static_cast<int>(imageSizePixels[2]), offset[0], offset[1], offset[2]);
120  this->SetScalarType(scalarType);
121  this->SetSpacing(0.2, 0.2, 1);
122  this->AllocateScalars();
123 
124  unsigned char* igtlImagePointer = (unsigned char*)(this->GetScalarPointer());
125  unsigned char* plusImagePointer = (unsigned char*)(this->m_TrackedFrame.GetImageData()->GetScalarPointer());
126 
127  memcpy(igtlImagePointer, plusImagePointer, this->GetImageSize());
128 
129  this->SetTimeStamp(igtlFrameTime);
130 
131  this->m_MessageHeader.m_DataType = 0;
132  if (this->m_TrackedFrame.IsFrameFieldDefined("SonixDataType"))
133  {
134  std::string fieldValue = this->m_TrackedFrame.GetFrameField("SonixDataType");
135  igsioCommon::StringToNumber<igtl_int32>(fieldValue, this->m_MessageHeader.m_DataType);
136  }
137 
139  if (this->m_TrackedFrame.IsFrameFieldDefined("SonixTransmitFrequency"))
140  {
141  std::string fieldValue = this->m_TrackedFrame.GetFrameField("SonixTransmitFrequency");
142  igsioCommon::StringToNumber<igtl_int32>(fieldValue, this->m_MessageHeader.m_TransmitFrequency);
143  }
144 
146  if (this->m_TrackedFrame.IsFrameFieldDefined("SonixSamplingFrequency"))
147  {
148  std::string fieldValue = this->m_TrackedFrame.GetFrameField("SonixSamplingFrequency");
149  igsioCommon::StringToNumber<igtl_int32>(fieldValue, this->m_MessageHeader.m_SamplingFrequency);
150  }
151 
152  this->m_MessageHeader.m_DataRate = 0;
153  if (this->m_TrackedFrame.IsFrameFieldDefined("SonixDataRate"))
154  {
155  std::string fieldValue = this->m_TrackedFrame.GetFrameField("SonixDataRate");
156  igsioCommon::StringToNumber<igtl_int32>(fieldValue, this->m_MessageHeader.m_DataRate);
157  }
158 
159  this->m_MessageHeader.m_LineDensity = 0;
160  if (this->m_TrackedFrame.IsFrameFieldDefined("SonixLineDensity"))
161  {
162  std::string fieldValue = this->m_TrackedFrame.GetFrameField("SonixLineDensity");
163  igsioCommon::StringToNumber<igtl_int32>(fieldValue, this->m_MessageHeader.m_LineDensity);
164  }
165 
167  if (this->m_TrackedFrame.IsFrameFieldDefined("SonixSteeringAngle"))
168  {
169  std::string fieldValue = this->m_TrackedFrame.GetFrameField("SonixSteeringAngle");
170  igsioCommon::StringToNumber<igtl_int32>(fieldValue, this->m_MessageHeader.m_SteeringAngle);
171  }
172 
173  this->m_MessageHeader.m_ProbeID = 0;
174  if (this->m_TrackedFrame.IsFrameFieldDefined("SonixProbeID"))
175  {
176  std::string fieldValue = this->m_TrackedFrame.GetFrameField("SonixProbeID");
177  igsioCommon::StringToNumber<igtl_int32>(fieldValue, this->m_MessageHeader.m_ProbeID);
178  }
179 
181  if (this->m_TrackedFrame.IsFrameFieldDefined("SonixExtensionAngle"))
182  {
183  std::string fieldValue = this->m_TrackedFrame.GetFrameField("SonixExtensionAngle");
184  igsioCommon::StringToNumber<igtl_int32>(fieldValue, this->m_MessageHeader.m_ExtensionAngle);
185  }
186 
187  this->m_MessageHeader.m_Elements = 0;
188  if (this->m_TrackedFrame.IsFrameFieldDefined("SonixElements"))
189  {
190  std::string fieldValue = this->m_TrackedFrame.GetFrameField("SonixElements");
191  igsioCommon::StringToNumber<igtl_int32>(fieldValue, this->m_MessageHeader.m_Elements);
192  }
193 
194  this->m_MessageHeader.m_Pitch = 0;
195  if (this->m_TrackedFrame.IsFrameFieldDefined("SonixPitch"))
196  {
197  std::string fieldValue = this->m_TrackedFrame.GetFrameField("SonixPitch");
198  igsioCommon::StringToNumber<igtl_int32>(fieldValue, this->m_MessageHeader.m_Pitch);
199  }
200 
201  this->m_MessageHeader.m_Radius = 0;
202  if (this->m_TrackedFrame.IsFrameFieldDefined("SonixRadius"))
203  {
204  std::string fieldValue = this->m_TrackedFrame.GetFrameField("SonixRadius");
205  igsioCommon::StringToNumber<igtl_int32>(fieldValue, this->m_MessageHeader.m_Radius);
206  }
207 
208  this->m_MessageHeader.m_ProbeAngle = 0;
209  if (this->m_TrackedFrame.IsFrameFieldDefined("SonixProbeAngle"))
210  {
211  std::string fieldValue = this->m_TrackedFrame.GetFrameField("SonixProbeAngle");
212  igsioCommon::StringToNumber<igtl_int32>(fieldValue, this->m_MessageHeader.m_ProbeAngle);
213  }
214 
215  this->m_MessageHeader.m_TxOffset = 0;
216  if (this->m_TrackedFrame.IsFrameFieldDefined("SonixTxOffset"))
217  {
218  std::string fieldValue = this->m_TrackedFrame.GetFrameField("SonixTxOffset");
219  igsioCommon::StringToNumber<igtl_int32>(fieldValue, this->m_MessageHeader.m_TxOffset);
220  }
221 
222  return PLUS_SUCCESS;
223  }
224 
225  //----------------------------------------------------------------------------
227  {
228  return GetSubVolumeImageSize() + IGTL_IMAGE_HEADER_SIZE + this->m_MessageHeader.GetMessageHeaderSize();
229  }
230 
231  //----------------------------------------------------------------------------
233  {
234  igtl::ImageMessage::PackContent();
235 
236  MessageHeader* header = (MessageHeader*)(m_Image + GetSubVolumeImageSize());
237  header->m_DataType = this->m_MessageHeader.m_DataType;
240  header->m_DataRate = this->m_MessageHeader.m_DataRate;
242 
244  header->m_ProbeID = this->m_MessageHeader.m_ProbeID;
246  header->m_Elements = this->m_MessageHeader.m_Elements;
247  header->m_Pitch = this->m_MessageHeader.m_Pitch;
248 
249  header->m_Radius = this->m_MessageHeader.m_Radius;
250  header->m_ProbeAngle = this->m_MessageHeader.m_ProbeAngle;
251  header->m_TxOffset = this->m_MessageHeader.m_TxOffset;
252 
253  // Convert header endian
254  header->ConvertEndianness();
255 
256  return 1;
257  }
258 
259  //----------------------------------------------------------------------------
261  {
262  igtl::ImageMessage::UnpackContent();
263 
264  MessageHeader* header = (MessageHeader*)(m_Image + GetSubVolumeImageSize());
265 
266  // Convert header endian
267  header->ConvertEndianness();
268 
269  this->m_MessageHeader.m_DataType = header->m_DataType;
272  this->m_MessageHeader.m_DataRate = header->m_DataRate;
274 
276  this->m_MessageHeader.m_ProbeID = header->m_ProbeID;
278  this->m_MessageHeader.m_Elements = header->m_Elements;
279  this->m_MessageHeader.m_Pitch = header->m_Pitch;
280 
281  this->m_MessageHeader.m_Radius = header->m_Radius;
282  this->m_MessageHeader.m_ProbeAngle = header->m_ProbeAngle;
283  this->m_MessageHeader.m_TxOffset = header->m_TxOffset;
284 
285  return 1;
286  }
287 } //namespace igtl
PlusStatus SetTrackedFrame(const igsioTrackedFrame &trackedFrame)
double * timestamp
Definition: phidget22.h:3432
igsioTrackedFrame m_TrackedFrame
igsioStatus PlusStatus
Definition: PlusCommon.h:40
virtual igtlUint64 CalculateContentBufferSize()
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
igsioTrackedFrame & GetTrackedFrame()
virtual igtl::MessageBase::Pointer Clone()
MessageHeader m_MessageHeader