quiddity 0.5.1

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
//! Closed rectangular cavities with analytic wall-to-floor rounds (#228).
//! A mouth inner wire and its entire bounded region prove closure independently
//! of the wall-pair floor-height heuristic. No open-end or shared-floor inference.
use super::*;
use crate::features::recess_records::FloorFillet;
use crate::kernel::geom::Surface;

fn close(a: f64, b: f64) -> bool {
    (a - b).abs() <= COORD_FLOOR
}

#[derive(Default)]
pub(super) struct Analysis {
    pub proposals: Vec<Proposal<Pocket>>,
    pub unsupported: Vec<BTreeSet<usize>>,
}

enum Reading {
    Supported(Box<Proposal<Pocket>>),
    Unsupported(BTreeSet<usize>),
}
impl Reading {
    fn region(&self) -> &BTreeSet<usize> {
        match self {
            Self::Supported(p) => &p.constituent,
            Self::Unsupported(r) => r,
        }
    }
}

pub(super) fn analyse(scan: &Scan<'_, '_>) -> Analysis {
    let part = scan.ctx.part;
    let mut out = Vec::new();
    for opening in &scan.faces {
        let Some(depth) = opening.axis else { continue };
        for wire in inner_wires(part, opening.node) {
            if let Some(p) = prove(scan, opening, depth, &wire) {
                out.push(p);
            }
        }
    }
    let overlaps: Vec<_> = out
        .iter()
        .enumerate()
        .map(|(i, p)| {
            out.iter()
                .enumerate()
                .any(|(j, q)| i != j && !p.region().is_disjoint(q.region()))
        })
        .collect();
    let mut analysis = Analysis::default();
    for (p, overlap) in out.into_iter().zip(overlaps) {
        if !overlap {
            match p {
                Reading::Supported(p) => analysis.proposals.push(*p),
                Reading::Unsupported(r) => analysis.unsupported.push(r),
            }
        }
    }
    analysis
}

