From 7b5d732e38c17cbf10c30e1604d7a65056e507db Mon Sep 17 00:00:00 2001 From: jyviko <2501648+jyviko@users.noreply.github.com> Date: Thu, 22 Oct 2026 07:20:11 -0400 Subject: [PATCH] kernel: project_onto_face, sketch curves projected onto a face as SMLib's curves project_onto_face projects sketch segments along a direction onto the face of a body at a point through Brep::project_curves (smlib_project_curves), each projected piece the B-spline SMLib made, returned as a record curve (RationalSpline) on its interval, within a millionth of the body's size. lib.rs (coordinator): export point_path::project_onto_face. (not built: base in progress) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017itGh5fpMEousnZ91cYcVt --- crates/kernel/src/point_path.rs | 16 +++++++++++++ crates/kernel/src/smlib_sweeps.rs | 39 +++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/crates/kernel/src/point_path.rs b/crates/kernel/src/point_path.rs index 5953b3de0..76be18b4a 100644 --- a/crates/kernel/src/point_path.rs +++ b/crates/kernel/src/point_path.rs @@ -155,6 +155,22 @@ pub fn edge_path(body: &Body, edge: usize) -> Result> { curve_path(&e.curve, e.interval) } +/// The sketch segments `segs` on `plane` projected along `direction` (pointing from them toward +/// the face) onto the face of `body` at `face_at`, as the record curves SMLib's projection makes +/// (one per piece of the projection that stays on the face), each on its interval. +pub fn project_onto_face( + body: &Body, + face_at: Vec3, + plane: &Plane, + segs: &[solidcraft_geom::Seg2], + direction: Vec3, +) -> Result> { + if !(face_at.is_finite() && direction.is_finite() && direction.len() > 1e-12) || segs.is_empty() || segs.len() > 4096 { + return Err(KernelError::Invalid("curve projection: a point, a direction and 1…4096 curves".into())); + } + crate::smlib_sweeps::project_onto_face(body, face_at, plane, segs, direction) +} + /// Most control points a record curve given to [`curve_path`] may have. const MAX_CURVE_POINTS: usize = 100_000; diff --git a/crates/kernel/src/smlib_sweeps.rs b/crates/kernel/src/smlib_sweeps.rs index f9d5bf993..8a8c163f1 100644 --- a/crates/kernel/src/smlib_sweeps.rs +++ b/crates/kernel/src/smlib_sweeps.rs @@ -72,7 +72,8 @@ pub(crate) struct Draft { #[cfg(feature = "smlib")] pub(crate) use imp::{ - body_from_record, curve, extrude, loft, loft_rails, loft_to_point, path_curve, revolve, screw, sphere, sweep, sweep_controlled, + body_from_record, curve, extrude, loft, loft_rails, loft_to_point, path_curve, project_onto_face, revolve, screw, sphere, sweep, + sweep_controlled, }; /// The error of an SMLib operation in a build without SMLib. @@ -91,6 +92,9 @@ mod stubs { pub(crate) fn sphere(_: Vec3, _: f64) -> Result { Err(not_built()) } + pub(crate) fn project_onto_face(_: &Body, _: Vec3, _: &Plane, _: &[Seg2], _: Vec3) -> Result> { + Err(not_built()) + } pub(crate) fn extrude(_: &Plane, _: &[Piece], _: Option<&Region2>, _: f64, _: f64, _: f64, _: Option) -> Result { Err(not_built()) } @@ -117,7 +121,9 @@ mod stubs { } } #[cfg(not(feature = "smlib"))] -pub(crate) use stubs::{body_from_record, extrude, loft, loft_rails, loft_to_point, revolve, screw, sphere, sweep, sweep_controlled}; +pub(crate) use stubs::{ + body_from_record, extrude, loft, loft_rails, loft_to_point, project_onto_face, revolve, screw, sphere, sweep, sweep_controlled, +}; /// Refuses what no SMLib sweep does, before any work. pub(crate) fn refuse(what: &str) -> KernelError { @@ -208,6 +214,35 @@ mod imp { body_of(&brep, what) } + /// The sketch segments `segs` on `plane` projected along `direction` onto the face of + /// `body` at `face_at`: SMLib's projection (smlib_project_curves), each projected curve the + /// B-spline SMLib made, as a record curve on its interval, within a millionth of the body's + /// size of the true projection. + pub(crate) fn project_onto_face( + body: &Body, + face_at: Vec3, + plane: &Plane, + segs: &[Seg2], + direction: Vec3, + ) -> Result> { + let c = segs.iter().map(|s| curve(plane, s)).collect::>>()?; + let size = body.size().max(1e-9); + let out = body.smlib()?.project_curves(p(face_at), size * 1e-3 + 1e-6, &c, p(direction), (size * 1e-6).max(1e-6)).map_err(|e| match e { + solidcraft_smlib::Error::Empty => KernelError::Invalid("the curve projects to nothing on the face".into()), + solidcraft_smlib::Error::NoMatch { .. } => KernelError::Invalid("no face of the body at the projection's point".into()), + e => failed("curve projection", e), + })?; + out.into_iter() + .map(|(c, interval)| match c { + Curve::Nurbs { points, knots, .. } => { + let homogeneous = points.iter().map(|q| [q[0] * q[3], q[1] * q[3], q[2] * q[3], q[3]]).collect(); + Ok((solidcraft_geom::brep::Curve::RationalSpline { knots, homogeneous }, interval)) + } + _ => Err(failed("curve projection", "a projected curve is not a B-spline")), + }) + .collect() + } + /// SMLib's sphere about `center`, each face swept from the arc (segment 0) of the half disc /// a sphere revolves. pub(crate) fn sphere(center: Vec3, radius: f64) -> Result {