function plotmesh(cables,P,Nodal_data,titleString,DVcng,data,cablesMAT, plt_cables, plt_nodal_col, plot3D)
% keyboard
Ncables = cablesMAT(end,1);
A = ones(Ncables,1);
Ncppc = cablesMAT(:,2);
%%
pltopt.plt_geom = 1;
pltopt.grid=0;
pltopt.cables = 1;
pltopt.nodal_col = 0;
pltopt.colorbar = 1;
pltopt.max_thickness = 1.0;
pltopt.ctrlp = 0;
pltopt.zval = 0;
pltopt.intrsctmrk = 0;
pltopt.intrsctnmb = 0;
pltopt.loadmarker = 0;
pltopt.loadval = 0;
pltopt.supports = 1;
pltopt.nodal_val = 0;
pltopt.title = 1;
pltopt.plt_cbl_elevetion=0; %cables that will be displayed in X-Z plane
pltopt.plot3D = 0;
pltopt.gpContourPlot = 0;
pltopt.plt_dof_cng = 0;
pltopt.fontsize=20;

%%
% plt_cables=pltopt.cables;
max_thickness=pltopt.max_thickness;
plt_ctrlp=pltopt.ctrlp;
plt_zval=pltopt.zval;
plt_intrsctmrk=pltopt.intrsctmrk;
plt_intrsctnmb=pltopt.intrsctnmb;
plt_loadmarker=pltopt.loadmarker;
plt_loadval=pltopt.loadval;
plt_supports=pltopt.supports;
plt_nodal_val=pltopt.nodal_val;
% plt_nodal_col=pltopt.nodal_col;
plt_colorbar=pltopt.colorbar;
plt_dof_cng=pltopt.plt_dof_cng;
% plot3D = pltopt.plot3D;
nelx=data.model.FE.nelx;
nely=data.model.FE.nely;
domx=data.geom.domx;
domy=data.geom.domy;
elements=data.model.FE.elements;
nodes=data.model.FE.nodes;
Nnodes=size(nodes,1);
Nelem=size(elements,1);
ex=data.model.FE.ex;
ey=data.model.FE.ex;

figure(1)
axis equal
if plt_intrsctmrk
    for i=1:size(cables,1)
        plot(cables(i,3),cables(i,4),'green','marker','o');
        if ~ishold; hold on; end
        if plt_intrsctnmb
            text(cables(i,3)+0.02,cables(i,4),sprintf('%d',i));
        end
    end    
