clear;clc;clear all;
dbstop if error
% profile on
Oa=[0 0 0]; 
Ra=[0 0 0];  
mp=Build_Cylinders();
% profile on
% step. 1
top_func(mp,1);
% profile viewer

%

% S.2
%------add rc
c_2d=Circle([0 0 0],27);
rc_skt=Sketch([0 0 0],Ra,34,c_2d);
rc=RoundCorner_Circle(8,rc_skt,c_2d,true,false);
rc_csg=CSG();rc_csg.init(rc);
rc_csg.saveAs('rc');
mp.c.insert_node_union("solidField",rc,'solidField');

modify_f(mp);
if mp.isrecord
    tic
    mp.fmt.reflash_table();
    mp.recorder.time_maintain_fmt(end+1)=toc;
else
    mp.fmt.reflash_table();
end
top_func(mp);

%

% S. 3
Pedx=[20 8 4];
Pedy=[8 20 4];
cu10=CSG("Cube","center",[20 8 4],Ra,Pedx,'cu10');

cu11=CSG("Cube","center",[-8 20 4],Ra,Pedy,'cu11');

cu12=CSG("Cube","center",[-20 -8 4],Ra,Pedx,'cu12');

cu13=CSG("Cube","center",[8 -20 4],Ra,Pedy,'cu13');

cu1=cu10+cu11+cu12+cu13;
cu1.saveAs("botPart");
mp.c.insert_node_union("solidField","botPart",'solidField');

cy10=CSG("Cylinder",[40 8 8],[0 -pi/2 0],8,40,'cy10');
cy11=CSG("Cylinder",[-8 40 8],[pi/2 0 0],8,40,'cy11');
cy12=CSG("Cylinder",[-40 -8 8],[0 pi/2 0],8,40,'cy12');
cy13=CSG("Cylinder",[8 -40 8],[-pi/2 0 0],8,40,'cy13');
cy1=cy10+cy11+cy12+cy13;
mp.c.insert_node_union("botPart",cy1);

    modify_f(mp);

if mp.isrecord
    tic
    mp.fmt.reflash_table();
    mp.recorder.time_maintain_fmt(end+1)=toc;
else
    mp.fmt.reflash_table();
end
top_func(mp);
mp.fixedDir=1;% to fixed Dir

%

% S. 4 (2)

ncy20=CSG("Cylinder",[40 8 8],[0 -pi/2 0],9.15/2,40,'ncy20');

ncy21=CSG("Cylinder",[-8 40 8],[pi/2 0 0],9.15/2,40,'ncy21');

ncy22=CSG("Cylinder",[-40 -8 8],[0 pi/2 0],9.15/2,40,'ncy22');

ncy23=CSG("Cylinder",[8 -40 8],[-pi/2 0 0],9.15/2,40,'ncy23');

ncy2=ncy20+ncy21+ncy22+ncy23;

mp.c.insert_node_union('emptyField',ncy2,"emptyField");

    modify_f(mp);

if mp.isrecord
    tic
    mp.fmt.reflash_table();
    mp.recorder.time_maintain_fmt(end+1)=toc;
else
    mp.fmt.reflash_table();
end

top_func(mp);

%

% S. 5 (3)
ncy30=CSG("Cylinder",[24.75 24.75 0],Ra,5.5/2,5,'ncy30');

ncy31=CSG("Cylinder",[-24.75 24.75 0],Ra,5.5/2,5,'ncy31');

ncy32=CSG("Cylinder",[-24.75 -24.75 0],Ra,5.5/2,5,'ncy32');

ncy33=CSG("Cylinder",[24.75 -24.75 0],Ra,5.5/2,5,'ncy33');

ncy3=ncy30+ncy31+ncy32+ncy33;
mp.c.insert_node_union("emptyField",ncy3,"emptyField");

    modify_f(mp);

if mp.isrecord
    tic
    mp.fmt.reflash_table();
    mp.recorder.time_maintain_fmt(end+1)=toc;
else
    mp.fmt.reflash_table();
end

top_func(mp);

%

% S. 6 (4) add top parts
ncy60=CSG("Cylinder",[24.75 24.75 94],Ra,8,16,'cy40');

