Lesson 3.7 · III Modelling
Booleans: union, difference, intersection
What it's about: Most parts are put together from
simple solids: a plate, bosses on it, holes through both.
union(a, b) joins two solids, difference(a, b)
takes b away from a, intersection(a, b) keeps what both
have in common. Underneath, this is the classic CAD-kernel job: find
where the faces meet (Lesson 3.6), cut them there, keep the right
pieces, and join everything into one closed solid. The faces stay exact
the whole time.

let base = box(width, depth, plate) |> move(z: plate / 2)
let bosses = [cylinder((x, y, plate), (x, y, plate + tall), boss) | (x, y) <- at]
let drills = [cylinder((x, y, -5), (x, y, plate + tall + 5), hole) | (x, y) <- at]
let solid = fold(bosses, base, union)
let holed = fold(drills, solid, difference)
export difference(holed, slot)
Assumed: solids (Lesson 3.4) and intersection curves (Lesson 3.6). Sections 1 to 4 show how to use the operations and where their limits are. Sections 5 and 6 show how they work inside, 7 and 8 how they are checked and which traps there are.
1. A tree, not a mesh
A boolean is not computed into triangles straight away. It is a tree: its two operands (solids, or booleans themselves), and everything they have in common, computed exactly when the boolean is built:
- cuts: the intersection curves of every face of one operand's solids with every face of the other's;
- splits: the points where an edge of one solid passes through a face of another (a curve meeting a surface: three equations, three unknowns);
- triple points: where three solids meet, a cut between two of them passes through a face of the third. Such a point lies on three surfaces; it is computed with six equations and six unknowns, (u, v) on each.
None of this depends on a tolerance. Only when the model is drawn or saved are the faces cut into triangles, as finely as asked. A mistake shows at the statement that made the boolean, not later.
A cap (the flat end of a cylinder, the top of a box) is not a NURBS surface in its own right. For cutting, it becomes a flat patch a little larger than its edge loop, and everything that comes out is cut back to the inside of the loop.

2. Putting things on things
A boss standing on a plate has its floor exactly in the plate's top face. There is no curve where they cross: they lie on each other. In CAD this is not a corner case but the normal one: things are put on things.
For two caps in one plane, each one's edge loop is drawn into the other, with the edge's own vertices. Then the regions are classified as always (section 6). The floor of the boss and the patch of the plate's top below it are inside the result on both sides: both go. Where both caps keep the same piece (two boxes with the same top, joined), the solid that comes first draws it, the other one leaves it out.
A hole through such a boss cuts the plate's top and the boss's floor in the same circle. Two polylines that are both within 0.0001 mm of it, but with different vertices, would cross each other on the wall of the hole. So a cut that is another one again uses that one's polyline.
"Caps in one plane" also covers box sides that line up (every side of a box is a flat face of its own), curved faces lying on each other (a pin in a matching hole, a cylinder extended by an equally thick one on the same axis), and the discs of a revolved part at the axis (a cone on a plate).
A clear message ("two of the solids share a face: move one of them a little") comes only where a flat ring of a turned part (a shoulder) lies on a plane. It is a piece of the round face, with a hole a flat cap cannot have.
Touching along a line is no error. Two holes just
touching; a cylinder lying on an edge; a boss pressed against a wall
from inside: there is no cut there. Both shells stay whole and touch,
the volume is right, the mesh is closed. Only where vertices of both
shells lie exactly on the line they touch along, an STL file (32-bit
numbers) makes them one, and four triangles hang on such an edge instead
of two. Slicers cope with that; kf check reports it.
The same solid twice in one tree
(union(a, difference(b, a))) is allowed as well: the first
of them stands for every other. The others are neither cut nor drawn,
and a point is inside them when it is inside the first.
3. Many solids
The sea urchin of Lesson 3.1, with all its spines joined to the body, is a union of 451 solids:

Nearly all the work lies in finding where curves pass through surfaces. To make that quick with many solids, each face makes its coarse triangles once, sorted into cells, and a pair whose boxes do not touch is skipped before anything is computed. That way the union takes a few seconds.
examples/urchin.kf has a slider whole: 0
gives the copies (fast, 451 separate parts), 1 the one solid.
4. Limits
The operations are made for parts as people draw them: plates, bosses, holes, slots, domes, revolved parts. Special positions in which several things meet exactly at one place may give an open mesh or are reported:
- Several touches in one point, where they do not come from whole millimetres (section 8): three circles through one point, a seam exactly on a line two faces touch along.
- Copies that nearly lie on each other (a solid and the same one moved by 0.1 mm): cuts at a very sharp angle, narrow lenses between them. Likewise an edge just next to a crease.
- Two cuts on one face that lie on each other, without their other faces being caps in one plane (a ball whose equator lies exactly on a cylinder's wall and also in a plate's top).
- Solids that touch only along an edge (two boxes corner to corner): the result is necessarily not a manifold; four triangles hang on the edge. The same happens when vertices of two touching shells become one when the STL is written.
- A flat ring of a revolved part on a plane (a shoulder on a box) and two cylinders of equal radius crossing (the Steinmetz case): reported, not handled.
What to do in these cases is in the handbook, chapter 7. What research knows about such special positions (measured tolerances for each corner and edge, shared pieces of edges) is in Lesson 3.15.
And two things that are no longer the Booleans' business:
- Walls with edges and floors: Lesson 3.8.
- Fillets: rounded rims, plate edges and bosses with
a fillet in Lesson 3.9; edges left by a Boolean are rounded by
fillet_edges(Lesson 3.10).
5. Every face triangulated with its cuts
Each face is triangulated in its own parameters (u, v), like the holes of Lesson 3.5: its grid points, the vertices of its edges and the points of all cuts across it are joined by a constrained Delaunay triangulation. A constraint is a segment that must appear as an edge of the mesh, even where Delaunay would place the edges differently; here these are the pieces of the cuts and edges. Every vertex on a cut or an edge is one vertex of the mesh, shared by all faces that meet there. So the result is closed by construction: no coordinates are compared.
Three details need particular care:
- Seams. A round face is closed in u: its parameters run from 0 to 1, and 1 is 0 again. A cut crossing the seam gets a point exactly on it, which appears in the triangulation twice, at u = 0 and at u = 1. Exactly means exactly: a point at u = 0.99999999 instead of 1 has no twin at 0, and the next constraint runs straight across the whole face.
- Triangles that leave the surface. A cylinder is straight along its length, so its grid has no lines there. Between two holes, the triangulation could join points of one hole directly to points of the other – straight through the inside, and on top of the wall of the other cylinder. So every triangle is checked: if its middle is further than the tolerance from the surface, a point of the surface is added there, until all of them follow it. Only what stays counts (section 6): the wall of a cylinder lying inside another solid is triangulated coarsely and then left out – that its triangles do not follow the surface is not worth a warning.
- Lines that come close. Cuts and edges on a face are polylines that differ from their curves by their sag. Where two lines come closer than that, their constraints cross, and the regions on both sides run into one. Against it, one rule holds for all lines on all faces: a piece is halved at its curve's parameter as long as it strays from its curve by more than a quarter of the way to the nearest other curve – down to a floor of about the root of the curve's error times its size, below which two curves cannot be told apart. Where two lines touch, their distance grows with the square of the way, and so does their sag: the pieces get shorter towards the touch by themselves. Below the floor, both get points at the same distances (section 8).
6. Which pieces stay
The cuts divide every face into regions: connected pieces of the face that no cut crosses any more. Each region is either part of the result's boundary or it is not, and it is decided at one point in the middle of the region. For the face's own solid the answer is clear: just in front is outside, just behind is inside. For every other solid the question is whether the point itself is inside it – the point lies on this face and on no other. (Testing the other solids just in front and behind as well would go wrong: in a thin solid, or in a wedge at its edge, both points would be outside.) For the tree, union then means "inside one of them", difference "inside a, not inside b", intersection "inside both".
If "in front" is inside the result and "behind" is not (or the other way round), the region is boundary, and its triangles face the side that is outside. Otherwise it goes. This one rule covers every kind of boolean and every nesting depth: a hole through a boss through a plate is just a tree of three solids.
Whether a point is inside a solid is decided by a ray: an odd number of crossings with its triangles. But the triangles are up to the tolerance off the exact surface. So where the point is closer than that to another solid's face, the exact face decides: its nearest point and the side of its normal. At a crease both sides count – behind both of a convex one, behind either of a concave one. And where a region is so thin that every point of it is too close to another solid's face, it takes the answer from a neighbouring region: across a cut with a solid K, "inside K" flips to "outside K"; across a cut with another solid, it stays.
7. How it is checked
Fixed tests check cases with a known result: known volumes (a box with a hole, the upper half of a ball, a drilled ball, 4/3 π (R² − r²)^(3/2), a plate with drilled bosses, three solids meeting in points), faces in one plane, touching, and every case that was ever wrong. Where there is no formula, the volumes must add up: V(a ∪ b) + V(a ∩ b) = V(a) + V(b) and V(a − b) = V(a) − V(a ∩ b). Fuzzers build random pairs, trees of three to six solids, solids on a grid and parts made of plates with bosses, holes and slots, also turned as a whole. Every result is checked: closed (also after rounding in the STL), volumes that add up, and tens of thousands of random points tested against the operands. On request the library Manifold also computes the same operation on the operands' meshes: a result can be closed and have volumes that add up while a small piece too many stands somewhere; only the Hausdorff distance between the two results shows that. Places thinner than the meshes can resolve don't count. Of the random pairs hardly one is wrong, of the parts a few percent, nearly all in the special positions of section 4. Browser and command line give the same bytes for every example.
8. Traps and the rules against them
What the fuzzers uncover becomes a rule in the code, with a test next to it. Nearly always a tolerance is behind it that does not fit another one, or a special position as people draw them: whole millimetres, centres on sides, holes through bosses. The rule against it is then a general method, not a special case. Three examples:
Splits and triple points. A cut follows the exact curve to 0.0001 mm. So a split or a triple point has to be recognised as a point of the cut with the same tolerance, not within 10⁻⁶ of the size – otherwise it is dropped. This one number can cause half of all failures.
Three touches in one point: 3-4-5. A dome of radius 5 sits 4 mm from the edge of a plate 3 mm thick. Then the plate's lower edge touches the ball, and the circles the ball cuts into the bottom and into the end face touch the edge and each other in the same point. If their common point lay a hair's breadth off the edge, the edge would stay whole there, and four triangles would hang on it. So it lies exactly on the edge and is one of its vertices. Because curves that touch move apart only slowly, all of them get points at the same distances towards the point of contact; otherwise their polylines would cross. Where three cuts touch in one point (the same dome over a slot), all three get the same point. With parts in whole millimetres this is common.
The point a region is decided at. A box, a copy of it moved 0.21 mm, and a prism through both: on the prism's wall there are strips 0.04 mm wide between the two boxes' sides. Any point of such a strip may lie 0.0005 mm beyond the exact cut, because the polyline follows it only to the tolerance. So the point furthest from the other solids' faces counts. Points right on another face (caps in one plane) count as equally close, or rounding noise would decide.
Try it yourself
cargo run --release -p kf -- render examples/bracket.kf -o out/bracket.png
cargo run --release -p kf -- render examples/mug.kf -D grip=30 -o out/mug.stl
cargo run --release -p kf -- render examples/urchin.kf -D whole=1 -o out/urchin.stl
- Make the holes of the bracket larger than the bosses
(
hole>boss). What happens to the bosses, and why does the plate still close? - In
mug.kf, move the handle inwards until it pokes into the cavity. Which part of the torus does the difference take away? - Build
intersection(sphere(10), box(16, 16, 16)). What does it look like, and which faces of the result come from the ball, which from the box? - Something to think about: why does the test "a point in front, a point behind" also give the right direction for the triangles of a region that comes from the subtracted solid b?
- Build a slot: a box and, at each end, a cylinder whose axis stands exactly on that end and whose diameter is the box's width. The box's sides only touch the cylinders. Why does the cylinder still get a line where the box's edge lies?