quiddity 0.5.5

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
//! Chamfer recognition (`quiddity.chamfers`).
//!
//! A prismatic chamfer is a small oblique planar face bevelling a convex edge between two
//! axis-aligned walls; a turned chamfer is a conical band meeting a coaxial external cylinder.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use super::Context;
use super::bevel::{classify_bevel, proved_bevel_planes, transverse_corner};
use super::cylinders::CylinderEvidence;
use super::evidence::{self, EvidenceError, Occurrence};
use super::placed::{Grid, Placed};
use super::policy::AXIS_ALIGNED_COS;
use super::turned::{self, cone_rims};
use crate::kernel::brep::Part;
use crate::kernel::classify::material_at;
use crate::kernel::geom::{self, COORD_FLOOR, Surface, V3, dominant_axis};
use crate::kernel::py;

#[derive(Clone, Debug, PartialEq, Serialize)]
#[non_exhaustive]
pub struct Chamfer {
    pub axis: char,
    pub leg1: f64,
    pub leg2: f64,
    pub angle: f64,
    pub at: Placed<V3>,
    pub turned: bool,
    /// The virtual sharp corner a planar chamfer replaces; `None` for turned chamfers
    /// or when reconstruction/empty-wedge proof fails.
    pub corner: Option<Placed<V3>>,
}

/// Options named as `recognise_chamfers`' keyword arguments.
#[derive(Clone, Copy, Debug, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct ChamferOptions {
    /// The minimum leg (an edge break below it is not reported); 0 by default.
    pub tol: Option<f64>,
    pub max_leg_frac: f64,
    /// `false` for a part already classified as rotational: only conical chamfers are reported.
    pub include_planar: bool,
}

impl Default for ChamferOptions {
    fn default() -> Self {
        ChamferOptions {
            tol: None,
            max_leg_frac: 0.45,
            include_planar: true,
        }
    }
}

internal_entry! {
/// The evidence path: each chamfer with its defining face, published only if every one is
/// owned by a valid solid.
pub fn discover_verified(
    ctx: &Context<'_>,
    opts: &ChamferOptions,
) -> Result<Vec<Occurrence<Chamfer>>, EvidenceError> {
    evidence::verified(ctx.part, discover(ctx, opts))
}
}

internal_entry! {
/// `recognise_chamfers`.
pub fn recognise_chamfers(part: &Part, opts: &ChamferOptions) -> Vec<Chamfer> {
    discover(&Context::new(part), opts)
        .into_iter()
        .map(|o| o.record)
        .collect()
}
}

/// Whether a measured leg is materially below the evidence floor (`_below_minimum_evidence`).
fn below_minimum(value: f64, minimum: f64) -> bool {
    value < minimum && (value - minimum).abs() > COORD_FLOOR
}

fn chamfer(
    axis: usize,
    leg_hi: f64,
    leg_lo: f64,
    at: V3,
    turned: bool,
    corner: Option<V3>,
) -> Chamfer {
    Chamfer {
        axis: ['x', 'y', 'z'][axis],
        leg1: py::round_to3(leg_hi),
        leg2: py::round_to3(leg_lo),
        angle: py::round_to(leg_lo.atan2(leg_hi).to_degrees(), 2),
        at: Placed::new(at, Grid::Decimals(3)),
        turned,
        corner: corner.map(|c| Placed::new(c, Grid::Decimals(3))),
    }
}

