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.
This commit is contained in:
raykholo
2026-10-21 00:58:49 +00:00
committed by GitHub
parent 1bcc3602b4
commit 79a843c963
2 changed files with 29 additions and 0 deletions
+9
View File
@@ -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]);
+20
View File
@@ -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}");
}