fn prove(scan: &Scan<'_, '_>, opening: &Face, depth: usize, wire: &[usize]) -> Option<Reading> {
    let part = scan.ctx.part;
    let g = &scan.graph;
    let seed = wire_seed(part, opening.node, wire);
    let sign = if opening.normal[depth] > 0. { 1 } else { -1 };
    let mouth = center(&opening.bb, depth);
    let planar: BTreeSet<_> = seed.iter().copied().filter(|&f| g.is_planar(f)).collect();
    if planar.len() != 4 || !matches!(seed.len(), 4 | 8) || wire.len() != seed.len() {
        return None;
    }
    // Reject holes and nonrectangular mouths before walking their whole region.
    let region = bounded_inner_region(g, opening.node, &seed);
    let members: Vec<_> = region.iter().copied().chain([opening.node]).collect();
    if common_valid_solid(part, &members) != Some(scan.solid) {
        return None;
    }
    let mut bounds = part.face_bounds(*seed.first()?);
    for &f in &seed {
        bounds.merge(&part.face_bounds(f));
    }
    let axes: Vec<_> = (0..3).filter(|&a| a != depth).collect();
    let sizes = axes
        .iter()
        .map(|&a| bounds.max[a] - bounds.min[a])
        .collect::<Vec<_>>();
    if close(sizes[0], sizes[1]) {
        return None;
    } // no arbitrary run on a square
    let (width, long) = if sizes[0] < sizes[1] {
        (axes[0], axes[1])
    } else {
        (axes[1], axes[0])
    };
    let mut sides = BTreeSet::new();
    for &f in &planar {
        let normal = graph::normal(part, f)?;
        let axis = super::super::recess_faces::dominant_axis(normal)?;
        if axis == depth {
            return None;
        }
        let b = part.face_bounds(f);
        let low = normal[axis] > 0.;
        let at = if low {
            bounds.min[axis]
        } else {
            bounds.max[axis]
        };
        if !close(b.min[axis], at)
            || !close(b.max[axis], at)
            || !close(if sign > 0 { b.max[depth] } else { b.min[depth] }, mouth)
            || !sides.insert((axis, low))
        {
            return None;
        }
    }
    // There must be exactly one intact planar floor, facing the opening. Every
    // other face below the mouth must be a proved floor transition or a mouth wall.
    let floors: Vec<_> = region
        .difference(&seed)
        .copied()
        .filter(|&f| g.is_planar(f))
        .collect();
    let [floor] = floors[..] else { return None };
    let fb = part.face_bounds(floor);
    let floor_at = center(&fb, depth);
    let floor_normal = graph::normal(part, floor)?;
    if floor_normal[depth] * f64::from(sign) < 1. - super::super::policy::AXIS_ZERO_COS
        || !close(fb.min[depth], fb.max[depth])
        || (mouth - floor_at) * f64::from(sign) <= COORD_FLOOR
        || part.faces[floor].loops.len() != 1
    {
        return None;
    }
    let transitions: BTreeSet<_> = region
        .difference(&seed)
        .copied()
        .filter(|&f| f != floor)
        .collect();
    if transitions.is_empty() {
        return None;
    } // sharp floors retain the existing path
    let floor_neighbours = g.neighbours(floor);
    let mut fillets = Vec::new();
    let mut unsupported = false;
    for &f in &transitions {
        let sphere = matches!(part.faces[f].surface, Surface::Sphere { .. });
        if !sphere && (!floor_neighbours.contains(&f) || g.arc(floor, f) != Some(Arc::Smooth)) {
            return None;
        }
        let walls: Vec<_> = g
            .neighbours(f)
            .iter()
            .copied()
            .filter(|w| seed.contains(w) && g.arc(f, *w) == Some(Arc::Smooth))
            .collect();
        let [wall] = walls[..] else { return None };
        if g.arc(wall, f) != Some(Arc::Smooth) {
            return None;
        }
        let radius = match transition_radius(scan, f, wall, depth, floor_at, sign) {
            Some(r) => r,
            None => {
                // STEP may encode a deep rolling corner as a nonanalytic
                // revolution. Its complete closed floor is still observed, but
                // its height is not a certified radius and is never published.
                if !matches!(
                    part.faces[f].surface,
                    Surface::Freeform {
                        kind: "REVOLUTION",
                        ..
                    }
                ) || !matches!(part.faces[wall].surface, Surface::Cylinder { .. })
                {
                    return None;
                }
                unsupported = true;
                let span = g.span(f, depth);
                span.1 - span.0
            }
        };
        if sphere {
            let Surface::Sphere { frame, .. } = part.faces[f].surface else {
                unreachable!()
            };
            let mut contact = frame.origin;
            contact[depth] = floor_at;
            if !graph::face_vertices(part, floor)
                .iter()
                .any(|&p| crate::kernel::geom::dist(p, contact) <= COORD_FLOOR)
            {
                return None;
            }
            let springs: Vec<_> = g
                .neighbours(f)
                .iter()
                .copied()
                .filter(|n| floor_neighbours.contains(n))
                .collect();
            if springs.len() != 2 || springs.iter().any(|&n| {
                g.arc(f, n) != Some(Arc::Smooth) || !matches!(part.faces[n].surface, Surface::Cylinder { radius: r, .. } if close(r, radius))
            }) { return None; }
        }
        if round_measurement(radius, 3) <= 0. {
            return None;
        }
        let b = part.face_bounds(f);
        let lo = floor_at.min(floor_at + f64::from(sign) * radius);
        let hi = floor_at.max(floor_at + f64::from(sign) * radius);
        if !close(b.min[depth], lo) || !close(b.max[depth], hi) {
            return None;
        }
        fillets.push(FloorFillet {
            face: f,
            wall_face: wall,
            radius,
        });
    }
    // Every floor edge is paired with either a sharp mouth wall or one transition;
    // holes, islands, open ends and extra lower faces are deliberately refused.
    let boundary = &part.faces[floor].loops[0];
    if boundary.edges.len() != floor_neighbours.len() {
        return None;
    }
    for &f in floor_neighbours.iter() {
        if transitions.contains(&f) {
            continue;
        }
        if !seed.contains(&f) || !g.is_planar(f) || g.arc(floor, f) != Some(Arc::Concave) {
            return None;
        }
    }
    for &f in &region {
        if g.neighbours(f)
            .iter()
            .any(|n| *n != opening.node && !region.contains(n))
        {
            return None;
        }
        let b = part.face_bounds(f);
        if b.min[depth] < floor_at.min(mouth) - COORD_FLOOR
            || b.max[depth] > floor_at.max(mouth) + COORD_FLOOR
        {
            return None;
        }
    }
    // A wall has exactly one bottom role, fully spanning its run. This excludes
    // partial-height shelves and split/ambiguous treatments.
    for &wall in &seed {
        let treatments: Vec<_> = fillets.iter().filter(|f| f.wall_face == wall).collect();
        let bottom = match treatments.as_slice() {
            [] if floor_neighbours.contains(&wall) => floor_at,
            [f] => {
                let b = part.face_bounds(f.face);
                let w = part.face_bounds(wall);
                for a in 0..3 {
                    if a != depth {
                        // Cylinders run along one footprint axis; corner tori
                        // share both footprint bounds with their vertical corner.
                        if let Surface::Cylinder { frame, .. } = part.faces[f.face].surface {
                            if frame.z[a].abs() > 1. - 1e-9
                                && (!close(b.min[a], w.min[a]) || !close(b.max[a], w.max[a]))
                            {
                                return None;
                            }
                        } else if !close(b.min[a], w.min[a]) || !close(b.max[a], w.max[a]) {
                            return None;
                        }
                    }
                }
                floor_at + f64::from(sign) * f.radius
            }
            _ => return None,
        };
        let b = part.face_bounds(wall);
        if !close(if sign > 0 { b.min[depth] } else { b.max[depth] }, bottom) {
            return None;
        }
    }
    let width_walls: Vec<_> = planar
        .iter()
        .copied()
        .filter(|&f| graph::normal(part, f).is_some_and(|n| n[width].abs() > 1. - 1e-9))
        .collect();
    let lo = width_walls
        .iter()
        .map(|&f| g.span(f, long).0)
        .fold(f64::NEG_INFINITY, f64::max);
    let hi = width_walls
        .iter()
        .map(|&f| g.span(f, long).1)
        .fold(f64::INFINITY, f64::min);
    let d_lo = seed
        .iter()
        .map(|&f| g.span(f, depth).0)
        .fold(f64::NEG_INFINITY, f64::max);
    let d_hi = seed
        .iter()
        .map(|&f| g.span(f, depth).1)
        .fold(f64::INFINITY, f64::min);
    let mut record = Pocket {
        width_axis: AXES[width],
        long_axis: AXES[long],
        width: round_measurement(bounds.max[width] - bounds.min[width], 2),
        length: round_measurement(hi - lo, 2),
        depth: round_measurement((mouth - floor_at).abs(), 2),
        w_center: ((bounds.min[width] + bounds.max[width]) / 2.).into(),
        lo: lo.into(),
        hi: hi.into(),
        d_lo: d_lo.into(),
        d_hi: d_hi.into(),
        open_sign: sign,
        edge_anchored: false,
        rectangular_boundary: false,
        floor_radius: None,
        floor_fillets: Vec::new(),
        body_key: Some(Vec::new().into()),
        end_radius: None,
        corner_radius: None,
    };
    if seed.len() == 8 {
        // Depth-axis r5 cylinders are vertical wall corners, not floor rounds.
        record = with_proved_corner_radius(record, &scan.cylinders);
        record.corner_radius?;
    }
    if !close(record.lo(), bounds.min[long]) || !close(record.hi(), bounds.max[long]) {
        return None;
    }
    if unsupported {
        return Some(Reading::Unsupported(region));
    }
    record.d_lo = floor_at.min(mouth).into();
    record.d_hi = floor_at.max(mouth).into();
    if floor_neighbours.iter().all(|f| transitions.contains(f))
        && fillets.iter().all(|f| close(f.radius, fillets[0].radius))
    {
        record.floor_radius = Some(fillets[0].radius);
    }
    record.floor_radius = record.floor_radius.map(|r| round_measurement(r, 3));
    for f in &mut fillets {
        f.radius = round_measurement(f.radius, 3);
    }
    record.floor_fillets = fillets;
    Some(Reading::Supported(Box::new(Proposal {
        record,
        planar,
        caps: vec![transitions],
        floors: BTreeSet::from([floor]),
        constituent: region,
    })))
}

