Skip to content

Commit 4fe967f

Browse files
author
Adam Rankin
committed
re #1010 Adding embedded image transform to tracked frame message
Optimizing message sending and receiving by not creating a factory for each receive Adjusting timeout to be based in seconds
1 parent 631bcde commit 4fe967f

14 files changed

Lines changed: 471 additions & 384 deletions

PlusLib/src/PlusCommon/PlusTrackedFrame.cxx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ PlusStatus PlusTrackedFrame::PrintToXML( vtkXMLDataElement* trackedFrame )
9898
trackedFrame->SetName( "TrackedFrame" );
9999
trackedFrame->SetDoubleAttribute( "Timestamp", this->Timestamp );
100100
trackedFrame->SetAttribute( "ImageDataValid", ( this->GetImageData()->IsImageValid() ? "true" : "false" ) );
101+
101102
if ( this->GetImageData()->IsImageValid() )
102103
{
103104
trackedFrame->SetIntAttribute( "NumberOfBits", this->GetNumberOfBitsPerScalar() );
@@ -112,6 +113,7 @@ PlusStatus PlusTrackedFrame::PrintToXML( vtkXMLDataElement* trackedFrame )
112113
int frameSizeSigned[3] = { static_cast<int>( FrameSize[0] ), static_cast<int>( FrameSize[1] ), static_cast<int>( FrameSize[2] ) };
113114
trackedFrame->SetVectorAttribute( "FrameSize", 3, frameSizeSigned );
114115
}
116+
115117
for ( FieldMapType::const_iterator it = this->CustomFrameFields.begin(); it != this->CustomFrameFields.end(); it++ )
116118
{
117119
vtkSmartPointer<vtkXMLDataElement> customField = vtkSmartPointer<vtkXMLDataElement>::New();
@@ -153,9 +155,11 @@ PlusStatus PlusTrackedFrame::PrintToXML( vtkXMLDataElement* trackedFrame )
153155
pointElement->SetVectorAttribute( "Position", 3, point );
154156
segmentedPoints->AddNestedElement( pointElement );
155157
}
158+
156159
segmentation->AddNestedElement( segmentedPoints );
157160
trackedFrame->AddNestedElement( segmentation );
158161
}
162+
159163
return PLUS_SUCCESS;
160164
}
161165

PlusLib/src/PlusDataCollection/OpenIGTLink/vtkPlusOpenIGTLinkDevice.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ vtkPlusOpenIGTLinkDevice::vtkPlusOpenIGTLinkDevice()
2020
, ServerAddress( NULL )
2121
, ServerPort( -1 )
2222
, IgtlMessageCrcCheckEnabled( 0 )
23-
, ReceiveTimeoutSec(0.5)
23+
, ReceiveTimeoutSec( 0.5 )
2424
, NumberOfRetryAttempts( 3 ) // try a few times, but adding of data items is blocked while trying to reconnect, so don't make it too long
2525
, DelayBetweenRetryAttemptsSec( 0.100 ) // there is already a delay with a CLIENT_SOCKET_TIMEOUT_MSEC timeout, so we just add a little extra idle delay
2626
, ClientSocket( igtl::ClientSocket::New() )
@@ -142,7 +142,7 @@ PlusStatus vtkPlusOpenIGTLinkDevice::ClientSocketReconnect()
142142
LOG_DEBUG( "Client successfully connected to server (" << this->ServerAddress << ":" << this->ServerPort << ")." );
143143
}
144144

145-
this->ClientSocket->SetTimeout(this->ReceiveTimeoutSec*1000); // *1000 because SetTimeout expects msec
145+
this->ClientSocket->SetTimeout( this->ReceiveTimeoutSec * 1000 ); // *1000 because SetTimeout expects msec
146146

147147
return SendRequestedMessageTypes();
148148
}

PlusLib/src/PlusDataCollection/OpenIGTLink/vtkPlusOpenIGTLinkVideoSource.cxx

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ See License.txt for details.
1616
#include "vtkPlusDataSource.h"
1717
#include "vtkPlusIgtlMessageCommon.h"
1818

