Skip to content

CAD kernel foundations · Part ten

Intersecting curves and surfaces

Where two pieces of geometry meet, how they meet, and how to find all of it

A Boolean starts by asking where the faces of one solid meet the faces of the other, and a fillet, an offset and a section start the same way. The answer has to be complete, because a missed branch leaves a hole in the result, and it has to be classified, because a face that touches another must be treated differently from one that crosses it. In this article we build the intersection algorithms of a kernel in order: the closed forms for the elementary geometry, with tangency decided as a distance; then the numerical methods for everything else, from two curves to the marching of two surfaces; and finally the closed loops that no starting point on a boundary can reach.

What an intersection returns

Let’s start with what the caller needs back. Two curves meet at points, a curve meets a surface at points, and two surfaces meet along curves. Each point should come with its parameters on both pieces of geometry, because the caller will split an edge or a face there and needs to know where in its domain. Each curve of a surface–surface intersection should come with its two images in the parameter rectangles of the surfaces, the pcurves that part one attached to every coedge, because the curve is about to become the edge of two new faces.

Points and curves are not enough. Take a plane and a cylinder. The plane can cut the cylinder in two lines, touch it along one line, or miss it, and these three answers lead a Boolean down three different paths: two lines split the face, a line of contact may only mark where two faces touch, and nothing means nothing to do. A plane through the cylinder’s axis direction can also lie on a face of a box that is flush with the cylinder, in which case the two faces overlap over an area. So an intersection routine returns one of four kinds of answer:

  • transversal: isolated points or curves where the two pieces cross;
  • tangent: points or curves where they touch without crossing;
  • coincident: the two share a whole curve or a whole region;
  • empty.

So each pair of analytic types gets a result that names its case, such as two lines, a tangent line, coincident or empty, and the caller has to handle each one. Every point comes with its multiplicity as well, 1 where the two pieces cross and 2 where they touch, so the caller never has to work out which it was. The classification is where most of the difficulty lies, and section three is about it. First, the two ways an intersection can be computed at all.

Two routes: substitution and iteration

Part three wrote each elementary surface parametrically, as a map from (u, v) to a point. Each one also has an implicit form, an equation F(X) = 0 that a point satisfies exactly when it lies on the surface. With W = X − C measured from the surface’s centre or apex, and A its unit axis, the implicit forms are:

SurfaceF(X) = 0Degree
Plane, normal nn·W1
Sphere, radius r|W|2 − r22
Cylinder, radius r|W|2 − (W·A)2 − r22
Cone, half-angle α, apex C(W·A)2 − cos2α |W|22
Torus, radii R, r(|W|2 + R2 − r2)2 − 4R2(|W|2 − (W·A)2)4

The cone’s equation describes both nappes, as the cone of part three does. If one piece of geometry has an implicit form and the other a parametric one, we can substitute: put the parametric curve into the implicit equation and get one equation in one unknown. A line O + tD substituted into a surface of degree n gives a polynomial of degree n in t, so a line meets a plane once, a quadric at most twice, and a torus at most four times. A conic substituted into a quadric gives degree at most four. These are closed forms, and every root they produce is exact to rounding.

When neither piece has a usable implicit form, which is the case for every B-spline, we iterate: write the condition that the two parametrisations give the same point, and solve it numerically with Newton’s method from starting points that a search has to supply. This is the route of sections eight to eleven. It works for any geometry, but it inherits the two problems of part six: an iteration converges to whatever is near its start, so the search must not miss anything, and at a tangency the equations become singular.

The two routes also say what kind of object an intersection curve is. Two quadrics meet in a curve of degree four in general, a space quartic, which is not one of the ten elementary geometries. Only in special positions does it break into conics and lines, and section six covers those. The quartic itself still has an exact description, because a cylinder and a cone are made of straight lines, and section seven builds it from them. Free-form surfaces, and a torus out of its special positions, go through the numerical route.

Tangency is a distance

Let’s take the simplest case where tangency matters: a line and a circle in a plane. A circle of radius 1 and a horizontal line at height 1 − d meet where x2 + (1 − d)2 = 1, at x = ±√(2d − d2). For d > 0 there are two roots, for d = 0 a double root, for d < 0 none.

Exact zero never happens in floating point, so the routine needs a tolerance, and the natural first attempt is to apply it to the roots: if the two roots are within 10−7 of each other, call them one tangent point. Let’s see what that does.

CUT, TOUCH, MISS two points one, tangent none TWO ROOTS, HOW FAR APART 1e-16 1e-12 1e-8 1e-4 1e-8 1e-4 1e0 8.9e-4 on the roots on the distance how far inside the circle the line is against the gap between its two roots
The gap between the two roots grows as the square root of how far inside the circle the line is: 2√(2d). A line 10−7 inside, one tolerance, has roots 8.9 × 10−4 apart, nine thousand tolerances. For the gap to fall to 10−7 the line must be within about 10−15 of touching, which is below the rounding of its own coordinates.

As we can see, a tolerance on the roots is a tolerance on the square root of the distance. A line that was built to touch the circle, and is off by rounding, comes out as two points almost a thousandth apart, or as nothing, depending on which way the rounding went. A Boolean given those two points splits the face at them and leaves a sliver between them.

The fix is to apply the tolerance to the quantity it is defined on, which is a distance in space. For the line and circle, the line’s closest approach to the centre is at distance δ = 1 − d, and the rule becomes: tangent if |δ − r| ≤ tolerance, two points if δ is smaller, nothing if larger. In general:

  1. Find the point of closest approach first. For a quadratic in t it is the vertex t* = −b/2a, the midpoint of the two roots, whether they are real or not.
  2. Measure the distance from the curve at t* to the surface. If it is within tolerance, report one tangent point at t*, with multiplicity two.
  3. Otherwise the sign of the discriminant decides between two points and none, and it is now safe, because the two roots are clearly separated.

With a tolerance of 10−7, a line along the top of the unit sphere, at height 1 + d, is one tangent point for every |d| ≤ 10−7. Just outside the band, at d = −2 × 10−7, it is two points already 1.265 × 10−3 apart, and above the band the line misses.

Two smaller rules make the closed forms of the next sections behave. First, centre the parameter. A line’s parameter starts wherever its origin happens to be, and a quadratic written about a far-away origin loses digits in its constant term; writing it about the closest approach, t = t* + s, makes s2 = r2 − δ2 the whole equation for a sphere. Second, solve quadratics without cancellation: compute q = −½(b + sign(b)√disc), and take the roots as q/a and c/q. The textbook formula subtracts two nearly equal numbers for the smaller root when the roots differ greatly in size.

Curves against curves in closed form

Two curves in space usually miss each other, so the closed forms for two curves work in a plane. When both curves lie in one plane, one of them is written as an implicit equation in that plane’s coordinates and the other is substituted into it, as section two did for surfaces. When their planes cross, a curve can meet the other only where it pierces the other’s plane. Two lines are the exception, solved directly in space. We parametrise the conics in a frame with centre or vertex P and in-plane axes X and Y:

ellipse  P + a cos t X + b sin t Y

parabola  P + (t2/4f) X + tY

hyperbola  P + a cosh t X + b sinh t Y

with the circle as the ellipse with a = b = r, f the parabola’s focal length, and the hyperbola one branch. In the frame’s coordinates (x, y) their implicit equations are

x2/a2 + y2/b2 = 1,  y2 = 4fx,  x2/a2 − y2/b2 = 1

A line substituted into a conic gives a quadratic, a circle gives a trigonometric equation that becomes a quartic, and two ellipses give a quartic in one coordinate. Each card below gives the equation, the cases with their conditions, and a figure with a panel per case. The figures look straight down on the plane of the two curves, so circles stay round, except for the panels whose case needs depth.

Two lines

For the lines X1(s) = O1 + sD1 and X2(t) = O2 + tD2, with unit directions, write W = O1 − O2, c = D1·D2, d = D1·W and e = D2·W. The closest pair of points, where X1 − X2 is square to both lines, is at

s = (ce − d)/(1 − c2),  t = (e − cd)/(1 − c2)

and 1 − c2 = |D1 × D2|2 is the squared sine of the angle between the lines. The lines meet if the two points are within ε of each other, and are skew otherwise. When 1 − c2 ≤ εa the lines are parallel, and the distance δ from O1 to the second line decides between parallel and coincident. In a plane the same system is solved as two equations in s and t, with determinant D1 × D2, and there is no skew case.

CaseConditionIntersection
point1 − c2 > εa, |X1(s) − X2(t)| ≤ εX1(s), with s and t
skew1 − c2 > εa, |X1(s) − X2(t)| > εnone; s, t and the distance
parallel1 − c2 ≤ εa, δ > εnone; the distance δ
coincident1 − c2 ≤ εa, δ ≤ εthe whole line
point D1 × D2 ≠ 0 parallel δ = 0.5 coincident δ = 0 skew distance 0.6
Two lines four times. Crossing, they meet at one point. Parallel and half a unit apart, or lying on each other, they make the determinant vanish, and the distance between them decides. The last pair is skew: one line runs along x and the other along y 0.6 above it, and the routine returns the closest pair, 0.6 apart.

A line and a circle

In the circle’s plane, a line meets |x − C|2 = r2 as it meets a sphere. With W = O − C,

t2 + 2(W·D) t + |W|2 − r2 = 0

with vertex t0 = −W·D, the closest approach to the centre at distance δ = |W + t0D|, and roots t0 ± √(r2 − δ2). A line that crosses the circle’s plane meets it at one point, the line–plane point of section five, and that point is a hit only if it lies on the circle. A touch in the plane is a double root and a pierce is a single one, so the multiplicity returned with the point tells the caller which it was.

CaseConditionIntersection
two pointsin the plane, δ < r − εt0 ± √(r2 − δ2)
tangentin the plane, |δ − r| ≤ εt0, multiplicity 2
emptyin the plane, δ > r + εnone
one pointcrossing the plane within ε of the circlethe crossing point, multiplicity 1
emptycrossing the plane off the circle, or parallel to the plane and off itnone
two points δ = 0.5 tangent δ = r empty δ = 1.4 one point pierces, on it empty pierces, inside
A unit circle and five lines. In its plane, lines 0.5, 1 and 1.4 from the centre cross it twice, touch it and miss it. Out of its plane, a line through a point of the circle meets it once, a crossing of multiplicity 1. A line through the disc, inside the circle, misses it.

Two circles

In one plane, let d = |C2 − C1| be the distance between the centres and u = (C2 − C1)/d. Subtracting the two circles’ equations leaves a line square to u, the radical line, at distance a from C1, and the points lie on it:

a = (r12 − r22 + d2)/2d,  h = √(r12 − a2),  C1 + au ± hu⊥

