quiddity 0.4.8

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
//! Original-support proof for open, at-most-semicircular cylindrical channels
//! (`quiddity._cylindrical_seats`): a concave native cylinder, possibly split at seams, whose
//! only neighbours are convex planar stock faces meeting it in two circular rims (one at each
//! axial end) and straight lips on one mouth plane. The proof rebuilds the trough as the arc
//! section swept along the axis, requires its cylindrical face and the original walls to cover
//! each other exactly, and probes the trough, a slab beyond each end and a slab outside the
//! mouth for material.
//!
//! An unanswered probe (or a section the kernel cannot sweep) proves nothing, so refuses.

use std::collections::BTreeSet;
use std::f64::consts::PI;

use super::Context;
use super::entry_treatments::material_fraction;
use super::evidence::common_valid_solid;
use super::graph::{face_vertices, is_planar, normal, shared_occurrences};
use super::policy::length_tol;
use super::regions::edge_length;
use super::sections::{LocalFrame, SectionVertex};
use super::support_patches::{covered_patch, ruled_prism};
use crate::kernel::brep::{Arc, Edge, Face, Loop, Part};
use crate::kernel::geom::{self, Curve, Frame, Surface, V3};
use crate::kernel::sampling::{edge_interval, sample_edge};
use crate::kernel::sweep::extrude_face;

/// A proved seat (`CylindricalSeatProof`): its wall faces, the planar faces around them, the
/// solid, the frame along the axis through the section's centroid, the open arc section as two
/// vertices (the first carrying the arc's bulge) and the axial interval.
#[derive(Clone, Debug, PartialEq)]
pub struct CylindricalSeatProof {
    pub walls: Vec<usize>,
    pub context: Vec<usize>,
    pub owner: usize,
    pub frame: LocalFrame,
    pub boundary: [SectionVertex; 2],
    pub run_interval: (f64, f64),
}

/// A native cylinder as the proof reads it (`_Cylinder`): radius, canonical axis direction and
/// the located axis point.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SeatCylinder {
    pub radius: f64,
    pub axis: V3,
    pub origin: V3,
}

impl SeatCylinder {
    /// Whether *other* is the same surface (`same_surface`): radius and axis line within
    /// `length_tol(radius, 1e-6)`, directions parallel to 1e-8.
    fn same_surface(&self, other: &SeatCylinder) -> bool {
        let tolerance = length_tol(self.radius, 1e-6);
        let delta = geom::sub(other.origin, self.origin);
        let across = geom::sub(delta, geom::scale(self.axis, geom::dot(delta, self.axis)));
        (self.radius - other.radius).abs() <= tolerance
            && geom::norm(geom::cross(self.axis, other.axis)) <= 1e-8
            && geom::norm(across) <= tolerance
    }
}

/// The edge's points at the start, middle and end of its parameter range (`position_at(0)`,
/// `(0.5)`, `(1)`: by arc length, which for lines and circles is by parameter).
fn samples(edge: &Edge) -> [V3; 3] {
    let (a, b) = edge_interval(
        &edge.curve,
        edge.start,
        edge.end,
        edge.same_sense,
        edge.is_closed(),
    );
    [a, 0.5 * (a + b), b].map(|t| edge.curve.value(t))
}

/// The circle through three points, as `(centre, radius, normal)` with the walk start → middle
/// → end counter-clockwise about the normal, and the length of that arc
/// (`Edge.make_three_point_arc`). `None` for collinear points.
fn three_point_arc(start: V3, middle: V3, end: V3) -> Option<(V3, f64, V3, f64)> {
    let (a, b) = (geom::sub(start, middle), geom::sub(end, middle));
    let normal = geom::cross(geom::sub(middle, start), geom::sub(end, middle));
    let n2 = geom::dot(normal, normal);
    if n2 <= 1e-300 {
        return None;
    }
    // Circumcentre relative to the middle point.
    let ab = geom::cross(a, b);
    let offset = geom::scale(
        geom::add(
            geom::scale(geom::cross(ab, a), geom::dot(b, b)),
            geom::scale(geom::cross(b, ab), geom::dot(a, a)),
        ),
        0.5 / n2,
    );
    let centre = geom::add(middle, offset);
    let radius = geom::norm(offset);
    let normal = geom::unit(normal)?;
    let turn = |p: V3, q: V3| {
        let (p, q) = (geom::sub(p, centre), geom::sub(q, centre));
        let angle = geom::dot(geom::cross(p, q), normal).atan2(geom::dot(p, q));
        if angle < 0.0 { angle + 2.0 * PI } else { angle }
    };
    let length = radius * (turn(start, middle) + turn(middle, end));
    Some((centre, radius, normal, length))
}

