%% symbol library for TikZ track schematics
%
% Copyright (c) 2018 - 2020, Martin Scheidt (ISC license)
%
% Permission to use, copy, modify, and/or distribute this file for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
%
\ProvidesFileRCS{tikzlibrarytrackschematic.topology.code.tex}%
%
%%%%%%%%%%%%%%%
% Requirements
%%%%%%%%%%%%%%%
\RequirePackage{tikz,etoolbox,lmodern}%
\usetikzlibrary{calc,patterns}%
%
% https://tex.stackexchange.com/questions/56353/extract-x-y-coordinate-of-an-arbitrary-point-on-curve-in-tikz
\providecommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax%
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y}%
}%
%
%%%%%%%%%%%%%%%
% tikz keys for multiple use
%%%%%%%%%%%%%%%
\pgfkeys{%
  /tikz/trackschematic/.is family,%
  /tikz/trackschematic/.cd,%
  %% color foreground
  foreground/.store in=\foreground,%
  foreground=black,% DEFAULT
  /tikz/foreground/.forward to=/tikz/trackschematic/foreground,%
  %% color background
  background/.store in=\background,%
  background=white,% DEFAULT
  /tikz/background/.forward to=/tikz/trackschematic/background,%
  %% face
  face/.value required,% forward OR backward
  face/.store in=\face,%
  /tikz/face/.forward to=/tikz/trackschematic/face,%
  /tikz/forward/.code={\pgfkeys{/tikz/trackschematic/face=forward}},%
  /tikz/backward/.code={\pgfkeys{/tikz/trackschematic/face=backward}},%
  /tikz/bidirectional/.code={\pgfkeys{/tikz/trackschematic/face=bidirectional}},%
  %% operation
  operation mode/.store in=\operationmode,% manual, remote OR none
  operation mode=none,% DEFAULT
  /tikz/operation/.forward to=/tikz/trackschematic/operation mode,%
  %% label
  shift label/.store in=\labelcoord,% (coord)
  shift label=(none),% DEFAULT
  /tikz/shift label/.forward to=/tikz/trackschematic/shift label,%
}%
%
\pgfkeys{%
  /tikz/trackschematic/topology/.is family,%
  /tikz/trackschematic/topology/.cd,%
  %% branch
  branch/.value required,% left OR right
  branch/.store in=\branch,% left OR right
  /tikz/branch/.forward to=/tikz/trackschematic/topology/branch,%
  %% fouling point
  fouling point/.value forbidden,%
  fouling point/.code={\settoggle{fouling_point}{true}},%
  /tikz/fouling point/.forward to=/tikz/trackschematic/topology/fouling point,%
  %% points
  points/.store in=\points,% moving, left, right OR none
  points=none,% DEFAULT
  /tikz/points/.forward to=/tikz/trackschematic/topology/points,%
}%
% options
\newtoggle{fouling_point}\settoggle{fouling_point}{false}%
%
%%%%%%%%%%%%%%%
% symbol track
%%%%%%%%%%%%%%%
%
% tracks
\newcommand\maintrack{}% just for safety
\def\maintrack{\path[draw,MainTrack]}% \maintrack (coord1) -- (coord2);
\newcommand\secondarytrack{}% just for safety
\def\secondarytrack{\path[draw,SecondaryTrack]}% \secondarytrack (coord1) -- (coord2);
\newcommand\sidetrack{}% just for safety
\def\sidetrack{\path[draw,SecondaryTrack]}% alias for \secondarytrack
%
\tikzset{MainTrack/.style={line width=2pt,\foreground}}%
\tikzset{SecondaryTrack/.style={line width=0.7pt,\foreground}}%
%
%%%%%%%%%%%%%%%
% symbol track number
%%%%%%%%%%%%%%%
%
%% command
\newcommand\tracklabel{}% just for safety
\def\tracklabel#1(#2)#3(#4){% \tracklabel at (coord) label (name);
  \pic at (#2) {track_label={#1/#3/#4}}% symbol
}%
%% tikz keys
% \pgfkeys{%
%   /tikz/trackschematic/.is family,%
%   /tikz/trackschematic/.cd,%
% }%
%% symbol definition
\tikzset{%
  pics/track_label/.default=,%
  pics/track_label/.style args={#1/#2/#3}{code={%
    %% settings
    \def\coordcommand{#1} % beware of leading and tailing spaces!
    \def\labelcommand{#2} % beware of leading and tailing spaces!
    \def\labelcontent{#3}
    %% label
    \ifdefstring{\labelcontent}{}{}{% label NOT empty
      \tikzset{every node/.style={fill=\background,text=\foreground}};%
      \coordinate (ts-tl-l) at (0,0);%
      \ifdefstring{\labelcoord}{(none)}{% default coord
      }{% initialize if NOT default
        \gettikzxy{\labelcoord}{\labelcoordX}{\labelcoordY}%
        \coordinate (ts-tl-l) at ($(ts-tl-l)+(\labelcoordX,\labelcoordY)$);%
      }%
      \node at (ts-tl-l) {\labelcontent};%
    }%
  }},%
}%
%
%%%%%%%%%%%%%%%
% symbol buffer stop
%%%%%%%%%%%%%%%
%
% command
\newcommand\bufferstop{}% just for safety
\def\bufferstop[#1]#2(#3){% \bufferstop[options] at (coord);
  \pic[#1] at (#3) {bufferstop={#2}}% symbol
}%
\newlength{\friction}\setlength{\friction}{0.5cm}
% tikz keys
\pgfkeys{%
  /tikz/trackschematic/topology/bufferstop/.is family,%
  /tikz/trackschematic/topology/bufferstop/.cd,%
  %% friction
  friction/.store in=\friction,% length OR none
  friction=none,% length OR none
  /tikz/friction/.forward to=/tikz/trackschematic/topology/bufferstop/friction,%
}%
% symbol definition
\tikzset{%
  pics/bufferstop/.default=,%
  pics/bufferstop/.style args={#1}{code={%
    %% settings
    \def\coordcommand{#1} % beware of leading and tailing spaces!
    %% face setup
    \ifdefstring{\face}{forward}{% face
      \pgfmathsetmacro{\facefactor}{1}%
    }{%
      \ifdefstring{\face}{backward}{% face
        \pgfmathsetmacro{\facefactor}{-1}%
      }{% error message
        \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/face}{“forward“ OR “backward“ as key required}}%
      }%
    }% end \ifdefstring{\face}
    %% symbol
    \ifdefstring{\friction}{none}{% friction
      \path[draw=\foreground, line width=1pt] ($\facefactor*(-0.1,0) + (0,0.2)$) --  ++($\facefactor*( 0.1,0)$) -- ++(0,-0.4) -- ++ ($\facefactor*(-0.1,0)$); % bufferstop marker
    }{
      \path[draw=\foreground, line width=1pt] ($\facefactor*(-\friction,0) + \facefactor*( 0.1,0) + (0,0.2)$) --  ++($\facefactor*(-0.1,0)$) -- ++(0,-0.4) -- ++ ($\facefactor*( 0.1,0)$); % bufferstop marker
      \fill[\foreground] (0,0) circle (0.06);% track closure indicator
    }
  }},%
}%
%
%%%%%%%%%%%%%%%
% symbol track closure
%%%%%%%%%%%%%%%
%
%% command
\newcommand\trackclosure{}% just for safety
\def\trackclosure#1(#2){% \trackclosure at (coord);
  \pic at (#2) {track_closure={#1}}% symbol
}%
\tikzset{%
  pics/track_closure/.default=,%
  pics/track_closure/.style args={#1}{code={%
    %% settings
    \def\coordcommand{#1} % beware of leading and tailing spaces!
    %%
    \fill[\foreground] (0,0) circle (0.06);% track closure indicator
    % \path[fill=\foreground] (0.04,0.02) -- (0.04,-0.02) -- (0.02,-0.04) -- (-0.02,-0.04) -- (-0.04,-0.02) -- (-0.04,0.02) -- (-0.02,0.04) -- (0.02,0.04) -- cycle;% label
  }},% end pics/track_closure/.style args={#1}
}%
%
%%%%%%%%%%%%%%%
% symbol turnout
%%%%%%%%%%%%%%%
%
% command
\newcommand\turnout{}% just for safety
\def\turnout[#1]#2(#3)#4(#5){% \turnout[options] at (coord) label (name);
  \pic[operation=remote,#1] at (#3) {turnout={#2/#4/#5}}% symbol
}%
% tikz keys
\pgfkeys{%
  /tikz/trackschematic/topology/turnout/.is family,%
  /tikz/trackschematic/topology/turnout/.cd,%
  %% points
  points/.store in=\points,% moving, left, right OR none
  points=none,% moving, left, right OR none
  /tikz/points/.forward to=/tikz/trackschematic/topology/turnout/points,%
}%
% symbol definition
\tikzset{%
  pics/turnout/.default=,%
  pics/turnout/.style args={#1/#2/#3}{code={%
    %% settings
    \def\coordcommand{#1}% beware of leading and tailing spaces!
    \def\labelcommand{#2}% beware of leading and tailing spaces!
    \def\labelcontent{#3}%
    %% face setup
    \ifdefstring{\face}{forward}{% face
      \pgfmathsetmacro{\facefactor}{1}%
    }{%
      \ifdefstring{\face}{backward}{% face
        \pgfmathsetmacro{\facefactor}{-1}%
      }{% error message
        \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/face}{“forward“ OR “backward“ as key required}}%
      }%
    }% end \ifdefstring{\face}
    %% branch setup
    \ifdefstring{\branch}{left}{% branch
      \pgfmathsetmacro{\branchfactor}{1}%
    }{%
      \ifdefstring{\branch}{right}{% branch
        \pgfmathsetmacro{\branchfactor}{-1}%
      }{% error message
        \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/branch}{“left“ OR “right“ as key required}}%
      }%
    }% end \ifdefstring{\branch}
    %% turnout marker
    \ifdefstring{\operationmode}{manual}{% operation=manual
      \ifdefstring{\face}{forward}{%
        \ifdefstring{\branch}{left}{% branch
          \def\patterntype{north west lines}%
        }{%
          \def\patterntype{north east lines}%
        }%
      }{%
        \ifdefstring{\branch}{right}{% branch
          \def\patterntype{north west lines}%
        }{%
          \def\patterntype{north east lines}%
        }%
      }%
      \path[draw=\foreground,pattern=\patterntype, pattern color=\foreground] (0,0) -- ++($\facefactor*(0.4,0)$) -- ++($\branchfactor*(0,0.4)$) -- cycle;% turnout marker
    }{%
      \ifdefstring{\operationmode}{remote}{% operation=remote
        \path[fill=\foreground] (0,0) -- ++($\facefactor*(0.4,0)$) -- ++($\branchfactor*(0,0.4)$) -- cycle;% turnout marker
      }{%
        \ifdefstring{\operationmode}{none}{
          \path[draw=\foreground] (0,0) -- ++($\facefactor*(0.4,0)$) -- ++($\branchfactor*(0,0.4)$) -- cycle;% turnout marker
        }{%
          \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/operation}{“manual“, “remote“ OR “none“ as key required}}%
        }%
      }
    }%
    %
    %% label
    \ifdefstring{\labelcontent}{}{}{% label NOT empty
      \tikzset{every node/.style={text=\foreground}};%
      \coordinate (ts-y-l) at ($\branchfactor*(0,-8pt)$);%
      \ifdefstring{\labelcoord}{(none)}{}{% initialize if NOT default
        \gettikzxy{\labelcoord}{\labelcoordX}{\labelcoordY}%
        \coordinate (ts-y-l) at ($(ts-y-l)+(\labelcoordX,\labelcoordY)$);%
      }%
      \node at (ts-y-l) {\footnotesize \labelcontent};%
    }%
    %
    %% fouling point indicator
    \iftoggle{fouling_point}{%
      \path[draw=\foreground] ($\facefactor*(0.7,0)$) -- ++($\branchfactor*(0,0.7)$);% fouling point indicator
    }{}%
    %
    %% points
    \ifdefstring{\points}{left}{% points left
      \ifdefstring{\branch}{left}{%
        \path[draw=\foreground,line width=1.5pt] ($\facefactor*(-0.035,0)+(0,0.1)$) -- ++($\facefactor*(0.2,0)+(0,0.2)$);%
      }{%
        \path[draw=\foreground,line width=1.5pt] ($\facefactor*( 0.035,0)+(0,0.1)$) -- ++($\facefactor*( 0.265,0  )$);%
      }%
    }{%
      \ifdefstring{\points}{right}{% points right
        \ifdefstring{\branch}{left}{%
          \path[draw=\foreground,line width=1.5pt] ($\facefactor*( 0.035,0)+(0,-0.1)$) -- ++($\facefactor*( 0.265,0  )$);%
        }{%
          \path[draw=\foreground,line width=1.5pt] ($\facefactor*(-0.035,0)+(0,-0.1)$) -- ++($\facefactor*(0.2,0)+(0,-0.2)$);%
        }%
      }{%
        \ifdefstring{\points}{moving}{% moving points
          \fill[\foreground] ($\facefactor*(0.075,0) + \branchfactor*(0,-0.1 )$) circle (0.05);% points indicator left
          \fill[\foreground] ($\facefactor*(0.225,0) + \branchfactor*(0,-0.1 )$) circle (0.05);% points indicator left
          \fill[\foreground] ($\facefactor*(0.015,0) + \branchfactor*(0, 0.15)$) circle (0.05);% points indicator right
          \fill[\foreground] ($\facefactor*(0.115,0) + \branchfactor*(0, 0.25)$) circle (0.05);% points indicator right
        }{% error message
          \ifdefstring{\points}{none}{%
          }{%
            \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/turnout/points}{“left“, “right“ OR “moving“ as key required}}%
          }%
        }%
      }%
    }% end \ifdefstring{\points}
    %
  }},% end pics/turnout/.style args={#1/#2/#3}
}%
%
%%%%%%%%%%%%%%%
% symbol slip switch turnout - using parts of turnout
%%%%%%%%%%%%%%%
%
\newcommand\slipturnout{}% just for safety
\def\slipturnout[#1]#2(#3)#4(#5)(#6){% \slipturnout[options] at (coord) label (name1)(name2);
  \pic[operation=remote,slip=double,#1] at (#3) {slipturnout={#2/#4/#5/#6}}% symbol
}%
% tikz keys
\pgfkeys{%
  /tikz/trackschematic/topology/slipturnout/.is family,%
  /tikz/trackschematic/topology/slipturnout/.cd,%
  %% points
  forward points/.store in=\forwardpoints,% moving, left, right OR none
  forward points=none,% moving, left, right OR none
  /tikz/forward points/.forward to=/tikz/trackschematic/topology/slipturnout/forward points,%
  backward points/.store in=\backwardpoints,% moving, left, right OR none
  backward points=none,% moving, left, right OR none
  /tikz/backward points/.forward to=/tikz/trackschematic/topology/slipturnout/backward points,%
  %% slips
  slip/.store in=\slip,% double, left, right OR none
  slip=none,% double, left, right OR none
  /tikz/slip/.forward to=/tikz/trackschematic/topology/slipturnout/slip,%
}%
% symbol definition
\tikzset{%
  pics/slipturnout/.default=,%
  pics/slipturnout/.style args={#1/#2/#3/#4}{code={%
    %% settings
    \def\coordcommand{#1}% beware of leading and tailing spaces!
    \def\labelcommand{#2}% beware of leading and tailing spaces!
    \def\labelcontentleft{#3}%
    \def\labelcontentright{#4}%
    %% branch setup
    \ifdefstring{\branch}{left}{% branch
      \pgfmathsetmacro{\branchfactor}{1}%
    }{%
      \ifdefstring{\branch}{right}{% branch
        \pgfmathsetmacro{\branchfactor}{-1}%
      }{% error message
        \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/branch}{“left“ OR “right“ as key required}}%
      }%
    }% end \ifdefstring{\branch}
    %% turnout marker
    \ifdefstring{\operationmode}{manual}{% operation=manual
      \ifdefstring{\branch}{left}{% branch
        \def\patterntype{north west lines}%
      }{%
        \def\patterntype{north east lines}%
      }%
      \path[draw,pattern=\patterntype, pattern color=\foreground] (0,0) -- ++($( 0.4,0)$) -- ++($\branchfactor*(0, 0.4)$) -- cycle;% turnout marker
      \path[draw,pattern=\patterntype, pattern color=\foreground] (0,0) -- ++($(-0.4,0)$) -- ++($\branchfactor*(0,-0.4)$) -- cycle;% turnout marker
    }{% operated automaticly
      \ifdefstring{\operationmode}{remote}{% operation=remote
        \path[fill=\foreground] (0,0) -- ++($( 0.4,0)$) -- ++($\branchfactor*(0, 0.4)$) -- cycle;% turnout marker
        \path[fill=\foreground] (0,0) -- ++($(-0.4,0)$) -- ++($\branchfactor*(0,-0.4)$) -- cycle;% turnout marker
      }{%
        \ifdefstring{\operationmode}{none}{}{%
          \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/operation}{“manual“, “remote“ OR “none“ as key required}}%
        }%
      }%
    }%
    %
    %% label
    \tikzset{every node/.style={text=\foreground}};%
    \ifdefstring{\labelcontentleft}{}{}{% label NOT empty
      \coordinate (ts-sy-l1) at ($(2pt,0) + \branchfactor*(0, 10pt)$);%
      \ifdefstring{\labelcoord}{(none)}{}{% initialize if NOT default
        \gettikzxy{\labelcoord}{\labelcoordX}{\labelcoordY}%
        \coordinate (ts-sy-l1) at ($(ts-sy-l1)+(\labelcoordX,\labelcoordY)$);%
      }%
      \node[left]  at (ts-sy-l1) {\footnotesize \labelcontentleft};%
    }%
    \ifdefstring{\labelcontentright}{}{}{% label NOT empty
      \coordinate (ts-sy-l2) at ($(2pt,0) + \branchfactor*(0,-10pt)$);%
      \ifdefstring{\labelcoord}{(none)}{}{% initialize if NOT default
        \gettikzxy{\labelcoord}{\labelcoordX}{\labelcoordY}%
        \coordinate (ts-sy-l2) at ($(ts-sy-l2)-(\labelcoordX,\labelcoordY)$);%
      }%
      \node[right] at (ts-sy-l2) {\footnotesize \labelcontentright};%
    }%
    %
    %% fouling point indicator
    \iftoggle{fouling_point}{%
      \path[draw=\foreground] ( 0.7,0) -- ++($\branchfactor*(0, 0.7)$);% fouling point indicator
      \path[draw=\foreground] (-0.7,0) -- ++($\branchfactor*(0,-0.7)$);% fouling point indicator
    }{}%
    %
    %% points
    \ifdefstring{\forwardpoints}{left}{% points left
      \ifdefstring{\branch}{left}{%
        \path[draw=\foreground,line width=1.5pt] (-0.035,0.1) -- ++(0.2,0.2);%
      }{%
        \path[draw=\foreground,line width=1.5pt] ( 0.035,0.1) -- ++(0.265,0);%
      }%
    }{%
      \ifdefstring{\forwardpoints}{right}{% points right
        \ifdefstring{\branch}{left}{%
          \path[draw=\foreground,line width=1.5pt] ( 0.035,-0.1) -- ++(0.265,0);
        }{%
          \path[draw=\foreground,line width=1.5pt] (-0.035,-0.1) -- ++(0.2,-0.2);%
        }%
      }{%
        \ifdefstring{\forwardpoints}{moving}{% moving points
          \fill[\foreground] ($(0.075,0) + \branchfactor*(0,-0.1 )$) circle (0.05);% points indicator left
          \fill[\foreground] ($(0.225,0) + \branchfactor*(0,-0.1 )$) circle (0.05);% points indicator left
          \fill[\foreground] ($(0.015,0) + \branchfactor*(0, 0.15)$) circle (0.05);% points indicator right
          \fill[\foreground] ($(0.115,0) + \branchfactor*(0, 0.25)$) circle (0.05);% points indicator right
        }{% error message
          \ifdefstring{\forwardpoints}{none}{%
          }{%
            \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/slipturnout/forward points}{“left“, “right“ OR “moving“ as key required}}%
          }%
        }%
      }%
    }% end of  \ifdefstring{\forwardpoints}
    \ifdefstring{\backwardpoints}{left}{% points left
      \ifdefstring{\branch}{left}{%
        \path[draw=\foreground,line width=1.5pt] (0.035,-0.1) -- ++(-0.2,-0.2);%
      }{%
        \path[draw=\foreground,line width=1.5pt] (-0.035,-0.1) -- ++(-0.265,0);%
      }%
    }{%
      \ifdefstring{\backwardpoints}{right}{% points right
        \ifdefstring{\branch}{left}{%
          \path[draw=\foreground,line width=1.5pt] (-0.035,0.1) -- ++(-0.265,0);
        }{%
          \path[draw=\foreground,line width=1.5pt] (0.035,0.1) -- ++(-0.2,0.2);%
        }%
      }{%
        \ifdefstring{\backwardpoints}{moving}{% moving points
          \fill[\foreground] ($(-0.075,0) + \branchfactor*(0, 0.1 )$) circle (0.05);% points indicator left
          \fill[\foreground] ($(-0.225,0) + \branchfactor*(0, 0.1 )$) circle (0.05);% points indicator left
          \fill[\foreground] ($(-0.015,0) + \branchfactor*(0,-0.15)$) circle (0.05);% points indicator right
          \fill[\foreground] ($(-0.115,0) + \branchfactor*(0,-0.25)$) circle (0.05);% points indicator right
        }{% error message
          \ifdefstring{\backwardpoints}{none}{%
          }{%
            \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/slipturnout/forward points}{“left“, “right“ OR “moving“ as key required}}%
          }%
        }%
      }%
    }% end of  \ifdefstring{\backwardpoints}
    %
    %% slips
    \ifdefstring{\slip}{double}{% slip
      \path[draw=\foreground,line width=0.75pt] ($\branchfactor*(-0.4,0) + (0, 0.1)$) --  ($\branchfactor*( 0.3,0) + (0, 0.4)$);% slip
      \path[draw=\foreground,line width=0.75pt] ($\branchfactor*( 0.4,0) + (0,-0.1)$) --  ($\branchfactor*(-0.3,0) + (0,-0.4)$);% slip
    }{%
      \ifdefstring{\slip}{left}{%
        \path[draw=\foreground,line width=0.75pt] ($\branchfactor*(-0.4,0) + (0,0.1)$) --  ($\branchfactor*(0.3,0) + (0,0.4)$);% slip
      }{%
        \ifdefstring{\slip}{right}{%
          \path[draw=\foreground,line width=0.75pt] ($\branchfactor*(0.4,0) + (0,-0.1)$) --  ($\branchfactor*(-0.3,0) + (0,-0.4)$);% slip
        }{%
          \ifdefstring{\slip}{none}{%
            %
          }{%error message
            \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/slipturnout/slip}{“double“, “left“, “right“ OR “none“ as key required}}%
          }%
        }%
      }%
    }%
    %
  }},% end of  pics/slipturnout/.style args={#1/#2/#3/#4}{
}%
%
%%%%%%%%%%%%%%%
% symbol crossing - using parts of turnout
%%%%%%%%%%%%%%%
%
\newcommand\crossing{}% just for safety
\def\crossing[#1]#2(#3)#4(#5){% \crossing[options] at (coord) label (name);
  \pic[#1] at (#3) {crossing={#2/#4/#5}}% symbol
}%
% symbol definition
\tikzset{%
  pics/crossing/.default=,%
  pics/crossing/.style args={#1/#2/#3}{code={%
    %% settings
    \def\coordcommand{#1}% beware of leading and tailing spaces!
    \def\labelcommand{#2}% beware of leading and tailing spaces!
    \def\labelcontent{#3}%
    %% face setup
    %% branch setup
    \ifdefstring{\branch}{left}{% branch
      \pgfmathsetmacro{\branchfactor}{1}%
    }{%
      \ifdefstring{\branch}{right}{% branch
        \pgfmathsetmacro{\branchfactor}{-1}%
      }{% error message
        \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/branch}{“left“ OR “right“ as key required}}%
      }%
    }%
    %
    %% crossing marker
    \path[draw=\foreground] (0,0) -- ++( 0.4,0) -- ++($\branchfactor*(0, 0.4)$) -- cycle;% turnout marker
    \path[draw=\foreground] (0,0) -- ++(-0.4,0) -- ++($\branchfactor*(0,-0.4)$) -- cycle;% turnout marker
    %
    %% label
    \ifdefstring{\labelcontent}{}{}{% label NOT empty
      \tikzset{every node/.style={text=\foreground}};%
      \coordinate (ts-x-l) at ($(2pt,0) + \branchfactor*(0,-9pt)$);%
      \ifdefstring{\labelcoord}{(none)}{}{% initialize if NOT default
        \gettikzxy{\labelcoord}{\labelcoordX}{\labelcoordY}%
        \coordinate (ts-x-l) at ($(ts-x-l)+(\labelcoordX,\labelcoordY)$);%
      }%
      \node[right] at (ts-x-l) {\footnotesize \labelcontent};%
    }%
    %
    %% fouling point indicator
    \iftoggle{fouling_point}{%
      \path[draw=\foreground] ( 0.7,0) -- ++($\branchfactor*(0, 0.7)$);% fouling point indicator
      \path[draw=\foreground] (-0.7,0) -- ++($\branchfactor*(0,-0.7)$);% fouling point indicator
    }{}%
    %
  }},% end of  pics/crossing/.style args={#1/#2/#3}{
}%
%
%%%%%%%%%%%%%%%
% symbol derailer
%%%%%%%%%%%%%%%
%
% command
\newcommand\derailer{}% just for safety
\def\derailer[#1]#2(#3)#4(#5){% \derailer[options] at (coord) label (name);
  \pic[#1] at (#3) {derailer={#2/#4/#5}}% symbol
}%
% symbol definition
\tikzset{%
  pics/derailer/.default=,%
  pics/derailer/.style args={#1/#2/#3}{code={%
    %% settings
    \def\coordcommand{#1}% beware of leading and tailing spaces!
    \def\labelcommand{#2}% beware of leading and tailing spaces!
    \def\labelcontent{#3}%
    %% face setup
    \ifdefstring{\face}{forward}{% face
      \pgfmathsetmacro{\facefactor}{1}%
    }{%
      \ifdefstring{\face}{backward}{% face
        \pgfmathsetmacro{\facefactor}{-1}%
      }{% error message
        \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/face}{“forward“ OR “backward“ as key required}}%
      }%
    }% end \ifdefstring{\face}
    %% branch setup
    \ifdefstring{\branch}{left}{% branch
      \ifdefstring{\face}{forward}{%
        \pgfmathsetmacro{\branchfactor}{1}%
      }{%
        \pgfmathsetmacro{\branchfactor}{-1}%
      }%
    }{%
      \ifdefstring{\branch}{right}{% branch
        \ifdefstring{\face}{forward}{%
          \pgfmathsetmacro{\branchfactor}{-1}%
        }{%
          \pgfmathsetmacro{\branchfactor}{1}%
        }%
      }{% error message
        \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/branch}{“left“ OR “right“ as key required}}%
      }%
    }% end \ifdefstring{\branch}
    %
    %% symbol
    \path[draw=\foreground, line width=1pt] (0,0.1) -- ++(0,-0.2);% derailer marker
    \path[draw=\foreground,->,>=latex,line width=1pt,dashed] (0,0) -- ++($\facefactor*(0.4,0) + \branchfactor*(0,0.4)$);% derailer arrow
    %
    %% label
    \ifdefstring{\labelcontent}{}{}{% label NOT empty
      \tikzset{every node/.style={text=\foreground}};%
      \coordinate (ts-dr-l) at ($\branchfactor*(0,-10pt)$);%
      \ifdefstring{\labelcoord}{(none)}{}{% initialize if NOT default
        \gettikzxy{\labelcoord}{\labelcoordX}{\labelcoordY}%
        \coordinate (ts-dr-l) at ($(ts-dr-l)+(\labelcoordX,\labelcoordY)$);%
      }%
      \node[right] at (ts-dr-l) {\footnotesize \labelcontent};%
    }%
    %
  }},%
}%
%
%%%%%%%%%%%%%%%
\endinput%
%