quiddity 0.2.3

Manufacturing feature recognition from STEP B-rep models: holes, bosses, pockets, slots, steps, patterns and turned features, with measured sizes.
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
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
//! Drilled-hole recognition (`quiddity.holes`).
//!
//! Coaxial internal cylinders are grouped into stacks — drill, optional counterbore and
//! optional spotface become one hole, and a bore interrupted by a crossing hole is recombined.
//! Each stack's ends are classified from the faces beyond them, which decides the opening, the
//! bottom and the depth.
//!
//! Python's default inventory retries with `local_degradation` set when this family's strict
//! evidence refuses on a part whose solids are not all valid; three corpus parts take it
//! (`tests/local_degradation.rs`). `discover_locally_degraded` is that run's evidence path: a
//! hole proving no one valid solid is skipped, not refused. The aggregate's inventory takes the
//! retry where `strict_refusal_retries` says Python's would (`super::inventory_in`).

use std::collections::BTreeSet;

use serde::{Deserialize, Serialize};

use super::Context;
use super::countersinks::{self, CounterSink, HoleMouth, countersink_matches_hole};
use super::cylinders::{
    STACK_GAP_FRAC, Segment, axis_point_at, full_cylinders, line_key, merge_runs, segments,
    z_then_cross,
};
use super::evidence::{self, EvidenceError, Occurrence};
use super::placed::{Grid, Placed};
use super::policy;
use super::stacks::{End, classify_end, end_partners};
use crate::kernel::brep::Part;
use crate::kernel::geom::{self, COORD_FLOOR, Surface, V3};
use crate::kernel::py;

/// Two quantised diameters are one diameter within this proportion.
const SAME_DIAMETER_FRAC: f64 = 1e-4;
/// Smallest diameter the proportional test divides by.
const DIAMETER_FLOOR: f64 = 1e-9;
/// A spotface must be shallower than this fraction of its diameter and at most
/// SPOTFACE_MAX_DEPTH_MM deep. Both tests use the published, rounded measurements.
const SPOTFACE_MAX_RATIO: f64 = 0.2;
/// Absolute millimetre limit, like ADR 0008's other minimum-evidence thresholds (#118):
/// a spotface only cleans up a surface; deeper recesses are functional counterbores.
const SPOTFACE_MAX_DEPTH_MM: f64 = 1.0;

/// A cylindrical enlargement at a hole mouth.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct CounterBore {
    pub diameter: f64,
    pub depth: f64,
}

/// What closes a hole's deep end.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Bottom {
    Through,
    Flat,
    DrillPoint,
    /// The adjacent geometry matches none of the others.
    Unknown,
}

/// A drilled hole: `axis` points from the opening into the hole, `location` is the axis point at
/// the opening; `diameter`/`depth` describe the bore (the narrowest segment), with `depth` from
/// the top of the bore to the hole's deep end. `bottom` is `"through"`, `"flat"`,
/// `"drill_point"` or `"unknown"`. For interchangeable mouths, axis/location are a
/// presentation end gauge. Identical counterbore, spotface and outer-step readings at both
/// mouths retain both sets of defining faces; differing readings are omitted. Identical countersinks
/// at opposite mouths retain both seats as context, presenting the seat at the gauge end.
/// With no proved attachment,
/// the gauge is on the bore itself. No material drilling-entry end is asserted.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct HoleRecord {
    pub axis: V3,
    pub location: Placed<V3>,
    pub diameter: f64,
    pub depth: f64,
    pub bottom: Bottom,
    pub cbore: Option<CounterBore>,
    pub spotface: Option<CounterBore>,
    /// Counterbore-class enlargements outside `cbore`, outermost first. Each depth
    /// is that cylinder's own mouth-to-floor span, not cumulative from the hole mouth.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub outer_steps: Vec<CounterBore>,
    pub csink: Option<CounterSink>,
}

/// Options named as `recognise_holes`' keyword arguments.
#[derive(Clone, Copy, Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct HoleOptions {
    /// Compose recognised countersinks onto the holes they flare: Python's
    /// `csinks=recognise_countersinks(part)`, spelled `"csinks": "auto"`. Off by default, as
    /// there.
    #[serde(rename = "csinks", deserialize_with = "auto_flag")]
    pub with_countersinks: bool,
}

/// `null` → off, `"auto"` → on.
fn auto_flag<'de, D: serde::Deserializer<'de>>(d: D) -> Result<bool, D::Error> {
    match Option::<String>::deserialize(d)?.as_deref() {
        None => Ok(false),
        Some("auto") => Ok(true),
        Some(other) => Err(serde::de::Error::custom(format!(
            "csinks must be \"auto\" or null, not {other:?}"
        ))),
    }
}

internal_entry! {
/// The evidence path: holes with their defining cylinder faces and the faces that close them,
/// published only if each lies in one valid solid and no countersink seats two holes.
pub fn discover_verified(
    ctx: &Context<'_>,
    csinks: &[Occurrence<CounterSink>],
) -> Result<Vec<Occurrence<HoleRecord>>, EvidenceError> {
    let found = discover(ctx, csinks);
    seats_once(csinks, &found)?;
    evidence::verified(ctx.part, found)
}
}

