% Defintion of Reo Channels and other utilities to design Reo Circuits
% using the pgf (tikz) by Till Tantau (pgf manual for v2.10)
%
% author: Nuno Oliveira
% version: 1.0
%
% This package may be distributed and modified to better suit the user's
% interests. Please, keep the credits to the original author (but you may
% add yours)
% 
% License: LaTeX Project Public License and/or GNU Public License.
%


\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{reotex}[2014/08/13 A Tex way to draw reo circuits!]

%releases:
%2014/08/13 - version 1.1 
%WHAT HAS CHANGED:
% - arrow tips used in drain/spout were corrected. The update to tikz3.0 caused a problem concerning the size of such arrows.

%2012/01/05 - version 1, first published at CTAN.

\RequirePackage{ifthen}
\RequirePackage{tikz}
\RequirePackage{verbatim}
\usetikzlibrary{arrows,%
                decorations.pathmorphing,%
				decorations.shapes,%
				calc%
				}

%makes @ as a normal letter and not as a special latex character...
\makeatletter

%%%%%% VARIABLES %%%%%%%%%%%

\newdimen\rt@thearrowsize
\newdimen\rt@width
\newdimen\rt@xa
\newdimen\rt@xb
\newdimen\rt@xc
\newdimen\rt@ya
\newdimen\rt@yb
\newdimen\rt@yc

\newcommand{\rt@arrowtip}{-stealth}

%%%%%%%%%%%% BASE COMMANDS %%%%%%%%%%%%


