Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Wave 401/509: Convert Mesh: a triangle of no area is dropped, not refused (#4561) (and 3 more)

jyviko54706bbb364f
diff --git a/crates/engine/src/tests/robustness.rs b/crates/engine/src/tests/robustness.rs
index dff5b6130a..ca302cfc8a 100644
--- a/crates/engine/src/tests/robustness.rs
+++ b/crates/engine/src/tests/robustness.rs
@@ -300,28 +300,32 @@ fn fillet_into_an_earlier_round() {
}
/// Clean seeds (seed 16: a partial face contact, fused), and the original seed 5 edge contact,
-/// now rejected without distortion.
+/// kept as two bodies without distortion.
#[test]
fn fixed_seeds_run_clean() {
for seed in FIXED_SEEDS {
if *seed == 5 {
- // Seed 5 originally joined these boxes along only an edge. Accepting
- // it required silently shifting the tool; that is now invalid input.
+ // Seed 5 originally joined these boxes along only an edge. Accepting it once required
+ // silently shifting the tool; since 94ee28d5c ("a join keeps solids that only touch as
+ // separate bodies") the join keeps the new box a body of its own and says why, as
+ // `join_touching_along_an_edge_keeps_a_separate_body` asserts in tests.rs.
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 25, "width": 15, "height": 10}));
- let error = s
- .execute(
- "solid.box",
- &json!({
- "corner": [25, 2.5, 10], "length": 17.5, "width": 17.5,
- "height": 20, "operation": "join"
- }),
- )
- .unwrap_err();
- assert!(error.to_string().contains("zero-thickness edge contact"), "{error}");
- assert!((total_volume(&s) - 3750.0).abs() < 1e-6);
- assert_eq!(s.model.state().bodies.len(), 1);
- assert!(s.model.state().bodies.first().unwrap().body.validity().is_empty());
+ let r = run(
+ &mut s,
+ "solid.box",
+ json!({
+ "corner": [25, 2.5, 10], "length": 17.5, "width": 17.5,
+ "height": 20, "operation": "join"
+ }),
+ );
+ assert_eq!(r["notice"]["contact"], "along_edge", "{r}");
+ // 25 x 15 x 10 and 17.5 x 17.5 x 20, unmoved and sharing no volume.
+ assert!((total_volume(&s) - (3750.0 + 6125.0)).abs() < 1e-6, "{}", total_volume(&s));
+ assert_eq!(s.model.state().bodies.len(), 2);
+ for b in &s.model.state().bodies {
+ assert!(b.body.validity().is_empty(), "{}: {:?}", b.name, b.body.validity());
+ }
continue;
}
if *seed == 16 {
diff --git a/crates/kernel/src/mesh_convert.rs b/crates/kernel/src/mesh_convert.rs
index 6a062f97a2..e5cf84141e 100644
--- a/crates/kernel/src/mesh_convert.rs
+++ b/crates/kernel/src/mesh_convert.rs
@@ -49,6 +49,7 @@ pub fn convert_prismatic_mesh(body: &Body, tolerance: f64) -> Result<Body> {
return Err(invalid("triangle refers to a missing vertex"));
};
let cross = (*pb - *pa).cross(*pc - *pa);
+ // `weld` dropped the triangles of no area, so none is left here.
let Some(mut n) = cross.normalized() else { return Err(invalid("degenerate triangle")) };
for (a, b) in [(a, b), (b, c), (c, a)] {
let entry = edges.entry((a.min(b), a.max(b))).or_default();
@@ -126,17 +127,39 @@ fn weld(mesh: &crate::meshbody::TriMesh, tolerance: f64) -> Result<Body> {
};
remap.push(i);
}
- let triangles = mesh
- .triangles
- .iter()
- .map(|t| {
- let (Some(&a), Some(&b), Some(&c)) = (remap.get(t[0] as usize), remap.get(t[1] as usize), remap.get(t[2] as usize)) else {
- return Err(invalid("missing triangle vertex"));
- };
- Ok([a, b, c])
- })
- .collect::<Result<Vec<_>>>()?;
- crate::mesh_body(&positions, &triangles)
+ // A triangle of no area (its corners on one line, or welded into one another) adds no
+ // surface, so it is dropped rather than refused (#4561); the closed-manifold check that
+ // follows still refuses a mesh that needed it to close. Only the vertices the kept triangles
+ // use stay, so a dropped triangle's corner cannot widen the part's extent.
+ let mut used: Vec<Option<u32>> = vec![None; positions.len()];
+ let mut kept_positions: Vec<Vec3> = Vec::new();
+ let mut triangles = Vec::with_capacity(mesh.triangles.len());
+ for t in &mesh.triangles {
+ let (Some(&a), Some(&b), Some(&c)) = (remap.get(t[0] as usize), remap.get(t[1] as usize), remap.get(t[2] as usize)) else {
+ return Err(invalid("missing triangle vertex"));
+ };
+ let (Some(&pa), Some(&pb), Some(&pc)) = (positions.get(a as usize), positions.get(b as usize), positions.get(c as usize)) else {
+ return Err(invalid("missing triangle vertex"));
+ };
+ if (pb - pa).cross(pc - pa).normalized().is_none() {
+ continue;
+ }
+ let mut keep = |i: u32| -> Result<u32> {
+ let slot = used.get_mut(i as usize).ok_or_else(|| invalid("missing triangle vertex"))?;
+ if let Some(k) = *slot {
+ return Ok(k);
+ }
+ let k = u32::try_from(kept_positions.len()).map_err(|_| invalid("too many vertices"))?;
+ kept_positions.push(positions.get(i as usize).copied().ok_or_else(|| invalid("missing triangle vertex"))?);
+ *slot = Some(k);
+ Ok(k)
+ };
+ triangles.push([keep(a)?, keep(b)?, keep(c)?]);
+ }
+ if triangles.is_empty() {
+ return Err(invalid("the mesh has no triangle of any area"));
+ }
+ crate::mesh_body(&kept_positions, &triangles)
}
fn extrusion(mesh: &crate::meshbody::TriMesh, axis: Vec3, tolerance: f64) -> Result<Option<Body>> {
@@ -475,6 +498,30 @@ mod tests {
}
}
+ /// #4561: a closed box mesh with one extra triangle of no area along its bottom front edge
+ /// ((0,0,0), (10,0,0), (20,0,0)) converts to the box: the triangle adds no surface and is
+ /// dropped, its middle corner with it. V = 20 * 10 * 5 = 1000, six faces.
+ #[test]
+ fn a_zero_area_triangle_is_dropped() {
+ let source = crate::box_solid(Vec3::ZERO, Vec3::new(20., 10., 5.)).unwrap();
+ let mesh = source.mesh(&crate::MeshTolerance::chord(0.005)).unwrap();
+ let mut positions = mesh.positions.clone();
+ let mut triangles = mesh.triangles.clone();
+ let at = |ps: &[Vec3], p: Vec3| ps.iter().position(|q| q.dist(p) < 1e-9).and_then(|i| u32::try_from(i).ok()).unwrap();
+ let (a, b) = (at(&positions, Vec3::ZERO), at(&positions, Vec3::new(20., 0., 0.)));
+ positions.push(Vec3::new(10., 0., 0.));
+ let m = u32::try_from(positions.len() - 1).unwrap();
+ triangles.push([a, m, b]);
+ let body = crate::mesh_body(&positions, &triangles).unwrap();
+ let result = convert_prismatic_mesh(&body, 0.01).unwrap();
+ assert_eq!(result.faces(0.01).unwrap().len(), 6);
+ assert!((crate::measure(&result).unwrap().volume - 1000.).abs() < 1e-6);
+ // A mesh of nothing but a triangle of no area has nothing left to convert.
+ let sliver = crate::mesh_body(&[Vec3::ZERO, Vec3::X, Vec3::new(2., 0., 0.)], &[[0, 1, 2]]).unwrap();
+ let refused = convert_prismatic_mesh(&sliver, 0.05).unwrap_err();
+ assert!(refused.to_string().contains("no triangle of any area"), "{refused}");
+ }
+
#[test]
fn a_mesh_over_the_budget_says_how_big_it_is_and_what_converts() {
// #346: a freeform mesh just over the limit (a strip of 50001 triangles) is refused with
diff --git a/tests/kernel-cases/import/KNOWN_FAILING b/tests/kernel-cases/import/KNOWN_FAILING
index 8266c0a4c9..0f96696619 100644
--- a/tests/kernel-cases/import/KNOWN_FAILING
+++ b/tests/kernel-cases/import/KNOWN_FAILING
@@ -2,4 +2,3 @@
# rig gate (.github/workflows/kernel-rig.yml) fails on a case that fails and is not listed here,
# and on a listed case that passes: a fix removes its line in the same pull request.
# Baseline: develop 5ada4fc8a with SMLib 90aabff (the run on 60b1c6a, aarch64 Linux).
-4561-convert-box-mesh-with-a-zero-area-triangle step 3 (mesh.convert): a zero-area triangle is refused as degenerate where the case expects it dropped
diff --git a/tests/kernel-cases/threads/2369-m8-thread-in-6.3mm-hole-refused/expected.json b/tests/kernel-cases/threads/2369-m8-thread-in-6.3mm-hole-refused/expected.json
index 913f3ee0d4..d8af296e56 100644
--- a/tests/kernel-cases/threads/2369-m8-thread-in-6.3mm-hole-refused/expected.json
+++ b/tests/kernel-cases/threads/2369-m8-thread-in-6.3mm-hole-refused/expected.json
@@ -7,7 +7,7 @@
],
"what": "a modelled thread on a hole that isn't the thread's size: refused with the size to use, never a silent change of the hole",
"source": "analytic",
- "method": "Decision (#1018, #2853): a modelled thread never changes the size of the hole it is on; one that isn't the thread's size is refused, naming the size to use. A fill-to-tap-drill or turn-down option would be opt-in, off by default (see #3060), and get its own case. The refusal's numbers, from ISO 68-1 (H = P sqrt3/2, basic depth 5H/8): M8 (P 1.25): tap drill = 8 - 1.25 = 6.75 mm; basic minor diameter D1 = 8 - 2 x 5H/8 = 8 - 1.353 = 6.647 mm; a 6.3 mm hole is below the minor diameter, so the thread's teeth would fill part of the hole. Expected message: \"this 6.3 mm hole is too narrow for a modelled M8 thread (its teeth would fill the hole); drill 6.75 mm for M8\".",
+ "method": "Decision (#1018, #2853): a modelled thread never changes the size of the hole it is on; one that isn't the thread's size is refused, naming the size to use. A fill-to-tap-drill or turn-down option would be opt-in, off by default (see #3060), and get its own case. The refusal's numbers, from ISO 68-1 (H = P sqrt3/2, basic depth 5H/8): M8 (P 1.25): the tap drill is the tap maker's, 6.8 mm (Yamawa YEU23 p.704, as tapped holes take it since 05f1c6b9a and case 2115 grades; major minus pitch, 6.75 mm, is only for a size no table lists); basic minor diameter D1 = 8 - 2 x 5H/8 = 8 - 1.353 = 6.647 mm; a 6.3 mm hole is below the minor diameter, so the thread's teeth would fill part of the hole. Expected message: \"this 6.3 mm hole is too narrow for a modelled M8 thread (its teeth would fill the hole); drill 6.8 mm for M8\".",
"outcome": "refused",
- "refusal": "this 6.3 mm hole is too narrow for a modelled M8 thread (its teeth would fill the hole); drill 6.75 mm for M8"
+ "refusal": "this 6.3 mm hole is too narrow for a modelled M8 thread (its teeth would fill the hole); drill 6.8 mm for M8"
}
diff --git a/tests/kernel-cases/threads/KNOWN_FAILING b/tests/kernel-cases/threads/KNOWN_FAILING
index bf482e9273..599467703e 100644
--- a/tests/kernel-cases/threads/KNOWN_FAILING
+++ b/tests/kernel-cases/threads/KNOWN_FAILING
@@ -2,4 +2,3 @@
# rig gate (.github/workflows/kernel-rig.yml) fails on a case that fails and is not listed here,
# and on a listed case that passes: a fix removes its line in the same pull request.
# Baseline: develop 5ada4fc8a with SMLib 90aabff (the run on 60b1c6a, aarch64 Linux).
-2369-m8-thread-in-6.3mm-hole-refused refused for the right reason, but the message differs from the one the case names