The circles touch when d is within ε of r1 + r2, from outside, or of |r1 − r2|, from inside, and the point of contact is C1 + au. Circles in parallel planes miss. Circles in crossing planes meet only on the line where the planes cross, and the last card of this section solves them.

CaseConditionIntersection
points|r1 − r2| + ε < d < r1 + r2 − εC1 + au ± hu⊥
tangentd within ε of r1 + r2 or of |r1 − r2|C1 + au
emptyd > r1 + r2 + ε, d < |r1 − r2| − ε, or parallel planes apartnone
coincidentd ≤ ε and |r1 − r2| ≤ εthe whole circle
crossing planesthe planes crossup to two points, on the line where the planes meet: see the last card
two points d = 1.2 tangent d = r1 + r2 tangent d = r1 − r2 empty d > r1 + r2 empty d < r1 − r2 coincident d = 0, r1 = r2
A unit circle and a second circle in its plane, six times. With centres 1.2 apart they cross twice. They touch from outside and from inside, lie apart, lie one inside the other without touching, and coincide.

A line and a conic

Write the line in the conic’s frame as (ox + tdx, oy + tdy). The ellipse’s equation becomes a quadratic in t,

(dx2/a2 + dy2/b2) t2 + 2(oxdx/a2 + oydy/b2) t + ox2/a2 + oy2/b2 − 1 = 0

the hyperbola’s is the same with the b2 terms negated, and the parabola’s is

dy2 t2 + (2oydy − 4fdx) t + oy2 − 4fox = 0

Tangency is decided at the vertex, as in section three. The parabola’s leading coefficient vanishes for a line parallel to its axis, dy = 0, and the hyperbola’s for a line parallel to an asymptote, dx/a = ±dy/b. The equation is then linear, and its one root is a crossing. The hyperbola here is one branch, so a root on the other branch is dropped, and a line across both branches comes back with one hit. Only the double root at the vertex is a touch; every other single hit is a crossing, of multiplicity 1. A line that crosses the conic’s plane is handled as for the circle.

CaseConditionIntersection
two pointstwo roots, both on the conicthe two points
tangenta double root at the vertexone point, multiplicity 2
one pointa linear root, parallel to the parabola’s axis or an asymptote; one root left after the other branch; or crossing the plane on the conicone point, multiplicity 1
emptyno root on the conic, or crossing the plane off itnone
two points ellipse tangent ellipse empty ellipse one point parabola, ∥ axis one point one branch
A line and a conic in one plane, five times. An ellipse of radii 1.2 and 0.7 is crossed at two points, touched at the point of parameter 1 and missed. A line parallel to a parabola’s axis crosses it once. A horizontal line crosses both branches of a rectangular hyperbola, and the one hit on the branch we model is drawn solid. All three single hits are crossings, of multiplicity 1.

A circle and a conic

Put the circle’s centre at (cx, cy) in the conic’s frame and its points at (cx + r cos s, cy + r sin s), with s measured from the conic’s X. Each conic’s equation has the form αx2 + βy2 + γx + κ = 0: the ellipse with α = 1/a2, β = 1/b2, γ = 0, κ = −1; the parabola with α = 0, β = 1, γ = −4f, κ = 0; the hyperbola as the ellipse with β = −1/b2. Substituting the circle and collecting the products of sines and cosines gives

A cos 2s + B sin 2s + C cos s + D sin s + E = 0

A = r2(α − β)/2,  B = 0,  C = r(2αcx + γ),  D = 2rβcy

E = αcx2 + βcy2 + γcx + κ + r2(α + β)/2

with B = 0 because no conic has an xy term in its own frame. The half-angle substitution u = tan(s/2), with cos s = (1 − u2)/(1 + u2) and sin s = 2u/(1 + u2), multiplied through by (1 + u2)2, gives a quartic whose coefficients in ascending powers of u are

A + C + E,  4B + 2D,  −6A + 2E,  −4B + 2D,  A − C + E

so there are at most four hits. Each root is checked on the conic, which also drops the hyperbola’s other branch. The substitution cannot reach s = π, which is u = ∞, so s = π is tried separately. Near it, a root is a large u, and a tangency there is a double root at large u that rounding can turn into a pair of complex roots or split in two. Near s = π, decide tangency on the distance between the two conics at their closest approach, as section three does, and not on the roots. A double root away from s = π is one point of multiplicity 2, and the multiplicity is what separates two contacts from two crossings. A circle on a circular ellipse with its centre and radius is coincident, and the planes are tested as for two circles.

CaseConditionIntersection
pointstwo to four roots on the conicthe points, each of multiplicity 1 where they cross and 2 where they touch
tangentexactly one root on the conic, a double oneone point, multiplicity 2
emptyno root on the conic, or parallel planes apartnone
coincidentan ellipse with a = b = r, its centre within ε of the circle’sthe whole circle
crossing planesthe planes crossup to two points, on the line where the planes meet: see the last card
four points r = 0.95 two points parabola tangent at a vertex two points r = b, touching empty r = 0.4 coincident a = b = r
A circle and a conic in one plane, six times. Against an ellipse of radii 1.2 and 0.7, a concentric circle of radius 0.95 crosses it four times. A circle of radius 0.7 crosses a parabola twice. A circle of radius 0.3 touches the ellipse from inside at the end of its major axis, and a concentric circle of radius 0.7 touches it at both ends of its minor axis, two points of multiplicity 2. A circle of radius 0.4 lies inside it, and the last circle lies on an ellipse whose two radii equal its own.

Two ellipses

Work in the first ellipse’s frame, where it is x2/a12 + y2/b12 = 1. The second, with centre c and axes the columns of a rotation R in that frame, is (x − c)TQ(x − c) = 1 with Q = R diag(1/a22, 1/b22) RT, which expands to a general conic

qax2 + qby2 + qcxy + qdx + qey + qf = 0

with qa = Q11, qb = Q22, qc = 2Q12, (qd, qe) = −2Qc and qf = cTQc − 1. Read it as a quadratic in y, with β(x) = qcx + qe its linear coefficient, and replace y2 by the first ellipse’s δ(x) = b12(1 − x2/a12). What is left is linear in y, β(x) y + η(x) = 0 with η(x) = qbδ(x) + qax2 + qdx + qf, and squaring it against y2 = δ(x) gives a quartic in x:

β(x)2 δ(x) − η(x)2 = 0

Each real root gives y = −η/β, or both ±√δ where β vanishes, and a point is kept when it lies on both ellipses within ε, which removes the roots the squaring added. Where the two values ±√δ are within ε of each other the ellipses touch at the end of the first one’s major axis, and we take the point on the axis, since √δ magnifies the rounding of x there. This quartic has no point it cannot reach, unlike the half-angle one. Ellipses with the same centre and radii coincide when their major axes are parallel, or when both are circles.

CaseConditionIntersection
pointstwo to four points keptthe points, each of multiplicity 1 where they cross and 2 where they touch
tangentexactly one point kept, a double oneone point, multiplicity 2
emptyno point kept, or parallel planes apartnone
coincidentsame centre and radii within ε, major axes parallelthe whole ellipse
crossing planesthe planes crossup to two points, on the line where the planes meet: see the last card
four points turned 90° two points offset tangent outside two points both touching empty nested coincident X reversed
Two ellipses in one plane, six times, the first of radii 1.2 and 0.6. The same ellipse turned 90° crosses it four times, and a smaller one, off-centre and turned 30°, crosses it twice. One of radii 0.6 and 0.3 touches it from above. A concentric one of radii 1.2 and 0.3 touches it at both ends of its major axis, two points of multiplicity 2. One lies inside it, and the same ellipse with its axis reversed coincides with it.

Conics in crossing planes

Two conics whose planes cross can only meet on the line L where the planes cross, so we intersect each conic with L in its own plane and compare. With the planes n1·X = d1 and n2·X = d2 and m = n1 × n2, the line is L0 + λu, with

L0 = (d1 n2 × m + d2 m × n1)/|m|2,  u = m/|m|

Each conic against L is the line–conic card above, in that conic’s plane, and gives at most two values of λ. A value that both conics share within ε is a hit. So two conics in crossing planes meet at most twice, and they cannot coincide. A hit is a crossing, of multiplicity 1, unless both conics touch L at that point. Then they share the tangent u there, and the hit has multiplicity 2.

CaseConditionIntersection
two pointstwo values of λ shared within εL0 + λu at each, multiplicity 1
one pointone value sharedthe point, multiplicity 1, or 2 where both conics touch L
emptyno value shared, or a conic that misses Lnone
two hits c = 0 one hit c = 2 empty c = 1
A unit circle lying flat and a unit circle standing upright, its centre c along the line where the planes cross. Centred together, both circles meet that line at ±1 and cross at two points. Moved two units along it, the upright circle meets the line at 1 and 3, and the circles share one point. Moved one unit, it meets the line at 0 and 2, the flat one at ±1, and they share none.

Curves against surfaces in closed form

A curve meets a surface where its parametrisation satisfies the surface’s implicit equation, and substituting one into the other leaves a single equation in the curve’s parameter. Its roots are the hits. The three quadrics share one form,

(x − Q)TM(x − Q) = k

with Q the sphere’s centre, a point of the cylinder’s axis or the cone’s apex. For the sphere, M = I and k = r2. For the cylinder with unit axis a, M = I − aaT and k = r2, and for the double cone of half-angle α, M = aaT − cos2α I and k = 0. A line gives a quadratic, a conic a quartic, and the torus a quartic for a line. Each card below gives the equation, the cases with their conditions, and a figure with a panel per case. A tangent hit is a double root, and it is decided as section three argued, on the distance from the surface at the vertex of the equation.

A line and a plane

For the line X(t) = O + tD, with D a unit vector, and the plane n·(x − P) = 0, substitution gives a linear equation:

(n·D) t + n·(O − P) = 0

CaseConditionIntersection
point|n·D| > εat = −n·(O − P)/n·D
parallel|n·D| ≤ εa, |n·(O − P)| > εnone
coincident|n·D| ≤ εa, |n·(O − P)| ≤ εthe whole line
point n·D ≠ 0 parallel n·D = 0, δ = 0.6 coincident n·D = 0, δ = 0
A line and a plane three times: crossing it, parallel to it 0.6 above, and lying in it.

A line and a sphere

With W = O − C, the sphere’s equation along the line is

t2 + 2(W·D) t + |W|2 − r2 = 0

Its vertex t0 = −W·D is the line’s closest approach to the centre, at a distance δ = |W + t0D|, and the roots are t0 ± √(r2 − δ2).

CaseConditionIntersection
two pointsδ < r − εt0 ± √(r2 − δ2)
tangent|δ − r| ≤ εt0
emptyδ > r + εnone
two points δ = 0.5 tangent δ = r empty δ = 1.4
A unit sphere and a horizontal line at three distances from its centre. At 0.5 it passes through at two points 1.73 apart, at 1 it touches the top, and at 1.4 it passes above. The line is dashed where the sphere hides it.

