-
Notifications
You must be signed in to change notification settings - Fork 4
/
igtlreceive.cpp
750 lines (626 loc) · 22.8 KB
/
igtlreceive.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*=========================================================================
Program: Matlab Open IGT Link Interface -- igtlreceive
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.
=========================================================================*/
//
// DATA = igtlreceive(SD, timeout);
//
// DATA: (structure) Data contents
// SD : (integer) Socket descriptor (-1 if failed to connect)
// timeout: (integer/optional) Timeout in milliseconds
//
// Data fields for IMAGE data
// DATA.Status:(uint16) 0:Disconnected; -1:Timeout 1:Received 2:Invalid 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.Status:(uint16) 0:Disconnected; -1:Timeout 1:Received 2:Invalid 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 "igtlTransformMessage.h"
#include "igtlPositionMessage.h"
#include "igtlImageMessage.h"
#include "igtlMexClientSocket.h"
using namespace std;
//#define pi (3.141592653589793)
#define ARG_ID_SD 0
#define ARG_ID_TIMEOUT 1 // timeout
#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 waitAndReceiveMessage(int sd, mxArray *plhs[], int timeout);
int receiveTransform(igtl::MexClientSocket::Pointer& socket,
igtl::MessageHeader::Pointer& headerMsg,
mxArray *plhs[]);
int receivePosition(igtl::MexClientSocket::Pointer& socket,
igtl::MessageHeader::Pointer& headerMsg,
mxArray *plhs[]);
int receiveImage(igtl::MexClientSocket::Pointer& socket,
igtl::MessageHeader::Pointer& headerMsg,
mxArray *plhs[]);
// -----------------------------------------------------------------
// Function definitions.
void mexFunction (int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
// ---------------------------------------------------------------
// Check arguments
if (checkArguments(nlhs, plhs, nrhs, prhs) == 0)
{
//double& retVal = createMatlabScalar(plhs[0]);
//retVal = 0;
return;
}
// ---------------------------------------------------------------
// Set socket descripter
int sd;
sd = (int)*mxGetPr(prhs[ARG_ID_SD]);
// ---------------------------------------------------------------
// Set Timeout, if specified
int timeout = -1; // -1 means no timeout.
if (nrhs > ARG_ID_TIMEOUT)
{
timeout = (int)*mxGetPr(prhs[ARG_ID_TIMEOUT]);
}
// ---------------------------------------------------------------
// Wait for the message
waitAndReceiveMessage(sd, plhs, timeout);
}
int checkArguments(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
// ---------------------------------------------------------------
// Check numbers of arguments and outputs
if (nrhs < 1 || 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;
}
// timeout -- socket descriptor
if (nrhs > ARG_ID_TIMEOUT)
{
if (!mxIsNumeric(prhs[ARG_ID_TIMEOUT]))
{
mexErrMsgTxt("timeout argument must be integer.");
return 0;
}
}
return 1;
}
void createReturnStructureAndSetError(mxArray* plhs[], int ret)
{
// Get strcutre for returned value
const char* fnames [] = {
"Status"
};
plhs[0] = mxCreateStructMatrix(1, 1, 1, fnames);
mxArray* statusString = mxCreateDoubleScalar(ret);
mxSetField(plhs[0], 0, "Status", statusString);
}
int waitAndReceiveMessage(int sd, mxArray *plhs[], int timeout)
{
int r;
// ---------------------------------------------------------------
// Create a message buffer to receive data
igtl::MessageHeader::Pointer headerMsg;
headerMsg = igtl::MessageHeader::New();
// ---------------------------------------------------------------
// Set up OpenIGTLink Connection
igtl::MexClientSocket::Pointer socket;
socket = igtl::MexClientSocket::New();
r = socket->SetDescriptor(sd);
if (r != 0)
{
mexErrMsgTxt("Invalid socket descriptor.");
createReturnStructureAndSetError(plhs, 0);
return 0;
}
// ---------------------------------------------------------------
// Set timeout if timeout >= 0
if (timeout >= 0)
{
socket->SetReceiveTimeout(timeout);
}
else
{
socket->SetReceiveTimeout(-1);
}
// ---------------------------------------------------------------
// Receive generic header from the socket
headerMsg->InitPack();
r = socket->Receive(headerMsg->GetPackPointer(), headerMsg->GetPackSize());
if (r == 0)
{
socket->CloseSocket();
createReturnStructureAndSetError(plhs, 0);
return 0;
}
if (r < 0)
{
createReturnStructureAndSetError(plhs, -1);
return -1;
}
if (r != headerMsg->GetPackSize())
{
createReturnStructureAndSetError(plhs, 2);
return 2;
}
// Deserialize the header
headerMsg->Unpack();
// ---------------------------------------------------------------
// Check data type and receive data body
if (strcmp(headerMsg->GetDeviceType(), "TRANSFORM") == 0)
{
// Get strcutre for returned value
const char* fnames [] = {
"Status", "Type", "Name", "Trans"
};
plhs[0] = mxCreateStructMatrix(1, 1, 4, fnames);
printf("procReceiveTransform(socket, headerMsg, plhs);\n");
receiveTransform(socket, headerMsg, plhs);
}
else if (strcmp(headerMsg->GetDeviceType(), "IMAGE") == 0)
{
// Get strcutre for returned value
const char* fnames [] = {
"Status", "Type", "Name", "Trans", "Image"
};
plhs[0] = mxCreateStructMatrix(1, 1, 5, fnames);
receiveImage(socket, headerMsg, plhs);
}
else if (strcmp(headerMsg->GetDeviceType(), "POSITION") == 0)
{
// Get strcutre for returned value
const char* fnames [] = {
"Status", "Type", "Name", "Pos", "Quat"
};
plhs[0] = mxCreateStructMatrix(1, 1, 5, fnames);
receivePosition(socket, headerMsg, plhs);
}
//else if (strcmp(headerMsg->GetDeviceType(), "STATUS") == 0)
// {
// procReceiveStatus(socket, headerMsg);
// }
else
{
socket->Skip(headerMsg->GetBodySizeToRead(), 0);
createReturnStructureAndSetError(plhs, 2);
return 2;
}
mxArray* statusString = mxCreateDoubleScalar(1);
mxSetField(plhs[0], 0, "Status", statusString);
return 1;
}
int receiveTransform(igtl::MexClientSocket::Pointer& socket,
igtl::MessageHeader::Pointer& headerMsg,
mxArray *plhs[])
{
std::cerr << "Receiving TRANSFORM data type." << std::endl;
// Create a message buffer to receive transform data
igtl::TransformMessage::Pointer transMsg;
transMsg = igtl::TransformMessage::New();
transMsg->SetMessageHeader(headerMsg);
transMsg->AllocatePack();
// Receive transform data from the socket
socket->Receive(transMsg->GetPackBodyPointer(), transMsg->GetPackBodySize());
// Deserialize the transform data
// If you want to skip CRC check, call Unpack() without argument.
int c = transMsg->Unpack(1);
if (c & igtl::MessageHeader::UNPACK_BODY) // if CRC check is OK
{
// Retrive the transform data
igtl::Matrix4x4 mat;
transMsg->GetMatrix(mat);
igtl::PrintMatrix(mat);
// Set type string
mxArray* typeString = mxCreateString("TRANSFORM");
mxSetField(plhs[0], 0, "Type", typeString);
// Set device name string
mxArray* nameString = mxCreateString(transMsg->GetDeviceName());
mxSetField(plhs[0], 0, "Name", nameString);
// Set transform
mxArray* transMatrix = mxCreateDoubleMatrix(4, 4, mxREAL);
double* trans = mxGetPr(transMatrix);
trans[0] = mat[0][0]; trans[4] = mat[0][1]; trans[8] = mat[0][2]; trans[12] = mat[0][3];
trans[1] = mat[1][0]; trans[5] = mat[1][1]; trans[9] = mat[1][2]; trans[13] = mat[1][3];
trans[2] = mat[2][0]; trans[6] = mat[2][1]; trans[10] = mat[2][2]; trans[14] = mat[2][3];
trans[3] = mat[3][0]; trans[7] = mat[3][1]; trans[11] = mat[3][2]; trans[15] = mat[3][3];
mxSetField(plhs[0], 0, "Trans", transMatrix);
return 1;
}
else
{
return 0;
}
}
int receivePosition(igtl::MexClientSocket::Pointer& socket,
igtl::MessageHeader::Pointer& headerMsg,
mxArray *plhs[])
{
std::cerr << "Receiving POSITION data type." << std::endl;
// Create a message buffer to receive transform data
igtl::PositionMessage::Pointer posMsg;
posMsg = igtl::PositionMessage::New();
posMsg->SetMessageHeader(headerMsg);
posMsg->AllocatePack();
// Receive transform data from the socket
socket->Receive(posMsg->GetPackBodyPointer(), posMsg->GetPackBodySize());
// Deserialize the transform data
// If you want to skip CRC check, call Unpack() without argument.
int c = posMsg->Unpack(1);
if (c & igtl::MessageHeader::UNPACK_BODY) // if CRC check is OK
{
// Retrive position and quaternion data
float position[3];
float quaternion[4];
posMsg->GetPosition(position);
posMsg->GetQuaternion(quaternion);
// print position and quaternion
std::cerr << "position = ("
<< position[0] << ","
<< position[1] << ","
<< position[2] << ")" << std::endl;
std::cerr << "quaternion = ("
<< quaternion[0] << ","
<< quaternion[1] << ","
<< quaternion[2] << ","
<< quaternion[3] << ")" << std::endl;
// Set type string
mxArray* typeString = mxCreateString("POSITION");
mxSetField(plhs[0], 0, "Type", typeString);
// Set device name string
mxArray* nameString = mxCreateString(posMsg->GetDeviceName());
mxSetField(plhs[0], 0, "Name", nameString);
// Set position and quaternion
mxArray* posMatrix = mxCreateDoubleMatrix(3, 1, mxREAL);
double* pos = mxGetPr(posMatrix);
mxArray* quatMatrix = mxCreateDoubleMatrix(4, 1, mxREAL);
double* quat = mxGetPr(quatMatrix);
pos[0] = position[0];
pos[1] = position[1];
pos[2] = position[2];
mxSetField(plhs[0], 0, "Pos", posMatrix);
quat[0] = quaternion[0];
quat[1] = quaternion[1];
quat[2] = quaternion[2];
quat[3] = quaternion[3];
mxSetField(plhs[0], 0, "Quat", quatMatrix);
return 1;
}
else
{
return 0;
}
}
int receiveImage(igtl::MexClientSocket::Pointer& socket,
igtl::MessageHeader::Pointer& headerMsg,
mxArray *plhs[])
{
// Create a message buffer to receive transform data
igtl::ImageMessage::Pointer imgMsg;
imgMsg = igtl::ImageMessage::New();
imgMsg->SetMessageHeader(headerMsg);
imgMsg->AllocatePack();
// Receive transform data from the socket
socket->Receive(imgMsg->GetPackBodyPointer(), imgMsg->GetPackBodySize());
// Deserialize the transform data
// If you want to skip CRC check, call Unpack() without argument.
int c = imgMsg->Unpack(1);
if (c & igtl::MessageHeader::UNPACK_BODY) // if CRC check is OK
{
// Retrive the image data
int size[3]; // image dimension
float spacing[3]; // spacing (mm/pixel)
int svsize[3]; // sub-volume size
int svoffset[3]; // sub-volume offset
int scalarType; // scalar type
igtl::Matrix4x4 mat; // Image origin and orientation matrix
scalarType = imgMsg->GetScalarType();
imgMsg->GetDimensions(size);
imgMsg->GetSpacing(spacing);
imgMsg->GetSubVolume(svsize, svoffset);
imgMsg->GetMatrix(mat);
// Set type string
mxArray* typeString = mxCreateString("IMAGE");
mxSetField(plhs[0], 0, "Type", typeString);
// Set device name string
mxArray* nameString = mxCreateString(imgMsg->GetDeviceName());
mxSetField(plhs[0], 0, "Name", nameString);
// Set transform
mxArray* transMatrix = mxCreateDoubleMatrix(4, 4, mxREAL);
double* trans = mxGetPr(transMatrix);
mat[0][0] *= spacing[0]; mat[0][1] *= spacing[1]; mat[0][2] *= spacing[2];
mat[1][0] *= spacing[0]; mat[1][1] *= spacing[1]; mat[1][2] *= spacing[2];
mat[2][0] *= spacing[0]; mat[2][1] *= spacing[1]; mat[2][2] *= spacing[2];
trans[0] = mat[0][0]; trans[4] = mat[0][1]; trans[8] = mat[0][2]; trans[12] = mat[0][3];
trans[1] = mat[1][0]; trans[5] = mat[1][1]; trans[9] = mat[1][2]; trans[13] = mat[1][3];
trans[2] = mat[2][0]; trans[6] = mat[2][1]; trans[10] = mat[2][2]; trans[14] = mat[2][3];
trans[3] = mat[3][0]; trans[7] = mat[3][1]; trans[11] = mat[3][2]; trans[15] = mat[3][3];
mxSetField(plhs[0], 0, "Trans", transMatrix);
// Set Image
mwSize dims[3];
dims[0] = size[0];
dims[1] = size[1];
dims[2] = size[2];
printf("size[] = {%d, %d, %d} \n", dims[0], dims[1], dims[2]);
//mxArray* imageMatrix = mxCreateNumericArray(3, dims, mxDOUBLE_CLASS, mxREAL);
//double* rdata = (double*) mxGetData(imageMatrix);
mxArray* imageMatrix;
int ni = size[0]; int nj = size[1]; int nk = size[2];
if (scalarType == igtl::ImageMessage::TYPE_INT8)
{
imageMatrix = mxCreateNumericArray(3, dims, mxINT8_CLASS, mxREAL);
igtlInt8* rdata = (igtlInt8*) mxGetData(imageMatrix);
printf("scalarType = TYPE_INT8\n");
igtlInt8* src = (igtlInt8*)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 ++)
{
rdata[koff + i*nj + j] = src[koff + j*ni + i];
}
}
}
}
else if (scalarType == igtl::ImageMessage::TYPE_UINT8)
{
imageMatrix = mxCreateNumericArray(3, dims, mxUINT8_CLASS, mxREAL);
igtlUint8* rdata = (igtlUint8*) mxGetData(imageMatrix);
printf("scalarType = TYPE_UINT8\n");
igtlUint8* src = (igtlUint8*)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 ++)
{
rdata[koff + i*nj + j] = src[koff + j*ni + i];
}
}
}
}
else if (scalarType == igtl::ImageMessage::TYPE_INT16)
{
imageMatrix = mxCreateNumericArray(3, dims, mxINT16_CLASS, mxREAL);
igtlInt16* rdata = (igtlInt16*) mxGetData(imageMatrix);
printf("scalarType = TYPE_INT16\n");
igtlInt16* src = (igtlInt16*)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 ++)
{
rdata[koff + i*nj + j] = src[koff + j*ni + i];
}
}
}
}
else if (scalarType == igtl::ImageMessage::TYPE_UINT16)
{
imageMatrix = mxCreateNumericArray(3, dims, mxUINT16_CLASS, mxREAL);
igtlUint16* rdata = (igtlUint16*) mxGetData(imageMatrix);
printf("scalarType = TYPE_UINT16\n");
igtlUint16* src = (igtlUint16*)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 ++)
{
rdata[koff + i*nj + j] = src[koff + j*ni + i];
}
}
}
}
else if (scalarType == igtl::ImageMessage::TYPE_INT32)
{
imageMatrix = mxCreateNumericArray(3, dims, mxINT32_CLASS, mxREAL);
igtlInt32* rdata = (igtlInt32*) mxGetData(imageMatrix);
printf("scalarType = TYPE_INT32\n");
igtlInt32* src = (igtlInt32*)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 ++)
{
rdata[koff + i*nj + j] = src[koff + j*ni + i];
}
}
}
}
else if (scalarType == igtl::ImageMessage::TYPE_UINT32)
{
imageMatrix = mxCreateNumericArray(3, dims, mxUINT32_CLASS, mxREAL);
igtlUint32* rdata = (igtlUint32*) mxGetData(imageMatrix);
printf("scalarType = TYPE_UINT32\n");
igtlUint32* src = (igtlUint32*)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 ++)
{
rdata[koff + i*nj + j] = src[koff + j*ni + i];
}
}
}
}
else if (scalarType == igtl::ImageMessage::TYPE_FLOAT32)
{
imageMatrix = mxCreateNumericArray(3, dims, mxSINGLE_CLASS, mxREAL);
igtlFloat32* rdata = (igtlFloat32*) mxGetData(imageMatrix);
printf("scalarType = TYPE_FLOAT32\n");
igtlFloat32* src = (igtlFloat32*)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 ++)
{
rdata[koff + i*nj + j] = src[koff + j*ni + i];
}
}
}
}
else if (scalarType == igtl::ImageMessage::TYPE_FLOAT64)
{
imageMatrix = mxCreateNumericArray(3, dims, mxDOUBLE_CLASS, mxREAL);
igtlFloat64* rdata = (igtlFloat64*) mxGetData(imageMatrix);
printf("scalarType = TYPE_FLOAT64\n");
igtlFloat64* src = (igtlFloat64*)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 ++)
{
rdata[koff + i*nj + j] = src[koff + j*ni + i];
}
}
}
}
mxSetField(plhs[0], 0, "Image", imageMatrix);
return 1;
}
return 0;
}
/*
int procImageData(int sd, const char* name, const mxArray *ptr)
{
mxArray* imageField = mxGetField(ptr, 0, "Image");
mxArray* transField = mxGetField(ptr, 0, "Trans");
double* rdata = mxGetPr(imageField);
int ndim = mxGetNumberOfDimensions(imageField);
const int* s = mxGetDimensions(imageField);
int size[3];
size[0] = s[0]; size[1] = s[1]; size[2] = (ndim == 3)? s[2]:1;
double* trans = mxGetPr(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 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[i];
norm_j[i] /= spacing[i];
norm_k[i] /= spacing[i];
}
// 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->SetSpacing(spacing);
imgMsg->SetNormals(norm_i, norm_j, norm_k);
imgMsg->SetScalarType(igtl::ImageMessage::TYPE_UINT16);
imgMsg->SetDeviceName(name);
imgMsg->SetSubVolume(size, svoffset);
imgMsg->AllocateScalars();
//int npixel = size[0]*size[1]*size[2];
int ni = size[0]; int nj = size[1]; int nk = size[2];
igtlUint16* dest = (igtlUint16*)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] = (igtlUint16)rdata[koff + i*nj + j];
}
}
}
// ---------------------------------------------------------------
// Send image message
imgMsg->Pack();
socket->Send(imgMsg->GetPackPointer(), imgMsg->GetPackSize());
socket->CloseSocket();
mexPrintf("The image has been sent.\n");
return 1;
}
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);
}
*/