19-
vtkStandardNewMacro(vtkPlusOpenIGTLinkVideoSource);
19+
vtkStandardNewMacro( vtkPlusOpenIGTLinkVideoSource );
2020

2121
//----------------------------------------------------------------------------
2222
vtkPlusOpenIGTLinkVideoSource::vtkPlusOpenIGTLinkVideoSource()
23-
: IgtlMessageFactory(vtkSmartPointer<vtkPlusIgtlMessageFactory>::New())
23+
: IgtlMessageFactory( vtkSmartPointer<vtkPlusIgtlMessageFactory>::New() )
2424
{
2525
this->RequireImageOrientationInConfiguration = true;
2626
}
@@ -31,118 +31,119 @@ vtkPlusOpenIGTLinkVideoSource::~vtkPlusOpenIGTLinkVideoSource()
3131
}
3232

3333
//----------------------------------------------------------------------------
34-
void vtkPlusOpenIGTLinkVideoSource::PrintSelf(ostream& os, vtkIndent indent)
34+
void vtkPlusOpenIGTLinkVideoSource::PrintSelf( ostream& os, vtkIndent indent )
3535
{
36-
this->Superclass::PrintSelf(os,indent);
36+
this->Superclass::PrintSelf( os, indent );
3737
}
3838

3939
//----------------------------------------------------------------------------
4040
PlusStatus vtkPlusOpenIGTLinkVideoSource::InternalUpdate()
4141
{
4242
LOG_TRACE( "vtkPlusOpenIGTLinkVideoSource::InternalUpdate" );
43-
if (!this->Recording)
43+
if ( !this->Recording )
4444
{
4545
// drop the frame, we are not recording data now
4646
return PLUS_SUCCESS;
4747
}
4848

4949
igtl::MessageHeader::Pointer headerMsg;
50-
ReceiveMessageHeaderWithErrorHandling(headerMsg);
51-
52-
if (headerMsg.IsNull())
50+
ReceiveMessageHeaderWithErrorHandling( headerMsg );
51+
52+
if ( headerMsg.IsNull() )
5353
{
5454
OnReceiveTimeout();
5555
return PLUS_FAIL;
5656
}
5757

5858
// We've received valid header data
59-
headerMsg->Unpack(this->IgtlMessageCrcCheckEnabled);
59+
headerMsg->Unpack( this->IgtlMessageCrcCheckEnabled );
6060

6161
// Set unfiltered and filtered timestamp by converting UTC to system timestamp
6262
double unfilteredTimestamp = vtkPlusAccurateTimer::GetSystemTime();
6363

6464
PlusTrackedFrame trackedFrame;
65-
igtl::MessageBase::Pointer bodyMsg = IgtlMessageFactory->CreateReceiveMessage(headerMsg);
65+
vtkSmartPointer<vtkMatrix4x4> embeddedImageTransform;
66+
igtl::MessageBase::Pointer bodyMsg = IgtlMessageFactory->CreateReceiveMessage( headerMsg );
6667

67-
if ( typeid(*bodyMsg) == typeid(igtl::ImageMessage) )
68+
if ( typeid( *bodyMsg ) == typeid( igtl::ImageMessage ) )
6869
{
69-
if (vtkPlusIgtlMessageCommon::UnpackImageMessage( bodyMsg, this->ClientSocket, trackedFrame, this->ImageMessageEmbeddedTransformName, this->IgtlMessageCrcCheckEnabled)!=PLUS_SUCCESS)
70+
if ( vtkPlusIgtlMessageCommon::UnpackImageMessage( bodyMsg, this->ClientSocket, trackedFrame, this->ImageMessageEmbeddedTransformName, this->IgtlMessageCrcCheckEnabled ) != PLUS_SUCCESS )
7071
{
71-
LOG_ERROR("Couldn't get image from OpenIGTLink server!");
72+
LOG_ERROR( "Couldn't get image from OpenIGTLink server!" );
7273
return PLUS_FAIL;
7374
}
7475
}
75-
else if ( typeid(*bodyMsg) == typeid(igtl::PlusTrackedFrameMessage) )
76+
else if ( typeid( *bodyMsg ) == typeid( igtl::PlusTrackedFrameMessage ) )
7677
{
77-
if ( vtkPlusIgtlMessageCommon::UnpackTrackedFrameMessage( bodyMsg, this->ClientSocket, trackedFrame, this->IgtlMessageCrcCheckEnabled ) != PLUS_SUCCESS )
78+
if ( vtkPlusIgtlMessageCommon::UnpackTrackedFrameMessage( bodyMsg, this->ClientSocket, trackedFrame, embeddedImageTransform, this->IgtlMessageCrcCheckEnabled ) != PLUS_SUCCESS )
7879
{
79-
LOG_ERROR("Couldn't get tracked frame from OpenIGTLink server!");
80-
return PLUS_FAIL;
80+
LOG_ERROR( "Couldn't get tracked frame from OpenIGTLink server!" );
81+
return PLUS_FAIL;
8182
}
8283
double unfilteredTimestampUtc = trackedFrame.GetTimestamp();
83-
if (this->UseReceivedTimestamps)
84+
if ( this->UseReceivedTimestamps )
8485
{
8586
// Use the timestamp in the OpenIGTLink message
8687
// The received timestamp is in UTC and timestamps in the buffer are in system time, so conversion is needed
87-
unfilteredTimestamp = vtkPlusAccurateTimer::GetSystemTimeFromUniversalTime(unfilteredTimestampUtc);
88+
unfilteredTimestamp = vtkPlusAccurateTimer::GetSystemTimeFromUniversalTime( unfilteredTimestampUtc );
8889
}
8990
}
9091
else
9192
{
92-
// if the data type is unknown, skip reading.
93-
this->ClientSocket->Skip(headerMsg->GetBodySizeToRead(), 0);
94-
return PLUS_SUCCESS;
93+
// if the data type is unknown, skip reading.
94+
this->ClientSocket->Skip( headerMsg->GetBodySizeToRead(), 0 );
95+
return PLUS_SUCCESS;
9596
}
9697

97-
// No need to filter already filtered timestamped items received over OpenIGTLink
98+
// No need to filter already filtered timestamped items received over OpenIGTLink
9899
// If the original timestamps are not used it's still safer not to use filtering, as filtering assumes uniform frame rate, which is not guaranteed
99100
double filteredTimestamp = unfilteredTimestamp;
100101

101-
// The timestamps are already defined, so we don't need to filter them,
102+
// The timestamps are already defined, so we don't need to filter them,
102103
// for simplicity, we increase frame number always by 1.
103104
this->FrameNumber++;
104105

105-
vtkPlusDataSource* aSource=NULL;
106-
if( this->GetFirstActiveOutputVideoSource(aSource) != PLUS_SUCCESS )
106+
vtkPlusDataSource* aSource = NULL;
107+
if( this->GetFirstActiveOutputVideoSource( aSource ) != PLUS_SUCCESS )
107108
{
108-
LOG_ERROR("Unable to retrieve the video source in the OpenIGTLinkVideo device.");
109+
LOG_ERROR( "Unable to retrieve the video source in the OpenIGTLinkVideo device." );
109110
return PLUS_FAIL;
110111
}
111112

112-
// If the buffer is empty, set the pixel type and frame size to the first received properties
113+
// If the buffer is empty, set the pixel type and frame size to the first received properties
113114
if ( aSource->GetNumberOfItems() == 0 )
114115
{
115-
PlusVideoFrame* videoFrame=trackedFrame.GetImageData();
116-
if (videoFrame==NULL)
116+
PlusVideoFrame* videoFrame = trackedFrame.GetImageData();
117+
if ( videoFrame == NULL )
117118
{
118-
LOG_ERROR("Invalid video frame received, cannot use it to initialize the video buffer");
119+
LOG_ERROR( "Invalid video frame received, cannot use it to initialize the video buffer" );
119120
return PLUS_FAIL;
120121
}
121122
aSource->SetPixelType( videoFrame->GetVTKScalarPixelType() );
122123
aSource->SetNumberOfScalarComponents( videoFrame->GetNumberOfScalarComponents() );
123124
aSource->SetImageType( videoFrame->GetImageType() );
124125
aSource->SetInputFrameSize( trackedFrame.GetFrameSize() );
125126
}
126-
PlusTrackedFrame::FieldMapType customFields=trackedFrame.GetCustomFields();
127-
PlusStatus status = aSource->AddItem( trackedFrame.GetImageData(), this->FrameNumber, unfilteredTimestamp, filteredTimestamp, &customFields);
127+
PlusTrackedFrame::FieldMapType customFields = trackedFrame.GetCustomFields();
128+
PlusStatus status = aSource->AddItem( trackedFrame.GetImageData(), this->FrameNumber, unfilteredTimestamp, filteredTimestamp, &customFields );
128129
this->Modified();
129130

130131
return status;
131132
}
132133

