From e69cbae539eb034aea4db1f79655bdc10ffd2f38 Mon Sep 17 00:00:00 2001 From: brett19 Date: Fri, 30 Oct 2026 23:26:37 -0700 Subject: [PATCH] smlib: port capi 2fc375f (split by a sheet) and 0f7b209 (extend run-off solved) to solidcore Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MNXLXkZLQ6T7TSi4wswcmk --- crates/smlib/src/core/blend.rs | 19 ++--- crates/smlib/src/core/blend_tests.rs | 17 +++- crates/smlib/src/core/split.rs | 122 ++++++++++++++++++++------- crates/smlib/src/core/split_tests.rs | 73 +++++++++++++++- crates/smlib/src/core/trim.rs | 112 ++++++++++++++++++------ crates/smlib/src/core/trim_tests.rs | 9 ++ crates/smlib/src/split.rs | 8 +- 7 files changed, 288 insertions(+), 72 deletions(-) diff --git a/crates/smlib/src/core/blend.rs b/crates/smlib/src/core/blend.rs index d2c13ab25..4cb9f69fc 100644 --- a/crates/smlib/src/core/blend.rs +++ b/crates/smlib/src/core/blend.rs @@ -28,6 +28,7 @@ use std::sync::PoisonError; use solidcore::EntityRef; use solidcore::SolidError; use solidcore::blend::fillet_executive::p1::{FilletLaw, FilletLawKind}; +use solidcore::blend::fillet_executive::p3::NoRoomReason; use solidcore::blend::fillet_executive::session::FilletSession; use solidcore::blend::fillet_geom::p1::{FilletCornerType, FilletErrorType}; use solidcore::blend::fillet_solver::p1::{FilletSurfaceGenerator, FilletSurfaceGeneratorType}; @@ -255,12 +256,6 @@ fn face_set(b: &Brep) -> solidcore::Result> { Ok(brep_p1::faces(b, None)?.into_iter().collect()) } -/// solidcore's refusal of a blend whose rail leaves the face it rolls on (its port of fork -/// fc43974, `refuse_blends_larger_than_faces`). It notes only `FilletErrorType::NoRoom` and this -/// comment, as its other blend-too-large refusals do, so the comment is all that tells it apart -/// (asked for a typed error on solidcore#960). -const RAIL_LEAVES_FACE: &str = "Fillet Error: a rail leaves the face it rolls on (the blend is larger than the faces allow)"; - /// `blend_failure`: a blend the executive found larger than its faces allow /// (`SM_FILERR_NO_ROOM`) is [`Error::NoRoom`]; a filleted vertex no corner type handles /// (`SM_FILERR_CORNER_UNSUPPORTED`, which solidcore notes under @@ -269,10 +264,12 @@ const RAIL_LEAVES_FACE: &str = "Fillet Error: a rail leaves the face it rolls on /// make is why a later step fails, so that note stands for it whatever the later step noted /// (as solidcore's own `FilletSession::do_fillet` keeps it). /// -/// Except a blend whose rail leaves its face: there the fork decides between building the -/// blend over the cliff (`SmFilletCliffOverflow.cpp`, fork 8ba4667) and refusing it, which -/// solidcore does not port, refusing every such blend. Its answer is the fork's only by chance, -/// so it is [`Error::Unsupported`]. +/// Except a blend whose rail leaves its face refused as [`NoRoomReason::RailLeavesFace`]: that +/// is solidcore without `fixes.fillet_cliff_overflow`, refusing every such blend where the fork +/// decides between building it over the cliff (`SmFilletCliffOverflow.cpp`, fork 8ba4667) and +/// refusing it. Its answer is the fork's only by chance, so it is [`Error::Unsupported`]. With +/// the cliff overflow on, solidcore makes the fork's choice, and its refusals (each with its own +/// reason) are [`Error::NoRoom`] like the fork's. fn blend_failure(s: &FilletSession<'_>, e: SolidError, corner: bool) -> Error { let info = s.error_info(); if e.is_unported() { @@ -281,7 +278,7 @@ fn blend_failure(s: &FilletSession<'_>, e: SolidError, corner: bool) -> Error { Error::CornerUnsupported } else if info.code != FilletErrorType::NoRoom as i32 { status(e) - } else if info.comment == RAIL_LEAVES_FACE { + } else if info.no_room_reason() == Some(NoRoomReason::RailLeavesFace) { Error::Unsupported } else { Error::NoRoom diff --git a/crates/smlib/src/core/blend_tests.rs b/crates/smlib/src/core/blend_tests.rs index f252b71ec..731eed94b 100644 --- a/crates/smlib/src/core/blend_tests.rs +++ b/crates/smlib/src/core/blend_tests.rs @@ -392,7 +392,7 @@ cases!( fillet_cylinder_rim, setback_corner, fillet_variable_linear, - fillet_over_a_cliff: ignore = "solidcore#960: no cliff overflow (fork 8ba4667) at 6a2e8c68f: the rail leaves its face (NoRoom), Unsupported", + fillet_over_a_cliff, fillet_too_large, chamfer_hole_rim_touching_a_face, pick_near_nothing, @@ -406,9 +406,6 @@ fn fork_only_blends_are_not_supported_yet() { let b = BLOCK.core(); // 1 to 2 and back to 1: a corner at the middle. let bent = fillet_variable(&b, TOP_FRONT, TOL, [0.0, 0.0, 10.0], 1, &[0.0, 0.0, 0.5, 1.0, 1.0], &[1.0, 2.0, 1.0]).err(); - // A round over a cliff (fork 8ba4667): solidcore refuses every rail leaving its face. - let cliff = blend(&Shape::Box([0.0; 3], [10.0; 3]).core(), Blend::Round, &[[5.0, 0.0, 10.0]], &[12.0], TOL, &[], &[], Corners::Filled).err(); - assert_eq!(cliff, Some(Error::Unsupported)); // A pick that fails still says so first. let far = chamfer_sides(&b, &[[100.0; 3]], TOL, 2.0, ChamferSecond::Distance(3.0), [0.0, 0.0, 1.0]).err(); (bent, far) @@ -417,6 +414,18 @@ fn fork_only_blends_are_not_supported_yet() { assert_eq!(r.1, Some(Error::NoMatch { point: 0 })); } +/// A cliff overflow the fork refuses too (`fixes.fillet_cliff_overflow`, solidcore#960): the +/// three edges at a 20 mm cube's corner rounded r20, their fillet's boundary on two faces' +/// surfaces, has no room, as on the fork, not "not supported". +#[test] +fn a_cliff_overflow_the_fork_refuses_has_no_room() { + let r = timed("cliff_refused", || { + let corner = [[10.0, 0.0, 0.0], [0.0, 10.0, 0.0], [0.0, 0.0, 10.0]]; + blend(&Shape::Box([0.0; 3], [20.0; 3]).core(), Blend::Round, &corner, &[20.0; 3], TOL, &[], &[], Corners::Filled).err() + }); + assert_eq!(r, Some(Error::NoRoom)); +} + /// The corner of r5 rounds at (60, 40, 20) on a 60 x 40 x 20 box, set back 8 and left open: the /// box's six faces and the three rounds (9 faces, 30 edges, 22 vertices), a sheet, and one hole /// of twelve edges (each section and face curve in two), chained end to start, each beside a face diff --git a/crates/smlib/src/core/split.rs b/crates/smlib/src/core/split.rs index 45abfb71f..d179c027a 100644 --- a/crates/smlib/src/core/split.rs +++ b/crates/smlib/src/core/split.rs @@ -1,6 +1,6 @@ //! Split Face and Split Body on solidcore: the C API's composition, ported from the fork's //! `capi/src/smlib_split.cpp` (`smlib_split_faces`, `smlib_split_body`, `cut_face`, -//! `plane_covers`, `copy_of`) and the tool surfaces of `capi/src/smlib_ssi.cpp` +//! `plane_covers`, `copy_of`, `slice`, `both_sides`, `smlib_split_body_by_sheet`) and the tool surfaces of `capi/src/smlib_ssi.cpp` //! (`smlib_surface_plane`, `_cylinder`, `_cone`, `_sphere`, `_torus`, `_nurbs`). //! //! As there, every operation works on copies of its body (`SmApiBrepCopy`). A face is cut where @@ -9,7 +9,8 @@ //! reaching over the face, `GlobalSurfaceIntersect` otherwise) and the untrimmed curves go to //! `SmBrep::MergeCurvesOnSurface` with the tool as the intersecting surface. Unlike the plane //! cutter, a failed merge fails the call. A body is split by slicing (`SM_BO_SLICE`) copies of it -//! with a one-face sheet on the tool surface, once with the surface turned over. +//! with a one-face sheet on the tool surface, once with the surface turned over; or with a sheet +//! of several faces as it is, its surfaces first turned to agree. //! //! A tool is a [`crate::ssi::Surface`]; solidcore's own surface is made from its description //! ([`Spec`]), as `smlib_ssi.cpp` makes SMLib's, over the surface's natural UV domain. @@ -39,9 +40,10 @@ use solidcore::surface::surface::Surface; use solidcore::surface::surface::p2::TsectCurveType; use solidcore::surface::torus::{Torus, TorusOptions}; use solidcore::topology::brep::p1 as brep_p1; -use solidcore::topology::brep::p2::is_manifold_solid; +use solidcore::topology::brep::p2::{is_manifold_solid, is_sheet}; use solidcore::topology::brep::p3::{new_brep, with_kernel_callbacks}; use solidcore::topology::brep::p8::create_face_from_surface; +use solidcore::topology::brep::p9::orient_sheet_surfaces; use solidcore::topology::brep::p10::merge_curves_on_surface_through_surface; use solidcore::topology::brep::planar_section::face_planar_section_curves; use solidcore::topology::brep_cache::CacheObject; @@ -380,14 +382,60 @@ pub(crate) fn split_faces(body: &Body, faces: &[usize], tools: &[&Spec]) -> Resu Ok(r.map(|(b, made)| (Body::new(b), made))) } -/// One side of `smlib_split_body`: a copy of `body` sliced by a one-face sheet on the tool's -/// surface (turned over with `flip`: SMLib's slice reads the side from the surface, not the -/// face's use). `None` when the side is empty. -fn side(k: &mut Kernel, body: &Body, spec: &Spec, flip: bool) -> solidcore::Result>> { +/// `slice`: a copy of `body` sliced by `sheet` (SMLib's slice reads the side from the surfaces, not +/// the faces' uses). `None` when nothing is left. +fn slice(k: &mut Kernel, body: &Body, sheet: Brep) -> solidcore::Result>> { let a = { let g = body.0.lock().unwrap_or_else(PoisonError::into_inner); copy_of(k, &g, 1)? }; + let options = MergeOptions::new(false, false, false, false, false); + let r = match boolean_with_options(k, a, sheet, BooleanOperation::Slice, options) { + Ok(r) => r.result, + Err(e) => return Ok(Err(status(e))), + }; + Ok(Ok((!r.face_list().is_empty()).then_some(r))) +} + +/// `both_sides`: both parts, `side` making each side's sheet (`true`: turned over, for the back). +/// A body that is no manifold solid is [`Error::NotSolid`]; a side left empty is `None` +/// (`SMLIB_ERR_NO_CUT`), and with `whole` so is a part that is no manifold solid: `SM_BO_SLICE` +/// with a sheet that only nicks a region behaves as a difference, leaving the sheet's piece as a +/// slit in the body and a lamina on the other side, which is no cut. +fn both_sides( + k: &mut Kernel, + body: &Body, + whole: bool, + mut side: impl FnMut(&mut Kernel, bool) -> solidcore::Result>, +) -> solidcore::Result>> { + let solid = { + let g = body.0.lock().unwrap_or_else(PoisonError::into_inner); + is_manifold_solid(&g)? + }; + if !solid { + return Ok(Err(Error::NotSolid)); + } + let mut parts = [None, None]; + for (part, flip) in parts.iter_mut().zip([true, false]) { + let sheet = match side(k, flip)? { + Ok(s) => s, + Err(e) => return Ok(Err(e)), + }; + *part = match slice(k, body, sheet)? { + Ok(p) => p, + Err(e) => return Ok(Err(e)), + }; + } + let [Some(back), Some(front)] = parts else { return Ok(Ok(None)) }; + if whole && !(is_manifold_solid(&back)? && is_manifold_solid(&front)?) { + return Ok(Ok(None)); + } + Ok(Ok(Some((back, front)))) +} + +/// One side of `smlib_split_body`: a one-face sheet on the tool's surface, turned over with `flip`. +fn side(k: &mut Kernel, body: &Body, spec: &Spec, flip: bool) -> solidcore::Result> { + let tolerance = body.0.lock().unwrap_or_else(PoisonError::into_inner).tolerance(); let t = match tool(k, spec)? { Ok(t) => t, Err(e) => return Ok(Err(e)), @@ -397,18 +445,13 @@ fn side(k: &mut Kernel, body: &Body, spec: &Spec, flip: bool) -> solidcore::Resu return Ok(Err(status(e))); } let mut sheet = new_brep(k, USE_DEFAULT, USE_DEFAULT)?; - sheet.set_tolerance(a.tolerance()); + sheet.set_tolerance(tolerance); let natural = surface.natural_uv_domain_with_nurb(k)?; if let Err(e) = build_faces(&mut sheet, |s, fenv| create_face_from_surface(k, s, Some(surface), &natural, fenv)) { return Ok(Err(status(e))); } sheet.refresh_cached_topology(); - let options = MergeOptions::new(false, false, false, false, false); - let r = match boolean_with_options(k, a, sheet, BooleanOperation::Slice, options) { - Ok(r) => r.result, - Err(e) => return Ok(Err(status(e))), - }; - Ok(Ok((!r.face_list().is_empty()).then_some(r))) + Ok(Ok(sheet)) } /// `smlib_split_body`: the manifold solid `body` cut in two by the tool: the part behind the @@ -418,23 +461,42 @@ pub(crate) fn split_body(body: &Body, spec: &Spec) -> Result Result> { + if std::ptr::eq(body, sheet) { + return Err(Error::Argument); + } + if body.0.is_poisoned() || sheet.0.is_poisoned() { + return Err(Error::Internal("smlib_split_body_by_sheet: a body was left unfinished by an earlier panic".to_owned())); + } + let r = run("smlib_split_body_by_sheet", |k| { + { + let g = sheet.0.lock().unwrap_or_else(PoisonError::into_inner); + if !is_sheet(k, &g)? || face >= g.face_list().len() { + return Ok(Err(Error::Argument)); + } } - let back = match side(k, body, spec, true)? { - Ok(b) => b, - Err(e) => return Ok(Err(e)), - }; - let front = match side(k, body, spec, false)? { - Ok(f) => f, - Err(e) => return Ok(Err(e)), - }; - Ok(Ok(back.zip(front))) + both_sides(k, body, true, |k, flip| { + let mut copy = { + let g = sheet.0.lock().unwrap_or_else(PoisonError::into_inner); + copy_of(k, &g, 2)? + }; + let Some(&f) = brep_p1::faces(©, None)?.get(face) else { return Ok(Err(Error::Argument)) }; + let turned = orient_sheet_surfaces(k, &mut copy, Some(f), !flip); + copy.refresh_cached_topology(); + Ok(match turned { + Ok(()) => Ok(copy), + Err(e) => Err(status(e)), + }) + }) })??; Ok(r.map(|(b, f)| (Body::new(b), Body::new(f)))) } diff --git a/crates/smlib/src/core/split_tests.rs b/crates/smlib/src/core/split_tests.rs index 32c2b0a87..ddbb38f64 100644 --- a/crates/smlib/src/core/split_tests.rs +++ b/crates/smlib/src/core/split_tests.rs @@ -7,7 +7,7 @@ //! The body is a 10 mm cube [0, 10]^3 on solidcore; the tools are described as //! [`crate::ssi::Surface`] describes them. -use super::split::{split_body, split_faces}; +use super::split::{split_body, split_body_by_sheet, split_faces}; use super::{Body, area, counts, make_box, to_arrays, volume}; use crate::Error; use crate::export::native; @@ -264,6 +264,77 @@ fn a_sheet_is_not_split_into_two_bodies() { assert_eq!(split_body(&sheet, &plane_x(5.0)).err(), Some(Error::NotSolid)); } +/// Faces of the box -20..5 x -20..20 x -20..5 copied off it into a sheet (`CopyFaces` with merge, +/// as the capi's test): the top z = 5 and, with `side`, the side x = 5 too. +fn lid(side: bool) -> Body { + use solidcore::EntityRef; + use solidcore::geom::tol::USE_DEFAULT; + use solidcore::query::api_queries::face_internal_point; + use solidcore::topology::brep::p1::faces; + use solidcore::topology::brep::p2::ArenaNotifyState; + use solidcore::topology::brep::p3::new_brep; + use solidcore::topology::brep::p7::copy_faces; + let slab = make_box([-20.0; 3], [25.0, 40.0, 25.0]).unwrap(); + super::run("sheet", |k| { + let mut whole = super::boolean::copy_of(k, &slab.0.lock().unwrap(), 1)?; + let mut picked = Vec::new(); + for f in faces(&whole, None)? { + let p = face_internal_point(k, &mut whole, f)?; + if (p.z - 5.0).abs() < 1e-9 || (side && (p.x - 5.0).abs() < 1e-9) { + picked.push(f); + } + } + assert_eq!(picked.len(), if side { 2 } else { 1 }); + let mut w = new_brep(k, USE_DEFAULT, USE_DEFAULT)?; + w.set_tolerance(whole.tolerance()); + { + let mut state = ArenaNotifyState::new(&w); + let mut env = state.env(None); + let key_of = env.key_of; + let this_face = |f| key_of(EntityRef::Face(f)); + copy_faces(k, &mut whole, &picked, &mut w, true, &this_face, &mut env)?; + } + w.refresh_cached_topology(); + Ok(w) + }) + .map(Body::new) + .unwrap() +} + +/// The capi's test of `smlib_split_body_by_sheet` (fork 2fc375f): a cube 0..10 cut by an L of two +/// faces: the corner x < 5, z < 5 (250) on one side, the rest (750) on the other, whichever face +/// the sides are read from; on C++ too. +#[test] +fn split_body_by_an_l_sheet() { + let (c, sheet) = (cube(), lid(true)); + for face in 0..2 { + let (b, f) = split_body_by_sheet(&c, &sheet, face).unwrap().expect("the sheet divides the cube"); + valid("L sheet", &b); + valid("L sheet", &f); + let (vb, vf) = (volume(&b, 1e-8).unwrap(), volume(&f, 1e-8).unwrap()); + assert!(rel(vb.min(vf), 250.0) < 1e-9 && rel(vb.max(vf), 750.0) < 1e-9, "face {face}: {vb} + {vf}"); + #[cfg(feature = "native")] + { + let (body, tool) = (crate::Brep::made_core(cube()), crate::Brep::made_core(lid(true))); + let (bc, fc) = crate::ffi::split::split_body_by_sheet(body.cpp().unwrap(), tool.cpp().unwrap(), face as i64).unwrap(); + let (vbc, vfc) = (crate::ffi::volume(&bc, 1e-8).unwrap(), crate::ffi::volume(&fc, 1e-8).unwrap()); + assert!(rel(vb, vbc) < 1e-9 && rel(vf, vfc) < 1e-9, "face {face}: {vb} + {vf} vs C++ {vbc} + {vfc}"); + } + } +} + +/// What `smlib_split_body_by_sheet` refuses: a body that is no sheet, a face out of range, the +/// sheet as its own body; a face that ends inside the cube only nicks it, which is no cut. +#[test] +fn split_by_a_sheet_refuses() { + let (c, sheet) = (cube(), lid(true)); + assert_eq!(split_body_by_sheet(&c, &make_box([-20.0; 3], [25.0, 40.0, 25.0]).unwrap(), 0).err(), Some(Error::Argument)); + assert_eq!(split_body_by_sheet(&c, &sheet, 2).err(), Some(Error::Argument)); + assert_eq!(split_body_by_sheet(&c, &c, 0).err(), Some(Error::Argument)); + assert!(split_body_by_sheet(&c, &lid(false), 0).unwrap().is_none()); + assert_eq!(split_body_by_sheet(&sheet, &lid(false), 0).err(), Some(Error::NotSolid)); +} + /// The body split is never changed. #[test] fn the_body_stays_as_it_was() { diff --git a/crates/smlib/src/core/trim.rs b/crates/smlib/src/core/trim.rs index 83cf648ea..1576a5e24 100644 --- a/crates/smlib/src/core/trim.rs +++ b/crates/smlib/src/core/trim.rs @@ -657,9 +657,89 @@ fn same_patch(k: &Kernel, p: &Surface, q: &Surface, tol: f64) -> bool { }) } +/// `extended_by`: the surface `surf` (whose face is its whole box `dom`) extended past side `s` as +/// `ct` says, so that it runs `distance` past that side measured across it ([`across`]), within +/// 1e-9 of it. `CreateExtendedSurface` takes its distance as an estimate, and how far it then runs +/// is increasing in what it is asked: a bracketed solve on that (each ask scaled by the distance +/// over what it gave until two asks fall either side, then Illinois false position). An extension +/// that moves the face's side off its parameters, or a target not met, is [`Error::NotExact`], +/// never the surface as it came out. +fn extended_by(k: &Kernel, surf: &Surface, dom: &Extent2d, s: Side, ct: ContinuityType, distance: f64, model_tol: f64) -> Result { + const BRACKET_TRIES: usize = 40; + const SOLVE_TRIES: usize = 100; + let tol = 1e-9 * distance; + // How far asking `ask` runs, and the surface it makes. + let try_ask = |ask: f64| -> Result<(Surface, f64)> { + let tried = surf.create_extended_surface_dir(k, s.which, ask, ct).map_err(status)?; + // The face's side stays where it was on the extended surface's parameters. + let mid = on_side(dom, s, 0.5); + let stays = matches!((point_at(k, surf, mid), point_at(k, &tried, mid)), (Some(was), Some(now)) if (was - now).length() <= model_tol); + if !stays { + return Err(Error::NotExact); + } + let got = across(k, &tried, dom, s); + if got.is_finite() && got > 0.0 { Ok((tried, got)) } else { Err(Error::NotExact) } + }; + // Asks that run short of and past `distance`, by f = got - distance. + let (mut short, mut past) = (None, None); + let mut ask = distance; + for _ in 0..BRACKET_TRIES { + let (made, got) = try_ask(ask)?; + let f = got - distance; + if f.abs() <= tol { + return Ok(made); + } + if f < 0.0 { + short = Some((ask, f)); + } else { + past = Some((ask, f)); + } + if short.is_some() && past.is_some() { + break; + } + ask *= distance / got; + } + let (Some((mut lo, mut flo)), Some((mut hi, mut fhi))) = (short, past) else { return Err(Error::NotExact) }; + // Illinois: false position, halving the value at the end that stays when the same end moves + // twice running. + #[derive(PartialEq)] + enum Moved { + Neither, + Lo, + Hi, + } + let mut moved = Moved::Neither; + for _ in 0..SOLVE_TRIES { + let x = hi - fhi * (hi - lo) / (fhi - flo); + if !(x > lo.min(hi) && x < lo.max(hi)) { + break; + } + let (made, got) = try_ask(x)?; + let f = got - distance; + if f.abs() <= tol { + return Ok(made); + } + if f < 0.0 { + (lo, flo) = (x, f); + if moved == Moved::Lo { + fhi *= 0.5; + } + moved = Moved::Lo; + } else { + (hi, fhi) = (x, f); + if moved == Moved::Hi { + flo *= 0.5; + } + moved = Moved::Hi; + } + } + Err(Error::NotExact) +} + /// `smlib_extend_sheet`: the one-face sheet `sheet`, whose face is its surface's whole parameter /// box, continued `distance` past the side its edge nearest `point` (within `tol`) runs along, -/// as `how` says. A sheet of another shape is [`Error::Unsupported`]. +/// as `how` says. A sheet of another shape is [`Error::Unsupported`]; an extension that cannot be +/// made `distance` long across the side (within 1e-9 of it) is [`Error::NotExact`]. pub(crate) fn extend_sheet(sheet: &Body, point: [f64; 3], tol: f64, distance: f64, how: Continuation) -> Result { if !point.iter().all(|x| x.is_finite()) || !tol.is_finite() || !(tol > 0.0) || !distance.is_finite() || !(distance > 0.0) { return Err(Error::Argument); @@ -717,29 +797,13 @@ pub(crate) fn extend_sheet(sheet: &Body, point: [f64; 3], tol: f64, distance: f6 Continuation::Curvature => ContinuityType::G1G2, Continuation::Natural => ContinuityType::CInfinity, }; - // CreateExtendedSurface takes its distance as an estimate: the distance asked of it is - // corrected until the extension measures `distance` across the side's middle. - let mut extended: Option = None; - let mut failed = None; - let mut ask = distance; - for _ in 0..12 { - let tried = match surf.create_extended_surface_dir(k, grow.which, ask, ct) { - Ok(t) => t, - Err(e) => { - failed = Some(status(e)); - break; - } - }; - let mid = on_side(&dom, grow, 0.5); - let stays = matches!((point_at(k, &surf, mid), point_at(k, &tried, mid)), (Some(was), Some(now)) if (was - now).length() <= model_tol); - let got = if stays { across(k, &tried, &dom, grow) } else { -1.0 }; - extended = Some(tried); - if !stays || !(got > 0.0) || (got - distance).abs() <= 1e-9 * distance { - break; - } - ask *= distance / got; - } - let Some(extended) = extended else { return Ok(Err(failed.unwrap_or(Error::Empty))) }; + // CreateExtendedSurface takes its distance as an estimate (a natural parabola falls 2.5% + // short, a G2 run-off of a circle runs more than twice as far): the distance asked of it + // is solved for until the extension measures `distance` across the side's middle. + let extended = match extended_by(k, &surf, &dom, grow, ct, distance, model_tol) { + Ok(e) => e, + Err(e) => return Ok(Err(e)), + }; // A cone or cylinder continued naturally stays one, as the analytic widened, else SMLib // gives it back its analytic kind where it can. let grown = match how { diff --git a/crates/smlib/src/core/trim_tests.rs b/crates/smlib/src/core/trim_tests.rs index 80e55bf50..4009a86d4 100644 --- a/crates/smlib/src/core/trim_tests.rs +++ b/crates/smlib/src/core/trim_tests.rs @@ -298,6 +298,13 @@ fn extend_case(name: &str) -> (Sheet, [f64; 3], f64, Continuation, Expect) { "extend_a_cylinder_round_its_axis" => { (Sheet::QuarterCylinder, [10.0, 0.0, 5.0], 5.0, Continuation::Natural, Expect::Sheet(10.0 * (quarter + 5.0), 1)) } + // Run-offs 5 long across the surface whatever their curvature: the strip stays its section + // swept 10 along z. A circle's G2 run-off runs about 2.3 times what it is asked and more + // than doubles with it (fork 0f7b209), so the ask must be solved for. + "extend_a_cylinder_tangent_run_off" => (Sheet::QuarterCylinder, [10.0, 0.0, 5.0], 5.0, Continuation::Tangent, Expect::Sheet(10.0 * (quarter + 5.0), 1)), + "extend_a_cylinder_curvature_run_off" => { + (Sheet::QuarterCylinder, [10.0, 0.0, 5.0], 5.0, Continuation::Curvature, Expect::Sheet(10.0 * (quarter + 5.0), 1)) + } "extend_picks_near_no_edge" => (Sheet::Square, [50.0, 50.0, 0.0], 5.0, Continuation::Natural, Expect::Fails(Error::NoMatch { point: 0 })), _ => panic!("no case {name}"), } @@ -392,6 +399,8 @@ extend_cases!( extend_a_square_curvature, extend_a_cylinder_along_its_axis, extend_a_cylinder_round_its_axis, + extend_a_cylinder_tangent_run_off, + extend_a_cylinder_curvature_run_off, extend_picks_near_no_edge, ); diff --git a/crates/smlib/src/split.rs b/crates/smlib/src/split.rs index e8f5d3672..692a7b1f9 100644 --- a/crates/smlib/src/split.rs +++ b/crates/smlib/src/split.rs @@ -51,10 +51,14 @@ impl Brep { /// normal and the part in front. A `sheet` that is no sheet, or a `face` it does not have, is /// [`Error::Argument`]. pub fn split_body_by_sheet(&self, sheet: &Brep, face: usize) -> Result> { + if route::Op::Split.backend() == route::Backend::Solidcore { + return core::split::split_body_by_sheet(self.core()?, sheet.core()?, face) + .map(|r| r.map(|(b, f)| (Brep::made_core(b), Brep::made_core(f)))); + } init()?; let face = i64::try_from(face).map_err(|_| Error::Argument)?; - match ffi::split::split_body_by_sheet(&self.0, &sheet.0, face) { - Ok((b, f)) => Ok(Some((Brep(b), Brep(f)))), + match ffi::split::split_body_by_sheet(self.cpp()?, sheet.cpp()?, face) { + Ok((b, f)) => Ok(Some((Brep::made(b), Brep::made(f)))), Err(ffi::ops::ERR_NO_CUT) => Ok(None), Err(s) => Err(error(s)), }