quiddity 0.5.2

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
//! Polygonal principal walls of open oblique steps. Kept in a separate family so that
//! adding these occurrences cannot renumber existing oblique-step Spec references.

use serde::Serialize;

use super::Context;
use super::body::BodyKey;
use super::evidence::{self, EvidenceError, Occurrence};
use super::oblique_through_steps::{
    linear_outer_quad, outer_vertices, principal_plane, seam_endpoints,
};
use super::placed::{Grid, Placed};
use super::planes::{coordinates, normalized, unit_plane_normal};
use super::policy::length_tol;
use crate::kernel::brep::{Arc, Part};
use crate::kernel::geom::{
    Bounds, COORD_FLOOR, Curve, SMOOTH_ARC_GAP, Surface, V3, add, cross, dot, norm, scale, sub,
};
use crate::kernel::py;

#[derive(Clone, Debug, PartialEq, Serialize)]
#[non_exhaustive]
pub struct PolygonalObliqueThroughStep {
    pub run: V3,
    pub length: f64,
    pub at: Placed<V3>,
    pub depth_direction: V3,
    /// Height above the principal wall, measured to the complete convex lip.
    pub depth: f64,
    pub across_direction: V3,
    /// Unoriented unit axis normal to the two stock-envelope run terminals.
    pub through_axis: V3,
    /// Acute angle between the seam and through_axis, in degrees.
    pub slant_degrees: f64,
    /// Principal floor's corners in (run, across) coordinates from the seam start.
    pub floor_outline: Vec<[f64; 2]>,
    pub body_key: Option<BodyKey>,
}

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

fn canonical_outline(points: &[[f64; 2]]) -> Vec<[f64; 2]> {
    let points: Vec<_> = points
        .iter()
        .map(|p| p.map(py::without_negative_zero))
        .collect();
    let mut best = points.clone();
    for reverse in [false, true] {
        let mut path = points.clone();
        if reverse {
            path.reverse();
        }
        for offset in 0..path.len() {
            let candidate: Vec<_> = path
                .iter()
                .cycle()
                .skip(offset)
                .take(path.len())
                .copied()
                .collect();
            let flat = |p: &Vec<[f64; 2]>| p.iter().flatten().copied().collect::<Vec<_>>();
            if py::tuple_order(&flat(&candidate), &flat(&best)).is_lt() {
                best = candidate;
            }
        }
    }
    best
}

/// Non-adjacent segments may neither cross nor touch. Reject uncertain separation
/// within the kernel's length floor, including repeated corners and overlapping edges.
fn simple_outline(points: &[[f64; 2]]) -> bool {
    let n = points.len();
    let side = |a: [f64; 2], b: [f64; 2], p: [f64; 2]| {
        let cross = (b[0] - a[0]) * (p[1] - a[1]) - (b[1] - a[1]) * (p[0] - a[0]);
        let tolerance = COORD_FLOOR * (b[0] - a[0]).hypot(b[1] - a[1]);
        if cross > tolerance {
            1
        } else if cross < -tolerance {
            -1
        } else {
            0
        }
    };
    for i in 0..n {
        let (a, b) = (points[i], points[(i + 1) % n]);
        if (b[0] - a[0]).hypot(b[1] - a[1]) <= COORD_FLOOR {
            return false;
        }
        if side(a, b, points[(i + 2) % n]) == 0 {
            return false;
        }
        for j in i + 1..n {
            let (c, d) = (points[j], points[(j + 1) % n]);
            if (a[0] - c[0]).hypot(a[1] - c[1]) <= COORD_FLOOR {
                return false;
            }
            if j == i + 1 || (i == 0 && j == n - 1) {
                continue;
            }
            let boxes_overlap = (0..2).all(|axis| {
                a[axis].min(b[axis]) <= c[axis].max(d[axis]) + COORD_FLOOR
                    && c[axis].min(d[axis]) <= a[axis].max(b[axis]) + COORD_FLOOR
            });
            if boxes_overlap
                && side(a, b, c) * side(a, b, d) <= 0
                && side(c, d, a) * side(c, d, b) <= 0
            {
                return false;
            }
        }
    }
    true
}

