Assemblies: owners of bodies looked up once per placement pass, body frames only when picked (#6356)
Placing a design's bodies asked each body's owning component once per placement, and each answer searched the features for the one that made the body: placements x bodies x features on every command, twice (the world state and the frames that map a command's picks into components). Document::model_body_components answers for all bodies at once, and world_state_reusing and body_frames_with_source use it. The engine's frames now work out the per-body table only when a command picks a body or names one; most commands pick none. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -293,8 +293,9 @@ impl Document {
|
||||
/// Body frames with explicit definition names; callers must not infer them from labels.
|
||||
pub fn body_frames_with_source(&self, st: &crate::ModelState) -> Vec<(String, String, u64, Vec<u64>, Mat)> {
|
||||
let mut out = Vec::new();
|
||||
let owners = self.model_body_components(st);
|
||||
for (comp, path, m) in self.placements() {
|
||||
for b in st.bodies.iter().filter(|b| self.model_body_component(b) == comp) {
|
||||
for (b, _) in st.bodies.iter().zip(&owners).filter(|(_, owner)| **owner == comp) {
|
||||
let name = self.world_body_name(st, &b.name, comp, &path);
|
||||
let w = match self.model_body_offset(b) {
|
||||
Some(o) => mat_mul(&m, o),
|
||||
|
||||
@@ -3025,6 +3025,24 @@ impl Document {
|
||||
if body.transferred || body.source_name.is_some() { body.component } else { self.legacy_body_component(&body.name, body.feature) }
|
||||
}
|
||||
|
||||
/// [`Document::model_body_component`] of every body of `state`, in its order, with the
|
||||
/// timeline looked up once: placing a large assembly asked it of every body for every
|
||||
/// placement, each time searching the features.
|
||||
pub fn model_body_components(&self, state: &crate::ModelState) -> Vec<u64> {
|
||||
let features: std::collections::HashMap<u64, u64> = self.features.iter().rev().map(|f| (f.id, f.component)).collect();
|
||||
state
|
||||
.bodies
|
||||
.iter()
|
||||
.map(|b| {
|
||||
if b.transferred || b.source_name.is_some() {
|
||||
b.component
|
||||
} else {
|
||||
self.body_components.get(&b.name).copied().unwrap_or_else(|| features.get(&b.feature).copied().unwrap_or(0))
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The components that own a body in `state` ([`Document::model_body_component`] of each),
|
||||
/// in one pass: the timeline is looked up once, not once per body.
|
||||
pub fn body_owners(&self, state: &crate::ModelState) -> std::collections::HashSet<u64> {
|
||||
|
||||
@@ -7872,8 +7872,10 @@ pub fn world_state_reusing(doc: &Document, st: &ModelState, placed_before: &mut
|
||||
// Every body placed elsewhere than its definition, placed on all cores: a large assembly
|
||||
// places hundreds of parts, each a B-rep transform.
|
||||
let mut jobs = Vec::new();
|
||||
let owners = doc.model_body_components(st);
|
||||
let bodies_of = |comp: u64| st.bodies.iter().zip(&owners).filter(move |(_, owner)| **owner == comp).map(|(b, _)| b);
|
||||
for (comp, path, m) in &placements {
|
||||
for b in st.bodies.iter().filter(|b| doc.model_body_component(b) == *comp) {
|
||||
for b in bodies_of(*comp) {
|
||||
let bm = body_matrix(b, m);
|
||||
if !crate::is_identity(&bm) {
|
||||
jobs.push((b, bm, doc.world_body_name(st, &b.name, *comp, path)));
|
||||
@@ -7884,7 +7886,7 @@ pub fn world_state_reusing(doc: &Document, st: &ModelState, placed_before: &mut
|
||||
for (comp, path, m) in placements {
|
||||
let primary = path == first_path(comp);
|
||||
let ident = crate::is_identity(&m);
|
||||
for b in st.bodies.iter().filter(|b| doc.model_body_component(b) == comp) {
|
||||
for b in bodies_of(comp) {
|
||||
let bm = body_matrix(b, &m);
|
||||
// Identical geometry also shares its display mesh cell with the definition.
|
||||
// A changed placement keeps a distinct mesh, at that placement's coordinates.
|
||||
|
||||
@@ -273,5 +273,8 @@ fn body_owners_match_each_bodys_component() {
|
||||
let each: std::collections::HashSet<u64> = st.bodies.iter().map(|b| s.doc.model_body_component(b)).collect();
|
||||
assert_eq!(s.doc.body_owners(&st), each);
|
||||
assert!(each.len() > 1);
|
||||
// The same answers body by body, in the state's order.
|
||||
let in_order: Vec<u64> = st.bodies.iter().map(|b| s.doc.model_body_component(b)).collect();
|
||||
assert_eq!(s.doc.model_body_components(&st), in_order);
|
||||
}
|
||||
}
|
||||
|
||||
+19
-10
@@ -92,20 +92,29 @@ fn table(id: &str) -> &'static [(&'static str, K)] {
|
||||
|
||||
/// The world placement of bodies (name → frame), with moves not captured yet.
|
||||
struct Frames {
|
||||
bodies: Vec<(String, String, Mat)>,
|
||||
/// Worked out on first use: most commands pick no body, and a large assembly's frames cost
|
||||
/// a walk of every body on every command.
|
||||
bodies: std::cell::OnceCell<Vec<(String, String, Mat)>>,
|
||||
active: Mat,
|
||||
/// The frame of the sketch being edited (its component), else the active component's.
|
||||
sketch: Mat,
|
||||
}
|
||||
|
||||
fn frames(s: &Session) -> Frames {
|
||||
let st = s.model.state();
|
||||
let doc = s.placed_document();
|
||||
// World name, the component's own body name, and its frame.
|
||||
let bodies = doc.body_frames_with_source(&st).into_iter().map(|(world, own, _, _, m)| (world, own, m)).collect();
|
||||
let sketch_comp = s.active_sketch.and_then(|id| doc.feature(id)).map(|f| f.component).unwrap_or(s.active_component);
|
||||
let sketch_comp = s.active_sketch.and_then(|id| s.doc.feature(id)).map(|f| f.component).unwrap_or(s.active_component);
|
||||
let active = s.active_component_frame();
|
||||
Frames { bodies, active, sketch: if sketch_comp == s.active_component { active } else { doc.component_transform(sketch_comp) } }
|
||||
let sketch = if sketch_comp == s.active_component { active } else { s.placed_document().component_transform(sketch_comp) };
|
||||
Frames { bodies: std::cell::OnceCell::new(), active, sketch }
|
||||
}
|
||||
|
||||
impl Frames {
|
||||
/// World name, the component's own body name, and its frame, for every body.
|
||||
fn bodies(&self, s: &Session) -> &[(String, String, Mat)] {
|
||||
self.bodies.get_or_init(|| {
|
||||
let st = s.model.state();
|
||||
s.placed_document().body_frames_with_source(&st).into_iter().map(|(world, own, _, _, m)| (world, own, m)).collect()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Does world geometry or its instance naming differ from source components?
|
||||
@@ -132,14 +141,14 @@ fn pt(p: Vec3) -> Value {
|
||||
/// The frame of the body a world point lies on (the nearest), or of the named body.
|
||||
fn frame_at(s: &Session, f: &Frames, p: Vec3, body: Option<&str>) -> Option<Mat> {
|
||||
if let Some(b) = body
|
||||
&& let Some((_, _, m)) = f.bodies.iter().find(|(w, _, _)| w == b)
|
||||
&& let Some((_, _, m)) = f.bodies(s).iter().find(|(w, _, _)| w == b)
|
||||
{
|
||||
return Some(*m);
|
||||
}
|
||||
let world = s.world_state();
|
||||
let mut best: Option<(f64, Mat)> = None;
|
||||
for wb in &world.bodies {
|
||||
let Some((_, _, m)) = f.bodies.iter().find(|(w, _, _)| *w == wb.name) else { continue };
|
||||
let Some((_, _, m)) = f.bodies(s).iter().find(|(w, _, _)| *w == wb.name) else { continue };
|
||||
let mesh = wb.mesh();
|
||||
let bb = mesh.bounds();
|
||||
let tol = (bb.diagonal() * 1e-3).max(1e-3);
|
||||
@@ -279,7 +288,7 @@ impl Mapper<'_> {
|
||||
|
||||
/// The component's body name for a world (instance) body name.
|
||||
fn own_name(&mut self, world: &str) -> Option<String> {
|
||||
let own = self.f.bodies.iter().find(|(w, _, _)| w == world).map(|(_, own, _)| own.clone())?;
|
||||
let own = self.f.bodies(self.s).iter().find(|(w, _, _)| w == world).map(|(_, own, _)| own.clone())?;
|
||||
(own != world).then(|| {
|
||||
self.changed = true;
|
||||
own
|
||||
|
||||
Reference in New Issue
Block a user