knotflow Lessons PDF ↓ Deutsch Handbook Modeller ↗ Source

Lesson 1.3 · I Geometry

Surfaces, solids, triangles

What it's about: Curves become surfaces, surfaces become closed solids, solids become triangles, and triangles become a file that a slicer opens without errors. The surfaces stay exact up to the very end. Only then are they cut into triangles, as finely as the tolerance asks, and in such a way that the hull is tight without ever comparing two points.

The belly profile of the cachepot from lesson 1.2, revolved once

1. From curve to surface

A curve has one parameter: t runs from 0 to 1, and the point travels along the curve. A surface has two parameters, usually u and v. Think of them like longitude and latitude on a globe: every pair (u, v) is exactly one point of the surface.

Instead of a row of control points, a NURBS surface has a control net – a grid, like a fishing net, that shapes the surface. It is evaluated in two steps, both with the de Boor algorithm from lesson 1.2:

  1. Every column of the net is a curve in v. All columns are evaluated at the v you want.
  2. The points obtained are the control points of a curve in u. That one is evaluated at u.

These curves "across the surface" are called isocurves (iso = equal: along an isocurve one of the two parameters stays the same). In the code: NurbsSurface::curve_u(v) and curve_v(u) in crates/geom/src/surface.rs. A surface needs nothing more – the rest is knowledge about curves.

2. How surfaces come about

Almost everything comes from three basic motions (crates/geom/src/construct.rs):

Operation Idea Example
Revolve (revolve) turn a profile around an axis pot, cachepot, rings
Extrude (extrude) move a curve along a straight line star
Loft (loft) stretch a skin over several cross-sections twisted vase

Revolving is exact. Every control point of the profile becomes the control polygon of a circle – exactly the 9-point circle from lesson 1.2, with weights. That is why a revolved pot is really round and not just a polygon with many corners.

A loft needs two preparations. The cross-sections have to "fit together": same degree, same number of control points, same knot vector. If a cross-section lacks knots, they are inserted – knot insertion adds a control point without changing the shape of the curve. Then, for every column of control points, a curve is sought that passes through all cross-sections. That is a small system of linear equations (one row per cross-section), solved with Gaussian elimination. The surface meets every cross-section to within 10⁻¹² mm.

Where the cross-sections lie. Every cross-section gets a parameter across, from 0 to 1: per column of control points the square root of its distance from section to section, averaged over all columns (centripetal). The distance itself (the chord length) would be the obvious choice, but then a small ring next to a large one lies far away. The parameters become uneven, and the curve across swings out beyond the end ring: in single lofts through rings of very different sizes the wall then reached below the floor, by up to a tenth of a millimetre. With the square root it stays between the rings.

When every cross-section has knots of its own. Insertion is exact, but not free: afterwards every cross-section carries the knots of all the others. That happens easily. A curve through points (interpolate) usually spreads its parameter by the distances between the points, and two rings with different distances get different knots. For the Moon from height data (examples/moon.kf, handbook chapter 11) that is, with step = 2, 90 rings of 180 points each. After insertion the surface would have about 16,000 knots around, and because tessellation starts at the knots (section 4), that would give 19 million triangles. With interpolate(…, spacing: "even") every point gets the same parameter step. All rings with the same number of points then have the same knots, nothing is inserted, and the same sphere needs 1.4 million triangles. In both cases the surface passes exactly through all points; it is only parametrised differently.

The Moon from NASA height data (one value per half degree), a loft through 360 rings with the same knots

The twisted vase is nine hexagons, each turned a little further and scaled differently (examples/vase.kf, in the language from lesson 2.1):

fn section(t) = polygon(sides, 40 + bulge * sin(t * 180))
  |> rotate(t * twist)
  |> move(z: t * height)

export loft([section(t) | t <- linspace(0, 1, 9)], degree: 3)

All kinds of shapes at a glance

Everything else is put together from the three basic motions. knotflow knows three kinds of shapes, and each becomes triangles in its own way at the end:

Kind What it is Made by Lesson
Surface a NURBS surface, open ends get flat caps revolve, extrude, loft; sweep, tube, pipe lead a profile along a curve; around turns a profile around an outline instead of an axis 1.3, 3.2, 3.9
Solid of faces several surfaces that share their edges (B-rep, boundary representation) solid(surface), walls with shell, holes, union/difference/intersection, rounded edges 3.4–3.10
Mesh triangles already, with no surfaces behind them isosurface (melted balls and rods), relief and plate (from fields) 3.12–3.14

The simple solids are written in the language itself, from the basic motions: sphere(r) revolves a half circle, cylinder(a, b, r) is a pipe along a straight line, cone and torus revolve a triangle and a circle, box extrudes a rectangle. So everything said here about surfaces holds for them.

3. Closed solids

A slicer has to know for every point whether it lies inside or outside. That only works for a closed solid (in slicers often manifold): every edge belongs to exactly two triangles, and the two triangles are oriented alike (both face outwards with their front side). Then there is no hole through which "outside" gets in, and no edge with a third face.

The obvious way to achieve this would be: make triangles, then merge points that are almost equal. That is exactly the comparing that rule 3 from lesson 1.4 warns against: wherever the line for "almost equal" is drawn, it is wrong for some model. knotflow does it the other way round. Every point that belongs to two triangles is computed only once, and the triangles get the same index:

A small detail shows how seriously this has to be taken: the circle from lesson 1.2 ends at (cos 2π, sin 2π) – and sin 2π is not 0 in double. Computed, its last control point would lie 2·10⁻¹⁶ beside the first. That is why circle sets it exactly onto the first.

