\documentclass[tikz]{standalone}

\input{plotsetup}

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

\begin{document}

\begin{tikzpicture}
    \pgfplotsset{
        cyl data/.code = \pgfplotsset{%
            table/x expr = \thisrow{r} * cos(deg(\thisrow{angle})),
            table/y expr = \thisrow{r} * sin(deg(\thisrow{angle})),
            table/z = z,
        },
        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 [cyl data] {\datapath/data.csv};

        % Iterates on hidden faces
        \addplot3[backiter1,onlyback] table [cyl data] {\datapath/x1_log.csv};
        \addplot3[backiter2,onlyback] table [cyl data] {\datapath/x2_log.csv};
        \addplot3[backiter3,onlyback] table [cyl data] {\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 [cyl data] {\datapath/data.csv};
        \addlegendentry{Data}

        % Iterates
        \addplot3[iter1,onlyfront] table [cyl data] {\datapath/x1_log.csv};
        \addlegendentry{Iterates 1}
        \addplot3[iter2,onlyfront] table [cyl data] {\datapath/x2_log.csv};
        \addlegendentry{Iterates 2}
        \addplot3[iter3,onlyfront] table [cyl data] {\datapath/x3_log.csv};
        \addlegendentry{Iterates 3}

        % Origin
        \addplot3[origin, onlyfront] table [cyl data] {\datapath/origin.csv};
        \addlegendentry{Origin}
    \end{axis}
\end{tikzpicture}

\end{document}