internal_entry! {
/// The evidence path as Python's locally degraded run takes it (`_discover_holes` on a graph
/// with `local_degradation` set): a hole whose cylinder and closing faces prove no one valid
/// solid is skipped instead of refusing every hole, and the countersink check is made over the
/// holes kept. Python's degraded run also admits a solid with at most three faulted faces,
/// skipping only the holes that touch them ([`evidence::locally_valid_solid`]). The seat face of a
/// composed countersink is proved with the hole, so a hole whose seat lies on another solid is
/// skipped, where Python refuses it (no captured call or corpus part has one).
pub fn discover_locally_degraded(
    ctx: &Context<'_>,
    csinks: &[Occurrence<CounterSink>],
) -> Result<Vec<Occurrence<HoleRecord>>, EvidenceError> {
    let mut found = discover(ctx, csinks);
    found.retain(|h| evidence::locally_proved(ctx.part, h));
    seats_once(csinks, &found)?;
    Ok(found)
}
}

/// Whether the aggregate retries locally degraded on these holes (`_take_inventory`): Python's
/// strict hole evidence refuses for want of one valid solid (countersinks seating one hole each,
/// [`discover_verified`]'s order), and the part's solids are not all valid
/// (`BRepCheck_Analyzer(solid).IsValid()`, so faulted faces count; a part with no solid
/// retries too).
pub(crate) fn strict_refusal_retries(
    part: &Part,
    csinks: &[Occurrence<CounterSink>],
    found: &[Occurrence<HoleRecord>],
) -> bool {
    let unproved = |h: &Occurrence<HoleRecord>| {
        let faces: Vec<usize> = h.defining.iter().chain(&h.context).copied().collect();
        h.defining.is_empty() || evidence::common_valid_solid(part, &faces).is_none()
    };
    let all_valid = !part.solids.is_empty()
        && (0..part.solids.len()).all(|s| part.solid_is_geometrically_valid(s));
    seats_once(csinks, found).is_ok() && found.iter().any(unproved) && !all_valid
}

/// No countersink may seat two holes.
fn seats_once(
    csinks: &[Occurrence<CounterSink>],
    found: &[Occurrence<HoleRecord>],
) -> Result<(), EvidenceError> {
    for cs in csinks {
        let seats = found
            .iter()
            .filter(|h| cs.defining.iter().all(|f| h.context.contains(f)))
            .count();
        if seats > 1 {
            return Err(EvidenceError::SharedEvidence);
        }
    }
    Ok(())
}

internal_entry! {
/// `recognise_holes`.
pub fn recognise_holes(part: &Part, opts: &HoleOptions) -> Vec<HoleRecord> {
    let ctx = Context::new(part);
    let csinks = if opts.with_countersinks {
        countersinks::discover(&ctx)
    } else {
        Vec::new()
    };
    discover(&ctx, &csinks)
        .into_iter()
        .map(|o| o.record)
        .collect()
}
}

fn same_diameter(a: f64, b: f64) -> bool {
    (a - b).abs() <= SAME_DIAMETER_FRAC * a.abs().max(b.abs()).max(DIAMETER_FLOOR)
}

/// A cone or torus spans the gap between *a*'s high end and *b*'s low end — the shoulder chamfer
/// or fillet that makes them steps of one hole, reached directly or one adjacency hop away
/// (`_shared_transition`).
fn shared_transition(part: &Part, a: &Segment, b: &Segment) -> bool {
    let a_partners = end_partners(part, a, a.s_hi);
    let b_partners = end_partners(part, b, b.s_lo);
    for (own, other) in [(&a_partners, &b_partners), (&b_partners, &a_partners)] {
        for &t in own {
            if !matches!(
                part.faces[t].surface,
                Surface::Cone { .. } | Surface::Torus { .. }
            ) {
                continue;
            }
            if other.contains(&t) || part.neighbours(t).iter().any(|n| other.contains(n)) {
                return true;
            }
        }
    }
    false
}

