Skip to content

Distance extrema are an operation; projection belongs to the shape

The local minima of the distance between two curves lived inside the numeric intersection: the search in intersect/numeric/curve_curve.rs, its B-spline half in curve_curve_hull.rs, its results in intersect/results/. They now form their own operation, operations/extrema/, laid out like discretization/ (ARCHITECTURE rule 3):

File Holds
extrema/curve_curve.rs curve_curve_extrema, its seeds and refinement, CurveCurveExtrema, CurveCurveExtremum
extrema/curve_curve_pieces.rs the minima search on the Bézier pieces of two B-splines

Point projection stays with each shape. Projecting a point is asked for the point and its parameters; the distance comes with it. On every elementary shape it is the closed-form inversion and one evaluation: parameters_of then point_at on a plane, cylinder, cone, sphere or torus, parameter_of then point_at on a line or circle (the cone picks the nearer of its two generator lines in the meridian plane, the torus the nearer of its two tube circles, so the inversion of any point is its closest point). The inversion is core to the shape, used by intersections and UV curves, and contains is read from it. So PointExtrema (curves) and project_point (a required method of ParametricSurface) are implemented in the shape's file, as on surfaces; only their result types are shared, in operations/projection.rs. A first attempt moved the curve impls to operations/; it split each curve's inversion from its shape for no gain and left curves and surfaces organised differently, so it was undone.

Extrema calls the shared Bézier-piece machinery, never the reverse. The numeric curve–curve intersection is read from the minima at zero distance (search_curve_curve, hits_at_minima), so it calls extrema::curve_curve, and its own grid fallback is sampled_curve_curve_extrema, a named entry point rather than a flag on the search. The extrema search runs on machinery the intersection searches share and that stays in intersect/numeric/: the subdivision driver (ADR 0019), the Bézier pieces, a curve as a B-spline over a range with the map back (pairs_with_bspline), and the curve's parameter domain (parameter_domain.rs; its seeding for the minima search stays in extrema). The seeding of the curve-pair minima (curve_curve_seeds) lives in extrema, so that machinery never calls extrema. Two result types still cross: the intersection's candidate hits are reported as CurveCurveExtremums, and the extrema carry the intersection's Completeness and CurveCurveRegion.

That machinery serves four searches (curve–curve hits and minima, curve–surface hits, the patch pairs that seed marching), not only the intersection. Moving it to a home of its own is left until curve–surface extrema need it too. The release build uses the default codegen units, so module layout changes what is inlined, and a split once cost 2.3 % on a benchmark: such a move is benchmarked first.

Not done here. Curve–surface and surface–surface extrema, and a local search refining one starting parameter next to the global one, have no caller yet. They go in extrema/ when one appears (curve_surface.rs, surface_surface.rs).

The level-function extrema that seed marching are a different notion; their module is marching/level_extrema.rs, so that "extrema" in a path means distance extrema between shapes only.