A line and a cylinder

With W = O − Q, wa = W·a and da = D·a, the cylinder’s equation along the line is

(1 − da2) t2 + 2(W·D − wada) t + |W|2 − wa2 − r2 = 0

The leading coefficient is sin2θ, with θ the angle between the line and the axis. While it is nonzero, the vertex is the line’s closest approach to the axis, at a distance δ, and the cases are the sphere’s. When it is zero the line is parallel to the axis, the linear term vanishes with it, and the line lies on the cylinder or misses it.

CaseConditionIntersection
two pointssin2θ > εa, δ < r − εthe two roots
tangentsin2θ > εa, |δ − r| ≤ εthe vertex
emptysin2θ > εa, δ > r + εnone
coincidentsin2θ ≤ εa, the line on the cylinderthe whole line, a generator
emptysin2θ ≤ εa, the line off itnone
two points δ = 0.4 tangent δ = r empty δ = 1.4 coincident ∥ axis, δ = r empty ∥ axis, δ = 0.5
A cylinder of radius 1 and five lines. Three run square to the axis, at distances 0.4, 1 and 1.4 from it. Two run parallel to it: one at distance 1 is a generator, and one at 0.5 runs inside without meeting it.

A line and a cone

With W = O − Q from the apex, the double cone’s equation along the line is

(da2 − cos2α) t2 + 2(wada − cos2α W·D) t + wa2 − cos2α |W|2 = 0

The leading coefficient is cos2θ − cos2α, and the angle θ between the line and the axis sorts the lines. A line steeper than the cone, θ < α, is inside the cone at both ends, so it crosses the surface twice, once on each nappe. A shallower line, θ > α, is outside at both ends, and it crosses one nappe twice, touches it, or misses it. A line parallel to a generator, θ = α, leaves a linear equation and meets the cone once. If the linear term vanishes too, the line is a generator or lies in the plane tangent along one without touching it. Any line through the apex has a double root there, because the cone is singular at its apex, so the apex comes back with multiplicity 2.

CaseConditionIntersection
two points|cos2θ − cos2α| > εa, two roots, the vertex off the coneone on each nappe when θ < α
tangent|cos2θ − cos2α| > εa, the vertex within ε of the conethe vertex: in a tangent plane, or the apex
emptyθ > α, no real rootnone
one point|cos2θ − cos2α| ≤ εa, linear term above εt = −c/b, constant over linear coefficient
coincident|cos2θ − cos2α| ≤ εa, linear term within ε, the line on the conethe whole line, a generator
emptythe same, the line off the conenone
two points one nappe two points both nappes tangent tangent plane empty δ = 0.8 one point ∥ a generator coincident a generator
A double cone of half-angle 30° and six lines. A horizontal line crosses the upper nappe twice, and a line 15° from the axis crosses each nappe once. A line in the plane tangent along a generator touches the cone where it crosses that generator, and a horizontal line 0.8 from the axis passes it. A line parallel to a generator meets the lower nappe once, and the generator itself lies on the cone.

A line and a torus

The torus gives a quartic. Writing ρ2(t) = |W + tD|2 − (wa + tda)2 for the squared distance from the axis, and k = |W|2 + R2 − r2, the equation (t2 + 2(W·D)t + k)2 = 4R2ρ2(t) expands to

t4 + 4m t3 + (4m2 + 2k − 4R2(1 − da2)) t2

+ (4mk − 8R2(m − wada)) t + k2 − 4R2(|W|2 − wa2) = 0,  m = W·D

A quartic has up to four real roots, and tangency appears as a double root. The tangency rule of section three still applies, with the critical points of the quartic (the roots of its derivative, a cubic) in place of the single vertex: wherever the curve’s distance to the torus at a critical point is within tolerance, the roots on either side of it merge into one tangent point.

CaseConditionIntersection
hitsreal roots, after merging at touching critical pointsup to four points, each with its multiplicity: 2 for a tangent hit
emptyno real rootnone
four hits z = 0.5 two hits offset 2.5 two tangent hits z = r one tangent hit offset R + r empty z = 1.4
A 3 by 1 torus and five lines. Across the middle at height 0.5, a line crosses the tube four times, at 2.134 and 3.866 from the axis on each side. Offset 2.5 from the axis, it crosses the outer side of the tube twice. Along the top, at height 1, it touches the tube twice, in two double roots at exactly t = ±3 from the axis. Offset 4 it touches the outer equator once, and at height 1.4 it passes above.

A conic and a plane

With the conics parametrised as in section four and e = n·(P − P0) for the plane through P0, the plane’s equation along each is short. For the ellipse, with C = a n·X and D = b n·Y,

C cos t + D sin t + e = 0,  t = φ ± arccos(−e/√(C2+D2)),  φ = atan2(D, C)

tangent when |e| is within εs of √(C2+D2), with s a length scale, |P − P0| + a + b for an ellipse and |P − P0| + r + 1 for a circle. The parabola gives a quadratic in t, (n·X/4f) t2 + (n·Y) t + e = 0. The hyperbola gives C cosh t + D sinh t + e = 0, which with u = et becomes the quadratic (C + D) u2 + 2e u + C − D = 0, keeping the roots with u > 0. When the conic’s plane is parallel to the plane, n·X = n·Y = 0, and e alone decides.

CaseConditionIntersection
hitsthe planes cross, real rootsup to two points, each with its multiplicity
emptyno real root, or the planes parallel and |e| > εnone
circlea circle with its plane parallel and |e| ≤ εthe circle itself
coincidentan ellipse, parabola or hyperbola in the planethe whole curve
two hits circle, δ = 0.4 one tangent hit circle, δ = r empty circle, δ = 1.2 circle in the plane two hits parabola
A horizontal plane and five conics. An upright circle of radius 0.9 centred 0.4 above the plane crosses it twice. Centred 0.9 above, it rests on the plane at one point, a double root, and centred 1.2 above it clears it. A circle lying in the plane comes back as itself, and a parabola with its vertex 0.6 below crosses it twice.

A conic and a quadric

Substituting the ellipse into the quadric form, with W = P − Q, the products of sines and cosines collect into

A cos 2t + B sin 2t + C cos t + D sin t + E = 0

A = ½(a2XTMX − b2YTMY),  B = ab XTMY,  C = 2a XTMW,  D = 2b YTMW

E = WTMW + ½(a2XTMX + b2YTMY) − k

and the half-angle substitution of section four, s = tan(t/2), turns it into the same quartic, which in ascending powers of s has the coefficients

A + C + E,  4B + 2D,  −6A + 2E,  −4B + 2D,  A − C + E

It has the blind spot part six found: t = π is s = ∞, so t = π is always added as a candidate. The parabola needs no substitution, because it is already polynomial:

(XTMX/16f2) t4 + (XTMY/2f) t3

+ (YTMY + XTMW/2f) t2 + 2YTMW t + WTMW − k = 0

The hyperbola gives the same five-term equation with cosh and sinh, its A and E having ½(a2XTMX + b2YTMY) and ½(a2XTMX − b2YTMY) in place of the ellipse’s. With u = et, multiplying by 2u2 gives the quartic

(A + B) u4 + (C + D) u3 + 2E u2 + (C − D) u + A − B = 0,  u > 0

Each candidate is then checked by evaluating the conic there and measuring its distance to the surface. That check is the final word, and its tolerance should be at least a few units in the last place of the point’s largest coordinate: a point a thousand units from the origin cannot be placed more finely than about 10−13, whatever the tolerance says.

The degree bound gives a cheap test for coincidence. On a conic, a quadric’s equation is a polynomial of degree at most four, in tan(t/2), in t or in et. A nonzero polynomial of degree four has at most four roots, so if five distinct points of the conic lie on the surface, the polynomial is identically zero and the whole conic lies on it.

CaseConditionIntersection
hitscandidates that pass the distance checkup to four points, each with its multiplicity: 2 for a tangent hit
emptyno candidate passesnone
circlea circle lying on the surfacethe circle itself
coincidentan ellipse, parabola or hyperbola with five distinct points on the surfacethe whole curve
two hits circle, sphere four hits circle, cylinder two tangent hits circle, cylinder circle a latitude coincident a plane section
Conics against a unit sphere and a cylinder of radius 1. An upright circle of radius 0.8 centred 0.9 above the sphere’s centre crosses it twice. An upright circle of radius 1.3 centred on the cylinder’s axis crosses it four times, and one of radius 1 touches it at two points, both double roots. A circle of latitude lies on the sphere, and the ellipse cut from the cylinder by a plane tilted 45° lies on the cylinder, so both come back whole.

Surfaces against surfaces in closed form

Two surfaces meet in circles, conics and lines only in special positions, and a kernel recognises the position first and computes second. This section covers those positions; section seven takes up two quadrics in general position, where the intersection is a quartic. Each pair has a table of its cases, with the condition that selects each one and the result, and a figure with a panel per case. Throughout, ε is the distance tolerance and εa the angular one, typically 10−7 and 10−12, and a tangent case is a band of width ε around an equality, as section three argued. Where a condition is a squared length, the band is scaled to match.

Plane and plane

With unit normals n1 and n2, the line runs along d = n1 × n2. A point on it satisfies both plane equations ni·x = ei. Set to zero the coordinate in which d is largest, and the two equations leave a 2 by 2 system in the other two, whose determinant is that largest component of d.

CaseConditionIntersection
line|d| > εathe line through that point along d
parallel|d| ≤ εa, planes more than ε apartnone
coincident|d| ≤ εa, planes within εthe whole plane
line normals cross parallel δ = 0.8 coincident δ = 0
Two planes three times. Crossing, they meet in a line along the cross product of their normals; parallel, they are apart or the same plane.

Plane and sphere

Let δ = n·(C − P) be the signed distance from the sphere’s centre C to the plane through P, and F = C − δn the foot of the centre on the plane.

CaseConditionIntersection
circle|δ| < r − εcentre F, axis n, radius √(r2 − δ2)
tangent point||δ| − r| ≤ εthe point F
empty|δ| > r + εnone
circle δ = 0.5 tangent point δ = r empty δ = 1.4
A unit sphere and a plane at three distances from its centre. At 0.5 the section is a circle of radius 0.866; at 1 it is the top of the sphere, a tangent point.

Sphere and sphere

With radii R and r, centres d apart and e the unit vector from the first centre to the second, subtracting the two sphere equations leaves a plane square to e, the radical plane, at

h = (d2 + R2 − r2) / 2d,  ρ2 = R2 − h2

from the first centre C1, and the intersection is the first sphere cut by that plane. ρ2 is a squared length, so its band is 2ε max(R + r + d, 1), about ε times the size of the configuration.

