36 #include <vtkImageImport.h>
37 #include <vtkImageData.h>
39 #include <vtkImageExtractComponents.h>
40 #include <vtkImageAppendComponents.h>
51 void GetRandomTestMatrix(igtl::Matrix4x4& matrix)
53 matrix[0][0] = 1.0; matrix[1][0] = 0.0; matrix[2][0] = 0.0; matrix[3][0] = 0.0;
54 matrix[0][1] = 0.0; matrix[1][1] = -1.0; matrix[2][1] = 0.0; matrix[3][1] = 0.0;
55 matrix[0][2] = 0.0; matrix[1][2] = 0.0; matrix[2][2] = 1.0; matrix[3][2] = 0.0;
56 matrix[0][3] = 0.0; matrix[1][3] = 0.0; matrix[2][3] = 0.0; matrix[3][3] = 1.0;
78 int size[] = {256, 256, 1};
79 rawImage->GetDimensions(size);
85 rawImage->GetSpacing(spacingD);
86 spacingF[0] = spacingD[0];
87 spacingF[1] = spacingD[1];
88 spacingF[2] = spacingD[2];
90 int svoffset[] = {0, 0, 0};
93 if (rawImage->GetNumberOfScalarComponents()==4)
95 if (rawImage->GetScalarType()==VTK_UNSIGNED_CHAR)
97 scalarType = igtl::ImageMessage::TYPE_UINT32;
101 if (rawImage->GetNumberOfScalarComponents()==1)
103 if (rawImage->GetScalarType()==VTK_UNSIGNED_SHORT)
105 scalarType = igtl::ImageMessage::TYPE_UINT16;
107 else if (rawImage->GetScalarType()==VTK_UNSIGNED_CHAR)
109 scalarType = igtl::ImageMessage::TYPE_UINT8;
115 std::cerr <<
"Unsupported file type based on vtk " << rawImage->GetScalarTypeAsString() << std::endl;
122 imgMsg->SetDimensions(size);
123 imgMsg->SetSpacing(spacingF);
124 imgMsg->SetScalarType(scalarType);
126 imgMsg->SetSubVolume(svsize, svoffset);
127 imgMsg->AllocateScalars();
129 QDateTime lastGrabTime = image->getAcquisitionTime();
130 igtl::TimeStamp::Pointer timestamp;
131 timestamp = igtl::TimeStamp::New();
133 double grabTime = 1.0 / 1000 * (double) lastGrabTime.toMSecsSinceEpoch();
134 timestamp->SetTime(grabTime);
135 imgMsg->SetTimeStamp(timestamp);
141 int fsize = imgMsg->GetImageSize();
144 memcpy(imgMsg->GetScalarPointer(), rawImage->GetScalarPointer(0,0,0), fsize);
148 igtl::Matrix4x4 matrix;
149 GetRandomTestMatrix(matrix);
150 imgMsg->SetMatrix(matrix);
171 scalarType = message->GetScalarType();
172 message->GetDimensions(size);
173 message->GetSpacing(spacing);
174 message->GetSubVolume(svsize, svoffset);
176 QString deviceName = message->GetDeviceName();
179 imageImport->SetNumberOfScalarComponents(1);
183 case IGTLinkImageMessage::TYPE_INT8:
184 std::cout <<
"signed char is not supported. Falling back to unsigned char." << std::endl;
185 imageImport->SetDataScalarTypeToUnsignedChar();
187 case IGTLinkImageMessage::TYPE_UINT8:
188 imageImport->SetDataScalarTypeToUnsignedChar();
190 case IGTLinkImageMessage::TYPE_INT16:
191 imageImport->SetDataScalarTypeToShort();
193 case IGTLinkImageMessage::TYPE_UINT16:
196 imageImport->SetNumberOfScalarComponents(2);
197 imageImport->SetDataScalarTypeToUnsignedChar();
199 case IGTLinkImageMessage::TYPE_INT32:
200 case IGTLinkImageMessage::TYPE_UINT32:
203 imageImport->SetNumberOfScalarComponents(4);
205 imageImport->SetDataScalarTypeToUnsignedChar();
208 case IGTLinkImageMessage::TYPE_FLOAT32:
209 imageImport->SetDataScalarTypeToFloat();
211 case IGTLinkImageMessage::TYPE_FLOAT64:
212 imageImport->SetDataScalarTypeToDouble();
215 std::cout <<
"unknown type. Falling back to unsigned char." << std::endl;
216 imageImport->SetDataScalarTypeToUnsignedChar();
220 igtl::TimeStamp::Pointer timestamp = igtl::TimeStamp::New();
221 message->GetTimeStamp(timestamp);
223 double timestampMS = timestamp->GetTimeStamp() * 1000;
224 imageImport->SetDataOrigin(0, 0, 0);
225 imageImport->SetDataSpacing(spacing[0], spacing[1], spacing[2]);
226 imageImport->SetWholeExtent(0, size[0] - 1, 0, size[1] - 1, 0, size[2] - 1);
227 imageImport->SetDataExtentToWholeExtent();
228 imageImport->SetImportVoidPointer(message->GetScalarPointer());
230 imageImport->Modified();
231 imageImport->Update();
233 ImagePtr retval(
new Image(deviceName, imageImport->GetOutput()));
234 retval->setAcquisitionTime(QDateTime::fromMSecsSinceEpoch(timestampMS));
235 retval = this->
decode(retval);
253 retval->SetOrigin(input->getOrigin_p().data());
255 retval->SetProbeType(input->getType());
257 retval->SetDepthStart(input->getDepthStart());
258 retval->SetDepthEnd(input->getDepthEnd());
259 retval->SetWidth(input->getWidth());
285 probeMessage->GetDepthStart(),
286 probeMessage->GetDepthEnd(),
287 probeMessage->GetWidth(),
289 retval->setOrigin_p(
Vector3D(probeMessage->GetOrigin()));
290 retval->setUid(probeMessage->GetDeviceName());
300 imageMessage->GetDimensions(size);
301 imageMessage->GetSpacing(spacing);
303 retval->setSpacing(
Vector3D(spacing[0], spacing[1], spacing[2]));
304 retval->setSize(QSize(size[0], size[1]));
305 retval->setClipRect_p(
DoubleBoundingBox3D(0, retval->getSize().width(), 0, retval->getSize().height(), 0, 0));
308 return this->
decode(retval);
313 QString newUid = msg->getUid();
314 QString format = this->extractColorFormat(msg->getUid(), &newUid);
315 vtkImageDataPtr imageRGB = this->createFilterFormat2RGB(format, msg->getBaseVtkImageData());
320 copy->DeepCopy(imageRGB);
323 retval->setAcquisitionTime(msg->getAcquisitionTime());
329 QString newUid = msg->getUid();
330 QString format = this->extractColorFormat(msg->getUid(), &newUid);
336 QString IGTLinkConversion::extractColorFormat(QString deviceName, QString* cleanedDeviceName)
339 QRegExp colorFormat(
"\\[[A-Za-z]{1,4}\\]");
340 if (colorFormat.indexIn(deviceName) > 0)
342 format = colorFormat.cap(0).remove(
"[").remove(
"]");
345 if (cleanedDeviceName)
347 *cleanedDeviceName = deviceName.remove(colorFormat).trimmed();
355 int R = format.indexOf(
'R', 0, Qt::CaseInsensitive);
356 int G = format.indexOf(
'G', 0, Qt::CaseInsensitive);
357 int B = format.indexOf(
'B', 0, Qt::CaseInsensitive);
358 if (R<0 || G<0 || B<0 || format.size()>4)
365 return this->createFilterAny2RGB(R, G, B, input);
371 if (input->GetNumberOfScalarComponents() == 1)
373 if (( input->GetNumberOfScalarComponents()==3 )&&( R==0 )&&( G==1 )&&( B==2 ))
378 splitterRGB->SetInputData(input);
379 splitterRGB->SetComponents(R, G, B);
380 merger->AddInputConnection(splitterRGB->GetOutputPort());
383 return merger->GetOutput();
boost::shared_ptr< class Image > ImagePtr
IGTLinkImageMessage::Pointer encode(ImagePtr image)
cstring_cast_Placeholder cstring_cast(const T &val)
vtkSmartPointer< class vtkImageAppendComponents > vtkImageAppendComponentsPtr
igtl::SmartPointer< Self > Pointer
Representation of a floating-point bounding box in 3D. The data are stored as {xmin,xmax,ymin,ymax,zmin,zmax}, in order to simplify communication with vtk.
virtual ~IGTLinkConversion()
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition of characteristics for an Ultrasound Probe Sector.
vtkSmartPointer< class vtkImageExtractComponents > vtkImageExtractComponentsPtr
vtkSmartPointer< class vtkImageImport > vtkImageImportPtr
ImagePtr decode(IGTLinkImageMessage::Pointer msg)
igtl::SmartPointer< Self > Pointer
boost::shared_ptr< class ProbeDefinition > ProbeDefinitionPtr
vtkSmartPointer< vtkImageImport > vtkImageImportPtr