\tikzmath
{
  function noTrans(\x){return \x;};
  function trans(\x, \m, \d){
    \a=\m/\d;
    \b=0-ln(\a);
    \r=max( (ln(\x+\a)+\b) / (ln(\m+\a)+\b), 0-1);
    return (\r>10)*round(\r) + (\r<=10)*\r;
    %return \x;
  };
  function invtrans(\x, \m, \d){
    \a=\m/\d;
    \b=0-ln(\a);
    \r=max( (exp(\x*(ln(\m+\a)+\b)-\b)-\a, 0-1);
    return (\r>10)*round(\r) + (\r<=10)*\r;
    %return \x;
  };
}

%Min-max-avg barb
\newcommand{\minMaxPlot}[5]{
\pgfplotsset{every axis plot post/.append style={bar width=0.25cm, very thick, mark=square,mark options={color=mainOrange, yscale=0.07}}}
\addplot+[#1, very thick, forget plot,only marks, mark=square, mark options={color=mainOrange, yscale=0.07}, xshift=#3] 
plot[very thick, error bars/.cd, y dir=plus, y explicit]
table[x expr=\coordindex,y expr={\thisrow{avg}},y error expr={trans(\thisrow{max},#4,#5)-trans(\thisrow{avg},#4,#5)}] {#2};
\addplot+[#1, very thick, only marks, mark=square, mark options={color=mainOrange, yscale=0.07},xticklabels=\empty, xshift=#3] 
  plot[very thick, error bars/.cd, y dir=minus, y explicit]
  table[x expr=\coordindex,y expr={\thisrow{avg}},y error expr={trans(\thisrow{avg},#4,#5)-trans(\thisrow{min},#4,#5)}] {#2};
}

%Simple bar 
\newcommand{\barPlot}[5]{
\addplot+[#1, very thick, forget plot,only marks,mark=square, mark options={color=#1, yscale=0.07, xscale=0.5}, xshift=#3] 
  plot[very thick, error bars/.cd, y dir=plus, y explicit]
  table[x expr={\coordindex},y expr={0},y error expr={trans(\thisrow{avg},#4,#5)}] {#2};
}

%Large chart 
\newcommand{\chart}[8]{
\begin{figure*}[!htbp]
\StrCount{#2}{,}[\numofelem]
\StrBehind[\numofelem]{#2}{,}[\yLast]

\ifnum60=#8\relax
    \def \firstOffset {0.162\textwidth}
\else
    \def \firstOffset {0.13\textwidth}
\fi

\def \shift {2.5}
\def \doubleShift {7.5}
\def \avgLegend {\addlegendimage{draw=mainOrange,line width=2pt, xscale=0.3, style={xshift=20}}}
\def \legendLabels {avg, linear static, linear moving, jagged static, jagged moving}
\ifnum0=#3\relax
    \def \columnSize {0.3\textwidth}
    \def \bottomText{
        \begin{tabular}{ 
        wr{0.13\textwidth}  wc{\columnSize}  wc{0.13\textwidth} wc{\columnSize}}
        background: & image & & black \\
         & \multicolumn{3}{c}{$Axis$}
         \end{tabular} 
    }
    \def \plots{
        \minMaxPlot{mainBlue}{\distortionsLS}{0-\doubleShift}{\yLast}{#8}
        \minMaxPlot{mainLightBlue}{\distortionsLM}{0-\shift}{\yLast}{#8}
        \minMaxPlot{mainRed}{\distortionsJS}{\shift}{\yLast}{#8}
        \minMaxPlot{mainLightRed}{\distortionsJM}{\doubleShift}{\yLast}{#8}
    }
\else
    \def \avgLegend {}
    \def \columnSize {0.11\textwidth}
    \def \firstColumnSize {0.11\textwidth}
    \def \spaceSize {0.04\textwidth}
    \def \bottomText{
        \begin{tabular}{ 
        wr{\firstOffset}  wc{\firstColumnSize}  wc{\spaceSize} wc{\columnSize}  wc{\spaceSize} wc{\columnSize}  wc{\spaceSize} wc{\columnSize}}
        background: & image & & black & & image & & black \\
        transform: & \multicolumn{3}{c}{rotation} & & \multicolumn{3}{c}{translation}  \\
         & \multicolumn{7}{c}{$Axis$}
         \end{tabular} 
    }
    \def \legendLabels {linear static, linear moving, jagged static, jagged moving}
    \def \plots{
        \barPlot{mainBlue}{\distortionsLS}{0-\doubleShift}{\yLast}{#8}
        \barPlot{mainLightBlue}{\distortionsLM}{0-\shift}{\yLast}{#8}
        \barPlot{mainRed}{\distortionsJS}{\shift}{\yLast}{#8}
        \barPlot{mainLightRed}{\distortionsJM}{\doubleShift}{\yLast}{#8}
    }
\fi

\pgfplotsset{
    y coord trafo/.code={\pgfmathparse{trans(##1,\yLast,#8)}},
    y coord inv trafo/.code={\pgfmathparse{invtrans(##1,\yLast,#8)}}
  }
\pgfplotsset{every error bar/.style={line width=0.7mm}}
\pgfplotsset{tick label style={font=\footnotesize}, ymin=0, xmin=0-0.5, ymax=\yLast, yticklabel style={/pgf/number format/fixed}, width=\linewidth, height=.4\textwidth}

\ifnum60=#8\relax
    \pgfplotsset{yticklabel style={
        /pgf/number format/.cd,
        fixed,
        fixed zerofill,
        precision=0,
        /tikz/.cd}}
\else
    \pgfkeys{/pgf/number format/.cd,1000 sep={\,}}
\fi

\pgfplotstableread{charts/trajectory/#4LinearStatic.txt}\distortionsLS
\pgfplotstableread{charts/trajectory/#4JaggedStatic.txt}\distortionsJS
\pgfplotstableread{charts/trajectory/#4LinearMoving.txt}\distortionsLM
\pgfplotstableread{charts/trajectory/#4JaggedMoving.txt}\distortionsJM
\begin{tikzpicture}[] 
\begin{axis} [
    xticklabels from table={\distortionsLS}{type},
    legend style={at={(#6,#7)},anchor=west, fill opacity=0.9, text opacity=1, draw opacity=1, nodes={scale=0.8, transform shape}},
    ylabel={#1},
    xtick=data,
    ytick={#2},
    xticklabels={x,y,z,,x,y,z,,x,y,z,,x,y,z},
    major x tick style = {opacity=0},
    minor x tick num = 1,
]
\foreach \label in \legendLabels {
    \addlegendentryexpanded{\label}
}
\avgLegend
\addlegendimage{draw=mainBlue,very thick, no markers}
\addlegendimage{draw=mainLightBlue,very thick, no markers}
\addlegendimage{draw=mainRed,very thick, no markers}
\addlegendimage{draw=mainLightRed,very thick, no markers}
\plots
  
\end{axis} 
\end{tikzpicture}
\vspace{13pt}
\small{\bottomText}
\caption{\label{fig:#4}
   #5}
\end{figure*}
}

%Short chart 
\newcommand{\shortChart}[8]{
\begin{figure}[!htbp]

\StrCount{#2}{,}[\numofelem]
\StrBehind[\numofelem]{#2}{,}[\yLast]

\def \shift {2.5}
\def \doubleShift {7.5}
\def \avgLegend {\addlegendimage{draw=mainOrange,line width=2pt, xscale=0.3, style={xshift=20}}}    

\def \xLabels {norm, inv, , norm, inv}
\ifnum0=#3\relax
    \def \avgLegend {}
    \def \legendLabels {static, moving}
    \def \plots{
        \barPlot{mainBlue}{\distortionsS}{0-\shift}{\yLast}{#8}
        \barPlot{mainRed}{\distortionsM}{\shift}{\yLast}{#8}
    }
\else
  \def \legendLabels {avg, static, moving}
    \ifnum2=#3\relax
        \def \xLabels {min, max, opt, , min, max, opt}
    \fi
    \def \plots{
        \minMaxPlot{mainBlue}{\distortionsS}{0-\shift}{\yLast}{#8}
        \minMaxPlot{mainRed}{\distortionsM}{\shift}{\yLast}{#8}
    }
\fi

\def \columnSize {0.25\linewidth}
\def \bottomText{
    \begin{tabular}{ 
    wr{0.15\linewidth} wc{\columnSize} wc{0.02\linewidth} wc{\columnSize}}
    background: & image & & black \\
     & \multicolumn{3}{c}{$type$}
     \end{tabular} 
}

\pgfplotsset{
    y coord trafo/.code={\pgfmathparse{trans(##1,\yLast,#8)}},
    y coord inv trafo/.code={\pgfmathparse{invtrans(##1,\yLast,#8)}},
  }

\pgfplotsset{every error bar/.style={line width=0.7mm}}
\pgfkeys{/pgf/number format/.cd,1000 sep={\,}}
\pgfplotsset{tick label style={font=\footnotesize}, ymin=0, xmin=0-0.5, ymax=\yLast, yticklabel style={/pgf/number format/fixed}, width=\linewidth, height=0.65\linewidth}

\pgfplotstableread{charts/trajectory/#4Static.txt}\distortionsS
\pgfplotstableread{charts/trajectory/#4Moving.txt}\distortionsM
\begin{tikzpicture}[] 
\begin{axis} [
    xticklabels from table={\distortionsS}{type},
    legend style={at={(#6,#7)},anchor=west, fill opacity=0.9, text opacity=1, draw opacity=1, nodes={scale=0.8, transform shape}},
    ylabel={#1},
    ytick={#2},
    xtick=data,
    xticklabels/.expand once=\xLabels,
    major x tick style = {opacity=0},
    minor x tick num = 1,
]
\foreach \label in \legendLabels {
    \addlegendentryexpanded{\label}
}
\avgLegend
\addlegendimage{draw=mainBlue,very thick}
\addlegendimage{draw=mainRed,very thick}
\plots
  
\end{axis} 
\end{tikzpicture}
\vspace{13pt}
\small{\bottomText}
\caption{\label{fig:#4}
   #5}
\end{figure}
}
