BREP_kernel 0.5.0

A boundary representation (BREP) geometry kernel for building CAD applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
use crate::{KernelRefusal, KernelStage, OrRefuse, RefusalClass};
use super::*;
use super::builder::snap_edge_curve_endpoints;
use super::edge_conform::edge_lies_on;
use super::polish::{collapse_short_one_use_edges, sew_coincident_one_use_edges};

/// Recover a shared 3D edge from two agreeing face p-curves when arrangement
/// welding selected an obsolete carrier curve at a pole transition.
fn repair_edges_from_consistent_pcurves(
    solid: &mut BrepSolid,
    tolerance: f64,
) -> Result<(), KernelRefusal> {
    // Assembly/search tolerances may be deliberately broad. Repairs are
    // governed by the committed pcurve contract so a search radius cannot
    // authorize geometry that the final validator must reject.
    let pcurve_contract = KernelTolerances::for_solid(solid, 1e-7).pcurve_consistency;
    let repair_tolerance = tolerance.min(pcurve_contract * 0.75);
    #[derive(Clone)]
    struct Use {
        face: usize,
        loop_index: usize,
        coedge: usize,
    }
    let faces = solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .collect::<Vec<_>>();
    let edges = solid
        .edges
        .iter()
        .map(|edge| (edge.id, edge))
        .collect::<HashMap<_, _>>();
    let points = solid
        .vertices
        .iter()
        .map(|vertex| (vertex.id, vertex.point))
        .collect::<HashMap<_, _>>();
    let mut uses = HashMap::<u64, Vec<Use>>::default();
    for (face_index, face) in faces.iter().enumerate() {
        for (loop_index, loop_record) in face.loops.iter().enumerate() {
            for (coedge_index, coedge) in loop_record.coedges.iter().enumerate() {
                uses.entry(coedge.edge_id).or_default().push(Use {
                    face: face_index,
                    loop_index,
                    coedge: coedge_index,
                });
            }
        }
    }
    let traversal_start = |coedge: &CoedgeRecord| {
        let edge = edges[&coedge.edge_id];
        if coedge.forward {
            edge.start_vertex_id
        } else {
            edge.end_vertex_id
        }
    };
    let traversal_end = |coedge: &CoedgeRecord| {
        let edge = edges[&coedge.edge_id];
        if coedge.forward {
            edge.end_vertex_id
        } else {
            edge.start_vertex_id
        }
    };
    let mut repairs = Vec::new();
    for edge in solid.edges.iter().filter(|edge| !edge.degenerate) {
        let Some(edge_uses) = uses.get(&edge.id).filter(|uses| uses.len() == 2) else {
            continue;
        };
        let first = &edge_uses[0];
        let first_face = faces[first.face];
        let first_loop = &first_face.loops[first.loop_index];
        let first_coedge = &first_loop.coedges[first.coedge];
        let previous = &first_loop.coedges
            [(first.coedge + first_loop.coedges.len() - 1) % first_loop.coedges.len()];
        let next = &first_loop.coedges[(first.coedge + 1) % first_loop.coedges.len()];
        let intended_traversal_start = traversal_end(previous);
        let intended_traversal_end = traversal_start(next);
        let [p0, p1] = first_coedge.pcurve.domain().or_refuse(KernelStage::Validate, "domain")?;
        // Repairs are uncommon and must satisfy the adaptive validator between
        // interpolation nodes. Use a dense carrier trace for every degree;
        // sparse samples can leave localized SSI-fit drift on tight offsets.
        let sample_count = 256;
        let mut traversal_points = Vec::with_capacity(sample_count + 1);
        let mut parameters = Vec::with_capacity(sample_count + 1);
        for index in 0..=sample_count {
            let fraction = index as f64 / sample_count as f64;
            let uv = first_coedge.pcurve.evaluate(p0 + (p1 - p0) * fraction).or_refuse(KernelStage::Validate, "evaluate")?;
            traversal_points.push(first_face.surface.evaluate(uv.x, uv.y).or_refuse(KernelStage::Validate, "evaluate")?);
            parameters.push(fraction);
        }
        let worst_current = adaptive_coedge_error(
            &first_face.surface,
            &first_coedge.pcurve,
            &edge.curve,
            edge,
            first_coedge.forward,
            repair_tolerance,
        ).or_refuse(KernelStage::Validate, "csg.boolean.assemble.finalize")?;
        if worst_current <= repair_tolerance {
            continue;
        }
        let repair_debug =
            std::env::var("BREP_OS_DEBUG").is_ok_and(|value| !value.is_empty() && value != "0");
        let second = &edge_uses[1];
        let second_face = faces[second.face];
        let second_coedge = &second_face.loops[second.loop_index].coedges[second.coedge];
        let [q0, q1] = second_coedge.pcurve.domain().or_refuse(KernelStage::Validate, "domain")?;
        let mut worst_pair = 0.0f64;
        let pcurves_agree = (0..=16).all(|index| {
            let fraction = index as f64 / 16.0;
            let uv = second_coedge.pcurve.evaluate(q0 + (q1 - q0) * fraction);
            let canonical_fraction = if second_coedge.forward {
                fraction
            } else {
                1.0 - fraction
            };
            let first_fraction = if first_coedge.forward {
                canonical_fraction
            } else {
                1.0 - canonical_fraction
            };
            uv.and_then(|uv| second_face.surface.evaluate(uv.x, uv.y))
                .is_ok_and(|point| {
                    first_coedge
                        .pcurve
                        .evaluate(p0 + (p1 - p0) * first_fraction)
                        .and_then(|first_uv| first_face.surface.evaluate(first_uv.x, first_uv.y))
                        .is_ok_and(|first_point| {
                            let deviation = point.sub(first_point).length();
                            worst_pair = worst_pair.max(deviation);
                            deviation <= repair_tolerance
                        })
                })
        });
        if repair_debug {
            eprintln!(
                "repair candidate edge {} worst_current={worst_current:.6} worst_pair={worst_pair:.6} agree={pcurves_agree} tol={repair_tolerance:.6}",
                edge.id,
            );
        }
        // A shared edge is one source of truth for both incident carriers.
        // Reconstruct it from the midpoint of the two agreeing pcurve traces,
        // rather than privileging whichever face happened to be visited first.
        let mut canonical_points = traversal_points.clone();
        if pcurves_agree {
            let mut averaged = Vec::with_capacity(traversal_points.len());
            let mut dense_pair_agrees = true;
            for (index, first_point) in traversal_points.iter().copied().enumerate() {
                let first_fraction = index as f64 / sample_count as f64;
                let canonical_fraction = if first_coedge.forward {
                    first_fraction
                } else {
                    1.0 - first_fraction
                };
                let second_fraction = if second_coedge.forward {
                    canonical_fraction
                } else {
                    1.0 - canonical_fraction
                };
                let second_uv = second_coedge
                    .pcurve
                    .evaluate(q0 + (q1 - q0) * second_fraction).or_refuse(KernelStage::Validate, "evaluate")?;
                let second_point = second_face.surface.evaluate(second_uv.x, second_uv.y).or_refuse(KernelStage::Validate, "evaluate")?;
                if first_point.sub(second_point).length() > repair_tolerance {
                    dense_pair_agrees = false;
                    break;
                }
                averaged.push(first_point.add(second_point).scale(0.5));
            }
            if dense_pair_agrees {
                canonical_points = averaged;
            }
        }
        let (start_vertex_id, end_vertex_id) = if first_coedge.forward {
            (intended_traversal_start, intended_traversal_end)
        } else {
            canonical_points.reverse();
            (intended_traversal_end, intended_traversal_start)
        };
        canonical_points[0] = points[&start_vertex_id];
        let last = canonical_points.len() - 1;
        canonical_points[last] = points[&end_vertex_id];
        let curve = interpolate_curve(&canonical_points, edge.curve.degree.min(3), &parameters).or_refuse(KernelStage::Validate, "interpolate_curve")?;
        let [candidate_t0, candidate_t1] = curve.domain().or_refuse(KernelStage::Validate, "domain")?;
        let candidate_edge = EdgeRecord {
            id: edge.id,
            curve: curve.clone(),
            t0: candidate_t0,
            t1: candidate_t1,
            start_vertex_id,
            end_vertex_id,
            degenerate: false,
            name: edge.name.clone(),
        };
        let mut first_pcurve = first_coedge.pcurve.clone();
        let mut second_pcurve = second_coedge.pcurve.clone();
        let mut first_error = adaptive_coedge_error(
            &first_face.surface,
            &first_pcurve,
            &curve,
            &candidate_edge,
            first_coedge.forward,
            pcurve_contract,
        ).or_refuse(KernelStage::Validate, "csg.boolean.assemble.finalize")?;
        let mut second_error = adaptive_coedge_error(
            &second_face.surface,
            &second_pcurve,
            &curve,
            &candidate_edge,
            second_coedge.forward,
            pcurve_contract,
        ).or_refuse(KernelStage::Validate, "csg.boolean.assemble.finalize")?;
        let mut rebuilt_pcurves = false;
        if first_error.max(second_error) > pcurve_contract {
            first_pcurve = build_pcurve_on_surface_range(
                &first_face.surface,
                &curve,
                candidate_t0,
                candidate_t1,
                first_coedge.forward,
                repair_tolerance,
            ).or_refuse(KernelStage::Validate, "csg.boolean.assemble.finalize")?;
            second_pcurve = build_pcurve_on_surface_range(
                &second_face.surface,
                &curve,
                candidate_t0,
                candidate_t1,
                second_coedge.forward,
                repair_tolerance,
            ).or_refuse(KernelStage::Validate, "csg.boolean.assemble.finalize")?;
            first_error = adaptive_coedge_error(
                &first_face.surface,
                &first_pcurve,
                &curve,
                &candidate_edge,
                first_coedge.forward,
                pcurve_contract,
            ).or_refuse(KernelStage::Validate, "csg.boolean.assemble.finalize")?;
            second_error = adaptive_coedge_error(
                &second_face.surface,
                &second_pcurve,
                &curve,
                &candidate_edge,
                second_coedge.forward,
                pcurve_contract,
            ).or_refuse(KernelStage::Validate, "csg.boolean.assemble.finalize")?;
            rebuilt_pcurves = true;
        }
        if first_error.max(second_error) > pcurve_contract
            || first_error.max(second_error) >= worst_current
        {
            continue;
        }
        if repair_debug {
            let [r0, r1] = curve.domain().or_refuse(KernelStage::Validate, "domain")?;
            let mut residual = 0.0f64;
            for (sample_index, target) in canonical_points.iter().enumerate() {
                let fraction = sample_index as f64 / (canonical_points.len() - 1) as f64;
                if let Ok(point) = curve.evaluate(r0 + (r1 - r0) * fraction) {
                    residual = residual.max(point.sub(*target).length());
                }
            }
            eprintln!("repair edge {} applied, residual={residual:.6}", edge.id);
        }
        let pcurve_repairs = if rebuilt_pcurves {
            vec![
                (first.clone(), first_pcurve),
                (second.clone(), second_pcurve),
            ]
        } else {
            Vec::<(Use, NurbsCurve)>::new()
        };
        repairs.push((
            edge.id,
            curve,
            start_vertex_id,
            end_vertex_id,
            pcurve_repairs,
        ));
    }
    drop(faces);
    drop(edges);
    for (edge_id, curve, start_vertex_id, end_vertex_id, pcurve_repairs) in repairs {
        let edge = solid
            .edges
            .iter_mut()
            .find(|edge| edge.id == edge_id)
            .unwrap();
        let domain = curve.domain().or_refuse(KernelStage::Validate, "domain")?;
        edge.curve = curve;
        edge.t0 = domain[0];
        edge.t1 = domain[1];
        edge.start_vertex_id = start_vertex_id;
        edge.end_vertex_id = end_vertex_id;
        let mut mutable_faces = solid
            .shells
            .iter_mut()
            .flat_map(|shell| shell.faces.iter_mut())
            .collect::<Vec<_>>();
        for (location, pcurve) in pcurve_repairs {
            mutable_faces[location.face].loops[location.loop_index].coedges[location.coedge]
                .pcurve = pcurve;
        }
    }
    Ok(())
}

