function Makemovie_3Dmeshes
%Program to create a movie of leaves in 3D. User is prompted to give the days to include in the movie. 
%For instance if the user gives [7,19], this will make a movie of characteristic leaves at days 7 and 19 
%(both times points will be included in the same movie). 
%
%This program concludes the 3D shape analysis part of the programs.
%Next programs deal with shape deformations
%Next program to run: Compute_deformation_patterns (to use 3rd order
%polynomial transformations), or Compute_deformation_patterns_lwm (to use
%local weighted means transformations).



%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);
%variable vals further down contains hard coded number (7) .

close all;

days=input('Give days to display in the movie, e.g. [7,19] to display days 7 and 19 only');
Pathname=uigetdir(cd,'Select main project folder in  which all shape results and figures are stored');
figsf=[Pathname '\Figures of characteristic leaves as 3D meshes'];
colines=[0 0 0];
colbkg=[1 1 1];
%figsf=uigetdir(pwd,['Select folder where figures are']);
cd(figsf);

resdir='Movie of characteristic leaves as 3D meshes';
if ~exist([Pathname '\' resdir],'dir')
    mkdir(Pathname,resdir);
end
mpd=[Pathname '\' resdir];

%mpd=uigetdir(pwd,['Select folder to save movie to']);
%fnameout=input('Movie file name (without extension)','s');
fnameout=['characteristicleaves_days'  num2str(days) '_in3D'];
fna=[mpd '\' fnameout];
nbf=20;
aviobj=avifile([fna '.avi'],'FPS',nbf,'Compression','none');
%time=init;



for i=1:length(days)
    uiopen([ '\FigureDAS' num2str(days(i)) '.fig'],1);
%set(gcf,'WindowStyle','normal');
%vals=[-500 500 -500 500 -120 120];
%vals=[-3000 3000 -3000 3000 -200 200];

gcf
colorbar off
axis equal
hold on,
h=line([0 0],[-500 500],[0 0]);
set(h,'Color',colines,'LineWidth',[3]);
time=days(i);
vals=valstot(time,:);

hh=text(0,0,0,['DAS' num2str(time)]);
set(hh,'Color',[0 0 0]);
axis vis3d
%vals=input('give matrix for axes values [xmin xmax ymin ymax zmin zmax]');
set(gca,'Color',colbkg);
set(gcf,'Color',colbkg);

view(-39,30)
%axis([-25 25 -20 20 -25 1])
vals2=[0 -30 -30 -30 ;90 90 60 30]';
for cc=1:4
    view(0,90);
    axis([vals]);
    
    %camorbit(5,0,'camera')
    
    drawnow
    %set(gcf,'Position',[360 500 420 315])
    %set(gcf,'Position',[1281 1 1280 950])
    set(gcf,'Position',[1553 203 744 549]);
    axis off;
    % set(gcf, 'Renderer', 'zbuffer');
    frame=gcf;
    
    aviobj=addframe(aviobj,frame);
    
    
end

for cc=1:72
    %view(cc,30);
    axis([vals]);
    camorbit(0,5,'camera')
    drawnow
    %set(gcf,'Position',[360 500 420 315])
    %set(gcf,'Position',[1281 1 1280 950])
    set(gcf,'Position',[1553 203 744 549]);
    axis off;
    % set(gcf, 'Renderer', 'zbuffer');
    frame=gcf;
    aviobj=addframe(aviobj,frame);
    
end

for cc=1:72
    %view(cc,30);
    axis([vals]);
    camorbit(5,0,'camera')
    drawnow
    %set(gcf,'Position',[360 500 420 315])
    %set(gcf,'Position',[1281 1 1280 950])
    set(gcf,'Position',[1553 203 744 549]);
    axis off;
    % set(gcf, 'Renderer', 'zbuffer');
    frame=gcf;
    aviobj=addframe(aviobj,frame);
    
end

for cc=1:4
    view(0,90);
    axis([vals]);
    %camorbit(5,0,'camera')
    drawnow
    %set(gcf,'Position',[360 500 420 315])
    %set(gcf,'Position',[1281 1 1280 950])
    set(gcf,'Position',[1553 203 744 549]);
    axis off;
    % set(gcf, 'Renderer', 'zbuffer');
    frame=gcf;
    aviobj=addframe(aviobj,frame);
    
end
%close all
%keyboard
%time=init+1;
end
aviobj=close(aviobj);
close all;
disp(['Movie has been saved under ' fna '.avi']);