A simple example: a tablecloth

Before launching the simulation, it is initially necessary to create the starting .cth file.

How to write a .cth file

The .cth format was defined in such manner to be able to be read and written using the POV-Ray 's input/output files directives (#fopen, #read, #write, #fclose). One thus will write the following macro:

#macro WriteClothFile(filename, n1, N2, nlng, ks, ht)
    #debug "\nWriting new cth file\n"
    #fopen file filename write
    #write(file, n1, ", ", n2, ", ", nlng, ", ", ks, ",\n";)
    #local l1 = nlng*(n1-1);
    #local l2 = nlng*(n2-1);
    #local st = seed(1234);
    #local i=0;
    #while (i < n1)
        #local j=0;
        #while (j < n2)
            #local tempx = -l1/2 + i*nlng;
            #local tempz = -l2/2 + j*nlng;
            #local tempy = ht + (-1+2*rand(st))*nlng*0.1;
            #write(file, tempx, ",", tempy, ",", tempz, ", 0.0, 0.0, 0.0,\n")
            #local j=j+1;
        #local i=i+1;
    #fclose file

After having opened the file (#fopen), the macro writes the first line (#write) which contains the number of points of the cloth in two dimensions (n1, n2), the normal distance between each points (nlng), and the strength coefficient of the springs (ks).

Then, the various points are distributed in a rectangle of dimension (nlng*(n1-1)) on (nlng*(n2-1)), parallel with the (xz) plane, and at height ht, centered around y axis. A small amount of noise is added to each point coordinates (rand()). Don't forget to close the file (#fclose)...

A tablecloth, that goes on a table...

The table will be defined in a simple manner:

#declare Table = union {
    cylinder { 8*y, 9*y, 8 }
    torus { 8, 05 sturm translate 85*y }
    cylinder { 85*y, 0, 05 }
    cylinder { 0, 05*y, 4 }

Visualize the starting position of fabric

To check that the simulation does not begin with part of fabric IN the table, we can create the mesh Nappe, using 0 iterations in simcloth:

WriteClothFile("nappe.cth", 50, 50, 1.8/50, 10, 0.95)

simcloth {
    iterations 0
	input "nappe.cth"
	mesh_output "nappe.msh"
	uv_mesh on

#declare Nappe = mesh {
    #include "nappe.msh"
    texture {
        pigment {
            checker color rgb<1, .5, .2> color rgb<1, .8 ,.4>
            scale <1/10, 1/10, 1/10>

Then, the ground, the walls, a light source, a camera, are added, and we obtain this:

Darling !!! Put the tablecloth on the table !!!

And for that, nothing better than the gravity (-.4*y). Moreover, the table is rough (and the fabric also), one thus will put the coefficient of friction at 0 (big frictions). As the distance between each point is rather small, one will also take a rather small time interval between each iterations (0.03).

simcloth {
    environment Table
    friction 0
    gravity -.4*y,
    damping .9,
    intervals 0.03
    iterations 50
    input "nappe.cth"
    output "nappe.cth"
    mesh_output "nappe.msh"
    smooth_mesh on
    uv_mesh on

Don't forget to look at the messages window (in Windows version) or ouput stream (in unix version), to know if the simulation ended OK or not (in case of problems while opening files, lack of memory, etc...).

The starting .cth file can be calculated only once. By putting the WriteClothFile(...) macro in comments, thereafter, each simulation will begin again where the preceding one was stopped.

In order to avoid coincident surfaces, you can reduce very slightly the dimensions of the table, and level up the tablecloth a little...

object { Nappe translate 0.001*y }
object { Table scale <.98, 1, .98>
    texture { T_Wood23 rotate <10, 20, 0> }

For those who are as I am (i.e. lazy), complete script available: nappe.pov

After 50 iterations...
... 50 additional iterations...
... and 100 more.