diff --git a/crates/ui-egui/src/preselect_tests.rs b/crates/ui-egui/src/preselect_tests.rs index fdc5f9091..db8b42ffb 100644 --- a/crates/ui-egui/src/preselect_tests.rs +++ b/crates/ui-egui/src/preselect_tests.rs @@ -2367,3 +2367,27 @@ fn combine_takes_bodies_clicked_in_the_browser_in(layout: &str, control: bool) { assert_eq!(d.inputs[0].items, vec![Sel::Body { name: "Block".into() }], "{layout} control={control} target: {:?}", d.inputs); assert_eq!(d.inputs[1].items, vec![Sel::Body { name: "Cutter".into() }], "{layout} control={control} tools: {:?}", d.inputs); } + +/// The model's box (what every `projection` reads for the scene's depth) is kept per world state, +/// so a design with hundreds of sketches no longer samples them all again each frame, and it still +/// follows the model: a sketch added far away grows it. +#[test] +fn model_bounds_are_kept_until_the_model_changes() { + let mut app = app_with(json!([ + {"command": "sketch.create", "params": {"plane": "XY", "name": "Near"}}, + {"command": "sketch.circle.center", "params": {"center": [0, 0], "radius": 10}}, + {"command": "sketch.finish", "params": {}}, + ])); + let near = crate::viewport::model_bounds(&app); + assert!((near.max.x - 10.0).abs() < 1e-6, "{near:?}"); + assert_eq!(crate::viewport::model_bounds(&app), near, "the same state gives the same box"); + for (cmd, p) in [ + ("sketch.create", json!({"plane": "XY", "name": "Far"})), + ("sketch.circle.center", json!({"center": [500, 0], "radius": 5})), + ("sketch.finish", json!({})), + ] { + app.run(cmd, p).unwrap(); + } + let far = crate::viewport::model_bounds(&app); + assert!((far.max.x - 505.0).abs() < 1e-6, "a new sketch grows the box: {far:?}"); +} diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 2ccfce42c..4dd6d4c82 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -61,6 +61,9 @@ pub struct ViewportState { /// Per-body boxes of one world state, held (not just its address, which a later state can /// reuse once the old one is freed, so stale boxes would come back). pick_bounds: std::sync::Mutex>, + /// The sketches' box in the world of one world state (held, as `pick_bounds` is). Every + /// `projection` reads it, several times a frame, and each sketch's box samples its curves. + sketch_bounds: std::sync::Mutex, solidcraft_engine::geom::Aabb3)>>, /// The model scene's blocks of bodies by key, from the last scene built. pub(crate) blocks: std::sync::Mutex>, /// The triangles of each mesh the last scene drew as instances, by the mesh's address (the @@ -1352,7 +1355,24 @@ type BodyBounds = (std::sync::Arc, std::sync /// The model's extent: bodies (from the cached boxes) and sketches. pub fn model_bounds(app: &SolveApp) -> solidcraft_engine::geom::Aabb3 { let st = crate::open_meshing::shown_world(app); - let mut b = body_bounds(app, &st).iter().fold(solidcraft_engine::geom::Aabb3::EMPTY, |a, (_, x)| a.union(x)); + let bodies = body_bounds(app, &st).iter().fold(solidcraft_engine::geom::Aabb3::EMPTY, |a, (_, x)| a.union(x)); + let mut b = bodies.union(&sketch_bounds(app, &st)); + for point in crate::sketch_attachment_ui::guide_lines(app).into_iter().flatten() { + b.add(point); + } + b +} + +/// The box of every sketch of `st` in the world, kept for that state: a design with hundreds of +/// sketches spent most of each frame sampling their splines again for `projection`. +fn sketch_bounds(app: &SolveApp, st: &std::sync::Arc) -> solidcraft_engine::geom::Aabb3 { + if let Ok(g) = app.viewport.sketch_bounds.lock() + && let Some((k, b)) = g.as_ref() + && std::sync::Arc::ptr_eq(k, st) + { + return *b; + } + let mut b = solidcraft_engine::geom::Aabb3::EMPTY; for ss in &st.sketches { if let Some((lo, hi)) = ss.sketch.bounds() { for p in [lo, hi] { @@ -1360,8 +1380,8 @@ pub fn model_bounds(app: &SolveApp) -> solidcraft_engine::geom::Aabb3 { } } } - for point in crate::sketch_attachment_ui::guide_lines(app).into_iter().flatten() { - b.add(point); + if let Ok(mut g) = app.viewport.sketch_bounds.lock() { + *g = Some((std::sync::Arc::clone(st), b)); } b }