Triangle Meshes

One reason I enjoy using POV-Ray is because its internal representations of object primitives are just mathematical functions. This works wonderfully for visualizing objects that can be represented as functions but not so well when trying to convert from other packages. Luckily, POV-Ray also allows the construction of objects based on triangles. The nature of triangle meshes means that, except for some trivial tutorial examples, external programs are needed to create the meshes.

Visualize

For the example images on this page I started by firing up GNUPlot to quickly visualize a few functions. For example:

gnuplot> set hidden
gnuplot> set isosamples 50
gnuplot> splot [-3:3] [-3:3] sin(x**2 + y**2)

This creates a graph similar to the following:
Plot of sin(x^2 + y^2)

Generate Mesh

I then created a short C program to generate the triangle meshes. Use your language of choice if needed. The important thing to remember is that the POV-Ray coordinate system is probably not the same "handedness" as what you're used to. There are a couple ways around this.

I found it easier to do the former. Peeking at the C sources you'll see that it was just a matter or switching around the Y/Z coordinates. To switch the handedness in POV-Ray use the "right" keyword during the camera setup. E.g.:
  right <-4/3,0,0>
I didn't use this option because 90% of my POV-Ray scene files assume a left-handed coordinate system. The important lines in the triangle program are:
#define X_MIN -3
#define X_MAX 3
#define Y_MIN -3
#define Y_MAX 3
The above sets the upper and lower bounds in the plane. Note that the X and Y in the program correspond with X and Y in POV-Ray coordinates. Yes, I suppose I should fix that :).
#define X_STEP_SIZE 0.0125
#define Y_STEP_SIZE 0.0125
This sets the granularity of the graph. If you're doing previews you should set this to a larger number such as .05 or so. Smallers values of STEP_SIZE will generate lots of triangles (50,000 or more in most cases).
return sin(pow(x,2) + pow(y,2));
This is, of course, the actual function definition. Please note that POV-Ray can calculate many functions internally using the poly and isosurface features. Compile the program with the following:
  gcc -o triangle triangle.c -lm
Next, run the triangle program and redirect output to an include file:
  ./triangle > mesh.inc

Create the POV scene file

Finally, we create a simple scene using our newly created mesh. The important lines are:

#include "mesh.inc"
This is the name of the file we generated from our triangle program. It must reside in either the current directory or the POV-Ray include path. We place the object by doing:

object { pov_curve 

 pigment { BrightGold }
     finish {
        ambient .1
        diffuse .1
        specular 1
        roughness .01
        metallic
        reflection {
          .65
          metallic
        }
     }
}

Render!

The final step is to render the scene. You can either call povray directly or setup a Makefile and opts.ini file to automate the commands. As mentioned previously, the mesh objects often consist of thousands of triangles. If you've generated very smooth curves (i.e., very small step values) then your render could take hours to complete or even run out of memory. At least you have an excuse to get better hardware :D.

Mathematical curve rendered
in POV

Gallery

Here are some other graphs. They're not terribly useful from a mathematical standpoint, but are pretty :D.

gnuplot> splot [-4:4] [-4:4] 1/(1+sqrt(x**2+y**2)) * (x*sin(4*( sqrt(x**2+y**2) )) +
y*cos( 4*sqrt(x**2+y**2)) )
GNUPlot curve POV-Ray curve
gnuplot> splot [-3:3] [-3:3] cos(x*y)
GNUPlot curve POV-Ray curve
gnuplot> splot [-3:3] [-3:3] sin(2*x*y)*cos(x*y)
GNUPlot curve POV-Ray curve