/// Commit assembler endpoint welds into edge geometry. Candidate matching is
/// intentionally broader than model accuracy; once a candidate is accepted
/// as one topological vertex, the 3D edge must share that vertex exactly.
///
/// Existing pcurves retain their established periodic branch. The correction
/// radius is far below their consistency contract, and rebuilding can choose
/// the opposite seam branch on a closed carrier.
///
/// Also reused to heal §6.9 fillet/chamfer surgery output, whose re-trimmed
/// original edges can drift from the freshly-built blend corner vertices by
/// the amplified solver/intersection noise of the input (see
/// `fillet::heal_edge_vertex_gaps`).
pub(crate) fn commit_nearby_edge_endpoints(
    solid: &mut BrepSolid,
    search_tolerance: f64,
) -> Result<(), KernelRefusal> {
    let points = solid
        .vertices
        .iter()
        .map(|vertex| (vertex.id, vertex.point))
        .collect::<HashMap<_, _>>();
    let strict_endpoint_tolerance = 1e-5;
    let commit_radius = commit_weld(search_tolerance);
    for edge in &mut solid.edges {
        if edge.degenerate {
            continue;
        }
        let Some(start) = points.get(&edge.start_vertex_id).copied() else {
            continue;
        };
        let Some(end) = points.get(&edge.end_vertex_id).copied() else {
            continue;
        };
        let curve_start = edge.curve.evaluate(edge.t0).or_refuse(KernelStage::Validate, "evaluate")?;
        let curve_end = edge.curve.evaluate(edge.t1).or_refuse(KernelStage::Validate, "evaluate")?;
        let start_gap = curve_start.sub(start).length();
        let end_gap = curve_end.sub(end).length();
        if start_gap <= strict_endpoint_tolerance && end_gap <= strict_endpoint_tolerance {
            continue;
        }
        if start_gap > commit_radius || end_gap > commit_radius {
            continue;
        }
        let curve = snap_edge_curve_endpoints(edge.curve.clone(), edge.t0, edge.t1, start, end)?;
        let [t0, t1] = curve.domain().or_refuse(KernelStage::Validate, "domain")?;
        edge.curve = curve;
        edge.t0 = t0;
        edge.t1 = t1;
    }
    Ok(())
}