fn transition_radius(
    scan: &Scan<'_, '_>,
    face: usize,
    wall: usize,
    depth: usize,
    floor: f64,
    sign: i32,
) -> Option<f64> {
    let part = scan.ctx.part;
    let aligned = |v: [f64; 3], a: usize| v[a].abs() > 1. - 1e-9;
    match (
        part.faces[face].surface.clone(),
        part.faces[wall].surface.clone(),
    ) {
        (Surface::Cylinder { frame, radius }, Surface::Plane { .. }) => {
            let normal = graph::normal(part, wall)?;
            let axis = super::super::recess_faces::dominant_axis(normal)?;
            if axis == depth {
                return None;
            }
            let run = 3 - axis - depth;
            let at = center(&part.face_bounds(wall), axis);
            (axis != depth
                && radius > 0.
                && aligned(frame.z, run)
                && part.frame_points_outward(face) == Some(false)
                && close(frame.origin[depth], floor + f64::from(sign) * radius)
                && close(frame.origin[axis], at + normal[axis] * radius))
            .then_some(radius)
        }
        (
            Surface::Torus {
                frame,
                major,
                minor,
            },
            Surface::Cylinder { frame: w, radius },
        ) => (minor > 0.
            && major > 0.
            && aligned(frame.z, depth)
            && aligned(w.z, depth)
            && part.frame_points_outward(wall) == Some(false)
            && close(major + minor, radius)
            && close(frame.origin[depth], floor + f64::from(sign) * minor)
            && (0..3)
                .filter(|&a| a != depth)
                .all(|a| close(frame.origin[a], w.origin[a])))
        .then_some(minor),
        (Surface::Sphere { frame, radius: r }, Surface::Cylinder { frame: w, radius }) => (r > 0.
            && close(r, radius)
            && aligned(w.z, depth)
            && part.frame_points_outward(face) == Some(false)
            && part.frame_points_outward(wall) == Some(false)
            && close(frame.origin[depth], floor + f64::from(sign) * r)
            && (0..3)
                .filter(|&a| a != depth)
                .all(|a| close(frame.origin[a], w.origin[a])))
        .then_some(r),
        _ => None,
    }
}