clear all
sample_name='Cylinders';
base_file=['New_Data/',sample_name,'/'];
eval_str=['load ',base_file,sample_name,'_plot_data mp'];
eval(eval_str);
xLen=length(mp.recorder.num_recal_cells);
xLimit=[1 xLen];
xRange=1:xLen;
xLableRange=1:5:xLen;

%

figure
% Num
x=xRange;
y=mp.recorder.num_recal_cells;
y2=mp.recorder.num_recal_cells0;



% axis tight
% axis([2 12 0 1000]);
box on
% semilogy(x,y2,'k-',x,y,'b:','LineWidth',1.5);
% semilogy(x,y2,'k-',x,y,'b:','LineWidth',1.5);
plot(x,y2,'k-',x,y,'b:','LineWidth',1.5);
xticks=xLableRange;% 设置x轴刻度标签
set(gca,"XTick",xticks);
% xticklabels({'1(5)','1.5(4.5)','2(4)','2.5(3.5)','3(3)'});
% yAxisLabel=[10]
% set(gca,'YTickLabel',yAxisList);
% text(36,0.10,'(b)');

xlabel('Step','FontWeight','bold',"FontSize",15);
ylabel('The number of active voxels','FontWeight','bold',"FontSize",15);
set(gca,'FontName','Times New Roman','FontSize',15,'FontWeight','bold')
hold on
% plot(x2,y2,'k-',x2,y,'b:','LineWidth',1.5);
% set(gca,'xaxislocation','top','color','none','xcolor','r');
ax=gca;% 科学计数法
ax.YAxis.Exponent=3;
hold on
box on

xlim(xLimit)
% ylim([0 4000])%%%%%%%%%%%%%%
set(gcf, 'position', [0 0 700 600*0.618]);% set figure size
legend({'Tradition','XVoxel'},'Location',"east",'FontSize',15,'FontWeight','bold','FontName','Times New Roman');
print(gcf,'-dpng','-r600',[base_file,'1Num']);

%

figure
x=xRange;
y=mp.recorder.time_Ke;
y2=mp.recorder.time_Ke0;

% axis tight
box on
% semilogy(x,y2,'k-',x,y,'b:','LineWidth',1.5);
plot(x,y2,'k-',x,y,'b:','LineWidth',1.5);

xticks=xLableRange;% 设置x轴刻度标签
set(gca,"XTick",xticks);

% xAxisLabel=[22.5:5:52.5];
% set(gca,'XTick',xAxisLabel);
% xticks=[1:0.5:3];% 设置x轴刻度标签
% xticklabels({'1(5)','1.5(4.5)','2(4)','2.5(3.5)','3(3)'});
% yAxisLabel=[0:0.5:1.5];
% set(gca,'YTick',yAxisLabel);
% yAxisLabel=[10]
% set(gca,'YTickLabel',yAxisList);
% text(36,0.10,'(b)');

xlabel('Step','FontWeight','bold',"FontSize",15);
ylabel('Elapsed time\rm (s)','FontWeight','bold',"FontSize",15);
set(gca,'FontName','Times New Roman','FontSize',15,'FontWeight','bold')
hold on
box on

xlim(xLimit)
% ylim([0 10])
set(gcf, 'position', [0 0 700 600*0.618]);% set figure size
legend({'Tradition','XVoxel'},'Location',"best",'FontWeight','bold',"FontSize",15,'FontWeight','bold','FontName','Times New Roman');
print(gcf,'-dpng','-r600',[base_file,'2Time_Ke']);

%
%% stress figure

clf;
% clear all
% sample_name='Cylinders';
% base_file=['TopCSG/New_Data/',sample_name,'/'];
% eval_str=['load ',base_file,sample_name,'_plot_data mp'];
% eval(eval_str);

% mp.viewxy=[60 60];
cAxis=[0 2];

%%%%%
figure
% cAxis=[0 30];
data=mp.recorder.data{13};
cdata=mp.recorder.sdata{13};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);
colorbar('off')
axis on;
caxis(cAxis);
%%%%%

%



pos2 = [0.05 0.75 0.25 0.25];
subplot('Position',pos2)
annPos=[0.05+0.10 0.7 0.12 0.05];
annotation('textbox',annPos,'String','Step. 1','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{1};
cdata=mp.recorder.sdata{1};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);colorbar('off')
caxis(cAxis);



subplot('Position',pos2+[0.3 0 0 0])
annotation('textbox',annPos+[0.3 0 0 0],'String','Step. 4','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{4};
cdata=mp.recorder.sdata{4};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);
colorbar('off')
caxis(cAxis);

subplot('Position',pos2+[0.3*2 0 0 0]);
annotation('textbox',annPos+[0.3*2 0 0 0],'String','Step. 7','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{7};
cdata=mp.recorder.sdata{7};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);
colorbar('off')
caxis(cAxis);


dcoord=[0 -0.3 0 0];
subplot('Position',pos2+dcoord);
annotation('textbox',annPos+dcoord,'String','Step. 11','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{11};
cdata=mp.recorder.sdata{11};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);
colorbar('off');
caxis(cAxis);

