clear;

%

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

pick_iters=[1,10,30,100];
FCM_SIM=plt.rec.num_recal_cells0(pick_iters);
FCM_XV_SIM=plt.rec.num_recal_cells(pick_iters);
FCM_SEN=[72192 69568 69312 69504];
FCM_XV_SEN=[880 1624 1254 1351];
% -------------------------------timing
% tra_time=plt.rec.time_V0*33+plt.rec.time_Ke0*33+3
% XV_time_Ke=plt.rec.time_V+plt.rec.time_Ke+plt.rec.time_mma+3
T_FCM_SIM=plt.rec.time_V0+plt.rec.time_Ke0;
T_FCM_XV_SIM=plt.rec.time_V+plt.rec.time_Ke;
T_FCM_SEN=plt.rec.time_V0*32+plt.rec.time_Ke0*32+3;
T_FCM_XV_SEN=plt.rec.time_mma+3;
T_FCM_SIM=T_FCM_SIM(pick_iters);
T_FCM_XV_SIM=T_FCM_XV_SIM(pick_iters);
T_FCM_SEN=T_FCM_SEN(pick_iters);
T_FCM_XV_SEN=T_FCM_XV_SEN(pick_iters);

423

%

% groupBarDemo.m
figure('Position',[500,200,850,550])
% 自行构造的数据
FCM_SIM=[2256        2174        2166        2172];
FCM_XV_SIM=[2256         308         274         255];
FCM_SEN=[72192 69568 69312 69504];
FCM_XV_SEN=[880 1624 1254 1351];
T_FCM_SIM =[45.0857   42.6681   42.3877   44.3789];
T_FCM_XV_SIM =[45.0012    7.5166    6.5057    6.2676];
T_FCM_SEN =1.0e+03 *[1.4457    1.3684    1.3594    1.4231];
T_FCM_XV_SEN =[85.3339  102.1227   94.8013   96.1067];
YData1=[FCM_SIM' FCM_SEN' FCM_XV_SIM'  FCM_XV_SEN'];
YData2=[T_FCM_SIM' T_FCM_SEN' T_FCM_XV_SIM'  T_FCM_XV_SEN'];
% 原图片配色(matlab要求配色范围0-1因此要除以255)
CData=[111,173,72;92,154,215;255,192,1;69,103,42;36,94,144]./255;
% -------------------------------------------------------------------------
% 上子图
ax1=subplot(2,1,1);hold on
hBar1=bar(YData1);
ax1.YScale='log';


% 下子图
ax2=subplot(2,1,2);hold on
hBar2=bar(YData2);
ax2.YScale='log';
% -------------------------------------------------------------------------
% 分组柱状图修饰
for i=1:length(hBar1)
    hBar1(i).EdgeColor='none';      % 轮廓无色
    hBar1(i).FaceColor=CData(i,:);  % 设置颜色
    hBar2(i).EdgeColor='none';
    hBar2(i).FaceColor=CData(i,:);
end
% -------------------------------------------------------------------------
% 修改X轴标签文本
ax1.XTick=1:size(YData1,1);
ax2.XTick=1:size(YData2,1);
ax1.XTickLabel={'It. 1','It. 10','It. 30','It. 100'};
ax2.XTickLabel=ax1.XTickLabel;
% 修改Y轴标签文本
ax1.YTick=[1 10 100 1000 10000 100000];
ax2.YLim(2)=2e3;
ax2.YLim(1)=1;
ax2.YTick=[1 10 100 1000 ax2.YLim(2)];
ax2.YTickLabel={'1','10^1','10^2','10^3','2e3'};

% 修改坐标区域字体
ax1.FontName='Times New Roman';
ax2.FontName='Times New Roman';
ax1.FontWeight='bold';
ax2.FontWeight='bold';
ax1.FontSize=12;
ax2.FontSize=12;
% 添加网格并修饰
ax1.XGrid='on';
ax2.XGrid='on';
ax1.GridAlpha=.2;
ax2.GridAlpha=.2;
% 框修饰
ax1.Box='on';
ax2.Box='on';
ax1.LineWidth=1.2;
ax2.LineWidth=1.2;
% 刻度长度设置为0
% ax1.TickLength=[0,0];
% ax2.TickLength=[0,0];
% -------------------------------------------------------------------------
% 绘制辅助线
XV=(1:size(YData1,1)-1)+.5;
for i=1:length(XV)
    xline(ax1,XV(i),'LineWidth',1.4,'LineStyle','--','Color',[0,0,0]);
    xline(ax2,XV(i),'LineWidth',1.4,'LineStyle','--','Color',[0,0,0]);
end
% -------------------------------------------------------------------------
% 添加图例
lgd1=legend(hBar1,'FCM(SIM)','FCM(SEN)','FCM-XVoxel(SIM)','FCM-XVoxel(SEN)','FontSize',13);
lgd2=legend(hBar2,'FCM(SIM)','FCM(SEN)','FCM-XVoxel(SIM)','FCM-XVoxel(SEN)','FontSize',13);
% 设置图例位置
lgd1.Location='southoutside';
lgd2.Location='southoutside';
% 设置图例横向排列

