diff --git a/crates/kernel/src/face_source_tests.rs b/crates/kernel/src/face_source_tests.rs index c46f0008b..2ba51f6c1 100644 --- a/crates/kernel/src/face_source_tests.rs +++ b/crates/kernel/src/face_source_tests.rs @@ -100,7 +100,14 @@ fn construction_planes_follow_reflected_and_sheared_affine_images() { [[-1., 0., 0., 0.], [0.4, 2., 0., 0.], [0., 0.3, 1., 0.], [8., 9., 10., 1.]], ] { let placed = transform_matrix(&body, m).unwrap(); - for (original, transformed) in body.face_sources().unwrap().iter().zip(placed.face_sources().unwrap()) { + // SMLib numbers the placed faces afresh: each is matched with the face its history names. + let history = placed.history().unwrap(); + let originals = body.face_sources().unwrap(); + for (f, transformed) in placed.face_sources().unwrap().iter().enumerate() { + let [crate::history::FaceOrigin::Face { operand: 0, face }] = history.faces[f].origins.as_slice() else { + panic!("face {f} has no one origin") + }; + let original = &originals[*face]; let ( Some(FaceSource::Box { producer: a, axis: aa, high: ah, plane: ap }), Some(FaceSource::Box { producer: b, axis: ba, high: bh, plane: bp }), diff --git a/crates/kernel/src/issue_2005_tests.rs b/crates/kernel/src/issue_2005_tests.rs index 532a07b43..98c67eda9 100644 --- a/crates/kernel/src/issue_2005_tests.rs +++ b/crates/kernel/src/issue_2005_tests.rs @@ -31,8 +31,10 @@ fn circle_path() -> [PathSeg; 2] { 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 has Euler characteristic 2 - 2g. Coincident caps gave 2. - assert_eq!(m.vertices + m.faces + 2 * genus, m.edges + 2, "{m:?}"); + // 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"); diff --git a/crates/kernel/src/ops.rs b/crates/kernel/src/ops.rs index fe9b33ad2..053368f66 100644 --- a/crates/kernel/src/ops.rs +++ b/crates/kernel/src/ops.rs @@ -141,15 +141,44 @@ pub fn transform_matrix(body: &Body, m: [[f64; 4]; 4]) -> Result { return placed.with_history(history, &[body]); } let sources = body.transformed_sources(m)?; - let placed = match Rigid::of_matrix(&m) { + let (placed, sources, paint) = match Rigid::of_matrix(&m) { Some(motion) => { let moved = Body::from_record(body.record().moved(&motion))?; let history = History::identity(&moved); - moved.with_history(history, &[body])? + (moved.with_history(history, &[body])?, sources, body.paint().cloned()) + } + None => { + // SMLib numbers the moved body's faces afresh: each face's source and paint follow it + // by the history (a face whose origin is not known keeps no source; with no history + // the paint stays as it was). + let placed = transformed(body, &m)?; + let origin: Vec> = (0..placed.face_count()).map(|f| moved_from(placed.history(), f)).collect(); + let sources = sources.map(|s| std::sync::Arc::new(origin.iter().map(|o| o.and_then(|j| s.get(j).cloned().flatten())).collect())); + let paint = body.paint().cloned().map(|mut p| { + if placed.history().is_none() { + return p; + } + p.faces = p + .faces + .iter() + .flat_map(|fp| { + origin.iter().enumerate().filter(|(_, o)| **o == Some(fp.face)).map(|(face, _)| crate::FacePaint { face, ..fp.clone() }) + }) + .collect(); + p + }); + (placed, sources, paint) } - None => transformed(body, &m)?, }; - Ok(placed.with_color(body.color()).with_paint(body.paint().cloned()).with_face_sources(sources).inherit_regularization_protection(body)) + Ok(placed.with_color(body.color()).with_paint(paint).with_face_sources(sources).inherit_regularization_protection(body)) +} + +/// The face of the operand that face `f` of a moved body is, by the move's history. +fn moved_from(history: Option<&History>, f: usize) -> Option { + match history?.faces.get(f)?.origins.as_slice() { + [crate::history::FaceOrigin::Face { operand: 0, face }] => Some(*face), + _ => None, + } } /// `body` moved by a matrix that is not a rigid motion, in SMLib, its history from SMLib's tags. diff --git a/crates/kernel/src/prism_curve.rs b/crates/kernel/src/prism_curve.rs index 611ffc3e2..508ef1f06 100644 --- a/crates/kernel/src/prism_curve.rs +++ b/crates/kernel/src/prism_curve.rs @@ -578,14 +578,35 @@ mod tests { } out } - fn trap() -> Vec { - let mut p = vec![1.]; - for k in 0..=16 { - p = multiply(&p, &[-(k as f64) / 16., 1.]); + /// The power basis `p` (in t) over the span t = a + h s, in s. + fn shifted(p: &[f64], a: f64, h: f64) -> Vec { + let mut out = vec![0.]; + for c in p.iter().rev() { + out = multiply(&out, &[a, h]); + out[0] += c; } - let mid = (0..=16).map(|k| 1. / 32. - k as f64 / 16.).product::(); - p.iter_mut().for_each(|v| *v /= mid); - p + out + } + /// A spline of `degree` over spans [k/16, (k+1)/16] (k = 0..16), joined C0 (each interior + /// knot `degree` times), whose homogeneous `x`, `y`, `w` are the power bases `f(k)` gives + /// in the span's own parameter s ∈ [0, 1]: a curve that bends only between the knots. + fn spans(degree: usize, f: impl Fn(usize) -> [Vec; 3]) -> Curve { + let mut knots = vec![0.; degree + 1]; + let mut homogeneous: Vec<[f64; 4]> = Vec::new(); + for k in 0..16 { + let [x, y, w] = f(k); + let (x, y, w) = (bernstein(&x, degree), bernstein(&y, degree), bernstein(&w, degree)); + let controls = x.iter().zip(y).zip(w).map(|((x, y), w)| [*x, y, 0., w]); + homogeneous.extend(controls.skip(usize::from(k > 0))); + knots.extend(std::iter::repeat_n((k + 1) as f64 / 16., degree)); + } + knots.push(1.); + Curve::RationalSpline { knots, homogeneous } + } + /// The bump s (1 - s) / (1/4) in the span's own parameter: 0 at both its ends (every old + /// sample location k/16 is a knot), 1 half way along the first span (t = 1/32). + fn bump(k: usize) -> Vec { + if k == 0 { vec![0., 4., -4.] } else { vec![0.] } } fn bezier_knots(degree: usize) -> Vec { std::iter::repeat_n(0., degree + 1).chain(std::iter::repeat_n(1., degree + 1)).collect() @@ -615,27 +636,37 @@ mod tests { Curve::RationalSpline { knots: bezier_knots(2), homogeneous: vec![[10., 0., 0., 1.], [10. * w, 10. * w, 0., w], [0., 10., 0., 1.]] } } + /// Sampling must not certify a spline cap that merely passes through the sample points. + /// Once that took a single polynomial of degree 17 with a root at each of the 17 old sample + /// locations k/16, more than a B-rep record holds (MAX_SPLINE_DEGREE 16); here the aliasing is + /// in the knots instead: a C0 quadratic spline with a knot at every k/16, on the line (or + /// the circle) at each of them, and bowing off it between (by 1 mm, 1/32 along). #[test] fn sample_aliasing_does_not_certify_spline_caps() { - let e = curve(&[0., 10.], &trap(), &[1.], 17); + let e = spans(2, |k| { + let h = 1. / 16.; + [shifted(&[0., 10.], k as f64 * h, h), bump(k), vec![1.]] + }); for k in 0..=16 { - assert!(at(&e, k as f64 / 16.).y.abs() < 1e-4, "old sample locations must see a line"); + assert!(at(&e, k as f64 / 16.).y.abs() < 1e-12, "old sample locations must see a line"); } - assert!((at(&e, 1. / 32.).y - 1.).abs() < 1e-6); + assert!((at(&e, 1. / 32.).y - 1.).abs() < 1e-12); assert!(segment(&run(&e, true), &Plane::XY, 1e-3, &mut 2_000_000).is_none()); - let q = trap(); - let mut perturb = q.iter().map(|v| v * 0.1).collect::>(); - perturb[0] += 1.; + // The quarter circle of radius 10, its homogeneous coordinates scaled by 1 + bump / 10 but + // its weight not: radius 10 at every k/16, 11 at 1/32. let w = std::f64::consts::FRAC_1_SQRT_2; - let x = multiply(&[10., 20. * (w - 1.), 10. * (1. - 2. * w)], &perturb); - let y = multiply(&[0., 20. * w, 10. * (1. - 2. * w)], &perturb); - let circle = curve(&x, &y, &[1., 2. * (w - 1.), 2. * (1. - w)], 19); + let (x, y, wt) = ([10., 20. * (w - 1.), 10. * (1. - 2. * w)], [0., 20. * w, 10. * (1. - 2. * w)], [1., 2. * (w - 1.), 2. * (1. - w)]); + let circle = spans(4, |k| { + let (a, h) = (k as f64 / 16., 1. / 16.); + let scale: Vec = bump(k).iter().enumerate().map(|(i, v)| v / 10. + if i == 0 { 1. } else { 0. }).collect(); + [multiply(&shifted(&x, a, h), &scale), multiply(&shifted(&y, a, h), &scale), shifted(&wt, a, h)] + }); for k in 0..=16 { let p = at(&circle, k as f64 / 16.); - assert!((p.x.hypot(p.y) - 10.).abs() < 1e-3, "old sample locations must see a circle"); + assert!((p.x.hypot(p.y) - 10.).abs() < 1e-9, "old sample locations must see a circle"); } let p = at(&circle, 1. / 32.); - assert!((p.x.hypot(p.y) - 11.).abs() < 1e-5); + assert!((p.x.hypot(p.y) - 11.).abs() < 1e-9); assert!(segment(&run(&circle, true), &Plane::XY, 1e-3, &mut 2_000_000).is_none()); } @@ -649,7 +680,8 @@ mod tests { assert!((sweep.abs() - std::f64::consts::FRAC_PI_2).abs() < 1e-8); assert_eq!(sweep > 0., forward); } - let line = curve(&[0., 10.], &[0.], &[1.], 17); + // A line written at the highest degree a B-rep record holds (MAX_SPLINE_DEGREE). + let line = curve(&[0., 10.], &[0.], &[1.], solidcraft_geom::bspline::MAX_SPLINE_DEGREE); assert!(matches!(segment(&run(&line, true), &Plane::XY, 1e-5, &mut 2_000_000).unwrap()[0], Seg2::Line { .. })); // The longitudinal derivative is negative at t=1/3, independently // establishing genuine retracing rather than a stationary tangent. diff --git a/crates/kernel/src/record_tests.rs b/crates/kernel/src/record_tests.rs index 54ac6c194..3590a7dca 100644 --- a/crates/kernel/src/record_tests.rs +++ b/crates/kernel/src/record_tests.rs @@ -439,13 +439,13 @@ fn check_sweep(b: &Body, plane: &Plane, r: &Region2, faces_per_segment: Option = (0..=40) .map(|i| { diff --git a/crates/kernel/src/smlib_sweeps.rs b/crates/kernel/src/smlib_sweeps.rs index 679a3630c..62386961f 100644 --- a/crates/kernel/src/smlib_sweeps.rs +++ b/crates/kernel/src/smlib_sweeps.rs @@ -565,9 +565,18 @@ mod imp { if runs.len() > 1 { return cornered(plane, pieces, path, &runs, translate); } - let (brep, _) = swept(plane, pieces, path, translate)?; + let (brep, [_, middle, last]) = swept(plane, pieces, path, translate)?; let body = body_of(&brep, "sweep")?; - let roles = roles(&body, plane, None, &seeds(plane, pieces, |q| q), &[], true)?; + // The walls known by the profile carried half way (past the start's edges: a wall the + // start touches by no edge, as round a closed path, and walls along later path segments). + let inside = seeds(plane, pieces, |q| apply(&middle, q)); + let start = path.first().map(crate::PathSeg::start); + let end = path.last().map(seg_ends).transpose()?.map(|(_, b)| b); + let roles = match (start, end) { + // Round a closed path there are no caps, and the start holds seams only. + (Some(a), Some(b)) if a.dist(b) <= 1e-6 => roles(&body, &plane.offset(body.size().max(1e-9) * 10.0), None, &[], &inside, false)?, + _ => roles(&body, plane, Some(&placed(plane, &last)), &seeds(plane, pieces, |q| q), &inside, false)?, + }; with_history(body, &roles, pieces) }