internal_entry! {
pub fn discover(ctx: &Context<'_>, opts: &ChamferOptions) -> Vec<Occurrence<Chamfer>> {
    let part = ctx.part;
    let tol = opts.tol.unwrap_or(0.0);
    let max_leg = opts.max_leg_frac * ctx.bounds().max_extent();
    let mut out = Vec::new();
    if opts.include_planar {
        out.extend((0..part.faces.len()).filter_map(|f| {
            planar(ctx, f, tol, max_leg).or_else(|| rolled_planar(ctx, f, tol, max_leg))
        }));
    }
    prune_polygon_sides(part, &mut out);
    let external = turned::external_cylinders(ctx);
    out.extend((0..part.faces.len()).filter_map(|f| turned(part, f, tol, max_leg, &external)));
    for o in &mut out {
        if let Some(corner) = o.record.corner {
            let r = &o.record;
            o.record.corner = super::bevel::proved_published_corner(ctx, &o.defining,
                match r.axis {'x'=>0,'y'=>1,_=>2}, *r.at.raw(), *corner.raw(),
                [r.leg1,r.leg2], r.angle);
        }
    }
    out.sort_by(|a, b| {
        a.record
            .axis
            .cmp(&b.record.axis)
            .then_with(|| py::tuple_order(&a.record.at[..], &b.record.at[..]))
    });
    // The edge breaks of conical rims are appended after the sorted records, so a saved rank of any
    // earlier chamfer does not move (#298).
    let rims: Vec<Occurrence<Chamfer>> = (0..part.faces.len())
        .filter(|f| !out.iter().any(|o| o.defining.contains(f)))
        .filter_map(|f| cone_body_rim(part, f, tol, max_leg))
        .collect();
    out.extend(rims);
    out
}
}

/// The planar faces sharing a straight edge parallel to `run` with `face`.
fn lateral_planes(part: &Part, face: usize, run: V3) -> Vec<usize> {
    part.neighbours(face)
        .into_iter()
        .filter(|&n| matches!(part.faces[n].surface, Surface::Plane { .. }))
        .filter(|&n| {
            part.shared_edges(face, n).into_iter().any(|e| {
                let edge = &part.edges[e];
                let span = geom::sub(edge.end, edge.start);
                matches!(edge.curve, geom::Curve::Line { .. })
                    && geom::norm(geom::cross(span, run)) <= 1e-6 * geom::norm(span)
            })
        })
        .collect()
}

/// Whether the chain of convexly joined run-parallel planes from `via` (beyond `from`) comes
/// back round to `start`: a closed, wholly convex section, as of a polygonal prism.
fn closes_convexly(part: &Part, run: V3, start: usize, from: usize, via: usize) -> bool {
    let convex = |a: usize, b: usize| part.arc(a, b) == Some(crate::kernel::brep::Arc::Convex);
    let (mut previous, mut current) = (from, via);
    for _ in 0..part.faces.len() {
        if current == start {
            return true;
        }
        if !convex(current, previous) {
            return false;
        }
        let onward: Vec<usize> = lateral_planes(part, current, run)
            .into_iter()
            .filter(|&n| n != previous)
            .collect();
        let [next] = onward[..] else { return false };
        (previous, current) = (current, next);
    }
    false
}

/// The share of a wall's width that a bevel's leg along it must take before the wall's far end
/// is asked to meet a perpendicular wall or another bevel.
const POLYGON_SIDE_SHARE: f64 = 1. / 3.;

