-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
151 lines (122 loc) · 5.35 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "SafeQueue.h"
#include <cast/cast.h>
#include <atomic>
#include <chrono>
#include <cstring>
#include <igtlImageMessage.h>
#include <igtlServerSocket.h>
#include <iostream>
#include <mutex>
using namespace std::chrono_literals;
void IGTServer(SafeQueue<igtl::ImageMessage::Pointer> &queue, int port) {
igtl::ServerSocket::Pointer serverSocket;
while (true) {
serverSocket = igtl::ServerSocket::New();
int r = serverSocket->CreateServer(port);
if (r < 0) {
std::cerr << "Cannot create a server socket. Trying next port..." << std::endl;
port++;
} else {
std::cout << "Created IGTLink server on port: " << port << std::endl;
break;
}
}
std::cout << "Server created correctly" << std::endl;
igtl::Socket::Pointer socket;
while (true) {
socket = serverSocket->WaitForConnection(1000);
if (socket.IsNotNull()) // if client connected
{
std::cout << "Got a connection" << std::endl;
while (true) {
auto imgMsg = queue.dequeue();
if (imgMsg == nullptr) {
std::cerr << "IGTLink Closing connection" << std::endl;
break;
}
imgMsg->Pack();
socket->Send(imgMsg->GetPackPointer(), imgMsg->GetPackSize());
}
socket->CloseSocket();
}
}
}
int main(int argc, char *argv[]) {
if (argc != 3) {
std::cout << "Usage: ClariusOpenIGTLinkBridge <ip> <port>" << std::endl;
return 1;
}
const char *ip = argv[1];
int port = atoi(argv[2]);
std::cout << "IP: " << ip << " port: " << port << std::endl;
static std::atomic<bool> run{false};
static SafeQueue<igtl::ImageMessage::Pointer> queue;
static long lastTimestamp = 0;
bool ok = cusCastInit(
0,
nullptr,
"/tmp/",
// New processed image function callback
[](const void *newImage, const CusProcessedImageInfo *nfo, int npos, const CusPosInfo *pos) {
if (!nfo)
return;
long timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
std::cout << "Time difference: " << (timestamp - lastTimestamp) << "ms" << std::endl;
lastTimestamp = timestamp;
int size[] = {nfo->width, nfo->height, 1}; // image dimension
float spacing[] = {1.0, 1.0, 5.0}; // spacing (mm/pixel)
int svsize[] = {nfo->width, nfo->height, 1}; // sub-volume size
int svoffset[] = {0, 0, 0}; // sub-volume offset
int scalarType = igtl::ImageMessage::TYPE_UINT8; // scalar type
//------------------------------------------------------------
// Create a new IMAGE type message
igtl::ImageMessage::Pointer imgMsg = igtl::ImageMessage::New();
imgMsg->SetDimensions(size);
imgMsg->SetSpacing(spacing);
imgMsg->SetScalarType(scalarType);
imgMsg->SetDeviceName("Clarius");
imgMsg->SetSubVolume(svsize, svoffset);
imgMsg->SetNumComponents(nfo->bitsPerPixel / 8);
imgMsg->AllocateScalars();
std::memcpy(imgMsg->GetScalarPointer(), newImage, nfo->imageSize);
igtl::Matrix4x4 matrix;
igtl::IdentityMatrix(matrix);
imgMsg->SetMatrix(matrix);
queue.enqueue(imgMsg);
},
// New raw image function callback
[](const void * /*newImage*/, const CusRawImageInfo *nfo, int /*npos*/, const CusPosInfo * /*pos*/) {
},
// New spectral image function callback
[](const void * /*newImage*/, const CusSpectralImageInfo * /*nfo*/) {},
// Freeze / unfreeze function callback
[](int val) {
if (val)
std::cout << "Frozen" << std::endl;
else
std::cout << "Imaging" << std::endl;
},
// Button function callback
[](CusButton btn, int clicks) {
std::cout << "Button event (button: " << static_cast<int>(btn) << ", clicks: " << clicks << ")" << std::endl;
},
// Progress function callback
[](int /*progress*/) {},
// Error function callback
[](const char *err) { std::cerr << "Clarius Cast reported error: " << err << std::endl; },
640,
480) >= 0;
if (!ok)
std::cout << "ERROR in cusCastInit";
ok = cusCastConnect(ip, port, "research", [](int port, int swRevMatch) {
if (swRevMatch == CUS_SUCCESS)
std::cout << "Clarius Cast connection OK" << std::endl;
else
std::cerr << "Clarius Cast version mismatch, ClariusOpenIGTLinkBridge was build with " << CLARIUS_CAST_VERSION
<< ". Please follow the instructions at https://github.com/ImFusionGmbH/ClariusOpenIGTLinkBridge#updating-cast-api-version to update the Cast version.";
}) >= 0;
if (!ok)
std::cout << "ERROR in cusCastConnect";
IGTServer(queue, 8080);
return 0;
}