-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathneighborhoodloop.c
473 lines (411 loc) · 12.2 KB
/
neighborhoodloop.c
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
#include "mex.h"
#include <string.h>
#define DEBUG 0
/*
* NEIGHBORHOODLOOP
*
* See the help text in neighborhoodloop.m for a description.
*
* Author: Gunnar Farnebäck
* Medical Informatics
* Linköping University, Sweden
*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int N;
int *neighborhoodsize;
int k, r;
int x;
int number_of_arrays;
int number_of_points;
int *number_of_elements;
int *number_of_elements2;
int max_number_of_dimensions;
/* Set if the callback function is a function handle
* or an inline object.
*/
int function_is_handle;
int function_name_length;
char *function_name;
int first_extra_parameter;
int number_of_extra_parameters;
mxArray **input_arrays;
mxArray **output_arrays;
int *dims;
int num_dims;
int *neighborhood_coords;
int *neighborhood_delta;
int number_of_neighborhood_points;
int *xcoords;
/* Check the input and output arguments. */
/* First we expect a scalar. */
if (!mxIsNumeric(prhs[0]) || mxIsComplex(prhs[0])
|| mxIsSparse(prhs[0]) || !mxIsDouble(prhs[0])
|| mxGetNumberOfDimensions(prhs[0]) > 2
|| mxGetDimensions(prhs[0])[0] != 1
|| mxGetDimensions(prhs[0])[1] != 1)
{
mexErrMsgTxt("N is expected to be a scalar.");
}
N = (int) mxGetScalar(prhs[0]);
if ((double) N != mxGetScalar(prhs[0]))
mexErrMsgTxt("N is expected to be an integer.");
if (N < 0)
mexErrMsgTxt("N must not be negative.");
/* Second we expect another scalar or a vector of length N. */
if (!mxIsNumeric(prhs[1]) || mxIsComplex(prhs[1])
|| mxIsSparse(prhs[1]) || !mxIsDouble(prhs[1])
|| mxGetNumberOfDimensions(prhs[1]) > 2
|| ((mxGetDimensions(prhs[1])[0] != 1
|| mxGetDimensions(prhs[1])[1] != 1)
&& (mxGetDimensions(prhs[1])[0]
* mxGetDimensions(prhs[1])[1] != N)))
{
mexErrMsgTxt("size is expected to be a scalar or a vector of length N.");
}
neighborhoodsize = mxCalloc(N, sizeof(*neighborhoodsize));
if (neighborhoodsize == NULL)
mexErrMsgTxt("Failed to allocate an array.");
number_of_neighborhood_points = 1;
for (r = 0; r < N; r++)
{
double size;
if (mxGetDimensions(prhs[1])[0] * mxGetDimensions(prhs[1])[1] == 1)
size = mxGetScalar(prhs[1]);
else
size = mxGetPr(prhs[1])[r];
neighborhoodsize[r] = (int) size;
number_of_neighborhood_points *= neighborhoodsize[r];
if ((double) neighborhoodsize[r] != size)
mexErrMsgTxt("size is expected to be integer.");
if (neighborhoodsize[r] < 0 || neighborhoodsize[r] % 2 != 1)
mexErrMsgTxt("size must be positive and odd.");
}
/* Then a number of arrays. */
number_of_arrays = 0;
number_of_points = 1;
max_number_of_dimensions = 0;
for (k = 2; k < nrhs; k++)
{
int n;
if (mxIsChar(prhs[k]))
{
function_is_handle = 0;
break;
}
if (mxGetNumberOfElements(prhs[k]) == 1)
{
function_is_handle = 1;
break;
}
if (!mxIsNumeric(prhs[k]) || mxIsSparse(prhs[k])
|| !mxIsDouble(prhs[k]))
{
mexPrintf("Expected array for argument %d.", k + 1);
mexErrMsgTxt("");
}
n = mxGetNumberOfDimensions(prhs[k]);
if (n < N)
{
mexPrintf("Argument %d has fewer dimensions than N.", k + 1);
mexErrMsgTxt("");
}
if (max_number_of_dimensions < n)
max_number_of_dimensions = n;
for (r = 0; r < N; r++)
{
int dim1, dim2;
if (mxGetNumberOfDimensions(prhs[2]) <= r)
dim1 = 1;
else
dim1 = mxGetDimensions(prhs[2])[r];
if (k == 2)
number_of_points *= dim1;
else
{
if (mxGetNumberOfDimensions(prhs[k]) <= r)
dim2 = 1;
else
dim2 = mxGetDimensions(prhs[k])[r];
if (dim1 != dim2)
{
mexPrintf("Argument %d and argument 3 have incompatible sizes.",
k + 2);
mexErrMsgTxt("");
}
}
}
number_of_arrays++;
}
if (number_of_arrays == 0)
mexErrMsgTxt("Expected array for argument 3.");
if (k == nrhs)
mexErrMsgTxt("No function name provided.");
if (!function_is_handle)
{
function_name_length = mxGetM(prhs[k]) * mxGetN(prhs[k]);
function_name = mxCalloc(function_name_length + 1, 1);
if (function_name == NULL)
mexErrMsgTxt("Failed to allocate space for function name.");
if (mxGetString(prhs[k], function_name, function_name_length + 1) != 0)
mexErrMsgTxt("Failed to convert function name to string.");
}
else
function_name = "feval";
first_extra_parameter = k + 1;
number_of_extra_parameters = nrhs - first_extra_parameter;
if (nlhs == 0)
nlhs = 1;
/* Create some auxiliary arrays. */
number_of_elements = mxCalloc(number_of_arrays,
sizeof(*number_of_elements));
if (number_of_elements == NULL)
mexErrMsgTxt("Failed to allocate an array.");
number_of_elements2 = mxCalloc(nlhs, sizeof(*number_of_elements2));
if (number_of_elements2 == NULL)
mexErrMsgTxt("Failed to allocate an array.");
/* Create array of input arrays for the callback. */
input_arrays = mxCalloc(number_of_arrays + function_is_handle
+ number_of_extra_parameters,
sizeof(*input_arrays));
if (input_arrays == NULL)
mexErrMsgTxt("Failed to allocate an array.");
/* If the function is a handle, we need to pass the handle as the
* first argument to the feval call.
*/
if (function_is_handle)
input_arrays[0] = (mxArray *) prhs[k];
/* Populate it with neighborhood arrays. */
dims = mxCalloc(max_number_of_dimensions, sizeof(*dims));
if (dims == NULL)
mexErrMsgTxt("Failed to allocate an array.");
for (k = 0; k < number_of_arrays; k++)
{
num_dims = 0;
number_of_elements[k] = 1;
for (r = 0; r < mxGetNumberOfDimensions(prhs[k + 2]); r++)
{
int d = mxGetDimensions(prhs[k + 2])[r];
if (r >= N) {
dims[r] = d;
number_of_elements[k] *= d;
}
else
dims[r] = neighborhoodsize[r];
num_dims++;
}
for (; num_dims < 2; num_dims++)
dims[num_dims] = 1;
input_arrays[k + function_is_handle] =
mxCreateNumericArray(num_dims, dims, mxDOUBLE_CLASS,
mxIsComplex(prhs[k + 2]) ?
mxCOMPLEX : mxREAL);
if (input_arrays[k + function_is_handle] == NULL)
mexErrMsgTxt("Failed to create an array.");
}
/* Continue population with the fixed parameters. */
for (k = 0; k < number_of_extra_parameters; k++)
input_arrays[number_of_arrays + function_is_handle + k] =
(mxArray *) prhs[first_extra_parameter + k];
/* Create array of output arrays for the callback. */
output_arrays = mxCalloc(nlhs, sizeof(*output_arrays));
if (output_arrays == NULL)
mexErrMsgTxt("Failed to allocate an array.");
/* Set up the first N dimension sizes. */
for (r = 0; r < N; r++)
{
if (r < mxGetNumberOfDimensions(prhs[2]))
dims[r] = mxGetDimensions(prhs[2])[r];
else
dims[r] = 1;
}
/* Create array to store x coordinates in. */
xcoords = mxCalloc(N, sizeof(*xcoords));
if (xcoords == NULL)
mexErrMsgTxt("Failed to allocate an array.");
/* Set up arrays to keep track of the neighborhood points. */
neighborhood_coords = mxCalloc(number_of_neighborhood_points * N,
sizeof(*neighborhood_coords));
if (neighborhood_coords == NULL)
mexErrMsgTxt("Failed to allocate an array.");
neighborhood_delta = mxCalloc(number_of_neighborhood_points,
sizeof(*neighborhood_delta));
if (neighborhood_delta == NULL)
mexErrMsgTxt("Failed to allocate an array.");
for (k = 0; k < number_of_neighborhood_points; k++)
{
int kk = k;
int delta = 0;
for (r = 0; r < N; r++)
{
int coord = kk % neighborhoodsize[r] - neighborhoodsize[r] / 2;
kk /= neighborhoodsize[r];
neighborhood_coords[k * N + r] = coord;
}
for (r = N - 1; r >= 0; r--)
{
int coord = neighborhood_coords[k * N + r];
delta *= dims[r];
delta += coord;
}
neighborhood_delta[k] = delta;
}
/* Time to start looping. */
for (x = 0; x < number_of_points; x++)
{
int xx = x;
for (k = 0; k < N; k++)
{
xcoords[k] = xx % dims[k];
xx /= dims[k];
}
/* Copy the content of neighborhoods at this point of each array. */
for (k = 0; k < number_of_arrays; k++)
{
int components;
double *source;
double *target;
/* Loop over the real and complex components. */
for (components = 0; components < 2; components++)
{
if (components == 0)
{
source = mxGetPr(prhs[k + 2]);
target = mxGetPr(input_arrays[k + function_is_handle]);
}
else
{
if (!mxIsComplex(input_arrays[k + function_is_handle]))
break;
source = mxGetPi(prhs[k + 2]);
target = mxGetPi(input_arrays[k + function_is_handle]);
}
/* Copy the neighborhood for this component. */
for (r = 0; r < number_of_elements[k]; r++)
{
int s;
for (s = 0; s < number_of_neighborhood_points; s++)
{
double value = 1.0;
int t;
for (t = 0; t < N; t++)
{
int coord = xcoords[t] + neighborhood_coords[t + s * N];
if (coord < 0 || coord >= dims[t])
{
value = 0.0;
break;
}
}
if (value != 0.0)
value = source[r * number_of_points + x
+ neighborhood_delta[s]];
target[r * number_of_neighborhood_points + s] = value;
}
}
}
}
/* Call back to matlab. */
mexCallMATLAB(nlhs, output_arrays,
number_of_arrays + number_of_extra_parameters
+ function_is_handle,
input_arrays, function_name);
/* If this is the first turn of the loop, allocate the output
* arrays from this function. We had to wait with this until
* we could know their sizes.
*/
if (x == 0) {
/* Check the maximum number of dimensions. */
max_number_of_dimensions = 0;
for (k = 0; k < nlhs; k++)
{
int M = N + mxGetNumberOfDimensions(output_arrays[k]);
if (max_number_of_dimensions < M)
max_number_of_dimensions = M;
}
/* Reallocate dims. */
mxFree(dims);
dims = mxCalloc(max_number_of_dimensions, sizeof(*dims));
if (dims == NULL)
mexErrMsgTxt("Failed to allocate an array.");
/* Set up the first N dimension sizes. */
for (r = 0; r < N; r++)
{
if (r < mxGetNumberOfDimensions(prhs[2]))
dims[r] = mxGetDimensions(prhs[2])[r];
else
dims[r] = 1;
}
/* Loop over each array. */
for (k = 0; k < nlhs; k++)
{
/* Add the remaining dimensions. */
num_dims = N;
number_of_elements2[k] = 1;
for (r = 0;
r < mxGetNumberOfDimensions(output_arrays[k]);
r++)
{
int d = mxGetDimensions(output_arrays[k])[r];
dims[N + r] = d;
num_dims++;
number_of_elements2[k] *= d;
}
plhs[k] = mxCreateNumericArray(num_dims, dims, mxDOUBLE_CLASS,
mxIsComplex(output_arrays[k]) ?
mxCOMPLEX : mxREAL);
if (plhs[k] == NULL)
mexErrMsgTxt("Failed to create an array.");
}
}
/* Copy output. */
for (k = 0; k < nlhs; k++)
{
double *source = mxGetPr(output_arrays[k]);
double *target = mxGetPr(plhs[k]);
for (r = 0; r < number_of_elements2[k]; r++)
target[r * number_of_points + x] = source[r];
if (mxIsComplex(output_arrays[k]))
{
if (!mxIsComplex(plhs[k]))
{
/* Oh, looks like we need to be complex after all.
* Allocate a new array and copy the real data.
*/
mxArray *new_array =
mxCreateNumericArray(mxGetNumberOfDimensions(plhs[k]),
mxGetDimensions(plhs[k]),
mxDOUBLE_CLASS, mxCOMPLEX);
if (new_array == NULL)
mexErrMsgTxt("Failed to create an array.");
for (r = 0;
r < number_of_points * number_of_elements2[k];
r++)
{
mxGetPr(new_array)[r] = mxGetPr(plhs[k])[r];
}
mxDestroyArray(plhs[k]);
plhs[k] = new_array;
}
source = mxGetPi(output_arrays[k]);
target = mxGetPi(plhs[k]);
for (r = 0; r < number_of_elements2[k]; r++)
target[r * number_of_points + x] = source[r];
}
/* Free array space. */
mxDestroyArray(output_arrays[k]);
}
}
mxFree(neighborhoodsize);
if (!function_is_handle)
mxFree(function_name);
mxFree(number_of_elements);
mxFree(number_of_elements2);
mxFree(input_arrays);
mxFree(output_arrays);
mxFree(dims);
mxFree(xcoords);
mxFree(neighborhood_coords);
mxFree(neighborhood_delta);
}