Files
solidcraft/crates/kernel/src/issue_2005_tests.rs
T
jyvikoandClaude Opus 5.5 e794c6b6e4 Wave 329/509: Tests: the sample-aliasing trap aliases by its knots, within the degree a record holds (and 3 more)
4 commits, squashed; the originals stay reachable from tag archive/merge-main-develop.

- d91d3f2a6 jyviko: Kernel: a body moved by a non-rigid matrix keeps each face's source and paint on that face
- c1ead0fef jyviko: Kernel: a sweep along a smooth path names every wall, and a closed one has no caps
- 6387be309 jyviko: Tests: the high-degree line prism_curve certifies is written at the record's highest degree
- 55f48e4e5 jyviko: Tests: the sample-aliasing trap aliases by its knots, within the degree a record holds

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
2026-10-29 00:31:39 -04:00

97 lines
4.1 KiB
Rust

//! Issue #2005: a closed sweep must join its end back to its start, without coincident caps.
//! The capped tool used to stall or fail when cutting the torus it lay inside.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use std::f64::consts::{FRAC_PI_2, PI, TAU};
use solidcraft_geom::{Loop2, Plane, Region2, Vec2, Vec3};
use crate::{Body, BoolOp, PathSeg, SweepControl, boolean, measure, sweep_with};
fn square(half: f64) -> Region2 {
Region2 {
outer: Loop2::polygon(&[Vec2::new(-half, -half), Vec2::new(half, -half), Vec2::new(half, half), Vec2::new(-half, half)]),
holes: vec![],
}
}
fn profile_plane() -> Plane {
Plane { origin: Vec3::new(30.0, 0.0, 0.0), x: Vec3::X, y: Vec3::Z }
}
fn circle_path() -> [PathSeg; 2] {
// A sketch circle reaches the kernel as two half arcs.
[
PathSeg::Arc { a: Vec3::new(30.0, 0.0, 0.0), center: Vec3::ZERO, axis: Vec3::Z, angle: PI },
PathSeg::Arc { a: Vec3::new(-30.0, 0.0, 0.0), center: Vec3::ZERO, axis: Vec3::Z, angle: PI },
]
}
fn tube(body: &Body, volume: f64, genus: usize) {
let m = measure(body).unwrap();
assert!((m.volume - volume).abs() < volume * 0.01, "{m:?}, expected {volume}");
// A closed orientable tube of genus g: V - E + F - (L - F) = 2 - 2g (Euler-Poincare, L the
// loops: an annular face has two). Coincident caps gave 2 - 2g + 2.
let loops: usize = body.record().faces.iter().map(|f| f.loops.len()).sum();
assert_eq!(m.vertices + 2 * m.faces + 2 * genus, m.edges + loops + 2, "{m:?}, {loops} loops");
// Every shell closed: each edge bounds two faces.
assert!(body.record().edges.iter().all(|e| e.uses.len() >= 2));
let h = body.history().expect("sweep provenance");
assert!(h.faces.iter().all(|f| f.origins.iter().all(|o| matches!(o, crate::history::FaceOrigin::Profile { .. }))));
}
#[test]
fn closed_circle_sweeps_have_no_caps() {
for twist in [0.0, FRAC_PI_2, PI, TAU, -PI] {
let ctl = SweepControl { twist, ..SweepControl::default() };
let b = sweep_with(&profile_plane(), &square(4.0), &circle_path(), &ctl).unwrap();
tube(&b, 64.0 * TAU * 30.0, 1);
}
}
#[test]
fn one_full_arc_closes_like_a_sketch_circle() {
for angle in [TAU, -TAU] {
let path = [PathSeg::Arc { a: Vec3::new(30.0, 0.0, 0.0), center: Vec3::ZERO, axis: Vec3::Z, angle }];
let b = sweep_with(&profile_plane(), &square(4.0), &path, &SweepControl::default()).unwrap();
tube(&b, 64.0 * TAU * 30.0, 1);
}
}
#[test]
fn closed_polyline_sweep_shares_its_starting_section() {
let path = crate::path_polyline(&circle_path()).unwrap();
let b = crate::sweep_path(&profile_plane(), &square(4.0), &path).unwrap();
tube(&b, 64.0 * TAU * 30.0, 1);
}
#[test]
fn closed_sweep_refuses_an_end_section_that_does_not_match() {
for ctl in [SweepControl { twist: PI / 4.0, ..SweepControl::default() }, SweepControl { scale: 2.0, ..SweepControl::default() }] {
let err = sweep_with(&profile_plane(), &square(4.0), &circle_path(), &ctl).unwrap_err();
assert!(err.to_string().contains("closed path"), "{err}");
}
}
#[test]
fn closed_holed_sweep_joins_every_loop() {
let region = Region2 { holes: vec![square(2.0).outer], ..square(4.0) };
let ctl = SweepControl { twist: PI, ..SweepControl::default() };
let b = sweep_with(&profile_plane(), &region, &circle_path(), &ctl).unwrap();
// Two disjoint toroidal shells, one bounding the cavity.
let m = measure(&b).unwrap();
assert!((m.volume - 48.0 * TAU * 30.0).abs() < m.volume * 0.02, "{m:?}");
assert_eq!(m.vertices + m.faces, m.edges, "{m:?}");
}
#[test]
fn half_turn_sweep_cuts_a_torus() {
let base = crate::torus(Vec3::ZERO, 30.0, 8.0).unwrap();
let tool = sweep_with(&profile_plane(), &square(4.0), &circle_path(), &SweepControl { twist: PI, ..SweepControl::default() }).unwrap();
let result = boolean(&base, &tool, BoolOp::Cut).unwrap().expect("cut leaves the torus wall");
let expected = (PI * 64.0 - 64.0) * TAU * 30.0;
let m = measure(&result).unwrap();
assert!((m.volume - expected).abs() < expected * 0.015, "{m:?}, expected {expected}");
}