# Amira Abdel-Rahman
# (c) Massachusetts Institute of Technology 2020

#####################################Ployt####################################################################
function topplot3d(xPhys)
    ix=[]
    iy=[]
    iz=[]
    for j in 1:nely
        for i in 1:nelx
            for k in 1:nelz
                if(xPhys[j,i,k]>0.0)
                    append!(ix,i)
                    append!(iy,j)
                    append!(iz,k)
                end
            end
        end
    end
    # r = 4.0
    # lim = FRect3D((-4,-4,-4*r),(8,8,8*r))
    return scatter(ix,iz,iy,color="black",label="",markersize =4, aspect_ratio=:equal,markerstrokealpha = 0.2,markeralpha = 0.6,markershape = :square,camera = (30, 60),xlim=(0,nelx),zlim=(0,nely),ylim=(-10,10))#,markershape = :square
end

function plotDisplacement(nelx,nely,nelz,xPhysC,getProblem=inverter,factor=0.04,cameraX=30,cameraY=60)
    U,ndof,freedofs=getDisplacement(xPhysC,nelx,nely,nelz,getProblem)
    displacement=reshape((U[:,2]),3,(nely+1),(nelx+1),(nelz+1));

    anim1=Animation()
    for step in 0:10
        ix=[]
        iy=[]
        iz=[]
        exg=factor*step
        for j in 1:nely
            for i in 1:nelx
                for k in 1:nelz
                    if(xPhysC[j,i,k]>0.0)
                        append!(ix,i+exg*(displacement[1,j,i,k]))
                        append!(iy,j+exg*(displacement[2,j,i,k]))
                        append!(iz,k+exg*(displacement[3,j,i,k]))
                    end
                end
            end
        end
        # r = 4.0
        # lim = FRect3D((-4,-4,-4*r),(8,8,8*r))
        scatter(ix,iz,iy,color="black",label="",markersize =4, aspect_ratio=:equal,markerstrokealpha = 0.2,markeralpha = 0.6,markershape = :square,camera = (cameraX,cameraY),xlim=(0,nelx*1.5),zlim=(-0.5*nely,nely*1.5),ylim=(-10,10))#,markershape = :square
        frame(anim1)
    end
    return anim1
    
end

function make_bitmap(p,nx,ny,alpha)
    color = [1 0 0; 0 0 .45; 0 1 0; 0 0 0; 1 1 1];
    I = zeros(nx*ny,3);
    for j = 1:p
        I[:,1:3] = I[:,1:3] + alpha[:,j] .*color[j,1:3]';
    end
    # I = imresize(reshape(I,ny,nx,3),10,'bilinear');
    I=reshape(I,ny,nx,3)
    II=hcat(I[:,end:-1:1,:],I)
    return II
end

function make_bitmap_compliant(p,nx,ny,alpha)
    color = [1 0 0; 0 0 .45; 0 1 0; 0 0 0; 1 1 1];
    I = zeros(nx*ny,3);
    for j = 1:p
        I[:,1:3] = I[:,1:3] + alpha[:,j] .*color[j,1:3]';
    end
    # I = imresize(reshape(I,ny,nx,3),10,'bilinear');
    I=reshape(I,ny,nx,3)
    # II=hcat(I[:,end:-1:1,:],I)
    II=vcat(I[end:-1:1,:,:],I)
    return II
end

function make_bitmap_3d(p,nx,ny,nz,alpha)
    color = [1 0 0; 0 0 1; 0 1 0; 0 0 0; 1 1 1];
    I = zeros(nx*ny*nz,3);
    for j = 1:p
        I[:,1:3] = I[:,1:3] + alpha[:,j] .*color[j,1:3]';
    end
    # I = imresize(reshape(I,ny,nx,3),10,'bilinear');
    I=reshape(I,ny,nx,nz,3)
    II=hcat(I[:,end:-1:1,:,:],I)
    return II
end

