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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
use crate::{KernelRefusal, KernelStage, OrRefuse};
use super::*;

/// Record one split parameter on `edge`, honouring the near-endpoint guard
/// and the dedupe band.  Two split points closer than the assembler's weld
/// tolerance can never survive as distinct vertices — they weld at assembly
/// and leave zero-length edges behind — so they are folded here instead.
///
/// Free-standing because the SOLID-level self-touch scan
/// ([`super::self_touch::self_touch_edge_splits`]) has no builder to record
/// into and must not diverge from the guard the builder applies.
pub(super) fn push_edge_split(
    parameters: &mut Vec<f64>,
    edge: &EdgeRecord,
    parameter: f64,
    model_tolerance: f64,
) -> Result<(), KernelRefusal> {
    let span = edge.t1 - edge.t0;
    let weld_distance = assembler_weld(model_tolerance);
    let tolerance = (KNOT_IDENTITY_TOL * span).max(parameter_tolerance(
        &edge.curve,
        parameter,
        weld_distance,
    )?);
    if parameter <= edge.t0 + tolerance || parameter >= edge.t1 - tolerance {
        return Ok(());
    }
    if !parameters
        .iter()
        .any(|existing| (*existing - parameter).abs() <= tolerance)
    {
        parameters.push(parameter);
    }
    Ok(())
}

pub(super) struct ImprintBuilder<'a> {
    pub(super) edges: HashMap<(u8, u64), &'a EdgeRecord>,
    pub(super) tolerance: f64,
    /// Combined bbox extent of the two operands ([`solid_scale`]); the merge
    /// bands scale with part SIZE via [`merge_scale`], not distance from origin.
    pub(super) scale: f64,
    pub(super) vertices: Vec<ImprintVertex>,
    pub(super) vertex_radii: HashMap<u64, f64>,
    pub(super) pieces: Vec<ImprintPieceRecord>,
    pub(super) by_face: HashMap<FaceKey, Vec<u64>>,
    pub(super) edge_splits: HashMap<(u8, u64), Vec<f64>>,
    pub(super) barrier_edges: HashSet<(u8, u64)>,
    /// Operand edges some section curve geometrically RODE (the overlap
    /// branch of `process_curve`): candidates for the PIECE-ENDPOINT
    /// post-pass (`exchange_piece_endpoint_junctions`). Deliberately a
    /// separate set from `barrier_edges` (same writer today) so the two
    /// mechanisms cannot silently couple if either gains another writer.
    pub(super) overlap_ridden_edges: HashSet<(u8, u64)>,
    pub(super) next_id: u64,
}

impl<'a> ImprintBuilder<'a> {
    fn vertex(&mut self, point: Vec3, merge_radius: f64) -> (u64, Vec3) {
        let base = self.tolerance * 10.0 * merge_scale(self.scale);
        for vertex in &self.vertices {
            let existing_radius = self.vertex_radii.get(&vertex.id).copied().unwrap_or(0.0);
            let distance = vertex.point.sub(point).length();
            if distance <= base.max(existing_radius).max(merge_radius) {
                self.vertex_radii
                    .insert(vertex.id, existing_radius.max(merge_radius));
                // The registry point is canonical.  Returning the caller's
                // near-duplicate here would give one topological vertex ID
                // several geometric positions on its incident curves.
                return (
                    vertex.id,
                    if distance <= base {
                        vertex.point
                    } else {
                        point
                    },
                );
            }
            // A broad fuzzy radius nominates a possible joint, but must not
            // collapse visibly distinct curve endpoints into one vertex.  The
            // caller's fuzzy-junction pass writes the same refined point into
            // both candidates when the joint is genuine.
        }
        let id = self.next_id;
        self.next_id += 1;
        self.vertices.push(ImprintVertex { id, point });
        self.vertex_radii.insert(id, merge_radius);
        (id, point)
    }

    pub(super) fn add_edge_split(
        &mut self,
        operand: u8,
        edge: &EdgeRecord,
        parameter: f64,
    ) -> Result<(), KernelRefusal> {
        let tolerance = self.tolerance;
        push_edge_split(
            self.edge_splits.entry((operand, edge.id)).or_default(),
            edge,
            parameter,
            tolerance,
        )
    }