/// Preserve every genuine corner; only remove vertices strictly inside a straight edge.
fn polygon(part: &Part, face: usize) -> Option<Vec<V3>> {
    if part.faces[face].loops.len() != 1
        || part
            .outer_edges(face)
            .iter()
            .any(|&e| !matches!(part.edges[e].curve, Curve::Line { .. }))
    {
        return None;
    }
    let vertices = outer_vertices(part, face);
    let n = vertices.len();
    // The legacy vertex walk deduplicates IDs. A repeated corner here is an
    // ambiguous/self-touching boundary, rather than a vertex to silently discard.
    if n < 5 || n != part.faces[face].loops[part.outer_loop(face)?].edges.len() {
        return None;
    }
    let corners: Vec<_> = (0..n)
        .filter_map(|i| {
            let (a, p, b) = (
                vertices[(i + n - 1) % n],
                vertices[i],
                vertices[(i + 1) % n],
            );
            let chord = sub(b, a);
            (!(dot(sub(p, a), sub(b, p)) >= 0.0
                && norm(cross(sub(p, a), chord)) <= COORD_FLOOR * norm(chord)))
            .then_some(p)
        })
        .collect();
    (corners.len() > 4).then_some(corners)
}

/// Exactly one exterior support owns each entire non-seam edge. Nonmanifold,
/// concave, or competing boundary supports do not establish an open boundary.
/// A curved support may trim a straight polygon edge; its normal is not a datum.
/// The measured far lip and both run terminals are separately proved planar.
fn boundary_supports(
    part: &Part,
    face: usize,
    other: usize,
    solid: &[usize],
) -> Option<Vec<usize>> {
    let mut supports = Vec::new();
    for e in part.face_edges(face) {
        let adjacent: Vec<_> = part
            .neighbours(face)
            .into_iter()
            .filter(|&f| part.face_edges(f).contains(&e))
            .collect();
        let [f] = adjacent[..] else {
            return None;
        };
        if f == other {
            continue;
        }
        if !solid.contains(&f) || part.arc(face, f) != Some(Arc::Convex) {
            return None;
        }
        supports.push(f);
    }
    supports.sort_unstable();
    supports.dedup();
    Some(supports)
}

