Skip to content

Marching is a surface pair, a tracer and a seeding policy

The marching intersection (intersect/numeric/marching.rs) composes three crate-private modules under intersect/numeric/marching/, and runs the coincidence check (coincidence.rs) before them:

  • pair.rs, the surface pair: the two surfaces and the point x = [u1, v1, u2, v2] on both. It converges a guess onto both surfaces (meet, with or without a plane through the predicted point; meet_touching where they touch; land on a parameter bound), clamps parameters into their bounds, predicts a step, and reads the local geometry: the direction where the surfaces cross (tangent) or touch (contact_direction), the directions of the branches crossing where they touch (crossing_branches), whether they touch (grazes) or lie on each other (osculates), and the box where traces end (trace_box: the smaller bounded side's, else the model-size cube). It knows nothing of steps or seeds. swapped and swap read the pair the other way round, once.
  • tracer.rs, the tracer: the step loop. From a seed it traces both ways and returns a Trace: a curve, with the unit tangent at each point, the parameters where the trace stalled and whether the surfaces touch along it; a touch at a point; or nothing. It sizes each step by the error of the cubic Hermite segment the fit will pass between its ends (step_error). It holds the pair's trace box, ends a trace where it leaves the box (box_exit), and tells the caller whether a seed lies in it (encloses). How the surfaces meet where a step is taken (crossing or touching) is the tracer's own per-step state, a private Contact value read at every step; it picks the direction and the corrector, and the pair and the caller never see it.
  • seeding.rs, the seeding policy: which sources run on a pair (patch pairs on two B-splines, the iso-curves of an elementary side, the parameter grids), in what order, and when one is left out (the iso-curves where the grid seeds only graze; the grids once the patch-pair search exhausts its budget). It returns the seeds in the order they are traced. It also seeds the branches crossing where a trace stalled (branch_seeds), which marching_intersection asks for after each trace. A patch-pair seed stays unconverged, with the box that skips it once an earlier curve crosses it, so that it is converged only when its turn comes, as before.

marching_intersection asks the policy for the seeds, hands each to the tracer, follows the branch seeds of its stalls, and fits and collects what it returns: a cubic Hermite curve through the points and tangents where the surfaces cross, an interpolant where they touch, both without the points the fit cannot tell apart (hermite_curve and without_confused, in curves/fitting.rs). Every result is bit for bit what it was before the split.

The split places the two roadmap items of marching. Tracing through a point where the surfaces touch changes only the tracer: a step that fails in one mode re-reads the pair (grazes) and switches its Contact, and the tracer reports the curve. Finding loops smaller than the iso-curve spacing changes only the seeding policy: a new source returns more seeds.

The surface pair stays generic over the two surface types, as the functions it replaces were, so the Newton loops are monomorphized for each pair of types and their evaluations inlined. Instruction counts on every marching bench moved by less than one percent.

Considered options

  • One file of free functions (before this decision): rejected. Its 28 generic functions each repeated the two surfaces and their bounds; the touching mode, decided once per seed, was threaded through four signatures; the seeding policy was spread over three functions; and the step loop could only be tested by running a whole march.
  • The touching mode on the pair, or passed by the caller: rejected. Either way a mode change in the middle of a trace would cross a module seam. On the tracer it is one variable of the step loop.
  • The pair over &dyn ParametricSurface: rejected. Every evaluation in the correctors would become an indirect call that cannot be inlined, in the innermost loops of the search.
  • Seeds as plain converged points: rejected. A patch-pair seed is skipped when an earlier trace already crosses its patches, and is converged only when it is reached; converging every patch-pair seed up front would cost Newton runs the march skips today.

Amendment, 2026-10-30

ADR 0026 adds a second round to the seeding policy: seeds returns the seeds traced first and whether later_seeds follows, and later_seeds runs with the curves traced so far: the extremum search of ADR 0026 when one surface is elementary, otherwise the patch-pair search, which drops the pairs those curves already cross. The policy still decides which sources run and in what order; marching_intersection traces the two rounds in turn.

Amendment, 2026-11-02

trace_box no longer falls back to the model-size cube. It returns a TraceBox: Bounded, the smaller bounded side's box grown by a tenth of its diagonal, where leaving the box is only a runaway guard; or Seeded, the box of the seeds grown by the larger surface size (a cylinder's or cone's radius, a sweep profile's diagonal) or, for two planes, by the larger of the seeds' spread and the size of their seeding windows (every grid sample along their line is a tie, so rounding may leave a single seed), where leaving the box cuts the branch and the cut is reported as IncompleteReason::Unbounded. The cube's size was an arbitrary model scale that every unbounded branch was cut at, and the same pair translated by a large distance traced different branches. A seeded box moves with the seeds, so the branches do not depend on where the pair lies. The tracer gains Cut, the points where a trace left its box, and the two rounds of traced_intersection rebuild the tracer with every seed reached so far, so later seeds beyond the first box are still enclosed.