perf: prism pairs before the mesh, bodies measured once, and a STEP import that copies less (#714)
A cut or an intersection between prisms (a plate and the tools through it) is now decided in 2D before either operand is meshed. A body's size and volume are measured once, a placement reads its orientation from its matrix, the evaluation cache is bounded to 64 MiB, and STEP import copies fewer shells. Hole plates evaluate 20-30% faster with less memory, and models are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a1cb8d4d36
commit
2bb24d1d2a
13 files changed
+630
-71
No files matched your search
@@ -14,6 +14,7 @@ mod parallel;
|
||||
mod split;
|
||||
|
||||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
use std::sync::Arc;
|
||||
|
||||
use mt::EuclideanSpace as _;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -31,6 +32,9 @@ const MAX_BODIES: usize = 10_000;
|
||||
/// Most product occurrences and mapped representations visited (a shared sub-assembly used
|
||||
/// twice at every level would otherwise grow exponentially).
|
||||
const MAX_VISITS: usize = 100_000;
|
||||
/// Most representation entries walked while deciding which items are converted exactly once. A
|
||||
/// larger file keeps every conversion instead of paying for the walk.
|
||||
const MAX_CLOSURE_WORK: usize = 1 << 16;
|
||||
|
||||
/// A body read from a STEP file.
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -216,6 +220,10 @@ fn rigid_inverse(m: &mt::Matrix4) -> mt::Matrix4 {
|
||||
m.invert().unwrap_or_else(mt::Matrix4::identity)
|
||||
}
|
||||
|
||||
/// One converted representation item: the truck solid, whether its shells are closed, the
|
||||
/// faces the file had, and the file face of each kernel face (band faces split into two).
|
||||
type SolidEntry = (mt::Solid, bool, usize, Vec<u64>);
|
||||
|
||||
struct Reader<'a> {
|
||||
ex: &'a Exchange,
|
||||
units: HashMap<u64, Units>,
|
||||
@@ -234,8 +242,12 @@ struct Reader<'a> {
|
||||
colors: HashMap<u64, [f32; 3]>,
|
||||
/// Opacity of styled items with a transparency (1 = opaque).
|
||||
opacity: HashMap<u64, f32>,
|
||||
/// Converted items (brep id → solid, or the error).
|
||||
solids: HashMap<u64, std::result::Result<(mt::Solid, bool, usize, Vec<u64>), String>>,
|
||||
/// Converted items (brep id → entry, or the error). Held behind `Arc` so the conversion is
|
||||
/// handed on without copying wherever the file cannot ask for the item a second time.
|
||||
solids: HashMap<u64, std::result::Result<Arc<SolidEntry>, String>>,
|
||||
/// Items the file converts exactly once (see `index`): keeping their solids would hold a
|
||||
/// second copy of the whole import for nothing.
|
||||
once: HashSet<u64>,
|
||||
used_items: HashSet<u64>,
|
||||
/// Items already instanced for the product being read.
|
||||
node_items: HashSet<u64>,
|
||||
@@ -261,6 +273,7 @@ impl<'a> Reader<'a> {
|
||||
colors: HashMap::new(),
|
||||
opacity: HashMap::new(),
|
||||
solids: HashMap::new(),
|
||||
once: HashSet::new(),
|
||||
used_items: HashSet::new(),
|
||||
node_items: HashSet::new(),
|
||||
visits: 0,
|
||||
@@ -280,11 +293,22 @@ impl<'a> Reader<'a> {
|
||||
let mut sdrs: Vec<(u64, u64)> = Vec::new(); // (PDS, rep)
|
||||
let mut cdsrs: Vec<(u64, u64)> = Vec::new(); // (relationship, PDS)
|
||||
let mut nauos: Vec<(u64, u64, u64)> = Vec::new(); // (id, parent, child)
|
||||
// Item → how many shape representations list it, and the one representation the item
|
||||
// comes from, to tell items the file converts exactly once (see `once`).
|
||||
let mut item_reps: HashMap<u64, usize> = HashMap::new();
|
||||
let mut item_rep: HashMap<u64, u64> = HashMap::new();
|
||||
// True as soon as the file can walk a product definition twice: every occurrence of a
|
||||
// component makes the reader convert the component's items again.
|
||||
let mut repeated = false;
|
||||
let mut ids: Vec<&u64> = ex.entities.keys().collect();
|
||||
ids.sort();
|
||||
for &id in ids {
|
||||
let Some(e) = ex.get(id) else { continue };
|
||||
if let Some(r) = representation(e) {
|
||||
for item in &r.0 {
|
||||
*item_reps.entry(*item).or_default() += 1;
|
||||
item_rep.entry(*item).or_insert(id);
|
||||
}
|
||||
self.reps.insert(id, r);
|
||||
}
|
||||
let p = e.params();
|
||||
@@ -301,11 +325,13 @@ impl<'a> Reader<'a> {
|
||||
}
|
||||
}
|
||||
"CONTEXT_DEPENDENT_SHAPE_REPRESENTATION" => {
|
||||
repeated = true;
|
||||
if let (Some(r), Some(d)) = (rf(0), rf(1)) {
|
||||
cdsrs.push((r, d));
|
||||
}
|
||||
}
|
||||
"NEXT_ASSEMBLY_USAGE_OCCURRENCE" | "ASSEMBLY_COMPONENT_USAGE" | "SPECIFIED_HIGHER_USAGE_OCCURRENCE" => {
|
||||
repeated = true;
|
||||
if let (Some(a), Some(b)) = (rf(3), rf(4)) {
|
||||
nauos.push((id, a, b));
|
||||
}
|
||||
@@ -344,6 +370,52 @@ impl<'a> Reader<'a> {
|
||||
self.children.entry(parent).or_default().push(id);
|
||||
}
|
||||
}
|
||||
// A file without component occurrences walks each product definition once, so a
|
||||
// representation reached from exactly one of them is walked once too. The walk follows
|
||||
// MAPPED_ITEM targets as the reader does: a mapping in one product can walk a
|
||||
// representation a second time from another product's node. An item that only one such
|
||||
// walk lists is converted exactly once and needs no cache entry; anything else keeps
|
||||
// every conversion, as before. A file too large to walk cheaply keeps every conversion
|
||||
// as well.
|
||||
if !repeated {
|
||||
let mut entries: HashMap<u64, usize> = HashMap::new();
|
||||
let mut seen: HashSet<u64> = HashSet::new();
|
||||
let mut queue: Vec<u64> = Vec::new();
|
||||
let mut work = 0usize;
|
||||
let mut within_budget = true;
|
||||
'pds: for reps in self.pd_reps.values() {
|
||||
seen.clear();
|
||||
queue.clear();
|
||||
queue.extend(self.rep_closure(reps));
|
||||
while let Some(r) = queue.pop() {
|
||||
if !seen.insert(r) {
|
||||
continue;
|
||||
}
|
||||
work += 1;
|
||||
if work > MAX_CLOSURE_WORK {
|
||||
within_budget = false;
|
||||
break 'pds;
|
||||
}
|
||||
*entries.entry(r).or_default() += 1;
|
||||
for item in self.reps.get(&r).map(|x| &x.0).into_iter().flatten() {
|
||||
queue.extend(self.mapped_targets(*item));
|
||||
}
|
||||
}
|
||||
}
|
||||
if within_budget {
|
||||
let mut once = HashSet::new();
|
||||
for (&item, &n) in &item_reps {
|
||||
if n != 1 {
|
||||
continue;
|
||||
}
|
||||
let Some(rep) = item_rep.get(&item) else { continue };
|
||||
if entries.get(rep).copied() == Some(1) {
|
||||
once.insert(item);
|
||||
}
|
||||
}
|
||||
self.once = once;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// First colour reachable from a style list (bounded search).
|
||||
@@ -448,7 +520,20 @@ impl<'a> Reader<'a> {
|
||||
seen
|
||||
}
|
||||
|
||||
fn item_solid(&mut self, item: u64, u: Units) -> Option<(mt::Solid, bool, usize, Vec<u64>)> {
|
||||
/// Representations a MAPPED_ITEM walks (empty for any other item).
|
||||
fn mapped_targets(&self, item: u64) -> Vec<u64> {
|
||||
let Some(e) = self.ex.get(item).filter(|e| e.name() == "MAPPED_ITEM") else { return Vec::new() };
|
||||
let mapped = e
|
||||
.params()
|
||||
.get(1)
|
||||
.and_then(Param::as_ref_id)
|
||||
.and_then(|map| self.ex.get(map))
|
||||
.and_then(|map| map.params().get(1))
|
||||
.and_then(Param::as_ref_id);
|
||||
mapped.map(|rep| self.rep_closure(&[rep])).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn item_solid(&mut self, item: u64, u: Units) -> Option<Arc<SolidEntry>> {
|
||||
if let Some(r) = self.solids.get(&item) {
|
||||
return r.as_ref().ok().cloned();
|
||||
}
|
||||
@@ -471,8 +556,13 @@ impl<'a> Reader<'a> {
|
||||
Err(msg)
|
||||
}
|
||||
};
|
||||
self.solids.insert(item, r.clone());
|
||||
r.ok()
|
||||
let entry = r.map(Arc::new);
|
||||
// An item the file converts exactly once hands its only reference on and keeps no copy;
|
||||
// anything it may ask for again keeps its conversion.
|
||||
if !self.once.contains(&item) {
|
||||
self.solids.insert(item, entry.clone());
|
||||
}
|
||||
entry.ok()
|
||||
}
|
||||
|
||||
fn label(&self, item: u64) -> String {
|
||||
@@ -523,7 +613,14 @@ impl<'a> Reader<'a> {
|
||||
self.warn(format!("more than {MAX_BODIES} bodies: the rest are left out"));
|
||||
return;
|
||||
}
|
||||
let Some((solid, closed, file_faces, face_origin)) = self.item_solid(item, u) else { continue };
|
||||
let Some(entry) = self.item_solid(item, u) else { continue };
|
||||
// A one-use item is the only reference to its entry, so the solid moves
|
||||
// out; an item whose conversion is cached for a later instance needs its
|
||||
// own copy.
|
||||
let (solid, closed, file_faces, face_origin) = match Arc::try_unwrap(entry) {
|
||||
Ok(entry) => entry,
|
||||
Err(shared) => (*shared).clone(),
|
||||
};
|
||||
sources.push((item, closed, file_faces, face_origin));
|
||||
solids.push(parallel::Job { solid, check_mesh: !self.unmeshed_checked.contains(&item) });
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user