fn one_pair(
    ctx: &Context<'_>,
    s: usize,
    principal: usize,
    oblique: usize,
    bounds: &Bounds,
    solid: &[usize],
) -> Option<Occurrence<PolygonalObliqueThroughStep>> {
    let part = ctx.part;
    if part.arc(principal, oblique) != Some(Arc::Concave)
        || principal_plane(part, oblique).is_some()
    {
        return None;
    }
    let (axis, _) = principal_plane(part, principal)?;
    let vertices = polygon(part, principal)?;
    if part.faces[oblique].loops.len() != 1 {
        return None;
    }
    if outer_vertices(part, oblique).len()
        != part.faces[oblique].loops[part.outer_loop(oblique)?]
            .edges
            .len()
    {
        return None;
    }
    let wall = linear_outer_quad(part, oblique)?;
    let normal = unit_plane_normal(part, principal)?;
    let across = unit_plane_normal(part, oblique)?;
    if dot(normal, across).abs() > SMOOTH_ARC_GAP {
        return None;
    }
    let (start, end) = seam_endpoints(part, principal, oblique)?;
    let length = norm(sub(end, start));
    let run = normalized(sub(end, start))?;
    if dot(run, normal).abs() > SMOOTH_ARC_GAP || dot(run, across).abs() > SMOOTH_ARC_GAP {
        return None;
    }
    // Through runs cross opposite sides of the same stock axis. Adjacent stock
    // sides bound a triangular blind-step wedge, even when another cut adds corners.
    let end_axes: Vec<_> = (0..3)
        .filter(|&a| {
            a != axis
                && ((near(start[a], bounds.min[a]) && near(end[a], bounds.max[a]))
                    || (near(start[a], bounds.max[a]) && near(end[a], bounds.min[a])))
        })
        .collect();
    let [end_axis] = end_axes[..] else {
        return None;
    };
    let depth = wall
        .iter()
        .map(|&v| dot(sub(v, start), normal))
        .fold(f64::NEG_INFINITY, f64::max);
    if depth <= COORD_FLOOR {
        return None;
    }
    // All four distinct corners must be those of the complete oblique rectangle.
    for along in [0.0, length] {
        for deep in [0.0, depth] {
            let expected = add(start, add(scale(run, along), scale(normal, deep)));
            if wall
                .iter()
                .filter(|&&p| norm(sub(p, expected)) <= COORD_FLOOR)
                .count()
                != 1
            {
                return None;
            }
        }
    }
    let wall_outline: Vec<_> = wall
        .iter()
        .map(|&p| {
            let offset = sub(p, start);
            [dot(offset, run), dot(offset, normal)]
        })
        .collect();
    if !simple_outline(&wall_outline) {
        return None;
    }
    let principal_supports = boundary_supports(part, principal, oblique, solid)?;
    let wall_supports = boundary_supports(part, oblique, principal, solid)?;
    // The full opposite edge has exactly one parallel, same-facing convex lip.
    // This allows a secondary cut's local opening, without inferring a stock envelope.
    let lips: Vec<_> = wall_supports
        .iter()
        .copied()
        .filter(|&f| {
            matches!(part.faces[f].surface, Surface::Plane { .. })
                && unit_plane_normal(part, f)
                    .is_some_and(|n| norm(sub(n, normal)) <= SMOOTH_ARC_GAP)
                && seam_endpoints(part, oblique, f).is_some_and(|(a, b)| {
                    norm(sub(a, add(start, scale(normal, depth)))) <= COORD_FLOOR
                        && norm(sub(b, add(end, scale(normal, depth)))) <= COORD_FLOOR
                })
        })
        .collect();
    let [lip] = lips[..] else {
        return None;
    };
    let mut context = principal_supports.clone();
    context.extend(&wall_supports);
    for endpoint in [start, end] {
        let far = add(endpoint, scale(normal, depth));
        let ends: Vec<_> = principal_supports
            .iter()
            .copied()
            .filter(|f| {
                wall_supports.contains(f)
                    && *f != lip
                    && principal_plane(part, *f)
                        .is_some_and(|(a, c)| a == end_axis && near(c, endpoint[a]))
                    && part.shared_edges(oblique, *f).iter().any(|&e| {
                        let edge = &part.edges[e];
                        (norm(sub(edge.start, endpoint)) <= COORD_FLOOR
                            && norm(sub(edge.end, far)) <= COORD_FLOOR)
                            || (norm(sub(edge.end, endpoint)) <= COORD_FLOOR
                                && norm(sub(edge.start, far)) <= COORD_FLOOR)
                    })
            })
            .collect();
        if ends.len() != 1 {
            return None;
        }
    }
    // Keep the stock-axis staircase rule, also admitting a diagonal boundary
    // actually owned by a proved convex chamfer. This uses the source edge and
    // treatment evidence, rather than interpreting an arbitrary skew lip as a step.
    let seam_edges = part.shared_edges(principal, oblique);
    for e in part.outer_edges(principal) {
        if seam_edges.contains(&e) {
            continue;
        }
        let direction = normalized(sub(part.edges[e].end, part.edges[e].start))?;
        let principal_edge = (0..3)
            .filter(|&a| a != axis)
            .any(|a| 1.0 - direction[a].abs() <= SMOOTH_ARC_GAP);
        let chamfer_edge = principal_supports.iter().any(|&support| {
            part.shared_edges(principal, support).contains(&e)
                && ctx
                    .reported_chamfers()
                    .iter()
                    .any(|treatment| treatment.defining.contains(&support))
        });
        if !principal_edge && !chamfer_edge {
            return None;
        }
    }
    if vertices
        .iter()
        .any(|&v| dot(sub(v, start), across) < -COORD_FLOOR)
        || !vertices
            .iter()
            .any(|&v| dot(sub(v, start), across) > COORD_FLOOR)
    {
        return None;
    }
    let raw_outline: Vec<_> = vertices
        .iter()
        .map(|&v| {
            let offset = sub(v, start);
            [dot(offset, run), dot(offset, across)]
        })
        .collect();
    let outline: Vec<_> = raw_outline.iter().map(|p| p.map(py::round_to3)).collect();
    if !simple_outline(&raw_outline) || !simple_outline(&outline) {
        return None;
    }
    let inset = length_tol(depth, 1e-6).min(depth / 4.0);
    if ctx.swept_face_volume(
        s,
        principal,
        scale(normal, inset),
        scale(normal, depth - 2.0 * inset),
    )? != 0.0
    {
        return None;
    }
    context.sort_unstable();
    context.dedup();
    Some(Occurrence {
        record: PolygonalObliqueThroughStep {
            run: coordinates(run, 6),
            length: py::round_to3(length),
            at: Placed::new(scale(add(start, end), 0.5), Grid::CleanDecimals(3)),
            depth_direction: coordinates(normal, 6),
            depth: py::round_to3(depth),
            across_direction: coordinates(across, 6),
            through_axis: std::array::from_fn(|a| if a == end_axis { 1.0 } else { 0.0 }),
            slant_degrees: py::round_to(run[end_axis].abs().clamp(0.0, 1.0).acos().to_degrees(), 6),
            floor_outline: canonical_outline(&outline),
            body_key: None,
        },
        defining: vec![principal, oblique],
        context,
    })
}

