A 1.2 MB pure-Rust rewrite of headless Blender built for AI agents — 3D B-rep modeling engine, CLI, and MCP server
fix: normal-offset shell on notched/holed parts and bevel(vertical) with holes (Fixes #34, Fixes #35)
Sukhpal Gill37ca24b55f0d
diff --git a/crates/forma-core/src/curves.rs b/crates/forma-core/src/curves.rsindex 5676889..a496cc5 100644--- a/crates/forma-core/src/curves.rs+++ b/crates/forma-core/src/curves.rs@@ -101,7 +101,7 @@ pub fn eval_nurbs_3d(ctrl: &[Vec3], weights: Option<&[f64]>, degree: usize, segsOk(out)}-fn poly_area_2d(pts: &[(f64, f64)]) -> f64 {+pub(crate) fn poly_area_2d(pts: &[(f64, f64)]) -> f64 {let n = pts.len();let mut a = 0.0;for i in 0..n {@@ -123,7 +123,7 @@ fn pt_in_tri_2d(p: (f64, f64), a: (f64, f64), b: (f64, f64), c: (f64, f64)) -> bc1 >= -1e-11 && c2 >= -1e-11 && c3 >= -1e-11}-fn segs_properly_intersect(a: (f64, f64), b: (f64, f64), c: (f64, f64), d: (f64, f64)) -> bool {+pub(crate) fn segs_properly_intersect(a: (f64, f64), b: (f64, f64), c: (f64, f64), d: (f64, f64)) -> bool {let d1 = cross2d(a, b, c);let d2 = cross2d(a, b, d);let d3 = cross2d(c, d, a);diff --git a/crates/forma-core/src/deform.rs b/crates/forma-core/src/deform.rsindex 50aeaa9..fc8ecee 100644--- a/crates/forma-core/src/deform.rs+++ b/crates/forma-core/src/deform.rs@@ -89,7 +89,7 @@ pub fn solidify_mesh(open_top: bool,op_id: u32,) -> Result<PolyMesh, String> {- if thickness.abs() < 1e-6 {+ if thickness.abs() < 1e-9 {return Err("solidify thickness must be non-zero".into());}if mesh.faces.is_empty() || mesh.verts.is_empty() {@@ -103,7 +103,8 @@ pub fn solidify_mesh(if open_top {let n = mesh.face_normal(fi);let z_avg = f.verts.iter().map(|&vi| mesh.verts[vi].z).sum::<f64>() / (f.verts.len().max(1) as f64);- if n.z > 0.85 && (z_avg - bb.max.z).abs() < z_tol {+ let all_at_top = f.verts.iter().all(|&vi| (mesh.verts[vi].z - bb.max.z).abs() < z_tol);+ if (n.z > 0.85 && (z_avg - bb.max.z).abs() < z_tol) || (n.len_sq() < 1e-12 && all_at_top) {continue;}}@@ -180,6 +181,7 @@ pub fn solidify_mesh(// Track directed boundary edges of the kept face set so we bridge the rim cleanlylet mut dir_count: HashMap<(usize, usize), usize> = HashMap::new();+ let mut has_pinched_face = false;for &fi in &keep_faces {let f = &mesh.faces[fi];let k = f.verts.len();@@ -204,7 +206,14 @@ pub fn solidify_mesh(if orig_n.len_sq() > 1e-12 {let out_n = out.face_normal(ofi);let in_n = out.face_normal(ifi);- if out_n.dot(orig_n) <= 1e-4 || (-in_n).dot(orig_n) <= 1e-4 {+ if out_n.dot(orig_n) <= 1e-4 {+ return Err(format!(+ "solidify thickness ({thickness}) inverts outer offset faces / self-intersects; reduce thickness"+ ));+ }+ if in_n.len_sq() < 1e-12 {+ has_pinched_face = true;+ } else if (-in_n).dot(orig_n) <= 1e-4 {return Err(format!("solidify thickness ({thickness}) inverts offset faces / self-intersects; reduce thickness"));@@ -230,6 +239,11 @@ pub fn solidify_mesh(}}+ if has_pinched_face {+ let tol = (bb.size().len() * 1e-7).clamp(1e-9, 1e-6);+ out.weld_and_clean(tol);+ }+out.ensure_outward_winding();if out.faces.len() <= 8_000 && crate::boolean::has_self_intersections(&out) {return Err(format!(diff --git a/crates/forma-core/src/ops.rs b/crates/forma-core/src/ops.rsindex cd3e35c..93b5dec 100644--- a/crates/forma-core/src/ops.rs+++ b/crates/forma-core/src/ops.rs@@ -3,6 +3,7 @@//! Catmull-Clark subdivision, shell/hollow, mirror, linear/radial/grid arrays, and relational placement.use crate::boolean::{boolean_op, BoolOp};+use crate::curves::{make_extrude_2d_with_holes, poly_area_2d, segs_properly_intersect};use crate::math::Vec3;use crate::mesh::{edge_key, Face, PolyMesh};use crate::primitives::make_extrude_2d;@@ -160,8 +161,20 @@ pub fn bevel_edges(segments: usize,op_id: u32,) -> Result<PolyMesh, String> {+ bevel_edges_with_warning(mesh, sel, width, segments, op_id).map(|(m, _)| m)+}++/// Bevel selected edges, optionally returning a non-fatal warning (e.g. when `segments > 1`+/// is requested on the general 1-segment chamfer fallback).+pub fn bevel_edges_with_warning(+ mesh: &PolyMesh,+ sel: &EdgeSelector,+ width: f64,+ segments: usize,+ op_id: u32,+) -> Result<(PolyMesh, Option<String>), String> {if width <= 1e-7 {- return Ok(mesh.clone());+ return Ok((mesh.clone(), None));}let segments = segments.clamp(1, 8);let edges = sel.select(mesh);@@ -169,10 +182,10 @@ pub fn bevel_edges(return Err("bevel: edge selector matched 0 edges".into());}- // Specialized exact path 1: Vertical edges of a Z-prism/box (e.g. beveled_enclosure)+ // Specialized exact path 1: Vertical edges of a Z-prism/box (with or without holes)if matches!(sel, EdgeSelector::Vertical) {if let Some(filleted) = try_bevel_vertical_prism(mesh, width, segments, op_id) {- return Ok(filleted);+ return Ok((filleted, None));}}@@ -184,22 +197,131 @@ pub fn bevel_edges(let do_top = matches!(sel, EdgeSelector::TopRim | EdgeSelector::OuterRim);let do_bot = matches!(sel, EdgeSelector::BottomRim | EdgeSelector::OuterRim);if let Some(beveled) = try_bevel_z_rims(mesh, width, segments, do_top, do_bot, op_id) {- return Ok(beveled);+ return Ok((beveled, None));}}// General path: chamfer/bevel arbitrary selected edges via vertex-split & corner bridging- bevel_general_polyhedron(mesh, &edges, width, op_id)+ let warn = if segments > 1 {+ Some(format!(+ "bevel: segs={segments} is only supported on prism/rim edges; fell back to 1-segment chamfer"+ ))+ } else {+ None+ };+ let out = bevel_general_polyhedron(mesh, &edges, width, op_id)?;+ Ok((out, warn))}-/// Exact 2D corner-fillet + Z-extrusion for vertical edges of an axis-aligned Z-prism/box.-fn try_bevel_vertical_prism(- mesh: &PolyMesh,- radius: f64,- segments: usize,- op_id: u32,-) -> Option<PolyMesh> {+fn simplify_collinear_2d(pts: &[(f64, f64)], rel_eps: f64) -> Vec<(f64, f64)> {+ let mut cur: Vec<(f64, f64)> = Vec::with_capacity(pts.len());+ for &p in pts {+ if cur.last().is_none_or(|&q| (p.0 - q.0).hypot(p.1 - q.1) > 1e-12) {+ cur.push(p);+ }+ }+ if cur.len() > 1 && (cur[0].0 - cur.last().unwrap().0).hypot(cur[0].1 - cur.last().unwrap().1) <= 1e-12 {+ cur.pop();+ }+ let mut changed = true;+ while changed && cur.len() >= 3 {+ changed = false;+ let n = cur.len();+ let mut next_pts = Vec::with_capacity(n);+ for i in 0..n {+ let a = cur[(i + n - 1) % n];+ let b = cur[i];+ let c = cur[(i + 1) % n];+ let dx1 = b.0 - a.0;+ let dy1 = b.1 - a.1;+ let dx2 = c.0 - b.0;+ let dy2 = c.1 - b.1;+ let l1 = dx1.hypot(dy1);+ let l2 = dx2.hypot(dy2);+ if l1 < 1e-12 || l2 < 1e-12 {+ changed = true;+ continue;+ }+ let cross = (dx1 * dy2 - dy1 * dx2).abs();+ let dot = dx1 * dx2 + dy1 * dy2;+ if cross <= rel_eps * l1 * l2 && dot > 0.0 {+ changed = true;+ continue;+ }+ next_pts.push(b);+ }+ cur = next_pts;+ }+ cur+}++fn point_in_poly_2d(p: (f64, f64), poly: &[(f64, f64)]) -> bool {+ let n = poly.len();+ let mut inside = false;+ let mut j = n.saturating_sub(1);+ for i in 0..n {+ let (xi, yi) = poly[i];+ let (xj, yj) = poly[j];+ if ((yi > p.1) != (yj > p.1)) && (p.0 < (xj - xi) * (p.1 - yi) / (yj - yi) + xi) {+ inside = !inside;+ }+ j = i;+ }+ inside+}++fn poly_has_self_intersection_2d(poly: &[(f64, f64)]) -> bool {+ let n = poly.len();+ for i in 0..n {+ let a = poly[i];+ let b = poly[(i + 1) % n];+ for j in (i + 2)..n {+ if i == 0 && j == n - 1 {+ continue;+ }+ if segs_properly_intersect(a, b, poly[j], poly[(j + 1) % n]) {+ return true;+ }+ }+ }+ false+}++fn loops_disjoint_and_contained(outer: &[(f64, f64)], hole: &[(f64, f64)]) -> bool {+ if hole.is_empty() || outer.len() < 3 {+ return false;+ }+ if !hole.iter().all(|&p| point_in_poly_2d(p, outer)) {+ return false;+ }+ let no = outer.len();+ let nh = hole.len();+ for i in 0..no {+ let a = outer[i];+ let b = outer[(i + 1) % no];+ for j in 0..nh {+ if segs_properly_intersect(a, b, hole[j], hole[(j + 1) % nh]) {+ return false;+ }+ }+ }+ true+}++struct ZPrismFootprint {+ z0: f64,+ z1: f64,+ outer: Vec<(f64, f64)>,+ holes: Vec<Vec<(f64, f64)>>,+}++/// Extract the 2D footprint if `mesh` is a watertight axis-aligned Z-prism+/// (possibly with triangulated caps and up to 32 inner through-holes).+fn extract_z_prism_footprint(mesh: &PolyMesh) -> Option<ZPrismFootprint> {let bb = mesh.aabb();+ if !bb.is_valid() {+ return None;+ }let z0 = bb.min.z;let z1 = bb.max.z;let h = z1 - z0;@@ -207,20 +329,206 @@ fn try_bevel_vertical_prism(return None;}let z_tol = (1e-4 * h).max(1e-9);- // Find top face polygon- let top_faces: Vec<usize> = (0..mesh.faces.len())- .filter(|&fi| mesh.face_normal(fi).z > 0.99 && (mesh.face_centroid(fi).z - z1).abs() < z_tol)- .collect();- if top_faces.len() != 1 {++ let mut top_faces: Vec<usize> = Vec::new();+ let mut bot_count = 0usize;+ for (fi, f) in mesh.faces.iter().enumerate() {+ if f.verts.len() < 3 {+ return None;+ }+ let all_top = f.verts.iter().all(|&vi| (mesh.verts[vi].z - z1).abs() < z_tol);+ let all_bot = f.verts.iter().all(|&vi| (mesh.verts[vi].z - z0).abs() < z_tol);+ let fnrm = mesh.face_normal(fi);+ if all_top && (fnrm.z > 0.99 || fnrm.len_sq() < 1e-12) {+ top_faces.push(fi);+ } else if all_bot && (fnrm.z < -0.99 || fnrm.len_sq() < 1e-12) {+ bot_count += 1;+ } else {+ let vertical_face = fnrm.z.abs() < 0.02 || fnrm.len_sq() < 1e-12;+ let verts_on_caps = f+ .verts+ .iter()+ .all(|&vi| (mesh.verts[vi].z - z0).abs() < z_tol || (mesh.verts[vi].z - z1).abs() < z_tol);+ if !vertical_face || !verts_on_caps {+ return None;+ }+ }+ }+ if top_faces.is_empty() || bot_count == 0 {+ return None;+ }++ let mut top_dir: HashSet<(usize, usize)> = HashSet::new();+ for &fi in &top_faces {+ let fv = &mesh.faces[fi].verts;+ let k = fv.len();+ for i in 0..k {+ let u = fv[i];+ let v = fv[(i + 1) % k];+ if u != v {+ top_dir.insert((u, v));+ }+ }+ }++ let mut next_v: HashMap<usize, usize> = HashMap::new();+ for &(u, v) in &top_dir {+ if !top_dir.contains(&(v, u)) && next_v.insert(u, v).is_some() {+ return None; // non-manifold boundary pinch on top cap+ }+ }+ if next_v.is_empty() {+ return None;+ }++ let mut starts: Vec<usize> = next_v.keys().copied().collect();+ starts.sort_unstable();+ let mut visited: HashSet<usize> = HashSet::new();+ let mut outer_ccw: Option<Vec<(f64, f64)>> = None;+ let mut holes_cw: Vec<Vec<(f64, f64)>> = Vec::new();+ let rel_eps = 1e-7;++ for s in starts {+ if visited.contains(&s) {+ continue;+ }+ let mut loop_v = Vec::new();+ let mut cur = s;+ while !visited.contains(&cur) {+ visited.insert(cur);+ loop_v.push((mesh.verts[cur].x, mesh.verts[cur].y));+ cur = *next_v.get(&cur)?;+ }+ if cur != s {+ return None;+ }+ let simp = simplify_collinear_2d(&loop_v, rel_eps);+ if simp.len() < 3 {+ return None;+ }+ let area = poly_area_2d(&simp);+ if area > 1e-12 {+ if outer_ccw.is_some() {+ return None; // multiple disconnected islands+ }+ outer_ccw = Some(simp);+ } else if area < -1e-12 {+ holes_cw.push(simp);+ } else {+ return None;+ }+ }++ if holes_cw.len() > 32 {return None;}- let fv = &mesh.faces[top_faces[0]].verts;- if fv.len() < 3 {+ let outer = outer_ccw?;+ Some(ZPrismFootprint {+ z0,+ z1,+ outer,+ holes: holes_cw,+ })+}++/// Left-hand 2D polygon offset by `dist` (inward for CCW outer polygons, outward-into-solid for CW holes).+/// Trims locally inverted short edges (e.g. corner fillets with `radius <= dist`) cleanly.+fn offset_polygon_2d(pts: &[(f64, f64)], dist: f64) -> Option<Vec<(f64, f64)>> {+ let n = pts.len();+ if n < 3 {return None;}- let poly: Vec<(f64, f64)> = fv.iter().map(|&vi| (mesh.verts[vi].x, mesh.verts[vi].y)).collect();+ // Each active line: (origin on offset line, unit direction vector)+ let mut lines: Vec<((f64, f64), (f64, f64))> = Vec::with_capacity(n);+ for i in 0..n {+ let a = pts[i];+ let b = pts[(i + 1) % n];+ let dx = b.0 - a.0;+ let dy = b.1 - a.1;+ let len = dx.hypot(dy);+ if len < 1e-12 {+ continue;+ }+ let dir = (dx / len, dy / len);+ let left_n = (-dir.1, dir.0);+ lines.push(((a.0 + left_n.0 * dist, a.1 + left_n.1 * dist), dir));+ }+ let orig_area = poly_area_2d(pts);++ for _iter in 0..=n {+ let m = lines.len();+ if m < 3 {+ return None;+ }+ let mut merged_parallel = false;+ let mut q: Vec<(f64, f64)> = Vec::with_capacity(m);+ for i in 0..m {+ let j = (i + 1) % m;+ let (o1, d1) = lines[i];+ let (o2, d2) = lines[j];+ let det = d1.0 * d2.1 - d1.1 * d2.0;+ if det.abs() < 1e-9 {+ if d1.0 * d2.0 + d1.1 * d2.1 > 0.0 {+ lines.remove(j);+ merged_parallel = true;+ break;+ } else {+ return None;+ }+ }+ let diff = (o2.0 - o1.0, o2.1 - o1.1);+ let t = (diff.0 * d2.1 - diff.1 * d2.0) / det;+ q.push((o1.0 + t * d1.0, o1.1 + t * d1.1));+ }+ if merged_parallel {+ continue;+ }++ let mut drop_idx = None;+ for i in 0..m {+ let p_start = q[(i + m - 1) % m];+ let p_end = q[i];+ let dir = lines[i].1;+ let proj = (p_end.0 - p_start.0) * dir.0 + (p_end.1 - p_start.1) * dir.1;+ if proj <= 1e-9 {+ drop_idx = Some(i);+ break;+ }+ }+ if let Some(idx) = drop_idx {+ lines.remove(idx);+ continue;+ }++ let ordered: Vec<(f64, f64)> = (0..m).map(|i| q[(i + m - 1) % m]).collect();+ let simp = simplify_collinear_2d(&ordered, 1e-7);+ if simp.len() < 3 {+ return None;+ }+ let new_area = poly_area_2d(&simp);+ if (orig_area > 0.0 && new_area <= 1e-12) || (orig_area < 0.0 && new_area >= -1e-12) {+ return None;+ }+ if poly_has_self_intersection_2d(&simp) {+ return None;+ }+ return Some(simp);+ }+ None+}++/// Exact 2D corner-fillet + Z-extrusion for vertical edges of an axis-aligned Z-prism/box+/// (including triangulated prisms and prisms with inner through-holes).+fn try_bevel_vertical_prism(+ mesh: &PolyMesh,+ radius: f64,+ segments: usize,+ op_id: u32,+) -> Option<PolyMesh> {+ let ZPrismFootprint { z0, z1, outer: poly, holes } = extract_z_prism_footprint(mesh)?;let n = poly.len();let mut rounded: Vec<(f64, f64)> = Vec::with_capacity(n * (segments + 1));+ let cos_sharp_thresh = 20.0_f64.to_radians().cos();for i in 0..n {let prev = poly[(i + n - 1) % n];@@ -235,6 +543,13 @@ fn try_bevel_vertical_prism(}let u1 = (e1.0 / l1, e1.1 / l1);let u2 = (e2.0 / l2, e2.1 / l2);+ // Skip nearly straight / smooth-curve vertices (turning angle < 20 deg)+ let cos_turn = -(u1.0 * u2.0 + u1.1 * u2.1);+ if cos_turn > cos_sharp_thresh {+ rounded.push(curr);+ continue;+ }+let d = radius.min(l1 * 0.45).min(l2 * 0.45);let p_start = (curr.0 + u1.0 * d, curr.1 + u1.1 * d);let p_end = (curr.0 + u2.0 * d, curr.1 + u2.1 * d);@@ -275,8 +590,26 @@ fn try_bevel_vertical_prism(}}}- let mut res = make_extrude_2d(&rounded, z0, z1, op_id).ok()?;+ let rounded = simplify_collinear_2d(&rounded, 1e-8);+ if rounded.len() < 3 || poly_has_self_intersection_2d(&rounded) {+ return None;+ }+ for hole in &holes {+ if !loops_disjoint_and_contained(&rounded, hole) {+ return None;+ }+ }+ let mut res = if holes.is_empty() {+ make_extrude_2d(&rounded, z0, z1, op_id).ok()?+ } else {+ make_extrude_2d_with_holes(&rounded, &holes, z0, z1, op_id).ok()?+ };res.tag_all_faces("bevel");+ if mesh.materials.len() == 1 {+ res.assign_material(mesh.materials[0].clone(), None);+ }+ res.smooth_angle_deg = mesh.smooth_angle_deg;+ res.weighted_normals = mesh.weighted_normals;Some(res)}@@ -1024,38 +1357,49 @@ pub fn shell_solid(}let bb = mesh.aabb();let sz = bb.size();- let c = bb.center();let sx = (sz.x - 2.0 * thickness) / sz.x.max(1e-9);let sy = (sz.y - 2.0 * thickness) / sz.y.max(1e-9);if sx <= 0.01 || sy <= 0.01 {return Err("shell thickness exceeds part dimensions".into());}- let mut inner = mesh.clone();if open_top {if thickness >= sz.z * 0.99 {return Err("shell thickness exceeds part height".into());}- // Keep floor thickness = `thickness` at bottom, extend cavity above top to cut open cleanly- let z_floor = bb.min.z + thickness;- let z_top_ext = bb.max.z + thickness.max(0.05 * sz.z.max(1e-6));- for v in &mut inner.verts {- v.x = c.x + (v.x - c.x) * sx;- v.y = c.y + (v.y - c.y) * sy;- let tz = ((v.z - bb.min.z) / sz.z.max(1e-9)).clamp(0.0, 1.0);- v.z = z_floor + tz * (z_top_ext - z_floor);- }} else {let sz_s = (sz.z - 2.0 * thickness) / sz.z.max(1e-9);if sz_s <= 0.01 {return Err("shell thickness exceeds part height".into());}- for v in &mut inner.verts {- v.x = c.x + (v.x - c.x) * sx;- v.y = c.y + (v.y - c.y) * sy;- v.z = c.z + (v.z - c.z) * sz_s;+ }++ if let Some(ZPrismFootprint { z0, z1, outer, holes }) = extract_z_prism_footprint(mesh) {+ let inner_outer = offset_polygon_2d(&outer, thickness)+ .ok_or_else(|| "shell thickness exceeds wall or notch width".to_string())?;+ let mut inner_holes: Vec<Vec<(f64, f64)>> = Vec::with_capacity(holes.len());+ for hole in &holes {+ let ih = offset_polygon_2d(hole, thickness)+ .ok_or_else(|| "shell thickness exceeds hole offset clearance".to_string())?;+ if !loops_disjoint_and_contained(&inner_outer, &ih) {+ return Err("shell thickness exceeds wall clearance around hole".into());+ }+ inner_holes.push(ih);}+ let z_floor = z0 + thickness;+ let z_top_ext = if open_top {+ z1 + thickness.max(0.05 * sz.z.max(1e-6))+ } else {+ z1 - thickness+ };+ let cavity = if inner_holes.is_empty() {+ make_extrude_2d(&inner_outer, z_floor, z_top_ext, op_id)?+ } else {+ make_extrude_2d_with_holes(&inner_outer, &inner_holes, z_floor, z_top_ext, op_id)?+ };+ return boolean_op(mesh, &cavity, BoolOp::Difference, Some("cavity"), op_id);}- boolean_op(mesh, &inner, BoolOp::Difference, Some("cavity"), op_id)++ crate::deform::solidify_mesh(mesh, thickness, -1.0, open_top, op_id)}/// Mirror a mesh across the X=0, Y=0, or Z=0 plane, optionally unioning with the original.diff --git a/crates/forma-core/src/selector.rs b/crates/forma-core/src/selector.rsindex c936be0..4cbc6fc 100644--- a/crates/forma-core/src/selector.rs+++ b/crates/forma-core/src/selector.rs@@ -206,7 +206,15 @@ impl EdgeSelector {}match self {Self::All => true,- Self::Vertical => d.z.abs() > 0.7 * len,+ Self::Vertical => {+ if d.z.abs() <= 0.7 * len {+ false+ } else if faces.len() == 2 {+ mesh.dihedral_angle(faces[0], faces[1]).to_degrees() >= 20.0+ } else {+ true+ }+ }Self::Horizontal => d.z.abs() < 0.2 * len,Self::TopRim => (pu.z - bmax.z).abs() < eps && (pv.z - bmax.z).abs() < eps,Self::BottomRim => (pu.z - bmin.z).abs() < eps && (pv.z - bmin.z).abs() < eps,diff --git a/crates/forma-core/tests/kernel_tests.rs b/crates/forma-core/tests/kernel_tests.rsindex 4b512f8..4df3dd8 100644--- a/crates/forma-core/tests/kernel_tests.rs+++ b/crates/forma-core/tests/kernel_tests.rs@@ -858,3 +858,103 @@ fn test_issues_10_11_twisted_loft_shell_cut_and_scale_invariant_vol() {assert!(cinv.is_valid_solid() && cinv.genus == 0, "Issue #11 loft cut SUB={sub}: {cinv:?}");}}++#[test]+fn test_issue_34_shell_preserves_walls_on_notches_and_holes() {+ // 1. L-shaped tray: outline area=5, inward 0.1 offset area=3.84 -> wall section=1.16, vol=1.544+ let mut s = Scene::new();+ s.strict_manifold = true;+ s.add_extrude_2d(+ "l_tray",+ &[(0.0, 0.0), (3.0, 0.0), (3.0, 1.0), (1.0, 1.0), (1.0, 3.0), (0.0, 3.0)],+ 0.0,+ 1.0,+ )+ .unwrap();+ let l_inv = s.shell("l_tray", 0.1, true).unwrap();+ assert!(l_inv.is_valid_solid(), "l_tray invalid: {l_inv:?}");+ assert!((l_inv.volume - 1.544).abs() < 1e-3, "l_tray vol={}, expected 1.544", l_inv.volume);+ let l_sec = s.section_z("l_tray", 0.5).unwrap();+ assert_eq!(l_sec.loops, 2, "l_tray section at z=0.5 should have outer+inner loop, got {:?}", l_sec);+ assert!((l_sec.area - 1.16).abs() < 1e-3, "l_tray section area={}, expected 1.16", l_sec.area);++ // 2. U-shaped tray: outline area=8, inward 0.1 offset area=5.8 -> wall section=2.2, vol=2.384 (Wait: 8*0.1 + (8-5.8)*0.9 = 2.78? Let's verify U-tray area!)+ // U-shape [(0,0),(4,0),(4,3),(3,3),(3,1),(1,1),(1,3),(0,3)] has area = 4*3 - 2*2 = 8; inward 0.1 offset has exact volume 2.384!+ s.add_extrude_2d(+ "u_tray",+ &[+ (0.0, 0.0),+ (4.0, 0.0),+ (4.0, 3.0),+ (3.0, 3.0),+ (3.0, 1.0),+ (1.0, 1.0),+ (1.0, 3.0),+ (0.0, 3.0),+ ],+ 0.0,+ 1.0,+ )+ .unwrap();+ let u_inv = s.shell("u_tray", 0.1, true).unwrap();+ assert!(u_inv.is_valid_solid(), "u_tray invalid: {u_inv:?}");+ assert!((u_inv.volume - 2.384).abs() < 1e-3, "u_tray vol={}, expected 2.384", u_inv.volume);++ // 3. Box with a through-hole: 4x4x1 minus cyl(r=0.5), shelled by 0.1 -> ~3.237 with 0.1 wall around hole+ s.add_box("holed", 4.0, 4.0, 1.0, Vec3::ZERO).unwrap();+ s.add_cyl("hole", 0.5, 3.0, 32, Vec3::ZERO, 'z').unwrap();+ s.boolean_parts("holed", "hole", BoolOp::Difference, false).unwrap();+ let h_inv = s.shell("holed", 0.1, true).unwrap();+ assert!(h_inv.is_valid_solid() && h_inv.genus == 1, "holed shell invalid: {h_inv:?}");+ assert!((h_inv.volume - 3.231).abs() < 0.01, "holed shell vol={}, expected ~3.231", h_inv.volume);+ let h_sec = s.section_z("holed", 0.0).unwrap();+ assert_eq!(h_sec.loops, 4, "expected 4 loops (outer box, cavity wall, hole wall, bore), got {:?}", h_sec);++ // 4. Plain box(4,4,1) control: vol=3.004+ s.add_box("plain", 4.0, 4.0, 1.0, Vec3::ZERO).unwrap();+ let p_inv = s.shell("plain", 0.1, true).unwrap();+ assert!(p_inv.is_valid_solid());+ assert!((p_inv.volume - 3.004).abs() < 1e-3, "plain vol={}, expected 3.004", p_inv.volume);+}++#[test]+fn test_issue_35_bevel_vertical_after_hole_cut_and_curved_wall_guard() {+ // 1. Cut hole first, then bevel(vertical, 0.2, segs=3) -> must match bevel-first cut-second+ let mut s_after = Scene::new();+ s_after.strict_manifold = true;+ s_after.add_box("a", 4.0, 2.0, 0.4, Vec3::ZERO).unwrap();+ s_after.add_cyl("h", 0.5, 1.0, 32, Vec3::ZERO, 'z').unwrap();+ s_after.boolean_parts("a", "h", BoolOp::Difference, false).unwrap();+ let inv_after = s_after.bevel("a", &EdgeSelector::Vertical, 0.2, 3).unwrap();++ let mut s_before = Scene::new();+ s_before.strict_manifold = true;+ s_before.add_box("a", 4.0, 2.0, 0.4, Vec3::ZERO).unwrap();+ s_before.bevel("a", &EdgeSelector::Vertical, 0.2, 3).unwrap();+ s_before.add_cyl("h", 0.5, 1.0, 32, Vec3::ZERO, 'z').unwrap();+ let inv_before = s_before.boolean_parts("a", "h", BoolOp::Difference, false).unwrap();++ assert!(inv_after.is_valid_solid() && inv_after.genus == 1, "inv_after={inv_after:?}");+ assert!(+ (inv_after.volume - inv_before.volume).abs() < 1e-3,+ "cut-then-bevel vol={} vs bevel-then-cut vol={}",+ inv_after.volume,+ inv_before.volume+ );+ let sec = s_after.section_z("a", 0.0).unwrap();+ assert_eq!(sec.loops, 2, "expected outer + hole loop, got {:?}", sec);++ // 2. Curved 32-gon vertical cylinder must not be chewed up by EdgeSelector::Vertical+ let mut s_cyl = Scene::new();+ s_cyl.add_cyl("c", 1.0, 0.4, 32, Vec3::ZERO, 'z').unwrap();+ assert!(+ s_cyl.bevel("c", &EdgeSelector::Vertical, 0.1, 1).is_err(),+ "smooth cylinder has no sharp vertical corners to bevel"+ );++ // 3. Fallback general polyhedron bevel with segs > 1 emits warning via bevel_edges_with_warning+ let b = forma_core::primitives::make_box(2.0, 2.0, 2.0, 1);+ let (res, warn) = forma_core::ops::bevel_edges_with_warning(&b, &EdgeSelector::All, 0.3, 4, 2).unwrap();+ assert!(MeshInvariants::compute(&res).is_valid_solid());+ assert!(warn.is_some_and(|w| w.contains("fell back to 1-segment chamfer")));+}diff --git a/crates/forma-lang/src/lib.rs b/crates/forma-lang/src/lib.rsindex 4008188..7f03141 100644--- a/crates/forma-lang/src/lib.rs+++ b/crates/forma-lang/src/lib.rs@@ -169,4 +169,17 @@ mod tests {);assert!(obs.text.contains("WARN:"));}++ #[test]+ fn test_issue_35_fallback_bevel_segs_warning() {+ let mut s = Scene::new();+ s.strict_manifold = true;+ let obs = eval_script(&mut s, Dialect::Pipe, "a = box(2, 2, 2) | bevel(all, 0.3, segs=4)").unwrap();+ assert!(obs.ok);+ assert!(+ obs.warnings.iter().any(|w| w.contains("fell back to 1-segment chamfer")),+ "expected fallback warning, got {:?}",+ obs.warnings+ );+ }}diff --git a/crates/forma-lang/src/pipeline.rs b/crates/forma-lang/src/pipeline.rsindex d1701fd..630f16b 100644--- a/crates/forma-lang/src/pipeline.rs+++ b/crates/forma-lang/src/pipeline.rs@@ -1073,7 +1073,12 @@ impl<'a> Parser<'a> {.ok_or(format!("{ctx}: missing width"))?.as_num(&ctx)?;let segs = kw.get("segs").or_else(|| kw.get("segments")).or_else(|| pos.get(2)).map(|v| v.as_num(&ctx)).transpose()?.unwrap_or(1.0) as usize;- forma_core::ops::bevel_edges(&mesh, &sel, w, segs, op_id).map_err(|e| format!("{ctx}: {e}"))+ let (out, warn) = forma_core::ops::bevel_edges_with_warning(&mesh, &sel, w, segs, op_id)+ .map_err(|e| format!("{ctx}: {e}"))?;+ if let Some(msg) = warn {+ self.scene.push_warning(msg);+ }+ Ok(out)}"shell" => {let t = kw