133134
//-----------------------------------------------------------------------------
134-
PlusStatus vtkPlusOpenIGTLinkVideoSource::ReadConfiguration(vtkXMLDataElement* rootConfigElement)
135+
PlusStatus vtkPlusOpenIGTLinkVideoSource::ReadConfiguration( vtkXMLDataElement* rootConfigElement )
135136
{
136-
XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_READING(deviceConfig, rootConfigElement);
137-
XML_READ_STRING_ATTRIBUTE_OPTIONAL(ImageMessageEmbeddedTransformName, deviceConfig);
137+
XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_READING( deviceConfig, rootConfigElement );
138+
XML_READ_STRING_ATTRIBUTE_OPTIONAL( ImageMessageEmbeddedTransformName, deviceConfig );
138139
return PLUS_SUCCESS;
139140
}
140141

141142
//----------------------------------------------------------------------------
142-
PlusStatus vtkPlusOpenIGTLinkVideoSource::WriteConfiguration(vtkXMLDataElement* rootConfigElement)
143+
PlusStatus vtkPlusOpenIGTLinkVideoSource::WriteConfiguration( vtkXMLDataElement* rootConfigElement )
143144
{
144-
XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_WRITING(deviceConfig, rootConfigElement);
145-
deviceConfig->SetAttribute("ImageMessageEmbeddedTransformName", this->ImageMessageEmbeddedTransformName.GetTransformName().c_str());
145+
XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_WRITING( deviceConfig, rootConfigElement );
146+
deviceConfig->SetAttribute( "ImageMessageEmbeddedTransformName", this->ImageMessageEmbeddedTransformName.GetTransformName().c_str() );
146147
return PLUS_SUCCESS;
147148
}
148149