function topplotmulti3d(nelx,nely,nelz,I,threshold)
    ixr=[]
    iyr=[]
    izr=[]
    
    ixg=[]
    iyg=[]
    izg=[]
    
    ixb=[]
    iyb=[]
    izb=[]
    for j in 1:nely
        for i in 1:nelx
            for k in 1:nelz
                if(I[j,i,k,1]>threshold)
                    append!(ixr,i)
                    append!(iyr,j)
                    append!(izr,k)
                end
                if(I[j,i,k,2]>threshold)
                    append!(ixg,i)
                    append!(iyg,j)
                    append!(izg,k)
                end
                if(I[j,i,k,3]>threshold)
                    append!(ixb,i)
                    append!(iyb,j)
                    append!(izb,k)
                end
            end
        end
    end
    p=scatter(ixr,izr,iyr,color="red",label="",markersize =4, aspect_ratio=:equal,markerstrokealpha = 0.2,markeralpha = 0.6,markershape = :square,camera = (30, 60),xlim=(0,nelx),zlim=(0,nely),ylim=(-10,10))#,markershape = :square
    p=scatter!(ixg,izg,iyg,color="green",label="",markersize =4, aspect_ratio=:equal,markerstrokealpha = 0.2,markeralpha = 0.6,markershape = :square,camera = (30, 60),xlim=(0,nelx),zlim=(0,nely),ylim=(-10,10))#,markershape = :square
    p=scatter!(ixb,izb,iyb,color="blue",label="",markersize =4, aspect_ratio=:equal,markerstrokealpha = 0.2,markeralpha = 0.6,markershape = :square,camera = (30, 60),xlim=(0,nelx),zlim=(0,nely),ylim=(-10,10))#,markershape = :square
    return p

end

function topplotmulti3d_mirror(nelx,nely,nelz,I,threshold,vertical)
    ixr=[]
    iyr=[]
    izr=[]
    
    ixg=[]
    iyg=[]
    izg=[]
    
    ixb=[]
    iyb=[]
    izb=[]
    if(vertical)
        for j in 1:nely
            for i in 1:nelx
                for k in 1:nelz
                    if(I[j,i,k,1]>threshold)
                        append!(ixr,i)
                        append!(ixr,i)
                        append!(iyr,nely-j)
                        append!(iyr,j+nely-1)
                        append!(izr,k)
                        append!(izr,k)
                    end
                    if(I[j,i,k,2]>threshold)
                        append!(ixg,i)
                        append!(ixg,i)
                        append!(iyg,nely-j)
                        append!(iyg,j+nely-1)
                        append!(izg,k)
                        append!(izg,k)
                    end
                    if(I[j,i,k,3]>threshold)
                        append!(ixb,i)
                        append!(ixb,i)
                        append!(iyb,nely-j)
                        append!(iyb,j+nely-1)
                        append!(izb,k)
                        append!(izb,k)
                    end
                end
            end
        end
    else
        for j in 1:nely
            for i in 1:nelx
                for k in 1:nelz
                    if(I[j,i,k,1]>threshold)
                        append!(ixr,nelx-i+nelx-1)
                        append!(ixr,i)
                        append!(iyr,j)
                        append!(izr,k)
                        append!(iyr,j)
                        append!(izr,k)
                    end
                    if(I[j,i,k,2]>threshold)
                        append!(ixg,nelx-i+nelx-1)
                        append!(ixg,i)
                        append!(iyg,j)
                        append!(izg,k)
                        append!(iyg,j)
                        append!(izg,k)
                    end
                    if(I[j,i,k,3]>threshold)
                        append!(ixb,nelx-i+nelx-1)
                        append!(ixb,i)
                        append!(iyb,j)
                        append!(izb,k)
                        append!(iyb,j)
                        append!(izb,k)
                        
                    end
                end
            end
        end
    end
    p=scatter(ixr,izr,iyr,color="red",label="",markersize =4, aspect_ratio=:equal,markerstrokealpha = 0.2,markeralpha = 0.6,markershape = :square,camera = (30, 60),xlim=(0,nelx),zlim=(0,nely),ylim=(-10,10))#,markershape = :square
    p=scatter!(ixg,izg,iyg,color="green",label="",markersize =4, aspect_ratio=:equal,markerstrokealpha = 0.2,markeralpha = 0.6,markershape = :square,camera = (30, 60),xlim=(0,nelx),zlim=(0,nely),ylim=(-10,10))#,markershape = :square
    p=scatter!(ixb,izb,iyb,color="blue",label="",markersize =4, aspect_ratio=:equal,markerstrokealpha = 0.2,markeralpha = 0.6,markershape = :square,camera = (30, 60),xlim=(0,nelx),zlim=(0,nely),ylim=(-10,10))#,markershape = :square
    return p