dcoord=[0.3 -0.3 0 0];
subplot('Position',pos2+dcoord)
annotation('textbox',annPos+dcoord,'String','Step. 12','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{12};
cdata=mp.recorder.sdata{12};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);caxis(cAxis);
caxis(cAxis);

dcoord=[0.3*2 -0.3 0 0];
subplot('Position',pos2+dcoord)
annotation('textbox',annPos+dcoord,'String','Step. 21','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{21};
cdata=mp.recorder.sdata{21};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);caxis(cAxis);
caxis(cAxis);

%%%
dcoord=[0 -0.3*2 0 0];
idx=31;
subplot('Position',pos2+dcoord);
annotation('textbox',annPos+dcoord,'String',['Step. ',num2str(idx)],'EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{idx};
cdata=mp.recorder.sdata{idx};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);
colorbar('off');
caxis(cAxis);

dcoord=[0.3 -0.3*2 0 0];
idx=41;
subplot('Position',pos2+dcoord)
annotation('textbox',annPos+dcoord,'String','Step. 41','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{idx};
cdata=mp.recorder.sdata{idx};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);caxis(cAxis);
caxis(cAxis);

dcoord=[0.3*2 -0.3*2 0 0];
idx=51;
subplot('Position',pos2+dcoord)
annotation('textbox',annPos+dcoord,'String','Step. 51','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{idx};
cdata=mp.recorder.sdata{idx};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);caxis(cAxis);
caxis(cAxis);

clb=colorbar('southoutside','AxisLocation','in','FontWeight',"bold",'FontName','Times New Roman','FontSize',10,'FontWeight','bold','Position',[0.12 0.05 0.72 0.02]);
set(gcf, 'position', [0 0 700 700]*2);% set figure size
print(gcf,'-dpng','-r600',[base_file,'3stress']);

%
%% movement figure

clf;cla;
% clear all
% sample_name='Cylinders';
% base_file=['TopCSG/New_Data/',sample_name,'/'];
% eval_str=['load ',base_file,sample_name,'_plot_data mp'];
% eval(eval_str);

% mp.viewxy=[60 60];
cAxis=[0 250];

%%%%%
figure
% cAxis=[0 30];
data=mp.recorder.data{13};
cdata=mp.recorder.udata{13};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);
colorbar('off')
axis on;
caxis(cAxis);
%%%%%

%



pos2 = [0.05 0.75 0.25 0.25];
subplot('Position',pos2)
annPos=[0.05+0.10 0.7 0.12 0.05];
annotation('textbox',annPos,'String','Step. 1','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{1};
cdata=mp.recorder.udata{1};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);colorbar('off')
caxis(cAxis);



subplot('Position',pos2+[0.3 0 0 0])
annotation('textbox',annPos+[0.3 0 0 0],'String','Step. 4','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{4};
cdata=mp.recorder.udata{4};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);
colorbar('off')
caxis(cAxis);

subplot('Position',pos2+[0.3*2 0 0 0]);
annotation('textbox',annPos+[0.3*2 0 0 0],'String','Step. 7','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{7};
cdata=mp.recorder.udata{7};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);
colorbar('off')
caxis(cAxis);


dcoord=[0 -0.3 0 0];
subplot('Position',pos2+dcoord);
annotation('textbox',annPos+dcoord,'String','Step. 11','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{11};
cdata=mp.recorder.udata{11};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);
colorbar('off');
caxis(cAxis);

dcoord=[0.3 -0.3 0 0];
subplot('Position',pos2+dcoord)
annotation('textbox',annPos+dcoord,'String','Step. 12','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{12};
cdata=mp.recorder.udata{12};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);caxis(cAxis);
caxis(cAxis);

dcoord=[0.3*2 -0.3 0 0];
subplot('Position',pos2+dcoord)
annotation('textbox',annPos+dcoord,'String','Step. 21','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{21};
cdata=mp.recorder.udata{21};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);caxis(cAxis);
caxis(cAxis);

%%%
dcoord=[0 -0.3*2 0 0];
idx=31;
subplot('Position',pos2+dcoord);
annotation('textbox',annPos+dcoord,'String',['Step. ',num2str(idx)],'EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{idx};
cdata=mp.recorder.udata{idx};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);
colorbar('off');
caxis(cAxis);

dcoord=[0.3 -0.3*2 0 0];
idx=41;
subplot('Position',pos2+dcoord)
annotation('textbox',annPos+dcoord,'String','Step. 41','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{idx};
cdata=mp.recorder.udata{idx};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);caxis(cAxis);
caxis(cAxis);

dcoord=[0.3*2 -0.3*2 0 0];
idx=51;
subplot('Position',pos2+dcoord)
annotation('textbox',annPos+dcoord,'String','Step. 51','EdgeColor','none','FontName','Times New Roman');
axis off
data=mp.recorder.data{idx};
cdata=mp.recorder.udata{idx};
cdata(cdata>cAxis(2))=cAxis(2);
mp.display_u(data,cdata);caxis(cAxis);
caxis(cAxis);

