%% symbol library for TikZ track schematics % % Copyright (c) 2018 - 2022, 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.vehicles.code.tex}% % %%%%%%%%%%%%%%% % Requirements %%%%%%%%%%%%%%% \RequirePackage{tikz,etoolbox}% \usetikzlibrary{calc}% % \ifdeflength{\objectlength}{}{% Not defined, so define it! \newlength{\objectlength}% }% \setlength{\objectlength}{4cm}% % %%%%%%%%%%%%%%% % 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, automatic OR undefined operation mode=none,% DEFAULT /tikz/operation/.forward to=/tikz/trackschematic/operation mode,% %% length length/.store in=\objectlength,% default length 4cm /tikz/length/.forward to=/tikz/trackschematic/length,% %% label shift label/.store in=\labelcoord,% (coord) shift label=(none),% DEFAULT /tikz/shift label/.forward to=/tikz/trackschematic/shift label,% }% % %%%%%%%%%%%%%%% % commands %%%%%%%%%%%%%%% % %% command: parked vehicles \newcommand\parkedvehicles{}% just for safety \def\parkedvehicles[#1]#2(#3)#4(#5){% \parkedvehicles[options] at (coord) label (name); \pic[parked,operation=undefined,forward,#1] at (#3) {vehicles={#2/#4/#5}}% symbol }% %% command: shunting movements \newcommand\shunting{}% just for safety \def\shunting[#1]#2(#3)#4(#5){% \shunting[options] at (coord) label (name); \pic[shunting mode,operation=undefined,#1] at (#3) {vehicles={#2/#4/#5}}% symbol }% %% command: train run \newcommand\train{}% just for safety \def\train[#1]#2(#3)#4(#5){% \train[options] at (coord) label (name); \pic[train mode,operation=undefined,#1] at (#3) {vehicles={#2/#4/#5}}% symbol }% % %%%%%%%%%%%%%%% % tikz keys %%%%%%%%%%%%%%% \pgfkeys{% /tikz/trackschematic/vehicles/.is family,% /tikz/trackschematic/vehicles/.cd,% %% parked vehicles parked/.value forbidden,% parked/.code={\settoggle{is_parked}{true}},% /tikz/parked/.forward to=/tikz/trackschematic/vehicles/parked,% %% shunting mode shunting mode/.value forbidden,% shunting mode/.code={\settoggle{in_shunting_mode}{true}},% /tikz/shunting mode/.forward to=/tikz/trackschematic/vehicles/shunting mode,% %% shunting movement movement/.value forbidden,% movement/.code={\settoggle{is_shunting}{true}},% /tikz/movement/.forward to=/tikz/trackschematic/vehicles/movement,% %% train mode train mode/.value forbidden,% train mode/.code={\settoggle{in_train_mode}{true}},% /tikz/train mode/.forward to=/tikz/trackschematic/vehicles/train mode,% %% ghost train ghost train/.value forbidden,% ghost train/.code={\settoggle{train_is_a_ghost}{true}},% /tikz/ghost/.forward to=/tikz/trackschematic/vehicles/ghost train,% %% train run run/.store in=\trainrun,% slow, normal, fast OR none run=none,% DEFAULT /tikz/run/.forward to=/tikz/trackschematic/vehicles/run,% %% label label align/.store in=\labelalign,% left, right OR center label align=center,% DEFAULT /tikz/label align/.forward to=/tikz/trackschematic/vehicles/label align,% %% train bend bend right at/.store in=\bendrightcoord,% (coord) bend right at=(none),% DEFAULT /tikz/bend right at/.forward to=/tikz/trackschematic/vehicles/bend right at,% bend left at/.store in=\bendleftcoord,% (coord) bend left at=(none),% DEFAULT /tikz/bend left at/.forward to=/tikz/trackschematic/vehicles/bend left at,% }% % options \newtoggle{is_parked}\settoggle{is_parked}{false}% \newtoggle{in_shunting_mode}\settoggle{in_shunting_mode}{false}% \newtoggle{is_shunting}\settoggle{is_shunting}{false}% \newtoggle{in_train_mode}\settoggle{in_train_mode}{false}% \newtoggle{train_is_a_ghost}\settoggle{train_is_a_ghost}{false}% \newtoggle{single_bend}\settoggle{single_bend}{false}% \newtoggle{double_bend}\settoggle{double_bend}{false}% % %%%%%%%%%%%%%%% % symbol definition %%%%%%%%%%%%%%% % \tikzset{% pics/vehicles/.default=,% pics/vehicles/.style args={#1/#2/#3}{code={% %% %%%%%%%[steps]%%%%%% %% %% 0. setup settings %% 1. calculate coordinates %% - calculation of vehicle coordinates: %% %% rear bend front bend %% (ts-b-r1) (ts-b-f1) %% (ts-v-r1)•__________•_______•__________•(ts-v-f1) %% | \ %% | label•(ts-v-l) •(ts-v-f) front tip %% |__________ _______ __________ / %% (ts-v-r2)• • • •(ts-v-f2) %% (ts-b-r2) (ts-b-f2) %% %% - the base coord is depend on usage %% for parked: (ts-v-l) %% else: (ts-v-f) %% %% 2. set style for line and background %% 3. draw arrows for speed indication %% 4. draw vehicle shape %% 5. draw operation mode %% 6. draw label %% %%%%%%%%%%%%%%%%%%%% %% 0. 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} %% bend setup % determine number of bends and find front and rear bend from left to right \ifdefstring{\bendleftcoord}{(none)}{}{% NOT default \settoggle{single_bend}{true}% \path \bendleftcoord;\pgfgetlastxy{\bendX}{\bendY}% absolut coord \pgfmathsetmacro\bendfactor{1}% }% \ifdefstring{\bendrightcoord}{(none)}{}{% NOT default \iftoggle{single_bend}{% \settoggle{double_bend}{true}% \settoggle{single_bend}{false}% \path \bendrightcoord;\pgfgetlastxy{\bendrightX}{\bendrightY} \ifdimcomp{\bendrightX}{>}{\bendX}{% which one comes first? \ifdefstring{\face}{forward}{% \pgfmathsetmacro\frontBendfactor{1}% \pgfmathsetmacro\rearBendfactor{-1}% \pgfmathsetlengthmacro\frontBendX{\bendrightX}% \pgfmathsetlengthmacro\rearBendX{\bendX}% \pgfmathsetlengthmacro\rearBendY{\bendY}% }{% face backward \pgfmathsetmacro\frontBendfactor{-1}% \pgfmathsetmacro\rearBendfactor{1}% \pgfmathsetlengthmacro\frontBendX{\bendX}% \pgfmathsetlengthmacro\rearBendX{\bendrightX}% \pgfmathsetlengthmacro\rearBendY{\bendrightY}% }% }{% {\bendrightX}{<}{\bendleftX}% \ifdefstring{\face}{forward}{% \pgfmathsetmacro\frontBendfactor{-1}% \pgfmathsetmacro\rearBendfactor{1}% \pgfmathsetlengthmacro\frontBendX{\bendX}% \pgfmathsetlengthmacro\rearBendX{\bendrightX}% \pgfmathsetlengthmacro\rearBendY{\bendrightY}% }{% face backward \pgfmathsetmacro\frontBendfactor{1}% \pgfmathsetmacro\rearBendfactor{-1}% \pgfmathsetlengthmacro\frontBendX{\bendrightX}% \pgfmathsetlengthmacro\rearBendX{\bendX}% \pgfmathsetlengthmacro\rearBendY{\bendY}% }% }% }{% NOT double bend \settoggle{single_bend}{true}% \path \bendrightcoord;\pgfgetlastxy{\bendX}{\bendY}% absolut coord \pgfmathsetmacro\bendfactor{-1}% }% }% %%%%%%%%%%%%%%%%%%%% %% 1. coordinate calculations %% basic coords for corners \iftoggle{is_parked}{% \coordinate (ts-v-f1) at ($ 0.5*(\objectlength,0) + (0, 0.3)$);% \coordinate (ts-v-f2) at ($ 0.5*(\objectlength,0) + (0,-0.3)$);% \coordinate (ts-v-r1) at ($-0.5*(\objectlength,0) + (0, 0.3)$);% \coordinate (ts-v-r2) at ($-0.5*(\objectlength,0) + (0,-0.3)$);% }{% \coordinate (ts-v-f1) at ($\facefactor*(-0.3,0) + (0, 0.3)$);% \coordinate (ts-v-f2) at ($\facefactor*(-0.3,0) + (0,-0.3)$);% \coordinate (ts-v-r1) at ($\facefactor*(-\objectlength,0) + (0, 0.3)$);% \coordinate (ts-v-r2) at ($\facefactor*(-\objectlength,0) + (0,-0.3)$);% }% %% front tip for traveling direction \iftoggle{is_parked}{% \coordinate (ts-v-f) at ($ 0.5*(\objectlength,0) + (0, 0 )$);% }{% \coordinate (ts-v-f) at (0,0);% }% %% bend - DEFAULT: no bend \coordinate (ts-b-f1) at (ts-v-f1);% \coordinate (ts-b-f2) at (ts-v-f2);% \coordinate (ts-b-r1) at (ts-v-r1);% \coordinate (ts-b-r2) at (ts-v-r2);% % \iftoggle{single_bend}{% % \bendX is a absolut coordinate % make them relative to base \path (0,0);\pgfgetlastxy{\baseX}{\baseY}% absolut coord % calc bend position \coordinate (ts-b-r1) at ($(-\baseX,0) + (\bendX, 0.3) + \facefactor*\bendfactor*( 0.1,0)$);% \coordinate (ts-b-r2) at ($(-\baseX,0) + (\bendX,-0.3) + \facefactor*\bendfactor*(-0.1,0)$);% % calc length from bend to end with factor ~0.7071 at 45 degree to a projected length \pgfmathsetlengthmacro\bendlength{(\facefactor*\objectlength - \baseX + \bendX)*0.7071}% \coordinate (ts-v-r1) at ($(ts-b-r1) + (-\bendlength,0) + \facefactor*\bendfactor*( 0.1,\bendlength) + (0,-0.1)$);% \coordinate (ts-v-r2) at ($(ts-b-r2) + (-\bendlength,0) + \facefactor*\bendfactor*(-0.1,\bendlength) + (0, 0.1)$);% }{}% \iftoggle{double_bend}{% % \bendX and \bendY are absolut coords % make them relative to base \path (0,0);\pgfgetlastxy{\baseX}{\baseY}% absolut coord % calc bend position \coordinate (ts-b-f1) at ($(-\baseX,0) + (\frontBendX, 0.3) + \facefactor*\frontBendfactor*(-0.1,0)$);% \coordinate (ts-b-f2) at ($(-\baseX,0) + (\frontBendX,-0.3) + \facefactor*\frontBendfactor*( 0.1,0)$);% \coordinate (ts-b-r1) at ($(-\baseX,0) + (\rearBendX, 0.3) + (0,-\baseY) + (0,\rearBendY) + \facefactor*\rearBendfactor*( 0.1,0)$);% \coordinate (ts-b-r2) at ($(-\baseX,0) + (\rearBendX,-0.3) + (0,-\baseY) + (0,\rearBendY) + \facefactor*\rearBendfactor*(-0.1,0)$);% % calc length from bend to end with factor ~1.4142 at 45 degree to a projected length \pgfmathsetlengthmacro\bendlength{\facefactor*\objectlength - (\frontBendX - \rearBendX)*1.4142 - (\baseX - \frontBendX)}% \coordinate (ts-v-r1) at ($(ts-b-r1) + (-\bendlength,0) + \facefactor*\rearBendfactor*(-0.1,0)$);% \coordinate (ts-v-r2) at ($(ts-b-r2) + (-\bendlength,0) + \facefactor*\rearBendfactor*( 0.1,0)$);% }{}% %% %%%%%%%%%%%%%%%%%%%% %% 2. vehicle style \iftoggle{in_train_mode}{% train mode \iftoggle{train_is_a_ghost}{% ghost train \tikzset{every path/.style={draw=\foreground, line width=0.8pt, dashed}};% }{% normal train \tikzset{every path/.style={draw=\foreground, line width=1pt, fill=\background}};% }% }{% shunting mode \tikzset{every path/.style={draw=\foreground, double, double distance=0.5mm, line width=0.3pt, fill=\background}};% }% %% %%%%%%%%%%%%%%%%%%%% %% 3. arrow shapes %% (order before vehicle shape due to background fill!) \iftoggle{is_shunting}{% arrow for shunting \iftoggle{in_shunting_mode}{% shunting mode \fill[draw=none,fill=\background] ($\facefactor*(-0.3, 0.3)$) --% ($\facefactor*(-0.1, 0.3)$) --% ($\facefactor*( 0.2, 0 )$) --% ($\facefactor*(-0.1,-0.3)$) --% ($\facefactor*(-0.3,-0.3)$) -- cycle;% \background fill \path[line cap=rect,fill=none] ($\facefactor*(-0.1, 0.3)$) --% ($\facefactor*( 0.2, 0 )$) --% ($\facefactor*(-0.1,-0.3)$);% arrow shape }{% train mode \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/vehicle/movement}{movement can only be combined with shunting}}% }% }{}% end \iftoggle{shunting_movement} \ifdefstring{\trainrun}{none}{}{% DEFAULT no arrow for train \iftoggle{in_train_mode}{% train mode \ifdefstring{\trainrun}{slow}{% \fill[draw=none,fill=\background] ($\facefactor*(-0.3, 0.3)$) --% ($\facefactor*(-0.1, 0.3)$) --% ($\facefactor*( 0.2, 0 )$) --% ($\facefactor*(-0.1,-0.3)$) --% ($\facefactor*(-0.3,-0.3)$) -- cycle;% \background fill \path[fill=none] ($\facefactor*(-0.15, 0.3)$) --% ($\facefactor*(-0.1 , 0.3)$) --% ($\facefactor*( 0.2 , 0 )$) --% ($\facefactor*(-0.1 ,-0.3)$) --% ($\facefactor*(-0.15,-0.3)$);% arrow shape }{% \ifdefstring{\trainrun}{normal}{% \fill[draw=none,fill=\background] ($\facefactor*(-0.3, 0.3)$) --% ($\facefactor*( 0.1, 0.3)$) --% ($\facefactor*( 0.4, 0 )$) --% ($\facefactor*( 0.1,-0.3)$) --% ($\facefactor*(-0.3,-0.3)$) -- cycle;% \background fill \path[fill=none] ($\facefactor*(-0.15, 0.3)$) --% ($\facefactor*(-0.1 , 0.3)$) --% ($\facefactor*( 0.2 , 0 )$) --% ($\facefactor*(-0.1 ,-0.3)$) --% ($\facefactor*(-0.15,-0.3)$);% arrow shape part 1 \path[fill=none] ($\facefactor*( 0.05, 0.3)$) --% ($\facefactor*( 0.1 , 0.3)$) --% ($\facefactor*( 0.4 , 0 )$) --% ($\facefactor*( 0.1 ,-0.3)$) --% ($\facefactor*( 0.05,-0.3)$);% arrow shape part 2 % }{% \ifdefstring{\trainrun}{fast}{% \fill[draw=none,fill=\background] ($\facefactor*(-0.3, 0.3)$) --% ($\facefactor*( 0.3, 0.3)$) --% ($\facefactor*( 0.6, 0 )$) --% ($\facefactor*( 0.3,-0.3)$) --% ($\facefactor*(-0.3,-0.3)$) -- cycle;% \background fill \path[fill=none] ($\facefactor*(-0.15, 0.3)$) --% ($\facefactor*(-0.1 , 0.3)$) --% ($\facefactor*( 0.2 , 0 )$) --% ($\facefactor*(-0.1 ,-0.3)$) --% ($\facefactor*(-0.15,-0.3)$);% arrow shape part 1 \path[fill=none] ($\facefactor*( 0.05, 0.3)$) --% ($\facefactor*( 0.1 , 0.3)$) --% ($\facefactor*( 0.4 , 0 )$) --% ($\facefactor*( 0.1 ,-0.3)$) --% ($\facefactor*( 0.05,-0.3)$);% arrow shape part 2 \path[fill=none] ($\facefactor*( 0.25, 0.3)$) --% ($\facefactor*( 0.3 , 0.3)$) --% ($\facefactor*( 0.6 , 0 )$) --% ($\facefactor*( 0.3 ,-0.3)$) --% ($\facefactor*( 0.25,-0.3)$);% arrow shape part 3 % }{%error message \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/vehicles/run}{“normal“, “slow“, “fast“ OR “none“ as key required}}% }% }% }% }{% shunting mode \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/vehicle/run}{run can only be combined with train}}% }% end \iftoggle{in_train_mode} }% end \ifdefstring{\trainrun}{none} % end arrow shapes %% %%%%%%%%%%%%%%%%%%%% %% 4. vehicle shape \path (ts-v-f)% -- (ts-v-f1) -- (ts-b-f1) -- (ts-b-r1) -- (ts-v-r1)% -- (ts-v-r2) -- (ts-b-r2) -- (ts-b-f2) -- (ts-v-f2)% -- cycle;% vehicle shape %% %%%%%%%%%%%%%%%%%%%% %% 5. operation mode \iftoggle{is_parked}{}{% vehicle NOT parked \ifdefstring{\operationmode}{manual}{% \path[draw=none, fill=\foreground] ($\facefactor*(-0.4,0)$) circle (0.15);% dot }{ \ifdefstring{\operationmode}{automatic}{% \path[draw=none, fill=\foreground] ($\facefactor*(-0.15,0)$) -- ++($\facefactor*(-0.15,0) +(0,0.15)$) -- ++(0,-0.3) -- cycle;% triangle }{% \ifdefstring{\operationmode}{undefined}{}{% \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/operation}{“manual“, “automatic“ OR “undefined“ as key required}}% }% }% end automatically_operated }% end manually_operated }% %% %%%%%%%%%%%%%%%%%%%% %% 6. label \iftoggle{is_parked}{% \coordinate (ts-v-l) at (0,0);% DEFAULT center }{% \coordinate (ts-v-l) at ($\facefactor*-0.5*(\objectlength,0)$);% DEFAULT center }% \iftoggle{train_is_a_ghost}{% ghost train \coordinate (ts-v-l) at ($(ts-v-l) + (0,0.16)$);% }{}% \ifdefstring{\operationmode}{manual}{% \coordinate (ts-v-l) at ($(ts-v-l) + \facefactor*(-0.25,0)$);% }{}% \ifdefstring{\operationmode}{automatic}{% \coordinate (ts-v-l) at ($(ts-v-l) + \facefactor*(-0.15,0)$);% }{}% \ifdefstring{\labelcoord}{(none)}{}{% initialize if NOT default \path let \p1=\labelcoord in coordinate (ts-v-l) at ($(ts-v-l)+(\x1,\y1)$);% }% \ifdefstring{\labelcontent}{}{}{% label NOT empty \ifdefstring{\labelalign}{center}{% DEFAULT label align center \def\labelanchor{center} }{% label align NOT default \ifdefstring{\labelalign}{left}{% label align left \def\labelanchor{east} }{% \ifdefstring{\labelalign}{right}{% label align left \def\labelanchor{west} }{% NOT center, left, or right \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/label align}{“center“, “left“ OR “right“ as key required}}% }% \ifdefstring{\labelalign}{right} }% \ifdefstring{\labelalign}{left} }% \ifdefstring{\labelalign}{center} \node[text=\foreground,anchor=\labelanchor,align=\labelalign] at (ts-v-l) {\footnotesize \labelcontent};% label }% }},% END pics/vehicles/.style args={#1/#2/#3} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % symbology entry symbology_parked_vehicles/.pic = {% \secondarytrack (0,0) -- (6,0);% \parkedvehicles[] at (3,0) label ();% },% % symbology entry symbology_train_shunt_mode/.pic = {% \maintrack (0,0) -- (6,0);% \shunting[forward] at (5,0) label ();% },% % symbology entry symbology_train_shunting/.pic = {% \maintrack (0,0) -- (6,0);% \shunting[movement,forward] at (5,0) label ();% },% % symbology entry symbology_train/.pic = {% \maintrack (0,0) -- (6,0);% \train[forward] at (5,0) label ();% },% % symbology entry symbology_train_moving_slow/.pic = {% \maintrack (0,0) -- (6,0);% \train[run=slow,forward] at (5,0) label ();% },% % symbology entry symbology_train_moving/.pic = {% \maintrack (0,0) -- (6,0);% \train[run=normal,forward] at (5,0) label ();% },% % symbology entry symbology_train_moving_fast/.pic = {% \maintrack (0,0) -- (6,0);% \train[run=fast,forward] at (5,0) label ();% },% % symbology entry symbology_train_ghost/.pic = {% \maintrack (0,0) -- (6,0);% \train[ghost,forward] at (5,0) label ();% },% % symbology entry symbology_train_drive_automatic/.pic = {% \maintrack (0,0) -- (6,0);% \train[operation=automatic,forward] at (5,0) label ();% },% % symbology entry symbology_train_drive_human/.pic = {% \maintrack (0,0) -- (6,0);% \train[operation=manual,forward] at (5,0) label ();% },% }% % %%%%%%%%%%%%%%% \endinput% %