diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index 052bf9241..82ccad82d 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -5854,7 +5854,7 @@ impl PlacedBodies { fresh.plan_mesh = placed.plan_mesh.clone(); fresh } - None => match kernel::transform_matrix(&b.body, *m) { + None => match kernel::place_matrix(&b.body, *m) { Ok(body) => { let mut fresh = ModelBody::new(name, body, b.feature); fresh.mesh_placement = placed_mesh::PlacedMesh::new(b, m).map(Arc::new); diff --git a/crates/kernel/src/body.rs b/crates/kernel/src/body.rs index 55627e628..be11a1199 100644 --- a/crates/kernel/src/body.rs +++ b/crates/kernel/src/body.rs @@ -53,6 +53,10 @@ pub struct Body { /// Meshes of this solid kept for reuse, set by [`Body::keeping_meshes`]: a check that /// intersects one large body with many small ones meshes it at the same tolerance each time. pub(crate) meshes: Option>, + /// Its revolved faces were turned away from their seams (see `rephase_revolved`), so a + /// placed copy, which keeps them where they are, need not turn them again: searching every + /// revolved face of every placed occurrence was most of the time moving an assembly took. + pub(crate) phased: bool, } /// The meshes made of one solid (see [`Body::keeping_meshes`]), by tolerance, angle and @@ -413,6 +417,8 @@ fn refined_with_cache(pm: &PolygonMesh, surf: &mt::Surface, tol: f64, memoize: b /// (same geometry) so the face sits around angle π, well away from the wrap. fn rephase_revolved(solid: Solid) -> Solid { use mt::{BoundedCurve, ParametricCurve, SearchNearestParameter}; + #[cfg(test)] + REPHASED.with(|n| n.set(n.get() + 1)); let r = guard("rephase", || { let mut changed = false; let mut shells = Vec::new(); @@ -531,6 +537,8 @@ fn polyline_mid(pts: &[Vec3]) -> (Vec3, f64) { thread_local! { /// Solids [`Body::new`] meshed on this thread to orient them (tests count the work an import does). pub(crate) static ORIENTED: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// Solids `rephase_revolved` searched on this thread. + pub(crate) static REPHASED: std::cell::Cell = const { std::cell::Cell::new(0) }; } /// Deflection the orientation mesh uses: coarse enough for a large solid, fine enough that a @@ -623,6 +631,7 @@ impl Body { history: None, display_of: None, meshes: None, + phased: true, }) } @@ -631,11 +640,12 @@ impl Body { /// unless it mirrors them, so its orientation is read from the matrix rather than meshed /// again. Only for closed solids; `coarse` is the input's cached missing-face count, which a /// rigid placement preserves exactly. - pub(crate) fn new_placed(solid: Solid, flip: bool, coarse: Option<(f64, usize)>) -> Result { + /// `phased`: the solid was placed from a body whose revolved faces are turned already. + pub(crate) fn new_placed(solid: Solid, flip: bool, coarse: Option<(f64, usize)>, phased: bool) -> Result { if solid.boundaries().is_empty() { return Err(KernelError::Failed("empty result".into())); } - Body::placed_as_is(rephase_revolved(solid), flip, coarse) + Body::placed_as_is(if phased { solid } else { rephase_revolved(solid) }, flip, coarse) } /// [`Body::new_placed`] for a solid whose revolved faces were already turned away from @@ -662,6 +672,7 @@ impl Body { history: None, display_of: None, meshes: None, + phased: true, }) } diff --git a/crates/kernel/src/fair.rs b/crates/kernel/src/fair.rs index 2c65620b1..95bbfa533 100644 --- a/crates/kernel/src/fair.rs +++ b/crates/kernel/src/fair.rs @@ -920,6 +920,7 @@ pub fn fair_faces(body: &Body, faces: &[usize], opts: &FairOptions) -> Result<(B result.volume = Some(std::sync::Arc::new(std::sync::Mutex::new(None))); result.coarse_missing = None; result.history = None; + result.phased = false; let faces: Vec = (0..all.len()).map(|i| crate::record::from_face(0, i, if picked.contains(&i) { Change::Modified } else { Change::Kept })).collect(); let result = crate::record::attach(result, &[crate::record::Operand::new(body)], Some(faces), std::collections::HashMap::new())?; diff --git a/crates/kernel/src/lib.rs b/crates/kernel/src/lib.rs index b68810477..4d01043c1 100644 --- a/crates/kernel/src/lib.rs +++ b/crates/kernel/src/lib.rs @@ -193,7 +193,7 @@ pub use native::{decode_native_body, encode_native_body, native_preview_mesh}; pub use offset::with_same_surface; pub use ops::{ BoolOp, boolean, can_join_all_touching_faces, can_join_touching_faces, compound_disjoint_extrusions, half_space, is_planar, join_apart, - split_by_plane, transform, transform_matrix, union_apart, with_attempt_budget, + place_matrix, split_by_plane, transform, transform_matrix, union_apart, with_attempt_budget, }; pub use polybool::{faceted_boolean, planar_boolean}; pub use polyhedron::{HalfSpace, convex_polyhedron, draft, offset_faces, shell}; diff --git a/crates/kernel/src/meshbody.rs b/crates/kernel/src/meshbody.rs index 170c0273c..07436cf3e 100644 --- a/crates/kernel/src/meshbody.rs +++ b/crates/kernel/src/meshbody.rs @@ -114,6 +114,7 @@ pub fn mesh_body(positions: &[Vec3], triangles: &[[u32; 3]]) -> Result { history: None, display_of: None, meshes: None, + phased: false, regularization_protected: false, }) } @@ -212,6 +213,7 @@ impl Body { history: None, display_of: None, meshes: None, + phased: false, regularization_protected: self.regularization_protected, }) } diff --git a/crates/kernel/src/native.rs b/crates/kernel/src/native.rs index 0b1af58a5..97a3bc60f 100644 --- a/crates/kernel/src/native.rs +++ b/crates/kernel/src/native.rs @@ -115,6 +115,7 @@ pub fn decode_native_body(bytes: &[u8]) -> Result { history: None, display_of: None, meshes: None, + phased: false, regularization_protected: false, }; metadata::apply(&mut body, saved.metadata)?; diff --git a/crates/kernel/src/ops.rs b/crates/kernel/src/ops.rs index 3339aa851..238ee378b 100644 --- a/crates/kernel/src/ops.rs +++ b/crates/kernel/src/ops.rs @@ -2095,6 +2095,19 @@ pub(crate) fn rigid_placement(m: &[[f64; 4]; 4]) -> bool { /// Affine transform by a column-major 4x4 matrix (rotations, translations, reflections). pub fn transform_matrix(body: &Body, m: [[f64; 4]; 4]) -> Result { + transform_matrix_keeping(body, m, false) +} + +/// [`transform_matrix`] for showing a body where an assembly places it (world placement): the +/// revolved faces stay turned the way the body has them instead of being searched again for +/// every placed copy (#3044). A modelling operation keeps using [`transform_matrix`]: where a +/// face wraps all the way round, the turn it finds depends on the copy's own coordinates, and +/// booleans on the copy depend on that. +pub fn place_matrix(body: &Body, m: [[f64; 4]; 4]) -> Result { + transform_matrix_keeping(body, m, body.phased) +} + +fn transform_matrix_keeping(body: &Body, m: [[f64; 4]; 4], phased: bool) -> Result { if m.iter().flatten().any(|x| !x.is_finite()) { return Err(KernelError::Invalid("non-finite transform".into())); } @@ -2129,6 +2142,7 @@ pub fn transform_matrix(body: &Body, m: [[f64; 4]; 4]) -> Result { edges.as_ref(), det3(&m) < 0.0, rigid_placement(&m).then_some(body.coarse_missing).flatten(), + phased, )?; Ok(placed.with_color(body.color()).with_paint(body.paint().cloned()).with_face_sources(sources).inherit_regularization_protection(body)) }) diff --git a/crates/kernel/src/outline.rs b/crates/kernel/src/outline.rs index 66c73ace4..e65d9331f 100644 --- a/crates/kernel/src/outline.rs +++ b/crates/kernel/src/outline.rs @@ -514,6 +514,7 @@ fn divided(body: &Body, faces: &[usize], other: &[(mt::Edge, Piece)], fr: &Frame regularization_protected: true, display_of: None, meshes: None, + phased: false, }; Ok(Some(record::rebuilt_body(split, &[&body.solid], &rec))) } diff --git a/crates/kernel/src/record.rs b/crates/kernel/src/record.rs index 5f6e2702c..f05de1c58 100644 --- a/crates/kernel/src/record.rs +++ b/crates/kernel/src/record.rs @@ -146,8 +146,9 @@ pub(crate) fn build_placed( edges: Option<&HashMap>>, flip: bool, coarse: Option<(f64, usize)>, + phased: bool, ) -> Result { - Ok(traced(Body::new_placed(solid, flip, coarse)?, counts, faces, edges)) + Ok(traced(Body::new_placed(solid, flip, coarse, phased)?, counts, faces, edges)) } /// The origins of `body`'s edges by identity, from its history `h` (edges in its edge order). diff --git a/crates/kernel/src/splitface.rs b/crates/kernel/src/splitface.rs index 9e5194c16..6575c2c5b 100644 --- a/crates/kernel/src/splitface.rs +++ b/crates/kernel/src/splitface.rs @@ -646,6 +646,7 @@ pub(crate) fn split_edges_at(body: &Body, points: &[Vec3]) -> Result { history: None, display_of: None, meshes: None, + phased: false, regularization_protected: true, }; Ok(crate::record::rebuilt_body(split, &[&body.solid], &rec)) @@ -1182,6 +1183,7 @@ pub(crate) fn split_any(body: &Body, faces: &[usize], tool: &SplitTool) -> Resul history: None, display_of: None, meshes: None, + phased: false, regularization_protected: true, }; Ok((crate::record::rebuilt_body(split, &[&body.solid], &imp.rec), imp)) diff --git a/crates/kernel/src/surfaces.rs b/crates/kernel/src/surfaces.rs index f47fd0d0d..87be14147 100644 --- a/crates/kernel/src/surfaces.rs +++ b/crates/kernel/src/surfaces.rs @@ -43,6 +43,7 @@ fn surface_of(shell: mt::Shell) -> Body { history: None, display_of: None, meshes: None, + phased: false, regularization_protected: false, } } diff --git a/crates/kernel/src/tests.rs b/crates/kernel/src/tests.rs index 2aaa7738e..d7546cbc5 100644 --- a/crates/kernel/src/tests.rs +++ b/crates/kernel/src/tests.rs @@ -2980,3 +2980,35 @@ fn an_attempt_budget_stops_the_boolean_and_is_shared_by_nesting() { assert!(inner && !after); assert!(boolean(&a, &b, BoolOp::Intersect).unwrap().is_some()); } + +/// Moving an assembly places every body of it again: a copy placed for showing keeps its +/// revolved faces where its source turned them, so placing searches none of them again (that search was most +/// of the time an occurrence move took on a large assembly, #3044), and the copy is the same +/// solid as placing a freshly turned one. +#[test] +fn placing_a_body_does_not_turn_its_revolved_faces_again() { + let body = cylinder(Vec3::new(1.0, 2.0, 0.0), Vec3::Z, 3.0, 5.0).unwrap(); + assert!(body.phased); + let m = [[0.0, 1.0, 0.0, 0.0], [-1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [10.0, 20.0, 30.0, 1.0]]; + crate::body::REPHASED.with(|c| c.set(0)); + let mut placed = body.clone(); + for _ in 0..10 { + placed = crate::place_matrix(&placed, m).unwrap(); + } + assert_eq!(crate::body::REPHASED.with(|c| c.get()), 0, "no placement searched the revolved faces again"); + // A modelling move (a pattern's copy, a hole's tool) still turns them in its own frame. + let _ = crate::transform_matrix(&body, m).unwrap(); + assert_eq!(crate::body::REPHASED.with(|c| c.get()), 1); + crate::body::REPHASED.with(|c| c.set(0)); + assert!(placed.phased); + let unphased = Body { phased: false, ..body.clone() }; + let mut turned = unphased; + for _ in 0..10 { + turned = crate::place_matrix(&turned, m).unwrap(); + } + assert!(crate::body::REPHASED.with(|c| c.get()) > 0, "a body not known to be turned still is"); + let (a, b) = (measure(&placed).unwrap(), measure(&turned).unwrap()); + assert!((a.volume - b.volume).abs() < 1e-9 * a.volume && (a.area - b.area).abs() < 1e-9 * a.area); + assert_eq!(a.faces, b.faces); + assert!(a.bbox.min.dist(b.bbox.min) < 1e-9 && a.bbox.max.dist(b.bbox.max) < 1e-9); +}