\ProvidesPackage{UpConvRelu}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This Block can draw Upsampling+convRelu combination
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tikzset{UpConvRelu/.pic={\tikzset{/block/.cd,#1}	 	

% Upsampling layer
\draw [deconv,fill=\upcolor]
(0,\cubescale*\cubey/2  , \cubescale*\cubez/2) coordinate (d) 
-- ++(-1*\cubescale,0,0) coordinate (a) 
-- ++(0,-\cubescale*\cubey,0) coordinate (b) 
edge coordinate[pos=1] (g) ++(0,0,-\cubescale*\cubez) 
-- ++(1*\cubescale,0,0) coordinate (c) 
edge coordinate[pos=1] (f) ++(0,0,-\cubescale*\cubez) 
-- cycle


(d) 
-- ++(0,0,-\cubescale*\cubez) coordinate (e) 
edge (f)
-- ++(-\cubescale*1,0,0) coordinate (h) 
edge (g) 
-- (a) 
-- cycle

(g) edge (f)
;

\fill[deconv] (e)--(f)--(g)--(h)--cycle; 
\fill[deconv] (c)--(f)--(g)--(b)--cycle;
\fill[deconv] (a)--(b)--(g)--(h)--cycle;

\path (d) -- ++(-1*\cubescale,0,-\cubescale*\cubez/2) coordinate (\cubeblockname-upnorth);
\path (c) -- ++(-1*\cubescale,0,-\cubescale*\cubez/2) coordinate (\cubeblockname-upsouth);

%% conv layers  	
\foreach \x  [ count=\i,evaluate=\x as \numfilters using {array({\cubexlabel},\i-1)}, evaluate=\x as \k using {\x+\prev}, remember=\k as \prev (initially 0)] in \cubex
{   			
\draw [relu,fill=\fillcolor,draw=\fillcolor]  
    (\k*\cubescale ,\cubescale*\cubey/2  , \cubescale*\cubez/2) coordinate (d) 
    -- ++(-\cubescale*\x/3,0,0) coordinate (a1) 
    -- ++(0,-\cubescale*\cubey,0) coordinate (b1) 
    -- ++(\cubescale*\x/3,0,0) coordinate (c) -- cycle
         	     
    (d) 
    -- (a1) 
    -- ++(0,0,-\cubescale*\cubez) coordinate (h1) 
    -- ++(+\cubescale*\x/3,0,0) coordinate (e) -- cycle 
    ; 
\draw [conv,fill=\fillcolor]   	       
    (d) 
    -- ++(-\cubescale*\x,0,0) coordinate (a) 
    -- ++(0,-\cubescale*\cubey,0) coordinate (b) 
    edge coordinate [pos=1] (g) ++(0,0,-\cubescale*\cubez) 
    -- (c) -- cycle    
    
    (d) 
    -- (e) 
    edge coordinate[pos=1] (f) ++(0,-\cubescale*\cubey,0) 
    
    (f) 
    edge (g)  	     
    
    (d) 
    -- (a) 
    -- ++(0,0,-\cubescale*\cubez) coordinate (h) 
    edge (g) 
    -- (e) -- cycle;     
% labelling each conv layer with number of filters in each layer	
\path [every edge/.append style={pic actions}]
    (b) edge ["\numfilters"',midway] (b -| c);		
\xdef\SecondLastLayerOriginX{\k} %making value of \k available after the loop as well 
}
\draw[relu,fill=\fillcolor,draw={rgb:black,1}]
    (d) 
    -- (e) 
    -- ++(0,-\cubescale*\cubey,0) coordinate (f) 
    -- (c) -- cycle;	



	       
% Labelling the block
\path [every edge/.append style={pic actions}]
    % name or size of depth dimension 		
    (c) edge ["\small\cubezlabel"',pos=0,text width=14*\cubescale*\cubez,text centered,sloped] (f)    		
    %(d) edge ["\bf\cubeblockname"',pos=0,text width=14*\cubescale*\cubez,text centered,sloped] (e)    		

    % name or size of height dimension
    (0,0,\cubez*\cubescale/2) coordinate (o) edge ["\cubeylabel",midway] (o |- o)   	  
    % Block name
    %(0,-\cubey*\cubescale/2,+\cubez*\cubescale/2) coordinate  (o)+(0,-20pt) coordinate (o1) edge ["\textcolor{black}{ \bf \cubeblocklabel}"',midway,white] (o1 -| c) ;
    (0,-\cubey*\cubescale/2,+\cubez*\cubescale/2) coordinate  (d)+(0,-20pt) coordinate (o1) edge ["\textcolor{black}{ \bf \cubeblocklabel}"',midway,white] (o1 -| c) ;

\draw (\SecondLastLayerOriginX*\cubescale, 0,0) coordinate (\cubeblockname-east);
\draw (0,0,0) coordinate (\cubeblockname-west);
\path (\cubeblockname-west) -- (\cubeblockname-east) coordinate[pos=0.5] (\cubeblockname-anchor);
\path (\cubeblockname-anchor) -- ++(0,\cubescale*\cubey/2,0) coordinate (\cubeblockname-north);
\path (\cubeblockname-anchor) -- ++(0,-\cubescale*\cubey/2,0) coordinate (\cubeblockname-south);
},
/block/.search also={/tikz},
/block/.cd,
width/.store        in=\cubex,
height/.store       in=\cubey,
depth/.store        in=\cubez,
scale/.store        in=\cubescale,
numFilters/.store   in=\cubexlabel,
ylabel/.store       in=\cubeylabel,
zlabel/.store       in=\cubezlabel,
blocklabel/.store   in=\cubeblocklabel,
blockname/.store    in=\cubeblockname,
fillcolor/.store    in=\fillcolor,
upcolor/.store      in=\upcolor,
fillcolor = {rgb:yellow,5;red,5;white,5},
upcolor   = {rgb:blue,5;green,2.5;white,5},
width=2,
height=13,
depth=15,
scale=.2,
numFilters=,
ylabel=,
zlabel=,
blocklabel=,
blockname=,
}
