Any surface searches as patches with a map back
ADR 0007 converts an elementary shape to its exact rational B-spline so that a curve against a B-spline goes through the proven subdivision search. This decision applies the same conversion to surface–surface pairs, and puts it in one module, intersect/numeric/surface_patches.rs.
SurfacePatches holds a surface as Bézier-searchable B-spline patches and maps a point of a patch back to the surface's own parameters:
- a B-spline is its own patch, borrowed, with nothing to map;
- an elementary surface is converted as before (a cone holding its apex split there into its two nappes; a sphere or torus whole);
- an extrusion of a bounded profile is converted with the profile's B-spline across and the sweep along, over the part inside a cover box; a revolution of a bounded profile is converted whole.
An unbounded surface (a plane, cylinder, cone or extrusion) needs a cover box, which every hit lies inside: against a B-spline curve, the box of its control points grown by a margin; against another surface, the other's box grown the same way. The cover is a closure, called only when the surface needs one, so a bounded surface never computes it. Two unbounded surfaces have no cover and are not converted. The map back reads a straight direction as is and an angle (a circle or ellipse profile, the turn of a revolution) through the curve's own parameter, nearest the point's own position on the patch so that it stays on its own side of a seam, then settled into the surface's range. The hits and regions of a curve search are carried back through the same map (carry_hits).
Its consumers:
- The curve searches (
pairs_with_bspline.rs): a B-spline curve against any surface that converts goes through the hull search. That adds the swept surfaces, which were sampled; a B-spline curve against an extrusion or a revolution of a bounded profile now reports complete or incomplete like the other pairs of ADR 0007. The conversion is exact, so the completeness it proves holds for the surface itself. - The patch-pair search (
patch_pairs.rs) takes twoSurfacePatchesand searches every pair of their patches. Two B-splines run as before, one patch each. - The seeding policy of marching (ADR 0025) searches, once its iso-curve or grid seeds are traced, for the loops smaller than the spacing of the iso-curves and grids: a bump of a B-spline finer than its grid against a plane, cylinder or extrusion, a small cap a plane cuts off a torus. The policy returns its seeds in two rounds:
seeds, traced first, andlater_seeds, searched with the curves traced so far. The later search takes one of two routes.
One side elementary: the extrema of its level (marching/extrema.rs). An elementary surface E is the zero set of a polynomial F (a plane (p - o) . n, a sphere |p - c|^2 - r^2, a cylinder, both nappes of a cone, a torus (|q|^2 + R^2 - r^2)^2 - 4 R^2 (|q|^2 - (q . a)^2)). Along the other surface S, g = F(S(u, v)) vanishes exactly on the intersection. The theorem: if a loop of the intersection bounds a disc D in the parameters of S, and g is not zero all over D, then g has a negative minimum or a positive maximum inside D (extreme value theorem), where its gradient is zero and, S being twice differentiable there, its Hessian semidefinite of the matching sign. An iso-curve of S through that point leaves D (it runs to the parameter bounds, or closes around a period, which no curve inside a disc does), so it crosses the loop; intersecting it with E gives a seed. Its preconditions are the limits of the guarantee: a loop that wraps around a periodic parameter bounds no disc and is left to the iso-curves that cross it, and a loop whose extremum lies within tol.confusion of E is a touch within the tolerance and seeds nothing.
Three rules follow from it, each with a test at its edge:
- A critical point that is a saddle, a minimum where g > 0 or a maximum where g < 0 holds no loop, and seeds nothing. A saddle-only pair (a plane above the saddle of z = x^2 - y^2) and a dent above a plane give no seeds; a saddle beside a bump the plane cuts does not hide the bump's loop.
- Plane against torus, closed form. With n the plane's normal and a the axis, the signed distance to the plane along the torus has, when n is not along a, four critical points: the highest and lowest points c +- (R n' + r n) (n' the unit part of n across the axis) and two saddles on the inner half, where the Gaussian curvature is negative. A loop bounding a disc encloses the highest point (above the plane) or the lowest (below it), so the meridian through each, intersected with the plane in closed form, seeds every such loop: this pair is complete in that sense. When n is along a the critical points fill the top and bottom circles, each at one distance and going round the axis; a disc bounded by a loop would hold one point of them and so the whole circle, which no disc on the torus does, so there is no such loop and no shot.
- A knot span whose tangents never meet the gradient of F at right angles holds no critical point. With the derivative cones of the span (axis a, spread s_a) and a cone of the gradient's direction over its box (axis c, spread s_c; exact for a plane, 2 h / d for a sphere or cylinder, h the half-diagonal of the box and d the distance of its centre from the centre or axis; none for a cone or torus), |c . a| > s_a + s_c for either tangent bounds |w . t| away from zero for every unit gradient w and tangent t in them. A tilted plane patch against a plane keeps no span.
The search for the extrema in the remaining spans is not proven. From each span's centre, the level climbs up and down (a Newton step on the Hessian with its eigenvalues made negative, the gradient where it is flat, in a trust box that doubles after each rise and shrinks to a quarter after each fall), and the climb is dropped when it leaves the span grown by half its size or does not settle in 24 samples. A climb started inside D never leaves it, since the level rises from where it starts and is zero on the loop; but the search starts only at the centres of the spans that may hold a critical point, so a loop is found when the climb from the span holding its extremum reaches it. Shots along the direction where the level curves least (along a ridge of maxima: a ruling, a circle of a torus) and repeats of the same iso-curve are taken once, and a shot whose control points all lie on a convex side of E (either side of a plane, inside a sphere or cylinder) skips the intersection, which it cannot have.
Neither side elementary (or no cover): the patch-pair search (patch_pairs.rs), as before. It drops a pair whose normals are never parallel as soon as a traced curve crosses its overlap, flat or not: no closed loop hides in such a pair, and a seed from it would land on that curve. A loop is still found unless a traced curve also crosses the open pairs around it.
The policy also leaves the later search out in two cases where it found nothing new in the probes and cost the most: - both surfaces bounded and neither a B-spline (two spheres or tori, a sphere and a torus): both parameter grids already sample each surface at its own scale; - the first seeds traced only touches (tangent curves and contact points): along a touch the patch-pair search would halve every pair down to its smallest pieces. This is read from what was traced rather than by testing each seed for a touch before tracing, which cost 5 % on a plane tangent to a cylinder patch.
Cost, profiled: the patch-pair search spent its instructions in the subdivision itself (derivative cones, halving and box tests), 60 % more seeding on a plane cutting a torus obliquely and up to 20 % on a plane or cylinder against a B-spline patch, most of it halving the pairs where the normals may be parallel (a plane perpendicular to a torus's axis is parallel to it along two whole circles). The extremum search replaces it wherever one side is elementary: a few dozen surface samples and at most a few iso-curve intersections. In-loop instructions against the marching it builds on, no marching bench where nothing new is found costs more than 1.8 % more (a plane parallel to a cylinder patch, from 19.7 % with the patch-pair search); a cylinder against an extruded wall costs 1.3 % more (from 19.6 %), a plane cutting a torus obliquely 1.0 % less (from 58.7 % more), a plane perpendicular to the axis of a torus patch 0.9 % more (from 20.8 %), and the cheapest marching call no more (from 6.4 %).
Marching stays sampled (ADR 0007): the extremum and patch-pair seeds add loops, but the iso-curve and grid seeds before them are still samples, the climbs to the extrema are not proven to reach every one, and neither search proves that each curve it seeds was traced whole.
The curve side (HullCurve, hull_curve) stays in pairs_with_bspline.rs. It maps one parameter, restricts a trimmed curve to the part among the other's control points, and serves the curve–curve searches as well, which never meet a surface; folding it into SurfacePatches would make one module of two halves with no shared caller. It shares the angle mapping (nearest, settle) and the cover helpers (grown, reach), which move to surface_patches.rs, and its map back of hits becomes one method like the surface's.
Considered options
- Searching the patch pairs before tracing anything (as for two B-splines): rejected. The search then halves every pair along the curves the iso-curves and grids find anyway, down to flatness: twice the seeding cost of a plane cutting a torus.
- Dropping seeds near a traced curve before converging them, or converting lazily: not enough on their own. Converging and tracing the extra seeds, and the conversion, are each a few percent of the extra cost; the subdivision is the rest.
- The patch-pair search for every pair that converts, pruned by the traced curves: rejected where one side is elementary. Halving to the pairs where the normals may be parallel is the cost, and it stays 20 % to 60 % over the seeding without it on a plane or cylinder against a patch; the extrema of the level find the same loops for a few dozen samples.
- Index theory (a loop encloses critical points whose indices sum to one): not needed on top of the extremum argument, which is the same fact in the form that names the point to shoot through.
- Newton's method on the gradient of the level from each span centre: rejected. Between the top of a bump and the flat around it the Hessian is indefinite, and the step runs to a saddle or off the patch; the climb keeps the level rising, so it ends at a maximum or minimum.
- Patch-pair seeds for every pair that converts: rejected. On two bounded elementary surfaces and on surfaces that only touch, it found no loop the grids missed, at the costs above.
- Converting the swept surfaces through the routing's operand view: rejected. The searches are generic over
ParametricSurface, and the swept surfaces join them through two hidden hooks,as_extrusionandas_revolution, likeas_bsplineandas_elementary(ADR 0022). - Converting a swept surface over an unbounded profile (a line, parabola or hyperbola) over the cover too: left out. It needs the profile trimmed to the cover first, and no pair in the tests or fixtures has one; such a pair keeps its sampled search.
- Computing the cover eagerly: rejected. A B-spline curve against a B-spline surface, the most common pair, would compute and discard a box on every call.