ncy61=CSG("Cylinder",[-24.75 24.75 94],Ra,8,16,'cy41');

ncy62=CSG("Cylinder",[-24.75 -24.75 94],Ra,8,16,'cy42');

ncy63=CSG("Cylinder",[24.75 -24.75 94],Ra,8,16,'cy43');

ncy6=ncy60+ncy61+ncy62+ncy63;
ncy6.saveAs("topPart");
mp.c.insert_node_union('solidField',"topPart","solidField");

    modify_f(mp);


if mp.isrecord
    tic
    mp.fmt.reflash_table();
    mp.recorder.time_maintain_fmt(end+1)=toc;
else
    mp.fmt.reflash_table();
end

top_func(mp);

%

% S. 7 add top sphere
sp50=CSG("Sphere",[24.75 24.75 94],8,'sp50');

sp51=CSG("Sphere",[-24.75 24.75 94],8,'sp51');

sp52=CSG("Sphere",[-24.75 -24.75 94],8,'sp52');

sp53=CSG("Sphere",[24.75 -24.75 94],8,'sp53');

sp5=sp50+sp51+sp52+sp53;
mp.c.insert_node_union("topPart",sp5,"topPart");

    modify_f(mp);

if mp.isrecord
    tic
    mp.fmt.reflash_table();
    mp.recorder.time_maintain_fmt(end+1)=toc;
else
    mp.fmt.reflash_table();
end

top_func(mp);

%

% S. 8 (6) add ncy
ncy60=CSG("Cylinder",[24.75 24.75 94],Ra,6.75/2,16,'ncy60');

ncy61=CSG("Cylinder",[-24.75 24.75 94],Ra,6.75/2,16,'ncy61');

ncy62=CSG("Cylinder",[-24.75 -24.75 94],Ra,6.75/2,16,'ncy62');

ncy63=CSG("Cylinder",[24.75 -24.75 94],Ra,6.75/2,16,'ncy63');

ncy6=ncy60+ncy61+ncy62+ncy63;
ncy6.saveAs("topNcy");
mp.c.insert_node_union('emptyField',"topNcy",'emptyField');

    modify_f(mp);

if mp.isrecord
    tic
    mp.fmt.reflash_table();
    mp.recorder.time_maintain_fmt(end+1)=toc;
else
    mp.fmt.reflash_table();
end

top_func(mp);

%

 % S. 9 (7+8) add right part
cu70=CSG("Cube","center",[27.25 58-12 60],Ra,[5.51/2 4 5],'cu70');
cu71=CSG("Cube","center",[0 58-12 32.75],Ra,[5 4 5.51/2],'cu71');
cu72=CSG("Cube","center",[-27.25 58-12 60],Ra,[5.51/2 4 5],'cu72');
cu73=CSG("Cube","center",[0 58-12 87.25],Ra,[5 4 5.51/2],'cu73');
cu7=cu70+cu71+cu72+cu73;
cu7.saveAs("rightPart");
mp.c.insert_node_union('solidField','rightPart',"solidField");
    % add right cy
ncy90=CSG("Cylinder",[30 50 60],[pi/2 0 0],5,8,'cy80');
cy81=CSG("Cylinder",[0 50 30],[pi/2 0 0],5,8,'cy81');
cy82=CSG("Cylinder",[-30 50 60],[pi/2 0 0],5,8,'cy82');
cy83=CSG("Cylinder",[0 50 90],[pi/2 0 0],5,8,'cy83');
cu8=ncy90+cy81+cy82+cy83;
mp.c.insert_node_union("rightPart",cu8,"rightPart");
    
modify_f(mp);

if mp.isrecord
    tic
    mp.fmt.reflash_table();
    mp.recorder.time_maintain_fmt(end+1)=toc;
else
    mp.fmt.reflash_table();
end
top_func(mp);

%

 % S. 10 Modify bot 
