From 17ba1aef30c40f75ef561e79b02566a312f5653d Mon Sep 17 00:00:00 2001 From: Sukhpal Gill <1947686+onlinegill@users.noreply.github.com> Date: Thu, 3 Dec 2026 12:21:07 -0600 Subject: [PATCH] fix(core): open_top guard, deterministic bevel(all/sharp), and centered voxel remesh (Fixes #93, Fixes #94, Fixes #81) --- crates/forma-core/src/deform.rs | 5 + crates/forma-core/src/ops.rs | 10 +- crates/forma-core/src/remesh.rs | 200 ++++++++++++++++-------- crates/forma-core/tests/kernel_tests.rs | 54 +++++++ 4 files changed, 196 insertions(+), 73 deletions(-) diff --git a/crates/forma-core/src/deform.rs b/crates/forma-core/src/deform.rs index d81e963..b689a19 100644 --- a/crates/forma-core/src/deform.rs +++ b/crates/forma-core/src/deform.rs @@ -99,17 +99,22 @@ pub fn solidify_mesh( let bb = mesh.aabb(); let z_tol = (1e-4 * bb.size().z).max(1e-9); let mut keep_faces: Vec = Vec::with_capacity(mesh.faces.len()); + let mut skipped_top = 0usize; for (fi, f) in mesh.faces.iter().enumerate() { if open_top { let n = mesh.face_normal(fi); let z_avg = f.verts.iter().map(|&vi| mesh.verts[vi].z).sum::() / (f.verts.len().max(1) as f64); 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) { + skipped_top += 1; continue; } } keep_faces.push(fi); } + if open_top && skipped_top == 0 { + return Err("open_top=true found no flat top face to open (cut a flat top first or pass open_top=false)".into()); + } if keep_faces.is_empty() { return Err("solidify: no faces left after removing top".into()); } diff --git a/crates/forma-core/src/ops.rs b/crates/forma-core/src/ops.rs index fc17653..2aa06d3 100644 --- a/crates/forma-core/src/ops.rs +++ b/crates/forma-core/src/ops.rs @@ -1209,9 +1209,11 @@ fn bevel_general_polyhedron( out.faces[out_fi].mat_id = f.mat_id; } - // Bridge bevel quads across every edge touching affected vertices + // Bridge bevel quads across every edge touching affected vertices (sorted for deterministic output) let adj = mesh.build_adjacency(); - for (&(u, v), inc_faces) in &adj.edge_faces { + let mut sorted_edges: Vec<_> = adj.edge_faces.iter().collect(); + sorted_edges.sort_unstable_by_key(|(&k, _)| k); + for (&(u, v), inc_faces) in sorted_edges { if inc_faces.len() != 2 { continue; } @@ -1250,8 +1252,8 @@ fn bevel_general_polyhedron( } } - // Fill vertex corner patches for every affected vertex - let mut v_to_faces: HashMap> = HashMap::new(); + // Fill vertex corner patches for every affected vertex (BTreeMap for deterministic order) + let mut v_to_faces: std::collections::BTreeMap> = std::collections::BTreeMap::new(); for (fi, f) in mesh.faces.iter().enumerate() { for &vi in &f.verts { if affected_verts.contains(&vi) { diff --git a/crates/forma-core/src/remesh.rs b/crates/forma-core/src/remesh.rs index ce6f431..43f1009 100644 --- a/crates/forma-core/src/remesh.rs +++ b/crates/forma-core/src/remesh.rs @@ -26,75 +26,110 @@ pub fn remesh_voxel(mesh: &PolyMesh, voxel_size: f64, op_id: u32) -> Result (f64, usize) { + let half_cells = 0.5 * span * inv_h; + let d_int = (half_cells - half_cells.round()).abs(); + let d_half = (half_cells - 0.5 - (half_cells - 0.5).round()).abs(); + let shift = if span < h || d_half < d_int { 0.0 } else { 0.5 }; + let half_n = ((0.5 * span + 2.0 * h) * inv_h).ceil() as usize + 1; + let orig = center - (half_n as f64 + shift) * h; + let count = 2 * half_n + if shift > 0.0 { 2 } else { 1 }; + (orig, count) + }; + let (ox, nx) = axis_grid(c.x, sz.x); + let (oy, ny) = axis_grid(c.y, sz.y); + let (oz, nz) = axis_grid(c.z, sz.z); + let origin = Vec3::new(ox, oy, oz); - // Collect triangles and project onto every `(y, z)` scanline with tiny deterministic jitter - // so rays never graze exact vertices or edges (Simulation of Simplicity parity guarantee!). - // Store `(x_hit, winding_delta)` using the sign of `det = ((v1-v0) x (v2-v0)).x` so non-zero - // winding unions overlapping multi-shell meshes instead of XOR-hollowing them. + // Collect triangles and project onto `(y, z)`, `(x, z)`, and `(x, y)` scanlines with tiny + // deterministic jitter so rays never graze exact vertices or edges (SoS parity guarantee!). let mut scanlines: Vec> = vec![Vec::new(); ny * nz]; + let mut scan_y: Vec> = vec![Vec::new(); nx * nz]; + let mut scan_z: Vec> = vec![Vec::new(); nx * ny]; + let eps_x = 1.793e-7 * h; let eps_y = 1.137e-7 * h; let eps_z = 2.713e-7 * h; - let inv_h = 1.0 / h; - for fi in 0..mesh.faces.len() { - for [ia, ib, ic] in mesh.triangulate_face(fi) { - let v0 = mesh.verts[ia]; - let v1 = mesh.verts[ib]; - let v2 = mesh.verts[ic]; + #[allow(clippy::too_many_arguments)] + fn project_tri_scanlines( + r0: f64, p0: f64, q0: f64, + r1: f64, p1: f64, q1: f64, + r2: f64, p2: f64, q2: f64, + op: f64, oq: f64, np: usize, nq: usize, + eps_p: f64, eps_q: f64, h: f64, inv_h: f64, + mut push_hit: F, + ) { + let min_p = p0.min(p1).min(p2); + let max_p = p0.max(p1).max(p2); + let min_q = q0.min(q1).min(q2); + let max_q = q0.max(q1).max(q2); + let p_lo = (((min_p - op) * inv_h).floor() as isize).clamp(0, np as isize - 1) as usize; + let p_hi = (((max_p - op) * inv_h).ceil() as isize).clamp(0, np as isize - 1) as usize; + let q_lo = (((min_q - oq) * inv_h).floor() as isize).clamp(0, nq as isize - 1) as usize; + let q_hi = (((max_q - oq) * inv_h).ceil() as isize).clamp(0, nq as isize - 1) as usize; - let min_y = v0.y.min(v1.y).min(v2.y); - let max_y = v0.y.max(v1.y).max(v2.y); - let min_z = v0.z.min(v1.z).min(v2.z); - let max_z = v0.z.max(v1.z).max(v2.z); + let d1p = p1 - p0; + let d1q = q1 - q0; + let d2p = p2 - p0; + let d2q = q2 - q0; + let det = d1p * d2q - d1q * d2p; + if det.abs() < 1e-18 { + return; + } + let inv_det = 1.0 / det; + let d1r = r1 - r0; + let d2r = r2 - r0; + let delta = if det < 0.0 { 1 } else { -1 }; - let y0 = (((min_y - origin.y) * inv_h).floor() as isize).clamp(1, ny as isize - 2) as usize; - let y1 = (((max_y - origin.y) * inv_h).ceil() as isize).clamp(1, ny as isize - 2) as usize; - let z0 = (((min_z - origin.z) * inv_h).floor() as isize).clamp(1, nz as isize - 2) as usize; - let z1 = (((max_z - origin.z) * inv_h).ceil() as isize).clamp(1, nz as isize - 2) as usize; + let a_u = d2q * inv_det; + let b_u = -d2p * inv_det; + let a_v = -d1q * inv_det; + let b_v = d1p * inv_det; + let du_dp = a_u * h; + let dv_dp = a_v * h; + let rp0 = (op + eps_p - p0) + (p_lo as f64) * h; + let rq_base = oq + eps_q - q0; + let u_base = rp0 * a_u; + let v_base = rp0 * a_v; - let d1y = v1.y - v0.y; - let d1z = v1.z - v0.z; - let d2y = v2.y - v0.y; - let d2z = v2.z - v0.z; - let det = d1y * d2z - d1z * d2y; - if det.abs() < 1e-18 { - continue; + for iq in q_lo..=q_hi { + let rq = rq_base + (iq as f64) * h; + let mut u = u_base + rq * b_u; + let mut v = v_base + rq * b_v; + for ip in p_lo..=p_hi { + if u >= 0.0 && v >= 0.0 && u + v <= 1.0 { + let r_hit = r0 + u * d1r + v * d2r; + push_hit(ip, iq, r_hit, delta); + } + u += du_dp; + v += dv_dp; } - let inv_det = 1.0 / det; - let d1x = v1.x - v0.x; - let d2x = v2.x - v0.x; - let delta = if det < 0.0 { 1 } else { -1 }; + } + } - let a_u = d2z * inv_det; - let b_u = -d2y * inv_det; - let a_v = -d1z * inv_det; - let b_v = d1y * inv_det; - let du_dy = a_u * h; - let dv_dy = a_v * h; - let ry0 = (origin.y + eps_y - v0.y) + (y0 as f64) * h; - let rz_base = origin.z + eps_z - v0.z; - let u_base = ry0 * a_u; - let v_base = ry0 * a_v; + for fi in 0..mesh.faces.len() { + for [ia, ib, ic] in mesh.triangulate_face(fi) { + let v0 = mesh.verts[ia]; + let v1 = mesh.verts[ib]; + let v2 = mesh.verts[ic]; - for iz in z0..=z1 { - let rz = rz_base + (iz as f64) * h; - let mut u = u_base + rz * b_u; - let mut v = v_base + rz * b_v; - let row_off = iz * ny; - for iy in y0..=y1 { - if u >= 0.0 && v >= 0.0 && u + v <= 1.0 { - let x_hit = v0.x + u * d1x + v * d2x; - scanlines[row_off + iy].push((x_hit, delta)); - } - u += du_dy; - v += dv_dy; - } - } + project_tri_scanlines( + v0.x, v0.y, v0.z, v1.x, v1.y, v1.z, v2.x, v2.y, v2.z, + origin.y, origin.z, ny, nz, eps_y, eps_z, h, inv_h, + |iy, iz, x_hit, delta| scanlines[iz * ny + iy].push((x_hit, delta)), + ); + project_tri_scanlines( + v0.y, v0.x, v0.z, v1.y, v1.x, v1.z, v2.y, v2.x, v2.z, + origin.x, origin.z, nx, nz, eps_x, eps_z, h, inv_h, + |ix, iz, y_hit, _| scan_y[iz * nx + ix].push(y_hit), + ); + project_tri_scanlines( + v0.z, v0.x, v0.y, v1.z, v1.x, v1.y, v2.z, v2.x, v2.y, + origin.x, origin.y, nx, ny, eps_x, eps_y, h, inv_h, + |ix, iy, z_hit, _| scan_z[iy * nx + ix].push(z_hit), + ); } } @@ -210,28 +245,55 @@ pub fn remesh_voxel(mesh: &PolyMesh, voxel_size: f64, op_id: u32) -> Result> a) & 1) != ((mask >> b) & 1) { let mut mid = (corners[a] + corners[b]) * 0.5; - if (a ^ b) == 1 { - let dy = (a >> 1) & 1; - let dz = (a >> 2) & 1; - let xs = &scanlines[(z + dz) * ny + (y + dy)]; - let mut best_x = mid.x; + let snap = |hits: &[f64], lo: f64, hi: f64, def: f64| -> f64 { + let mut best = def; let mut best_d = h * 0.6; - for &(xh, _) in xs { - if xh >= px0 - 1e-9 && xh <= px0 + h + 1e-9 { - let d = (xh - mid.x).abs(); + for &xh in hits { + if xh >= lo - 1e-9 && xh <= hi + 1e-9 { + let d = (xh - def).abs(); if d < best_d { best_d = d; - best_x = xh; + best = xh; } } } - mid.x = best_x; + best + }; + match a ^ b { + 1 => { + let dy = (a >> 1) & 1; + let dz = (a >> 2) & 1; + let xs = &scanlines[(z + dz) * ny + (y + dy)]; + let mut best = mid.x; + let mut best_d = h * 0.6; + for &(xh, _) in xs { + if xh >= px0 - 1e-9 && xh <= px0 + h + 1e-9 { + let d = (xh - mid.x).abs(); + if d < best_d { + best_d = d; + best = xh; + } + } + } + mid.x = best; + } + 2 => { + let dx = a & 1; + let dz = (a >> 2) & 1; + mid.y = snap(&scan_y[(z + dz) * nx + (x + dx)], py0, py0 + h, mid.y); + } + 4 => { + let dx = a & 1; + let dy = (a >> 1) & 1; + mid.z = snap(&scan_z[(y + dy) * nx + (x + dx)], pz0, pz0 + h, mid.z); + } + _ => {} } sum += mid; cnt += 1; diff --git a/crates/forma-core/tests/kernel_tests.rs b/crates/forma-core/tests/kernel_tests.rs index 5095959..b8da137 100644 --- a/crates/forma-core/tests/kernel_tests.rs +++ b/crates/forma-core/tests/kernel_tests.rs @@ -1150,3 +1150,57 @@ fn test_deform_curve_on_3d_bezier_has_no_false_self_intersection() { let inv = s.set_part("curved", curved).expect("smooth curved beam must pass strict manifold check"); assert!(inv.is_valid_solid()); } + +#[test] +fn test_issue_93_open_top_requires_flat_top_face() { + let sph = forma_core::primitives::make_sphere(1.0, 24, 16, 1); + let err = forma_core::ops::shell_solid(&sph, 0.1, true, 2).unwrap_err(); + assert!(err.contains("open_top=true"), "expected open_top error on sphere, got {err}"); + let err2 = forma_core::deform::solidify_mesh(&sph, 0.1, -1.0, true, 3).unwrap_err(); + assert!(err2.contains("open_top=true"), "expected open_top error on solidify sphere, got {err2}"); + + let mut hcyl = forma_core::primitives::make_cylinder(1.0, 3.0, 24, 4); + hcyl.rotate_deg(0.0, 90.0, 0.0); + assert!(forma_core::ops::shell_solid(&hcyl, 0.1, true, 5).is_err()); + + // Closed shell (open_top=false) on sphere still works cleanly + let closed = forma_core::ops::shell_solid(&sph, 0.1, false, 6).unwrap(); + let cinv = MeshInvariants::compute(&closed); + assert!(cinv.is_valid_solid() && cinv.components == 2); +} + +#[test] +fn test_issue_94_bevel_general_polyhedron_is_deterministic() { + let build = || -> Vec { + let mut m = forma_core::primitives::make_box(2.0, 1.0, 1.0, 1); + m = forma_core::ops::bevel_edges(&m, &EdgeSelector::All, 0.1, 1, 2).unwrap(); + let stats = forma_core::uv::unwrap_lscm(&mut m, 45.0, 0.01).unwrap(); + assert_eq!(stats.inverted_tris, 0); + forma_core::export::write_glb_bytes(&m, true).unwrap() + }; + let first = build(); + for _ in 0..8 { + assert_eq!(build(), first, "bevel(all) + unwrap() must produce byte-identical GLB"); + } +} + +#[test] +fn test_issue_81_remesh_voxel_preserves_center_all_axes() { + for &vs in &[0.03, 0.05, 0.1] { + let b = forma_core::primitives::make_box(1.0, 1.0, 1.0, 1); + let r = forma_core::remesh::remesh_voxel(&b, vs, 2).unwrap(); + let c = r.aabb().center(); + assert!( + c.len() < 1e-3, + "voxel_size={vs}: expected center near (0,0,0), got {c:?}" + ); + } + let mut b_off = forma_core::primitives::make_box(1.0, 1.0, 1.0, 3); + b_off.translate(Vec3::new(10.0, 10.0, 10.0)); + let r_off = forma_core::remesh::remesh_voxel(&b_off, 0.1, 4).unwrap(); + let c_off = r_off.aabb().center(); + assert!( + (c_off - Vec3::new(10.0, 10.0, 10.0)).len() < 1e-3, + "translated box center shifted to {c_off:?}" + ); +}