26 #include "vtkImageData.h" 28 #include <igtl_util.h> 39 igtl::ImageMessage::Pointer retval = igtl::ImageMessage::New();
43 this->encode_vtkImageData(image->getBaseVtkImageData(), retval);
44 this->encode_rMd(image, retval, externalSpace);
53 QString deviceName = msg->GetDeviceName();
56 retval->setAcquisitionTime(timestamp);
57 this->decode_rMd(msg, retval);
69 int swapCopy16(igtlUint16 * dst, igtlUint16 * src,
int n)
71 igtlUint16 * esrc = src + n;
74 *dst = BYTE_SWAP_INT16(*src);
81 int swapCopy32(igtlUint32 * dst, igtlUint32 * src,
int n)
83 igtlUint32 * esrc = src + n;
86 *dst = BYTE_SWAP_INT32(*src);
93 int swapCopy64(igtlUint64 * dst, igtlUint64 * src,
int n)
95 igtlUint64 * esrc = src + n;
98 *dst = BYTE_SWAP_INT64(*src);
106 vtkImageDataPtr IGTLinkConversionImage::decode_vtkImageData(igtl::ImageMessage *imgMsg)
121 scalarType = IGTLToVTKScalarType( imgMsg->GetScalarType() );
122 endian = imgMsg->GetEndian();
123 imgMsg->GetDimensions(size);
124 imgMsg->GetSpacing(spacing);
125 numComponents = imgMsg->GetNumComponents();
126 imgMsg->GetSubVolume(svsize, svoffset);
130 int sizeInNode[3]={0,0,0};
131 int scalarTypeInNode=VTK_VOID;
132 int numComponentsInNode=0;
134 vtkSmartPointer<vtkImageData> imageData = vtkSmartPointer<vtkImageData>::New();
135 imageData->SetDimensions(size[0], size[1], size[2]);
136 imageData->SetExtent(0, size[0]-1, 0, size[1]-1, 0, size[2]-1);
137 imageData->SetOrigin(0.0, 0.0, 0.0);
139 imageData->SetSpacing(spacing[0], spacing[1], spacing[2]);
140 imageData->AllocateScalars(scalarType, numComponents);
143 int scalarSize = imgMsg->GetScalarSize();
147 if (scalarSize > 1 &&
148 ((igtl_is_little_endian() && endian == igtl::ImageMessage::ENDIAN_BIG) ||
149 (!igtl_is_little_endian() && endian == igtl::ImageMessage::ENDIAN_LITTLE)))
155 if (imgMsg->GetImageSize() == imgMsg->GetSubVolumeImageSize())
165 swapCopy16((igtlUint16 *)imageData->GetScalarPointer(),
166 (igtlUint16 *)imgMsg->GetScalarPointer(),
167 imgMsg->GetSubVolumeImageSize() / 2);
170 swapCopy32((igtlUint32 *)imageData->GetScalarPointer(),
171 (igtlUint32 *)imgMsg->GetScalarPointer(),
172 imgMsg->GetSubVolumeImageSize() / 4);
175 swapCopy64((igtlUint64 *)imageData->GetScalarPointer(),
176 (igtlUint64 *)imgMsg->GetScalarPointer(),
177 imgMsg->GetSubVolumeImageSize() / 8);
185 memcpy(imageData->GetScalarPointer(),
186 imgMsg->GetScalarPointer(), imgMsg->GetSubVolumeImageSize());
194 char* imgPtr = (
char*) imageData->GetScalarPointer();
195 char* bufPtr = (
char*) imgMsg->GetScalarPointer();
199 int subsizei = svsize[0];
201 int bg_i = svoffset[0];
203 int bg_j = svoffset[1];
204 int ed_j = bg_j + svsize[1];
205 int bg_k = svoffset[2];
206 int ed_k = bg_k + svsize[2];
213 for (
int k = bg_k; k < ed_k; k ++)
215 for (
int j = bg_j; j < ed_j; j ++)
217 swapCopy16((igtlUint16 *)&imgPtr[(sizei*sizej*k + sizei*j + bg_i)*scalarSize],
218 (igtlUint16 *)bufPtr,
220 bufPtr += subsizei*scalarSize;
225 for (
int k = bg_k; k < ed_k; k ++)
227 for (
int j = bg_j; j < ed_j; j ++)
229 swapCopy32((igtlUint32 *)&imgPtr[(sizei*sizej*k + sizei*j + bg_i)*scalarSize],
230 (igtlUint32 *)bufPtr,
232 bufPtr += subsizei*scalarSize;
237 for (
int k = bg_k; k < ed_k; k ++)
239 for (
int j = bg_j; j < ed_j; j ++)
241 swapCopy64((igtlUint64 *)&imgPtr[(sizei*sizej*k + sizei*j + bg_i)*scalarSize],
242 (igtlUint64 *)bufPtr,
244 bufPtr += subsizei*scalarSize;
254 for (
int k = bg_k; k < ed_k; k ++)
256 for (
int j = bg_j; j < ed_j; j ++)
258 memcpy(&imgPtr[(sizei*sizej*k + sizei*j + bg_i)*scalarSize],
259 bufPtr, subsizei*scalarSize);
260 bufPtr += subsizei*scalarSize;
267 imageData->Modified();
271 void IGTLinkConversionImage::encode_vtkImageData(
vtkImageDataPtr in, igtl::ImageMessage *outmsg)
281 int svoffset[] = {0, 0, 0};
284 scalarType = imageData->GetScalarType();
285 imageData->GetDimensions(isize);
286 spacing = imageData->GetSpacing();
287 int numComponents = imageData->GetNumberOfScalarComponents();
290 endian = igtl::ImageMessage::ENDIAN_BIG;
291 if (igtl_is_little_endian())
293 endian = igtl::ImageMessage::ENDIAN_LITTLE;
296 outmsg->SetDimensions(isize);
297 outmsg->SetSpacing((
float)spacing[0], (
float)spacing[1], (
float)spacing[2]);
298 outmsg->SetScalarType(scalarType);
299 outmsg->SetEndian(endian);
300 outmsg->SetSubVolume(isize, svoffset);
301 outmsg->SetNumComponents(numComponents);
302 outmsg->AllocateScalars();
304 memcpy(outmsg->GetScalarPointer(),
305 imageData->GetScalarPointer(),
306 outmsg->GetImageSize());
310 void IGTLinkConversionImage::decode_rMd(igtl::ImageMessage *msg,
ImagePtr out)
314 Vector3D c = out->boundingBox().center();
318 PATIENT_COORDINATE_SYSTEM s = this->getPatientCoordinateSystem(msg->GetCoordinateSystem());
323 out->get_rMd_History()->setRegistration(rMd);
326 int IGTLinkConversionImage::getIgtlCoordinateSystem(PATIENT_COORDINATE_SYSTEM space)
const 329 return igtl::ImageMessage::COORDINATE_RAS;
331 return igtl::ImageMessage::COORDINATE_LPS;
333 return igtl::ImageMessage::COORDINATE_RAS;
336 PATIENT_COORDINATE_SYSTEM IGTLinkConversionImage::getPatientCoordinateSystem(
int igtlSpace)
const 338 if (igtlSpace==igtl::ImageMessage::COORDINATE_RAS)
340 if (igtlSpace==igtl::ImageMessage::COORDINATE_LPS)
346 void IGTLinkConversionImage::encode_rMd(
ImagePtr image, igtl::ImageMessage *outmsg, PATIENT_COORDINATE_SYSTEM externalSpace)
356 outmsg->SetCoordinateSystem(this->getIgtlCoordinateSystem(externalSpace));
372 Vector3D c = image->boundingBox().center();
380 this->setMatrix(outmsg, sMigtl);
383 Transform3D IGTLinkConversionImage::getMatrix(igtl::ImageMessage *msg)
385 igtl::Matrix4x4 matrix;
386 msg->GetMatrix(matrix);
387 return Transform3D::fromFloatArray(matrix);
390 void IGTLinkConversionImage::setMatrix(igtl::ImageMessage *msg,
Transform3D matrix)
393 for (
int r = 0; r < 4; ++r)
394 for (
int c = 0; c < 4; ++c)
395 m[r][c] = matrix(r,c);
400 int IGTLinkConversionImage::IGTLToVTKScalarType(
int igtlType)
404 case igtl::ImageMessage::TYPE_INT8:
return VTK_CHAR;
405 case igtl::ImageMessage::TYPE_UINT8:
return VTK_UNSIGNED_CHAR;
406 case igtl::ImageMessage::TYPE_INT16:
return VTK_SHORT;
407 case igtl::ImageMessage::TYPE_UINT16:
return VTK_UNSIGNED_SHORT;
408 case igtl::ImageMessage::TYPE_INT32:
return VTK_UNSIGNED_LONG;
409 case igtl::ImageMessage::TYPE_UINT32:
return VTK_UNSIGNED_LONG;
410 case igtl::ImageMessage::TYPE_FLOAT32:
return VTK_FLOAT;
411 case igtl::ImageMessage::TYPE_FLOAT64:
return VTK_DOUBLE;
pcsRAS
Right-Anterior-Superior, used by Slicer3D, ITK-Snap, nifti, MINC.
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)
Namespace for all CustusX production code.