63d81cc096
* claim: #5248 * feat(motion): regenerate kept traces when source inputs change * feat(motion): add a parametric Chebyshev trace demo * perf(joints): reuse face measurements across motion samples Face-centred joint origins called Body::faces on every solve, bypassing the reference-mesh reuse used for rigid placements. Share face measurements with unchanged model geometry at the existing tolerance, while guarding replaced bodies and applying offsets after the cached measurements. Cover repeated named/nearest-face solves, shape edits, retained snapshots and direct replacement with deterministic work counts. The 145-point Chebyshev dimension and range updates retain identical sampled and interpolated curves. * perf(naming): skip preparation for cached face names Both face lookup paths spawned fresh workers over every body even when their name cells were ready. Prepare only cold cells, with direct handling of zero or one cold body. Preserve the existing parallel naming path and all face selection semantics. Deterministic regressions cover repeated picked and unique-name lookups, misses, partial warmup, renames, snapshots and moved copies. Release Chebyshev medians fall from 859 to 50 ms for a dimension edit and 871 to 34 ms for a sweep edit; all 145 samples and interpolated points remain identical. Full cargo xtask ci passes with RUST_TEST_THREADS=4 and the existing local Windows validation overlays.
260 lines
12 KiB
Rust
260 lines
12 KiB
Rust
//! What an evaluation costs after an edit at the end of a large design: the features before
|
|
//! the edit are reused, so the edit's cost must not grow with the bodies and imported files
|
|
//! the design already holds. A sketch on a fixed plane once moved every body of every placed
|
|
//! component into its frame, and every evaluation wrote every imported STEP file out again to
|
|
//! fingerprint it, so each sketch command took longer the more parts were inserted.
|
|
|
|
use std::cell::Cell;
|
|
use std::sync::Arc;
|
|
|
|
use solidcraft_geom::{Plane, Vec2, Vec3};
|
|
use solidcraft_kernel as kernel;
|
|
use solidcraft_sketch::Sketch;
|
|
|
|
use crate::*;
|
|
|
|
/// Work counted on this thread: bodies moved, STEP texts digested, joint faces measured.
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
|
pub(super) struct Counts {
|
|
pub moved: usize,
|
|
pub digested: usize,
|
|
pub joint_faces: usize,
|
|
}
|
|
|
|
thread_local! {
|
|
static COUNTS: Cell<Counts> = const { Cell::new(Counts { moved: 0, digested: 0, joint_faces: 0 }) };
|
|
}
|
|
|
|
pub(super) fn count(f: impl FnOnce(&mut Counts)) {
|
|
COUNTS.with(|c| {
|
|
let mut n = c.get();
|
|
f(&mut n);
|
|
c.set(n);
|
|
});
|
|
}
|
|
|
|
/// The work `f` does on this thread.
|
|
fn counted(f: impl FnOnce()) -> Counts {
|
|
COUNTS.with(|c| c.set(Counts::default()));
|
|
f();
|
|
COUNTS.with(Cell::get)
|
|
}
|
|
|
|
#[test]
|
|
fn joint_face_measurements_are_shared_across_motion_samples_and_snap_kinds() {
|
|
use crate::joints::{Joint, JointKind, JointOrigin, Snap, XRef};
|
|
let mut doc = Document::new("joint face work");
|
|
let part = doc.add_component(Some("Crank"), 0).unwrap();
|
|
let occurrence = doc.occurrences.iter().find(|o| o.component == part).unwrap().id;
|
|
let id = doc
|
|
.add_feature(
|
|
FeatureKind::Cylinder {
|
|
base: point_expr(Vec3::ZERO),
|
|
axis: Dir(Vec3::Z),
|
|
radius: "2".into(),
|
|
height: "10".into(),
|
|
operation: Operation::NewBody,
|
|
},
|
|
None,
|
|
)
|
|
.unwrap();
|
|
doc.feature_mut(id).unwrap().component = part;
|
|
let mut model = Model::new();
|
|
model.evaluate(&doc);
|
|
let state = model.state();
|
|
let pick = Vec3::new(0.0, 0.0, 10.0);
|
|
let face = joints::face_name_at(&doc, &state, part, pick, false).unwrap();
|
|
doc.assembly.joints.push(Joint {
|
|
id: 100,
|
|
name: "Pivot".into(),
|
|
kind: JointKind::Revolute,
|
|
a: JointOrigin { occurrence: 0, snap: Snap::Frame { origin: pick, z: Vec3::Z, x: Some(Vec3::X) } },
|
|
b: JointOrigin { occurrence, snap: Snap::FaceCenter { pick, face: None, x: Some(XRef::Dir(Vec3::X)) } },
|
|
values: vec![0.0],
|
|
limits: Vec::new(),
|
|
flip: true,
|
|
offset: 0.0,
|
|
angle: 0.0,
|
|
suppressed: false,
|
|
exprs: Default::default(),
|
|
});
|
|
// One body supplies both old nearest-face snaps and persistent named-face snaps.
|
|
// A scratch model shares its measurements, even though each sample changes the pose.
|
|
let snapshot = model.clone();
|
|
let work = counted(|| {
|
|
for name in [None, Some(face)] {
|
|
doc.assembly.joints[0].b.snap = Snap::FaceCenter { pick, face: name, x: Some(XRef::Dir(Vec3::X)) };
|
|
for k in 0..145 {
|
|
let angle = (k as f64 * 2.5).to_radians();
|
|
doc.assembly.joints[0].values[0] = angle;
|
|
let solution = joints::solve(&doc, &snapshot.state());
|
|
assert!(solution.errors.is_empty() && solution.conflicts.is_empty(), "{solution:?}");
|
|
let point = apply_point(&solution.world[&occurrence], Vec3::new(5.0, 0.0, 10.0));
|
|
assert!(point.dist(Vec3::new(5.0 * angle.cos(), 5.0 * angle.sin(), 10.0)) < 1e-9);
|
|
doc.apply_joint_solution(&solution);
|
|
}
|
|
}
|
|
});
|
|
assert_eq!(work.joint_faces, 1, "pose-only samples must not remeasure unchanged faces: {work:?}");
|
|
|
|
// Editing geometry creates a fresh measurement without changing a retained snapshot.
|
|
let FeatureKind::Cylinder { height, .. } = &mut doc.feature_mut(id).unwrap().kind else { panic!("cylinder") };
|
|
*height = "12".into();
|
|
model.evaluate(&doc);
|
|
let origin = &doc.assembly.joints[0].b;
|
|
let work = counted(|| {
|
|
let changed = joints::resolve_origin(&doc, &model.state(), origin).unwrap();
|
|
let retained = joints::resolve_origin(&doc, &snapshot.state(), origin).unwrap();
|
|
assert!((changed[3][2] - 12.0).abs() < 1e-9);
|
|
assert!((retained[3][2] - 10.0).abs() < 1e-9);
|
|
});
|
|
assert_eq!(work.joint_faces, 1, "only the edited body must be measured: {work:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn joint_face_measurements_never_follow_a_replaced_body_or_stale_offset() {
|
|
use crate::joints::{JointOrigin, Snap};
|
|
let mut doc = Document::new("replaced joint geometry");
|
|
let mut state = ModelState::default();
|
|
state.bodies.push(ModelBody::new("Pin".into(), kernel::cylinder(Vec3::ZERO, Vec3::Z, 2.0, 10.0).unwrap(), 0));
|
|
let origin = JointOrigin { occurrence: 0, snap: Snap::FaceCenter { pick: Vec3::new(0.0, 0.0, 10.0), face: None, x: None } };
|
|
let work = counted(|| {
|
|
joints::resolve_origin(&doc, &state, &origin).unwrap();
|
|
});
|
|
assert_eq!(work.joint_faces, 1);
|
|
let retained = state.clone();
|
|
// Body is public; a clone's old measurement must never mask a direct replacement.
|
|
state.bodies[0].body = kernel::cylinder(Vec3::ZERO, Vec3::Z, 2.0, 12.0).unwrap();
|
|
let changed = joints::resolve_origin(&doc, &state, &origin).unwrap();
|
|
assert!((changed[3][2] - 12.0).abs() < 1e-9);
|
|
let work = counted(|| {
|
|
let old = joints::resolve_origin(&doc, &retained, &origin).unwrap();
|
|
assert!((old[3][2] - 10.0).abs() < 1e-9);
|
|
doc.body_offsets.insert("Pin".into(), rigid(Vec3::new(0.0, 0.0, 1.0), Vec3::ZERO, Vec3::Z, 0.0));
|
|
let moved = joints::resolve_origin(&doc, &retained, &origin).unwrap();
|
|
assert!((moved[3][2] - 11.0).abs() < 1e-9);
|
|
});
|
|
assert_eq!(work.joint_faces, 0, "offsets are applied after the shared face measurements");
|
|
}
|
|
|
|
/// `parts` imported files of two boxes each, every one in its own component placed away from
|
|
/// the origin (as inserted parts are).
|
|
fn placed_imports(parts: usize) -> Document {
|
|
let mut doc = Document::new("placed imports");
|
|
for i in 0..parts {
|
|
let x = 30.0 * i as f64;
|
|
let a = kernel::box_solid(Vec3::new(x, 0.0, 0.0), Vec3::new(x + 10.0, 10.0, 10.0)).unwrap();
|
|
let b = kernel::box_solid(Vec3::new(x, 20.0, 0.0), Vec3::new(x + 5.0, 25.0, 5.0)).unwrap();
|
|
let step: Arc<str> = kernel::step_export(&[&a, &b], "test").unwrap().into();
|
|
let comp = doc.add_component(Some(&format!("Part {i}")), 0).unwrap();
|
|
let o = doc.occurrences.iter_mut().find(|o| o.component == comp).unwrap();
|
|
o.transform = rigid(Vec3::new(0.0, 0.0, 5.0 * i as f64), Vec3::ZERO, Vec3::Z, 0.05 * i as f64);
|
|
let kind = FeatureKind::Import { file: format!("part{i}.step"), step, components: Vec::new(), skip: Vec::new() };
|
|
let id = doc.add_feature(kind, Some(&format!("Part {i}"))).unwrap();
|
|
doc.feature_mut(id).unwrap().component = comp;
|
|
}
|
|
doc
|
|
}
|
|
|
|
fn line_sketch(plane: PlaneRef, to: Vec2) -> FeatureKind {
|
|
let mut sk = Sketch::new();
|
|
let a = sk.add_point(Vec2::new(0.0, 0.0), None).unwrap();
|
|
let b = sk.add_point(to, None).unwrap();
|
|
sk.add_line_pts(a, b, None).unwrap();
|
|
FeatureKind::Sketch { plane, sketch: sk }
|
|
}
|
|
|
|
#[test]
|
|
fn a_sketch_on_a_fixed_plane_costs_the_same_in_a_large_placed_design() {
|
|
let parts = 12;
|
|
let mut doc = placed_imports(parts);
|
|
let mut model = Model::new();
|
|
model.evaluate(&doc);
|
|
assert!(model.results.iter().all(|r| r.error.is_none()), "{:?}", model.results.iter().find(|r| r.error.is_some()));
|
|
assert_eq!(model.state().bodies.len(), parts * 2);
|
|
|
|
// A construction plane, then sketches on the origin planes, on an offset of one and on the
|
|
// construction plane, as commands make them one after another: no body is moved and no
|
|
// file digested again.
|
|
let plane = doc.add_feature(FeatureKind::ConstructionPlane { plane: PlaneRef::Origin { name: "XZ".into() } }, Some("Datum")).unwrap();
|
|
assert_eq!(counted(|| model.evaluate(&doc)), Counts::default(), "construction plane");
|
|
assert!(model.result(plane).unwrap().error.is_none());
|
|
let datum = doc.feature(plane).unwrap().name.clone();
|
|
let planes = [
|
|
PlaneRef::Origin { name: "XY".into() },
|
|
PlaneRef::Offset { base: Box::new(PlaneRef::Origin { name: "YZ".into() }), distance: "5 mm".into() },
|
|
PlaneRef::Construction { name: datum.into() },
|
|
];
|
|
for (k, plane) in planes.into_iter().enumerate() {
|
|
let id = doc.add_feature(line_sketch(plane, Vec2::new(10.0, 1.0 + k as f64)), None).unwrap();
|
|
let work = counted(|| model.evaluate(&doc));
|
|
assert_eq!(work, Counts::default(), "sketch {k}");
|
|
assert_eq!(model.last_recomputed, 1);
|
|
let r = model.result(id).unwrap();
|
|
assert!(r.error.is_none(), "{:?}", r.error);
|
|
assert_eq!(model.state().bodies.len(), parts * 2);
|
|
// Editing the sketch again recomputes it alone, for the same cost.
|
|
if let Ok(sk) = doc.sketch_mut(id)
|
|
&& let Some(p) = sk.points.get_mut(1)
|
|
{
|
|
p.pos = Vec2::new(12.0, 2.0);
|
|
}
|
|
let work = counted(|| model.evaluate(&doc));
|
|
assert_eq!(work, Counts::default(), "sketch {k} edited");
|
|
assert_eq!(model.last_recomputed, 1);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_sketch_on_a_face_still_sees_the_placed_bodies() {
|
|
let parts = 3;
|
|
let mut doc = placed_imports(parts);
|
|
let mut model = Model::new();
|
|
model.evaluate(&doc);
|
|
// A face pick reads the model as the sketch's component sees it: the other components'
|
|
// bodies are moved into its frame, as before.
|
|
let top = Plane::new(Vec3::new(0.0, 0.0, 10.0), Vec3::X, Vec3::Y).unwrap();
|
|
let face = PlaneRef::Face { plane: top, at: Vec3::new(5.0, 5.0, 10.0), name: None, anchor: None };
|
|
doc.add_feature(line_sketch(face, Vec2::new(3.0, 3.0)), None).unwrap();
|
|
let work = counted(|| model.evaluate(&doc));
|
|
assert!(work.moved >= parts * 2 - 2, "{work:?}");
|
|
assert_eq!(work.digested, 0);
|
|
}
|
|
|
|
#[test]
|
|
fn an_imported_file_is_fingerprinted_by_its_text() {
|
|
let doc = placed_imports(2);
|
|
let mut model = Model::new();
|
|
let work = counted(|| model.evaluate(&doc));
|
|
assert_eq!(work.digested, 2);
|
|
let fingerprints = |m: &Model| m.results.iter().map(|r| r.fingerprint).collect::<Vec<_>>();
|
|
let before = fingerprints(&model);
|
|
|
|
// The same text held by another copy (an undo state, a design opened again): the same
|
|
// fingerprints, and nothing evaluated again.
|
|
let mut copy = doc.clone();
|
|
for f in &mut copy.features {
|
|
if let FeatureKind::Import { step, .. } = &mut f.kind {
|
|
*step = Arc::from(step.to_string());
|
|
}
|
|
}
|
|
let work = counted(|| model.evaluate(©));
|
|
assert_eq!(work.digested, 2);
|
|
assert_eq!(fingerprints(&model), before);
|
|
assert_eq!(model.last_recomputed, 0);
|
|
|
|
// Other text: that feature and the ones after it are evaluated again.
|
|
let other = kernel::box_solid(Vec3::ZERO, Vec3::new(4.0, 4.0, 4.0)).unwrap();
|
|
let text: Arc<str> = kernel::step_export(&[&other], "test").unwrap().into();
|
|
if let Some(FeatureKind::Import { step, .. }) = copy.features.last_mut().map(|f| &mut f.kind) {
|
|
*step = text;
|
|
}
|
|
let work = counted(|| model.evaluate(©));
|
|
assert_eq!(work.digested, 1);
|
|
let after = fingerprints(&model);
|
|
assert_eq!(after.first(), before.first());
|
|
assert_ne!(after.last(), before.last());
|
|
assert_eq!(model.last_recomputed, 1);
|
|
assert_eq!(model.state().bodies.len(), 3);
|
|
}
|