SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Wave 179/509: solidcraft-smlib: Brep::project_curves, curves projected onto a face as SMLib's B-splines (and 1 more)
jyviko4b94403dd673
diff --git a/crates/kernel/src/surface_ruled.rs b/crates/kernel/src/surface_ruled.rsindex 7e497ca13c..c5495e8c6e 100644--- a/crates/kernel/src/surface_ruled.rs+++ b/crates/kernel/src/surface_ruled.rs@@ -3,14 +3,17 @@//! lines from the edge in a direction set by the face beside it: along its normal, continuing//! it, at an angle from a reference direction (a draft), or along a fixed vector.//!-//! A strip is exact where the rulings move the edge by an affine map: a straight edge with a-//! constant ruling direction gives a plane, an arc whose rulings keep their angle to its axis-//! gives a plane (a ring), or a ruled NURBS surface between the edge and its image (a cylinder-//! or a cone, exactly). Any other strip is a ruled B-spline (degree 1 across) fitted within-//! [`FIT_TOLERANCE`]. Strips share their side edges where their rulings agree; at a sharp-//! corner of the chain they are mitred (each extended along its far edge until the two meet, or-//! cut back where they overlap) or rounded (a planar or conical sector SMLib revolves from one-//! ruling to the other; an inside corner is always cut back).+//! A strip is exact where the rulings move the edge by an affine map: a constant ruling direction+//! (a plane, or the ruled NURBS between the edge and its translate), or an arc whose rulings keep+//! their angle to its axis (a plane ring, or the ruled NURBS of a cylinder or a cone, exactly).+//! Rulings that turn any other way along the edge (a normal turning along a free-form face, a+//! tangent continuing a face round a free-form curve) end on an offset curve, which has no exact+//! NURBS form in general (SMLib keeps one only implicitly, `SmOffsetCurve`, or approximates it,+//! `N_CrvOffsetApprox`), so such a strip is refused, never fitted. Strips share their side edges+//! where their rulings agree; at a sharp corner of the chain they are mitred (each extended along+//! its far edge until the two meet, or cut back where they overlap) or rounded (a planar or+//! conical sector SMLib revolves from one ruling to the other; an inside corner is always cut+//! back).//!//! Every strip and corner face is built on its own on the record (each edge a NURBS curve, the//! edge's own curve as a NURBS where it stands on the body), and the faces are stitched by SMLib@@ -28,11 +31,8 @@ use crate::history::FaceOrigin;use crate::surfaces::{Parts, nearest_face, picked_chain, sheet_of, spline_curve, spline_surface, uv_line};use crate::{KernelError, Result, record};-/// How far a fitted strip may stray from the true ruled surface (mm).-const FIT_TOLERANCE: f64 = 1e-3;-/// Samples along an edge when classifying its strip, and the most a fitted strip is refined to.+/// Samples along an edge when classifying its strip.const SAMPLES: usize = 16;-const MAX_FIT_SAMPLES: usize = 513;/// Rulings closer than this (as unit vectors) agree: their strips share a side edge.const SAME_RULING: f64 = 1e-7;@@ -403,16 +403,16 @@ struct End {enum Kind {/// The plane through a point spanned by the edge's direction and the ruling (normal t × r).Plane { o: Vec3, t: Vec3, r: Vec3 },- /// The ruled NURBS surface from the base curve (v = 0) to the top (v = 1) on their shared- /// knots and weights.+ /// The ruled NURBS surface from the base curve (v = 0) to its image, the top (v = 1), on+ /// their shared knots and weights.Ruled,}/// The strip on one edge.struct Strip {- /// The edge as a NURBS curve run along the chain (or its fit, for a fitted strip).+ /// The edge as a NURBS curve run along the chain.base: CurveN<4>,- /// The far edge's curve (from the start's ruling end to the end's), on the base's knots.+ /// The far edge's curve (from the start's ruling end to the end's): the base's image.top: CurveN<4>,kind: Kind,/// A plane through a straight edge: its far edge is the line between its corners.@@ -447,13 +447,14 @@ impl Strip {plane_normal.is_some_and(|nn| samples.iter().map(|s| s.0).chain(tops.iter().copied()).all(|q| (q - first.0).dot(nn).abs() <= eps));let exact = affine(&samples, d, eps);let line = planar && base.degree == 1 && base.ctrl.len() == 2 && exact.is_some();- let (base, top) = match exact {- Some(m) => {- let top = mapped(&base, |p| m.apply(p));- (base, top)- }- None => fitted(&at, d)?,+ let Some(m) = exact else {+ return Err(KernelError::Invalid(+ "not supported yet: a ruled strip whose rulings turn along the edge other than about an arc's axis \+ (their ends trace an offset curve, which has no exact NURBS form): give a fixed direction"+ .into(),+ ));};+ let top = mapped(&base, |p| m.apply(p));let kind = if planar { Kind::Plane { o: first.0, t: first.1, r: first.3 } } else { Kind::Ruled };let tangent = |f: f64| point_tangent(&top, f).and_then(|(_, t)| t.normalized()).unwrap_or(first.1);let end = |(p, _, n, r): (Vec3, Vec3, Vec3, Vec3), tan: Vec3| End { at: p, ruling: r, top: p + r * d, tangent: tan, normal: n };@@ -681,39 +682,6 @@ fn affine(samples: &[(Vec3, Vec3, Vec3, Vec3)], d: f64, eps: f64) -> Option<AffiSome(Affine::Radial { c: centre, a: axis, scale: 1.0 + radial? / radius, shift: axis * along? })}-/// A ruled B-spline fitted to the strip (degree 1 across): cubics through matching samples of the-/// edge and of its rulings' ends (at the same parameters, so they share their knots), refined-/// until both are within [`FIT_TOLERANCE`]; the edge's fit and the far edge's.-fn fitted(at: &dyn Fn(f64) -> Result<(Vec3, Vec3, Vec3, Vec3)>, d: f64) -> Result<(CurveN<4>, CurveN<4>)> {- let fail = |e: solidcraft_geom::bspline::SplineError| KernelError::Failed(format!("ruled surface: fitting a strip: {e}"));- let lift = |c: CurveN<3>| CurveN { degree: c.degree, knots: c.knots, ctrl: c.ctrl.iter().map(|p| [p[0], p[1], p[2], 1.0]).collect() };- let mut m = SAMPLES + 1;- loop {- let params: Vec<f64> = (0..m).map(|k| k as f64 / (m - 1) as f64).collect();- let pts: Vec<(Vec3, Vec3, Vec3, Vec3)> = params.iter().map(|f| at(*f)).collect::<Result<_>>()?;- let near: Vec<[f64; 3]> = pts.iter().map(|s| [s.0.x, s.0.y, s.0.z]).collect();- let far: Vec<[f64; 3]> = pts.iter().map(|s| s.0 + s.3 * d).map(|q| [q.x, q.y, q.z]).collect();- let (base, top) = (lift(CurveN::interpolate(¶ms, &near, 3).map_err(fail)?), lift(CurveN::interpolate(¶ms, &far, 3).map_err(fail)?));- // Checked between the samples, where a fit strays most.- let mut worst = 0.0f64;- for k in 0..m - 1 {- let f = (k as f64 + 0.5) / (m - 1) as f64;- let (p, _, _, r) = at(f)?;- let (Some(q0), Some(q1)) = (point(&base, f), point(&top, f)) else {- return Err(KernelError::Failed("ruled surface: fitting a strip".into()));- };- worst = worst.max(q0.dist(p)).max(q1.dist(p + r * d));- }- if worst <= FIT_TOLERANCE {- return Ok((base, top));- }- if m >= MAX_FIT_SAMPLES {- return Err(KernelError::Invalid(format!("the strip can't be fitted within {FIT_TOLERANCE} mm (off by up to {worst:.4} mm)")));- }- m = (2 * m - 1).min(MAX_FIT_SAMPLES);- }-}-/// Which strip of a joint: the one ending there or the one starting there.#[derive(Clone, Copy, PartialEq, Eq)]enum Side {@@ -1003,9 +971,11 @@ mod tests {let cases = [(ruled(RuledMode::Normal, 2.0, top), 80.0, 4, 4),(ruled(RuledMode::Tangent, 2.0, top), 14.0 * 14.0 - 100.0, 4, 4),- (round(RuledMode::Tangent), 80.0 + PI * 4.0, 12, 12),+ // Each round corner is one face, SMLib's revolve of a quarter turn (truck's cone+ // builder split it in two): four strips and four sectors, each sharing its two sides.+ (round(RuledMode::Tangent), 80.0 + PI * 4.0, 8, 8),(ruled(angle, 2.0, top), 80.0 + 4.0 * 2.0 * h, 4, 4),- (round(angle), 80.0 + PI * h * 2.0, 12, 12),+ (round(angle), 80.0 + PI * h * 2.0, 8, 8),];for (r, want, faces, joins) in cases {let s = ruled_surface(&b, &rim, &r).unwrap();@@ -1063,10 +1033,10 @@ mod tests {}/// Off a doubly curved face (a saddle patch over a cup's rim) its normal turns along a- /// straight edge: the strip is a fitted ruled B-spline, its rulings square to the edge, so- /// it reaches exactly 3 from the edge's line (within the fit) and covers it end to end.+ /// straight edge: the rulings' ends trace an offset curve with no exact NURBS form, so the+ /// strip is refused, by name.#[test]- fn a_strip_off_a_curved_face_is_fitted() {+ fn a_strip_off_a_curved_face_is_refused() {let b = box_solid(Vec3::new(-14.0, -14.0, 0.0), Vec3::new(14.0, 14.0, 20.0)).unwrap();let tool = cylinder(Vec3::new(0.0, -60.0, 50.0), Vec3::Y, 40.0, 120.0).unwrap();let cup = crate::boolean(&b, &tool, crate::BoolOp::Cut).unwrap().unwrap();@@ -1074,14 +1044,8 @@ mod tests {let rim = [Vec3::new(14.0, 0.0, z(14.0)), Vec3::new(0.0, 14.0, z(0.0)), Vec3::new(-14.0, 0.0, z(-14.0)), Vec3::new(0.0, -14.0, z(0.0))];let cover = crate::patch_edges(&cup, &rim).unwrap();let edge = [Vec3::new(14.0, 0.0, z(14.0))];- let s = ruled_surface(&cover, &edge, &ruled(RuledMode::Normal, 3.0, Vec3::new(0.0, 0.0, 12.0))).unwrap();- assert!(s.face_count() == 1 && face_surface(&s, 0).is_none(), "{:?}", face_surface(&s, 0));- let mesh = s.mesh(&crate::MeshTolerance::chord(0.01)).unwrap();- let from_line = |p: &Vec3| Vec3::new(p.x - 14.0, 0.0, p.z - z(14.0)).len();- let far = mesh.positions.iter().map(from_line).fold(0.0, f64::max);- assert!((far - 3.0).abs() < 2e-3, "{far}");- let bb = measure(&s).unwrap().bbox;- assert!((bb.min.y + 14.0).abs() < 1e-3 && (bb.max.y - 14.0).abs() < 1e-3, "{bb:?}");+ let e = ruled_surface(&cover, &edge, &ruled(RuledMode::Normal, 3.0, Vec3::new(0.0, 0.0, 12.0))).unwrap_err().to_string();+ assert!(e.contains("offset curve"), "{e}");}/// Each strip is made in place of the edge it stands on; a corner face in place of the twodiff --git a/crates/smlib/SMLIB_REV b/crates/smlib/SMLIB_REVindex 091b71394a..f57dd0a707 100644--- a/crates/smlib/SMLIB_REV+++ b/crates/smlib/SMLIB_REV@@ -1 +1 @@-f6d2c5e00e63b0923351566714f161decf64373d+9dcfcd6f581eff5d37a038910fdc5296f8354743diff --git a/crates/smlib/src/ffi/sweeps.rs b/crates/smlib/src/ffi/sweeps.rsindex d3b9476050..7720e3f45a 100644--- a/crates/smlib/src/ffi/sweeps.rs+++ b/crates/smlib/src/ffi/sweeps.rs@@ -1,5 +1,6 @@//! The sweeps block of `capi/include/smlib.h` (`smlib_extrude`, `smlib_revolve`, `smlib_sweep`,-//! `smlib_sweep_placed`, `smlib_loft`, `smlib_loft_to_point`, `smlib_screw_sweep`, `smlib_sweep_stations`, `smlib_planar_faces`; the fork's+//! `smlib_sweep_placed`, `smlib_project_curves` (with `smlib_curves_*`), `smlib_loft`, `smlib_loft_to_point`, `smlib_screw_sweep`,+//! `smlib_sweep_stations`, `smlib_planar_faces`; the fork's//! `capi/src/smlib_sweeps.cpp`), declared by hand.//! Part of the FFI boundary, like its parent: called only through [`crate::sweeps`], which builds//! the curve lists.@@ -104,6 +105,29 @@ unsafe extern "C" {out: *mut *mut RawBrep,placed: *mut f64,) -> i32;+ fn smlib_project_curves(+ brep: *const RawBrep,+ face_point: *const f64,+ pick_tolerance: f64,+ kinds: *const i64,+ n_kinds: i64,+ reals: *const f64,+ n_reals: i64,+ direction: *const f64,+ tolerance: f64,+ out: *mut *mut RawCurves,+ ) -> i32;+ fn smlib_curves_size(curves: *const RawCurves, n_kinds: *mut i64, n_reals: *mut i64, n_curves: *mut i64) -> i32;+ fn smlib_curves_read(+ curves: *const RawCurves,+ kinds: *mut i64,+ n_kinds: i64,+ reals: *mut f64,+ n_reals: i64,+ intervals: *mut f64,+ n_intervals: i64,+ ) -> i32;+ fn smlib_curves_free(curves: *mut RawCurves);fn smlib_loft(kinds: *const i64,n_kinds: i64,@@ -183,6 +207,61 @@ pub fn sweep_placed(profile: &List, path: &List, frame: i32, cap: bool, placed:new_brep(|out| unsafe { smlib_sweep_placed(k, nk, r, nr, pk, npk, pr, npr, frame, i32::from(cap), out, at) })}+/// The C API's `smlib_curves`.+#[cfg(feature = "native")]+#[repr(C)]+pub struct RawCurves {+ _private: [u8; 0],+}++/// Curves a call made, read out of the C API's `smlib_curves`: a curve list (kinds, reals) and+/// two interval numbers per curve.+pub struct OutCurves {+ pub kinds: Vec<i64>,+ pub reals: Vec<f64>,+ pub intervals: Vec<f64>,+}++/// `smlib_project_curves`: the curve list projected along `direction` onto the face of `brep`+/// nearest `face_point`.+#[cfg(feature = "native")]+pub fn project_curves(+ brep: &Handle,+ face_point: &[f64; 3],+ pick_tolerance: f64,+ curves: &List,+ direction: &[f64; 3],+ tolerance: f64,+) -> Result<OutCurves, i32> {+ let (k, nk, r, nr) = raw(curves)?;+ let mut out: *mut RawCurves = std::ptr::null_mut();+ let s = unsafe { smlib_project_curves(brep.raw(), face_point.as_ptr(), pick_tolerance, k, nk, r, nr, direction.as_ptr(), tolerance, &mut out) };+ if s != super::OK {+ return Err(s);+ }+ if out.is_null() {+ return Err(super::ERR_EMPTY);+ }+ let read = || -> Result<OutCurves, i32> {+ let (mut nk, mut nr, mut nc) = (0i64, 0i64, 0i64);+ let s = unsafe { smlib_curves_size(out, &mut nk, &mut nr, &mut nc) };+ if s != super::OK {+ return Err(s);+ }+ let len = |n: i64| usize::try_from(n).map_err(|_| ERR_ARG);+ let ni = nc.checked_mul(2).ok_or(ERR_ARG)?;+ let mut c = OutCurves { kinds: vec![0; len(nk)?], reals: vec![0.0; len(nr)?], intervals: vec![0.0; len(ni)?] };+ let s = unsafe { smlib_curves_read(out, c.kinds.as_mut_ptr(), nk, c.reals.as_mut_ptr(), nr, c.intervals.as_mut_ptr(), ni) };+ if s != super::OK {+ return Err(s);+ }+ Ok(c)+ };+ let c = read();+ unsafe { smlib_curves_free(out) };+ c+}+#[cfg(feature = "native")]pub fn loft(sections: &List, sizes: &[i64], degree: i32, cap: bool) -> Result<Handle, i32> {let (k, nk, r, nr) = raw(sections)?;@@ -238,6 +317,11 @@ pub fn sweep_placed(_: &List, _: &List, _: i32, _: bool, _: &mut [f64; 36]) -> RErr(NOT_BUILT)}+#[cfg(not(feature = "native"))]+pub fn project_curves(_: &Handle, _: &[f64; 3], _: f64, _: &List, _: &[f64; 3], _: f64) -> Result<OutCurves, i32> {+ Err(NOT_BUILT)+}+#[cfg(not(feature = "native"))]pub fn loft(_: &List, _: &[i64], _: i32, _: bool) -> Result<Handle, i32> {Err(NOT_BUILT)diff --git a/crates/smlib/src/sweeps.rs b/crates/smlib/src/sweeps.rsindex bc6e016b61..1b1b982501 100644--- a/crates/smlib/src/sweeps.rs+++ b/crates/smlib/src/sweeps.rs@@ -189,6 +189,29 @@ impl Brep {Ok((brep, [motion(0), motion(1), motion(2)]))}+ /// The `curves` projected along `direction` (pointing from them toward the face) onto the+ /// face nearest `face_point` (within `pick_tolerance`; [`Error::NoMatch`] or+ /// [`Error::Ambiguous`] otherwise): SMLib's parallel projection onto the face's surface,+ /// trimmed to the face, each curve within `tolerance` of the true projection, as the NURBS+ /// SMLib made with its interval. No projection at all is [`Error::Empty`].+ pub fn project_curves(+ &self,+ face_point: [f64; 3],+ pick_tolerance: f64,+ curves: &[Curve],+ direction: [f64; 3],+ tolerance: f64,+ ) -> Result<Vec<(Curve, [f64; 2])>> {+ init()?;+ let l = List::of(curves)?;+ let out = ffi::sweeps::project_curves(&self.0, &face_point, pick_tolerance, &l.raw(), &direction, tolerance).map_err(|s| match s {+ ffi::ops::ERR_PICK_NONE => Error::NoMatch { point: 0 },+ ffi::ops::ERR_PICK_AMBIGUOUS => Error::Ambiguous { point: 0 },+ s => error(s),+ })?;+ nurbs_list(&out)+ }+/// A loft (skin) through `sections` (at least two closed loops), `degree` (1 ruled, 2 or 3)/// across them.pub fn loft(sections: &[Vec<Curve>], degree: u32, cap: bool) -> Result<Brep> {@@ -238,6 +261,30 @@ impl Brep {}}+/// The NURBS curves of a curve list the C API made, each with its interval.+fn nurbs_list(out: &ffi::sweeps::OutCurves) -> Result<Vec<(Curve, [f64; 2])>> {+ let (mut k, mut r) = (0usize, 0usize);+ let mut curves = Vec::new();+ for ivl in out.intervals.as_chunks::<2>().0 {+ let (Some(&ffi::sweeps::CURVE_NURBS), Some(°ree), Some(&count)) = (out.kinds.get(k), out.kinds.get(k + 1), out.kinds.get(k + 2)) else {+ return Err(Error::Argument);+ };+ k += 3;+ let degree = usize::try_from(degree).map_err(|_| Error::Argument)?;+ let count = usize::try_from(count).map_err(|_| Error::Argument)?;+ let pts = out.reals.get(r..r + 4 * count).ok_or(Error::Argument)?;+ r += 4 * count;+ let knots = out.reals.get(r..r + count + degree + 1).ok_or(Error::Argument)?.to_vec();+ r += count + degree + 1;+ let points = pts.as_chunks::<4>().0.to_vec();+ curves.push((Curve::Nurbs { degree, points, knots }, [ivl[0], ivl[1]]));+ }+ if k != out.kinds.len() || r != out.reals.len() {+ return Err(Error::Argument);+ }+ Ok(curves)+}+#[cfg(test)]#[path = "sweeps_tests.rs"]mod tests;