Moving an occurrence places every body under it again, and each placed copy searched every revolved face for where it sits to turn it away from its seam, although its source had been turned already. On the Prusa Core One assembly that search was most of the 12.7 s a move of the top occurrence took. A body now records that its revolved faces are turned, and world placement (`place_matrix`) skips the search for such a body. Modelling moves (pattern copies, hole tools) keep `transform_matrix`, which still turns them in the copy's own frame: where a face wraps all the way round the turn it finds depends on those coordinates, and later booleans depend on it. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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<std::sync::Arc<MeshMemo>>,
|
||||
/// 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<usize> = const { std::cell::Cell::new(0) };
|
||||
/// Solids `rephase_revolved` searched on this thread.
|
||||
pub(crate) static REPHASED: std::cell::Cell<usize> = 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<Body> {
|
||||
/// `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<Body> {
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -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<crate::history::FaceProv> =
|
||||
(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())?;
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -114,6 +114,7 @@ pub fn mesh_body(positions: &[Vec3], triangles: &[[u32; 3]]) -> Result<Body> {
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -115,6 +115,7 @@ pub fn decode_native_body(bytes: &[u8]) -> Result<Body> {
|
||||
history: None,
|
||||
display_of: None,
|
||||
meshes: None,
|
||||
phased: false,
|
||||
regularization_protected: false,
|
||||
};
|
||||
metadata::apply(&mut body, saved.metadata)?;
|
||||
|
||||
@@ -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<Body> {
|
||||
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<Body> {
|
||||
transform_matrix_keeping(body, m, body.phased)
|
||||
}
|
||||
|
||||
fn transform_matrix_keeping(body: &Body, m: [[f64; 4]; 4], phased: bool) -> Result<Body> {
|
||||
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<Body> {
|
||||
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))
|
||||
})
|
||||
|
||||
@@ -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)))
|
||||
}
|
||||
|
||||
@@ -146,8 +146,9 @@ pub(crate) fn build_placed(
|
||||
edges: Option<&HashMap<mt::EdgeID, Vec<EdgeOrigin>>>,
|
||||
flip: bool,
|
||||
coarse: Option<(f64, usize)>,
|
||||
phased: bool,
|
||||
) -> Result<Body> {
|
||||
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).
|
||||
|
||||
@@ -646,6 +646,7 @@ pub(crate) fn split_edges_at(body: &Body, points: &[Vec3]) -> Result<Body> {
|
||||
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))
|
||||
|
||||
@@ -43,6 +43,7 @@ fn surface_of(shell: mt::Shell) -> Body {
|
||||
history: None,
|
||||
display_of: None,
|
||||
meshes: None,
|
||||
phased: false,
|
||||
regularization_protected: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user