Skip to main content

brep_kernel/offset/
offset.rs

1use crate::{KernelRefusal, KernelStage, OrRefuse};
2use crate::fit::solve_dense;
3use crate::topology::{BrepSolid, CoedgeRecord, EdgeRecord, FaceRecord, LoopRecord, VertexRecord};
4use crate::{
5    interpolate_curve, measure_edge_against_pcurve_image,
6    measure_surface_fit_against_pointwise_offset, offset_construction_band, solid_model_scale,
7    vertex_endpoint_gap, vertex_tolerance_from_edges, KnotVector, MeasuredTolerance, NurbsCurve,
8    NurbsSurface, OffsetEvaluator, OffsetNormal, Vec2, Vec3, Vec4,
9};
10use rustc_hash::FxHashMap as HashMap;
11use serde::Serialize;
12
13fn domains(surface: &NurbsSurface) -> Result<([f64; 2], [f64; 2]), KernelRefusal> {
14    Ok((
15        KnotVector::new(surface.knots_u.clone(), surface.degree_u).or_refuse(KernelStage::Refine, "new")?.domain(),
16        KnotVector::new(surface.knots_v.clone(), surface.degree_v).or_refuse(KernelStage::Refine, "new")?.domain(),
17    ))
18}
19
20/// The normal `offset_surface` offsets along: the face's oriented normal with
21/// the singular-row recovery, i.e. the shared evaluator's
22/// [`OffsetNormal::FaceStable`] lane, whose body this function used to be.
23fn stable_face_normal(face: &FaceRecord, u: f64, v: f64) -> Result<Vec3, KernelRefusal> {
24    OffsetEvaluator::new(
25        "offset_surface",
26        &face.surface,
27        OffsetNormal::FaceStable {
28            same_sense: face.same_sense,
29        },
30    )
31    .normal(u, v)
32    .or_refuse(KernelStage::Refine, "normal")
33}
34
35fn greville_parameters(knots: &KnotVector) -> Vec<f64> {
36    let mut parameters = (0..knots.control_point_count())
37        .map(|index| {
38            knots.knots[index + 1..=index + knots.degree]
39                .iter()
40                .sum::<f64>()
41                / knots.degree as f64
42        })
43        .collect::<Vec<_>>();
44    let domain = knots.domain();
45    parameters[0] = domain[0];
46    *parameters.last_mut().unwrap() = domain[1];
47    parameters
48}
49
50/// Rational collocation matrix: rows are the rational basis functions
51/// R_i(t) = N_i(t)·w_i / Σ_k N_k(t)·w_k evaluated at each parameter. With
52/// uniform weights this reduces to the ordinary B-spline collocation matrix.
53fn collocation_matrix(knots: &KnotVector, parameters: &[f64], weights: &[f64]) -> Vec<Vec<f64>> {
54    parameters
55        .iter()
56        .map(|parameter| {
57            let mut row = vec![0.0; knots.control_point_count()];
58            let span = knots.find_span(*parameter);
59            for (offset, value) in knots
60                .basis_functions(span, *parameter)
61                .into_iter()
62                .enumerate()
63            {
64                let index = span - knots.degree + offset;
65                row[index] = value * weights[index];
66            }
67            let denominator: f64 = row.iter().sum();
68            if denominator.abs() > 0.0 {
69                for value in &mut row {
70                    *value /= denominator;
71                }
72            }
73            row
74        })
75        .collect()
76}
77
78/// Split the weight grid into per-direction factors when it is separable
79/// (w_ij = a_i·b_j), which covers every tensor surface built from rational
80/// profile/rail curves (cylinders, cones, spheres, tori, revolves).
81fn separable_weights(weights: &[Vec<f64>]) -> Option<(Vec<f64>, Vec<f64>)> {
82    let first_row = weights.first()?;
83    let anchor = *first_row.first()?;
84    if anchor.abs() <= 1e-12 {
85        return None;
86    }
87    let a: Vec<f64> = weights.iter().map(|row| row[0]).collect();
88    let b: Vec<f64> = first_row.iter().map(|w| w / anchor).collect();
89    for (i, row) in weights.iter().enumerate() {
90        for (j, &w) in row.iter().enumerate() {
91            if (w - a[i] * b[j]).abs() > 1e-10 * (1.0 + w.abs()) {
92                return None;
93            }
94        }
95    }
96    Some((a, b))
97}
98
99/// Interpolate the sample grid in the SOURCE surface's rational basis (same
100/// knots and weights). When the true offset is representable in that basis —
101/// planes, cylinders, cones, spheres, tori — collocation at the Greville grid
102/// recovers it EXACTLY, so offset carriers stay real analytic surfaces
103/// instead of non-rational approximations with span-scale wobble.
104fn interpolate_tensor(
105    knot_u: &KnotVector,
106    knot_v: &KnotVector,
107    parameters_u: &[f64],
108    parameters_v: &[f64],
109    samples: &[Vec<Vec3>],
110    weights: &[Vec<f64>],
111) -> Result<Vec<Vec<Vec4>>, KernelRefusal> {
112    let count_u = parameters_u.len();
113    let count_v = parameters_v.len();
114    if let Some((weights_u, weights_v)) = separable_weights(weights) {
115        let matrix_u = collocation_matrix(knot_u, parameters_u, &weights_u);
116        let matrix_v = collocation_matrix(knot_v, parameters_v, &weights_v);
117        let mut intermediate = vec![vec![Vec3::default(); count_v]; count_u];
118        for column in 0..count_v {
119            let solve_axis = |axis: fn(Vec3) -> f64| {
120                solve_dense(
121                    matrix_u.clone(),
122                    samples.iter().map(|row| axis(row[column])).collect(),
123                )
124            };
125            let x = solve_axis(|point| point.x).or_refuse(KernelStage::Refine, "solve_axis")?;
126            let y = solve_axis(|point| point.y).or_refuse(KernelStage::Refine, "solve_axis")?;
127            let z = solve_axis(|point| point.z).or_refuse(KernelStage::Refine, "solve_axis")?;
128            for row in 0..count_u {
129                intermediate[row][column] = Vec3::new(x[row], y[row], z[row]);
130            }
131        }
132        let mut controls = vec![vec![Vec4::from_point(Vec3::default(), 1.0); count_v]; count_u];
133        for row in 0..count_u {
134            let solve_axis = |axis: fn(Vec3) -> f64| {
135                solve_dense(
136                    matrix_v.clone(),
137                    intermediate[row].iter().copied().map(axis).collect(),
138                )
139            };
140            let x = solve_axis(|point| point.x).or_refuse(KernelStage::Refine, "solve_axis")?;
141            let y = solve_axis(|point| point.y).or_refuse(KernelStage::Refine, "solve_axis")?;
142            let z = solve_axis(|point| point.z).or_refuse(KernelStage::Refine, "solve_axis")?;
143            for column in 0..count_v {
144                controls[row][column] = Vec4::from_point(
145                    Vec3::new(x[column], y[column], z[column]),
146                    weights[row][column],
147                );
148            }
149        }
150        return Ok(controls);
151    }
152
153    // Non-separable weights: solve the full tensor collocation system with
154    // the exact 2D rational basis. Nets are small in practice.
155    let unknowns = count_u * count_v;
156    let mut matrix = vec![vec![0.0; unknowns]; unknowns];
157    for (k, &u) in parameters_u.iter().enumerate() {
158        let span_u = knot_u.find_span(u);
159        let basis_u = knot_u.basis_functions(span_u, u);
160        for (l, &v) in parameters_v.iter().enumerate() {
161            let span_v = knot_v.find_span(v);
162            let basis_v = knot_v.basis_functions(span_v, v);
163            let row = &mut matrix[k * count_v + l];
164            let mut denominator = 0.0;
165            for (du, value_u) in basis_u.iter().enumerate() {
166                let i = span_u - knot_u.degree + du;
167                for (dv, value_v) in basis_v.iter().enumerate() {
168                    let j = span_v - knot_v.degree + dv;
169                    let entry = value_u * value_v * weights[i][j];
170                    row[i * count_v + j] = entry;
171                    denominator += entry;
172                }
173            }
174            if denominator.abs() > 0.0 {
175                for value in row.iter_mut() {
176                    *value /= denominator;
177                }
178            }
179        }
180    }
181    let solve_axis = |axis: fn(Vec3) -> f64| {
182        solve_dense(
183            matrix.clone(),
184            samples
185                .iter()
186                .flat_map(|row| row.iter().copied().map(axis))
187                .collect(),
188        )
189    };
190    let x = solve_axis(|point| point.x).or_refuse(KernelStage::Refine, "solve_axis")?;
191    let y = solve_axis(|point| point.y).or_refuse(KernelStage::Refine, "solve_axis")?;
192    let z = solve_axis(|point| point.z).or_refuse(KernelStage::Refine, "solve_axis")?;
193    let mut controls = vec![vec![Vec4::from_point(Vec3::default(), 1.0); count_v]; count_u];
194    for row in 0..count_u {
195        for column in 0..count_v {
196            let index = row * count_v + column;
197            controls[row][column] = Vec4::from_point(
198                Vec3::new(x[index], y[index], z[index]),
199                weights[row][column],
200            );
201        }
202    }
203    Ok(controls)
204}
205
206/// Which branch of [`offset_surface`] built a carrier — and, with it, whether
207/// comparing that carrier against the pointwise offset at the same `(u, v)` is
208/// even the right question.
209///
210/// Recorded rather than inferred, in the pattern `offset/reintersect.rs`
211/// established for its own two lanes: two of this function's three branches
212/// deliberately move the result off the pointwise offset, and a measurement that
213/// did not know which branch ran would report a designed divergence as a fit
214/// error.
215#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
216pub enum OffsetSurfaceLane {
217    /// A rigid control-net shift of an affine (plane-like) carrier. The normal
218    /// is constant, so the shifted net IS the pointwise offset — exact by
219    /// construction, with no fit to measure.
220    Affine,
221    /// A CLOSED-FORM offset built from the source's recognized analytic shape
222    /// rather than sampled: a sphere's concentric mate, whose net is the
223    /// source's scaled about the centre. Exact like `Affine`, but derived from
224    /// geometry the recognizer proved rather than from a constant normal, so
225    /// `offset_surface_measured_sided` measures it instead of declaring it.
226    Analytic,
227    /// Greville collocation of the pointwise offset. `S_fit(u, v)` is meant to
228    /// BE `offset(u, v)`, and the distance between them is the fit error this
229    /// slice measures.
230    #[default]
231    Fit,
232    /// The result was deliberately moved off the pointwise offset: the planar /
233    /// ruled EXTENSION (which grows the carrier past its source rim, so the same
234    /// `(u, v)` names a different point) or the apex-cone PINCH RETRIM (which
235    /// pulls the crossed sample row back to the offset cone's own apex). Both
236    /// are correct and intended; neither is comparable pointwise.
237    Reparameterised,
238}
239
240/// A fitted offset carrier together with what its construction measured about
241/// itself.
242#[derive(Clone, Debug)]
243pub struct MeasuredOffsetSurface {
244    pub surface: NurbsSurface,
245    pub lane: OffsetSurfaceLane,
246    /// `max ‖S_fit(u, v) − offset(u, v)‖` over
247    /// [`crate::measure_surface_fit_against_pointwise_offset`]'s grid, against
248    /// the band it was judged with. When the RULED EXTENSION fired, the offset
249    /// compared against is the offset of the source EXTENDED ALONG ITS RULINGS —
250    /// the surface the carrier is the pointwise offset of — not of `face.surface`.
251    ///
252    /// `None` for [`OffsetSurfaceLane::Reparameterised`] — see that variant.
253    pub fit: Option<MeasuredTolerance>,
254}
255
256/// How far an offset carrier's TRIM grows past the source face at each of
257/// its four parametric sides, in world units.
258///
259/// A carrier is trimmed by the parametric IMAGE of the source loops, so the
260/// growth is realised by reshaping the carrier surface's net while the cloned
261/// pcurves stay put: an affine plane's 2x2 net slides along the plane, a ruled
262/// (linear-v) net stretches each sampled ruling. Both are solved so that the
263/// trim's own uv bounding box moves by exactly these amounts — a trim that
264/// occupies a sub-range of its surface's domain (every booleaned or
265/// blend-trimmed face) grows by the same distance as one that spans it. The
266/// legacy `planar_extension: f64` entry points are the uniform case.
267#[derive(Clone, Copy, Debug, PartialEq)]
268pub struct CarrierExtension {
269    pub u_min: f64,
270    pub u_max: f64,
271    pub v_min: f64,
272    pub v_max: f64,
273}
274
275impl CarrierExtension {
276    pub const NONE: CarrierExtension = CarrierExtension {
277        u_min: 0.0,
278        u_max: 0.0,
279        v_min: 0.0,
280        v_max: 0.0,
281    };
282
283    pub fn uniform(amount: f64) -> CarrierExtension {
284        CarrierExtension {
285            u_min: amount,
286            u_max: amount,
287            v_min: amount,
288            v_max: amount,
289        }
290    }
291
292    pub fn is_active(&self) -> bool {
293        self.u_min > 0.0 || self.u_max > 0.0 || self.v_min > 0.0 || self.v_max > 0.0
294    }
295
296    fn extends_v(&self) -> bool {
297        self.v_min > 0.0 || self.v_max > 0.0
298    }
299}
300
301/// Lane R2 row 9 (2026-10-19): `BREP_OFFSET_EXACT_TRIM_BOX=1`/`on` closes
302/// [`trim_domain_fractions`]' sampled box on each pcurve's true extremes
303/// ([`refine_trim_extreme`]; the default since its census at 9183d81d4);
304/// `=0`/`off` keeps the 25-sample box; `set_exact_trim_box_for_test` forces
305/// it on a test's own thread.
306///
307/// The slide is solved for that box, so a box short of the trim slides the
308/// net unevenly and the cloned pcurves leave their edges.
309const EXACT_TRIM_BOX_DEFAULT: bool = true;
310
311fn exact_trim_box_on() -> bool {
312    switch!(live "BREP_OFFSET_EXACT_TRIM_BOX", EXACT_TRIM_BOX_DEFAULT)
313}
314
315
316/// Golden-section steps [`refine_trim_extreme`] takes: the bracket shrinks to
317/// 0.618^64 ≈ 4e-14 of two sample spacings, past where the extreme's value
318/// can still move (it is quadratic in the parameter there).
319const TRIM_EXTREME_STEPS: usize = 64;
320
321/// The extreme of `sign · uv[axis]` (`axis` 0 is u, 1 is v) over one pcurve,
322/// returned as the coordinate itself. Bracketed by the pcurve's best sample
323/// and that sample's two neighbours, and closed by a golden-section search of
324/// a fixed [`TRIM_EXTREME_STEPS`] steps; never worse than the best sample.
325/// On an arc the bracketed piece is unimodal, so this is its true extreme.
326/// A line's extremes are its ends, which are samples, so the caller does not
327/// search one (an interior read of a straight trim can land an ulp past its
328/// end).
329fn refine_trim_extreme(
330    pcurve: &NurbsCurve,
331    samples: &[(f64, Vec3)],
332    axis: usize,
333    sign: f64,
334) -> Result<f64, KernelRefusal> {
335    let read = |uv: Vec3| sign * if axis == 0 { uv.x } else { uv.y };
336    let at = |parameter: f64| -> Result<f64, KernelRefusal> {
337        Ok(read(pcurve.evaluate(parameter).or_refuse(KernelStage::Refine, "evaluate")?))
338    };
339    let best = (0..samples.len())
340        .max_by(|&a, &b| read(samples[a].1).total_cmp(&read(samples[b].1)))
341        .expect("a pcurve has samples");
342    let mut found = read(samples[best].1);
343    let (mut a, mut b) = (samples[best.saturating_sub(1)].0, samples[(best + 1).min(samples.len() - 1)].0);
344    const GOLDEN: f64 = 0.618_033_988_749_894_9;
345    let mut c = b - GOLDEN * (b - a);
346    let mut d = a + GOLDEN * (b - a);
347    let (mut fc, mut fd) = (at(c)?, at(d)?);
348    for _ in 0..TRIM_EXTREME_STEPS {
349        found = found.max(fc).max(fd);
350        if fc >= fd {
351            (b, d, fd) = (d, c, fc);
352            c = b - GOLDEN * (b - a);
353            fc = at(c)?;
354        } else {
355            (a, c, fc) = (c, d, fd);
356            d = a + GOLDEN * (b - a);
357            fd = at(d)?;
358        }
359    }
360    Ok(sign * found.max(fc).max(fd))
361}
362
363/// The trim's uv bounding box as FRACTIONS of the surface domain:
364/// `([ta_u, tb_u], [ta_v, tb_v])`, each in `[0, 1]`. Sampled over every
365/// coedge pcurve of every loop, and closed on the pcurves' true extremes
366/// under `BREP_OFFSET_EXACT_TRIM_BOX`.
367fn trim_domain_fractions(
368    face: &FaceRecord,
369    [u0, u1]: [f64; 2],
370    [v0, v1]: [f64; 2],
371) -> Result<([f64; 2], [f64; 2]), KernelRefusal> {
372    let mut low = Vec2 {
373        x: f64::INFINITY,
374        y: f64::INFINITY,
375    };
376    let mut high = Vec2 {
377        x: f64::NEG_INFINITY,
378        y: f64::NEG_INFINITY,
379    };
380    for coedge in face
381        .loops
382        .iter()
383        .flat_map(|loop_record| &loop_record.coedges)
384    {
385        let [p0, p1] = coedge.pcurve.domain().or_refuse(KernelStage::Refine, "domain")?;
386        let mut samples = Vec::with_capacity(25);
387        for sample in 0..=24 {
388            let parameter = p0 + (p1 - p0) * sample as f64 / 24.0;
389            let uv = coedge
390                .pcurve
391                .evaluate(parameter).or_refuse(KernelStage::Refine, "evaluate")?;
392            low.x = low.x.min(uv.x);
393            low.y = low.y.min(uv.y);
394            high.x = high.x.max(uv.x);
395            high.y = high.y.max(uv.y);
396            samples.push((parameter, uv));
397        }
398        if coedge.pcurve.degree > 1 && exact_trim_box_on() {
399            low.x = low.x.min(refine_trim_extreme(&coedge.pcurve, &samples, 0, -1.0)?);
400            high.x = high.x.max(refine_trim_extreme(&coedge.pcurve, &samples, 0, 1.0)?);
401            low.y = low.y.min(refine_trim_extreme(&coedge.pcurve, &samples, 1, -1.0)?);
402            high.y = high.y.max(refine_trim_extreme(&coedge.pcurve, &samples, 1, 1.0)?);
403        }
404    }
405    let fraction = |value: f64, start: f64, end: f64| {
406        let span = end - start;
407        if span.abs() <= f64::EPSILON || !value.is_finite() {
408            return None;
409        }
410        Some(((value - start) / span).clamp(0.0, 1.0))
411    };
412    let u = match (fraction(low.x, u0, u1), fraction(high.x, u0, u1)) {
413        (Some(a), Some(b)) if b - a > 1e-6 => [a, b],
414        _ => [0.0, 1.0],
415    };
416    let v = match (fraction(low.y, v0, v1), fraction(high.y, v0, v1)) {
417        (Some(a), Some(b)) if b - a > 1e-6 => [a, b],
418        _ => [0.0, 1.0],
419    };
420    Ok((u, v))
421}
422
423/// How far the surface's domain ENDS must move (`(back, forward)`: the start
424/// end backwards, the far end forwards, world units) so that a trim occupying
425/// the domain fractions `[ta, tb]` grows by exactly `grow_min` at its start and
426/// `grow_max` at its end when its pcurves are left untouched. A point at
427/// fraction `t` of a linearly re-mapped domain moves by
428/// `-(1 - t)·back + t·forward`; solving that at `ta` and `tb` gives the pair
429/// below. It reduces to `(grow_min, grow_max)` for a full-domain trim, and
430/// never shrinks either end (both results are non-negative for non-negative
431/// inputs).
432fn domain_end_moves(grow_min: f64, grow_max: f64, [ta, tb]: [f64; 2]) -> (f64, f64) {
433    let span = tb - ta;
434    if span <= 1e-6 {
435        return (grow_min, grow_max);
436    }
437    let rate = (grow_min + grow_max) / span;
438    let back = grow_min + ta * rate;
439    let forward = (1.0 - ta) * rate - grow_min;
440    (back.max(0.0), forward.max(0.0))
441}
442
443/// Construct the same fitted offset carrier surface as the reference shell
444/// implementation. Positive distance follows its convention and moves
445/// opposite the face's outward normal.
446pub fn offset_surface(
447    face: &FaceRecord,
448    distance: f64,
449    planar_extension: f64,
450) -> Result<NurbsSurface, KernelRefusal> {
451    offset_surface_with_lane(face, distance, &CarrierExtension::uniform(planar_extension))
452        .map(|(surface, _)| surface)
453}
454
455/// [`offset_surface`], plus the deviation MEASURED between the carrier it built
456/// and the pointwise offset that carrier approximates.
457///
458/// The surface is bit-identical to [`offset_surface`]'s — this calls the same
459/// body and adds a read-only pass afterwards. Nothing here can change the
460/// carrier: measuring the deviation AFTER the carrier is built records what
461/// happened; the size-derived band stays the floor and the bar it is judged
462/// against. A measurement never widens a band.
463pub fn offset_surface_measured(
464    face: &FaceRecord,
465    distance: f64,
466    planar_extension: f64,
467    band: f64,
468) -> Result<MeasuredOffsetSurface, KernelRefusal> {
469    offset_surface_measured_sided(
470        face,
471        distance,
472        &CarrierExtension::uniform(planar_extension),
473        band,
474    )
475}
476
477/// [`offset_surface_measured`] with a per-side [`CarrierExtension`].
478pub fn offset_surface_measured_sided(
479    face: &FaceRecord,
480    distance: f64,
481    extension: &CarrierExtension,
482    band: f64,
483) -> Result<MeasuredOffsetSurface, KernelRefusal> {
484    offset_surface_measured_sided_mode(face, distance, extension, band,
485        std::env::var("BREP_OFFSET_SPHERE_READING").map_or(true, |v| v != "0" && v != "off"))
486}
487
488fn offset_surface_measured_sided_mode(
489    face: &FaceRecord, distance: f64, extension: &CarrierExtension, band: f64, reuse: bool,
490) -> Result<MeasuredOffsetSurface, KernelRefusal> {
491    let mut sphere_reading = None;
492    let (surface, lane, extended) = offset_surface_constructed(face, distance, extension, &mut sphere_reading)?;
493    let fit = match lane {
494        OffsetSurfaceLane::Affine => Some(MeasuredTolerance::exact(band)),
495        // `Analytic` is exact by construction and already verified against the
496        // pointwise offset before it was returned — but it is measured here
497        // like a fit anyway, so the record carries the number instead of the
498        // claim. It reads ~1e-13 where `Fit` reads ~1e-6.
499        OffsetSurfaceLane::Analytic if reuse && sphere_reading.is_some() => {
500            // The construction already read this exact source/result pair on
501            // the same grid. Only the report band differs; it is not a grid
502            // input. Retain the measured deviation, never substitute zero.
503            if crate::profile::profile_enabled() {
504                profile_line!("offset.sphere_reading.profile reused=1");
505            }
506            Some(MeasuredTolerance::new(sphere_reading.unwrap(), band))
507        }
508        OffsetSurfaceLane::Analytic | OffsetSurfaceLane::Fit => {
509            Some(measure_surface_fit_against_pointwise_offset(
510                extended.as_ref().unwrap_or(&face.surface),
511                face.same_sense,
512                &surface,
513                distance,
514                band,
515            )?)
516        }
517        OffsetSurfaceLane::Reparameterised => None,
518    };
519    Ok(MeasuredOffsetSurface { surface, lane, fit })
520}
521
522/// The EXACT concentric offset of a sphere, or `None` for every other carrier.
523///
524/// A sphere's normal is radial, so its pointwise offset at distance `d` is the
525/// concentric sphere of radius `R' = R ± d`. A NURBS map is affine in its
526/// control points — `S = Σ N_i w_i P_i / Σ N_i w_i` — so scaling every control
527/// point about the centre by `R'/R` scales the whole image by that factor about
528/// that point, and the result IS that sphere to rounding. Knots, degrees and
529/// weights are untouched, so the same `(u, v)` still names the corresponding
530/// point: the trims `offset_face_carrier_impl` clones onto this carrier
531/// verbatim land exactly where they did on the source.
532///
533/// The Greville fit below reaches the same surface only to its collocation
534/// error. On `offset-shell-box-bore-dished-face` that error put the dished
535/// face's volume contribution 4.69e-3 away from the closed form — the larger
536/// share of the case's 3.9435e-3 deficit.
537///
538/// Everything here is COMPUTED, never claimed. The offset radius comes from the
539/// same [`OffsetEvaluator`] the fit lane samples, so this lane cannot invent a
540/// sign convention; the guards below reject a centre the recognizer got wrong
541/// or a sphere turned inside out by an inward offset deeper than its radius;
542/// and the finished net is checked against that evaluator on a grid before it
543/// is returned. Every refusal falls through to the fit, which is what built
544/// these carriers before.
545///
546/// A sphere the recognizer does not recognize — a FITTED patch that merely
547/// looks spherical — stays on the fit lane, unchanged. This lane widens nothing
548/// on its own: cylinders, cones and tori want a scaling about an axis or a
549/// spine rather than a point, which is a different construction.
550fn sphere_offset_surface(face: &FaceRecord, distance: f64, reading: &mut Option<f64>) -> Result<Option<NurbsSurface>, KernelRefusal> {
551    // Escape hatch, the same shape as `BREP_NO_SPHERE_CHARTS`: it makes the
552    // closed form and the collocation fit two cells of one binary, so a
553    // measurement can separate this lane's effect from everything else.
554    if std::env::var_os("BREP_NO_ANALYTIC_OFFSET").is_some() {
555        return Ok(None);
556    }
557    let source = &face.surface;
558    let Some((centre, radius)) = source.analytic().and_then(|shape| shape.sphere_geometry()) else {
559        return Ok(None);
560    };
561    let ([u0, u1], [v0, v1]) = domains(source)?;
562    // `offset_surface`'s positive distance moves OPPOSITE the face normal while
563    // the evaluator's moves ALONG it; the negation is the fit's own, reused
564    // here rather than re-derived (audit §4.1's hand negations get no new one).
565    // A normal this lane cannot read is a DECLINE, not an error: the fit below
566    // samples the same evaluator across the whole Greville grid and has its own
567    // singular-row recovery, so anything that offset before must still offset.
568    let Ok(sample) = OffsetEvaluator::new(
569        "offset_surface",
570        source,
571        OffsetNormal::FaceStable {
572            same_sense: face.same_sense,
573        },
574    )
575    .at((u0 + u1) / 2.0, (v0 + v1) / 2.0, -distance)
576    else {
577        return Ok(None);
578    };
579    let source_radial = sample.source.sub(centre);
580    let offset_radial = sample.point.sub(centre);
581    let offset_radius = offset_radial.length();
582    let scale = radius.max(1.0);
583    if radius <= 1e-12
584        || offset_radius <= 1e-9 * scale
585        // Offset through the centre and out the far side: the concentric mate
586        // is a sphere turned inside out, not this construction's answer.
587        || offset_radial.dot(source_radial) <= 0.0
588        // The move must be radial and exactly |d| long. A recognized centre
589        // that is not the real one fails here before it can scale anything.
590        || ((offset_radius - radius).abs() - distance.abs()).abs() > 1e-9 * scale
591    {
592        return Ok(None);
593    }
594    let factor = offset_radius / radius;
595    // A zero-weight control has no Euclidean point to scale. The fit lane never
596    // asks one for a position, so declining keeps this lane strictly additive.
597    let Ok(controls) = source
598        .control_points
599        .iter()
600        .map(|row| {
601            row.iter()
602                .map(|control| {
603                    Ok(Vec4::from_point(
604                        centre.add(control.point().or_refuse(KernelStage::Refine, "point")?.sub(centre).scale(factor)),
605                        control.w,
606                    ))
607                })
608                .collect::<Result<Vec<_>, KernelRefusal>>()
609        })
610        .collect::<Result<Vec<Vec<_>>, KernelRefusal>>()
611    else {
612        return Ok(None);
613    };
614    let Ok(surface) = NurbsSurface::new(
615        source.degree_u,
616        source.degree_v,
617        source.knots_u.clone(),
618        source.knots_v.clone(),
619        controls,
620    ) else {
621        return Ok(None);
622    };
623    // The claim, tested. A net that does not land on the pointwise offset
624    // everywhere is not this lane's surface, whatever it was recognized as —
625    // and a measurement that could not be taken is not a pass.
626    match crate::offset_measure::sphere_fit_reading(
627        source,
628        face.same_sense,
629        &surface,
630        distance,
631        1e-9 * scale,
632    ) {
633        Ok(measured) if measured.within_band() => {
634            *reading = Some(measured.deviation());
635            Ok(Some(surface))
636        },
637        _ => Ok(None),
638    }
639}
640
641/// The EXACT offset of a surface of REVOLUTION whose generatrix is ONE CIRCLE
642/// ARC — a torus of any kind (ring, horn, spindle), over a full or a partial
643/// sweep — or `None` for every other carrier.
644///
645/// Why it is exact. A revolution's normal lies in the meridian plane and equals
646/// the generatrix's own in-plane normal there, so the offset surface IS the
647/// revolution of the offset generatrix, and the offset of a circle arc of
648/// radius `r` about `c` is the concentric arc of radius `r ± d`. On the net:
649/// row `i` of a revolution's control net is an AFFINE image `A_i` of the
650/// generatrix's controls — a rotation for the on-circle rows, but a rotation
651/// STRETCHED radially by `1/cos` of the half-span for the mid rows, which is
652/// not a similarity. Affine maps commute with scaling about a point, so scaling
653/// row `i` about `C_i = A_i(c)` by `(r ± d)/r` is exactly `A_i` applied to the
654/// offset circle's net; that commutation is the only reason the stretched rows
655/// come out right. `C_i` is found without reading `A_i` at all: `c` is an affine
656/// combination of three generatrix controls, and the same combination of row
657/// `i`'s controls is `A_i(c)`. Knots, degrees and weights are untouched, so the
658/// same `(u, v)` still names the corresponding point and cloned trims land
659/// where they did, as on [`sphere_offset_surface`].
660///
661/// Everything is computed and then tested, never claimed. The generatrix is
662/// row 0 of the net, and it must lie on one circle to rounding. The sign and
663/// size of the move come from the same [`OffsetEvaluator`] the fit samples,
664/// read at a generatrix point against the circle's centre: the move must be
665/// radial and exactly `|d|` long, and an offset through the centre (`r − d ≤
666/// 0`, where the offset circle turns inside out) declines. The finished net
667/// must land on the pointwise offset everywhere on the measurement grid, to
668/// `1e-9` of the model's scale. Every decline falls through to the fit.
669///
670/// Measured on the partial lemon (a 106° arc of radius 5 centred 3 off the
671/// axis) shelled inward by 1: the fitted carrier sat 1.4e-5 off the pointwise
672/// offset and its shell 3.6e-7 short of the Pappus form in volume.
673fn revolved_arc_offset_surface(
674    face: &FaceRecord,
675    distance: f64,
676) -> Result<Option<NurbsSurface>, KernelRefusal> {
677    if std::env::var_os("BREP_NO_ANALYTIC_OFFSET").is_some() {
678        return Ok(None);
679    }
680    let source = &face.surface;
681    match source.analytic() {
682        Some(crate::AnalyticSurface::Revolution { .. } | crate::AnalyticSurface::Torus { .. }) => {}
683        _ => return Ok(None),
684    }
685    let rows = &source.control_points;
686    let Some(first_row) = rows.first() else {
687        return Ok(None);
688    };
689    if rows.iter().flatten().any(|control| !(control.w > 0.0)) {
690        return Ok(None);
691    }
692    let Ok(generatrix) = NurbsCurve::new(source.degree_v, source.knots_v.clone(), first_row.clone())
693    else {
694        return Ok(None);
695    };
696    let [t0, t1] = generatrix.domain().or_refuse(KernelStage::Refine, "domain")?;
697    let at = |fraction: f64| generatrix.evaluate(t0 + (t1 - t0) * fraction);
698    // The circle through three generatrix points, then every sample held to it.
699    let (a, b, e) = (at(0.0).or_refuse(KernelStage::Refine, "at")?, at(1.0 / 3.0).or_refuse(KernelStage::Refine, "at")?, at(2.0 / 3.0).or_refuse(KernelStage::Refine, "at")?);
700    let (ab, ae) = (b.sub(a), e.sub(a));
701    let normal = ab.cross(ae);
702    let normal_length_squared = normal.dot(normal);
703    let Some(centre) = crate::circumcenter(a, b, e) else { return Ok(None); };
704    let radius = a.sub(centre).length();
705    let scale = (centre.length() + radius).max(1.0);
706    let band = 1e-9 * scale;
707    let unit_normal = normal.scale(1.0 / normal_length_squared.sqrt());
708    let samples = 16 * generatrix.control_points.len();
709    for index in 0..=samples {
710        let point = at(index as f64 / samples as f64).or_refuse(KernelStage::Refine, "at")?;
711        let radial = point.sub(centre);
712        if (radial.length() - radius).abs() > band || radial.dot(unit_normal).abs() > band {
713            return Ok(None);
714        }
715    }
716    // `centre` as an affine combination of three generatrix controls, the best
717    // conditioned pair against the first.
718    let controls = first_row
719        .iter()
720        .map(|control| control.point().or_refuse(KernelStage::Refine, "point"))
721        .collect::<Result<Vec<_>, KernelRefusal>>()?;
722    let origin = controls[0];
723    let mut best = (0usize, 0usize, 0.0f64);
724    for first in 1..controls.len() {
725        for second in first + 1..controls.len() {
726            let area = controls[first]
727                .sub(origin)
728                .cross(controls[second].sub(origin))
729                .length();
730            if area > best.2 {
731                best = (first, second, area);
732            }
733        }
734    }
735    let (first, second, area) = best;
736    if area <= 1e-9 * scale * scale {
737        return Ok(None);
738    }
739    let (along_first, along_second) = (controls[first].sub(origin), controls[second].sub(origin));
740    let offset_from_origin = centre.sub(origin);
741    let (g11, g12, g22) = (
742        along_first.dot(along_first),
743        along_first.dot(along_second),
744        along_second.dot(along_second),
745    );
746    let determinant = g11 * g22 - g12 * g12;
747    let (r1, r2) = (along_first.dot(offset_from_origin), along_second.dot(offset_from_origin));
748    let s = (r1 * g22 - r2 * g12) / determinant;
749    let t = (r2 * g11 - r1 * g12) / determinant;
750    if origin
751        .add(along_first.scale(s))
752        .add(along_second.scale(t))
753        .sub(centre)
754        .length()
755        > band
756    {
757        return Ok(None);
758    }
759    // The move, read off the shared evaluator on the generatrix row. The
760    // negation is the fit's own, as in `sphere_offset_surface`.
761    let ([u0, _], [v0, v1]) = domains(source)?;
762    let Ok(sample) = OffsetEvaluator::new(
763        "offset_surface",
764        source,
765        OffsetNormal::FaceStable {
766            same_sense: face.same_sense,
767        },
768    )
769    .at(u0, (v0 + v1) / 2.0, -distance)
770    else {
771        return Ok(None);
772    };
773    let source_radial = sample.source.sub(centre);
774    let offset_radial = sample.point.sub(centre);
775    let offset_radius = offset_radial.length();
776    if (source_radial.length() - radius).abs() > band
777        || offset_radius <= band
778        || offset_radial.dot(source_radial) <= 0.0
779        || ((offset_radius - radius).abs() - distance.abs()).abs() > band
780    {
781        return Ok(None);
782    }
783    let factor = offset_radius / radius;
784    let Ok(offset_rows) = rows
785        .iter()
786        .map(|row| {
787            let points = row.iter().map(|control| control.point().or_refuse(KernelStage::Refine, "point")).collect::<Result<Vec<_>, KernelRefusal>>()?;
788            let row_origin = points[0];
789            let row_centre = row_origin
790                .add(points[first].sub(row_origin).scale(s))
791                .add(points[second].sub(row_origin).scale(t));
792            Ok(points
793                .iter()
794                .zip(row)
795                .map(|(point, control)| {
796                    Vec4::from_point(row_centre.add(point.sub(row_centre).scale(factor)), control.w)
797                })
798                .collect::<Vec<_>>())
799        })
800        .collect::<Result<Vec<Vec<_>>, KernelRefusal>>()
801    else {
802        return Ok(None);
803    };
804    let Ok(surface) = NurbsSurface::new(
805        source.degree_u,
806        source.degree_v,
807        source.knots_u.clone(),
808        source.knots_v.clone(),
809        offset_rows,
810    ) else {
811        return Ok(None);
812    };
813    match measure_surface_fit_against_pointwise_offset(
814        source,
815        face.same_sense,
816        &surface,
817        distance,
818        band,
819    ) {
820        Ok(measured) if measured.within_band() => Ok(Some(surface)),
821        _ => Ok(None),
822    }
823}
824
825/// Lane O row 35's switch, default on since 2026-11-02 (batch 19; `0`/`off`
826/// restores the fit): a circular cylinder's offset carrier is built exactly
827/// ([`cylinder_offset_surface`]). The carve's certificate of the same factor
828/// is row 35a's own switch (`carriers.rs`).
829pub(crate) fn exact_cylinder_offsets_on() -> bool {
830    switch!("BREP_OFFSET_EXACT_CYLINDER", true)
831}
832
833
834/// The axis, radius and offset scale factor of a recognized CIRCULAR CYLINDER
835/// offset by `distance` (`offset_surface`'s sign: positive opposite the face
836/// normal), or `None` for every other carrier and for an offset through the
837/// axis. The move is read off the shared [`OffsetEvaluator`] at the middle of
838/// the domain, as in [`sphere_offset_surface`]: it must be radial and exactly
839/// `|d|` long, so this cannot invent a sign convention.
840pub(crate) fn cylinder_offset_factor(
841    surface: &NurbsSurface,
842    same_sense: bool,
843    distance: f64,
844) -> Result<Option<(Vec3, Vec3, f64, f64)>, KernelRefusal> {
845    if std::env::var_os("BREP_NO_ANALYTIC_OFFSET").is_some() {
846        return Ok(None);
847    }
848    let Some(crate::AnalyticSurface::RuledRevolution { frame, rho0, rho1, height }) = surface.analytic() else {
849        return Ok(None);
850    };
851    let radius = 0.5 * (rho0 + rho1);
852    if !(radius > 0.0) || (rho0 - rho1).abs() > 1e-9 * radius.max(1.0) {
853        return Ok(None);
854    }
855    let Ok(axis) = frame.axis.normalized() else {
856        return Ok(None);
857    };
858    let origin = frame.origin;
859    let band = 1e-9 * (origin.length() + radius + height.abs()).max(1.0);
860    let perpendicular = |point: Vec3| {
861        let along = point.sub(origin);
862        along.sub(axis.scale(along.dot(axis)))
863    };
864    let [u0, u1] = surface.domain_u().or_refuse(KernelStage::Refine, "domain_u")?;
865    let [v0, v1] = surface.domain_v().or_refuse(KernelStage::Refine, "domain_v")?;
866    let Ok(sample) = OffsetEvaluator::new("offset_surface", surface, OffsetNormal::FaceStable { same_sense })
867        .at((u0 + u1) * 0.5, (v0 + v1) * 0.5, -distance)
868    else {
869        return Ok(None);
870    };
871    let (source_radial, offset_radial) = (perpendicular(sample.source), perpendicular(sample.point));
872    let offset_radius = offset_radial.length();
873    if (source_radial.length() - radius).abs() > band
874        || offset_radius <= band
875        || offset_radial.dot(source_radial) <= 0.0
876        || ((offset_radius - radius).abs() - distance.abs()).abs() > band
877        || sample.point.sub(sample.source).dot(axis).abs() > band
878    {
879        return Ok(None);
880    }
881    Ok(Some((origin, axis, radius, offset_radius / radius)))
882}
883
884/// Lane O row 35: the EXACT offset of a circular cylinder, or `None`.
885///
886/// A cylinder's normal is radial and constant along each ruling, so its
887/// pointwise offset is the coaxial cylinder of radius `R ± d`. Every column
888/// of the net (a rational circle) is an affine image of one polygon in a
889/// plane normal to the axis, and scaling about the axis by `(R ± d)/R` maps
890/// it to the same image of the offset circle; the rulings stay parallel to
891/// the axis. Knots, degrees and weights are untouched, so the same `(u, v)`
892/// names the corresponding point and cloned trims land where they did. The
893/// finished net must land on the pointwise offset of `reference` everywhere
894/// on the measurement grid, to `1e-9` of the model's scale; every decline
895/// falls through to the fit.
896fn cylinder_offset_surface(
897    reference: &NurbsSurface,
898    same_sense: bool,
899    distance: f64,
900) -> Result<Option<NurbsSurface>, KernelRefusal> {
901    let Some((origin, axis, radius, factor)) = cylinder_offset_factor(reference, same_sense, distance)? else {
902        return Ok(None);
903    };
904    if reference.control_points.iter().flatten().any(|control| !(control.w > 0.0)) {
905        return Ok(None);
906    }
907    let Ok(rows) = reference
908        .control_points
909        .iter()
910        .map(|row| {
911            row.iter()
912                .map(|control| {
913                    let point = control.point()?;
914                    let foot = origin.add(axis.scale(point.sub(origin).dot(axis)));
915                    Ok(Vec4::from_point(foot.add(point.sub(foot).scale(factor)), control.w))
916                })
917                .collect::<Result<Vec<_>, String>>()
918        })
919        .collect::<Result<Vec<Vec<_>>, String>>()
920    else {
921        return Ok(None);
922    };
923    let Ok(surface) = NurbsSurface::new(
924        reference.degree_u,
925        reference.degree_v,
926        reference.knots_u.clone(),
927        reference.knots_v.clone(),
928        rows,
929    ) else {
930        return Ok(None);
931    };
932    // The construction is exact once the recognizer has proved the cylinder
933    // and the evaluator has fixed the move, so this reads the finished net at
934    // 5 × 3 stations, both domain ends included, against the pointwise offset:
935    // a check of the build, not a measurement. `offset_surface_measured_*`
936    // still takes its full grid reading of this carrier, as of any fit.
937    let band = 1e-9 * (origin.length() + radius * factor.max(1.0)).max(1.0);
938    let evaluator = OffsetEvaluator::new("offset_surface", reference, OffsetNormal::FaceStable { same_sense });
939    let [u0, u1] = reference.domain_u().or_refuse(KernelStage::Refine, "domain_u")?;
940    let [v0, v1] = reference.domain_v().or_refuse(KernelStage::Refine, "domain_v")?;
941    for i in 0..=4 {
942        for j in 0..=2 {
943            let (u, v) = (u0 + (u1 - u0) * i as f64 / 4.0, v0 + (v1 - v0) * j as f64 / 2.0);
944            let (Ok(want), Ok(got)) = (evaluator.at(u, v, -distance), surface.evaluate(u, v)) else {
945                return Ok(None);
946            };
947            if !(want.point.sub(got).length() <= band) {
948                return Ok(None);
949            }
950        }
951    }
952    Ok(Some(surface))
953}
954
955pub(crate) fn offset_surface_with_lane(
956    face: &FaceRecord,
957    distance: f64,
958    extension: &CarrierExtension,
959) -> Result<(NurbsSurface, OffsetSurfaceLane), KernelRefusal> {
960    offset_surface_constructed(face, distance, extension, &mut None).map(|(surface, lane, _)| (surface, lane))
961}
962
963/// [`offset_surface_with_lane`], plus the surface the carrier is the pointwise
964/// offset OF when that is not the face's own: the source extended along its
965/// rulings, for a ruled extension (see the RULED EXTENSION block).
966pub(crate) fn transpose_offset_surface(surface: &NurbsSurface) -> Result<NurbsSurface, KernelRefusal> {
967    let controls = (0..surface.control_points[0].len())
968        .map(|column| surface.control_points.iter().map(|row| row[column]).collect())
969        .collect();
970    NurbsSurface::new(surface.degree_v, surface.degree_u, surface.knots_v.clone(),
971        surface.knots_u.clone(), controls).or_refuse(KernelStage::Refine, "offset_transpose")
972}
973
974fn offset_surface_constructed(
975    face: &FaceRecord,
976    distance: f64,
977    extension: &CarrierExtension,
978    sphere_reading: &mut Option<f64>,
979) -> Result<(NurbsSurface, OffsetSurfaceLane, Option<NurbsSurface>), KernelRefusal> {
980    let source = &face.surface;
981    let extension_active = extension.is_active();
982    // Imported cones can put the ruling in u. Normalize that chart for the
983    // existing exact ruling/pinch construction, then restore the caller's UV.
984    // Transposition reverses Su x Sv, so the face sense reverses with it.
985    // The recognizer must prove a ruled revolution; arbitrary lofts keep
986    // their existing evaluator and fit semantics.
987    let normalize_ruling = std::env::var("BREP_OFFSET_FIT_RESIDUAL").as_deref() != Ok("0")
988        && source.degree_u == 1 && source.control_points.len() == 2;
989    let normalize_sphere = std::env::var("BREP_OFFSET_FOLD_CHART").as_deref() != Ok("0")
990        && source.degree_u == 2 && source.degree_v == 2 && source.analytic().is_none();
991    if normalize_ruling || normalize_sphere {
992        let transposed = transpose_offset_surface(source)?;
993        let ruled = normalize_ruling && matches!(transposed.analytic(), Some(crate::AnalyticSurface::RuledRevolution { .. }));
994        let spherical = normalize_sphere && transposed.analytic().and_then(|shape| shape.sphere_geometry()).is_some();
995        if ruled || spherical {
996            let mut turned = face.clone();
997            turned.surface = transposed;
998            turned.same_sense = !turned.same_sense;
999            for coedge in turned.loops.iter_mut().flat_map(|lp| &mut lp.coedges) {
1000                let controls = coedge.pcurve.control_points.iter().map(|point| {
1001                    let mut swapped = *point;
1002                    std::mem::swap(&mut swapped.x, &mut swapped.y);
1003                    swapped
1004                }).collect();
1005                coedge.pcurve = NurbsCurve::new(coedge.pcurve.degree,
1006                    coedge.pcurve.knots.clone(), controls).or_refuse(KernelStage::Refine, "offset_transpose_pcurve")?;
1007            }
1008            let turned_extension = CarrierExtension {
1009                u_min: extension.v_min, u_max: extension.v_max,
1010                v_min: extension.u_min, v_max: extension.u_max,
1011            };
1012            let (surface, lane, extended) = offset_surface_constructed(&turned, distance, &turned_extension, &mut None)?;
1013            return Ok((transpose_offset_surface(&surface)?, lane,
1014                extended.as_ref().map(transpose_offset_surface).transpose()?));
1015        }
1016    }
1017    if source.is_affine().or_refuse(KernelStage::Refine, "is_affine")? {
1018        let ([u0, u1], [v0, v1]) = domains(source)?;
1019        let normal = stable_face_normal(face, (u0 + u1) / 2.0, (v0 + v1) / 2.0)?;
1020        let shift = normal.scale(-distance);
1021        let mut points = source
1022            .control_points
1023            .iter()
1024            .map(|row| {
1025                row.iter()
1026                    .map(|control| Ok(control.point().or_refuse(KernelStage::Refine, "point")?.add(shift)))
1027                    .collect::<Result<Vec<_>, KernelRefusal>>()
1028            })
1029            .collect::<Result<Vec<_>, KernelRefusal>>()?;
1030        if extension_active {
1031            let p00 = points[0][0];
1032            let p01 = points[0][1];
1033            let p10 = points[1][0];
1034            let direction_u = p10.sub(p00).normalized().or_refuse(KernelStage::Refine, "normalized")?;
1035            let direction_v = p01.sub(p00).normalized().or_refuse(KernelStage::Refine, "normalized")?;
1036            // The net slides while the cloned pcurves stay put, so a trim
1037            // that spans only part of the domain would grow by less than
1038            // asked (a 20-wide face trimmed to [0,16] by a blend moved its
1039            // x=16 edge 0.6 for a 1.0 pad). Solve the slide for the TRIM's
1040            // own bounding box instead.
1041            let (trim_u, trim_v) = trim_domain_fractions(face, [u0, u1], [v0, v1])?;
1042            let (back_u, forward_u) = domain_end_moves(extension.u_min, extension.u_max, trim_u);
1043            let (back_v, forward_v) = domain_end_moves(extension.v_min, extension.v_max, trim_v);
1044            points[0][0] = p00
1045                .sub(direction_u.scale(back_u))
1046                .sub(direction_v.scale(back_v));
1047            points[0][1] = p01
1048                .sub(direction_u.scale(back_u))
1049                .add(direction_v.scale(forward_v));
1050            points[1][0] = p10
1051                .add(direction_u.scale(forward_u))
1052                .sub(direction_v.scale(back_v));
1053            points[1][1] = points[1][1]
1054                .add(direction_u.scale(forward_u))
1055                .add(direction_v.scale(forward_v));
1056        }
1057        let controls = points
1058            .into_iter()
1059            .enumerate()
1060            .map(|(row, points)| {
1061                points
1062                    .into_iter()
1063                    .enumerate()
1064                    .map(|(column, point)| {
1065                        Vec4::from_point(point, source.control_points[row][column].w)
1066                    })
1067                    .collect()
1068            })
1069            .collect();
1070        // The extension slides the control net along the plane, so the same
1071        // `(u, v)` no longer names the pointwise offset of the same source
1072        // point; without it the shift is rigid and exact.
1073        let lane = if extension_active {
1074            OffsetSurfaceLane::Reparameterised
1075        } else {
1076            OffsetSurfaceLane::Affine
1077        };
1078        return Ok((
1079            NurbsSurface::new(
1080                source.degree_u,
1081                source.degree_v,
1082                source.knots_u.clone(),
1083                source.knots_v.clone(),
1084                controls,
1085            ).or_refuse(KernelStage::Refine, "new")?,
1086            lane,
1087            None,
1088        ));
1089    }
1090
1091    // A recognized SPHERE has a closed-form concentric offset that needs no
1092    // collocation at all. Taken before the fit so the exact answer wins
1093    // wherever it exists; `sphere_offset_surface` declines on everything else,
1094    // and on a sphere whose construction it cannot verify.
1095    //
1096    // The extension cannot reach a sphere either way: both blocks below sit
1097    // under `parameters_v.len() == 2`, and a sphere's meridian is a degree-2
1098    // net of five controls. Taking this lane under an active extension keeps
1099    // exactly the behaviour the fit had — no growth — and only sharpens it.
1100    if let Some(surface) = sphere_offset_surface(face, distance, sphere_reading)? {
1101        return Ok((surface, OffsetSurfaceLane::Analytic, None));
1102    }
1103    // The same holds for a revolved ARC: its generatrix has three or more
1104    // controls, so no extension below could reach it.
1105    if let Some(surface) = revolved_arc_offset_surface(face, distance)? {
1106        return Ok((surface, OffsetSurfaceLane::Analytic, None));
1107    }
1108
1109    let knot_u = KnotVector::new(source.knots_u.clone(), source.degree_u).or_refuse(KernelStage::Refine, "new")?;
1110    let knot_v = KnotVector::new(source.knots_v.clone(), source.degree_v).or_refuse(KernelStage::Refine, "new")?;
1111    let parameters_u = greville_parameters(&knot_u);
1112    let parameters_v = greville_parameters(&knot_v);
1113    // The Greville sample grid IS a pointwise offset evaluation — this fit is
1114    // the shared evaluator's consumer, not its peer. `offset_surface`'s
1115    // positive distance moves OPPOSITE the face normal while the evaluator's
1116    // moves ALONG it, so the negation happens once, here, with a name on it
1117    // (audit §4.1's four hand negations get no fifth).
1118    let evaluator = OffsetEvaluator::new(
1119        "offset_surface",
1120        source,
1121        OffsetNormal::FaceStable {
1122            same_sense: face.same_sense,
1123        },
1124    );
1125    let mut samples = Vec::new();
1126    for &u in &parameters_u {
1127        let mut row = Vec::new();
1128        for &v in &parameters_v {
1129            row.push(evaluator.at(u, v, -distance).or_refuse(KernelStage::Refine, "at")?.point);
1130        }
1131        samples.push(row);
1132    }
1133    // APEX-CONE PINCH RETRIM: offsetting an apex cone INWARD moves each
1134    // ruling past the axis — the sampled far row becomes a ring on the far
1135    // side (radius d·cos half-angle, mirrored through the axis) and the
1136    // offset surface self-pinches inside the v-domain. The genuine cavity
1137    // ends AT the pinch (the offset cone's own apex). For a linear-v net
1138    // (two sample rows — every made/booleaned cone) the pinch lies on each
1139    // ruling at the fraction where the radial vector vanishes: detect the
1140    // inversion (far-row radials anti-parallel to near-row radials about the
1141    // row centroids) and pull the far row back to the pinch point, so the
1142    // fitted surface ends in a proper degenerate apex row instead of a
1143    // parasitic inverted tip ending in an unweldable ring.
1144    // Both blocks below move the sample grid OFF the pointwise offset on
1145    // purpose. Recording that is what lets `offset_surface_measured` decline to
1146    // report a designed divergence as a fit error.
1147    let mut reparameterised = false;
1148    let mut extended_source: Option<NurbsSurface> = None;
1149    // Two u samples are enough for both: a 2 × 2 bilinear net (a planar
1150    // trapezoid wall of a lofted square frustum, which the affine test above
1151    // rejects on its parallelogram defect) has straight rulings like any
1152    // other linear-v net, and without the extension its inward carrier
1153    // stopped 0.059 short of the opening the reach lane had measured
1154    // (2026-09-26, the SHARP rim corner fixture).
1155    if parameters_v.len() == 2 && parameters_u.len() >= 2 {
1156        let centroid = |column: usize| {
1157            let mut sum = Vec3::default();
1158            for row in &samples {
1159                sum = sum.add(row[column]);
1160            }
1161            sum.scale(1.0 / samples.len() as f64)
1162        };
1163        let near_centroid = centroid(0);
1164        let far_centroid = centroid(1);
1165        let mut inverted = true;
1166        let mut pinch_fraction = 0.0f64;
1167        let mut near_mean = 0.0f64;
1168        let mut far_mean = 0.0f64;
1169        for row in &samples {
1170            let near_radial = row[0].sub(near_centroid);
1171            let far_radial = row[1].sub(far_centroid);
1172            let near_len = near_radial.length();
1173            let far_len = far_radial.length();
1174            if near_len <= 1e-9 || far_len <= 1e-9 {
1175                inverted = false;
1176                break;
1177            }
1178            if near_radial.dot(far_radial) >= 0.0 {
1179                inverted = false;
1180                break;
1181            }
1182            pinch_fraction += near_len / (near_len + far_len) / samples.len() as f64;
1183            near_mean += near_len / samples.len() as f64;
1184            far_mean += far_len / samples.len() as f64;
1185        }
1186        if inverted {
1187            let retrim_far = far_mean <= near_mean;
1188            // WHOSE fold is it?  The retrim exists so a cavity that ends at the
1189            // offset cone's own apex ends there in the TOPOLOGY too, instead of
1190            // running past the pinch into a mirrored ring no weld can use.  It
1191            // pays for that by moving the sample grid off the pointwise offset,
1192            // and the price is the shared-basis identity every consumer rests
1193            // on: after it, the SAME `(u, v)` no longer names the offset of the
1194            // same source point.
1195            //
1196            // That price is only worth paying when the face's TRIM actually
1197            // reaches past the pinch.  A trim on a cone's wide half offsets
1198            // perfectly well; its carrier folds somewhere the face never goes,
1199            // and shearing its parameterization to tidy that away sheared the
1200            // trim's own pcurves with it — a 45° cone sheet trimmed to its wide
1201            // half shipped a slab 0.86 % off its Pappus volume, validating and
1202            // wrong (`thicken.rs`,
1203            // `a_re_parameterized_offset_carrier_keeps_the_pointwise_offset`).
1204            //
1205            // So read the trim, the way the extension below already does.  With
1206            // a linear v basis the pinch sits at the v FRACTION
1207            // `pinch_fraction`, and the folded side is whichever end the retrim
1208            // would have pulled.  A face with no loops still reports the whole
1209            // domain, which keeps every full-domain carrier on its exact
1210            // previous path.
1211            let (_, trim_v) = trim_domain_fractions(face, knot_u.domain(), knot_v.domain())?;
1212            let trim_reaches_the_pinch = if retrim_far {
1213                trim_v[1] > pinch_fraction + 1e-9
1214            } else {
1215                trim_v[0] < pinch_fraction - 1e-9
1216            };
1217            if trim_reaches_the_pinch {
1218                // Pull the crossed (smaller-ring, past-the-pinch) end back to
1219                // the pinch point on each ruling.
1220                reparameterised = true;
1221                for row in &mut samples {
1222                    let near = row[0];
1223                    let far = row[1];
1224                    let pinch = near.add(far.sub(near).scale(pinch_fraction));
1225                    if retrim_far {
1226                        row[1] = pinch;
1227                    } else {
1228                        row[0] = pinch;
1229                    }
1230                }
1231            }
1232        }
1233        // RULED EXTENSION: `planar_extension` is a no-op for curved carriers
1234        // above, but a cone/cylinder lateral joined at a reflex edge needs
1235        // its offset skin to GROW past the source rim exactly like a plane
1236        // (a cylinder piercing a cone: the two offsets only meet past both
1237        // cloned rims). The carrier's parameterization stays (knots/pcurves
1238        // untouched) while its world image — and with it the cloned trim's
1239        // image — inflates: the carrier is the pointwise offset of the SOURCE
1240        // EXTENDED ALONG ITS RULINGS, which a linear-v net does exactly.
1241        //
1242        // Stretching the sampled OFFSET rulings instead reaches the same
1243        // surface only where the source is DEVELOPABLE — its normal constant
1244        // along each ruling, as on a cylinder, a cone or an extruded wall —
1245        // because only there is the offset ruled too. A twisted loft is ruled
1246        // and not developable: its stretched offset rulings left the offset,
1247        // and an outward shell of one validated 0.235 short (1.0 %). So the
1248        // stretch is MEASURED against the extended source's pointwise offset,
1249        // at both ends and the middle of every sampled ruling, and kept only
1250        // within the fit contract; otherwise the extended source is sampled
1251        // pointwise, and either way the refinement reads the carrier against it.
1252        if extension.extends_v() && !inverted {
1253            let mut min_ruling = f64::MAX;
1254            let mut back_allowance = f64::MAX;
1255            let mut forward_allowance = f64::MAX;
1256            let mut extendable = true;
1257            for row in &samples {
1258                let ruling = row[1].sub(row[0]);
1259                let length = ruling.length();
1260                min_ruling = min_ruling.min(length);
1261                // Radii about the row centroids expose a converging (conic)
1262                // ruling sheaf; the extension must stop short of its apex or
1263                // the sheet folds through it.
1264                let near_radial = row[0].sub(near_centroid).length();
1265                let far_radial = row[1].sub(far_centroid).length();
1266                if (far_radial - near_radial).abs() > 1e-9 {
1267                    let apex_at = near_radial / (near_radial - far_radial);
1268                    if (-1e-9..=1.0 + 1e-9).contains(&apex_at) {
1269                        // Apex inside the span: degenerate sheet, do not touch.
1270                        extendable = false;
1271                        break;
1272                    }
1273                    if apex_at < 0.0 {
1274                        back_allowance = back_allowance.min(0.9 * -apex_at);
1275                    } else {
1276                        forward_allowance = forward_allowance.min(0.9 * (apex_at - 1.0));
1277                    }
1278                }
1279            }
1280            // The allowances above are read off the OFFSET rows. The source's
1281            // own ruling sheaf converges at a different rate — an outward
1282            // offset moves a cone's apex FARTHER along the ruling — so a
1283            // growth the offset rows allow can still carry the EXTENDED SOURCE
1284            // through its own apex, and its pointwise offset would then be
1285            // taken on a surface folded back on itself. Read the same radial
1286            // ratio on the source rows and clamp by the tighter of the two.
1287            let mut source_back = f64::MAX;
1288            let mut source_forward = f64::MAX;
1289            if extendable {
1290                let source_centroid = |column: usize| {
1291                    let mut sum = Vec3::default();
1292                    for row in &source.control_points {
1293                        if let Ok(point) = row[column].point() {
1294                            sum = sum.add(point);
1295                        }
1296                    }
1297                    sum.scale(1.0 / source.control_points.len() as f64)
1298                };
1299                let (near_source, far_source) = (source_centroid(0), source_centroid(1));
1300                for row in &source.control_points {
1301                    let (near, far) = (row[0].point().or_refuse(KernelStage::Refine, "point")?, row[1].point().or_refuse(KernelStage::Refine, "point")?);
1302                    let near_radial = near.sub(near_source).length();
1303                    let far_radial = far.sub(far_source).length();
1304                    if (far_radial - near_radial).abs() > 1e-9 {
1305                        let apex_at = near_radial / (near_radial - far_radial);
1306                        if (-1e-9..=1.0 + 1e-9).contains(&apex_at) {
1307                            extendable = false;
1308                            break;
1309                        }
1310                        if apex_at < 0.0 {
1311                            source_back = source_back.min(0.9 * -apex_at);
1312                        } else {
1313                            source_forward = source_forward.min(0.9 * (apex_at - 1.0));
1314                        }
1315                    }
1316                }
1317            }
1318            let (back_allowance, forward_allowance) =
1319                (back_allowance.min(source_back), forward_allowance.min(source_forward));
1320            if extendable && min_ruling > 1e-9 {
1321                // Solved for the TRIM's v-extent, not the domain's: a bore
1322                // face keeps its drill's full-height surface and is trimmed
1323                // to the part it pierces, so moving the surface's own ends
1324                // by |d| moved the trim's rims by only a fraction of that
1325                // (a 30-long ruling trimmed to 20 gave 0.667 for 1.0) — and
1326                // an outward shell's bore never reached the grown planes.
1327                let (_, trim_v) = trim_domain_fractions(face, knot_u.domain(), knot_v.domain())?;
1328                let (back_world, forward_world) =
1329                    domain_end_moves(extension.v_min, extension.v_max, trim_v);
1330                let (requested_back, requested_forward) = (back_world / min_ruling, forward_world / min_ruling);
1331                // APEX END: the 0.9 above keeps a growth from folding through an
1332                // apex it might not have located exactly. When the source is an
1333                // analytic cone the offset apex IS located exactly, and a reach
1334                // that would pass it ends AT it: the far row becomes the pole the
1335                // cavity really ends in. Stopping at 0.9 left a ring there (radius
1336                // 2.99e-3 on the d = 1 inward cone) that the carrier's rim
1337                // collapse then named a point, so the shipped edge was a point on
1338                // a trim that is a ring, and the shell was open over its disk —
1339                // 2.69e-5 of volume, all of it. Only while the SOURCE's own apex
1340                // lies beyond (its clamped allowance covers the reach), so the
1341                // extended source never folds; otherwise the clamp above stands.
1342                let apex_end = analytic_offset_apex(source, &samples, offset_fit_contract(source)).and_then(
1343                    |(at, apex)| {
1344                        if at > 1.0 && at - 1.0 <= requested_forward && at - 1.0 <= source_forward {
1345                            Some((false, at - 1.0, apex))
1346                        } else if at < 0.0 && -at <= requested_back && -at <= source_back {
1347                            Some((true, -at, apex))
1348                        } else {
1349                            None
1350                        }
1351                    },
1352                );
1353                let back = match apex_end {
1354                    Some((true, reach, _)) => reach,
1355                    _ => requested_back.min(back_allowance),
1356                };
1357                let forward = match apex_end {
1358                    Some((false, reach, _)) => reach,
1359                    _ => requested_forward.min(forward_allowance),
1360                };
1361                let stretched = samples
1362                    .iter()
1363                    .map(|row| {
1364                        let ruling = row[1].sub(row[0]);
1365                        let (near, far) = (row[0].sub(ruling.scale(back)), row[1].add(ruling.scale(forward)));
1366                        match apex_end {
1367                            Some((true, _, apex)) => vec![apex, far],
1368                            Some((false, _, apex)) => vec![near, apex],
1369                            None => vec![near, far],
1370                        }
1371                    })
1372                    .collect::<Vec<_>>();
1373                let extended = extend_along_rulings(source, back, forward)?;
1374                let extended_offsets = OffsetEvaluator::new(
1375                    "offset_surface",
1376                    &extended,
1377                    OffsetNormal::FaceStable {
1378                        same_sense: face.same_sense,
1379                    },
1380                );
1381                let [v0, v1] = knot_v.domain();
1382                let mut developability = 0.0f64;
1383                for (row, &u) in stretched.iter().zip(&parameters_u) {
1384                    for fraction in [0.0, 0.5, 1.0] {
1385                        let exact = extended_offsets.at(u, v0 + (v1 - v0) * fraction, -distance).or_refuse(KernelStage::Refine, "at")?.point;
1386                        let along = row[0].add(row[1].sub(row[0]).scale(fraction));
1387                        developability = developability.max(exact.sub(along).length());
1388                    }
1389                }
1390                let contract = offset_fit_contract(source);
1391                let stretch_kept = developability <= contract;
1392                samples = if stretch_kept {
1393                    stretched
1394                } else {
1395                    parameters_u
1396                        .iter()
1397                        .map(|&u| {
1398                            parameters_v
1399                                .iter()
1400                                .map(|&v| Ok(extended_offsets.at(u, v, -distance).or_refuse(KernelStage::Refine, "at")?.point))
1401                                .collect::<Result<Vec<_>, KernelRefusal>>()
1402                        })
1403                        .collect::<Result<Vec<_>, KernelRefusal>>()?
1404                };
1405                if let Ok(path) = std::env::var("BREP_OFFSET_FIT_TRACE") {
1406                    use std::io::Write;
1407                    let line = format!(
1408                        "extension deg=({},{}) net=({},{}) d={distance:.6} back={back:.6} forward={forward:.6} \
1409                         developability={developability:.3e} contract={contract:.3e} lane={} thread={} args={}\n",
1410                        source.degree_u,
1411                        source.degree_v,
1412                        source.control_points.len(),
1413                        source.control_points[0].len(),
1414                        if stretch_kept { "stretch" } else { "pointwise" },
1415                        std::thread::current().name().unwrap_or("-"),
1416                        std::env::args().skip(1).collect::<Vec<_>>().join(" "),
1417                    );
1418                    if let Ok(mut file) = std::fs::OpenOptions::new().create(true).append(true).open(path) {
1419                        let _ = file.write_all(line.as_bytes());
1420                    }
1421                }
1422                extended_source = Some(extended);
1423            }
1424        }
1425    }
1426    let reference = extended_source.as_ref().unwrap_or(source);
1427    // Lane O row 35 (`BREP_OFFSET_EXACT_CYLINDER`, default on): a CIRCULAR
1428    // CYLINDER's offset is the coaxial cylinder, built exactly from the
1429    // reference the extension above settled on (so every growth decision is
1430    // the fit's own) and measured before it is kept; any decline falls through
1431    // to the fit below, unchanged.
1432    if !reparameterised && exact_cylinder_offsets_on() {
1433        if let Some(surface) = cylinder_offset_surface(reference, face.same_sense, distance)? {
1434            return Ok((surface, OffsetSurfaceLane::Analytic, extended_source));
1435        }
1436    }
1437    let weights = reference
1438        .control_points
1439        .iter()
1440        .map(|row| row.iter().map(|point| point.w).collect::<Vec<_>>())
1441        .collect::<Vec<_>>();
1442    let fitted = NurbsSurface::new(
1443        source.degree_u,
1444        source.degree_v,
1445        source.knots_u.clone(),
1446        source.knots_v.clone(),
1447        interpolate_tensor(
1448            &knot_u,
1449            &knot_v,
1450            &parameters_u,
1451            &parameters_v,
1452            &samples,
1453            &weights,
1454        )?,
1455    ).or_refuse(KernelStage::Refine, "new")?;
1456    if reparameterised {
1457        return Ok((fitted, OffsetSurfaceLane::Reparameterised, None));
1458    }
1459    let reference_offsets = OffsetEvaluator::new(
1460        "offset_surface",
1461        reference,
1462        OffsetNormal::FaceStable {
1463            same_sense: face.same_sense,
1464        },
1465    );
1466    Ok((
1467        refine_offset_fit(reference, &reference_offsets, distance, fitted)?,
1468        OffsetSurfaceLane::Fit,
1469        extended_source,
1470    ))
1471}
1472
1473
1474/// Where the sampled OFFSET rulings of an analytic cone or frustum meet its
1475/// axis: the ruling fraction (past 1 beyond the far row, below 0 before the near
1476/// row) and the point. The radial ratio above already gives the fraction (both
1477/// rows and their centroids scale about the apex); what it does not give is the
1478/// POINT the far row must become, so each ruling is met with the recognized axis
1479/// here and the crossings are checked to be one point. `None` unless the source
1480/// is a `RuledRevolution` and every ruling meets the axis within `contract`,
1481/// within `contract` of one point.
1482fn analytic_offset_apex(source: &NurbsSurface, samples: &[Vec<Vec3>], contract: f64) -> Option<(f64, Vec3)> {
1483    let Some(crate::AnalyticSurface::RuledRevolution { frame, .. }) = crate::analytic_surface::recognize(source) else {
1484        return None;
1485    };
1486    let radial = |point: Vec3| {
1487        let offset = point.sub(frame.origin);
1488        offset.sub(frame.axis.scale(offset.dot(frame.axis)))
1489    };
1490    let mut crossings = Vec::with_capacity(samples.len());
1491    for row in samples {
1492        let (near, far) = (radial(row[0]), radial(row[1]));
1493        let step = far.sub(near);
1494        let at = -near.dot(step) / step.dot(step);
1495        if !at.is_finite() {
1496            return None;
1497        }
1498        crossings.push((at, row[0].add(row[1].sub(row[0]).scale(at))));
1499    }
1500    // Averaged as offsets from the first crossing, so a part far from the world
1501    // origin sums small differences, not large coordinates.
1502    let count = crossings.len() as f64;
1503    let at = crossings.iter().map(|(at, _)| at).sum::<f64>() / count;
1504    let first = crossings.first()?.1;
1505    let spread = crossings.iter().fold(Vec3::default(), |sum, (_, point)| sum.add(point.sub(first)));
1506    let centre = first.add(spread.scale(1.0 / count));
1507    let apex = frame.origin.add(frame.axis.scale(centre.sub(frame.origin).dot(frame.axis)));
1508    // On the axis, and one point: a cylinder's near-parallel rulings give a
1509    // finite `at` from rounding, and only these two readings refuse it.
1510    crossings
1511        .iter()
1512        .all(|(_, point)| radial(*point).length() <= contract && point.sub(apex).length() <= contract)
1513        .then_some((at, apex))
1514}
1515
1516/// A linear-v surface continued along its own rulings by `back` and `forward`
1517/// ruling lengths — the same surface, re-parameterized onto the longer span.
1518/// Exact: each ruling is linear in homogeneous coordinates, so extrapolating the
1519/// homogeneous control rows traces the ruling past both ends. Refused where a
1520/// continued weight stops being positive, which is where the ruling leaves the
1521/// rational patch.
1522fn extend_along_rulings(surface: &NurbsSurface, back: f64, forward: f64) -> Result<NurbsSurface, KernelRefusal> {
1523    let rows = surface
1524        .control_points
1525        .iter()
1526        .map(|row| {
1527            let step = row[1].add(row[0].scale(-1.0));
1528            vec![row[0].add(step.scale(-back)), row[1].add(step.scale(forward))]
1529        })
1530        .collect::<Vec<_>>();
1531    if rows.iter().flatten().any(|point| point.w <= 1e-12) {
1532        return Err(KernelRefusal::unsupported(KernelStage::Refine, "offset_ruled_extension_weight", format!(
1533            "offset_surface: extending a ruled face by ({back:.6}, {forward:.6}) of its rulings \
1534             carries a weight through zero; the extended face is not this surface"
1535        )));
1536    }
1537    NurbsSurface::new(surface.degree_u, 1, surface.knots_u.clone(), surface.knots_v.clone(), rows)
1538        .or_refuse(KernelStage::Refine, "new")
1539}
1540
1541/// The fit contract a carrier of `source` is refined toward: the kernel's
1542/// intersection-fit tolerance for the source net's extent.
1543fn offset_fit_contract(source: &NurbsSurface) -> f64 {
1544    let scale = crate::model_scale(
1545        source
1546            .control_points
1547            .iter()
1548            .flatten()
1549            .filter_map(|point| point.point().ok()),
1550    );
1551    crate::KernelTolerances::for_scale(scale, 1e-7).intersection_fit
1552}
1553
1554/// Halvings of every knot span [`refine_offset_fit`] may take, per direction.
1555const OFFSET_FIT_REFINEMENTS: usize = 6;
1556
1557/// The largest net one refined collocation may solve: separable weights solve
1558/// one direction at a time, non-separable ones solve the whole tensor densely.
1559const OFFSET_FIT_MAX_CONTROLS_SEPARABLE: usize = 16_384;
1560const OFFSET_FIT_MAX_CONTROLS_TENSOR: usize = 900;
1561
1562/// The out-of-sample deviation of a collocation fit from the pointwise offset,
1563/// read at the MIDPOINTS between consecutive Greville parameters, where the fit
1564/// interpolates nothing: along u (u midpoints on the v Greville rows), along v,
1565/// and between both.
1566#[derive(Clone, Debug)]
1567struct OffsetFitDeviation {
1568    along_u: f64,
1569    along_v: f64,
1570    between: f64,
1571    u_sites: Vec<(f64, f64)>,
1572    v_sites: Vec<(f64, f64)>,
1573    between_uv: (f64, f64),
1574}
1575
1576impl OffsetFitDeviation {
1577    fn worst(&self) -> f64 {
1578        self.along_u.max(self.along_v).max(self.between)
1579    }
1580
1581    fn refinement_sites(&self, bar: f64) -> (Vec<f64>, Vec<f64>) {
1582        let mut u = self.u_sites.iter().filter(|(_, miss)| *miss > bar)
1583            .map(|(parameter, _)| *parameter).collect::<Vec<_>>();
1584        let mut v = self.v_sites.iter().filter(|(_, miss)| *miss > bar)
1585            .map(|(parameter, _)| *parameter).collect::<Vec<_>>();
1586        if u.is_empty() && v.is_empty() && self.between > bar {
1587            u.push(self.between_uv.0);
1588            v.push(self.between_uv.1);
1589        }
1590        (u, v)
1591    }
1592}
1593
1594fn greville_midpoints(parameters: &[f64]) -> Vec<f64> {
1595    parameters
1596        .windows(2)
1597        .filter(|pair| pair[1] - pair[0] > 1e-12)
1598        .map(|pair| 0.5 * (pair[0] + pair[1]))
1599        .collect()
1600}
1601
1602fn offset_fit_deviation(
1603    fitted: &NurbsSurface,
1604    evaluator: &OffsetEvaluator,
1605    distance: f64,
1606) -> Result<OffsetFitDeviation, KernelRefusal> {
1607    let parameters_u = greville_parameters(&KnotVector::new(fitted.knots_u.clone(), fitted.degree_u).or_refuse(KernelStage::Refine, "new")?);
1608    let parameters_v = greville_parameters(&KnotVector::new(fitted.knots_v.clone(), fitted.degree_v).or_refuse(KernelStage::Refine, "new")?);
1609    let (middle_u, middle_v) = (greville_midpoints(&parameters_u), greville_midpoints(&parameters_v));
1610    let residual_locus = std::env::var("BREP_OFFSET_FIT_RESIDUAL").as_deref() != Ok("0");
1611    let gap = |u: f64, v: f64| -> Result<f64, KernelRefusal> {
1612        Ok(fitted.evaluate(u, v).or_refuse(KernelStage::Refine, "evaluate")?.sub(evaluator.at(u, v, -distance).or_refuse(KernelStage::Refine, "at")?.point).length())
1613    };
1614    let mut deviation = OffsetFitDeviation {
1615        along_u: 0.0,
1616        along_v: 0.0,
1617        between: 0.0,
1618        u_sites: Vec::new(),
1619        v_sites: if residual_locus { middle_v.iter().map(|&v| (v, 0.0)).collect() } else { Vec::new() },
1620        between_uv: (0.0, 0.0),
1621    };
1622    for &u in &middle_u {
1623        let mut along = 0.0f64;
1624        for &v in &parameters_v {
1625            let miss = gap(u, v)?;
1626            along = along.max(miss);
1627            deviation.along_u = deviation.along_u.max(miss);
1628        }
1629        if residual_locus { deviation.u_sites.push((u, along)); }
1630        for &v in &middle_v {
1631            let miss = gap(u, v)?;
1632            if miss > deviation.between { deviation.between_uv = (u, v); }
1633            deviation.between = deviation.between.max(miss);
1634        }
1635    }
1636    for &u in &parameters_u {
1637        for (index, &v) in middle_v.iter().enumerate() {
1638            let miss = gap(u, v)?;
1639            deviation.along_v = deviation.along_v.max(miss);
1640            if residual_locus { deviation.v_sites[index].1 = deviation.v_sites[index].1.max(miss); }
1641        }
1642    }
1643    Ok(deviation)
1644}
1645
1646/// `surface` with each requested LINEAR direction raised to cubic, the same
1647/// geometry: every span re-expressed as a cubic Bezier on the homogeneous net
1648/// (the points at thirds), joined C0 by triple knots. `None` when a requested
1649/// direction is not linear or carries a repeated interior knot.
1650///
1651/// A linear direction is raised rather than halved because the offset of a
1652/// ruled surface is generally not ruled, and a piecewise-linear fit converges
1653/// only as h^2: the twisted loft's v direction still read 3.3e-6 after six
1654/// halvings, where the cubic basis converges as h^4.
1655fn raise_linear_directions(
1656    surface: &NurbsSurface,
1657    in_u: bool,
1658    in_v: bool,
1659) -> Result<Option<NurbsSurface>, KernelRefusal> {
1660    fn raise(knots: &[f64], row: &[Vec4]) -> Option<(Vec<f64>, Vec<Vec4>)> {
1661        // Degree 1: knots are [k0, k0, k1, ..., kn, kn] for n + 1 controls.
1662        let count = row.len();
1663        if knots.len() != count + 2 {
1664            return None;
1665        }
1666        let breaks = &knots[1..=count];
1667        if breaks.windows(2).any(|pair| pair[1] - pair[0] <= 1e-12) {
1668            return None;
1669        }
1670        let mut raised_knots = vec![breaks[0]; 4];
1671        for &interior in &breaks[1..count - 1] {
1672            raised_knots.extend([interior; 3]);
1673        }
1674        raised_knots.extend([breaks[count - 1]; 4]);
1675        let mut points = Vec::with_capacity(3 * (count - 1) + 1);
1676        for pair in row.windows(2) {
1677            let step = pair[1].add(pair[0].scale(-1.0));
1678            points.push(pair[0]);
1679            points.push(pair[0].add(step.scale(1.0 / 3.0)));
1680            points.push(pair[0].add(step.scale(2.0 / 3.0)));
1681        }
1682        points.push(row[count - 1]);
1683        Some((raised_knots, points))
1684    }
1685    let mut knots_u = surface.knots_u.clone();
1686    let mut knots_v = surface.knots_v.clone();
1687    let mut degree_u = surface.degree_u;
1688    let mut degree_v = surface.degree_v;
1689    let mut net = surface.control_points.clone();
1690    if in_v {
1691        if degree_v != 1 {
1692            return Ok(None);
1693        }
1694        let mut rows = Vec::with_capacity(net.len());
1695        for row in &net {
1696            let Some((knots, points)) = raise(&surface.knots_v, row) else {
1697                return Ok(None);
1698            };
1699            knots_v = knots;
1700            rows.push(points);
1701        }
1702        net = rows;
1703        degree_v = 3;
1704    }
1705    if in_u {
1706        if degree_u != 1 {
1707            return Ok(None);
1708        }
1709        let count_v = net[0].len();
1710        let mut columns = Vec::with_capacity(count_v);
1711        for column in 0..count_v {
1712            let row = net.iter().map(|row| row[column]).collect::<Vec<_>>();
1713            let Some((knots, points)) = raise(&surface.knots_u, &row) else {
1714                return Ok(None);
1715            };
1716            knots_u = knots;
1717            columns.push(points);
1718        }
1719        let count_u = columns[0].len();
1720        net = (0..count_u)
1721            .map(|row| columns.iter().map(|column| column[row]).collect())
1722            .collect();
1723        degree_u = 3;
1724    }
1725    Ok(Some(NurbsSurface::new(degree_u, degree_v, knots_u, knots_v, net).or_refuse(KernelStage::Refine, "new")?))
1726}
1727
1728/// `surface`, the same geometry with a knot inserted at the midpoint of every
1729/// non-empty span of each requested direction (exact Boehm insertion on the
1730/// homogeneous net, so the weights refine with it).
1731fn halve_knot_spans(surface: &NurbsSurface, in_u: bool, in_v: bool) -> Result<NurbsSurface, KernelRefusal> {
1732    fn midpoints(knots: &[f64], degree: usize) -> Vec<f64> {
1733        let last = knots.len() - 1 - degree;
1734        knots[degree..=last]
1735            .windows(2)
1736            .filter(|pair| pair[1] - pair[0] > 1e-12)
1737            .map(|pair| 0.5 * (pair[0] + pair[1]))
1738            .collect()
1739    }
1740    insert_offset_fit_knots(surface,
1741        &if in_u { midpoints(&surface.knots_u, surface.degree_u) } else { Vec::new() },
1742        &if in_v { midpoints(&surface.knots_v, surface.degree_v) } else { Vec::new() })
1743}
1744
1745/// Insert the fit's measured residual coordinates, rather than relocating
1746/// every collocation node by blindly halving all spans. In particular a
1747/// quadratic's first midpoint insertion loses its old central Greville node;
1748/// inserting the two residual lobes preserves that node and resolves them.
1749fn insert_offset_fit_knots(surface: &NurbsSurface, u: &[f64], v: &[f64]) -> Result<NurbsSurface, KernelRefusal> {
1750    let refine = |degree: usize, knots: &[f64], net: Vec<Vec<Vec4>>, inserted: &[f64]| -> Result<(Vec<f64>, Vec<Vec<Vec4>>), KernelRefusal> {
1751        let mut inserted = inserted.to_vec();
1752        inserted.sort_by(f64::total_cmp);
1753        inserted.dedup_by(|a, b| (*a - *b).abs() <= 1e-12);
1754        inserted.retain(|parameter| knots.iter().filter(|knot| (**knot - *parameter).abs() <= 1e-12).count() < degree);
1755        let mut refined_knots = knots.to_vec();
1756        let mut rows = Vec::with_capacity(net.len());
1757        for row in net {
1758            let mut curve = NurbsCurve::new(degree, knots.to_vec(), row).or_refuse(KernelStage::Refine, "new")?;
1759            for &parameter in &inserted {
1760                curve = curve.insert_knot(parameter, 1).or_refuse(KernelStage::Refine, "insert_knot")?;
1761            }
1762            refined_knots = curve.knots.clone();
1763            rows.push(curve.control_points);
1764        }
1765        Ok((refined_knots, rows))
1766    };
1767    let mut knots_u = surface.knots_u.clone();
1768    let mut knots_v = surface.knots_v.clone();
1769    let mut net = surface.control_points.clone();
1770    if !v.is_empty() {
1771        let (knots, rows) = refine(surface.degree_v, &knots_v, net, v)?;
1772        knots_v = knots;
1773        net = rows;
1774    }
1775    if !u.is_empty() {
1776        // Transpose so each u column is a curve, refine, transpose back.
1777        let count_v = net[0].len();
1778        let columns = (0..count_v)
1779            .map(|column| net.iter().map(|row| row[column]).collect::<Vec<_>>())
1780            .collect::<Vec<_>>();
1781        let (knots, refined) = refine(surface.degree_u, &knots_u, columns, u)?;
1782        knots_u = knots;
1783        let count_u = refined[0].len();
1784        net = (0..count_u)
1785            .map(|row| refined.iter().map(|column| column[row]).collect())
1786            .collect();
1787    }
1788    NurbsSurface::new(surface.degree_u, surface.degree_v, knots_u, knots_v, net).or_refuse(KernelStage::Refine, "new")
1789}
1790
1791/// A collocation fit of the pointwise offset, refined until its OUT-OF-SAMPLE
1792/// deviation meets the kernel's intersection-fit contract.
1793///
1794/// The Greville fit interpolates the pointwise offset in the SOURCE's basis.
1795/// That is exact wherever the offset lives in that basis — planes, cylinders,
1796/// cones, spheres, tori, which is why it was built that way — and, on a
1797/// free-form face, only as good as the source's span count: a single-span cubic
1798/// Bezier wall offset by 0.3 was measured 1.2e-2 to 1.7e-2 off its pointwise
1799/// offset, with the fit exact at its four Greville rows and nowhere else.
1800///
1801/// Refinement halves the knot spans of the direction the deviation is read in
1802/// (exact knot insertion, so the parameterization does not move: the same
1803/// `(u, v)` still names the offset of the same source point, and every trim
1804/// pcurve cloned onto the carrier stays valid) and re-fits. A fit already under
1805/// the bar is returned untouched, bit for bit. The bar is the one a carrier's
1806/// EDGES are already held to (`offset_face_carrier_impl`'s `fit_tolerance`),
1807/// derived from the source net's extent.
1808///
1809/// The contract is a TARGET; the offset construction band is the GATE. A fit
1810/// that is still over the contract when the rounds run out
1811/// ([`OFFSET_FIT_REFINEMENTS`]), when a round stops improving, or when the next
1812/// net would pass the solver ceilings, is ACCEPTED as its best measured round if
1813/// that round is inside `offset_construction_band` — the allowance every
1814/// offset construction in this family is already judged against — and refused
1815/// by name if it is not. Accepted over the contract is a decision, not a
1816/// silence: `offset_surface_measured` reports the deviation, and
1817/// `a_fit_the_rounds_leave_over_the_contract_is_accepted_inside_the_band` pins
1818/// it. (Measured on the corpus: two imported `OFFSET_SURFACE`s end their rounds
1819/// at 1.3e-5 and 3.4e-5 against a contract of 2.15e-6 and a band of 0.107.)
1820///
1821/// Cost is bounded by the rounds: a direction's spans grow at most 64-fold, a
1822/// round costs one pointwise offset per Greville node plus about four per node
1823/// to measure, and one dense collocation solve per row and per column
1824/// (`n_v·n_u³ + n_u·n_v³` for separable weights), and the rounds' costs are
1825/// geometric, so the last round is about half of the whole.
1826fn refine_offset_fit(
1827    source: &NurbsSurface,
1828    evaluator: &OffsetEvaluator,
1829    distance: f64,
1830    fitted: NurbsSurface,
1831) -> Result<NurbsSurface, KernelRefusal> {
1832    let scale = crate::model_scale(
1833        source
1834            .control_points
1835            .iter()
1836            .flatten()
1837            .filter_map(|point| point.point().ok()),
1838    );
1839    let bar = crate::KernelTolerances::for_scale(scale, 1e-7).intersection_fit;
1840    let trace = std::env::var("BREP_OFFSET_FIT_TRACE").ok();
1841    let started = web_time::Instant::now();
1842    let enabled = std::env::var("BREP_OFFSET_FIT_REFINE").as_deref() != Ok("0");
1843    let Ok(initial) = offset_fit_deviation(&fitted, evaluator, distance) else {
1844        return Ok(fitted);
1845    };
1846    let mut best = fitted;
1847    let mut best_deviation = initial.clone();
1848    let mut refined_source = source.clone();
1849    let mut rounds = 0usize;
1850    let mut residual_mode = false;
1851    let mut stop = "under bar";
1852    if enabled {
1853        while best_deviation.worst() > bar {
1854            if rounds == OFFSET_FIT_REFINEMENTS {
1855                stop = "refinement limit";
1856                break;
1857            }
1858            let (along_u, along_v) = (best_deviation.along_u > bar, best_deviation.along_v > bar);
1859            let (in_u, in_v) = if along_u || along_v {
1860                (along_u, along_v)
1861            } else {
1862                (true, true)
1863            };
1864            let (raise_u, raise_v) = (in_u && refined_source.degree_u == 1, in_v && refined_source.degree_v == 1);
1865            let raised = if raise_u || raise_v {
1866                raise_linear_directions(&refined_source, raise_u, raise_v)?
1867            } else {
1868                None
1869            };
1870            let (halve_u, halve_v) = match raised {
1871                Some(_) => (in_u && !raise_u, in_v && !raise_v),
1872                None => (in_u, in_v),
1873            };
1874            let raised = raised.unwrap_or_else(|| refined_source.clone());
1875            let candidate_source = if halve_u || halve_v {
1876                if residual_mode {
1877                    let (u, v) = best_deviation.refinement_sites(bar);
1878                    insert_offset_fit_knots(&raised,
1879                        if halve_u { &u } else { &[] }, if halve_v { &v } else { &[] })?
1880                } else {
1881                    halve_knot_spans(&raised, halve_u, halve_v)?
1882                }
1883            } else {
1884                raised
1885            };
1886            let weights = candidate_source
1887                .control_points
1888                .iter()
1889                .map(|row| row.iter().map(|point| point.w).collect::<Vec<_>>())
1890                .collect::<Vec<_>>();
1891            let controls = weights.len() * weights[0].len();
1892            let ceiling = if separable_weights(&weights).is_some() {
1893                OFFSET_FIT_MAX_CONTROLS_SEPARABLE
1894            } else {
1895                OFFSET_FIT_MAX_CONTROLS_TENSOR
1896            };
1897            if controls > ceiling {
1898                stop = "net ceiling";
1899                break;
1900            }
1901            let knot_u = KnotVector::new(candidate_source.knots_u.clone(), candidate_source.degree_u).or_refuse(KernelStage::Refine, "new")?;
1902            let knot_v = KnotVector::new(candidate_source.knots_v.clone(), candidate_source.degree_v).or_refuse(KernelStage::Refine, "new")?;
1903            let parameters_u = greville_parameters(&knot_u);
1904            let parameters_v = greville_parameters(&knot_v);
1905            let samples = parameters_u
1906                .iter()
1907                .map(|&u| {
1908                    parameters_v
1909                        .iter()
1910                        .map(|&v| Ok(evaluator.at(u, v, -distance).or_refuse(KernelStage::Refine, "at")?.point))
1911                        .collect::<Result<Vec<_>, KernelRefusal>>()
1912                })
1913                .collect::<Result<Vec<_>, KernelRefusal>>();
1914            let Ok(samples) = samples else {
1915                stop = "unevaluable sample";
1916                break;
1917            };
1918            let candidate = NurbsSurface::new(
1919                candidate_source.degree_u,
1920                candidate_source.degree_v,
1921                candidate_source.knots_u.clone(),
1922                candidate_source.knots_v.clone(),
1923                interpolate_tensor(&knot_u, &knot_v, &parameters_u, &parameters_v, &samples, &weights)?,
1924            ).or_refuse(KernelStage::Refine, "new")?;
1925            let Ok(deviation) = offset_fit_deviation(&candidate, evaluator, distance) else {
1926                stop = "unmeasurable round";
1927                break;
1928            };
1929            rounds += 1;
1930            if deviation.worst() >= best_deviation.worst() {
1931                // One midpoint split of a quadratic Bezier adds no freedom
1932                // to its symmetric cross-section: the C1 halves still form
1933                // one symmetric quadratic. Its shifted interpolation nodes
1934                // can make this round worse even though the next subdivision
1935                // improves substantially. Try that next subdivision once,
1936                // keeping the previous fit until an improvement is measured.
1937                let first_quadratic_split = rounds == 1
1938                    && ((halve_u && source.degree_u == 2 && source.knots_u.len() == 6)
1939                        || (halve_v && source.degree_v == 2 && source.knots_v.len() == 6));
1940                if first_quadratic_split {
1941                    refined_source = candidate_source;
1942                    continue;
1943                }
1944                // Keep every improving uniform round on its existing path.
1945                // Only a measured stall switches the next attempt to the
1946                // residual locus, starting again from the best accepted net.
1947                // The rejected candidate never replaces that best fit.
1948                if !residual_mode && best_deviation.worst() > offset_construction_band(scale)
1949                    && std::env::var("BREP_OFFSET_FIT_RESIDUAL").as_deref() != Ok("0")
1950                {
1951                    residual_mode = true;
1952                    continue;
1953                }
1954                stop = "stopped improving";
1955                break;
1956            }
1957            best = candidate;
1958            best_deviation = deviation;
1959            refined_source = candidate_source;
1960        }
1961    } else if best_deviation.worst() > bar {
1962        stop = "refinement disabled";
1963    }
1964    // The gate. Refinement aims at the fit contract; a carrier it could not
1965    // bring inside the offset family's own acceptance band is refused by name
1966    // rather than handed on — a fit measured six orders past its band used to be
1967    // recorded and used anyway.
1968    let band = offset_construction_band(scale);
1969    let refused = enabled && best_deviation.worst() > band;
1970    if let Some(path) = trace {
1971        use std::io::Write;
1972        let caller = std::env::args().skip(1).collect::<Vec<_>>().join(" ");
1973        let thread = std::thread::current().name().unwrap_or("-").to_string();
1974        let line = format!(
1975            "fit deg=({},{}) net=({},{})->({},{}) d={distance:.6} scale={scale:.4} bar={bar:.3e} \
1976             initial=(u {:.3e}, v {:.3e}, uv {:.3e}) final={:.3e} rounds={rounds} stop={stop} \
1977             refused={refused} seconds={:.4} thread={thread} args={caller}\n",
1978            source.degree_u,
1979            source.degree_v,
1980            source.control_points.len(),
1981            source.control_points[0].len(),
1982            best.control_points.len(),
1983            best.control_points[0].len(),
1984            initial.along_u,
1985            initial.along_v,
1986            initial.between,
1987            best_deviation.worst(),
1988            started.elapsed().as_secs_f64(),
1989        );
1990        if let Ok(mut file) = std::fs::OpenOptions::new().create(true).append(true).open(path) {
1991            let _ = file.write_all(line.as_bytes());
1992        }
1993    }
1994    if refused {
1995        return Err(KernelRefusal::non_convergence(KernelStage::Refine, "offset_carrier_fit", format!(
1996            "offset_surface: the fitted offset carrier stays {:.3e} off its pointwise offset at \
1997             distance {distance} after {rounds} refinement round(s) ({stop}), past the offset \
1998             construction band {band:.3e}",
1999            best_deviation.worst()
2000        )));
2001    }
2002    Ok(best)
2003}
2004
2005/// Two offset sheets of one source, re-expressed on ONE basis without moving
2006/// either: a linear direction facing a cubic one is raised, then each sheet
2007/// takes the knots the other has and it lacks (exact insertion).
2008///
2009/// [`refine_offset_fit`] refines each carrier by its own measured deviation, so
2010/// two sheets of the same source at different distances can leave on different
2011/// nets; a consumer that pairs their iso curves control point for control point
2012/// (`thicken`'s ruled walls) needs them on the same one. Weights follow the
2013/// insertions, and both sheets carry the source's weights refined the same way.
2014pub(crate) fn unify_offset_sheet_bases(
2015    first: &NurbsSurface,
2016    second: &NurbsSurface,
2017) -> Result<(NurbsSurface, NurbsSurface), KernelRefusal> {
2018    fn raise_to(surface: &NurbsSurface, degree_u: usize, degree_v: usize) -> Result<NurbsSurface, KernelRefusal> {
2019        let raise_u = surface.degree_u == 1 && degree_u == 3;
2020        let raise_v = surface.degree_v == 1 && degree_v == 3;
2021        if !raise_u && !raise_v {
2022            return Ok(surface.clone());
2023        }
2024        raise_linear_directions(surface, raise_u, raise_v)?
2025            .ok_or_else(|| {
2026                KernelRefusal::internal(
2027                    KernelStage::Refine,
2028                    "offset_raise_linear",
2029                    "offset sheets: a linear direction could not be raised",
2030                )
2031            })
2032    }
2033    let degree_u = first.degree_u.max(second.degree_u);
2034    let degree_v = first.degree_v.max(second.degree_v);
2035    let mut first = raise_to(first, degree_u, degree_v)?;
2036    let mut second = raise_to(second, degree_u, degree_v)?;
2037    if first.degree_u != second.degree_u || first.degree_v != second.degree_v {
2038        return Err(KernelRefusal::internal(KernelStage::Refine, "offset_common_basis", format!(
2039            "offset sheets: degrees ({},{}) and ({},{}) have no common basis",
2040            first.degree_u, first.degree_v, second.degree_u, second.degree_v
2041        )));
2042    }
2043    // Knots one vector has beyond the other, counted with multiplicity.
2044    fn missing(from: &[f64], into: &[f64]) -> Vec<f64> {
2045        let mut extra = Vec::new();
2046        let mut index = 0usize;
2047        for &knot in from {
2048            while index < into.len() && into[index] < knot - 1e-12 {
2049                index += 1;
2050            }
2051            if index < into.len() && (into[index] - knot).abs() <= 1e-12 {
2052                index += 1;
2053            } else {
2054                extra.push(knot);
2055            }
2056        }
2057        extra
2058    }
2059    fn insert(surface: &NurbsSurface, knots: &[f64], along_u: bool) -> Result<NurbsSurface, KernelRefusal> {
2060        if knots.is_empty() {
2061            return Ok(surface.clone());
2062        }
2063        let (degree, vector) = if along_u {
2064            (surface.degree_u, &surface.knots_u)
2065        } else {
2066            (surface.degree_v, &surface.knots_v)
2067        };
2068        let lines = if along_u {
2069            (0..surface.control_points[0].len())
2070                .map(|column| surface.control_points.iter().map(|row| row[column]).collect::<Vec<_>>())
2071                .collect::<Vec<_>>()
2072        } else {
2073            surface.control_points.clone()
2074        };
2075        let mut refined_knots = vector.clone();
2076        let mut refined = Vec::with_capacity(lines.len());
2077        for line in lines {
2078            let mut curve = NurbsCurve::new(degree, vector.clone(), line).or_refuse(KernelStage::Refine, "new")?;
2079            for &knot in knots {
2080                curve = curve.insert_knot(knot, 1).or_refuse(KernelStage::Refine, "insert_knot")?;
2081            }
2082            refined_knots = curve.knots.clone();
2083            refined.push(curve.control_points);
2084        }
2085        let net = if along_u {
2086            (0..refined[0].len())
2087                .map(|row| refined.iter().map(|column| column[row]).collect())
2088                .collect()
2089        } else {
2090            refined
2091        };
2092        let (knots_u, knots_v) = if along_u {
2093            (refined_knots, surface.knots_v.clone())
2094        } else {
2095            (surface.knots_u.clone(), refined_knots)
2096        };
2097        NurbsSurface::new(surface.degree_u, surface.degree_v, knots_u, knots_v, net).or_refuse(KernelStage::Refine, "new")
2098    }
2099    let (to_first_u, to_second_u) = (
2100        missing(&second.knots_u, &first.knots_u),
2101        missing(&first.knots_u, &second.knots_u),
2102    );
2103    first = insert(&first, &to_first_u, true)?;
2104    second = insert(&second, &to_second_u, true)?;
2105    let (to_first_v, to_second_v) = (
2106        missing(&second.knots_v, &first.knots_v),
2107        missing(&first.knots_v, &second.knots_v),
2108    );
2109    first = insert(&first, &to_first_v, false)?;
2110    second = insert(&second, &to_second_v, false)?;
2111    Ok((first, second))
2112}
2113
2114/// The 3D curve of one carrier edge: the image of the source coedge's pcurve
2115/// on the carrier surface, with the parameter range `(t0, t1)` that matches the
2116/// pcurve's domain fraction for fraction (the contract every coedge of this
2117/// carrier relies on, since the source pcurves are reused verbatim).
2118///
2119/// Built through the [`crate::image_curve`] ladder — exact on an affine sheet
2120/// (every planar offset), the exact iso-curve where the pcurve holds one
2121/// coordinate constant (a blend rail, a cylinder's cap rim, every
2122/// extrude/revolve boundary), and a fitted curve verified to `fit_tolerance`
2123/// off its nodes otherwise. Only where the ladder refuses does the edge fall
2124/// back to the degree-1 interpolant through `points` (the historical
2125/// construction), so nothing that offset before is refused now.
2126///
2127/// Why the polyline is no longer the first choice: its adaptive subdivision
2128/// stops at a 5e-4 chord sag, and a 128-chord rim on an r = 8.5 cylinder sits
2129/// up to 1.6e-4 inside the true arc. That is 16x the boolean imprint's
2130/// coincidence band, so when a later operation's section curve runs along that
2131/// rim (a cutter's side wall coplanar with the offset shell's opening wall,
2132/// 2026-09-06 report) the imprint cannot recognise the rim as the section it
2133/// already is, and reports every chord vertex as a crossing — one edge split
2134/// into forty. An exact arc is recognised as coincident and left alone.
2135fn carrier_edge_curve(
2136    surface: &NurbsSurface,
2137    pcurve: &NurbsCurve,
2138    points: &[Vec3],
2139    parameters: &[f64],
2140    fit_tolerance: f64,
2141) -> Result<(NurbsCurve, f64, f64), KernelRefusal> {
2142    // `BREP_CARRIER_POLYLINE=1` restores the polyline for an A/B comparison.
2143    // TRUSTED AFFINE IMAGE (lane O row 23, 2026-10-29;
2144    // on by default since the batch 6 promotion, `BREP_CARRIER_AFFINE_IMAGE=0` sweeps). On an affine carrier the image
2145    // is the pcurve's control net through the patch's own affine map, exact up to
2146    // round-off for any degree and weights, so `image_curve`'s 25-sample sweep
2147    // can only confirm it; this caller discards the deviation it measures. The
2148    // image and its range are what `image_curve`'s affine tier returns.
2149    if affine_image_trusted() && surface.is_affine().or_refuse(KernelStage::Refine, "is_affine")? {
2150        let [q0, q1] = pcurve.domain().or_refuse(KernelStage::Refine, "domain")?;
2151        if (q1 - q0).is_finite() && (q1 - q0).abs() > 0.0 {
2152            let curve = crate::image_curve::affine_image_curve(surface, pcurve).or_refuse(KernelStage::Refine, "affine_image_curve")?;
2153            return Ok((curve, q0, q1));
2154        }
2155    }
2156    let image = if env_read!("BREP_CARRIER_POLYLINE").as_deref() == Some("1") {
2157        Err("BREP_CARRIER_POLYLINE set".to_string())
2158    } else {
2159        crate::image_curve::image_curve(surface, pcurve, fit_tolerance, "offset_face_carrier")
2160    };
2161    match image {
2162        Ok(image) if image.t0 <= image.t1 => Ok((image.curve, image.t0, image.t1)),
2163        Ok(image) => {
2164            // The image runs against the pcurve: reverse it so the edge range
2165            // is increasing, reflecting the range through the curve's domain.
2166            let [start, end] = image.curve.domain().or_refuse(KernelStage::Refine, "domain")?;
2167            let curve = image.curve.reversed().or_refuse(KernelStage::Refine, "reversed")?;
2168            Ok((curve, start + end - image.t0, start + end - image.t1))
2169        }
2170        Err(_) => {
2171            let curve = interpolate_curve(points, 1, parameters).or_refuse(KernelStage::Refine, "interpolate_curve")?;
2172            let [t0, t1] = curve.domain().or_refuse(KernelStage::Refine, "domain")?;
2173            Ok((curve, t0, t1))
2174        }
2175    }
2176}
2177
2178fn affine_image_trusted() -> bool {
2179    switch!("BREP_CARRIER_AFFINE_IMAGE", true)
2180}
2181
2182
2183/// Avoid constructing the fallback trace when the exact image will be used.
2184/// Positive, bounded control nets on well-separated knots make every skipped
2185/// in-domain evaluation total. The collapse witness is an actual vertex of
2186/// the old adaptive trace: its last point, or its midpoint when the first
2187/// subdivision fires. Near-collapsed images and uncertified inputs stay eager.
2188/// `BREP_OFFSET_LAZY_CARRIER_TRACE`; on by default since
2189/// 2026-11-02 (lane O batch 21), `0`/`off` restores the eager trace.
2190fn lazy_carrier_trace(
2191    surface: &NurbsSurface,
2192    pcurve: &NurbsCurve,
2193    collapse_tolerance: f64,
2194    fit_tolerance: f64,
2195) -> Option<(Vec<Vec3>, (NurbsCurve, f64, f64))> {
2196    if !switch!("BREP_OFFSET_LAZY_CARRIER_TRACE", true) {
2197        return None;
2198    }
2199    // A straight image costs only the initial five samples. Do not spend
2200    // certificate/ladder setup on those small traces.
2201    if pcurve.degree == 1 && pcurve.control_points.len() == 2 {
2202        let (a, b) = (pcurve.control_points[0], pcurve.control_points[1]);
2203        if (surface.degree_u <= 1 && surface.degree_v <= 1)
2204            || (a.w == b.w && a.y == b.y && surface.degree_u <= 1)
2205            || (a.w == b.w && a.x == b.x && surface.degree_v <= 1)
2206        {
2207            return None;
2208        }
2209    }
2210    fn knots_ok(knots: &[f64], degree: usize, count: usize) -> bool {
2211        degree <= 3 && count <= 64
2212            && KnotVector::new(knots.to_vec(), degree).ok().is_some_and(|k| k.control_point_count() == count)
2213            && knots.iter().all(|k| k.is_finite() && k.abs() <= 1e6)
2214            && knots.windows(2).all(|k| k[0] == k[1] || k[1] - k[0] >= 1e-6)
2215    }
2216    let bounded = |p: &Vec4| (1e-3..=1e3).contains(&p.w)
2217        && [p.x, p.y, p.z].iter().all(|x| x.is_finite() && x.abs() <= 1e12);
2218    let columns = surface.control_points.first()?.len();
2219    if !knots_ok(&surface.knots_u, surface.degree_u, surface.control_points.len())
2220        || !knots_ok(&surface.knots_v, surface.degree_v, columns)
2221        || surface.control_points.iter().any(|row| row.len() != columns || row.iter().any(|p| !bounded(p)))
2222        || !knots_ok(&pcurve.knots, pcurve.degree, pcurve.control_points.len())
2223        || pcurve.control_points.iter().any(|p| !bounded(p))
2224    {
2225        return None;
2226    }
2227    let ([u0, u1], [v0, v1]) = (surface.domain_u().ok()?, surface.domain_v().ok()?);
2228    let uv = pcurve.control_points.iter().map(|p| p.point().ok()).collect::<Option<Vec<_>>>()?;
2229    if uv.iter().any(|p| p.x < u0 || p.x > u1 || p.y < v0 || p.y > v1) {
2230        return None;
2231    }
2232    // Only the ladder's exact tiers: a fitted image is not worth speculating.
2233    if !surface.is_affine().ok()? && !uv.iter().all(|p| p.x == uv[0].x) && !uv.iter().all(|p| p.y == uv[0].y) {
2234        return None;
2235    }
2236    let [start, end] = pcurve.domain().ok()?;
2237    let evaluate = |fraction: f64| {
2238        let uv = pcurve.evaluate(start + (end - start) * fraction).ok()?;
2239        surface.evaluate(uv.x, uv.y).ok()
2240    };
2241    let first = evaluate(0.0)?;
2242    let last = evaluate(1.0)?;
2243    let samples = [evaluate(0.25)?, evaluate(0.5)?, evaluate(0.75)?];
2244    let deviation = samples.iter().enumerate().map(|(index, point)| {
2245        point.sub(first.add(last.sub(first).scale((index + 1) as f64 * 0.25))).length()
2246    }).fold(0.0, f64::max);
2247    if last.sub(first).length() <= collapse_tolerance
2248        && !(deviation > 5e-4 && samples[1].sub(first).length() > collapse_tolerance)
2249    {
2250        return None;
2251    }
2252    // Empty fallback data deliberately declines a refused image; the caller
2253    // then takes the old trace and preserves its refusal and interpolation.
2254    let image = carrier_edge_curve(surface, pcurve, &[], &[], fit_tolerance).ok()?;
2255    Some((vec![first, samples[1], last], image))
2256}
2257
2258fn mapped_pcurve_polyline(
2259    surface: &NurbsSurface,
2260    pcurve: &NurbsCurve,
2261    degenerate: bool,
2262) -> Result<(Vec<Vec3>, Vec<f64>), KernelRefusal> {
2263    let [start, end] = pcurve.domain().or_refuse(KernelStage::Refine, "domain")?;
2264    let evaluate = |fraction: f64| -> Result<Vec3, KernelRefusal> {
2265        let uv = pcurve.evaluate(start + (end - start) * fraction).or_refuse(KernelStage::Refine, "evaluate")?;
2266        surface.evaluate(uv.x, uv.y).or_refuse(KernelStage::Refine, "evaluate")
2267    };
2268    let first = evaluate(0.0)?;
2269    let last = evaluate(1.0)?;
2270    if degenerate {
2271        return Ok((vec![first, last], vec![0.0, 1.0]));
2272    }
2273    fn append(
2274        evaluate: &impl Fn(f64) -> Result<Vec3, KernelRefusal>,
2275        a_fraction: f64,
2276        a: Vec3,
2277        b_fraction: f64,
2278        b: Vec3,
2279        depth: usize,
2280        parameters: &mut Vec<f64>,
2281        points: &mut Vec<Vec3>,
2282    ) -> Result<(), KernelRefusal> {
2283        let fractions =
2284            [0.25, 0.5, 0.75].map(|local| a_fraction + (b_fraction - a_fraction) * local);
2285        let samples = fractions
2286            .map(evaluate)
2287            .into_iter()
2288            .collect::<Result<Vec<_>, KernelRefusal>>()?;
2289        let deviation = samples
2290            .iter()
2291            .enumerate()
2292            .map(|(index, point)| {
2293                point
2294                    .sub(a.add(b.sub(a).scale((index + 1) as f64 * 0.25)))
2295                    .length()
2296            })
2297            .fold(0.0, f64::max);
2298        if deviation <= 5e-4 || depth >= 10 {
2299            parameters.push(b_fraction);
2300            points.push(b);
2301            return Ok(());
2302        }
2303        append(
2304            evaluate,
2305            a_fraction,
2306            a,
2307            fractions[1],
2308            samples[1],
2309            depth + 1,
2310            parameters,
2311            points,
2312        )?;
2313        append(
2314            evaluate,
2315            fractions[1],
2316            samples[1],
2317            b_fraction,
2318            b,
2319            depth + 1,
2320            parameters,
2321            points,
2322        )
2323    }
2324    let mut parameters = vec![0.0];
2325    let mut points = vec![first];
2326    append(
2327        &evaluate,
2328        0.0,
2329        first,
2330        1.0,
2331        last,
2332        0,
2333        &mut parameters,
2334        &mut points,
2335    )?;
2336    Ok((points, parameters))
2337}
2338
2339/// Everything an offset carrier's construction MEASURED about itself.
2340///
2341/// The half this kernel lacked: a tolerance MEASURED from the geometry that was
2342/// actually built, rather than a band chosen from `scale` before building. It
2343/// lives in the one place that needs no durable-format change: alongside the
2344/// transient construction result, never as a field on a [`crate::BrepSolid`]
2345/// record. `io/snapshot.rs` is a documented durable format and
2346/// `SOLID_CODEC_VERSION` a versioned wire layout; persisting per-entity
2347/// tolerances is real, planned, and separately designed. This lands the
2348/// measurement with no format churn at all.
2349///
2350/// Every number here is a RECORD. None of it widens a band — see
2351/// [`MeasuredTolerance`]'s direction rule.
2352#[derive(Clone, Debug)]
2353pub struct CarrierDeviation {
2354    /// The derived band every measurement below was judged against:
2355    /// [`crate::offset_construction_band`] of the source solid's extent.
2356    pub band: f64,
2357    /// Which branch built the carrier surface.
2358    pub lane: OffsetSurfaceLane,
2359    /// The carrier surface's own fit error, when the lane has one.
2360    pub surface: Option<MeasuredTolerance>,
2361    /// `max_t ‖C_3d(t) − S_off(p(t))‖` per carrier edge id, folded over every
2362    /// coedge that references the edge — OCCT's `FillEdgeData` rule
2363    /// (`BRepOffset_SimpleOffset.cxx:296-310`), which takes the maximum over
2364    /// **every** adjacent face rather than the first one.
2365    pub edges: Vec<(u64, MeasuredTolerance)>,
2366    /// Per carrier vertex id, propagated from the incident edge ends by
2367    /// [`crate::vertex_tolerance_from_edges`] — which is also where the verdict
2368    /// on OCCT's 1.001 inflation factor is recorded.
2369    pub vertices: Vec<(u64, f64)>,
2370}
2371
2372impl CarrierDeviation {
2373    /// The worst thing the construction did, against the tightest band it
2374    /// faced. `None` only when there was nothing at all to measure.
2375    pub fn worst(&self) -> Option<MeasuredTolerance> {
2376        MeasuredTolerance::worst(
2377            self.surface
2378                .into_iter()
2379                .chain(self.edges.iter().map(|(_, measured)| *measured))
2380                .chain(
2381                    self.vertices
2382                        .iter()
2383                        .map(|(_, gap)| MeasuredTolerance::new(*gap, self.band)),
2384                ),
2385        )
2386    }
2387
2388    /// The entities whose measured deviation exceeded the derived band — the
2389    /// interesting case, and the only one any gate acts on.
2390    pub fn exceedances(&self) -> Vec<String> {
2391        let mut out = Vec::new();
2392        if let Some(surface) = self.surface {
2393            if surface.exceeds_band() {
2394                out.push(format!("carrier surface fit {}", surface.describe()));
2395            }
2396        }
2397        for (id, measured) in &self.edges {
2398            if measured.exceeds_band() {
2399                out.push(format!("edge {id} {}", measured.describe()));
2400            }
2401        }
2402        for (id, gap) in &self.vertices {
2403            let measured = MeasuredTolerance::new(*gap, self.band);
2404            if measured.exceeds_band() {
2405                out.push(format!("vertex {id} {}", measured.describe()));
2406            }
2407        }
2408        out
2409    }
2410}
2411
2412#[derive(Clone, Debug, Serialize)]
2413pub struct OffsetFaceCarrier {
2414    pub vertices: Vec<VertexRecord>,
2415    pub edges: Vec<EdgeRecord>,
2416    pub face: FaceRecord,
2417    /// What the construction measured about itself, or `None` when it was built
2418    /// through the unmeasured [`offset_face_carrier`] entry point.
2419    ///
2420    /// `#[serde(skip)]` on purpose: this struct crosses the wasm ABI as JSON
2421    /// (`abi/modeling_b.rs:500`), and a measurement is a diagnostic about a
2422    /// build, not part of the carrier the caller asked for. Skipping it keeps
2423    /// that payload byte-identical.
2424    #[serde(skip)]
2425    pub deviation: Option<CarrierDeviation>,
2426    /// Which branch built the carrier surface (always known; `deviation`
2427    /// carries it too, but only on the measured entry point). Skipped on the
2428    /// ABI for the same reason as `deviation`.
2429    #[serde(skip)]
2430    pub lane: OffsetSurfaceLane,
2431}
2432
2433/// The carrier vertex imaging `source_id` at `point`. A source vertex
2434/// normally has ONE image, claimed by the first edge to reach it. An APEX
2435/// does not: the offset of a corner blend's apex (a partial surface of
2436/// revolution about a reflex edge, its degenerate edge spanning the turn) is
2437/// an open ARC of radius |d|, and the two meridian edges into the apex end at
2438/// the arc's two ends. Handing every edge the first image collapsed that arc
2439/// onto one vertex while its curve still ran 1.7·d to the other end — a loop
2440/// that closes in ids and not in space, which the assembly could only leave
2441/// open (the 2026-10-18 shell report). An image farther than `split` from
2442/// every image already claimed is a vertex of its own.
2443fn claim_vertex_image(
2444    source_id: u64,
2445    point: Vec3,
2446    split: f64,
2447    vertex_images: &mut HashMap<u64, Vec<u64>>,
2448    vertices: &mut Vec<VertexRecord>,
2449    next_id: &mut u64,
2450) -> u64 {
2451    if let Some(ids) = vertex_images.get(&source_id) {
2452        let near = ids.iter().copied().find(|id| {
2453            vertices
2454                .iter()
2455                .find(|vertex| vertex.id == *id)
2456                .is_some_and(|vertex| vertex.point.sub(point).length() <= split)
2457        });
2458        if let Some(id) = near {
2459            return id;
2460        }
2461    }
2462    let id = *next_id;
2463    *next_id += 1;
2464    vertices.push(VertexRecord { id, point });
2465    vertex_images.entry(source_id).or_default().push(id);
2466    id
2467}
2468
2469pub fn offset_face_carrier(
2470    solid: &BrepSolid,
2471    face_id: u64,
2472    distance: f64,
2473    planar_extension: f64,
2474) -> Result<OffsetFaceCarrier, KernelRefusal> {
2475    offset_face_carrier_impl(
2476        solid,
2477        face_id,
2478        distance,
2479        &CarrierExtension::uniform(planar_extension),
2480        false,
2481    )
2482}
2483
2484/// [`offset_face_carrier`] with a per-side [`CarrierExtension`].
2485pub fn offset_face_carrier_sided(
2486    solid: &BrepSolid,
2487    face_id: u64,
2488    distance: f64,
2489    extension: &CarrierExtension,
2490) -> Result<OffsetFaceCarrier, KernelRefusal> {
2491    offset_face_carrier_impl(solid, face_id, distance, extension, false)
2492}
2493
2494/// [`offset_face_carrier`], with every entity it builds measured against the
2495/// deviation it was meant to reproduce.
2496///
2497/// The carrier is bit-identical to [`offset_face_carrier`]'s — same body, same
2498/// arithmetic, in the same order — with a read-only measurement pass appended.
2499/// Three quantities land in [`CarrierDeviation`]:
2500///
2501/// * the carrier SURFACE's fit against the pointwise offset it interpolates
2502///   ([`offset_surface_measured`]);
2503/// * each carrier EDGE's 3D curve against the locus its pcurve traces on that
2504///   surface — the trim boundary is built by sampling exactly that composition
2505///   and interpolating the images, so this is the construction's own claim,
2506///   measured rather than assumed;
2507/// * each carrier VERTEX, propagated from the incident edge ends.
2508///
2509/// The interesting output is [`CarrierDeviation::exceedances`]: an entity whose
2510/// measured deviation is worse than the size-derived band assumed. That is a
2511/// construction that went wrong in a way the derived band alone cannot see, and
2512/// it is the case a caller should refuse on rather than ship.
2513pub fn offset_face_carrier_measured(
2514    solid: &BrepSolid,
2515    face_id: u64,
2516    distance: f64,
2517    planar_extension: f64,
2518) -> Result<OffsetFaceCarrier, KernelRefusal> {
2519    offset_face_carrier_impl(
2520        solid,
2521        face_id,
2522        distance,
2523        &CarrierExtension::uniform(planar_extension),
2524        true,
2525    )
2526}
2527
2528/// The measured deviations of one built carrier: surface fit, per edge, per
2529/// vertex.
2530///
2531/// Read-only over what the construction produced. The edge measurement is
2532/// [`crate::measure_edge_against_pcurve_image`] — validate's own
2533/// `adaptive_coedge_error` floored by a span-midpoint pass, because the sampler
2534/// alone is aliased against exactly the curves this construction builds (see
2535/// that module's doc) — taken against the far tighter
2536/// [`crate::offset_construction_band`] instead of the vendor-forgiving
2537/// `pcurve_acceptance` validate will use later.
2538fn measure_carrier(
2539    surface: &NurbsSurface,
2540    vertices: &[VertexRecord],
2541    edges: &[EdgeRecord],
2542    loops: &[LoopRecord],
2543    lane: OffsetSurfaceLane,
2544    surface_fit: Option<MeasuredTolerance>,
2545    band: f64,
2546) -> Result<CarrierDeviation, KernelRefusal> {
2547    let edge_by_id: HashMap<u64, &EdgeRecord> = edges.iter().map(|edge| (edge.id, edge)).collect();
2548    // Fold over coedges, not edges: a seam edge is referenced twice with two
2549    // different pcurves, and OCCT's `FillEdgeData` takes the maximum over every
2550    // adjacent face for exactly that reason.
2551    let mut per_edge: HashMap<u64, MeasuredTolerance> = HashMap::default();
2552    for loop_record in loops {
2553        for coedge in &loop_record.coedges {
2554            let Some(edge) = edge_by_id.get(&coedge.edge_id) else {
2555                continue;
2556            };
2557            let measured = measure_edge_against_pcurve_image(
2558                surface,
2559                &coedge.pcurve,
2560                edge,
2561                coedge.forward,
2562                band,
2563            )?;
2564            per_edge
2565                .entry(edge.id)
2566                .and_modify(|existing| *existing = existing.worse_of(measured))
2567                .or_insert(measured);
2568        }
2569    }
2570
2571    let mut measured_edges: Vec<(u64, MeasuredTolerance)> = per_edge.into_iter().collect();
2572    measured_edges.sort_by_key(|(id, _)| *id);
2573    let deviation_of: HashMap<u64, f64> = measured_edges
2574        .iter()
2575        .map(|(id, measured)| (*id, measured.deviation()))
2576        .collect();
2577
2578    // Edge ENDS by the vertex they claim, built once. A degenerate edge claims
2579    // the same vertex at both ends and contributes both, which is right: the
2580    // question is how far every representation meeting there actually lands.
2581    let mut ends_at: HashMap<u64, Vec<(&EdgeRecord, f64)>> = HashMap::default();
2582    for edge in edges {
2583        ends_at
2584            .entry(edge.start_vertex_id)
2585            .or_default()
2586            .push((edge, edge.t0));
2587        ends_at
2588            .entry(edge.end_vertex_id)
2589            .or_default()
2590            .push((edge, edge.t1));
2591    }
2592
2593    let mut measured_vertices = Vec::with_capacity(vertices.len());
2594    for vertex in vertices {
2595        let mut gaps = Vec::new();
2596        let mut incident = Vec::new();
2597        for (edge, parameter) in ends_at.get(&vertex.id).into_iter().flatten() {
2598            gaps.push(vertex_endpoint_gap(
2599                vertex.point,
2600                edge.curve.evaluate(*parameter).or_refuse(KernelStage::Refine, "evaluate")?,
2601            ));
2602            incident.push(deviation_of.get(&edge.id).copied().unwrap_or(0.0));
2603        }
2604        measured_vertices.push((vertex.id, vertex_tolerance_from_edges(gaps, incident)));
2605    }
2606
2607    Ok(CarrierDeviation {
2608        band,
2609        lane,
2610        surface: surface_fit,
2611        edges: measured_edges,
2612        vertices: measured_vertices,
2613    })
2614}
2615
2616fn offset_face_carrier_impl(
2617    solid: &BrepSolid,
2618    face_id: u64,
2619    distance: f64,
2620    extension: &CarrierExtension,
2621    measure: bool,
2622) -> Result<OffsetFaceCarrier, KernelRefusal> {
2623    let source = solid
2624        .shells
2625        .iter()
2626        .flat_map(|shell| &shell.faces)
2627        .find(|face| face.id == face_id)
2628        .ok_or_else(|| format!("offset_face_carrier: missing face {face_id}")).or_refuse(KernelStage::Refine, "offset_missing_face")?;
2629    // The band is derived from the SOURCE SOLID's extent, matching every other
2630    // direct-edit/offset site (`face_offset.rs:103` and its siblings all take
2631    // `solid_model_scale`). The alternative basis is the FACE's own extent — the
2632    // reference shell keys its size-relative offset quantities on the 3D arc
2633    // length of the face's own boundary iso, never on the assembly. Which basis
2634    // is better is unsettled: it is a measurement to make over the refusal
2635    // corpus before switching, not a change to smuggle in here.
2636    let band = offset_construction_band(solid_model_scale(solid));
2637    // The accuracy bar a carrier EDGE's 3D curve must meet against the locus its
2638    // pcurve traces on the carrier surface — the kernel's own SSI fit contract,
2639    // the same bar `thicken` holds its wall boundaries to. NOT the construction
2640    // band: that is the surface-fit allowance (5e-4 of the part), 50x looser
2641    // than the boolean's coincidence band, and an edge fitted only that well is
2642    // what a later imprint mistakes for a crossing (see `carrier_edge_curve`).
2643    let fit_tolerance =
2644        crate::KernelTolerances::for_scale(solid_model_scale(solid), 1e-7).intersection_fit;
2645    let (surface, lane, surface_fit) = if measure {
2646        let measured = offset_surface_measured_sided(source, distance, extension, band)?;
2647        (measured.surface, measured.lane, measured.fit)
2648    } else {
2649        let (surface, lane) = offset_surface_with_lane(source, distance, extension)?;
2650        (surface, lane, None)
2651    };
2652    let source_edges = solid
2653        .edges
2654        .iter()
2655        .map(|edge| (edge.id, edge))
2656        .collect::<HashMap<_, _>>();
2657    let source_vertices = solid
2658        .vertices
2659        .iter()
2660        .map(|vertex| (vertex.id, vertex))
2661        .collect::<HashMap<_, _>>();
2662    let mut vertices = Vec::new();
2663    let mut vertex_images = HashMap::default();
2664    // Two images of one source vertex this far apart are two vertices (an
2665    // apex's arc, `claim_vertex_image`); fitting noise at a pole stays far
2666    // below it.
2667    let vertex_split = (distance.abs() * 0.1).max(band);
2668    let mut edges = Vec::new();
2669    let mut edge_images = HashMap::default();
2670    let mut loops = Vec::new();
2671    let mut next_id = 1u64;
2672
2673    for source_loop in &source.loops {
2674        let mut coedges = Vec::new();
2675        for source_coedge in &source_loop.coedges {
2676            let source_edge = source_edges
2677                .get(&source_coedge.edge_id)
2678                .ok_or_else(|| "offset_face_carrier: missing source edge".to_string()).or_refuse(KernelStage::Refine, "offset_missing_edge")?;
2679            let (source_start, source_end) = if source_coedge.forward {
2680                (source_edge.start_vertex_id, source_edge.end_vertex_id)
2681            } else {
2682                (source_edge.end_vertex_id, source_edge.start_vertex_id)
2683            };
2684            if !source_vertices.contains_key(&source_start)
2685                || !source_vertices.contains_key(&source_end)
2686            {
2687                return Err(KernelRefusal::internal(KernelStage::Refine, "offset_missing_vertex", "offset_face_carrier: missing source vertex"));
2688            }
2689            // Map even DEGENERATE source edges through the full polyline: a
2690            // cone apex's image on the offset surface is a genuine CIRCLE
2691            // (radius d·cos half-angle), not a point — shortcutting to the
2692            // two endpoints would collapse the ring and leave the carrier's
2693            // topology inconsistent with its surface. Edges whose image truly
2694            // collapses (sphere poles, planar corners) still interpolate to a
2695            // point-sized curve and keep their degenerate flag below.
2696            // Map even DEGENERATE source edges through the full polyline: an
2697            // EXTERIOR cone offset turns the apex point into a genuine RING
2698            // (radius d·cos half-angle) — shortcutting to the endpoints would
2699            // collapse it and leave the carrier topology inconsistent with
2700            // its surface (and the ring imprint would be dropped as
2701            // boundary-coincident with a "degenerate" edge). Images that
2702            // truly collapse (sphere poles; interior apexes after the pinch
2703            // retrim) stay degenerate below. The threshold scales with the
2704            // offset distance: a real ring measures ~d·cos α, while fitted
2705            // pole rows wobble ~1e-4 absolute.
2706            let collapse_tolerance = 1e-6f64.max(distance.abs() * 1e-2);
2707            let (points, parameters, exact_image) = match lazy_carrier_trace(
2708                &surface, &source_coedge.pcurve, collapse_tolerance, fit_tolerance,
2709            ) {
2710                Some((points, image)) => (points, Vec::new(), Some(image)),
2711                None => {
2712                    let (points, parameters) = mapped_pcurve_polyline(&surface, &source_coedge.pcurve, false)?;
2713                    (points, parameters, None)
2714                }
2715            };
2716            let image_collapsed = points
2717                .iter()
2718                .all(|point| point.sub(points[0]).length() <= collapse_tolerance);
2719            let (edge_id, forward) =
2720                if let Some((edge_id, edge_start_vertex_id, creator_forward)) =
2721                    edge_images.get(&source_edge.id)
2722                {
2723                    if source_start == source_end {
2724                        // A CLOSED source edge (a seam of a periodic face: both
2725                        // ends are the same vertex) is referenced twice by the
2726                        // same loop, once per seam side, and the two references
2727                        // traverse it in OPPOSITE senses — that is what closes
2728                        // the loop. The endpoint test below cannot see that:
2729                        // both ends map to the same vertex image, so it answers
2730                        // `true` for both references and the returning coedge
2731                        // comes back mis-oriented.
2732                        //
2733                        // Measured on a full torus (one vertex, two seam edges):
2734                        // the source records `forward: false` on the two
2735                        // returning coedges and validates clean, while the
2736                        // carrier recorded `forward: true` on both and its rim
2737                        // edge measured 12.5 — a whole part diameter — against
2738                        // its own composed pcurve image. The measured tolerance
2739                        // this slice adds is what surfaced it; no existing gate
2740                        // reaches this shape.
2741                        //
2742                        // The source coedge's own sense is the answer: the image
2743                        // edge runs along the CREATING coedge's pcurve, so a
2744                        // later reference runs with it exactly when the two
2745                        // source coedges traverse the source edge the same way.
2746                        (*edge_id, source_coedge.forward == *creator_forward)
2747                    } else {
2748                        // UNCHANGED for every open edge: the image edge's start
2749                        // vertex identifies which way this coedge runs.
2750                        (
2751                            *edge_id,
2752                            vertex_images
2753                                .get(&source_start)
2754                                .is_some_and(|ids: &Vec<u64>| ids.contains(edge_start_vertex_id)),
2755                        )
2756                    }
2757                } else {
2758                    let (curve, t0, t1) = if image_collapsed {
2759                        let curve = NurbsCurve::new(
2760                            1,
2761                            vec![0.0, 0.0, 1.0, 1.0],
2762                            vec![
2763                                Vec4::from_point(points[0], 1.0),
2764                                Vec4::from_point(points[0], 1.0),
2765                            ],
2766                        ).or_refuse(KernelStage::Refine, "new")?;
2767                        (curve, 0.0, 1.0)
2768                    } else if let Some(image) = exact_image {
2769                        image
2770                    } else {
2771                        carrier_edge_curve(
2772                            &surface,
2773                            &source_coedge.pcurve,
2774                            &points,
2775                            &parameters,
2776                            fit_tolerance,
2777                        )?
2778                    };
2779                    let start_vertex_id = claim_vertex_image(
2780                        source_start,
2781                        points[0],
2782                        vertex_split,
2783                        &mut vertex_images,
2784                        &mut vertices,
2785                        &mut next_id,
2786                    );
2787                    let end_vertex_id = claim_vertex_image(
2788                        source_end,
2789                        points[points.len() - 1],
2790                        vertex_split,
2791                        &mut vertex_images,
2792                        &mut vertices,
2793                        &mut next_id,
2794                    );
2795                    let id = next_id;
2796                    next_id += 1;
2797                    edges.push(EdgeRecord {
2798                        id,
2799                        curve,
2800                        t0,
2801                        t1,
2802                        start_vertex_id,
2803                        end_vertex_id,
2804                        // Degenerate exactly when the IMAGE collapsed. A cone
2805                        // apex maps to a real RING on an exterior offset and
2806                        // must carry a real closed edge; a real source edge
2807                        // whose image collapses to a point is a degenerate edge
2808                        // ON THE CARRIER whatever it was on the source.
2809                        //
2810                        // That second half is what the CARVE needs. The new
2811                        // boundary a carve introduces is a piece of the fold
2812                        // locus, which on the source is an ordinary curve — a
2813                        // parallel circle on a cone — and whose whole defining
2814                        // property is that the offset collapses there. Reading
2815                        // the source's flag as well left it a zero-length
2816                        // NON-degenerate closed edge, which no weld can use.
2817                        degenerate: image_collapsed,
2818                        // Image of a named source edge on the offset carrier;
2819                        // suffixed so it cannot collide with the source edge
2820                        // when both faces survive into one solid.
2821                        name: source_edge
2822                            .name
2823                            .as_ref()
2824                            .map(|name| format!("{name}_Offset")),
2825                    });
2826                    edge_images.insert(
2827                        source_edge.id,
2828                        (id, start_vertex_id, source_coedge.forward),
2829                    );
2830                    (id, true)
2831                };
2832            let id = next_id;
2833            next_id += 1;
2834            coedges.push(CoedgeRecord {
2835                id,
2836                edge_id,
2837                forward,
2838                pcurve: source_coedge.pcurve.clone(),
2839            });
2840        }
2841        let id = next_id;
2842        next_id += 1;
2843        loops.push(LoopRecord { id, coedges });
2844    }
2845    let deviation = if measure {
2846        Some(measure_carrier(
2847            &surface,
2848            &vertices,
2849            &edges,
2850            &loops,
2851            lane,
2852            surface_fit,
2853            band,
2854        )?)
2855    } else {
2856        None
2857    };
2858    Ok(OffsetFaceCarrier {
2859        vertices,
2860        edges,
2861        face: FaceRecord {
2862            id: next_id,
2863            surface,
2864            same_sense: source.same_sense,
2865            loops,
2866            name: source.name.as_ref().map(|name| format!("{name}_Offset")),
2867        },
2868        deviation,
2869        lane,
2870    })
2871}
2872
2873
2874
2875