Skip to content
Snippets Groups Projects
Commit e1730b3e authored by Nicola Vigano's avatar Nicola Vigano
Browse files

Gradient/divergence: Reduced their performance impact

parent 1fbcb871
No related branches found
No related tags found
No related merge requests found
...@@ -219,9 +219,9 @@ function output = projection(algo_id, output_id, input_id, input) ...@@ -219,9 +219,9 @@ function output = projection(algo_id, output_id, input_id, input)
end end
function d = gradient(vol) function d = gradient(vol)
dx = cat(1, vol(2:end, :, :) - vol(1:end-1, :, :), -vol(end, :, :)); dx = diff(padarray(vol, [1 0 0], 'post'), 1, 1);
dy = cat(2, vol(:, 2:end, :) - vol(:, 1:end-1, :), -vol(:, end, :)); dy = diff(padarray(vol, [0 1 0], 'post'), 1, 2);
dz = cat(3, vol(:, :, 2:end) - vol(:, :, 1:end-1), -vol(:, :, end)); dz = diff(padarray(vol, [0 0 1], 'post'), 1, 3);
d = cat(4, dx, dy, dz); d = cat(4, dx, dy, dz);
end end
...@@ -229,22 +229,8 @@ function vol = divergence(d) ...@@ -229,22 +229,8 @@ function vol = divergence(d)
dx = d(:, :, :, 1); dx = d(:, :, :, 1);
dy = d(:, :, :, 2); dy = d(:, :, :, 2);
dz = d(:, :, :, 3); dz = d(:, :, :, 3);
ddx = cat(1, dx(1, :, :), dx(2:end, :, :) - dx(1:end-1, :, :)); ddx = diff(padarray(dx, [1 0 0], 'pre'), 1, 1);
ddy = cat(2, dy(:, 1, :), dy(:, 2:end, :) - dy(:, 1:end-1, :)); ddy = diff(padarray(dy, [0 1 0], 'pre'), 1, 2);
ddz = cat(3, dz(:, :, 1), dz(:, :, 2:end) - dz(:, :, 1:end-1)); ddz = diff(padarray(dz, [0 0 1], 'pre'), 1, 3);
vol = ddx + ddy + ddz; vol = ddx + ddy + ddz;
end end
% function d = gradient2D(vol)
% dx = cat(1, vol(1:end-1, :, :) - vol(2:end, :, :), vol(end, :, :));
% dy = cat(2, vol(:, 1:end-1, :) - vol(:, 2:end, :), vol(:, end, :));
% d = cat(4, dx, dy, dz);
% end
%
% function vol = divergence2D(d)
% dx = d(:, :, :, 1);
% dy = d(:, :, :, 2);
% ddx = cat(1, -dx(1, :, :), dx(1:end-1, :, :) - dx(2:end, :, :));
% ddy = cat(2, -dy(:, 1, :), dy(:, 1:end-1, :) - dy(:, 2:end, :));
% vol = ddx + ddy;
% end
function dd = gtMathsDivergence(d) function d = gtMathsDivergence(x)
% FUNCTION d = gtMathsDivergence(d) % FUNCTION d = gtMathsDivergence(x)
% Computes the divergence of a 1D-2D-3D object. % Computes the divergence of an n-dimensional vector object.
if (numel(d) >= 3) num_dims = numel(x);
dz = d{3}; d = cell(num_dims, 1);
dd{3} = cat(3, dz(:, :, 1), dz(:, :, 2:end) - dz(:, :, 1:end-1)); for ii_d = 1:num_dims
end padsize = zeros(1, num_dims);
if (numel(d) >= 2) padsize(ii_d) = 1;
dy = d{2};
dd{2} = cat(2, dy(:, 1, :), dy(:, 2:end, :) - dy(:, 1:end-1, :)); d{ii_d} = diff(padarray(x{ii_d}, padsize, 'pre'), 1, ii_d);
end end
dx = d{1};
dd{1} = cat(1, dx(1, :, :), dx(2:end, :, :) - dx(1:end-1, :, :));
dd = gtMathsSumCellVolumes(dd); d = gtMathsSumCellVolumes(d);
end end
\ No newline at end of file
function d = gtMathsGradient(x) function d = gtMathsGradient(x)
% FUNCTION d = gtMathsGradient(vol) % FUNCTION d = gtMathsGradient(x)
% Computes the gradient of a 1D-2D-3D object and outputs a cell % Computes the gradient of an n-dimensional object and outputs a cell
% concatenation of the directional gradients. % concatenation of the directional gradients.
if (size(x, 3) > 1) dims = size(x);
d{3} = cat(3, x(:, :, 2:end) - x(:, :, 1:end-1), -x(:, :, end)); num_dims = numel(dims);
end d = cell(num_dims, 1);
if (size(x, 2) > 1) for ii_d = 1:num_dims
d{2} = cat(2, x(:, 2:end, :) - x(:, 1:end-1, :), -x(:, end, :)); padsize = zeros(1, num_dims);
padsize(ii_d) = 1;
d{ii_d} = diff(padarray(x, padsize, 'post'), 1, ii_d);
end end
d{1} = cat(1, x(2:end, :, :) - x(1:end-1, :, :), -x(end, :, :));
end end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment