Skip to content

Commit 1c9ba54

Browse files
Qt: OpenIGTLink: send received images
Also handling client reconnection, since this is where we have to detect disconnects.
1 parent a6423f1 commit 1c9ba54

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

examples/solum_qt/solum/openigtlink.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
SolumIGTL::SolumIGTL()
44
{
5+
msg_ = igtl::ImageMessage::New();
56
QObject::connect(&clientConnectTimer_, &QTimer::timeout, [this]()
67
{
78
client_ = server_->WaitForConnection(1); // 0 would wait forever.
@@ -33,6 +34,37 @@ void SolumIGTL::setNodeName(const QString &name)
3334
nodeName_ = name.toStdString();
3435
}
3536

37+
void SolumIGTL::sendImage(const void* img, int w, int h, int bpp, size_t sz)
38+
{
39+
if (!isClientConnected())
40+
return;
41+
42+
assert(bpp == 32);
43+
44+
int wPrev, hPrev, zPrev;
45+
msg_->GetDimensions(wPrev, hPrev, zPrev);
46+
if ((wPrev != w) || (hPrev != h))
47+
{
48+
msg_->SetDimensions(w, h, 1);
49+
msg_->SetSpacing(1.0f, 1.0f, 5.0f); // required for reslicing to not crash
50+
msg_->SetScalarType(igtl::ImageMessage::TYPE_UINT32);
51+
msg_->SetSubVolume(w, h, 1, 0, 0, 0); // determines the buffer size!
52+
msg_->SetDeviceName(nodeName_);
53+
msg_->AllocateScalars();
54+
}
55+
56+
// Also necessary to force a repack below.
57+
msg_->SetMessageID(msg_->GetMessageID() + 1);
58+
59+
// Even C++23 does not have output ranges anyway...
60+
// (https://thephd.dev/output-ranges)
61+
memcpy(msg_->GetScalarPointer(), img, sz);
62+
63+
msg_->Pack();
64+
if (client_->Send(msg_->GetPackPointer(), msg_->GetPackSize()) == 0)
65+
reconnectClient();
66+
}
67+
3668
void SolumIGTL::disconnectClient()
3769
{
3870
clientConnectTimer_.stop();

examples/solum_qt/solum/openigtlink.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "igtlImageMessage.h"
34
#include "igtlServerSocket.h"
45

56
/// OpenIGTLink module
@@ -14,6 +15,7 @@ class SolumIGTL : public QObject
1415
void close();
1516

1617
void setNodeName(const QString& name);
18+
void sendImage(const void* img, int w, int h, int bpp, size_t sz);
1719

1820
bool isServing() const;
1921
bool isClientConnected() const;
@@ -29,4 +31,7 @@ class SolumIGTL : public QObject
2931
igtl::Socket::Pointer client_;
3032
QTimer clientConnectTimer_;
3133
std::string nodeName_;
34+
35+
// Sharing an allocation for all images of the same size.
36+
igtl::ImageMessage::Pointer msg_;
3237
};

examples/solum_qt/solum/solumqt.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ void Solum::setProgress(int progress)
695695
/// @param[in] imu the imu data if valid
696696
void Solum::newProcessedImage(const void* img, int w, int h, int bpp, int sz, const QQuaternion& imu)
697697
{
698+
igtl_.sendImage(img, w, h, bpp, sz);
698699
image_->loadImage(img, w, h, bpp, sz);
699700
if (!imu.isNull())
700701
render_->update(imu);

0 commit comments

Comments
 (0)