File: igtlNDArrayMessage.cxx

package info (click to toggle)
openigtlink 3.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,080 kB
  • sloc: cpp: 20,076; ansic: 6,704; sh: 227; perl: 74; makefile: 46
file content (290 lines) | stat: -rw-r--r-- 5,485 bytes parent folder | download
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
/*=========================================================================

  Program:   The OpenIGTLink Library
  Language:  C++
  Web page:  http://openigtlink.org/

  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.

=========================================================================*/

#include "igtlNDArrayMessage.h"

#include "igtlTypes.h"

#include "igtl_header.h"
#include "igtl_ndarray.h"

// Disable warning C4996 (strncpy() may be unsafe) in Windows. 
#define _CRT_SECURE_NO_WARNINGS

#include <string.h>

namespace igtl {


ArrayBase::ArrayBase()
{
  this->m_ByteArray = NULL;
  this->m_Size.clear();
}


ArrayBase::~ArrayBase()
{

  if (this->m_ByteArray)
    {
    delete (igtlUint8 *) this->m_ByteArray;
    }
}
  
int ArrayBase::SetSize(IndexType size)
{

  if (this->m_Size == size)
    {
    // If the size of the array is same as specified,
    // do nothing
    return 1;
    }

  if (this->m_ByteArray != NULL)
    {
    delete (igtlUint8 *) this->m_ByteArray;
    }
  this->m_Size = size;

  this->m_ByteArray = (void *) new igtlUint8[GetRawArraySize()];

  if (this->m_ByteArray)
    {
    return 1;
    }
  else
    {
    this->m_Size.clear();
    return 0;
    }
}


int ArrayBase::SetArray(void * array)
{
  if (this->m_ByteArray)
    {
    memcpy(this->m_ByteArray, array, GetRawArraySize());
    return 1;
    }
  else
    {
    return 0;
    }
};


igtlUint64 ArrayBase::GetRawArraySize()
{
  return (igtlUint64) GetNumberOfElements() * (igtlUint64) GetElementSize();
}


igtlUint32 ArrayBase::GetNumberOfElements()
{
  igtlUint32 len = 1;
  IndexType::iterator iter;
  for (iter = this->m_Size.begin(); iter != this->m_Size.end(); iter ++)
    {
    len *= *iter;
    }
  return len;
}


igtlUint32 ArrayBase::Get1DIndex(IndexType index)
{
  igtlUint32 p;
  igtlUint32 step;
  igtlUint16 dim = this->m_Size.size();

  if (this->m_Size.size() != index.size())
    {
    // TODO: how to pass the error to the caller?
    return 0;
    }

  p = 0;
  step = 1;
  IndexType::iterator iter;
  for (int i = dim-1; i >= 0; i --)
    {
    p += index[i] * step;
    step *= this->m_Size[i];
    }
  igtlUint32 m = GetNumberOfElements();
  if (p <= m)
    {
    return p;
    }
  else
    {
    // TODO: how to pass the error to the caller?
    return m; 
    }
}


NDArrayMessage::NDArrayMessage():
  MessageBase()
{
  this->m_SendMessageType = "NDARRAY";
  this->m_Array = NULL;
  this->m_Type = 0;
}


NDArrayMessage::~NDArrayMessage()
{
}


int NDArrayMessage::SetArray(int type, ArrayBase * a)
{
  // Check if type is valid
  if (type < 2 || type > 13 ||
      type == 8 || type == 9 || type == 12)
    {
    return 0;
    }
  this->m_Type = type;

  if (a)
    {
    this->m_Array = a;
    return 1;
    }
  else
    {
    return 0;
    }
}


int NDArrayMessage::CalculateContentBufferSize()
{
  int dataSize;
  int dim;
  if (this->m_Array == NULL)
  {
    return 0;
  }
  dim = this->m_Array->GetDimension();
  dataSize = sizeof(igtlUint8) * 2 + sizeof(igtlUint16) * (igtl_uint64) dim
  + this->m_Array->GetRawArraySize();

  return  dataSize;
}


int NDArrayMessage::PackContent()
{
  // Allocate buffer
  AllocateBuffer();

  if (this->m_Array == NULL)
    {
    return 0;
    }

  igtl_ndarray_info info;

  igtl_ndarray_init_info(&info);
  info.dim  = this->m_Array->GetDimension();
  info.type = this->m_Type;

  ArrayBase::IndexType size = this->m_Array->GetSize();

  igtl_uint16 * s = new igtl_uint16[info.dim];
  for (int i = 0; i < info.dim; i ++)
    {
    s[i] = size[i];
    }
  int r = igtl_ndarray_alloc_info(&info, s);
  delete [] s;

  if (r == 0)
    {
    return 0;
    }

// size.data() doesn't work for some environtments (MacOS X 10.5 and Win32, as far as I know)
//  if (igtl_ndarray_alloc_info(&info, size.data()) == 0)
//    {
//    return 0;
//    }

  memcpy(info.array, this->m_Array->GetRawArray(), this->m_Array->GetRawArraySize());
  igtl_ndarray_pack(&info, this->m_Content, IGTL_TYPE_PREFIX_NONE);

  return 1;
}


int NDArrayMessage::UnpackContent()
{
  igtl_ndarray_info info;
  igtl_ndarray_unpack(IGTL_TYPE_PREFIX_NONE, this->m_Content, &info, this->CalculateReceiveContentSize());

  this->m_Type = info.type;
  ArrayBase::IndexType size;
  size.resize(info.dim);
  for (int i = 0; i < info.dim; i ++)
    {
    size[i] = info.size[i];
    }

  switch (this->m_Type)
    {
    case TYPE_INT8:
      this->m_Array = new Array<igtlInt8>;
      break;
    case TYPE_UINT8:
      this->m_Array = new Array<igtlUint8>;
      break;
    case TYPE_INT16:
      this->m_Array = new Array<igtlInt16>;
      break;
    case TYPE_UINT16:
      this->m_Array = new Array<igtlUint16>;
      break;
    case TYPE_INT32:
      this->m_Array = new Array<igtlInt32>;
      break;
    case TYPE_UINT32:
      this->m_Array = new Array<igtlUint32>;
      break;
    case TYPE_FLOAT32:
      this->m_Array = new Array<igtlFloat32>;
      break;
    case TYPE_FLOAT64:
      this->m_Array = new Array<igtlFloat64>;
      break;
    case TYPE_COMPLEX:
      this->m_Array = new Array<igtlComplex>;
      break;
    default:
      return 0;
      break;
    }

  this->m_Array->SetSize(size);
  memcpy(this->m_Array->GetRawArray(), info.array, this->m_Array->GetRawArraySize());
  
  return 1;
}

} // namespace igtl