CaseConditionIntersection
circleρ2 above the bandcentre C1 + he, axis e, radius ρ
tangent pointρ2 within the band: d = R + r or d = |R − r|the point C1 + he
emptyρ2 below the band, or d ≤ ε with |R − r| > εnone
coincidentd ≤ ε and |R − r| ≤ εthe whole sphere
circle d = 1.3 tangent point d = R + r tangent point d = R − r empty d = 2.3 coincident d = 0, R = r
A unit sphere and a second sphere five times. Overlapping, they meet in a circle in the radical plane. They touch from outside at d = R + r and from inside at d = R − r, both a single point. Apart, or one inside the other without touching, they do not meet, and with the same centre and radius they are the same sphere.

Plane and cylinder

Let a be the cylinder’s axis, θ the angle between the plane’s normal and a, and s = n·a = cos θ. Where the axis crosses the plane, which is the centre of the section, the cylinder’s cross-section is stretched along the axis’s shadow on the plane by 1/|cos θ|. When the plane is parallel to the axis, the distance δ from the axis to the plane decides, as for the sphere.

CaseConditionIntersection
circlesin θ ≤ εaradius r
ellipsesin θ > εa, |s| > εasemi-axes r/|s| along the axis’s shadow and r across
two lines|s| ≤ εa, |δ| < r − εgenerators ±√(r2 − δ2) either side of the axis’s foot
tangent line|s| ≤ εa, ||δ| − r| ≤ εthe generator through the foot
empty|s| ≤ εa, |δ| > r + εnone
circle θ = 0 ellipse θ = 45° two lines θ = 90°, δ = 0.5 tangent line θ = 90°, δ = r empty θ = 90°, δ = 1.4
A cylinder of radius 1 and five planes. Square to the axis the section is a circle, and tilted 45° it is an ellipse of semi-axes √2 and 1. Parallel to the axis the plane cuts two generators, touches one, or misses, by its distance from the axis.

Plane and cone

The conic sections. With α the cone’s half-angle, θ the angle between the plane’s normal and the axis folded into [0, 90°], and σ the distance from the apex to the plane, the cases split first on whether the plane passes through the apex and then on θ against 90° − α, the angle at which the plane is parallel to a generator.

Off the apex, take coordinates in the plane from the foot of the apex: λ along M, the axis’s shadow on the plane, and w across it. A point X is on the cone when (X − apex)·a = |X − apex| cos α. With s = n·a = ±cos θ, squaring and dividing by cos2α gives

w2 + Aλ2 + Bλ + C = 0

A = (cos2α − sin2θ)/cos2α,  B = −2σs sin θ/cos2α,  C = σ2(cos2α − s2)/cos2α

The sign of A is the classification: positive below θ = 90° − α, zero at it, negative beyond. Completing the square, the centre of the ellipse or hyperbola is at λc = −B/2A. With K = B2/4A − C, the semi-axes are √(K/A) along M and √K across, using absolute values for the hyperbola. The parabola’s vertex is at λ = −C/B, and its focal length is |B|/4.

Through the apex, every generator meets the plane at the apex unless it lies in the plane. A generator is the line through the apex along d(φ) = (sin α cos φ,  sin α sin φ,  cos α), and it lies in the plane when n·d(φ) = 0. With the normal leaning in the XZ plane, that condition is cos φ = −cot α cot θ.

CaseConditionIntersection
circle|σ| > ε, sin θ ≤ εaradius |σ| tan α, at the foot of the apex
ellipse|σ| > ε, θ < 90° − α − εaclosed, on one nappe
parabola|σ| > ε, |θ − (90° − α)| ≤ εaone open branch
hyperbola|σ| > ε, θ > 90° − α + εatwo branches, one on each nappe
apex point|σ| ≤ ε, θ < 90° − α − εathe apex: no generator lies in the plane
tangent line|σ| ≤ ε, |θ − (90° − α)| ≤ εaone generator, the double root φ = π
two lines|σ| ≤ ε, θ > 90° − α + εathe generators at φ = ±arccos(−cot α cot θ)

We decide the tangent rows on the angle θ itself, which changes linearly as the plane tilts. A test on cos φ would be far narrower. For a 30° cone, the section is an ellipse at θ = 45°, a parabola at 60° and a hyperbola at 75°.

PLANES MISSING THE APEX circle θ = 0° ellipse θ = 45° parabola θ = 60° hyperbola θ = 75° PLANES THROUGH THE APEX apex point θ = 0° tangent line θ = 60° two lines θ = 90°
A double cone of half-angle 30° cut by seven planes, with θ the angle between the plane’s normal and the axis. Top, planes through a point of the axis above the apex. Below 60° the section is closed. At 60° the plane is parallel to a generator and the section is a parabola, and beyond it the plane reaches both nappes, so the hyperbola has a branch on each. Bottom, planes through the apex itself. Square to the axis, the plane meets only the apex. At 60° it touches the cone along one generator, and at 90° it contains the axis and cuts two. Every point drawn is a generator through the apex meeting the plane.

Two cylinders with parallel axes

Parallel cylinders are two circles in a cross-section, extruded along the axis. With radii R and r, axes d apart and e the unit vector across from the first axis to the second, the cross-sections meet on the radical line at s = (d2 + R2 − r2)/2d, at heights ±h = ±√(R2 − s2) along a × e. Axes that are not parallel give a space quartic, which section seven solves.

CaseConditionIntersection
two lines|R − r| + ε < d < R + r − εlines along a through se ± h a × e
tangent lined within ε of R + r or of |R − r|the line through se
emptyd > R + r + ε, or d < |R − r| − εnone
coincidentd ≤ ε and |R − r| ≤ εthe whole cylinder
two lines d = 1.3 tangent line d = R + r tangent line d = R − r empty d = 2.3 coincident d = 0, R = r
The two-sphere cases again, with cylinders. Overlapping, two parallel cylinders meet in two lines; touching outside or inside, in one; apart or nested, not at all.

Surfaces of revolution on one axis

When two surfaces of revolution share an axis, the whole problem lives in one meridian half-plane, with coordinates (ρ, z). Each surface is a curve there: a plane square to the axis is a horizontal line, the cylinder a vertical line, the cone a pair of slanted lines from its apex, the sphere a half circle centred on the axis, and the torus a circle off it. Intersecting the two surfaces is intersecting these two plane curves, which is a line or circle problem with a closed form. Each meeting point (ρ, z) with ρ > 0 is a circle of latitude in space, of radius ρ at height z. A meeting point on the axis is a single point, meridians that touch give surfaces that touch along a whole circle, and meridians that coincide give coincident surfaces.

ρ z plane z = h ρ z cylinder ρ = r ρ z cone ρ = k|z − a| ρ z sphere ρ²+(z−c)² = R² ρ z torus (ρ−R)²+z² = r²
The meridian of each surface of revolution, with the axis dashed. Two coaxial surfaces meet in one circle for each point where their meridians cross.

Two meridian circles meet where their equations, subtracted, agree. The squares cancel and leave a line, the radical line. Substituting it back into one circle gives a quadratic in z, whose two, one or no roots are two circles, a tangent circle, or none. That is how each pair below is solved.

Sphere and cylinder on its axis

A sphere of radius R centred at height c on the axis, and a cylinder of radius r: the meridians ρ2 + (z − c)2 = R2 and ρ = r meet at z = c ± √(R2 − r2).

CaseConditionIntersection
two circlesr < R − εradius r at z = c ± √(R2 − r2)
circle|r − R| ≤ εthe equator, where the two touch
emptyr > R + εnone
two circles r = 0.6 circle r = R empty r = 1.3
A unit sphere and a cylinder on its axis. A thinner cylinder passes through it in two circles at ±0.8; one of the same radius touches it round the equator, in a single circle.

Sphere and cone on its axis

Measure t along the axis from the apex, put the sphere’s centre at t = c, and let its radius be R. The cone’s meridian ρ = |t| tan α in the sphere’s gives t2/cos2α − 2ct + c2 − R2 = 0, so

t = c cos2α ± cos α √D,  D = R2 − c2sin2α

Each root is a circle of radius |t| tan α. Both lie on one nappe unless the sphere holds the apex, |c| < R, when there is one on each. D is a squared length, and its band is ε max(R2 + c2, 1).

CaseConditionIntersection
two circlesD above the bandthe two roots
tangent circleD within the band: R = |c| sin αone circle at t = c cos2α
emptyD below the bandnone
two circles c = 1, R = 0.7 two circles c = 0, R = 1 tangent circle R = c sin α empty c = 1, R = 0.35
A double cone of half-angle 30° and a sphere centred on its axis. A sphere of radius 0.7 centred 1 above the apex cuts two circles on the upper nappe. A unit sphere round the apex cuts one on each. A sphere of radius c sin α = 0.5 rests in the cone, touching it round one circle, and a smaller one misses it.

A cone with a cylinder or a cone on its axis

A cylinder of radius r on a cone’s axis meets each nappe once, at t = ±r/tan α from the apex: two circles, both of radius r.

For two cones on one axis, with half-angles α and α′, k = tan α, k′ = tan α′, and the second apex at t = d on the first cone’s axis, the meridians meet where k|t| = k′|t − d|. That happens at

t = k′d / (k′ + k)  and  t = k′d / (k′ − k)

The first is between the apexes and the second outside them, and the second disappears when the angles are equal.

CaseConditionIntersection
two circles|d| > ε, |α − α′| > εathe two rings above, of radius k|t|
circle|d| > ε, |α − α′| ≤ εaone ring at t = d/2
apex point|d| ≤ ε, |α − α′| > εathe shared apex
coincident|d| ≤ ε, |α − α′| ≤ εathe whole cone
two circles a cylinder two circles α′ ≠ α circle α′ = α apex point same apex coincident same cone
A double cone of half-angle 30° and, from the left: a cylinder on its axis; a 15° cone with its apex 1.2 higher, meeting it between the apexes and below both; a 30° cone with its apex 1.2 higher, meeting it halfway; a 15° cone sharing its apex; and the same cone.

Torus and plane

Take the torus with major radius R and minor radius r, centred at the origin about a. Three positions of the plane have a closed form. A plane square to the axis at height h is the coaxial case, a horizontal line meeting the tube’s meridian circle. A plane containing the axis cuts the two meridian circles themselves. A plane through the centre tilted from the equator by θ = asin(r/R), θ being the angle between n and a, touches the tube at two points and cuts it in two circles of radius R, the Villarceau circles. Their centres lie at distance r from the torus’s centre, one on each side, along the line where the plane crosses the equatorial plane, and the circles cross each other at the two points of contact. Tilted 10−7 radians further, the plane is no longer special, and the section is a general quartic, left to marching.