/// A flat of a polygonal section is not a chamfer of its neighbours. Two walls and the bevel
/// between them are read as a chamfered corner only when each wall's far end also meets a
/// perpendicular wall, another bevel or a blend: a wall that runs on into an oblique plane that
/// is no bevel belongs to a polygonal section (a hexagonal bar, say), whose flats could equally
/// be called chamfers of one another (#301). Only a bevel whose leg takes a third or more of a
/// wall's width is held to this; a short break beside some other oblique face is still a chamfer.
/// A removed bevel can orphan its neighbours, so repeat until nothing changes.
fn prune_polygon_sides(part: &Part, out: &mut Vec<Occurrence<Chamfer>>) {
    loop {
        let bevels: Vec<usize> = out
            .iter()
            .filter(|o| !o.record.turned)
            .map(|o| o.defining[0])
            .collect();
        let before = out.len();
        out.retain(|o| {
            if o.record.turned {
                return true;
            }
            let bevel = o.defining[0];
            let mut run = [0.; 3];
            run[match o.record.axis {
                'x' => 0,
                'y' => 1,
                _ => 2,
            }] = 1.;
            let walls = lateral_planes(part, bevel, run)
                .into_iter()
                .filter(|&wall| part.arc(bevel, wall) == Some(crate::kernel::brep::Arc::Convex));
            walls.into_iter().all(|wall| {
                let Some(wall_normal) = super::planes::unit_plane_normal(part, wall) else {
                    return true;
                };
                // A short break does not make its wall a side of a polygon: only a bevel whose
                // leg takes a real share of the wall's own width asks for a square far end.
                let across = geom::cross(run, wall_normal);
                let width = |face: usize| {
                    let (lo, hi) = part.extent_along(&[face], across);
                    hi - lo
                };
                if width(bevel) <= POLYGON_SIDE_SHARE * width(wall) {
                    return true;
                }
                lateral_planes(part, wall, run).into_iter().all(|far| {
                    far == bevel
                        || bevels.contains(&far)
                        // A concave far end (an internal bevel) is another feature.
                        || part.arc(wall, far) != Some(crate::kernel::brep::Arc::Convex)
                        || super::planes::unit_plane_normal(part, far)
                            .is_none_or(|n| geom::dot(n, wall_normal).abs() <= 1e-6)
                        // An oblique plane only makes a polygon side if the section closes
                        // convexly through it; a groove, dovetail or step does not.
                        || !closes_convexly(part, run, bevel, wall, far)
                })
            })
        });
        if out.len() == before {
            return;
        }
    }
}

/// An oblique planar bevel on a convex edge between two axis-aligned walls.
fn planar(ctx: &Context<'_>, face: usize, tol: f64, max_leg: f64) -> Option<Occurrence<Chamfer>> {
    let part = ctx.part;
    let bevel = classify_bevel(part, face).ok()?;
    let centre = bevel.bounds.centre();
    if below_minimum(bevel.leg_lo, tol) || bevel.leg_hi > max_leg {
        return None;
    }
    let planes = proved_bevel_planes(ctx, face, centre, bevel.edge_axis)?;
    let corner = [0, 1, 2].map(|i| {
        if i == bevel.edge_axis {
            centre[i]
        } else {
            planes[&i]
        }
    });
    let at = part.face_centre(face)?;
    Some(Occurrence {
        record: chamfer(
            bevel.edge_axis,
            bevel.leg_hi,
            bevel.leg_lo,
            at,
            false,
            Some(corner),
        ),
        defining: vec![face],
        context: vec![],
    })
}