    /// OVERLAP-JUNCTION EXCHANGE, PIECE-ENDPOINT POST-PASS (rotated
    /// equator-tangency, cardinal-sphere sub-configuration — problemInbox
    /// rot2 2026-08-14): the per-call exchange in `process_curve` can only
    /// imprint junctions IT sees onto edges IT sees — but the seam-crossing
    /// junction of a section riding an operand ring can be minted by a
    /// boundary-copy call whose `split_faces` exclude the ring, while the
    /// analytic call that DOES ride the ring carries that same junction at
    /// its closed curve's parameterization origin (param 0/1), where the
    /// endpoint guards suppress it (`perpendicular(cap_normal)` derives the
    /// analytic circle's origin, which lands exactly on the sphere seam for
    /// cardinal sphere axes). No single call ever holds both halves, so the
    /// ring stays one CLOSED record against two OPEN arc pieces — the
    /// one-use S=2 genus=1 signature. This post-pass runs once ALL calls
    /// have minted their pieces: every endpoint of an OPEN piece that RIDES
    /// an overlap-ridden edge, and that is a real junction (referenced by
    /// ≥ 2 piece-endpoint slots — a marched curve's deliberate slack end is
    /// degree 1 and must NOT imprint; see the `!refine` guard in the
    /// overlap branch), is imprinted onto that edge via `add_edge_split`
    /// (which keeps the near-endpoint and dedup guards — a junction already
    /// at the ring's own vertex is correctly rejected there).
    /// Escape hatches: `BREP_OVERLAP_PIECE_ENDPOINT_SPLIT=0` (this pass) or
    /// the family hatch `BREP_OVERLAP_JUNCTION_SPLIT=0` (whole exchange).
    pub(super) fn exchange_piece_endpoint_junctions(&mut self) -> Result<(), KernelRefusal> {
        if std::env::var("BREP_OVERLAP_JUNCTION_SPLIT").as_deref() == Ok("0")
            || std::env::var("BREP_OVERLAP_PIECE_ENDPOINT_SPLIT").as_deref() == Ok("0")
        {
            return Ok(());
        }
        if self.overlap_ridden_edges.is_empty() || self.pieces.is_empty() {
            return Ok(());
        }
        let overlap_limit = self.tolerance.max(1e-5);
        let mut endpoint_degree: HashMap<u64, usize> = HashMap::default();
        for piece in &self.pieces {
            *endpoint_degree.entry(piece.start_vertex_id).or_insert(0) += 1;
            *endpoint_degree.entry(piece.end_vertex_id).or_insert(0) += 1;
        }
        let mut ridden: Vec<(u8, u64)> = self.overlap_ridden_edges.iter().copied().collect();
        ridden.sort_unstable();
        let debug = std::env::var("BREP_DEBUG_BOOL").is_ok();
        let stats = std::env::var("BREP_ROT2_STATS").is_ok();
        // web_time::Instant (imported above), NOT std::time::Instant — std's clock
        // is unimplemented on wasm32-unknown-unknown and Instant::now() ABORTS there
        // ("RuntimeError: unreachable"). This pass runs on knife-edge tangency booleans
        // (overlap_ridden_edges non-empty), so a raw std::time call here traps the whole
        // cyl×sphere union in the browser while passing natively. See lib.rs boolean path.
        let t_start = Instant::now();
        let mut ride_tests = 0usize;
        let mut planned: Vec<(u8, &EdgeRecord, f64)> = Vec::new();
        for &(operand, edge_id) in &ridden {
            let Some(&edge) = self.edges.get(&(operand, edge_id)) else {
                continue;
            };
            if edge.degenerate {
                continue;
            }
            let low = edge.t0.min(edge.t1);
            let high = edge.t0.max(edge.t1);
            for piece in &self.pieces {
                // Open pieces only: a CLOSED piece riding a closed ring is
                // the shared-section RING reuse's territory; splitting the
                // ring at its origin would recreate the closed-vs-open
                // mismatch this pass exists to remove.
                if piece.start_vertex_id == piece.end_vertex_id || !(piece.t1 > piece.t0) {
                    continue;
                }
                // Ride test over the piece's LIVE window (records may carry
                // a window narrower than the curve's domain).
                ride_tests += 1;
                let mut on = 0usize;
                for fraction in [0.1, 0.3, 0.5, 0.7, 0.9] {
                    let t = piece.t0 + (piece.t1 - piece.t0) * fraction;
                    let point = piece.curve.evaluate(t).or_refuse(KernelStage::Intersect, "evaluate")?;
                    let projection = project_point_to_curve(&edge.curve, point).or_refuse(KernelStage::Intersect, "project_point_to_curve")?;
                    let nearest = edge.curve.evaluate(projection.u.clamp(low, high)).or_refuse(KernelStage::Intersect, "evaluate")?;
                    if nearest.sub(point).length() <= overlap_limit {
                        on += 1;
                    }
                }
                if on < 3 {
                    continue;
                }
                for (t, vertex_id) in [
                    (piece.t0, piece.start_vertex_id),
                    (piece.t1, piece.end_vertex_id),
                ] {
                    if endpoint_degree.get(&vertex_id).copied().unwrap_or(0) < 2 {
                        continue;
                    }
                    let point = piece.curve.evaluate(t).or_refuse(KernelStage::Intersect, "evaluate")?;
                    let projection = project_point_to_curve(&edge.curve, point).or_refuse(KernelStage::Intersect, "project_point_to_curve")?;
                    let nearest = edge.curve.evaluate(projection.u.clamp(low, high)).or_refuse(KernelStage::Intersect, "evaluate")?;
                    if nearest.sub(point).length() <= overlap_limit {
                        if debug {
                            eprintln!(
                                "piece-endpoint exchange: piece {} vertex {} \
                                 -> op={operand} edge={edge_id} u={:.6}",
                                piece.id, vertex_id, projection.u
                            );
                        }
                        planned.push((operand, edge, projection.u));
                    }
                }
            }
        }
        if stats {
            eprintln!(
                "ROT2_STATS: ridden_edges={} pieces={} ride_tests={} planned={} post_pass_ms={:.1}",
                ridden.len(),
                self.pieces.len(),
                ride_tests,
                planned.len(),
                t_start.elapsed().as_secs_f64() * 1000.0
            );
        }
        for (operand, edge, parameter) in planned {
            self.add_edge_split(operand, edge, parameter)?;
        }
        Ok(())
    }

