% Generate many random parameters that will collectively cover the
% parameters space, and hence cover the model manifold
clear all; close all;
K=500000;
para = 10.^(rand(2,K)*6-4);
% define the colors
cmap_tmp = colormap('jet');
color_code_data = sum(log10(para)); 
prc95 = 3.5;
prc05 = -2.5;
color_code_data = (color_code_data - prc05)/(prc95-prc05);
color_code_data(color_code_data<0.01)=0.01;
color_code_data(color_code_data>0.99)=0.99;
color_code_data = round(color_code_data*100)/100;  
node_color = interp1(((1:size(cmap_tmp,1))'-1)/(size(cmap_tmp,1)-1),cmap_tmp,color_code_data);
% plot out the model manifold in the initial 2D data space 
h = figure(1); set(h,'position',[100,100,800,400])
t_obs=[1 3]';
y_obs = toy_system_evaluate(para,t_obs);
for i=1:100
    ind = find(round(color_code_data*100)==i);
    if isempty(ind), continue; end
    if i==1, hold off; end
    figure(1); subplot(1,2,1); plot(y_obs(1,ind),y_obs(2,ind),'.','markersize',15,'color',node_color(ind(1),:))
    hold on;
end
% plot out the model manifold in the 3D data space after the first iteration of experimental design
t_obs=[1 3 0.0007]';
y_obs = toy_system_evaluate(para,t_obs);
for i=1:100
    ind = find(round(color_code_data*100)==i);
    if isempty(ind), continue; end
    if i==1, hold off; end
    figure(1); subplot(1,2,2); plot3(y_obs(1,ind),y_obs(2,ind),y_obs(3,ind),'.','markersize',15,'color',node_color(ind(1),:))
    hold on
end
figure(1); subplot(1,2,1); xlabel('obs_1'); ylabel('obs_2'); axis([-0.5,2.5,-0.5,2.5])
figure(1); h = subplot(1,2,2); set(h,'CameraPosition',[ -5,-11, 3]); xlabel('obs_1'); ylabel('obs_2'); zlabel('obs_3')
drawnow 
snapnow

possible_CameraPosition = [ -5        -11        3; 
                            -3.9596   -8.8048    3;
                            -3.5003   -8.3236    3;
                            -2.6892   -6.9239    3;
                            -2.3311   -6.1548    3;
                            -1.5591   -4.5641    3;
                            -0.6241   -2.4464    3;
                             0.0774   -0.8453    3;
                             0.3644   -0.2713    3;
                             0.9405    0.8809    3.6320;
                          ];
%%
possible_CameraPosition = [interp1([0;1],[-5,   -11, 3; 0.3, -0.3, 3],0:0.05:1); 
                           interp1([0;1],[0.3, -0.3, 3; 0.9835   -0.6632    3.6240],0:0.05:1);
                           interp1([0;1],[0.9835   -0.6632    3.6240;  0.8292  -16.2219    2.0586],0:0.05:1);
                           interp1([0;1],[0.8292   -16.2219   2.0586; -5.4910  -14.7214    2.1590],0:0.05:1);
                           interp1([0;1],[-5.4910  -14.7214   2.1590; -5,   -11, 3],0:0.1:1);
                           ]
loops=65535; %forever
delay=0.05; %second
for k=1:size(possible_CameraPosition,1)
    figure(1); h = subplot(1,2,2); set(h,'xtick',[0:2], 'ytick',[0:2], 'ztick',[1.9:0.1:2], 'CameraPosition',possible_CameraPosition(k,1:3));%set(h,'CameraPosition',possible_CameraPosition(k,1:3), 'CameraViewAngle',possible_CameraPosition(k,4));
    zlim([1.85, 2])
    h = figure(1); 
    c = getframe(h);
    a = c.cdata;
    [M  c_map]= rgb2ind(a,256);
    if k==1
        imwrite(M,c_map,['manifold_shape_change.gif'],'gif','LoopCount',Inf,'DelayTime',delay)
    else
        imwrite(M,c_map,['manifold_shape_change.gif'],'gif','WriteMode','append','DelayTime',delay)
    end
end