/// Recombine coaxial stacks that are one hole (`_merge_stacks`): equal bores either side of an
/// internal cylinder whose finite cylinder contains the gap (a crossing bore), or different
/// diameters bridged by a shoulder chamfer or fillet.
fn merge_stacks(part: &Part, stacks: Vec<Vec<Segment>>) -> Vec<Vec<Segment>> {
    let original: Vec<Segment> = stacks.iter().flatten().cloned().collect();
    let owner_of = |solid: usize, face: usize| {
        original
            .iter()
            .position(|s| s.solid == solid && s.faces.contains(&face))
    };
    let sources = |seg: &Segment| -> BTreeSet<usize> {
        let mut found = BTreeSet::new();
        for &face in &seg.faces {
            let own = owner_of(seg.solid, face);
            for other in part.neighbours(face) {
                if let Some(index) = owner_of(seg.solid, other)
                    && Some(index) != own
                {
                    found.insert(index);
                }
            }
        }
        found
    };
    let shares_interruption = |a: &Segment, b: &Segment| -> bool {
        let endpoints = [axis_point_at(a, a.s_hi), axis_point_at(b, b.s_lo)];
        let (sa, sb) = (sources(a), sources(b));
        sa.intersection(&sb).any(|&index| {
            let cut = &original[index];
            let tolerance = policy::length_tol(cut.diameter, STACK_GAP_FRAC);
            let radius = cut.diameter / 2.0;
            let radial_tolerance = policy::length_tol(radius, 5e-6);
            endpoints.iter().all(|&point| {
                let axial = py::dot(&point, &cut.direction);
                let centre = axis_point_at(cut, axial);
                cut.s_lo - tolerance <= axial
                    && axial <= cut.s_hi + tolerance
                    && py::dist(&point, &centre) < radius + radial_tolerance
            })
        })
    };
    let mut by_line: Vec<(Vec<f64>, Vec<Vec<Segment>>)> = Vec::new();
    for stack in stacks {
        let key = line_key(&stack[0]);
        match by_line.iter_mut().find(|(k, _)| *k == key) {
            Some((_, group)) => group.push(stack),
            None => by_line.push((key, vec![stack])),
        }
    }
    let mut merged = Vec::new();
    for (_, mut line) in by_line {
        let low = |st: &Vec<Segment>| st.iter().map(|s| s.s_lo).fold(f64::INFINITY, f64::min);
        line.sort_by(|a, b| py::order(low(a), low(b)));
        let mut stacks = line.into_iter();
        let mut cur = stacks.next().expect("a non-empty group");
        for nxt in stacks {
            let ai = end_segment(&cur, |s| s.s_hi);
            let bi = end_segment(&nxt, |s| -s.s_lo);
            let (a, b) = (&cur[ai], &nxt[bi]);
            let closed = |end: End| matches!(end, End::Flat | End::DrillPoint);
            if same_diameter(a.diameter, b.diameter)
                && !closed(classify_end(part, a, a.s_hi, true).0)
                && !closed(classify_end(part, b, b.s_lo, false).0)
                && shares_interruption(a, b)
            {
                let mut joined = a.clone();
                joined.s_hi = b.s_hi;
                joined.faces.extend(&b.faces);
                let mut next: Vec<Segment> = cur
                    .iter()
                    .enumerate()
                    .filter(|(i, _)| *i != ai)
                    .map(|(_, s)| s.clone())
                    .collect();
                next.push(joined);
                next.extend(
                    nxt.iter()
                        .enumerate()
                        .filter(|(i, _)| *i != bi)
                        .map(|(_, s)| s.clone()),
                );
                cur = next;
            } else if b.s_lo - a.s_hi
                <= policy::length_tol(a.diameter.max(b.diameter), STACK_GAP_FRAC)
                    + (a.diameter - b.diameter).abs()
                && shared_transition(part, a, b)
            {
                cur.extend(nxt);
            } else {
                merged.push(std::mem::replace(&mut cur, nxt));
            }
        }
        merged.push(cur);
    }
    merged
}

/// Which end is the opening. Mouth diameter, then the complete inward axial profile decide
/// intrinsically. An unresolved side is an end gauge: retain the bore and any identical
/// mouth-step readings, without asserting a material drilling-entry end.
struct Drilling {
    from_hi: bool,
    opening: usize,
    opening_s: f64,
    bottom: Bottom,
    terminal: Vec<usize>,
    presentation: bool,
}

fn drilled_from(part: &Part, stack: &[Segment]) -> Drilling {
    let lo = end_segment(stack, |s| -s.s_lo);
    let hi = end_segment(stack, |s| s.s_hi);
    let (lo_state, lo_faces) = classify_end(part, &stack[lo], stack[lo].s_lo, false);
    let (hi_state, hi_faces) = classify_end(part, &stack[hi], stack[hi].s_hi, true);
    let (from_hi, presentation) = match (lo_state == End::Open, hi_state == End::Open) {
        (true, false) => (false, false),
        (false, true) => (true, false),
        _ if lo == hi => (true, true),
        _ => match mouth_order(stack, lo, hi) {
            Some(side) => (side, false),
            None => (true, true),
        },
    };
    let gauge_faces = if presentation {
        let mut faces: Vec<_> = lo_faces.iter().chain(&hi_faces).copied().collect();
        faces.sort_unstable();
        faces.dedup();
        faces
    } else {
        vec![]
    };
    let (opening, opening_s, bottom, faces) = if from_hi {
        (hi, stack[hi].s_hi, lo_state, lo_faces)
    } else {
        (lo, stack[lo].s_lo, hi_state, hi_faces)
    };
    let bottom = match bottom {
        _ if presentation && lo_state != hi_state => Bottom::Unknown,
        End::Open => Bottom::Through,
        End::Flat => Bottom::Flat,
        End::DrillPoint => Bottom::DrillPoint,
        End::Unknown => Bottom::Unknown,
    };
    // Only a closing face is evidence of the bottom.
    let terminal = if matches!(bottom, Bottom::Flat | Bottom::DrillPoint) {
        if presentation { gauge_faces } else { faces }
    } else {
        vec![]
    };
    Drilling {
        from_hi,
        opening,
        opening_s,
        bottom,
        terminal,
        presentation,
    }
}

