Lesson 3.6 · III Modelling
Where surfaces meet
What it's about: Two surfaces that cross meet along
a curve. Finding this curve is the most important building block of a
CAD kernel. The Booleans (Lesson 3.7) cut faces along it; general CAD
kernels need it for fillets and walls as well.
intersect(a, b) gives these curves as a list.

let ball = sphere(10)
let rod = cylinder((-25, 0, 3), (25, 0, 3), 6)
export [tube(c, 0.4) | c <- intersect(ball, rod)] color "plum"
To use it, this is enough: the list is empty if the surfaces do not cross or only touch in one point; each curve is closed if it goes all the way round, and otherwise ends exactly on the edge of a surface; if two surfaces lie on each other, there is a message. The rest of the lesson shows how the curves are found – section 1 why there is no formula, sections 2 to 4 the method, section 6 how it is checked. Assumed: Newton's method from Lesson 3.5 and the Hermite pieces from Lesson 3.3.
1. Not a NURBS curve either
A plane cuts a ball in a circle, and a circle is a NURBS curve. That is the exception. Two cylinders at right angles already meet in a curve of degree 4 that no NURBS curve reproduces exactly. Two bicubic patches, the most common pieces of free-form surfaces, can meet in a curve of algebraic degree 324. Nobody writes that down as a formula.
So it goes as with the offset (Lesson 3.3): exact points, cubic pieces in between, and a check that the pieces stay within 0.0001 mm of the exact curve.
2. Four unknowns, three equations
A point of the intersection lies on both surfaces: S₁(u₁, v₁) = S₂(u₂, v₂). These are three equations (x, y and z), for four unknowns (u and v on each surface). One equation is missing, and that is right: a curve is a one-dimensional set of solutions, and the fourth equation says which of its points is meant. Here it is a plane across the curve: "the point where the curve passes through this plane".
The four equations are solved with Newton's method, as in Lessons 3.5 and 3.3, here with a 4×4 system. Two precautions:
- No jumping. A step of Newton's method may move each parameter by at most a tenth of its range. Otherwise it can land on another branch of the curve, if there is one nearby.
- At the edge of a surface the plane is replaced by the boundary itself: "the point of the curve where u₁ = 0". The end of a branch then lies exactly on the edge, not somewhere close.
3. Following the curve
The curve runs at right angles to both normals, that is along t = n₁ × n₂. From one point to the next:
- Ahead along t, by a step h.
- Back onto the curve: Newton, with the plane through the point ahead, at right angles to t.
- Check: between the two points lies a cubic piece through both, in the exact directions (Hermite, as in Lesson 3.3). At a quarter, half and three quarters of the way it is compared with the exact curve. Is it more than 0.0001 mm off, or has the direction turned by more than 20° in one step, the step is halved. If it fits, the next step may be one and a half times as long.
A branch ends:
- where it comes back to its start: a closed curve, like the ring where a tube passes through a ball;
- where it leaves a surface, exactly on its edge;
- where the surfaces touch: there n₁ and n₂ are parallel, n₁ × n₂ is zero and there is no direction. What happens there depends on how the surfaces bend away from each other (section 4).
A branch found in the middle is followed in both directions and joined. Two open branches whose ends meet become one. That happens at the pole of a revolved surface, where the direction gets lost: the curve is followed up to it from both sides.
Corners. A surface with a crease (a hexagonal prism, a cone where its side meets the floor) has no normal on the crease, and the curve has a corner there. A step that crosses a crease therefore ends on it: the fourth equation becomes "u₁ is the knot of the crease", and the node there gets two directions, the one arriving and the one leaving. A hexagonal prism cut by a slanted plane gives one closed ring with six corners.
4. Where to start
Following needs a first point on each branch. For that, both surfaces are cut into coarse triangles (as for the STL, with a tolerance of a five-hundredth of each surface's size, and at least eight steps in each direction). Wherever a triangle of one surface comes closer to a triangle of the other than the two may deviate from their surfaces, the surfaces may meet there. From the middle of the smaller triangle and the closest point on the other one, Newton's method looks for a point on both surfaces: three equations, four unknowns, and each step the smallest one that solves them. If it finds one, that is a start point; if it gets no closer, the surfaces only came near each other there.
Taking only triangles whose edges cross is not enough. That misses every intersection that lies within the triangles' deviation: a plane 0.01 mm inside the side of a cylinder cuts it in two lines 20 mm long, but the coarse triangles of the cylinder do not even reach the plane.
There are many start points along every curve. After a branch has been followed, a start point is skipped if it lies on one of its pieces (measured by projecting it onto the piece, to within four times the tolerance).
Touching. Where two surfaces only touch, S₁ − S₂ is almost flat all around. Newton's method "solves" the equations there in a small ring already, since the surfaces are closer than the tolerance there, and that would become a bogus curve. That is why knotflow looks, from every new start point, for the nearest place where both normals are parallel: a critical point of the distance between the surfaces. It satisfies four equations: S₁ − S₂ is at right angles to both derivatives of S₁, and the normal of S₁ is at right angles to both derivatives of S₂. If the surfaces are at most the tolerance apart there, they touch, and what counts is how they bend away from each other. From the common tangent plane, each surface rises by ½ wᵀ II w for a small step w, with its second fundamental form II; their distance is ½ wᵀ (II₁ − II₂) w. The two eigenvalues λ₁, λ₂ of this difference decide:
| λ₁, λ₂ | Meaning | Example |
|---|---|---|
| of one sign | a single point of contact | a ball on a plane |
| of different signs | two branches cross here, along the directions with wᵀ (II₁ − II₂) w = 0 | a saddle on its tangent plane, two cylinders of one radius at right angles |
| one of them zero | contact along a curve, in the direction of its eigenvector | a cylinder lying on a plane |
| both zero | contact of higher order | a ball in a bowl of its own radius |
"Zero" means here: curved so little that the surfaces part by less
than the tolerance over the size of the parts. A start point in the zone
round a touch where the surfaces are closer than the tolerance is that
touch, and becomes no curve. A point of contact is left out. Contact
along a curve is reported by intersect, and the Booleans
let the two solids touch there. In contact of higher order the second
order does not tell how far the zone reaches: where there is a
direction, it is followed (the monkey saddle z = x³ − 3xy² crosses its
tangent plane in three straight lines through the origin); where there
is none, the surfaces lie on each other there, and such places far apart
are reported. Where two branches cross, following them mostly steps
right across; if it stalls there, it goes straight on through, and the
other branch starts at the crossing. On a crease a surface has two
tangent planes, and a touch counts only if the normals are parallel on
both sides. The end of a rounding's arc touches the face it rolls along,
but the flat piece after it stands across that face: that is a
curve.
(Patrikalakis and Maekawa write the same form in the parameters of the first surface, with the discriminant b₁₂² − b₁₁b₂₂; it has the sign of −λ₁λ₂. The eigenvalues, though, do not depend on how a surface is parametrised, and their eigenvectors give the directions as well.)
One trap remains:
- Tiny loops. A loop is found if the coarse triangles come close enough to each other there. A plane just below the top of a ball of 20 mm gives loops down to a radius of 0.014 mm. There is no proof that nothing is missed, though. Programs that must not miss anything prove with bounds on the normals that a patch cannot contain a loop; knotflow does not. How research finds small loops and touching points for certain is shown in Lesson 3.15.
If two surfaces lie on each other (a ball and the same ball, turned), there is no curve but a shared area; that is reported.
5. What comes out
Each branch is a cubic curve in space, made of the checked pieces. In
addition, for every node, it keeps where it lies on both
surfaces, as (u, v). In the language intersect returns only
the curve in space; the Booleans need the (u, v) as well: to cut a
surface along the curve, you need the curve in the surface's own
parameters.
| Test | Known result | Measured |
|---|---|---|
| Plane z = 6 through a ball of radius 10 | circle of radius 8, length 2π · 8 | every point within 0.0001 mm, length to 0.001 mm |
| Two balls of radius 10, centres 12 apart | circle of radius 8 in the plane x = 6 | within 0.0001 mm |
| Cylinders of radius 5 (upright) and 4 (lying) | two closed curves, 5 from one axis and 4 from the other | within 0.0002 mm |
| Cylinder cut by an upright plane at x = 3 | two straight lines at y = ±4, from z = 0 to z = 10 | ends exactly on the edges (10⁻⁹) |
| Surfaces apart, or touching in one point | nothing | an empty list |
| Cylinder lying on a plane | contact along a straight line | a message (intersect), nothing (Booleans) |
| Saddle z = x·y and the plane z = 0 | both axes, crossing at the origin | 4 long together, to 10⁻⁶ |
| Two cylinders of radius 5 at right angles | two ellipses that cross twice | their length to 10⁻⁴ |
Plane through the axis of cone(10, 5, 20) |
the trapezoid, caps included | one closed curve, 71.231 long |
| Hexagonal prism, slanted plane | one ring with six corners | closed, the corners on the creases |
Both intersections in the picture together take about 20 ms.
6. How it is checked
The tests above check cases with a known result. For everything else there is an independent cross-check: every piece of every curve is projected onto both surfaces (does it really lie on both?), and, to find what is missing, fine triangles of both surfaces are intersected with each other and every crossing is compared with the curves found. A fuzzer runs this over random pairs of surfaces.
The rules of sections 3 and 4 each close a trap that this cross-check uncovers. Three examples:
- "Already found" with a fixed reach swallows rings. If a start point counted as "already on a branch" whenever it lay within the spacing of the samples, a vase with a 0.2 mm wall, cut by a plane, would lose its second ring. That is why a start point is projected onto the pieces themselves and only counts if it lies within four times the tolerance.
- Flat parts need grid lines. A flat floor is one span of degree 1; the coarse triangles would have no lines inside it, and a plane through the axis of a cone frustum would lose half of the floor. That is why every direction has at least eight steps.
- What a crease is depends on the knot's multiplicity. A circle's knots are no creases (otherwise the curve would be handed back and forth between them forever), but the seam of a closed polygon is.
Nearly always such a trap comes from a tolerance or a comparison that holds for the examples one has in mind (a radius that is too large, coordinates in place of sizes, "the edges cross" instead of "they come close") – the same experience as in Lesson 3.3.
Try it yourself
cargo run --release -p kf -- render examples/intersections.kf -o out/intersections.png
cargo run --release -p kf -- render examples/intersections.kf -D lift=4 -D width=6 -o out/x.png
- Move the tube up with
lift. At which height do the two rings become one? What happens in between, when the tube only just grazes the ball from inside? intersect(sphere(10), move(sphere(10), x: 20)): the balls touch in one point. What doesintersectreturn, and why?- Tilt the plane through the vase further (
tilt). When does the curve stop being one ring? - Something to think about: why is the fourth equation a plane across the curve, and not, for example, "u₁ has this value"? When would "u₁ has this value" fail?