Solids of several faces follow the same principle on a larger scale: neighbouring faces share their edge, the points on it are computed once, and every face is cut into triangles with these points as constrained edges (lessons 3.4, 3.5 and 3.7). Meshes from isosurface and relief are built from shared corners from the start.

4. Tessellation: the grid in parameter space

Cutting into triangles is called tessellation. The idea is simple:

  1. Choose values for u and for v – a grid in parameter space.
  2. Evaluate the surface at every grid point.
  3. Every four neighbouring points form a quadrilateral that splits into two triangles (along the shorter diagonal – that gives better-shaped triangles).

The art lies in choosing the grid. It starts at the knots (where the curve pieces change), and every interval is halved until the isocurves deviate from their chords by at most the tolerance. A chord is the straight connection between two points of a curve – exactly what is left over later as a triangle edge.

Tolerance 3 mm

Tolerance 0.3 mm

Tolerance Triangles Volume deviation
3 mm 128 −2,850 mm³
0.3 mm 512 −180 mm³
0.01 mm 2,048 −11 mm³
0.001 mm 8,192 −0.7 mm³

Two things stand out:

Where the surface is flat, the grid stays coarse: the bottom of the pot is a single row of triangles, whatever the tolerance.

The pitfall: twisted cells

Checking only the edges of the grid is not enough. On a twisted surface – the vase turns as it rises – the edges of a grid cell lie correctly, but the middle of the cell bulges away from the two triangles. A vase whose grid was chosen by the edges only looks strangely zigzagged. On a strongly twisted square, the middle of a cell would lie 4.2 mm beside the triangles at 0.05 mm tolerance. So the middle of every cell is checked as well; if it deviates too far, the cell is divided in both directions.

What matters is what you measure against. The obvious choice would be the distance from the cell's middle to the middle of the diagonal. In a trapezoidal cell, though – at the round bottom, say, narrow inside and wide outside – the middle of the surface does not lie on the middle of the diagonal at all, although everything is completely flat. The flat bottom would be cut up far too finely for nothing. The right measure is the distance from the middle of the surface to the triangles themselves.

So the vase needs 73,436 triangles for 0.01 mm. With the check at the edges only it would be 1,200 – and they would not keep the tolerance.

5. STL and 3MF

A binary STL file could hardly be simpler: an 80-byte header, the number of triangles, then per triangle a normal and three corners, all as f32. There are no shared points in the file – every triangle writes its corners down again. A slicer finds neighbours by comparing corners exactly. That is why it matters so much that the same point is always written bit for bit the same – and it is, because it was computed only once.

3MF is a ZIP archive with XML inside. It stores every point once and the triangles as indices, plus objects, colours and the unit. Every export becomes an object of its own in its colour, and copies from instance appear only once. What exactly is in it is explained in lesson 4.6.

Model Triangles (0.01 mm) open edges Parts
pot 2,048 0 1
twisted vase 73,436 0 1
belly cachepot 99,324 0 1
rings 107,200 0 3
star 36 0 1

6. How it is checked

For every surface there is a computation that is known without tessellation: the volume of a cylinder, a sphere, a torus, a solid of revolution by Pappus's rule. The tests cut such shapes up at different tolerances and demand that the volume of the triangles deviates from the exact one by at most surface area × tolerance and that every edge has exactly two triangles. A loft has to meet each of its cross-sections. Randomly generated profiles and cross-sections search for cases nobody would have written down by hand. And kf check reads a finished STL file the way a slicer does and counts open edges and edges with more than two triangles.

In depth: a small renderer

The pictures in this lesson come from a mini renderer (crates/cli/src/render.rs, a good 100 lines) that writes SVG directly. In a small way it does what every graphics card does:

  1. Projection: map every point onto the image plane (here without perspective).
  2. Back-face culling: leave out triangles whose front faces away from the viewer.
  3. Painter's algorithm: draw from back to front, what is in front paints over what is behind.
  4. Shading: the more directly a triangle faces the light, the brighter (Lambert).

Graphics cards replace step 3 with a depth buffer (remembering the distance for each pixel) – that comes in Lesson 4.6 with the PNG renderer and in Lesson 4.1 with three.js.

Twisted vase, tolerance 0.5 mm

Rings

Star with non-convex caps

Try it yourself

mkdir -p out
cargo run --release -p kf -- render examples/vase.kf -o out/vase.stl     # write STL
cargo run --release -p kf -- check out/vase.stl                          # check like a slicer
cargo run --release -p kf -- render examples/vase.kf --tol 1 -o out/vase.svg --view -30,10

The models are .kf files in examples/; lesson 2.1 explains the language. You can open the STL files directly in your slicer. With -o out/vase.3mf you get a 3MF file with colours instead (lesson 4.6).

Exercises:

  1. Render the pot with -D height=60 -D bottom_d=20. The bottom becomes wider than asked for – why? (Hint: the line with clamp in examples/pot.kf.)
  2. Make the vase with --tol 1, 0.1 and 0.01. How does the number of triangles develop, and why does it jump so much between 0.05 and 0.01?
  3. Set -D twist=0 for the vase. How many triangles does it need then – and why?
  4. In examples/star.kf, replace the outline by polygon(5, 30). How many triangles does a cap have?
  5. Write the same model as STL and as 3MF and compare the sizes. How much smaller is the 3MF, and why?