CaseConditionIntersection
two circlessquare to the axis, |h| < r − εradii R ± √(r2 − h2) at height h
tangent circlesquare to the axis, ||h| − r| ≤ εradius R at height h
emptysquare to the axis, |h| > r + εnone
circlesquare to the axis, R < √(r2 − h2), a spindle torusthe outer circle only, radius R + √(r2 − h2)
two circlesthe axis lies in the planeradius r, centred ±R along n × a
two circlesthrough the centre, |R sin θ − r| ≤ εradius R, centred ±r along n × a
generalany other planea quartic, for marching
two circles h = 0.5 tangent circle h = r empty h = 1.4 two circles through the axis two circles tilt asin(r/R)
A 3 by 1 torus and five planes. Square to the axis, a plane at height 0.5 cuts circles of radius 3.866 and 2.134, a plane at height 1 rests on it along the top circle, and a higher one misses. A plane through the axis cuts two meridian circles. A plane through the centre tilted asin(1/3) = 19.47° cuts two circles of radius 3 centred at ±1, which cross where the plane touches the tube.

Torus and cylinder on its axis

A cylinder of radius c is the vertical line ρ = c in the meridian plane, which meets the tube’s circle (ρ − R)2 + z2 = r2 at z = ±√(r2 − (c − R)2).

CaseConditionIntersection
two circles|c − R| < r − εradius c at those two heights
tangent circle||c − R| − r| ≤ εradius c at z = 0, the outer or inner equator
empty|c − R| > r + εnone
two circles c = 2.5 tangent circle c = R + r tangent circle c = R − r empty c = 1.5
A 3 by 1 torus and a cylinder on its axis. A cylinder of radius 2.5 crosses the tube in two circles at z = ±0.866. Cylinders of radius 4 and 2 touch the outer and inner equators, and one of radius 1.5 passes through the hole.

Torus and sphere on its axis

A sphere of radius s centred at height c on the axis has the meridian ρ2 + (z − c)2 = s2. Subtracting the tube’s circle leaves the radical line, and substituting it into the sphere gives a quadratic in z:

2Rρ = K + 2cz,  K = R2 − r2 − c2 + s2

(1 + c2/R2) z2 + 2(Kc/2R2 − c) z + K2/4R2 + c2 − s2 = 0

Each root z is a circle of radius ρ = (K + 2cz)/2R, kept only if ρ ≥ 0. A sphere centred off the axis meets the torus in a curve of degree eight, left to marching.

CaseConditionIntersection
two circlestwo roots, both with ρ ≥ 0a circle at each root
circletwo roots, one with ρ < 0the other circle
tangent circlea double rootone circle, where the two touch
emptyno real rootnone
two circles c = 0, s = 3 two circles c = 1.6, s = 2.8 tangent circle c = 0, s = 2 empty c = 0, s = 1.5
A 3 by 1 torus and a sphere centred on its axis. A sphere of radius 3 at the centre crosses the tube in two circles at z = ±0.986, and one of radius 2.8 centred 1.6 above crosses it in two circles of different radii. A sphere of radius 2 sits in the hole touching the inner equator, and one of radius 1.5 sits in it clear of the tube.

Torus and cone on its axis

A cone with its apex at height a on the axis and k = tan α has the meridian ρ = m(z − a) on each nappe, with m = k on one and −k on the other. In the tube’s circle it gives a quadratic per nappe:

(m2 + 1) z2 − 2m(ma + R) z + (ma + R)2 − r2 = 0

Only roots with m(z − a) ≥ 0 lie on that nappe. Up to four rings come out, and a double root is a tangent ring. A cone whose axis or apex is off the torus’s axis meets it in a curve of degree eight, left to marching.

CaseConditionIntersection
ringsa root on either nappea ring of radius m(z − a) at each root, flagged tangent for a double root
emptyno root on either nappenone
four rings α = 75° two tangent rings cos α = r/R empty α = 60° two rings apex z = 2, α = 45°
A 3 by 1 torus and a cone on its axis. With the apex at the centre, a cone of half-angle 75° crosses the tube twice on each nappe. At cos α = r/R it touches the tube along one ring per nappe, and at 60° it passes through the hole. A 45° cone with its apex 2 above the centre crosses the tube on its lower nappe only.

Two tori on one axis

With the second torus of radii R′ and r′ centred at height z′, the two tube circles subtract to the radical line

2(R′ − R) ρ = r2 − r′2 − R2 + R′2 + z′2 − 2zz′

Substituting it into the first circle gives a quadratic in z, as for the sphere. When the major radii are equal the line is horizontal instead, at z* = (r2 − r′2 + z′2)/2z′, and the rings have radii R ± √(r2 − z*2). Tori on different axes meet in a curve of degree up to sixteen, left to marching.

CaseConditionIntersection
two circlestwo roots, both with ρ ≥ 0a circle at each root
circletwo roots, one with ρ < 0the other circle
tangent circlea double rootone circle, where the two touch
emptyno real rootnone
coincidentsame centre, same radiithe whole torus
two circles R′ = 4.2, r′ = 0.8 two circles z′ = 1.2 tangent circle z′ = 2r empty z′ = 2.5 coincident the same torus
A 3 by 1 torus and a second torus on its axis. A wider torus round it, of radii 4.2 and 0.8, crosses the tube in two circles. The same torus stacked 1.2 higher crosses it in two circles at z = 0.6. Stacked 2 higher it rests on it, touching along one circle, and at 2.5 it is clear.

Everything else. Two cylinders with crossing axes, a sphere off a cylinder’s axis, a cone tilted against another: two quadrics off these special positions meet in a space quartic, and section seven still solves them without iterating. A slanted plane through a torus, or a quadric off the torus’s axis, meets it in a curve of degree four to eight, and the kernel hands those to the marching of section ten. The closed forms are worth having for the cases above because they are exact and cheap, and because the special positions are the ones designers build: a hole drilled along an axis, a fillet sphere on a cylinder’s axis, a plane cut square to a shaft.

Two quadrics in general position

Off the special positions of section six, two quadrics meet in a space quartic, a curve of degree four. It still has a closed form, because one of the two surfaces is almost always ruled. A cylinder and a cone are made of straight lines, their generators, and among the elementary quadrics only the sphere is not ruled. Two spheres meet in a circle, and a plane against anything is section six, so every pair left has a cylinder or a cone in it. A generator meets the other quadric in a quadratic, and solving that quadratic for every generator gives the whole curve, one point per root, in closed form. Levin’s method for any two quadrics rests on the same idea, and Dupont, Lazard, Lazard and Petitjean made it exact and near-optimal; with a cylinder or a cone already in hand, the walk below is the simple case of it.

Walking the generators

Write the ruled quadric as a family of lines X = B(t) + sD(t), with t going once round it. For a cylinder of radius r about the line through C along a, with u and v completing a frame,

B(t) = C + r(cos t u + sin t v),  D = a

and for a cone with apex O, axis a and half-angle α, B = O and D(t) = cos α a + sin α(cos t u + sin t v), with negative s on the other nappe. Put the generator into the other quadric’s equation F from section two. F has degree two, so

F(B + sD) = A(t) s2 + 2β(t) s + γ(t) = 0

with A the quadratic part of F applied to D, 2β = ∇F(B)·D and γ = F(B). The generator meets the other quadric where

s±(t) = (−β ± √Δ)/A,  Δ(t) = β2 − Aγ

computed without cancellation as in section three, q = −(β + sign(β)√Δ) and the roots q/A and γ/q. The sign of Δ over the circle of generators gives the shape of the curve.

  • Δ > 0 for every t: two loops, one per root, each going once round the ruled quadric.
  • Δ ≥ 0 on arcs of t, zero at their ends: one loop per arc, out along s+ and back along s−. At the ends the generator touches the other quadric and the two roots meet.
  • Δ with a double zero inside an arc: the roots meet there without parting, so the loop passes through the same point twice and crosses itself. This is a node, and the two surfaces touch at it.
  • Δ ≤ 0 with a single double zero: a tangent point, where one generator touches the other quadric.
  • Δ < 0 everywhere: empty.
  • A = 0 at some t: the generator is parallel to an asymptotic direction of the other quadric, one root runs off to infinity, and the curve has unbounded branches. This needs a cone, and the card on two cones treats it.

The arc ends are closed forms for each pair below, usually an arccosine. Tangency is decided on distances, as section three argued: a node or a tangent point is a band of width ε round an equality between distances, and a configuration inside the band is moved onto the equality before the curve is computed, for instance by changing each cylinder’s radius by half the difference, so that the node comes out as an exact double zero of Δ. Otherwise the two roots would come out almost a thousandth apart, by the square-root rule of section three, and a loop that should cross itself would come apart into two near-touching loops.

Each loop is exact as a function of t, and a kernel can keep it that way, as an evaluator of the formulas. To hand it to modelling as a B-spline, interpolate points and tangents from the formulas, a cubic Hermite span between consecutive samples, and refine until the midpoint and the quarter points of every span lie within ε/4 of the exact curve. Measure that against the curve, not against the two surfaces. Where the surfaces meet at a small angle, with c the cosine between their normals, a point at distances g1 and g2 from them lies at

√((g12 + g22 − 2c g1g2)/(1 − c2))

from their intersection to first order, which grows without bound as c → 1, so a span close to both surfaces can still be far from the curve near a node. The pcurve on the ruled quadric is (t, s(t)) directly. On the other surface it comes from that surface’s closed-form inverse, which has no answer at a pole of a sphere, so a loop through a pole is split there or the sphere’s frame is turned to keep the poles off it.

Two cylinders

Take the first cylinder of radius R about the z axis, and put x along the common normal of the two axes. The second, of radius r, has its axis through (d, 0, 0) along (0, sin ψ, cos ψ), so d is the distance between the axes and ψ the angle between them, and ψ = 0 is the parallel case of section six. Along the generator B = (R cos t, R sin t, 0), D = z of the first,

A = sin2ψ,  β = −R sin t sin ψ cos ψ,  γ = (R cos t − d)2 + R2sin2t cos2ψ − r2

Δ = sin2ψ (r2 − (R cos t − d)2),  s± = (R sin t cos ψ ± √(r2 − (R cos t − d)2))/sin ψ

A generator meets the second cylinder when |R cos t − d| ≤ r, that is for cos t between (d − r)/R and (d + r)/R, and the angle ψ plays no part in which generators meet it. Walk the wider cylinder, R ≥ r. The node and the tangent point are both at t = 0, the point (R, 0, 0).

CaseConditionIntersection
two loopsd < R − r − εone per arc, t between acos((d + r)/R) and acos((d − r)/R), and its mirror in t = 0
crossing loopd within ε of R − rone loop through the node (R, 0, 0)
one loopR − r + ε < d < R + r − ε|t| ≤ acos((d − r)/R)
tangent pointd within ε of R + r(R, 0, 0)
emptyd > R + r + εnone
two ellipsesd ≤ ε and |R − r| ≤ εa split case: see below
two loops d = 0.2 crossing loop d = 0.5 one loop d = 1 tangent point d = 1.5 empty d = 1.9
A cylinder of radius 1 crossed at right angles by one of radius 0.5, at five distances d between the axes. At 0.2 the thin cylinder passes right through, in two loops. At 0.5 it touches the far side, and the loop crosses itself there. At 1 it bites the side in one loop, at 1.5 it touches it at one point, and at 1.9 it misses.