\newcommand{\rt@midpoints}[4]{%
	\pgfextractx{\rt@xa}{\pgfpointlineattime{#1}{\pgfpointanchor{#3}{center}}{\pgfpointanchor{#4}{center}}}
	\pgfextracty{\rt@ya}{\pgfpointlineattime{#1}{\pgfpointanchor{#3}{center}}{\pgfpointanchor{#4}{center}}}
	\pgfextractx{\rt@xb}{\pgfpointlineattime{#2}{\pgfpointanchor{#3}{center}}{\pgfpointanchor{#4}{center}}}
	\pgfextracty{\rt@yb}{\pgfpointlineattime{#2}{\pgfpointanchor{#3}{center}}{\pgfpointanchor{#4}{center}}}
}


\newcommand{\rt@middlepoint}[2]{%
	\pgfextractx{\rt@xc}{\pgfpointlineattime{.5}{\pgfpointanchor{#1}{center}}{\pgfpointanchor{#2}{center}}}
	\pgfextracty{\rt@yc}{\pgfpointlineattime{.5}{\pgfpointanchor{#1}{center}}{\pgfpointanchor{#2}{center}}}
}



%%%%%%%% TIKZ DEFINITIONS %%%%%%%%%%%%%%%

\tikzset{cross/.style={path picture={
		\draw[black]
			(path picture bounding box.south east) --
			(path picture bounding box.north west)
			(path picture bounding box.south west) --
			(path picture bounding box.north east);
		}
	}
}


%%%%%%%%%%%%%%%% SYNCHRONOUS CHANNELS %%%%%%%%%%




% Synchronous

\newcommand{\sync}{%
	\@ifstar\sync@noarrow\sync@arrow
}


\newcommand{\sync@arrow}[3]{%
	\draw[\rt@arrowtip] #1 to #3 #2;
}

\newcommand{\sync@noarrow}[3]{%
	\draw #1 to #3 #2;
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Lossy Synchronous

\newcommand{\lossysync}{%
	\@ifstar\lossysync@noarrow\lossysync@arrow
}

\newcommand{\lossysync@arrow}[3]{%
	\draw[\rt@arrowtip,dashed] #1 to #3 #2;
}

\newcommand{\lossysync@noarrow}[3]{%
	\draw[dashed] #1 to #3 #2;
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



% Synchronous Drain
\newcommand{\syncdrain}{%
	\@ifstar \syncdrain@arrow \syncdrain@arrow
}

\newcommand{\syncdrain@arrow}[3]{%
	\draw[inwards-inwards, shorten >=-0.5mm, shorten <=-0.5mm] #1 to #3 #2;
}

%\newcommand{\syncdrain@noarrow}[3]{%
%	\draw[inwards-inwards] #1 to #2 #3;
%}


% Synchronous Spout
\newcommand{\syncspout}{%
	\@ifstar \syncspout@arrow \syncspout@arrow
}
\newcommand{\syncspout@arrow}[3]{%
	\draw[outwards-outwards,shorten >=-0.5mm, shorten <=-0.5mm] #1 to #3 #2;
}
%\newcommand{\syncspout@noarrow}[3]{%
%	\draw[outwards-outwards,] #1 to #2 #3;
%}







%Filter

\newcommand{\filter}{%
	\@ifstar\filter@noarrow\filter@arrow
}

\newcommand{\filter@arrow}[3]{%	
\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@midpoints{0.25}{0.75}{rt@p1}{rt@p2}

\draw[thin] #1 to (\rt@xa,\rt@ya);
\draw decorate [decoration=zigzag] {(\rt@xa,\rt@ya) -- #3 (\rt@xb,\rt@yb)};
\draw[\rt@arrowtip, thin] (\rt@xb,\rt@yb) to #2;

}


\newcommand{\filter@noarrow}[3]{%	
\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@midpoints{0.25}{0.75}{rt@p1}{rt@p2}

\draw[thin] #1 to (\rt@xa,\rt@ya);
\draw decorate [decoration=zigzag] {(\rt@xa,\rt@ya) -- #3 (\rt@xb,\rt@yb)};
\draw[thin] (\rt@xb,\rt@yb) to #2;

}


%%%%%%%%%%%%%%%%%%%%%%%%%%%




%%%%%%%% ASYNCHRONOUS CHANNELS %%%%%%%%%%%%

%Fifo1 (Empty)

\newcommand{\fifoe}{%
	\@ifstar\fifoe@noarrow\fifoe@arrow
}

\newcommand{\fifoe@arrow}[3]{%	
\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@midpoints{0.30}{0.65}{rt@p1}{rt@p2}
\pgfmathsetlength{\rt@width}{sqrt((\rt@xa - \rt@xb)^2 + (\rt@ya - \rt@yb)^2)}
%, shorten <=1mm
\draw[thin] #1 to (\rt@xa,\rt@ya);
\draw decorate[decoration={shape backgrounds, shape=rectangle, shape height=7pt, shape width=\rt@width, shape evenly spread=1}] {(\rt@xa,\rt@ya) --  (\rt@xb,\rt@yb)}; 
\draw[\rt@arrowtip, thin] (\rt@xb,\rt@yb) to #3 #2;	
}


\newcommand{\fifoe@noarrow}[3]{%	
\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@midpoints{0.30}{0.70}{rt@p1}{rt@p2}
\pgfmathsetlength{\rt@width}{sqrt((\rt@xa - \rt@xb)^2 + (\rt@ya - \rt@yb)^2)}

\draw[thin] #1 to (\rt@xa,\rt@ya);
\draw decorate[decoration={shape backgrounds, shape=rectangle, shape height=8pt, shape width=\rt@width, shape evenly spread=1}] {(\rt@xa,\rt@ya) --  (\rt@xb,\rt@yb)}; 
\draw[thin] (\rt@xb,\rt@yb) to #3 #2;	
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%Fifo1 (Full)

\newcommand{\fifof}{%
	\@ifstar\fifof@noarrow\fifof@arrow
}

\newcommand{\fifof@arrow}[3]{%	
\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@midpoints{0.30}{0.65}{rt@p1}{rt@p2}

\pgfmathsetlength{\rt@width}{sqrt((\rt@xa - \rt@xb)^2 + (\rt@ya - \rt@yb)^2)}

\draw[thin] #1 to (\rt@xa,\rt@ya);
\draw decorate[decoration={shape backgrounds, shape=rectangle, shape height=7pt, shape width=\rt@width, shape evenly spread=1}] {(\rt@xa,\rt@ya) -- node[sloped] {$\bullet$} (\rt@xb,\rt@yb)}; 
\draw[\rt@arrowtip, thin] (\rt@xb,\rt@yb) to #3 #2;	
}


\newcommand{\fifof@noarrow}[3]{%	
\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@midpoints{0.30}{0.70}{rt@p1}{rt@p2}

\pgfmathsetlength{\rt@width}{sqrt((\rt@xa - \rt@xb)^2 + (\rt@ya - \rt@yb)^2)}

\draw[thin] #1 to (\rt@xa,\rt@ya);
\draw decorate[decoration={shape backgrounds, shape=rectangle, shape height=7pt, shape width=\rt@width, shape evenly spread=1}] {(\rt@xa,\rt@ya) -- node[sloped] {$\bullet$} (\rt@xb,\rt@yb)}; 
\draw[thin] (\rt@xb,\rt@yb) to #3 #2;	
}



%AsyncDrain
\newcommand{\asyncdrain}{%
	\@ifstar \asyncdrain@arrow \asyncdrain@arrow
}

\newcommand{\asyncdrain@arrow}[3]{%

\coordinate (rt@p1) at ($#1!.47!#2$);
\coordinate (rt@p2) at ($(rt@p1)!6pt!90:#1$);
\coordinate (rt@p3) at ($(rt@p1)!6pt!-90:#1$);

\coordinate (rt@p4) at ($#1!.53!#2$);
\coordinate (rt@p5) at ($(rt@p4)!6pt!90:#1$);
\coordinate (rt@p6) at ($(rt@p4)!6pt!-90:#1$);

\draw[inwards-inwards, thin, shorten >=-0.5mm, shorten <=-0.5mm] 
  #1 to #3 #2;
\draw[thin] (rt@p1) -- (rt@p2);
\draw[thin] (rt@p1) -- (rt@p3);
\draw[thin] (rt@p4) -- (rt@p5);
\draw[thin] (rt@p4) -- (rt@p6);
}



%AsyncSpout

\newcommand{\asyncspout}{%
	\@ifstar \asyncspout@arrow \asyncspout@arrow
}

\newcommand{\asyncspout@arrow}[3]{%	
\coordinate (rt@p1) at ($#1!.47!#2$);
\coordinate (rt@p2) at ($(rt@p1)!6pt!90:#1$);
\coordinate (rt@p3) at ($(rt@p1)!6pt!-90:#1$);

\coordinate (rt@p4) at ($#1!.53!#2$);
\coordinate (rt@p5) at ($(rt@p4)!6pt!90:#1$);
\coordinate (rt@p6) at ($(rt@p4)!6pt!-90:#1$);

\draw[outwards-outwards, thin,shorten >=-0.5mm, shorten <=-0.5mm] 
  #1 to #3 #2;
\draw[thin] (rt@p1) -- (rt@p2);
\draw[thin] (rt@p1) -- (rt@p3);
\draw[thin] (rt@p4) -- (rt@p5);
\draw[thin] (rt@p4) -- (rt@p6);
}

%\newcommand{\asyncspout@noarrow}[3]{%	
%\coordinate (rt@p1) at ($#1!.47!#2$);
%\coordinate (rt@p2) at ($(rt@p1)!6pt!90:#1$);
%\coordinate (rt@p3) at ($(rt@p1)!6pt!-90:#1$);

%\coordinate (rt@p4) at ($#1!.53!#2$);
%\coordinate (rt@p5) at ($(rt@p4)!6pt!90:#1$);
%\coordinate (rt@p6) at ($(rt@p4)!6pt!-90:#1$);

%\draw[outwards-outwards, thin] 
%  #1 to #3 #2;
%\draw[thin] (rt@p1) -- (rt@p2);
%\draw[thin] (rt@p1) -- (rt@p3);
%\draw[thin] (rt@p4) -- (rt@p5);
%\draw[thin] (rt@p4) -- (rt@p6);
%}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%FifoN

\newcommand{\fifon}{%
	\@ifstar\fifon@noarrow\fifon@arrow
}

\newcommand{\fifon@arrow}[3]{%	

\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@midpoints{0.30}{0.65}{rt@p1}{rt@p2}

\pgfmathsetlength{\rt@width}{sqrt((\rt@xa - \rt@xb)^2 + (\rt@ya - \rt@yb)^2)}

\draw[thin] #1 to (\rt@xa,\rt@ya);
\draw decorate[decoration={shape backgrounds, shape=rectangle, shape height=8pt, shape width=\rt@width, shape evenly spread=1}] {(\rt@xa,\rt@ya) -- node[sloped] {\tiny 1$|$...$|$n} (\rt@xb,\rt@yb)}; 
\draw[\rt@arrowtip, thin] (\rt@xb,\rt@yb) to #3 #2;	
}



\newcommand{\fifon@noarrow}[3]{%	

\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@midpoints{0.30}{0.70}{rt@p1}{rt@p2}

\pgfmathsetlength{\rt@width}{sqrt((\rt@xa - \rt@xb)^2 + (\rt@ya - \rt@yb)^2)}

\draw[thin] #1 to (\rt@xa,\rt@ya);
\draw decorate[decoration={shape backgrounds, shape=rectangle, shape height=8pt, shape width=\rt@width, shape evenly spread=1}] {(\rt@xa,\rt@ya) -- node[sloped] {\tiny 1$|$...$|$n} (\rt@xb,\rt@yb)}; 
\draw[thin] (\rt@xb,\rt@yb) to #3 #2;	
}


%%%%%%%%%%%%%%%%%%%%%%%%%



% Shift Fifo n

\newcommand{\shiftfifon}{%
	\@ifstar\shiftfifon@noarrow\shiftfifon@arrow
}

\newcommand{\shiftfifon@arrow}[3]{%	
 
\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@midpoints{0.30}{0.65}{rt@p1}{rt@p2}

\pgfmathsetlength{\rt@width}{sqrt((\rt@xa - \rt@xb)^2 + (\rt@ya - \rt@yb)^2)}

\draw[thin] #1 to (\rt@xa,\rt@ya);
\draw[dashed] decorate[decoration={shape backgrounds, shape=rectangle, shape height=8pt, shape width=\rt@width, shape evenly spread=1}] {(\rt@xa,\rt@ya) -- node[sloped] {\tiny 1$|$...$|$n} (\rt@xb,\rt@yb)}; 
\draw[\rt@arrowtip, thin, dashed] (\rt@xb,\rt@yb) to #3 #2;	
}


\newcommand{\shiftfifon@noarrow}[3]{%	
 
\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@midpoints{0.30}{0.70}{rt@p1}{rt@p2}

\pgfmathsetlength{\rt@width}{sqrt((\rt@xa - \rt@xb)^2 + (\rt@ya - \rt@yb)^2)}

\draw[thin] #1 to (\rt@xa,\rt@ya);
\draw[dashed] decorate[decoration={shape backgrounds, shape=rectangle, shape height=8pt, shape width=\rt@width, shape evenly spread=1}] {(\rt@xa,\rt@ya) -- node[sloped] {\tiny 1$|$...$|$n} (\rt@xb,\rt@yb)}; 
\draw[thin, dashed] (\rt@xb,\rt@yb) to #3 #2;	
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%



% Lossy Fifo n

\newcommand{\lossyfifon}{%
	\@ifstar\lossyfifon@noarrow\lossyfifon@arrow
}


\newcommand{\lossyfifon@arrow}[3]{%	

\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@midpoints{0.30}{0.65}{rt@p1}{rt@p2}

\pgfmathsetlength{\rt@width}{sqrt((\rt@xa - \rt@xb)^2 + (\rt@ya - \rt@yb)^2)}

\draw[dashed, thin] #1 to (\rt@xa,\rt@ya);
\draw[dashed] decorate[decoration={shape backgrounds, shape=rectangle, shape height=8pt, shape width=\rt@width, shape evenly spread=1}] {(\rt@xa,\rt@ya) -- node[sloped] {\tiny 1$|$...$|$n} (\rt@xb,\rt@yb)}; 
\draw[\rt@arrowtip, thin] (\rt@xb,\rt@yb) to #3 #2;	
}


\newcommand{\lossyfifon@noarrow}[3]{%	

\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@midpoints{0.30}{0.70}{rt@p1}{rt@p2}

\pgfmathsetlength{\rt@width}{sqrt((\rt@xa - \rt@xb)^2 + (\rt@ya - \rt@yb)^2)}

\draw[dashed, thin] #1 to (\rt@xa,\rt@ya);
\draw[dashed] decorate[decoration={shape backgrounds, shape=rectangle, shape height=8pt, shape width=\rt@width, shape evenly spread=1}] {(\rt@xa,\rt@ya) -- node[sloped] {\tiny 1$|$...$|$n} (\rt@xb,\rt@yb)}; 
\draw[thin] (\rt@xb,\rt@yb) to #3 #2;	
}




%Timer




\newcommand{\timer}{%
	\@ifstar\timer@noarrow\timer@arrow
}

\newcommand{\timer@arrow}[3]{%	
\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@middlepoint{rt@p1}{rt@p2}
\rt@midpoints{0.4}{0.60}{rt@p1}{rt@p2}
\pgfmathsetlength{\rt@width}{(sqrt((\rt@xa - \rt@xc)^2 + (\rt@ya - \rt@yc)^2))}

\draw[thin] #1 to (\rt@xa,\rt@ya);
\draw (\rt@xc,\rt@yc) circle (\rt@width) ;
\draw[\rt@arrowtip, thin] (\rt@xb,\rt@yb) to #3 #2;	
}


\newcommand{\timer@noarrow}[3]{%	
\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@middlepoint{rt@p1}{rt@p2}
\rt@midpoints{0.4}{0.60}{rt@p1}{rt@p2}
\pgfmathsetlength{\rt@width}{(sqrt((\rt@xa - \rt@xc)^2 + (\rt@ya - \rt@yc)^2))}

\draw[thin] #1 to (\rt@xa,\rt@ya);
\draw (\rt@xc,\rt@yc) circle (\rt@width) ;
\draw[\rt@arrowtip, thin] (\rt@xb,\rt@yb) to #3 #2;	
}












%Auxiliary Channel

\newcommand{\baseasyncchannels@arrow}[4]{%	

\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@midpoints{0.25}{0.75}{rt@p1}{rt@p2}

\draw[thin]% 
	#1 to (\rt@xa,\rt@ya) ;
\draw[white, thin]
	(\rt@xa,\rt@ya) to node[color=black, sloped] {{\small #4}} (\rt@xb,\rt@yb);
\draw[\rt@arrowtip, thin]%
	 (\rt@xb,\rt@yb) to #3 #2;

}


\newcommand{\baseasyncchannels@noarrow}[4]{%	

\node (rt@p1) at #1 {} ;
\node (rt@p2) at #2 {} ;

\rt@midpoints{0.25}{0.75}{rt@p1}{rt@p2}

\draw[thin]% 
	#1 to (\rt@xa,\rt@ya) ;
\draw[white, thin]
	(\rt@xa,\rt@ya) to node[color=black, sloped] {{\small #4}} (\rt@xb,\rt@yb);
\draw[ thin]%
	 (\rt@xb,\rt@yb) to #3 #2;

}


%Ordered

\newcommand{\ordered}{%
	\@ifstar \ordered@noarrow \ordered@arrow
}

\newcommand{\ordered@arrow}[3]{%
  \baseasyncchannels@arrow{#1}{#2}{#3}{ord}
}

\newcommand{\ordered@noarrow}[3]{%
  \baseasyncchannels@noarrow{#1}{#2}{#3}{ord}
}


%%%%%%%%%%%%%%



%Ordered N
\newcommand{\orderedn}{%
	\@ifstar \orderedn@noarrow \orderedn@arrow
}

\newcommand{\orderedn@arrow}[3]{%
  \baseasyncchannels@arrow{#1}{#2}{#3}{ord$_n$}
}

\newcommand{\orderedn@noarrow}[3]{%
  \baseasyncchannels@noarrow{#1}{#2}{#3}{ord$_n$}
}


%%%%%%%%%%%%%%%%%%

%Bag

\newcommand{\bag}{%
	\@ifstar \bag@noarrow \bag@arrow
}

\newcommand{\bag@arrow}[3]{%
  \baseasyncchannels@arrow{#1}{#2}{#3}{({\tiny ...})}
}

\newcommand{\bag@noarrow}[3]{%
  \baseasyncchannels@noarrow{#1}{#2}{#3}{({\tiny ...})}
}


%Bag N

\newcommand{\bagn}{%
	\@ifstar \bagn@noarrow \bagn@arrow
}

\newcommand{\bagn@arrow}[3]{%
  \baseasyncchannels@arrow{#1}{#2}{#3}{({\tiny ...})$_n$}
}

\newcommand{\bagn@noarrow}[3]{%
  \baseasyncchannels@noarrow{#1}{#2}{#3}{({\tiny ...})$_n$}
}


%Set

\newcommand{\set}{%
	\@ifstar \set@noarrow \set@arrow
}

\newcommand{\set@arrow}[3]{%
  \baseasyncchannels@arrow{#1}{#2}{#3}{\{{\tiny ...}\}}
}

\newcommand{\set@noarrow}[3]{%
  \baseasyncchannels@noarrow{#1}{#2}{#3}{\{{\tiny ...}\}}
}


%Set N

\newcommand{\setn}{%
	\@ifstar \setn@noarrow \setn@arrow
}

\newcommand{\setn@arrow}[3]{%
  \baseasyncchannels@arrow{#1}{#2}{#3}{\{{\tiny ...}\}$_n$}
}

\newcommand{\setn@noarrow}[3]{%
  \baseasyncchannels@noarrow{#1}{#2}{#3}{\{{\tiny ...}\}$_n$}
}


%Dealy Set

\newcommand{\delayset}{%
	\@ifstar \delayset@noarrow \delayset@arrow
}

\newcommand{\delayset@arrow}[3]{%
  \baseasyncchannels@arrow{#1}{#2}{#3}{DSet}
}

\newcommand{\delayset@noarrow}[3]{%
  \baseasyncchannels@noarrow{#1}{#2}{#3}{DSet}
}


%Delay Set N
\newcommand{\delaysetn}{%
	\@ifstar \delaysetn@noarrow \delaysetn@arrow
}

\newcommand{\delaysetn@arrow}[3]{%
  \baseasyncchannels@arrow{#1}{#2}{#3}{DSet$_n$}
}

\newcommand{\delaysetn@noarrow}[3]{%
  \baseasyncchannels@noarrow{#1}{#2}{#3}{DSet$_n$}
}



%Keyed Set

\newcommand{\keyedset}{%
	\@ifstar \keyedset@noarrow \keyedset@arrow
}

\newcommand{\keyedset@arrow}[3]{%
  \baseasyncchannels@arrow{#1}{#2}{#3}{KSet}
}


\newcommand{\keyedset@noarrow}[3]{%
  \baseasyncchannels@noarrow{#1}{#2}{#3}{KSet}
}

%Keyed Set N

\newcommand{\keyedsetn}{%
	\@ifstar \keyedsetn@noarrow \keyedsetn@arrow
}

\newcommand{\keyedsetn@arrow}[3]{%
  \baseasyncchannels@arrow{#1}{#2}{#3}{KSet$_n$}
}

\newcommand{\keyedsetn@noarrow}[3]{%
  \baseasyncchannels@noarrow{#1}{#2}{#3}{KSet$_n$}
}





%%%%%%%%%%%%%% L shaped %%%%%%%%%%%%%%%%%%%
%Generic\Lhannel*?[<option>]?{<channel name>}{<start>}{<target>}{<height>}{<orientation>}{<polarization>}{<node>}
%Example: \Lhannel[??]{fifoe}{(0,0)}{(2,0.5)}{0.5}{v}{+}{node {a}}
%|-a>

\newcommand{\Lchannel}{
	\@ifstar \Lchannel@arrow@part \Lchannel@arrow@channel 
}

\newcommand{\Lchannel@arrow@channel}[8][solid]{	
	
	\ifthenelse{\equal{#6}{v} \and \equal{#7}{+}}{
		\rt@pin{(rt@pinlc1)}{($ #3 + (0,#5) $)}	
	}
	{
		\ifthenelse{\equal{#6}{v} \and \equal{#7}{-}}{
			\rt@pin{(rt@pinlc1)}{($ #3 - (0,#5) $)}
		}
		{
			\ifthenelse{\equal{#6}{h} \and \equal{#7}{+}}{
				\rt@pin{(rt@pinlc1)}{($ #3 + (#5,0) $)}
			}
			{
				\ifthenelse{\equal{#6}{h} \and \equal{#7}{-}}{
					\rt@pin{(rt@pinlc1)}{($ #3 - (#5,0) $)}
				}
				{
					\PackageError{reotex}{Expecting: v/h and +/- as 5th and 6th arguments (resp.)}{The 5th argument must be one of v or h, for vertical or horizontal orientation.\MessageBreak	The 6th argument must be one of + or - for a positive or negative polarisation.}
				} % ERROR STATE
			}
		}
	}
	\rt@cp[#1]{#3}{(rt@pinlc1)}
	\csname #2\endcsname{(rt@pinlc1)}{#4}{#8}
	%csname expands the argument name x to \x. Care should be taken: there should not exist a space after the name to expand as command. The arguments of the command should come after the \endcsname
}
	
	
	
\newcommand{\Lchannel@arrow@part}[8][solid]{
	\ifthenelse{\equal{#6}{v} \and \equal{#7}{+}}{
		\rt@pin{(rt@pinlc2)}{($ #3 + (0,#5) $)}	
	}
	{
		\ifthenelse{\equal{#6}{v} \and \equal{#7}{-}}{
			\rt@pin{(rt@pinlc2)}{($ #3 - (0,#5) $)}
		}
		{
			\ifthenelse{\equal{#6}{h} \and \equal{#7}{+}}{
				\rt@pin{(rt@pinlc2)}{($ #3 + (#5,0) $)}
			}
			{
				\ifthenelse{\equal{#6}{h} \and \equal{#7}{-}}{
					\rt@pin{(rt@pinlc2)}{($ #3 - (#5,0) $)}
				}
				{
					\PackageError{reotex}{Expecting: v/h and +/- as 5th and 6th arguments (resp.)}{The 5th argument must be one of v or h, for vertical or horizontal orientation.\MessageBreak	The 6th argument must be one of + or - for a positive or negative polarisation.}
				} % ERROR STATE
			}
		}
	}
	\rt@cp*[#1]{(rt@pinlc2)}{#3}
	\csname #2\endcsname *{#4}{(rt@pinlc2)}{#8}
}






%%%%%%%%%%%%%% U shaped %%%%%%%%%%%%%%%%%%%
%Generic\Uhannel*?[<option>]?{<channel name>}{<start>}{<target>}{<height>}{<orientation>}{<polarization>}{<label>}
%Example: \Uchannel[dashed]{lossysync}{(0,0)}{(2,0.5)}{0.5}{v}{+}{node {a}}
%|---a>

%\newcommand{\Uchannel}{
%	\@ifstar \Uchannel@arrow@target \Uchannel@arrow@target 
%}

\newcommand{\Uchannel}[8][solid]{	
	
	\ifthenelse{\equal{#6}{v} \and \equal{#7}{+}}{
		\rt@pin{(rt@pinuc1)}{($ #3 + (0,#5) $)}
		\rt@pin{(rt@pinuc2)}{($ #4 + (0,#5) $)}	
	}
	{
	\ifthenelse{\equal{#6}{v} \and \equal{#7}{-}}{
		\rt@pin{(rt@pinuc1)}{($ #3 - (0,#5) $)}
		\rt@pin{(rt@pinuc2)}{($ #4 - (0,#5) $)}
	}
	{
		\ifthenelse{\equal{#6}{h} \and \equal{#7}{+}}{
			\rt@pin{(rt@pinuc1)}{($ #3 + (#5,0) $)}
			\rt@pin{(rt@pinuc2)}{($ #4 + (#5,0) $)}
		}
		{
			\ifthenelse{\equal{#6}{h} \and \equal{#7}{-}}{
				\rt@pin{(rt@pinuc1)}{($ #3 - (#5,0) $)}
				\rt@pin{(rt@pinuc2)}{($ #4 - (#5,0) $)}
			}
			{
				\PackageError{reotex}{Expecting: v/h and +/- as 5th and 6th arguments (resp.)}{The 5th argument must be one of v or h, for vertical or horizontal orientation.\MessageBreak	The 6th argument must be one of + or - for a positive or negative polarisation.}
			} % ERROR STATE
		}
	}
}
%
\ifthenelse{\equal{#2}{shiftfifon}}{
	\rt@cp*[dashed]{(rt@pinuc2)}{#4}
	\rt@cp[solid]{#3}{(rt@pinuc1)}
}
{
	\ifthenelse{\equal{#2}{lossyfifon}}{
		\rt@cp[dashed]{#3}{(rt@pinuc1)}		
		\rt@cp*[solid]{(rt@pinuc2)}{#4}
	}
	{
		\rt@cp[#1]{#3}{(rt@pinuc1)}		
		\rt@cp*[#1]{(rt@pinuc2)}{#4}
	}
}
\csname #2\endcsname *{(rt@pinuc1)}{(rt@pinuc2)}{#8}

}
















%%%%%%%%%% ARROWS %%%%%%%%%%%%%

%outwards
\pgfarrowsdeclare{outwards}{outwards}
{
  \rt@thearrowsize=1.4pt
  \advance\rt@thearrowsize by .5\pgflinewidth
  \pgfarrowsleftextend{-.5\pgflinewidth}
  \pgfarrowsrightextend{3\rt@thearrowsize+.5\pgflinewidth}
}
{
  \advance\rt@thearrowsize by .5\pgflinewidth
  \pgfpathmoveto{\pgfpoint{-4.5\rt@thearrowsize}{1.2\rt@thearrowsize}}
  \pgfpathlineto{\pgfpoint{-4.5\rt@thearrowsize}{-1.2\rt@thearrowsize}}
  \pgfpathlineto{\pgfpoint{-2.0\rt@thearrowsize}{0pt}}
  \pgfpathlineto{\pgfpoint{-4.5\rt@thearrowsize}{1.2\rt@thearrowsize}}
  \pgfusepathqfill
}








%inwards
\pgfarrowsdeclare{inwards}{inwards}
{
  \rt@thearrowsize=1.4pt
  \advance\rt@thearrowsize by .5\pgflinewidth
  \pgfarrowsleftextend{-.5\pgflinewidth}
  \pgfarrowsrightextend{3\rt@thearrowsize+.5\pgflinewidth}
}
{
  \advance\rt@thearrowsize by .5\pgflinewidth
  \pgfpathmoveto{\pgfpoint{-2\rt@thearrowsize}{1.2\rt@thearrowsize}}
  \pgfpathlineto{\pgfpoint{-2\rt@thearrowsize}{-1.2\rt@thearrowsize}}
  \pgfpathlineto{\pgfpoint{-4.5\rt@thearrowsize}{0pt}}
  \pgfpathlineto{\pgfpoint{-2\rt@thearrowsize}{1.2\rt@thearrowsize}}
  \pgfusepathqfill
}






%%%%%%%%%% NODES %%%%%%%%%%%%%%%%%%


\newcommand{\rt@pin}[2]{%
	\node[outer sep=-1.3mm] #1 at #2 {} ;
}


\newcommand{\ionode}[3]{%
	\node[outer sep=-0.15mm] #1 at #2 {} ;
	\draw[fill=white, minimum size=2pt] #2 circle (2.7pt) #3 ;
}


\newcommand{\mixednode}[3]{%
    \node[outer sep=-0.2mm] #1 at #2 {} ;
    \draw[fill=gray!20!black, minimum size=2.9pt] #2 circle (2.2pt) #3 ;
}


\newcommand{\reader}[3]{%
    \node[outer sep=0mm] #1 at #2 {} ;
    \draw[fill=white, minimum size=3pt] ($#2 + (-0.15,0.15)$) rectangle ($ #2 + (0.15,-0.15) $) #3 ;
}

\newcommand{\writer}[3]{%
    \node[outer sep=0mm] #1 at #2 {} ;
    \draw[fill=black, minimum size=3pt] ($#2 + (-0.15,0.15)$) rectangle ($ #2 + (0.15,-0.15) $) #3 ;
}



\newcommand{\xrouter}[3]{%
	\node[outer sep=0.35mm] #1 at #2 {} ;
	\draw[cross, fill=white, minimum size=2.3pt] #2 circle (5pt) #3 ;
}



%%%%%%%%%%%% UTILITIES %%%%%%%%%%%%%%%%%%%%

\newcommand{\rt@cp}{%
	\@ifstar \rt@cp@arrow \rt@cp@noarrow
}

\newcommand{\rt@cp@noarrow}[3][solid]{%
	\draw[#1] #2 to #3;
}

\newcommand{\rt@cp@arrow}[3][solid]{%
	\draw[\rt@arrowtip ,#1] #2 to #3;
}




%% added on: 2012/07/27 %%

%%%%%%%%%%% COMPONENTS (BLACK BOXES)%%%%%
% arg1: corner 1
% arg2: corner 2
% arg3: text node
\newcommand{\component}[3]{%
	\draw[rounded corners=1ex] #1 rectangle #2 #3;
}


\makeatother


\endinput