function Display_deformations_forgridmovie3D
%Modified version of Display_deformations_and_growth_daytonext3D to save figures of 3D grid deformations in format good to
%make a movie (those figures are used by the next program to make a movie).
%
%Next program to run:Makemovie_3Dgriddeformations
%
days=7:19;

close all

%the values below are good values for axes for optimal display based on our
%dataset
valstot1=zeros(6,6);
valstot2=[-600 600 -600 600 -600 600; ...
    -900 900 -900 900 -600 600; ...
    -1300 1300 -1300 1300 -1300 1300; ...
    -1600 1600 -1600 1600 -600 600; ...
    -2000 2000 -2000 2000 -600 600; ...
    -2300 2300 -2300 2300 -600 600; ...
    -2600 2600 -2600 2600 -600 600; ...
    -2800 2800 -2800 2800 -600 600; ...
    -2800 2800 -2800 2800 -600 600; ...
    -2800 2800 -2800 2800 -600 600; ...
    -2800 2800 -2800 2800 -600 600; ...
    -2800 2800 -2800 2800 -600 600; ...
    -2800 2800 -2800 2800 -600 600];
valstot=cat(1,valstot1,valstot2);

%note: caxis used with hard coded values further down to set the best colour
%scale for our dataset

maingrid=5;
mainthick=1;%thickness of main grid lines
secondthick=0.1;
secondcol=[0.8 0.8 0.8];
scalegrid=0;


Pathname=uigetdir(cd,'Select main project folder in  which all shape results and figures are stored');
trsfo=input('If you used Compute_deformation_patterns Press 1, if you used Compute_deformation_patterns_lwm press 2');
if trsfo==1
    load([Pathname '\Grid deformations in 2D\Meangrid_day_by_day_deform07-19.mat']); % we load this because predef and postdef mean outlines are stored there
    addname2='pol3';
else
    load([Pathname '\Grid deformations in 2D\Meangrid_day_by_day_deform_lwm07-19.mat']); % we load this because predef and postdef mean outlines are stored there
    addname2='lwm';
end
%load([Pathname '\Grid deformations in 2D\Meangrid_day_by_day_deform07-19.mat']);
load([Pathname '\3D surfaces for 3D deformations\Mean3Dsurfaces_for_3D_deformations.mat']);


