knotflow Lessons PDF ↓ Deutsch Handbook Modeller ↗ Source

Lesson 3.12 · III Modelling

Implicit shapes and simplifying meshes

What it's about: For flowing transitions that look grown, knotflow describes shapes a second way: not by control points, but by a function that gives the distance to the surface for every point. Such implicit shapes melt together with fillets, and isosurface turns them into a mesh of very many equally small triangles. The grid has to be as fine as the finest spot; on straight stretches that wouldn't be necessary at all. simplify(mesh, tol) merges triangles there as long as the surface deviates from the original by at most tol. For the Voronoi nets from Lesson 3.14, 3.9 million triangles become one million, the STL shrinks from 197 to 50 MB, and the naked eye can't see any difference.

Left: 34,196 triangles from isosurface. Right: the same fork with simplify(tol: 0.05), 4,268 triangles

On the left, the branch fork from examples/fork.kf (grid 0.4 mm) as isosurface delivers it; on the right, with simplify(tol: 0.05). On the straight shaft the triangles become long strips along the axis, because there the surface is curved in only one direction. In the fillet and at the rounded ends, where it curves in every direction, they stay small.

1. Grown transitions: implicit shapes

The shapes of the previous lessons are made of surfaces with control points. For flowing transitions there is a second way to describe shapes: implicitly, by a function that gives, for every point in space, the distance to the surface – negative inside, positive outside. For a bar that is the distance to the centre line minus the radius.

The trick: you unite two shapes by taking the minimum of the two distances. If you take a smooth minimum instead (the same tool as for the bone in 2D, Lesson 3.13), a fillet forms where the shapes come close to each other:

let directions = [(0, 0, 1), normalize((1, 0, 1)), normalize((-1, 0, 1)), normalize((0, 1, 1))]
let parts = [rod((0, 0, 0), d * 12, 1.2, r_end: 0.6) | d <- directions]
export isosurface(blend(parts, k: 1.5), voxel: 0.15)

rod is a bar whose radius changes from one end to the other, blend merges parts with fillets of size k, isosurface turns the result into triangles.

Three things have to be solved for this:

  1. Tessellating: space is divided into small cubes (voxels), each cube into six tetrahedra; wherever the distance changes sign, a triangle is created (marching tetrahedra). Tetrahedra rather than the better-known marching cubes, because cubes can produce holes in special cases and tetrahedra can't. Only the region near the bars is computed – otherwise the Voronoi sphere from Lesson 3.14 would need 400³ = 64 million points.
  2. Strings of beads: a strand is a chain of short bars. If every link of the chain merges softly with the next, a small bump appears at every seam. That is why each element of the list passed to blend forms a group: within a group the union is hard, between groups it is soft.
  3. Lumps: where five branches meet, a smooth minimum over all parts adds up five times into a lump. That is why only the two nearest parts merge softly – the fillet is the same size everywhere.

On top of that comes a trap from Lesson 1.4, twice over: an STL stores points in single precision (f32). Points that lie almost exactly on a grid corner collapse into one there; and three nets tessellated on the same grid share points where they pass close to each other. Both give edges with more than two triangles. That is why every point stays at least 1 % of a voxel away from the corners, and every shape gets its own slightly shifted grid, derived from the shape itself.

2. Collapsing an edge

The basic building block is the edge collapse: the two ends of an edge are merged into one corner. The two triangles that share the edge disappear. All other triangles at the two ends remain, but now hang on the new corner.

    c                    c
   / \                  /|\
  /   \                / | \
 a ─── b      →       /  v  \
  \   /                \ | /
   \ /                  \|/
    d                    d

a and b become v; the triangles abc and abd disappear.

Two triangles fewer per step. You repeat this until no collapse is allowed any more. Two questions need answering: which edge first, and where does v go? A measure of the error answers both.

3. The error quadric

The idea comes from Michael Garland and Paul Heckbert (1997): every corner remembers the planes of the original triangles it stands for. At the start, those are the planes of the triangles around it. The error of a position p is the sum of the squared distances to these planes.

A plane is n·p + d = 0 with unit normal n. The distance of p is n·p + d, and its square is

(n·p + d)² = pᵀ (n nᵀ) p + 2d (n·p) + d².

That is a quadratic form in p: a symmetric 3×3 matrix A = n nᵀ, a vector b = d n and a number c = d². Together they make the quadric Q = (A, b, c), ten numbers. And now the trick: the sum of quadrics is again a quadric – you just add the ten numbers. So a corner doesn't carry a list of planes around, only their sum, whether there are three planes or three hundred.

When a and b collapse, v gets the sum of the two quadrics. That way the error always refers to the original surface, not to the already simplified one. So errors can't quietly add up from step to step.