/// The planar region between the arc start → end about *frame* (its z the arc's normal) and the
/// chord back (`Face(Wire([arc, Edge.make_line(end, start)]))`), as a one-face part.
fn segment_face(frame: Frame, radius: f64, start: V3, end: V3) -> Part {
    let arc = Curve::Circle { frame, radius };
    let chord = Curve::Line {
        origin: end,
        dir: geom::sub(start, end),
    };
    let edges = vec![
        Edge {
            samples: sample_edge(&arc, start, end, true, false),
            curve: arc,
            start,
            end,
            vertices: (0, 1),
            same_sense: true,
        },
        Edge {
            curve: chord,
            start: end,
            end: start,
            vertices: (1, 0),
            same_sense: true,
            samples: vec![end, start],
        },
    ];
    let face = Face {
        surface: Surface::Plane { frame },
        reversed: false,
        loops: vec![Loop {
            edges: vec![(0, true), (1, true)],
            vertex: None,
        }],
        solid: None,
        pcurves: Vec::new(),
    };
    Part::new(vec![face], edges, Vec::new())
}

/// Whether *probe* is built and holds no more than 1e-9 of its volume in material; an unbuilt
/// or unanswered probe proves nothing.
fn empty(ctx: &Context<'_>, owner: usize, probe: Option<&Part>) -> bool {
    probe
        .and_then(|p| material_fraction(ctx, owner, p))
        .is_some_and(|f| f <= 1e-9)
}

