Wave 237/509: kernel, kernel-cases: a convex blend overflowing a cliff is built, with its analytic answer (and 3 more)
4 commits, squashed; the originals stay reachable from tag archive/merge-main-develop. - 237f0ae74 jyviko: kernel, kernel-cases: a convex blend overflowing a cliff is built, with its analytic answer - 7d300984b jyviko: kernel, kernel-cases: a shell whose walls would meet is refused, saying so - 3be6ad433 jyviko: kernel: a tangent chain stops where the edges' convexity flips - 098fac037 jyviko: geom: a face's loops lie in one turn of its periodic surface Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
This commit is contained in:
@@ -388,7 +388,10 @@ pub fn chamfer_sides(body: &Body, edges: &[Vec3], d1: f64, side: ChamferSide, fl
|
||||
/// tangentially (within 0.5° at a shared vertex), until no more do. Each pick takes its edge as a
|
||||
/// blend does (a pick at a vertex where a seam meets a rim takes the rim). An edge only one face
|
||||
/// uses ends a chain, and so do a seam and a smooth edge (its two faces meet tangentially, within
|
||||
/// 0.5°): they have no corner to round. Picks that find no edge are refused.
|
||||
/// 0.5°): they have no corner to round. A chain does not run on into an edge of the other
|
||||
/// convexity (a boss's concave base running on tangentially as the convex rim of its overhang):
|
||||
/// a round cannot pass from one side of the faces to the other, so it ends at that vertex.
|
||||
/// Picks that find no edge are refused.
|
||||
pub fn tangent_chain(body: &Body, picks: &[Vec3]) -> Result<Vec<Vec3>> {
|
||||
/// Edges meeting at an angle below this continue one another.
|
||||
const TANGENT_DEGREES: f64 = 0.5;
|
||||
@@ -423,6 +426,23 @@ pub fn tangent_chain(body: &Body, picks: &[Vec3]) -> Result<Vec<Vec3>> {
|
||||
_ => false,
|
||||
}
|
||||
};
|
||||
// Whether edge `e` is convex at its end `v` (its faces meet at less than 180° inside the
|
||||
// body): the direction into its second face, square to it, points against its first face's
|
||||
// outward normal. A face lies to the left of its loop seen from its surface's natural normal.
|
||||
// None for an edge without exactly two uses, or where a normal cannot be found.
|
||||
let convex = |e: usize, v: u32| -> Option<bool> {
|
||||
let edge = rec.rec.edges.get(e)?;
|
||||
let [ua, ub] = edge.uses.as_slice() else { return None };
|
||||
let forward_b = match rec.rec.faces.get(ub.face as usize)?.loops.get(ub.r#loop as usize)? {
|
||||
Loop::Edges { edges } => edges.get(ub.position as usize)?.forward,
|
||||
Loop::Vertex { .. } => return None,
|
||||
};
|
||||
let p = rec.point(v)?;
|
||||
let along = rec.tangent(e, edge.vertices[0] != v)?;
|
||||
let natural_b = rec.normal_at(ub.face as usize, p, accept)? * rec.side(ub.face as usize)?;
|
||||
let into_b = natural_b.cross(if forward_b { along } else { along * -1.0 });
|
||||
Some(into_b.dot(rec.normal_at(ua.face as usize, p, accept)?) < 0.0)
|
||||
};
|
||||
let mut k = 0;
|
||||
while k < chain.len() {
|
||||
let Some(&e) = chain.get(k) else { break };
|
||||
@@ -435,7 +455,8 @@ pub fn tangent_chain(body: &Body, picks: &[Vec3]) -> Result<Vec<Vec3>> {
|
||||
if passed_over || chain.contains(&f) || other.uses.len() < 2 || !other.vertices.contains(&v) || smooth(f) {
|
||||
continue;
|
||||
}
|
||||
if leaving(f, v).is_some_and(|g| d.dot(g) < -cos) {
|
||||
let turns_over = matches!((convex(e, v), convex(f, v)), (Some(a), Some(b)) if a != b);
|
||||
if !turns_over && leaving(f, v).is_some_and(|g| d.dot(g) < -cos) {
|
||||
chain.push(f);
|
||||
}
|
||||
}
|
||||
@@ -610,7 +631,14 @@ mod tests {
|
||||
assert!(fillet_g2(&body, &[], 2.0).is_err());
|
||||
assert!(fillet_g2(&body, &[Vec3::new(f64::NAN, 0.0, 0.0)], 2.0).is_err());
|
||||
assert!(fillet_g2(&body, &[Vec3::new(1000.0, 0.0, 0.0)], 2.0).is_err());
|
||||
assert!(fillet_g2(&body, &edge, 25.0).is_err());
|
||||
// Wider than the faces (20), over their far edges, cliffs: cut as if they went on. The
|
||||
// section is the quintic from 25 out on each face, its inner control points a third and
|
||||
// two thirds of the way to the corner; it takes the region between it and the corner
|
||||
// inside the 20 x 20 section (clipped polygon of the curve, 2e5 pieces), along 20.
|
||||
let wide = fillet_g2(&body, &edge, 25.0).unwrap();
|
||||
let v = crate::measure(&wide).unwrap().volume;
|
||||
assert!((v - 6138.1606889014).abs() < 1e-6 * 6138.1606889014, "{v}");
|
||||
assert!(wide.validity().is_empty());
|
||||
}
|
||||
|
||||
/// A box with a cylinder joined across one side (#556): the bottom edge of that side runs
|
||||
@@ -813,7 +841,15 @@ mod tests {
|
||||
for point in [Vec3::new(slope * 2.0, 2.0, 20.0), Vec3::new(40.0 - slope * 2.0, 2.0, 20.0)] {
|
||||
assert!(has_vertex(&result, point, 1e-8));
|
||||
}
|
||||
assert!(chamfer(&body, &pick, 25.0).is_err());
|
||||
// Past the faces (20 deep, 20 high), over their far edges, cliffs: cut as if the
|
||||
// faces and the sloped end planes went on. The plane 25 back on each face takes, of
|
||||
// the section, z >= y - 5 (y, z in [0, 20]), each line of it 40 - 2 slope y long
|
||||
// between the end planes: 11500 - 4625 slope.
|
||||
let past = chamfer(&body, &pick, 25.0).unwrap();
|
||||
let removed = 11500.0 - 4625.0 * slope;
|
||||
let actual = crate::measure(&body).unwrap().volume - crate::measure(&past).unwrap().volume;
|
||||
assert!((actual - removed).abs() < 1e-6 * removed, "{actual} vs {removed}");
|
||||
assert!(past.validity().is_empty());
|
||||
// A fillet ends in each sloped end plane along an ellipse: the removed section's
|
||||
// length is taken at its centroid, c from the edge.
|
||||
let rounded = fillet(&body, &pick, 2.0).unwrap();
|
||||
|
||||
@@ -557,8 +557,17 @@ mod tests {
|
||||
let g = fillet_variable(&b, Vec3::new(10.0, 10.0, 30.0), Vec3::new(10.0, 0.0, 30.0), 3.0, 3.0).unwrap();
|
||||
let k = crate::fillet(&b, &[Vec3::new(10.0, 10.0, 30.0)], 3.0).unwrap();
|
||||
assert!(rel(measure(&g).unwrap().volume, measure(&k).unwrap().volume) < 1e-6);
|
||||
// Too big for the faces: refused.
|
||||
assert!(fillet_variable(&b, Vec3::new(10.0, 10.0, 30.0), Vec3::new(10.0, 0.0, 30.0), 2.0, 50.0).is_err());
|
||||
// Wider than the faces: past r = 10 the contact on the top leaves it across its far edge
|
||||
// x = 0 (past r = 30 the one on the side across its bottom), cliffs. The round is built as
|
||||
// if the faces went on, and what it removes is cut from the box: at each y the section of
|
||||
// the r x r square at the edge less the quarter disc about (10 - r, 30 - r), inside
|
||||
// [0, 10] x [0, 30], in closed form (a, b from the disc's centre: a, b in [0, r],
|
||||
// a^2 + b^2 >= r^2, a >= r - 10, b >= r - 30), integrated along y by Gauss-Legendre
|
||||
// between the kinks at r = 10 and 30: 2453.81232061 removed, 3546.18767939 left (to 1e-4,
|
||||
// as the variable rounds above).
|
||||
let w = fillet_variable(&b, Vec3::new(10.0, 10.0, 30.0), Vec3::new(10.0, 0.0, 30.0), 2.0, 50.0).unwrap();
|
||||
let vw = measure(&w).unwrap().volume;
|
||||
assert!(rel(vw, 3546.1876793935) < 1e-4, "{vw} vs 3546.1876793935");
|
||||
}
|
||||
|
||||
/// A radius through a middle point: straight pieces (exact: each piece removes
|
||||
@@ -704,9 +713,15 @@ mod tests {
|
||||
assert!(fillet_variable_spline(&b, at, start, &unclamped).is_err());
|
||||
let dips = RadiusSpline { radii: vec![2.0, 0.0, 0.0, 3.0, 2.0], ..law.clone() };
|
||||
assert!(fillet_variable_spline(&b, at, start, &dips).is_err());
|
||||
// A middle radius beyond the faces: too large.
|
||||
// A middle radius beyond the faces (up to 21.25 on the top 10 wide): the contact on the top
|
||||
// leaves it across its far edge, a cliff, and the round is cut as if the top went on: at
|
||||
// each y the r x r square at the edge less the quarter disc about (10 - r, 30 - r), inside
|
||||
// [0, 10] x [0, 30], in closed form, integrated along y (Gauss-Legendre, split at the
|
||||
// knot and where r crosses 10): 726.5334566720 removed.
|
||||
let big = RadiusSpline { radii: vec![2.0, 2.0, 40.0, 3.0, 2.0], ..law };
|
||||
assert!(matches!(fillet_variable_spline(&b, at, start, &big), Err(KernelError::TooLarge(_))));
|
||||
let wide = fillet_variable_spline(&b, at, start, &big).unwrap();
|
||||
let vw = fine_volume(&wide);
|
||||
assert!(rel(vw, 6000.0 - 726.5334566720) < 1e-4, "{vw} vs {}", 6000.0 - 726.5334566720);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -58,6 +58,6 @@ pub(crate) fn heal(body: &Body, keep: &[Vec<Vec3>]) -> Result<Body> {
|
||||
}
|
||||
}
|
||||
let tolerance = record.tolerance * 10.0;
|
||||
let (healed, history) = crate::offset_backend::run("healing", body, |b| b.merge_faces(&kept, tolerance))?;
|
||||
let (healed, history) = crate::offset_backend::run("healing", body, None, |b| b.merge_faces(&kept, tolerance))?;
|
||||
Ok(healed.with_checked_history(history, &crate::record::counts(&[body])).with_split_lines(body.splits.clone()))
|
||||
}
|
||||
|
||||
@@ -552,6 +552,17 @@ mod smlib {
|
||||
verified(&r, &plate, reach(2.5 * 1.5));
|
||||
}
|
||||
|
||||
/// A 40 x 40 x 10 plate with an r2 hole 3 from its side, shelled 1.5 open at the top: the
|
||||
/// hole's offset just reaches the side's, so the walls would meet along a line. Refused,
|
||||
/// saying so and with the wall, never as SMLib's bare status.
|
||||
#[test]
|
||||
fn a_shell_whose_walls_would_meet_is_refused_saying_so() {
|
||||
let hole = Loop2::circle(Vec2::new(5.0, 20.0), 2.0);
|
||||
let plate = extrude(&Plane::XY, &[Region2 { outer: square(0.0, 0.0, 40.0, 40.0), holes: vec![hole] }], 0.0, 10.0).unwrap().pop().unwrap();
|
||||
let e = offset_backend::shell(&plate, &[Vec3::new(35.0, 20.0, 10.0)], 1.5).unwrap_err().to_string();
|
||||
assert!(e.contains("the 1.5 mm walls would meet along a line"), "{e}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn offset_moves_a_face() {
|
||||
let b = ell();
|
||||
|
||||
@@ -26,10 +26,19 @@ fn failed(op: &str, what: &str, e: impl std::fmt::Display) -> KernelError {
|
||||
KernelError::Failed(format!("SMLib {op}: {what}: {e}"))
|
||||
}
|
||||
|
||||
/// The error for SMLib's refusal of `op`, in the words of the operation.
|
||||
fn refused(op: &str, e: SmError) -> KernelError {
|
||||
/// The error for SMLib's refusal of `op`, in the words of the operation; `wall` is its wall's
|
||||
/// thickness, where it has one.
|
||||
fn refused(op: &str, wall: Option<f64>, e: SmError) -> KernelError {
|
||||
match e {
|
||||
SmError::NoRoom => KernelError::Invalid(format!("{op}: the wall is too thick: it leaves no room inside the body")),
|
||||
// The walls just meet: thinner they stay apart, thicker they cross and merge.
|
||||
SmError::WallsTouch => {
|
||||
let wall = wall.map(|t| format!(" {t} mm")).unwrap_or_default();
|
||||
KernelError::Invalid(format!(
|
||||
"{op}: the{wall} walls would meet along a line (between a hole and the side, for example), which leaves no solid there; \
|
||||
use a thinner or thicker wall, or move the feature"
|
||||
))
|
||||
}
|
||||
SmError::NoMatch { point } => KernelError::Invalid(format!("{op}: no face at selection point {}", point + 1)),
|
||||
SmError::Ambiguous { point } => KernelError::Invalid(format!("{op}: selection point {} is as near to two faces", point + 1)),
|
||||
SmError::Unsupported => KernelError::Failed(format!("not supported yet: SMLib {op} for this geometry")),
|
||||
@@ -48,12 +57,18 @@ fn points(p: &[Vec3]) -> Vec<[f64; 3]> {
|
||||
}
|
||||
|
||||
/// Runs `op` on `b` in SMLib, tagged as operand 0, and brings the result back with SMLib's
|
||||
/// history over `[b]` (none where an order cannot be matched).
|
||||
pub(crate) fn run(name: &str, b: &Body, op: impl FnOnce(&Brep) -> std::result::Result<Brep, SmError>) -> Result<(Body, Option<History>)> {
|
||||
/// history over `[b]` (none where an order cannot be matched). `wall`: the operation's wall
|
||||
/// thickness, for its refusals.
|
||||
pub(crate) fn run(
|
||||
name: &str,
|
||||
b: &Body,
|
||||
wall: Option<f64>,
|
||||
op: impl FnOnce(&Brep) -> std::result::Result<Brep, SmError>,
|
||||
) -> Result<(Body, Option<History>)> {
|
||||
b.require_brep(name)?;
|
||||
let (sb, ob) = smlib_body_ordered(b)?;
|
||||
let tagged = sb.tagged(0).map_err(|e| failed(name, "tag", e))?;
|
||||
let r = op(&tagged).map_err(|e| refused(name, e))?;
|
||||
let r = op(&tagged).map_err(|e| refused(name, wall, e))?;
|
||||
let check = r.check(CheckLevel::All).map_err(|e| failed(name, "check", e))?;
|
||||
if !check.is_valid() {
|
||||
let input = sb.check(CheckLevel::All).is_ok_and(|c| !c.is_valid());
|
||||
@@ -84,7 +99,7 @@ pub(crate) fn shell(b: &Body, open: &[Vec3], thickness: f64) -> Result<Body> {
|
||||
let opened = crate::offset::with_same_surface(b, &picked);
|
||||
let tol = pick_tolerance(b);
|
||||
let pts = points(&face_points(b, &opened)?);
|
||||
let (r, h) = run("shell", b, |s| s.shell(&pts, tol, -thickness))?;
|
||||
let (r, h) = run("shell", b, Some(thickness), |s| s.shell(&pts, tol, -thickness))?;
|
||||
let h = h.and_then(|h| with_offsets(&r, b, h, thickness));
|
||||
Ok(r.with_checked_history(h, &record::counts(&[b])))
|
||||
}
|
||||
@@ -96,7 +111,7 @@ pub(crate) fn offset_faces(b: &Body, chosen: &[usize], distance: f64) -> Result<
|
||||
let at = face_points(b, chosen)?;
|
||||
let tol = pick_tolerance(b);
|
||||
let pts = points(&at);
|
||||
let (r, h) = run("offset faces", b, |s| s.offset_faces(&pts, tol, distance))?;
|
||||
let (r, h) = run("offset faces", b, None, |s| s.offset_faces(&pts, tol, distance))?;
|
||||
let h = h.map(|mut h| {
|
||||
for f in &mut h.faces {
|
||||
if f.change == Change::Kept && f.origins.iter().any(|o| matches!(o, FaceOrigin::Face { face, .. } if chosen.contains(face))) {
|
||||
@@ -114,7 +129,7 @@ pub(crate) fn thicken(b: &Body, thickness: f64, symmetric: bool) -> Result<Body>
|
||||
if !(thickness.is_finite() && thickness.abs() > 1e-9) {
|
||||
return Err(KernelError::Invalid("thickness must be non-zero".into()));
|
||||
}
|
||||
let (r, h) = run("thicken", b, |s| s.thicken(thickness, symmetric))?;
|
||||
let (r, h) = run("thicken", b, Some(thickness.abs()), |s| s.thicken(thickness, symmetric))?;
|
||||
let step = if symmetric { thickness.abs() / 2.0 } else { thickness.abs() };
|
||||
// The walls first, while the sheet's edges still carry their tags on both copies.
|
||||
let h = h.map(with_edge_walls).and_then(|h| with_offsets(&r, b, h, step));
|
||||
@@ -138,7 +153,7 @@ pub(crate) fn draft(b: &Body, at: &[Vec3], neutral: &solidcraft_geom::Plane, pul
|
||||
let tol = pick_tolerance(b);
|
||||
let pts = points(&face_points(b, &chosen)?);
|
||||
let o = neutral.origin;
|
||||
let (r, h) = run("draft", b, |s| s.draft(&pts, tol, [o.x, o.y, o.z], [normal.x, normal.y, normal.z], [pull.x, pull.y, pull.z], angle))?;
|
||||
let (r, h) = run("draft", b, None, |s| s.draft(&pts, tol, [o.x, o.y, o.z], [normal.x, normal.y, normal.z], [pull.x, pull.y, pull.z], angle))?;
|
||||
let h = h.map(|mut h| {
|
||||
for f in &mut h.faces {
|
||||
if f.change == Change::Kept && f.origins.iter().any(|o| matches!(o, FaceOrigin::Face { face, .. } if chosen.contains(face))) {
|
||||
|
||||
@@ -396,16 +396,19 @@ fn fillet_max_each_reduces_only_where_the_radius_does_not_fit() {
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let edges = [Vec3::new(21.0, 15.0, 30.0), Vec3::new(40.0, 0.0, 10.0)];
|
||||
assert!(matches!(fillet(&body, &edges, 4.0), Err(KernelError::TooLarge(_))));
|
||||
let (one, _) = fillet_max(&body, &edges, 4.0).unwrap();
|
||||
// R4 on the fin's top edge runs over the fin's 2 mm top, past its far edge (a cliff): it is
|
||||
// cut as if the top went on, so both edges take 4. The fin's section loses the 4 x 4 square
|
||||
// at its edge less the quarter disc about (17, 26), inside the fin (x >= 19): 2 x 4 less the
|
||||
// disc's part, in closed form; the corner's round (1 − π/4)·16 along 20.
|
||||
let both = fillet(&body, &edges, 4.0).unwrap();
|
||||
let (radii, b) = fillet_max_each(&body, &edges, 4.0).unwrap();
|
||||
assert_eq!(radii.get(1), Some(&4.0), "{radii:?}");
|
||||
let fin_r = radii.first().copied().unwrap();
|
||||
assert!(fin_r < 4.0 && (fin_r - one).abs() < 1e-9, "{radii:?}, one radius {one}");
|
||||
assert_eq!(radii, vec![4.0, 4.0], "{radii:?}");
|
||||
assert!(b.validity().is_empty(), "{:?}", b.validity());
|
||||
// Each round takes (1 − π/4)·r² along its edge: the fin's 30 long, the corner's 20.
|
||||
let removed = (1.0 - PI / 4.0) * (fin_r * fin_r * 30.0 + 16.0 * 20.0);
|
||||
assert!(rel(measure(&b).unwrap().volume, block + fin - removed) < 2e-4);
|
||||
let disc = |a: f64| 0.5 * (a * (16.0 - a * a).max(0.0).sqrt() + 16.0 * (a / 4.0).asin());
|
||||
let fin_section = 2.0 * 4.0 - (disc(4.0) - disc(2.0));
|
||||
let removed = fin_section * 30.0 + (1.0 - PI / 4.0) * 16.0 * 20.0;
|
||||
assert!(rel(measure(&both).unwrap().volume, block + fin - removed) < 1e-6, "{}", measure(&both).unwrap().volume);
|
||||
assert!(rel(measure(&b).unwrap().volume, block + fin - removed) < 1e-6);
|
||||
// The post's four edges limit each other: one radius, as `fillet_max`.
|
||||
let post = box_solid(Vec3::new(14.0, 0.0, 0.0), Vec3::new(28.0, 6.0, 10.0)).unwrap();
|
||||
let mids = [Vec3::new(14.0, 0.0, 5.0), Vec3::new(28.0, 0.0, 5.0), Vec3::new(14.0, 6.0, 5.0), Vec3::new(28.0, 6.0, 5.0)];
|
||||
|
||||
Reference in New Issue
Block a user