diff --git a/crates/smlib/src/core/offset.rs b/crates/smlib/src/core/offset.rs index fb07417ef..3dea3dada 100644 --- a/crates/smlib/src/core/offset.rs +++ b/crates/smlib/src/core/offset.rs @@ -16,13 +16,14 @@ //! A draft tilts planes and, since tangency-labs/smlib#27, turns a cylinder about the pull into //! a cone (solidcore's `Cone::lean_cylinder`, solidcore#1545); a cone that is not a cylinder //! (smlib#53) is [`Error::Unsupported`]. A draft's tilted faces go in through solidcore's -//! `SmBrep::LocalOperation` ([`local_operation`]); the capi's check that it moved some face (`moved_faces`) needs face -//! history solidcore does not report (solidcore#1462) and is not made. +//! `SmBrep::LocalOperation` ([`local_operation`]). The capi's check that it moved some face +//! (`moved_faces`) marks the replacement surfaces; solidcore names a surface by its owning face, so +//! the mark goes on the picked faces, which it stays with through the operation (solidcore#1875). use solidcore::EntityRef; use solidcore::SolidError; use solidcore::curve::curve::Curve; -use solidcore::geom::attribute::{AttributeStore, ObjectKey}; +use solidcore::geom::attribute::{Attribute, AttributeStore, Behavior, ObjectKey, ids}; use solidcore::geom::axis2_placement::Axis2Placement; use solidcore::geom::object::{ObjectRef, Shape}; use solidcore::geom::vector3d::{Point3, Vec3}; @@ -611,6 +612,37 @@ fn drafted_cone( Ok(answer(cyl.lean_cylinder(k, slope, v0))?.ok_or(Error::Unsupported)) } +/// The capi's `Before` mark on what `SmBrep::LocalOperation` is to move: above the history's tag. +const MOVED_MARK: u64 = ids::USER_1 + 8; + +/// `Before::mark` on each replacement surface. A surface has no identity apart from its owning +/// face here, and `faces[i]`'s offset face takes `surfaces[i]`, so the mark goes on the picked face +/// and travels as the surface's does (Copy behaviour; solidcore#1875). The one difference, recorded +/// in solidcore's `topology.brep_local_operation--1875-face-mark-for-surface-mark` finding: a merge +/// keeping an unpicked face over a picked one hands that survivor the mark, where SMLib's survivor +/// keeps its own unmarked surface. +pub(super) fn mark_moved(w: &mut Brep, picked: &[FaceId]) -> solidcore::Result<()> { + for &f in picked { + w.add_attribute(EntityRef::Face(f), Attribute::long(MOVED_MARK, 0, Behavior::Copy))?; + } + Ok(()) +} + +/// `moved_faces`'s reading (`read_marks`): whether any face of `w` carries the mark, every mark +/// removed. +pub(super) fn moved_faces(w: &mut Brep) -> solidcore::Result { + let mut moved = false; + for f in w.face_list().to_vec() { + let e = EntityRef::Face(f); + let marks: Vec<_> = w.entity_attributes(e).into_iter().filter(|(_, a)| a.attribute_id() == MOVED_MARK).map(|(h, _)| h).collect(); + moved = moved || !marks.is_empty(); + for h in marks { + w.remove_attribute(e, h)?; + } + } + Ok(moved) +} + /// `smlib_draft`: its checks, the solid check, the pick, then `draft_faces`: each picked face (a /// plane parallel to the pull) gets a copy of its plane turned by `angle` about its line on the /// neutral plane, and a cylinder about the pull the cone `drafted_cone` gives @@ -705,15 +737,16 @@ pub(crate) fn draft( } surfaces.push(tilted); } - // `moved_faces(b, status(b->LocalOperation(faces, surfaces)))`. The capi marks the - // tilted surfaces first and, when SMLib reports success, answers SMLIB_ERR_NO_EFFECT if - // no face of the result lies on a marked one. solidcore surfaces carry no attributes and - // the local operation reports no face history (solidcore#1462), so that check is not - // made here: a success is the capi's answer whenever SMLib's marks survive. + // `moved_faces(b, status(b->LocalOperation(faces, surfaces)))`, its marks on the picked + // faces (`mark_moved`). + mark_moved(&mut w, &picked)?; let mut notes = Notifications::new(&mut w); if let Err(e) = called(local_operation(k, &mut w, &picked, surfaces, &mut notes.context()))? { return Ok(Err(e)); } + if !moved_faces(&mut w)? { + return Ok(Err(Error::NoEffect)); + } finish(w, true) }) } diff --git a/crates/smlib/src/core/offset_tests.rs b/crates/smlib/src/core/offset_tests.rs index 2f36deedb..dc15b57aa 100644 --- a/crates/smlib/src/core/offset_tests.rs +++ b/crates/smlib/src/core/offset_tests.rs @@ -18,7 +18,7 @@ use solidcore::geom::vector3d::Vec3; use solidcore::primitive::api_curves::create_line_segment; use solidcore::sweep::api_brep::create_linear_sweep; -use super::offset::{draft, offset_faces, offset_sheet, shell, thicken}; +use super::offset::{draft, mark_moved, moved_faces, offset_faces, offset_sheet, shell, thicken}; use super::{Body, area, bounds, counts, make_box, make_cylinder, make_sphere, run, to_arrays, volume}; use crate::export::native; use crate::{BooleanOp, Error}; @@ -575,6 +575,84 @@ fn a_solidcore_error_in_a_pick_is_an_error() { assert!(matches!(unported, Err(SolidError::Unported { .. })), "{unported:?}"); } +/// The capi's `moved_faces` reading: no mark, no move (SMLIB_ERR_NO_EFFECT); a mark is read once, +/// and gone. +#[test] +fn a_draft_with_no_marked_face_left_has_no_effect() { + let body = make_box([0.0; 3], [10.0; 3]).unwrap(); + super::with_body("marks", &body, |_, w| { + assert!(!moved_faces(w)?, "no face marked"); + let f = w.face_list()[0]; + mark_moved(w, &[f])?; + assert!(moved_faces(w)?, "a face marked"); + assert!(!moved_faces(w)?, "the mark removed"); + Ok(()) + }) + .unwrap(); +} + +/// A draft leaves no mark of its own on what it returns, on a plane or on a cylinder turned cone. +#[test] +fn a_draft_leaves_no_mark() { + let z = [0.0, 0.0, 1.0]; + let bx = make_box([0.0; 3], [20.0, 30.0, 10.0]).unwrap(); + let plane = draft(&bx, &[[10.0, 0.0, 5.0]], TOL, [0.0; 3], z, z, 0.1).unwrap(); + let cyl = make_cylinder([0.0; 3], 5.0, 10.0).unwrap(); + let cone = draft(&cyl, &[[5.0, 0.0, 5.0]], TOL, [0.0; 3], z, z, 0.1).unwrap(); + for b in [plane, cone] { + super::with_body("marks", &b, |_, w| { + assert!(!moved_faces(w)?); + Ok(()) + }) + .unwrap(); + } +} + +/// `moved_faces` on both kernels: drafts so small they leave a plane or cylinder where it was +/// still moved a face (SMLib's mark and solidcore's both stay on it), an angle that brings a wall +/// onto the plane of the wall above it leaves the two faces apart, and no draft here is +/// SMLIB_ERR_NO_EFFECT on either: the same answer, volume and counts. The C++ check holds a local +/// operation to what it promised and no body found so far trips it. +#[cfg(feature = "native")] +#[test] +fn a_draft_moves_a_face_as_on_cpp() { + use crate::sweeps::{Curve, DraftCorner}; + use crate::{Brep, ffi}; + crate::init().unwrap(); + let z = [0.0, 0.0, 1.0]; + let make = |h: ffi::Handle| Brep::made(h); + let plate = make(ffi::make_box(&[0.0; 3], &[40.0, 40.0, 10.0]).unwrap()); + let rod = make(ffi::make_cylinder(&[20.0, 20.0, -5.0], 5.0, 30.0).unwrap()); + let holed = make(ffi::ops::boolean(plate.cpp().unwrap(), rod.cpp().unwrap(), ffi::ops::BOOLEAN_SUBTRACT).unwrap()); + let pinned = make(ffi::ops::boolean(plate.cpp().unwrap(), rod.cpp().unwrap(), ffi::ops::BOOLEAN_UNION).unwrap()); + let t = 5f64.to_radians().tan(); + // A wall x = 20 up to z = 10, then a wall leaning in 5 degrees: the lower wall turned 5 + // degrees about z = 10 lies on the upper one's plane. + let p = [[0.0, 0.0, 0.0], [20.0, 0.0, 0.0], [20.0, 0.0, 10.0], [20.0 - 10.0 * t, 0.0, 20.0], [0.0, 0.0, 20.0]]; + let profile: Vec = (0..5).map(|i| Curve::Line { start: p[i], end: p[(i + 1) % 5] }).collect(); + crate::sweeps::ON_CPP.with(|c| c.set(true)); + let leaning = Brep::extrude(&profile, [0.0, 1.0, 0.0], 10.0, 0.0, DraftCorner::default(), true).unwrap(); + crate::sweeps::ON_CPP.with(|c| c.set(false)); + let five = 5f64.to_radians(); + let cases: [(&str, &Brep, [f64; 3], [f64; 3], f64); 7] = [ + ("tiny on a plane", &plate, [20.0, 0.0, 5.0], [0.0; 3], 1e-14), + ("tiny on a hole", &holed, [25.0, 20.0, 5.0], [0.0, 0.0, 5.0], 1e-14), + ("tiny on a pin", &pinned, [25.0, 20.0, 20.0], [0.0, 0.0, 5.0], -1e-14), + ("small on a hole", &holed, [25.0, 20.0, 5.0], [0.0, 0.0, 5.0], 1e-6), + ("onto the wall above", &leaning, [20.0, 5.0, 5.0], [0.0, 0.0, 10.0], five), + ("away from the wall above", &leaning, [20.0, 5.0, 5.0], [0.0, 0.0, 10.0], -five), + ("hinged far off", &plate, [20.0, 0.0, 5.0], [0.0, 0.0, 1000.0], five), + ]; + for (name, b, at, origin, angle) in cases { + let cpp = ffi::ops::draft(b.cpp().unwrap(), &[at], TOL, &origin, &z, &z, angle).unwrap(); + let (v, n) = (ffi::volume(&cpp, 1e-8).unwrap(), ffi::counts(&cpp).unwrap().map(|x| usize::try_from(x).unwrap())); + let core = draft(b.core().unwrap(), &[at], TOL, origin, z, z, angle).unwrap(); + let w = volume(&core, 1e-8).unwrap(); + assert!(rel(w, v) < 1e-9, "{name}: solidcore {w} vs C++ {v}"); + assert_eq!(counts(&core).unwrap(), n, "{name}"); + } +} + /// Face and edge tags (`Brep::tagged`) through offsets routed to solidcore, as on C++: the box is /// tagged on the C++ kernel, its solidcore body converted from that and tagged the same way, and /// the two results' histories must be the same.