/// The exact heal sequence shared by `finalize_assembled_solid` (the offset
/// pipeline) and the boolean assembly repair fallback (roadmap T4.4): commit
/// nearby endpoint welds into edge geometry, collapse tolerance-length one-use
/// slivers, sew coincident one-use pairs, and recover shared edges from
/// agreeing pcurves. No genus/validation/volume gating — that stays with each
/// caller, so this helper's behavior is byte-identical to the inlined sequence
/// it replaces in `finalize_assembled_solid`.
pub(crate) fn apply_assembly_heal_chain(
    solid: &mut BrepSolid,
    tolerance: f64,
) -> Result<(), KernelRefusal> {
    commit_nearby_edge_endpoints(solid, tolerance)?;
    collapse_short_one_use_edges(solid, tolerance * 4.0)?;
    sew_coincident_one_use_edges(solid, tolerance)?;
    repair_edges_from_consistent_pcurves(solid, tolerance)?;
    Ok(())
}

pub(crate) fn finalize_assembled_solid(
    mut solid: BrepSolid,
    tolerance: f64,
) -> Result<BrepSolid, KernelRefusal> {
    apply_assembly_heal_chain(&mut solid, tolerance)?;
    let mut use_counts = HashMap::<u64, usize>::default();
    for coedge in solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .flat_map(|face| &face.loops)
        .flat_map(|loop_record| &loop_record.coedges)
    {
        *use_counts.entry(coedge.edge_id).or_default() += 1;
    }
    let face_count: i64 = solid
        .shells
        .iter()
        .map(|shell| shell.faces.len() as i64)
        .sum();
    let hole_count: i64 = solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .map(|face| face.loops.len().saturating_sub(1) as i64)
        .sum();
    let edge_count = solid.edges.iter().filter(|edge| !edge.degenerate).count() as i64;
    let non_degenerate_vertex_ids = solid
        .edges
        .iter()
        .filter(|edge| !edge.degenerate)
        .flat_map(|edge| [edge.start_vertex_id, edge.end_vertex_id])
        .collect::<HashSet<_>>();
    let vertex_count = solid
        .vertices
        .iter()
        .filter(|vertex| non_degenerate_vertex_ids.contains(&vertex.id))
        .count() as i64;
    let euler = vertex_count - edge_count + face_count - hole_count;
    let numerator = solid.shells.len() as i64 * 2 - euler;
    let edge_degenerate = solid
        .edges
        .iter()
        .map(|edge| (edge.id, edge.degenerate))
        .collect::<HashMap<_, _>>();
    let has_degenerate_open_edge = use_counts
        .iter()
        .any(|(edge_id, count)| *count == 1 && edge_degenerate.get(edge_id) == Some(&true));
    let only_degenerate_open_edges = has_degenerate_open_edge
        && use_counts.iter().all(|(edge_id, count)| {
            *count == 2 || (*count == 1 && edge_degenerate.get(edge_id) == Some(&true))
        });
    if (numerator < 0 || numerator % 2 != 0) && !only_degenerate_open_edges {
        let edge_by_id = solid
            .edges
            .iter()
            .map(|edge| (edge.id, edge))
            .collect::<HashMap<_, _>>();
        let point_by_id = solid
            .vertices
            .iter()
            .map(|vertex| (vertex.id, vertex.point))
            .collect::<HashMap<_, _>>();
        let open = use_counts
            .iter()
            .filter(|(_, count)| **count == 1)
            .filter_map(|(edge_id, _)| {
                let edge = edge_by_id.get(edge_id)?;
                Some((
                    *edge_id,
                    point_by_id[&edge.start_vertex_id],
                    point_by_id[&edge.end_vertex_id],
                ))
            })
            .collect::<Vec<_>>();
        let open_faces = solid
            .shells
            .iter()
            .flat_map(|shell| &shell.faces)
            .flat_map(|face| {
                face.loops
                    .iter()
                    .flat_map(|loop_record| &loop_record.coedges)
                    .filter(|coedge| use_counts.get(&coedge.edge_id) == Some(&1))
                    .map(|coedge| (coedge.edge_id, face.id))
                    .collect::<Vec<_>>()
            })
            .collect::<Vec<_>>();
        let open_ids = use_counts
            .iter()
            .filter(|(_, count)| **count == 1)
            .map(|(edge_id, _)| *edge_id)
            .collect::<Vec<_>>();
        let overused = use_counts
            .iter()
            .filter(|(_, count)| **count > 2)
            .map(|(edge_id, count)| (*edge_id, *count))
            .collect::<Vec<_>>();
        let mut open_matches = Vec::new();
        for (index, first_id) in open_ids.iter().enumerate() {
            for second_id in open_ids.iter().skip(index + 1) {
                if edge_lies_on(edge_by_id[first_id], edge_by_id[second_id], 2e-3).unwrap_or(false)
                    || edge_lies_on(edge_by_id[second_id], edge_by_id[first_id], 2e-3)
                        .unwrap_or(false)
                {
                    open_matches.push((*first_id, *second_id));
                }
            }
        }
        return Err(KernelRefusal::new(
            RefusalClass::NonIntegralGenus {
                shells: solid.shells.len() as u32,
                euler: vertex_count as i64 - edge_count as i64 + face_count as i64 - hole_count as i64,
            },
            KernelStage::Validate,
            format!(
            "boolean assembly: completion produced non-integral genus \
             (V={} E={} F={} H={} S={} one_use={} {open:?} \
              faces={open_faces:?} matches={open_matches:?} overused={overused:?})",
            vertex_count,
            edge_count,
            face_count,
            hole_count,
            solid.shells.len(),
            use_counts.values().filter(|count| **count == 1).count(),
        )));
    }
    solid.genus = if only_degenerate_open_edges {
        0
    } else {
        numerator / 2
    };
    let repair_debug =
        std::env::var("BREP_OS_DEBUG").is_ok_and(|value| !value.is_empty() && value != "0");
    if repair_debug {
        let pre_issues = solid.validate();
        eprintln!(
            "pre-merge validation issues: {:?}",
            pre_issues
                .iter()
                .map(|issue| &issue.message)
                .collect::<Vec<_>>(),
        );
    }
    let solid = merge_curve_continuation_edges(&solid, tolerance).or_refuse(KernelStage::Validate, "merge_curve_continuation_edges")?;
    let issues = solid.validate();
    if !issues.is_empty() {
        let edge_by_id = solid
            .edges
            .iter()
            .map(|edge| (edge.id, edge))
            .collect::<HashMap<_, _>>();
        let point_by_id = solid
            .vertices
            .iter()
            .map(|vertex| (vertex.id, vertex.point))
            .collect::<HashMap<_, _>>();
        let mut counts = HashMap::<u64, usize>::default();
        for coedge in solid
            .shells
            .iter()
            .flat_map(|shell| &shell.faces)
            .flat_map(|face| &face.loops)
            .flat_map(|loop_record| &loop_record.coedges)
        {
            *counts.entry(coedge.edge_id).or_default() += 1;
        }
        let open = counts
            .iter()
            .filter(|(_, count)| **count == 1)
            .filter_map(|(edge_id, _)| {
                let edge = edge_by_id.get(edge_id)?;
                Some((
                    *edge_id,
                    point_by_id[&edge.start_vertex_id],
                    point_by_id[&edge.end_vertex_id],
                ))
            })
            .collect::<Vec<_>>();
        return Err(KernelRefusal::new(
            RefusalClass::DegenerateArrangement {
                open_edges: open.len() as u32,
                issues: issues.len() as u32,
            },
            KernelStage::Validate,
            format!(
            "boolean assembly: completed shell has invalid topology: {issues:?}; open={open:?}"
        )));
    }
    let volume = solid_signed_volume(&solid).or_refuse(KernelStage::Validate, "solid_signed_volume")?;
    if volume <= tolerance.powi(3) {
        return Err(KernelRefusal::new(
            RefusalClass::NonPositiveVolume,
            KernelStage::Validate,
            "boolean assembly produced non-positive volume",
        ));
    }
    Ok(solid)
}