/// Compare the two inward profiles using only distances and diameters. Source support signs,
/// face indices and the input order carry no machining-side evidence. Equal profiles leave
/// one-sided attribution unresolved, while retaining the bore and identical mouth steps.
fn mouth_order(stack: &[Segment], lo: usize, hi: usize) -> Option<bool> {
    let profile = |from_hi: bool| {
        let mut values: Vec<[f64; 3]> = stack
            .iter()
            .map(|s| {
                let (near, far) = if from_hi {
                    (stack[hi].s_hi - s.s_hi, stack[hi].s_hi - s.s_lo)
                } else {
                    (s.s_lo - stack[lo].s_lo, s.s_hi - stack[lo].s_lo)
                };
                [near, -s.diameter, -far]
            })
            .collect();
        values.sort_by(|a, b| py::tuple_order(a, b));
        values
    };
    let (low, high) = (profile(false), profile(true));
    for (a, b) in low.iter().zip(&high) {
        for (a, b) in a.iter().zip(b) {
            if (a - b).abs() > COORD_FLOOR {
                return Some(b < a);
            }
        }
    }
    None
}

/// The segment that forms one end of a stack: the one reaching furthest by *reach*, and of
/// those reaching it (within COORD_FLOOR) the widest, which is the mouth. Python takes the first
/// in stack order, which is face order, so turning the part over can pick a different segment.
fn end_segment(stack: &[Segment], reach: impl Fn(&Segment) -> f64) -> usize {
    let far = stack.iter().map(&reach).fold(f64::NEG_INFINITY, f64::max);
    let mut best: Option<usize> = None;
    for (i, s) in stack.iter().enumerate() {
        if far - reach(s) <= COORD_FLOOR && best.is_none_or(|b| s.diameter > stack[b].diameter) {
            best = Some(i);
        }
    }
    best.expect("a stack has segments")
}

/// The counterbore and spotface between the opening and the bore (`_near_side_steps`). Steps
/// must narrow inward (a wider one is a groove); lands of one diameter are unioned.
#[derive(Default)]
struct NearSideSteps {
    cbore: Option<CounterBore>,
    spotface: Option<CounterBore>,
    outer_steps: Vec<CounterBore>,
    faces: Vec<usize>,
}

fn near_side_steps(steps: &[Segment]) -> NearSideSteps {
    let mut spans: Vec<(f64, f64, f64, Vec<usize>)> = Vec::new(); // key, lo, hi, faces
    let mut min_d = f64::INFINITY;
    for step in steps {
        if step.diameter > min_d && !same_diameter(step.diameter, min_d) {
            continue;
        }
        min_d = step.diameter;
        let key = py::quantise(step.diameter, 4);
        match spans.iter_mut().find(|s| s.0 == key) {
            Some(s) => {
                s.1 = s.1.min(step.s_lo);
                s.2 = s.2.max(step.s_hi);
                s.3.extend(&step.faces);
            }
            None => spans.push((key, step.s_lo, step.s_hi, step.faces.clone())),
        }
    }
    let mut found = NearSideSteps::default();
    for (key, lo, hi, step_faces) in spans {
        let spec = CounterBore {
            diameter: key,
            depth: policy::round_measurement(hi - lo, 2),
        };
        if spec.depth <= SPOTFACE_MAX_DEPTH_MM && spec.depth < SPOTFACE_MAX_RATIO * spec.diameter {
            if found.spotface.is_none() {
                found.spotface = Some(spec);
                found.faces.extend(step_faces);
            }
        } else {
            found.outer_steps.push(spec);
            found.faces.extend(step_faces);
        }
    }
    // Spans are ordered from the opening towards the bore. Retain every counterbore
    // wall, reserving the singular `cbore` field for the innermost enlargement (#118).
    found.cbore = found.outer_steps.pop();
    found
}

/// Read a stack inward from either end, putting the widest level segment first.
fn ordered_from(stack: &[Segment], from_hi: bool) -> Vec<Segment> {
    let mut ordered = stack.to_vec();
    // Nearest the opening first: by the near end of each segment, which is s_hi drilling
    // down from the high end and s_lo drilling up from the low end. (Python sorts by s_hi
    // both ways, so overlapping segments order differently once the part is turned over.)
    // Segments starting level with each other put the wider first, as the mouth is outermost
    // (Python keeps them in face order). The sort is stable.
    ordered.sort_by(|a, b| {
        let near = if from_hi {
            py::order(a.s_hi, b.s_hi).reverse()
        } else {
            py::order(a.s_lo, b.s_lo)
        };
        let level = if from_hi {
            a.s_hi - b.s_hi
        } else {
            a.s_lo - b.s_lo
        };
        if level.abs() <= COORD_FLOOR {
            py::order(a.diameter, b.diameter).reverse()
        } else {
            near
        }
    });
    ordered
}

/// Depth from the top of the bore to the deep end (`_bore_depth`): the deep end is the whole
/// stack for a blind hole (a bottom relief groove counts) but only the bore for a through hole.
fn bore_depth(
    stack: &[Segment],
    bore: &Segment,
    through: bool,
    from_hi: bool,
) -> (f64, Vec<usize>) {
    let bore_segs: Vec<&Segment> = stack
        .iter()
        .filter(|s| same_diameter(s.diameter, bore.diameter))
        .collect();
    let deep: Vec<&Segment> = if through {
        bore_segs.clone()
    } else {
        stack.iter().collect()
    };
    let mut defining: Vec<&Segment> = bore_segs.clone();
    let depth = if from_hi {
        let deep_end = deep.iter().map(|s| s.s_lo).fold(f64::INFINITY, f64::min);
        if !through {
            defining.extend(deep.iter().filter(|s| s.s_lo == deep_end));
        }
        bore_segs
            .iter()
            .map(|s| s.s_hi)
            .fold(f64::NEG_INFINITY, f64::max)
            - deep_end
    } else {
        let deep_end = deep
            .iter()
            .map(|s| s.s_hi)
            .fold(f64::NEG_INFINITY, f64::max);
        if !through {
            defining.extend(deep.iter().filter(|s| s.s_hi == deep_end));
        }
        deep_end
            - bore_segs
                .iter()
                .map(|s| s.s_lo)
                .fold(f64::INFINITY, f64::min)
    };
    (
        depth,
        defining
            .iter()
            .flat_map(|s| s.faces.iter().copied())
            .collect(),
    )
}