@@ -151,22 +152,22 @@ PlusStatus vtkPlusOpenIGTLinkVideoSource::NotifyConfigured()
151152
{
152153
if( this->OutputChannels.size() > 1 )
153154
{
154-
LOG_WARNING("vtkPlusOpenIGTLinkVideoSource is expecting one output channel and there are " << this->OutputChannels.size() << " channels. First output channel will be used.");
155+
LOG_WARNING( "vtkPlusOpenIGTLinkVideoSource is expecting one output channel and there are " << this->OutputChannels.size() << " channels. First output channel will be used." );
155156
return PLUS_FAIL;
156157
}
157158

158159
if( this->OutputChannels.empty() )
159160
{
160-
LOG_ERROR("No output channels defined for vtkPlusOpenIGTLinkVideoSource. Cannot proceed." );
161-
this->SetCorrectlyConfigured(false);
161+
LOG_ERROR( "No output channels defined for vtkPlusOpenIGTLinkVideoSource. Cannot proceed." );
162+
this->SetCorrectlyConfigured( false );
162163
return PLUS_FAIL;
163164
}
164165

165166
return PLUS_SUCCESS;
166167
}
167168

168169
//----------------------------------------------------------------------------
169-
PlusStatus vtkPlusOpenIGTLinkVideoSource::SetImageMessageEmbeddedTransformName(const char* nameString)
170+
PlusStatus vtkPlusOpenIGTLinkVideoSource::SetImageMessageEmbeddedTransformName( const char* nameString )
170171
{
171-
return this->ImageMessageEmbeddedTransformName.SetTransformName(nameString);
172+
return this->ImageMessageEmbeddedTransformName.SetTransformName( nameString );
172173
}

