Wave 92/509: kernel: fillets and chamfers through SMLib when SOLIDCRAFT_KERNEL=smlib (and 2 more)

3 commits, squashed; the originals stay reachable from tag archive/merge-main-develop.

- baae4ee19 jyviko: kernel: fillets and chamfers through SMLib when SOLIDCRAFT_KERNEL=smlib
- f73d40383 jyviko: kernel: an SMLib blend pick at a seam's end takes the sharp edge there
- 1d8299aed jyviko: kernel: an SMLib body keeps each edge's and vertex's tolerance through its record

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
This commit is contained in:
jyviko
2026-10-21 10:45:10 -04:00
parent f9f8f21918
commit 7622329499
20 changed files with 638 additions and 63 deletions
+9 -4
View File
@@ -38,13 +38,15 @@ pub enum Error {
type Result<T> = std::result::Result<T, Error>;
/// One oriented edge of a loop, as [`pcurves`] reads it: the edge's index (a seam edge, used
/// twice, is fitted once), its curve record and interval, and whether the loop runs along it.
/// twice, is fitted once), its curve record and interval, whether the loop runs along it, and the
/// edge record's own tolerance (its pcurve is fitted to that, never closer than the face's).
#[derive(Clone, Copy, Debug)]
pub struct EdgeUse<'a> {
pub edge: u32,
pub curve: &'a Curve,
pub interval: [f64; 2],
pub forward: bool,
pub tolerance: f64,
}
/// Knot spans a fitted pcurve may have (the schema takes 4096 control points).
@@ -61,8 +63,8 @@ const MAX_USES: usize = 100_000;
const CHECKS: usize = 16;
/// For each loop of a face on `surface`, the pcurve of each of its edge uses, in order, within
/// `tolerance` mm of the edge when lifted onto the surface. Loops are as the record keeps them
/// (relative to the surface's natural normal: outer counter-clockwise, inner clockwise); each
/// `tolerance` mm (or the use's own edge tolerance where larger) of the edge when lifted onto
/// the surface. Loops are as the record keeps them (relative to the surface's natural normal: outer counter-clockwise, inner clockwise); each
/// pcurve runs on its use's interval in its curve's direction, whatever `forward` says.
pub fn pcurves(surface_record: &Surface, loops: &[Vec<EdgeUse<'_>>], tolerance: f64) -> Result<Vec<Vec<Curve2>>> {
if !(tolerance.is_finite() && tolerance > 0.0 && tolerance <= 1e3) {
@@ -87,7 +89,10 @@ pub fn pcurves(surface_record: &Surface, loops: &[Vec<EdgeUse<'_>>], tolerance:
curve(u.curve, 0, &mut budget).map_err(|e| Error::Invalid(format!("loop {w} use {i}: {e}")))?;
interval(&u.interval, curve_natural(u.curve)).map_err(|e| Error::Invalid(format!("loop {w} use {i}: {e}")))?;
let c = CurveEval::new(u.curve)?;
let pc = on.pcurve(u.curve, &c, u.interval, tolerance).map_err(|e| {
// An edge record that holds a looser tolerance than the face's (another
// kernel's approximated edge) is fitted to its own, up to the same cap.
let own = if u.tolerance.is_finite() && u.tolerance <= 1e3 { tolerance.max(u.tolerance) } else { tolerance };
let pc = on.pcurve(u.curve, &c, u.interval, own).map_err(|e| {
let at = |t: f64| c.point(t).map(|p| [p.x, p.y, p.z]);
let message = format!("pcurve of loop {w} use {i} ({:?} to {:?}): {e}", at(u.interval[0]), at(u.interval[1]));
match e {
+39 -24
View File
@@ -11,7 +11,7 @@ fn about_z(origin: [f64; 3], down: bool) -> Frame {
frame(origin, [1., 0., 0.], if down { [0., -1., 0.] } else { [0., 1., 0.] })
}
fn one(curve: &Curve, interval: [f64; 2]) -> Vec<Vec<EdgeUse<'_>>> {
vec![vec![EdgeUse { edge: 0, curve, interval, forward: true }]]
vec![vec![EdgeUse { edge: 0, curve, interval, forward: true, tolerance: 0.0 }]]
}
/// The largest distance between the lifted pcurve and the curve, at 65 points and each span's
/// middle.
@@ -185,10 +185,10 @@ fn a_seam_used_twice_gets_pcurves_a_period_apart() {
let high = Curve::Circle { frame: about_z([0., 0., 4.], false), radius: 3. };
let full = [0., TAU];
let l = vec![
EdgeUse { edge: 0, curve: &low, interval: full, forward: true },
EdgeUse { edge: 1, curve: &seam, interval: [0., 1.], forward: true },
EdgeUse { edge: 2, curve: &high, interval: full, forward: false },
EdgeUse { edge: 1, curve: &seam, interval: [0., 1.], forward: false },
EdgeUse { edge: 0, curve: &low, interval: full, forward: true, tolerance: 0.0 },
EdgeUse { edge: 1, curve: &seam, interval: [0., 1.], forward: true, tolerance: 0.0 },
EdgeUse { edge: 2, curve: &high, interval: full, forward: false, tolerance: 0.0 },
EdgeUse { edge: 1, curve: &seam, interval: [0., 1.], forward: false, tolerance: 0.0 },
];
let pcs = &pcurves(&cylinder, std::slice::from_ref(&l), 1e-9).unwrap()[0];
let (ends, area) = walk(&l, pcs);
@@ -214,8 +214,8 @@ fn meridians_run_straight_into_the_poles() {
let meridian = Curve::Circle { frame: frame([0.; 3], [1., 0., 0.], [0., 0., 1.]), radius: 6. };
let half = [-FRAC_PI_2, FRAC_PI_2];
let l = vec![
EdgeUse { edge: 0, curve: &meridian, interval: half, forward: false },
EdgeUse { edge: 0, curve: &meridian, interval: half, forward: true },
EdgeUse { edge: 0, curve: &meridian, interval: half, forward: false, tolerance: 0.0 },
EdgeUse { edge: 0, curve: &meridian, interval: half, forward: true, tolerance: 0.0 },
];
for tol in [1e-3, 1e-6] {
let pcs = &pcurves(&sphere, std::slice::from_ref(&l), tol).unwrap()[0];
@@ -268,7 +268,8 @@ fn a_seam_from_pole_to_pole_encloses_the_whole_sphere() {
};
for (seam, iv) in [(&circle, [-FRAC_PI_2, FRAC_PI_2]), (&nurbs, [0., 1.])] {
for order in [[true, false], [false, true]] {
let l: Vec<EdgeUse> = order.iter().map(|&forward| EdgeUse { edge: 0, curve: seam, interval: iv, forward }).collect();
let l: Vec<EdgeUse> =
order.iter().map(|&forward| EdgeUse { edge: 0, curve: seam, interval: iv, forward, tolerance: 0.0 }).collect();
let pcs = &pcurves(&sphere, std::slice::from_ref(&l), tol).unwrap()[0];
let what = format!("{sf:?} {turn} {off} {seam:?} {order:?}");
for pc in pcs {
@@ -306,10 +307,10 @@ fn a_torus_bounded_by_its_seams_spans_both_periods() {
let tube = Curve::Circle { frame: Frame { origin: p(ax.o + ax.x * 10.), x_axis: p(ax.x), y_axis: p(ax.z) }, radius: 2. };
let full = [0., TAU];
let l = vec![
EdgeUse { edge: 0, curve: &around, interval: full, forward: true },
EdgeUse { edge: 1, curve: &tube, interval: full, forward: true },
EdgeUse { edge: 0, curve: &around, interval: full, forward: false },
EdgeUse { edge: 1, curve: &tube, interval: full, forward: false },
EdgeUse { edge: 0, curve: &around, interval: full, forward: true, tolerance: 0.0 },
EdgeUse { edge: 1, curve: &tube, interval: full, forward: true, tolerance: 0.0 },
EdgeUse { edge: 0, curve: &around, interval: full, forward: false, tolerance: 0.0 },
EdgeUse { edge: 1, curve: &tube, interval: full, forward: false, tolerance: 0.0 },
];
let pcs = &pcurves(&torus, std::slice::from_ref(&l), 1e-9).unwrap()[0];
let (ends, area) = walk(&l, pcs);
@@ -392,10 +393,10 @@ fn a_seam_on_a_spline_closed_in_u_gets_pcurves_a_period_apart() {
let top = Curve::Circle { frame: about_z([0., 0., small], false), radius: big };
let quarter = Curve::Circle { frame: frame([big, 0., 0.], [1., 0., 0.], [0., 0., 1.]), radius: small };
let l = vec![
EdgeUse { edge: 0, curve: &outer, interval: full, forward: true },
EdgeUse { edge: 1, curve: &quarter, interval: [0., FRAC_PI_2], forward: true },
EdgeUse { edge: 2, curve: &top, interval: full, forward: false },
EdgeUse { edge: 1, curve: &quarter, interval: [0., FRAC_PI_2], forward: false },
EdgeUse { edge: 0, curve: &outer, interval: full, forward: true, tolerance: 0.0 },
EdgeUse { edge: 1, curve: &quarter, interval: [0., FRAC_PI_2], forward: true, tolerance: 0.0 },
EdgeUse { edge: 2, curve: &top, interval: full, forward: false, tolerance: 0.0 },
EdgeUse { edge: 1, curve: &quarter, interval: [0., FRAC_PI_2], forward: false, tolerance: 0.0 },
];
for tol in [1e-4, 1e-7] {
closes_around_the_box(&band, &l, [1, 3], None, tol, &format!("torus band, tol {tol}"));
@@ -406,10 +407,10 @@ fn a_seam_on_a_spline_closed_in_u_gets_pcurves_a_period_apart() {
(Curve::Circle { frame: about_z([0.; 3], false), radius: 3. }, Curve::Circle { frame: about_z([0., 0., 4.], false), radius: 3. });
let line = Curve::Line { start: [3., 0., 0.], end: [3., 0., 4.] };
let l = vec![
EdgeUse { edge: 0, curve: &low, interval: full, forward: true },
EdgeUse { edge: 1, curve: &line, interval: [0., 1.], forward: true },
EdgeUse { edge: 2, curve: &high, interval: full, forward: false },
EdgeUse { edge: 1, curve: &line, interval: [0., 1.], forward: false },
EdgeUse { edge: 0, curve: &low, interval: full, forward: true, tolerance: 0.0 },
EdgeUse { edge: 1, curve: &line, interval: [0., 1.], forward: true, tolerance: 0.0 },
EdgeUse { edge: 2, curve: &high, interval: full, forward: false, tolerance: 0.0 },
EdgeUse { edge: 1, curve: &line, interval: [0., 1.], forward: false, tolerance: 0.0 },
];
closes_around_the_box(&cylinder, &l, [1, 3], None, 1e-7, "cylinder");
// The dome: radius 6 from its equator (v = 0) to its pole (v = 1), the seam a meridian.
@@ -417,9 +418,9 @@ fn a_seam_on_a_spline_closed_in_u_gets_pcurves_a_period_apart() {
let equator = Curve::Circle { frame: about_z([0.; 3], false), radius: 6. };
let meridian = Curve::Circle { frame: frame([0.; 3], [1., 0., 0.], [0., 0., 1.]), radius: 6. };
let l = vec![
EdgeUse { edge: 0, curve: &equator, interval: full, forward: true },
EdgeUse { edge: 1, curve: &meridian, interval: [0., FRAC_PI_2], forward: true },
EdgeUse { edge: 1, curve: &meridian, interval: [0., FRAC_PI_2], forward: false },
EdgeUse { edge: 0, curve: &equator, interval: full, forward: true, tolerance: 0.0 },
EdgeUse { edge: 1, curve: &meridian, interval: [0., FRAC_PI_2], forward: true, tolerance: 0.0 },
EdgeUse { edge: 1, curve: &meridian, interval: [0., FRAC_PI_2], forward: false, tolerance: 0.0 },
];
closes_around_the_box(&dome, &l, [1, 2], Some(2), 1e-7, "dome");
// A spline whose ends merely come near each other stays open: no period, so the seam's
@@ -483,7 +484,7 @@ fn hostile_records_give_errors() {
let collapsed = Curve::Circle { frame: about_z([0.; 3], false), radius: 0. };
assert!(matches!(pcurves(&plane, &one(&collapsed, [0., 1.]), 1e-6), Err(Error::Invalid(_))));
// Too many uses, refused before any work.
let many = vec![vec![EdgeUse { edge: 0, curve: &line, interval: [0., 1.], forward: true }; MAX_USES + 1]];
let many = vec![vec![EdgeUse { edge: 0, curve: &line, interval: [0., 1.], forward: true, tolerance: 0.0 }; MAX_USES + 1]];
assert!(matches!(pcurves(&plane, &many, 1e-6), Err(Error::TooLarge(_))));
// A polyline of many points on a curved surface: too many spans for one pcurve.
let cylinder = Surface::Cylinder { frame: about_z([0.; 3], false), radius: 1. };
@@ -506,3 +507,17 @@ fn hostile_records_give_errors() {
let far = Curve::Circle { frame: about_z([0.; 3], false), radius: 1e12 };
let _ = pcurves(&torus, &one(&far, [0., 1.]), 1e-6);
}
/// An edge that lies up to 7.5e-5 mm off its plane (another kernel's approximation) gets no
/// pcurve at the face's 1e-5 mm, and one within its own record tolerance of 2e-4 mm when the
/// use carries it.
#[test]
fn an_edge_is_fitted_to_its_own_tolerance() {
let plane = Surface::PlaneThrough { origin: [1., 2., 3.], u_point: [3., 2., 3.], v_point: [1., 2., 7.] };
let off =
Curve::Spline { knots: vec![0., 0., 0., 0., 1., 1., 1., 1.], points: vec![[1., 2., 3.], [2., 2.0001, 4.], [3., 2.0001, 5.], [4., 2., 6.]] };
assert!(pcurves(&plane, &one(&off, [0., 1.]), 1e-5).is_err());
let own = vec![vec![EdgeUse { edge: 0, curve: &off, interval: [0., 1.], forward: true, tolerance: 2e-4 }]];
let pc = &pcurves(&plane, &own, 1e-5).unwrap()[0][0];
assert!(lift_error(&plane, &off, [0., 1.], pc) <= 2e-4);
}
+1 -1
View File
@@ -36,7 +36,7 @@ fn fit_pcurves(b: &mut Brep) {
.iter()
.map(|u| {
let e = &b.edges[u.edge as usize];
pcurve::EdgeUse { edge: u.edge, curve: &e.curve, interval: e.interval, forward: u.forward }
pcurve::EdgeUse { edge: u.edge, curve: &e.curve, interval: e.interval, forward: u.forward, tolerance: e.tolerance }
})
.collect(),
Loop::Vertex { .. } => vec![],
+22
View File
@@ -1119,6 +1119,9 @@ pub fn fillet_g2(body: &Body, edges: &[Vec3], radius: f64) -> Result<Body> {
if edges.is_empty() || edges.len() > 1000 || edges.iter().any(|p| ![p.x, p.y, p.z].into_iter().all(f64::is_finite)) {
return Err(KernelError::Invalid("G2 fillet needs 1 to 1000 finite edge points".into()));
}
if crate::blend_smlib::selected()? {
return crate::blend_smlib::fillet_g2(body, edges, radius);
}
let counts = record::counts(&[body]);
let mut cur = crate::boolean_record::healed(body)?;
for p in edges {
@@ -1847,6 +1850,9 @@ fn closed(input: &Body, b: Body) -> Result<Body> {
/// largest that works).
pub fn fillet(body: &Body, edges: &[Vec3], radius: f64) -> Result<Body> {
body.require_brep("fillet")?;
if crate::blend_smlib::selected()? {
return crate::blend_smlib::fillet(body, edges, radius);
}
// A round that would run past its faces into other geometry or another round (#1080).
// Checked once built, so a refusal of the build itself stands as it is.
let size = crate::blend_fit::Size::Round(radius);
@@ -1919,6 +1925,9 @@ fn corners(body: &Body, edges: &[Vec3], r: f64, shape: Shape, what: &str, g2: bo
/// unchanged.
pub fn fillet_g2_corners(body: &Body, edges: &[Vec3], radius: f64) -> Result<Body> {
body.require_brep("fillet")?;
if crate::blend_smlib::selected()? {
return Err(crate::blend_smlib::unsupported("a fillet with G2 corner patches"));
}
if !(radius.is_finite() && radius > 0.0) {
return Err(KernelError::Invalid("the blend radius must be finite and positive".into()));
}
@@ -2004,6 +2013,9 @@ pub fn fillet_each(body: &Body, edges: &[Vec3], radii: &[f64]) -> Result<Body> {
if radii.len() != edges.len() {
return Err(KernelError::Invalid("one radius per edge".into()));
}
if crate::blend_smlib::selected()? {
return crate::blend_smlib::fillet_each(body, edges, radii);
}
if let Some(r) = radii.first().filter(|r| radii.iter().all(|x| x.to_bits() == r.to_bits())) {
return fillet(body, edges, *r);
}
@@ -2089,6 +2101,9 @@ pub fn chamfer(body: &Body, edges: &[Vec3], distance: f64) -> Result<Body> {
if edges.is_empty() || edges.len() > 1000 {
return Err(KernelError::Invalid("select 1…1000 edges".into()));
}
if crate::blend_smlib::selected()? {
return crate::blend_smlib::chamfer(body, edges, distance);
}
// A chamfer that would run past its faces into other geometry or another chamfer, or whose
// result changes more than the chamfers can (#1080).
let size = crate::blend_fit::Size::Chamfer(distance);
@@ -2327,6 +2342,13 @@ pub fn chamfer_sides(body: &Body, edges: &[Vec3], d1: f64, side: ChamferSide, fl
if edges.is_empty() || edges.len() > 1000 {
return Err(KernelError::Invalid("select 1…1000 edges".into()));
}
if crate::blend_smlib::selected()? {
// SMLib has no solver for unequal setbacks: equal ones are its equal-distance chamfer.
return match side {
ChamferSide::Distance(d2) if d2 == d1 => crate::blend_smlib::chamfer(body, edges, d1),
_ => Err(crate::blend_smlib::unsupported("a chamfer with two different setbacks (two distances, or a distance and an angle)")),
};
}
let size = crate::blend_fit::Size::Sides(d1, side, flip);
let counts = record::counts(&[body]);
let cut = || -> Result<Body> {
+4
View File
@@ -33,6 +33,10 @@ pub fn fillet_radii(body: &Body, edges: &[(Vec3, f64)]) -> Result<Body> {
if edges.iter().any(|(p, r)| !(p.is_finite() && r.is_finite() && *r > 1e-6 && *r < 1e6)) {
return Err(KernelError::Invalid("fillet size must be positive".into()));
}
if crate::blend_smlib::selected()? {
let (points, radii): (Vec<Vec3>, Vec<f64>) = edges.iter().copied().unzip();
return crate::blend_smlib::fillet_each(body, &points, &radii);
}
// Each step's result carries its history over `[body]`.
let counts = crate::record::counts(&[body]);
let mut cur = crate::boolean_record::healed(body)?;
+373
View File
@@ -0,0 +1,373 @@
//! Fillets and chamfers through SMLib, when [`crate::backend`] selects it.
//!
//! Each kernel entry point ([`crate::fillet`], [`crate::chamfer`], …) asks [`selected`] first and,
//! when SMLib is the kernel, hands its arguments here unchanged: the body goes into SMLib as its
//! schema 4 record (healed where SMLib's check refuses it as given), tagged as operand 0; each
//! point picks the body's nearest edge in its own edge order, as truck's blends do, and SMLib is
//! given that edge's midpoint, which no other edge is as near to (a point at a vertex between two
//! edges would leave SMLib to choose); SMLib blends those edges with their tangent chains, its check must pass, and the result comes back decoded with its
//! geometry checked. An option SMLib has no construction for is refused as "not supported yet",
//! never built by truck instead, so a comparison never compares truck with itself.
//!
//! The history is SMLib's tags, renumbered into the body's orders; a blend face, which SMLib
//! leaves untagged, comes from the edge it replaces ([`solidcraft_smlib::history::blends`]).
use solidcraft_geom::Vec3;
use crate::blend_variable::RadiusSpline;
use crate::body::Body;
use crate::{KernelError, Result};
/// Whether SMLib runs the blends ([`crate::backend`]). A setting naming SMLib in a build without
/// it, or naming no kernel, is the error it is for every operation.
pub(crate) fn selected() -> Result<bool> {
Ok(crate::backend()? == crate::Backend::Smlib)
}
/// What SMLib cannot build yet: refused rather than built by truck.
pub(crate) fn unsupported(what: &str) -> KernelError {
KernelError::Invalid(format!("not supported yet: {what} with the SMLib kernel"))
}
/// A constant-radius round of the edges nearest `edges` ([`crate::fillet`]).
pub(crate) fn fillet(body: &Body, edges: &[Vec3], radius: f64) -> Result<Body> {
imp::blend(body, "fillet", Kind::Round, edges, &vec![radius; edges.len()])
}
/// Each edge rounded at its own radius ([`crate::fillet_each`]).
pub(crate) fn fillet_each(body: &Body, edges: &[Vec3], radii: &[f64]) -> Result<Body> {
if radii.len() != edges.len() {
return Err(KernelError::Invalid("one radius per edge".into()));
}
imp::blend(body, "fillet", Kind::Round, edges, radii)
}
/// Curvature-continuous rounds with the setback of a round of `radius` ([`crate::fillet_g2`]).
pub(crate) fn fillet_g2(body: &Body, edges: &[Vec3], radius: f64) -> Result<Body> {
imp::blend(body, "G2 fillet", Kind::Curvature, edges, &vec![radius; edges.len()])
}
/// Rounds `chord` wide from contact to contact ([`crate::fillet_chord`]).
pub(crate) fn fillet_chord(body: &Body, edges: &[Vec3], chord: f64) -> Result<Body> {
imp::blend(body, "chord fillet", Kind::Chord, edges, &vec![chord; edges.len()])
}
/// An equal-distance chamfer ([`crate::chamfer`]).
pub(crate) fn chamfer(body: &Body, edges: &[Vec3], distance: f64) -> Result<Body> {
imp::blend(body, "chamfer", Kind::Chamfer, edges, &vec![distance; edges.len()])
}
/// One edge rounded with the radius `spline` (checked by the caller) along it from the end
/// nearest `start` ([`crate::fillet_variable_spline`]).
pub(crate) fn fillet_variable(body: &Body, edge_at: Vec3, start: Vec3, spline: &RadiusSpline) -> Result<Body> {
imp::variable(body, edge_at, start, spline)
}
#[derive(Clone, Copy)]
enum Kind {
Round,
Curvature,
Chord,
Chamfer,
}
#[cfg(feature = "smlib")]
mod imp {
use std::collections::BTreeSet;
use solidcraft_geom::Vec3;
use solidcraft_smlib::{Blend, Brep, CheckLevel};
use super::Kind;
use crate::blend_variable::RadiusSpline;
use crate::body::Body;
use crate::history::{BodyHistory, EdgeOrigin, History};
use crate::native::{body_from_smlib_ordered, smlib_body_ordered};
use crate::{KernelError, Result, guard, record};
fn failed(what: &str, step: &str, e: impl std::fmt::Display) -> KernelError {
KernelError::Failed(format!("SMLib {what}: {step}: {e}"))
}
/// How far a pick may lie from its edge: as far as truck's blends accept one.
fn pick_tolerance(body: &Body) -> f64 {
body.size() * 0.05 + 1e-3
}
/// The edges `picks` select: each one's index in the body's edge order and the point halfway
/// along it, for SMLib to pick. A pick as near to a smooth join (a seam where a face meets
/// itself, or faces meeting tangentially: never a blend's edge) as to a sharp edge, such as
/// one at the vertex where a cylinder's seam meets its rim, takes the sharp edge.
pub(super) fn resolve(body: &Body, picks: &[Vec3]) -> Result<Picked> {
if picks.iter().any(|p| !p.is_finite()) {
return Err(KernelError::Invalid("edge picks must be finite points".into()));
}
let mesh = body.tessellate((body.size() * 2e-3).max(1e-3))?;
let seams = crate::topo::seam_flags(body, &mesh);
let (smooth, _) = crate::smooth_edges::flags(body, &mesh);
let reach = pick_tolerance(body);
let tie = (body.size() * 1e-6).max(1e-6);
let mut out = Picked { edges: Vec::with_capacity(picks.len()), points: Vec::with_capacity(picks.len()) };
for p in picks {
// The nearest edge, and the nearest that is no seam.
let (mut any, mut edge): (Option<(f64, usize)>, Option<(f64, usize)>) = (None, None);
for (i, line) in mesh.edges.iter().enumerate() {
let d =
line.windows(2).filter_map(|w| if let [a, b] = w { Some(p.dist_to_segment(*a, *b)) } else { None }).fold(f64::INFINITY, f64::min);
let seam = seams.get(i).copied().unwrap_or(false) || smooth.get(i).copied().unwrap_or(false);
for (slot, take) in [(&mut any, true), (&mut edge, !seam)] {
if take && slot.is_none_or(|(bd, _)| d < bd) {
*slot = Some((d, i));
}
}
}
let chosen = match (any, edge) {
(Some((d, _)), Some(e)) if e.0 <= d + tie => Some(e),
(a, _) => a,
};
let Some((_, i)) = chosen.filter(|(d, _)| *d <= reach) else {
return Err(KernelError::Invalid(format!("no edge near the pick at ({:.3}, {:.3}, {:.3})", p.x, p.y, p.z)));
};
let (mid, _) = crate::body::polyline_mid(mesh.edges.get(i).map(Vec::as_slice).unwrap_or(&[]));
out.edges.push(i);
out.points.push([mid.x, mid.y, mid.z]);
}
Ok(out)
}
pub(super) struct Picked {
/// Each pick's edge in the body's edge order.
pub(super) edges: Vec<usize>,
/// A point halfway along each.
pub(super) points: Vec<[f64; 3]>,
}
fn sizes_ok(what: &str, sizes: &[f64]) -> Result<()> {
if sizes.is_empty() || sizes.len() > solidcraft_smlib::MAX_POINTS {
return Err(KernelError::Invalid(format!("{what}: select 1…{} edges", solidcraft_smlib::MAX_POINTS)));
}
if sizes.iter().any(|s| !(s.is_finite() && *s > 1e-6 && *s < 1e6)) {
return Err(KernelError::Invalid(format!("{what}: the size must be positive and finite")));
}
Ok(())
}
pub(super) fn blend(body: &Body, what: &str, kind: Kind, edges: &[Vec3], sizes: &[f64]) -> Result<Body> {
body.require_brep(what)?;
sizes_ok(what, sizes)?;
let picked = resolve(body, edges)?;
let kind = match kind {
Kind::Round => Blend::Round,
Kind::Curvature => Blend::Curvature,
Kind::Chord => Blend::Chord,
Kind::Chamfer => Blend::Chamfer,
};
let tol = pick_tolerance(body);
run(body, what, &picked.edges, |b| b.blend(kind, &picked.points, sizes, tol))
}
pub(super) fn variable(body: &Body, edge_at: Vec3, start: Vec3, spline: &RadiusSpline) -> Result<Body> {
body.require_brep("fillet")?;
if !start.is_finite() {
return Err(KernelError::Invalid("the start point must be finite".into()));
}
let picked = resolve(body, &[edge_at])?;
let [at] = picked.points[..] else { return Err(KernelError::Invalid("one edge".into())) };
let tol = pick_tolerance(body);
run(body, "variable fillet", &picked.edges, |b| {
b.fillet_variable(at, tol, [start.x, start.y, start.z], spline.degree, &spline.knots, &spline.radii)
})
}
/// `op` on `body` in SMLib, tagged as operand 0, checked, decoded, with its history.
fn run(body: &Body, what: &str, edges: &[usize], op: impl FnOnce(&Brep) -> solidcraft_smlib::Result<Brep>) -> Result<Body> {
guard(what, || {
let (sb, order) = smlib_body_ordered(body)?;
let tagged = sb.tagged(0).map_err(|e| failed(what, "tag", e))?;
let r = op(&tagged).map_err(|e| failed(what, "operation", e))?;
let check = r.check(CheckLevel::All).map_err(|e| failed(what, "check", e))?;
if !check.is_valid() {
let input_fails = sb.check(CheckLevel::All).is_ok_and(|c| !c.is_valid());
let step =
if input_fails { "SMLib's check refuses the result (the input already fails it)" } else { "SMLib's check refuses the result" };
return Err(failed(what, step, check));
}
let (out, result_order) = body_from_smlib_ordered(&r)?;
let history = match (order, result_order) {
(Some(o), Some(ro)) => match r.history().map_err(|e| failed(what, "history", e))?.history(&[o], &ro) {
Ok(mut h) => {
let operand = History::identity(body).edges;
let replaced = blended(body, edges, &h);
solidcraft_smlib::history::blends(&mut h, &operand, &replaced);
Some(h)
}
Err(e) => {
log::debug!(target: "smlib", "{what} history: {e}");
None
}
},
_ => None,
};
Ok(out.with_checked_history(history, &record::counts(&[body])).inherit_regularization_protection(body))
})
}
/// The operand's edges the blend replaced: each picked edge, and every edge no result edge
/// comes from (the rest of the tangent chains), in the operand's edge order.
fn blended(body: &Body, picked: &[usize], h: &History) -> Vec<usize> {
let mut out: BTreeSet<usize> = picked.iter().copied().collect();
let kept: BTreeSet<usize> = h
.edges
.iter()
.flat_map(|e| e.origins.iter())
.filter_map(|o| match o {
EdgeOrigin::Edge { operand: 0, edge } => Some(*edge),
_ => None,
})
.collect();
out.extend((0..body.edge_count()).filter(|e| !kept.contains(e)));
out.into_iter().collect()
}
}
#[cfg(not(feature = "smlib"))]
mod imp {
use solidcraft_geom::Vec3;
use super::Kind;
use crate::blend_variable::RadiusSpline;
use crate::body::Body;
use crate::{KernelError, Result};
fn not_built() -> KernelError {
KernelError::Invalid("not supported yet: this build has no SMLib".into())
}
pub(super) fn blend(_: &Body, _: &str, _: Kind, _: &[Vec3], _: &[f64]) -> Result<Body> {
Err(not_built())
}
pub(super) fn variable(_: &Body, _: Vec3, _: Vec3, _: &RadiusSpline) -> Result<Body> {
Err(not_built())
}
}
#[cfg(all(test, feature = "smlib"))]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use solidcraft_geom::Vec3;
use super::*;
use crate::history::{Change, FaceOrigin};
use crate::{box_solid, measure};
const QUARTER: f64 = 1.0 - std::f64::consts::FRAC_PI_4;
fn cube() -> Body {
box_solid(Vec3::new(0.0, 0.0, 0.0), Vec3::new(20.0, 20.0, 20.0)).unwrap()
}
fn volume(b: &Body) -> f64 {
measure(b).unwrap().volume
}
/// Within the kernel measure's accuracy on a curved face (a few 1e-6 of the volume; SMLib's
/// own exact volumes match to 1e-3 mm³ in the C API's tests).
fn near(got: f64, want: f64) -> bool {
(got - want).abs() <= 5e-6 * want.abs()
}
/// The edge of `b` nearest `p`, in its edge order.
fn edge_at(b: &Body, p: Vec3) -> usize {
b.nearest_edge(p, 1e-3).unwrap().unwrap().0
}
#[test]
fn a_round_loses_its_corner_and_its_face_comes_from_the_edge() {
let b = cube();
let at = Vec3::new(10.0, 20.0, 20.0);
let r = imp::blend(&b, "fillet", Kind::Round, &[at], &[2.0]).unwrap();
assert!(near(volume(&r), 8000.0 - QUARTER * 4.0 * 20.0), "{}", volume(&r));
assert_eq!(r.face_count(), 7);
let h = r.history().expect("a history");
let blends: Vec<_> = h.faces.iter().filter(|f| f.change == Change::Generated).collect();
assert_eq!(blends.len(), 1);
assert_eq!(blends[0].origins, vec![FaceOrigin::Edge { operand: 0, edge: edge_at(&b, at) }]);
}
#[test]
fn a_pick_at_a_vertex_takes_the_nearest_edge_by_its_midpoint() {
// (20, 20, 10) is on the vertical edge only; (20, 20, 20) is a corner of three edges, which
// the body's own order resolves as truck's blends do, never left to SMLib.
let b = cube();
assert!(imp::blend(&b, "fillet", Kind::Round, &[Vec3::new(20.0, 20.0, 20.0)], &[1.0]).is_ok());
assert!(imp::blend(&b, "fillet", Kind::Round, &[Vec3::new(60.0, 60.0, 60.0)], &[1.0]).is_err());
}
#[test]
fn a_pick_where_a_seam_meets_a_rim_takes_the_rim() {
// Every point of the top rim's circle, the seam's top end among them, is on the rim.
let c = crate::cylinder(Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 8.0, 10.0).unwrap();
assert!(c.edge_count() > 2, "the cylinder has a seam");
for k in 0..8 {
let a = k as f64 * std::f64::consts::FRAC_PI_4;
let picked = imp::resolve(&c, &[Vec3::new(8.0 * a.cos(), 8.0 * a.sin(), 10.0)]).unwrap();
let [_, _, z] = picked.points[0];
assert!((z - 10.0).abs() < 1e-9, "the pick at {a} took an edge through z = {z}");
}
}
#[test]
fn each_edge_takes_its_own_size() {
let b = cube();
let picks = [Vec3::new(10.0, 20.0, 20.0), Vec3::new(10.0, 0.0, 20.0)];
let r = imp::blend(&b, "fillet", Kind::Round, &picks, &[2.0, 3.0]).unwrap();
assert!(near(volume(&r), 8000.0 - QUARTER * 13.0 * 20.0), "{}", volume(&r));
let c = imp::blend(&b, "chamfer", Kind::Chamfer, &picks, &[2.0, 3.0]).unwrap();
assert!(near(volume(&c), 8000.0 - 0.5 * 13.0 * 20.0), "{}", volume(&c));
assert_eq!(c.history().map(|h| h.faces.iter().filter(|f| matches!(f.origins[..], [FaceOrigin::Edge { .. }])).count()), Some(2));
}
#[test]
fn a_linear_radius_runs_from_the_start_end() {
// 3 at x = 0 to 1 at x = 20: (1 - pi/4) L (r0^2 + r0 r1 + r1^2) / 3 taken away, from
// either end given as the start with the law reversed.
let b = cube();
let at = Vec3::new(10.0, 20.0, 20.0);
let law = RadiusSpline { degree: 1, knots: vec![0.0, 0.0, 1.0, 1.0], radii: vec![3.0, 1.0] };
let r = imp::variable(&b, at, Vec3::new(0.0, 20.0, 20.0), &law).unwrap();
let want = 8000.0 - QUARTER * 20.0 * 13.0 / 3.0;
assert!((volume(&r) - want).abs() < 0.05, "{}", volume(&r));
let back = imp::variable(&b, at, Vec3::new(20.0, 20.0, 20.0), &law.reversed()).unwrap();
assert!((volume(&back) - volume(&r)).abs() < 1e-6 * 8000.0);
}
/// The ladder's 08/006: a 2 mm chamfer of a cylinder's top rim. SMLib's cone and rail lie some
/// 2e-5 mm off the faces they bound, more than the kernel's working tolerance; the body keeps
/// the tolerances SMLib's record gave them, so its own record (and the saved body's) is valid
/// and SMLib's check takes it back.
#[test]
fn an_smlib_blend_keeps_its_record_valid() {
let c = crate::cylinder(Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 10.0, 20.0).unwrap();
let r = imp::blend(&c, "chamfer", Kind::Chamfer, &[Vec3::new(10.0, 0.0, 20.0)], &[2.0]).unwrap();
let v = crate::validity(&r);
assert!(v.record.is_ok(), "{:?}", v.record);
assert!(matches!(v.smlib, Some(crate::SmlibCheck::Valid)), "{:?}", v.smlib);
let saved = crate::decode_native_body(&crate::encode_native_body(&r).unwrap()).unwrap();
assert!(crate::validity(&saved).record.is_ok());
assert!(
crate::validity(&saved.placed(&truck_modeling::Matrix4::from_translation(truck_modeling::Vector3::new(5.0, 0.0, 0.0))).unwrap())
.record
.is_ok()
);
}
#[test]
fn options_smlib_cannot_build_are_refused() {
assert!(unsupported("x").to_string().contains("not supported yet"));
let b = cube();
assert!(imp::blend(&b, "fillet", Kind::Round, &[Vec3::new(10.0, 20.0, 20.0)], &[f64::NAN]).is_err());
assert!(imp::blend(&b, "fillet", Kind::Round, &[Vec3::new(f64::NAN, 20.0, 20.0)], &[1.0]).is_err());
assert!(imp::blend(&b, "fillet", Kind::Round, &[], &[]).is_err());
}
}
+6
View File
@@ -210,6 +210,9 @@ pub fn fillet_variable_points(body: &Body, edge_at: Vec3, start: Vec3, points: &
pub fn fillet_variable_spline(body: &Body, edge_at: Vec3, start: Vec3, spline: &RadiusSpline) -> Result<Body> {
body.require_brep("fillet")?;
spline.check()?;
if crate::blend_smlib::selected()? {
return crate::blend_smlib::fillet_variable(body, edge_at, start, spline);
}
let edge = crate::surfaces::edge_near(body, edge_at).ok_or_else(|| KernelError::Invalid(format!("no edge at {edge_at:?}")))?;
guard("variable fillet", || {
let Ok(s) = EdgeSetup::new(body, &edge) else {
@@ -599,6 +602,9 @@ pub fn fillet_chord(body: &Body, edges: &[Vec3], chord: f64) -> Result<Body> {
if edges.is_empty() || edges.len() > 1000 {
return Err(KernelError::Invalid("select 1…1000 edges".into()));
}
if crate::blend_smlib::selected()? {
return crate::blend_smlib::fillet_chord(body, edges, chord);
}
let found: Vec<mt::Edge> = edges
.iter()
.map(|p| crate::surfaces::edge_near(body, *p).ok_or_else(|| KernelError::Invalid(format!("no edge at {p:?}"))))
+44 -7
View File
@@ -68,6 +68,19 @@ pub struct Body {
/// placed copy, which keeps them where they are, need not turn them again: searching every
/// revolved face of every placed occurrence was most of the time moving an assembly took.
pub(crate) phased: bool,
/// The tolerances the record this solid was decoded from held for its edges and vertices
/// (another kernel's approximations, which lie farther from their faces than this kernel's
/// working tolerance), so that its own record of the solid holds them again. `None`: the
/// working tolerance everywhere. Dropped by anything that makes a new solid.
pub(crate) tolerances: Option<std::sync::Arc<Tolerances>>,
}
/// Per edge and per vertex, in [`Body::unique_edges`] and [`Body::unique_vertices`] order, the
/// tolerance a decoded record gave it (see `Body::tolerances`).
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct Tolerances {
pub(crate) edges: Vec<f64>,
pub(crate) vertices: Vec<f64>,
}
/// The meshes made of one solid (see [`Body::keeping_meshes`]), by tolerance, angle and
@@ -542,7 +555,7 @@ pub(crate) fn edge_order<'a, P: 'a, C: 'a, S: 'a>(
(edges, bounds)
}
fn polyline_mid(pts: &[Vec3]) -> (Vec3, f64) {
pub(crate) fn polyline_mid(pts: &[Vec3]) -> (Vec3, f64) {
let len: f64 = pts.windows(2).map(|w| w.first().zip(w.get(1)).map(|(a, b)| a.dist(*b)).unwrap_or(0.0)).sum();
let mut acc = 0.0;
for w in pts.windows(2) {
@@ -684,6 +697,7 @@ impl Body {
display_of: None,
meshes: None,
phased: true,
tolerances: None,
})
}
@@ -736,6 +750,7 @@ impl Body {
display_of: None,
meshes: None,
phased: true,
tolerances: None,
})
}
@@ -753,12 +768,16 @@ impl Body {
return Err(KernelError::Invalid("degenerate placement".into()));
}
let rigid = crate::ops::rigid_placement(&a);
let out = Body::placed_as_is(mt::builder::transformed(&*self.solid, *m), det < 0.0, rigid.then_some(self.coarse_missing).flatten())?;
// A placement that does not change distances leaves the boundaries exactly as far off
// their surfaces as they were, so the file's own figure still describes them (see
// [`Body::boundary_tol`]); one that scales the part does not, and the copy declares
// nothing. The same rule as the mesh count above.
Ok(if rigid { out.with_boundary_tol(self.boundary_tol) } else { out })
let mut placed = Body::placed_as_is(mt::builder::transformed(&*self.solid, *m), det < 0.0, rigid.then_some(self.coarse_missing).flatten())?;
// A rigid placement moves every edge and vertex as it is, in the same order, and leaves
// the boundaries exactly as far off their surfaces as they were, so the file's own figure
// still describes them (see [`Body::boundary_tol`]); one that scales the part does not,
// and the copy declares nothing. The same rule as the mesh count above.
if rigid {
placed.tolerances = self.tolerances.clone();
placed = placed.with_boundary_tol(self.boundary_tol);
}
Ok(placed)
}
pub(crate) fn inherit_regularization_protection(mut self, input: &Body) -> Self {
@@ -1240,6 +1259,24 @@ impl Body {
solid.edge_iter().filter(|e| seen.insert(e.id())).collect()
}
/// The solid's vertices, each once, in the order [`Solid::vertex_iter`] first meets them.
pub(crate) fn unique_vertices(solid: &Solid) -> Vec<mt::Vertex> {
let mut seen = std::collections::HashSet::new();
solid.vertex_iter().filter(|v| seen.insert(v.id())).collect()
}
/// The record tolerances this body keeps for its own solid (`Body::tolerances`), when they
/// still number its edges and vertices.
pub(crate) fn kept_tolerances(&self) -> Option<&Tolerances> {
self.tolerances.as_deref().filter(|t| t.edges.len() == self.edge_count() && t.vertices.len() == Body::unique_vertices(&self.solid).len())
}
/// This body keeping `t` as its record tolerances (see `Body::tolerances`).
pub(crate) fn with_tolerances(mut self, t: Option<Tolerances>) -> Self {
self.tolerances = t.map(std::sync::Arc::new);
self
}
/// Triangulate with chord tolerance `tol` (mm). Triangles carry their face index.
pub fn tessellate(&self, tol: f64) -> Result<Mesh> {
self.tessellate_with(tol, false)
+1
View File
@@ -16,6 +16,7 @@ mod backend;
mod blend;
mod blend_corner;
mod blend_fit;
mod blend_smlib;
mod blend_variable;
mod blend_vertex;
#[cfg(test)]
+2
View File
@@ -127,6 +127,7 @@ pub fn mesh_body(positions: &[Vec3], triangles: &[[u32; 3]]) -> Result<Body> {
display_of: None,
meshes: None,
phased: false,
tolerances: None,
regularization_protected: false,
})
}
@@ -227,6 +228,7 @@ impl Body {
display_of: None,
meshes: None,
phased: false,
tolerances: None,
regularization_protected: self.regularization_protected,
})
}
+10 -3
View File
@@ -148,12 +148,12 @@ pub fn decode_native_body(bytes: &[u8]) -> Result<Body> {
validate_value(&value, 0)?;
let saved: SavedBody = serde_json::from_value(value).map_err(invalid)?;
validate_topology(&saved)?;
let solid = adapter::decode(saved.solid)?;
let (solid, numbering) = adapter::decode_numbered(saved.solid, adapter::Producer::Own)?;
let Runtime { size, coarse_missing } = saved.runtime;
if !(size.is_finite() && size >= 0.0) || coarse_missing.is_some_and(|(tol, n)| !tol.is_finite() || n > MAX_ITEMS as u64) {
return Err(invalid("runtime values"));
}
let mut body = Body {
let body = Body {
solid: Arc::new(solid),
size,
volume: Some(std::sync::Arc::new(std::sync::Mutex::new(None))),
@@ -167,8 +167,12 @@ pub fn decode_native_body(bytes: &[u8]) -> Result<Body> {
display_of: None,
meshes: None,
phased: false,
tolerances: None,
regularization_protected: false,
};
// A body made by another kernel keeps the tolerances its record saved for it.
let tolerances = adapter::decoded_tolerances(&numbering, &body);
let mut body = body.with_tolerances(tolerances);
metadata::apply(&mut body, saved.metadata)?;
Ok(body)
})
@@ -193,7 +197,10 @@ pub(crate) fn body_from_smlib_ordered(brep: &solidcraft_smlib::Brep) -> Result<(
let (solid, numbering) = adapter::decode_numbered(record, adapter::Producer::Foreign)?;
let body = Body::oriented(solid)?;
let kept = numbering.kept_by(&body.solid);
Ok((body, kept.then_some(order)))
// SMLib's approximated edges lie farther from their faces than the kernel's working
// tolerance: the body keeps the tolerances the record gave them, so its own record does.
let tolerances = adapter::decoded_tolerances(&numbering, &body);
Ok((body.with_tolerances(tolerances), kept.then_some(order)))
})
}
+55 -13
View File
@@ -100,19 +100,23 @@ pub(super) fn encode(body: &Body) -> Result<b::Brep> {
encode_within(body, working_tolerance(body))?
}
/// The record of `body`'s solid with `tolerance` for the body, its edges and vertices.
/// `Ok(Err(_))`: some edge lies farther than that from a face it bounds, so no pcurve fits it
/// within the tolerance.
/// The record of `body`'s solid with `tolerance` for the body, its edges and vertices, or for
/// an edge or vertex the larger tolerance the body keeps for it from the record it was decoded
/// from (`Body::tolerances`). `Ok(Err(_))`: some edge lies farther than its tolerance from a face
/// it bounds, so no pcurve fits it.
pub(super) fn encode_within(body: &Body, tolerance: f64) -> Result<Result<b::Brep>> {
let solid = &body.solid;
let shells = solid.boundaries();
let regions = regions(body)?;
let (edge_tol, vertex_tol) = kept_tolerances(body);
let edge_tolerance = |e: &mt::Edge| edge_tol.get(&e.id()).map_or(tolerance, |t| t.max(tolerance));
let vertex_tolerance = |v: &mt::Vertex| vertex_tol.get(&v.id()).map_or(tolerance, |t| t.max(tolerance));
// Faces in truck's order; edges and vertices in first appearance, each edge's curve record
// and interval written when it first appears.
let mut face_index = HashMap::new();
let mut edge_index = HashMap::new();
let mut edges: Vec<(mt::Edge, b::Curve, [f64; 2])> = Vec::new();
let mut edges: Vec<(mt::Edge, b::Curve, [f64; 2], f64)> = Vec::new();
let mut uses: Vec<Vec<b::EdgeUseRef>> = Vec::new();
let mut faces = Vec::new();
let mut shell_records = Vec::new();
@@ -136,7 +140,7 @@ pub(super) fn encode_within(body: &Body, tolerance: f64) -> Result<Result<b::Bre
None => {
let curve = e.curve();
let (t0, t1) = curve.try_range_tuple().ok_or_else(|| invalid("unbounded edge curve"))?;
edges.push((e.absolute_clone(), save_curve(&curve, 0)?, [t0, t1]));
edges.push((e.absolute_clone(), save_curve(&curve, 0)?, [t0, t1], edge_tolerance(e)));
uses.push(Vec::new());
edge_index.insert(e.id(), edges.len() - 1);
edges.len() - 1
@@ -156,18 +160,18 @@ pub(super) fn encode_within(body: &Body, tolerance: f64) -> Result<Result<b::Bre
let mut vertex_index = HashMap::new();
let mut vertices = Vec::new();
let mut edge_records = Vec::with_capacity(edges.len());
for ((e, curve, interval), uses) in edges.into_iter().zip(uses) {
for ((e, curve, interval, edge_tolerance), uses) in edges.into_iter().zip(uses) {
if uses.len() > 2 {
return Err(unsupported("the radial order of an edge with more than two uses"));
}
let mut vertex = |x: &mt::Vertex| {
*vertex_index.entry(x.id()).or_insert_with(|| {
vertices.push(b::Vertex { point: point(x.point()), tolerance });
vertices.push(b::Vertex { point: point(x.point()), tolerance: vertex_tolerance(x) });
vertices.len() - 1
})
};
let ends = [vertex(e.absolute_front()), vertex(e.absolute_back())];
edge_records.push(b::Edge { curve, interval, vertices: [index(ends[0])?, index(ends[1])?], tolerance, uses });
edge_records.push(b::Edge { curve, interval, vertices: [index(ends[0])?, index(ends[1])?], tolerance: edge_tolerance, uses });
}
let result = b::Brep { version: b::VERSION, tolerance, regions, shells: shell_records, faces, edges: edge_records, vertices };
result.validate().map_err(invalid)?;
@@ -181,7 +185,7 @@ fn face_record(
face: &mt::Face,
wires: &[mt::Wire],
edge_index: &HashMap<mt::EdgeID, usize>,
edges: &[(mt::Edge, b::Curve, [f64; 2])],
edges: &[(mt::Edge, b::Curve, [f64; 2], f64)],
tolerance: f64,
) -> Result<Result<b::Face>> {
let surface = save_surface(&face.surface(), 0)?;
@@ -191,8 +195,8 @@ fn face_record(
.edge_iter()
.map(|e| {
let i = *edge_index.get(&e.id()).ok_or_else(|| invalid("edge index"))?;
let (_, curve, interval) = edges.get(i).ok_or_else(|| invalid("edge index"))?;
Ok(b::pcurve::EdgeUse { edge: index(i)?, curve, interval: *interval, forward: e.orientation() })
let (_, curve, interval, tolerance) = edges.get(i).ok_or_else(|| invalid("edge index"))?;
Ok(b::pcurve::EdgeUse { edge: index(i)?, curve, interval: *interval, forward: e.orientation(), tolerance: *tolerance })
})
.collect::<Result<Vec<_>>>()?;
uses.push(l);
@@ -222,7 +226,7 @@ pub(super) fn face_alone(face: &mt::Face, tolerance: f64) -> Result<b::Face> {
if let std::collections::hash_map::Entry::Vacant(slot) = edge_index.entry(e.id()) {
let curve = e.curve();
let (t0, t1) = curve.try_range_tuple().ok_or_else(|| invalid("unbounded edge curve"))?;
edges.push((e.absolute_clone(), save_curve(&curve, 0)?, [t0, t1]));
edges.push((e.absolute_clone(), save_curve(&curve, 0)?, [t0, t1], tolerance));
slot.insert(edges.len() - 1);
}
}
@@ -337,10 +341,12 @@ pub(super) enum Producer {
/// The solid of a record: validated, its geometry checked against its topology (truck builds
/// edges and faces from it unchecked), then built as the kernel built it.
#[cfg(test)]
pub(super) fn decode(body: b::Brep) -> Result<mt::Solid> {
decode_from(body, Producer::Own)
}
#[cfg(test)]
pub(super) fn decode_from(body: b::Brep, producer: Producer) -> Result<mt::Solid> {
decode_numbered(body, producer).map(|(solid, _)| solid)
}
@@ -352,6 +358,9 @@ pub(super) fn decode_from(body: b::Brep, producer: Producer) -> Result<mt::Solid
pub(super) struct Numbering {
pub faces: Vec<(mt::FaceID, std::collections::HashSet<mt::EdgeID>)>,
pub edges: Vec<mt::EdgeID>,
/// Each record edge's and vertex's tolerance, by its identity in the solid.
pub edge_tolerances: Vec<(mt::EdgeID, f64)>,
pub vertex_tolerances: Vec<(mt::VertexID, f64)>,
}
#[cfg_attr(not(feature = "smlib"), allow(dead_code))]
@@ -367,6 +376,32 @@ impl Numbering {
}
}
/// The tolerances a decoded record gave `body`'s edges and vertices, in the body's order, where
/// any is larger than the body's working tolerance (another kernel's approximated geometry);
/// `None` otherwise. An edge or vertex the record does not name (a face rebuilt on decode) takes
/// the largest the record gave.
pub(super) fn decoded_tolerances(numbering: &Numbering, body: &Body) -> Option<crate::body::Tolerances> {
let floor = working_tolerance(body);
let most = numbering.edge_tolerances.iter().map(|(_, t)| *t).chain(numbering.vertex_tolerances.iter().map(|(_, t)| *t)).fold(0.0, f64::max);
if !(most.is_finite() && most > floor) {
return None;
}
let edges: HashMap<mt::EdgeID, f64> = numbering.edge_tolerances.iter().copied().collect();
let vertices: HashMap<mt::VertexID, f64> = numbering.vertex_tolerances.iter().copied().collect();
Some(crate::body::Tolerances {
edges: Body::unique_edges(&body.solid).iter().map(|e| edges.get(&e.id()).copied().unwrap_or(most)).collect(),
vertices: Body::unique_vertices(&body.solid).iter().map(|v| vertices.get(&v.id()).copied().unwrap_or(most)).collect(),
})
}
/// The tolerances `body` keeps for its edges and vertices (`Body::tolerances`), by identity.
fn kept_tolerances(body: &Body) -> (HashMap<mt::EdgeID, f64>, HashMap<mt::VertexID, f64>) {
let Some(t) = body.kept_tolerances() else { return (HashMap::new(), HashMap::new()) };
let edges = Body::unique_edges(&body.solid).iter().map(mt::Edge::id).zip(t.edges.iter().copied()).collect();
let vertices = Body::unique_vertices(&body.solid).iter().map(mt::Vertex::id).zip(t.vertices.iter().copied()).collect();
(edges, vertices)
}
/// [`decode_from`], with each record face's and edge's identity in the solid: a caller holding
/// indices into the record (an SMLib history) checks with them that the solid's face and edge
/// order is the record's.
@@ -375,8 +410,10 @@ pub(super) fn decode_numbered(body: b::Brep, producer: Producer) -> Result<(mt::
body.check_geometry().map_err(invalid)?;
compatible(&body, producer)?;
let b::Brep { shells, faces, edges, vertices, .. } = body;
let vertex_tolerances_in: Vec<f64> = vertices.iter().map(|v| v.tolerance).collect();
let vertices: Vec<mt::Vertex> = vertices.into_iter().map(|v| mt::Vertex::new(p(v.point))).collect();
let vertex = |i: u32| vertices.get(i as usize).ok_or_else(|| invalid("edge vertices"));
let edge_tolerances_in: Vec<f64> = edges.iter().map(|e| e.tolerance).collect();
let edges = edges
.into_iter()
.map(|e| Ok(mt::Edge::new_unchecked(vertex(e.vertices[0])?, vertex(e.vertices[1])?, load_edge_curve(e.curve, e.interval)?)))
@@ -406,7 +443,12 @@ pub(super) fn decode_numbered(body: b::Brep, producer: Producer) -> Result<(mt::
return Err(invalid("non-manifold shell"));
}
let faces = face_ids.into_iter().collect::<Option<Vec<_>>>().ok_or_else(|| invalid("a face no shell uses"))?;
let numbering = Numbering { faces, edges: edges.iter().map(mt::Edge::id).collect() };
let numbering = Numbering {
faces,
edges: edges.iter().map(mt::Edge::id).collect(),
edge_tolerances: edges.iter().map(mt::Edge::id).zip(edge_tolerances_in).collect(),
vertex_tolerances: vertices.iter().map(mt::Vertex::id).zip(vertex_tolerances_in).collect(),
};
Ok((mt::Solid::new_unchecked(shells), numbering))
}
/// A face's topology as recorded, references already checked: built as the kernel built it,
+1
View File
@@ -516,6 +516,7 @@ fn divided(body: &Body, faces: &[usize], other: &[(mt::Edge, Piece)], fr: &Frame
display_of: None,
meshes: None,
phased: false,
tolerances: None,
};
Ok(Some(record::rebuilt_body(split, &[&body.solid], &rec)))
}
+2
View File
@@ -648,6 +648,7 @@ pub(crate) fn split_edges_at(body: &Body, points: &[Vec3]) -> Result<Body> {
display_of: None,
meshes: None,
phased: false,
tolerances: None,
regularization_protected: true,
};
Ok(crate::record::rebuilt_body(split, &[&body.solid], &rec))
@@ -1186,6 +1187,7 @@ pub(crate) fn split_any(body: &Body, faces: &[usize], tool: &SplitTool) -> Resul
display_of: None,
meshes: None,
phased: false,
tolerances: None,
regularization_protected: true,
};
Ok((crate::record::rebuilt_body(split, &[&body.solid], &imp.rec), imp))
+1
View File
@@ -45,6 +45,7 @@ fn surface_of(shell: mt::Shell) -> Body {
display_of: None,
meshes: None,
phased: false,
tolerances: None,
regularization_protected: false,
}
}
+1 -1
View File
@@ -1 +1 @@
d041e7f9679436a4ad3a9620861965dd41105a27
ca2559f785a662c9ec589aa2cdcfaf3217597db8
+61 -9
View File
@@ -24,8 +24,10 @@
//! - **Pcurves:** computed from each edge record on its face's surface record
//! (`solidcraft_geom::brep::pcurve`), never SMLib's UV trims, moved by whole periods into the
//! face's domain.
//! - **Tolerances:** SMLib's intersection tolerance, or 10⁻⁶ of the body's size (at least 10⁻⁵ mm)
//! when larger, for the body, every edge and every vertex.
//! - **Tolerances:** each edge's and vertex's own as SMLib reports it (`EDGE_TOLERANCE`,
//! `VERTEX_TOLERANCE`), never below a floor of 10⁻⁶ of the body's size (at least 10⁻⁵ mm),
//! which is the body's; from a library without them, SMLib's intersection tolerance (or the
//! floor where larger) for the body, every edge and every vertex.
use std::f64::consts::TAU;
use std::collections::HashMap;
@@ -214,6 +216,8 @@ const F64_IDS: &[i32] = &[
FACE_TORUS_REF_DIRECTION,
FACE_TORUS_MAJOR_RADIUS,
FACE_TORUS_MINOR_RADIUS,
EDGE_TOLERANCE,
VERTEX_TOLERANCE,
];
// The kind values as the arrays hold them (int64); a local item shadows the glob import.
@@ -561,6 +565,7 @@ fn onto(r: [f64; 2], natural: b::Natural) -> [f64; 2] {
// ---- The mapping ----------------------------------------------------------------------------
/// One face before renumbering: loops relative to the record's normal, as (edge, forward).
#[derive(Clone)]
struct Face {
surface: b::Surface,
domain: [[f64; 2]; 2],
@@ -607,11 +612,26 @@ fn native_ordered(arrays: &Arrays) -> Res<(b::Brep, Report, Order)> {
return Err(invalid("extent"));
}
let floor = (1e-6 * size).max(1e-5);
let tolerance = match flat.f(TOLERANCE) {
let largest = match flat.f(TOLERANCE) {
[t] if t.is_finite() && *t > floor => *t,
[_] => floor,
_ => return Err(invalid("tolerance")),
};
// Each edge's and vertex's own tolerance where SMLib gives them (never below the floor); the
// body's is then the floor, and only what needs more carries more. Without them, every
// tolerance is the largest SMLib reports for the body.
let own = |id: i32, n: usize| -> Res<Option<Vec<f64>>> {
match flat.f(id) {
[] => Ok(None),
t if t.len() == n && t.iter().all(|x| x.is_finite() && *x > 0.0) => Ok(Some(t.iter().map(|x| x.max(floor)).collect())),
_ => Err(invalid("edge or vertex tolerances")),
}
};
let n_vertex_points = flat.f(VERTEX_POSITION).len() / 3;
let edge_tolerances = own(EDGE_TOLERANCE, flat.i(EDGE_CURVE_TYPE).len())?;
let vertex_tolerances = own(VERTEX_TOLERANCE, n_vertex_points)?;
let tolerance = if edge_tolerances.is_some() && vertex_tolerances.is_some() { floor } else { largest };
let tolerances = Tolerances { body: tolerance, edges: edge_tolerances, vertices: vertex_tolerances };
// Topology blocks.
let n_faceuses = flat.i(FACEUSE_FACE).len();
@@ -695,7 +715,7 @@ fn native_ordered(arrays: &Arrays) -> Res<(b::Brep, Report, Order)> {
faces.push(Face { surface, domain, agrees: true, loops: out });
}
for face in &mut faces {
if outer_area(face, &edges, tolerance)? < 0.0 {
if outer_area(face, &edges, largest)? < 0.0 {
face.agrees = false;
for l in &mut face.loops {
if let Loop::Edges(uses) = l {
@@ -796,10 +816,22 @@ fn native_ordered(arrays: &Arrays) -> Res<(b::Brep, Report, Order)> {
}
let shells: Vec<Vec<(u32, bool)>> = order.iter().filter_map(|s| shells.get(*s as usize).cloned()).collect();
canonical(Parts { faces, edges, shells, regions }, n_vertices, flat, tolerance).map(|(brep, order)| (brep, report, order))
let parts = Parts { faces, edges, shells, regions };
let made = match canonical(parts.clone(), n_vertices, flat, &tolerances) {
// SMLib's own tolerance for an edge or vertex can understate what its approximated
// geometry needs here (a blend's end curve 2.3e-5 mm off its plane at 2e-5): then the
// largest SMLib reports for the body, everywhere, as without per-edge tolerances.
Err(e) if tolerances.edges.is_some() || tolerances.vertices.is_some() => {
let uniform = Tolerances { body: largest, edges: None, vertices: None };
canonical(parts, n_vertices, flat, &uniform).map_err(|_| e)
}
other => other,
};
made.map(|(brep, order)| (brep, report, order))
}
/// The mapped records before renumbering.
#[derive(Clone)]
struct Parts {
faces: Vec<Face>,
edges: Vec<Edge>,
@@ -811,9 +843,28 @@ struct Parts {
/// the faces (a face whose first oriented face is reversed walked backwards), vertices through
/// the edges and then the vertex loops, with each face's pcurves; validated. With it, the
/// canonical index of each array face and edge.
fn canonical(parts: Parts, n_vertices: usize, flat: View, tolerance: f64) -> Res<(b::Brep, Order)> {
/// The record's tolerances: the body's, and each array edge's and vertex's own where given.
struct Tolerances {
body: f64,
edges: Option<Vec<f64>>,
vertices: Option<Vec<f64>>,
}
impl Tolerances {
fn edge(&self, e: usize) -> f64 {
self.edges.as_ref().and_then(|t| t.get(e)).copied().unwrap_or(self.body)
}
fn vertex(&self, v: usize) -> f64 {
self.vertices.as_ref().and_then(|t| t.get(v)).copied().unwrap_or(self.body)
}
}
fn canonical(parts: Parts, n_vertices: usize, flat: View, tolerances: &Tolerances) -> Res<(b::Brep, Order)> {
let tolerance = tolerances.body;
let Parts { mut faces, mut edges, shells, regions } = parts;
let n_edges = edges.len();
// A copy made by the split keeps the tolerance of the array edge or vertex it copies.
let (edge_source, vertex_source) = split_shared(&mut faces, &mut edges, &shells, n_vertices)?;
let n_vertices = vertex_source.len();
let mut face_new: Vec<Option<u32>> = vec![None; faces.len()];
@@ -899,7 +950,8 @@ fn canonical(parts: Parts, n_vertices: usize, flat: View, tolerance: f64) -> Res
let mut edge_records = Vec::with_capacity(edge_order.len());
for ((&e, vertices), uses) in edge_order.iter().zip(edge_vertices).zip(edge_uses) {
let edge = edges.get(e as usize).ok_or_else(|| invalid("edge"))?;
edge_records.push(b::Edge { curve: edge.curve.clone(), interval: edge.interval, vertices, tolerance, uses });
let source = edge_source.get(e as usize).copied().ok_or_else(|| invalid("edge"))?;
edge_records.push(b::Edge { curve: edge.curve.clone(), interval: edge.interval, vertices, tolerance: tolerances.edge(source), uses });
}
let mut face_records = Vec::with_capacity(face_order.len());
for (fi, &f) in face_order.iter().enumerate() {
@@ -935,7 +987,7 @@ fn canonical(parts: Parts, n_vertices: usize, flat: View, tolerance: f64) -> Res
.iter()
.map(|&v| {
let at = vertex_source.get(v as usize).copied().ok_or_else(|| invalid("vertex"))?;
Ok(b::Vertex { point: v3(positions, at, "vertex")?, tolerance })
Ok(b::Vertex { point: v3(positions, at, "vertex")?, tolerance: tolerances.vertex(at) })
})
.collect::<Res<Vec<_>>>()?;
let shells = shells
@@ -1069,7 +1121,7 @@ fn pcurves(surface: &b::Surface, domain: [[f64; 2]; 2], loops: &[Loop], edges: &
let mut loop_uses = Vec::with_capacity(l.len());
for &(e, forward) in l {
let edge = edges.get(e as usize).ok_or_else(|| invalid("edge"))?;
loop_uses.push(b::pcurve::EdgeUse { edge: e, curve: &edge.curve, interval: edge.interval, forward });
loop_uses.push(b::pcurve::EdgeUse { edge: e, curve: &edge.curve, interval: edge.interval, forward, tolerance: edge.tolerance });
}
uses.push(loop_uses);
}
+2
View File
@@ -100,6 +100,8 @@ pub const FACE_TORUS_AXIS: i32 = 160;
pub const FACE_TORUS_REF_DIRECTION: i32 = 161;
pub const FACE_TORUS_MAJOR_RADIUS: i32 = 162;
pub const FACE_TORUS_MINOR_RADIUS: i32 = 163;
pub const EDGE_TOLERANCE: i32 = 164;
pub const VERTEX_TOLERANCE: i32 = 165;
/// `SMLIB_FLAT_MAX_LEN`: no array is longer.
pub const MAX_LEN: usize = 1 << 27;
pub const SURFACE_NURBS: u8 = 0;
+3
View File
@@ -203,12 +203,15 @@ fn laid_out(brep: &b::Brep, trusted: bool, analytic: bool) -> R<Arrays> {
curve(&mut a, &e.curve)?;
a.i(flat::EDGE_VERTICES).extend([i64::from(e.vertices[0]), i64::from(e.vertices[1])]);
a.r(flat::EDGE_RANGE).extend(e.interval);
// Each edge's own tolerance, never below the body's.
a.r(flat::EDGE_TOLERANCE).push(e.tolerance.max(brep.tolerance));
}
let (mut lo, mut hi) = ([f64::INFINITY; 3], [f64::NEG_INFINITY; 3]);
for v in &brep.vertices {
a.i(flat::VERTEX_POINT_TYPE).push(1);
a.r3(flat::VERTEX_POSITION, v.point);
a.r(flat::VERTEX_TOLERANCE).push(v.tolerance.max(brep.tolerance));
for k in 0..3 {
lo[k] = lo[k].min(v.point[k]);
hi[k] = hi[k].max(v.point[k]);
+1 -1
View File
@@ -931,7 +931,7 @@ impl<'b, 'a> Builder<'b, 'a> {
let mut u = Vec::with_capacity(l.len());
for &(e, forward) in l {
let edge = self.edges.get(e as usize).ok_or("edge reference")?;
u.push(EdgeUse { edge: e, curve: &edge.curve, interval: edge.interval, forward });
u.push(EdgeUse { edge: e, curve: &edge.curve, interval: edge.interval, forward, tolerance: 0.0 });
}
uses.push(u);
}