knotflow Lessons PDF ↓ Deutsch Handbook Modeller ↗ Source

Lesson 3.5 · III Modelling

Holes through curved walls

What it's about: The air-pruning pot from Lesson 1.1, this time with its 874 hexagonal cells in the wall. As a mesh model in OpenSCAD, such a pot needs facet phases, eps gaps and detours to avoid sliver triangles. Here the wall stays an exact NURBS surface, the holes are cut out in its parameter space, and triangles only appear at the very end. The result is closed, without a single workaround: 101,320 triangles, not one open edge, in a fraction of a second.

Assumed: NURBS surfaces and their parameter space (u, v) from Lesson 1.3. Holes through a cap, or subtracting whole solids from each other, is what difference does (Lesson 3.7); perforate is the simpler and faster way for many equal holes through a curved wall.

The air-pruning pot from examples/air_pot.kf

The same model with larger cells: hole = 11, web = 2.4

1. A hole is a prism

As in the OpenSCAD model, a cell is a prism: a hexagon in its own plane, pushed through the wall in one direction. For the pot, the plane lies on the axis and the direction points horizontally outwards. The prism cuts the sloping wall the way a drill would go through it.

let cell  = polygon(6, hole / sqrt(3)) |> rotate(90)        // points at top and bottom
let holes = [radial_hole(cell, 360 * (c + (r % 2) / 2) / cols, z0 + r * pitch)
             | r <- [0 .. rows - 1], c <- [0 .. cols - 1]]
export perforate(revolve(section), holes)

A hole is a record {shape, at, along, up}. radial_hole(shape, angle, z) from the prelude builds one for the most common case. Every edge of the prism, that is every straight line from the outline in the direction along, hits the surface of the solid twice: where it enters the material and where it leaves it. These points form two closed curves on the surface, the trim curves: one on the inner wall, the other on the outer wall.

2. Where does a line hit the surface?

We are looking for a point that lies both on the surface and on the line:

S(u, v) = o + t · d

These are three equations (x, y, z) in three unknowns (u, v, t). Newton's method solves them: start from a guess, replace the surface there by its tangent plane, solve the linear system, and repeat. The matrix of the system is made of the three directions ∂S/∂u, ∂S/∂v and −d. A 3×3 system can be solved directly with Cramer's rule. Newton converges quadratically: once you are close enough, the number of correct digits doubles with every step. After three or four steps the point is accurate to 10⁻¹¹.

"Close enough" is the catch. Starting values come from two sources:

Whether a hit is an entry or an exit is told by the normal: if it points against the line, the line enters the material. The error messages come from this too, for example hole 3: starts inside the material when at lies inside the wall.

Because Newton evaluates the surface very often, one evaluation computes the point and both derivatives in a single pass, directly from the B-spline basis functions (The NURBS Book, algorithm A2.3), without allocating memory for it.

3. How densely to sample?

The outline is sampled at its corners (the knots) and then adaptively: between two lines a third one is placed. If its hit point deviates by more than tol (0.01 mm) from the chord between its neighbours, it is subdivided further. This is the same principle as in the tessellation in Lesson 1.3. A straight hexagon edge running through the curved wall becomes a slightly bent curve on the pot and so gets a few points in between.

4. Triangles in parameter space

Now the trim curves go into the tessellation. The picture shows the outer wall of examples/tube_holes.kf (a tube with five cells), unrolled into the plane. Horizontally is the angle, that is the parameter u; vertically the height:

Outer wall of a tube with five cells, unrolled. In red the trim curves, dashed the seam at u = 0 and u = 1

  1. The grid from Lesson 1.3, finer around the holes: cells at most a sixth of the circumference of the smallest hole. Grid points inside a hole, or closer to a trim curve than a few tenths of a cell, are left out; they would only produce slivers.
  2. A constrained Delaunay triangulation (CDT, crate spade) connects grid points and trim curve points into triangles. "Delaunay" means: as few pointed triangles as possible (no other point lies inside a triangle's circumcircle). "Constrained" means: the edges of the trim curves must appear as triangle edges, even where Delaunay would place them differently. Triangulation happens not in millimetres but in cells of the grid: each cell counts as a square of side 1, however long it is on the surface. The grid holds the tolerance cell by cell, with long thin cells on a cylinder, so a triangle no wider than the cells round it holds it too; in millimetres Delaunay would fan out from a point of the trim curve across many thin cells and cut the surface short.
  3. The triangles inside the holes are dropped: from every trim curve edge a "flood" runs inwards, triangle by triangle, without crossing a trim curve.
  4. The wall of the hole (its reveal) is a strip between the entry and the exit curve. Point k of one and point k of the other lie on the same prism edge, so they are joined into quads and then into triangles. These points are the same vertices as in the wall. As always, this is watertight by construction, not by comparing coordinates (Lesson 1.4).

The triangulation is deterministic: spade uses exact orientation tests, as in Lesson 1.4. That is why the browser and the command line produce even the honeycomb pot byte for byte the same.

The seam

At the far left and far right of the picture you can see a cell split into two halves. The surface is closed all the way round, but its parameter u runs from 0 to 1, and at 0 = 1 there is a seam. A hole across the seam has trim curves that leave on the left and come back in on the right. To make both sides fit together, every trim curve gets its own point exactly on the seam (found by bisection). It appears twice in the triangulation, at u = 0 and at u = 1, but in the mesh it is one and the same vertex.

A trap here: if a sample point already lies almost on the seam (10⁻¹³ away) and the seam point were added as well, two points 10⁻¹³ apart would make a triangle with a vanishingly short edge – and the mesh would have open edges. That is why a point that close to the seam is moved exactly onto it instead of adding a second one. This is "topology before geometry" again: better to share a vertex deliberately than to hope two nearly equal ones will match.

5. What this changes compared to OpenSCAD

OpenSCAD (mesh model) here
Wall polygon with $fn facets exact NURBS surface
Hole Boolean difference of two meshes trim curve on the surface
Slivers where hole edge and facet nearly coincide wall_phase, even $fn don't exist: facets only appear after trimming
Resolution up front, per part at the end, one tolerance for everything
Holes on the seam – seam is split

6. Limits

Try it yourself

cargo run --release -p kf -- render examples/air_pot.kf -o out/air_pot.stl
cargo run --release -p kf -- render examples/air_pot.kf -D hole=11 -o out/large.png
cd web && npm run dev     # model "air_pot" or "tube_holes"

Exercises:

  1. In tube_holes, change the angle of one cell to 2°, 1°, 0.5°: it moves across the seam. Does the mesh stay closed? (The status line reports errors.)
  2. Make circles instead of hexagons in the pot: let cell = circle(hole / 2). How can you tell from the mesh that the outline is now a NURBS curve? (Hint: the "Triangles" setting.)
  3. In tube_holes, push two cells into each other: angles [0, 10, 60] instead of [0, 30, 60]. What does perforate report, and where in the text?
  4. Something to think about: why does Newton's method cope with the seam, even though u jumps from 1 to 0 there? (Hint: what is u = 1.02 on a surface closed all round?)