Syntax Subtleties

© Mike Williams 2001,2002,2003,2004

Up | Previous: Simple Surfaces | Next: Differences between MegaPOV and POV 3.5 | Alphabetical Index

Max_gradient

POV-Ray 3.5 almost always needs to be told the maximum gradient of the function that you are asking it to evaluate. If you don't specify a max_gradient, or you specify a value that is too low then things will usually go horribly wrong. If you specify a value that is too high, the surface will render correctly but more slowly than necessary.

Here's a very simple example where nasty holes appear in the surface:-

                    
isosurface {
  function { x*x + y*y + z*z - 1 }
        accuracy 0.0001
        contained_by{sphere{0,1.2}}
        pigment {rgb .9}
        finish {phong 0.5 phong_size 10}
}

That's just the Sphere from the Simple Surfaces page, but with the max_gradient 2.4 missing.

What happens is that the function evaluator guesses where each ray hits the surface and then improves the guess by walking up and down the ray. If max_gradient is set higher then the evaluator will take a larger number of small steps. If max_gradient is set too low, then the evaluator will take fewer large steps and may step right past the intersection without noticing.

If you're mathematically inclined, then it is possible to calculate the actual maximum gradient. In the case of this sphere, we happen to know that the function is symmetrical so we can go looking for the maximum gradient in any direction. Let's choose to look along the x axis. Along the x axis the value of y and z are always zero, so the function simplifies to x*x -1. If we differentiate that we obtain the gradient as 2*x. Within contained_by{sphere{0,1.2}}, 2*x ranges from -2.4 to +2.4, so we should add max_gradient 2.4.

This mathematical method is rather tedious, and we'll soon be meeting isosurface functions that we don't know how to differentiate, so we need to find another way to find the max_gradient. We could just guess, and keep trying higher values until the nasty holes disappear, or we could use the evaluate keyword.

In practice, I usually guess something like max_gradient 2 and render a small image and look at the Messages pane. There will be a warning in the Messages if POVRay considers the max_gradient to be incorrect, e.g.

Warning: The maximum gradient found was 4.734, but max_gradient of the isosurface was set to 7.000. Adjust max_gradient to get a faster rendering of the isosurface.

Some functions contain singularities: points where the gradient becomes infinite. The message will say that the maximum gradient found was some large number like 518238.857. In most such cases you can get away with using a considerably lower value if you are prepared to accept that the regions extremely close to the singularity may not render correctly.
 


Download a zip file containing the POV source files for all the images that appear on this page.

Up | Previous: Simple Surfaces | Next: Differences between MegaPOV and POV 3.5 | Alphabetical Index