Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lauras changes #86

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ include(GenerateExportHeader)
# Find dependencies
find_package(OpenIGTLink REQUIRED NO_MODULE)
find_package(VTK REQUIRED NO_MODULE)
if(VTK_VERSION VERSION_LESS 8.9.0)
set(IGTLIO_VTK_PREFIX vtk)
set(IGTLIO_MODULE_PREFIX vtk)
else()
set(IGTLIO_VTK_PREFIX VTK::)
set(IGTLIO_MODULE_PREFIX "")
endif()
include(${OpenIGTLink_USE_FILE})

###########################################################
Expand Down Expand Up @@ -172,7 +179,7 @@ install(EXPORT OpenIGTLinkIO
FILE "OpenIGTLinkIOTargets.cmake"
)

install(FILES ${CMAKE_BINARY_DIR}/CMakeFiles/install/OpenIGTLinkIOConfig.cmake
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/install/OpenIGTLinkIOConfig.cmake
DESTINATION "${OpenIGTLinkIO_PACKAGE_INSTALL}"
COMPONENT CMakeFiles
)
11 changes: 8 additions & 3 deletions Converter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ project(igtlioConverter)
set(${PROJECT_NAME}_EXPORT_DIRECTIVE "OPENIGTLINKIO_CONVERTER_EXPORT")

set(VTK_MODULES
vtkIOImage
vtkImagingMath
${IGTLIO_MODULE_PREFIX}IOImage
${IGTLIO_MODULE_PREFIX}ImagingMath
)

find_package(VTK REQUIRED NO_MODULE
COMPONENTS
${VTK_MODULES}
)
include(${VTK_USE_FILE})

if(VTK_VERSION VERSION_LESS 8.9.0)
include(${VTK_USE_FILE})
endif()

