Skip to main content

brep_kernel/blending/blend/edge/
closed.rs

1use crate::{KernelRefusal, KernelStage, OrRefuse};
2use super::*;
3
4/// General closed-edge rolling-ball fillet or chamfer by direct §6.9
5/// topology surgery.
6pub fn blend_closed_edge(
7    solid: &BrepSolid,
8    edge_id: u64,
9    radius: f64,
10    chamfer: bool,
11    name: Option<&str>,
12) -> Result<BrepSolid, KernelRefusal> {
13    // One blend operation: the reports of an earlier one, never consumed, are
14    // dropped as it starts (nested calls keep this operation's).
15    let _operation = crate::blend::BlendOperation::enter();
16    if !(radius > 0.0) || !radius.is_finite() {
17        return Err(KernelRefusal::input(KernelStage::Collect, "radius", "blend: radius must be positive"));
18    }
19    blend_closed_edge_impl(solid, edge_id, &|_| radius, Some(radius), chamfer, name)
20}
21
22/// Variable-radius blend (4.9.5): radius stops as (edge fraction, radius)
23/// pairs, linearly interpolated along the edge parameter and clamped at
24/// the ends.  Closed edges must supply matching first/last radii.
25pub fn blend_edge_variable(
26    solid: &BrepSolid,
27    edge_id: u64,
28    radii: &[(f64, f64)],
29    chamfer: bool,
30    name: Option<&str>,
31) -> Result<BrepSolid, KernelRefusal> {
32    // One blend operation: the reports of an earlier one, never consumed, are
33    // dropped as it starts (nested calls keep this operation's).
34    let _operation = crate::blend::BlendOperation::enter();
35    if radii.is_empty() || radii.iter().any(|(_, radius)| !(*radius > 0.0)) {
36        return Err(KernelRefusal::input(KernelStage::Collect, "radius_stop", "blend: every radius stop must be positive"));
37    }
38    let mut stops = radii.to_vec();
39    stops.sort_by(|a, b| a.0.total_cmp(&b.0));
40    let edge = solid
41        .edges
42        .iter()
43        .find(|edge| edge.id == edge_id)
44        .ok_or_else(|| format!("blend: edge {edge_id} not found")).or_refuse(KernelStage::Refine, "ok_or_else")?;
45    let closed = edge.start_vertex_id == edge.end_vertex_id;
46    if closed && (stops[0].1 - stops[stops.len() - 1].1).abs() > 1e-12 {
47        return Err(KernelRefusal::input(KernelStage::Collect, "closed_edge_radii", "blend: closed edges need equal first/last radii"));
48    }
49    // CONSTANT stops must DEGENERATE to the exact constant-radius
50    // machinery (§6.9 exact cylinder cutters where the mates allow them,
51    // the same general march otherwise).  The general march's fitted
52    // NURBS is geometrically exact for a constant radius, but downstream
53    // booleans treat it as a generic surface: marched SSI curves against
54    // a tangent mate drift O(√ε) along the tangency (measured 1.2e-3 on
55    // a 10-box chain miter), while the exact representation intersects
56    // analytically — this is what keeps chain-corner miters composable.
57    let first_radius = stops[0].1;
58    if stops
59        .iter()
60        .all(|(_, radius)| (radius - first_radius).abs() <= 1e-12 * (1.0 + first_radius.abs()))
61    {
62        return if chamfer {
63            crate::fillet::chamfer_edge(solid, edge_id, first_radius, name)
64        } else {
65            crate::fillet::fillet_edge(solid, edge_id, first_radius, name)
66        };
67    }
68    let (t0, t1) = (edge.t0, edge.t1);
69    let radius_at = move |t: f64| -> f64 {
70        let raw = (t - t0) / (t1 - t0);
71        // Closed edges WRAP (the anchored march may start mid-edge and run
72        // past t1 once around — clamping misplaced mid-profile stops
73        // there); open edges CLAMP flat past the ends, which is only the
74        // marched overshoot region beyond the trims.  The blend-end
75        // stations are pinned exactly at t0/t1 by the open march, so the
76        // profile kink the clamp creates sits ON an interpolation node and
77        // the fitted rows still pass through the closed-form end stations.
78        let fraction = if closed {
79            raw.rem_euclid(1.0)
80        } else {
81            raw.clamp(0.0, 1.0)
82        };
83        if fraction <= stops[0].0 {
84            return stops[0].1;
85        }
86        for pair in stops.windows(2) {
87            if fraction <= pair[1].0 {
88                let width = (pair[1].0 - pair[0].0).max(1e-12);
89                let local = (fraction - pair[0].0) / width;
90                return pair[0].1 + (pair[1].1 - pair[0].1) * local;
91            }
92        }
93        stops[stops.len() - 1].1
94    };
95    // A VARIABLE profile (constant stops left above for the constant entry):
96    // no constant-radius provenance, so no rolling-ball wall verdict.
97    if closed {
98        blend_closed_edge_impl(solid, edge_id, &radius_at, None, chamfer, name)
99    } else {
100        blend_open_edge_impl(solid, edge_id, &radius_at, None, chamfer, name)
101    }
102}
103
104/// `constant`: the radius when the blend came in through a CONSTANT-radius
105/// entry point (`blend_closed_edge`), `None` for a variable profile — the
106/// only provenance on which the wall is judged against one rolling ball.
107fn blend_closed_edge_impl(
108    solid: &BrepSolid,
109    edge_id: u64,
110    radius_at: &dyn Fn(f64) -> f64,
111    constant: Option<f64>,
112    chamfer: bool,
113    name: Option<&str>,
114) -> Result<BrepSolid, KernelRefusal> {
115    if let Some(separated) = separate_spherical_domain_cut(solid, edge_id)? {
116        return blend_closed_edge_impl(&separated, edge_id, radius_at, constant, chamfer, name);
117    }
118    if let Some(simplified) = remove_retraced_cuts(solid, edge_id)? {
119        return blend_closed_edge_impl(&simplified, edge_id, radius_at, constant, chamfer, name);
120    }
121    let edge = solid
122        .edges
123        .iter()
124        .find(|edge| edge.id == edge_id)
125        .ok_or_else(|| format!("blend: edge {edge_id} not found")).or_refuse(KernelStage::Refine, "ok_or_else")?;
126    if edge.start_vertex_id != edge.end_vertex_id {
127        return Err(KernelRefusal::input(KernelStage::Collect, "closed_edge", "blend: general path currently requires a CLOSED edge"));
128    }
129    let (face_a, loop_a, coedge_a) = locate_mate(solid, edge_id, None)?;
130    let (face_b, loop_b, coedge_b) = locate_mate(solid, edge_id, Some((face_a.id, loop_a)))?;
131    if face_a.id == face_b.id && loop_a == loop_b {
132        return Err(KernelRefusal::unsupported(KernelStage::Classify, "seam_edge", "blend: edge is used twice by one loop (seam edge?)"));
133    }
134
135    // Seam-structured loops force the blend seam onto that carrier's seam
136    // meridian; at most one mate may need it.
137    let seam_a = face_a.loops[loop_a].coedges.len() > 1;
138    let seam_b = face_b.loops[loop_b].coedges.len() > 1;
139    let (first_face, first_loop, first_coedge, second_face, second_loop, second_coedge) =
140        if seam_a || !seam_b {
141            (face_a, loop_a, coedge_a, face_b, loop_b, coedge_b)
142        } else {
143            (face_b, loop_b, coedge_b, face_a, loop_a, coedge_a)
144        };
145    let anchored = first_face.loops[first_loop].coedges.len() > 1;
146    let second_seam = anchored && second_face.loops[second_loop].coedges.len() > 1;
147    let anchor_u = if anchored {
148        // Lock onto the carrier's seam meridian: the u-domain start.
149        Some(first_face.surface.domain_u().or_refuse(KernelStage::Refine, "domain_u")?[0])
150    } else {
151        None
152    };
153
154    // Signed radii from the cross-section seed at the edge midpoint.
155    let mid_radius = radius_at(edge.t0 + (edge.t1 - edge.t0) * 0.5);
156    let (rho1, rho2) = signed_radii(
157        edge,
158        first_face,
159        first_coedge,
160        second_face,
161        second_coedge,
162        mid_radius,
163    )?;
164
165    let first_mate = BlendMate {
166        face: first_face,
167        coedge: first_coedge,
168        loop_index: first_loop,
169        rho: rho1,
170    };
171    let second_mate = BlendMate {
172        face: second_face,
173        coedge: second_coedge,
174        loop_index: second_loop,
175        rho: rho2,
176    };
177    let mid_t = edge.t0 + (edge.t1 - edge.t0) * 0.5;
178    // Anchored FIRST, always — everything that already works takes this rung
179    // and is unchanged.  An anchored march freezes the first carrier's u on
180    // its seam meridian and frees t (`solve_anchored_start`), so it is only
181    // solvable when the edge actually reaches that meridian.  A closed edge
182    // cut by a plane through a surface of revolution's AXIS is one whole
183    // MERIDIAN of that carrier — constant u, a full turn in v — so no station
184    // on it can sit at the seam, and the seam may even have been trimmed off
185    // the face (the 2026-09-01 reported document: the kept torus was
186    // u ∈ [0.1369, 0.8869] while the anchor locked u = 0).  That march reports
187    // "anchored seam start did not converge"; the UNANCHORED march solves the
188    // same edge exactly, because the edge's own endpoints already sit on the
189    // carrier's OTHER (v) seam.  Retrying is what makes this a rung of the
190    // existing ladder rather than a predicate with a tolerance to tune: a
191    // march that needs its anchor still gets one.
192    use crate::blend::chain::FoldPolicy;
193    let march_with = |fold: FoldPolicy, depth_budget: usize| -> Result<Vec<Station>, KernelRefusal> {
194        match march_stations(edge, &first_mate, &second_mate, radius_at, anchor_u, fold, depth_budget) {
195            Ok(stations) => Ok(stations),
196            Err(anchored_error) if anchor_u.is_some() => {
197                march_stations(edge, &first_mate, &second_mate, radius_at, None, fold, depth_budget).map_err(
198                    |retry_error| {
199                        if crate::blend::is_wall_fold(&retry_error)
200                        || crate::blend::is_marched_fit_off_carriers(&retry_error) {
201                            retry_error
202                        } else {
203                            anchored_error
204                        }
205                    },
206                )
207            }
208            Err(anchored_error) => Err(anchored_error),
209        }
210    };
211    let rail_bar = 0.5 * crate::KernelTolerances::for_solid(solid, 1e-7).intersection_fit;
212    // A FILLET of one constant radius is also judged on its wall between the
213    // rails, against that ball's sweep, at the unchanged `intersection_fit`
214    // (`ClosedWallCheck`); a chamfer or a varying radius is not.
215    let wall_check = {
216        // Constant-radius PROVENANCE (the entry point), never sampled
217        // constancy: a variable profile can agree at any finite set of samples.
218        let radius = constant.unwrap_or(f64::NAN);
219        let constant = constant.is_some();
220        // The construction request as on the closed chain's plain fillet
221        // rungs: `KernelTolerances::model` on the wall between the rails.
222        let model = crate::KernelTolerances::for_solid(solid, 1e-7).model;
223        let bar = crate::KernelTolerances::for_solid(solid, 1e-7).intersection_fit;
224        (!chamfer && constant && radius.is_finite() && radius != 0.0).then(|| crate::blend::stations::ClosedWallCheck {
225            radius: radius.abs(),
226            bar,
227            model: model.is_finite().then_some(model),
228        })
229    };
230    crate::blend::stations::CLOSED_MODEL_UNMET.with(|slot| slot.set(None));
231    crate::blend::stations::CLOSED_REQUEST_RAILS.with(|slot| slot.set(None));
232    let marched = match march_stations_with_rail_bar(edge, &first_mate, &second_mate, radius_at, anchor_u, FoldPolicy::Refuse, MAX_REFINEMENT_DEPTH, rail_bar, wall_check) {
233        Ok(stations) => Ok(stations),
234        Err(failure) => {
235            // Post-refinement errors return directly, before either the
236            // unanchored retry or the legacy edge-preserving construction.
237            let anchored_error = failure.into_initial()?;
238            if anchor_u.is_some() {
239                if std::env::var("BREP_DEBUG_CLOSED_RAILS").is_ok() {
240                    eprintln!("closed initial anchor fallback: edge {}: {anchored_error}", edge.id);
241                }
242                match march_stations_with_rail_bar(edge, &first_mate, &second_mate, radius_at, None, FoldPolicy::Refuse, MAX_REFINEMENT_DEPTH, rail_bar, wall_check) {
243                    Ok(stations) => Ok(stations),
244                    Err(failure) => {
245                        let retry_error = failure.into_initial()?;
246                        if crate::blend::is_wall_fold(&retry_error) { Err(retry_error) }
247                        else { Err(anchored_error) }
248                    }
249                }
250            } else { Err(anchored_error) }
251        }
252    };
253    // A wall that FOLDS through itself: the lens of the envelope inside the
254    // swept ball volume is CARVED (`blend/carve.rs`), the way the closed chain
255    // lane carves it, and the crease sewn as an inner loop of the blend face.
256    // A fold that reaches a RAIL is not a lens (nothing to keep on one side)
257    // and stays the named refusal; so does a chamfer's (a straight section
258    // has no ball centre to read the fold locus against).  The march is
259    // re-run under `FoldPolicy::Carve` so the folding wall is built; the lane
260    // then checks the fitted rows are accurate enough to carve and that the
261    // lens does not straddle the wall's own seam before anything is cut.
262    // The march's unmet construction request (if any), taken before any other
263    // march runs; it is reported only for the plain wall this lane builds
264    // from that march's stations.
265    let model_unmet = crate::blend::stations::CLOSED_MODEL_UNMET.with(|slot| slot.take());
266    let request_rails = crate::blend::stations::CLOSED_REQUEST_RAILS.with(|slot| slot.take());
267    let mut carve_fold = false;
268    let marched = match marched {
269        Err(error)
270            if crate::blend::is_wall_fold(&error)
271                && !crate::blend::fold::is_rail_fold(&error)
272                && !chamfer =>
273        {
274            crate::blend::carve::carve_trace(format_args!(
275                "blend closed-edge: the wall folds ({error}); re-marching under Carve"
276            ));
277            // The CARVE LADDER: the adaptive march's depth budget is raised
278            // rung by rung until the rows fitted through its stations stand
279            // on their carriers between stations to the network's rail bar
280            // (half `intersection_fit`, read at 0.25/0.5/0.75 of every span)
281            // — the same climb the closed chain lane makes per segment
282            // (`carve_ladder`).  Measured on the narrowed mouth-over-wall
283            // variant 2026-10-03: the refusing march's own budget left the
284            // rails 2.7e-4 off their carriers, two decades over the bar.
285            let rail_bar = 0.5 * crate::KernelTolerances::for_solid(solid, 1e-7).intersection_fit;
286            let mut climbed: Option<Vec<Station>> = None;
287            let mut last_off = f64::NAN;
288            for depth_budget in MAX_REFINEMENT_DEPTH..=CARVE_MAX_REFINEMENT_DEPTH {
289                let stations = match march_with(FoldPolicy::Carve, depth_budget) {
290                    Ok(stations) => stations,
291                    // The Carve march did not build: the fold stays the answer.
292                    Err(_) => break,
293                };
294                let parameters = station_parameters(&stations);
295                let off = match fit_closed_rows(&stations, &parameters, chamfer) {
296                    Ok(rows) => rails_off_closed_carriers(&rows, &stations, &parameters, &first_mate, &second_mate)?,
297                    Err(_) => break,
298                };
299                last_off = off;
300                crate::blend::carve::carve_trace(format_args!(
301                    "blend closed-edge carve ladder: depth budget {depth_budget} ({} stations) leaves the rails {off:.3e} off their carriers (bar {rail_bar:.3e})",
302                    stations.len()
303                ));
304                if off <= rail_bar {
305                    climbed = Some(stations);
306                    break;
307                }
308            }
309            match climbed {
310                Some(stations) => {
311                    carve_fold = true;
312                    Ok(stations)
313                }
314                None => Err(crate::blend::fold::wall_fold_refusal(
315                    Vec::new(),
316                    format!(
317                        "{} {} fits this edge: the wall folds, and at depth budget {CARVE_MAX_REFINEMENT_DEPTH} — the top of the closed march's carve ladder — its fitted rails still stand {last_off:.3e} off their carriers between stations against a bar of {rail_bar:.3e}, so there is no wall accurate enough to carve ({error})",
318                        crate::blend::WALL_FOLDS,
319                        radius_at(mid_t).abs()
320                    ),
321                )),
322            }
323        }
324        other => other,
325    };
326    let stations = match marched {
327        Ok(stations) => stations,
328        // A wall that FOLDS is refused here for the same reason an escaped
329        // march is: the edge-preserving construction would build a blend on the
330        // same folding centre curve, and if it converged it would return the
331        // very surface this refusal exists to stop.  It is not the fallback's
332        // question to answer, so the geometry is reported as-is.
333        Err(march_error) if crate::blend::is_wall_fold(&march_error)
334            || crate::blend::is_marched_fit_off_carriers(&march_error) => return Err(march_error),
335        Err(march_error) if crate::blend::is_ball_off_carrier(&march_error) => {
336            // An ESCAPED march is not a non-convergence: the tangency system
337            // had no solution at all, so no fallback can build the blend the
338            // rolling ball never made.  Report the escape itself — routing it
339            // to the edge-preserving construction below only reports THAT
340            // lane's complaint about a face it was never meant to consume
341            // ("ambiguous preserved boundary edge" on the 2026-09-10 report).
342            return Err(march_error);
343        }
344        Err(march_error) => {
345            // A non-converging march often means the rolling ball falls off
346            // one face — try the edge-preserving construction (4.9.7).
347            return blend_closed_edge_keep(
348                solid,
349                edge,
350                &first_mate,
351                &second_mate,
352                radius_at(mid_t),
353                rho1,
354                chamfer,
355                name,
356            )
357            .or_else(|_| {
358                blend_closed_edge_keep(
359                    solid,
360                    edge,
361                    &second_mate,
362                    &first_mate,
363                    radius_at(mid_t),
364                    rho2,
365                    chamfer,
366                    name,
367                )
368            })
369            .map_err(|keep_error| {
370                format!("{march_error}; edge-preserving blend also failed: {keep_error}")
371            }).or_refuse(KernelStage::Refine, "map_err");
372        }
373    };
374    // Support-out-of-trim detection (4.9.7 trigger): a tangency track that
375    // leaves its face's trimmed region cannot be trimmed there — the far
376    // boundary must be PRESERVED instead.
377    let support_exits = |face: &FaceRecord, second_side: bool| -> bool {
378        // Adaptive marches vary the station count; keep at least the old fixed
379        // grid's ~16-probe floor while inheriting extra density where the
380        // march refined (which is where an exit would hide).
381        let stride = (stations.len() / 16).max(1);
382        stations.iter().step_by(stride).any(|station| {
383            let mut uv = if second_side {
384                station.uv2
385            } else {
386                station.uv1
387            };
388            if let Ok((closed_u, closed_v)) = face.surface.closed_directions() {
389                if closed_u {
390                    if let Ok([low, high]) = face.surface.domain_u() {
391                        uv[0] = low + (uv[0] - low).rem_euclid(high - low);
392                    }
393                }
394                if closed_v {
395                    if let Ok([low, high]) = face.surface.domain_v() {
396                        uv[1] = low + (uv[1] - low).rem_euclid(high - low);
397                    }
398                }
399            }
400            crate::parameter_point_in_face(face, crate::Vec2 { x: uv[0], y: uv[1] }, 1e-6)
401                .map(|class| class == crate::PolygonClass::Outside)
402                .unwrap_or(true)
403        })
404    };
405    if support_exits(second_face, true) {
406        return blend_closed_edge_keep(
407            solid,
408            edge,
409            &first_mate,
410            &second_mate,
411            radius_at(mid_t),
412            rho1,
413            chamfer,
414            name,
415        );
416    }
417    if support_exits(first_face, false) {
418        return blend_closed_edge_keep(
419            solid,
420            edge,
421            &second_mate,
422            &first_mate,
423            radius_at(mid_t),
424            rho2,
425            chamfer,
426            name,
427        );
428    }
429    if std::env::var("BREP_DEBUG_BLEND_MARCH").is_ok() {
430        let last = stations.len() - 1;
431        for index in [
432            0usize,
433            1.min(last),
434            2.min(last),
435            last.saturating_sub(1),
436            last,
437        ] {
438            let station = &stations[index];
439            eprintln!(
440                "station {index}: uv1=({:.6},{:.6}) uv2=({:.6},{:.6}) p1=({:.6},{:.6},{:.6}) w={:.6}",
441                station.uv1[0], station.uv1[1], station.uv2[0], station.uv2[1],
442                station.p1.x, station.p1.y, station.p1.z, station.weight,
443            );
444        }
445    }
446    let parameters = station_parameters(&stations);
447    let exact_rows = exact_closed_revolution_rows(&stations, &first_mate, &second_mate, chamfer);
448    let rows = match exact_rows {
449        Some(rows) => rows?,
450        None => {
451            // The support pcurves interpolate the stations' (u, v) and are
452            // read nowhere between them: on the 20-degree crossing's single
453            // closed exit edge they stood 1.01e-4 (thin carrier) and
454            // 2.6e-5 to 3.8e-5 (fat carrier) off the rails they trim, while the
455            // rails stood on their carriers to 1e-6 — the whole of a 5e-6 to
456            // 1.1e-5 shell vector-area residual and of a seam-placement-
457            // dependent volume (dumps of c5ea8fb80 and 635ef7630, read
458            // input-free). Each is refitted to its rail's own projected track
459            // at the pcurve floor, by the closed chain's support-piece fitter.
460            let mut rows = fit_closed_rows(&stations, &parameters, chamfer)?;
461            rows.cr_pcurve = refit_closed_support_pcurve(&rows.cr, &rows.cr_pcurve, &first_mate.face.surface)?;
462            rows.cs_pcurve = refit_closed_support_pcurve(&rows.cs, &rows.cs_pcurve, &second_mate.face.surface)?;
463            rows
464        }
465    };
466    let crease = if carve_fold {
467        Some(carve_closed_wall(solid, &rows, &stations, &parameters, &first_mate, &second_mate, radius_at(mid_t).abs())?)
468    } else {
469        None
470    };
471
472    // Both mates seam-structured: the second support crosses ITS carrier's
473    // seam meridian somewhere mid-loop; split cs there so face2's loop can
474    // keep its seam-in-one-loop structure.
475    let second_split = if second_seam {
476        let seam2 = second_face.surface.domain_u().or_refuse(KernelStage::Refine, "domain_u")?[0];
477        let period2 = {
478            let [d0, d1] = second_face.surface.domain_u().or_refuse(KernelStage::Refine, "domain_u")?;
479            d1 - d0
480        };
481        // Bracket: unwrapped uv2 track crossing seam2 + k·period.
482        let u2_first = stations[0].uv2[0];
483        let u2_last = stations[stations.len() - 1].uv2[0];
484        let direction = (u2_last - u2_first).signum();
485        let mut k = ((u2_first - seam2) / period2).ceil();
486        if direction < 0.0 {
487            k = ((u2_first - seam2) / period2).floor();
488        }
489        let target = seam2 + k * period2;
490        let inside =
491            (target - u2_first) * direction > 1e-6 && (u2_last - target) * direction > 1e-6;
492        // ALIGNED seams (both carriers' meridians meet at the shared
493        // vertex — the natural coaxial construction): the crossing sits
494        // at the march boundary and no mid-loop split is needed; the
495        // blend seam vertex already lies on both meridians.
496        let aligned = (target - u2_first).abs() <= 1e-6
497            || (target - u2_last).abs() <= 1e-6
498            || ((u2_first - seam2) / period2).fract().abs() <= 1e-9;
499        if !inside && aligned {
500            None
501        } else if !inside {
502            return Err(KernelRefusal::internal(KernelStage::Refine, "seam_crossing_bracket", "blend: second seam crossing not bracketed by the march"));
503        } else {
504            // Fit-space crossing parameter on the fitted cs (Newton via the
505            // fitted pcurve so the split lands exactly where the FITTED track
506            // crosses the meridian).
507            let [fit_low, fit_high] = rows.u_domain;
508            let mut p = fit_low
509                + (fit_high - fit_low) * {
510                    // Seed from the bracketing stations.
511                    let mut seed = 0.5;
512                    for pair in 0..stations.len() - 1 {
513                        let a = stations[pair].uv2[0];
514                        let b = stations[pair + 1].uv2[0];
515                        if (target - a) * (target - b) <= 0.0 {
516                            let local = (target - a) / (b - a);
517                            seed = (parameters[pair]
518                                + (parameters[pair + 1] - parameters[pair]) * local)
519                                .clamp(0.0, 1.0);
520                            break;
521                        }
522                    }
523                    seed
524                };
525            for _ in 0..NEWTON_ITERATIONS {
526                let value = rows.cs_pcurve.evaluate(p).or_refuse(KernelStage::Refine, "evaluate")?.x - target;
527                if value.abs() <= 1e-12 {
528                    break;
529                }
530                let step = 1e-8;
531                let probed = rows.cs_pcurve.evaluate(p + step).or_refuse(KernelStage::Refine, "evaluate")?.x - target;
532                let derivative = (probed - value) / step;
533                if derivative.abs() <= 1e-14 {
534                    return Err(KernelRefusal::non_convergence(KernelStage::Refine, "seam_crossing_newton", "blend: second seam crossing Newton stalled"));
535                }
536                p -= value / derivative;
537            }
538            let crossing_v = rows.cs_pcurve.evaluate(p).or_refuse(KernelStage::Refine, "evaluate")?.y;
539            Some(SecondSeamSplit {
540                fit_parameter: p,
541                crossing_v,
542                period: period2 * direction,
543            })
544        }
545    } else {
546        None
547    };
548
549    // A wall that SHIPS short of its construction request says so, typed, on
550    // the blend face it built (exact name and surface); a carved wall or a
551    // fallback construction makes no request.
552    let model_unmet = if carve_fold { None } else { model_unmet };
553    let wall_surface = model_unmet.map(|_| rows.surface.clone());
554    let built = build_surgery(
555        solid,
556        edge,
557        &first_mate,
558        &second_mate,
559        rows,
560        second_split,
561        name,
562        crease,
563    )?;
564    if let (Some((declared, reason, round, stations, ceiling)), Some(surface)) = (model_unmet, wall_surface) {
565        crate::blend::record_wall_model_report(crate::blend::WallModelReport {
566            name: name.map(str::to_string),
567            surface,
568            request: crate::KernelTolerances::for_solid(solid, 1e-7).model,
569            residual: declared,
570            // The request covers the rails (the shipped support curves) and
571            // the wall; the residual is the worse of the two.
572            detail: request_rails.map(|(rails, unread, wall)| {
573                format!("last request read: rails {rails:.3e} off their carriers at j/16 ({unread} interval(s) unread), wall {wall:.3e}")
574            }),
575            budget: crate::ApproximationBudget {
576                reason,
577                rounds_used: round,
578                rounds_limit: crate::blend::stations::REFINE_ROUNDS,
579                stations,
580                station_limit: ceiling,
581                mechanism: crate::BudgetMechanism::LocalRounds,
582                // The request covers rails and wall: which one the residual
583                // reads, and the rail intervals it could not read.
584                measured_component: match request_rails {
585                    Some((rails, _, wall)) if rails > wall => crate::MeasuredComponent::Rails,
586                    _ => crate::MeasuredComponent::Wall,
587                },
588                unread: request_rails.map_or(0, |(_, unread, _)| unread),
589            },
590        });
591    }
592    Ok(built)
593}
594
595/// A closed spherical hole may meet the carrier's full-domain seam at its
596/// loop vertex. The domain rectangle is only a parameter cut, so moving the
597/// hole need not move that cut. Separate the ring from the rectangle and give
598/// it its own coincident vertex before surgery. Unlike a retraced slit, the
599/// opposite seam branches and collapsed pole edges remain in the solid.
600fn separate_spherical_domain_cut(
601    solid: &BrepSolid,
602    edge_id: u64,
603) -> Result<Option<BrepSolid>, KernelRefusal> {
604    let Some(edge) = solid.edges.iter().find(|edge| edge.id == edge_id) else {
605        return Ok(None);
606    };
607    if edge.start_vertex_id != edge.end_vertex_id {
608        return Ok(None);
609    }
610    let mut result = solid.clone();
611    let mut take_id = crate::blend::edge::fresh_id_source(solid);
612    let mut separated = false;
613    for face in result.shells.iter_mut().flat_map(|shell| &mut shell.faces) {
614        let Some(atlas) = crate::sphere_chart::SphereAtlas::of_surface(&face.surface) else {
615            continue;
616        };
617        let [u0, u1] = face
618            .surface
619            .domain_u()
620            .or_refuse(KernelStage::Collect, "domain_cut_u")?;
621        let [v0, v1] = face
622            .surface
623            .domain_v()
624            .or_refuse(KernelStage::Collect, "domain_cut_v")?;
625        if [u0, u1, v0, v1] != [0.0, 1.0, 0.0, 1.0] {
626            continue;
627        }
628        if face
629            .surface
630            .closed_directions()
631            .or_refuse(KernelStage::Collect, "domain_cut_closed")?
632            != (true, false)
633        {
634            continue;
635        }
636        let band = 1e-8 * atlas.radius;
637        let bottom = face
638            .surface
639            .evaluate(u0, v0)
640            .or_refuse(KernelStage::Collect, "domain_cut_bottom")?;
641        let top = face
642            .surface
643            .evaluate(u0, v1)
644            .or_refuse(KernelStage::Collect, "domain_cut_top")?;
645        if bottom.add(top).scale(0.5).sub(atlas.centre).length() > band
646            || (bottom.sub(top).length() - 2.0 * atlas.radius).abs() > band
647        {
648            continue;
649        }
650        let mut additions = Vec::new();
651        let mut cut_ids = std::collections::HashSet::new();
652        for rim in &mut face.loops {
653            if rim.coedges.len() < 5 {
654                continue;
655            }
656            let positions: Vec<_> = rim
657                .coedges
658                .iter()
659                .enumerate()
660                .filter_map(|(i, c)| (c.edge_id == edge_id).then_some(i))
661                .collect();
662            if positions.len() != 1 {
663                continue;
664            }
665            let position = positions[0];
666            let ring = &rim.coedges[position];
667            let [a, b] = ring
668                .pcurve
669                .domain()
670                .or_refuse(KernelStage::Collect, "domain_cut_ring")?;
671            if ring
672                .pcurve
673                .evaluate(a)
674                .or_refuse(KernelStage::Collect, "domain_cut_ring_start")?
675                .sub(
676                    ring.pcurve
677                        .evaluate(b)
678                        .or_refuse(KernelStage::Collect, "domain_cut_ring_end")?,
679                )
680                .length()
681                > 1e-7
682            {
683                continue;
684            }
685            let rest: Vec<_> = rim
686                .coedges
687                .iter()
688                .filter(|c| c.edge_id != edge_id)
689                .collect();
690            let mut points = Vec::new();
691            let mut domain_cut = true;
692            for coedge in &rest {
693                let [a, b] = coedge
694                    .pcurve
695                    .domain()
696                    .or_refuse(KernelStage::Collect, "domain_cut_trim")?;
697                let start = coedge
698                    .pcurve
699                    .evaluate(a)
700                    .or_refuse(KernelStage::Collect, "domain_cut_start")?;
701                let end = coedge
702                    .pcurve
703                    .evaluate(b)
704                    .or_refuse(KernelStage::Collect, "domain_cut_end")?;
705                points.push(start);
706                let degenerate = solid
707                    .edges
708                    .iter()
709                    .any(|e| e.id == coedge.edge_id && e.degenerate);
710                let at = |x: f64, low: f64, high: f64| {
711                    (x - low).abs() <= 1e-8 || (x - high).abs() <= 1e-8
712                };
713                let on_boundary = if degenerate {
714                    (start.y - end.y).abs() <= 1e-8
715                        && at(start.y, v0, v1)
716                        && coedge
717                            .pcurve
718                            .control_points
719                            .iter()
720                            .all(|p| (p.y / p.w - start.y).abs() <= 1e-8)
721                } else {
722                    at(start.x, u0, u1)
723                        && (start.x - end.x).abs() <= 1e-8
724                        && coedge
725                            .pcurve
726                            .control_points
727                            .iter()
728                            .all(|p| (p.x / p.w - start.x).abs() <= 1e-8)
729                        && rest.iter().filter(|c| c.edge_id == coedge.edge_id).count() == 2
730                        && rest
731                            .iter()
732                            .any(|c| c.edge_id == coedge.edge_id && c.forward != coedge.forward)
733                };
734                domain_cut &= on_boundary;
735            }
736            let twice_area: f64 = (0..points.len())
737                .map(|i| {
738                    let a = points[i];
739                    let b = points[(i + 1) % points.len()];
740                    a.x * b.y - a.y * b.x
741                })
742                .sum();
743            let expected = (u1 - u0) * (v1 - v0) * if face.same_sense { 1.0 } else { -1.0 };
744            if !domain_cut || (0.5 * twice_area - expected).abs() > 1e-7 * expected.abs() {
745                continue;
746            }
747            // A relocated cut must belong only to this sphere. Its vertices
748            // may touch the selected ring or collapsed poles, but no other
749            // material edge. Its active parameter must already be sphere v.
750            let cuts: std::collections::HashSet<_> = rest
751                .iter()
752                .filter_map(|c| {
753                    solid
754                        .edges
755                        .iter()
756                        .find(|e| e.id == c.edge_id && !e.degenerate)
757                        .map(|e| e.id)
758                })
759                .collect();
760            for cut in solid.edges.iter().filter(|e| cuts.contains(&e.id)) {
761                let users = solid
762                    .shells
763                    .iter()
764                    .flat_map(|s| &s.faces)
765                    .flat_map(|f| &f.loops)
766                    .flat_map(|l| &l.coedges)
767                    .filter(|c| c.edge_id == cut.id)
768                    .count();
769                let exclusive_vertices = solid
770                    .edges
771                    .iter()
772                    .filter(|e| {
773                        e.start_vertex_id == cut.start_vertex_id
774                            || e.end_vertex_id == cut.start_vertex_id
775                            || e.start_vertex_id == cut.end_vertex_id
776                            || e.end_vertex_id == cut.end_vertex_id
777                    })
778                    .all(|e| e.degenerate || cuts.contains(&e.id) || e.id == edge_id);
779                let mut same_parameter = cut.t0 >= v0 && cut.t1 <= v1 && cut.t0 < cut.t1;
780                for i in 0..=16 {
781                    let t = cut.t0 + (cut.t1 - cut.t0) * i as f64 / 16.0;
782                    let point = cut
783                        .curve
784                        .evaluate(t)
785                        .or_refuse(KernelStage::Collect, "domain_cut_curve")?;
786                    let carrier = face
787                        .surface
788                        .evaluate(u0, t)
789                        .or_refuse(KernelStage::Collect, "domain_cut_carrier")?;
790                    same_parameter &= point.sub(carrier).length() <= band;
791                }
792                domain_cut &= users == 2 && exclusive_vertices && same_parameter;
793            }
794            if !domain_cut {
795                continue;
796            }
797            cut_ids.extend(cuts);
798            additions.push(LoopRecord {
799                id: take_id(),
800                coedges: vec![rim.coedges.remove(position)],
801            });
802            separated = true;
803        }
804        if !additions.is_empty() {
805            // The freed contact can cross the old parameter seam. Move the
806            // artificial cut by a quarter turn, which is an exact shift of
807            // this rational circle chart, to keep every real trim in-domain.
808            let real = |id: u64| {
809                !cut_ids.contains(&id) && !solid.edges.iter().any(|e| e.id == id && e.degenerate)
810            };
811            let curves: Vec<_> = face
812                .loops
813                .iter()
814                .flat_map(|l| &l.coedges)
815                .chain(additions.iter().flat_map(|l| &l.coedges))
816                .filter(|c| real(c.edge_id) || c.edge_id == edge_id)
817                .map(|c| &c.pcurve)
818                .collect();
819            let mut candidates = Vec::new();
820            for shift in [0.25, 0.5, 0.75] {
821                let mut clearance = f64::INFINITY;
822                let mut fits = true;
823                for curve in &curves {
824                    let first = curve.control_points[0].x / curve.control_points[0].w;
825                    let offset = -(first - shift).floor();
826                    for p in &curve.control_points {
827                        let u = p.x / p.w - shift + offset;
828                        fits &= u >= 0.0 && u <= 1.0;
829                        clearance = clearance.min(u.min(1.0 - u));
830                    }
831                }
832                if fits {
833                    candidates.push((clearance, shift));
834                }
835            }
836            let Some((_, shift)) = candidates.into_iter().max_by(|a, b| a.0.total_cmp(&b.0)) else {
837                return Err(KernelRefusal::unsupported(
838                    KernelStage::Collect,
839                    "sphere_domain_cut",
840                    "blend: no quarter-turn sphere chart keeps the real trims in-domain",
841                ));
842            };
843            let seam_direction = face
844                .surface
845                .evaluate(shift, 0.5)
846                .or_refuse(KernelStage::Collect, "sphere_cut_direction")?
847                .sub(atlas.centre);
848            let surface = crate::make_sphere_surface_framed(
849                atlas.centre,
850                atlas.radius,
851                atlas.basis[2],
852                Some(seam_direction),
853            )
854            .or_refuse(KernelStage::Collect, "sphere_cut_surface")?;
855            // Rebuilding must be only a chart change, including handedness.
856            for i in 0..=16 {
857                for v in [0.0, 0.3, 0.5, 0.7, 1.0] {
858                    let u = i as f64 / 16.0;
859                    let old = face
860                        .surface
861                        .evaluate(u, v)
862                        .or_refuse(KernelStage::Collect, "sphere_cut_old")?;
863                    let new = surface
864                        .evaluate((u - shift).rem_euclid(1.0), v)
865                        .or_refuse(KernelStage::Collect, "sphere_cut_new")?;
866                    if old.sub(new).length() > band {
867                        return Err(KernelRefusal::unsupported(
868                            KernelStage::Collect,
869                            "sphere_cut_chart",
870                            "blend: the sphere quarter turn is not the same carrier",
871                        ));
872                    }
873                }
874            }
875            let seam = surface
876                .iso_curve_u(0.0)
877                .or_refuse(KernelStage::Collect, "sphere_cut_seam")?;
878            for coedge in face
879                .loops
880                .iter_mut()
881                .flat_map(|l| &mut l.coedges)
882                .chain(additions.iter_mut().flat_map(|l| &mut l.coedges))
883            {
884                if solid
885                    .edges
886                    .iter()
887                    .any(|e| e.id == coedge.edge_id && e.degenerate)
888                {
889                    continue;
890                }
891                if !cut_ids.contains(&coedge.edge_id) {
892                    let first =
893                        coedge.pcurve.control_points[0].x / coedge.pcurve.control_points[0].w;
894                    let offset = -(first - shift).floor() - shift;
895                    for p in &mut coedge.pcurve.control_points {
896                        p.x += offset * p.w;
897                    }
898                }
899            }
900            for cut in result.edges.iter_mut().filter(|e| cut_ids.contains(&e.id)) {
901                for (vertex_id, t) in [(cut.start_vertex_id, cut.t0), (cut.end_vertex_id, cut.t1)] {
902                    let point = seam
903                        .evaluate(t)
904                        .or_refuse(KernelStage::Collect, "sphere_cut_vertex")?;
905                    let vertex = result
906                        .vertices
907                        .iter_mut()
908                        .find(|v| v.id == vertex_id)
909                        .ok_or(KernelRefusal::internal(
910                            KernelStage::Collect,
911                            "sphere_cut_vertex",
912                            "blend: sphere seam vertex is missing",
913                        ))?;
914                    vertex.point = point;
915                }
916                cut.curve = seam.clone();
917            }
918            face.surface = surface;
919        }
920        face.loops.extend(additions);
921    }
922    if !separated {
923        return Ok(None);
924    }
925    let old_vertex = solid
926        .vertices
927        .iter()
928        .find(|v| v.id == edge.start_vertex_id)
929        .ok_or(KernelRefusal::internal(
930            KernelStage::Collect,
931            "domain_cut_vertex",
932            "blend: closed ring vertex is missing",
933        ))?;
934    let vertex_id = take_id();
935    result.vertices.push(VertexRecord {
936        id: vertex_id,
937        point: old_vertex.point,
938    });
939    let ring = result
940        .edges
941        .iter_mut()
942        .find(|e| e.id == edge_id)
943        .ok_or(KernelRefusal::internal(
944            KernelStage::Collect,
945            "domain_cut_ring",
946            "blend: closed ring edge is missing",
947        ))?;
948    ring.start_vertex_id = vertex_id;
949    ring.end_vertex_id = vertex_id;
950    Ok(Some(result))
951}
952
953/// Imported faces can carry a slit from a closed rim to a pole and straight
954/// back along the same UV track. It has no area and is not a periodic seam
955/// (whose two pcurves differ by a period). Moving the rim must not drag this
956/// artificial cut onto a different meridian. Cancel only adjacent opposite
957/// uses of an edge with no other users, and prove that their UV tracks agree.
958fn remove_retraced_cuts(
959    solid: &BrepSolid,
960    edge_id: u64,
961) -> Result<Option<BrepSolid>, KernelRefusal> {
962    let mut result = solid.clone();
963    let mut removed = Vec::new();
964    for face in result.shells.iter_mut().flat_map(|shell| &mut shell.faces) {
965        for rim in &mut face.loops {
966            if !rim.coedges.iter().any(|coedge| coedge.edge_id == edge_id) {
967                continue;
968            }
969            loop {
970                let n = rim.coedges.len();
971                let mut pair = None;
972                for i in 0..n {
973                    let j = (i + 1) % n;
974                    let (a, b) = (&rim.coedges[i], &rim.coedges[j]);
975                    if a.edge_id == edge_id || a.edge_id != b.edge_id || a.forward == b.forward {
976                        continue;
977                    }
978                    let users = solid
979                        .shells
980                        .iter()
981                        .flat_map(|s| &s.faces)
982                        .flat_map(|f| &f.loops)
983                        .flat_map(|l| &l.coedges)
984                        .filter(|c| c.edge_id == a.edge_id)
985                        .count();
986                    if users != 2 {
987                        continue;
988                    }
989                    let [a0, a1] = a
990                        .pcurve
991                        .domain()
992                        .or_refuse(KernelStage::Collect, "cut_domain")?;
993                    let [b0, b1] = b
994                        .pcurve
995                        .domain()
996                        .or_refuse(KernelStage::Collect, "cut_domain")?;
997                    // Compare the complete reversed rational representation,
998                    // not a few probes that could miss a curved excursion.
999                    let agrees =
1000                        a.pcurve.degree == b.pcurve.degree
1001                            && a.pcurve.knots.len() == b.pcurve.knots.len()
1002                            && a.pcurve.control_points.len() == b.pcurve.control_points.len()
1003                            && a.pcurve.knots.iter().zip(b.pcurve.knots.iter().rev()).all(
1004                                |(x, y)| {
1005                                    ((x - a0) / (a1 - a0) - (b1 - y) / (b1 - b0)).abs() <= 1e-9
1006                                },
1007                            )
1008                            && a.pcurve
1009                                .control_points
1010                                .iter()
1011                                .zip(b.pcurve.control_points.iter().rev())
1012                                .all(|(p, q)| {
1013                                    p.point()
1014                                        .ok()
1015                                        .zip(q.point().ok())
1016                                        .is_some_and(|(p, q)| p.sub(q).length() <= 1e-9)
1017                                        && (p.w / a.pcurve.control_points[0].w
1018                                            - q.w / b.pcurve.control_points.last().unwrap().w)
1019                                            .abs()
1020                                            <= 1e-9
1021                                });
1022                    if agrees {
1023                        pair = Some((i, j, a.edge_id));
1024                        break;
1025                    }
1026                }
1027                let Some((i, j, id)) = pair else {
1028                    break;
1029                };
1030                rim.coedges.remove(i.max(j));
1031                rim.coedges.remove(i.min(j));
1032                removed.push(id);
1033            }
1034        }
1035    }
1036    if removed.is_empty() {
1037        return Ok(None);
1038    }
1039    result.edges.retain(|edge| !removed.contains(&edge.id));
1040    super::open::prune_orphan_vertices(&mut result);
1041    Ok(Some(result))
1042}
1043
1044/// Where the second support crosses ITS seam-structured carrier's seam
1045/// meridian (both-seam closed edges).
1046struct SecondSeamSplit {
1047    fit_parameter: f64,
1048    crossing_v: f64,
1049    /// Signed carrier period travelled by the unwrapped pcurve track.
1050    period: f64,
1051}
1052
1053/// Replace the blended edge in both mating loops, trim the seam edge of an
1054/// anchored closed carrier, and insert the blend face.
1055/// The carve of a folding closed-edge wall: measure the fitted rows against
1056/// the carriers between the stations they were fitted through (the network's
1057/// own rail bar, half `intersection_fit`), trace the crease on the fitted
1058/// surface and fit it to `pcurve_consistency`, and refuse by name a lens
1059/// that straddles the wall's own seam (the closed rows join at u = 0 ≡ 1, and
1060/// the crease tracer reads one parameter rectangle).
1061/// Top of the closed march's carve ladder: `SEED_INTERVALS << 8` = 4096
1062/// intervals, the station count the chain lane's ladder also tops out near
1063/// (`CHAIN_CARVE_MAX_PER_SEGMENT` per segment).
1064const CARVE_MAX_REFINEMENT_DEPTH: usize = MAX_REFINEMENT_DEPTH + 4;
1065
1066/// How far the closed rows' rails stand off their carriers BETWEEN the
1067/// stations they were fitted through, at 0.25/0.5/0.75 of every span — the
1068/// open march's `rails_off_carriers` reading on closed rows.
1069fn rails_off_closed_carriers(
1070    rows: &FittedRows,
1071    stations: &[Station],
1072    parameters: &[f64],
1073    first: &BlendMate,
1074    second: &BlendMate,
1075) -> Result<f64, KernelRefusal> {
1076    Ok(closed_rail_interval_misses(rows, stations, parameters,
1077        [&first.face.surface, &second.face.surface])?.into_iter().fold(0.0_f64, f64::max))
1078}
1079
1080fn carve_closed_wall(
1081    solid: &BrepSolid,
1082    rows: &FittedRows,
1083    stations: &[Station],
1084    parameters: &[f64],
1085    first: &BlendMate,
1086    second: &BlendMate,
1087    radius: f64,
1088) -> Result<crate::blend::carve::CarvedCrease, KernelRefusal> {
1089    let policy = crate::KernelTolerances::for_solid(solid, 1e-7);
1090    let rail_bar = 0.5 * policy.intersection_fit;
1091    let off = rails_off_closed_carriers(rows, stations, parameters, first, second)?;
1092    crate::blend::carve::carve_trace(format_args!(
1093        "blend closed-edge carve: {} stations, rails {off:.3e} off their carriers between stations (bar {rail_bar:.3e})",
1094        parameters.len()
1095    ));
1096    if off > rail_bar {
1097        return Err(crate::blend::fold::wall_fold_refusal(
1098            Vec::new(),
1099            format!(
1100                "{} {radius} fits this edge: the wall folds, and the closed march's fitted rails stand {off:.3e} off their carriers between stations (bar {rail_bar:.3e}), so there is no wall accurate enough to carve",
1101                crate::blend::WALL_FOLDS
1102            ),
1103        ));
1104    }
1105    let Some(carved) = crate::blend::carve::trace_wall_crease(&rows.surface, radius)? else {
1106        return Err(KernelRefusal::internal(KernelStage::Refine, "carve_no_fold", "blend closed-edge carve: the wall re-marched under Carve carries no fold to carve, but the march refused one"));
1107    };
1108    let [u0, u1] = rows.u_domain;
1109    let margin = 1e-3 * (u1 - u0);
1110    let straddles = carved.ends.iter().any(|end| end[0] <= u0 + margin || end[0] >= u1 - margin);
1111    if straddles {
1112        return Err(crate::blend::fold::wall_fold_refusal(
1113            Vec::new(),
1114            format!(
1115                "{} {radius} fits this edge: the wall folds, and the fold band straddles the wall's own seam (lens ends at u = {:.6} and {:.6} of [{u0:.3}, {u1:.3}]), which the crease tracer reads as two half-lenses; carving across the seam is not built",
1116                crate::blend::WALL_FOLDS, carved.ends[0][0], carved.ends[1][0]
1117            ),
1118        ));
1119    }
1120    crate::blend::carve::fit_crease(&rows.surface, &carved, policy.pcurve_consistency)
1121}
1122
1123/// A support rail's pcurve on its mate `surface` (a closed edge's whole rail,
1124/// or an open edge's rail trimmed to its crossing window), fitted to the
1125/// rail's own projected track at the pcurve floor
1126/// (`chain::project_piece_pcurve`, the closed chain's support-piece fitter,
1127/// which refuses a fit off that floor), on the rail's parameter domain and in
1128/// the same carrier period as the interpolated pcurve `old` it replaces. A
1129/// closed rail around a periodic carrier spans a whole period, so its ends are
1130/// not pinned by the fitter (both would go to one seam); instead an end whose
1131/// `old` coordinate lies exactly on a seam keeps that coordinate, so the loop
1132/// still closes on the seam the surgery built it on.
1133///
1134/// The shipped curve is read again at the floor (`read_shipped_pcurve`). The
1135/// fitter's stencil reads j/16 of each TRACK interval and the shipped read
1136/// j/16 of each span of the fitted curve's own (averaged) knots, so a fit the
1137/// stencil reads just under the floor can read just over it as shipped (the
1138/// 2026-09-27 revolve: 9.923e-8 on the stencil, 1.037e-7 at t 0.4902). Such
1139/// a fit is refitted, at the SAME floor, with its track refined where the
1140/// shipped read found the miss, up to `SHIPPED_READ_REPAIRS` times; the floor
1141/// never moves, and a fit read within it the first time ships as before.
1142pub(super) fn refit_closed_support_pcurve(rail: &NurbsCurve, old: &NurbsCurve, surface: &NurbsSurface) -> Result<NurbsCurve, KernelRefusal> {
1143    const SHIPPED_READ_REPAIRS: usize = 3;
1144    let floor = crate::pcurve::PCURVE_REFINEMENT_TOLERANCE;
1145    let [d0, d1] = rail.domain().or_refuse(KernelStage::Refine, "domain")?;
1146    let mut near: Vec<f64> = Vec::new();
1147    let (mut fitted, mut read) = fit_and_read_support(rail, old, surface, &near)?;
1148    for _ in 0..SHIPPED_READ_REPAIRS {
1149        if read.0 <= floor || !(d1 > d0) {
1150            break;
1151        }
1152        near.push((read.3 - d0) / (d1 - d0));
1153        match fit_and_read_support(rail, old, surface, &near) {
1154            Ok(repaired) => (fitted, read) = repaired,
1155            Err(_) => break,
1156        }
1157    }
1158    let (miss, standoff, samples, _) = read;
1159    if !(miss <= floor) {
1160        return Err(KernelRefusal::non_convergence(KernelStage::Refine, "closed_support_pcurve_floor", format!(
1161            "{} a blend's support pcurve, as shipped, misses its rail's track by {miss:.3e} at {samples} samples, \
1162             against a floor of {floor:.1e}",
1163            crate::blend::PCURVE_OFF_FLOOR,
1164        )));
1165    }
1166    Ok(fitted)
1167}
1168
1169/// [`refit_closed_support_pcurve`]'s fit, its track refined near the
1170/// fractions `near` first, and the shipped curve's read (miss, standoff,
1171/// samples, worst rail parameter).
1172fn fit_and_read_support(
1173    rail: &NurbsCurve,
1174    old: &NurbsCurve,
1175    surface: &NurbsSurface,
1176    near: &[f64],
1177) -> Result<(NurbsCurve, (f64, f64, usize, f64)), KernelRefusal> {
1178    let [d0, d1] = rail.domain().or_refuse(KernelStage::Refine, "domain")?;
1179    // Every foot of the track is sought from the march's own trim `old` at
1180    // the same rail parameter (`pcurve_guide`), on the branch it runs on.
1181    let guide = crate::blend::chain::pcurve_guide(old, surface)?;
1182    let mut fitted = crate::blend::chain::project_piece_pcurve_within(rail, surface, false, false, Some(&guide), near)?;
1183    // The fitter's parameter is the track fraction; the surgery reads the
1184    // pcurve at the rail's own parameter.
1185    let [f0, f1] = fitted.domain().or_refuse(KernelStage::Refine, "domain")?;
1186    for knot in fitted.knots.iter_mut() {
1187        *knot = d0 + (*knot - f0) / (f1 - f0) * (d1 - d0);
1188    }
1189    let debug = std::env::var_os("BREP_DEBUG_TRACK_FIT").is_some();
1190    if debug {
1191        // Diagnostic only: the same read before the period shift and the seam
1192        // ends are set (the shift alone moves no point of the carrier).
1193        match crate::blend::track_fit::read_shipped_pcurve(surface, &|t| rail.evaluate(t), &fitted, d0, d1) {
1194            Ok((miss, standoff, samples, worst)) => eprintln!(
1195                "TRACK_FIT closed-edge support: before the seam ends, miss {miss:.3e} at t {worst:.9} ({samples} samples), standoff {standoff:.3e}"),
1196            Err(error) => eprintln!("TRACK_FIT closed-edge support: before the seam ends, unreadable: {error}"),
1197        }
1198    }
1199    let (closed_u, closed_v) = surface.closed_directions().or_refuse(KernelStage::Refine, "closed_directions")?;
1200    let [u0, u1] = surface.domain_u().or_refuse(KernelStage::Refine, "domain_u")?;
1201    let [v0, v1] = surface.domain_v().or_refuse(KernelStage::Refine, "domain_v")?;
1202    let old_start = old.evaluate(d0).or_refuse(KernelStage::Refine, "evaluate")?;
1203    let old_end = old.evaluate(d1).or_refuse(KernelStage::Refine, "evaluate")?;
1204    let new_start = fitted.evaluate(d0).or_refuse(KernelStage::Refine, "evaluate")?;
1205    // Whole periods only: the same carrier point, in the old pcurve's period.
1206    let shift = |closed: bool, from: f64, to: f64, period: f64| if closed { ((to - from) / period).round() * period } else { 0.0 };
1207    let (shift_u, shift_v) = (shift(closed_u, new_start.x, old_start.x, u1 - u0), shift(closed_v, new_start.y, old_start.y, v1 - v0));
1208    for point in fitted.control_points.iter_mut() {
1209        point.x += shift_u * point.w;
1210        point.y += shift_v * point.w;
1211    }
1212    let on_seam = |closed: bool, value: f64, low: f64, high: f64| {
1213        let period = high - low;
1214        closed && ((value - low) / period - ((value - low) / period).round()).abs() * period <= 1e-12 * period.max(1.0)
1215    };
1216    let last = fitted.control_points.len() - 1;
1217    for (index, end) in [(0, old_start), (last, old_end)] {
1218        let point = &mut fitted.control_points[index];
1219        if on_seam(closed_u, end.x, u0, u1) {
1220            point.x = end.x * point.w;
1221        }
1222        if on_seam(closed_v, end.y, v0, v1) {
1223            point.y = end.y * point.w;
1224        }
1225    }
1226    // The curve SHIPPED is not the one the fitter checked (its domain, period
1227    // and seam ends were edited), so it is read again, at the rail's own
1228    // parameter, against the fitter's floor; a miss refuses as the fitter's
1229    // own would, and an unreadable sample refuses.
1230    let (miss, standoff, samples, worst) =
1231        crate::blend::track_fit::read_shipped_pcurve(surface, &|t| rail.evaluate(t), &fitted, d0, d1)?;
1232    if debug {
1233        eprintln!("TRACK_FIT closed-edge support: shipped pcurve miss {miss:.3e} at t {worst:.9} of [{d0:.9}, {d1:.9}] ({samples} samples); rail standoff from its carrier {standoff:.3e}");
1234    }
1235    Ok((fitted, (miss, standoff, samples, worst)))
1236}
1237
1238
1239fn build_surgery(
1240    solid: &BrepSolid,
1241    edge: &EdgeRecord,
1242    first: &BlendMate,
1243    second: &BlendMate,
1244    rows: FittedRows,
1245    second_split: Option<SecondSeamSplit>,
1246    name: Option<&str>,
1247    crease: Option<crate::blend::carve::CarvedCrease>,
1248) -> Result<BrepSolid, KernelRefusal> {
1249    let mut result = solid.clone();
1250    let mut take_id = crate::blend::edge::fresh_id_source(solid);
1251
1252    let [u_start, u_end] = rows.u_domain;
1253    let cr_start = rows.cr.evaluate(u_start).or_refuse(KernelStage::Refine, "evaluate")?;
1254    let cs_start = rows.cs.evaluate(u_start).or_refuse(KernelStage::Refine, "evaluate")?;
1255    let vertex1_id = take_id();
1256    let vertex2_id = take_id();
1257    result.vertices.push(VertexRecord {
1258        id: vertex1_id,
1259        point: cr_start,
1260    });
1261    result.vertices.push(VertexRecord {
1262        id: vertex2_id,
1263        point: cs_start,
1264    });
1265
1266    let cr_edge_id = take_id();
1267    result.edges.push(EdgeRecord {
1268        id: cr_edge_id,
1269        curve: rows.cr.clone(),
1270        t0: u_start,
1271        t1: u_end,
1272        start_vertex_id: vertex1_id,
1273        end_vertex_id: vertex1_id,
1274        degenerate: false,
1275        name: None,
1276    });
1277    // Second support: whole closed edge, or two pieces split where it
1278    // crosses ITS carrier's seam meridian (both-seam closed edges).  Each
1279    // piece records (edge id, fit-space window, forward pcurve).
1280    let mut cs_pieces: Vec<(u64, [f64; 2], NurbsCurve)> = Vec::new();
1281    let mut second_seam_vertex = vertex2_id;
1282    let mut second_seam_point = cs_start;
1283    let mut second_crossing_v = 0.0;
1284    if let Some(split) = &second_split {
1285        let p = split.fit_parameter;
1286        second_crossing_v = split.crossing_v;
1287        let (cs_a, cs_b) = rows.cs.split(p).or_refuse(KernelStage::Refine, "split")?;
1288        let (pc_a, pc_b) = rows.cs_pcurve.split(p).or_refuse(KernelStage::Refine, "split")?;
1289        // Bring the wrapped second piece back into the carrier's domain.
1290        let mut pc_b = pc_b;
1291        for point in pc_b.control_points.iter_mut() {
1292            point.x -= split.period * point.w;
1293        }
1294        let w2s = take_id();
1295        second_seam_vertex = w2s;
1296        second_seam_point = rows.cs.evaluate(p).or_refuse(KernelStage::Refine, "evaluate")?;
1297        result.vertices.push(VertexRecord {
1298            id: w2s,
1299            point: second_seam_point,
1300        });
1301        let edge_a = take_id();
1302        result.edges.push(EdgeRecord {
1303            id: edge_a,
1304            curve: cs_a,
1305            t0: u_start,
1306            t1: p,
1307            start_vertex_id: vertex2_id,
1308            end_vertex_id: w2s,
1309            degenerate: false,
1310            name: None,
1311        });
1312        cs_pieces.push((edge_a, [u_start, p], pc_a));
1313        let edge_b = take_id();
1314        result.edges.push(EdgeRecord {
1315            id: edge_b,
1316            curve: cs_b,
1317            t0: p,
1318            t1: u_end,
1319            start_vertex_id: w2s,
1320            end_vertex_id: vertex2_id,
1321            degenerate: false,
1322            name: None,
1323        });
1324        cs_pieces.push((edge_b, [p, u_end], pc_b));
1325    } else {
1326        let cs_edge_id = take_id();
1327        result.edges.push(EdgeRecord {
1328            id: cs_edge_id,
1329            curve: rows.cs.clone(),
1330            t0: u_start,
1331            t1: u_end,
1332            start_vertex_id: vertex2_id,
1333            end_vertex_id: vertex2_id,
1334            degenerate: false,
1335            name: None,
1336        });
1337        cs_pieces.push((cs_edge_id, [u_start, u_end], rows.cs_pcurve.clone()));
1338    }
1339    let seam_curve = rows.surface.iso_curve_u(u_start).or_refuse(KernelStage::Refine, "iso_curve_u")?;
1340    let [seam_t0, seam_t1] = seam_curve.domain().or_refuse(KernelStage::Refine, "domain")?;
1341    let blend_seam_id = take_id();
1342    result.edges.push(EdgeRecord {
1343        id: blend_seam_id,
1344        curve: seam_curve,
1345        t0: seam_t0,
1346        t1: seam_t1,
1347        start_vertex_id: vertex1_id,
1348        end_vertex_id: vertex2_id,
1349        degenerate: false,
1350        name: None,
1351    });
1352
1353    let old_vertex = edge.start_vertex_id;
1354    let replace_in_face = |result: &mut BrepSolid,
1355                           mate: &BlendMate,
1356                           pieces: &[(u64, [f64; 2], NurbsCurve)],
1357                           uv_new: [f64; 2]|
1358     -> Result<bool, KernelRefusal> {
1359        let v_new = uv_new[1];
1360        // (edge id, the START vertex is the removed one, the END vertex is).
1361        let seam_edge_ends: Vec<(u64, bool, bool)> = result
1362            .edges
1363            .iter()
1364            .filter(|candidate| {
1365                candidate.id != edge.id
1366                    && (candidate.start_vertex_id == old_vertex
1367                        || candidate.end_vertex_id == old_vertex)
1368            })
1369            .map(|candidate| {
1370                (
1371                    candidate.id,
1372                    candidate.start_vertex_id == old_vertex,
1373                    candidate.end_vertex_id == old_vertex,
1374                )
1375            })
1376            .collect();
1377        let seam_edge_ids: Vec<u64> = seam_edge_ends.iter().map(|(id, ..)| *id).collect();
1378        let pcurve_band = crate::KernelTolerances::for_solid(result, 1e-7).pcurve_consistency;
1379        let debug = std::env::var("BREP_DEBUG_NETWORK").is_ok();
1380        let face = result
1381            .shells
1382            .iter_mut()
1383            .flat_map(|shell| &mut shell.faces)
1384            .find(|face| face.id == mate.face.id)
1385            .ok_or(KernelRefusal::internal(KernelStage::Sew, "mate_face", "blend: mate face lost during surgery"))?;
1386        let face_id = face.id;
1387        let FaceRecord { surface, loops, .. } = &mut *face;
1388        let loop_record = &mut loops[mate.loop_index];
1389        let position = loop_record
1390            .coedges
1391            .iter()
1392            .position(|coedge| coedge.edge_id == edge.id)
1393            .ok_or(KernelRefusal::internal(KernelStage::Sew, "edge_coedge", "blend: edge coedge lost during surgery"))?;
1394        let old_forward = loop_record.coedges[position].forward;
1395        let old_seam_v = {
1396            let pcurve = &loop_record.coedges[position].pcurve;
1397            let [d0, _] = pcurve.domain().or_refuse(KernelStage::Refine, "domain")?;
1398            pcurve.evaluate(d0).or_refuse(KernelStage::Refine, "evaluate")?.y
1399        };
1400        // Replacement coedges: pieces run in u order; a reversed loop use
1401        // takes them in reverse order, each reversed.
1402        let mut replacements = Vec::new();
1403        let ordered: Vec<&(u64, [f64; 2], NurbsCurve)> = if old_forward {
1404            pieces.iter().collect()
1405        } else {
1406            pieces.iter().rev().collect()
1407        };
1408        for (index, (edge_id, _, pcurve)) in ordered.iter().enumerate() {
1409            replacements.push(CoedgeRecord {
1410                id: if index == 0 {
1411                    loop_record.coedges[position].id
1412                } else {
1413                    0 // patched by the caller with fresh ids
1414                },
1415                edge_id: *edge_id,
1416                forward: old_forward,
1417                pcurve: if old_forward {
1418                    pcurve.clone()
1419                } else {
1420                    pcurve.reversed().or_refuse(KernelStage::Refine, "reversed")?
1421                },
1422            });
1423        }
1424        loop_record
1425            .coedges
1426            .splice(position..=position, replacements);
1427        let mut repaired = false;
1428        if loop_record.coedges.len() > 1 {
1429            // Anchored carrier: the loop's other coedges ride the seam
1430            // meridian between the old edge and the rest of the trim.
1431            // Pull the pcurve endpoint that met the old edge down to the
1432            // support crossing.  (The seam EDGE itself is trimmed after
1433            // both replacements, outside this borrow.)
1434            for coedge in &mut loop_record.coedges {
1435                if pieces
1436                    .iter()
1437                    .any(|(edge_id, ..)| *edge_id == coedge.edge_id)
1438                    || !seam_edge_ids.contains(&coedge.edge_id)
1439                {
1440                    continue;
1441                }
1442                let controls = &coedge.pcurve.control_points;
1443                let last_index = controls.len() - 1;
1444                let first_u = controls[0].x / controls[0].w;
1445                let last_u = controls[last_index].x / controls[last_index].w;
1446                let constant_u = (first_u - last_u).abs()
1447                    <= 1e-9 * (1.0 + first_u.abs().max(last_u.abs()));
1448                // Which end of the pcurve met the removed vertex, and the uv
1449                // the crossing sits at on this carrier.
1450                let (target, target_uv, fraction) = if constant_u {
1451                    // Seam MERIDIAN pcurve: constant u, runs along the v
1452                    // direction; the endpoint at the removed edge's v moves
1453                    // onto the support crossing.
1454                    let first_v = controls[0].y / controls[0].w;
1455                    let last_v = controls[last_index].y / controls[last_index].w;
1456                    let target = if (first_v - old_seam_v).abs() < (last_v - old_seam_v).abs() {
1457                        0
1458                    } else {
1459                        last_index
1460                    };
1461                    let span = last_v - first_v;
1462                    let fraction = if span.abs() > 0.0 { (v_new - first_v) / span } else { 0.0 };
1463                    (target, [first_u, v_new], fraction)
1464                } else {
1465                    // The adjacent trim runs along U at constant v — the
1466                    // carrier's OTHER (v) seam, which is what a closed edge
1467                    // lying on one whole MERIDIAN borders (an axial-plane
1468                    // collar on a torus).  Its endpoint moves in U, not V,
1469                    // and the v-distance rule above cannot pick the end at
1470                    // all: both ends share the same v, so it always lands on
1471                    // `last_index`, which is wrong for one of the seam PAIR
1472                    // (the v-seam edge appears twice in the loop, at v = 0
1473                    // and v = 1).  Take the end from the TOPOLOGY.
1474                    let Some((_, start_is_old, end_is_old)) = seam_edge_ends
1475                        .iter()
1476                        .find(|(id, ..)| *id == coedge.edge_id)
1477                        .copied()
1478                    else {
1479                        continue;
1480                    };
1481                    if start_is_old == end_is_old {
1482                        return Err(KernelRefusal::ill_posed(KernelStage::Sew, "neighbour_trim_end", format!(
1483                            "blend: neighbour edge {} meets the blended edge at both ends; \
1484                             its trim end is ambiguous",
1485                            coedge.edge_id
1486                        )));
1487                    }
1488                    // `forward` maps the coedge's pcurve domain start onto the
1489                    // edge's t0 (its start vertex).
1490                    let target = if start_is_old == coedge.forward { 0 } else { last_index };
1491                    let v_end = controls[target].y / controls[target].w;
1492                    let span = last_u - first_u;
1493                    let fraction = if span.abs() > 0.0 { (uv_new[0] - first_u) / span } else { 0.0 };
1494                    (target, [uv_new[0], v_end], fraction)
1495                };
1496                // The trim is a SPLIT of the pcurve at the crossing's own
1497                // parameter, never a moved control point.  Moving the end
1498                // control point of a pcurve with interior knots re-slopes its
1499                // end span alone and breaks the pcurve ↔ edge parameter
1500                // correspondence validate reads (measured 2026-10-03 on the
1501                // torus-box-edge and axial-collar documents: the torus's
1502                // v-seam pcurve, a 3-span polyline, read 0.548 mm off its
1503                // edge at the same parameter while its image was on the edge
1504                // to 1e-10).  The seam-edge pass below projects the same
1505                // physical point onto the edge curve for the edge's own
1506                // parameter, so both sides keep the correspondence they had.
1507                let [q0, q1] = coedge.pcurve.domain().or_refuse(KernelStage::Refine, "domain")?;
1508                let target_point = surface
1509                    .evaluate_extended(target_uv[0], target_uv[1])
1510                    .or_refuse(KernelStage::Refine, "evaluate_extended")?;
1511                // Seeded at the AFFINE fraction of the pcurve's extent in the
1512                // moving direction (the seed `trim_edge_at` uses), not at the
1513                // pcurve's end: a crossing deep in a long seam would hand the
1514                // point solver a far seed and turn a blend that built into a
1515                // refusal.  The solver refines the seed to the physical point.
1516                let seed = (q0 + (q1 - q0) * fraction).clamp(q0.min(q1), q0.max(q1));
1517                let q_split = trim_parameter_at_point(
1518                    surface, &coedge.pcurve, seed, target_point, pcurve_band,
1519                )?;
1520                let interior = q_split > q0 + 1e-9 * (q1 - q0) && q_split < q1 - 1e-9 * (q1 - q0);
1521                if debug {
1522                    eprintln!(
1523                        "closed-edge seam trim: face {face_id} edge {} coedge {} ({}): {} at q {q_split:.9} of [{q0:.6}, {q1:.6}], {} control points",
1524                        coedge.edge_id, coedge.id, if constant_u { "meridian, constant u" } else { "along u" },
1525                        if interior { "SPLIT" } else { "EXTEND" }, controls.len()
1526                    );
1527                }
1528                if interior {
1529                    let (low, high) = coedge.pcurve.split(q_split).or_refuse(KernelStage::Refine, "split")?;
1530                    coedge.pcurve = if target == 0 { high } else { low };
1531                } else {
1532                    // The crossing lies at or past the pcurve's end: the seam
1533                    // EXTENDS to meet it.  Exact for a two-point linear
1534                    // pcurve, whose one span IS the whole parameterisation;
1535                    // anything else would need a refit and is refused by name.
1536                    if coedge.pcurve.degree != 1 || controls.len() != 2 {
1537                        return Err(KernelRefusal::unsupported(KernelStage::Sew, "neighbour_seam_extend_curved", format!(
1538                            "blend: neighbour seam edge {} would have to extend past its own end to meet the blend's support crossing, and its pcurve is not a single straight span (degree {}, {} control points)",
1539                            coedge.edge_id, coedge.pcurve.degree, controls.len()
1540                        )));
1541                    }
1542                    let w = coedge.pcurve.control_points[target].w;
1543                    coedge.pcurve.control_points[target] = crate::Vec4::from_point(
1544                        Vec3::new(target_uv[0], target_uv[1], 0.0), w,
1545                    );
1546                }
1547                repaired = true;
1548            }
1549        }
1550        Ok(repaired)
1551    };
1552    let uv1_new = rows.cr_pcurve.evaluate(u_start).or_refuse(KernelStage::Refine, "evaluate")?;
1553    let v1_new = uv1_new.y;
1554    let cr_pieces = vec![(cr_edge_id, [u_start, u_end], rows.cr_pcurve.clone())];
1555    let mut repaired = replace_in_face(&mut result, first, &cr_pieces, [uv1_new.x, v1_new])?;
1556    let uv2_new = rows.cs_pcurve.evaluate(u_start).or_refuse(KernelStage::Refine, "evaluate")?;
1557    let v2_new = if second_split.is_some() {
1558        second_crossing_v
1559    } else {
1560        uv2_new.y
1561    };
1562    repaired |= replace_in_face(&mut result, second, &cs_pieces, [uv2_new.x, v2_new])?;
1563
1564    // A contact circle about an axis oblique to a sphere's stored polar axis
1565    // can wind once around that sphere's U period.  Such a circle is not an
1566    // ordinary hole in the full-domain sphere rectangle: it bounds a cap with
1567    // one of the collapsed pole rims.  Keep that pole as the companion loop so
1568    // containment, integration, and tessellation see the actual cap topology.
1569    let collapse_winding_sphere_cap =
1570        |result: &mut BrepSolid, face_id: u64, support_ids: &[u64]| -> Result<(), KernelRefusal> {
1571            let face = result
1572                .shells
1573                .iter_mut()
1574                .flat_map(|shell| &mut shell.faces)
1575                .find(|face| face.id == face_id)
1576                .ok_or(KernelRefusal::internal(KernelStage::Sew, "sphere_carrier", "blend: sphere carrier lost during cap surgery"))?;
1577            if !matches!(
1578                face.surface.analytic(),
1579                Some(crate::AnalyticSurface::Sphere { .. })
1580            ) || face.surface.closed_directions().or_refuse(KernelStage::Refine, "closed_directions")? != (true, false)
1581            {
1582                return Ok(());
1583            }
1584            let [u0, u1] = face.surface.domain_u().or_refuse(KernelStage::Refine, "domain_u")?;
1585            let [v0, v1] = face.surface.domain_v().or_refuse(KernelStage::Refine, "domain_v")?;
1586            let period = u1 - u0;
1587            let support_loop = face.loops.iter().position(|loop_record| {
1588                !loop_record.coedges.is_empty()
1589                    && loop_record
1590                        .coedges
1591                        .iter()
1592                        .all(|coedge| support_ids.contains(&coedge.edge_id))
1593            });
1594            let Some(support_loop) = support_loop else {
1595                return Ok(());
1596            };
1597            if face.loops[support_loop].coedges.len() != 1 {
1598                return Ok(());
1599            }
1600            let support = &face.loops[support_loop].coedges[0];
1601            let [s0, s1] = support.pcurve.domain().or_refuse(KernelStage::Refine, "domain")?;
1602            let support_start = support.pcurve.evaluate(s0).or_refuse(KernelStage::Refine, "evaluate")?;
1603            let support_end = support.pcurve.evaluate(s1).or_refuse(KernelStage::Refine, "evaluate")?;
1604            let support_winding = support_end.x - support_start.x;
1605            if (support_winding.abs() - period).abs() > 0.05 * period {
1606                return Ok(());
1607            }
1608
1609            let mut pole: Option<(usize, CoedgeRecord)> = None;
1610            for (loop_index, loop_record) in face.loops.iter().enumerate() {
1611                if loop_index == support_loop {
1612                    continue;
1613                }
1614                for coedge in &loop_record.coedges {
1615                    let [p0, p1] = coedge.pcurve.domain().or_refuse(KernelStage::Refine, "domain")?;
1616                    let start = coedge.pcurve.evaluate(p0).or_refuse(KernelStage::Refine, "evaluate")?;
1617                    let end = coedge.pcurve.evaluate(p1).or_refuse(KernelStage::Refine, "evaluate")?;
1618                    let winding = end.x - start.x;
1619                    let at_pole = (start.y - v0).abs() <= 1e-8 || (start.y - v1).abs() <= 1e-8;
1620                    if at_pole
1621                        && (end.y - start.y).abs() <= 1e-8
1622                        && (winding.abs() - period).abs() <= 0.05 * period
1623                        && winding * support_winding < 0.0
1624                    {
1625                        pole = Some((loop_index, coedge.clone()));
1626                        break;
1627                    }
1628                }
1629                if pole.is_some() {
1630                    break;
1631                }
1632            }
1633            let Some((pole_loop, pole_coedge)) = pole else {
1634                return Ok(());
1635            };
1636            let support_record = face.loops[support_loop].clone();
1637            let pole_id = face.loops[pole_loop].id;
1638            face.loops = vec![
1639                LoopRecord {
1640                    id: pole_id,
1641                    coedges: vec![pole_coedge],
1642                },
1643                support_record,
1644            ];
1645            Ok(())
1646        };
1647    collapse_winding_sphere_cap(&mut result, first.face.id, &[cr_edge_id])?;
1648    let cs_edge_ids: Vec<u64> = cs_pieces.iter().map(|(edge_id, ..)| *edge_id).collect();
1649    collapse_winding_sphere_cap(&mut result, second.face.id, &cs_edge_ids)?;
1650    // Patch the zero coedge ids introduced for extra pieces.
1651    for shell in &mut result.shells {
1652        for face in &mut shell.faces {
1653            for loop_record in &mut face.loops {
1654                for coedge in &mut loop_record.coedges {
1655                    if coedge.id == 0 {
1656                        coedge.id = take_id();
1657                    }
1658                }
1659            }
1660        }
1661    }
1662
1663    // Trim seam EDGES that ended on the removed vertex: their endpoint
1664    // moves onto the support-curve crossing on THEIR carrier (first
1665    // mate's seam -> the blend seam vertex; second mate's -> its own
1666    // seam-crossing vertex).
1667    let new_edge_ids: Vec<u64> = std::iter::once(cr_edge_id)
1668        .chain(cs_pieces.iter().map(|(edge_id, ..)| *edge_id))
1669        .chain(std::iter::once(blend_seam_id))
1670        .collect();
1671    let first_face_edges: Vec<u64> = first
1672        .face
1673        .loops
1674        .iter()
1675        .flat_map(|loop_record| loop_record.coedges.iter().map(|coedge| coedge.edge_id))
1676        .collect();
1677    for seam_edge in result.edges.iter_mut() {
1678        if new_edge_ids.contains(&seam_edge.id) {
1679            continue;
1680        }
1681        if seam_edge.start_vertex_id != old_vertex && seam_edge.end_vertex_id != old_vertex {
1682            continue;
1683        }
1684        let (target_vertex, target_v, target_point) = if first_face_edges.contains(&seam_edge.id) {
1685            (vertex1_id, v1_new, cr_start)
1686        } else {
1687            (second_seam_vertex, v2_new, second_seam_point)
1688        };
1689        let [c0, c1] = seam_edge.curve.domain().or_refuse(KernelStage::Refine, "domain")?;
1690        let clamped = target_v.clamp(c0.min(c1), c0.max(c1));
1691        // Using the support crossing's V as the neighbour's CURVE parameter is
1692        // only valid when that neighbour is the carrier's seam MERIDIAN, whose
1693        // 3D curve is parameterized by v.  Verify it against the vertex the
1694        // endpoint is moving to; a neighbour running along U instead (the
1695        // carrier's v-seam, bordered by a collar edge that lies on one whole
1696        // meridian) lands nowhere near it — the 2026-09-01 report trimmed the
1697        // torus' equator seam to u = 0, 10.885 away from its own vertex, and
1698        // the solid failed validation with "curve start does not match vertex".
1699        let band = 1e-6 * (1.0 + target_point.length());
1700        let split_parameter = if seam_edge.id == edge.id {
1701            // The BLENDED edge itself still carries the removed vertex at this
1702            // point.  Its coedges have already been replaced in both mates, so
1703            // the used-edge sweep below discards it; leave its (meaningless)
1704            // trim exactly as it was rather than asking a doomed edge to pass
1705            // through the support start.
1706            clamped
1707        } else {
1708            match seam_edge.curve.evaluate(clamped) {
1709                Ok(point) if point.sub(target_point).length() <= band => clamped,
1710                _ => {
1711                    repaired = true;
1712                    let projection =
1713                        crate::project_point_to_curve(&seam_edge.curve, target_point).or_refuse(KernelStage::Refine, "project_point_to_curve")?;
1714                    if projection.distance > band {
1715                        return Err(KernelRefusal::unsupported(KernelStage::Sew, "neighbour_off_support", format!(
1716                            "blend: neighbour edge {} does not pass through the blend's support \
1717                             start (off by {:.9}); the trim has no parameter to move to",
1718                            seam_edge.id, projection.distance
1719                        )));
1720                    }
1721                    projection.u.clamp(c0.min(c1), c0.max(c1))
1722                }
1723            }
1724        };
1725        if seam_edge.start_vertex_id == old_vertex {
1726            seam_edge.t0 = split_parameter;
1727            seam_edge.start_vertex_id = target_vertex;
1728        }
1729        if seam_edge.end_vertex_id == old_vertex {
1730            seam_edge.t1 = split_parameter;
1731            seam_edge.end_vertex_id = target_vertex;
1732        }
1733    }
1734
1735    // Blend face: outward orientation matches F1's outward at the v=0 rim.
1736    let mid_u = (u_start + u_end) * 0.5;
1737    let blend_normal = raw_normal(&rows.surface, mid_u, 0.0)?;
1738    let station_uv = rows.cr_pcurve.evaluate(mid_u).or_refuse(KernelStage::Refine, "evaluate")?;
1739    let n1 = raw_normal(&first.face.surface, station_uv.x, station_uv.y)?;
1740    let out1 = if first.face.same_sense {
1741        n1
1742    } else {
1743        n1.scale(-1.0)
1744    };
1745    let same_sense = blend_normal.dot(out1) >= 0.0;
1746    let loop_id = take_id();
1747    let mut coedges = vec![
1748        CoedgeRecord {
1749            id: take_id(),
1750            edge_id: cr_edge_id,
1751            forward: true,
1752            pcurve: crate::sweep_topology::parameter_line(u_start, 0.0, u_end, 0.0).or_refuse(KernelStage::Refine, "parameter_line")?,
1753        },
1754        CoedgeRecord {
1755            id: take_id(),
1756            edge_id: blend_seam_id,
1757            forward: true,
1758            pcurve: crate::sweep_topology::parameter_line(u_end, 0.0, u_end, 1.0).or_refuse(KernelStage::Refine, "parameter_line")?,
1759        },
1760    ];
1761    for (edge_id, window, _) in cs_pieces.iter().rev() {
1762        coedges.push(CoedgeRecord {
1763            id: take_id(),
1764            edge_id: *edge_id,
1765            forward: false,
1766            pcurve: crate::sweep_topology::parameter_line(window[1], 1.0, window[0], 1.0).or_refuse(KernelStage::Refine, "parameter_line")?,
1767        });
1768    }
1769    coedges.push(CoedgeRecord {
1770        id: take_id(),
1771        edge_id: blend_seam_id,
1772        forward: false,
1773        pcurve: crate::sweep_topology::parameter_line(u_start, 1.0, u_start, 0.0).or_refuse(KernelStage::Refine, "parameter_line")?,
1774    });
1775    // The coedges above trace the parameter rectangle counter-clockwise in
1776    // (u, v); that traversal is only the outward boundary when the blend
1777    // surface normal already points outward (same_sense).  When it does not
1778    // (a mate whose support rim runs the other way — e.g. the reversed cap
1779    // of a seam carrier), the loop must wind the OTHER way so its coedges
1780    // traverse the shared support edges opposite to the mate, and so the
1781    // classification tangent test (parameter_point_in_face) stays consistent
1782    // with same_sense.  Reversing the coedge order and each coedge (forward
1783    // flag + pcurve) flips the winding without touching same_sense.
1784    if !same_sense {
1785        coedges.reverse();
1786        for coedge in &mut coedges {
1787            coedge.forward = !coedge.forward;
1788            coedge.pcurve = coedge.pcurve.reversed().or_refuse(KernelStage::Refine, "reversed")?;
1789        }
1790    }
1791    let mut loops = vec![LoopRecord {
1792        id: loop_id,
1793        coedges,
1794    }];
1795    // The CARVED lens: an INNER loop of two coedges on ONE new edge — the
1796    // crease — both of them this face's own, the slit pattern the closed
1797    // chain lane sews (`chain/closed_surgery.rs`): the two sheets of the wall
1798    // are one surface, so the curve where they meet has one edge and two
1799    // coedges.  The lens between them is the part of the swept envelope
1800    // inside the swept ball volume, and dropping it is what makes the wall
1801    // the blend.  Winding: the inner loop runs AGAINST the outer one, read
1802    // off both loops' signed areas rather than assumed.
1803    if let Some(crease) = crease {
1804        let start_vertex = take_id();
1805        let end_vertex = take_id();
1806        let crease_edge = take_id();
1807        result.vertices.push(VertexRecord { id: start_vertex, point: crease.start });
1808        result.vertices.push(VertexRecord { id: end_vertex, point: crease.end });
1809        let [crease_t0, crease_t1] = crease.curve.domain().or_refuse(KernelStage::Refine, "domain")?;
1810        result.edges.push(EdgeRecord {
1811            id: crease_edge,
1812            curve: crease.curve,
1813            t0: crease_t0,
1814            t1: crease_t1,
1815            start_vertex_id: start_vertex,
1816            end_vertex_id: end_vertex,
1817            degenerate: false,
1818            name: name.map(|value| format!("{value}:CREASE")),
1819        });
1820        let outer = crate::blend::chain::signed_area(&loops[0].coedges)?;
1821        let forward_first = crate::blend::chain::signed_area(&[
1822            CoedgeRecord { id: 0, edge_id: crease_edge, forward: true, pcurve: crease.first.clone() },
1823            CoedgeRecord { id: 0, edge_id: crease_edge, forward: false, pcurve: crease.second.reversed().or_refuse(KernelStage::Refine, "reversed")? },
1824        ])?;
1825        let inner = if outer * forward_first < 0.0 {
1826            vec![
1827                CoedgeRecord { id: take_id(), edge_id: crease_edge, forward: true, pcurve: crease.first },
1828                CoedgeRecord { id: take_id(), edge_id: crease_edge, forward: false, pcurve: crease.second.reversed().or_refuse(KernelStage::Refine, "reversed")? },
1829            ]
1830        } else {
1831            vec![
1832                CoedgeRecord { id: take_id(), edge_id: crease_edge, forward: true, pcurve: crease.second },
1833                CoedgeRecord { id: take_id(), edge_id: crease_edge, forward: false, pcurve: crease.first.reversed().or_refuse(KernelStage::Refine, "reversed")? },
1834            ]
1835        };
1836        loops.push(LoopRecord { id: take_id(), coedges: inner });
1837    }
1838    let blend_face = FaceRecord {
1839        id: take_id(),
1840        surface: rows.surface,
1841        same_sense,
1842        loops,
1843        name: name.map(|value| value.to_string()),
1844    };
1845    let shell_index = result
1846        .shells
1847        .iter()
1848        .position(|shell| shell.faces.iter().any(|face| face.id == first.face.id))
1849        .ok_or(KernelRefusal::internal(KernelStage::Sew, "mate_shell", "blend: mate shell lost during surgery"))?;
1850    result.shells[shell_index].faces.push(blend_face);
1851
1852    let used_edges: std::collections::HashSet<u64> = result
1853        .shells
1854        .iter()
1855        .flat_map(|shell| &shell.faces)
1856        .flat_map(|face| &face.loops)
1857        .flat_map(|loop_record| &loop_record.coedges)
1858        .map(|coedge| coedge.edge_id)
1859        .collect();
1860    result
1861        .edges
1862        .retain(|candidate| used_edges.contains(&candidate.id));
1863    let used_vertices: std::collections::HashSet<u64> = result
1864        .edges
1865        .iter()
1866        .flat_map(|candidate| [candidate.start_vertex_id, candidate.end_vertex_id])
1867        .collect();
1868    result
1869        .vertices
1870        .retain(|candidate| used_vertices.contains(&candidate.id));
1871    // A surgery that had to REPAIR a neighbour trim took a branch no existing
1872    // fixture exercises, so it does not get the benefit of the doubt: prove the
1873    // result before returning it.  Paths that never repaired are untouched (the
1874    // check does not run), so this cannot slow or change anything that already
1875    // works — and the new branch can never ship a plausible-looking wrong solid
1876    // the way the un-repaired trim did.
1877    if repaired {
1878        let problems = result.validate();
1879        if !problems.is_empty() {
1880            return Err(KernelRefusal::internal(KernelStage::Validate, "neighbour_trim_closure", format!(
1881                "blend: the repaired neighbour trim did not close ({} issue(s), first: {})",
1882                problems.len(),
1883                problems
1884                    .first()
1885                    .map(|issue| issue.message.as_str())
1886                    .unwrap_or("unknown")
1887            )));
1888        }
1889    }
1890    Ok(result)
1891}