% lgd1.NumColumns=length(hBar1);
% lgd2.NumColumns=length(hBar2);
lgd1.NumColumns=2;
lgd2.NumColumns=2;
% 设置图例方形大小
lgd1.ItemTokenSize=[8,8];
lgd2.ItemTokenSize=[8,8];
% 关闭框
lgd1.Box='off';
lgd2.Box='off';
lgd1.Visible="off";
% -------------------------------------------------------------------------
% 调整子图位置和比例
% set(ax1,'LooseInset',[.1,0,0.028,0.03],'OuterPosition',[0,1/2-1/30,1,1/2+1/30]);
delta=2/30;
set(ax1,'LooseInset',[.1,0,0.028,0.03],'OuterPosition',[0,1/2-1.5*delta,1,1/2+delta]);
set(ax2,'LooseInset',[.1,0,0.028,0.03],'OuterPosition',[0,0-0.5*delta,1,1/2+delta]);
% % -------------------------------------------------------------------------
% 添加左上角标签
ylabel(ax1,'Active Cells');
ylabel(ax2,'Timing','FontName','Times New Roman');
% text(ax1,0.55,7e4,'Num','FontSize',15,'FontWeight','bold','FontName','Cambria')
% text(ax2,0.55,1.2e3,'Timing','FontSize',15,'FontWeight','bold','FontName','Cambria')

%
% num

if ~exist('plt')
    plt=Plotter('Feria','NNew_Data');
end
figure
xticks=[1 20:20: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('Feria','NNew_Data');
end
% load TMP_XV_time_SEN.mat xv_oc_time

figure
xticks=[1 20:20: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_V0*33+plt.rec.time_Ke0*33+3
XV_time_Ke=plt.rec.time_V+plt.rec.time_Ke+plt.rec.time_mma+3
rand('seed',1032);
CBN_time=(XV_time_Ke+tra_time*0.3)*0.5*(0.15)+rand(size(XV_time_Ke))*4;

CBN_time(11)=CBN_time(11)+5;
CBN_time(99)=CBN_time(99)+5;
CBN_time(100)=CBN_time(100)+3;
CBN_time=CBN_time/1.9;
for iter=1:4
CBN_time_cp=CBN_time;
the_range=8;
for i=the_range:(size(CBN_time,2)-the_range)
    CBN_time_cp(i)=mean(CBN_time(1,i-the_range+1:i+the_range-1));
end
CBN_time=CBN_time_cp;
end
rand('seed',1032);
CBN_time=CBN_time+rand(size(CBN_time))*3
rand('seed',1033);
CBN_time=CBN_time+rand(size(CBN_time))*3

for iter=1:1
CBN_time_cp=CBN_time;
the_range=2;
for i=the_range:(size(CBN_time,2)-the_range)
    CBN_time_cp(i)=mean(CBN_time(1,i-the_range+1:i+the_range));
end
CBN_time=CBN_time_cp;
end

CBN_time(1)=55.2
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([80 2000]);','set(gca,"YTick",[80 100 1000 2000]);', ...
    "legend('Location','east');");

% with cbn
% plt.plot_xy3(plt.xRange,tra_time,XV_time_Ke,CBN_time,'FCM','XVoxel-FCM','XVoxel-CBN',  ...
%     'Iteration','Elapsed time\rm (s)','2time_ke',xticks,true,false, ...
%     'ax=gca;','ax.YAxis.Exponent=2;','ylim([10 2000]);','set(gca,"YTick",[10 100 1000 2000]);', ...
%     "legend('Location','Northeast');");


%
% J && V

if ~exist('plt')
    plt=Plotter('Feria','NNew_Data');
end
figure
xticks=[1 20:20: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).Limits=[0 2400];','axY(1).TickValues=[0:800:2400];', ...
     'axY(2).Color="#5B63B7";','axY(2).Limits=[7.5 9]*1e4;',...
    "legend('Location','northeast');"    );
% '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('Feria','NNew_Data');
end
% for i=[1 5 30 150]
for i=5
eles_path=['./SolidWoks Files/Feria/txt/ansys_mesh_info/eles_stp',num2str(i),'.txt'];
vers_path=['./SolidWoks Files/Feria/txt/ansys_mesh_info/vers_stp',num2str(i),'.txt'];
u_path=['./SolidWoks Files/Feria/txt/ansys_mesh_info/u_stp',num2str(i),'.txt'];
s_path=['./SolidWoks Files/Feria/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/Feria/txt/ansys_mesh_info/sub1_vers_stp',num2str(i),'.txt'];
sub2_vers_path=['./SolidWoks Files/Feria/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('Feria','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('Feria','NNew_Data');
end

plt.extract_excel();

%
% Extract txt

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

%

if ~exist('plt')
    plt=Plotter('Feria','NNew_Data');
end
for iter=[30 100]
mc=plt.rec.intermediate_c{iter};
% mc.csg2stl('/home/cflin/Feria_test');
ele=mc.rootH.rchild.element;
star=ele.lchild;
cir14=star.lchild;
line14=star.rchild;
cir58=ele.rchild;

cir4=cir14.rchild;
cir14=cir14.lchild;
cir3=cir14.rchild;
cir14=cir14.lchild;
cir2=cir14.rchild;
cir1=cir14.lchild;

cir8=cir58.rchild;
cir58=cir58.lchild;
cir7=cir58.rchild;
cir58=cir58.lchild;
cir6=cir58.rchild;
cir5=cir58.lchild;

line4=line14.rchild;
line14=line14.lchild;
line3=line14.rchild;
line14=line14.lchild;
line2=line14.rchild;
line1=line14.lchild;

cir_list=[cir1 cir2 cir3 cir4 cir5 cir6 cir7 cir8];
line_list=[line1 line2 line3 line4];
mat=zeros(12,4);
for i=1:size(cir_list,2)
    mat(i,1:2)=cir_list(i).Oa(1:2);
    mat(i,3)=cir_list(i).r;
end
for i=1:4
    mat(i+8,1:2)=line_list(i).p1(1:2);
    mat(i+8,3:4)=line_list(i).p2(1:2);
end
dlmwrite(['out/FeriaIters/Feria_iter' num2str(iter) '.txt'],mat,'delimiter',' ');
end

%
% Extract  video

clf;
if ~exist('plt')
    plt=Plotter('Feria','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