use serde::Serialize;
use super::Context;
use super::channels::Channel;
use super::evidence::{self, EvidenceStatus, Occurrence};
use super::graph::normal;
use super::plates::Plate;
use super::regions::length_tolerance;
use crate::kernel::brep::{Arc, Part};
use crate::kernel::geom::{self, Curve, Surface, V3};
use crate::kernel::py;
#[derive(Clone, Debug, PartialEq, Serialize)]
#[non_exhaustive]
pub struct RectangularThroughNotch {
pub axis: V3,
pub opening: V3,
pub at: V3,
pub width: f64,
pub depth: f64,
pub thickness: f64,
pub corner_radius: Option<f64>,
#[serde(skip)]
pub(crate) plate: usize,
}
struct Slab<'a> {
axis: usize,
lo: f64,
hi: f64,
caps: &'a [usize],
tol: f64,
}
const PERPENDICULAR: f64 = 1e-9;
fn line(part: &Part, edge: usize) -> bool {
matches!(part.edges[edge].curve, Curve::Line { .. })
}
impl Slab<'_> {
fn along(&self, edge: &crate::kernel::brep::Edge) -> bool {
let d = geom::sub(edge.end, edge.start);
let (lo, hi) = (edge.start[self.axis].min(edge.end[self.axis]), {
edge.start[self.axis].max(edge.end[self.axis])
});
(0..3)
.filter(|&k| k != self.axis)
.all(|k| d[k].abs() <= self.tol)
&& (lo - self.lo).abs() <= self.tol
&& (hi - self.hi).abs() <= self.tol
}
fn ends(&self, part: &Part, face: usize) -> Option<Vec<usize>> {
let b = part.face_bounds(face);
if (b.min[self.axis] - self.lo).abs() > self.tol
|| (b.max[self.axis] - self.hi).abs() > self.tol
{
return None;
}
let edges = part.face_edges(face);
let mut caps = Vec::new();
for level in [self.lo, self.hi] {
let cap = self.caps.iter().copied().find(|&g| {
let gb = part.face_bounds(g);
(gb.min[self.axis] - level).abs() <= self.tol
&& (gb.max[self.axis] - level).abs() <= self.tol
&& part.arc(face, g) == Some(Arc::Convex)
&& part.outer_edges(g).iter().any(|e| edges.contains(e))
})?;
caps.push(cap);
}
Some(caps)
}
fn wall(&self, part: &Part, face: usize) -> Option<(V3, [usize; 2], Vec<usize>)> {
let f = &part.faces[face];
if !matches!(f.surface, Surface::Plane { .. })
|| f.loops.len() != 1
|| f.loops[0].edges.len() != 4
{
return None;
}
let n = normal(part, face)?;
if n[self.axis].abs() > PERPENDICULAR {
return None;
}
let edges = part.face_edges(face);
if edges.len() != 4 || !edges.iter().all(|&e| line(part, e)) {
return None;
}
let axial: Vec<usize> = edges
.iter()
.copied()
.filter(|&e| self.along(&part.edges[e]))
.collect();
let &[a, b] = axial.as_slice() else {
return None;
};
Some((n, [a, b], self.ends(part, face)?))
}
}
fn across(part: &Part, edge: usize, face: usize) -> Option<usize> {
let [a, b] = part.edge_faces()[edge][..] else {
return None;
};
match (a == face, b == face) {
(true, false) => Some(b),
(false, true) => Some(a),
_ => None,
}
}
struct Side {
wall: usize,
normal: V3,
round: Option<(usize, f64)>,
mouth: usize,
mouth_edge: usize,
caps: Vec<usize>,
}
fn side(part: &Part, slab: &Slab<'_>, bottom: usize, opening: V3, edge: usize) -> Option<Side> {
let next = across(part, edge, bottom)?;
let mut caps = Vec::new();
let (wall, from, round) = match part.faces[next].surface {
Surface::Plane { .. } => {
(part.arc(bottom, next) == Some(Arc::Concave)).then_some(())?;
(next, edge, None)
}
Surface::Cylinder { frame, radius } => {
let f = &part.faces[next];
if part.arc(bottom, next) != Some(Arc::Smooth)
|| part.frame_points_outward(next) != Some(false)
|| f.loops.len() != 1
|| f.loops[0].edges.len() != 4
|| (0..3)
.filter(|&k| k != slab.axis)
.any(|k| frame.z[k].abs() > PERPENDICULAR)
{
return None;
}
caps.extend(slab.ends(part, next)?);
let axial: Vec<usize> = part
.face_edges(next)
.into_iter()
.filter(|&e| e != edge && line(part, e) && slab.along(&part.edges[e]))
.collect();
let &[other] = axial.as_slice() else {
return None;
};
let wall = across(part, other, next)?;
if part.arc(next, wall) != Some(Arc::Smooth) {
return None;
}
(wall, other, Some((next, radius)))
}
_ => return None,
};
if wall == bottom {
return None;
}
let (n, axial, ends) = slab.wall(part, wall)?;
if geom::dot(n, opening).abs() > PERPENDICULAR {
return None;
}
caps.extend(ends);
let mouth_edge = if axial[0] == from {
axial[1]
} else if axial[1] == from {
axial[0]
} else {
return None;
};
let mouth = across(part, mouth_edge, wall)?;
if !matches!(part.faces[mouth].surface, Surface::Plane { .. })
|| part.arc(wall, mouth) != Some(Arc::Convex)
|| geom::dot(normal(part, mouth)?, opening) < 1. - PERPENDICULAR
{
return None;
}
Some(Side {
wall,
normal: n,
round,
mouth,
mouth_edge,
caps,
})
}
fn from_bottom(
part: &Part,
slab: &Slab<'_>,
plate: usize,
bottom: usize,
) -> Option<Occurrence<RectangularThroughNotch>> {
let (opening, axial, caps) = slab.wall(part, bottom)?;
let sides = [
side(part, slab, bottom, opening, axial[0])?,
side(part, slab, bottom, opening, axial[1])?,
];
let [left, right] = &sides;
if left.wall == right.wall || left.mouth == bottom || right.mouth == bottom {
return None;
}
let at_wall = |s: &Side| part.edges[s.mouth_edge].start;
let width = geom::dot(left.normal, geom::sub(at_wall(right), at_wall(left)));
if geom::dot(left.normal, right.normal) > -1. + PERPENDICULAR
|| width <= slab.tol
|| geom::dot(right.normal, geom::sub(at_wall(left), at_wall(right))) <= slab.tol
{
return None;
}
let base = part.edges[axial[0]].start;
let depth = |s: &Side| geom::dot(opening, geom::sub(at_wall(s), base));
let (d0, d1) = (depth(left), depth(right));
if d0 <= slab.tol || (d0 - d1).abs() > slab.tol {
return None;
}
let corner_radius = match (left.round, right.round) {
(None, None) => None,
(Some((_, r0)), Some((_, r1))) if (r0 - r1).abs() <= slab.tol => Some(0.5 * (r0 + r1)),
_ => return None,
};
let mut defining = vec![bottom, left.wall, right.wall];
defining.sort_unstable();
defining.dedup();
if defining.len() != 3 {
return None;
}
let mut context: Vec<usize> = caps
.into_iter()
.chain(sides.iter().flat_map(|s| {
s.caps
.iter()
.copied()
.chain([s.mouth])
.chain(s.round.map(|r| r.0))
}))
.collect();
context.sort_unstable();
context.dedup();
if context.iter().any(|f| defining.contains(f))
|| evidence::strict_status(part, &defining, &context) != EvidenceStatus::Verified
{
return None;
}
let mid = |e: usize| part.edges[e].midpoint();
let mut axis = [0.; 3];
axis[slab.axis] = 1.;
Some(Occurrence {
record: RectangularThroughNotch {
axis,
opening,
at: geom::scale(geom::add(mid(axial[0]), mid(axial[1])), 0.5),
width,
depth: 0.5 * (d0 + d1),
thickness: slab.hi - slab.lo,
corner_radius,
plate,
},
defining,
context,
})
}
internal_entry! {
pub fn discover(
ctx: &Context<'_>,
plates: &[Occurrence<Plate>],
channels: &[Occurrence<Channel>],
) -> Vec<Occurrence<RectangularThroughNotch>> {
let part = ctx.part;
let mut found = Vec::new();
for (index, plate) in plates.iter().enumerate() {
let Some(axis) = ['x', 'y', 'z'].iter().position(|&c| c == plate.record.axis) else {
continue;
};
let Some(solid) = plate.defining.first().and_then(|&f| part.faces[f].solid) else {
continue;
};
let (lo, hi) = (*plate.record.lo.raw(), *plate.record.hi.raw());
let bounds = part.solid_bounds(solid);
let size = (0..3)
.flat_map(|k| [bounds.min[k], bounds.max[k]])
.fold(0.0_f64, |m, v| m.max(v.abs()));
let slab = Slab {
axis,
lo,
hi,
caps: &plate.defining,
tol: length_tolerance(&[size, lo, hi]),
};
found.extend(
part.solids[solid]
.faces
.iter()
.filter(|f| !plate.defining.contains(f))
.filter_map(|&f| from_bottom(part, &slab, index, f)),
);
}
let mut kept: Vec<Occurrence<RectangularThroughNotch>> = found
.iter()
.filter(|o| {
!found
.iter()
.any(|p| !std::ptr::eq(*o, p) && p.defining.iter().any(|f| o.defining.contains(f)))
&& !channels
.iter()
.any(|c| c.defining.iter().any(|f| o.defining.contains(f)))
})
.cloned()
.collect();
kept.sort_by(|a, b| {
py::tuple_order(&a.record.at, &b.record.at)
.then(py::order(a.record.width, b.record.width))
});
kept
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::kernel::step::IDENTITY;
fn fixture() -> Part {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/plate_edge_notches.step");
crate::read_step_file(&path).unwrap()
}
fn near(a: f64, b: f64) {
assert!((a - b).abs() < 1e-6, "{a} != {b}");
}
#[test]
fn rounded_and_sharp_notches_are_found_and_measured_under_motion() {
let base = fixture();
let (s, c) = 0.41_f64.sin_cos();
for motion in [
IDENTITY,
[[c, -s, 0., 31.5], [s, c, 0., -7.25], [0., 0., 1., 12.]],
] {
let part = base.transformed(&motion).unwrap();
let features = super::super::recognise(&part);
let notches = &features.rectangular_through_notches;
assert_eq!(notches.len(), 2, "{notches:?}");
let faces = &features.defining["rectangular_through_notches"];
let mut seen: Vec<_> = notches.iter().zip(faces).collect();
seen.sort_by(|a, b| py::order(a.0.width, b.0.width));
let (sharp, sharp_faces) = seen[0];
let (round, round_faces) = seen[1];
near(sharp.width, 30.);
near(sharp.depth, 20.);
near(sharp.thickness, 12.);
assert_eq!(sharp.corner_radius, None);
near(round.width, 40.);
near(round.depth, 25.);
near(round.thickness, 12.);
near(round.corner_radius.unwrap(), 6.);
assert_eq!(*round_faces, [18, 22, 25]);
assert_eq!(*sharp_faces, [11, 12, 14]);
assert_eq!(features.defining["v_channels"], [vec![3, 7]]);
near(features.v_channels[0].trough_radius.unwrap(), 5.);
near(features.v_channels[0].depth, 35.);
near(features.v_channels[0].included_angle_degrees, 60.);
for f in [4, 17, 19, 21, 26, 42, 32, 35, 36, 39] {
assert!(!faces.iter().flatten().any(|&g| g == f), "face {f}");
}
}
}
#[test]
fn walls_that_do_not_span_the_slab_or_lack_a_mouth_are_not_notches() {
let part = fixture();
let ctx = Context::new(&part);
let plates = super::super::plates::discover(&ctx, &Default::default());
let plate = plates.iter().position(|p| p.record.axis == 'z').unwrap();
assert_eq!(plates[plate].defining, [40, 0]);
let slab = Slab {
axis: 2,
lo: 0.,
hi: 12.,
caps: &plates[plate].defining,
tol: 1e-6,
};
for f in 0..part.faces.len() {
let found = from_bottom(&part, &slab, plate, f).is_some();
assert_eq!(found, f == 12 || f == 22, "face {f}");
}
let thick = Slab { lo: -30., ..slab };
assert!((0..part.faces.len()).all(|f| from_bottom(&part, &thick, plate, f).is_none()));
let found = discover(&ctx, &plates, &[]);
assert_eq!(found.len(), 2);
use super::super::placed::{Grid, Placed};
let at = |v| Placed::new(v, Grid::Decimals(3));
let channel = Occurrence {
record: Channel {
width_axis: 'x',
long_axis: 'z',
width: 30.,
w_center: at(0.),
lo: at(0.),
hi: at(12.),
d_lo: at(60.),
d_hi: at(80.),
open_sign: 1,
body_key: None,
},
defining: vec![11, 14],
context: Vec::new(),
};
assert_eq!(discover(&ctx, &plates, &[channel]).len(), 1);
}
#[test]
fn notches_leave_with_their_rejected_plate() {
use super::super::reconcile::{Candidate, Disposition, Family, Outcome, ReasonCode};
let part = fixture();
let inventory = super::super::inventory(&part).unwrap();
let notches = &inventory.physical.rectangular_through_notches;
assert_eq!(notches.len(), 2);
let plate = notches[0].plate;
assert!(notches.iter().all(|n| n.plate == plate));
assert_eq!(inventory.physical.plates[plate].axis, 'z');
let plates = inventory.physical.plates.len();
let reject = |index: usize| {
let mut inventory = inventory.clone();
inventory.dispositions.push(Disposition {
candidate: Candidate {
family: Family::Plates,
index,
defining: Vec::new(),
},
outcome: Outcome::Rejected,
reason: ReasonCode::PlateSupersededByThinWall,
related: vec![Candidate {
family: Family::ThinWallBodies,
index: 0,
defining: Vec::new(),
}],
});
inventory.accepted()
};
let kept = inventory.clone().accepted();
assert_eq!(kept.rectangular_through_notches.len(), 2);
let after = reject(plate);
assert_eq!(after.plates.len(), plates - 1);
assert!(after.rectangular_through_notches.is_empty());
assert!(after.defining["rectangular_through_notches"].is_empty());
assert!(after.evidence_status["rectangular_through_notches"].is_empty());
if plates > 1 {
let other = (plate + 1) % plates;
assert_eq!(reject(other).rectangular_through_notches.len(), 2);
}
}
}