/// A principal-run bevel with rolled supporting walls. Keep the legacy record and its
/// principal run, measuring the legs on the actual supports instead of a coordinate box.
fn rolled_planar(
    ctx: &Context<'_>,
    face: usize,
    tol: f64,
    max_leg: f64,
) -> Option<Occurrence<Chamfer>> {
    use super::planes::unit_plane_normal;
    use crate::kernel::geom::SMOOTH_ARC_GAP;
    let part = ctx.part;
    let Surface::Plane { .. } = part.faces[face].surface else {
        return None;
    };
    // Preserve the principal-run / oblique-face contract of this axis-letter family.
    let axis = classify_bevel(part, face).ok()?.edge_axis;
    if !super::planes::linear_quad(part, face) {
        return None;
    }
    let normal = unit_plane_normal(part, face)?;
    let at = part.face_centre(face)?;
    let mut run = [0.; 3];
    run[axis] = 1.;
    let corner = transverse_corner(part, face, at, run)?;
    // A broad stock wall between two small bevel strips has the same local plane
    // arrangement. It is not a third chamfer: the supporting walls must dominate the strip.
    let area = part.face_mass(face)?[0];
    if corner
        .areas
        .iter()
        .any(|&support| !super::policy::clears_threshold(support, area))
    {
        return None;
    }
    let supports = corner.normals;
    let corner = corner.point;
    // A bevel faces out between its supports and replaces their outer virtual edge.
    if supports
        .iter()
        .any(|&n| geom::dot(normal, n) <= SMOOTH_ARC_GAP)
        || geom::dot(geom::sub(at, corner), normal) >= -COORD_FLOOR
    {
        return None;
    }
    // This path repairs rolled supports only; an axis-aligned ambiguity must still
    // be resolved by the existing corner proof, never bypassed here.
    if !supports
        .iter()
        .any(|n| n.iter().all(|c| c.abs() < AXIS_ALIGNED_COS))
    {
        return None;
    }
    let distance = geom::dot(geom::sub(at, corner), normal).abs();
    // The supports can differ from a right angle by the existing angular tolerance.
    // Measure on each actual support, rather than treating its partner's normal as a tangent.
    let legs = supports.map(|n| {
        let tangent = geom::cross(run, n);
        distance * geom::norm(tangent) / geom::dot(tangent, normal).abs()
    });
    let (leg_lo, leg_hi) = (legs[0].min(legs[1]), legs[0].max(legs[1]));
    if below_minimum(leg_lo, tol) || leg_hi > max_leg || leg_lo <= COORD_FLOOR {
        return None;
    }
    let near = geom::add(
        corner,
        geom::scale(geom::sub(at, corner), super::policy::INTERIOR_PROBE_FRAC),
    );
    if material_at(ctx.classifier(), near) {
        return None;
    }
    Some(Occurrence {
        record: chamfer(axis, leg_hi, leg_lo, at, false, Some(corner)),
        defining: vec![face],
        context: vec![],
    })
}

/// A conical band on turned stock, proved by a coaxial external cylinder and either a
/// transverse shoulder plane or a second band.
fn turned(
    part: &Part,
    face: usize,
    tol: f64,
    max_leg: f64,
    external: &BTreeMap<usize, &CylinderEvidence>,
) -> Option<Occurrence<Chamfer>> {
    let Surface::Cone { frame, .. } = part.faces[face].surface else {
        return None;
    };
    let ((minor_r, minor_c), (major_r, major_c), _) = cone_rims(part, face)?;
    let d = frame.z;
    let edge_axis = dominant_axis(d.map(f64::abs));
    if d[edge_axis].abs() < AXIS_ALIGNED_COS {
        return None;
    }
    let radial = major_r - minor_r;
    let axial = geom::dot(geom::sub(major_c, minor_c), d).abs();
    let (leg_hi, leg_lo) = (axial.max(radial), axial.min(radial));
    if below_minimum(leg_lo, tol) || leg_hi > max_leg {
        return None;
    }
    let neighbours = part.neighbours(face);
    let coaxial = turned::coaxial_cylinders(&neighbours, external, frame.origin, d);
    if coaxial.is_empty() {
        return None;
    }
    let transverse = !turned::transverse_planes(part, &neighbours, d).is_empty();
    if !transverse && !turned::bridges_two_bands(&coaxial) {
        return None; // a taper meeting one band, not a bevelled edge
    }
    let at = part.face_centre(face)?;
    Some(Occurrence {
        record: chamfer(edge_axis, leg_hi, leg_lo, at, true, None),
        defining: vec![face],
        context: vec![],
    })
}

/// The legs of a conical rim chamfer must agree to within 1.5 published cells.
const CONE_RIM_EQUAL_LEGS: f64 = 0.0015;

