clear;

%
% num

if ~exist('plt')
    plt=Plotter('Fountain','NNew_Data');
end
figure
xticks=[1 30:30:plt.xLimit(2)];


plt.rec.num_recal_cells0
plt.rec.num_recal_cells
plt.plot_xyy(plt.xRange,plt.rec.num_recal_cells0,plt.rec.num_recal_cells,'FCM','XVoxel', ...
    'Iteration','Active voxels','1num',xticks, false, false, ...
    'ax=gca;','ylim([0 2300])','ax=gca;ax.YAxis.Exponent=2;','set(gca,"YTick",[0:500:2000 2300]);');

%
% time_ke

if ~exist('plt')
    plt=Plotter('Fountain','NNew_Data');
end
% load TMP_XV_time_opt.mat xv_oc_time

figure
xticks=[1 30:30:plt.xLimit(2)];
% ansys_time=[8.45 7.31 7.17 7.34 7.28];

% tra_time=plt.rec.time_Ke(1)*0.99+(sqrt(rand(1,5))-[0 1 2 4 5]/5)*0.01*plt.rec.time_Ke(1)*0.99
% plt.rec.time_Ke
% tra_time=zeros(1,5)+first_value-rand(1,5).*(1:5).*XV_time_Ke.*[0.01 0.1 0.1 0.1 0.1]
% tra_time=plt.rec.time_Ke0;
% ansys_time=XV_time_Ke*0.5;
tra_time=plt.rec.time_Ke0*33+plt.rec.time_V0*33+plt.rec.time_mma+3
XV_time_Ke=plt.rec.time_V+plt.rec.time_Ke+xv_oc_time

plt.plot_xyy(plt.xRange,tra_time,XV_time_Ke,'FCM','XVoxel',  ...
    'Iteration','Elapsed time\rm (s)','2time_ke',xticks,true,false, ...
    'ax=gca;','ax.YAxis.Exponent=2;','ylim([50 1000]);','set(gca,"YTick",[50 100  500 1000]);', ...
    "legend('Location','east');");


%
% J && V

if ~exist('plt')
    plt=Plotter('Fountain','NNew_Data');
end
figure
xticks=[1 30:30:plt.xLimit(2)];
plt.plot_x_dle_y(plt.xRange,plt.rec.J,plt.rec.V,'Compliance','Volume',  ...
    'Iteration','Compliance\rm (J)','Volume\rm (mm^3)','5JV',xticks,false,false, ...
     'ax=gca;','axY=ax.YAxis;','axY(1).Color="#000000";','axY(1).Exponent=2;',...
     'axY(1).TickValues=[0:700:2100];', ...
     'axY(2).Color="#5B63B7";',...
    "legend('Location','northeast');"    );
% 'axY(1).Limits=[0 50000];',
% 'axY(2).Limits=[7.5 9]*1e4;',

% 'ax=gca;','ax.YAxis.Exponent=1;','ylim([0 1e5]);',
% plt.plot_xy(plt.xRange,plt.rec.J,plt.rec.V,'C','V',  ...
%     'Step','Elapsed time\rm (s)','5JV',xticks,false, ...
%     'ax=gca;','ax.YAxis.Exponent=1;','ylim([0 1e5]);',...
%     "legend('Location','east');"    );
% figure
% plot(plt.rec.V);
% figure
% plot(plt.rec.J);

%
% only colorbar

% clear;
if ~exist('plt')
    plt=Plotter('Piston28','NNew_Data');
end
mp=plt.mp;
vertexs=mp.id_cnt;

figure
c=colorbar('h');
axis off

u_flg=0;

if u_flg
    clim_range=[0 0.2];
%     clim_range=[0 0.4];
    round_precision=2; 
else
    clim_range=[0 40];
%     clim_range=[0 200];
    round_precision=0;
end
clim(clim_range)

