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.


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:
- For the first line of a hole: a coarse triangulation of the surface is tested against the line (Möller–Trumbore, the standard "ray hits triangle" test). Every triangle that is hit provides a (u, v) to start from.
- For all the others: the solution of the neighbouring line. It lies only a few tenths of a millimetre away, ideal for Newton. Feeling your way along like this is called continuation.
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:
- 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.
- 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. - The triangles inside the holes are dropped: from every trim curve edge a "flood" runs inwards, triangle by triangle, without crossing a trim curve.
- 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
- Prisms only: a hole is pushed straight through.
Holes along the surface normal (perpendicular to the wall, even where it
slopes) or tapering tunnels are beyond
perforate. - One face in, one face out: each of the two trim
loops must lie on one face of the solid – a curved face or a flat one
(the lids of
extrude). A hole over an edge or a crease (where the profile has a corner) is reported. - Round a pole: drill a ball along its axis and the loop runs once right round in u; in the parameter plane it is not closed at all. On the solid it still bounds a disc, in the parameter plane the band from the loop to the pole, and that goes. Without a pole on that side of the loop (the side of a cone cut short, a tube along its axis) the rest would stay in the hole, and that is reported.
- For everything else: Boolean operations.
difference(Lesson 3.7) subtracts any solid, through caps and creases too. That needs two surfaces to be intersected, not a line and a surface (Lesson 3.6) – more general, but also slower thanperforate. - Holes and Boolean operations:
perforatetakes results ofunionanddifferencetoo, and a perforated solid can be combined further. Holes the other solid does not touch go into the result as trim loops; a touched hole becomes a prism and takes the general way. Together that gives an order: (solids − cutouts) ∪ additions − openings.examples/plant_pot.kfwith 756 holes, ribs and 67 openings costs four times as much as punching alone; with 16 000 holes the ribs and the tube cost next to nothing, but 1521 openings ten times as much. Only a loop over the line where two solids meet is reported byperforate: there is no one face for it to lie on. - Rounded cell corners come from rounding the outline
first:
let cell = fillet(polygon(6, hole / sqrt(3)) |> rotate(90), 0.8)(Lesson 3.9).
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:
- 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.) - 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.) - In
tube_holes, push two cells into each other: angles[0, 10, 60]instead of[0, 30, 60]. What doesperforatereport, and where in the text? - 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?)