A sphere and a cylinder off its axis

Take the cylinder of radius r about the z axis and the sphere of radius R centred at (e, 0, c), with e > 0 its distance from the axis; e = 0 is the coaxial card of section six. Along a vertical generator A = 1 and β = −c, and

Δ = R2 − r2 − e2 + 2re cos t = 2re(cos t − c0),  c0 = (r2 + e2 − R2)/2re

X = (r cos t, r sin t, c ± √Δ)

so the generators that meet the sphere are those with cos t ≥ c0, and c0 sorts the cases. The best-known member is Viviani’s curve, R = 2r and e = r: the cylinder passes through the sphere’s centre and touches it from inside, and the loop crosses itself at the point of contact.

CaseConditionIntersection
two loopsR > r + e + εz = c ± √Δ all round
crossing loopR within ε of r + eone loop through the node (−r, 0, c)
one loop|r − e| + ε < R < r + e − ε|t| ≤ acos c0
tangent pointR within ε of |r − e|(r, 0, c), from inside the cylinder if e < r
emptyR < |r − e| − εnone
two loops e = 0.3, R = 1.1 crossing loop e = r, R = 2r one loop e = 0.5, R = 0.7 tangent point R = e − r empty e = 1.5, R = 0.7
A cylinder of radius 0.5 and a sphere whose centre is off its axis, five times. A sphere of radius 1.1 centred 0.3 off the axis holds the cylinder and is cut in two loops. Viviani’s curve, e = r and R = 2r, crosses itself where the sphere touches the cylinder. A sphere of radius 0.7 centred on the cylinder cuts one loop, the same sphere 1.2 off the axis touches it from outside, and 1.5 off it misses.

A sphere and a cone off its axis

Take the cone with its apex at the origin about the z axis, half-angle α, and the sphere of radius R centred at C = (e, 0, h). A point sD(t) of a generator is on the sphere when s2 − 2ms + k = 0, with

m(t) = C·D = h cos α + e sin α cos t,  k = |C|2 − R2,  s = m ± √(m2 − k)

Here k is the power of the apex with respect to the sphere, and √(|C|2 − m2) is the distance from the centre to the generator, so a generator meets the sphere when that distance is at most R. The sign of k splits the cases first. With the apex inside the sphere, k < 0, the roots have opposite signs for every t, and there is one loop on each nappe. With the apex on the sphere, k = 0, the roots are 0 and 2m: the apex itself, and one loop s = 2m(t), which passes through the apex only where m changes sign. With the apex outside, k > 0, both roots have the sign of m, so a generator meets the sphere only on the nappe m points to, and only where |m| ≥ √k. For the upper nappe, with m between mmin = h cos α − e sin α at t = π and mmax = h cos α + e sin α at t = 0, the cases are in the table; the lower nappe is the same with −m.

CaseConditionIntersection
two loopsk < 0: the apex insideone loop on each nappe
loop and apexk = 0: the apex on the spheres = 2m(t), and the apex
two loopsk > 0, mmin > √ks+ and s− all round
crossing loopmmin = √kone loop through the node at t = π
one loopmmin < √k < mmaxcos t ≥ (√k − h cos α)/(e sin α)
tangent pointmmax = √kmmaxD(0)
emptymmax < √k on both nappesnone
two tangent pointsh = 0, R = e cos αe sin α D(0) and −e sin α D(π), one on each nappe

The equalities are bands of width ε on the distance from the centre to the generator at t = 0 and t = π, against R. In the last row the sphere sits level with the apex, and the distance from its centre to the generators at t = 0 and t = π is e cos α on both nappes.

two loops apex inside loop and apex apex on the sphere two tangent points R = e cos α
A double cone of half-angle 30° and a sphere off its axis. A sphere of radius 1 centred at (0.4, 0, 0.5) holds the apex and cuts one loop on each nappe. A sphere through the apex cuts one loop and meets the cone again at the apex itself. A sphere centred level with the apex, at distance 1 from it, with radius cos 30°, touches each nappe at one point.
two loops R = 0.75 crossing loop √k = m min one loop R = 0.5 tangent point √k = m max empty R = 0.25
The same cone and a sphere centred at (0.2, 0, 1), shrinking. Of radius 0.75 it is cut in two loops; of radius 0.6732, where √k = mmin, in one loop that crosses itself; of radius 0.5 in one loop. Of radius 0.3268, where √k = mmax, it touches the cone at one point, and of radius 0.25 it misses.

A cone and a cylinder

Here the cylinder is the one to walk. Let the cylinder have axis a1 and radius r, and the cone apex O, axis a2 and half-angle φ, with cos ψ = a1·a2. In the plane through O square to a1, let W(t) run from the apex to the foot of generator t, round a circle of radius r centred where the cylinder’s axis crosses that plane. A generator is O + W + sa1, and since W is square to a1,

A = cos2ψ − cos2φ,  β = cos ψ (W·a2),  γ = (W·a2)2 − cos2φ |W|2

Δ = cos2φ [(W·a2)2 + A |W|2]

When the cylinder is steeper than the cone, ψ < φ and A > 0, Δ is positive for every generator, and the curve is two loops round the cylinder, one on each nappe. When it is shallower, A < 0, and Δ ≥ 0 says that W lies within an angle λ of the direction of a2 projected into the plane, on either side, with

sin λ = sin φ/sin ψ

That double wedge is the shadow of the cone along a1, and the generators that meet the cone are the arcs of the cylinder’s circle inside it. So the cases are read in the plane, from a circle against two pairs of rays. The arc ends are where the circle crosses the rays, a quadratic, and the node and the tangent point are where it touches one, decided on the distance from the circle’s centre to the ray against r.

CaseConditionIntersection
two loopsA > 0: the cylinder steeper than the coneone loop round the cylinder on each nappe
two loopsthe circle inside one half of the shadowthe cylinder passes through both walls of a nappe
two loopsthe circle crossing both edges of one halfa nappe passes through the cylinder
two loopsthe circle round the apexone arc in each half, one loop on each nappe
one loopthe circle crossing one edgethe arc inside the shadow
crossing loopthe circle touching an edge from insidea loop through the node
tangent pointthe circle touching an edge from outsideone point
emptythe circle outside the shadownone

Between the two, A = 0: the cylinder is parallel to a generator of the cone, the quadratic drops to a linear equation, and the curve runs off to infinity along that direction. It is left to marching here.

two loops A > 0 two loops through it one loop one edge two loops round apex two loops wide pipe empty outside
A double cone of half-angle 30° and a cylinder, six times. Tilted 10° from the cone’s axis, a thin cylinder is steeper than the cone and passes through both nappes. The other five lie across the cone. A pipe of radius 0.3 at height 1.5 goes through both walls of the upper nappe; moved 0.8 sideways it bites one wall in one loop. A pipe of radius 0.5 round the apex has one loop on each nappe, and through a pipe of radius 1 the upper nappe passes. Beside the cone, the pipe misses it.

Two cones

Walk the first cone, with apex O1, axis a1 and half-angle φ1, along d(t) = cos φ1 a1 + sin φ1(cos t e1 + sin t e2), against the second with apex O2, axis a2 and half-angle φ2. With w = O1 − O2,

A = (d·a2)2 − cos2φ2,  β = d·m,  γ = (w·a2)2 − cos2φ2 |w|2,  m = (w·a2)a2 − cos2φ2 w

and Δ = dTMd with M = mmT − γ(a2a2T − cos2φ2 I). Since Mw = 0, Δ only sees the part of d across w. The sign of γ says where the first apex is. With γ > 0 it is inside the second cone, every generator of the first meets the second twice, and when A keeps one sign the curve is two loops round the first cone. With γ < 0 it is outside, and Δ factors as

Δ = (n0·d)(n1·d)

with n0 and n1 the normals of the two planes through both apexes that touch the second cone. A generator meets the second cone when it lies between those planes, and the arc ends, n·d(t) = 0, are of the form C cos t + D sin t + e = 0 from section five, solved by one arccosine each.

Two cones also bring the case A = 0, where a generator of the first is parallel to one of the second. One root runs to infinity there and comes back from infinity of the other sign, on the other nappe, so the pieces of the curve between the zeros of A are unbounded branches. Each branch is still exact. Parametrise it by a u that maps the open t-interval between two zeros onto the whole line, such as t = tm + (2h/π) atan u for an interval of half-width h about tm, and near a zero of A compute the root that stays finite as γ/q, which the cancellation-free formula above already does.

CaseConditionIntersection
two loopsγ > 0, A of one signs+ and s− all round
loopsγ < 0, A of one signone loop per arc between the tangent planes, up to two
branchesA changes signthe loops, and unbounded branches between the zeros of A
emptyno generator between the tangent planesnone
two loops holds the apex two loops apex outside loop and two branches A changes sign empty apex outside
A double cone of half-angle 30° against a second cone, four times. A cone of half-angle 45° whose inside holds the first apex cuts two loops round the first cone. A thin cone pointing at the first from the side, its apex outside, passes through both walls of the upper nappe in two loops. A thin cone tilted across it gives one loop and two branches that run off to infinity, drawn as far as the frame. A cone turned away misses it.

When the quartic splits

Some positions off the coaxial ones of section six still break the quartic into conics or lines, and a kernel should recognise them before walking, because the pieces are exact conics with their own closed forms.

Quadrics round one sphere. Two quadrics that touch the same sphere meet in two conics. Take the sphere of radius ρ at C, W = X − C and S = |W|2 − ρ2. A cylinder of radius ρ through C along a is S − (W·a)2 = 0, and a cone that touches the sphere, with half-angle φ and its apex at C − (ρ/sin φ)a, is cos2φ S − (W·a + ρ sin φ)2 = 0. Divide the cone’s equation by cos2φ and subtract the two equations: S cancels, and a difference of two squares is left, two planes.

cylinder–cylinder  W·a1 = ±W·a2

cone–cylinder  cos φ (W·a1) = ±(W·a2 + ρ sin φ)

cone–cone  (W·a1 + ρ sin φ1)/cos φ1 = ±(W·a2 + ρ sin φ2)/cos φ2

On either plane the two quadrics agree, so each plane cuts one conic from both, by the plane–cylinder or plane–cone card of section six. The two conics cross where both quadrics touch the sphere: each touches it along a circle, the cylinder in the plane W·a = 0 and the cone in W·a = −ρ sin φ, and the circles meet in up to two contact points. The test is on the axes: two cylinders of equal radius whose axes meet, a cylinder of radius ρ and a cone whose axis meets the cylinder’s at distance ρ/sin φ from the apex, or two cones whose axes meet at distances ρ/sin φ1 and ρ/sin φ2 from their apexes. The first is the pipe tee, and the second a reducer.

