SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
fix(smlib): NURBS weights compare within rounding of the coordinates' size (#11974)
Andrzej Błasik9df9d1fbdc44
diff --git a/crates/smlib/src/core/convert.rs b/crates/smlib/src/core/convert.rsindex fa2e0789cc..c2e56de575 100644--- a/crates/smlib/src/core/convert.rs+++ b/crates/smlib/src/core/convert.rs@@ -1832,7 +1832,7 @@ fn bspline_curve_from_arrays(points: &[Point3], weights: &[f64], knots: &[f64],/// `smu_CreateSmBSplineSurface_FromArrays`: control points U-major; an unclamped knot vector goes/// through `SmBSplineSurface::CreateCanonical`.-fn bspline_surface_from_arrays(+pub(super) fn bspline_surface_from_arrays(points: &[Point3],weights: &[f64],u_knots: &[f64],@@ -1958,9 +1958,10 @@ fn bspline_surface_3d(f: &In<'_>, c: &mut Cursor) -> Result<BSplineSurface> {s}-/// `SameNurbs`: equal sizes and orders, knots and weights within rounding, control points within-/// `tol`.-fn same_nurbs(a: &NurbSurface, b: &NurbSurface, tol: f64) -> bool {+/// `SameNurbs`: equal sizes and orders, knots within rounding, weights within rounding of the+/// coordinates' size (a weight is the cosine of an arc's half angle, found from the points: their+/// rounding grows with the coordinates, 4e-12 at 320), control points within `tol`.+pub(super) fn same_nurbs(a: &NurbSurface, b: &NurbSurface, tol: f64) -> bool {let (mut fa, mut fb) = (Out::new(), Out::new());if add_bspline_surface(&mut fa, a).is_err() || add_bspline_surface(&mut fb, b).is_err() {return false;@@ -1970,10 +1971,11 @@ fn same_nurbs(a: &NurbSurface, b: &NurbSurface, tol: f64) -> bool {return false;}}+ let size = fa.r(FACE_NURBS_CVS).iter().fold(1.0_f64, |m, x| m.max(x.abs()));let close = |x: &[f64], y: &[f64], t: f64| x.len() == y.len() && x.iter().zip(y).all(|(a, b)| (a - b).abs() <= t * a.abs().max(1.0));close(fa.r(FACE_NURBS_U_KNOTS), fb.r(FACE_NURBS_U_KNOTS), 1e-12)&& close(fa.r(FACE_NURBS_V_KNOTS), fb.r(FACE_NURBS_V_KNOTS), 1e-12)- && close(fa.r(FACE_NURBS_WEIGHTS), fb.r(FACE_NURBS_WEIGHTS), 1e-12)+ && close(fa.r(FACE_NURBS_WEIGHTS), fb.r(FACE_NURBS_WEIGHTS), 1e-12 * size)&& close(fa.r(FACE_NURBS_CVS), fb.r(FACE_NURBS_CVS), tol)}diff --git a/crates/smlib/src/core/convert_tests.rs b/crates/smlib/src/core/convert_tests.rsindex 847f14392b..6ab398b674 100644--- a/crates/smlib/src/core/convert_tests.rs+++ b/crates/smlib/src/core/convert_tests.rs@@ -282,3 +282,84 @@ fn shells_listed_inner_first_import_outer_first() {let v = volume(&back, 1e-9).unwrap();assert!(rel(v, SEALED_VOID_VOLUME) < 1e-9, "inner first: {v}");}++/// A half torus of minor radius 0.1 at coordinates of about 200 (a fillet round a hole): its weights,+/// found from the points, differ from the rebuilt ones by rounding of the coordinates' size, not of 1.+#[test]+fn nurbs_weights_far_from_the_origin_compare_within_their_rounding() {+ use solidcore::Vec3 as P;+ let cv = [+ 197.06,+ 33.35,+ 62.217999999999996,+ 197.06,+ 33.25,+ 62.218,+ 197.06,+ 33.25,+ 62.117999999999995,+ 191.16000000000003,+ 33.35,+ 62.218,+ 191.16000000000005,+ 33.25,+ 62.217999999999996,+ 191.26,+ 33.25,+ 62.118,+ 191.16000000000003,+ 33.35,+ 56.318,+ 191.16000000000003,+ 33.25,+ 56.318000000000005,+ 191.26000000000002,+ 33.25,+ 56.318,+ 191.16000000000003,+ 33.35,+ 50.418,+ 191.16000000000003,+ 33.25,+ 50.418,+ 191.26,+ 33.25,+ 50.518,+ 197.06,+ 33.35,+ 50.418,+ 197.06,+ 33.25,+ 50.418,+ 197.06,+ 33.25,+ 50.518,+ ];+ let pts: Vec<_> = cv.chunks(3).map(|c| P::new(c[0], c[1], c[2])).collect();+ let mut w = [+ 1.0,+ std::f64::consts::FRAC_1_SQRT_2,+ 1.0,+ std::f64::consts::FRAC_1_SQRT_2,+ 0.5000000000000001,+ std::f64::consts::FRAC_1_SQRT_2,+ 1.0,+ std::f64::consts::FRAC_1_SQRT_2,+ 1.0,+ std::f64::consts::FRAC_1_SQRT_2,+ 0.5000000000000001,+ std::f64::consts::FRAC_1_SQRT_2,+ 1.0,+ std::f64::consts::FRAC_1_SQRT_2,+ 1.0,+ ];+ let (uk, vk) = ([2.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, 4.0], [0.0, 0.0, 0.0, 1.0, 1.0, 1.0]);+ let make = |w: &[f64]| super::convert::bspline_surface_from_arrays(&pts, w, &uk, &vk, 2, 2).unwrap();+ let s = make(&w);++ // weights 4e-12 apart at coordinates of 200 are the same NURBS; 1e-9 apart are not+ w[1] += 4e-12;+ assert!(super::convert::same_nurbs(s.gw_nurb().unwrap(), make(&w).gw_nurb().unwrap(), 0.01));+ w[1] += 1e-9;+ assert!(!super::convert::same_nurbs(s.gw_nurb().unwrap(), make(&w).gw_nurb().unwrap(), 0.01));+}