PlusLib/src/PlusOpenIGTLink/igtlPlusTrackedFrameMessage.cxx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ See License.txt for details.
77
#include "PlusConfigure.h"
88
#include "igtlPlusTrackedFrameMessage.h"
99
#include "vtkImageData.h"
10+
#include "vtkMatrix4x4.h"
1011
#include "vtkPlusIgtlMessageFactory.h"
1112

1213
namespace igtl
@@ -98,6 +99,34 @@ namespace igtl
9899
return this->m_TrackedFrame;
99100
}
100101

102+
//----------------------------------------------------------------------------
103+
PlusStatus PlusTrackedFrameMessage::SetEmbeddedImageTransform(vtkSmartPointer<vtkMatrix4x4> matrix)
104+
{
105+
for (int i = 0; i < 4; ++i)
106+
{
107+
for (int j = 0; j < 4; ++j)
108+
{
109+
m_MessageHeader.m_EmbeddedImageTransform[i][j] = matrix->GetElement(i, j);
110+
}
111+
}
112+
113+
return PLUS_SUCCESS;
114+
}
115+
116+
//----------------------------------------------------------------------------
117+
vtkSmartPointer<vtkMatrix4x4> PlusTrackedFrameMessage::GetEmbeddedImageTransform()
118+
{
119+
vtkSmartPointer<vtkMatrix4x4> mat(vtkSmartPointer<vtkMatrix4x4>::New());
120+
for (int i = 0; i < 4; ++i)
121+
{
122+
for (int j = 0; j < 4; ++j)
123+
{
124+
mat->SetElement(i, j, m_MessageHeader.m_EmbeddedImageTransform[i][j]);
125+
}
126+
}
127+
return mat;
128+
}
129+
101130
//----------------------------------------------------------------------------
102131
int PlusTrackedFrameMessage::CalculateContentBufferSize()
103132
{
@@ -121,6 +150,7 @@ namespace igtl
121150
header->m_FrameSize[2] = this->m_MessageHeader.m_FrameSize[2];
122151
header->m_ImageDataSizeInBytes = this->m_MessageHeader.m_ImageDataSizeInBytes;
123152
header->m_XmlDataSizeInBytes = this->m_MessageHeader.m_XmlDataSizeInBytes;
153+
memcpy( header->m_EmbeddedImageTransform, this->m_MessageHeader.m_EmbeddedImageTransform, sizeof( igtl::Matrix4x4 ) );
124154

125155
// Copy xml data
126156
char* xmlData = ( char* )( this->m_Content + header->GetMessageHeaderSize() );
@@ -159,6 +189,7 @@ namespace igtl
159189
this->m_MessageHeader.m_FrameSize[2] = header->m_FrameSize[2];
160190
this->m_MessageHeader.m_ImageDataSizeInBytes = header->m_ImageDataSizeInBytes;
161191
this->m_MessageHeader.m_XmlDataSizeInBytes = header->m_XmlDataSizeInBytes;
192+
memcpy( this->m_MessageHeader.m_EmbeddedImageTransform, header->m_EmbeddedImageTransform, sizeof( igtl::Matrix4x4 ) );
162193

163194
// Copy xml data
164195
char* xmlData = ( char* )( this->m_Content + header->GetMessageHeaderSize() );

0 commit comments

Comments
 (0)