    fn face_has_seam_at(&self, face: TaggedFace<'_>, point: Vec3) -> Result<bool, KernelRefusal> {
        let mut counts = HashMap::default();
        for coedge in face
            .face
            .loops
            .iter()
            .flat_map(|loop_record| &loop_record.coedges)
        {
            *counts.entry(coedge.edge_id).or_insert(0usize) += 1;
        }
        let tolerance = WELD_FLOOR * merge_scale(self.scale);
        for (edge_id, count) in counts {
            let edge = self.edges[&(face.operand, edge_id)];
            if count >= 2
                && !edge.degenerate
                && project_point_to_curve(&edge.curve, point).or_refuse(KernelStage::Intersect, "project_point_to_curve")?.distance <= tolerance
            {
                return Ok(true);
            }
        }
        Ok(false)
    }

    fn polish_endpoint(
        &self,
        point: Vec3,
        first: &NurbsSurface,
        second: &NurbsSurface,
    ) -> Result<Vec3, KernelRefusal> {
        let threshold = (self.tolerance * 100.0).max(1e-6);
        let original = project_point_to_surface(first, point).or_refuse(KernelStage::Intersect, "project_point_to_surface")?
            .distance
            .max(project_point_to_surface(second, point).or_refuse(KernelStage::Intersect, "project_point_to_surface")?.distance);
        if original <= threshold {
            return Ok(point);
        }
        let mut current = point;
        let mut best = point;
        let mut best_residual = original;
        for _ in 0..30 {
            let on_first = project_point_to_surface(first, current).or_refuse(KernelStage::Intersect, "project_point_to_surface")?.point;
            let on_second = project_point_to_surface(second, on_first).or_refuse(KernelStage::Intersect, "project_point_to_surface")?.point;
            let back = project_point_to_surface(first, on_second).or_refuse(KernelStage::Intersect, "project_point_to_surface")?.point;
            let candidate = back.add(on_second).scale(0.5);
            let residual = project_point_to_surface(first, candidate).or_refuse(KernelStage::Intersect, "project_point_to_surface")?
                .distance
                .max(project_point_to_surface(second, candidate).or_refuse(KernelStage::Intersect, "project_point_to_surface")?.distance);
            if residual < best_residual {
                best = candidate;
                best_residual = residual;
            }
            current = candidate;
            if residual <= (self.tolerance * 10.0).max(1e-7) {
                break;
            }
        }
        Ok(if best_residual <= threshold {
            best
        } else {
            point
        })
    }

