How to draw the circle projection from surface in TiKz

tikz-pgf

I want to replicate the following figure.

enter image description here

I don't want to use addplot3 and tried something already as below

\begin{tikzpicture}[font=\sffamily,declare function={%
    f(\x,\y)=3+0.075*cos(\x*100)*cos(\y*100)-0.035*(\y-5)^2-0.01*(\x-5)^2; % f(x,y) = 3 + 0.075*cos(x*100)*cos(y*100) - 0.035*(y-5)^2 - 0.01*(x-5)^2

    \tdplotsetmaincoords{70}{110}

    \begin{scope}[tdplot_main_coords]

        \draw[-latex] (0,0,0) -- (8,0,0) node[anchor=north] {$x$};
        \draw[-latex] (0,0,0) -- (0,8,0) node[anchor=west] {$y$};
        \draw[-latex] (0,0,0) -- (0,0,4) node[anchor=south] {$z$};

        % draw 2 x Grid       
        \foreach \a in {3,...,7}
        {
            \draw[thin,gray] plot[variable=\b,samples=60,smooth,domain=3:7] ({\a},{\b},0);
            \draw[thin,gray] plot[variable=\b,samples=60,smooth,domain=3:7] ({\b},{\a},0);
            \draw[thin,gray] plot[variable=\b,samples=60,smooth,domain=3:7] ({\a},{\b},{f(\a,\b)});
            \draw[thin,gray] plot[variable=\b,samples=60,smooth,domain=3:7] ({\b},{\a},{f(\b,\a)});
        }


        \fill[black,name path=x0] (5,5,0) circle (1pt) node[anchor=north] {\footnotesize $x_0$};

        
        \coordinate (x0) at (5,5,0);
        \draw[densely dashed,red,name path=circle_2D] (x0) circle (1);
        
        \coordinate (z0) at (5,5,{f(5,5)});
        \coordinate (z1) at (4,5,{f(4,5)});
        \coordinate (z2) at (5,6,{f(5,6)});
        \coordinate (z3) at (6,5,{f(6,5)});
        \coordinate (z4) at (4,5,{f(4,5)});
        
    \end{scope}
\end{tikzpicture}

compiled with XeLatex

enter image description here

but I can't draw the circle on the surface, can any body help me?
Many thanks in advance.

Best Answer

You can do it with the help of the function f that defines the surface. The parametric equations of the projection circle are

x=5+cos(x),
y=5+sin(x),

And the function f will give you the z coordinate

z=f(x,y)=f(5+cos(x),5+sin(x))

So, the complete code could be:

\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}[font=\sffamily,declare function={%
    f(\x,\y)=3+0.075*cos(\x*100)*cos(\y*100)-0.035*(\y-5)^2-0.01*(\x-5)^2; % f(x,y) = 3 + 0.075*cos(x*100)*cos(y*100) - 0.035*(y-5)^2 - 0.01*(x-5)^2
    g(\x) = 0.075*(\x-4)^2+4;
    h(\x) = 0.02*(\x-4)^3-.2*(\x-4)+4;
    t(\x,\y)=f(4,4)-0.05*(\x-4) 0.02*(\y-4);}]

    \tdplotsetmaincoords{70}{110}

    \begin{scope}[tdplot_main_coords]
        \draw[-latex] (0,0,0) -- (8,0,0) node[anchor=north] {$x$};
        \draw[-latex] (0,0,0) -- (0,8,0) node[anchor=west] {$y$};
        \draw[-latex] (0,0,0) -- (0,0,4) node[anchor=south] {$z$};

        % draw 2 x Grid       
        \foreach \a in {3,...,7}
        {
            \draw[thin,gray] plot[variable=\b,samples=60,smooth,domain=3:7] ({\a},{\b},0);
            \draw[thin,gray] plot[variable=\b,samples=60,smooth,domain=3:7] ({\b},{\a},0);
            \draw[thin,gray] plot[variable=\b,samples=60,smooth,domain=3:7] ({\a},{\b},{f(\a,\b)});
            \draw[thin,gray] plot[variable=\b,samples=60,smooth,domain=3:7] ({\b},{\a},{f(\b,\a)});
        }
        % Circle on the surface
        \draw[red] plot[samples=60,smooth,domain=0:360] ({5+cos(\x)},{5+sin(\x)},{f(5+cos(\x),5+sin(\x)});
        % Points
        \fill[black,name path=x0] (5,5,0) circle (1pt) node[anchor=north] {\footnotesize $x_0$};

        % \node[circle,fill=myorange,minimum size=6pt,inner sep=0pt] (P) at (2,2){}; 
        
        \coordinate (x0) at (5,5,0);
        \draw[densely dashed,red,name path=circle_2D] (x0) circle (1);
        
        % \fill[red] (5,5,{f(5,5)}) circle (1pt) node[anchor=north east] {};
        \coordinate (z0) at (5,5,{f(5,5)});
        \coordinate (z1) at (4,5,{f(4,5)});
        \coordinate (z2) at (5,6,{f(5,6)});
        \coordinate (z3) at (6,5,{f(6,5)});
        \coordinate (z4) at (4,5,{f(4,5)});
        
    \end{scope}
\end{tikzpicture}
\end{document}

And the output: enter image description here

Edit: As suggested in the comments, I'm adding some filling to the surface. The 'circle' is easy but the grid needs to close four plots.

\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz-3dplot}
\tdplotsetmaincoords{70}{110}

\tikzset{my plot/.style={samples=61,smooth}}

\begin{document}
\begin{tikzpicture}[font=\sffamily,tdplot_main_coords,declare function={%
    f(\x,\y)=3+0.075*cos(\x*100)*cos(\y*100)-0.035*(\y-5)^2-0.01*(\x-5)^2;}]
    % axes
    \draw[-latex] (0,0,0) -- (8,0,0) node[anchor=north] {$x$};
    \draw[-latex] (0,0,0) -- (0,8,0) node[anchor=west]  {$y$};
    \draw[-latex] (0,0,0) -- (0,0,4) node[anchor=south] {$z$};
    % grids   
    \foreach \a in {3,...,7}
    {
       \draw[thin,gray] plot[my plot,variable=\b,domain=3:7] ({\a},{\b},0);
       \draw[thin,gray] plot[my plot,variable=\b,domain=3:7] ({\b},{\a},0);
       \draw[thin,gray] plot[my plot,variable=\b,domain=3:7] ({\a},{\b},{f(\a,\b)});
       \draw[thin,gray] plot[my plot,variable=\b,domain=3:7] ({\b},{\a},{f(\b,\a)});
    }
    \fill[gray,fill opacity=0.2] plot[my plot,variable=\b,domain=3:7] (3,{\b},{f(3,\b)}) -- 
                                 plot[my plot,variable=\a,domain=3:7] ({\a},7,{f(\a,7)}) --
                                 plot[my plot,variable=\b,domain=7:3] (7,{\b},{f(7,\b)}) --
                                 plot[my plot,variable=\a,domain=7:3] ({\a},3,{f(\a,3)});
    % Circle on the surface
    \draw[red,fill=red,fill opacity=0.2] plot[my plot,domain=0:360] ({5+cos(\x)},{5+sin(\x)},{f(5+cos(\x),5+sin(\x)});
    % Point and other circle
    \coordinate (x0) at (5,5,0);
    \fill (x0) circle (1pt) node[anchor=north] {\footnotesize $x_0$};
    \draw[densely dashed,red] (x0) circle (1);
\end{tikzpicture}
\end{document}

enter image description here