set(${PROJECT_NAME}_INCLUDE_DIRECTORIES PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
Expand All @@ -31,6 +34,7 @@ set(${PROJECT_NAME}_SRCS
igtlioStringConverter.cxx
igtlioTransformConverter.cxx
igtlioTrackingDataConverter.cxx
igtlioNDArrayConverter.cxx
)

set(${PROJECT_NAME}_HDRS
Expand All @@ -47,6 +51,7 @@ set(${PROJECT_NAME}_HDRS
igtlioTransformConverter.h
igtlioUsSectorDefinitions.h
igtlioTrackingDataConverter.h
igtlioNDArrayConverter.h
)

IF(OpenIGTLink_ENABLE_VIDEOSTREAMING)
Expand Down
20 changes: 15 additions & 5 deletions Converter/igtlioImageConverter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
#include <igtl_util.h>
#include <igtlImageMessage.h>

#include <vtkDataArray.h>
#include <vtkImageData.h>
#include <vtkMatrix4x4.h>
#include <vtkPointData.h>
#include <vtkVersion.h>

namespace // unnamed namespace
Expand Down Expand Up @@ -115,6 +117,7 @@ int igtlioImageConverter::IGTLToVTKImageData(igtl::ImageMessage::Pointer imgMsg,
// Retrieve the image data
int size[3]; // image dimension
float spacing[3]; // spacing (mm/pixel)
float origin[3]; // origin (mm)
int svsize[3]; // sub-volume size
int svoffset[3]; // sub-volume offset
int scalarType; // VTK scalar type
Expand All @@ -125,10 +128,11 @@ int igtlioImageConverter::IGTLToVTKImageData(igtl::ImageMessage::Pointer imgMsg,
endian = imgMsg->GetEndian();
imgMsg->GetDimensions(size);
imgMsg->GetSpacing(spacing);
imgMsg->GetOrigin(origin);
numComponents = imgMsg->GetNumComponents();
imgMsg->GetSubVolume(svsize, svoffset);

// check if the IGTL data fits to the current MRML node
// check if the IGTL data fits to the current MRML node
int sizeInNode[3]={0,0,0};
int scalarTypeInNode=VTK_VOID;
int numComponentsInNode=0;
Expand All @@ -139,7 +143,7 @@ int igtlioImageConverter::IGTLToVTKImageData(igtl::ImageMessage::Pointer imgMsg,
scalarTypeInNode = imageData->GetScalarType();
numComponentsInNode = imageData->GetNumberOfScalarComponents();
}

if (imageData.GetPointer()==NULL
|| sizeInNode[0] != size[0] || sizeInNode[1] != size[1] || sizeInNode[2] != size[2]
|| scalarType != scalarTypeInNode
Expand All @@ -149,8 +153,8 @@ int igtlioImageConverter::IGTLToVTKImageData(igtl::ImageMessage::Pointer imgMsg,
dest->image = imageData;
imageData->SetDimensions(size[0], size[1], size[2]);
imageData->SetExtent(0, size[0]-1, 0, size[1]-1, 0, size[2]-1);
imageData->SetOrigin(0.0, 0.0, 0.0);
imageData->SetSpacing(1.0, 1.0, 1.0);
imageData->SetOrigin(origin[0], origin[1], origin[2]);
imageData->SetSpacing(spacing[0], spacing[1], spacing[2]);
#if (VTK_MAJOR_VERSION <= 5)
imageData->SetNumberOfScalarComponents(numComponents);
imageData->SetScalarType(scalarType);
Expand Down Expand Up @@ -284,7 +288,13 @@ int igtlioImageConverter::IGTLToVTKImageData(igtl::ImageMessage::Pointer imgMsg,
}

}


// Mark scalars as modified
// This is required for getting up-to-date scalar range.
if (imageData->GetPointData() && imageData->GetPointData()->GetScalars())
{
imageData->GetPointData()->GetScalars()->Modified();
}
imageData->Modified();

return 1;
Expand Down
1 change: 1 addition & 0 deletions Converter/igtlioImageMetaConverter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,6 @@ int igtlioImageMetaConverter::toIGTL(const HeaderData& header, const ContentData
msg->AddImageMetaElement(imageMetaElement);
}

msg->Pack();
return 1;
}
1 change: 1 addition & 0 deletions Converter/igtlioLabelMetaConverter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@ int igtlioLabelMetaConverter::toIGTL(const HeaderData& header, const ContentData
msg->AddLabelMetaElement(labelMetaElement);
}

msg->Pack();
return 1;
}
56 changes: 56 additions & 0 deletions Converter/igtlioNDArrayConverter.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "igtlioNDArrayConverter.h"
#include "igtlNDArrayMessage.h"
#include <igtl_util.h>
#include <vtkDataArray.h>
#include <vtkDoubleArray.h>
#include <vtkUnsignedIntArray.h>
#include <vtkCollection.h>

int igtlioNDArrayConverter::fromIGTL(igtl::MessageBase::Pointer source,
HeaderData* header,
ContentData* dest,
bool checkCRC,
igtl::MessageBase::MetaDataMap& outMetaInfo)
{
igtl::NDArrayMessage::Pointer msg;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 space indentation

msg = igtl::NDArrayMessage::New();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a factory to create the message

msg->Copy(source);

int c = msg->Unpack(checkCRC);
if ((c & igtl::MessageHeader::UNPACK_BODY == 0))
{
return 0;
}

if (!IGTLtoHeader(dynamic_pointer_cast<igtl::MessageBase>(msg), header, outMetaInfo))
return 0;

vtkSmartPointer<vtkCollection> collection = vtkSmartPointer<vtkCollection>::New();

for (int i = 0; i < msg->GetArray()->GetSize()[0]; ++i) {
vtkSmartPointer<vtkDoubleArray> NDArray_msg = vtkSmartPointer<vtkDoubleArray>::New();
NDArray_msg->SetNumberOfTuples(msg->GetArray()->GetSize()[0]);
memcpy(NDArray_msg->GetPointer(0), static_cast<char*>(msg->GetArray()->GetRawArray()) + (NDArray_msg->GetDataSize() * NDArray_msg->GetDataTypeSize() * i), NDArray_msg->GetDataSize() * NDArray_msg->GetDataTypeSize());
collection->AddItem(NDArray_msg);
}
dest->collection = collection;
return 1;
}

int igtlioNDArrayConverter::toIGTL(const HeaderData& header, const ContentData& source, igtl::NDArrayMessage::Pointer* dest, igtl::MessageBase::MetaDataMap metaInfo)
{
if (dest->IsNull())
*dest = igtl::NDArrayMessage::New();
(*dest)->InitPack();
igtl::NDArrayMessage::Pointer msg = *dest;

if (!metaInfo.empty())
{
msg->SetHeaderVersion(IGTL_HEADER_VERSION_2);
}
igtl::MessageBase::Pointer basemsg = dynamic_pointer_cast<igtl::MessageBase>(msg);
HeadertoIGTL(header, &basemsg, metaInfo);

msg->Pack();
return 1;
}
27 changes: 27 additions & 0 deletions Converter/igtlioNDArrayConverter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef NDARRAYCONVERTER_H
#define NDARRAYCONVERTER_H

#include "igtlioConverterExport.h"
#include <igtlNDArrayMessage.h>
#include "igtlioBaseConverter.h"
class vtkDataArray;
typedef vtkSmartPointer<class igtlioNDArrayDevice> igtlioNDArrayDevicePointer;
class OPENIGTLINKIO_CONVERTER_EXPORT igtlioNDArrayConverter : public igtlioBaseConverter
{
public:


struct ContentData
{
vtkSmartPointer<vtkDataArray> NDArray_msg;
};
Copy link
Collaborator

@adamrankin adamrankin Nov 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ContentData will also have to store what kind of scalar the array is. Use the IGTL type defines for this.


static const char* GetIGTLName() { return GetIGTLTypeName(); }
static const char* GetIGTLTypeName() {return "ARRAY"; }

static int fromIGTL(igtl::MessageBase::Pointer source, HeaderData* header, ContentData* content, bool checkCRC, igtl::MessageBase::MetaDataMap& outMetaInfo);
static int toIGTL(const HeaderData& header, const ContentData& source, igtl::NDArrayMessage::Pointer* dest, igtl::MessageBase::MetaDataMap metaInfo = igtl::MessageBase::MetaDataMap());

};

#endif //NDARRAYCONVERTER_H
20 changes: 18 additions & 2 deletions Converter/igtlioPointConverter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,33 @@ int igtlioPointConverter::toIGTL(const HeaderData& header, const ContentData& so
igtl::MessageBase::Pointer basemsg = dynamic_pointer_cast<igtl::MessageBase>(msg);
HeadertoIGTL(header, &basemsg, metaInfo);

if (msg->GetNumberOfPointElement() > source.PointElements.size())
{
// there is no API to delete a single element, so if we need to delete then we delete all
msg->ClearPointElement();
}
int pointIndex = 0;
for (PointList::const_iterator pointIt = source.PointElements.begin(); pointIt != source.PointElements.end(); ++pointIt)
{
igtl::PointElement::Pointer pointElement = igtl::PointElement::New();
igtl::PointElement::Pointer pointElement;
if (pointIndex < msg->GetNumberOfPointElement())
{
msg->GetPointElement(pointIndex, pointElement);
}
else
{
pointElement = igtl::PointElement::New();
msg->AddPointElement(pointElement);
}
pointElement->SetName(pointIt->Name.c_str());
pointElement->SetGroupName(pointIt->GroupName.c_str());
pointElement->SetRGBA(pointIt->RGBA[0], pointIt->RGBA[1], pointIt->RGBA[2], pointIt->RGBA[3]);
pointElement->SetPosition(pointIt->Position[0], pointIt->Position[1], pointIt->Position[2]);
pointElement->SetRadius(pointIt->Radius);
pointElement->SetOwner(pointIt->Owner.c_str());
msg->AddPointElement(pointElement);
pointIndex++;
}

msg->Pack();
return 1;
}
16 changes: 11 additions & 5 deletions Converter/igtlioTransformConverter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ int igtlioTransformConverter::fromIGTL(igtl::MessageBase::Pointer source,
// Deserialize the transform data
// If CheckCRC==0, CRC check is skipped.
int c = transMsg->Unpack(checkCRC);

if (!(c & igtl::MessageHeader::UNPACK_BODY)) // if CRC check fails
{
// TODO: error handling
Expand All @@ -43,12 +42,15 @@ int igtlioTransformConverter::fromIGTL(igtl::MessageBase::Pointer source,

// get header
if (!IGTLtoHeader(dynamic_pointer_cast<igtl::MessageBase>(transMsg), header, outMetaInfo))
{
return 0;
}

// get additional transform header info
if (!IGTLHeaderToTransformInfo(dynamic_pointer_cast<igtl::MessageBase>(transMsg), dest))
{
return 0;

}

igtl::Matrix4x4 matrix;
transMsg->GetMatrix(matrix);
Expand Down Expand Up @@ -122,20 +124,24 @@ int igtlioTransformConverter::IGTLHeaderToTransformInfo(igtl::MessageBase::Point
int igtlioTransformConverter::toIGTL(const HeaderData& header, const ContentData& source, igtl::TransformMessage::Pointer* dest, igtl::MessageBase::MetaDataMap metaInfo)
{
if (dest->IsNull())
{
*dest = igtl::TransformMessage::New();
}
(*dest)->InitPack();
igtl::TransformMessage::Pointer msg = *dest;

if (!metaInfo.empty())
{
{
msg->SetHeaderVersion(IGTL_HEADER_VERSION_2);
}
}
igtl::MessageBase::Pointer basemsg = dynamic_pointer_cast<igtl::MessageBase>(msg);
HeadertoIGTL(header, &basemsg, metaInfo);
TransformMetaDataToIGTL(source, &basemsg);

if (source.transform.Get()==NULL)
if (source.transform.Get() == NULL)
{
std::cerr << "Got NULL input transform" << std::endl;
}

vtkSmartPointer<vtkMatrix4x4> matrix = source.transform;
igtl::Matrix4x4 igtlmatrix;
Expand Down
13 changes: 10 additions & 3 deletions Devices/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ project(igtlioDevices)
set(${PROJECT_NAME}_EXPORT_DIRECTIVE "OPENIGTLINKIO_DEVICES_EXPORT")

set(VTK_MODULES
vtkIOImage
vtkImagingMath
${IGTLIO_MODULE_PREFIX}CommonSystem
${IGTLIO_MODULE_PREFIX}IOImage
${IGTLIO_MODULE_PREFIX}ImagingMath
)

find_package(VTK REQUIRED NO_MODULE
COMPONENTS
${VTK_MODULES}
)
include(${VTK_USE_FILE})

if(VTK_VERSION VERSION_LESS 8.9.0)
include(${VTK_USE_FILE})
endif()

set(${PROJECT_NAME}_INCLUDE_DIRECTORIES PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
Expand All @@ -28,8 +32,10 @@ set(${PROJECT_NAME}_SRCS
igtlioPolyDataDevice.cxx
igtlioStatusDevice.cxx
igtlioStringDevice.cxx
igtlioNDArrayDevice.cxx
igtlioTransformDevice.cxx
igtlioTrackingDataDevice.cxx

)

set(${PROJECT_NAME}_HDRS
Expand All @@ -41,6 +47,7 @@ set(${PROJECT_NAME}_HDRS
igtlioPolyDataDevice.h
igtlioStatusDevice.h
igtlioStringDevice.h
igtlioNDArrayDevice.h
igtlioTransformDevice.h
igtlioTrackingDataDevice.h
)
Expand Down
8 changes: 8 additions & 0 deletions Devices/igtlioDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@
#ifndef IGTLIODEVICE_H
#define IGTLIODEVICE_H

// VTK includes
#include <vtkCommand.h>
#include <vtkIntArray.h>

// STL includes
#include <set>

// local includes
#include "igtlioDevicesExport.h"
#include "igtlioBaseConverter.h"

typedef vtkSmartPointer<class igtlioDevice> igtlioDevicePointer;

#ifndef VTK_OVERRIDE
#define VTK_OVERRIDE override
#endif

/// A vtkIGTLIODevice represents one device connected over OpenIGTLink.
///
/// The Device has a specific type, e.g. Image, Transform..., and
Expand Down
2 changes: 1 addition & 1 deletion Devices/igtlioImageDevice.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ igtl::MessageBase::Pointer igtlioImageDevice::GetIGTLMessage()
{
if (!Content.image)
{
vtkWarningMacro("Image is NULL, message not generated.")
vtkWarningMacro("Image is NULL, message not generated.");
return 0;
}

Expand Down
Loading