/// Exact repair fallback for a CLOSED boolean assembly (roadmap T4.4).
///
/// When `assemble_fragments_impl` is about to refuse a closed result because
/// one-use edges (open boundaries left by independently-fitted intersection
/// endpoints drifting beyond the assembler weld) push the Euler/genus count off
/// an integer, this offers the SAME heal chain the offset pipeline already runs
/// via `finalize_assembled_solid` — commit endpoints, collapse short one-use
/// slivers, sew coincident one-use pairs, repair pcurves — then re-checks the
/// result and continues the normal post-assembly path (curve-continuation
/// merge + triple-junction polish + validate + volume gate).
///
/// The healed solid is accepted ONLY when it is genuinely a valid closed
/// manifold: NO one-use edges remain, the recomputed genus is integral, and
/// `validate()` is empty (plus the normal post path succeeds with positive
/// volume). Otherwise it returns `None` so the caller falls through to its
/// original, unmodified diagnostic error — the repair never masks a real
/// intersector failure and never returns anything worse than the error it
/// replaces. Runs ONLY on the path that already errors, so every currently
/// succeeding boolean is byte-for-byte untouched.
pub(in crate::boolean) fn repair_open_assembly_via_heal_chain(
    provisional: BrepSolid,
    tolerance: f64,
) -> Option<BrepSolid> {
    let mut solid = provisional;
    // Shared heal sequence (identical to finalize_assembled_solid).
    apply_assembly_heal_chain(&mut solid, tolerance).ok()?;

    // A closed manifold has every edge used by exactly two coedges. Decline
    // the moment any one-use edge survives the heal (no masking of open shells).
    let mut use_counts = HashMap::<u64, usize>::default();
    for coedge in solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .flat_map(|face| &face.loops)
        .flat_map(|loop_record| &loop_record.coedges)
    {
        *use_counts.entry(coedge.edge_id).or_default() += 1;
    }
    if use_counts.values().any(|count| *count == 1) {
        return None;
    }

    // Recompute the Euler/genus exactly as the normal assembly path does
    // (all vertices; non-degenerate edges) so the accepted solid's genus is
    // computed identically to a normally-assembled one.
    let face_count: i64 = solid
        .shells
        .iter()
        .map(|shell| shell.faces.len() as i64)
        .sum();
    let hole_count: i64 = solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .map(|face| face.loops.len().saturating_sub(1) as i64)
        .sum();
    let edge_count = solid.edges.iter().filter(|edge| !edge.degenerate).count() as i64;
    let euler = solid.vertices.len() as i64 - edge_count + face_count - hole_count;
    let numerator = solid.shells.len() as i64 * 2 - euler;
    if numerator < 0 || numerator % 2 != 0 {
        return None;
    }
    if !solid.validate().is_empty() {
        return None;
    }
    solid.genus = numerator / 2;

    // Continue the normal post-assembly path with the healed solid. Any
    // failure here means the repair declines (fall through to the caller's
    // original error) rather than surfacing a different/worse failure.
    let mut solid = merge_curve_continuation_edges(&solid, tolerance).ok()?;
    polish_triple_junction_vertices(&mut solid, tolerance).ok()?;
    if !solid.validate().is_empty() {
        return None;
    }
    let volume = solid_signed_volume(&solid).ok()?;
    if volume <= tolerance.powi(3) {
        return None;
    }
    Some(solid)
}