ncy90=CSG("Cylinder",[30 50 60],[pi/2 0 0],5/2,8,'ncy90');
ncy91=CSG("Cylinder",[0 50 30],[pi/2 0 0],5/2,8,'ncy91');
ncy92=CSG("Cylinder",[-30 50 60],[pi/2 0 0],5/2,8,'ncy92');
ncy93=CSG("Cylinder",[0 50 90],[pi/2 0 0],5/2,8,'ncy93');
ncy9=ncy90+ncy91+ncy92+ncy93;
ncy9.saveAs("rightNcy");
mp.c.insert_node_union("emptyField","rightNcy","emptyField");

    modify_f(mp);

if mp.isrecord
    tic
    mp.fmt.reflash_table();
    mp.recorder.time_maintain_fmt(end+1)=toc;
else
    mp.fmt.reflash_table();
end
top_func(mp);

%

 % S. 11-15 Start loop ......
move=4;
for i=1:(20/move)
    mp.c.modify_node_by_tag_A_argname("cy03",'h',-move,'dai');
    mp.c.modify_node_by_tag_A_argname("topPart",'z',-move,'dai');
    mp.c.modify_node_by_tag_A_argname("topNcy",'z',-move,'dai');
    mp.c.modify_node_by_tag_A_argname("neu1",'z',-move,'dai');

    modify_f(mp);
    
    Neuman_csg=mp.Neuman_csg;
    cur_height=110-move*i;
    Neuman_csg.judge_func=@(X) ((X(:,2)<40) & abs(X(:,3)-cur_height)<Neuman_csg.eepsN) | ((X(:,2)>=40) & abs(X(:,2)-50)<Neuman_csg.eepsN);

    if mp.isrecord
        tic
        mp.fmt.reflash_table();
        mp.recorder.time_maintain_fmt(end+1)=toc;
    else
        mp.fmt.reflash_table();
    end
    if i==20/move
        ;
    end
    top_func(mp);
end

%

 % S. 16-20 Start loop ......
move=4;
for i=1:(20/move)   
    mp.c.modify_node_by_tag_A_argname("cy04",'h',move,'dai');
    mp.c.modify_node_by_tag_A_argname("rightPart",'y',move,'dai');
    mp.c.modify_node_by_tag_A_argname("rightNcy",'y',move,'dai');
    mp.c.modify_node_by_tag_A_argname('neu2','y',move,'dai');
    
    modify_f(mp);

    Neuman_csg=mp.Neuman_csg;
    cur_y=50+move*i;
    Neuman_csg.judge_func=@(X) ((X(:,2)<40) & abs(X(:,3)-(110-20))<Neuman_csg.eepsN) | ((X(:,2)>=40) & abs(X(:,2)-cur_y)<Neuman_csg.eepsN);

    if mp.isrecord
        tic
        mp.fmt.reflash_table();
        mp.recorder.time_maintain_fmt(end+1)=toc;
    else
        mp.fmt.reflash_table();
    end
    if i==20/move
        ;
    end
    top_func(mp);
end


%

mp.saveKe=[];
mp.DBC_cache=[];
save_file_name='NNew_Data/Cylinders/Cylinders_plot_data.mat';
try
    eval_str=['save ',save_file_name,' mp'];
    eval(eval_str);
catch
    eval_str=['save ',save_file_name,' mp -v7.3'];
    eval(eval_str);
end

%

function modify_f(mp)

    Neuman_csg=mp.Neuman_csg;
    c=mp.c;
    fcmdv=FCM_dV(mp.Pst,mp.Ped,mp.pace,mp.maxdeep,mp.order,@c.calSDF,@(X) ones(size(X,1),1));% |||1 dV
    neutop=mp.handleMap('neutop');
    neuright=mp.handleMap('neuright');  
    neutop.judge_func=@(X) (abs(X(:,3)-neutop.rootH.Oa(3))<neutop.eepsN);
    neuright.judge_func=@(X) (abs(X(:,2)-neuright.rootH.Oa(2))<neuright.eepsN);

    St=fcmdv.calIntegral_S(neutop);
    Sr=fcmdv.calIntegral_S(neuright);
    Neuman_csg.fdv=@(X) (X(:,2)<40).*[0 0 -200*1e3/St]+(X(:,2)>=40).*[0 -100*1e3/Sr 0];
end