
I’m preparing some tutorials on openSCAD for a workshop at Bristol Hackspace next week and was reviewing some commands I hadn’t used a lot. The surface() command reads a data file containing a matrix of values and generates a 3D object using this data as spot surface heights. Complex surfaces can be created by first generating the data with an external scrip, but it would be nice to be able to create surfaces entirely in openSCAD.
Dan Newman devised a way to construct computed surfaces by creating a cuboid for each grid cell, but as it relies on unioning multiple objects, its quite slow.
Now that variable length vectors can be computed, it is possible to generate the surface as a single polyhedron. The vertices are created over a grid so the x,y values are grid coordinates, and the z-values computed using a function. To construct a closed polyhedron, we construct the surface faces as quadrilaterals (no need to triangulate in the later version of openSCAD), the bottom faces and the faces on four sides. The ‘Thrown together’ viewing mode is the best way to check that the vertex order for faces is correct. A reverse() function is useful to reverse the order when it’s wrong.
Using this approach, we can generate the surface below using this function where x , y = [-4,4)
function ftop(x,y) =sin(60*x)*sin(90*y) + 2 ;

In this case the bottom surface is the xy-plane generated by the function:
function fbottom(x,y) = 0;
To make a skin, perhaps suitable for printing at Shapeways, we can use the surface function offset downwards:

A classic surface is the sinc function sin® / r which can be formed with these functions
function ftop(x,y) =
let (eps=0.001,r = sqrt(x*x+y*y+eps))
sin(n*360*r)/r;

We can then use this approach to make objects. This is a parabolic bowl with the functions below and truncated, perhaps to be used as a parabolic reflector?
function fquartic(x,y) = x*x+y*y;
function ftop(x,y) =fquartic(x,y)+5;

whilst the function x*x - y*y generates the hyperbolic paraboloid or saddle surface:

The arch at the head of the page is based on the function x^2 * y"^2
Generating surfaces this way is very flexible, fast and consequently smooth surfaces can be created and used in constructions. Code is on Github.
shadowbehindthebread liked this
gaudeval liked this kitwallace posted this