figdir='3D figures of day to day grid deformations - for 3D movie';
if ~exist([Pathname '\' figdir addname2],'dir')
    mkdir(Pathname,[figdir addname2]);
end
Pathname=[Pathname '\' figdir addname2];

cd(Pathname)


fignum=0;

for t=days(1):days(end-1)
    figure; hold on
    set(gcf, 'color', 'white');
    axis equal;
    
    if scalegrid==1
        rangex=max(max(avxpredef{t}))-min(min(avxpredef{t}));
        scfac=1000/rangex;
    else scfac=1;
    end
    

    plot3([Mode_outlineZ{t,1}(:,1);Mode_outlineZ{t,1}(1,1)],[Mode_outlineZ{t,1}(:,2);Mode_outlineZ{t,1}(1,2)],[Mode_outlineZ{t,1}(:,3);Mode_outlineZ{t,1}(1,3)],'color',[0 0 0],'LineWidth',1)

    %title(['DAS' num2str(t,'%02.0f') 'before deformation']);
    axis(valstot(t+1,:));
    if t<=8
        caxis([0 300]);
    elseif t<=10
        caxis([0 180]);
    elseif t<=12
        caxis([0 80]);
    elseif t<=14
        caxis([0 40]);
    else
        caxis([0 20]);
    end
    set(gcf, 'color', 'white');

    
    nrows=prod(size(leaf_XIg{t,1}));
    leafX=reshape(leaf_XIg{t,1},nrows,1);
    leafY=reshape(leaf_YIg{t,1},nrows,1);
    leafZ=reshape(Mode_leaf_ZIg{t,1},nrows,1);
    pts=[[leafX;Mode_outlineZ{t,1}(:,1)],[leafY; Mode_outlineZ{t,1}(:,2)],[leafZ;Mode_outlineZ{t,1}(:,3)]];
    pts=unique(pts,'rows');
    indx=find(~isnan(pts(:,3)));
    pts=pts(indx,:);
    F=scatteredInterpolant(pts(:,1),pts(:,2),pts(:,3),'linear','none');
    gZ{t}=F(gX{t},gY{t});
    
    
    nrows=prod(size(leaf_XIg{t+1,2}));
    leafX=reshape(leaf_XIg{t+1,2},nrows,1);
    leafY=reshape(leaf_YIg{t+1,2},nrows,1);
    leafZ=reshape(Mode_leaf_ZIg{t+1,2},nrows,1);
    
    pts=[[leafX;Mode_outlineZ{t+1,2}(:,1)],[leafY; Mode_outlineZ{t+1,2}(:,2)],[leafZ;Mode_outlineZ{t+1,2}(:,3)]];
    pts=unique(pts,'rows');
    indx=find(~isnan(pts(:,3)));
    pts=pts(indx,:);
    F=scatteredInterpolant(pts(:,1),pts(:,2),pts(:,3),'linear','none');
    F2=scatteredInterpolant(pts(:,1),pts(:,2),pts(:,3),'linear','linear');
    
    
    zm{t+1}=F(xm{t+1},ym{t+1});
    [gridinterpX,gridinterpY,gridinterpZ]= growth_fromgrid_ag3D([avxpredef{t} avypredef{t}],gX{t},gY{t},gZ{t},xm{t+1},ym{t+1},zm{t+1},0);

    for k=1:size(gX{t},1) %drawing lines instead of mesh b/c mesh won't save to eps files
        if rem(k-1,maingrid)==0
            plot3(gX{t}(k,:)*scfac,gY{t}(k,:)*scfac,gZ{t}(k,:)*scfac,'k','LineWidth',mainthick);
        else
            hh=plot3(gX{t}(k,:)*scfac,gY{t}(k,:)*scfac,gZ{t}(k,:)*scfac,'LineWidth',secondthick);
            set(hh,'Color',secondcol);
        end
    end
    for j=1:size(gX{t},2)
        
        if rem(j-1,maingrid)==0
            plot3(gX{t}(:,j)*scfac,gY{t}(:,j)*scfac,gZ{t}(:,j)*scfac,'k','LineWidth',mainthick);
        else
            hh=plot3(gX{t}(:,j)*scfac,gY{t}(:,j)*scfac,gZ{t}(:,j)*scfac,'k','LineWidth',secondthick);
            set(hh,'Color',secondcol);
        end
    end
 
    
    
    fignum=fignum+1;
    axis off;
    saveas(gcf,['Figure' num2str(fignum)],'fig');
    cla     
    
    hold on
    plot3([Mode_outlineZ{t+1,2}(:,1);Mode_outlineZ{t+1,2}(1,1)],[Mode_outlineZ{t+1,2}(:,2);Mode_outlineZ{t+1,2}(1,2)],[Mode_outlineZ{t+1,2}(:,3);Mode_outlineZ{t+1,2}(1,3)],'color',[0 0 0],'LineWidth',1)
    olzw{t+1}=F2(olxw{t+1},olyw{t+1});
    plot3([olxw{t+1};olxw{t+1}(1)]*scfac,[olyw{t+1};olyw{t+1}(1)]*scfac,[olzw{t+1};olzw{t+1}(1)]*scfac,'color', [0 191/255 1],'LineWidth',mainthick);
    
    
    
    for k=1:size(xm{t+1},1) %drawing lines instead of mesh b/c mesh won't save to eps files
        if rem(k-1,maingrid)==0
            h=plot3(xm{t+1}(k,:),ym{t+1}(k,:),zm{t+1}(k,:),'k','LineWidth',mainthick);
        else
            hh=plot3(xm{t+1}(k,:),ym{t+1}(k,:),zm{t+1}(k,:),'LineWidth',secondthick);
            set(hh,'Color',secondcol);
        end
        
    end
    for j=1:size(xm{t+1},2)
        if rem(j-1,maingrid)==0
            h=plot3(xm{t+1}(:,j),ym{t+1}(:,j),zm{t+1}(:,j),'k','LineWidth',mainthick);
        else
            hh=plot3(xm{t+1}(:,j),ym{t+1}(:,j),zm{t+1}(:,j),'LineWidth',secondthick);
            set(hh,'Color',secondcol);
        end
        
        
    end
    
    axis equal
    
    %title(['DAS' num2str(t+1,'%02.0f') 'deformed from DAS' num2str(t,'%02.0f')]);
    axis(valstot(t+1,:));
    if t<=8
        caxis([0 300]);
    elseif t<=10
        caxis([0 180]);
    elseif t<=12
        caxis([0 80]);
    elseif t<=14
        caxis([0 40]);
    else
        caxis([0 20]);
    end
    
    set(gcf, 'color', 'white');
    axis equal;
    
    
    fignum=fignum+1;
    axis off;
    
    saveas(gcf,['Figure' num2str(fignum)],'fig');
    close gcf;
    
end

disp(['Figures of day to day grid deformations in 3D for making a movie are saved in ' Pathname]);