/// Equal seats at the two ends of a through bore are an interchangeable pair, not
/// competing attributions at one mouth. Keep both as evidence and use the gauge end
/// only to present the one-seat field. Distinct seats at the same end still refuse.
fn symmetric_seats(matching: &[&Occurrence<CounterSink>], mouth: &HoleMouth) -> bool {
    if !mouth.through || matching.len() != 2 {
        return false;
    }
    let a = &matching[0].record;
    let b = &matching[1].record;
    let spec = |cs: &CounterSink| {
        [
            cs.major_diameter,
            cs.drill_diameter,
            cs.included_angle,
            cs.depth,
        ]
    };
    if spec(a)
        .iter()
        .zip(spec(b))
        .any(|(a, b)| (a - b).abs() > COORD_FLOOR)
    {
        return false;
    }
    let tol = policy::length_tol(mouth.diameter, 1e-4);
    let at_end = |cs: &CounterSink, far: bool| {
        let direction = if far { -1. } else { 1. };
        let end = geom::add(
            mouth.location,
            geom::scale(mouth.axis, if far { mouth.depth } else { 0. }),
        );
        let minor = geom::add(*cs.location, geom::scale(cs.axis, cs.depth));
        geom::unit(cs.axis).is_some_and(|axis| geom::dot(axis, mouth.axis) * direction > 1. - 1e-6)
            && geom::norm(geom::sub(minor, end)) <= tol
    };
    (at_end(a, false) && at_end(b, true)) || (at_end(b, false) && at_end(a, true))
}

internal_entry! {
/// Holes in stack order, each with its defining cylinder faces; *csinks* are composed onto the
/// holes they flare.
pub fn discover(
    ctx: &Context<'_>,
    csinks: &[Occurrence<CounterSink>],
) -> Vec<Occurrence<HoleRecord>> {
    let part = ctx.part;
    let cyls = ctx.cylinders();
    let (z, cross) = z_then_cross(cyls);
    let internal: Vec<_> = full_cylinders(&z)
        .into_iter()
        .chain(full_cylinders(&cross))
        .filter(|c| !c.external)
        .collect();
    if internal.is_empty() {
        return Vec::new();
    }
    let stacks = merge_stacks(part, merge_runs(&segments(&internal), line_key));
    let mut out = Vec::new();
    for stack in stacks {
        let d = stack[0].direction;
        let Drilling {
            from_hi,
            opening,
            opening_s,
            bottom,
            mut terminal,
            presentation,
        } = drilled_from(part, &stack);
        let ordered = ordered_from(&stack, from_hi);
        let bore_i = py::first_min(&ordered, |s| s.diameter);
        let bore = &ordered[bore_i];
        let NearSideSteps {
            mut cbore,
            mut spotface,
            mut outer_steps,
            faces: mut step_faces,
        } = near_side_steps(&ordered[..bore_i]);
        if presentation {
            let opposite = ordered_from(&stack, !from_hi);
            let other_bore_i = py::first_min(&opposite, |s| s.diameter);
            let other = near_side_steps(&opposite[..other_bore_i]);
            // An end gauge cannot choose between different published readings. Identical
            // readings retain their dimensions and claim both mouths, so reversal changes
            // only the representative end, never an attachment or its supporting evidence.
            if cbore == other.cbore && spotface == other.spotface && outer_steps == other.outer_steps {
                step_faces.extend(other.faces);
                step_faces.sort_unstable();
            } else {
                cbore = None;
                spotface = None;
                outer_steps.clear();
                step_faces.clear();
            }
        }
        let (depth, mut defining) = bore_depth(
            &stack,
            bore,
            bottom == Bottom::Through || presentation,
            from_hi,
        );
        defining.extend(step_faces);
        // A face counts once however many selections name it (Python resolves them to a set).
        let mut seen = std::collections::BTreeSet::new();
        defining.retain(|f| seen.insert(*f));
        let axis = if from_hi { geom::scale(d, -1.0) } else { d }.map(py::without_negative_zero);
        let location = if presentation && cbore.is_none() && spotface.is_none() {
            axis_point_at(bore, if from_hi { bore.s_hi } else { bore.s_lo })
        } else {
            axis_point_at(&stack[opening], opening_s)
        }
        ;
        let location = Placed::new(location, Grid::Figures(11));
        let mut record = HoleRecord {
            axis,
            location,
            diameter: bore.diameter,
            depth: policy::round_measurement(depth, 2),
            bottom,
            cbore,
            spotface,
            outer_steps,
            csink: None,
        };
        let mouth = HoleMouth {
            axis: record.axis,
            location: *record.location,
            diameter: record.diameter,
            depth: record.depth,
            through: record.bottom == Bottom::Through,
        };
        let mut matching: Vec<_> = csinks
            .iter()
            .filter(|cs| countersink_matches_hole(&cs.record, &mouth))
            .collect();
        let rank = |cs: &Occurrence<CounterSink>| {
            [
                cs.record.major_diameter,
                cs.record.depth,
                cs.record.included_angle,
            ]
        };
        matching.sort_by(|a, b| py::tuple_order(&rank(b), &rank(a)));
        let ambiguous_seat = matching.len() > 1
            && rank(matching[0])
                .iter()
                .zip(rank(matching[1]))
                .all(|(a, b)| (a - b).abs() <= COORD_FLOOR);
        // Prove the pair on the measured bore, before its depth is rounded for publication.
        let symmetric = presentation && symmetric_seats(&matching, &HoleMouth {
            location: *record.location.raw(),
            depth,
            ..mouth
        });
        if symmetric {
            // The representative end can change under motion, just as for a plain bore.
            // Both seats remain consulted evidence whichever representative is presented.
            for cs in &matching {
                terminal.extend(&cs.defining);
            }
            matching.sort_by(|a, b| {
                py::order(geom::dot(b.record.axis, axis), geom::dot(a.record.axis, axis))
            });
        }
        // Ambiguous attribution cannot remove the independently established bore.
        if let Some(cs) = matching.first().filter(|_| !ambiguous_seat || symmetric) {
            record.csink = Some(cs.record.clone());
            // The seat's face is consulted evidence: it must share the hole's solid.
            if !symmetric {
                terminal.extend(&cs.defining);
            }
        }
        out.push(Occurrence {
            record,
            defining,
            context: terminal,
        });
    }
    out
}
}

