\documentclass[tikz]{standalone}

\input{plotsetup}

\def\datapath{../res/cylinder}

\begin{document}

\begin{tikzpicture}
    \pgfplotsset{
        onlyfront/.code = \pgfplotsset{x filter/.expression={%
            \thisrow{face} == 0 || (%
                and(\thisrow{face}==2, or(2*pi+\a <= \thisrow{angle}, \thisrow{angle} <= \b))
            ) ? x : nan%
        },},
        onlyback/.code = \pgfplotsset{x filter/.expression={%
            \thisrow{face} == 1 || (%
                and(\thisrow{face}==2, or(2*pi+\a > \thisrow{angle}, \thisrow{angle} > \b))
            ) ? x : nan%
        },},
    }

    \pgfmathsetmacro{\a}{-pi*5/6}
    \pgfmathsetmacro{\b}{pi/6}

    \begin{axis}[illustr3d]
        % Hidden edges
        \addplot3[edge, domain=\b:(2*pi+\a), samples = 90, samples y = 0] ({cos(deg(x))}, {sin(deg(x))}, {-0.5});

        % Data on hidden faces
        \addplot3[backdata] table[
            x expr = \thisrow{r} * cos(deg(\thisrow{angle})),
            y expr = \thisrow{r} * sin(deg(\thisrow{angle})),
            z = z,
        ] {\datapath/data.csv};

        % Iterates on hidden faces
        \addplot3[backiter1,onlyback] table[
            x expr = \thisrow{r} * cos(deg(\thisrow{angle})),
            y expr = \thisrow{r} * sin(deg(\thisrow{angle})),
            z = z,
        ] {\datapath/x1_log.csv};
        \addplot3[backiter2,onlyback] table[
            x expr = \thisrow{r} * cos(deg(\thisrow{angle})),
            y expr = \thisrow{r} * sin(deg(\thisrow{angle})),
            z = z,
        ] {\datapath/x2_log.csv};
        \addplot3[backiter3,onlyback] table[
            x expr = \thisrow{r} * cos(deg(\thisrow{angle})),
            y expr = \thisrow{r} * sin(deg(\thisrow{angle})),
            z = z,
        ] {\datapath/x3_log.csv};

        % Surface
        \addplot3[
            surf,
            mesh/ordering=x varies, % the second input coordinate stays fixed while first varies
            mesh/cols=16, % number of first input coordinate points untils second changes
            surfstyle,
            %x filter/.expression = {or(2*pi+\a <= \thisrow{angle}, \thisrow{angle} <= \b)},
        ] table [
            x expr = cos(deg(\thisrow{angle})),
            y expr = sin(deg(\thisrow{angle})),
            z = v,
            point meta = \thisrow{value},
         ] {\datapath/side_front.csv};
        \addplot3[
            surf,
            mesh/ordering=x varies, % the second input coordinate stays fixed while first varies
            mesh/cols=32, % number of first input coordinate points untils second changes
            surfstyle,
        ] table [
            x expr = \thisrow{v} * cos(deg(\thisrow{angle})),
            y expr = \thisrow{v} * sin(deg(\thisrow{angle})),
            z expr = 0.5,
            point meta = \thisrow{value},
         ] {\datapath/top.csv};
     
        % Edges
        \addplot3[edge, domain={0:2*pi}, samples = 60, samples y = 0] ({cos(deg(x))}, {sin(deg(x))}, {0.5});
        \addplot3[edge, domain={\a:\b}, samples = 60, samples y = 0] ({cos(deg(x))}, {sin(deg(x))}, {-0.5});
        \addplot3[edge] coordinates { (cos(deg(\a)), sin(deg(\a)), -0.5) (cos(deg(\a)), sin(deg(\a)), 0.5) };
        \addplot3[edge] coordinates { (cos(deg(\b)), sin(deg(\b)), -0.5) (cos(deg(\b)), sin(deg(\b)), 0.5) };

        % Data
        \addplot3[data, onlyfront] table[
            x expr = \thisrow{r} * cos(deg(\thisrow{angle})),
            y expr = \thisrow{r} * sin(deg(\thisrow{angle})),
            z = z,
        ] {\datapath/data.csv};
        \addlegendentry{Data}

        \addplot3[origin, onlyfront] table[
            x expr = \thisrow{r} * cos(deg(\thisrow{angle})),
            y expr = \thisrow{r} * sin(deg(\thisrow{angle})),
            z = z,
        ] {\datapath/origin.csv};
        \addlegendentry{Origin}

        % Iterates
        \addplot3[iter1,onlyfront] table[
            x expr = \thisrow{r} * cos(deg(\thisrow{angle})),
            y expr = \thisrow{r} * sin(deg(\thisrow{angle})),
            z = z,
        ] {\datapath/x1_log.csv};
        \addlegendentry{Iterates 1}
        \addplot3[iter2,onlyfront] table[
            x expr = \thisrow{r} * cos(deg(\thisrow{angle})),
            y expr = \thisrow{r} * sin(deg(\thisrow{angle})),
            z = z,
        ] {\datapath/x2_log.csv};
        \addlegendentry{Iterates 2}
        \addplot3[iter3,onlyfront] table[
            x expr = \thisrow{r} * cos(deg(\thisrow{angle})),
            y expr = \thisrow{r} * sin(deg(\thisrow{angle})),
            z = z,
        ] {\datapath/x3_log.csv};
        \addlegendentry{Iterates 3}
    \end{axis}
\end{tikzpicture}

\end{document}