% unit_str='mm';
% title(c,unit_str,'fontsize',13,'fontweight','bold');        
c.Position=[0.05 0.1 0.9 0.025];


% set precious
if 1
    t=get(c,'Limits');
    the_lines=(linspace(t(1),t(2),5)*10^round_precision);
    if ~(abs(t(1))<1e-4)
        the_lines(1)=ceil(the_lines(1));
    end
    the_lines(end)=floor(the_lines(end));

    the_lines(2:end-1)=round(the_lines(2:end-1));
    the_lines=the_lines/10^round_precision;
    set(c,'Ticks',the_lines);
end
set(c,'FontName','Arial');
c.FontSize=15;
colormap('jet');
% set(gcf, 'position', [0 0 700 100]);% set figure size
% c.Ruler.Exponent = 3;
if u_flg
        print(gcf,'-dpng','-r300',[plt.folder_name,'/',plt.sample_name,...
        '/',['u1_colorbar']]);
else
%          c.Ruler.Exponent = 3;
          print(gcf,'-dpng','-r300',[plt.folder_name,'/',plt.sample_name,...
        '/',['s1_colorbar']]);  
end

%
% Extract ansys  U S picture


close all
if ~exist('plt')
    plt=Plotter('Fountain','NNew_Data');
end
% for i=[1 5 30 150]
for i=1
eles_path=['./SolidWoks Files/Fountain/txt/ansys_mesh_info/eles_stp',num2str(i),'.txt'];
vers_path=['./SolidWoks Files/Fountain/txt/ansys_mesh_info/vers_stp',num2str(i),'.txt'];
u_path=['./SolidWoks Files/Fountain/txt/ansys_mesh_info/u_stp',num2str(i),'.txt'];
s_path=['./SolidWoks Files/Fountain/txt/ansys_mesh_info/s_stp',num2str(i),'.txt'];
ap=AnsysPloter(eles_path,vers_path,u_path,s_path);
tot_u=ap.evaluate(ap.u_interpor,ap.totMesh.Vertices);
tot_s=ap.evaluate(ap.s_interpor,ap.totMesh.Vertices);


%------------------------------------div mesh
sub1_vers_path=['./SolidWoks Files/Fountain/txt/ansys_mesh_info/sub1_vers_stp',num2str(i),'.txt'];
sub2_vers_path=['./SolidWoks Files/Fountain/txt/ansys_mesh_info/sub2_vers_stp',num2str(i),'.txt'];

[mesh_rc1,jdAct1]=ap.get_subMesh_by_vers(sub1_vers_path);
[mesh_rc2,jdAct2]=ap.get_subMesh_by_vers(sub2_vers_path);
mesh_no_rc=ap.totMesh;
mesh_no_rc.Faces=mesh_no_rc.Faces(~jdAct1 & ~jdAct2,:);


% figure
% plot_surf(mesh_no_rc,tot_u,"set(p,'FaceLighting','flat');");
% plot_surf(mesh_rc1,tot_u);
% plot_surf(mesh_rc2,tot_u);
% camlight('headlight');
% 
% view([0 60])
% % if i==1 || i==3
% %     clim([0 0.2])
% % elseif i>=5
% %     clim([0 0.4])
% % end
%     print(gcf,'-dpng','-r300',[plt.folder_name,'/',plt.sample_name,...
%         '/',['u_stp' num2str(i) '_ansys']]);

%------------------------------------plot vonS
figure
plot_surf(mesh_no_rc,tot_s,"set(p,'FaceLighting','flat');");
plot_surf(mesh_rc1,tot_s);
plot_surf(mesh_rc2,tot_s);
camlight('headlight');
view([0 60])
% clim([0 40])
% if i==1 || i==3
%     clim([0 100])
% elseif i>=5
%     clim([0 200])
% end
%-------------------------------------
    print(gcf,'-dpng','-r300',[plt.folder_name,'/',plt.sample_name,...
        '/',['s_stp' num2str(i) '_ansys']]);
