diff --git a/crates/smlib/src/core.rs b/crates/smlib/src/core.rs index 0408fffe9..03c3cf973 100644 --- a/crates/smlib/src/core.rs +++ b/crates/smlib/src/core.rs @@ -49,7 +49,7 @@ pub(crate) mod trim; mod trim_tests; pub(crate) use convert::{from_arrays, to_arrays}; -pub(crate) use measure::{area, bounds, check, edge_face_distance, is_manifold_solid, mass_properties, solid_count}; +pub(crate) use measure::{area, bounds, check, classify_point, edge_face_distance, is_manifold_solid, mass_properties, solid_count}; /// A solidcore body. Its own lock: solidcore's operations take `&mut Brep`, even to measure. pub(crate) struct Body(pub(crate) Mutex); diff --git a/crates/smlib/src/core/measure.rs b/crates/smlib/src/core/measure.rs index ba21ce6da..02aeea597 100644 --- a/crates/smlib/src/core/measure.rs +++ b/crates/smlib/src/core/measure.rs @@ -7,10 +7,13 @@ use solidcore::geom::assert_array::{AssertArray, AssertReport, labels, obj_type_string}; use solidcore::geom::attribute::{AttributeStore, ObjectKey}; -use solidcore::geom::tol::{SmallTopoType, context_xsect_tol_3d}; +use solidcore::error::EntityRef; +use solidcore::geom::tol::{SmallTopoType, brep_zone_tol_3d, context_xsect_tol_3d}; +use solidcore::intersect::point_class::PointClassType; use solidcore::heal::gap::GapFunction; use solidcore::mass::integrator::properties::{MassPropertyBehavior, PropertyAttributes, brep_properties, face_properties}; use solidcore::nurbs::b_spline_surface_n_lib::p2::OrientType; +use solidcore::query::brep::BrepClassifier; use solidcore::query::api_queries::{brep_bounding_box, brep_is_manifold_solid, brep_material_census, face_bounding_box}; use solidcore::topology::NamingScheme; use solidcore::topology::brep::p1 as bl; @@ -18,9 +21,10 @@ use solidcore::topology::brep::validation::{CheckLevel as SolidcoreCheckLevel, c use solidcore::topology::face::p2::{PPF_AREA, PPF_AREA_FIRST_MOMENTS}; use solidcore::{Kernel, Point3, SolidError, Vec3}; +use super::blend::status; use super::{Body, with_body}; use crate::check::{CheckLevel, CheckReport}; -use crate::measure::MassProperties; +use crate::measure::{MassProperties, PointClass}; use crate::{Error, Result}; const OP: &str = "smlib_measure"; @@ -173,6 +177,39 @@ pub(crate) fn edge_face_distance(body: &Body, edge: usize, face: usize) -> Resul found.ok_or(Error::Argument) } +/// `smlib_brep_classify_point`: `SmBrep::Point3DClassify` at the body's own zone tolerance +/// (`SmTol::GetZoneTol3d` of the body), with boundary intersections, so a point within that +/// tolerance of a vertex, an edge or a face is on the boundary and never decided by a ray; else +/// the region it lies in, outside when that region is void (`IsVoid() == TRUE`; `UNSURE` is +/// material, as the capi compares with `TRUE`). Runs on a private copy when a solve needs one +/// (`BrepClassifier`): upstream's `const` method may add UV trim curves to the body. +pub(crate) fn classify_point(body: &Body, p: [f64; 3]) -> Result { + if !finite(&p) { + return Err(Error::Argument); + } + let class = with_body("classify_point", body, |k, b| { + let tol = brep_zone_tol_3d(k, b, SmallTopoType::NormalSize); + let mut classifier = BrepClassifier::of_brep(b); + let pc = match classifier.point_3d_classify(k, Point3::new(p[0], p[1], p[2]), tol, true) { + Ok(pc) => pc, + // Its status, as the capi passes it on (no ray finding a region is SM_ERR). + Err(e) => return Ok(Err(status(e))), + }; + Ok(match (pc.point_class(), pc.object()) { + (PointClassType::Vertex | PointClassType::Edge | PointClassType::Face, _) => Ok(PointClass::OnBoundary), + (PointClassType::Region, Some(EntityRef::Region(r))) => { + Ok(if classifier.brep().region(r)?.is_void() == 1 { PointClass::Outside } else { PointClass::Inside }) + } + // Point3DClassify reports only the four classes above on success: SM_ERR. + _ => Err(Error::Status(SM_ERR)), + }) + })?; + class +} + +/// `SM_ERR`. +const SM_ERR: i32 = 1001; + /// `smlib_brep_is_manifold_solid`: `SmApiBrepIsManifoldSolid`. pub(crate) fn is_manifold_solid(body: &Body) -> Result { with_body("is_manifold_solid", body, |_, b| brep_is_manifold_solid(b)) diff --git a/crates/smlib/src/core/measure_tests.rs b/crates/smlib/src/core/measure_tests.rs index 2241f03c5..d1311e93b 100644 --- a/crates/smlib/src/core/measure_tests.rs +++ b/crates/smlib/src/core/measure_tests.rs @@ -403,3 +403,51 @@ fn faces_apart_with_a_shell_each_check_valid() { assert!(r.is_valid(), "{level:?}: {r}"); } } + +/// `smlib_brep_classify_point` (fork 34d6b2b), the capi test's cases: a 10 x 20 x 30 box at +/// `(1, -2, 3)`: inside, outside, on a face, an edge and a vertex; the cylinder of radius 5 about +/// the z axis through `(1, 2)`: off its curved wall by 1e-8 on it (within the zone tolerance), by +/// 1e-4 inside or outside, on its axis inside, on its top's centre on it. With SMLib built, the +/// C++ kernel's own primitives classify the same. +#[test] +fn points_classify_as_the_capi() { + use super::classify_point; + use crate::measure::PointClass::{Inside, OnBoundary, Outside}; + + let (bx, _) = case("box"); + let (cyl, _) = case("cylinder"); + let c45 = std::f64::consts::FRAC_1_SQRT_2; + let wall = |r: f64| [1.0 + r * c45, 2.0 + r * c45, 3.0]; + let cases = [ + ("box", [6.0, 8.0, 18.0], Inside), + ("box", [6.0, 8.0, 34.0], Outside), + ("box", [0.0, -3.0, 2.0], Outside), + ("box", [6.0, 8.0, 33.0], OnBoundary), + ("box", [11.0, 8.0, 33.0], OnBoundary), + ("box", [11.0, 18.0, 33.0], OnBoundary), + ("box", [1.0, -2.0, 3.0], OnBoundary), + ("cylinder", wall(5.0 + 1e-8), OnBoundary), + ("cylinder", wall(5.0 - 1e-8), OnBoundary), + ("cylinder", wall(5.0 + 1e-4), Outside), + ("cylinder", wall(5.0 - 1e-4), Inside), + ("cylinder", [1.0, 2.0, 3.0], Inside), + ("cylinder", [1.0, 2.0, 9.0], OnBoundary), + ]; + for (name, p, want) in cases { + let body = if name == "box" { &bx } else { &cyl }; + assert_eq!(classify_point(body, p).unwrap(), want, "{name} {p:?}"); + #[cfg(feature = "native")] + { + use crate::ffi::measure::{POINT_INSIDE, POINT_ON_BOUNDARY, POINT_OUTSIDE, classify_point as cpp_classify}; + let theirs = cpp_classify(cpp(name).cpp().unwrap(), p).unwrap(); + let want_cpp = match want { + Inside => POINT_INSIDE, + Outside => POINT_OUTSIDE, + OnBoundary => POINT_ON_BOUNDARY, + }; + assert_eq!(theirs, want_cpp, "C++ {name} {p:?}"); + } + } + assert_eq!(classify_point(&bx, [f64::NAN, 0.0, 0.0]).unwrap_err(), crate::Error::Argument); + assert_eq!(classify_point(&bx, [0.0, f64::INFINITY, 0.0]).unwrap_err(), crate::Error::Argument); +} diff --git a/crates/smlib/src/measure.rs b/crates/smlib/src/measure.rs index e6f4ce299..e6c45cd9a 100644 --- a/crates/smlib/src/measure.rs +++ b/crates/smlib/src/measure.rs @@ -55,7 +55,10 @@ impl Brep { /// else inside a material region or outside. A body with no material (a sheet, an open shell) /// has no inside. A non-finite coordinate is [`Error::Argument`](crate::Error::Argument). pub fn classify_point(&self, p: [f64; 3]) -> Result { - match ffi::measure::classify_point(&self.0, p).map_err(error)? { + if on_core() { + return core::classify_point(self.core()?, p); + } + match ffi::measure::classify_point(self.cpp()?, p).map_err(error)? { ffi::measure::POINT_OUTSIDE => Ok(PointClass::Outside), ffi::measure::POINT_INSIDE => Ok(PointClass::Inside), ffi::measure::POINT_ON_BOUNDARY => Ok(PointClass::OnBoundary),