Skip to content

One closed-form pair table owns routing

Numeric is the default and exactness is the exception. One module, intersect/routing/closed_form.rs, the closed-form pair table, says which pairs of shapes intersect solves in closed form and which solver answers each; every other pair goes to the numeric search. The router asks the table about every pair. It no longer decides first whether an operand "has an elementary basis" and only then asks the table, and the plane cuts of the swept surfaces (#130) are entries of the table rather than a second exact route beside it.

Every operand is read as its basis, the shape the table matches on, and every operand has one. A concrete elementary curve or surface is its own basis, so a pair whose types are known at compile time still reaches its typed solver with no run-time dispatch (ADR 0011). Any other operand is read as its kind: CurveKind (an ElementaryCurve, or unlisted), SurfaceKind (an ElementarySurface, a swept surface, or unlisted) and CurveKind2D (a Curve2D holding an elementary curve, or unlisted). A B-spline or Bézier shape is unlisted: the table pairs it with nothing. A trimmed curve is read as the basis of the curve it trims, with its range. Every surface pair is matched on the two kinds, one match that holds the plane–swept entries beside the elementary ones; a surface pair costs a closed-form solver or a march, next to which the match is noise.

ExactFamily<B> is the table's interface. It is implemented for every pair of bases, from the entries, and answers two questions: exact_family, the family result through the pair's solver, or the numeric search handed in where the table does not list the pair or the solver does not cover the configuration (two cylinders with skew axes); and has_closed_form, whether the table lists the pair. Marching reads has_closed_form to decide whether the circles of an elementary surface seed against the other surface, where it used to restate one entry ("not against a torus") by hand.

The operand lists come from the shape enums' variant lists (ADR 0017), not from one hand-written list per family. The Intersect impls take every shape of curve_variants!(Curve, …), surface_variants!(Surface, …) and curve_2d_variants! plus the enums, and the table's enum and kind forwarding takes the elementary variants of the same lists. A variant list is already the place a new shape type is added, so deriving from it means a new shape type edits no routing list at all; a separate operand list per family would be one more list to keep in step, the cost this decision removes. The entries themselves are written out pair by pair (Line => [Plane, Cylinder, Cone, Sphere, Torus]), because which pairs have a closed form is the content of the table, not something derived. Adding a closed form is one solver and one entry: a typed solver under intersect/exact/, read into its family result in exact_family.rs and listed in its family's solved entry, or, like the plane cuts, a solver over kinds listed in the SurfaceKind match.

Considered options

  • Keep the two-step test, elementary basis first and the table second: rejected. It made "exact" the default question and spread the answer over three places: the operand traits (elementary_basis, surface_basis returning Option), the table, and as_plane/as_swept with their fallback arm. A swept surface had to be special-cased beside the table because it has no elementary basis.
  • Route every pair through the kinds, curves included: rejected. ADR 0011 measured 13–16 ns saved per circle pair by the typed path, and curve pairs are the hot loop. Only surface pairs, where the solver dominates, are matched on kinds.
  • One operand list per family, written once in routing.rs: rejected for the reason above: the variant lists already exist and are already the place a shape is added. The macros that read them stay small (shapes!, operands!, curve_operands!, surface_operands!, curve_operands_2d!).
  • A has_closed_form query without a value, on the types: rejected. The enum and kind bases answer only from their variant, so the query takes the operands like exact_family; marching passes a unit circle, since the table answers by kind.
  • A closed-form-first path for Python (ADR 0021 deferred it): left out. No consumer has asked for it; has_closed_form stays crate-private until one does.

Consequences

  • Behaviour is unchanged: the same pairs go exact, with the same results. A test walks every pair of enum-variant samples, the swept surfaces and the Bézier types included, and checks that each routes to the closed form exactly when the table lists it, against an expectation written from the shapes.
  • The public interface is unchanged: the same 522 Intersect impls. The impls for a trimmed curve over any operand now carry a where bound on the crate-private ExactFamily, which every crate operand meets.
  • CurveBasis and CurveBasis2D shrink to what the readers in exact_family.rs need (PointExtrema, Copy, conversion to the enum); they no longer list one ExactFamily bound per partner.
  • SurfaceOperand::Basis is a generic associated type, since a swept surface's kind borrows the surface; a trimmed curve's impls against a surface bound it for every lifetime.
  • Instructions counted inside the benchmarked routine: the typed paths and conic_quadric are unchanged, curve pairs through the enums are within 0.5 % (a 2D circle pair through Curve2D is 15.6 % cheaper), and surface pairs pay the one kind match: up to 1.4 % through Surface, and 1.6 to 2.8 % on a plane cut of a swept surface, a solver of a thousand to four thousand instructions.