SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Transform: refuse a matrix that is not an affine placement (#3900)
raykholo79a843c963bd
diff --git a/crates/kernel/src/ops.rs b/crates/kernel/src/ops.rsindex b591fe9606..49df105d78 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) -> Resuif 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.rsindex 88c53df043..f63183233d 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}");+}