Where does v go? To where the error is smallest. You find the minimum of a quadratic form by setting the derivative to zero: A p = −b, a linear 3×3 system (Quadric::minimum, Cramer's rule). If three planes meet in a corner, that is exactly this corner point. If all the planes are parallel, as on a flat piece, A is not invertible: every point of the plane is equally good. Then the best of a, b and the midpoint of the edge is taken.

Which edge first? The one with the smallest error. The edges go into a queue, and after every collapse the edges around v are re-evaluated.

Why the tolerance is a real bound

Most programs weight each plane by the area of its triangle. knotflow doesn't, and there is a reason: without weights, every term is exactly one squared distance. If the sum is at most tol², then every single distance is at most tol. No corner is further than tol away from any of the original planes it stands for. That is a promise, not a rule of thumb. Measured on the branch fork, the volume changes from 742.99 mm³ to 742.84 mm³ (tol 0.02 mm), that is, by 0.02 %.

Strictly speaking, "distance to the planes" is not the same as "distance to the surface": a plane extends beyond its triangle. In practice the difference is far below the tolerance (section 7).

4. Closed stays closed

A collapse can break a clean surface. Four checks prevent that (can_collapse, cheapest first):

  1. Exactly two triangles at the edge. An edge on the rim of a hole, or one with more than two triangles, is left alone.
  2. The opposite corners c and d keep at least three neighbours. Otherwise a piece of the surface folds flat. In the smallest closed solid, a tetrahedron, every corner has exactly three neighbours, and that is where it stops.
  3. No triangle flips over. When a corner moves, the triangles around it turn. If one of them turns by more than about 73° (cosine of the normals below 0.3), the collapse is rejected. A flipped triangle would be a crease in the surface.
  4. The link condition (Dey, Edelsbrunner et al. 1999): apart from c and d, a and b may not have any common neighbours. If they had another one, say e, the collapse would leave two edges v–e on top of each other. Four triangles instead of two would then hang on that edge, and the surface would be pinched like an hourglass.

A fifth check is not about the mesh in memory but about the file. A collapse can put a corner 5·10⁻⁸ mm away from its neighbours. In memory the mesh is still closed, but when rounded to f32 for the STL file both land on the same point, and the slicer sees one corner instead of two – with edges that have more than two triangles. It is the same trap as with the implicit shapes in section 1. That is why no edge becomes shorter than 64 f32 rounding steps (about 2 µm at a coordinate of 300 mm).

5. Fast enough

Programmed straightforwardly, the method would be correct but slower than the mesh itself. Two decisions make it fast:

Many entries of the queue become stale because one of their corners has moved in the meantime. They are recognised and skipped when taken out, which costs almost nothing. The real work is re-evaluating the eight or so edges around v after every collapse, and the method requires that.

6. In the language

fn net(k) = isosurface(blend([strand(k, e, ms[e]) | …], k: grow), voxel: voxel)
  |> finish

fn finish(m) | detail > 0 = simplify(m, tol: detail)
fn finish(m)              = m

simplify is an ordinary function from mesh to mesh and also accepts lists. It doesn't happen automatically but is written in the model, like everything else. There are two reasons for that:

NURBS surfaces are only split into triangles at the very end. For them there is already the tessellation tolerance (--tol, Lesson 1.3); they don't need simplify.

Model before tol: 0.02 tol: 0.05
Branch fork (grid 0.25 mm) 87,468 10,998 6,556
Voronoi nets 3,934,964 990,124 667,982
Tree roof 4,396,200 1,680,666 1,025,184
Bone (tol: 0) 23,220 9,136 –

The branch fork shrinks to an eighth, the nets to a quarter, the tree roof only to just over a third. The thicker the bars are relative to the grid, the more flat surface there is to merge. The thinnest bars of the nets (0.55 mm) are barely thicker than two grid cells.

The browser and the command line produce byte-identical files with simplify too (tools/same-stl.mjs). The method uses only + − × ÷ and √, and ties are broken by order, not by chance.

7. How it is checked

A simplified sphere must stay round and closed: no corner lies further from the true sphere than the corners of the fine mesh plus tol, and every edge has exactly two triangles. Every model of the gallery is written as STL and read back the way a slicer does, simplified too, and must stay closed. Browser and command line must write the same file. The same holds for isosurface, plus a volume against the exact computation: a single sphere from ball must have the volume of a sphere, up to what the grid allows.

8. Limits

Try it yourself

cargo run --release -p kf -- render examples/fork.kf -D detail=0    -o out/fork-fine.stl
cargo run --release -p kf -- render examples/fork.kf -D detail=0.05 -o out/fork.stl
cd web && npm run dev     # model "fork", turn on "Triangles", drag detail

Exercises:

  1. In the browser, slowly drag detail for the branch fork from 0 to 0.3. Where do the triangles disappear first, and where last? What happens to the rounded ends?
  2. Set fillet to 0 (no fillet, a hard edge at the fork). How are the triangles distributed now?
  3. Something to think about: why would it be wrong to recompute v's quadric from the triangles around v after each collapse, instead of adding the quadrics of a and b? (Hint: what would v still know about the triangles that have already disappeared?)
  4. For the curious: the error quadric is a 4×4 matrix if you extend p with a 1: (x, y, z, 1). Write A, b and c as one matrix and convince yourself that pᵀ Q p gives the same result.