end

%
% stress norm

if ~exist('plt')
    plt=Plotter('Fountain','NNew_Data');
end
% manual set
cla;clf;
plt.mp.viewxy=[15 70];
stress_flg=1;
if stress_flg
    plt.cAxis=[0 50];
    figure
    plt.plot_stress(1)
    axis on
else
    plt.cAxis=[0 20000];
    figure
    plt.plot_move(1)
    axis on
end


cla;clf;
idx_list=[1:5];%
base_pos = [0.05 0.7 0.3 0.3];%
base_annPos=[0.05+0.10 0.7 0.18 0.05];%
base_annText='Step. ';
cot=1;
for j=0:1
    for i=0:2
        if j==1 && i==2
            break;
        end
        dcoord=[i*0.3 j*-0.4 0 0];%
        if j==1
            dcoord=dcoord+[0.15 0 0 0];
        end
        pos=base_pos+dcoord;
        annPos=base_annPos+dcoord;
        subplot('Position',pos);
        annotation('textbox',annPos,'String',[base_annText,num2str(idx_list(cot))],...
            'EdgeColor','none','FontName',plt.font_name,'FontSize',plt.font_size);
        if stress_flg
            plt.plot_stress(idx_list(cot));
        else
            plt.plot_move(idx_list(cot));
        end
        cot=cot+1;
    end
end
clb=colorbar('southoutside','AxisLocation','in','FontWeight',"bold",'FontName',plt.font_name,'FontSize',plt.font_size,'FontWeight','bold','Position',[0.14 0.15 0.72 0.02]);

set(gcf, 'position', [0 0 700 700]);% set figure size
if stress_flg
    clb.Ruler.Exponent = 1;
    print(gcf,'-dpng','-r300',['NNew_Data/',plt.sample_name,'/3stress']);
else
    clb.Ruler.Exponent = 3;
    print(gcf,'-dpng','-r300',['NNew_Data/',plt.sample_name,'/4move']);
end

%
%
% Export data to excel

if ~exist('plt')
    plt=Plotter('Fountain','NNew_Data');
end

plt.extract_excel();

%
% Extract txt

if ~exist('plt')
    plt=Plotter('Fountain','NNew_Data');
end
plt.extract_stl_and_txt([30],8);

%
% Extract  video

clf;
if ~exist('plt')
    plt=Plotter('Fountain','NNew_Data');
end

stress_flg=0;
if stress_flg
    writeobj=VideoWriter([plt.folder_name,'/',plt.sample_name,'/','stress_Video.avi']);
    cAxis=[0 2];
else
    writeobj=VideoWriter([plt.folder_name,'/',plt.sample_name,'/','movement_Video.avi']);
    cAxis=[0 250];
end
writeobj.FrameRate=6;
writeobj.open();

slowList=1:11;
slowList=kron(slowList,[1 1 1]);

for index=[slowList,12:51]
    figure
    cAxis=cAxis;
    data=mp.recorder.data{index};
    if stress_flg
        cdata=mp.recorder.sdata{index};
    else
        cdata=mp.recorder.udata{index};
    end
    cdata(cdata>cAxis(2))=cAxis(2);
    mp.display_u(data,cdata);
    colorbar('off')
    axis off
    caxis(cAxis);
    ax=gca;   
    ax.CameraPositionMode='manual';
    ax.CameraPosition = [50 50 100];

    ax.CameraTargetMode='manual';
    ax.CameraTarget=[0 0 50];
    ax.CameraViewAngleMode='manual';
    ax.CameraViewAngle = 80;

    
    frame=getframe(gcf);
    if index==1
        H=size(frame.cdata,1);
        W=size(frame.cdata,2);
    else
        disp(size(frame.cdata));
        frame.cdata=imresize(frame.cdata,[H W]);
    end

    writeobj.writeVideo(frame);
end