clb=colorbar('southoutside','AxisLocation','in','FontWeight',"bold",'FontName','Times New Roman','FontSize',10,'FontWeight','bold','Position',[0.12 0.05 0.72 0.02]);
set(gcf, 'position', [0 0 700 700]*2);% set figure size
print(gcf,'-dpng','-r600',[base_file,'3movement']);

%
% Export pic to video

clf;
stress_flg=0;
if stress_flg
    writeobj=VideoWriter([base_file,'stress_Video.avi']);
    cAxis=[0 2];
else
    writeobj=VideoWriter([base_file,'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
writeobj.close();

%
%
%
%
% Export data to excel

data=[mp.recorder.num_recal_cells',mp.recorder.num_recal_cells0',mp.recorder.time_Ke',mp.recorder.time_Ke0',mp.recorder.time_maintain_fmt'];
title={'Number of recalculated cells(Ours)','Number of recalculated cells(Traditional)','Time of calculating Ke(Ours)  (s)','Time of calculating Ke(Traditional)  (s)','Time of maintaining feature_mapping_table  (s)'};
data=num2cell(data);
data=[title;data];
filePath=[base_file,'origin_data.xls'];
writecell(data,filePath);
% set(gcf,'color','none'); %图形背景设为无色
% set(gca,'color','none'); %坐标轴背景设为无色，这条更重要，通常图形背景的白色实际为坐标轴背景色
% print(gcf,'-dpng','-r600',['TopCsgTmp\saveFigures\',index,'6NormU']);
% fprintf('TotalActiveVoxelsRatio: %.6f',sum(y)/sum(y2))
% fprintf('mean time of Ke by traditon method: %.6f',mean(y2))
% fprintf('mean time of Ke by Xvol method: %.6f',mean(y))

%
% Extract stl

whichOne=41;
data=mp.recorder.data{whichOne};
filePath=[base_file,sample_name,num2str(whichOne),'_testSTL'];
mp.extract_stl_by_data(data,filePath);


%
% Extract txt


%% write txt

% prec=8;
% for i=1:50
%     whichOne=i;
%     target_file='C:/Users/cflin/Cloud/vscodeField/pythonField/cw/';
%     % extract stl
%     data=mp.recorder.data{whichOne};
%     filePath=[target_file,'output/',sample_name,'_',num2str(whichOne)];
%     cur_c=mp.recorder.intermediate_c{whichOne};
%     cur_c.set_display_args(mp.Pst,mp.Ped,mp.pace/prec,'viewxy',mp.viewxy);
%     cur_c.csg2stl(filePath);
% end

whichOne=21;
recder=mp.recorder;
num_row=size(recder.flag_recal_cells{whichOne},1);

fid=fopen([target_file,'txtFile/','Info.txt'],'wt');
fprintf(fid,'%s\n',sample_name);
fprintf(fid,'%d\n',num_row);
fclose(fid);
dlmwrite([target_file,'txtFile/',sample_name,'_OursFlg.txt'], recder.flag_recal_cells{whichOne}, 'delimiter', '\t','precision', 6,'newline', 'pc')
dlmwrite([target_file,'txtFile/',sample_name,'_TradFlg.txt'], recder.flag_recal_cells0{whichOne}, 'delimiter', '\t','precision', 6,'newline', 'pc')
dlmwrite([target_file,'txtFile/',sample_name,'_Coords.txt'], recder.corner_coords, 'delimiter', '\t','precision', 6,'newline', 'pc')

%
% Extract CSG Tree

prec=8;
last_rootH=mp.recorder.intermediate_c{end}.rootH;
output_file=['C:/Users/cflin/Desktop/Paint Files/Cylinders/','last_CSG_tree/'];
que={};
last_rootH.level=1;
que{end+1}=last_rootH;
cot=0;
last_level=-1;
while(~isempty(que))
    rootH=que{1};
    level=rootH.level;
    if(level~=last_level)
        last_level=level;
        cot=0;
    end
    que(1)=[];
    cot=cot+1;
    
    commit='Primitive';
    if(isa(rootH,'Operator'))
        rootH.lchild.level=level+1;
        rootH.rchild.level=level+1;
        que{end+1}=rootH.lchild;
        que{end+1}=rootH.rchild;
        if(isa(rootH,'OperateMinus'))
            commit='Minus';
        elseif(isa(rootH,'OperateMtimes'))
            commit='Mtimes';
        elseif(isa(rootH,'OperatePlus'))
            commit='Plus';
        elseif(isa(rootH,'FULL'))
            commit='FULL';
        elseif(isa(rootH,'NULL'))
            commit='NULL';
        end
    end
%     if level==3 && cot==2
%         rootH.node2stl(mp.Pst,mp.Ped,mp.pace/prec,[output_file,'node_',num2str(level),'_',num2str(cot),'_',commit]);
%     end
    tmp=rootH.get_outer_points();
    the_Pst=min(tmp(:,1:3));
    the_Ped=max(tmp(:,4:6));
    rootH.node2stl(the_Pst,the_Ped,mp.pace/prec,[output_file,'node_',num2str(level),'_',num2str(cot),'_',commit]);

        
end