function gridDisplacements = shapeMap(xinit,e,IDD,IDD1,IDD2,Ishape,shapeDisplacements)

% gridDisplacements = shapeMap(xinit,e,IDD,IDD1,IDD2,Ishape,
%                              shapeDisplacements)
%            calculates the grid displacement based on a solution
%            to the 2d linear elasticity problem with inhomogeneous
%            Dirichlet b.c. that corresponds to shape displacement.

%sprintf('%s','shapeMap')

global K b u_el shapeDisplacements_last shapeSolved;

if max(size(shapeDisplacements_last)~=size(shapeDisplacements))==1
    shapeSolved = [];
elseif max(max(shapeDisplacements_last~=shapeDisplacements))==1
    shapeSolved = [];
end

if ~in(shapeSolved,'b')
    [K,b] = assemble_el(xinit,e,IDD,IDD1,IDD2,Ishape,shapeDisplacements);
    shapeSolved = [shapeSolved 'b'];
end

if ~in(shapeSolved,'u')
    u_el = K\b;
    shapeSolved = [shapeSolved 'u'];
end

n = max(size(u_el))/2;

gridDisplacements = zeros(2,n);

for i=1:n
    gridDisplacements(1,i) = u_el(2*i-1);
    gridDisplacements(2,i) = u_el(2*i);
end

for i=1:max(size(Ishape))
    gridDisplacements(:,Ishape(i)) = shapeDisplacements(:,i);
end

shapeDisplacements_last = shapeDisplacements;