internal_entry! {
pub fn discover_verified(ctx: &Context<'_>) -> Result<Vec<Occurrence<PolygonalObliqueThroughStep>>, EvidenceError> {
    evidence::verified(ctx.part,discover(ctx))
}
}

internal_entry! {
pub fn discover(ctx: &Context<'_>) -> Vec<Occurrence<PolygonalObliqueThroughStep>> {
    let part = ctx.part;
    let mut found = Vec::new();
    for (s,solid) in part.solids.iter().enumerate() {
        if !part.solid_is_geometrically_valid(s) { continue; }
        let bounds = part.solid_bounds(s);
        for &principal in &solid.faces {
            if principal_plane(part,principal).is_none() { continue; }
            for oblique in part.neighbours(principal) {
                if !solid.faces.contains(&oblique) { continue; }
                if let Some(record) = one_pair(ctx,s,principal,oblique,&bounds,&solid.faces) { found.push((s,record)); }
            }
        }
    }
    let keys = if found.is_empty() { vec![] } else { ctx.body_keys(true) };
    let mut found: Vec<_> = found.into_iter().map(|(s,mut occurrence)| { occurrence.record.body_key = keys[s].clone(); occurrence }).collect();
    found.sort_by(|a,b| {
        let key = |r: &PolygonalObliqueThroughStep| { let mut k = r.run.to_vec(); k.push(r.length); k.extend(*r.at); k.extend(r.depth_direction); k.push(r.depth); k.extend(r.across_direction); k.extend(r.floor_outline.iter().flatten()); k.extend(r.body_key.iter().flatten()); k };
        py::tuple_order(&key(&a.record),&key(&b.record))
    });
    found
}
}

#[cfg(test)]
mod tests {
    use super::simple_outline;

    #[test]
    fn crossed_touching_and_collapsed_polygons_are_ambiguous() {
        assert!(simple_outline(&[
            [0., 0.],
            [4., 0.],
            [4., 1.],
            [2., 1.],
            [2., 3.],
            [0., 3.]
        ]));
        assert!(!simple_outline(&[
            [0., 0.],
            [4., 3.],
            [0., 3.],
            [4., 0.],
            [4., 1.],
            [2., 1.]
        ]));
        assert!(!simple_outline(&[
            [0., 0.],
            [4., 0.],
            [4., 3.],
            [2., 0.],
            [0., 3.]
        ]));
        assert!(!simple_outline(&[
            [0., 0.],
            [4., 0.],
            [4., 0.],
            [4., 3.],
            [0., 3.]
        ]));
    }
}