Skip to main content

brep_kernel/offset/offset_shell/
pipeline.rs

1use crate::{KernelRefusal, KernelStage, OrRefuse};
2use super::*;
3
4/// Put every face on one normal convention (surface normal on the CCW
5/// parameter-winding side) and then restore outward normals. The offset shell's
6/// source and offset skins are assembled as independent components whose
7/// per-face `same_sense` can follow opposite conventions; `validate()` only
8/// checks coedge-direction coherence (the `forward` flags), so a joined,
9/// watertight, coedge-coherent manifold can still be normal-incoherent and
10/// report the wrong volume. Re-deriving `same_sense` from the loop winding makes
11/// the coedge-coherent manifold normal-coherent; the signed-volume flip then
12/// orients it outward. Only invoked after a non-coplanar rim weld, so the
13/// coplanar cylinder/prism shells keep their exact assembled orientation.
14fn coheres_face_normals(solid: &mut BrepSolid) -> Result<(), KernelRefusal> {
15    for face in solid.shells.iter_mut().flat_map(|shell| &mut shell.faces) {
16        face.same_sense = parameter_space_area(face).or_refuse(KernelStage::Sew, "parameter_space_area")? > 0.0;
17    }
18    // Outward is a PER-SHELL property: a through-hole shelled at both ends
19    // legitimately produces two disjoint closed components (the outer wall
20    // tube and the hole wall tube), and a single global signed-volume flip
21    // cannot orient both.
22    for shell_index in 0..solid.shells.len() {
23        let single = BrepSolid {
24            mass_properties_cache: Default::default(),
25            id: solid.id,
26            vertices: solid.vertices.clone(),
27            edges: solid.edges.clone(),
28            shells: vec![solid.shells[shell_index].clone()],
29            genus: 0,
30        };
31        if crate::solid_signed_volume(&single).or_refuse(KernelStage::Sew, "solid_signed_volume")? < 0.0 {
32            flip_shell_faces(&mut solid.shells[shell_index])?;
33        }
34    }
35    Ok(())
36}
37
38/// Mint a vertex at every SELF-TOUCH of the source's faces before the shell
39/// reads their trims.
40///
41/// A face whose loops touch at an EDGE INTERIOR is a pinched region: the
42/// reported 2026-09-09 document's r = 6 through-hole kisses both r = 4 fillet
43/// setbacks on the top face, at (16, 20, 10) and (10, 20, 16), with no vertex
44/// there.  The fragment stage's 2D arrangement resolves such a pinch by
45/// carving the touched-off corner into its own region, whose loop walks the
46/// closed hole chain in a PARTIAL run — which `build_loop` can only assemble
47/// when the touch is a vertex of both touching edges, and otherwise refuses
48/// with "chain N fragmented into an incomplete run".
49///
50/// The boolean lane mints that vertex inside `build_imprints`
51/// (`imprint/self_touch.rs`).  Nothing minted it here: the shell fragments
52/// its SOURCE against an EMPTY imprint, and its offset carriers — whose trims
53/// are copied from the source faces — deliberately keep their boundaries
54/// un-split, so both arrived at the arrangement pinched.  Splitting the
55/// source once, before the carriers are built from it, fixes both: every
56/// carrier inherits the split trim by construction.
57///
58/// Returns `None` when no face self-touches, so every part that was never
59/// pinched keeps its exact previous path.  Escape hatch:
60/// `BREP_SELF_TOUCH_SPLIT=0`, the same one the imprint lane honours.
61fn split_self_touching_source(source: &BrepSolid) -> Result<Option<BrepSolid>, KernelRefusal> {
62    if std::env::var("BREP_SELF_TOUCH_SPLIT").as_deref() == Ok("0") {
63        return Ok(None);
64    }
65    // The same derivation `offset_shell_impl` uses for every other band, so
66    // the source scan and the carrier-pair scans agree on what "touching"
67    // means.
68    let scale = crate::solid_scale(source);
69    let tolerance = 1e-7f64.max(scale * 1e-9);
70    let edge_splits = crate::imprint::self_touch_edge_splits(source, SOURCE_OPERAND, tolerance, scale)?;
71    if edge_splits.is_empty() {
72        return Ok(None);
73    }
74    os_debug!(
75        "self-touch: source split on {} edge(s): {:?}",
76        edge_splits.len(),
77        edge_splits
78            .iter()
79            .map(|split| split.edge_id)
80            .collect::<Vec<_>>(),
81    );
82    let imprint = ImprintResultRecord {
83        edge_splits,
84        ..empty_imprint()
85    };
86    Ok(Some(apply_edge_splits(source, SOURCE_OPERAND, &imprint)?))
87}
88
89/// How far past the grown outline a planar opening wall that meets a RULED
90/// opening is padded, in units of the offset-skin band the wall selection reads
91/// (see the wall construction).
92///
93/// Measured on the torus wedge through the pipeline, grown by 0.05, 0.1, 0.5
94/// and 0.999 (band 2e-3): a clearance of 2 or 2.5 bands still shreds the cap
95/// into 22 to 36 faces with the area 5% to 11% high; from 4 bands on every
96/// member builds its 6 faces, and from 4 to 32 bands each member's volume moves
97/// by at most 1.2e-7. 16 sits four times above the first clean reading.
98const RULED_NEIGHBOUR_CLEARANCE_BANDS: f64 = 16.0;
99
100/// How far past its opening plane an inward carrier is grown, as a fraction of
101/// the shell distance (see the fall-short reach in the carrier loop).
102const REACH_CLEARANCE: f64 = 0.25;
103
104pub fn offset_shell(
105    source: &BrepSolid,
106    opening_face_ids: &[u64],
107    distance: f64,
108) -> Result<OffsetShellResultRecord, KernelRefusal> {
109    let result = offset_shell_any_lane(source, opening_face_ids, distance)?;
110    outward_shell_is_connected(source, distance, &result)?;
111    Ok(result)
112}
113
114/// Every shell of an OUTWARD result must bound volume. The shell is the grown
115/// body less the source with the openings held, so it can be disconnected
116/// (opening a cylinder's lateral face leaves its two grown caps as two
117/// slabs), but each of its shells encloses material. A shell that encloses
118/// none, a lone face that happened to close, is a fragment of an assembly that
119/// went wrong and still validated. Measured on 2026-09-26: a free-form D whose
120/// fitted opening wall sat within the fit tolerance of its carriers' sections
121/// returned its body plus 62 single-face shells of volume under 3e-24, each
122/// valid, with the right total volume. So this refuses by name for EVERY
123/// outward shell, not only for the lane that produced those.
124pub(super) fn outward_shell_is_connected(
125    source: &BrepSolid,
126    distance: f64,
127    result: &OffsetShellResultRecord,
128) -> Result<(), KernelRefusal> {
129    if distance >= 0.0 || result.solid.shells.len() <= 1 {
130        return Ok(());
131    }
132    let scale = crate::solid_scale(source).max(1.0);
133    let volumes = result
134        .solid
135        .shells
136        .iter()
137        .map(|shell| {
138            let mut one = result.solid.clone();
139            one.shells = vec![shell.clone()];
140            crate::solid_mass_properties(&one)
141                .map(|properties| properties.volume)
142                .or_refuse(KernelStage::Validate, "solid_mass_properties")
143        })
144        .collect::<Result<Vec<f64>, KernelRefusal>>()?;
145    let total = volumes.iter().map(|volume| volume.abs()).sum::<f64>();
146    let floor = (1e-9 * total).max(1e-12 * scale * scale * scale);
147    let empty = volumes.iter().filter(|volume| volume.abs() <= floor).count();
148    if empty == 0 {
149        return Ok(());
150    }
151    Err(refusals::empty_shell(result.solid.shells.len(), empty, floor).into())
152}
153
154fn offset_shell_any_lane(
155    source: &BrepSolid,
156    opening_face_ids: &[u64],
157    distance: f64,
158) -> Result<OffsetShellResultRecord, KernelRefusal> {
159    // Pinched source faces are un-modellable by the arrangement until the
160    // touch is a vertex; do it once here so BOTH `offset_shell_impl` attempts
161    // and every carrier built from the source see the same topology.
162    let prepared = split_self_touching_source(source)?;
163    let source = prepared.as_ref().unwrap_or(source);
164    // An outward shell of the one circular face of a partial revolve is planar
165    // in the meridian half-plane, fold band and all: build it there and revolve
166    // it once (`revolved_wedge.rs`). Anything else declines and takes the
167    // imprint pipeline, unchanged.
168    match revolved_wedge::recognize(source, opening_face_ids, distance) {
169        Ok(revolved_wedge::Recognition::Wedge(wedge)) => {
170            os_debug!("revolved wedge lane: {}", wedge.report());
171            return revolved_wedge::build(&wedge, source);
172        }
173        Ok(revolved_wedge::Recognition::Declined(reason)) => {
174            os_debug!("revolved wedge lane declined: {reason}");
175        }
176        // A source the lane cannot even read is not one it takes; the pipeline
177        // owns every refusal.
178        Err(error) => {
179            os_debug!("revolved wedge lane declined: could not read the source ({error})");
180        }
181    }
182    imprint_attempts(source, opening_face_ids, distance)
183}
184
185
186fn imprint_attempts(
187    source: &BrepSolid,
188    opening_face_ids: &[u64],
189    distance: f64,
190) -> Result<OffsetShellResultRecord, KernelRefusal> {
191    // First try with oblique hole-wall carriers extended past their opening
192    // planes (the lane that closes tilted/conical through-holes). If that
193    // extension fired but the arrangement could not assemble the harder
194    // configuration it produced (e.g. the extended bore genuinely intersects
195    // other offset walls), fall back to the un-extended pipeline so every
196    // previously-supported shape keeps its exact path and every refusal keeps
197    // the canonical honesty-gate message.
198    let mut extension_fired = false;
199    let mut reflex_rebuilds = 0usize;
200    let mut shadowed_points = Vec::new();
201    let mut omitted = HashSet::default();
202    let mut vacated_notes = Vec::new();
203    let result = match offset_shell_impl(
204        source,
205        opening_face_ids,
206        distance,
207        true,
208        &mut extension_fired,
209        &mut reflex_rebuilds,
210        &HashSet::default(),
211        &mut shadowed_points,
212        &mut omitted,
213        &mut vacated_notes,
214    ) {
215        Ok(result) => {
216            // Attribution marker: which pipeline produced this result. A
217            // retry-produced Ok can silently mask a broken extension lane —
218            // always say which attempt won. The reflex-rebuild count rides
219            // along because that lane fires on BOTH attempts: "extension
220            // idle" alone would misread as "the plain pipeline did this".
221            os_debug!(
222                "offset_shell result: FIRST attempt (extension {}, reflex-rebuilt {})",
223                if extension_fired { "fired" } else { "idle" },
224                reflex_rebuilds,
225            );
226            Ok(result)
227        }
228        Err(error) if extension_fired => {
229            os_debug!("extended pipeline failed ({error}); retrying without carrier extension");
230            let mut unused = false;
231            let mut retry_reflex = 0usize;
232            shadowed_points.clear();
233            omitted.clear();
234            let retried = offset_shell_impl(
235                source,
236                opening_face_ids,
237                distance,
238                false,
239                &mut unused,
240                &mut retry_reflex,
241                &HashSet::default(),
242                &mut shadowed_points,
243                &mut omitted,
244                &mut vacated_notes,
245            );
246            os_debug!(
247                "offset_shell result: RETRY without extension ({}, reflex-rebuilt {retry_reflex})",
248                if retried.is_ok() { "ok" } else { "err" },
249            );
250            retried
251        }
252        Err(error) => Err(error),
253    };
254    // A refusal from a shell whose selection saw two offsets CROSS inside
255    // material thinner than twice the distance says so first: a rib thinner
256    // than 2d shelled inward is that configuration, and the assembly's own
257    // message names only the symptom. A refusal that already names its cause
258    // keeps it. The crossing is looked for here, on the failure path only.
259    match result {
260        // A refusal that already names its CAUSE keeps its own sentence; one
261        // that names a symptom ("connector edge left its plane", an assembly
262        // message) is worth a cause in front of it. Reading the whole prefix as
263        // "anything this module wrote" was too wide: the twin bosses came out
264        // led by a notch sentence when their cause was the shadow cascade, and
265        // the partial cone lost its junction cause behind a connector's.
266        // Each cause is put IN FRONT of the symptom with `with_message`, so the
267        // symptom's class (an assembly's `NonIntegralGenus`, …) survives.
268        Err(error) if !refusal_names_its_cause(&error.message) => {
269            let crossing = name_offset_crossing(source, opening_face_ids, distance, &shadowed_points);
270            let junction = crossing
271                .is_none()
272                .then(|| name_retained_opening_junction(source, opening_face_ids, distance))
273                .flatten();
274            Err(cause_in_front(error, crossing, junction, vacated_notes.first()))
275        }
276        result => result,
277    }
278}
279
280/// The symptom `error` with the first named cause IN FRONT of it: an offset
281/// crossing, else a retained/opening junction, else a vacated rim's note.
282/// Each prefix is a `with_message`, so the symptom's CLASS (an assembly's
283/// `NonIntegralGenus`, …) survives the sentence put before it.
284pub(super) fn cause_in_front(
285    error: KernelRefusal,
286    crossing: Option<String>,
287    junction: Option<String>,
288    note: Option<&String>,
289) -> KernelRefusal {
290    if let Some(crossing) = crossing {
291        return error.with_message(|error| {
292            format!(
293                "offset_shell: {crossing}; a skin partly shadowed that way is not yet \
294                 assembled ({error})"
295            )
296        });
297    }
298    if let Some(junction) = junction {
299        return error.with_message(|error| format!("offset_shell: {junction} ({error})"));
300    }
301    // A rim the closure could not bridge, with what it measured, in front of
302    // whatever the assembly then said about the hole it left.
303    match note {
304        Some(note) => error.with_message(|error| {
305            format!(
306                "offset_shell: a rim vacated by an omitted support was left open — {note} \
307                 ({error})"
308            )
309        }),
310        None => error,
311    }
312}
313
314/// The refusals this module's own cause-naming lanes produce. Anything else is
315/// a symptom another stage reported, and takes a cause in front of it.
316fn refusal_names_its_cause(error: &str) -> bool {
317    [
318        " coincide near ",
319        "cannot tell whether",
320        "omitting the shadowed offsets of source faces",
321        "no retained face keeps any offset skin",
322        "is shadowed at every one of its",
323        "a rim vacated by an omitted support was left open",
324        "junction, so its offset ends inside the cavity",
325        "cross near",
326        // The opening-wall lane's own named refusals, so a cause of mine never
327        // arrives in front of one of theirs.
328        "folds where the carve",
329        "is curved and not a ruled surface",
330        "cannot be built",
331    ]
332    .iter()
333    .any(|name| error.contains(name))
334}
335
336fn offset_shell_impl(
337    source: &BrepSolid,
338    opening_face_ids: &[u64],
339    distance: f64,
340    extend_oblique_carriers: bool,
341    extension_fired: &mut bool,
342    reflex_rebuilds: &mut usize,
343    shadowed_supports: &HashSet<u64>,
344    shadowed_points: &mut Vec<(Vec3, u64)>,
345    omitted_supports: &mut HashSet<u64>,
346    vacated_notes: &mut Vec<String>,
347) -> Result<OffsetShellResultRecord, KernelRefusal> {
348    omitted_supports.clone_from(shadowed_supports);
349    vacated_notes.clear();
350    if !distance.is_finite() || distance.abs() <= MINIMUM_DISTANCE {
351        return Err(KernelRefusal::input(KernelStage::Collect, "offset_shell_distance", "offset_shell: distance must be finite and non-zero"));
352    }
353    if opening_face_ids.is_empty() {
354        return Err(KernelRefusal::input(KernelStage::Collect, "offset_shell_no_opening", "offset_shell: at least one opening face is required"));
355    }
356    let source_faces = source
357        .shells
358        .iter()
359        .flat_map(|shell| &shell.faces)
360        .collect::<Vec<_>>();
361    let opening_set = opening_face_ids.iter().copied().collect::<HashSet<_>>();
362    if opening_set
363        .iter()
364        .any(|id| !source_faces.iter().any(|face| face.id == *id))
365    {
366        return Err(KernelRefusal::input(KernelStage::Collect, "offset_shell_foreign_opening", "offset_shell: opening face does not belong to source"));
367    }
368    let retained = source_faces
369        .iter()
370        .filter(|face| !opening_set.contains(&face.id))
371        .map(|face| face.id)
372        .collect::<Vec<_>>();
373    if retained.is_empty() {
374        return Err(refusals::no_retained_face().into());
375    }
376    let mut carriers = Vec::new();
377    // Faces whose folded part was carved away, by source face id. Later stages
378    // that read a retained face's TRIM — the apex-cap scan below is the one —
379    // must read the carved trim, not the one the source still carries.
380    let mut carved_supports = HashMap::<u64, FaceRecord>::default();
381
382    for face_id in &retained {
383        let face = source_faces.iter().find(|face| face.id == *face_id).unwrap();
384        if offset_support_collapsed(face, distance)? {
385            os_debug!("omitting collapsed offset support for source face {face_id}");
386            continue;
387        }
388        // A support a previous pass found SHADOWED contributes no skin, and its
389        // carrier must not imprint its neighbours either — see the selection.
390        if shadowed_supports.contains(face_id) {
391            os_debug!("omitting shadowed offset support for source face {face_id}");
392            continue;
393        }
394        // A planar carrier whose source face meets a neighbour at a REFLEX
395        // (concave) edge extends its trim by |distance|: at such a junction
396        // the offset skin must GROW past the source footprint to meet the
397        // neighbour's offset (a pocket wall's source [5,15] → skin
398        // [3.5,16.5]); a source-sized trim leaves a gap no miter can bridge.
399        // The surplus at the face's convex ends is clipped by the imprints +
400        // on-skin filter. Convex-only faces keep their exact source-sized
401        // trim — extending them perturbs delicately-selected fragment
402        // landscapes for no benefit.
403        let miter = source_faces
404            .iter()
405            .find(|face| face.id == *face_id)
406            .map(|face| face_reflex_miter_tan(source, face))
407            .transpose()?
408            .flatten();
409        // The offsets of two faces joined at a reflex edge meet d·tan(θn/2)
410        // past the source rim — d only for a perpendicular join. Scale the
411        // extension to the worst sampled miter (×1.5 headroom so the true
412        // intersection lands strictly INSIDE the extended trim, never on its
413        // boundary where the arrangement drops boundary-coincident cuts) and
414        // cap it: a near-tangential join would ask for an unbounded trim.
415        let reflex_extension = miter.map(|worst_tan| {
416            (distance.abs() * worst_tan * 1.5)
417                .max(distance.abs())
418                .min(distance.abs() * 12.0)
419        });
420        if let Some(extension) = reflex_extension {
421            os_debug!("carrier for src face {face_id}: reflex-extended by {extension:.4}");
422        }
423        // OUTWARD: every retained carrier grows by |distance| across its
424        // sharp and opening-facing sides (the offsets of a convex junction
425        // meet past both source rims; a wall plane cuts the surplus), and
426        // by nothing across a smooth junction into a retained neighbour —
427        // see `outward_carrier_extension`. A reflex junction needs no
428        // outward growth at all (those offsets cross inside the trims).
429        let extension = if distance < 0.0 {
430            outward_carrier_extension(source, face, &source_faces, &opening_set, distance.abs())?
431        } else {
432            let mut inward = crate::CarrierExtension::uniform(reflex_extension.unwrap_or(0.0));
433            // An INWARD carrier that falls short of the planar opening it meets
434            // is grown until it crosses that plane, so the imprint cuts the
435            // skin there instead of leaving a rim for the ruled weld to cork
436            // below the opening (`fallshort_opening_reach`).
437            if let Some((at_v_max, reach)) =
438                fallshort_opening_reach(source, face, &source_faces, &opening_set, distance)?
439            {
440                // Past the plane by a quarter of the shell's own thickness, so
441                // the section is strictly inside the carrier's trim rather than
442                // on its boundary. Measured 2026-09-17 over the offset-shell
443                // suite: every fraction from 0.01 to 2.0 of |d| gives the same
444                // 73 passing members, while a margin sized in COINCIDENCE BANDS
445                // (1 to 32 of them, 2e-5 each here) breaks the sphere-under-
446                // frustum member by 9.4e-2 — the fragile margins are the ones
447                // far below the construction's own length.
448                let grown = reach + REACH_CLEARANCE * distance.abs();
449                if at_v_max {
450                    inward.v_max = inward.v_max.max(grown);
451                } else {
452                    inward.v_min = inward.v_min.max(grown);
453                }
454                os_debug!(
455                    "carrier for src face {face_id}: inward reach to its opening plane {reach:.6}                      at v_{} ",
456                    if at_v_max { "max" } else { "min" },
457                );
458            }
459            // A CURVED opening has no such construction, and the pipeline
460            // closes the cavity with whatever completion reaches for: measured
461            // 7.6% short on the hemisphere cup shelled through its dome.
462            if let Some((opening, gap)) =
463                offset_falls_short_of_curved_opening(source, face, &source_faces, &opening_set, distance)?
464            {
465                return Err(KernelRefusal::unsupported(KernelStage::Refine, "offset_shell_curved_opening_short", format!(
466                    "offset_shell: the offset skin of source face {face_id} stops {gap:.3e} short of \
467                     curved opening face {opening}; only a planar opening's skin is grown to meet it"
468                )));
469            }
470            inward
471        };
472        if distance < 0.0 {
473            os_debug!(
474                "carrier for src face {face_id}: outward extension u=({:.3},{:.3}) v=({:.3},{:.3})",
475                extension.u_min,
476                extension.u_max,
477                extension.v_min,
478                extension.v_max,
479            );
480        }
481        // CARVE: a support whose offset folds over PART of its trim keeps the
482        // regular part and drops the rest, instead of being built whole with
483        // the fold in it. The carrier is built over the CARVED source, so the
484        // trim it clones stops at the fold locus — and, because that trim no
485        // longer reaches past the offset's pinch, `offset_surface`'s apex-cone
486        // pinch retrim does not fire and the cloned pcurves still name the
487        // pointwise offset.
488        //
489        // A carve that cannot be made is not a licence to build the folded
490        // support anyway: the refusal is the answer, because a carrier with an
491        // inverted patch in it welds into a shell that validates and bounds the
492        // wrong solid.
493        let carved = carve_folded_support(source, face, distance)?;
494        // A CUT RIM — the face's rim with a planar opening running across
495        // its surface rather than along a v end — has the trim rebuilt over
496        // the surface's full rectangle so the opening plane truncates the
497        // offset skin (`cut_rim_full_rectangle_source`); like the carve, a
498        // substitute source the carrier is built over.
499        let cut_rim = if carved.is_none() {
500            cut_rim_full_rectangle_source(source, face, &source_faces, &opening_set, distance)?
501        } else {
502            None
503        };
504        // A curved face's SHARP outward u-side: the carrier is built over the
505        // surface continued past it, so it reaches the mitre with its sharp
506        // neighbour's offset (`sharp_side_continued_source`).
507        let sharp_side = if carved.is_none() && cut_rim.is_none() {
508            sharp_side_continued_source(source, face, &source_faces, &opening_set, distance, &extension)?
509        } else {
510            None
511        };
512        let carrier_source = match (&carved, &cut_rim) {
513            (None, None) if sharp_side.is_some() => sharp_side.as_ref().unwrap(),
514            (None, Some(rebuilt)) => rebuilt,
515            (Some(carved), _) => {
516                os_debug!(
517                    "carving the fold off source face {face_id}: {:.1}% of its trim kept, \
518                     {} new boundary pcurve(s)",
519                    carved.kept_fraction * 100.0,
520                    carved.fold_boundary
521                );
522                carved_supports.insert(*face_id, carved.face.clone());
523                &carved.solid
524            }
525            (None, None) => source,
526        };
527        let mut carrier = carrier_solid_sided(carrier_source, *face_id, distance, &extension)?;
528        if (distance < 0.0 || reflex_extension.is_some()) && face.surface.is_affine().or_refuse(KernelStage::Sew, "is_affine")? {
529            // PADDED planes: the pad slides the plane's net while the trim's
530            // loops are cloned in uv, so an INTERIOR loop (a bore or boss rim
531            // in the face) is carried outward with the grown outline instead
532            // of following the neighbour's offset.
533            //   OUTWARD: a bore's rim grew (r=4.68 → 5.15) while the shrunk
534            //   offset bore (r=3.68) that must hole this plane fell inside the
535            //   enlarged void, so neither the imprint nor the orphan-rim weld
536            //   could cut it.
537            //   INWARD, reflex-extended: the same slide moves a REFLEX hole
538            //   rim the wrong way — a sphere sitting under a frustum's base
539            //   annulus (the user's 2026-09-07 document) had its r=8.3 rim
540            //   scaled to 9.22 while the shrunk offset sphere (r=7.3) crosses
541            //   the offset plane at r=7.23, inside the enlarged hole: the
542            //   pair imprint found nothing and the rim stayed one-use.
543            // Drop the interior loops (as the wall carriers do): the
544            // neighbour's carrier imprints — or its rim, welded in as a
545            // hole — cut the true opening, and the seeded selection drops
546            // the disk over it. A CONVEX interior rim on the same face (a
547            // through-bore in a pocket floor) is safe to drop too: its
548            // offset bore imprints at r+d and the ring inside is rejected by
549            // class (Out) or by the on-skin filter, no seed involved.
550            drop_wall_interior_loops(&mut carrier)?;
551            // A free-form outline grown uniformly outward is trimmed by its
552            // outline OFFSET, not the slid net's scaled trim
553            // (`outline_offset_carrier`).
554            let uniform = extension.u_min == extension.u_max
555                && extension.u_min == extension.v_min
556                && extension.u_min == extension.v_max;
557            if distance < 0.0 && uniform {
558                // Past the walls' sections by the same clearance a planar
559                // neighbour of a ruled wall gets, so each section lies
560                // strictly INSIDE the fitted trim rather than on it.
561                let clearance =
562                    RULED_NEIGHBOUR_CLEARANCE_BANDS * offset_skin_band(crate::solid_scale(source), distance);
563                if let Some(rebuilt) = outline_offset_carrier(source, face, extension.u_min, clearance, &carrier)? {
564                    os_debug!(
565                        "carrier for src face {face_id}: outline trimmed by its offset by {:.4}",
566                        extension.u_min + clearance
567                    );
568                    carrier = rebuilt;
569                }
570            }
571        }
572        // An inward reflex junction needs a construction plane extending PAST
573        // the source rim. Sliding the net under a free-form trim scales that
574        // trim instead: a notched revolve cap's notch grows, and clips away the
575        // sections with the box's offsets. Even its curved convex boundary can
576        // fall inside the neighbouring offset section after that scaling.
577        // Use the extended plane's full rectangle and let the neighbouring
578        // carriers cut the skin. The source face and the offset surface stay
579        // unchanged. Unlike an outline offset, this needs no approximation of
580        // the planar rim's parallel curve (which may collapse at a tight turn).
581        if distance > 0.0
582            && reflex_extension.is_some()
583            && face.surface.is_affine().or_refuse(KernelStage::Refine, "is_affine")?
584            && outline_has_free_element(source, face)?
585        {
586            carrier = full_rectangle_face(&carrier, &carrier.shells[0].faces[0])?;
587        }
588        // The rim a shadowed support's removal VACATED in this carrier's trim:
589        // a slot narrower than 2|d| that runs out through this face leaves a
590        // notch its own skin has to close, because the dilation fills the slot.
591        // The source face keeps its notch; only the carrier is bridged.
592        if distance < 0.0 && !shadowed_supports.is_empty() {
593            let report = close_vacated_rims(
594                &mut carrier,
595                source_faces
596                    .iter()
597                    .find(|face| face.id == *face_id)
598                    .copied()
599                    .unwrap_or(face),
600                &source_faces,
601                shadowed_supports,
602            )?;
603            if report.closed > 0 {
604                os_debug!(
605                    "carrier for src face {face_id}: {} vacated rim run(s) bridged",
606                    report.closed
607                );
608            }
609            for declined in &report.declined {
610                os_debug!("carrier for src face {face_id}: vacated rim left alone — {declined}");
611                vacated_notes.push(format!("source face {face_id}: {declined}"));
612            }
613        }
614        carriers.push(Carrier {
615            solid: carrier,
616            source_face_id: *face_id,
617            kind: OffsetFaceRole::Offset,
618        });
619    }
620    // APEX-CAP carriers: a retained face whose DEGENERATE edge (an apex
621    // point, e.g. a conic crater's tip) offsets into a RING is an EXTERIOR
622    // cone offset — the parallel surface truncates at the ring and the exact
623    // rolling-ball offset closes it with a SPHERE of radius |distance| about
624    // the apex. Emit that sphere as an extra carrier: the ring imprint
625    // (sphere × offset frustum) splits it, the on-skin filter keeps exactly
626    // the cap sector (its points are |d| from the apex and ≥|d| from the
627    // rest of the face), and the ring becomes two-use.
628    let mut apex_cap_sources = HashSet::default();
629    let mut apex_cap_pairs: Vec<(usize, usize)> = Vec::new();
630    {
631        let retained_count = carriers.len();
632        for index in 0..retained_count {
633            // A CARVED support's trim is the carved one. Its coedges name edges
634            // that live only in the carved clone, so the `source.edges` lookup
635            // below finds nothing and the apex-cap lane skips the face — which
636            // is right: the carve already ended that carrier at the offset's own
637            // pinch, and there is no apex ring left to cap.
638            let carved_face = carved_supports.get(&carriers[index].source_face_id);
639            let source_face = carved_face
640                .or_else(|| source_by_id_lookup(&source_faces, carriers[index].source_face_id));
641            let Some(source_face) = source_face else {
642                continue;
643            };
644            let carrier_surface = carriers[index].solid.shells[0].faces[0].surface.clone();
645            for coedge in source_face
646                .loops
647                .iter()
648                .flat_map(|loop_record| &loop_record.coedges)
649            {
650                let Some(edge) = source
651                    .edges
652                    .iter()
653                    .find(|edge| edge.id == coedge.edge_id)
654                else {
655                    continue;
656                };
657                if !edge.degenerate {
658                    continue;
659                }
660                // Image of the degenerate edge on the offset surface.
661                let [p0, p1] = coedge.pcurve.domain().or_refuse(KernelStage::Sew, "domain")?;
662                let mut ring = Vec::new();
663                for sample in 0..=8 {
664                    let uv = coedge
665                        .pcurve
666                        .evaluate(p0 + (p1 - p0) * sample as f64 / 8.0).or_refuse(KernelStage::Sew, "evaluate")?;
667                    ring.push(carrier_surface.evaluate(uv.x, uv.y).or_refuse(KernelStage::Sew, "evaluate")?);
668                }
669                let spread = ring
670                    .iter()
671                    .map(|point| point.sub(ring[0]).length())
672                    .fold(0.0f64, f64::max);
673                if spread <= distance.abs() * 1e-2 {
674                    continue;
675                }
676                let apex = source
677                    .vertices
678                    .iter()
679                    .find(|vertex| vertex.id == edge.start_vertex_id)
680                    .map(|vertex| vertex.point)
681                    .ok_or_else(|| "offset_shell: apex vertex missing".to_string()).or_refuse(KernelStage::Sew, "offset_shell_apex_vertex_missing")?;
682                let centroid = ring
683                    .iter()
684                    .fold(Vec3::default(), |sum, point| sum.add(*point))
685                    .scale(1.0 / ring.len() as f64);
686                let axis = apex.sub(centroid);
687                let axis = if axis.length() > 1e-9 {
688                    axis.normalized().or_refuse(KernelStage::Sew, "normalized")?
689                } else {
690                    Vec3::new(0.0, 0.0, 1.0)
691                };
692                os_debug!(
693                    "apex-cap carrier for src face {} at ({:.3},{:.3},{:.3}) r={:.3}",
694                    carriers[index].source_face_id,
695                    apex.x,
696                    apex.y,
697                    apex.z,
698                    distance.abs(),
699                );
700                apex_cap_sources.insert(index);
701                apex_cap_sources.insert(carriers.len());
702                apex_cap_pairs.push((index, carriers.len()));
703                carriers.push(Carrier {
704                    solid: crate::make_sphere_brep(apex, distance.abs(), axis).or_refuse(KernelStage::Sew, "make_sphere_brep")?,
705                    source_face_id: carriers[index].source_face_id,
706                    kind: OffsetFaceRole::Offset,
707                });
708            }
709        }
710    }
711    // A fold the carve's probe did not see (`outward_fold_the_carve_missed`):
712    // a band too thin for its grid is built whole, and the torus wedge grown by
713    // 1.001 then held the reflected lemon beyond its axis. Asked of every
714    // retained face the carve neither carved nor omitted, on every outward
715    // shell: a ruled wall is what first let the pipeline CLOSE such a shape,
716    // but the geometry is the trigger. Refused by name, the tangent onset
717    // included; a partial revolve of that kind is the revolved-wedge lane's
718    // question, and that lane runs in front of this pipeline.
719    if distance < 0.0 {
720        for face_id in &retained {
721            if carved_supports.contains_key(face_id)
722                || !carriers.iter().any(|carrier| carrier.source_face_id == *face_id)
723            {
724                continue;
725            }
726            let face = source_by_id_lookup(&source_faces, *face_id).unwrap();
727            if let Some(fold) = outward_fold_the_carve_missed(face, distance)? {
728                return Err(KernelRefusal::unsupported(KernelStage::Refine, "offset_shell_outward_fold_band", format!(
729                    "offset_shell: the outward offset of source face {face_id} folds where the carve \
730                     saw no fold (area factor {:.3e} at u={:.6}, v={:.6}); a fold band that thin is \
731                     not built",
732                    fold.factor, fold.u, fold.v
733                )));
734            }
735        }
736    }
737    // Wall carriers built as a RULED opening face extended along itself, by
738    // carrier index: the in-front guard reads their sides off the surface
739    // normal where it would read a plane's.
740    let mut ruled_walls = HashSet::default();
741    // Inward walls at reflex rims need construction area beyond the source
742    // opening too. Track them for source imprints and material-side selection.
743    let mut inward_extended_walls = HashSet::default();
744    let mut reflex_wall_neighbors = HashMap::default();
745    // OUTWARD through a CURVED opening. A ruled one — the cone a revolved
746    // straight profile edge sweeps, a cylinder — has its wall built as the
747    // opening face held in place and EXTENDED along itself, exactly as a plane
748    // is padded (`extended_ruled_wall`); the pad is the plane's, read off the
749    // same convex junctions. Built first, so the planar walls know which
750    // neighbours are ruled walls.
751    //
752    // Any other curved opening is refused by name. Its wall used to be the
753    // unpadded source face, which never reaches the grown offset: on a torus
754    // wedge whose chord revolves to a cone, the pair offset x cone cut 0
755    // pieces, the cone's one fragment was dropped as a void cross-section, and
756    // the result read "completion produced non-integral genus" with 12 open
757    // edges. No outward shell in the kernel suites or the case corpus closed
758    // through that wall (census, 2026-09-17).
759    let mut ruled_built = HashMap::<u64, BrepSolid>::default();
760    if distance < 0.0 {
761        for face_id in opening_face_ids {
762            let source_face = source_by_id_lookup(&source_faces, *face_id).unwrap();
763            if source_face.surface.is_affine().or_refuse(KernelStage::Sew, "is_affine")? {
764                continue;
765            }
766            if !ruled_opening(source_face)? {
767                return Err(KernelRefusal::unsupported(KernelStage::Refine, "offset_shell_curved_opening", format!(
768                    "offset_shell: opening face {face_id} is curved and not a ruled surface; an \
769                     outward wall is built only on a planar or ruled opening"
770                )));
771            }
772            let pad = outward_wall_pad(
773                source,
774                source_face,
775                &source_faces,
776                &opening_set,
777                distance.abs(),
778            )?;
779            let tolerance = 1e-7f64.max(crate::solid_scale(source) * 1e-9);
780            match extended_ruled_wall(source, source_face, pad, tolerance)? {
781                Ok(wall) => {
782                    os_debug!(
783                        "wall for opening face {face_id}: ruled, pad {pad:.4}, sides reach {:.4?}",
784                        wall.reached
785                    );
786                    ruled_built.insert(*face_id, wall.solid);
787                }
788                Err(reason) => {
789                    return Err(KernelRefusal::unsupported(KernelStage::Refine, "offset_shell_ruled_wall", format!(
790                        "offset_shell: the outward wall of ruled opening face {face_id} cannot be \
791                         built: {reason}"
792                    )));
793                }
794            }
795        }
796    }
797    let ruled_openings = ruled_built
798        .keys()
799        .filter_map(|id| source_by_id_lookup(&source_faces, *id))
800        .collect::<Vec<_>>();
801    for face_id in opening_face_ids {
802        let source_face = source_faces
803            .iter()
804            .find(|face| face.id == *face_id)
805            .unwrap();
806        if let Some(wall) = ruled_built.remove(face_id) {
807            ruled_walls.insert(carriers.len());
808            carriers.push(Carrier {
809                solid: wall,
810                source_face_id: *face_id,
811                kind: OffsetFaceRole::Wall,
812            });
813            continue;
814        }
815        if distance > 0.0
816            && source_face.surface.is_affine().or_refuse(KernelStage::Refine, "is_affine")?
817        {
818            if let Some((pad, neighbors)) =
819                inward_wall_pad(source, source_face, &source_faces, &opening_set, distance)?
820            {
821                // Scaling a notched/free-form outline can still clip the new
822                // rim. Use the whole plane rectangle; source and offset
823                // imprints below recover both exact boundaries of the wall.
824                let padded = carrier_solid(source, *face_id, 0.0, pad)?;
825                let wall = full_rectangle_face(&padded, &padded.shells[0].faces[0])?;
826                inward_extended_walls.insert(carriers.len());
827                reflex_wall_neighbors.insert(carriers.len(), neighbors);
828                carriers.push(Carrier {
829                    solid: wall,
830                    source_face_id: *face_id,
831                    kind: OffsetFaceRole::Wall,
832                });
833                continue;
834            }
835        }
836        carriers.push(Carrier {
837            solid: if distance < 0.0 && source_face.surface.is_affine().or_refuse(KernelStage::Sew, "is_affine")? {
838                let pad = outward_wall_pad(
839                    source,
840                    source_face,
841                    &source_faces,
842                    &opening_set,
843                    distance.abs(),
844                )?;
845                // A plane meeting a RULED opening is padded past the grown
846                // outline, not onto it. Padded by exactly the pad, its trim's
847                // arc lands ON the grown offset's section wherever the join is
848                // perpendicular, and the ruled wall's section then ENDS on that
849                // coincident pair: on the torus wedge the cap shredded into 60
850                // slivers along the grown circle. The clearance is counted in
851                // the band that decides whether the strip beyond the grown
852                // outline is wall or void, so the strip always resolves as its
853                // own fragment and is dropped as void.
854                let pad = if ruled_openings
855                    .iter()
856                    .any(|ruled| source_faces_adjacent(source_face, ruled))
857                {
858                    pad + RULED_NEIGHBOUR_CLEARANCE_BANDS
859                        * offset_skin_band(crate::solid_scale(source), distance)
860                } else if outline_has_free_element(source, source_face)? {
861                    // A free-form outline's wall is a FIT: without the same
862                    // clearance its outline sits within the fit tolerance of
863                    // the carriers' own sections on the plane, and the
864                    // near-coincident curves shred the wall's imprint (26 to
865                    // 121 fragments on the free-form D band, 2026-09-26). With
866                    // it, the surplus ring is cut away cleanly.
867                    pad + RULED_NEIGHBOUR_CLEARANCE_BANDS * offset_skin_band(crate::solid_scale(source), distance)
868                } else {
869                    pad
870                };
871                os_debug!("wall for opening face {face_id}: outward pad {pad:.4}");
872                // The wall is the opening's outline OFFSET by the pad in its
873                // own plane (`outline_offset.rs`). The pad used to SCALE that
874                // outline about the patch centre, which is the offset only for
875                // a rectangle filling its patch or a circle centred on it: an
876                // L-shaped opening's notch edges pass through the centre and
877                // did not move at all, so the notch faces' grown offsets
878                // crossed the plane outside the wall ("non-integral genus",
879                // `l-step-open-bottom` at d = −1.5 in the membrane census).
880                //
881                // An outline this module does not offset keeps the scaled wall,
882                // but only when that wall still COVERS the offset outline along
883                // every edge a retained face meets — which is what the sections
884                // have to land inside. Otherwise the shell refuses by name
885                // rather than assembling an open rim.
886                let mut scaled = carrier_solid(source, *face_id, 0.0, pad)?;
887                drop_wall_interior_loops(&mut scaled)?;
888                let mut wall = match offset_outline_wall(source, source_face, pad)? {
889                    Ok(outline) => {
890                        // Where the scaled wall already trims the offset
891                        // outline's own region — a rectangle filling its patch,
892                        // a circle centred on it — it is kept, so no shell that
893                        // was right before is rebuilt on a different patch.
894                        let same = walls_trim_the_same_region(
895                            &outline,
896                            &scaled,
897                            1e-9f64.max(crate::solid_scale(source) * 1e-11),
898                        )?;
899                        if same {
900                            os_debug!("wall for opening face {face_id}: scaled, which IS its outline offset");
901                            scaled
902                        } else {
903                            os_debug!("wall for opening face {face_id}: outline offset by {pad:.4}");
904                            outline
905                        }
906                    }
907                    Err(reason) => {
908                        let carrier_surfaces = carriers
909                            .iter()
910                            .filter(|carrier| matches!(carrier.kind, OffsetFaceRole::Offset))
911                            .map(|carrier| (carrier.source_face_id, &carrier.solid.shells[0].faces[0].surface))
912                            .collect::<Vec<_>>();
913                        if let Err(uncovered) = scaled_wall_covers_offset(
914                            source,
915                            source_face,
916                            &scaled,
917                            pad,
918                            &source_faces,
919                            &opening_set,
920                            &carrier_surfaces,
921                        )? {
922                            return Err(KernelRefusal::unsupported(KernelStage::Refine, "offset_shell_opening_wall_coverage", format!(
923                                "offset_shell: opening face {face_id}'s outline is not offset \
924                                 ({reason}), and its scaled outward wall does not contain every \
925                                 retained neighbour's grown offset section (a point of one lies \
926                                 {uncovered:.3e} outside)"
927                            )));
928                        }
929                        os_debug!(
930                            "wall for opening face {face_id}: scaled, which covers its offset ({reason})"
931                        );
932                        scaled
933                    }
934                };
935                // The pad extends the wall by STRETCHING the plane's 2x2 net
936                // while the trim pcurves are cloned in UV, so every loop
937                // scales about the patch centre by (w+2|d|)/w. For the OUTER
938                // loop of a full-domain rectangle that lands exactly on the
939                // grown outline — the intended wall extent. But an INTERIOR
940                // loop (a carve outline: a bore, gouge or crater rim in the
941                // opening face) is scaled OUTWARD too, and the enlarged hole
942                // then swallows both circles the wall must be cut by (the
943                // source carve rim from the wall x source imprint and the
944                // shrunk rim from the wall x offset-carrier imprint) — the
945                // imprints clip to nothing and the rims dangle one-use. Drop
946                // interior loops instead: the wall covers the carve mouth,
947                // the imprints cut the true rims, and selection (void-wall
948                // guard + on-skin filter) drops the fragments over the void.
949                drop_wall_interior_loops(&mut wall)?;
950                wall
951            } else {
952                standalone_face(source, *face_id)?
953            },
954            source_face_id: *face_id,
955            kind: OffsetFaceRole::Wall,
956        });
957    }
958    if carriers.len() >= SOURCE_OPERAND as usize {
959        return Err(KernelRefusal::unsupported(KernelStage::Collect, "offset_shell_carrier_count", "offset_shell: too many carrier faces for current ABI"));
960    }
961    // The characteristic length every tolerance below is derived from.  This
962    // was `max ‖vertex‖` — the distance from the WORLD ORIGIN — until audit
963    // slice 0: that form made every band widen as the part was moved away from
964    // the origin while ignoring the part's actual size, so the same shell
965    // solved to different tolerances depending only on where it was modelled.
966    // `solid_scale` is the bbox diagonal, floored at 1.0 exactly as the old
967    // fold was, so this changes ONE thing: placement-dependence.
968    let scale = crate::solid_scale(source);
969    crate::report_scale_migration("offset_shell", scale, || {
970        source
971            .vertices
972            .iter()
973            .map(|vertex| vertex.point.length())
974            .fold(1.0, f64::max)
975    });
976    let smooth = synchronize_smooth_offset_boundaries(&mut carriers, &source_faces, scale)?;
977    // Reflex-rim rebuilds run on BOTH attempts: the fallback for a broken
978    // extension is an honest refusal, but the fallback for a skipped reflex
979    // rebuild is a silently WRONG (membrane-capped) shell.
980    let reflex_rebuilt = rebuild_reflex_rim_carriers(
981        &mut carriers,
982        source,
983        &source_faces,
984        &smooth.pairs,
985        scale,
986        distance,
987    )?;
988    *reflex_rebuilds = reflex_rebuilt.len();
989    if extend_oblique_carriers {
990        let oblique_extended = extend_offset_carriers_past_open_hole_rims(
991            &mut carriers,
992            source,
993            &source_faces,
994            &opening_set,
995            &smooth.pairs,
996            scale,
997        )?;
998        let mut already_extended = oblique_extended.clone();
999        already_extended.extend(reflex_rebuilt.iter().copied());
1000        let fallshort_extended = extend_fallshort_curved_carriers(
1001            &mut carriers,
1002            source,
1003            &source_faces,
1004            &opening_set,
1005            &smooth.pairs,
1006            &already_extended,
1007            scale,
1008            distance,
1009        )?;
1010        *extension_fired = !oblique_extended.is_empty() || fallshort_extended > 0;
1011        os_debug!(
1012            "extended {} oblique + {fallshort_extended} fall-short carriers past their opening planes",
1013            oblique_extended.len(),
1014        );
1015    }
1016    let tolerance = 1e-7f64.max(scale * 1e-9);
1017    let pair_tolerance = (scale * 1e-8).max(2e-6);
1018    let reach = distance.abs() * 4.0 + pair_tolerance;
1019    let carrier_samples = carriers
1020        .iter()
1021        .map(|carrier| carrier_extent_points(&carrier.solid))
1022        .collect::<Result<Vec<_>, _>>()?;
1023    let carrier_bounds = carrier_samples
1024        .iter()
1025        .map(|samples| Bounds::from_points(samples))
1026        .collect::<Option<Vec<_>>>()
1027        .ok_or_else(|| "offset_shell: carrier has no boundary samples".to_string()).or_refuse(KernelStage::Sew, "offset_shell_carrier_has_no_boundary_samples")?;
1028    let source_by_id = source_faces
1029        .iter()
1030        .map(|face| (face.id, *face))
1031        .collect::<HashMap<_, _>>();
1032    let source_samples = source_faces
1033        .iter()
1034        .map(|face| {
1035            let standalone = standalone_face(source, face.id)?;
1036            Ok((face.id, edge_sample_points(&standalone)?))
1037        })
1038        .collect::<Result<HashMap<_, _>, KernelRefusal>>()?;
1039    if debug_enabled() {
1040        for (index, carrier) in carriers.iter().enumerate() {
1041            let face = &carrier.solid.shells[0].faces[0];
1042            let samples = &carrier_samples[index];
1043            let bounds =
1044                samples
1045                    .iter()
1046                    .fold((Vec3::default(), Vec3::default()), |(low, high), point| {
1047                        (
1048                            Vec3 {
1049                                x: low.x.min(point.x),
1050                                y: low.y.min(point.y),
1051                                z: low.z.min(point.z),
1052                            },
1053                            Vec3 {
1054                                x: high.x.max(point.x),
1055                                y: high.y.max(point.y),
1056                                z: high.z.max(point.z),
1057                            },
1058                        )
1059                    });
1060            let surface = &face.surface;
1061            let rational = surface
1062                .control_points
1063                .iter()
1064                .flatten()
1065                .any(|point| (point.w - 1.0).abs() > 1e-12);
1066            os_debug!(
1067                "carrier[{index}] kind={:?} source_face={} loops={} deg=({},{}) net={}x{} rational={rational} bounds=({:.3},{:.3},{:.3})..({:.3},{:.3},{:.3})",
1068                carrier.kind, carrier.source_face_id, face.loops.len(),
1069                surface.degree_u, surface.degree_v,
1070                surface.control_points.len(),
1071                surface.control_points.first().map(|row| row.len()).unwrap_or(0),
1072                bounds.0.x, bounds.0.y, bounds.0.z, bounds.1.x, bounds.1.y, bounds.1.z,
1073            );
1074        }
1075        for (index, carrier) in carriers.iter().enumerate() {
1076            if !matches!(carrier.kind, OffsetFaceRole::Offset) {
1077                continue;
1078            }
1079            let source_face = source_by_id[&carrier.source_face_id];
1080            let carrier_surface = &carrier.solid.shells[0].faces[0].surface;
1081            let (ku, kv) = (
1082                crate::KnotVector::new(
1083                    source_face.surface.knots_u.clone(),
1084                    source_face.surface.degree_u,
1085                ),
1086                crate::KnotVector::new(
1087                    source_face.surface.knots_v.clone(),
1088                    source_face.surface.degree_v,
1089                ),
1090            );
1091            if let (Ok(ku), Ok(kv)) = (ku, kv) {
1092                let [u0, u1] = ku.domain();
1093                let [v0, v1] = kv.domain();
1094                let mut worst = 0.0f64;
1095                let mut worst_at = (0.0f64, 0.0f64);
1096                for iu in 0..=24 {
1097                    for iv in 0..=24 {
1098                        let u = u0 + (u1 - u0) * iu as f64 / 24.0;
1099                        let v = v0 + (v1 - v0) * iv as f64 / 24.0;
1100                        let expected = face_offsets(source_face)
1101                            .at(u, v, -distance)
1102                            .map(|sample| sample.point);
1103                        let actual = carrier_surface.evaluate(u, v);
1104                        if let (Ok(expected), Ok(actual)) = (expected, actual) {
1105                            let error = expected.sub(actual).length();
1106                            if error > worst {
1107                                worst = error;
1108                                worst_at = (u, v);
1109                            }
1110                        }
1111                    }
1112                }
1113                let mut normal_swing = 0.0f64;
1114                let mut previous: Option<Vec3> = None;
1115                for iu in 0..=48 {
1116                    let u = u0 + (u1 - u0) * iu as f64 / 48.0;
1117                    let v = (v0 + v1) / 2.0;
1118                    if let Ok(normal) = face_normal(source_face, u, v) {
1119                        if let Some(previous) = previous {
1120                            normal_swing = normal_swing.max(previous.sub(normal).length());
1121                        }
1122                        previous = Some(normal);
1123                    }
1124                }
1125                let weights = source_face
1126                    .surface
1127                    .control_points
1128                    .iter()
1129                    .flatten()
1130                    .map(|point| point.w)
1131                    .fold((f64::MAX, f64::MIN), |(low, high), w| {
1132                        (low.min(w), high.max(w))
1133                    });
1134                os_debug!(
1135                    "carrier[{index}] offset fit error max={worst:.6} at=({:.4},{:.4}) domain_u=({u0:.4},{u1:.4}) normal_step_max={normal_swing:.4} src_net={}x{} src_deg=({},{}) src_w=({:.4},{:.4}) knots_u={:?}",
1136                    worst_at.0, worst_at.1,
1137                    source_face.surface.control_points.len(),
1138                    source_face.surface.control_points.first().map(|row| row.len()).unwrap_or(0),
1139                    source_face.surface.degree_u, source_face.surface.degree_v,
1140                    weights.0, weights.1,
1141                    &source_face.surface.knots_u,
1142                );
1143            }
1144        }
1145        os_debug!("smooth pairs: {:?}", smooth.pairs);
1146    }
1147    let mut imprint = empty_imprint();
1148    let mut next_piece_id = 1;
1149    let mut next_vertex_id = 1;
1150    for first in 0..carriers.len() {
1151        for second in first + 1..carriers.len() {
1152            let first_source = source_by_id[&carriers[first].source_face_id];
1153            let second_source = source_by_id[&carriers[second].source_face_id];
1154            if matches!(carriers[first].kind, OffsetFaceRole::Wall)
1155                && matches!(carriers[second].kind, OffsetFaceRole::Wall)
1156            {
1157                // Unextended inward walls are the opening faces themselves:
1158                // they never overlap; adjacent ones only touch along their
1159                // shared edge, so there is nothing to imprint. OUTWARD walls
1160                // are the opening planes EXTENDED by |distance|: the walls of
1161                // two ADJACENT openings cross along the line of their shared
1162                // source edge, and each wall ring must stop there (the sheet
1163                // past that line lies in front of the other opening). Without
1164                // the cut the wall × source chain below stays open on the
1165                // shared-edge side (its neighbour there is an opening, not a
1166                // retained face), the wall never fragments into its ring, and
1167                // every source rim along both openings dangles one-use.
1168                // Extended inward walls need those mutual cuts as well.
1169                if (distance > 0.0
1170                    && !inward_extended_walls.contains(&first)
1171                    && !inward_extended_walls.contains(&second))
1172                    || !source_faces_adjacent(first_source, second_source)
1173                {
1174                    continue;
1175                }
1176            }
1177            if smooth.pairs.contains(&(first, second)) {
1178                os_debug!("pair ({first},{second}) skipped: smooth");
1179                continue;
1180            }
1181            if !carrier_bounds[first].intersects(carrier_bounds[second], pair_tolerance) {
1182                os_debug!("pair ({first},{second}) skipped: bounds");
1183                continue;
1184            }
1185            if !source_faces_adjacent(first_source, second_source)
1186                && sample_separation(
1187                    &source_samples[&first_source.id],
1188                    &source_samples[&second_source.id],
1189                ) > reach
1190            {
1191                os_debug!("pair ({first},{second}) skipped: separation");
1192                continue;
1193            }
1194            let pair = match crate::imprint::build_carrier_imprints(
1195                &carriers[first].solid,
1196                &carriers[second].solid,
1197                &ImprintOptions {
1198                    tolerance,
1199                    maximum_fit_points: 96,
1200                    // The GLOBAL interpolation lane. `local_fit: true` until
1201                    // 2026-09-13, which cost three orders on every smooth
1202                    // section: measured on the apex cone's base circle, the
1203                    // local lane needs 3,070 control points to reach 2.0e-5
1204                    // where the global one reaches 1.4e-8 with 384. What the
1205                    // local lane is for — a rim through a corner, where its
1206                    // monotonicity limiter holds a kink the global cubic rings
1207                    // across, 4.9x measured — is now the fit's own fallback:
1208                    // `fit_polyline` re-runs the other lane whenever the asked
1209                    // one cannot meet the tolerance and returns the better
1210                    // MEASURED curve, so this flag chooses which ladder is
1211                    // paid for first, never which curve is delivered.
1212                    local_fit: false,
1213                    fit_chunk_points: None,
1214                    // Offset carriers can meet in short, tightly curved
1215                    // branches. The general SSI default (diagonal / 15) is
1216                    // too coarse here and lets independently fitted branches
1217                    // miss a shared junction when thickness crosses one of
1218                    // those branches.
1219                    maximum_ssi_step: Some((scale * 0.0005).max(tolerance * 100.0)),
1220                },
1221            ) {
1222                Ok(pair) => pair,
1223                // A failed pair imprint (e.g. the marching SSI exhausting its
1224                // step budget on a long closed sphere/plane intersection) is
1225                // not fatal to the shell: the affected rim simply stays
1226                // unsplit and the rim-weld passes below get to close it. The
1227                // final watertightness gate still refuses anything the welds
1228                // cannot reach, so tolerating the miss only enlarges the
1229                // honest success set.
1230                Err(error) => {
1231                    os_debug!("pair ({first},{second}) imprint failed: {error}");
1232                    continue;
1233                }
1234            };
1235            let pieces_before = imprint.pieces.len();
1236            merge_pair_imprint(
1237                &mut imprint,
1238                pair,
1239                first as u8,
1240                second as u8,
1241                &mut next_piece_id,
1242                &mut next_vertex_id,
1243                tolerance,
1244                scale,
1245            );
1246            os_debug!(
1247                "pair ({first},{second}) src=({},{}) pieces+={}",
1248                carriers[first].source_face_id,
1249                carriers[second].source_face_id,
1250                imprint.pieces.len() - pieces_before,
1251            );
1252        }
1253    }
1254    if distance < 0.0 || !inward_extended_walls.is_empty() {
1255        // An extended opening-wall carrier (outward, or inward at a reflex
1256        // rim) no longer carries the source outline. OUTWARD it has the GROWN
1257        // outline. Its inner boundary — where the wall ring stops at the
1258        // source solid — is the source outline, and the retained SOURCE faces
1259        // are not carriers, so no carrier×carrier pair ever imprints it.
1260        // Un-cut, the wall stays one whole-plane fragment covering the
1261        // opening void, and the source faces' rim edges dangle one-use.
1262        // Imprint each wall carrier against the retained source faces that
1263        // share an edge with its opening: the wall then fragments into the
1264        // ring (kept) and the opening hole (dropped by the void guard).
1265        for index in 0..carriers.len() {
1266            if !matches!(carriers[index].kind, OffsetFaceRole::Wall) {
1267                continue;
1268            }
1269            if distance > 0.0 && !inward_extended_walls.contains(&index) {
1270                continue;
1271            }
1272            let opening = source_by_id[&carriers[index].source_face_id];
1273            for retained_id in &retained {
1274                let retained_face = source_by_id[retained_id];
1275                if !source_faces_adjacent(opening, retained_face) {
1276                    continue;
1277                }
1278                let neighbor = standalone_face(source, *retained_id)?;
1279                let pair = match build_imprints(
1280                    &carriers[index].solid,
1281                    &neighbor,
1282                    &ImprintOptions {
1283                        tolerance,
1284                        maximum_fit_points: 96,
1285                        // The global lane, as above.
1286                        local_fit: false,
1287                        fit_chunk_points: None,
1288                        maximum_ssi_step: Some((scale * 0.0005).max(tolerance * 100.0)),
1289                    },
1290                ) {
1291                    Ok(pair) => pair,
1292                    Err(error) => {
1293                        os_debug!(
1294                            "wall×source pair ({index},{retained_id}) imprint failed: {error}"
1295                        );
1296                        continue;
1297                    }
1298                };
1299                let pieces_before = imprint.pieces.len();
1300                merge_pair_imprint(
1301                    &mut imprint,
1302                    pair,
1303                    index as u8,
1304                    SOURCE_OPERAND,
1305                    &mut next_piece_id,
1306                    &mut next_vertex_id,
1307                    tolerance,
1308                    scale,
1309                );
1310                os_debug!(
1311                    "wall×source pair ({index},{retained_id}) pieces+={}",
1312                    imprint.pieces.len() - pieces_before,
1313                );
1314            }
1315        }
1316    }
1317    // APEX-CAP ring splits: the cap × cone section lies ON the cone
1318    // carrier's promoted ring edge (an overlap, not a crossing), so
1319    // `build_imprints` mints no edge splits for the cone side. Without them
1320    // the cone keeps ONE closed ring edge while the cap carries the section
1321    // as arcs — mismatched segmentation, both sides one-use. Record the arc
1322    // junctions as splits on the cone's coincident edge.
1323    for (cone_index, cap_index) in &apex_cap_pairs {
1324        let cap_face_id = carriers[*cap_index].solid.shells[0].faces[0].id;
1325        let piece_ids = imprint
1326            .by_face
1327            .iter()
1328            .find(|entry| entry.operand == *cap_index as u8 && entry.face_id == cap_face_id)
1329            .map(|entry| entry.piece_ids.clone())
1330            .unwrap_or_default();
1331        let coincidence_band = 2e-5f64.max(scale * 1e-7);
1332        for piece_id in piece_ids {
1333            let Some((curve, t0, t1)) = imprint
1334                .pieces
1335                .iter()
1336                .find(|piece| piece.id == piece_id)
1337                .map(|piece| (piece.curve.clone(), piece.t0, piece.t1))
1338            else {
1339                continue;
1340            };
1341            for endpoint in [curve.evaluate(t0).or_refuse(KernelStage::Sew, "evaluate")?, curve.evaluate(t1).or_refuse(KernelStage::Sew, "evaluate")?] {
1342                for edge in &carriers[*cone_index].solid.edges {
1343                    if edge.degenerate {
1344                        continue;
1345                    }
1346                    let projection = crate::project_point_to_curve(&edge.curve, endpoint).or_refuse(KernelStage::Sew, "project_point_to_curve")?;
1347                    if projection.distance > coincidence_band
1348                        || projection.u < edge.t0 + 1e-9
1349                        || projection.u > edge.t1 - 1e-9
1350                    {
1351                        continue;
1352                    }
1353                    let parameter = projection.u;
1354                    if let Some(existing) = imprint.edge_splits.iter_mut().find(|split| {
1355                        split.operand == *cone_index as u8 && split.edge_id == edge.id
1356                    }) {
1357                        if !existing
1358                            .parameters
1359                            .iter()
1360                            .any(|value| (*value - parameter).abs() <= 1e-8)
1361                        {
1362                            existing.parameters.push(parameter);
1363                        }
1364                    } else {
1365                        imprint.edge_splits.push(EdgeSplitRecord {
1366                            operand: *cone_index as u8,
1367                            edge_id: edge.id,
1368                            parameters: vec![parameter],
1369                        });
1370                    }
1371                    os_debug!(
1372                        "apex-cap ring split: cone carrier {cone_index} edge {} at t={parameter:.6}",
1373                        edge.id,
1374                    );
1375                }
1376            }
1377        }
1378    }
1379    if debug_enabled() {
1380        for piece in &imprint.pieces {
1381            let start = piece.curve.evaluate(piece.t0);
1382            let mid = piece.curve.evaluate((piece.t0 + piece.t1) * 0.5);
1383            let end = piece.curve.evaluate(piece.t1);
1384            if let (Ok(start), Ok(mid), Ok(end)) = (start, mid, end) {
1385                os_debug!(
1386                    "piece[{}] ({:.4},{:.4},{:.4})..({:.4},{:.4},{:.4})..({:.4},{:.4},{:.4})",
1387                    piece.id,
1388                    start.x,
1389                    start.y,
1390                    start.z,
1391                    mid.x,
1392                    mid.y,
1393                    mid.z,
1394                    end.x,
1395                    end.y,
1396                    end.z,
1397                );
1398            }
1399        }
1400    }
1401    synchronize_smooth_edge_splits(&mut imprint, &smooth.edge_pairs);
1402    // This decides whether an imprint is genuinely the carrier's existing
1403    // trim, not whether independently fitted curves can later be sewn. The
1404    // looser assembly tolerance can erase a short separating branch while it
1405    // is still departing a boundary.
1406    let boundary_coincidence_tolerance = 2e-5f64.max(scale * 1e-7);
1407    let piece_geometry = imprint
1408        .pieces
1409        .iter()
1410        .map(|piece| {
1411            (
1412                piece.id,
1413                FragmentEdgeGeometry {
1414                    curve: piece.curve.clone(),
1415                    t0: piece.t0,
1416                    t1: piece.t1,
1417                },
1418            )
1419        })
1420        .collect::<HashMap<_, _>>();
1421    for (index, carrier) in carriers.iter().enumerate() {
1422        if !matches!(carrier.kind, OffsetFaceRole::Offset) {
1423            continue;
1424        }
1425        let boundaries = carrier
1426            .solid
1427            .edges
1428            .iter()
1429            .map(|edge| FragmentEdgeGeometry {
1430                curve: edge.curve.clone(),
1431                t0: edge.t0,
1432                t1: edge.t1,
1433            })
1434            .collect::<Vec<_>>();
1435        if let Some(by_face) = imprint
1436            .by_face
1437            .iter_mut()
1438            .find(|entry| entry.operand == index as u8)
1439        {
1440            let before = by_face.piece_ids.clone();
1441            by_face.piece_ids.retain(|piece_id| {
1442                let piece = &piece_geometry[piece_id];
1443                !boundaries.iter().any(|boundary| {
1444                    fragment_edge_lies_on(piece, boundary, boundary_coincidence_tolerance)
1445                        .unwrap_or(false)
1446                })
1447            });
1448            if debug_enabled() && before.len() != by_face.piece_ids.len() {
1449                os_debug!(
1450                    "carrier[{index}] dropped boundary-coincident pieces: {:?} -> {:?}",
1451                    before,
1452                    by_face.piece_ids,
1453                );
1454            }
1455        }
1456    }
1457
1458    if debug_enabled() {
1459        for entry in &imprint.by_face {
1460            os_debug!(
1461                "by_face operand={} face={} pieces={:?}",
1462                entry.operand,
1463                entry.face_id,
1464                entry.piece_ids,
1465            );
1466        }
1467    }
1468    let mut split_carriers = Vec::new();
1469    let mut fragments_by_carrier = Vec::new();
1470    for (index, carrier) in carriers.iter().enumerate() {
1471        // Offset carriers keep their boundary un-split (their trims never
1472        // coincide with imprints — the boundary-coincidence filter drops such
1473        // pieces instead). An apex-cap ring breaks that assumption: the
1474        // frustum's ring is its TRIM boundary while the cap crosses it as TWO
1475        // imprint arcs — without splitting, the two sides segment differently
1476        // and both stay one-use. Split exactly the cap-affected carriers.
1477        let split = if matches!(carrier.kind, OffsetFaceRole::Offset)
1478            && !apex_cap_sources.contains(&index)
1479        {
1480            carrier.solid.clone()
1481        } else {
1482            apply_edge_splits(&carrier.solid, index as u8, &imprint)?
1483        };
1484        let fragments = fragment_solid(&split, index as u8, &imprint)?;
1485        os_debug!(
1486            "carrier[{index}] kind={:?} src={} fragments={}",
1487            carrier.kind,
1488            carrier.source_face_id,
1489            fragments.len(),
1490        );
1491        if debug_enabled() {
1492            for (fragment_index, fragment) in fragments.iter().enumerate() {
1493                let mut boundary_count = 0usize;
1494                let mut imprint_pieces = Vec::new();
1495                let mut derived_count = 0usize;
1496                for coedge in fragment
1497                    .loops
1498                    .iter()
1499                    .flat_map(|loop_record| &loop_record.coedges)
1500                {
1501                    match &coedge.source {
1502                        FragmentEdgeSource::Boundary { .. }
1503                        | FragmentEdgeSource::SharedBoundary { .. } => boundary_count += 1,
1504                        FragmentEdgeSource::Imprint { piece_id } => imprint_pieces.push(*piece_id),
1505                        FragmentEdgeSource::Derived { .. } => derived_count += 1,
1506                    }
1507                }
1508                os_debug!(
1509                    "  frag[{index}.{fragment_index}] test=({:.3},{:.3},{:.3}) uv=({:.3},{:.3}) boundary={boundary_count} derived={derived_count} pieces={imprint_pieces:?}",
1510                    fragment.test_point.x, fragment.test_point.y, fragment.test_point.z,
1511                    fragment.test_uv.x, fragment.test_uv.y,
1512                );
1513            }
1514        }
1515        split_carriers.push(split);
1516        fragments_by_carrier.push(fragments);
1517    }
1518    let mut assembly_sources = [(SOURCE_OPERAND, source)]
1519        .into_iter()
1520        .collect::<HashMap<_, _>>();
1521    for (index, carrier) in split_carriers.iter().enumerate() {
1522        assembly_sources.insert(index as u8, carrier);
1523    }
1524
1525    let source_edge_by_id = source
1526        .edges
1527        .iter()
1528        .map(|edge| (edge.id, edge))
1529        .collect::<HashMap<_, _>>();
1530    let retained_faces_with_edges = retained
1531        .iter()
1532        .map(|face_id| {
1533            let face = source_by_id[face_id];
1534            let edges = face
1535                .loops
1536                .iter()
1537                .flat_map(|loop_record| &loop_record.coedges)
1538                .filter_map(|coedge| source_edge_by_id.get(&coedge.edge_id).copied())
1539                .collect::<Vec<_>>();
1540            (face, edges)
1541        })
1542        .collect::<Vec<_>>();
1543    let offset_skin_tolerance = offset_skin_band(scale, distance);
1544    // OUTWARD shells: a wall fragment lying IN FRONT of another opening —
1545    // past that opening's plane on its void side, within the extent of its
1546    // (extended) wall carrier — is not material: an opening removes
1547    // everything in front of it. Such a fragment is the sheet of this wall
1548    // between the source outline and the grown outline on the far side of an
1549    // ADJACENT opening's shared edge (the x∈[−1,0] strip of an opened −Z
1550    // face's wall, in front of the opened −X face). It sits within |distance|
1551    // of a retained face's corner edge, so the void-cross-section guard reads
1552    // it as wall thickness; only the other opening's plane tells it apart.
1553    //
1554    // A RULED opening's wall is curved, so it has no one plane: its front is
1555    // read at the point's foot on the wall carrier, along the carrier's normal
1556    // turned to the opening face's outward side. On the torus wedge that is
1557    // what drops the start and end caps' strips beyond the extended chord,
1558    // which lie within |distance| of the torus and in front of the cone.
1559    let mut opening_planes: Vec<(usize, Vec3, Vec3)> = Vec::new();
1560    let mut opening_ruled: Vec<(usize, f64)> = Vec::new();
1561    if distance < 0.0 {
1562        for (index, carrier) in carriers.iter().enumerate() {
1563            if !matches!(carrier.kind, OffsetFaceRole::Wall) {
1564                continue;
1565            }
1566            let opening = source_by_id[&carrier.source_face_id];
1567            let [u0, u1] = opening.surface.domain_u().or_refuse(KernelStage::Sew, "domain_u")?;
1568            let [v0, v1] = opening.surface.domain_v().or_refuse(KernelStage::Sew, "domain_v")?;
1569            let (u, v) = ((u0 + u1) * 0.5, (v0 + v1) * 0.5);
1570            if ruled_walls.contains(&index) {
1571                let wall = &split_carriers[index].shells[0].faces[0];
1572                let probe = project_point_to_surface(&wall.surface, opening.surface.evaluate(u, v).or_refuse(KernelStage::Sew, "evaluate")?).or_refuse(KernelStage::Sew, "project_point_to_surface")?;
1573                let side = face_normal(wall, probe.u, probe.v)?.dot(face_normal(opening, u, v)?);
1574                opening_ruled.push((index, if side < 0.0 { -1.0 } else { 1.0 }));
1575                continue;
1576            }
1577            if !opening.surface.is_affine().or_refuse(KernelStage::Sew, "is_affine")? {
1578                continue;
1579            }
1580            opening_planes.push((
1581                index,
1582                opening.surface.evaluate(u, v).or_refuse(KernelStage::Sew, "evaluate")?,
1583                face_normal(opening, u, v)?,
1584            ));
1585        }
1586    }
1587    let in_front_of_other_opening = |wall_index: usize, point: Vec3| -> Result<bool, KernelRefusal> {
1588        let band = 2e-3f64.max(scale * 5e-5);
1589        for (index, plane_point, outward) in &opening_planes {
1590            if *index == wall_index || point.sub(*plane_point).dot(*outward) <= band {
1591                continue;
1592            }
1593            let face = &split_carriers[*index].shells[0].faces[0];
1594            let projection = project_point_to_surface(&face.surface, point).or_refuse(KernelStage::Sew, "project_point_to_surface")?;
1595            let uv = Vec2 {
1596                x: projection.u,
1597                y: projection.v,
1598            };
1599            if parameter_point_in_face(face, uv, 1e-8).or_refuse(KernelStage::Sew, "parameter_point_in_face")? != PolygonClass::Outside {
1600                return Ok(true);
1601            }
1602        }
1603        for (index, side) in &opening_ruled {
1604            if *index == wall_index {
1605                continue;
1606            }
1607            let face = &split_carriers[*index].shells[0].faces[0];
1608            let projection = project_point_to_surface(&face.surface, point).or_refuse(KernelStage::Sew, "project_point_to_surface")?;
1609            let outward = face_normal(face, projection.u, projection.v)?.scale(*side);
1610            if point.sub(projection.point).dot(outward) <= band {
1611                continue;
1612            }
1613            let uv = Vec2 {
1614                x: projection.u,
1615                y: projection.v,
1616            };
1617            if parameter_point_in_face(face, uv, 1e-8).or_refuse(KernelStage::Sew, "parameter_point_in_face")? != PolygonClass::Outside {
1618                return Ok(true);
1619            }
1620        }
1621        Ok(false)
1622    };
1623
1624    let mut chosen_offsets = Vec::new();
1625    let mut chosen_walls = Vec::new();
1626    let mut newly_shadowed = HashSet::<u64>::default();
1627    for (index, carrier) in carriers.iter().enumerate() {
1628        let raw_seeds = source_seeds(source, carrier.source_face_id)?;
1629        let mut seeds = Vec::with_capacity(raw_seeds.len());
1630        for raw_seed in raw_seeds {
1631            let seed = if matches!(carrier.kind, OffsetFaceRole::Offset) {
1632                offset_seed(
1633                    source_by_id[&carrier.source_face_id],
1634                    raw_seed,
1635                    &split_carriers[index].shells[0].faces[0].surface,
1636                    distance,
1637                    2e-3f64.max(scale * 5e-5),
1638                )?
1639                .unwrap_or(raw_seed)
1640            } else if inward_extended_walls.contains(&index) {
1641                offset_seed(
1642                    source_by_id[&carrier.source_face_id],
1643                    raw_seed,
1644                    &split_carriers[index].shells[0].faces[0].surface,
1645                    0.0,
1646                    2e-3f64.max(scale * 5e-5),
1647                )?
1648                .unwrap_or(raw_seed)
1649            } else {
1650                raw_seed
1651            };
1652            if seed.sub(raw_seed).length() > 1e-9 {
1653                os_debug!(
1654                    "  seed[{index}] moved from ({:.4},{:.4}) to the offset image ({:.4},{:.4})",
1655                    raw_seed.x,
1656                    raw_seed.y,
1657                    seed.x,
1658                    seed.y
1659                );
1660            }
1661            seeds.push(seed);
1662        }
1663        let seed = seeds[0];
1664        if matches!(carrier.kind, OffsetFaceRole::Offset) {
1665            // Fragments whose test point sits at the full offset distance
1666            // from every retained source face are the true offset-skin
1667            // regions. Fragments closer to some other face are shadowed
1668            // pockets/strips that another carrier owns.
1669            // A fragment whose offset lies ON another retained face's offset
1670            // (material exactly twice the shell distance thick: a slot, fin or
1671            // floor at t = 2d) reads as SHADOWED by that face. One hair
1672            // thinner, the same fragment reads shadowed on its own and is
1673            // dropped by the on-skin filter, or omits its carrier by the
1674            // skinless rule below, so the answer is the limit of that
1675            // construction: the census's L∞ forms are continuous across the
1676            // onset. Without this the fragment read On, was selected, and the
1677            // shell refused as "a cavity pinched to zero thickness". The
1678            // coincidence test is `coincident_offset_face`'s (the other face's
1679            // distance within `offset_skin_tolerance` of |d|, its foot on the
1680            // opposite side of the point from this face's own), taken only on
1681            // the THIN side of the onset.
1682            let mut readings = Vec::with_capacity(fragments_by_carrier[index].len());
1683            for fragment in &fragments_by_carrier[index] {
1684                let reading = offset_skin_reading(
1685                    fragment.test_point,
1686                    &retained_faces_with_edges,
1687                    distance,
1688                    offset_skin_tolerance,
1689                );
1690                let reading = match reading {
1691                    SkinReading::On => match coincident_offset_face(
1692                        fragment.test_point,
1693                        carrier.source_face_id,
1694                        &retained_faces_with_edges,
1695                        distance,
1696                        offset_skin_tolerance,
1697                    )? {
1698                        // ONE-SIDED and tight: only material no thicker than
1699                        // 2|d| (to 10× the model tolerance) takes the thin
1700                        // construction, which is exact there. A wall inside
1701                        // the skin band but genuinely thicker than 2|d| keeps
1702                        // its sliver cavity's named refusal below; taking the
1703                        // thin construction there would fill a real cavity.
1704                        Some((face, separation)) if separation <= distance.abs() + tolerance * 10.0 => {
1705                            SkinReading::Shadowed { face, separation }
1706                        }
1707                        _ => SkinReading::On,
1708                    },
1709                    other => other,
1710                };
1711                readings.push(reading);
1712            }
1713            for (fragment, reading) in fragments_by_carrier[index].iter().zip(&readings) {
1714                if let SkinReading::Unmeasured { face, reason } = reading {
1715                    return Err(unmeasured_skin_refusal(fragment.test_point, *face, reason));
1716                }
1717            }
1718            // EVERY fragment measured nearer than the distance to some other
1719            // retained face: this support's offset is SHADOWED over its whole
1720            // extended carrier — the side walls of a slot narrower than 2|d|
1721            // grown outward, a pocket floor within 2d of the face under it.
1722            // It owns no skin. It used to switch the filter off and keep its
1723            // seeded fragment anyway, which built a closed, validating shell
1724            // with an inverted box inside it (+90 on a width-2 slot grown by
1725            // 1.5, with the 90 of material at its centre missing). Dropping
1726            // only its fragments is not enough: its carrier has already cut
1727            // its neighbours, and the top skin over that slot keeps a hole
1728            // nothing fills. So the pass is re-run with the support omitted
1729            // before the imprint, like a collapsed one. An apex-cap sphere
1730            // shares its cone's source id and is not judged here.
1731            //
1732            // Only the FIRST pass omits. A test point speaks for its fragment
1733            // because every other carrier's imprint bounds it; omitting a
1734            // carrier removes cuts, merges fragments and moves test points,
1735            // but changes no distance, so a support that reads shadowed only
1736            // AFTER an omission is reading a test point that no longer stands
1737            // for its fragment. Measured on two bosses 1 apart grown outward
1738            // by 1.5: once the facing boss walls were omitted, the plate's
1739            // grown top — one uncut fragment, mostly skin — read shadowed at
1740            // its single test point.
1741            //
1742            // "Every fragment" counts a fragment the selection would drop by
1743            // CLASS as not skin, as the selection does: the extended carrier's
1744            // surplus past a slot's end reads at least the distance from every
1745            // face, lies inside the source, and on a slot 1 wide grown outward
1746            // left the slot floor unjudged until the side walls were gone.
1747            // ADRIFT: a fragment whose test point is farther from its OWN face
1748            // than the distance, in the mitered metric, is not that face's skin
1749            // — it is inside the cavity, past where its neighbours' offsets cut
1750            // it. A rib, a pin or a divider thinner than 2d shelled inward
1751            // keeps such fragments, and the assembly then fails on them.
1752            let adrift = fragments_by_carrier[index]
1753                .iter()
1754                .map(|fragment| {
1755                    point_adrift_of_its_own_offset(
1756                        fragment.test_point,
1757                        carrier.source_face_id,
1758                        &retained_faces_with_edges,
1759                        distance,
1760                        offset_skin_tolerance,
1761                    )
1762                })
1763                .collect::<Vec<_>>();
1764            let mut skinless = readings
1765                .iter()
1766                .any(|reading| matches!(reading, SkinReading::Shadowed { .. }))
1767                || adrift.iter().any(|flag| *flag);
1768            for (fragment, reading) in fragments_by_carrier[index].iter().zip(&readings) {
1769                if !skinless {
1770                    break;
1771                }
1772                if matches!(reading, SkinReading::On)
1773                    && !adrift[fragments_by_carrier[index]
1774                        .iter()
1775                        .position(|other| std::ptr::eq(other, fragment))
1776                        .unwrap_or(0)]
1777                {
1778                    let class = classify_point(fragment.test_point, source, tolerance * 10.0).or_refuse(KernelStage::Sew, "classify_point")?.class;
1779                    skinless = (distance > 0.0 && class == PointClass::Out)
1780                        || (distance < 0.0 && class == PointClass::In);
1781                }
1782            }
1783            if skinless && !apex_cap_sources.contains(&index) {
1784                if !shadowed_supports.is_empty() {
1785                    let mut omitted = shadowed_supports.iter().copied().collect::<Vec<_>>();
1786                    omitted.sort_unstable();
1787                    return Err(KernelRefusal::unsupported(KernelStage::Select, "offset_shell_shadow_cascade", format!(
1788                        "offset_shell: omitting the shadowed offsets of source faces {omitted:?} \
1789                         leaves the offset of source face {} shadowed at every one of its {} \
1790                         fragment test points, which no longer stand for their fragments once \
1791                         those carriers stop cutting it",
1792                        carrier.source_face_id,
1793                        readings.len()
1794                    )));
1795                }
1796                os_debug!(
1797                    "  select[{index}] src={} SHADOWED over all {} fragment(s): {:?}",
1798                    carrier.source_face_id,
1799                    readings.len(),
1800                    readings
1801                );
1802                newly_shadowed.insert(carrier.source_face_id);
1803                continue;
1804            }
1805            // Partly shadowed: keep a bounded sample of the shadowed test
1806            // points, so a refusal can say where two offsets cross.
1807            for (fragment, reading) in fragments_by_carrier[index].iter().zip(&readings) {
1808                if shadowed_points.len() >= MAXIMUM_CROSSING_READS {
1809                    break;
1810                }
1811                if matches!(reading, SkinReading::Shadowed { .. }) {
1812                    shadowed_points.push((fragment.test_point, carrier.source_face_id));
1813                }
1814            }
1815            let on_skin = readings
1816                .iter()
1817                .zip(&adrift)
1818                .map(|(reading, adrift)| matches!(reading, SkinReading::On) && !adrift)
1819                .collect::<Vec<_>>();
1820            // A synthetic apex sphere has no source-face seed of its own to
1821            // fall back to. When its whole cap is hidden by retained offsets,
1822            // keep its cuts but contribute no skin. Otherwise the fallback
1823            // selects the uncut sphere as a stray patch inside the material.
1824            // The parent cone remains subject to the ordinary selection.
1825            let skin_filter_active = on_skin.iter().any(|flag| *flag)
1826                || apex_cap_pairs.iter().any(|(_, cap)| *cap == index);
1827            let opening_limits = if inward_extended_walls.is_empty() {
1828                Vec::new()
1829            } else {
1830                let openings = inward_extended_walls
1831                    .iter()
1832                    .map(|index| source_by_id[&carriers[*index].source_face_id])
1833                    .collect::<Vec<_>>();
1834                opening_trim_halfspaces(source_by_id[&carrier.source_face_id], &openings, tolerance)?
1835            };
1836            let mut classified = Vec::new();
1837            let mut viable = Vec::new();
1838            for (fragment_index, fragment) in fragments_by_carrier[index].iter().enumerate() {
1839                let class = classify_point(fragment.test_point, source, tolerance * 10.0).or_refuse(KernelStage::Sew, "classify_point")?.class;
1840                let excluded_by_class = (distance > 0.0 && class == PointClass::Out)
1841                    || (distance < 0.0 && class == PointClass::In);
1842                let contact = if excluded_by_class {
1843                    false
1844                } else {
1845                    distance > 0.0
1846                        && fragment_has_sustained_source_contact(
1847                            fragment,
1848                            &source_faces,
1849                            &assembly_sources,
1850                            &imprint,
1851                            2e-3f64.max(scale * 5e-5),
1852                        )?
1853                };
1854                os_debug!(
1855                    "  select[{index}.{fragment_index}] class={class:?} contact={contact} on_skin={} seed_in={:?} uv=({:.3},{:.3})",
1856                    on_skin[fragment_index],
1857                    parameter_point_in_face(&fragment_as_trim(fragment), seed, 1e-8),
1858                    fragment.test_uv.x, fragment.test_uv.y,
1859                );
1860                let past_opening = opening_limits.iter().any(|(origin, normal)| {
1861                    fragment.test_point.sub(*origin).dot(*normal) < -tolerance * 10.0
1862                });
1863                if excluded_by_class
1864                    || past_opening
1865                    || (skin_filter_active && !on_skin[fragment_index])
1866                {
1867                    continue;
1868                }
1869                classified.push(fragment.clone());
1870                if contact {
1871                    continue;
1872                }
1873                viable.push(fragment.clone());
1874            }
1875            // One skin per REGION of the source face (see `source_seeds`):
1876            // every region of a pinched face is offset like a face of its
1877            // own, and the selection below answers each region's seed. An
1878            // ordinary face has the one seed and the one answer it always had.
1879            let mut region_skins = Vec::new();
1880            for &seed in &seeds {
1881                os_debug!("  select[{index}] seed=({:.4},{:.4})", seed.x, seed.y);
1882                let seeded = viable
1883                    .iter()
1884                    .filter(|fragment| {
1885                        parameter_point_in_face(&fragment_as_trim(fragment), seed, 1e-8)
1886                            .is_ok_and(|class| class != PolygonClass::Outside)
1887                    })
1888                    .cloned()
1889                    .collect::<Vec<_>>();
1890                let seeded_classified = classified
1891                    .iter()
1892                    .filter(|fragment| {
1893                        parameter_point_in_face(&fragment_as_trim(fragment), seed, 1e-8)
1894                            .is_ok_and(|class| class != PolygonClass::Outside)
1895                    })
1896                    .cloned()
1897                    .collect::<Vec<_>>();
1898                let selected = if viable.is_empty() {
1899                    if !skin_filter_active {
1900                        // NOTHING on this carrier reads on-skin: every classified
1901                        // fragment's test point is measurably CLOSER than the
1902                        // offset distance to some retained face — a buried strip
1903                        // another carrier owns. Forcing the largest one in anyway
1904                        // plants a wrong skin patch that collides with the
1905                        // completion phase's reconstruction (3-use edges).
1906                        // Contribute nothing: `complete_opening_boundary_cycles`
1907                        // rebuilds the true skin from the carrier surface, and the
1908                        // watertight gate still refuses if it cannot.
1909                        None
1910                    } else {
1911                        let mut by_area = classified
1912                            .iter()
1913                            .cloned()
1914                            .map(|fragment| {
1915                                let area = parameter_space_area(&fragment_as_trim(&fragment)).or_refuse(KernelStage::Sew, "parameter_space_area")?.abs();
1916                                Ok((fragment, area))
1917                            })
1918                            .collect::<Result<Vec<_>, KernelRefusal>>()?;
1919                        by_area.sort_by(|a, b| b.1.total_cmp(&a.1));
1920                        by_area.into_iter().next().map(|entry| entry.0)
1921                    }
1922                } else if !seeded_classified.is_empty() {
1923                    let mut candidates = seeded_classified;
1924                    candidates.sort_by(|a, b| {
1925                        a.test_uv
1926                            .sub(seed)
1927                            .length()
1928                            .total_cmp(&b.test_uv.sub(seed).length())
1929                    });
1930                    candidates.into_iter().next()
1931                } else if viable.len() > 2 {
1932                    let mut by_area = viable
1933                        .iter()
1934                        .cloned()
1935                        .map(|fragment| {
1936                            let area = parameter_space_area(&fragment_as_trim(&fragment)).or_refuse(KernelStage::Sew, "parameter_space_area")?.abs();
1937                            Ok((fragment, area))
1938                        })
1939                        .collect::<Result<Vec<_>, KernelRefusal>>()?;
1940                    by_area.sort_by(|a, b| b.1.total_cmp(&a.1));
1941                    by_area.into_iter().next().map(|entry| entry.0)
1942                } else {
1943                    let mut candidates = if seeded.is_empty() { viable.clone() } else { seeded };
1944                    candidates.sort_by(|a, b| {
1945                        a.test_uv
1946                            .sub(seed)
1947                            .length()
1948                            .total_cmp(&b.test_uv.sub(seed).length())
1949                    });
1950                    candidates.into_iter().next()
1951                };
1952                if let Some(fragment) = &selected {
1953                    os_debug!(
1954                        "  select[{index}] src={} chosen test point departs its own face's offset \
1955                         distance by {:?}",
1956                        carrier.source_face_id,
1957                        own_offset_departure(
1958                            fragment.test_point,
1959                            carrier.source_face_id,
1960                            &retained_faces_with_edges,
1961                            distance
1962                        )
1963                    );
1964                    os_debug!(
1965                        "  select[{index}] src={} chosen test point departs in the MITER metric by {:?}",
1966                        carrier.source_face_id,
1967                        own_miter_departure(
1968                            fragment.test_point,
1969                            carrier.source_face_id,
1970                            &retained_faces_with_edges,
1971                            distance
1972                        )
1973                    );
1974                    if let Some((other, separation)) = coincident_offset_face(
1975                        fragment.test_point,
1976                        carrier.source_face_id,
1977                        &retained_faces_with_edges,
1978                        distance,
1979                        offset_skin_tolerance,
1980                    )? {
1981                        return Err(KernelRefusal::unsupported(KernelStage::Select, "offset_shell_coincident_offsets", format!(
1982                            "offset_shell: the offsets of source faces {} and {other} coincide near \
1983                             ({:.4}, {:.4}, {:.4}) — the material between those faces is twice the \
1984                             shell distance thick (separation {separation:.6} against {:.6}, band \
1985                             {offset_skin_tolerance:.1e}), so the cavity between them has no \
1986                             thickness there; a cavity pinched to zero thickness is not supported",
1987                            carrier.source_face_id,
1988                            fragment.test_point.x,
1989                            fragment.test_point.y,
1990                            fragment.test_point.z,
1991                            distance.abs(),
1992                        )));
1993                    }
1994                }
1995                if let Some(fragment) = selected {
1996                    // A pinched face's seeds each find their own region; a
1997                    // fallback that ignores the seed can answer the same
1998                    // fragment twice, and one skin must enter once.
1999                    if !region_skins
2000                        .iter()
2001                        .any(|other: &FaceFragmentRecord| {
2002                            other.test_point.sub(fragment.test_point).length() <= 1e-12
2003                        })
2004                    {
2005                        region_skins.push(fragment);
2006                    }
2007                }
2008            }
2009            if !inward_extended_walls.is_empty() && skin_filter_active {
2010                // The extended opening cuts beyond the source footprint.
2011                // Other finite carriers can subdivide a connected offset
2012                // region there (the box/revolve report splits both box caps
2013                // at x=1). A single source seed keeps only one of those cells.
2014                // Keep every cell already proved inside the source and on
2015                // the offset skin, then merge their shared boundaries.
2016                region_skins = classified;
2017            }
2018            chosen_offsets.extend(region_skins);
2019        } else {
2020            // Keep arrangement order stable. Besides making the result
2021            // reproducible, this matches the reference kernel's Set insertion
2022            // order when the final manifold-wall guard admits fragments.
2023            let opening = source_by_id[&carrier.source_face_id];
2024            let wall_seeds = opening_wall_seeds(
2025                opening,
2026                &split_carriers[index].shells[0].faces[0],
2027                source,
2028                &opening_set,
2029                distance,
2030            )?;
2031            // The wall-seed exemptions below rest on "the coplanar-rim weld
2032            // cuts the void hole later" — which only ever happens on PLANAR
2033            // opening carriers. A curved carrier (e.g. the sphere face used
2034            // directly as the opening) keeps its un-split fragment forever:
2035            // exempting it would seal the opening with the source face itself
2036            // and fabricate a closed hollow solid. Curved carriers therefore
2037            // get the strict void guard.
2038            let carrier_planar =
2039                surface_is_planar(&split_carriers[index].shells[0].faces[0].surface, tolerance)
2040                    .unwrap_or(false);
2041            let extended_inward = inward_extended_walls.contains(&index);
2042            let outside_source = |point| -> Result<bool, KernelRefusal> {
2043                Ok(extended_inward
2044                    && classify_point(point, source, tolerance * 10.0)
2045                        .or_refuse(KernelStage::Select, "classify_point")?.class == PointClass::Out)
2046            };
2047            let wall_on_skin = |point| {
2048                if extended_inward {
2049                    let inside_source = classify_point(point, source, tolerance * 10.0)
2050                        .or_refuse(KernelStage::Select, "classify_point")?.class == PointClass::In;
2051                    point_on_inward_opening_skin(
2052                        point,
2053                        &retained_faces_with_edges,
2054                        &reflex_wall_neighbors[&index],
2055                        inside_source,
2056                        distance,
2057                        offset_skin_tolerance,
2058                    )
2059                } else {
2060                    point_on_offset_skin(
2061                        point,
2062                        &retained_faces_with_edges,
2063                        distance,
2064                        offset_skin_tolerance,
2065                    )
2066                }
2067            };
2068            let mut selected_indices = Vec::new();
2069            for (fragment_index, fragment) in fragments_by_carrier[index].iter().enumerate() {
2070                if outside_source(fragment.test_point)? {
2071                    continue;
2072                }
2073                if parameter_point_in_face(&fragment_as_trim(fragment), seed, 1e-8).or_refuse(KernelStage::Sew, "parameter_point_in_face")?
2074                    == PolygonClass::Outside
2075                {
2076                    if in_front_of_other_opening(index, fragment.test_point)? {
2077                        os_debug!(
2078                            "  wall[{index}.{fragment_index}] skipped: in front of another \
2079                             opening at ({:.3},{:.3},{:.3})",
2080                            fragment.test_point.x,
2081                            fragment.test_point.y,
2082                            fragment.test_point.z,
2083                        );
2084                        continue;
2085                    }
2086                    // Wall = the material cross-section exposed at the
2087                    // opening: every point of a genuine wall fragment lies
2088                    // WITHIN the offset distance of some retained face (it is
2089                    // the thickness between that face and its offset skin). A
2090                    // fragment at ≥ distance from EVERY retained face — and
2091                    // containing no expected wall seed (an un-split cap
2092                    // fragment can span both the void AND the wall annulus;
2093                    // the coplanar-rim weld cuts its hole later) — is the
2094                    // core's own cross-section: a HOLE in the opening (e.g.
2095                    // the corner void where a carve's offset circle passes
2096                    // just inside the opening's corner). Welding it shut
2097                    // would fabricate a wall over the void.
2098                    if wall_on_skin(fragment.test_point)? && !(carrier_planar
2099                        && !extended_inward
2100                        && wall_seeds.iter().any(|wall_seed| {
2101                            parameter_point_in_face(&fragment_as_trim(fragment), *wall_seed, 1e-8)
2102                                .is_ok_and(|class| class != PolygonClass::Outside)
2103                        }))
2104                    {
2105                        os_debug!(
2106                            "  wall[{index}.{fragment_index}] skipped: void cross-section at \
2107                             ({:.3},{:.3},{:.3})",
2108                            fragment.test_point.x,
2109                            fragment.test_point.y,
2110                            fragment.test_point.z,
2111                        );
2112                        continue;
2113                    }
2114                    // A SURPLUS fragment beyond a sharp convex corner: it
2115                    // touches the wall's own outline, no edge of it was cut by
2116                    // the SOURCE (the rim every genuine wall region borders),
2117                    // and it holds no wall seed. The void test above reads it
2118                    // as material, because it measures a planar neighbour in the
2119                    // MITER metric (its extended plane, 0.277 from this region
2120                    // on the free-form D turning 83.7° at d = 0.3). The
2121                    // opening wall's clearance cuts such a region off between
2122                    // the carriers' tails past the mitre, and kept, it
2123                    // assembled as "non-integral genus" (8 of the D band's 42
2124                    // cells, 2026-09-26).
2125                    if distance < 0.0
2126                        && wall_surplus_beyond_the_sections(fragment, index as u8, |piece_id| {
2127                            imprint.pieces.iter().find(|piece| piece.id == piece_id).is_some_and(|piece| {
2128                                piece.support_faces.iter().any(|key| key.operand == SOURCE_OPERAND)
2129                            })
2130                        })
2131                        && !wall_seeds.iter().any(|wall_seed| {
2132                            parameter_point_in_face(&fragment_as_trim(fragment), *wall_seed, 1e-8)
2133                                .is_ok_and(|class| class != PolygonClass::Outside)
2134                        })
2135                    {
2136                        os_debug!(
2137                            "  wall[{index}.{fragment_index}] skipped: surplus beyond the sections at \
2138                             ({:.3},{:.3},{:.3}), touching the wall outline and no source rim",
2139                            fragment.test_point.x,
2140                            fragment.test_point.y,
2141                            fragment.test_point.z,
2142                        );
2143                        continue;
2144                    }
2145                    selected_indices.push(fragment_index);
2146                }
2147            }
2148            for wall_seed in wall_seeds {
2149                let fragments = &fragments_by_carrier[index];
2150                let nearest_containing = fragments
2151                    .iter()
2152                    .enumerate()
2153                    .filter(|(_, fragment)| {
2154                        parameter_point_in_face(&fragment_as_trim(fragment), wall_seed, 1e-8)
2155                            .is_ok_and(|class| class != PolygonClass::Outside)
2156                    })
2157                    .min_by(|(_, first), (_, second)| {
2158                        first
2159                            .test_uv
2160                            .sub(wall_seed)
2161                            .length()
2162                            .total_cmp(&second.test_uv.sub(wall_seed).length())
2163                    })
2164                    .map(|(fragment_index, _)| fragment_index);
2165                // Arrangement fitting can leave the seed just outside every
2166                // fragment by a small amount. Match the reference kernel's
2167                // geometric fallback instead of silently omitting the wall
2168                // fragment and leaving the assembled shell open.
2169                let nearest = if nearest_containing.is_some() {
2170                    nearest_containing
2171                } else {
2172                    let seed_point = split_carriers[index].shells[0].faces[0]
2173                        .surface
2174                        .evaluate(wall_seed.x, wall_seed.y).or_refuse(KernelStage::Sew, "evaluate")?;
2175                    fragments
2176                        .iter()
2177                        .enumerate()
2178                        .min_by(|(_, first), (_, second)| {
2179                            first
2180                                .test_point
2181                                .sub(seed_point)
2182                                .length()
2183                                .total_cmp(&second.test_point.sub(seed_point).length())
2184                        })
2185                        .map(|(fragment_index, _)| fragment_index)
2186                };
2187                if let Some(fragment_index) = nearest {
2188                    let candidate = &fragments_by_carrier[index][fragment_index];
2189                    if outside_source(candidate.test_point)?
2190                        || in_front_of_other_opening(index, candidate.test_point)?
2191                    {
2192                        continue;
2193                    }
2194                    // Void-cross-section guard. On a PLANAR carrier it applies
2195                    // to the GEOMETRIC FALLBACK only: a fragment that
2196                    // genuinely contains this wall seed is wall content by
2197                    // construction (even when un-split it also spans void —
2198                    // the rim weld cuts the hole later), but a merely-nearest
2199                    // fragment must not resurrect a hole in the opening. On a
2200                    // CURVED carrier no later weld cuts the hole, so even a
2201                    // seed-containing fragment gets the strict guard.
2202                    // Extended inward walls are cut by the source as well:
2203                    // a seed must not resurrect the opposite side of a
2204                    // reflex rim merely because its unsigned distance is small.
2205                    if (nearest_containing.is_none() || !carrier_planar || extended_inward)
2206                        && wall_on_skin(candidate.test_point)?
2207                    {
2208                        os_debug!(
2209                            "  wall[{index}.{fragment_index}] seed-hit skipped: void \
2210                             cross-section (planar={carrier_planar}) at ({:.3},{:.3},{:.3})",
2211                            candidate.test_point.x,
2212                            candidate.test_point.y,
2213                            candidate.test_point.z,
2214                        );
2215                        continue;
2216                    }
2217                    let existing = chosen_offsets.iter().cloned().chain(
2218                        selected_indices
2219                            .iter()
2220                            .map(|selected| fragments_by_carrier[index][*selected].clone()),
2221                    );
2222                    if !selected_indices.contains(&fragment_index)
2223                        && !would_overuse_existing_boundary(
2224                            candidate,
2225                            existing,
2226                            &assembly_sources,
2227                            &imprint,
2228                            2e-3,
2229                        )?
2230                    {
2231                        selected_indices.push(fragment_index);
2232                    }
2233                }
2234            }
2235            for fragment_index in selected_indices.iter().copied() {
2236                let mut fragment = fragments_by_carrier[index][fragment_index].clone();
2237                // Past a reflex rim the wall closes the cavity on the other
2238                // side of the opening plane, so its normal reverses too.
2239                if extended_inward
2240                    && classify_point(fragment.test_point, source, tolerance * 10.0)
2241                        .or_refuse(KernelStage::Select, "classify_point")?.class == PointClass::In
2242                {
2243                    flip_fragment(&mut fragment)?;
2244                }
2245                chosen_walls.push(fragment);
2246            }
2247        }
2248    }
2249    if debug_enabled() {
2250        for fragment in &chosen_offsets {
2251            os_debug!(
2252                "chosen offset: operand={} src={} test=({:.3},{:.3},{:.3})",
2253                fragment.operand,
2254                carriers[fragment.operand as usize].source_face_id,
2255                fragment.test_point.x,
2256                fragment.test_point.y,
2257                fragment.test_point.z,
2258            );
2259            for (loop_index, loop_record) in fragment.loops.iter().enumerate() {
2260                for coedge in &loop_record.coedges {
2261                    let surface = &fragment.surface;
2262                    let [c0, c1] = coedge.pcurve.domain().unwrap_or([0.0, 0.0]);
2263                    let uv0 = coedge.pcurve.evaluate(c0);
2264                    let uv1 = coedge.pcurve.evaluate(c1);
2265                    if let (Ok(uv0), Ok(uv1)) = (uv0, uv1) {
2266                        let p0 = surface.evaluate(uv0.x, uv0.y);
2267                        let p1 = surface.evaluate(uv1.x, uv1.y);
2268                        if let (Ok(p0), Ok(p1)) = (p0, p1) {
2269                            let kind = match &coedge.source {
2270                                FragmentEdgeSource::Boundary { edge_id, .. } => {
2271                                    format!("bnd:{edge_id}")
2272                                }
2273                                FragmentEdgeSource::SharedBoundary { edge_id, .. } => {
2274                                    format!("sbnd:{edge_id}")
2275                                }
2276                                FragmentEdgeSource::Imprint { piece_id } => {
2277                                    format!("imp:{piece_id}")
2278                                }
2279                                FragmentEdgeSource::Derived { .. } => "derived".to_string(),
2280                            };
2281                            os_debug!(
2282                                "    loop{loop_index} {kind} ({:.4},{:.4},{:.4})..({:.4},{:.4},{:.4})",
2283                                p0.x, p0.y, p0.z, p1.x, p1.y, p1.z,
2284                            );
2285                        }
2286                    }
2287                }
2288            }
2289        }
2290        for fragment in &chosen_walls {
2291            os_debug!(
2292                "chosen wall: operand={} src={} test=({:.3},{:.3},{:.3})",
2293                fragment.operand,
2294                carriers[fragment.operand as usize].source_face_id,
2295                fragment.test_point.x,
2296                fragment.test_point.y,
2297                fragment.test_point.z,
2298            );
2299        }
2300    }
2301    if !newly_shadowed.is_empty() {
2302        let mut omitted = shadowed_supports.clone();
2303        omitted.extend(newly_shadowed.iter().copied());
2304        os_debug!(
2305            "re-running without shadowed offset supports {:?} (omitted before: {:?})",
2306            newly_shadowed,
2307            shadowed_supports
2308        );
2309        // What this pass sampled belongs to carriers the next pass may not
2310        // build.
2311        shadowed_points.clear();
2312        return offset_shell_impl(
2313            source,
2314            opening_face_ids,
2315            distance,
2316            extend_oblique_carriers,
2317            extension_fired,
2318            reflex_rebuilds,
2319            &omitted,
2320            shadowed_points,
2321            omitted_supports,
2322            vacated_notes,
2323        );
2324    }
2325    if chosen_offsets.is_empty() && !shadowed_supports.is_empty() {
2326        let mut omitted = shadowed_supports.iter().copied().collect::<Vec<_>>();
2327        omitted.sort_unstable();
2328        return Err(KernelRefusal::unsupported(KernelStage::Select, "offset_shell_all_shadowed", format!(
2329            "offset_shell: no retained face keeps any offset skin once the shadowed offsets of \
2330             source faces {omitted:?} are omitted — the solid is nowhere thicker than twice the \
2331             shell distance, so there is no cavity to shell"
2332        )));
2333    }
2334    if chosen_offsets.is_empty() {
2335        return Err(KernelRefusal::internal(KernelStage::Select, "offset_shell_no_boundary", "offset_shell: carrier intersections produced no offset boundary"));
2336    }
2337    // Opening-wall fragments can overlap at dense carrier junctions. The
2338    // seed-time guard above handles geometric containment; this final pass
2339    // mirrors the topology-level rule and counts exact fragment edge sources.
2340    let mut boundary_use_count = HashMap::<[i64; 9], usize>::default();
2341    for fragment in &chosen_offsets {
2342        for coedge in fragment
2343            .loops
2344            .iter()
2345            .flat_map(|loop_record| &loop_record.coedges)
2346        {
2347            let key = fragment_edge_segment_key(&coedge.source, &assembly_sources, &imprint)?;
2348            *boundary_use_count.entry(key).or_default() += 1;
2349        }
2350    }
2351    let mut manifold_walls = Vec::new();
2352    for fragment in chosen_walls {
2353        let keys = fragment
2354            .loops
2355            .iter()
2356            .flat_map(|loop_record| &loop_record.coedges)
2357            .map(|coedge| fragment_edge_segment_key(&coedge.source, &assembly_sources, &imprint))
2358            .collect::<Result<Vec<_>, _>>()?;
2359        if !keys
2360            .iter()
2361            .any(|key| boundary_use_count.get(key).copied().unwrap_or(0) >= 2)
2362        {
2363            for key in keys {
2364                *boundary_use_count.entry(key).or_default() += 1;
2365            }
2366            manifold_walls.push(fragment);
2367        }
2368    }
2369    os_debug!(
2370        "manifold walls kept: {} operands={:?}",
2371        manifold_walls.len(),
2372        manifold_walls
2373            .iter()
2374            .map(|fragment| fragment.operand)
2375            .collect::<Vec<_>>(),
2376    );
2377    let chosen_walls = manifold_walls;
2378
2379    let source_fragments = fragment_solid(source, SOURCE_OPERAND, &empty_imprint())?;
2380    let mut selected = source_fragments
2381        .into_iter()
2382        .filter(|fragment| retained.contains(&fragment.source_face_id))
2383        .collect::<Vec<_>>();
2384    let mut face_images = selected
2385        .iter()
2386        .map(|fragment| OffsetShellFaceImageRecord {
2387            role: OffsetFaceRole::Source,
2388            source_face_id: fragment.source_face_id,
2389        })
2390        .collect::<Vec<_>>();
2391    if distance < 0.0 {
2392        for fragment in &mut selected {
2393            flip_fragment(fragment)?;
2394        }
2395    }
2396    if distance > 0.0 {
2397        for fragment in &mut chosen_offsets {
2398            flip_fragment(fragment)?;
2399        }
2400    }
2401    face_images.extend(
2402        chosen_offsets
2403            .iter()
2404            .map(|fragment| OffsetShellFaceImageRecord {
2405                role: OffsetFaceRole::Offset,
2406                source_face_id: carriers[fragment.operand as usize].source_face_id,
2407            }),
2408    );
2409    face_images.extend(
2410        chosen_walls
2411            .iter()
2412            .map(|fragment| OffsetShellFaceImageRecord {
2413                role: OffsetFaceRole::Wall,
2414                source_face_id: carriers[fragment.operand as usize].source_face_id,
2415            }),
2416    );
2417    selected.extend(chosen_offsets);
2418    selected.extend(chosen_walls);
2419    let mut solid = assemble_open_fragments(
2420        selected,
2421        &assembly_sources,
2422        &imprint,
2423        2e-3f64.max(scale * 5e-5),
2424    )?;
2425    orient_open_solid_faces(&mut solid)?;
2426    let connector_images = complete_sharp_offset_connectors(
2427        &mut solid,
2428        source,
2429        &face_images,
2430        distance,
2431        2e-3f64.max(scale * 5e-5),
2432    )?;
2433    os_debug!(
2434        "sharp connector completion added {} faces",
2435        connector_images.len()
2436    );
2437    if !connector_images.is_empty() {
2438        // Wall fragments were arranged before the sharp seam was closed, so
2439        // their prospective inner boundary was an open chain. Rebuild the
2440        // wall from the now-closed source/offset cycles instead of retaining
2441        // that pre-connector fragment.
2442        for shell in &mut solid.shells {
2443            shell.faces.retain(|face| {
2444                !matches!(
2445                    face_images
2446                        .get(face.id.saturating_sub(1) as usize)
2447                        .map(|image| image.role),
2448                    Some(OffsetFaceRole::Wall)
2449                )
2450            });
2451        }
2452        solid.shells.retain(|shell| !shell.faces.is_empty());
2453    }
2454    face_images.extend(connector_images);
2455    orient_open_solid_faces(&mut solid)?;
2456    // Weld curved-face offset rims that the wall fragmentation left orphaned
2457    // into their coplanar opening cap (cylinder / straight-hole shells). This
2458    // is the seam-rim consistency step the offset pipeline previously lacked.
2459    let welded_rims = weld_coplanar_orphan_rims(&mut solid, 2e-3f64.max(scale * 5e-5))?;
2460    os_debug!("welded {welded_rims} coplanar orphan rims into opening caps");
2461    // A rim that is not a circle: a free-form or non-iso rim whose offset image
2462    // falls short of the opening. It runs after every earlier weld because it
2463    // only ever sees rims none of them closed.
2464    let rim_band = weld_free_form_rim_bands(
2465        &mut solid,
2466        &mut face_images,
2467        &carriers,
2468        source,
2469        2e-3f64.max(scale * 5e-5),
2470    )?;
2471    let rim_bands = rim_band.welded;
2472    os_debug!("welded {rim_bands} free-form rim bands");
2473    // Weld leftover coaxial coplanar rim PAIRS (a through-hole's exposed wall
2474    // thickness at each opening) into new planar annulus faces.
2475    let pair_rims =
2476        weld_coplanar_rim_pair_annuli(&mut solid, &mut face_images, 2e-3f64.max(scale * 5e-5))?;
2477    os_debug!("welded {pair_rims} coplanar rim pairs into annulus walls");
2478    if debug_enabled() {
2479        for (shell_index, shell) in solid.shells.iter().enumerate() {
2480            for face in &shell.faces {
2481                os_debug!(
2482                    "POSTWELD shell {} face {} same_sense={} loops={}",
2483                    shell_index,
2484                    face.id,
2485                    face.same_sense,
2486                    face.loops.len()
2487                );
2488                for (loop_index, loop_record) in face.loops.iter().enumerate() {
2489                    let walk = loop_record
2490                        .coedges
2491                        .iter()
2492                        .map(|coedge| {
2493                            let spin = (|| -> Result<f64, KernelRefusal> {
2494                                let [d0, d1] = coedge.pcurve.domain().or_refuse(KernelStage::Sew, "domain")?;
2495                                let a = coedge.pcurve.evaluate(d0 + (d1 - d0) * 0.45).or_refuse(KernelStage::Sew, "evaluate")?;
2496                                let b = coedge.pcurve.evaluate(d0 + (d1 - d0) * 0.55).or_refuse(KernelStage::Sew, "evaluate")?;
2497                                let pa = face.surface.evaluate(a.x, a.y).or_refuse(KernelStage::Sew, "evaluate")?;
2498                                let pb = face.surface.evaluate(b.x, b.y).or_refuse(KernelStage::Sew, "evaluate")?;
2499                                Ok(pa.cross(pb).z)
2500                            })()
2501                            .unwrap_or(f64::NAN);
2502                            format!(
2503                                "{}{}(spin{:+.0})",
2504                                if coedge.forward { "+" } else { "-" },
2505                                coedge.edge_id,
2506                                spin.signum()
2507                            )
2508                        })
2509                        .collect::<Vec<_>>();
2510                    os_debug!("    loop {} {:?}", loop_index, walk);
2511                }
2512            }
2513        }
2514    }
2515    let completion_carriers = carriers.iter().collect::<Vec<_>>();
2516    let completion_tolerance = 2e-3f64.max(scale * 5e-5);
2517    // Unify geometric duplicate arc records BEFORE completion: a ring that is
2518    // already covered by both its faces (frustum + apex cap) must not read as
2519    // an open boundary, or completion papers over it with duplicate faces.
2520    let duplicate_welds = weld_duplicate_one_use_arcs(&mut solid, completion_tolerance)?;
2521    os_debug!("welded {duplicate_welds} duplicate one-use boundary arcs");
2522    if debug_enabled() {
2523        let mut use_counts = HashMap::<u64, usize>::default();
2524        for coedge in solid
2525            .shells
2526            .iter()
2527            .flat_map(|shell| &shell.faces)
2528            .flat_map(|face| &face.loops)
2529            .flat_map(|loop_record| &loop_record.coedges)
2530        {
2531            *use_counts.entry(coedge.edge_id).or_default() += 1;
2532        }
2533        let open = use_counts.values().filter(|count| **count == 1).count();
2534        os_debug!(
2535            "assembled: faces={} open_edges={open}",
2536            solid
2537                .shells
2538                .iter()
2539                .map(|shell| shell.faces.len())
2540                .sum::<usize>()
2541        );
2542    }
2543    let completed_images = complete_opening_boundary_cycles(
2544        &mut solid,
2545        &completion_carriers,
2546        &face_images,
2547        completion_tolerance,
2548    )?;
2549    os_debug!("completion added {} faces", completed_images.len());
2550    face_images.extend(completed_images);
2551    let solid = merge_same_surface_faces_open(&solid, (1e-7f64).max(scale * 1e-9))?;
2552    let assembly_tolerance = 2e-3f64.max(scale * 5e-5);
2553    // No faceted fallback here: an offset shell must be made of real analytic
2554    // surfaces. Failing loudly beats silently returning a tessellated BREP
2555    // with destroyed face provenance.
2556    // A shell the rim band lane recognized but could not close fails here as it
2557    // always did; the refusal now carries the lane's reason.
2558    let mut solid = finalize_assembled_solid(solid, assembly_tolerance).map_err(|error| {
2559        match &rim_band.decline {
2560            Some(reason) => error.with_message(|message| format!("{message}; offset_shell: {reason}")),
2561            None => error,
2562        }
2563    })?;
2564    if rim_bands + pair_rims > 0 || !inward_extended_walls.is_empty() {
2565        // Rim welds and extended reflex walls join independently-oriented skins;
2566        // put the whole finalized manifold on one normal convention so the
2567        // shell reports its exact wall volume (coplanar-only shells keep
2568        // their assembled sense).
2569        coheres_face_normals(&mut solid)?;
2570    }
2571    let solid = solid;
2572    face_images = solid
2573        .shells
2574        .iter()
2575        .flat_map(|shell| &shell.faces)
2576        .map(|face| {
2577            face_images
2578                .get(face.id.saturating_sub(1) as usize)
2579                .cloned()
2580                .ok_or_else(|| "offset_shell: merged face lost provenance".to_string())
2581        })
2582        .collect::<Result<Vec<_>, _>>().or_refuse(KernelStage::Sew, "offset_shell_merged_face_lost_provenance")?;
2583    // A bore's end ring carries the bore wall's provenance whichever
2584    // construction built it (the opening's fragmentation or the rim-pair
2585    // weld), so its name does not depend on the path the rims took.
2586    let bore_rings = claim_bore_end_rings(&solid, &mut face_images)?;
2587    os_debug!("{bore_rings} bore end rings took the bore wall's provenance");
2588    // Honesty gate: a real rim left one-use is a hole in the shell that the
2589    // degenerate marking hides from `validate()` (degenerate edges are allowed
2590    // to be face-local). Refuse such a result instead of shipping a
2591    // non-watertight solid as valid. A rim is a CLOSED edge whose interior
2592    // sweeps measurably away from its seam vertex — a genuine circle, not a
2593    // legitimate pole point placeholder (which stays one-use by design).
2594    let mut rim_use_counts = HashMap::<u64, usize>::default();
2595    for coedge in solid
2596        .shells
2597        .iter()
2598        .flat_map(|shell| &shell.faces)
2599        .flat_map(|face| &face.loops)
2600        .flat_map(|loop_record| &loop_record.coedges)
2601    {
2602        *rim_use_counts.entry(coedge.edge_id).or_default() += 1;
2603    }
2604    if debug_enabled() {
2605        for (shell_index, shell) in solid.shells.iter().enumerate() {
2606            for face in &shell.faces {
2607                let role = face_images
2608                    .get(face.id.saturating_sub(1) as usize)
2609                    .map(|image| image.role);
2610                let c = face.surface.evaluate(0.5, 0.5).unwrap_or_default();
2611                os_debug!(
2612                    "DUMPFACE shell {} face {} affine={} role={:?} loops={} center=({:.3},{:.3},{:.3})",
2613                    shell_index, face.id, face.surface.is_affine().unwrap_or(false), role,
2614                    face.loops.len(), c.x, c.y, c.z
2615                );
2616                for (li, lp) in face.loops.iter().enumerate() {
2617                    let ids = lp.coedges.iter().map(|c| c.edge_id).collect::<Vec<_>>();
2618                    os_debug!("    loop {} edges {:?}", li, ids);
2619                }
2620            }
2621        }
2622        for edge in &solid.edges {
2623            let uses = rim_use_counts.get(&edge.id).copied().unwrap_or(0);
2624            let s = edge.curve.evaluate(edge.t0).or_refuse(KernelStage::Sew, "evaluate")?;
2625            let m = edge.curve.evaluate((edge.t0 + edge.t1) * 0.5).or_refuse(KernelStage::Sew, "evaluate")?;
2626            let q = edge.curve.evaluate(edge.t0 + (edge.t1 - edge.t0) * 0.25).or_refuse(KernelStage::Sew, "evaluate")?;
2627            os_debug!(
2628                "DUMPEDGE edge {} uses={} closed={} deg={} cpts={} cdeg={} knots={:?} start=({:.3},{:.3},{:.3}) q=({:.3},{:.3},{:.3}) mid=({:.3},{:.3},{:.3})",
2629                edge.id, uses, edge.start_vertex_id == edge.end_vertex_id, edge.degenerate,
2630                edge.curve.control_points.len(), edge.curve.degree, edge.curve.knots,
2631                s.x, s.y, s.z, q.x, q.y, q.z, m.x, m.y, m.z
2632            );
2633        }
2634    }
2635    for edge in &solid.edges {
2636        if rim_use_counts.get(&edge.id).copied().unwrap_or(0) != 1
2637            || edge.start_vertex_id != edge.end_vertex_id
2638        {
2639            continue;
2640        }
2641        let anchor = edge.curve.evaluate(edge.t0).or_refuse(KernelStage::Sew, "evaluate")?;
2642        let sweep_threshold = 1e-4f64.max(scale * 1e-6);
2643        let sweeps = [0.25, 0.5, 0.75].into_iter().any(|fraction| {
2644            edge.curve
2645                .evaluate(edge.t0 + (edge.t1 - edge.t0) * fraction)
2646                .map(|point| point.sub(anchor).length() > sweep_threshold)
2647                .unwrap_or(false)
2648        });
2649        if sweeps {
2650            if debug_enabled() {
2651                let rim_mid = edge.curve.evaluate((edge.t0 + edge.t1) * 0.5).or_refuse(KernelStage::Sew, "evaluate")?;
2652                os_debug!(
2653                    "PROBE orphan rim edge {} anchor=({:.4},{:.4},{:.4}) mid=({:.4},{:.4},{:.4})",
2654                    edge.id,
2655                    anchor.x,
2656                    anchor.y,
2657                    anchor.z,
2658                    rim_mid.x,
2659                    rim_mid.y,
2660                    rim_mid.z
2661                );
2662                for (shell_index, shell) in solid.shells.iter().enumerate() {
2663                    for face in &shell.faces {
2664                        let uses_rim = face
2665                            .loops
2666                            .iter()
2667                            .flat_map(|l| &l.coedges)
2668                            .any(|c| c.edge_id == edge.id);
2669                        let affine = face.surface.is_affine().unwrap_or(false);
2670                        let on = edge_on_surface(edge, &face.surface, 2e-3f64.max(scale * 5e-5))
2671                            .unwrap_or(false);
2672                        let proj = project_point_to_surface(&face.surface, rim_mid).or_refuse(KernelStage::Sew, "project_point_to_surface")?;
2673                        let contains = if on {
2674                            parameter_point_in_face(
2675                                face,
2676                                Vec2 {
2677                                    x: proj.u,
2678                                    y: proj.v,
2679                                },
2680                                2e-3f64.max(scale * 5e-5),
2681                            ).or_refuse(KernelStage::Sew, "parameter_point_in_face")? != PolygonClass::Outside
2682                        } else {
2683                            false
2684                        };
2685                        if on || uses_rim {
2686                            os_debug!(
2687                                "  face {} shell {} affine={} uses_rim={} on_surf={} proj_dist={:.5} contains={}",
2688                                face.id, shell_index, affine, uses_rim, on, proj.distance, contains
2689                            );
2690                        }
2691                    }
2692                }
2693            }
2694            return Err(refusals::unwelded_curved_rim(edge.id, anchor).into());
2695        }
2696    }
2697    // The shell's single exit. An offset skin that crosses another wall, or
2698    // folds through itself where the source turned tighter than the distance,
2699    // leaves a body that validates and measures — so it is asked here, and
2700    // repaired or refused by name rather than returned.
2701    let mut solid = solid;
2702    // Face-local rim construction and planar merging can reuse wire IDs.
2703    // Reserve every existing ID, then replace only duplicates so the completed
2704    // body can cross the arena/buffer ingestion boundary. These IDs have no
2705    // external references; face/edge identities and provenance stay intact.
2706    let mut loop_ids = HashSet::default();
2707    let mut coedge_ids = HashSet::default();
2708    for face in solid.shells.iter().flat_map(|shell| &shell.faces) {
2709        for wire in &face.loops {
2710            loop_ids.insert(wire.id);
2711            coedge_ids.extend(wire.coedges.iter().map(|coedge| coedge.id));
2712        }
2713    }
2714    let mut seen_loops = HashSet::default();
2715    let mut seen_coedges = HashSet::default();
2716    let mut next_loop = 1;
2717    let mut next_coedge = 1;
2718    for face in solid.shells.iter_mut().flat_map(|shell| &mut shell.faces) {
2719        for wire in &mut face.loops {
2720            if !seen_loops.insert(wire.id) {
2721                while !loop_ids.insert(next_loop) { next_loop += 1; }
2722                wire.id = next_loop;
2723            }
2724            for coedge in &mut wire.coedges {
2725                if !seen_coedges.insert(coedge.id) {
2726                    while !coedge_ids.insert(next_coedge) { next_coedge += 1; }
2727                    coedge.id = next_coedge;
2728                }
2729            }
2730        }
2731    }
2732    let solid = crate::accept_sound(solid, "offsetShell")?;
2733    Ok(OffsetShellResultRecord { solid, face_images })
2734}
2735
2736/// Run offset shell with stable counters and validation diagnostics suitable
2737/// for automated regressions and UI presentation.
2738pub fn offset_shell_with_diagnostics(
2739    source: &BrepSolid,
2740    opening_face_ids: &[u64],
2741    distance: f64,
2742    tolerances: Option<KernelTolerances>,
2743) -> Result<KernelOutcome<OffsetShellResultRecord>, KernelRefusal> {
2744    let policy = tolerances.unwrap_or_else(|| KernelTolerances::for_solid(source, 1e-7));
2745    policy.check().or_refuse(KernelStage::Sew, "check")?;
2746    let mut diagnostics = KernelDiagnostics::default();
2747    diagnostics.count_n(
2748        "collect.source_faces",
2749        source
2750            .shells
2751            .iter()
2752            .map(|shell| shell.faces.len() as u64)
2753            .sum(),
2754    );
2755    diagnostics.count_n("collect.opening_faces", opening_face_ids.len() as u64);
2756    diagnostics.measure_max("offset.distance", distance.abs());
2757    diagnostics.measure_max("tolerance.model", policy.model);
2758    // Tally every pcurve fit the shell makes (its own re-trims and any boolean
2759    // it runs) so `pcurve.unmet_floor` reports a trim that stopped at the
2760    // sample ceiling instead of passing it off as one that met the floor.
2761    let fit_scope = crate::PcurveFitScope::open();
2762    // Same for the polyline fits of its carrier×carrier sections, which is
2763    // where a marched rim's accuracy is decided (`geometry/fit.rs`).
2764    let polyline_scope = crate::PolylineFitScope::open();
2765    let result = offset_shell(source, opening_face_ids, distance)?;
2766    fit_scope.close().report_into(&mut diagnostics);
2767    polyline_scope.close().report_into(&mut diagnostics);
2768    diagnostics.count_n(
2769        "sew.output_faces",
2770        result
2771            .solid
2772            .shells
2773            .iter()
2774            .map(|shell| shell.faces.len() as u64)
2775            .sum(),
2776    );
2777    diagnostics.count_n("sew.face_images", result.face_images.len() as u64);
2778    let validation = result.solid.validate_detailed(&policy);
2779    diagnostics.measure_max("validate.max_pcurve_error", validation.max_pcurve_error);
2780    diagnostics.count_n("validate.issues", validation.issues.len() as u64);
2781    diagnostics.count_n(
2782        "validate.wire_warnings",
2783        validation.wire_warnings.len() as u64,
2784    );
2785    for warning in validation.wire_warnings {
2786        diagnostics.event(
2787            DiagnosticSeverity::Warning,
2788            KernelStage::Validate,
2789            "validate.uv_wire",
2790            warning.message,
2791        );
2792    }
2793    if !validation.issues.is_empty() {
2794        for issue in &validation.issues {
2795            diagnostics.event(
2796                DiagnosticSeverity::Error,
2797                KernelStage::Validate,
2798                "validate.brep",
2799                issue.message.clone(),
2800            );
2801        }
2802        return Err(KernelRefusal::internal(KernelStage::Validate, "offset_shell_diagnostic_result", format!(
2803            "offset_shell: invalid diagnostic result: {:?}",
2804            validation.issues
2805        )));
2806    }
2807    Ok(KernelOutcome {
2808        value: result,
2809        diagnostics,
2810    })
2811}
2812
2813/// Whether a wall fragment lies wholly in the wall's surplus: it has an edge on
2814/// the wall carrier's own outline (`Boundary` of operand `wall_operand`) and no
2815/// edge imprinted by the SOURCE solid, whose rim every genuine opening-wall
2816/// region borders. `cut_by_source` answers whether an imprint piece has the
2817/// source among its two support faces.
2818pub(super) fn wall_surplus_beyond_the_sections(
2819    fragment: &FaceFragmentRecord,
2820    wall_operand: u8,
2821    cut_by_source: impl Fn(u64) -> bool,
2822) -> bool {
2823    let mut on_outline = false;
2824    for coedge in fragment.loops.iter().flat_map(|record| &record.coedges) {
2825        match &coedge.source {
2826            FragmentEdgeSource::Boundary { operand, .. } if *operand == wall_operand => on_outline = true,
2827            FragmentEdgeSource::Boundary { operand, .. } | FragmentEdgeSource::SharedBoundary { operand, .. }
2828                if *operand == SOURCE_OPERAND =>
2829            {
2830                return false;
2831            }
2832            FragmentEdgeSource::Imprint { piece_id } => {
2833                if cut_by_source(*piece_id) {
2834                    return false;
2835                }
2836            }
2837            _ => {}
2838        }
2839    }
2840    on_outline
2841}
2842