end

function plotBoundaryConditions3D(nelx,nely,nelz,U,F,freedofs,z=1)
    display("Supports")
    UU=U[:,1]
    UU[freedofs].=1.0
    UUU=reshape(UU,3,nely+1,nelx+1,nelz+1)[1,:,:,z]
    display(heatmap(UUU,aspect_ratio=:equal))
    UUU=reshape(UU,3,nely+1,nelx+1,nelz+1)[2,:,:,z]
    display(heatmap(UUU,aspect_ratio=:equal))
    UUU=reshape(UU,3,nely+1,nelx+1,nelz+1)[3,:,:,z]
    display(heatmap(UUU,aspect_ratio=:equal))


    UU=U[:,2]
    UU[freedofs].=1.0
    UUU=reshape(UU,3,nely+1,nelx+1,nelz+1)[1,:,:,z]
    display(heatmap(UUU,aspect_ratio=:equal))
    UUU=reshape(UU,3,nely+1,nelx+1,nelz+1)[2,:,:,z]
    display(heatmap(UUU,aspect_ratio=:equal))
    UUU=reshape(UU,3,nely+1,nelx+1,nelz+1)[3,:,:,z]
    display(heatmap(UUU,aspect_ratio=:equal))

    display("Forces")
    display("din")
    FF=F[:,1]
    FFF=Array(reshape(FF,3,nely+1,nelx+1,nelz+1)[1,:,:,z])
    display(heatmap(FFF,aspect_ratio=:equal))
    FFF=Array(reshape(FF,3,nely+1,nelx+1,nelz+1)[2,:,:,z])
    display(heatmap(FFF,aspect_ratio=:equal))
    FFF=Array(reshape(FF,3,nely+1,nelx+1,nelz+1)[3,:,:,z])
    display(heatmap(FFF,aspect_ratio=:equal))

    display("dout")
    FF=F[:,2]
    FFF=Array(reshape(FF,3,nely+1,nelx+1,nelz+1)[1,:,:,z])
    display(heatmap(FFF,aspect_ratio=:equal))
    FFF=Array(reshape(FF,3,nely+1,nelx+1,nelz+1)[2,:,:,z])
    display(heatmap(FFF,aspect_ratio=:equal))
    FFF=Array(reshape(FF,3,nely+1,nelx+1,nelz+1)[3,:,:,z])
    display(heatmap(FFF,aspect_ratio=:equal))
end


function topplot3d(xPhys,nelx,nely,nelz)
    ix=[]
    iy=[]
    iz=[]
    for j in 1:Int(nely/1)
        for i in 1:Int(nelx/1)
            for k in 1:Int(nelz/1)
                if(xPhys[j,i,k]>0.9)
                    append!(ix,i)
                    append!(iy,j)
                    append!(iz,k)
                end
            end
        end
    end
    # r = 4.0
    # lim = FRect3D((-4,-4,-4*r),(8,8,8*r))
    return scatter(ix,iz,iy,color="black",label="",markersize =4, aspect_ratio=:equal,markerstrokealpha = 1.0,markeralpha = 1.0,markershape = :square,camera = (30, 60),xlim=(0,nelx),zlim=(0,nelz),ylim=(0,nely))#,markershape = :square
end