This repository has been archived by the owner on Jul 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathCalcGamma.m
687 lines (570 loc) · 27.5 KB
/
CalcGamma.m
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
function gamma = CalcGamma(varargin)
% CalcGamma computes 1-D, 2-D, or 3-D global or absolute gamma between two
% datasets (reference and target) given a defined coordinate space. The
% datasets must have the same number of dimensions, although they can be
% different sizes. Gamma will be computed for each target dose point by
% shifting the reference image (using linear interpolation) and determining
% the minimum Gamma index across all shifts.
%
% This function optionally uses the Parallel Computing Toolbox GPU interp
% functions to increase computation speed. A try-catch statement is used
% to test for GPU support. In addition, for memory management, the
% meshgrid and data arrays are converted to single precision during
% interpolation. This function calls Event() to log execution status, if
% available.
%
% For more information on the Gamma evaluation function, see D. A. Low et
% al., "A technique for the quantitative evaluation of dose distributions",
% Med Phys. 1998 May; 25(5): 656-61.
%
% The following variables are required for proper execution:
% varargin{1}: structure containing the reference data, where the field
% start is an array containing the coordinates along each dimension
% of the first voxel, width is an array containing the width of each
% voxel along each dimension, and data is an n-dimensional array
% varargin{2}: structure containing the target data, where the field
% start is an array containing the coordinates along each dimension
% of the first voxel, width is an array containing the width of each
% voxel along each dimension, and data is an n-dimensional array
% varargin{3}: Gamma absolute criterion percentage
% varargin{4}: Gamma Distance To Agreement (DTA) criterion, in the same
% units as the reference and target width structure fields
% varargin{5:end} (optional): additional parameters preceded by option
% flags. The available options are 'local', 'refval', 'restrict',
% 'res', and 'limit'.
%
% The following options can be passed to this argument as name/value pairs:
% local: boolean, indicates whether to perform a local (1) or global (0)
% Gamma computation. If not present, the function will assume a
% global Gamma computation.
% refval: reference value for the global absolute criterion. Is used with
% the percentage from varargin{3} to compute absolute value. If not
% present, the maximum value in the reference data is used.
% restrict: restricted search flag. If 1 or not provided, only the gamma
% values along the X/Y/Z axes are computed during 3D comptation. If
% 0, the entire rectangular/volumetric search space is computed.
% res: DTA resolution factor. The number of distance steps equal the
% resolution factor multiplied by the limit. If not provided, the
% factor is 100 for 1D/2D and 20 for 3D calculations.
% limit: The DTA limit. This number determines how far the function will
% search in the distance axes when computing Gamma. This also
% therefore specifies the maximum "real" Gamma Index value.
% cpu: boolean, set to 1 to force CPU computation.
%
% The following variables are returned upon succesful completion:
% gamma: array of the same dimensions as varargin{2}.data containing the
% computed gamma values
%
% Below is an example of how the function is used:
%
% reference.start = [-10 -10]; % mm
% reference.width = [0.1 0.1]; % mm
% reference.data = rand(200);
%
% target.start = [-10 -10]; % mm
% target.width = [0.1 0.1]; % mm
% target.data = rand(200);
%
% percent = 3;
% dta = 0.5; % mm
% local = 0; % Perform global gamma
%
% gamma = CalcGamma(reference, target, percent, dta, 'local', local);
%
% Author: Mark Geurts, mark.w.geurts@gmail.com
% Copyright (C) 2014 University of Wisconsin Board of Regents
%
% This program is free software: you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by the
% Free Software Foundation, either version 3 of the License, or (at your
% option) any later version.
%
% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
% Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program. If not, see http://www.gnu.org/licenses/.
%% Validate Inputs
% Verify at least four input arguments are provided
if nargin < 4
% If not, throw an error and stop execution
if exist('Event', 'file') == 2
Event('Too few input argumenst passed to CalcGamma', 'ERROR');
else
error('Too few input argumenst passed to CalcGamma');
end
end
% Check if the reference structure contains width, start, and data fields,
% and if the size of the width and start vectors are equal
if ~isfield(varargin{1}, 'width') || ~isfield(varargin{1}, 'start') || ...
~isfield(varargin{1}, 'data') || ~isequal(length(varargin{1}.width), ...
length(varargin{1}.start))
% If not, throw an error and stop execution
if exist('Event', 'file') == 2
Event(['Incorrect reference data format. Must contain width, ', ...
'start, and data fields and be of equal dimensions'], 'ERROR');
else
error(['Incorrect reference data format. Must contain width, ', ...
'start, and data fields and be of equal dimensions']);
end
% Check if the target structure contains width, start, and data fields,
% and if the size of the width and start vectors are equal
elseif ~isfield(varargin{2}, 'width') || ~isfield(varargin{2}, 'start') || ...
~isfield(varargin{2}, 'data') || ~isequal(length(varargin{2}.width), ...
length(varargin{2}.start))
% If not, throw an error and stop execution
if exist('Event', 'file') == 2
Event(['Incorrect target data format. Must contain width, ', ...
'start, and data fields and be of equal dimensions'], 'ERROR');
else
error(['Incorrect target data format. Must contain width, ', ...
'start, and data fields and be of equal dimensions']);
end
% Check if the reference and target data arrays are the same number of
% dimensions. Calculating the gamma from a lower dimensional dataset to a
% higher dimensional reference is currently not supported
elseif ~isequal(size(size(varargin{1}.data)), size(size(varargin{2}.data)))
% If not, throw an error and stop execution
if exist('Event', 'file') == 2
Event(['The fixed and target data arrays must be of the same', ...
' dimensions'], 'ERROR');
else
error(['The fixed and target data arrays must be of the same', ...
' dimensions']);
end
end
% Log validation completed
if exist('Event', 'file') == 2
Event('Data validation completed, beginning Gamma computation');
tic;
end
%% Declare default options
% local indicates whether to perform a local (1) or global (0) Gamma
% computation. If not present, the function will assume a global Gamma
% computation.
local = 0;
% refval is the reference value for the global absolute criterion. Is used
% with the percentage from varargin{3} to compute absolute value. If not
% present, the maximum value in the reference data is used.
refval = max(max(max(varargin{1}.data)));
% restricted search flag. If 1, only the gamma values along the X/Y/Z axes
% are computed during 3D comptation. If 0, the entire rectangular/
% volumetric search space is computed.
restrict = 1;
% The resolution parameter determines the number of steps (relative to
% the distance to agreement) that each reference voxel will be
% interpolated to and gamma calculated. A value of 5 with a DTA of 3
% mm means that gamma will be calculated at intervals of 3/5 = 0.6 mm.
% Different resolutions can be set for different dimensions of data.
if size(varargin{2}.width,2) == 1
% Set 1-D resolution
res = 100;
elseif size(varargin{2}.width,2) == 2
% Set 2-D resolution
res = 50;
elseif size(varargin{2}.width,2) == 3
% Set 3-D resolution
res = 20;
end
% The search limit parameter determines how far the function will search in
% the distance axes when computing Gamma. This also therefore specifies
% the maximum believable Gamma Index value. Typically this value is 2.
% This should always be set to an integer.
limit = 2;
%% Search for provided options
% Load data structure from varargin
for i = 5:nargin
% If the local option is set
if strcmp(varargin{i}, 'local')
local = varargin{i+1};
% If the refval option is set
elseif strcmp(varargin{i}, 'refval')
refval = varargin{i+1};
% If the restrict option is set
elseif strcmp(varargin{i}, 'restrict')
restrict = varargin{i+1};
% If the res option is set
elseif strcmp(varargin{i}, 'res')
res = varargin{i+1};
% If the limit option is set
elseif strcmp(varargin{i}, 'limit')
limit = varargin{i+1};
% If the cpu option is set
elseif strcmp(varargin{i}, 'cpu')
cpu = varargin{i+1};
end
end
%% Log options
% If Event reporting is enabled
if exist('Event', 'file') == 2
% Log local
if local == 1
Event('Gamma calculation set to local');
else
Event('Gamma calculation assumed to global');
end
% Log refval
Event(sprintf('Reference value set to %g', refval));
% Log restrict
if restrict == 1
Event('Restricted search enabled');
else
Event('Restricted search disabled');
end
% Log res
Event(sprintf('Resolution set to %g', res));
% Log limit
Event(sprintf('DTA limit set to %g', limit));
end
%% Compute mesh grids
% If the reference dataset is 1-D
if length(varargin{1}.width) == 1
% Log event
if exist('Event', 'file') == 2
Event('Reference dataset is 1-D');
end
% Check if the data is in rows or columns (this is only needed for 1-D)
if size(varargin{1}.data,1) > size(varargin{1}.data,2)
% If in rows, transpose
varargin{1}.data = varargin{1}.data';
end
% Compute the reference X coordinates using the start and width values
refX = single(varargin{1}.start(1):varargin{1}.width(1):varargin{1}.start(1) ...
+ varargin{1}.width(1) * (size(varargin{1}.data,2) - 1));
% Otherwise, if the reference dataset is 2-D
elseif length(varargin{1}.width) == 2
% Log event
if exist('Event', 'file') == 2
Event('Reference dataset is 2-D');
end
% Compute X and Y meshgrids for the reference dataset positions using
% the start and width values
[refX, refY] = meshgrid(single(varargin{1}.start(2): ...
varargin{1}.width(2):varargin{1}.start(2) + varargin{1}.width(2) * ...
(size(varargin{1}.data,2) - 1)), single(varargin{1}.start(1): ...
varargin{1}.width(1):varargin{1}.start(1) + varargin{1}.width(1)...
* (size(varargin{1}.data,1) - 1)));
% Otherwise, if the reference dataset is 3-D
elseif length(varargin{1}.width) == 3
% Log event
if exist('Event', 'file') == 2
Event('Reference dataset is 3-D');
end
% Compute X, Y, and Z meshgrids for the reference dataset positions
% using the start and width values, permuting X/Y
[refX, refY, refZ] = meshgrid(single(varargin{1}.start(2): ...
varargin{1}.width(2):varargin{1}.start(2) + varargin{1}.width(2) * ...
(size(varargin{1}.data,2) - 1)), single(varargin{1}.start(1): ...
varargin{1}.width(1):varargin{1}.start(1) + varargin{1}.width(1)...
* (size(varargin{1}.data,1) - 1)), single(varargin{1}.start(3):...
varargin{1}.width(3):varargin{1}.start(3) + varargin{1}.width(3)...
* (size(varargin{1}.data,3) - 1)));
% Otherwise, if the reference data is of higher dimension
else
% Throw an error and stop execution
if exist('Event', 'file') == 2
Event('The fixed data structure contains too many dimensions', ...
'ERROR');
else
error('The fixed data structure contains too many dimensions');
end
end
% If the target dataset is 1-D
if length(varargin{2}.width) == 1
% Log event
if exist('Event', 'file') == 2
Event('Target dataset is 1-D');
end
% Check if the data is in rows or columns (this is only needed for 1-D)
if size(varargin{2}.data,1) > size(varargin{2}.data,2)
% If in rows, transpose
varargin{2}.data = varargin{2}.data';
end
% Compute the target X coordinates using the start and width values
tarX = single(varargin{2}.start(1):varargin{2}.width(1):varargin{2}.start(1) ...
+ varargin{2}.width(1) * (size(varargin{2}.data,2) - 1));
% Otherwise, if the target dataset is 2-D
elseif length(varargin{2}.width) == 2
% Log event
if exist('Event', 'file') == 2
Event('Target dataset is 2-D');
end
% Compute X and Y meshgrids for the target dataset positions using the
% start and width values
[tarX, tarY] = meshgrid(single(varargin{2}.start(2):...
varargin{2}.width(2):varargin{2}.start(2) + varargin{2}.width(2) * ...
(size(varargin{2}.data,2) - 1)), single(varargin{2}.start(1): ...
varargin{2}.width(1):varargin{2}.start(1) + varargin{2}.width(1) ...
* (size(varargin{2}.data,1) - 1)));
% Otherwise, if the target dataset is 3-D
elseif length(varargin{2}.width) == 3
% Log event
if exist('Event', 'file') == 2
Event('Target dataset is 3-D');
end
% Compute X, Y, and Z meshgrids for the target dataset positions using
% the start and width values, permuting X/Y
[tarX, tarY, tarZ] = meshgrid(single(varargin{2}.start(2):...
varargin{2}.width(2):varargin{2}.start(2) + varargin{2}.width(2) * ...
(size(varargin{2}.data,2) - 1)), single(varargin{2}.start(1): ...
varargin{2}.width(1):varargin{2}.start(1) + varargin{2}.width(1) ...
* (size(varargin{2}.data,1) - 1)), single(varargin{2}.start(3):...
varargin{2}.width(3):varargin{2}.start(3) + varargin{2}.width(3) ...
* (size(varargin{2}.data,3) - 1)));
end
%% Initialize variables
% Generate an initial gamma volume with values of 2^2 (this is the maximum
% reliable value of gamma, see description of limit above). Note that
% gamma-squared is stored during computation; sqrt is computed at the end.
gamma = ones(size(varargin{2}.data)) * (limit ^ 2);
% Log number of gamma calculations (for status updates on 3D calcs)
if restrict == 1
% Compute number of restricted search calcs
num = (res * (limit * 2) + 1) * length(varargin{2}.width);
else
% Compute total number of calcs
num = (res * (limit * 2) + 1) ^ length(varargin{2}.width);
end
% num is the number of iterations, num * numel the total number of
% interpolations being performed
if exist('Event', 'file') == 2
Event(sprintf('Number of gamma calculations = %g', num * numel(gamma)));
end
% Initialize counter (for progress indicator)
n = 0;
%% Begin computation
% Start try-catch block to safely test for CUDA functionality
try
% If the cpu option is set, throw an error to force CPU computation
if exist('cpu', 'var') == 1 && cpu == 1
error('Reverting to CPU computation');
end
% Clear and initialize GPU memory. If CUDA is not enabled, or if the
% Parallel Computing Toolbox is not installed, this will error, and the
% function will automatically rever to CPU computation via the catch
% statement
gpuDevice(1);
% Start a for loop to interpolate the dose array along the x-direction.
% Note to support parfor loops indices must be integers, so x varies
% from -2 to +2 multiplied by the number of interpolation steps.
% Effectively, this evaluates gamma from -2 * DTA to +2 * DTA.
for x = -limit*res:limit*res
% i is the x axis step value
i = x/res * varargin{4};
% Initialize j and k as zero (they will be updated if the data is
% of higher dimension)
j = 0;
k = 0;
% If the data contains a second dimension
if length(varargin{1}.width) > 1
% Start a for loop to interpolate the dose array along the
% y-direction. Note to support parfor loops indices must be
% integers, so y varies from -2 to +2 multiplied by the number
% of interpolation steps. Effectively, this evaluates gamma
% from -2 * DTA to +2 * DTA.
for y = -limit*res:limit*res
% j is the y axis step value
j = y/res * varargin{4};
% Initialize k as zero (it will be updated if the data is
% of higher dimension)
k = 0;
% If the data contains a third dimension
if length(varargin{1}.width) > 2
% Start a for loop to interpolate the dose array along
% the z-direction. Note to support parfor loops
% indices must be integers, so z varies from -2 to +2
% multiplied by the number of interpolation steps.
% Effectively, this evaluates gamma from -2 * DTA to
% +2 * DTA.
for z = -limit*res:limit*res
% k is the z axis step value
k = z/res * varargin{4};
% Check restricted search flag
if restrict == 0 || sum(abs([x y z]) > 0) == 1
% Run GPU interp3 function to compute the reference
% values at the specified target coordinate points
interp = gather(interp3(gpuArray(refX), gpuArray(refY), ...
gpuArray(refZ), gpuArray(single(varargin{1}.data)), ...
gpuArray(tarX + i), gpuArray(tarY + j), ...
gpuArray(tarZ + k), 'linear', 0));
% Update the gamma array by returning the minimum
% of the existing value or the new value
gamma = min(gamma, GammaEquation(interp, ...
varargin{2}.data, i, j, k, varargin{3}, varargin{4}, ...
refval, local));
% Update counter
n = n + 1;
% If counter is at an even %, display progress
if mod((n-1)/num, 0.01) > 0.005 && ...
mod(n/num, 0.01) < 0.005
fprintf('%0.1f%%\n', n/num*100);
end
end
end
% Otherwise, the data is 2-D
else
% Run GPU interp2 function to compute the reference
% values at the specified target coordinate points
interp = gather(interp2(gpuArray(refX), gpuArray(refY), ...
gpuArray(single(varargin{1}.data)), gpuArray(tarX + i), ...
gpuArray(tarY + j), 'linear', 0));
% Update the gamma array by returning the minimum
% of the existing value or the new value
gamma = min(gamma, GammaEquation(interp, varargin{2}.data, ...
i, j, k, varargin{3}, varargin{4}, ...
refval, local));
end
end
% Otherwise, the data is 1-D
else
% Run GPU interp function to compute the reference values at
% the specified target coordinate points
interp = gather(interp1(gpuArray(refX), ...
gpuArray(single(varargin{1}.data)), gpuArray(tarX + i), ...
'linear', 0));
% Update the gamma array by returning the minimum of the
% existing value or the new value
gamma = min(gamma, GammaEquation(interp, varargin{2}.data, ...
i, j, k, varargin{3}, varargin{4}, ...
refval, local));
end
end
% If GPU fails, revert to CPU computation
catch
% Log GPU failure (if cpu flag is not set)
if exist('Event', 'file') == 2 && exist('cpu', 'var') ~= 1
Event('GPU failed, reverting to CPU method', 'WARN');
end
% Start a for loop to interpolate the dose array along the x-direction.
% Note to support parfor loops indices must be integers, so x varies
% from -2 to +2 multiplied by the number of interpolation steps.
% Effectively, this evaluates gamma from -2 * DTA to +2 * DTA.
for x = -limit*res:limit*res
% i is the x axis step value
i = x/res * varargin{4};
% Initialize j and k as zero (they will be updated if the data is
% of higher dimension)
j = 0;
k = 0;
% If the data contains a second dimension
if length(varargin{1}.width) > 1
% Start a for loop to interpolate the dose array along the
% y-direction. Note to support parfor loops indices must be
% integers, so y varies from -2 to +2 multiplied by the number
% of interpolation steps. Effectively, this evaluates gamma
% from -2 * DTA to +2 * DTA.
for y = -limit*res:limit*res
% j is the y axis step value
j = y/res * varargin{4};
% Initialize k as zero (it will be updated if the data is
% of higher dimension)
k = 0;
% If the data contains a third dimension
if length(varargin{1}.width) > 2
% Start a for loop to interpolate the dose array along
% the z-direction. Note to support parfor loops
% indices must be integers, so z varies from -2 to +2
% multiplied by the number of interpolation steps.
% Effectively, this evaluates gamma from -2 * DTA to
% +2 * DTA.
for z = -limit*res:limit*res
% k is the z axis step value
k = z/res * varargin{4};
% Check restricted search flag
if restrict == 0 || sum(abs([x y z]) > 0) == 1
% Run CPU interp3 function to compute the reference
% values at the specified target coordinate points
interp = interp3(refX, refY, refZ, ...
single(varargin{1}.data), tarX + i, ...
tarY + j, tarZ + k, '*linear', 0);
% Update the gamma array by returning the minimum
% of the existing value or the new value
gamma = min(gamma, GammaEquation(interp, ...
varargin{2}.data, i, j, k, varargin{3}, ...
varargin{4}, refval, local));
% Update counter
n = n + 1;
% If counter is at an even %, display progress
if mod((n-1)/num, 0.01) > 0.005 && ...
mod(n/num, 0.01) < 0.005
fprintf('%0.1f%%\n', n/num*100);
end
end
end
% Otherwise, the data is 2-D
else
% Run CPU interp2 function to compute the reference
% values at the specified target coordinate points
interp = interp2(refX, refY, single(varargin{1}.data), ...
tarX + i, tarY + j, '*linear', 0);
% Update the gamma array by returning the minimum
% of the existing value or the new value
gamma = min(gamma, GammaEquation(interp, ...
varargin{2}.data, i, j, k, varargin{3}, ...
varargin{4}, refval, local));
end
end
% Otherwise, the data is 1-D
else
% Run CPU interp function to compute the reference values at
% the specified target coordinate points
interp = interp1(refX, single(varargin{1}.data), tarX + i, ...
'*linear', 0);
% Update the gamma array by returning the minimum of the
% existing value or the new value
gamma = min(gamma, GammaEquation(interp, varargin{2}.data, ...
i, j, k, varargin{3}, varargin{4}, ...
refval, local));
end
end
end
%% Finish up
% Take square root of result
gamma = sqrt(gamma);
% Log completion
if exist('Event', 'file') == 2
Event(sprintf(['Gamma calculation completed successfully in ', ...
'%0.3f seconds'], toc));
end
% Clear temporary variables
clear refX refY refZ tarX tarY tarZ interp;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function gamma = GammaEquation(ref, tar, i, j, k, perc, dta, refval, local)
% GammaEquation is the programmatic form of the Gamma definition as given
% by Low et al in matrix form. This function computes both local and
% global Gamma, and is a subfunction for CalcGamma. Note that this
% equation returns Gamma-squared instead of Gamma.
%
% The following inputs are used for computation and are required:
% ref: the reference 3D array. Must be the same size as tar
% tar: the target 3D array. Must be the same size as ref
% i: magnitude of x position offset of tar to ref, relative to dta
% j: magnitude of y position offset of tar to ref, relative to dta
% k: magnitude of z position offset of tar to ref, relative to dta
% perc: the percent Gamma criterion, given in % (i.e. 3 for 3%)
% dta: the distance to agreement Gamma criterion, unitless but relative
% to i, j, and k
% refval: if global, the reference value to base the % criterion from
% local: boolean, indicates whether to perform a local (1) or global (0)
% Gamma computation
%
% The following variables are returned:
% gamma: a 3D array of the same dimensions as ref and interp of the
% computed gamma-squared value for each voxel based on interp and
% i, j, k
% If local is set to 1, perform a local Gamma computation
if local == 1
% Gamma is defined as the sqrt((abs difference/relative tolerance)^2 +
% sum((voxel offset/dta)^2)). The sqrt is removed for computational
% efficiency.
gamma = ((tar - ref) ./ (ref * perc / 100)).^2 + ...
(i/dta)^2 + (j/dta)^2 + (k/dta)^2;
else
% Gamma is defined as the sqrt((abs difference/absolute tolerance)^2 +
% sum((voxel offset/dta)^2)). The sqrt is removed for computational
% efficiency.
gamma = ((tar - ref) ./ (refval * perc / 100)).^2 + ...
(i/dta)^2 + (j/dta)^2 + (k/dta)^2;
end