two ellipses a tee two ellipses cylinder, cone two ellipses two cones
Three pairs that touch one sphere of radius 0.5. Two cylinders of that radius whose axes cross at right angles meet in two ellipses in the planes bisecting the axes. A cylinder and a cone of half-angle 25° whose axis is tilted 70° from it, and two cones of half-angles 25° and 15°, also meet in two ellipses. In each pair the ellipses cross at the two points where both surfaces touch the sphere.

Two cones with one apex. Every point of both cones lies on a line through the shared apex, so the intersection is lines. A unit direction d lies on both when d·a1 = c1 = ±cos φ1 and d·a2 = c2 = ±cos φ2. With g = a1·a2, write d = xa1 + ya2 + z(a1 × a2):

x = (c1 − gc2)/(1 − g2),  y = (c2 − gc1)/(1 − g2),  z2 = (1 − g2 − c12 − c22 + 2gc1c2)/(1 − g2)2

Fixing the sign of c1, since −d is the same line, leaves two signs of c2 and two of z, so up to four lines. Where z2 is within the tolerance of 0 the two lines of a pair merge into one tangent line, along which the cones touch. In angles, with ω the angle between the axes, the pair c2 = +cos φ2 gives lines when |φ1 − φ2| ≤ ω ≤ φ1 + φ2, and the other pair when the same holds for π − ω. The apex belongs to every answer, and coaxial cones with one apex are the apex alone, as in section six.

four lines ω = 90°, φ′ = 60° two lines ω = 60°, φ′ = 30° tangent line ω = 75°, φ′ = 30° apex ω = 80°, φ′ = 30°
A double cone of half-angle 45° and a second cone with the same apex, its axis turned by ω in the plane of the page. With ω = 90° and φ′ = 60° both sign pairs give two lines, four in all. With ω = 60° and φ′ = 30° one pair gives two. At ω = 75° = 45° + 30° the two merge into a tangent line, and at 80° only the apex is left.

Parallel cones of one angle. Two cones with parallel axes a and the same half-angle φ have equations that differ by a polynomial of degree one, so they meet in a plane. With v = O2 − O1 and W = X − O1, subtracting the two equations gives

2W·[(v·a)a − cos2φ v] = (v·a)2 − cos2φ |v|2

a plane through the midpoint of the two apexes, and the curve is its section of either cone. With the second apex inside the first cone the section is an ellipse, outside it a hyperbola with one branch on each nappe, and on it the line through both apexes, along which the cones touch. A parabola does not occur: the plane is parallel to a generator only when the second apex lies on the first cone, and then the section is that line.

two branches apexes level ellipse apex inside tangent line apex on the cone
Two cones of half-angle 30° with parallel axes. Apexes level and one unit apart, they meet in a hyperbola in the plane halfway between, one branch on each nappe. With the second apex inside the first cone, at (0.3, 0, 1.5), they meet in an ellipse. With it on the first cone they touch along the line through both apexes.

What stays numeric

A few positions of two quadrics are left to the marching of section ten. A cone and a cylinder parallel to one of its generators run to infinity along a single direction. Two cones need marching when an apex lies on the other cone, where γ = 0 and every generator through the apex is a root; when they touch along a curve; when A has a double zero, so that a branch goes to infinity and comes straight back; and when β vanishes together with A, so that both roots run off at once. Each is a meeting of two of the conditions above, and each can be recognised by the same tests on distances before the walk starts.

Two free-form curves

Two B-spline curves meet where A(s) = B(t). In a plane these are two equations in two unknowns, and Newton’s method on them converges quadratically from a nearby start. The problem, as in part six, is the start: the curves can meet many times, and a starting grid fine enough to see every crossing has to be finer than the closest pair of crossings, which is unknown.

The convex hull property removes the guesswork. Cut both curves into Bézier pieces. A piece lies inside the box of its control points, so two pieces whose boxes do not overlap cannot meet. That gives a subdivision algorithm:

  1. Start with every pair of pieces, one from each curve.
  2. Take a pair. If its boxes are disjoint, drop it.
  3. If both boxes are small enough, keep the pair as a candidate.
  4. Otherwise halve the larger piece with de Casteljau’s algorithm, and put the two new pairs back.
  5. From each candidate, run Newton on A(s) − B(t) = 0 from the middle of its two parameter intervals, and keep the converged points that are new.
BOXES THAT STILL OVERLAP first curve three cubic spans second curve one cubic span 93 pairs of pieces tested, 8 left small and overlapping; Newton from each finds the 5 points
A cubic B-spline of three spans and a cubic Bézier curve. Of 93 pairs of pieces examined, eight survive until both boxes are small, all of them clustered at the crossings, and Newton from each finds the five intersection points.

Near a crossing at a good angle the boxes separate quickly, and the number of pairs grows only with the number of crossings. Near a tangency they do not separate at all, because the two curves stay within each other’s boxes over a stretch whose length grows as the square root of the box size. So the subdivision needs a depth limit, and a pair that reaches it is a tangency candidate: there, minimise the distance between the curves instead of solving for zero, and report a tangent point if the minimum is within tolerance. The same applies to two curves that overlap over an interval, where every subdivided pair survives; checking a few points along the candidate interval tells overlap from tangency.

In three dimensions two curves generically do not meet at all, and A(s) = B(t) is three equations in two unknowns. The problem is then the minimum of |A(s) − B(t)|2, a projection problem in two parameters, solved by the bounded Newton iteration and the box bounds of part six. A minimum within tolerance is an intersection, and the formulation has a useful side effect: the same search returns the closest approach of two curves that miss, which a tolerance check on an edge needs anyway.

A curve and a free-form surface

A curve meets a surface where C(t) = S(u, v): three equations in three unknowns. Newton’s step δ = (δt, δu, δv) solves

[ C′  −Su  −Sv ] δ = −(C(t) − S(u, v))

a 3 by 3 system whose matrix has the three derivative vectors as columns. Its determinant is C′·(Su × Sv), the component of the curve’s tangent along the surface normal. It vanishes exactly when the curve is tangent to the surface, so the equation degenerates in the same place as the closed forms, for the same reason.

Seeds come from the same box test as before, between the Bézier pieces of the curve and the Bézier patches of the surface, subdividing the larger of the two until both are small. Where the determinant is small near the converged point, the contact is a tangency candidate, and the routine switches to minimising |C − S|2 over (t, u, v). This is less precise than it sounds. The distance near a tangent contact grows with the square of the distance along the curve, so a whole stretch of curve lies within tolerance of the surface, and the contact point can only be located to about the square root of the tolerance. Solving the tangent line on the 3 by 1 torus this way finds the two contacts only to a few times 10−7 in t; the closed form of section five gives 3 and 9 exactly. That is a reason to prefer the closed form whenever one exists, not a defect of the numerical method.

Two surfaces: marching

Two surfaces meet along curves, and the curve is the solution set of three equations in four unknowns:

S1(u1, v1) − S2(u2, v2) = 0

One unknown too many is what makes the solution a curve rather than points. Marching traces it: start from a point on it, step along its tangent, pull the step back onto both surfaces, and repeat. Let’s take a cylinder of radius 1 and a sphere of radius 1.78 centred 0.8 off its axis, which meet in one closed curve that turns tightly where the sphere comes within 0.02 of the cylinder.

ONE CURVE, THREE PLACES seed ON THE CYLINDER −π u π −1.5 v 1.5 ON THE SPHERE −π u π −π/2 v π/2 68 steps, from 0.10 to 0.27 long: short where the curve turns tightly
The march starts where the cylinder’s generator u = 0 meets the sphere, a line–sphere intersection from section five, and closes after 68 steps. The same points, carried with their parameters, draw the curve in the cylinder’s rectangle and in the sphere’s. Steps run from 0.10 to 0.27 in length, shortest at the two tight turns.

Each step has four parts.

The tangent. The curve lies in both surfaces, so its tangent is perpendicular to both normals:

T = (N1 × N2) / |N1 × N2|

Its sign is arbitrary, so we keep T·Tprev > 0 from one step to the next. To move in parameters as well as in space, we need the parameter direction (ai, bi) on each surface with aiSu + biSv = T. T lies in the tangent plane, so this has an exact solution, from the 2 by 2 system of the first fundamental form of part five:

[ Su·Su  Su·Sv ;  Su·Sv  Sv·Sv ] (a, b) = (T·Su,  T·Sv)

The step length. A chord of length h on a curve of curvature κ deviates from the curve by about κh2/8 at its middle. To keep that deviation within a chosen ε, take

h = √(8ε / κ),  clamped to [hmin, hmax]

The curvature of the intersection curve comes from the surfaces. Each surface bends the curve by its normal curvature in the direction T, κi = ai2Suu·Ni + 2aibiSuv·Ni + bi2Svv·Ni. The curve’s curvature vector k is perpendicular to T, so it lies in the plane of the two normals, and its components along them are those two normal curvatures. Writing k = λN1 + μN2 and c = N1·N2:

λ = (κ1 − c κ2) / (1 − c2),  μ = (κ2 − c κ1) / (1 − c2),  κ = |k|

The 1 − c2 in the denominators is sin2 of the angle between the surfaces: where they meet at a shallow angle, the curve bends sharply and the steps shorten, which is what the figure shows at its two turns. The curvature can also grow along a step, so after each step we compute it again at the new point and retake the step if the new value asks for one clearly shorter. Six of the 68 steps in the figure were retaken this way.

The predictor and the corrector. The predicted parameters are (ui, vi) + h(ai, bi) on each surface. They are off the curve by about the deviation ε, and the corrector brings them back. It needs a fourth equation to pin down where along the curve the new point lands, and the natural one is the plane at distance h along T from the current point Pk:

S1 − S2 = 0,  (S1 − Pk)·T − h = 0

Newton on these four equations in (u1, v1, u2, v2) has the Jacobian with rows [S1u, S1v, −S2u, −S2v] for the three coordinates, and [T·S1u, T·S1v, 0, 0] for the plane. In the figure it converges to 10−13 in three iterations on most steps and five at the tight turns. If it does not converge within a handful of iterations, halve h and try again.

A simpler corrector is tempting: project the predicted point onto the first surface, then onto the second, and repeat until it stops moving. Each projection is a call to the routine of part six, so it needs no new code. But it converges linearly, at a rate set by the angle θ between the surfaces. Near the intersection each pair of projections multiplies the error by about cos2θ:

Angle between the surfaces60°30°10°5°1°
Passes of alternating projection to 10−712534591,74838,451
Newton steps11111

