pub mod analytic_surfaces;
pub mod angled_steps;
pub mod bevel;
pub mod blend_view;
pub mod blends;
pub mod body;
pub mod bosses;
pub mod chamfers;
pub mod channels;
pub mod circular_blind_steps;
pub mod circular_face_patterns;
mod duplicate_references;
mod edge_treatments;
mod isolation;
pub use isolation::FamilyFailure;
pub mod through_notches;
pub mod v_channels;
internal_entry! { pub mod context; }
pub mod annular_grooves;
pub mod countersinks;
pub mod cylinders;
pub mod cylindrical_channels;
pub mod cylindrical_end_surface;
pub mod cylindrical_passages;
pub mod cylindrical_pockets;
pub mod cylindrical_profiles;
pub mod cylindrical_seats;
pub mod discovery;
pub mod edge_open;
pub mod edge_open_circular;
pub mod edge_open_prismatic;
pub mod effective_surfaces;
pub mod entry_treatments;
pub mod evidence;
pub mod experimental_geometry;
pub mod face_grooves;
pub mod fillets;
pub mod flats;
pub mod freeform_surfaces;
pub mod geometry_evidence;
pub mod graph;
pub mod grooves;
pub mod gussets;
pub mod helical_springs;
pub mod hole_patterns;
pub mod holes;
pub mod interior_voids;
pub mod internal_bevels;
pub mod levels;
pub mod local_steps;
pub mod modelled_threads;
pub mod oblique_through_steps;
pub mod oriented_chamfers;
pub mod oriented_slots;
pub mod outer_profile;
pub mod pads;
pub mod paired_ramp_steps;
pub mod passage_compat;
pub mod passages;
pub(crate) mod pattern_geometry;
pub mod placed;
pub mod plane_envelope_passages;
pub mod planes;
pub mod plates;
pub mod pockets;
pub mod policy;
pub mod polygonal_bosses;
pub mod polygonal_oblique_through_steps;
pub mod prismatic_pockets;
pub mod prismatic_through_cuts;
pub mod probes;
pub mod profiled_bores;
pub mod recess_core;
pub mod recess_faces;
pub mod recess_obround;
pub mod recess_patterns;
pub mod recess_radii;
pub mod recess_records;
pub mod recess_reduce;
pub mod reconcile;
pub mod rectangular_blind_slots;
pub mod regions;
pub mod repeating_profiles;
pub mod rings;
pub mod round_bottom_slots;
pub mod round_grooves;
pub mod section_passages;
pub mod section_recess;
pub mod section_recess_discovery;
pub mod section_recess_family;
pub mod section_recess_geometry;
pub mod sections;
pub mod semicircular_blind_slots;
pub mod sheet_metal;
pub mod slots;
pub mod solid_properties;
pub mod stacks;
pub mod support_patches;
pub mod thin_walls;
pub mod through_steps;
pub mod turned;
pub mod turned_bodies;
pub mod turned_profile_records;
pub mod turned_steps;
pub mod volume_probe;
pub mod wire_seed;
internal_entry! { pub use context::Context; }
pub use evidence::{EvidenceError, Occurrence};
use std::collections::{BTreeMap, BTreeSet};
use serde::Serialize;
use crate::kernel::brep::Part;
#[derive(Clone, Debug, Default, Serialize)]
#[non_exhaustive]
pub struct Features {
#[serde(skip)]
pub failures: Vec<FamilyFailure>,
#[serde(skip)]
pub run_mode: evidence::RunMode,
#[serde(skip)]
pub discovery_refusals: BTreeMap<&'static str, Vec<discovery::DiscoveryRefusal>>,
#[serde(skip)]
pub evidence_status: BTreeMap<&'static str, Vec<evidence::EvidenceStatus>>,
#[serde(skip)]
pub(crate) boss_caps: BTreeMap<Vec<usize>, Vec<usize>>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub helical_springs: Vec<helical_springs::HelicalSpring>,
pub fillets: Vec<fillets::Fillet>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub cylindrical_profiles: Vec<cylindrical_profiles::CylindricalProfile>,
pub chamfers: Vec<chamfers::Chamfer>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub internal_bevels: Vec<internal_bevels::InternalBevel>,
pub bosses: Vec<bosses::BossRecord>,
pub angled_steps: Vec<angled_steps::AngledStep>,
pub flats: Vec<flats::Flat>,
pub paired_ramp_steps: Vec<paired_ramp_steps::PairedRampStep>,
pub oriented_chamfers: Vec<oriented_chamfers::OrientedChamfer>,
pub circular_face_patterns: Vec<circular_face_patterns::CircularFacePattern>,
pub oblique_through_steps: Vec<oblique_through_steps::ObliqueThroughStep>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub polygonal_oblique_through_steps:
Vec<polygonal_oblique_through_steps::PolygonalObliqueThroughStep>,
pub circular_blind_steps: Vec<circular_blind_steps::CircularBlindStep>,
pub holes: Vec<holes::HoleRecord>,
pub countersinks: Vec<countersinks::CounterSink>,
pub hole_patterns: Vec<hole_patterns::HolePattern>,
pub gusset_ribs: Vec<gussets::GussetRib>,
pub gusset_rib_patterns: Vec<gussets::GussetRibPattern>,
pub thin_wall_bodies: Vec<thin_walls::ThinWallBody>,
pub interior_voids: Vec<interior_voids::InteriorVoid>,
#[serde(skip)]
pub interior_void_refusals: Vec<interior_voids::InteriorVoidRefusal>,
pub through_steps: Vec<through_steps::ThroughStep>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub local_steps: Vec<local_steps::LocalStep>,
pub turned_steps: Vec<turned_steps::TurnedStep>,
pub grooves: Vec<grooves::Groove>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub annular_grooves: Vec<annular_grooves::AnnularGroove>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub face_grooves: Vec<face_grooves::FaceGroove>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub round_grooves: Vec<round_grooves::RoundGroove>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub tapers: Vec<turned_profile_records::Taper>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub v_grooves: Vec<turned_profile_records::VGroove>,
pub plates: Vec<plates::Plate>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub turned_bodies: Vec<turned_bodies::TurnedBody>,
pub round_bottom_blind_slots: Vec<round_bottom_slots::RoundBottomBlindSlot>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub semicircular_blind_slots: Vec<semicircular_blind_slots::SemicircularBlindSlot>,
pub rectangular_blind_slots: Vec<rectangular_blind_slots::RectangularBlindSlot>,
pub double_d_bores: Vec<profiled_bores::DoubleDBore>,
pub edge_open_circular_pockets: Vec<edge_open_circular::EdgeOpenCircularPocket>,
pub edge_open_prismatic_recesses: Vec<edge_open_prismatic::EdgeOpenPrismaticRecess>,
pub blends: Vec<blends::Blend>,
pub sheet_metal_bodies: Vec<sheet_metal::SheetMetalBody>,
pub slots: Vec<slots::Slot>,
pub pockets: Vec<pockets::Pocket>,
pub channels: Vec<channels::Channel>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub v_channels: Vec<v_channels::VChannel>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub rectangular_through_notches: Vec<through_notches::RectangularThroughNotch>,
pub slot_patterns: Vec<recess_patterns::SlotPattern>,
pub pocket_patterns: Vec<recess_patterns::PocketPattern>,
pub repeating_radial_profiles: Vec<repeating_profiles::RepeatingRadialProfile>,
pub freeform_surfaces: Vec<freeform_surfaces::FreeformSurface>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub modelled_threads: Vec<modelled_threads::ModelledThread>,
#[serde(skip)]
pub modelled_thread_refusals: Vec<modelled_threads::ModelledThreadRefusal>,
pub polygonal_bosses: Vec<polygonal_bosses::PolygonalBoss>,
pub polygonal_stock: Vec<polygonal_bosses::PolygonalStock>,
pub section_passages: Vec<passages::SectionPassage>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub prismatic_through_cuts: Vec<prismatic_through_cuts::PrismaticThroughCut>,
pub prismatic_pockets: Vec<prismatic_pockets::PrismaticPocket>,
pub oriented_slots: Vec<oriented_slots::OrientedSlot>,
pub oriented_slot_patterns: Vec<oriented_slots::OrientedSlotPattern>,
pub pads: Vec<pads::RaisedPad>,
pub step_levels: Vec<levels::FaceLevel>,
pub risers: Vec<levels::RiserEvidence>,
pub section_recesses: Vec<section_recess::SectionRecess>,
#[serde(skip)]
pub section_recess_patterns: Vec<section_recess::SectionRecessPattern>,
#[serde(skip)]
pub section_recess_refusals: Vec<section_recess::SectionRecessRefusal>,
#[serde(skip)]
pub section_recess_refusal: Option<section_recess_family::SectionRecessFamilyError>,
#[serde(skip)]
pub defining: Defining,
}
pub type Defining = BTreeMap<&'static str, Vec<Vec<usize>>>;
internal_entry! {
pub fn recognise(part: &Part) -> Features {
recognise_with(part, |_, features| features)
}
}
pub(crate) fn recognise_with<T>(part: &Part, then: impl FnOnce(&Context<'_>, Features) -> T) -> T {
let mut then = Some(then);
take_inventory(part, |ctx, inventory| {
then.take().unwrap()(ctx, recognise_in(ctx, inventory))
})
.unwrap_or_else(|error| {
let ctx = Context::new(part);
let features = reconciliation_refusal(part, error);
then.take().unwrap()(&ctx, features)
})
}
pub(crate) fn reconciliation_refusal(part: &Part, error: reconcile::ReconcileError) -> Features {
Features {
discovery_refusals: BTreeMap::from([(
"reconciliation_refusals",
vec![discovery::DiscoveryRefusal::new(error, 0..part.faces.len())],
)]),
..Features::default()
}
}
pub(crate) type RecessProjection = Option<
Result<
section_recess_family::SectionRecessProjection,
section_recess_family::SectionRecessFamilyError,
>,
>;
pub(crate) fn recognise_in(ctx: &Context<'_>, mut inventory: Inventory) -> Features {
let projection = project_recesses(ctx, &mut inventory);
recognise_projected(ctx, inventory, projection)
}
pub(crate) fn recognise_projected(
ctx: &Context<'_>,
inventory: Inventory,
projection: RecessProjection,
) -> Features {
let constituents = |family: &str| -> BTreeMap<Vec<usize>, Vec<usize>> {
inventory.physical.defining[family]
.iter()
.cloned()
.zip(inventory.constituent[family].iter().cloned())
.collect()
};
let hole_constituents = constituents("holes");
let pocket_constituents = constituents("pockets");
let prismatic_pocket_constituents = constituents("prismatic_pockets");
let boss_constituents = constituents("polygonal_bosses");
let mut features = inventory.accepted();
let (section_recesses, refusals, patterns, refusal) = match projection {
Some(Ok(p)) => (p.section_recesses, p.refusals, p.patterns, None),
Some(Err(e)) => (Vec::new(), Vec::new(), Vec::new(), Some(e)),
None => (Vec::new(), Vec::new(), Vec::new(), None),
};
let unmeasured: BTreeSet<usize> = unmeasured_faces(ctx.part).into_iter().collect();
let (section_recesses, patterns) = if unmeasured.is_empty()
|| !section_recesses.iter().any(|r| {
r.evidence()
.defining_faces()
.iter()
.any(|f| unmeasured.contains(f))
}) {
(section_recesses, patterns)
} else {
let kept: Vec<_> = section_recesses
.into_iter()
.filter(|r| {
!r.evidence()
.defining_faces()
.iter()
.any(|f| unmeasured.contains(f))
})
.collect();
(kept, Vec::new())
};
let defining = section_recesses
.iter()
.map(|r| r.evidence().defining_faces().to_vec())
.collect();
features.defining.insert("section_recesses", defining);
features.evidence_status.insert(
"section_recesses",
section_recesses
.iter()
.map(|r| {
evidence::strict_status(
ctx.part,
r.evidence().defining_faces(),
r.evidence().constituent_faces(),
)
})
.collect(),
);
features.section_recesses = section_recesses;
features.section_recess_refusals = refusals;
features.section_recess_patterns = patterns;
features.section_recess_refusal = refusal;
let mut failures = std::mem::take(&mut features.failures);
let before = failures.len();
macro_rules! post {
($family:expr, $stage:literal, $work:expr $(,)?) => {
if failures.len() == before {
isolation::guarded(&mut failures, $family, $stage, $work);
}
};
}
post!("holes", "claim_drill_point", || {
for (record, faces) in features
.holes
.iter()
.zip(features.defining.get_mut("holes").unwrap())
{
if let Some(constituent) = hole_constituents.get(faces) {
holes::claim_drill_point(ctx.part, record, faces, constituent);
}
}
});
post!("pockets", "claim_floor_spheres", || {
let spheres: BTreeSet<_> = features
.pockets
.iter()
.flat_map(|p| &p.floor_fillets)
.map(|f| f.face)
.filter(|&f| {
matches!(
ctx.part.faces[f].surface,
crate::kernel::geom::Surface::Sphere { .. }
)
})
.collect();
if spheres.is_empty() {
return;
}
for faces in features.defining.get_mut("fillets").unwrap() {
faces.retain(|f| !spheres.contains(f));
assert!(
!faces.is_empty(),
"floor-only fillets must yield during reconciliation"
);
}
});
post!("slots", "claim_rectangular_end_walls", || {
for (record, faces) in features
.slots
.iter()
.zip(features.defining.get_mut("slots").unwrap())
{
slots::claim_rectangular_end_walls(ctx.part, record, faces);
}
});
post!("polygonal_bosses", "claim_terminal_cap", || {
for (record, faces) in features
.polygonal_bosses
.iter()
.zip(features.defining.get_mut("polygonal_bosses").unwrap())
{
if let Some(constituent) = boss_constituents.get(faces) {
polygonal_bosses::claim_terminal_cap(ctx.part, record, faces, constituent);
}
}
});
post!("prismatic_through_cuts", "append", || {
prismatic_through_cuts::append(ctx, &mut features)
});
post!("cylindrical_profiles", "append", || {
cylindrical_profiles::append(ctx, &mut features)
});
post!("turned_bodies", "suppress_od_bosses", || {
turned_bodies::suppress_od_bosses(ctx.part, &mut features)
});
post!("face_grooves", "append", || {
face_grooves::append(ctx, &mut features)
});
post!("round_grooves", "append", || {
round_grooves::append(ctx, &mut features)
});
post!("v_grooves", "append", || {
turned_profile_records::append_v_grooves(ctx, &mut features)
});
post!("tapers", "append", || {
turned_profile_records::append_tapers(ctx, &mut features)
});
if failures.len() == before {
let mut floors = Vec::new();
for (index, record) in features.holes.iter().enumerate() {
if record.bottom == holes::Bottom::Flat {
floors.push(("holes", index, record.axis, &hole_constituents));
}
}
for (index, record) in features.pockets.iter().enumerate() {
let axis = recess_records::third_axis(
recess_records::axis_index(record.width_axis),
recess_records::axis_index(record.long_axis),
);
let mut direction = [0.; 3];
direction[axis] = -f64::from(record.open_sign);
floors.push(("pockets", index, direction, &pocket_constituents));
}
for (index, record) in features.prismatic_pockets.iter().enumerate() {
let axis = recess_records::axis_index(record.axis.chars().next().unwrap());
let mut direction = [0.; 3];
direction[axis] = -f64::from(record.open_sign);
floors.push((
"prismatic_pockets",
index,
direction,
&prismatic_pocket_constituents,
));
}
let occupied = floor_claims_occupied(&features.defining);
let mut floor_owners = BTreeSet::new();
for (family, index, direction, constituents) in floors {
post!(family, "claim_flat_floor", || {
let faces = &mut features.defining.get_mut(family).unwrap()[index];
if let Some(constituent) = constituents.get(faces) {
holes::claim_flat_floor(
ctx.part,
direction,
faces,
constituent,
&occupied,
&mut floor_owners,
);
}
});
}
}
post!("step_levels", "other_axes", || {
other_axis_levels(ctx, &mut features)
});
if failures.len() != before {
let inventory = inventory_in_with(ctx, failures)
.expect("reconciliation remains outside the panic boundary")
.expect("a completed run retains its degradation mode");
return recognise_in(ctx, inventory);
}
failures.sort_by(|a, b| (&a.family, &a.stage).cmp(&(&b.family, &b.stage)));
features.failures = failures;
let unmeasured = unmeasured_faces(ctx.part);
if !unmeasured.is_empty() {
for faces in features.defining.values_mut().flatten() {
faces.retain(|f| !unmeasured.contains(f));
}
}
duplicate_references::mark(ctx.part, &mut features);
features
}
fn other_axis_levels(ctx: &Context<'_>, features: &mut Features) {
let fins: BTreeSet<usize> = features
.defining
.get("repeating_radial_profiles")
.into_iter()
.flatten()
.flatten()
.copied()
.collect();
if !fins.is_empty() {
let gone: BTreeSet<usize> = features.defining["step_levels"]
.iter()
.enumerate()
.filter(|(_, faces)| faces.iter().any(|f| fins.contains(f)))
.map(|(i, _)| i)
.collect();
if !gone.is_empty() {
let dropped: Vec<levels::FaceLevel> = gone
.iter()
.map(|&i| features.step_levels[i].clone())
.collect();
drop_rejected(&mut features.step_levels, &gone);
drop_rejected(features.defining.get_mut("step_levels").unwrap(), &gone);
drop_rejected(
features.evidence_status.get_mut("step_levels").unwrap(),
&gone,
);
for riser in &mut features.risers {
riser.body_levels.retain(|l| !dropped.contains(l));
}
}
}
let mut occupied: BTreeSet<usize> = features
.defining
.iter()
.filter(|(family, _)| !matches!(**family, "step_levels" | "risers"))
.flat_map(|(_, claims)| claims.iter().flatten().copied())
.collect();
occupied.extend(
features
.discovery_refusals
.values()
.flatten()
.flat_map(|r| r.faces.iter().copied()),
);
occupied.extend(
features
.section_recess_refusals
.iter()
.flat_map(|r| r.evidence().defining_faces().iter().copied()),
);
occupied.extend(
features
.interior_void_refusals
.iter()
.flat_map(|r| r.candidate.void_faces.iter().copied()),
);
occupied.extend(
features
.modelled_thread_refusals
.iter()
.flat_map(|r| r.faces.iter().copied()),
);
occupied.extend(crate::body::evidence(ctx.part, features).into_keys());
let taken: BTreeSet<usize> = ["step_levels", "risers"]
.iter()
.flat_map(|family| features.defining[family].iter().flatten().copied())
.collect();
let (new_levels, new_risers) = levels::other_axes(ctx, &occupied, &taken);
for (family, found) in [
(
"step_levels",
new_levels.iter().map(|o| &o.defining).collect::<Vec<_>>(),
),
("risers", new_risers.iter().map(|o| &o.defining).collect()),
] {
features.evidence_status.get_mut(family).unwrap().extend(
found
.iter()
.map(|d| evidence::strict_status(ctx.part, d, &[])),
);
features
.defining
.get_mut(family)
.unwrap()
.extend(found.into_iter().cloned());
}
features
.step_levels
.extend(new_levels.into_iter().map(|o| o.record));
features
.risers
.extend(new_risers.into_iter().map(|o| o.record));
}
pub(crate) fn floor_claims_occupied(defining: &Defining) -> BTreeSet<usize> {
defining
.iter()
.filter_map(|(&family, claims)| {
(!matches!(
family,
"step_levels"
| "risers"
| "thin_wall_bodies"
| "sheet_metal_bodies"
| "plates"
| "polygonal_stock"
| "turned_bodies"
))
.then_some(claims)
})
.flatten()
.flatten()
.copied()
.collect()
}
pub(crate) fn project_recesses(ctx: &Context<'_>, inventory: &mut Inventory) -> RecessProjection {
#[cfg(test)]
if isolation::INJECT.get() == Some(("section_recesses", "returned_error")) {
let walls = inventory.physical.defining["prismatic_pockets"][0].clone();
inventory.constituent.get_mut("prismatic_pockets").unwrap()[0]
.retain(|face| !walls.contains(face));
}
let mut failures = std::mem::take(&mut inventory.physical.failures);
let projection = isolation::guarded(
&mut failures,
"section_recesses",
"section_recess_projection",
|| {
Some(section_recess_family::section_recess_projection_in(
ctx, inventory,
))
},
);
match &projection {
Some(Ok(p)) if !p.source_refusals.is_empty() => {
inventory
.physical
.discovery_refusals
.insert("section_recess_source_refusals", p.source_refusals.clone());
}
Some(Err(error)) => failures.push(FamilyFailure {
family: "section_recesses".into(),
stage: "section_recess_projection".into(),
message: Some(error.to_string()),
blocked_by: Vec::new(),
}),
_ => {}
}
inventory.physical.failures = failures;
projection
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Inventory {
pub physical: Features,
pub evidence: reconcile::Evidence,
pub dispositions: Vec<reconcile::Disposition>,
pub constituent: Defining,
}
impl Inventory {
internal_entry! {
pub fn accepted(self) -> Features {
let mut f = self.physical;
let mut rejected: BTreeMap<reconcile::Family, BTreeSet<usize>> = BTreeMap::new();
for d in &self.dispositions {
if d.outcome == reconcile::Outcome::Rejected {
rejected
.entry(d.candidate.family)
.or_default()
.insert(d.candidate.index);
}
}
for (family, indices) in &rejected {
let field = family.field().unwrap_or("risers");
let statuses = f
.evidence_status
.get_mut(field)
.expect("every candidate has a status");
drop_rejected(statuses, indices);
use reconcile::Family as F;
match family {
F::AngledSteps => drop_rejected(&mut f.angled_steps, indices),
F::Blends => drop_rejected(&mut f.blends, indices),
F::Bosses => drop_rejected(&mut f.bosses, indices),
F::Chamfers => drop_rejected(&mut f.chamfers, indices),
F::CircularBlindSteps => drop_rejected(&mut f.circular_blind_steps, indices),
F::DoubleDBores => drop_rejected(&mut f.double_d_bores, indices),
F::EdgeOpenCircularPockets => {
drop_rejected(&mut f.edge_open_circular_pockets, indices)
}
F::Fillets => drop_rejected(&mut f.fillets, indices),
F::Grooves => drop_rejected(&mut f.grooves, indices),
F::Holes => drop_rejected(&mut f.holes, indices),
F::OrientedSlots => drop_rejected(&mut f.oriented_slots, indices),
F::Passages => drop_rejected(&mut f.section_passages, indices),
F::Plates => drop_rejected(&mut f.plates, indices),
F::Pockets => drop_rejected(&mut f.pockets, indices),
F::PrismaticPockets => drop_rejected(&mut f.prismatic_pockets, indices),
F::RectangularBlindSlots => drop_rejected(&mut f.rectangular_blind_slots, indices),
F::Risers => {
drop_rejected(&mut f.risers, indices);
let defining = f.defining.get_mut("risers").expect("risers are carried");
drop_rejected(defining, indices);
continue;
}
F::Slots => drop_rejected(&mut f.slots, indices),
F::ThinWallBodies => drop_rejected(&mut f.thin_wall_bodies, indices),
F::TurnedSteps => drop_rejected(&mut f.turned_steps, indices),
}
let field = family.field().expect("a carried family has a field");
let defining = f
.defining
.get_mut(field)
.expect("every family has defining faces");
drop_rejected(defining, indices);
}
if let Some(plates) = rejected.get(&reconcile::Family::Plates)
&& let Some(defining) = f.defining.get_mut("rectangular_through_notches")
{
let gone: BTreeSet<usize> = f
.rectangular_through_notches
.iter()
.enumerate()
.filter(|(_, n)| plates.contains(&n.plate))
.map(|(i, _)| i)
.collect();
drop_rejected(&mut f.rectangular_through_notches, &gone);
drop_rejected(defining, &gone);
if let Some(statuses) = f.evidence_status.get_mut("rectangular_through_notches") {
drop_rejected(statuses, &gone);
}
}
f.hole_patterns = isolation::guarded(&mut f.failures, "hole_patterns", "discover", || hole_patterns::recognise_hole_patterns(&f.holes));
f.gusset_rib_patterns = isolation::guarded(&mut f.failures, "gusset_rib_patterns", "discover", || gussets::recognise_gusset_rib_patterns(&f.gusset_ribs));
f.slot_patterns = isolation::guarded(&mut f.failures, "slot_patterns", "discover", || recess_patterns::recognise_slot_patterns(&f.slots));
f.pocket_patterns = isolation::guarded(&mut f.failures, "pocket_patterns", "discover", || recess_patterns::recognise_pocket_patterns(&f.pockets));
f.oriented_slot_patterns =
isolation::guarded(&mut f.failures, "oriented_slot_patterns", "discover", || oriented_slots::recognise_oriented_slot_patterns(&f.oriented_slots));
f.failures.sort_by(|a, b| (&a.family, &a.stage).cmp(&(&b.family, &b.stage)));
f
}
}
}
fn drop_rejected<T>(items: &mut Vec<T>, indices: &BTreeSet<usize>) {
let mut at = 0;
items.retain(|_| {
let keep = !indices.contains(&at);
at += 1;
keep
});
}
internal_entry! {
pub fn inventory(part: &Part) -> Result<Inventory, reconcile::ReconcileError> {
take_inventory(part, |_, inventory| inventory)
}
}
pub(crate) fn take_inventory<'p, T>(
part: &'p Part,
then: impl FnOnce(&Context<'p>, Inventory) -> T,
) -> Result<T, reconcile::ReconcileError> {
if !evidence::local_degradation() {
let ctx = Context::new(part);
if let Some(inventory) = inventory_in(&ctx)? {
return Ok(then(&ctx, inventory));
}
}
evidence::locally_degraded(|| {
let ctx = Context::new(part);
let inventory = inventory_in(&ctx)?.expect("a locally degraded run does not retry");
Ok(then(&ctx, inventory))
})
}
fn degraded_skip<R>(
found: Vec<Occurrence<R>>,
proved: impl Fn(&Occurrence<R>) -> bool,
) -> Vec<Occurrence<R>> {
if !evidence::local_degradation() {
return found;
}
found.into_iter().filter(|o| proved(o)).collect()
}
fn skip_unproved<R>(part: &Part, found: Vec<Occurrence<R>>) -> Vec<Occurrence<R>> {
degraded_skip(found, |o| evidence::locally_proved(part, o))
}
pub(crate) fn inventory_in(
ctx: &Context<'_>,
) -> Result<Option<Inventory>, reconcile::ReconcileError> {
inventory_in_with(ctx, Vec::new())
}
fn inventory_in_with(
ctx: &Context<'_>,
mut failures: Vec<FamilyFailure>,
) -> Result<Option<Inventory>, reconcile::ReconcileError> {
isolation::install_hook();
macro_rules! discover {
($family:literal, $value:expr) => {
isolation::guarded(&mut failures, $family, "discover", || $value)
};
}
let part = ctx.part;
let seats = discover!("countersinks", countersinks::discover(ctx));
let (found_holes, retry) = discover!("holes", {
let found = holes::discover(ctx, &seats);
let retry =
!evidence::local_degradation() && holes::strict_refusal_retries(part, &seats, &found);
(skip_unproved(part, found), retry)
});
if retry {
return Ok(None);
}
let unmeasured = unmeasured_faces(part);
let mut defining = Kept {
part,
unmeasured: unmeasured.iter().copied().collect(),
claimed: Defining::new(),
defining: Defining::new(),
constituent: Defining::new(),
evidence_status: BTreeMap::new(),
};
let found_springs = discover!("helical_springs", helical_springs::discover(ctx));
let helical_springs = if found_springs.is_empty() {
Vec::new()
} else {
kept(&mut defining, "helical_springs", found_springs)
};
let holes = kept(&mut defining, "holes", found_holes);
let countersinks = kept(&mut defining, "countersinks", seats);
let gusset_ribs = kept(
&mut defining,
"gusset_ribs",
discover!("gusset_ribs", gussets::discover(ctx)),
);
let slots = kept(
&mut defining,
"slots",
discover!("slots", skip_unproved(part, slots::discover(ctx))),
);
let pocket_occurrences = discover!("pockets", skip_unproved(part, pockets::discover(ctx)));
let thin_wall_bodies = kept_shared(
&mut defining,
"thin_wall_bodies",
discover!("thin_wall_bodies", thin_walls::discover(ctx)),
);
let mut discovery_refusals = BTreeMap::new();
let (over_budget, unmeasured): (Vec<usize>, Vec<usize>) = unmeasured
.iter()
.partition(|&&face| part.integration_refused(face));
for (family, reason, faces) in [
("unmeasured_face_refusals", "unmeasured_face", unmeasured),
(
"integration_budget_refusals",
"integration_budget",
over_budget,
),
] {
if !faces.is_empty() {
discovery_refusals.insert(
family,
faces
.into_iter()
.map(|face| discovery::DiscoveryRefusal::new(reason, [face]))
.collect(),
);
}
}
let (passages, compatibility) =
isolation::guarded(&mut failures, "section_passages", "discover", || {
Some((|| {
let passages = match passages::discover(ctx) {
Ok(found) => found,
Err(error) => {
let faces = passages::proposal_faces(ctx);
discovery_refusals.insert(
"section_passage_refusals",
vec![discovery::DiscoveryRefusal::new(error, faces)],
);
Vec::new()
}
};
let compatibility = if discovery_refusals.contains_key("section_passage_refusals") {
Vec::new()
} else {
passages::compatibility_views(ctx)?
};
Ok::<_, reconcile::ReconcileError>((passages, compatibility))
})())
})
.unwrap_or(Ok(Default::default()))?;
let evidence = reconcile::Evidence::with_passage_views(ctx, &pocket_occurrences, compatibility);
let pockets = kept(&mut defining, "pockets", pocket_occurrences);
let oriented_slots = kept(
&mut defining,
"oriented_slots",
discover!(
"oriented_slots",
match oriented_slots::discover(ctx, &passages) {
Ok(found) => found,
Err(error) => {
discovery_refusals.insert(
"oriented_slot_refusals",
vec![discovery::DiscoveryRefusal::new(
error,
passages
.iter()
.flat_map(|p| p.defining.iter().chain(&p.context))
.copied(),
)],
);
Vec::new()
}
}
),
);
for (family, refusals) in [
("section_passages", "section_passage_refusals"),
("oriented_slots", "oriented_slot_refusals"),
] {
if failures.iter().any(|failure| failure.family == family) {
discovery_refusals.remove(refusals);
}
}
let empty_threads = modelled_threads::ThreadAnalysis::default();
let thread_analysis =
discover!("modelled_threads", Some(ctx.modelled_threads())).unwrap_or(&empty_threads);
let claimed: BTreeSet<_> = thread_analysis
.threads
.iter()
.flat_map(|o| o.defining.iter().copied())
.collect();
let modelled_threads = if thread_analysis.threads.is_empty() {
Vec::new()
} else {
kept(
&mut defining,
"modelled_threads",
thread_analysis.threads.clone(),
)
};
let freeform_surfaces = kept_shared(
&mut defining,
"freeform_surfaces",
discover!(
"freeform_surfaces",
freeform_surfaces::discover(ctx, &thin_wall_bodies)
.into_iter()
.filter(|o| !claimed.contains(&o.record.face))
.collect()
),
);
let step_level_occurrences = discover!(
"step_levels",
degraded_skip(levels::discover_step_levels(ctx), |o| {
evidence::locally_valid_solid(part, &o.defining).is_some()
})
);
let found_risers = discover!(
"risers",
degraded_skip(levels::discover_risers(ctx, &step_level_occurrences), |o| {
evidence::locally_valid_solid(part, &o.defining).is_some()
})
);
let risers = kept(&mut defining, "risers", found_risers);
let step_levels = kept(&mut defining, "step_levels", step_level_occurrences);
let void_analysis = discover!("interior_voids", interior_voids::analyse(ctx));
let plate_occurrences = discover!("plates", plates::discover(ctx, &Default::default()));
let channel_occurrences = discover!("channels", skip_unproved(part, channels::discover(ctx)));
let notches = discover!(
"rectangular_through_notches",
through_notches::discover(ctx, &plate_occurrences, &channel_occurrences)
);
let boss_analysis = discover!(
"polygonal_bosses",
polygonal_bosses::analyse(ctx, &Default::default())
);
if !boss_analysis.refusals.is_empty() {
discovery_refusals.insert("polygonal_boss_refusals", boss_analysis.refusals);
}
let pad_analysis = discover!("pads", pads::analyse(ctx, &Default::default()));
let mut radial_faces: BTreeSet<usize> = BTreeSet::new();
let prismatic_analysis = discover!("prismatic_pockets", prismatic_pockets::analyse(ctx));
if !pad_analysis.refusals.is_empty() {
discovery_refusals.insert("pad_refusals", pad_analysis.refusals);
}
if !prismatic_analysis.refusals.is_empty() {
discovery_refusals.insert("prismatic_pocket_refusals", prismatic_analysis.refusals);
}
let floor_refusals = discover!("pockets", recess_core::floor_blend_refusals(ctx));
if !floor_refusals.is_empty() {
discovery_refusals.insert("pocket_refusals", floor_refusals);
}
let mut physical = Features {
helical_springs,
discovery_refusals,
boss_caps: BTreeMap::new(),
run_mode: if evidence::local_degradation() {
evidence::RunMode::LocallyDegraded
} else {
evidence::RunMode::Strict
},
cylindrical_profiles: Vec::new(),
chamfers: kept(
&mut defining,
"chamfers",
discover!("chamfers", ctx.reported_chamfers().to_vec()),
),
bosses: kept(
&mut defining,
"bosses",
discover!("bosses", bosses::discover(ctx)),
),
angled_steps: kept(
&mut defining,
"angled_steps",
discover!("angled_steps", angled_steps::discover(ctx)),
),
flats: kept(
&mut defining,
"flats",
discover!("flats", flats::discover(ctx)),
),
paired_ramp_steps: kept(
&mut defining,
"paired_ramp_steps",
discover!("paired_ramp_steps", paired_ramp_steps::discover(ctx)),
),
oriented_chamfers: kept(
&mut defining,
"oriented_chamfers",
discover!(
"oriented_chamfers",
ctx.reported_oriented_chamfers().to_vec()
),
),
circular_face_patterns: kept(
&mut defining,
"circular_face_patterns",
discover!(
"circular_face_patterns",
circular_face_patterns::discover(ctx)
),
),
oblique_through_steps: kept(
&mut defining,
"oblique_through_steps",
discover!(
"oblique_through_steps",
oblique_through_steps::discover(ctx)
),
),
polygonal_oblique_through_steps: {
let found = discover!(
"polygonal_oblique_through_steps",
polygonal_oblique_through_steps::discover(ctx)
);
if found.is_empty() {
Vec::new()
} else {
kept(&mut defining, "polygonal_oblique_through_steps", found)
}
},
circular_blind_steps: kept(
&mut defining,
"circular_blind_steps",
discover!("circular_blind_steps", circular_blind_steps::discover(ctx)),
),
hole_patterns: Vec::new(),
holes,
gusset_rib_patterns: Vec::new(),
gusset_ribs,
countersinks,
thin_wall_bodies,
interior_voids: kept(&mut defining, "interior_voids", void_analysis.accepted),
interior_void_refusals: void_analysis.refusals,
through_steps: kept(
&mut defining,
"through_steps",
discover!("through_steps", through_steps::discover(ctx)),
),
internal_bevels: {
let found = discover!("internal_bevels", internal_bevels::discover(ctx));
if found.is_empty() {
Vec::new()
} else {
kept(&mut defining, "internal_bevels", found)
}
},
local_steps: {
let found = discover!("local_steps", local_steps::discover(ctx));
if found.is_empty() {
Vec::new()
} else {
kept(&mut defining, "local_steps", found)
}
},
turned_steps: kept(
&mut defining,
"turned_steps",
discover!(
"turned_steps",
turned_steps::sorted_occurrences(turned_steps::discover(ctx))
),
),
grooves: kept(
&mut defining,
"grooves",
discover!("grooves", grooves::discover(ctx)),
),
annular_grooves: {
let found = discover!("annular_grooves", annular_grooves::discover(ctx));
if found.is_empty() {
Vec::new()
} else {
kept(&mut defining, "annular_grooves", found)
}
},
face_grooves: Vec::new(),
round_grooves: Vec::new(),
tapers: Vec::new(),
v_grooves: Vec::new(),
plates: kept_shared(&mut defining, "plates", plate_occurrences),
turned_bodies: {
let found = discover!("turned_bodies", turned_bodies::discover(ctx));
if found.is_empty() {
Vec::new()
} else {
kept_shared(&mut defining, "turned_bodies", found)
}
},
round_bottom_blind_slots: kept(
&mut defining,
"round_bottom_blind_slots",
discover!(
"round_bottom_blind_slots",
round_bottom_slots::discover(ctx)
),
),
semicircular_blind_slots: {
let found = discover!(
"semicircular_blind_slots",
semicircular_blind_slots::discover(ctx)
);
if found.is_empty() {
Vec::new()
} else {
kept(&mut defining, "semicircular_blind_slots", found)
}
},
rectangular_blind_slots: kept(
&mut defining,
"rectangular_blind_slots",
discover!(
"rectangular_blind_slots",
rectangular_blind_slots::discover(ctx)
),
),
double_d_bores: kept(
&mut defining,
"double_d_bores",
discover!("double_d_bores", profiled_bores::discover(ctx)),
),
edge_open_circular_pockets: kept(
&mut defining,
"edge_open_circular_pockets",
discover!(
"edge_open_circular_pockets",
skip_unproved(part, edge_open_circular::discover(ctx))
),
),
edge_open_prismatic_recesses: kept(
&mut defining,
"edge_open_prismatic_recesses",
discover!(
"edge_open_prismatic_recesses",
skip_unproved(part, edge_open_prismatic::discover(ctx))
),
),
blends: kept_shared(
&mut defining,
"blends",
discover!("blends", blends::discover(ctx)),
),
sheet_metal_bodies: kept_shared(
&mut defining,
"sheet_metal_bodies",
discover!("sheet_metal_bodies", sheet_metal::discover(ctx)),
),
slot_patterns: Vec::new(),
slots,
pocket_patterns: Vec::new(),
pockets,
v_channels: {
let found = discover!("v_channels", v_channels::discover(ctx, &passages));
if found.is_empty() {
Vec::new()
} else {
kept(&mut defining, "v_channels", found)
}
},
channels: kept(&mut defining, "channels", channel_occurrences),
rectangular_through_notches: if notches.is_empty() {
Vec::new()
} else {
kept(&mut defining, "rectangular_through_notches", notches)
},
repeating_radial_profiles: {
let found = discover!(
"repeating_radial_profiles",
repeating_profiles::discover(ctx)
);
radial_faces.extend(found.iter().flat_map(|o| o.defining.iter().copied()));
kept(&mut defining, "repeating_radial_profiles", found)
},
freeform_surfaces,
modelled_threads,
modelled_thread_refusals: thread_analysis.refusals.clone(),
polygonal_bosses: kept(&mut defining, "polygonal_bosses", boss_analysis.accepted),
polygonal_stock: kept_shared(
&mut defining,
"polygonal_stock",
discover!(
"polygonal_stock",
polygonal_bosses::discover_stock(ctx, &Default::default())
),
),
section_passages: kept(&mut defining, "section_passages", passages),
prismatic_through_cuts: Vec::new(),
prismatic_pockets: kept(
&mut defining,
"prismatic_pockets",
skip_unproved(part, prismatic_analysis.accepted),
),
oriented_slot_patterns: Vec::new(),
oriented_slots,
pads: kept(
&mut defining,
"pads",
pad_analysis
.accepted
.into_iter()
.filter(|o| !o.defining.iter().all(|f| radial_faces.contains(f)))
.collect(),
),
step_levels,
risers,
section_recesses: Vec::new(),
section_recess_refusals: Vec::new(),
section_recess_patterns: Vec::new(),
section_recess_refusal: None,
fillets: kept_shared(
&mut defining,
"fillets",
discover!(
"fillets",
skip_unproved(part, ctx.reported_fillets().to_vec())
),
),
failures,
defining: defining.defining,
evidence_status: defining.evidence_status,
};
if !physical.bosses.is_empty() {
physical.boss_caps = bosses::caps(ctx);
}
let mut evidence = evidence;
evidence.risers = physical.defining["risers"].clone();
let mut dispositions = reconcile::reconcile(&physical, &evidence)?;
let rejected: BTreeSet<_> = dispositions
.iter()
.filter(|d| d.outcome == reconcile::Outcome::Rejected)
.map(|d| {
(
d.candidate.family.field().unwrap_or("risers"),
d.candidate.index,
)
})
.collect();
let claimed: BTreeSet<_> = defining
.claimed
.iter()
.flat_map(|(family, records)| {
records
.iter()
.enumerate()
.filter(|(i, _)| !rejected.contains(&(*family, *i)))
.flat_map(|(_, faces)| faces.iter().copied())
})
.collect();
let gone: BTreeSet<_> = physical.defining["fillets"]
.iter()
.enumerate()
.filter(|(i, faces)| {
!rejected.contains(&("fillets", *i)) && faces.iter().any(|f| claimed.contains(f))
})
.map(|(i, _)| i)
.collect();
if !gone.is_empty() {
drop_rejected(&mut physical.fillets, &gone);
drop_rejected(physical.defining.get_mut("fillets").unwrap(), &gone);
drop_rejected(physical.evidence_status.get_mut("fillets").unwrap(), &gone);
drop_rejected(defining.constituent.get_mut("fillets").unwrap(), &gone);
dispositions = reconcile::reconcile(&physical, &evidence)?;
}
let mut inventory = Inventory {
physical,
evidence,
dispositions,
constituent: defining.constituent,
};
if inventory
.physical
.fillets
.iter()
.any(|f| f.side == "concave")
&& let Some(Ok(projection)) = project_recesses(ctx, &mut inventory)
{
let reserved: BTreeSet<_> = projection
.section_recesses
.iter()
.flat_map(|r| r.evidence().defining_faces().iter().copied())
.collect();
let gone: BTreeSet<_> = inventory
.physical
.fillets
.iter()
.enumerate()
.filter(|(i, f)| {
f.side == "concave"
&& inventory.physical.defining["fillets"][*i]
.iter()
.any(|f| reserved.contains(f))
})
.map(|(i, _)| i)
.collect();
if !gone.is_empty() {
drop_rejected(&mut inventory.physical.fillets, &gone);
drop_rejected(
inventory.physical.defining.get_mut("fillets").unwrap(),
&gone,
);
drop_rejected(
inventory
.physical
.evidence_status
.get_mut("fillets")
.unwrap(),
&gone,
);
drop_rejected(inventory.constituent.get_mut("fillets").unwrap(), &gone);
inventory.dispositions =
reconcile::reconcile(&inventory.physical, &inventory.evidence)?;
}
}
Ok(Some(inventory))
}
struct Kept<'p> {
part: &'p Part,
unmeasured: BTreeSet<usize>,
claimed: Defining,
defining: Defining,
constituent: Defining,
evidence_status: BTreeMap<&'static str, Vec<evidence::EvidenceStatus>>,
}
fn kept<R>(kept: &mut Kept<'_>, family: &'static str, found: Vec<Occurrence<R>>) -> Vec<R> {
let found = without_spring_claims(kept, family, found);
kept.claimed
.insert(family, found.iter().map(|o| o.defining.clone()).collect());
kept_shared(kept, family, found)
}
pub(super) fn unmeasured_faces(part: &Part) -> Vec<usize> {
(0..part.faces.len())
.filter(|&f| {
part.unresolved_faces().binary_search(&f).is_err() && part.face_moments(f).is_none()
})
.collect()
}
const MOMENT_CONTEXT_FAMILIES: &[&str] = &["blends", "helical_springs"];
fn without_spring_claims<R>(
kept: &Kept<'_>,
family: &str,
found: Vec<Occurrence<R>>,
) -> Vec<Occurrence<R>> {
let found = if kept.unmeasured.is_empty() {
found
} else {
let reads_context = MOMENT_CONTEXT_FAMILIES.contains(&family);
found
.into_iter()
.filter(|o| {
let hit = |f: &usize| kept.unmeasured.contains(f);
!(o.defining.iter().any(hit) || (reads_context && o.context.iter().any(hit)))
})
.collect()
};
if family == "helical_springs" {
return found;
}
let Some(springs) = kept.defining.get("helical_springs") else {
return found;
};
found
.into_iter()
.filter(|o| {
!springs
.iter()
.any(|faces| o.defining.iter().any(|f| faces.binary_search(f).is_ok()))
})
.collect()
}
fn kept_shared<R>(kept: &mut Kept<'_>, family: &'static str, found: Vec<Occurrence<R>>) -> Vec<R> {
let found = without_spring_claims(kept, family, found);
kept.evidence_status.insert(
family,
found
.iter()
.map(|o| evidence::strict_status(kept.part, &o.defining, &o.context))
.collect(),
);
kept.defining
.insert(family, found.iter().map(|o| o.defining.clone()).collect());
kept.constituent.insert(
family,
found
.iter()
.map(|o| {
let faces: BTreeSet<usize> = o.defining.iter().chain(&o.context).copied().collect();
faces.into_iter().collect()
})
.collect(),
);
records(found)
}
internal_entry! {
pub fn records<R>(found: Vec<Occurrence<R>>) -> Vec<R> {
found.into_iter().map(|o| o.record).collect()
}
}
#[cfg(test)]
mod unmeasured_tests {
use super::*;
#[test]
fn an_unmeasured_context_face_keeps_a_record_that_does_not_read_it() {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/unmeasured_face_closed.step");
let part = crate::read_step_file(&path).unwrap();
assert_eq!(unmeasured_faces(&part), [7]);
let kept = Kept {
part: &part,
unmeasured: BTreeSet::from([7]),
claimed: Defining::new(),
defining: Defining::new(),
constituent: Defining::new(),
evidence_status: BTreeMap::new(),
};
let found = || {
vec![
Occurrence::new("defines", vec![7], vec![2]),
Occurrence::new("consults", vec![6], vec![2, 7]),
]
};
let names = |family| -> Vec<_> {
without_spring_claims(&kept, family, found())
.into_iter()
.map(|o| o.record)
.collect()
};
assert_eq!(names("holes"), ["consults"]);
assert!(names("blends").is_empty());
}
}