end
if plt_nodal_col || plt_nodal_val
    Nodal_data = full(Nodal_data);
    if size(Nodal_data,1) == 5*size(data.model.FE.nodes,1)
        Nodal_data=full(reshape(Nodal_data,[5,Nnodes]))';
        w=Nodal_data(:,3);
    else
        w=Nodal_data;
    end
    w_element=max(w(elements(:,2:5))')';%*[1;1;1;1]*0.25;
    wmax=max(w);
    wmin=min(w);
    J = jet;
    title(sprintf('W_m_a_x=%1.6f  W_m_i_n=%1.6f',wmin,wmax))
    transperancy=ones(size(w));
    wplot = (sparse(data.model.FE.nodes(:,22),data.model.FE.nodes(:,23),w));
    transperancy = (sparse(data.model.FE.nodes(:,22),data.model.FE.nodes(:,23),transperancy));
    wplot = full(wplot);
    imagesc([0 data.geom.domx], [0 data.geom.domy],wplot,'AlphaData',transperancy);

    if ~ishold; hold on; end
    colormap(J)
    if plt_colorbar
        ticks = [0 1 2 3 4 5 6 7 8 9 10]/10*(wmax-wmin)+wmin;
        colorbar('position',[0.9,0.11,0.018,0.815],'FontSize',20)
%         [wplot(14,14), wplot(14,1), wplot(39,14), wplot(39,39), wplot(51,26)]
     end
 end


for cable = 1:Ncables
    Pcable=P(sum(Ncppc(1:cable-1))*3+1:sum(Ncppc(1:cable))*3,1);
    Pcable=reshape(Pcable,[3,Ncppc(cable)]);
    if and(plt_ctrlp, A(cable) > 0) 
        plot(Pcable(1,:),Pcable(2,:),'--','color','red','marker','o');
        if ~ishold; hold on; end
    end
    if plt_cables
        if A(cable)>0
            [x,y,z,Xp]=My_bsline(Pcable, data);
            z_rel = z/(data.geom.h-data.model.PT.dsbot-data.model.PT.dstop)+0.5;
            [R,G,B,r,g,b]=rgbf(z_rel');
            for i=2:size(R,1)
                plot([x(i);x(i-1)],[y(i);y(i-1)],'color',([R(i),G(i),B(i)]+[R(i-1),G(i-1),B(i-1)])*0.5/255,'LineWidth',A(cable)*max_thickness);
                if ~ishold; hold on; end
            end
%             i=0;
             if plt_zval
                for cp=2:size(Pcable,2)-1
                    zval=Pcable(3,cp);
                    s=sprintf('%1.2f',zval);
                    text(Pcable(1,cp),Pcable(2,cp),s);
                end
            end
        end
    end
end
if and(plt_colorbar,plt_cables)
    x=domx+2*ex+3*[0,0,0,0,0,0,0,0,0,0,0,...
                ex,ex,ex,ex,ex,ex,ex,ex,ex,ex,ex];
    y=[ 0,0.1*domy,0.2*domy,0.3*domy,0.4*domy,0.5*domy,0.6*domy,0.7*domy,0.8*domy,0.9*domy,domy,...
        0,0.1*domy,0.2*domy,0.3*domy,0.4*domy,0.5*domy,0.6*domy,0.7*domy,0.8*domy,0.9*domy,domy];
    for i=1:10
        patch([x(i),x(11+i),x(12+i),x(i+1)],[y(i),y(11+i),y(12+i),y(i+1)],[r(i)/255,g(i)/255,b(i)/255]);
        text(x(i)+3.3*ex,y(i),sprintf('%1.0f',1000*((i-1)/11*(data.geom.h-data.model.PT.dsbot-data.model.PT.dstop)-(data.geom.h-data.model.PT.dsbot-data.model.PT.dstop)/2)),'fontsize',pltopt.fontsize,'color','black');

    end
    text(x(11)+3.3*ex,y(11),sprintf('%1.0f',1000*(data.geom.h-data.model.PT.dsbot-data.model.PT.dstop)/2),'fontsize',pltopt.fontsize,'color','black');
end
for cable = 1:Ncables
    if pltopt.plt_cbl_elevetion == cable 
%         figure
        Pcable=P((cable-1)*Ncppc*3+1:(cable)*Ncppc*3,1);
        Pcable=reshape(Pcable,[3,Ncppc]);
        z_scale_value = 10;
        Pcable(3,:) = Pcable(3,:)*z_scale_value;
        [x,y,z,Xp]=My_bsline(Pcable, data);
        plot(x,z,'linewidth',2,'color','r','LineWidth',A(cable)*max_thickness);
        hold on
        axis equal
        i=0;
        while i<size(x,2)
            i=i+1;
            if z(1,i)>1e-5 
                j=1;
                while z(i+j-1)>1e-5 && i+j-1<size(z,2)
                    j=j+1;
                end
                plot(x(i:i+j-1),z(i:i+j-1),'linewidth',2,'color','b','LineWidth',A(cable)*max_thickness);
                i=i+j;
            end
        end
        if and(plt_ctrlp, A(cable) > 0) 
            plot(Pcable(1,:),Pcable(3,:),'--','color','red','marker','o');
        end
    end
end
if plt_loadmarker
    if p ~= 0
        plot(slx,sly,'red','marker','o')
        if ~ishold; hold on; end
    end
    for el=1:Nelem
        if elements(el,12) ~= 0
            plot(elements(el,13),elements(el,14),'red','marker','s','MarkerSize',10)
            if ~ishold; hold on; end
            if plt_loadval
                text(elements(el,13),elements(el,14),sprintf('%1.1f',elements(el,12)),'fontsize',11,'color','blue');
            end
        end
    end
end
if pltopt.title
    title(titleString);
end

if plt_dof_cng
    Pplt_dof_cng=reshape(P,[3,Ncables*Ncppc]);
    DVcng=DVcng(1:end-Ncables,end);
    DVcng=reshape(DVcng,[3,Ncables*Ncppc]);
    quiver(Pplt_dof_cng(1,:),Pplt_dof_cng(2,:),DVcng(1,:),DVcng(2,:),'color',[0.4660 0.6740 0.1880])
    if ~ishold; hold on; end
end

plotGeom(data)
hold off
a=0.05;
axis off
set(gca,'Ydir','normal')
if plot3D
%     figure
    plot3DCables(P,data,pltopt,cablesMAT)
end    
end

function [ R,G,B,r,g,b ] = rgbf(relw)

%this function returns the rgb triple for the input vector (ranging from 0 to 1)
R=zeros(size(relw));
G=zeros(size(relw));
B=zeros(size(relw));

r=[0,7,14,22,30,55,216,238,246,248,250];
g=[39,132,220,235,237,240,242,244,203,139,0];
b=[229 231,233,166,91,38,46,54,62,71,0];

sec1=relw<0.1;
sec2=(relw>0.1).*(relw<0.2);
sec3=(relw>0.2).*(relw<0.3);
sec4=(relw>0.3).*(relw<0.4);
sec5=(relw>0.4).*(relw<0.5);
sec6=(relw>0.5).*(relw<0.6);
sec7=(relw>0.6).*(relw<0.7);
sec8=(relw>0.7).*(relw<0.8);
sec9=(relw>0.8).*(relw<0.9);
sec10=(relw>0.9);

R(sec1,1)=relw(sec1)/0.1*(r(2)-r(1))+r(1);
R(sec2>0,1)=(relw(sec2>0)-0.1)/0.1*(r(3)-r(2))+r(2);
R(sec3>0,1)=(relw(sec3>0)-0.2)/0.1*(r(4)-r(3))+r(3);
R(sec4>0,1)=(relw(sec4>0)-0.3)/0.1*(r(5)-r(4))+r(4);
R(sec5>0,1)=(relw(sec5>0)-0.4)/0.1*(r(6)-r(5))+r(5);
R(sec6>0,1)=(relw(sec6>0)-0.5)/0.1*(r(7)-r(6))+r(6);
R(sec7>0,1)=(relw(sec7>0)-0.6)/0.1*(r(8)-r(7))+r(7);
R(sec8>0,1)=(relw(sec8>0)-0.7)/0.1*(r(9)-r(8))+r(8);
R(sec9>0,1)=(relw(sec9>0)-0.8)/0.1*(r(10)-r(9))+r(9);
R(sec10>0,1)=(relw(sec10>0)-0.9)/0.1*(r(11)-r(10))+r(10);

G(sec1,1)=relw(sec1)/0.1*(g(2)-g(1))+g(1);
G(sec2>0,1)=(relw(sec2>0)-0.1)/0.1*(g(3)-g(2))+g(2);
G(sec3>0,1)=(relw(sec3>0)-0.2)/0.1*(g(4)-g(3))+g(3);
G(sec4>0,1)=(relw(sec4>0)-0.3)/0.1*(g(5)-g(4))+g(4);
G(sec5>0,1)=(relw(sec5>0)-0.4)/0.1*(g(6)-g(5))+g(5);
G(sec6>0,1)=(relw(sec6>0)-0.5)/0.1*(g(7)-g(6))+g(6);
G(sec7>0,1)=(relw(sec7>0)-0.6)/0.1*(g(8)-g(7))+g(7);
G(sec8>0,1)=(relw(sec8>0)-0.7)/0.1*(g(9)-g(8))+g(8);
G(sec9>0,1)=(relw(sec9>0)-0.8)/0.1*(g(10)-g(9))+g(9);
G(sec10>0,1)=(relw(sec10>0)-0.9)/0.1*(g(11)-g(10))+g(10);

B(sec1,1)=relw(sec1)/0.1*(b(2)-b(1))+b(1);
B(sec2>0,1)=(relw(sec2>0)-0.1)/0.1*(b(3)-b(2))+b(2);
B(sec3>0,1)=(relw(sec3>0)-0.2)/0.1*(b(4)-b(3))+b(3);
B(sec4>0,1)=(relw(sec4>0)-0.3)/0.1*(b(5)-b(4))+b(4);
B(sec5>0,1)=(relw(sec5>0)-0.4)/0.1*(b(6)-b(5))+b(5);
B(sec6>0,1)=(relw(sec6>0)-0.5)/0.1*(b(7)-b(6))+b(6);
B(sec7>0,1)=(relw(sec7>0)-0.6)/0.1*(b(8)-b(7))+b(7);
B(sec8>0,1)=(relw(sec8>0)-0.7)/0.1*(b(9)-b(8))+b(8);
B(sec9>0,1)=(relw(sec9>0)-0.8)/0.1*(b(10)-b(9))+b(9);
B(sec10>0,1)=(relw(sec10>0)-0.9)/0.1*(b(11)-b(10))+b(10);


end


function  plotGeom(data)
    axis equal
    xlim([0 data.geom.domx]);
    ylim([0 data.geom.domy]);
    plot([data.geom.contour.x,data.geom.contour.x(1)],[data.geom.contour.y,data.geom.contour.y(1)],'k','LineWidth',2);
    hold on
    for c = 1:size(data.geom.col.x,1)
%         plot(data.geom.Cx,data.geom.Cy,'sk','LineWidth',1,'MarkerSize',data.geom.col_size)
        col_x = data.geom.col.x_act(c);
        col_y = data.geom.col.y_act(c);
        col_a = data.geom.col.a(c);
        col_b = data.geom.col.b(c);
        rectangle('Position',[col_x-col_a/2 col_y-col_b/2 col_a col_b],'EdgeColor', 'k','LineWidth',0.2,'FaceColor','none')
    end
    % for i=1:size(data.Cx,1)
    %     text(data.Cx(i,1),data.Cy(i,1),sprintf('%1.0i',i))
    % end
    for wall = 1:size(data.geom.wall.x,1)
        plot([data.geom.wall.x{wall,1}],[data.geom.wall.y{wall,1}],'k','LineWidth',2);
    end
    for opening = 1:size(data.geom.opening.x,1)
        patch([data.geom.opening.x{opening,1}],[data.geom.opening.y{opening,1}],'g','FaceAlpha',0.1);
    end

    hold off
    axis off
end

function plot3DCables(P,data,pltopt,cablesMAT)

Ncables = size(cablesMAT,1);
A = ones(Ncables,1);
Ncppc = cablesMAT(:,2);
max_thickness=pltopt.max_thickness;
figure
hold on
zscale = 10;
%% plot 3d floor

nodes = data.model.FE.nodes;
nodex = [nodes(:,2);nodes(:,2)];
nodey = [nodes(:,3);nodes(:,3)];
nodez = [-ones(size(nodes,1),1)*data.geom.h/2;ones(size(nodes,1),1)*data.geom.h/2]*zscale;
floor = alphaShape(nodex,nodey,nodez);
plot(floor,'EdgeColor','none','FaceColor',[0.5,0.5,0.5],'FaceAlpha',0.2)

for cable = 1:Ncables
    Pcable=P(sum(Ncppc(1:cable-1))*3+1:sum(Ncppc(1:cable))*3,1);
    Pcable=reshape(Pcable,[3,Ncppc(cable)]);
    if A(cable)>0
        [x,y,z,Xp]=My_bsline(Pcable, data);
        z_rel = z/(data.geom.h-data.model.PT.dsbot-data.model.PT.dstop)+0.5;
        [R,G,B,r,g,b]=rgbf(z_rel');
        for i=2:size(R,1)
            plot3([x(i);x(i-1)],[y(i);y(i-1)],[z(i);z(i-1)]*zscale,'color',([R(i),G(i),B(i)]+[R(i-1),G(i-1),B(i-1)])*0.5/255,'LineWidth',A(cable)*max_thickness);
        end
    end
%     Plot3(CableX,CableY,CableZ,'linewdth',1,'color','b')
view(30,20)
end

end

function [x,y,z,Xp] = My_bsline(P, data)

n=size(P,2)-1;
N_lines = 3;
if n>2
    t=(0:1/N_lines:1);
    T=[t.^2;t;t.^0];
    M1=data.model.PT.M1;
    M2=data.model.PT.M2;
    M3=data.model.PT.M3;
    B=P(:,1:3)*M1*T;
    S=B;
    Xp=P(:,1:3)*M1*[1;1;1];
    for i=2:n-2
        B=P(:,i:i+2)*M2*T;
        S=[S B];
        Xpt=P(:,i:i+2)*M2*[1;1;1];
        Xp=[Xp Xpt];
    end
    B=P(:,n-1:n+1)*M3*T;
    S=[S B];
    x=S(1,:);
    y=S(2,:);
    z=S(3,:);
else
    t=(0:1/N_lines:1);
    T=[t.^2;t;t.^0];
    M=data.M0;
    B=P(:,1:3)*M*T;
    S=B;
    Xp=P(:,1:3)*M*[1;1;1];
    x=S(1,:);
    y=S(2,:);
    z=S(3,:);
end
end

