From fcc4d9b628281bf412bbea72dc8f441e6831baf2 Mon Sep 17 00:00:00 2001 From: jyviko <2501648+jyviko@users.noreply.github.com> Date: Thu, 22 Oct 2026 00:35:06 -0400 Subject: [PATCH] kernel: the tessellator's tests plan the body's schema 4 record The tests in tess/ build their bodies as before and plan crate::brep_record of them; the assertions stay. Changed where what they compared is gone: - surface.rs: each face evaluates through eval_surface and its record-parameter map as the record's own evaluator (SurfaceEval) evaluates it, point and natural normal over the face's box (was: as truck evaluates it); the kinds counted are the record's (planes and analytic surfaces; SMLib's primitives have no spline faces, which truck's sphere was). - edges.rs: edges are the record's; the intersection-node test becomes: every edge between two curved faces puts each node, through each face's pcurve at the node's curve parameter, on that face's surface at the node, within the edge's tolerance (was: Newton's per-node parameters within 1e-7, which tess/ssi.rs no longer computes). - tests.rs: "reference mesher" comparisons are against Body::mesh (the same mesher on the same record, at a finer tolerance for the cross-hole volume). Removed the assertion that seam::turns_more_than_once finds the thread's winding walls: that test of which mesher to use no longer exists. (not built: base in progress) tess/ has no errors under cargo check --tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017itGh5fpMEousnZ91cYcVt --- crates/kernel/src/tess/edges.rs | 98 ++++++++++++++++++++----------- crates/kernel/src/tess/surface.rs | 83 +++++++++++++------------- crates/kernel/src/tess/tests.rs | 33 +++++++---- 3 files changed, 125 insertions(+), 89 deletions(-) diff --git a/crates/kernel/src/tess/edges.rs b/crates/kernel/src/tess/edges.rs index 3dc9f3c7f..1b75d55b3 100644 --- a/crates/kernel/src/tess/edges.rs +++ b/crates/kernel/src/tess/edges.rs @@ -121,16 +121,38 @@ fn turn(a: Vec3, b: Vec3) -> f64 { mod tests { use super::*; + fn record(b: crate::Result) -> brep::Brep { + match b.and_then(|b| crate::brep_record(&b)) { + Ok(r) => r, + Err(e) => panic!("{e}"), + } + } + + fn sample(b: &brep::Brep, ei: usize, rules: &Rules, h: f64) -> Nodes { + match sample_edge(b, ei, rules, h) { + Ok(n) => n, + Err(e) => panic!("{e}"), + } + } + + fn ends(b: &brep::Brep, ei: usize) -> [Vec3; 2] { + b.edges[ei].vertices.map(|v| { + let p = b.vertices[v as usize].point; + Vec3::new(p[0], p[1], p[2]) + }) + } + #[test] fn circle_edges_are_within_tolerance_and_end_on_vertices() { - let Ok(cyl) = crate::cylinder(Vec3::ZERO, Vec3::Z, 10.0, 5.0) else { panic!("cylinder") }; + let cyl = record(crate::cylinder(Vec3::ZERO, Vec3::Z, 10.0, 5.0)); for tol in [0.1, 0.01, 0.001] { - for e in cyl.solid.edge_iter() { - let pts = sample_edge(&e, &Rules { tol, max_len: f64::INFINITY, turn: None }, f64::INFINITY).points; + for (ei, e) in cyl.edges.iter().enumerate() { + let pts = sample(&cyl, ei, &Rules { tol, max_len: f64::INFINITY, turn: None }, f64::INFINITY).points; assert!(pts.len() >= 2); - assert_eq!(pts.first().copied(), Some(from_p3(e.absolute_front().point()))); - assert_eq!(pts.last().copied(), Some(from_p3(e.absolute_back().point()))); - if matches!(e.curve(), mt::Curve::Line(_)) { + let [front, back] = ends(&cyl, ei); + assert_eq!(pts.first().copied(), Some(front)); + assert_eq!(pts.last().copied(), Some(back)); + if matches!(e.curve, Curve::Line { .. }) { assert_eq!(pts.len(), 2); continue; } @@ -146,55 +168,61 @@ mod tests { #[test] fn edges_are_split_to_the_side_length_and_turn() { - let Ok(cyl) = crate::cylinder(Vec3::ZERO, Vec3::Z, 10.0, 5.0) else { panic!("cylinder") }; - for e in cyl.solid.edge_iter() { - let pts = sample_edge(&e, &Rules { tol: 1.0, max_len: f64::INFINITY, turn: None }, 1.2).points; + let cyl = record(crate::cylinder(Vec3::ZERO, Vec3::Z, 10.0, 5.0)); + for (ei, e) in cyl.edges.iter().enumerate() { + let pts = sample(&cyl, ei, &Rules { tol: 1.0, max_len: f64::INFINITY, turn: None }, 1.2).points; for w in pts.windows(2) { assert!(w[0].dist(w[1]) <= 1.2 + 1e-9, "side {}", w[0].dist(w[1])); } - if matches!(e.curve(), mt::Curve::Line(_)) { + if matches!(e.curve, Curve::Line { .. }) { // 5 mm in sides of at most 1.2: five equal sides. assert_eq!(pts.len(), 6); } // The ends are the vertices' own points, to the last bit. - assert_eq!(pts.first().copied(), Some(from_p3(e.absolute_front().point()))); - assert_eq!(pts.last().copied(), Some(from_p3(e.absolute_back().point()))); + let [front, back] = ends(&cyl, ei); + assert_eq!(pts.first().copied(), Some(front)); + assert_eq!(pts.last().copied(), Some(back)); } // A coarse tolerance alone would leave a few long chords; a 30° facet angle keeps every // side within 30° of arc: at least 12 around each of the two circles. let thirty = Rules { tol: 5.0, max_len: f64::INFINITY, turn: Some(crate::tess::size::Turn { angle: std::f64::consts::PI / 6.0, floor: 0.0 }) }; - let mut seen = std::collections::HashSet::new(); - let sides: usize = cyl - .solid - .edge_iter() - .filter(|e| seen.insert(e.id()) && !matches!(e.curve(), mt::Curve::Line(_))) - .map(|e| sample_edge(&e, &thirty, f64::INFINITY).points.len() - 1) + let sides: usize = (0..cyl.edges.len()) + .filter(|&ei| !matches!(cyl.edges[ei].curve, Curve::Line { .. })) + .map(|ei| sample(&cyl, ei, &thirty, f64::INFINITY).points.len() - 1) .sum(); assert!(sides >= 24, "{sides}"); } - /// An intersection edge's nodes carry their parameters on both faces: each face on one of - /// the edge's surfaces gets them, and the surface there is the node. + /// The nodes of every edge where two curved faces meet carry their curve parameters: each + /// face's pcurve of the edge puts each node on that face's surface, at the node. #[test] - fn intersection_nodes_carry_their_parameters_on_both_faces() { - let Ok(pin) = crate::cylinder(Vec3::new(5.0, 5.0, -1.0), Vec3::Z, 2.0, 6.0) else { panic!("pin") }; - let Ok(cross) = crate::cylinder(Vec3::new(5.0, -1.0, 2.0), Vec3::Y, 1.0, 12.0) else { panic!("cross") }; - let Ok(Some(body)) = crate::boolean(&pin, &cross, crate::BoolOp::Cut) else { panic!("cut") }; + fn edge_nodes_carry_their_parameters_on_both_faces() { + let pin = crate::cylinder(Vec3::new(5.0, 5.0, -1.0), Vec3::Z, 2.0, 6.0); + let cross = crate::cylinder(Vec3::new(5.0, -1.0, 2.0), Vec3::Y, 1.0, 12.0); + let body = record(pin.and_then(|pin| { + let cross = cross?; + crate::boolean(&pin, &cross, crate::BoolOp::Cut)?.ok_or_else(|| crate::KernelError::Failed("cut: nothing left".into())) + })); let rules = Rules { tol: 0.01, max_len: f64::INFINITY, turn: None }; - let surfaces: Vec = body.solid.face_iter().filter_map(|f| super::super::surface::eval_surface(&f.surface())).collect(); + let curved = |f: u32| !matches!(body.faces[f as usize].surface, brep::Surface::Plane { .. } | brep::Surface::PlaneThrough { .. }); let (mut edges, mut checked) = (0, 0); - let mut seen = std::collections::HashSet::new(); - for e in body.solid.edge_iter().filter(|e| seen.insert(e.id()) && matches!(e.curve(), mt::Curve::IntersectionCurve(_))) { - let n = sample_edge(&e, &rules, f64::INFINITY); - let on: Vec<_> = surfaces.iter().filter_map(|s| n.params_on(s).map(|q| (s, q))).collect(); - assert_eq!(on.len(), 2, "both faces of an intersection edge know its parameters"); + for (ei, e) in body.edges.iter().enumerate() { + if e.uses.len() != 2 || !e.uses.iter().all(|u| curved(u.face)) { + continue; + } + let n = sample(&body, ei, &rules, f64::INFINITY); edges += 1; - for (s, q) in on { - for (p, uv) in n.points.iter().zip(q).skip(1).take(n.points.len().saturating_sub(2)) { - let Some(uv) = uv else { continue }; - let Some((at, _, _)) = s.derivs(uv[0], uv[1]) else { panic!("evaluates") }; - assert!(at.dist(*p) < 1e-7, "{at:?} vs {p:?}"); + for u in &e.uses { + let face = &body.faces[u.face as usize]; + let brep::Loop::Edges { edges: loop_edges } = &face.loops[u.r#loop as usize] else { panic!("an edge loop") }; + let oe = &loop_edges[u.position as usize]; + let Ok((s, map)) = crate::tess::surface::eval_surface(&face.surface, face.domain) else { panic!("evaluates") }; + for (t, p) in n.params.iter().zip(&n.points) { + let Some(q) = oe.pcurve.eval(*t) else { panic!("pcurve at {t}") }; + let q = map.to_eval(q); + let Some((at, _, _)) = s.derivs(q[0], q[1]) else { panic!("evaluates") }; + assert!(at.dist(*p) < 1e-6 + e.tolerance, "{at:?} vs {p:?}"); checked += 1; } } diff --git a/crates/kernel/src/tess/surface.rs b/crates/kernel/src/tess/surface.rs index 67b5daa68..ad2e5035e 100644 --- a/crates/kernel/src/tess/surface.rs +++ b/crates/kernel/src/tess/surface.rs @@ -168,36 +168,33 @@ impl Reversed for EvalCurve { #[cfg(test)] mod tests { use super::*; - use crate::body::Solid; - /// Every face of `solid` evaluates as truck evaluates it, over its own parameter box. - fn assert_matches_truck(solid: &Solid) -> [usize; 4] { - let mut kinds = [0; 4]; - for face in solid.face_iter() { - let surface = face.surface(); - let kind = match surface { - mt::Surface::Plane(_) => 0, - mt::Surface::BSplineSurface(_) => 1, - mt::Surface::NurbsSurface(_) => 2, - mt::Surface::RevolutedCurve(_) => 3, + /// Every face of `b` evaluates, through [`eval_surface`] and its [`Params`], as the record's + /// own evaluator ([`SurfaceEval`]) evaluates it, over the face's parameter box: the same + /// point and the same natural normal. Counts of planes, analytic surfaces and others. + fn assert_matches_the_record(b: &brep::Brep) -> [usize; 3] { + let mut kinds = [0; 3]; + for face in &b.faces { + let kind = match face.surface { + brep::Surface::Plane { .. } | brep::Surface::PlaneThrough { .. } => 0, + brep::Surface::Cylinder { .. } | brep::Surface::Cone { .. } | brep::Surface::Sphere { .. } | brep::Surface::Torus { .. } => 1, + _ => 2, }; - let Some(eval) = eval_surface(&surface) else { panic!("not evaluable: {surface:?}") }; - // Sample the face's parameter box from its boundary vertices. - let pts: Vec<(f64, f64)> = face.vertex_iter().filter_map(|v| surface.search_parameter(v.point(), None, 100)).collect(); - if pts.is_empty() { - continue; - } - let (u0, u1) = pts.iter().fold((f64::MAX, f64::MIN), |(a, b), p| (a.min(p.0), b.max(p.0))); - let (v0, v1) = pts.iter().fold((f64::MAX, f64::MIN), |(a, b), p| (a.min(p.1), b.max(p.1))); + let (eval, map) = match eval_surface(&face.surface, face.domain) { + Ok(e) => e, + Err(e) => panic!("not evaluable: {e} ({:?})", face.surface), + }; + let Ok(reference) = SurfaceEval::new(&face.surface) else { panic!("the record's evaluator: {:?}", face.surface) }; + let [[u0, u1], [v0, v1]] = face.domain; for i in 0..=6 { for j in 0..=6 { - let (u, v) = (u0 + (u1 - u0) * i as f64 / 6.0, v0 + (v1 - v0) * j as f64 / 6.0); - let (p, n) = eval.eval(u, v); - let want = from_p3(surface.subs(u, v)); - assert!(p.dist(want) < 1e-9 * (1.0 + want.len()), "point at ({u}, {v}): {p:?} vs {want:?}"); - let wn = from_v3(surface.normal(u, v)); - if wn.len() > 0.5 { - assert!(n.dist(wn) < 1e-6, "normal at ({u}, {v}): {n:?} vs {wn:?} ({surface:?})"); + let uv = [u0 + (u1 - u0) * i as f64 / 6.0, v0 + (v1 - v0) * j as f64 / 6.0]; + let q = map.to_eval(uv); + let (p, n) = eval.eval(q[0], q[1]); + let Some((want, su, sv)) = reference.derivs(uv[0], uv[1]) else { panic!("reference at {uv:?}") }; + assert!(p.dist(want) < 1e-9 * (1.0 + want.len()), "point at {uv:?}: {p:?} vs {want:?}"); + if let Some(wn) = su.cross(sv).normalized().filter(|_| su.cross(sv).len() > 1e-9 * (su.len() * sv.len()).max(1e-12)) { + assert!(n.dist(wn) < 1e-6, "normal at {uv:?}: {n:?} vs {wn:?} ({:?})", face.surface); } kinds[kind] += 1; } @@ -206,32 +203,36 @@ mod tests { kinds } - fn solid(b: crate::Body) -> Solid { - (*b.solid).clone() + fn record(b: crate::Result) -> brep::Brep { + match b.and_then(|b| crate::brep_record(&b)) { + Ok(r) => r, + Err(e) => panic!("{e}"), + } } #[test] - fn primitives_evaluate_like_truck() { - let Ok(bx) = crate::box_solid(Vec3::new(-1.0, -2.0, 0.0), Vec3::new(3.0, 4.0, 5.0)) else { panic!("box") }; - let Ok(cyl) = crate::cylinder(Vec3::new(1.0, 2.0, 3.0), Vec3::new(0.0, 1.0, 1.0), 2.5, 7.0) else { panic!("cylinder") }; - let Ok(sph) = crate::sphere(Vec3::new(0.0, 0.0, 1.0), 4.0) else { panic!("sphere") }; - let Ok(tor) = crate::torus(Vec3::ZERO, 6.0, 1.5) else { panic!("torus") }; - let mut kinds = [0; 4]; - for b in [bx, cyl, sph, tor] { - for (k, n) in kinds.iter_mut().zip(assert_matches_truck(&solid(b))) { + fn primitives_evaluate_like_the_record() { + let bodies = [ + crate::box_solid(Vec3::new(-1.0, -2.0, 0.0), Vec3::new(3.0, 4.0, 5.0)), + crate::cylinder(Vec3::new(1.0, 2.0, 3.0), Vec3::new(0.0, 1.0, 1.0), 2.5, 7.0), + crate::sphere(Vec3::new(0.0, 0.0, 1.0), 4.0), + crate::torus(Vec3::ZERO, 6.0, 1.5), + ]; + let mut kinds = [0; 3]; + for b in bodies { + for (k, n) in kinds.iter_mut().zip(assert_matches_the_record(&record(b))) { *k += n; } } - // Planes, rational patches (sphere) and revolved profiles (cylinder, torus) all covered. - assert!(kinds[0] > 0 && kinds[3] > 0 && kinds[1] + kinds[2] > 0, "{kinds:?}"); + // Planes and the analytic surfaces (cylinder, sphere, torus) all covered. + assert!(kinds[0] > 0 && kinds[1] > 0, "{kinds:?}"); } #[test] - fn transformed_and_mirrored_bodies_evaluate_like_truck() { + fn transformed_and_mirrored_bodies_evaluate_like_the_record() { let Ok(cyl) = crate::cylinder(Vec3::ZERO, Vec3::Z, 2.0, 5.0) else { panic!("cylinder") }; // Column-major: x → 3 − x, z → z + 1. let mirror = [[-1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [3.0, 0.0, 1.0, 1.0]]; - let Ok(m) = crate::transform_matrix(&cyl, mirror) else { panic!("transform") }; - assert!(assert_matches_truck(&solid(m)).iter().sum::() > 0); + assert!(assert_matches_the_record(&record(crate::transform_matrix(&cyl, mirror))).iter().sum::() > 0); } } diff --git a/crates/kernel/src/tess/tests.rs b/crates/kernel/src/tess/tests.rs index 2e5f98046..977955cdc 100644 --- a/crates/kernel/src/tess/tests.rs +++ b/crates/kernel/src/tess/tests.rs @@ -26,8 +26,13 @@ fn assert_watertight(m: &Mesh, name: &str) { assert!(open.is_empty(), "{name}: {} unmatched directed edges, e.g. {:?}", open.len(), open.first()); } +/// The body's schema 4 record, which the planner takes. +fn record(b: &Body) -> solidcraft_geom::brep::Brep { + ok(crate::brep_record(b)) +} + fn plan_body(b: &Body, tol: f64) -> (Mesh, PlanStats) { - match plan(&b.solid, tol, Some(crate::tess::size::Turn { angle: crate::DISPLAY_ANGLE, floor: tol * 1e-3 })) { + match plan(&record(b), tol, Some(crate::tess::size::Turn { angle: crate::DISPLAY_ANGLE, floor: tol * 1e-3 })) { Ok((p, stats)) => (p.evaluate(), stats), Err(e) => panic!("plan: {e}"), } @@ -42,7 +47,7 @@ fn check(name: &str, b: &Body, tol: f64, volume: f64) -> PlanStats { assert!((got - volume).abs() <= area * tol * 1.5 + 1e-9, "{name}: volume {got} vs {volume} (area {area}, tol {tol})"); // Outward normals: the divergence theorem gives a positive volume only if they are. assert!(got > 0.0, "{name}: inside out"); - let faces = b.solid.face_iter().count(); + let faces = b.face_count(); assert_eq!(stats.planar + stats.structured + stats.delaunay, faces, "{name}: every face planned"); stats } @@ -116,8 +121,9 @@ fn holes_and_trimmed_faces_are_watertight() { let (m, stats) = plan_body(&body, tol); assert_watertight(&m, "cross hole"); assert_eq!(stats.delaunay, 0, "{stats:?}"); - // Against the reference mesher's volume (no closed form for the two-cylinder intersection). - let Ok(reference) = body.mesh(&crate::MeshTolerance::chord(tol * 0.2)) else { panic!("reference mesh") }; + // Against the body's mesh at a fifth of the tolerance (no closed form for the two-cylinder + // intersection). + let Ok(reference) = body.mesh(&crate::MeshTolerance::chord(tol * 0.2)) else { panic!("finer mesh") }; let (a, b) = (m.measure().volume, reference.measure().volume); assert!((a - b).abs() < 0.01 * b, "cross hole volume {a} vs {b}"); } @@ -126,10 +132,11 @@ fn holes_and_trimmed_faces_are_watertight() { fn faces_and_edges_keep_the_body_order() { let body = ok(crate::cylinder(Vec3::ZERO, Vec3::Z, 2.0, 5.0)); let (m, _) = plan_body(&body, 0.01); - let Ok(reference) = body.mesh(&crate::MeshTolerance::chord(0.01)) else { panic!("reference mesh") }; + // Body::mesh, which plans the body's own record: the same faces and edges, in its order. + let Ok(reference) = body.mesh(&crate::MeshTolerance::chord(0.01)) else { panic!("body mesh") }; assert_eq!(m.edges.len(), reference.edges.len()); assert_eq!(m.edge_faces, reference.edge_faces); - // Same faces in the same order: each face's area agrees with the reference mesher's. + // Same faces in the same order: each face's area agrees with the body mesh's. let face_area = |m: &Mesh, f: u32| -> f64 { m.triangles .iter() @@ -141,18 +148,19 @@ fn faces_and_edges_keep_the_body_order() { }) .sum() }; - for f in 0..body.solid.face_iter().count() as u32 { + for f in 0..body.face_count() as u32 { let (a, b) = (face_area(&m, f), face_area(&reference, f)); assert!((a - b).abs() < 0.01 * b.max(1e-9), "face {f}: {a} vs {b}"); } - // Each edge polyline ends on the same points as the reference's. + // Each edge polyline ends on the same points as the body mesh's. for (a, b) in m.edges.iter().zip(&reference.edges) { assert!(a.first().zip(b.first()).is_some_and(|(p, q)| p.dist(*q) < 1e-9)); assert!(a.last().zip(b.last()).is_some_and(|(p, q)| p.dist(*q) < 1e-9)); } } -/// Side-by-side wireframes, reference mesher (left) and plan (right): +/// Side-by-side wireframes, the body's mesh at the chord alone (left) and the plan at the +/// display angle (right): /// `SOLIDCRAFT_TESS_SVG= cargo test -p solidcraft-kernel render_wireframes -- --ignored`. #[test] #[ignore] @@ -173,10 +181,10 @@ fn render_wireframes() { ]; for (name, body) in shapes { let t0 = std::time::Instant::now(); - let Ok(old) = body.mesh(&crate::MeshTolerance::chord(tol)) else { panic!("reference") }; + let Ok(old) = body.mesh(&crate::MeshTolerance::chord(tol)) else { panic!("body mesh") }; let t_old = t0.elapsed().as_secs_f64() * 1e3; let t0 = std::time::Instant::now(); - let new = match plan(&body.solid, tol, Some(crate::tess::size::Turn { angle: crate::DISPLAY_ANGLE, floor: tol * 1e-3 })) { + let new = match plan(&record(&body), tol, Some(crate::tess::size::Turn { angle: crate::DISPLAY_ANGLE, floor: tol * 1e-3 })) { Ok((p, st)) => Some((p.evaluate(), st)), Err(e) => { println!("{name}: no plan ({e})"); @@ -186,7 +194,7 @@ fn render_wireframes() { let t_new = t0.elapsed().as_secs_f64() * 1e3; let mut svg = String::from(""); let label = |x: f64, text: String| format!("{text}"); - svg += &label(20.0, format!("meshalgo (Delaunay): {} tris, {t_old:.1} ms", old.triangles.len())); + svg += &label(20.0, format!("chord alone: {} tris, {t_old:.1} ms", old.triangles.len())); svg += &draw(&old, 0.0); if let Some((m, st)) = &new { svg += &label( @@ -499,7 +507,6 @@ fn a_modelled_thread_in_a_bore_meshes_inside_its_block() { }; let threaded = smlib_cut(&smlib_cut(&block, &bore), &smlib_groove(iso_groove(1.0, 8.0, 3.5, true), -1.0, 1.0, 12.0)); let body = ok(crate::native::body_from_smlib(&threaded)); - assert!(seam::turns_more_than_once(&body.solid), "the groove's walls wind round the bore"); let m = ok(body.mesh(&crate::MeshTolerance::chord(0.01))); let (lo, hi) = m .positions