internal_entry! {
/// The seat proof for one group of same-surface walls, sorted by face (`_prove`); `None` for
/// anything outside the contract.
pub fn prove(
    ctx: &Context<'_>,
    walls: &[usize],
    cylinder: &SeatCylinder,
) -> Option<CylindricalSeatProof> {
    let part = ctx.part;
    let own: BTreeSet<usize> = walls.iter().copied().collect();
    let context: Vec<usize> = walls
        .iter()
        .flat_map(|&w| part.neighbours(w))
        .filter(|n| !own.contains(n))
        .collect::<BTreeSet<usize>>()
        .into_iter()
        .collect();
    let members: Vec<usize> = walls.iter().chain(&context).copied().collect();
    let owner = common_valid_solid(part, &members)?;
    if context.is_empty() || context.iter().any(|&n| !is_planar(part, n)) {
        return None;
    }
    let (radius, axis) = (cylinder.radius, cylinder.axis);
    let tolerance = length_tol(radius, 1e-6);
    let along: Vec<f64> = walls
        .iter()
        .flat_map(|&w| face_vertices(part, w))
        .map(|p| geom::dot(p, axis))
        .collect();
    if along.is_empty() {
        return None;
    }
    let low = along.iter().copied().fold(f64::INFINITY, f64::min);
    let high = along.iter().copied().fold(f64::NEG_INFINITY, f64::max);
    if high - low <= 2.0 * tolerance {
        return None;
    }
    let mut rims: [BTreeSet<usize>; 2] = [BTreeSet::new(), BTreeSet::new()];
    let mut lips: BTreeSet<usize> = BTreeSet::new();
    let mut opening_normal: Option<V3> = None;
    for &wall in walls {
        for node in part.neighbours(wall) {
            if own.contains(&node) {
                continue;
            }
            // Tangent fillets and interior corner blends are not open seats.
            if part.arc(wall, node) != Some(Arc::Convex) {
                return None;
            }
            let normal = normal(part, node)?;
            for e in shared_occurrences(part, wall, node) {
                let edge = &part.edges[e];
                let points = samples(edge);
                match edge.curve {
                    Curve::Circle { .. } => {
                        let middle = geom::dot(points[1], axis);
                        let end_index = usize::from((middle - low).abs() >= (middle - high).abs());
                        let (at, sign) = if end_index == 0 {
                            (low, -1.0)
                        } else {
                            (high, 1.0)
                        };
                        if geom::dot(normal, axis) * sign < 1.0 - 1e-8
                            || points
                                .iter()
                                .any(|&p| (geom::dot(p, axis) - at).abs() > tolerance)
                        {
                            return None;
                        }
                        rims[end_index].insert(e);
                    }
                    Curve::Line { dir, .. } => {
                        // Native seams may leave arbitrarily short lip fragments. Prove
                        // direction from the line, independent of fragment length; the complete
                        // axial span has its own minimum above.
                        let tangent = geom::unit(dir)?;
                        if geom::norm(geom::sub(points[2], points[0])) == 0.0
                            || geom::norm(geom::cross(tangent, axis)) > 1e-8
                            || geom::dot(normal, axis).abs() > 1e-8
                        {
                            return None;
                        }
                        if opening_normal.is_some_and(|o| geom::dot(normal, o) < 1.0 - 1e-8) {
                            return None;
                        }
                        opening_normal = Some(normal);
                        lips.insert(e);
                    }
                    _ => return None,
                }
            }
        }
    }
    let opening_normal = opening_normal?;
    if rims.iter().any(BTreeSet::is_empty) || lips.is_empty() {
        return None;
    }
    // A rim may be subdivided at a native seam. Its two degree-one vertices determine the
    // physical arc; all subdivisions retain their source faces.
    let mut counts: Vec<(usize, V3, usize)> = Vec::new();
    for &e in &rims[0] {
        let edge = &part.edges[e];
        let mut ends = vec![(edge.vertices.0, edge.start)];
        if !edge.is_closed() {
            ends.push((edge.vertices.1, edge.end));
        }
        for (vertex, point) in ends {
            match counts.iter_mut().find(|c| c.0 == vertex) {
                Some(c) => c.2 += 1,
                None => counts.push((vertex, point, 1)),
            }
        }
    }
    let ends: Vec<V3> = counts.iter().filter(|c| c.2 == 1).map(|c| c.1).collect();
    if ends.len() != 2 || counts.iter().any(|c| !matches!(c.2, 1 | 2)) {
        return None;
    }
    let sweep = rims[0].iter().map(|&e| edge_length(part, e)).sum::<f64>() / radius;
    if !(1e-6 < sweep && sweep <= PI + 1e-8) {
        return None;
    }
    let (start, end) = (ends[0], ends[1]);
    if geom::dot(geom::sub(end, start), opening_normal).abs() > tolerance {
        return None;
    }
    let mouth_at = geom::dot(start, opening_normal);
    if lips.iter().any(|&e| {
        (geom::dot(samples(&part.edges[e])[1], opening_normal) - mouth_at).abs() > tolerance
    }) {
        return None;
    }
    // The circle's low point opposite the exterior normal fixes which of the two arcs is the
    // concave trough, including exactly semicircular seats.
    let origin = cylinder.origin;
    let centre = geom::add(origin, geom::scale(axis, low - geom::dot(origin, axis)));
    let middle = geom::sub(centre, geom::scale(opening_normal, radius));
    let (arc_centre, arc_radius, arc_normal, arc_length) = three_point_arc(start, middle, end)?;
    if (arc_length - radius * sweep).abs() > tolerance {
        return None;
    }
    let x = geom::unit(geom::sub(start, arc_centre))?;
    let frame = Frame {
        origin: arc_centre,
        x,
        y: geom::cross(arc_normal, x),
        z: arc_normal,
    };
    let section = segment_face(frame, arc_radius, start, end);
    let length = high - low;
    let prism = extrude_face(&section, 0, [0.0; 3], geom::scale(axis, length))?;
    let support: Vec<usize> = (0..prism.faces.len())
        .filter(|&f| matches!(prism.faces[f].surface, Surface::Cylinder { .. }))
        .collect();
    let originals: Vec<(&Part, usize)> = walls.iter().map(|&w| (part, w)).collect();
    if support.len() != 1
        || !covered_patch((&prism, support[0]), &originals)
        || originals
            .iter()
            .any(|&f| !covered_patch(f, &[(&prism, support[0])]))
    {
        return None;
    }
    // `length_tol(radius, rel=1e-4, floor=2e-5)`.
    let thickness = 1e-4 * radius + 2e-5;
    let mouth = [
        start,
        end,
        geom::add(end, geom::scale(axis, length)),
        geom::add(start, geom::scale(axis, length)),
    ];
    let lifted = |by: f64| mouth.map(|p| geom::add(p, geom::scale(opening_normal, by)));
    let slabs = [
        extrude_face(
            &section,
            0,
            geom::scale(axis, -thickness),
            geom::scale(axis, thickness - 1e-6),
        ),
        extrude_face(
            &section,
            0,
            geom::scale(axis, length + 1e-6),
            geom::scale(axis, thickness - 1e-6),
        ),
        ruled_prism(&lifted(1e-6), &lifted(thickness)),
    ];
    if !empty(ctx, owner, Some(&prism)) || slabs.iter().any(|p| !empty(ctx, owner, p.as_ref())) {
        return None;
    }
    // The section face's centroid: on the bisector of the arc, at the circular segment's
    // centroid distance from the arc's centre.
    let angle = arc_length / arc_radius;
    let bisector = geom::add(
        geom::scale(frame.x, (0.5 * angle).cos()),
        geom::scale(frame.y, (0.5 * angle).sin()),
    );
    let distance = 4.0 * arc_radius * (0.5 * angle).sin().powi(3) / (3.0 * (angle - angle.sin()));
    let centroid = geom::add(arc_centre, geom::scale(bisector, distance));
    let frame = LocalFrame::canonical(axis, centroid).ok()?;
    let project = |point: V3| {
        let delta = geom::sub(point, frame.origin);
        [geom::dot(delta, frame.u), geom::dot(delta, frame.v)]
    };
    let (first, last) = (project(start), project(end));
    let (radial_start, radial_end) = (geom::sub(start, centre), geom::sub(end, centre));
    let turn = geom::dot(geom::cross(radial_start, radial_end), axis);
    let mut sign = if turn > 0.0 { 1.0 } else { -1.0 };
    // At a semicircle the cross product vanishes. The midpoint fixes orientation.
    if turn.abs() < radius * radius * 1e-8 {
        let towards = geom::dot(geom::cross(radial_start, geom::sub(middle, centre)), axis);
        sign = if towards > 0.0 { 1.0 } else { -1.0 };
    }
    let boundary = [
        SectionVertex::new(first, sign * (sweep / 4.0).tan()).ok()?,
        SectionVertex::new(last, 0.0).ok()?,
    ];
    Some(CylindricalSeatProof {
        walls: walls.to_vec(),
        context,
        owner,
        frame,
        boundary,
        run_interval: (low, high),
    })
}
}

