# Why does Plot3D appear to traverse the points twice?

While plotting a slow to evaluate function and trying to get some sense of progress, I noticed that Mathematica traverses the grid of points twice. Here's the reduced example:

points = {};
Dynamic[
Show[
ListPlot[Take[points, Max[0, Length@points - 5]], PlotRange -> All],
ListPlot[
Take[points, -Min[Length@points, 5]], PlotRange -> All,
PlotStyle -> Directive[PointSize[Large], Opacity[0.1], Red]
],
AspectRatio -> Automatic
]
]

Monitor[
Plot3D[Pause[0.05]; 0, {a, -5, 5}, {b, -5, 5},
PlotRange -> All, PlotPoints -> 10, MaxRecursion -> 0],
AppendTo[points, {a, b}];
]


If you watch the plot, you'll see that the red points move twice over the grid. If you inspect the plots variable after plotting, you'll see that not all the points are duplicates: some are offset from their originals by $\sim10^{-8}$.

Why does Plot3D do this, even though I asked for MaxRecursion -> 0? Is there any way to convince it to only scan the function once (not having to resort to ListPlot3D + Table)?

• I am not sure why Plot3D does this, but you can avoid repeated evaluation using a memory pattern, e.g. f[x_, y_] := f[x, y] = .... – JEM_Mosig Jan 13 '18 at 22:35
• Maybe for computing the vertex normals? Try NormalsFunction -> None as option. – Henrik Schumacher Jan 13 '18 at 22:53
• Ruslan, very nice animation, btw. – Henrik Schumacher Jan 14 '18 at 0:31
• @HenrikSchumacher this appears to be correct. Why not post it as an answer? – Ruslan Jan 14 '18 at 7:58
• 1) The normals require 3 function evaluations (computed from finite differences), so that's a lot of overhead for a slow function. 2) You can add VertexNormals computed from the plot, instead of from the function, with RegionMeshMeshCellNormals. It helps to use Mesh -> All when converting the plot to a region: DiscretizeGraphics[Plot[..., Mesh -> All]]. – Michael E2 Jan 14 '18 at 14:46

I guess the second pass is for computing the vertex normals. At least the behavior of Plot3D is quite different (and somewhat erratic) when we set the option NormalsFunction -> None.