47 #include "vtkImageData.h"
49 #include <igtl_util.h>
60 igtl::ImageMessage::Pointer retval = igtl::ImageMessage::New();
64 this->encode_vtkImageData(image->getBaseVtkImageData(), retval);
65 this->encode_rMd(image, retval, externalSpace);
74 QString deviceName = msg->GetDeviceName();
77 retval->setAcquisitionTime(timestamp);
78 this->decode_rMd(msg, retval);
89 int swapCopy16(igtlUint16 * dst, igtlUint16 * src,
int n)
91 igtlUint16 * esrc = src + n;
94 *dst = BYTE_SWAP_INT16(*src);
101 int swapCopy32(igtlUint32 * dst, igtlUint32 * src,
int n)
103 igtlUint32 * esrc = src + n;
106 *dst = BYTE_SWAP_INT32(*src);
113 int swapCopy64(igtlUint64 * dst, igtlUint64 * src,
int n)
115 igtlUint64 * esrc = src + n;
118 *dst = BYTE_SWAP_INT64(*src);
126 vtkImageDataPtr IGTLinkConversionImage::decode_vtkImageData(igtl::ImageMessage *imgMsg)
141 scalarType = IGTLToVTKScalarType( imgMsg->GetScalarType() );
142 endian = imgMsg->GetEndian();
143 imgMsg->GetDimensions(size);
144 imgMsg->GetSpacing(spacing);
145 numComponents = imgMsg->GetNumComponents();
146 imgMsg->GetSubVolume(svsize, svoffset);
150 int sizeInNode[3]={0,0,0};
151 int scalarTypeInNode=VTK_VOID;
152 int numComponentsInNode=0;
154 vtkSmartPointer<vtkImageData> imageData = vtkSmartPointer<vtkImageData>::New();
155 imageData->SetDimensions(size[0], size[1], size[2]);
156 imageData->SetExtent(0, size[0]-1, 0, size[1]-1, 0, size[2]-1);
157 imageData->SetOrigin(0.0, 0.0, 0.0);
159 imageData->SetSpacing(spacing[0], spacing[1], spacing[2]);
160 imageData->AllocateScalars(scalarType, numComponents);
163 int scalarSize = imgMsg->GetScalarSize();
167 if (scalarSize > 1 &&
168 ((igtl_is_little_endian() && endian == igtl::ImageMessage::ENDIAN_BIG) ||
169 (!igtl_is_little_endian() && endian == igtl::ImageMessage::ENDIAN_LITTLE)))
175 if (imgMsg->GetImageSize() == imgMsg->GetSubVolumeImageSize())
185 swapCopy16((igtlUint16 *)imageData->GetScalarPointer(),
186 (igtlUint16 *)imgMsg->GetScalarPointer(),
187 imgMsg->GetSubVolumeImageSize() / 2);
190 swapCopy32((igtlUint32 *)imageData->GetScalarPointer(),
191 (igtlUint32 *)imgMsg->GetScalarPointer(),
192 imgMsg->GetSubVolumeImageSize() / 4);
195 swapCopy64((igtlUint64 *)imageData->GetScalarPointer(),
196 (igtlUint64 *)imgMsg->GetScalarPointer(),
197 imgMsg->GetSubVolumeImageSize() / 8);
205 memcpy(imageData->GetScalarPointer(),
206 imgMsg->GetScalarPointer(), imgMsg->GetSubVolumeImageSize());
214 char* imgPtr = (
char*) imageData->GetScalarPointer();
215 char* bufPtr = (
char*) imgMsg->GetScalarPointer();
219 int subsizei = svsize[0];
221 int bg_i = svoffset[0];
223 int bg_j = svoffset[1];
224 int ed_j = bg_j + svsize[1];
225 int bg_k = svoffset[2];
226 int ed_k = bg_k + svsize[2];
233 for (
int k = bg_k; k < ed_k; k ++)
235 for (
int j = bg_j; j < ed_j; j ++)
237 swapCopy16((igtlUint16 *)&imgPtr[(sizei*sizej*k + sizei*j + bg_i)*scalarSize],
238 (igtlUint16 *)bufPtr,
240 bufPtr += subsizei*scalarSize;
245 for (
int k = bg_k; k < ed_k; k ++)
247 for (
int j = bg_j; j < ed_j; j ++)
249 swapCopy32((igtlUint32 *)&imgPtr[(sizei*sizej*k + sizei*j + bg_i)*scalarSize],
250 (igtlUint32 *)bufPtr,
252 bufPtr += subsizei*scalarSize;
257 for (
int k = bg_k; k < ed_k; k ++)
259 for (
int j = bg_j; j < ed_j; j ++)
261 swapCopy64((igtlUint64 *)&imgPtr[(sizei*sizej*k + sizei*j + bg_i)*scalarSize],
262 (igtlUint64 *)bufPtr,
264 bufPtr += subsizei*scalarSize;
274 for (
int k = bg_k; k < ed_k; k ++)
276 for (
int j = bg_j; j < ed_j; j ++)
278 memcpy(&imgPtr[(sizei*sizej*k + sizei*j + bg_i)*scalarSize],
279 bufPtr, subsizei*scalarSize);
280 bufPtr += subsizei*scalarSize;
287 imageData->Modified();
291 void IGTLinkConversionImage::encode_vtkImageData(
vtkImageDataPtr in, igtl::ImageMessage *outmsg)
301 int svoffset[] = {0, 0, 0};
304 scalarType = imageData->GetScalarType();
305 imageData->GetDimensions(isize);
306 spacing = imageData->GetSpacing();
307 int numComponents = imageData->GetNumberOfScalarComponents();
310 endian = igtl::ImageMessage::ENDIAN_BIG;
311 if (igtl_is_little_endian())
313 endian = igtl::ImageMessage::ENDIAN_LITTLE;
316 outmsg->SetDimensions(isize);
317 outmsg->SetSpacing((
float)spacing[0], (
float)spacing[1], (
float)spacing[2]);
318 outmsg->SetScalarType(scalarType);
319 outmsg->SetEndian(endian);
320 outmsg->SetSubVolume(isize, svoffset);
321 outmsg->SetNumComponents(numComponents);
322 outmsg->AllocateScalars();
324 memcpy(outmsg->GetScalarPointer(),
325 imageData->GetScalarPointer(),
326 outmsg->GetImageSize());
330 void IGTLinkConversionImage::decode_rMd(igtl::ImageMessage *msg,
ImagePtr out)
334 Vector3D c = out->boundingBox().center();
338 PATIENT_COORDINATE_SYSTEM s = this->getPatientCoordinateSystem(msg->GetCoordinateSystem());
343 out->get_rMd_History()->setRegistration(rMd);
346 int IGTLinkConversionImage::getIgtlCoordinateSystem(PATIENT_COORDINATE_SYSTEM space)
const
349 return igtl::ImageMessage::COORDINATE_RAS;
351 return igtl::ImageMessage::COORDINATE_LPS;
353 return igtl::ImageMessage::COORDINATE_RAS;
356 PATIENT_COORDINATE_SYSTEM IGTLinkConversionImage::getPatientCoordinateSystem(
int igtlSpace)
const
358 if (igtlSpace==igtl::ImageMessage::COORDINATE_RAS)
360 if (igtlSpace==igtl::ImageMessage::COORDINATE_LPS)
366 void IGTLinkConversionImage::encode_rMd(
ImagePtr image, igtl::ImageMessage *outmsg, PATIENT_COORDINATE_SYSTEM externalSpace)
376 outmsg->SetCoordinateSystem(this->getIgtlCoordinateSystem(externalSpace));
392 Vector3D c = image->boundingBox().center();
400 this->setMatrix(outmsg, sMigtl);
403 Transform3D IGTLinkConversionImage::getMatrix(igtl::ImageMessage *msg)
405 igtl::Matrix4x4 matrix;
406 msg->GetMatrix(matrix);
407 return Transform3D::fromFloatArray(matrix);
410 void IGTLinkConversionImage::setMatrix(igtl::ImageMessage *msg,
Transform3D matrix)
413 for (
int r = 0; r < 4; ++r)
414 for (
int c = 0; c < 4; ++c)
415 m[r][c] = matrix(r,c);
420 int IGTLinkConversionImage::IGTLToVTKScalarType(
int igtlType)
424 case igtl::ImageMessage::TYPE_INT8:
return VTK_CHAR;
425 case igtl::ImageMessage::TYPE_UINT8:
return VTK_UNSIGNED_CHAR;
426 case igtl::ImageMessage::TYPE_INT16:
return VTK_SHORT;
427 case igtl::ImageMessage::TYPE_UINT16:
return VTK_UNSIGNED_SHORT;
428 case igtl::ImageMessage::TYPE_INT32:
return VTK_UNSIGNED_LONG;
429 case igtl::ImageMessage::TYPE_UINT32:
return VTK_UNSIGNED_LONG;
430 case igtl::ImageMessage::TYPE_FLOAT32:
return VTK_FLOAT;
431 case igtl::ImageMessage::TYPE_FLOAT64:
return VTK_DOUBLE;
pcsRAS
Right-Anterior-Superior, used by Slicer3D, ITK-Snap, nifti.
QDateTime decode_timestamp(igtl::MessageBase *msg)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
cxResource_EXPORT Transform3D createTransformFromReferenceToExternal(PATIENT_COORDINATE_SYSTEM external)
boost::shared_ptr< class Image > ImagePtr
cstring_cast_Placeholder cstring_cast(const T &val)
ImagePtr decode(igtl::ImageMessage *in)
Transform3D createTransformTranslate(const Vector3D &translation)
pcsLPS
Left-Posterior-Superior, used internally by CustusX, also DICOM, ITK.
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
igtl::ImageMessage::Pointer encode(ImagePtr in, PATIENT_COORDINATE_SYSTEM externalSpace)
void encode_timestamp(QDateTime ts, igtl::MessageBase *msg)
#define CX_LOG_CHANNEL_ERROR(channel)