/// Extend a completed inventory's record claim with its already-consulted drill
/// point, after reconciliation. Mouth seats remain context, not tip faces.
pub(crate) fn claim_drill_point(
    part: &Part,
    record: &HoleRecord,
    faces: &mut Vec<usize>,
    constituent: &[usize],
) {
    if record.bottom != Bottom::DrillPoint {
        return;
    }
    let axis = record.axis;
    let tol = 1e-7 * record.diameter.max(record.depth).max(1.);
    let end = faces
        .iter()
        .flat_map(|&f| part.face_edges(f))
        .flat_map(|e| [part.edges[e].start, part.edges[e].end])
        .map(|p| geom::dot(p, axis))
        .fold(f64::NEG_INFINITY, f64::max);
    let tips: Vec<_> = constituent
        .iter()
        .copied()
        .filter(|&f| {
            let Some(apex) = part.faces[f].surface.cone_apex() else {
                return false;
            };
            let delta = geom::sub(apex, *record.location.raw());
            if geom::norm(geom::cross(delta, axis)) > tol || geom::dot(apex, axis) <= end + tol {
                return false;
            }
            faces.iter().any(|&g| {
                let shared = part.shared_edges(f, g);
                !shared.is_empty()
                    && shared.iter().all(|&e| {
                        [
                            part.edges[e].start,
                            part.edges[e].midpoint(),
                            part.edges[e].end,
                        ]
                        .iter()
                        .all(|&p| (geom::dot(p, axis) - end).abs() <= tol)
                    })
            })
        })
        .collect();
    faces.extend(tips);
    faces.sort_unstable();
    faces.dedup();
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::kernel::brep::{Edge, Face, Loop, Solid};
    use crate::kernel::geom::{Curve, Frame};
    use crate::kernel::step::IDENTITY;
    use std::f64::consts::TAU;

    #[test]
    fn spotface_depth_and_ratio_boundaries_use_published_measurements() {
        for (diameter, raw_depth, published_depth, is_spotface) in [
            (16.4, 1.0, 1.0, true),
            (16.4, 1.01, 1.01, false),
            (40., 0.5, 0.5, true),
            (4., 0.9, 0.9, false),
            (4., 0.8, 0.8, false), // Ratio equality is excluded.
            (4., 0.79, 0.79, true),
            (16.4, 1.004, 1.0, true),
            (16.4, 1.006, 1.01, false),
            (4., 0.796, 0.8, false), // Ratio also reads rounded depth.
            (4., 0.794, 0.79, true),
        ] {
            let step = Segment {
                solid: 0,
                diameter,
                axis: 2,
                axis_point: [0.; 3],
                direction: [0., 0., 1.],
                s_lo: 0.,
                s_hi: raw_depth,
                external: false,
                faces: vec![7],
            };
            let NearSideSteps {
                cbore,
                spotface,
                outer_steps,
                faces,
            } = near_side_steps(&[step]);
            assert!(outer_steps.is_empty());
            let spec = Some(CounterBore {
                diameter,
                depth: published_depth,
            });
            assert_eq!(spotface, if is_spotface { spec.clone() } else { None });
            assert_eq!(cbore, if is_spotface { None } else { spec });
            assert_eq!(faces, [7]);
        }
    }

    /// Independently built turned ring: diameter 10 through, diameter 16.4 x 1.4
    /// recess at each end, with diameter 11.2 collars rising 0.7 from each floor.
    /// Only these analytic dimensions are shared with the #118 example; no STEP is read.
    fn collared_recesses() -> Part {
        // Walk the material boundary, starting up the outside then into the top mouth.
        let rings = [
            (9.5, 0.),
            (9.5, 29.),
            (8.2, 29.),
            (8.2, 27.6),
            (5.6, 27.6),
            (5.6, 28.3),
            (5., 28.3),
            (5., 0.7),
            (5.6, 0.7),
            (5.6, 1.4),
            (8.2, 1.4),
            (8.2, 0.),
        ];
        turned_ring(&rings)
    }

    /// Close a hand-written radial/axial profile with analytic circle and seam edges.
    fn turned_ring(rings: &[(f64, f64)]) -> Part {
        let frame = |z| Frame {
            origin: [0., 0., z],
            x: [1., 0., 0.],
            y: [0., 1., 0.],
            z: [0., 0., 1.],
        };
        let mut edges: Vec<_> = rings
            .iter()
            .enumerate()
            .map(|(i, &(radius, z))| {
                let curve = Curve::Circle {
                    frame: frame(z),
                    radius,
                };
                Edge {
                    start: curve.value(0.),
                    end: curve.value(0.),
                    samples: (0..=32)
                        .map(|j| curve.value(TAU * j as f64 / 32.))
                        .collect(),
                    curve,
                    vertices: (i, i),
                    same_sense: true,
                }
            })
            .collect();
        let mut faces = Vec::new();
        for (i, &(radius, z)) in rings.iter().enumerate() {
            let j = (i + 1) % rings.len();
            let (next_radius, next_z) = rings[j];
            let (surface, reversed, boundaries) = if radius == next_radius {
                let (bottom, top) = if z < next_z { (i, j) } else { (j, i) };
                let seam = edges.len();
                let (start, end) = (edges[bottom].start, edges[top].start);
                edges.push(Edge {
                    curve: Curve::Line {
                        origin: start,
                        dir: [0., 0., 1.],
                    },
                    start,
                    end,
                    vertices: (bottom, top),
                    same_sense: true,
                    samples: vec![start, end],
                });
                (
                    Surface::Cylinder {
                        frame: frame(0.),
                        radius,
                    },
                    z > next_z,
                    vec![vec![
                        (bottom, z < next_z),
                        (seam, true),
                        (top, z > next_z),
                        (seam, false),
                    ]],
                )
            } else {
                assert_eq!(z, next_z);
                let reversed = radius < next_radius;
                (
                    Surface::Plane { frame: frame(z) },
                    reversed,
                    vec![vec![(i, true)], vec![(j, false)]],
                )
            };
            faces.push(Face {
                surface,
                reversed,
                solid: Some(0),
                pcurves: Vec::new(),
                loops: boundaries
                    .into_iter()
                    .map(|edges| Loop {
                        edges,
                        vertex: None,
                    })
                    .collect(),
            });
        }
        Part::new(
            faces,
            edges,
            vec![Solid {
                faces: (0..rings.len()).collect(),
            }],
        )
    }

    #[test]
    fn collared_recesses_are_counterbores_and_collars_stay_bosses() {
        let base = collared_recesses();
        assert!(base.solid_is_geometrically_valid(0));
        let (s, c) = 0.37_f64.sin_cos();
        for motion in [
            IDENTITY,
            [[c, 0., s, 123.456], [0., 1., 0., -78.9], [-s, 0., c, 41.3]],
        ] {
            let part = base.transformed(&motion).unwrap();
            let found = discover(&Context::new(&part), &[]);
            assert_eq!(found.len(), 1);
            let hole = &found[0].record;
            assert_eq!(
                (hole.diameter, hole.depth, hole.bottom),
                (10., 27.6, Bottom::Through)
            );
            assert_eq!(
                hole.cbore,
                Some(CounterBore {
                    diameter: 16.4,
                    depth: 1.4
                })
            );
            assert!(hole.spotface.is_none());
            let mut faces = found[0].defining.clone();
            faces.sort_unstable();
            assert_eq!(faces, [2, 6, 10], "bore and both recess walls");
            let recognition = crate::correspondence::recognise(&part);
            let doc = crate::recognition::document(&recognition, part.faces.len());
            let holes: Vec<_> = doc.records.iter().filter(|r| r.family == "holes").collect();
            assert_eq!(holes.len(), 1);
            assert_eq!(
                holes[0].parameters["cbore"],
                serde_json::json!({"diameter":16.4,"depth":1.4})
            );
            assert!(holes[0].parameters["spotface"].is_null());
            let collars: Vec<_> = doc
                .records
                .iter()
                .filter(|r| r.family == "bosses" && r.parameters["diameter"] == 11.2)
                .collect();
            assert_eq!(collars.len(), 2);
            for collar in collars {
                assert_eq!(collar.parameters["height"], 0.7);
            }
        }
    }

    #[test]
    fn two_step_holes_keep_the_inner_counterbore_and_publish_the_outer_classification() {
        let (s, c) = 0.37_f64.sin_cos();
        for outer_depth in [5., 0.5] {
            let floor = 30. - outer_depth;
            let base = turned_ring(&[
                (40., 0.),
                (40., 30.),
                (30., 30.),
                (30., floor),
                (9., floor),
                (9., floor - 6.),
                (5.05, floor - 6.),
                (5.05, 0.),
            ]);
            assert!(base.solid_is_geometrically_valid(0));
            for motion in [
                IDENTITY,
                [[c, 0., s, 123.456], [0., 1., 0., -78.9], [-s, 0., c, 41.3]],
            ] {
                let part = base.transformed(&motion).unwrap();
                let found = discover(&Context::new(&part), &[]);
                assert_eq!(found.len(), 1);
                let hole = &found[0].record;
                assert_eq!(
                    hole.cbore,
                    Some(CounterBore {
                        diameter: 18.,
                        depth: 6.
                    })
                );
                let outer = CounterBore {
                    diameter: 60.,
                    depth: outer_depth,
                };
                if outer_depth == 5. {
                    assert_eq!(hole.outer_steps, [outer]);
                    assert!(hole.spotface.is_none());
                } else {
                    assert!(hole.outer_steps.is_empty());
                    assert_eq!(hole.spotface, Some(outer));
                }
                let mut faces = found[0].defining.clone();
                faces.sort_unstable();
                assert_eq!(
                    faces,
                    [2, 4, 6],
                    "every cylindrical step remains defining evidence"
                );
                let recognition = crate::correspondence::recognise(&part);
                let doc = crate::recognition::document(&recognition, part.faces.len());
                let record = doc.records.iter().find(|r| r.id == "holes/0").unwrap();
                assert_eq!(
                    record.parameters["cbore"],
                    serde_json::json!({"diameter":18.,"depth":6.})
                );
                assert_eq!(record.faces, [2, 4, 6]);
                let fp = recognition
                    .fingerprints
                    .features
                    .iter()
                    .find(|f| f.id == "holes/0")
                    .unwrap();
                assert_eq!(fp.sizes["cbore.depth"], 6.);
                assert_eq!(fp.sizes["cbore.diameter"], 18.);
                if outer_depth == 5. {
                    assert_eq!(
                        record.parameters["outer_steps"],
                        serde_json::json!([{"diameter":60.,"depth":5.}])
                    );
                    assert_eq!(fp.sizes["outer_steps.count"], 1.);
                    assert_eq!(fp.sizes["outer_steps.0.diameter"], 60.);
                    assert_eq!(fp.sizes["outer_steps.0.depth"], 5.);
                } else {
                    assert!(record.parameters.get("outer_steps").is_none());
                    assert!(!fp.sizes.keys().any(|k| k.starts_with("outer_steps")));
                }
                let encoded = serde_json::to_value(hole).unwrap();
                let decoded: HoleRecord = serde_json::from_value(encoded.clone()).unwrap();
                assert_eq!(*hole, decoded);
                let mut legacy = encoded;
                legacy.as_object_mut().unwrap().remove("outer_steps");
                let decoded: HoleRecord = serde_json::from_value(legacy).unwrap();
                assert!(
                    decoded.outer_steps.is_empty(),
                    "old records default to no outer steps"
                );
                assert!(
                    serde_json::to_value(decoded)
                        .unwrap()
                        .get("outer_steps")
                        .is_none()
                );
            }
        }
    }

    #[test]
    fn multiple_outer_steps_keep_mouth_order_and_fingerprint_diameter_depth_pairs() {
        let part = turned_ring(&[
            (45., 0.),
            (45., 40.),
            (35., 40.),
            (35., 35.),
            (25., 35.),
            (25., 32.),
            (9., 32.),
            (9., 26.),
            (5., 26.),
            (5., 0.),
        ]);
        assert!(part.solid_is_geometrically_valid(0));
        let found = discover(&Context::new(&part), &[]);
        assert_eq!(found.len(), 1);
        assert_eq!(
            found[0].record.cbore,
            Some(CounterBore {
                diameter: 18.,
                depth: 6.
            })
        );
        assert_eq!(
            found[0].record.outer_steps,
            [
                CounterBore {
                    diameter: 70.,
                    depth: 5.
                },
                CounterBore {
                    diameter: 50.,
                    depth: 3.
                }
            ]
        );
        let recognition = crate::correspondence::recognise(&part);
        let fp = recognition
            .fingerprints
            .features
            .iter()
            .find(|f| f.id == "holes/0")
            .unwrap();
        assert_eq!(fp.sizes["outer_steps.count"], 2.);
        assert_eq!(fp.sizes["outer_steps.0.diameter"], 70.);
        assert_eq!(fp.sizes["outer_steps.0.depth"], 5.);
        assert_eq!(fp.sizes["outer_steps.1.diameter"], 50.);
        assert_eq!(fp.sizes["outer_steps.1.depth"], 3.);

        let mut holes: Vec<_> = [0., 100., 200.]
            .into_iter()
            .map(|x| {
                let mut hole = found[0].record.clone();
                hole.location = [x, 0., 40.].into();
                hole
            })
            .collect();
        let patterns = super::super::hole_patterns::recognise_hole_patterns(&holes);
        assert_eq!(
            patterns.len(),
            1,
            "equal outer steps can form a hole pattern"
        );
        let published = serde_json::to_value(&patterns[0]).unwrap();
        assert_eq!(
            published["holes"][0]["outer_steps"],
            serde_json::json!([
                {"diameter":70.,"depth":5.}, {"diameter":50.,"depth":3.}
            ])
        );
        holes[1].outer_steps[0].depth = 4.;
        assert!(
            super::super::hole_patterns::recognise_hole_patterns(&holes).is_empty(),
            "different outer-step specs must not be grouped just because the cbore matches"
        );
    }
}