-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathigtlsend.cpp
372 lines (295 loc) · 12.3 KB
/
igtlsend.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*=========================================================================
Program: Matlab Open IGT Link Interface -- igtlsend
Module: $RCSfile: $
Language: C++
Date: $Date: $
Version: $Revision: $
Copyright (c) Insight Software Consortium. All rights reserved.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
//
// r = igtlsend(SD, DATA);
//
// SD : (integer) Socket descriptor (-1 if failed to connect)
// DATA: (structure) Data contents
//
// Data fields for IMAGE data
// DATA.Type : (string) must be 'IMAGE'
// DATA.Name : (string) Data name (DEVICE_NAME in OpenIGTLink)
// DATA.Image: (uint16[][]) Image data
// DATA.Trans: (real[4][4]) Affine transform matrix (4x4)
//
// Data fields for TRANSFORM data
// DATA.Type : (string) must be 'TRANSFORM'
// DATA.NAME : (string) Data name (DEVICE_NAME in OpenIGTLink)
// DATA.Trans: (real[4][4]) Affine transform matrix (4x4)
//
#include "mex.h"
#include <math.h>
#include <string.h>
#include "igtlOSUtil.h"
#include "igtlImageMessage.h"
#include "igtlTransformMessage.h"
#include "igtlMexClientSocket.h"
using namespace std;
//#define pi (3.141592653589793)
#define ARG_ID_SD 0
#define ARG_ID_DATA 1 // data
#define ARG_ID_NUM 2 // total number of arguments
#define MAX_STRING_LEN 256
//extern void _main();
// -----------------------------------------------------------------
// Function declarations.
double getMatlabScalar (const mxArray* ptr);
double& createMatlabScalar (mxArray*& ptr);
int checkArguments(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]);
int procTransformData(int sd, const char* name, const mxArray *ptr);
int procImageData(int sd, const char* name, const mxArray *ptr);
template<typename DATATYPE> void procTypedImageData(int sd, const char* name, const mxArray* imField, const mxArray* trField, DATATYPE dtype);
//int checkData(const char* type, const mxArray* prhs);
// -----------------------------------------------------------------
// Function definitions.
void mexFunction (int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
// ---------------------------------------------------------------
// Get reference to return value
double& retVal = createMatlabScalar(plhs[0]);
// ---------------------------------------------------------------
// Check arguments
if (checkArguments(nlhs, plhs, nrhs, prhs) == 0)
{
retVal = 0;
return;
}
// ---------------------------------------------------------------
// Set header variables
int sd;
char type[MAX_STRING_LEN];
char name[MAX_STRING_LEN];
sd = (int)*mxGetPr(prhs[ARG_ID_SD]);
// Get DATA.type
mxArray* typeField = mxGetField(prhs[ARG_ID_DATA], 0, "Type");
if (typeField == NULL)
{
mexErrMsgTxt("No DATA.Type field.");
retVal = 0;
return;
}
mxGetString(typeField, type, MAX_STRING_LEN);
// Get DATA.name
mxArray* nameField = mxGetField(prhs[ARG_ID_DATA], 0, "Name");
if (nameField == NULL)
{
mexErrMsgTxt("No DATA.Name field.");
retVal = 0;
return;
}
mxGetString(nameField, name, MAX_STRING_LEN);
// ---------------------------------------------------------------
// Process data field and send through OpenIGTLink connection
int r;
if (strcmp(type, "TRANSFORM") == 0)
{
r = procTransformData(sd, name, prhs[ARG_ID_DATA]);
}
else if (strcmp(type, "IMAGE") == 0)
{
r = procImageData(sd, name, prhs[ARG_ID_DATA]);
}
// ---------------------------------------------------------------
// Return result to Matlab
retVal = r;
}
int checkArguments(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
// ---------------------------------------------------------------
// Check numbers of arguments and outputs
if (nrhs != ARG_ID_NUM)
{
mexErrMsgTxt("Incorrect number of input arguments");
return 0;
}
if (nlhs != 1)
{
mexErrMsgTxt("Incorrect number of output arguments");
return 0;
}
// ---------------------------------------------------------------
// Check types of arguments
// SD -- socket descriptor
if (!mxIsNumeric(prhs[ARG_ID_SD]))
{
mexErrMsgTxt("SD argument must be integer.");
return 0;
}
// DATA
if (!mxIsStruct(prhs[ARG_ID_DATA]))
{
mexErrMsgTxt("DATA argument must be structure.");
return 0;
}
return 1;
}
int procTransformData(int sd, const char* name, const mxArray *ptr)
{
mxArray* transField = mxGetField(ptr, 0, "Trans");
double* trans = mxGetPr(transField);
const mwSize* s = mxGetDimensions(transField);
igtl::Matrix4x4 mat;
mat[0][0] = trans[0]; mat[0][1] = trans[4]; mat[0][2] = trans[8]; mat[0][3] = trans[12];
mat[1][0] = trans[1]; mat[1][1] = trans[5]; mat[1][2] = trans[9]; mat[1][3] = trans[13];
mat[2][0] = trans[2]; mat[2][1] = trans[6]; mat[2][2] = trans[10]; mat[2][3] = trans[14];
mat[3][0] = trans[3]; mat[3][1] = trans[7]; mat[3][2] = trans[11]; mat[3][3] = trans[15];
float norm_i[] = {mat[0][0], mat[1][0], mat[2][0]};
float norm_j[] = {mat[0][1], mat[1][1], mat[2][1]};
float norm_k[] = {mat[0][2], mat[1][2], mat[2][2]};
float pos[] = {mat[0][3], mat[1][3], mat[2][3]};
float a[3];
// calculate absolutes
a[0] = sqrt(norm_i[0]*norm_i[0] + norm_i[1]*norm_i[1] + norm_i[2]*norm_i[2]);
a[1] = sqrt(norm_j[0]*norm_j[0] + norm_j[1]*norm_j[1] + norm_j[2]*norm_j[2]);
a[2] = sqrt(norm_k[0]*norm_k[0] + norm_k[1]*norm_k[1] + norm_k[2]*norm_k[2]);
// normalize
for (int i = 0; i < 3; i ++)
{
norm_i[i] /= a[i];
norm_j[i] /= a[i];
norm_k[i] /= a[i];
}
// print variables
mexPrintf("Data Name : %s\n", name);
mexPrintf("Transform : [%1.6f, %1.6f %1.6f %1.6f]\n", mat[0][0], mat[0][1], mat[0][2], mat[0][3]);
mexPrintf(" [%1.6f, %1.6f %1.6f %1.6f]\n", mat[1][0], mat[1][1], mat[1][2], mat[1][3]);
mexPrintf(" [%1.6f, %1.6f %1.6f %1.6f]\n", mat[2][0], mat[2][1], mat[2][2], mat[2][3]);
mexPrintf(" [%1.6f, %1.6f %1.6f %1.6f]\n", mat[3][0], mat[3][1], mat[3][2], mat[3][3]);
// ---------------------------------------------------------------
// Set up OpenIGTLink Connection
igtl::MexClientSocket::Pointer socket;
socket = igtl::MexClientSocket::New();
int r = socket->SetDescriptor(sd);
if (r != 0)
{
mexErrMsgTxt("Invalid socket descriptor.");
}
// ---------------------------------------------------------------
// Prepare Transform message
igtl::TransformMessage::Pointer transMsg = igtl::TransformMessage::New();
transMsg->SetDeviceName(name);
transMsg->SetMatrix(mat);
transMsg->Pack();
socket->Send(transMsg->GetPackPointer(), transMsg->GetPackSize());
mexPrintf("The transform has been sent.\n");
return 1;
}
int procImageData(int sd, const char* name, const mxArray *ptr)
{
mxArray* imageField = mxGetField(ptr, 0, "Image");
mxArray* transField = mxGetField(ptr, 0, "Trans");
mxClassID DataType;
DataType = mxGetClassID(imageField);
switch(DataType)
{
case mxINT8_CLASS: { igtlInt8 dummy = igtl::ImageMessage::TYPE_INT8; procTypedImageData(sd, name, imageField, transField, dummy); break; }
case mxUINT8_CLASS: { igtlUint8 dummy = igtl::ImageMessage::TYPE_UINT8; procTypedImageData(sd, name, imageField, transField, dummy); break; }
case mxINT16_CLASS: { igtlInt16 dummy = igtl::ImageMessage::TYPE_INT16; procTypedImageData(sd, name, imageField, transField, dummy); break; }
case mxUINT16_CLASS: { igtlUint16 dummy = igtl::ImageMessage::TYPE_UINT16; procTypedImageData(sd, name, imageField, transField, dummy); break; }
case mxINT32_CLASS: { igtlInt32 dummy = igtl::ImageMessage::TYPE_INT32; procTypedImageData(sd, name, imageField, transField, dummy); break; }
case mxUINT32_CLASS: { igtlUint32 dummy = igtl::ImageMessage::TYPE_UINT32; procTypedImageData(sd, name, imageField, transField, dummy); break; }
case mxSINGLE_CLASS: { igtlFloat32 dummy = igtl::ImageMessage::TYPE_FLOAT32; procTypedImageData(sd, name, imageField, transField, dummy); break; }
case mxDOUBLE_CLASS: { igtlFloat64 dummy = igtl::ImageMessage::TYPE_FLOAT64; procTypedImageData(sd, name, imageField, transField, dummy); break; }
default: { igtlFloat32 dummy = igtl::ImageMessage::TYPE_FLOAT32; procTypedImageData(sd, name, imageField, transField, dummy); break; }
}
return 1;
}
template<typename DATATYPE> void procTypedImageData(int sd, const char* name, const mxArray *imField, const mxArray *trField, DATATYPE dtype)
{
DATATYPE* rdata = (DATATYPE*)mxGetPr(imField);
int ndim = mxGetNumberOfDimensions(imField);
const mwSize* s = mxGetDimensions(imField);
int size[3];
size[0] = s[0]; size[1] = s[1]; size[2] = (ndim == 3)? s[2] : 1;
double* trans = mxGetPr(trField);
float norm_i[] = {trans[0], trans[1], trans[2]};
float norm_j[] = {trans[4], trans[5], trans[6]};
float norm_k[] = {trans[8], trans[9], trans[10]};
float pos[] = {trans[12], trans[13], trans[14]};
float spacing[3];
int svoffset[] = {0, 0, 0};
// calculate spacing
spacing[0] = sqrt(norm_i[0]*norm_i[0] + norm_i[1]*norm_i[1] + norm_i[2]*norm_i[2]);
spacing[1] = sqrt(norm_j[0]*norm_j[0] + norm_j[1]*norm_j[1] + norm_j[2]*norm_j[2]);
spacing[2] = sqrt(norm_k[0]*norm_k[0] + norm_k[1]*norm_k[1] + norm_k[2]*norm_k[2]);
// normalize
for (int i = 0; i < 3; i ++)
{
norm_i[i] /= spacing[0];
norm_j[i] /= spacing[1];
norm_k[i] /= spacing[2];
}
igtl::Matrix4x4 mat;
mat[0][0] = norm_i[0]; mat[0][1] = norm_j[0]; mat[0][2] = norm_k[0]; mat[0][3] = pos[0];
mat[1][0] = norm_i[1]; mat[1][1] = norm_j[1]; mat[1][2] = norm_k[1]; mat[1][3] = pos[1];
mat[2][0] = norm_i[2]; mat[2][1] = norm_j[2]; mat[2][2] = norm_k[2]; mat[2][3] = pos[2];
mat[3][0] = 0.0; mat[3][1] = 0.0; mat[3][2] = 0.0; mat[3][3] = 1;
// print variables
mexPrintf("Data Name : %s\n", name);
mexPrintf("Size : (%d, %d, %d)\n", size[0], size[1], size[2]);
mexPrintf("Spacing : (%f, %f, %f)\n", spacing[0], spacing[1], spacing[2]);
mexPrintf("Transform : [%1.6f, %1.6f %1.6f %1.6f]\n", mat[0][0], mat[0][1], mat[0][2], mat[0][3]);
mexPrintf(" [%1.6f, %1.6f %1.6f %1.6f]\n", mat[1][0], mat[1][1], mat[1][2], mat[1][3]);
mexPrintf(" [%1.6f, %1.6f %1.6f %1.6f]\n", mat[2][0], mat[2][1], mat[2][2], mat[2][3]);
mexPrintf(" [%1.6f, %1.6f %1.6f %1.6f]\n", mat[3][0], mat[3][1], mat[3][2], mat[3][3]);
// ---------------------------------------------------------------
// Set up OpenIGTLink Connection
igtl::MexClientSocket::Pointer socket;
socket = igtl::MexClientSocket::New();
int r = socket->SetDescriptor(sd);
if (r != 0)
{
mexErrMsgTxt("Invalid socket descriptor.");
}
// ---------------------------------------------------------------
// Prepare image message
igtl::ImageMessage::Pointer imgMsg = igtl::ImageMessage::New();
imgMsg->SetDimensions(size);
imgMsg->SetMatrix(mat);
imgMsg->SetSpacing(spacing);
imgMsg->SetEndian(igtl::ImageMessage::ENDIAN_LITTLE);
imgMsg->SetScalarType(dtype);
imgMsg->SetDeviceName(name);
imgMsg->SetSubVolume(size, svoffset);
imgMsg->AllocateScalars();
int ni = size[0]; int nj = size[1]; int nk = size[2];
DATATYPE* dest = (DATATYPE*)imgMsg->GetScalarPointer();
for (int k = 0; k < nk; k ++)
{
int koff = k*ni*nj;
for (int j = 0; j < nj; j ++)
{
for (int i = 0; i < ni; i ++)
{
dest[koff + j*ni + i] = (DATATYPE)rdata[koff + i*nj + j];
}
}
}
// ---------------------------------------------------------------
// Send image message
imgMsg->Pack();
socket->Send(imgMsg->GetPackPointer(), imgMsg->GetPackSize());
mexPrintf("The image has been sent.\n");
}
double getMatlabScalar (const mxArray* ptr) {
// Make sure the input argument is a scalar in double-precision.
if (!mxIsDouble(ptr) || mxGetNumberOfElements(ptr) != 1)
mexErrMsgTxt("The input argument must be a double-precision scalar");
return *mxGetPr(ptr);
}
double& createMatlabScalar (mxArray*& ptr) {
ptr = mxCreateDoubleMatrix(1,1,mxREAL);
return *mxGetPr(ptr);
}