use std::cmp::Ordering;
use std::collections::BTreeSet;
use serde::{Deserialize, Serialize};
use super::Context;
use super::evidence::{EvidenceError, Occurrence};
use super::experimental_geometry::{GeometryGraph, SurfaceKind};
use super::geometry_evidence::GeometryEvidence;
use super::placed::{Grid, Placed};
use super::policy::AXIS_ALIGNED_COS;
use crate::kernel::brep::Part;
use crate::kernel::geom::V3;
use crate::kernel::py;
const TOL: f64 = 0.2;
const SIDE_VERTICAL_COS: f64 = 0.02;
const AXES: [(usize, &str); 3] = [(2, "z"), (0, "x"), (1, "y")];
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PolygonalPrism {
pub axis: String,
pub center: Placed<V3>,
pub side_count: usize,
pub across_flats: f64,
pub base: Placed<f64>,
pub top: Placed<f64>,
pub flat_directions: Vec<V3>,
pub flat_centres: Vec<Placed<V3>>,
}
pub type PolygonalBoss = PolygonalPrism;
pub type PolygonalStock = PolygonalPrism;
impl PolygonalPrism {
pub fn length(&self) -> f64 {
*self.top - *self.base
}
}
#[derive(Clone, Copy, Debug, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct PolygonalOptions {
pub tol: Option<f64>,
pub angle_tol: f64,
}
impl Default for PolygonalOptions {
fn default() -> Self {
PolygonalOptions {
tol: None,
angle_tol: 2f64.to_radians(),
}
}
}
fn record_order(a: &PolygonalPrism, b: &PolygonalPrism) -> Ordering {
let points = |x: &[V3], y: &[V3]| {
x.iter()
.zip(y)
.map(|(p, q)| py::tuple_order(p, q))
.find(|o| o.is_ne())
.unwrap_or_else(|| x.len().cmp(&y.len()))
};
a.axis
.cmp(&b.axis)
.then_with(|| py::tuple_order(&a.center[..], &b.center[..]))
.then_with(|| a.side_count.cmp(&b.side_count))
.then_with(|| py::order(a.across_flats, b.across_flats))
.then_with(|| py::order(*a.base, *b.base))
.then_with(|| py::order(*a.top, *b.top))
.then_with(|| points(&a.flat_directions, &b.flat_directions))
.then_with(|| {
let centres =
|p: &PolygonalPrism| p.flat_centres.iter().map(|c| **c).collect::<Vec<_>>();
points(¢res(a), ¢res(b))
})
}
struct Proposal {
record: PolygonalPrism,
sides: Vec<usize>,
lower_cap: usize,
upper_cap: usize,
terminal_cap: Option<usize>,
}
fn rounded(value: f64, digits: usize) -> f64 {
py::without_negative_zero(py::round_to(value, digits))
}
fn connected_components(items: &[usize], joined: impl Fn(usize, usize) -> bool) -> Vec<Vec<usize>> {
let mut unseen: BTreeSet<usize> = items.iter().copied().collect();
let mut components = Vec::new();
while let Some(first) = unseen.pop_first() {
let mut connected = vec![first];
let mut frontier = vec![first];
while let Some(current) = frontier.pop() {
let attached: Vec<usize> = unseen
.iter()
.copied()
.filter(|&o| joined(current, o))
.collect();
for &o in &attached {
unseen.remove(&o);
}
connected.extend(&attached);
frontier.extend(attached);
}
connected.sort_unstable();
components.push(connected);
}
components
}
fn cap_coordinate(
graph: &GeometryGraph<'_>,
face: usize,
tol: f64,
axis: usize,
positive: bool,
lower_than: Option<f64>,
higher_than: Option<f64>,
) -> Option<f64> {
if !graph.is_planar(face) {
return None;
}
let normal = graph.normal(face)?;
if (positive && normal[axis] < AXIS_ALIGNED_COS)
|| (!positive && normal[axis] > -AXIS_ALIGNED_COS)
{
return None;
}
let (lo, hi) = graph.bounds(face)[axis];
if hi - lo > tol {
return None;
}
let coordinate = (lo + hi) / 2.0;
if lower_than.is_some_and(|l| coordinate > l + tol)
|| higher_than.is_some_and(|h| coordinate < h - tol)
{
return None;
}
Some(coordinate)
}
#[allow(clippy::too_many_arguments)]
fn common_cap(
component: &[usize],
graph: &GeometryGraph<'_>,
tol: f64,
axis: usize,
upper: bool,
positive: bool,
wall_lo: f64,
wall_hi: f64,
) -> Option<(usize, f64)> {
let mut boundary = BTreeSet::new();
for &side in component {
let choices: Vec<usize> = graph
.neighbours(side)
.into_iter()
.filter(|o| !component.contains(o))
.filter(|&o| {
let (lo, hi) = graph.bounds(o)[axis];
if upper {
(lo - wall_hi).abs() <= tol
} else {
(hi - wall_lo).abs() <= tol
}
})
.collect();
let &[choice] = choices.as_slice() else {
return None;
};
boundary.insert(choice);
}
let candidates: Vec<usize> = if boundary.len() == 1 {
boundary.iter().copied().collect()
} else {
let mut faces = boundary.iter();
let mut common: BTreeSet<usize> = graph
.neighbours(*faces.next().expect("a ring has sides"))
.into_iter()
.collect();
for &f in faces {
let next: BTreeSet<usize> = graph.neighbours(f).into_iter().collect();
common.retain(|n| next.contains(n));
}
common
.into_iter()
.filter(|n| !component.contains(n) && !boundary.contains(n))
.collect()
};
let (lower_than, higher_than) = if upper {
(None, Some(wall_hi))
} else {
(Some(wall_lo), None)
};
let caps: Vec<(usize, f64)> = candidates
.into_iter()
.filter_map(|n| {
cap_coordinate(graph, n, tol, axis, positive, lower_than, higher_than).map(|c| (n, c))
})
.collect();
match caps.as_slice() {
&[cap] => Some(cap),
_ => None,
}
}
fn principal_side_faces(graph: &GeometryGraph<'_>, tol: f64, axis: usize) -> Vec<usize> {
graph
.faces()
.iter()
.copied()
.filter(|&f| {
graph.is_planar(f)
&& graph
.normal(f)
.is_some_and(|n| n[axis].abs() <= SIDE_VERTICAL_COS)
&& {
let (lo, hi) = graph.bounds(f)[axis];
hi - lo > tol
}
})
.collect()
}
fn pair(a: usize, b: usize) -> (usize, usize) {
(a.min(b), a.max(b))
}
fn support_cycle_indices(pairs: &[(usize, usize)], side_count: usize) -> Vec<usize> {
let mut remaining: BTreeSet<usize> = (0..pairs.len()).collect();
let mut selected = Vec::new();
while let Some(seed) = remaining.pop_first() {
let mut component = BTreeSet::from([seed]);
let mut supports = BTreeSet::from([pairs[seed].0, pairs[seed].1]);
let mut changed = true;
while changed {
changed = false;
for at in remaining.clone() {
let (l, r) = pairs[at];
if supports.contains(&l) || supports.contains(&r) {
remaining.remove(&at);
component.insert(at);
supports.extend([l, r]);
changed = true;
}
}
}
let component_pairs: Vec<(usize, usize)> = component.iter().map(|&i| pairs[i]).collect();
let distinct: BTreeSet<&(usize, usize)> = component_pairs.iter().collect();
if component.len() != side_count
|| supports.len() != side_count
|| distinct.len() != side_count
{
continue;
}
if supports.iter().any(|s| {
component_pairs
.iter()
.filter(|(l, r)| l == s || r == s)
.count()
!= 2
}) {
continue;
}
selected.extend(component);
}
selected
}
fn contains_cycle(pairs: &[(usize, usize)]) -> bool {
if pairs.len() < 4 {
return false;
}
let supports: Vec<usize> = pairs
.iter()
.flat_map(|&(l, r)| [l, r])
.collect::<BTreeSet<_>>()
.into_iter()
.collect();
connected_components(&supports, |l, r| pairs.contains(&pair(l, r)))
.iter()
.any(|component| {
matches!(component.len(), 4 | 6)
&& pairs
.iter()
.filter(|(l, r)| component.contains(l) && component.contains(r))
.count()
>= component.len()
})
}
fn blend_bridges(
graph: &GeometryGraph<'_>,
sides: &[usize],
tol: f64,
axis: usize,
) -> Result<BTreeSet<(usize, usize)>, super::discovery::DiscoveryRefusal> {
let same_span = |a: usize, b: usize| {
let (sa, sb) = (graph.bounds(a)[axis], graph.bounds(b)[axis]);
(sa.0 - sb.0).abs() <= tol && (sa.1 - sb.1).abs() <= tol
};
let mut possible: Vec<(usize, (usize, usize))> = Vec::new();
for &node in graph.faces() {
if sides.contains(&node) {
continue;
}
let supports: BTreeSet<usize> = graph
.neighbours(node)
.into_iter()
.filter(|n| sides.contains(n))
.collect();
let &[left, right] = supports.iter().copied().collect::<Vec<_>>().as_slice() else {
continue;
};
if !same_span(left, right) {
continue;
}
possible.push((node, (left, right)));
}
let possible_pairs: Vec<(usize, usize)> = possible.iter().map(|&(_, p)| p).collect();
if !contains_cycle(&possible_pairs) {
return Ok(BTreeSet::new());
}
let cylindrical: Vec<(usize, usize)> = possible
.iter()
.filter(|&&(node, _)| {
graph
.surface_fact(node)
.is_some_and(|f| f.kind == SurfaceKind::Cylinder)
})
.map(|&(_, p)| p)
.collect();
if !contains_cycle(&cylindrical) {
return Ok(BTreeSet::new());
}
let plane = |f: usize| {
graph
.surface_fact(f)
.is_some_and(|s| s.kind == SurfaceKind::Plane)
};
let wall = |f: usize| {
graph
.normal(f)
.is_some_and(|n| n[axis].abs() <= SIDE_VERTICAL_COS)
};
let mut eligible: Vec<(usize, (usize, usize))> = Vec::new();
for (index, fact) in graph.blend_facts().iter().enumerate() {
if fact.side != super::blend_view::SmoothSide::Convex || fact.blend_faces.len() != 1 {
continue;
}
let [l, r] = &fact.supports;
let (&[left], &[right]) = (l.as_slice(), r.as_slice()) else {
continue;
};
if left == right || !plane(left) || !plane(right) || !wall(left) || !wall(right) {
continue;
}
if !same_span(left, right) {
continue;
}
eligible.push((index, pair(left, right)));
}
let eligible_pairs: Vec<(usize, usize)> = eligible.iter().map(|&(_, p)| p).collect();
let selected: BTreeSet<usize> = [4, 6]
.into_iter()
.flat_map(|count| support_cycle_indices(&eligible_pairs, count))
.collect();
if selected.is_empty() {
return Ok(BTreeSet::new());
}
let chains: Vec<usize> = selected.iter().map(|&at| eligible[at].0).collect();
let facts = graph.blend_facts();
let faces: BTreeSet<_> = chains
.iter()
.flat_map(|&i| {
facts[i]
.blend_faces
.iter()
.chain(facts[i].supports.iter().flatten())
})
.copied()
.collect();
let refuse = |reason| super::discovery::DiscoveryRefusal::new(reason, faces.iter().copied());
let bridges = graph.collapsed_bridges(&chains).map_err(refuse)?;
for &at in &selected {
let (chain, support_pair) = eligible[at];
let arcs: Vec<_> = bridges
.iter()
.filter(|b| pair(b.supports.0, b.supports.1) == support_pair)
.collect();
let &[bridge] = arcs.as_slice() else {
return Err(refuse(
"selected Polygonal Boss blend chain has no unique logical bridge",
));
};
let fact = &facts[chain];
let mut expected: Vec<usize> = fact
.blend_faces
.iter()
.chain(fact.supports.iter().flatten())
.copied()
.collect();
expected.sort_unstable();
let mut boundary = fact.boundary.clone();
boundary.sort_by_key(super::blend_view::Shared::key);
if bridge.faces != expected || bridge.boundary != boundary {
return Err(refuse(
"selected Polygonal Boss bridge lost original provenance",
));
}
}
Ok(selected.iter().map(|&at| eligible[at].1).collect())
}
fn regular_ring_order(
component: &[usize],
headings: &dyn Fn(usize) -> (f64, f64),
angle_tol: f64,
) -> Option<Vec<usize>> {
let n = component.len();
let angle = |key: usize| {
let (across, along) = headings(key);
along.atan2(across)
};
let mut ordered = component.to_vec();
ordered.sort_by(|&a, &b| py::order(angle(a), angle(b)));
let turn = 2.0 * std::f64::consts::PI;
let angles: Vec<f64> = ordered
.iter()
.map(|&i| py::modulo(angle(i), turn))
.collect();
let expected = turn / n as f64;
if (0..n)
.any(|i| (py::modulo(angles[(i + 1) % n] - angles[i], turn) - expected).abs() > angle_tol)
{
return None;
}
let opposite = n / 2;
if (0..opposite).any(|i| {
let (a, b) = (headings(ordered[i]), headings(ordered[i + opposite]));
a.0 * b.0 + a.1 * b.1 > -angle_tol.cos()
}) {
return None;
}
Some(ordered)
}
fn ring_profile(
headings: &[(f64, f64)],
centres: &[(f64, f64)],
tol: f64,
) -> Option<(f64, f64, f64)> {
let n = headings.len();
let opposite = n / 2;
let offsets: Vec<f64> = headings
.iter()
.zip(centres)
.map(|(h, p)| h.0 * p.0 + h.1 * p.1)
.collect();
let midplanes: Vec<(f64, f64, f64)> = (0..opposite)
.map(|i| {
(
headings[i].0,
headings[i].1,
(offsets[i] - offsets[i + opposite]) / 2.0,
)
})
.collect();
let sxx = py::sum(midplanes.iter().map(|m| m.0 * m.0));
let sxy = py::sum(midplanes.iter().map(|m| m.0 * m.1));
let syy = py::sum(midplanes.iter().map(|m| m.1 * m.1));
let bx = py::sum(midplanes.iter().map(|m| m.0 * m.2));
let by = py::sum(midplanes.iter().map(|m| m.1 * m.2));
let determinant = sxx * syy - sxy * sxy;
let cx = (bx * syy - by * sxy) / determinant;
let cy = (sxx * by - sxy * bx) / determinant;
let supports: Vec<f64> = headings
.iter()
.zip(&offsets)
.map(|(h, offset)| offset - h.0 * cx - h.1 * cy)
.collect();
if supports.iter().copied().fold(f64::INFINITY, f64::min) <= tol {
return None; }
let across_values: Vec<f64> = (0..opposite)
.map(|i| supports[i] + supports[i + opposite])
.collect();
let across = py::sum(across_values.iter().copied()) / across_values.len() as f64;
if across_values.iter().any(|v| (v - across).abs() > tol)
|| supports.iter().any(|v| (v - across / 2.0).abs() > tol)
{
return None;
}
Some((cx, cy, across))
}
fn recognise_one(
graph: &GeometryGraph<'_>,
tol: f64,
angle_tol: f64,
whole_stock: bool,
(axis, axis_name): (usize, &str),
refusals: &mut Vec<super::discovery::DiscoveryRefusal>,
) -> Vec<Proposal> {
let part = graph.part;
let transverse = match axis {
0 => (1, 2),
1 => (0, 2),
_ => (0, 1),
};
let sides = principal_side_faces(graph, tol, axis);
if sides.len() < if whole_stock { 6 } else { 4 } {
return Vec::new();
}
let bridges = if whole_stock {
BTreeSet::new()
} else {
match blend_bridges(graph, &sides, tol, axis) {
Ok(bridges) => bridges,
Err(refusal) => {
refusals.push(refusal);
BTreeSet::new()
}
}
};
let shares_edge =
|i: usize, j: usize| graph.neighbours(i).contains(&j) || bridges.contains(&pair(i, j));
let same_span = |i: usize, j: usize| {
let (si, sj) = (graph.bounds(i)[axis], graph.bounds(j)[axis]);
(si.0 - sj.0).abs() <= tol && (si.1 - sj.1).abs() <= tol
};
let components = connected_components(&sides, |i, j| same_span(i, j) && shares_edge(i, j));
let mut found = Vec::new();
for component in components {
let side_count = component.len();
if !(side_count == 6 || (!whole_stock && side_count == 4)) {
continue;
}
if whole_stock && graph.len() != side_count + 2 {
continue;
}
if component.iter().any(|&side| {
component
.iter()
.filter(|&&o| o != side && shares_edge(side, o))
.count()
!= 2
}) {
continue;
}
let heading = |f: usize| {
let n = graph.normal(f).expect("side faces have normals");
(
py::without_negative_zero(n[transverse.0]),
py::without_negative_zero(n[transverse.1]),
)
};
let Some(ordered) = regular_ring_order(&component, &heading, angle_tol) else {
continue;
};
let Some(centres) = ordered
.iter()
.map(|&f| part.face_moments(f).map(|m| m.centroid))
.collect::<Option<Vec<V3>>>()
else {
continue;
};
let headings: Vec<(f64, f64)> = ordered.iter().map(|&f| heading(f)).collect();
let planar: Vec<(f64, f64)> = centres
.iter()
.map(|c| (c[transverse.0], c[transverse.1]))
.collect();
let Some((cx, cy, across)) = ring_profile(&headings, &planar, tol) else {
continue;
};
let wall_lo =
py::sum(component.iter().map(|&f| graph.bounds(f)[axis].0)) / side_count as f64;
let wall_hi =
py::sum(component.iter().map(|&f| graph.bounds(f)[axis].1)) / side_count as f64;
let directions: &[bool] = if whole_stock { &[true] } else { &[true, false] };
let mut cap_pairs = Vec::new();
for &positive in directions {
let cap = |upper: bool| {
let facing = if whole_stock { upper } else { positive };
common_cap(
&component, graph, tol, axis, upper, facing, wall_lo, wall_hi,
)
};
if let (Some(base), Some(top)) = (cap(false), cap(true)) {
cap_pairs.push((base, top, positive));
}
}
let &[(base, top, positive)] = cap_pairs.as_slice() else {
continue;
};
if top.1 - base.1 <= tol {
continue;
}
if whole_stock && ((base.1 - wall_lo).abs() > tol || (top.1 - wall_hi).abs() > tol) {
continue;
}
let mut center = [0.0; 3];
center[transverse.0] = cx;
center[transverse.1] = cy;
center[axis] = (base.1 + top.1) / 2.0;
let flat_direction = |(across, along): (f64, f64)| {
let mut d = [0.0; 3];
d[transverse.0] = rounded(across, 3);
d[transverse.1] = rounded(along, 3);
d
};
found.push(Proposal {
record: PolygonalPrism {
axis: axis_name.to_owned(),
center: Placed::new(center, Grid::CleanDecimals(4)),
side_count,
across_flats: rounded(across, 4),
base: Placed::new(base.1, Grid::CleanDecimals(4)),
top: Placed::new(top.1, Grid::CleanDecimals(4)),
flat_directions: headings.iter().map(|&h| flat_direction(h)).collect(),
flat_centres: centres
.iter()
.map(|&c| Placed::new(c, Grid::CleanDecimals(3)))
.collect(),
},
sides: ordered,
lower_cap: base.0,
upper_cap: top.0,
terminal_cap: (!whole_stock).then_some(if positive { top.0 } else { base.0 }),
});
}
found
}
internal_entry! {
pub fn recognise_polygonal_bosses(part: &Part, opts: &PolygonalOptions) -> Vec<PolygonalBoss> {
super::records(discover(&Context::new(part), opts))
}
}
internal_entry! {
pub fn discover(ctx: &Context<'_>, opts: &PolygonalOptions) -> Vec<Occurrence<PolygonalBoss>> {
analyse(ctx, opts).accepted
}
}
pub(crate) fn analyse(
ctx: &Context<'_>,
opts: &PolygonalOptions,
) -> super::discovery::Analysis<PolygonalBoss> {
let mut refusals = Vec::new();
let part = ctx.part;
let tol = opts.tol.unwrap_or(TOL);
let graphs: Vec<GeometryGraph<'_>> = if part.solids.is_empty() {
vec![GeometryGraph::new(part)]
} else {
(0..part.solids.len())
.map(|s| GeometryGraph::for_solid(part, s))
.collect()
};
let mut found: Vec<(Proposal, usize)> = Vec::new();
for (g, graph) in graphs.iter().enumerate() {
for axis in AXES {
found.extend(
recognise_one(graph, tol, opts.angle_tol, false, axis, &mut refusals)
.into_iter()
.map(|p| (p, g)),
);
}
}
found.sort_by(|a, b| record_order(&a.0.record, &b.0.record));
let accepted = found
.into_iter()
.map(|(p, g)| {
let bridge = GeometryEvidence::new(&graphs[g]);
let refs = bridge.refs(&p.sides);
let terminal = p.terminal_cap.expect("a boss keeps its terminal cap");
let mut constituent = refs.clone();
constituent.push(terminal);
bridge.occurrence(p.record, &refs, &constituent)
})
.collect();
super::discovery::Analysis { accepted, refusals }
}
pub(crate) fn claim_terminal_cap(
part: &Part,
record: &PolygonalBoss,
faces: &mut Vec<usize>,
constituent: &[usize],
) {
let Some(axis) = AXES
.iter()
.find(|(_, n)| *n == record.axis)
.map(|&(a, _)| a)
else {
return;
};
let caps: Vec<usize> = constituent
.iter()
.copied()
.filter(|f| !faces.contains(f))
.collect();
let &[cap] = caps.as_slice() else {
return;
};
let tol = TOL;
let Some(normal) = super::graph::normal(part, cap) else {
return;
};
let cb = part.face_bounds(cap);
let level = (cb.min[axis] + cb.max[axis]) / 2.;
if !matches!(
part.faces[cap].surface,
crate::kernel::geom::Surface::Plane { .. }
) || normal[axis].abs() < AXIS_ALIGNED_COS
|| cb.max[axis] - cb.min[axis] > tol
|| ![*record.base.raw(), *record.top.raw()]
.iter()
.any(|&at| (level - at).abs() <= tol)
{
return;
}
let inside = (0..3).filter(|&k| k != axis).all(|k| {
let lo = faces
.iter()
.map(|&f| part.face_bounds(f).min[k])
.fold(f64::INFINITY, f64::min);
let hi = faces
.iter()
.map(|&f| part.face_bounds(f).max[k])
.fold(f64::NEG_INFINITY, f64::max);
cb.min[k] >= lo - tol && cb.max[k] <= hi + tol
});
if inside {
faces.push(cap);
faces.sort_unstable();
}
}
internal_entry! {
pub fn discover_verified(
ctx: &Context<'_>,
opts: &PolygonalOptions,
) -> Result<Vec<Occurrence<PolygonalBoss>>, EvidenceError> {
let found = discover(ctx, opts);
let graph = GeometryGraph::new(ctx.part);
let bridge = GeometryEvidence::new(&graph);
let mut used: Vec<usize> = Vec::new();
for o in &found {
if o.defining.iter().any(|f| used.contains(f)) {
return Err(EvidenceError::SharedEvidence);
}
bridge.validate_defining(&o.defining)?;
let constituent: Vec<usize> = o.defining.iter().chain(&o.context).copied().collect();
bridge.validate_defining(&constituent)?;
used.extend(&o.defining);
}
Ok(found)
}
}
internal_entry! {
pub fn recognise_polygonal_stock(part: &Part, opts: &PolygonalOptions) -> Vec<PolygonalStock> {
super::records(discover_stock(&Context::new(part), opts))
}
}
internal_entry! {
pub fn discover_stock(
ctx: &Context<'_>,
opts: &PolygonalOptions,
) -> Vec<Occurrence<PolygonalStock>> {
let part = ctx.part;
if part.solids.len() != 1 || part.faces.len() != 8 {
return Vec::new();
}
let tol = opts.tol.unwrap_or(TOL);
let graph = GeometryGraph::new(part);
let mut proposals = Vec::new();
for axis in AXES {
proposals = recognise_one(&graph, tol, opts.angle_tol, true, axis, &mut Vec::new());
if !proposals.is_empty() {
break;
}
}
proposals.sort_by(|a, b| record_order(&a.record, &b.record));
let bridge = GeometryEvidence::new(&graph);
proposals
.into_iter()
.map(|p| {
let mut faces = p.sides.clone();
faces.extend([p.lower_cap, p.upper_cap]);
let refs = bridge.refs(&faces);
bridge.occurrence(p.record, &refs, &refs)
})
.collect()
}
}
internal_entry! {
pub fn discover_stock_verified(
ctx: &Context<'_>,
opts: &PolygonalOptions,
) -> Result<Vec<Occurrence<PolygonalStock>>, EvidenceError> {
let found = discover_stock(ctx, opts);
let graph = GeometryGraph::new(ctx.part);
let bridge = GeometryEvidence::new(&graph);
for o in &found {
assert_eq!(o.defining.len(), graph.len(), "stock owns every face");
bridge.validate_defining(&o.defining)?;
}
Ok(found)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_boss_claims_its_own_top_but_not_a_wider_face() {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/golden_polygonal_boss.step");
let part = crate::read_step_file(&path).unwrap();
let features = super::super::recognise(&part);
assert_eq!(
features.defining["polygonal_bosses"],
[vec![6, 7, 8, 9, 10, 11, 12]]
);
let record = &features.polygonal_bosses[0];
let sides = vec![6, 7, 8, 9, 10, 11];
let mut faces = sides.clone();
claim_terminal_cap(&part, record, &mut faces, &[2, 6, 7, 8, 9, 10, 11]);
assert_eq!(faces, sides, "the support plane extends beyond the ring");
claim_terminal_cap(&part, record, &mut faces, &[1, 6, 7, 8, 9, 10, 11]);
assert_eq!(faces, sides, "a side of the block is not a cap");
}
}