The table is for two planes, where Newton is exact in one step; on curved surfaces it takes a few, quadratically. Shallow angles are exactly where marching is hardest, and where the alternating corrector fails.

SURFACES AT 30° predicted on both alternating: 53 passes Newton: one step SURFACES AT 10° predicted on both alternating: 459 passes Newton: one step
Two surfaces seen along the curve where they meet, so each is a line and the curve a point. From the same predicted point, alternating projection zigzags into the wedge between them, in 53 passes at 30° and 459 at 10°. Newton solves both equations at once and lands on the curve in one step.

Stopping. A march ends in one of three ways, and the routine has to tell them apart:

  • It closes. The starting point lies within the next step and ahead of the current point. The curve is a loop.
  • It leaves a parameter rectangle. The step crosses the boundary of either surface’s domain. Clip it exactly: intersect the boundary curve of that surface with the other surface, using section nine, and end the branch there. The branch is open, and the other end will be found the same way when marching from the starting point in the opposite direction.
  • The normals become parallel. |N1 × N2| falls below the angular tolerance, so T is undefined: the surfaces are tangent there, or the curve crosses itself. Stop and hand the point to whatever handles tangent contact, which is beyond this article.

The output of a march is a sequence of points with four parameters each. That is three curves at once: the points in space, and their (u, v) images on each surface, which become the pcurves of the new edges. Each is fitted with a B-spline, after unwrapping any periodic parameter against its neighbour as part six described, and the fitted curves are checked against the surfaces between the marched points.

Finding every branch

Marching traces a branch once it has a point on it. The difficult part of surface–surface intersection is producing at least one starting point on every branch, and no more than necessary.

Open branches are the easy kind. A branch that is not a closed loop must leave the domain of one of the surfaces, so it crosses the boundary of a parameter rectangle. Intersecting each of the four boundary curves of each surface with the other surface, using section nine, finds a point on every open branch, and in fact both ends of each. March from each such point, and drop the ones that a previous march already reached.

Closed loops touch no boundary, so no boundary point finds them, and a grid of samples finds them only if a sample happens to fall close enough. Let’s take a surface over the unit square cut by a horizontal plane, with a narrow bump inside the domain.

A PLANE ACROSS A BUMPY SURFACE from the edges two points where the curve crosses the boundary; one branch joins them from inside a closed loop touching no edge and holding no sample; it surrounds the top of the bump (x), where the normal is parallel to the plane’s, and a ray from there reaches it
The two boundary points start the one open branch. The small loop round the bump, 0.044 in radius, touches no edge, and the nearest sample of a 6 by 6 grid is 0.100 from its centre. It surrounds the top of the bump, where the surface’s normal is parallel to the plane’s, and a ray from that point in any direction crosses the loop and starts its march.

The point marked in the figure is not a coincidence. Inside a closed loop of the section, the height of the surface above the plane is zero on the loop and has one sign within it, so it has a maximum or a minimum inside, and there its gradient vanishes: the surface is level, and its normal is parallel to the plane’s. The statement for two surfaces is the same with both normals: when a closed loop bounds a region on each of two smooth patches, there is a line normal to both surfaces, meeting them at a point of each, with both normals parallel to the segment between those points. Those pairs of points are the stationary points of the distance between the two surfaces, the solutions of

(S1 − S2)·S1u = (S1 − S2)·S1v = (S1 − S2)·S2u = (S1 − S2)·S2v = 0

four equations in four unknowns, which is part six’s projection condition with a surface in place of the point. So a kernel can find every loop in two steps: find all the stationary points of the distance between the two surfaces, and from each one, walk along either surface until the signed distance to the other changes sign, which gives a curve–surface intersection and a starting point on the loop. Points that land on a branch already traced are dropped.

Finding the stationary points is itself a search that must not miss anything, and in the figure it is a real one: Newton started from a 9 by 9 grid never reaches the top of the narrow bump, and a 30 by 30 grid is needed. The robust way is the bounds and subdivision of part six, applied to pairs of patches. The cheaper alternative avoids solving for the stationary points at all. If the normals of one patch all lie in a cone of directions and the normals of the other lie in a cone that cannot contain a direction parallel to any of them, then no pair of parallel normals exists, and no closed loop can lie inside that pair of patches. So subdivide pairs of overlapping patches until each pair either passes that normal-cone test, in which case its boundary points find all of its branches, or is too small to matter.

Implementation checklist

If we implement intersection from scratch, these are the decisions in the order the problems arrive.

Return a classification, not only geometry. Transversal, tangent, coincident or empty, with parameters on both pieces for every point, and both pcurves for every curve.

Use the closed forms where they exist: substitution for lines and conics against the elementary surfaces, and the special positions of two surfaces, including the coaxial pairs, which reduce to their meridians. Recognise the position before computing.

Decide tangency on a distance. Evaluate the distance at the closest approach (the vertex of a quadratic, the critical points of a quartic) and compare that with the tolerance. Never compare root separations with it.

Centre the parameter and solve quadratics without cancellation.

Add the candidates a substitution cannot reach, such as t = π for tan(t/2), and verify every candidate by measuring its distance to the other surface, with a tolerance no finer than the rounding of its coordinates.

Test coincidence by the degree bound: five points of a conic on a quadric means the whole conic is on it.

Recognise the quadric pairs that split before walking: two quadrics touching one sphere meet in two conics, two cones with one apex in lines, two parallel cones of one angle in one conic.

Walk the generators of the ruled quadric for any other pair: one quadratic per generator, the shape decided by the sign of its discriminant over the circle of generators, the arc ends in closed form, nodes and tangent points snapped onto their exact configuration, and every fitted span checked against the curve, not against the surfaces.

For free-form curves and curve–surface pairs, subdivide by boxes before iterating, and switch to minimising the distance where the Jacobian becomes singular.

March with a Newton corrector on four equations, the step length from the curvature of the intersection curve, rechecked at the end of the step, and a consistent orientation of the tangent.

Stop for three reasons and tell them apart: closure, the boundary (clipped exactly), parallel normals.

Seed every branch: boundary curves against the other surface for open branches, stationary points of the distance or a normal-cone test for closed loops.

What this article leaves out

Tangential and singular intersections. Where two surfaces touch along a curve (a cylinder lying on a plane, a fillet meeting its support faces) or where the intersection curve crosses itself, the normals are parallel and marching has no direction. Kernels treat these with dedicated methods, and they are among the hardest cases in the field.

Algebraic methods. Resultants eliminate variables between implicit equations to give a single polynomial whose roots are the intersection, and the pencil method of Levin parametrises the intersection of any two quadrics through a ruled quadric in the pencil they span. Section seven is the case where one of the two is already ruled; for two spheres, two ellipsoids or any pair with no ruled member, the pencil finds one, and Dupont, Lazard, Lazard and Petitjean made the method exact, with the fewest square roots. Resultants and the general pencil method are beyond the scope of this series.

Self-intersections. A curve or a surface that meets itself, as an offset does once its distance exceeds the radius of curvature. The same tools apply, with the difficulty that every point trivially meets itself.

Making the result consistent. An intersection curve computed for one pair of faces must end exactly where the curves of the neighbouring pairs end, at the same vertices, or the Boolean’s result does not close. Organising the intersections into a consistent set of new edges and vertices is the subject of part eleven.

Reading list

Sources

N. M. Patrikalakis and T. Maekawa, Shape Interrogation for Computer Aided Design and Manufacturing, Springer, 2002, chapter 5, “Intersection problems”. Not consulted directly. The standard survey of the field, covering the classification of intersection problems, subdivision, marching and loop detection.

T. W. Sederberg and T. Nishita, “Curve intersection using Bézier clipping”, Computer-Aided Design 22 (1990), 538–549. Not consulted directly. A sharper version of the box subdivision of section eight, which clips each curve to the strip that can contain the other instead of halving it.

J. Levin, “A parametric algorithm for drawing pictures of solid objects composed of quadric surfaces”, Communications of the ACM 19 (1976), 555–563. Not consulted directly. The pencil method: a ruled quadric in the pencil of the two, walked along its generators as in section seven.

L. Dupont, D. Lazard, S. Lazard and S. Petitjean, “Near-optimal parameterization of the intersection of quadrics”, Journal of Symbolic Computation 43 (2008). Not consulted directly. Three papers that make the pencil method exact for any two quadrics, with a parametrisation in the fewest square roots and every singular and split case classified.

R. E. Barnhill and S. N. Kersey, “A marching method for parametric surface/surface intersection”, Computer Aided Geometric Design 7 (1990), 257–280. Not consulted directly. Marching in the four parameters of the two surfaces, as in section ten.

T. W. Sederberg, H. N. Christiansen and S. Katz, “Improved test for closed loops in surface intersections”, Computer-Aided Design 21 (1989), 505–508. Not consulted directly. The normal-cone criterion of section eleven for ruling out closed loops between two patches.

Method

The figures are drawn by a program that computes what they show: the root separations of the tangency figure from their formula, the hits of sections four and five and the sections of section six from the closed forms above, with the plane sections of the cone from the generators through the apex, the boxes by running the subdivision of section eight, the marching round the cylinder and sphere with the predictor, corrector and step rule of section ten, the zigzags by running the alternating corrector, and the loop by tracing from the boundary points and from the critical points of the height. The quartics of section seven are drawn by walking the generators with the formulas of their cards, the arc ends and the thresholds of every panel computed from the closed forms. Each hit and each point of a section or a quartic drawn in sections four to seven lies on both of its objects to 10−9. Each march point lies on both surfaces to 10−12, and the midpoint of every chord stays within 1.6ε of the curve.

Every case panel of sections four to seven, 160 panels in thirty-six figures, was rebuilt in a second implementation of the closed forms, written separately from the figure program. Each returned the case its panel is titled with, the same number of points or curves and the same multiplicities, and every point drawn lies on its result to 10−9, or 10−7 for the conics in crossing planes and the quartics of section seven. The tangency band, the two-sphere circle, the Villarceau circles, the conic coincidence, the line–torus quartic and the five crossings of the subdivision figure were checked the same way.

The marching of section ten was also run in the second implementation, seeded from the grid samples where the distance to the other surface is a local minimum, corrected by Newton in the plane normal to the tangent, with the step set from the curvature. On the cylinder and sphere of the marching figure it returns one closed curve, the figure’s marched points lie within 1.1 × 10−7 of it, and its cubic B-spline through the marched points stays within 2.8 × 10−5 of both surfaces between them. Corrected by alternating projection instead, and seeded only from samples already on the other surface, the same march misses both this curve and a plane at height 0.3 across a unit sphere. The alternating-projection table was measured on two planes with the figure program.

Revision history

. New section seven: two quadrics in general position, with a card per pair and the cases that split into conics or lines. A card for conics in crossing planes.

. Part numbers renumbered for the new part two, The Mathematics a Kernel Stands On.