/// An edge break on the rim of a conical body: a narrow coaxial cone between a transverse end
/// plane and the body's cone, both joined convexly (a chamfer of a cone's base or top). The
/// legs are measured along the end plane and along the body's cone, from the virtual sharp
/// rim where the two meet, as for a chamfer between any two faces (#298). The band must be
/// shorter than the body cone it breaks into, so the body itself is never read as a rim.
fn cone_body_rim(part: &Part, face: usize, tol: f64, max_leg: f64) -> Option<Occurrence<Chamfer>> {
    let Surface::Cone {
        frame: band,
        semi_angle: band_angle,
        ..
    } = part.faces[face].surface
    else {
        return None;
    };
    let d = band.z;
    let edge_axis = dominant_axis(d.map(f64::abs));
    if d[edge_axis].abs() < AXIS_ALIGNED_COS {
        return None;
    }
    let axial = |p: V3| geom::dot(geom::sub(p, band.origin), d);
    // Radius and axial position of the circle two faces share.
    let rim_with = |other: usize| {
        part.shared_edges(face, other)
            .into_iter()
            .find_map(|e| match part.edges[e].curve {
                geom::Curve::Circle { frame, radius } => Some((radius, axial(frame.origin))),
                _ => None,
            })
    };
    let slope = |frame: crate::kernel::geom::Frame, angle: f64| angle.tan() * geom::dot(frame.z, d);
    let mut plane = None;
    let mut body = None;
    for n in part.neighbours(face) {
        if part.arc(face, n) != Some(crate::kernel::brep::Arc::Convex) {
            continue;
        }
        let surface = &part.faces[n].surface;
        if let Surface::Plane { frame } = surface
            && geom::dot(frame.z, d).abs() > AXIS_ALIGNED_COS
        {
            if plane.replace((n, *frame)).is_some() {
                return None;
            }
        } else if let Surface::Cone {
            frame,
            radius,
            semi_angle,
        } = surface
            && geom::dot(frame.z, d).abs() > 1. - 1e-9
            && geom::norm(geom::cross(geom::sub(frame.origin, band.origin), d)) <= COORD_FLOOR
            && (slope(*frame, *semi_angle) - slope(band, band_angle)).abs() > 1e-6
            && body.replace((n, *frame, *radius, *semi_angle)).is_some()
        {
            return None;
        }
    }
    let ((plane_face, plane_frame), (body_face, body_frame, body_radius, body_angle)) =
        (plane?, body?);
    let (r_plane, z_plane) = rim_with(plane_face)?;
    let (r_body, z_body) = rim_with(body_face)?;
    // The body's own radius where the end plane cuts it: the virtual sharp rim.
    let r_corner = body_radius
        + geom::dot(
            geom::sub(plane_frame.origin, body_frame.origin),
            body_frame.z,
        ) * body_angle.tan();
    if r_plane <= 0. || r_body <= 0. || r_corner <= r_plane {
        return None;
    }
    let leg_plane = r_corner - r_plane;
    let leg_body = (r_corner - r_body).hypot(z_plane - z_body);
    let slant = match turned::cone_rims(part, body_face) {
        Some(((r0, c0), (r1, c1), _)) => (r1 - r0).hypot(geom::dot(geom::sub(c1, c0), d)),
        None => r_body / body_angle.sin().abs(),
    };
    let (leg_hi, leg_lo) = (leg_plane.max(leg_body), leg_plane.min(leg_body));
    // Only a C x 45 degree edge break: the two legs equal within the published precision (a
    // 0.001 mm cell), and the break strictly shorter than the body band left, so a tie between
    // two equal bands reads neither as the rim. A cone at any other angle, or with two
    // distances, cannot be told from a two-cone profile (a flare, a lead taper) by proportion,
    // so it is left alone.
    if below_minimum(leg_lo, tol)
        || leg_hi > max_leg
        || leg_hi - leg_lo > CONE_RIM_EQUAL_LEGS
        || leg_body >= slant * (1. - 1e-9)
    {
        return None;
    }
    let at = part.face_centre(face)?;
    Some(Occurrence {
        record: chamfer(edge_axis, leg_hi, leg_lo, at, true, None),
        defining: vec![face],
        context: vec![],
    })
}