internal_entry! {
/// Every proved seat (`cylindrical_seat_proofs`): the concave native cylinders grouped, from
/// each face in order, with the neighbours on the same surface, and each group proved.
pub fn cylindrical_seat_proofs(ctx: &Context<'_>) -> Vec<CylindricalSeatProof> {
    let part = ctx.part;
    let mut cylinders: Vec<(usize, SeatCylinder)> = Vec::new();
    for node in 0..part.faces.len() {
        let Surface::Cylinder { frame, radius } = part.faces[node].surface else {
            continue;
        };
        if part.frame_points_outward(node) != Some(false) {
            continue;
        }
        let Ok(canonical) = LocalFrame::canonical(frame.z, [0.0; 3]) else {
            continue;
        };
        cylinders.push((
            node,
            SeatCylinder {
                radius,
                axis: canonical.run,
                origin: frame.origin,
            },
        ));
    }
    let of = |node: usize| cylinders.iter().find(|c| c.0 == node).map(|c| c.1);
    let mut remaining: BTreeSet<usize> = cylinders.iter().map(|c| c.0).collect();
    let mut proofs = Vec::new();
    for &(seed, cylinder) in &cylinders {
        if !remaining.remove(&seed) {
            continue;
        }
        let mut walls = BTreeSet::from([seed]);
        let mut pending = vec![seed];
        while let Some(node) = pending.pop() {
            for neighbour in part.neighbours(node) {
                if remaining.contains(&neighbour)
                    && of(neighbour).is_some_and(|other| cylinder.same_surface(&other))
                {
                    remaining.remove(&neighbour);
                    walls.insert(neighbour);
                    pending.push(neighbour);
                }
            }
        }
        let walls: Vec<usize> = walls.into_iter().collect();
        if let Some(proof) = prove(ctx, &walls, &cylinder) {
            proofs.push(proof);
        }
    }
    proofs
}
}

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

    #[test]
    fn three_point_arc_reads_a_semicircle() {
        let (centre, radius, normal, length) =
            three_point_arc([1.0, 0.0, 0.0], [0.0, -1.0, 0.0], [-1.0, 0.0, 0.0]).unwrap();
        assert!(geom::norm(centre) < 1e-15);
        assert!((radius - 1.0).abs() < 1e-15);
        assert!((normal[2] + 1.0).abs() < 1e-15);
        assert!((length - PI).abs() < 1e-14);
        assert!(three_point_arc([0.0; 3], [1.0, 0.0, 0.0], [2.0, 0.0, 0.0]).is_none());
    }
}