    pub(super) fn process_curve(
        &mut self,
        curve: NurbsCurve,
        first: TaggedFace<'_>,
        second: TaggedFace<'_>,
        test_faces: &[TaggedFace<'_>],
        target_faces: &[TaggedFace<'_>],
        refine: bool,
    ) -> Result<(), KernelRefusal> {
        let mut split_parameters = Vec::new();
        let mut fuzzy_junctions: Vec<(Vec3, f64)> = Vec::new();
        // Operand edges the section curve geometrically RIDES (the overlap
        // branch below): junctions minted on the section must be exchanged
        // onto them once the full junction set is known — see the
        // OVERLAP-JUNCTION EXCHANGE after the split_parameters dedup.
        let mut overlapped_edges: Vec<(u8, &EdgeRecord)> = Vec::new();
        let [whole_start, whole_end] = curve.domain().or_refuse(KernelStage::Intersect, "domain")?;
        // Anything finer than the assembler's weld tolerance cannot survive
        // as distinct topology, so that is the meaningful noise floor for
        // overlap detection (marched curves wiggle a few 1e-6 around the
        // exact curve and every wiggle would otherwise count as a crossing).
        let overlap_limit = self.tolerance.max(1e-5);
        // Crossings this close to the curve's own endpoints are the
        // endpoint junction itself seen through the finders' spatial
        // tolerance: splitting there leaves a sliver piece whose surviving
        // half evaluates measurably off the true corner (§4.13 — the
        // parametric guard below is far tighter than the finders' 1e-5).
        let whole_start_point = curve.evaluate(whole_start).or_refuse(KernelStage::Intersect, "evaluate")?;
        let whole_end_point = curve.evaluate(whole_end).or_refuse(KernelStage::Intersect, "evaluate")?;
        let near_curve_end = |point: Vec3| {
            point.sub(whole_start_point).length() <= overlap_limit
                || point.sub(whole_end_point).length() <= overlap_limit
        };
        let split_faces = if refine {
            vec![first, second]
        } else {
            target_faces.to_vec()
        };
        for face in split_faces {
            let other = if face.key() == first.key() {
                second
            } else {
                first
            };
            for edge in face_edges(face, &self.edges)? {
                if edge.degenerate {
                    continue;
                }
                // Propagate failures: a swallowed error here silently drops
                // junction splits and corrupts topology far downstream.
                let pierces = if refine {
                    intersect_curve_surface(
                        &edge.curve,
                        &other.face.surface,
                        self.tolerance.max(1e-9),
                    )
                    .map_err(|error| {
                        format!("imprint pierce recovery on edge {}: {error}", edge.id)
                    }).or_refuse(KernelStage::Intersect, "csg.imprint.builder")?
                } else {
                    Vec::new()
                };
                // A (near-)coincident overlap must exchange ENDPOINTS only:
                // intersect_curves between overlapping curves reports
                // spurious subdivision hits along the whole shared span,
                // spraying micro edge-splits (glue unions copy each face's
                // edges onto the other and then hit-test them against their
                // own coincident twins).
                if curve_overlaps_edge(&curve, edge, overlap_limit)? {
                    self.barrier_edges.insert((face.operand, edge.id));
                    self.overlap_ridden_edges.insert((face.operand, edge.id));
                    overlapped_edges.push((face.operand, edge));
                    // Marched (refine) curves stop with deliberate slack past
                    // the trim boundary, so their endpoints are NOT junctions;
                    // exchanging them here plants off-by-a-band vertices on
                    // the overlapped edge. Exact-endpoint sources (cosurface
                    // boundary copies) still exchange.
                    if !refine {
                        for parameter in [whole_start, whole_end] {
                            let point = curve.evaluate(parameter).or_refuse(KernelStage::Intersect, "evaluate")?;
                            let projection = project_point_to_curve(&edge.curve, point).or_refuse(KernelStage::Intersect, "project_point_to_curve")?;
                            let nearest = edge.curve.evaluate(
                                projection
                                    .u
                                    .clamp(edge.t0.min(edge.t1), edge.t0.max(edge.t1)),
                            ).or_refuse(KernelStage::Intersect, "csg.imprint.builder")?;
                            if nearest.sub(point).length() <= overlap_limit {
                                self.add_edge_split(face.operand, edge, projection.u)?;
                            }
                        }
                    }
                    for parameter in [edge.t0, edge.t1] {
                        let point = edge.curve.evaluate(parameter).or_refuse(KernelStage::Intersect, "evaluate")?;
                        let projection = project_point_to_curve(&curve, point).or_refuse(KernelStage::Intersect, "project_point_to_curve")?;
                        if projection.distance <= overlap_limit
                            && projection.u > whole_start + 1e-7
                            && projection.u < whole_end - 1e-7
                            && !split_parameters
                                .iter()
                                .any(|value: &f64| (*value - projection.u).abs() <= 1e-6)
                        {
                            split_parameters.push(projection.u);
                        }
                    }
                    continue;
                }
                let curve_hits = intersect_curves(&curve, &edge.curve, 1e-5).map_err(|error| {
                    format!("imprint curve-edge crossing on edge {}: {error}", edge.id)
                }).or_refuse(KernelStage::Intersect, "csg.imprint.builder")?;
                for hit in curve_hits {
                    if hit.tangential && refine {
                        continue;
                    }
                    if hit.t < edge.t0 - 1e-9 || hit.t > edge.t1 + 1e-9 {
                        continue;
                    }
                    let mut curve_parameter = hit.s;
                    let mut edge_parameter = hit.t;
                    if refine {
                        let hit_point = curve.evaluate(curve_parameter).or_refuse(KernelStage::Intersect, "evaluate")?;
                        let search_radius = 4e-3 * (1.0 + hit_point.length());
                        let mut candidates = pierces
                            .iter()
                            .filter(|pierce| {
                                pierce.t >= edge.t0 - 1e-9 && pierce.t <= edge.t1 + 1e-9
                            })
                            .map(|pierce| (pierce, pierce.point.sub(hit_point).length()))
                            .collect::<Vec<_>>();
                        candidates.sort_by(|a, b| a.1.total_cmp(&b.1));
                        if let Some((best, best_distance)) = candidates.first() {
                            let second_distance = candidates
                                .get(1)
                                .map(|candidate| candidate.1)
                                .unwrap_or(f64::INFINITY);
                            if *best_distance <= search_radius
                                && *best_distance < 0.5 * second_distance
                            {
                                let projection = project_point_to_curve(&curve, best.point).or_refuse(KernelStage::Intersect, "project_point_to_curve")?;
                                if projection.distance <= 2e-3 * (1.0 + best.point.length()) {
                                    curve_parameter = projection.u;
                                    edge_parameter = best.t;
                                    fuzzy_junctions.push((
                                        best.point,
                                        (2.0 * projection.distance).max(2e-5).min(1.5e-3),
                                    ));
                                }
                            }
                        }
                    }
                    self.add_edge_split(face.operand, edge, edge_parameter)?;
                    let [start, end] = curve.domain().or_refuse(KernelStage::Intersect, "domain")?;
                    if curve_parameter > start + 1e-7
                        && curve_parameter < end - 1e-7
                        && !near_curve_end(curve.evaluate(curve_parameter).or_refuse(KernelStage::Intersect, "evaluate")?)
                        && !split_parameters
                            .iter()
                            .any(|value: &f64| (*value - curve_parameter).abs() <= 1e-6)
                    {
                        split_parameters.push(curve_parameter);
                    }
                }

                // A fitted SSI curve can miss a boundary curve by chord sag.
                // Exact edge/surface pierces recover those topological cuts.
                if refine {
                    for pierce in &pierces {
                        if pierce.t < edge.t0 - 1e-9 || pierce.t > edge.t1 + 1e-9 {
                            continue;
                        }
                        let scale = 1.0 + pierce.point.length();
                        let mut projection = project_point_to_curve(&curve, pierce.point).or_refuse(KernelStage::Intersect, "project_point_to_curve")?;
                        if projection.distance > 2e-3 * scale {
                            // `project_point_to_curve`'s per-knot-span seed grid
                            // (4..deg+2 samples/span) + Newton can settle in the
                            // WRONG basin on a near-closed section curve, mapping
                            // an on-curve pierce onto the far endpoint: 00000170's
                            // box-plane × revolution-band corner grazes the band's
                            // v=1 rim in a shallow <0.2mm arc and re-enters at the
                            // exact edge×plane pierce P0 (on the section at
                            // t≈0.978), yet the projector returns t=0 (the section
                            // start, 0.86mm away) so this gate drops the split, the
                            // tiny on-band corner arc is never minted, the mating
                            // box face's loop cannot close and the band's own corner
                            // fragment strands the shared rim/seam edges one-use
                            // (pool t288). Locating the parameter of a point KNOWN
                            // to lie on the curve (both pierce finders put it there)
                            // is deterministic INVERSION, not a containment verdict,
                            // so a projector-free dense-sample-then-bracket rescue is
                            // sound here — and it runs ONLY on pierces the projector
                            // already rejected, re-verified against the SAME gate, so
                            // no currently-accepted split changes. Escape hatch
                            // BREP_PIERCE_PARAM_RESCUE=0.
                            if std::env::var("BREP_PIERCE_PARAM_RESCUE").as_deref() == Ok("0") {
                                continue;
                            }
                            let [c_start, c_end] = curve.domain().or_refuse(KernelStage::Intersect, "domain")?;
                            const RESCUE_STATIONS: usize = 512;
                            let mut best_u = c_start;
                            let mut best_d2 = f64::INFINITY;
                            for index in 0..=RESCUE_STATIONS {
                                let u = c_start
                                    + (c_end - c_start) * (index as f64) / (RESCUE_STATIONS as f64);
                                let d2 =
                                    curve.evaluate(u).or_refuse(KernelStage::Intersect, "evaluate")?.sub(pierce.point).length_squared();
                                if d2 < best_d2 {
                                    best_d2 = d2;
                                    best_u = u;
                                }
                            }
                            let step = (c_end - c_start) / (RESCUE_STATIONS as f64);
                            let mut lo = (best_u - step).max(c_start);
                            let mut hi = (best_u + step).min(c_end);
                            for _ in 0..64 {
                                let m1 = lo + (hi - lo) / 3.0;
                                let m2 = hi - (hi - lo) / 3.0;
                                let d1 =
                                    curve.evaluate(m1).or_refuse(KernelStage::Intersect, "evaluate")?.sub(pierce.point).length_squared();
                                let d2 =
                                    curve.evaluate(m2).or_refuse(KernelStage::Intersect, "evaluate")?.sub(pierce.point).length_squared();
                                if d1 < d2 {
                                    hi = m2;
                                } else {
                                    lo = m1;
                                }
                            }
                            let rescued_u = 0.5 * (lo + hi);
                            let rescued_point = curve.evaluate(rescued_u).or_refuse(KernelStage::Intersect, "evaluate")?;
                            let rescued_distance = rescued_point.sub(pierce.point).length();
                            if rescued_distance > 2e-3 * scale {
                                continue;
                            }
                            projection = crate::CurveProjection {
                                u: rescued_u,
                                point: rescued_point,
                                distance: rescued_distance,
                            };
                        }
                        let mut ambiguous = false;
                        for other_pierce in &pierces {
                            if std::ptr::eq(pierce, other_pierce)
                                || other_pierce.point.sub(pierce.point).length() > 4e-3 * scale
                            {
                                continue;
                            }
                            let other_projection =
                                project_point_to_curve(&curve, other_pierce.point).or_refuse(KernelStage::Intersect, "project_point_to_curve")?;
                            if projection.distance >= 0.5 * other_projection.distance {
                                ambiguous = true;
                                break;
                            }
                        }
                        if ambiguous {
                            continue;
                        }
                        let curve_tangent = curve.derivatives(projection.u, 1).or_refuse(KernelStage::Intersect, "derivatives")?[1];
                        let edge_tangent = edge.curve.derivatives(pierce.t, 1).or_refuse(KernelStage::Intersect, "derivatives")?[1];
                        let parallel_scale = curve_tangent.length() * edge_tangent.length();
                        if parallel_scale > 1e-12
                            && curve_tangent.cross(edge_tangent).length() <= 1e-3 * parallel_scale
                        {
                            continue;
                        }
                        self.add_edge_split(face.operand, edge, pierce.t)?;
                        let [start, end] = curve.domain().or_refuse(KernelStage::Intersect, "domain")?;
                        let interior = projection.u > start + 1e-7
                            && projection.u < end - 1e-7
                            && !near_curve_end(curve.evaluate(projection.u).or_refuse(KernelStage::Intersect, "evaluate")?);
                        if interior
                            && !split_parameters
                                .iter()
                                .any(|value: &f64| (*value - projection.u).abs() <= 1e-6)
                        {
                            split_parameters.push(projection.u);
                        }
                        fuzzy_junctions.push((
                            pierce.point,
                            if interior {
                                (2.0 * projection.distance).max(2e-5).min(1.5e-3)
                            } else {
                                (1.2 * projection.distance).max(2e-5)
                            },
                        ));
                    }
                }
            }
        }
        // SECTION DOMAIN-EXIT SPLITS. A refine section on an ANALYTIC carrier
        // (plane×cylinder ellipse; the deg-(2,1) revolution "band" faces of
        // 00000170) can leave the carrier's PARAMETRIC domain in a non-closed
        // direction: the ellipse rises past the band's v=1 top, so a piece
        // straddling the boundary has an on-carrier midpoint but an endpoint
        // 12.5 mm off the surface — `build_pcurve_on_surface` then hard-errors
        // (pool trial 645). The section is on the carrier only within [0,1];
        // add a split exactly where it crosses the domain edge so the
        // on-carrier arc mints cleanly and the off-carrier tail is skipped by
        // the existing midpoint Outside gate. Detection is closed-form via
        // `AnalyticSurface::project` (whose clamped-projection distance is the
        // 3D gap once a coordinate leaves [0,1]) — NEVER the poisoned NURBS
        // projector — and every candidate is verified evaluate-vs-evaluate
        // before it is accepted. Planes self-clip through their real coplanar
        // trim edges, so they are excluded. A shallow graze that never leaves
        // the on-carrier band (max excursion below `out_tol`) is left
        // untouched: only a genuine deep exit, the class that errors today,
        // gets a split. Escape hatch `BREP_SECTION_DOMAIN_SPLIT=0`.
        if refine && std::env::var("BREP_SECTION_DOMAIN_SPLIT").as_deref() != Ok("0") {
            for face in [first, second] {
                let Some(analytic) = face.face.surface.analytic() else {
                    continue;
                };
                if matches!(analytic, crate::AnalyticSurface::Plane { .. }) {
                    continue;
                }
                // Local per-point tolerances: `in_tol` is the on-carrier band
                // (kept << build_pcurve's 1e-3·scale endpoint floor so a
                // clipped endpoint always passes); `out_tol` confirms a real
                // deep exit before any split is registered.
                let off_gap = |f: f64| -> Result<f64, KernelRefusal> {
                    let point = curve.evaluate(f).or_refuse(KernelStage::Intersect, "evaluate")?;
                    Ok(analytic
                        .project(&face.face.surface, point)
                        .map(|projection| projection.point.sub(point).length())
                        .unwrap_or(0.0))
                };
                let local_scale = |f: f64| -> Result<f64, KernelRefusal> {
                    Ok(1.0 + curve.evaluate(f).or_refuse(KernelStage::Intersect, "evaluate")?.length())
                };
                let is_inside = |f: f64| -> Result<bool, KernelRefusal> {
                    Ok(off_gap(f)? <= 1e-4 * local_scale(f)?)
                };
                const STATIONS: usize = 96;
                let mut previous_f = whole_start;
                let mut previous_inside = is_inside(previous_f)?;
                let mut previous_far = off_gap(previous_f)?;
                for index in 1..=STATIONS {
                    let f = whole_start + (whole_end - whole_start) * (index as f64) / (STATIONS as f64);
                    let inside = is_inside(f)?;
                    let far = off_gap(f)?;
                    // A genuine crossing: the two flanking stations disagree on
                    // membership AND at least one is DEEP off the carrier
                    // (>out_tol), so marched-section noise around 0 can never
                    // register a spurious split.
                    let deep = previous_far.max(far) > 1e-2 * local_scale(f)?;
                    if inside != previous_inside && deep {
                        // Bisect to the boundary, keeping the accepted parameter
                        // on the INSIDE flank so the retained arc ends on-carrier.
                        let (mut lo, mut hi) = (previous_f, f);
                        let lo_inside = previous_inside;
                        for _ in 0..48 {
                            let mid = 0.5 * (lo + hi);
                            if is_inside(mid)? == lo_inside {
                                lo = mid;
                            } else {
                                hi = mid;
                            }
                        }
                        let crossing = if lo_inside { lo } else { hi };
                        if crossing > whole_start + 1e-7
                            && crossing < whole_end - 1e-7
                            && !near_curve_end(curve.evaluate(crossing).or_refuse(KernelStage::Intersect, "evaluate")?)
                            && off_gap(crossing)? <= 1e-4 * local_scale(crossing)?
                            && !split_parameters
                                .iter()
                                .any(|value: &f64| (*value - crossing).abs() <= 1e-6)
                        {
                            split_parameters.push(crossing);
                        }
                    }
                    previous_f = f;
                    previous_inside = inside;
                    previous_far = far;
                }
            }
        }
        let [curve_start, curve_end] = curve.domain().or_refuse(KernelStage::Intersect, "domain")?;
        let split_tolerance = 2e-9f64.max((curve_end - curve_start) * KNOT_IDENTITY_TOL);
        split_parameters.sort_by(f64::total_cmp);
        split_parameters.dedup_by(|a, b| (*a - *b).abs() <= split_tolerance);
        // OVERLAP-JUNCTION EXCHANGE (rotated equator-tangency): when the
        // section curve RIDES an operand edge (the overlap branch above —
        // the tangency circle IS the cylinder's cap ring), every junction
        // minted ON the section (its split_parameters; e.g. the sphere-seam
        // crossing) is a junction OF that edge too. Without the matching
        // edge split the ring stays one CLOSED record while the section
        // arrives as open ARCS, and closed-ring vs open-arc records can
        // never endpoint-weld — both sides strand one-use (S=2 genus=1).
        // The overlap branch already exchanges the curve's ENDPOINTS; this
        // adds its interior junctions. Escape hatch:
        // BREP_OVERLAP_JUNCTION_SPLIT=0.
        if !overlapped_edges.is_empty()
            && std::env::var("BREP_OVERLAP_JUNCTION_SPLIT").as_deref() != Ok("0")
        {
            for parameter in &split_parameters {
                let point = curve.evaluate(*parameter).or_refuse(KernelStage::Intersect, "evaluate")?;
                for (operand, edge) in &overlapped_edges {
                    let projection = project_point_to_curve(&edge.curve, point).or_refuse(KernelStage::Intersect, "project_point_to_curve")?;
                    let nearest = edge.curve.evaluate(
                        projection
                            .u
                            .clamp(edge.t0.min(edge.t1), edge.t0.max(edge.t1)),
                    ).or_refuse(KernelStage::Intersect, "csg.imprint.builder")?;
                    if nearest.sub(point).length() <= overlap_limit {
                        self.add_edge_split(*operand, edge, projection.u)?;
                    }
                }
            }
        }
        if std::env::var("BREP_DEBUG_PROCESS_CURVE").is_ok() {
            eprintln!(
                "process_curve[{:?}/{:?}]: domain=[{curve_start:.6},{curve_end:.6}] splits={} at {:?}",
                first.key(),
                second.key(),
                split_parameters.len(),
                split_parameters
                    .iter()
                    .map(|value| (value * 1e4).round() / 1e4)
                    .collect::<Vec<_>>()
            );
        }
        let mut pieces = Vec::new();
        let mut rest = curve;
        for parameter in split_parameters {
            if parameter <= curve_start + split_tolerance
                || parameter >= curve_end - split_tolerance
            {
                continue;
            }
            let (left, right) = rest.split(parameter).or_refuse(KernelStage::Intersect, "split")?;
            pieces.push(left);
            rest = right;
        }
        pieces.push(rest);

        let debug_process = std::env::var("BREP_DEBUG_PROCESS_CURVE").is_ok();
        if debug_process {
            let spans: Vec<String> = pieces
                .iter()
                .map(|piece| {
                    piece
                        .domain()
                        .map(|[a, b]| format!("[{a:.4},{b:.4}]"))
                        .unwrap_or_else(|_| "[?]".into())
                })
                .collect();
            eprintln!(
                "process_curve[{:?}/{:?}]: {} piece(s): {}",
                first.key(),
                second.key(),
                pieces.len(),
                spans.join(" ")
            );
        }
        for piece in pieces {
            let [t0, t1] = piece.domain().or_refuse(KernelStage::Intersect, "domain")?;
            if debug_process {
                eprintln!(
                    "process_curve[{:?}/{:?}]: ENTER piece [{t0:.4},{t1:.4}] end_dist={:.3e} rough_len={:.3e}",
                    first.key(),
                    second.key(),
                    piece.evaluate(t0).or_refuse(KernelStage::Intersect, "evaluate")?.sub(piece.evaluate(t1).or_refuse(KernelStage::Intersect, "evaluate")?).length(),
                    curve_length_rough(&piece)?
                );
            }
            if piece.evaluate(t0).or_refuse(KernelStage::Intersect, "evaluate")?.sub(piece.evaluate(t1).or_refuse(KernelStage::Intersect, "evaluate")?).length() <= self.tolerance * 10.0
                && curve_length_rough(&piece)? <= self.tolerance * 100.0
            {
                if debug_process {
                    let p = piece.evaluate((t0 + t1) / 2.0).or_refuse(KernelStage::Intersect, "evaluate")?;
                    eprintln!(
                        "process_curve[{:?}/{:?}]: sub-seg SKIP short len={:.3e} mid=({:.5},{:.5},{:.5})",
                        first.key(),
                        second.key(),
                        curve_length_rough(&piece)?,
                        p.x, p.y, p.z
                    );
                }
                continue;
            }
            let midpoint = piece.evaluate((t0 + t1) / 2.0).or_refuse(KernelStage::Intersect, "evaluate")?;
            let mut statuses = HashMap::default();
            for face in test_faces {
                let projection = project_point_to_surface(&face.face.surface, midpoint).or_refuse(KernelStage::Intersect, "project_point_to_surface")?;
                statuses.insert(
                    face.key(),
                    parameter_point_in_face(
                        face.face,
                        Vec2 {
                            x: projection.u,
                            y: projection.v,
                        },
                        1e-7,
                    ).or_refuse(KernelStage::Intersect, "csg.imprint.builder")?,
                );
            }
            if statuses
                .values()
                .any(|status| *status == PolygonClass::Outside)
            {
                if debug_process {
                    eprintln!(
                        "process_curve[{:?}/{:?}]: sub-seg SKIP outside len={:.3e} mid=({:.5},{:.5},{:.5}) statuses={:?}",
                        first.key(),
                        second.key(),
                        curve_length_rough(&piece)?,
                        midpoint.x, midpoint.y, midpoint.z,
                        statuses
                    );
                }
                continue;
            }
            let mut attach = Vec::new();
            let mut invalid_boundary = false;
            for face in target_faces {
                if statuses.get(&face.key()) == Some(&PolygonClass::Boundary) {
                    if !self.face_has_seam_at(*face, midpoint)? {
                        invalid_boundary = true;
                        break;
                    }
                } else {
                    attach.push(*face);
                }
            }
            if invalid_boundary || attach.is_empty() {
                if debug_process {
                    eprintln!(
                        "process_curve[{:?}/{:?}]: sub-seg SKIP {} len={:.3e} mid=({:.5},{:.5},{:.5})",
                        first.key(),
                        second.key(),
                        if invalid_boundary { "invalid-boundary" } else { "attach-empty" },
                        curve_length_rough(&piece)?,
                        midpoint.x, midpoint.y, midpoint.z
                    );
                }
                continue;
            }
            let mut start = self.polish_endpoint(
                piece.evaluate(t0).or_refuse(KernelStage::Intersect, "evaluate")?,
                &first.face.surface,
                &second.face.surface,
            )?;
            let mut end = self.polish_endpoint(
                piece.evaluate(t1).or_refuse(KernelStage::Intersect, "evaluate")?,
                &first.face.surface,
                &second.face.surface,
            )?;
            let fuzzy_at = |point: Vec3| {
                fuzzy_junctions
                    .iter()
                    .filter_map(|(junction, radius)| {
                        let distance = junction.sub(point).length();
                        (distance <= radius.max(1e-5)).then_some((*junction, *radius, distance))
                    })
                    .min_by(|a, b| a.2.total_cmp(&b.2))
            };
            let start_fuzzy = fuzzy_at(start);
            let end_fuzzy = fuzzy_at(end);
            let start_radius = start_fuzzy.map(|value| value.1).unwrap_or(0.0);
            let end_radius = end_fuzzy.map(|value| value.1).unwrap_or(0.0);
            if let Some((junction, _, _)) = start_fuzzy {
                start = junction;
            }
            if let Some((junction, _, _)) = end_fuzzy {
                end = junction;
            }
            let (start_vertex_id, canonical_start) = self.vertex(start, start_radius);
            let (end_vertex_id, canonical_end) = self.vertex(end, end_radius);
            start = canonical_start;
            end = canonical_end;
            if start_vertex_id == end_vertex_id
                && curve_length_rough(&piece)?
                    < (4.0 * piece.evaluate(t0).or_refuse(KernelStage::Intersect, "evaluate")?.sub(piece.evaluate(t1).or_refuse(KernelStage::Intersect, "evaluate")?).length()).max(1e-3)
            {
                if debug_process {
                    eprintln!(
                        "process_curve[{:?}/{:?}]: sub-seg SKIP loopback v{start_vertex_id} len={:.3e} start=({:.5},{:.5},{:.5}) end=({:.5},{:.5},{:.5}) radii=({start_radius:.3e},{end_radius:.3e})",
                        first.key(),
                        second.key(),
                        curve_length_rough(&piece)?,
                        start.x, start.y, start.z,
                        end.x, end.y, end.z
                    );
                }
                continue;
            }
            let snapped = snap_curve_ends(&piece, start, end)?;
            let pcurves = attach
                .iter()
                .map(|face| {
                    Ok(FacePcurve {
                        operand: face.operand,
                        face_id: face.face.id,
                        // Seeded march (see build_pcurve_on_surface_marched): a
                        // section riding a compressed/self-overlapping carrier row
                        // must not alias interior samples onto a distant preimage
                        // sheet, which folds the pcurve across the trim and strands
                        // the shared section one-use. Fail-soft to the global build.
                        pcurve: build_pcurve_on_surface_marched(&face.face.surface, &snapped).or_refuse(KernelStage::Intersect, "build_pcurve_on_surface_marched")?,
                    })
                })
                .collect::<Result<Vec<_>, KernelRefusal>>()?;
            let id = self.next_id;
            self.next_id += 1;
            if debug_process {
                eprintln!(
                    "process_curve[{:?}/{:?}]: MINT piece {id} [{t0:.4},{t1:.4}] attach={:?}",
                    first.key(),
                    second.key(),
                    attach.iter().map(|face| face.key()).collect::<Vec<_>>()
                );
            }
            for face in &attach {
                self.by_face.entry(face.key()).or_default().push(id);
            }
            self.pieces.push(ImprintPieceRecord {
                id,
                curve: snapped,
                t0,
                t1,
                start_vertex_id,
                end_vertex_id,
                pcurves,
                support_faces: [first.key(), second.key()],
                shared_edge: None,
            });
        }
        Ok(())
    }
}