Amendment: one Python method for both extrema (#201)

In Python, a 3D curve's extrema(other) takes a point or a curve. A point gives its projections, a list of CurveProjection. A curve gives the CurveCurveExtrema of the pair, which geomcore.curve_curve_extrema(first, second) returned before; that function now warns and goes in the next release. Users tried curve.extrema(other_curve) first and got a type error about Point. The Rust split stays: PointExtrema::extrema with each shape, and the free function curve_curve_extrema in operations/extrema/. A 2D curve's extrema still takes a point alone, since there is no 2D curve-pair extrema.

The point case keeps its plain list. A list has len and iteration, and a point's projections have no completeness to report, so a result class would only wrap them. Point extrema on the elementary and swept surfaces, which only B-spline and Bézier surfaces have today, are a separate issue.

Amendment: curve–surface and surface–surface extrema (#251)

A clearance check needs the distance between a curve and a surface and between two surfaces, so both pairs now have their extrema: curve_surface_extrema in extrema/curve_surface.rs and surface_surface_extrema in extrema/surface_surface.rs, with the numeric search they share in extrema/surface_search.rs. Their results, CurveSurfaceExtrema and SurfaceSurfaceExtrema, are built like CurveCurveExtrema: the minima closest first, each with the parameters on both shapes, both points and the distance, and the search's completeness. The minimum distance of a pair is the closest of them (closest()); no separate function computes it, since the search that proves a global minimum is the one that finds the local ones.

Minima only. The maxima of the distance between two shapes are at infinity for every unbounded pair, and a saddle answers no question a caller has asked. Shapes that meet report each crossing at distance zero; two surfaces meeting along a curve report one point of it, like any other valley.

Closed forms first. A whole elementary pair with a closed form is solved exactly and reports Complete: a line against a plane, a sphere or a cylinder, a circle against a plane, two planes, two spheres, and two parallel or coaxial cylinders. "Whole" means bounds and periods equal to the elementary shape's, so a trimmed one goes to the search, which reads its bounds. A valley of equal minima (a line parallel to a plane, concentric spheres, coaxial cylinders) reports one pair: the first shape at its reference parameters where the valley passes there.

Every other pair is sampled, and says so. Each operand is sampled on a coarse grid (24 samples a finite surface direction, 96 along a curve, a geometric progression along an unbounded direction, as in the curve-pair search). Every sample of one operand is paired with the nearest sample of the other, and the discrete minima of that distance seed the minimiser from both operands, so a far wall of a channel behind a nearer one is still found. The minimiser is nested: the outer parameters (the curve's, or the first surface's) take bounded Newton steps on the distance to their projection on the second surface, found from the last one, with the outer Hessian reduced by the projection's (its Schur complement), so the outer step is the joint one. The result reports Sampled. A proven search on Bézier pieces, as curve_curve_pieces.rs does for two curves, is the follow-up for the B-spline pairs.

The same answer at any size and placement (ADR 0051). The similarity judge covers both pairs, with no known failures. What it took:

  • Along a valley the gradient is rounding alone. Rates below the rounding of the coordinates read as zero, and each outer step is held to a window: a period about the start in a periodic direction, and, in an unbounded one, as far as the seeds reach. Without it the step ran off along the valley to where the coordinates round coarser than the tolerance.
  • Two minima are one basin when their distances agree within the tolerance (or the lower is a contact) and no ridge rises between them. Its way is walked on the outer shape, each projection started from the last in steps of a quarter of the scale, so it stays on one sheet of the other shape and must arrive at the other minimum's point; or, for a valley of the second surface over one outer point, along the straight way in both parameter spaces. A basin reports its lowest minimum, replaced by a representative read at the reference parameters, so that a valley's pair does not depend on where the search entered it.
  • Near a shallow crossing far from the origin the gradient of the squared distance (the gap times the sine between the shapes) falls below rounding first, and the minimiser stops short. Gauss–Newton steps on the gap itself close it.
  • Where a parameter collapses (a sphere's pole, a cone's apex) the box test on the parameters cannot tell a minimum, so the distance is read around the point instead. Where the outer point sits on a centre of curvature of the second surface (a cylinder's axis), the projection is not isolated and the reduced Hessian overstates the curvature; a minimum there is kept only if the distance does not fall a probe step away along each outer parameter.

The nearest sample is a plain scan. The grids hold a few hundred samples, and those of one operand often lie at much the same distance from the other's, where a box hierarchy pruned little: the scan halved the search's instructions on the benchmarks.

Python. A 3D curve's extrema(other) takes a surface too, whole or trimmed, and a surface's extrema(other) takes a curve or a surface, on every surface class and TrimmedSurface. A point stays an operand of the B-spline and Bézier surfaces only; the other surfaces raise TypeError for it and name project_point, as the point extrema of #247 were closed as not planned. minimum_distance(other) on curves and surfaces gives the closest minimum or None. The surfaces' extrema moved from the B-spline and Bézier method set surface_extrema into parametric_surface, which every surface and TrimmedSurface take (ADR 0029).

Amendment: curve–curve extrema in the plane (#422)

curve_curve_extrema takes two 2D curves too, with the contract it has in space: local minima closest first, both parameters and both points of each, a valley of equal minima reported once at its representative pair, and the search's completeness. The plane is not a second search. The pair is embedded in space at z = 0, as the 2D numeric intersection does (intersect/numeric/embedding_2d.rs): each curve keeps its parameters and its exact B-spline or elementary basis, so the closed forms of two straight curves and of a full line against a curve, the proven search on Bézier pieces and the grid all run as they do in space, and the minima are read back in the plane. Two 2D curves and the same curves lifted to z = 0 give the same minima to the bit (tests/curve_curve_extrema_2d.rs).

Results generic over the tier (ADR 0014). GenericCurveCurveExtremum<P> and GenericCurveCurveExtrema<P> are the result, with CurveCurveExtremum/CurveCurveExtrema in space and CurveCurveExtremum2D/CurveCurveExtrema2D in the plane, so callers name them as before. The completeness keeps CurveCurveRegion, whose corners in the plane sit at z = 0, as the 2D intersection's do.

One function, one public trait for its operands. curve_curve_extrema<A, B> bounds its operands by CurveExtremaOperand, whose associated Point names the tier and makes a pair of mixed tiers a type error. Every 3D ParametricCurve implements it through a blanket impl, so a caller's own curve and a generic caller bounded by ParametricCurve are unchanged; the 2D curves of the crate implement it one by one, from the Curve2D variant list, plus TrimmedCurve2D over them. Its other items, the curve as the search in space takes it and the reading back of the minima, are hidden as the routing hooks of ParametricCurve are (ADR 0022).

Considered options

  • Write the search once over the tier's point: rejected for now. The search reads the cross product of tangents and chords in a dozen places (the sine between two straight curves, the curvature of an unbounded curve, the single-minimum test on chords) and its Bézier-piece half runs on BSplineCurve and the pieces' 3D bounds, so a generic search is a rewrite of both files for no change of result. The embedding costs one copy of a B-spline's poles per call and a third coordinate per evaluation: on the three 2D extrema benchmarks (circles against ellipses, B-spline ellipses against ellipses and against each other) the plane pairs spend 98 to 100 % of the instructions of the same pairs lifted to z = 0 and searched in space.
  • A separate curve_curve_extrema_2d: rejected. The issue asked for one function, as intersect serves both tiers, and a second name doubles every example and binding.
  • A 2D operand trait bounded by ParametricCurve2D for a caller's own 2D curve: not now. A blanket impl over ParametricCurve2D would overlap the one over ParametricCurve, and no caller has a 2D curve of its own; the 2D intersection takes the crate's curves alone too.

The judge. tests/similarity_invariance.rs has a 2D family: every pair of the 2D samples, and every pair of their trimmed pieces, compared by the count and completeness of their minima, with no known failure. Their points are left out, as the 3D family leaves them out: a tangent contact's point moves under a translation by the rounding over the angle (ADR 0051). A whole curve against a piece of itself was left out too, its valley split or merged depending on the placement; the amendment of #444 below brought it in.

Python. A 2D curve's extrema(other) takes a 2D curve or TrimmedCurve2D as well as a point, minimum_distance(other) is the closest minimum, and the deprecated geomcore.curve_curve_extrema takes 2D pairs. A 2D curve against a 3D curve or a surface raises TypeError naming the tier. The amendment of #201 above, that a 2D curve's extrema takes a point alone, no longer holds.

Amendment: a valley along a piece of a shape (#444)

A whole curve against a piece of itself has a valley of zero distance along the piece, and so do a circle against a piece of a coaxial cylinder (at the gap between their radii) and a surface against a piece of itself. Its result depended on the placement (ADR 0051): the sample circle against its arc gave two minima at the origin and one after a translation by 1e3, and the B-spline and Bézier samples against their pieces moved the reported pair by more than the judge's slack. Two things broke.

Members were merged along the straight way between their parameters. On a closed curve that way is the shorter way round, which leaves an arc that covers more than half the circle, so two members of one valley on either side of the circle's start were told apart. Two curve minima are now one valley when their distances agree within tol.confusion and either curve, walked from one to the other in 16 steps, keeps its closest point on the other curve (each found from the last) at that distance and arrives on the other minimum's point; the straight way still counts, as it is the valley of two copies of one curve. Two surfaces already walked either one.

The representative was read at the first shape's reference alone. A valley along a piece of the second shape does not pass the first shape's reference (the circle's 0 is not on an arc from 1.26 to 5.65), so the reported pair was whichever member the search reached. The representative is now, in order: the first shape at its reference parameters with its closest point on the second, then (surfaces) that pair carried along one parameter of the first, then the second shape at its reference parameters with its closest point on the first, then (surfaces) carried along one parameter of the second; the first of them on the valley is reported. Each is read from the shapes, not from the search. One of the first and the third lies on any valley whose ends are the ends of the shapes' domains, since the valley starts where the later of the two pieces starts: an overlap of two pieces, a whole shape against a piece of itself, parallel segments, concentric arcs. A carried pair that does not move from the member is skipped: the valley runs along the other parameter, and the member is not a point read from the shapes. The representative replaces a minimum that the search reached alone on its valley as well, not only one met at distinct places, so that the pair does not depend on how many members the search found; an isolated minimum is kept, since no candidate lies on a valley with it. A curve and a surface walk the surface between a minimum and the candidate read from the surface's reference, so that two members of a valley the shorter way round a closed curve parts are both moved to that candidate and then merged as copies. Representatives of two kept minima that turn out to share a valley are reported once.

An overlap stays Incomplete. On a B-spline or Bézier pair the overlap keeps the piece search from settling, which reports Incomplete(BudgetExhausted) with the overlap's region. Recognising the overlap and reporting it complete was considered and rejected: the extrema search would need the overlap detection of the intersection, and the region is still one where it proved nothing, which is what the caller is told. The result already holds at every placement, and the valley's one minimum is now the same pair at every placement. A caller asking whether two curves overlap asks the intersection, which reports overlaps.

Cost. The walks and the second shape's candidates are paid only at a minimum where the distance is flat to second order in a direction it is free to move in: the smallest eigenvalue of the Hessian of half the squared distance (reduced onto the outer shape for a surface), with each parameter read per unit length and those held at a bound left out, is at most 1e-4. On a valley it is zero to rounding; at an isolated minimum where two curves cross at the angle a it is about sin(a)^2. The test is dimensionless, so it holds at any size. It is read at the foot already found, without a new search. A minimum the search reached alone is now replaced only when it lies on a valley, and copies of one minimum skip the straight-way test. The first candidate of a curve–surface or surface–surface valley, and the minimum the search reaches from it, do not depend on the minimum represented, so a search reads them once rather than once per kept minimum. Every extrema benchmark got faster: the curve–curve ones by up to 3 %, the curve–surface ones by up to 5 %, two B-spline surfaces by 24 %.

The judge. The 3D family takes every pair of the trimmed pieces and every sample against every piece, as the 2D family does now, and for a whole curve against its own piece compares the valley's two points as well. A new valley-extrema family takes curves along valleys of pieces of a cylinder, a plane and a sphere (a circle on the cylinder and in the plane, a coaxial circle inside the cylinder, an axis of the plane and a segment of it), and a cylinder and a sphere against pieces of themselves, and compares every point of every minimum. None has a known failure.

A cylinder about the z axis against a piece of itself loses the minimum held on the two pieces' edges across the gap of the first after a translation by 1e7: the search does not reach it, before this change as after (#448). The tilted placement of the judge keeps it.