From 79a843c963bde12dcc474831444e7f746e2bd703 Mon Sep 17 00:00:00 2001 From: raykholo Date: Wed, 21 Oct 2026 00:58:49 +0000 Subject: [PATCH] Transform: refuse a matrix that is not an affine placement (#3900) transform_matrix took any finite 4x4 matrix. A perspective entry in the last row, or a singular linear part, was built into faces that mesh to nonsense: a revolved ring placed with such a matrix measured hundreds of millions of mm3, and a boolean on it ran for over half an hour before failing with "no result". Matrices come from design files and command parameters, so they are refused with KernelError::Invalid instead. --- crates/kernel/src/ops.rs | 9 +++++++++ crates/kernel/src/tests.rs | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/crates/kernel/src/ops.rs b/crates/kernel/src/ops.rs index b591fe960..49df105d7 100644 --- a/crates/kernel/src/ops.rs +++ b/crates/kernel/src/ops.rs @@ -2126,6 +2126,15 @@ fn transform_matrix_keeping(body: &Body, m: [[f64; 4]; 4], phased: bool) -> Resu if m.iter().flatten().any(|x| !x.is_finite()) { return Err(KernelError::Invalid("non-finite transform".into())); } + // A perspective row or a flattening (from a design file or a command's parameters) leaves + // no solid: refused rather than built into faces that mesh to nonsense. + let scale = (0..3).map(|c| Vec3::new(m[c][0], m[c][1], m[c][2]).len()).fold(0.0, f64::max); + if m[0][3].abs() > 1e-12 || m[1][3].abs() > 1e-12 || m[2][3].abs() > 1e-12 || (m[3][3] - 1.0).abs() > 1e-12 { + return Err(KernelError::Invalid("the transform is not affine: its last row must be 0, 0, 0, 1".into())); + } + if det3(&m).abs() <= 1e-12 * scale.powi(3) { + return Err(KernelError::Invalid("the transform flattens the body: its linear part is singular".into())); + } if let Some(placed) = body.mesh_transformed(&m) { let history = crate::history::History::identity(&placed); return placed.with_history(history, &[body]); diff --git a/crates/kernel/src/tests.rs b/crates/kernel/src/tests.rs index 88c53df04..f63183233 100644 --- a/crates/kernel/src/tests.rs +++ b/crates/kernel/src/tests.rs @@ -3132,3 +3132,23 @@ fn a_box_cut_round_a_small_hole_of_a_long_plate_keeps_the_hole() { } } } + +/// A transform with a perspective term or a singular linear part is refused: placed into the +/// body, it built a revolved ring whose mesh measured hundreds of millions of mm³, and cutting +/// a pocket into it ran for over half an hour before failing. A translation still places it. +#[test] +fn transform_refuses_a_matrix_that_is_not_an_affine_placement() { + let rect = Loop2::polygon(&[Vec2::new(20.0, 0.0), Vec2::new(60.0, 0.0), Vec2::new(60.0, 10.0), Vec2::new(20.0, 10.0)]); + let ring = revolve(&Plane::XZ, &[Region2 { outer: rect, holes: vec![] }], Vec2::ZERO, Vec2::Y, std::f64::consts::TAU).unwrap().remove(0); + // Column-major: the last entry of the third column is a perspective term. + let perspective = [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 10.0], [0.0, 0.0, 0.0, 1.0]]; + let flat = [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0]]; + for m in [perspective, flat] { + assert!(matches!(crate::transform_matrix(&ring, m), Err(KernelError::Invalid(_))), "{m:?}"); + } + let lifted = crate::transform_matrix(&ring, [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 10.0, 1.0]]).unwrap(); + let pocket = box_solid(Vec3::new(37.0, -3.0, 7.0), Vec3::new(43.0, 3.0, 12.0)).unwrap(); + let cut = boolean(&lifted, &pocket, BoolOp::Cut).unwrap().unwrap(); + let removed = measure(&lifted).unwrap().volume - measure(&cut).unwrap().volume; + assert!((removed - 6.0 * 6.0 * 3.0).abs() < 1.0, "{removed}"); +}