SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Wave 114/509: kernel: the tessellator's tests plan the body's schema 4 record (and 1 more)
jyviko699bfc12e059
diff --git a/crates/kernel/src/tess/edges.rs b/crates/kernel/src/tess/edges.rsindex ccbbd999d1..1b75d55b35 100644--- a/crates/kernel/src/tess/edges.rs+++ b/crates/kernel/src/tess/edges.rs@@ -16,6 +16,9 @@ const MAX_DEPTH: u32 = 24;/// An edge's nodes, from its start vertex (interval min) to its end vertex (interval max).pub(crate) struct Nodes {pub points: Vec<Vec3>,+ /// The curve's parameter at each node (the edge's interval, ends included): where each+ /// face's pcurve of the edge gives the node's (u, v).+ pub params: Vec<f64>,}/// Nodes of edge `ei` of `b` from its start to its end: every chord within the tolerance of@@ -32,6 +35,10 @@ pub(crate) fn sample_edge(b: &brep::Brep, ei: usize, rules: &Rules, h: f64) -> R};let (front, back) = (vertex(0)?, vertex(1)?);let tol = rules.tol;+ let [t0, t1] = edge.interval;+ if !(t0.is_finite() && t1.is_finite() && t1 > t0) {+ return Err(fail(format!("interval [{t0}, {t1}]")));+ }if matches!(edge.curve, Curve::Line { .. }) {// Evenly, as few segments as keep each at most `h`. The ends are the vertices' own// points: `front + (back − front) · 1` is not `back` to the last bit, and the face that@@ -39,14 +46,12 @@ pub(crate) fn sample_edge(b: &brep::Brep, ei: usize, rules: &Rules, h: f64) -> R// forwards takes the next edge's first node (the vertex) — a crack.let n = if h > 0.0 { (front.dist(back) / h).ceil().clamp(1.0, (MAX_EDGE_NODES - 1) as f64) as usize } else { 1 };let mut pts: Vec<Vec3> = (0..=n).map(|i| front + (back - front) * (i as f64 / n as f64)).collect();- if let Some(p) = pts.last_mut() {+ let mut params: Vec<f64> = (0..=n).map(|i| t0 + (t1 - t0) * (i as f64 / n as f64)).collect();+ if let (Some(p), Some(t)) = (pts.last_mut(), params.last_mut()) {*p = back;+ *t = t1;}- return Ok(Nodes { points: pts });- }- let [t0, t1] = edge.interval;- if !(t0.is_finite() && t1.is_finite() && t1 > t0) {- return Err(fail(format!("interval [{t0}, {t1}]")));+ return Ok(Nodes { points: pts, params });}let curve = CurveEval::new(&edge.curve).map_err(|e| fail(e.to_string()))?;let at =@@ -67,7 +72,7 @@ pub(crate) fn sample_edge(b: &brep::Brep, ei: usize, rules: &Rules, h: f64) -> Rmid: Option<Vec3>,depth: u32,}- let mut out: Vec<Vec3> = vec![front];+ let mut out: Vec<(f64, Vec3)> = vec![(t0, front)];let mut stack: Vec<Seg> = Vec::with_capacity(64);for (i, w) in ends.windows(2).enumerate().rev() {let (Some(&pa), Some(&pb)) = (w.first(), w.get(1)) else { continue };@@ -94,13 +99,13 @@ pub(crate) fn sample_edge(b: &brep::Brep, ei: usize, rules: &Rules, h: f64) -> Rstack.push(Seg { a: m, b, pa: pm, pb, mid: Some(q3), depth: sg.depth + 1 });stack.push(Seg { a, b: m, pa, pb: pm, mid: Some(q1), depth: sg.depth + 1 });} else {- out.push(pb);+ out.push((b, pb));}}if let Some(p) = out.last_mut() {- *p = back;+ *p = (t1, back);}- Ok(Nodes { points: out })+ Ok(Nodes { points: out.iter().map(|p| p.1).collect(), params: out.iter().map(|p| p.0).collect() })}/// Angle (radians) between two directions; zero when either is empty.@@ -116,16 +121,38 @@ fn turn(a: Vec3, b: Vec3) -> f64 {mod tests {use super::*;+ fn record(b: crate::Result<crate::Body>) -> 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, extent: 0.0, turn: None, boundary_tol: 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;}@@ -141,60 +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, extent: 0.0, turn: None, boundary_tol: 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,- extent: 0.0,- turn: Some(crate::tess::size::Turn { angle: std::f64::consts::PI / 6.0, floor: 0.0 }),- boundary_tol: None,- };- 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 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 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") };- let rules = Rules { tol: 0.01, max_len: f64::INFINITY, extent: 0.0, turn: None, boundary_tol: None };- let surfaces: Vec<EvalSurface> = body.solid.face_iter().filter_map(|f| super::super::surface::eval_surface(&f.surface())).collect();+ 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 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/mod.rs b/crates/kernel/src/tess/mod.rsindex 7d06bded3b..1fd684444a 100644--- a/crates/kernel/src/tess/mod.rs+++ b/crates/kernel/src/tess/mod.rs@@ -1,6 +1,8 @@//! SolidCraft's mesher: each face of a schema 4 record ([`brep::Brep`]) triangulated in its own//! parameters, with every edge sampled once so neighbouring faces share their boundary points-//! exactly, handed over as a [`TessPlan`] for the CPU or GPU evaluator.+//! exactly, handed over as a [`TessPlan`] for the CPU or GPU evaluator. A boundary node's+//! parameters on each face come from that face's exact pcurve of the edge, at the node's curve+//! parameter: nothing is projected, and a seam's two sides are its two pcurves.//!//! Per face://! 1. a cheaper layout where the face suits one: planar faces whose boundary runs along their@@ -12,15 +14,15 @@//! with interior seeds and Ruppert refinement, every node then evaluated on the exact//! surface (and refined where a triangle sags off it);//! 3. a face even that refuses (its loops cross or do not close in (u, v), or wind round a-//! seam or pole) is an error naming the face and why. Its cause is upstream, in the face's-//! boundary or parameters, and is fixed there; there is no other mesher to fall back to.+//! seam or pole), or whose pcurves do not lie on its edges, is an error naming the face (and+//! the edge) and why. Its cause is upstream, in the record's pcurves or seams, and is fixed+//! there; there is no other mesher to fall back to.pub(crate) mod cdt;pub(crate) mod collar;pub(crate) mod coons;pub(crate) mod cutgrid;pub(crate) mod edges;-pub(crate) mod fallback;pub(crate) mod grid;pub(crate) mod motorcycle;pub(crate) mod pieces;@@ -44,8 +46,6 @@ pub struct PlanStats {pub planar: usize,pub structured: usize,pub delaunay: usize,- /// Faces no method could plan, filled flat from their boundary nodes.- pub filled: usize,}/// Curved faces from which their sizes are worked out on the worker pool.@@ -64,15 +64,6 @@ const PARALLEL_EDGES: usize = 256;const MAX_LINES: usize = 8192;/// Most points Ruppert refinement may add to one face, beyond its seeds.const MAX_REFINE: usize = 20_000;-/// Two boundary nodes this close together on the surface (mm) are one node. Far below any-/// spacing a mesh can carry, and far above the parameters a projection tells apart. See-/// [`drop_collapsed`].-const SAME_NODE: f64 = 1e-6;-/// How far a face's boundary may lie off its surface and still be planned on it, as a fraction of-/// the body's extent, at every display quality (ten chord tolerances at Normal).-const ON_SURFACE: f64 = 1e-2;-/// How much finer a refused face's edges are divided, retry by retry.-const REFINE_STEPS: [f64; 2] = [3.0, 10.0];fn unsupported(what: impl std::fmt::Display) -> KernelError {KernelError::Failed(format!("not supported yet: tessellation: {what}"))@@ -141,11 +132,12 @@ pub(crate) fn plan(b: &brep::Brep, tol: f64, turn: Option<size::Turn>) -> Result// its faces; faces then read their nodes. A planar face costs microseconds and a curved// one about a hundred, so the pool is used only where curved faces (or intersection// curves) make up the work.- let evals: Vec<EvalSurface> = faces+ let evals: Vec<(EvalSurface, surface::Params)> = faces.iter().enumerate().map(|(fi, f)| surface::eval_surface(&f.surface, f.domain).map_err(|e| unsupported(format!("face {fi} ({}): {e}", kind(&f.surface))))).collect::<Result<_>>()?;+ let (evals, params): (Vec<EvalSurface>, Vec<surface::Params>) = evals.into_iter().unzip();let curved = evals.iter().filter(|e| !matches!(e, EvalSurface::Plane { .. })).count();let sizes: Vec<(f64, [f64; 2])> =crate::par::map("face sizes", &evals, curved >= PARALLEL_SIZES, |s| Ok(size::face_size(s, &uv::domain(s), &rules)))@@ -163,46 +155,12 @@ pub(crate) fn plan(b: &brep::Brep, tol: f64, turn: Option<size::Turn>) -> Resultrelax_crowded_faces(b, &jobs, &rules, &mut nodes)?;let indexed: Vec<usize> = (0..faces.len()).collect();let planned = crate::par::map("face tessellation", &indexed, curved >= PARALLEL_FACES, |&fi| {- let (Some(face), Some(eval), Some(size)) = (faces.get(fi), evals.get(fi), sizes.get(fi)) else {+ let (Some(face), Some(eval), Some(map), Some(size)) = (faces.get(fi), evals.get(fi), params.get(fi), sizes.get(fi)) else {return Err(unsupported(format!("face {fi}: missing")));};- // Boundary nodes, loop by loop (each edge's last node is the next edge's first). A- // vertex loop (a pole) bounds no area and has no nodes of its own.- let mut loops3: Vec<Vec<Vec3>> = Vec::new();- for l in &face.loops {- let Loop::Edges { edges } = l else { continue };- let mut pts = Vec::new();- for oe in edges {- let n = nodes.get(oe.edge as usize).ok_or_else(|| unsupported(format!("face {fi}: edge {} without nodes", oe.edge)))?;- let mut ep = n.points.clone();- if !oe.forward {- ep.reverse();- }- ep.pop();- pts.extend(ep);- }- if pts.len() >= 2 {- loops3.push(pts);- }- }- Ok((loops3, known))- };- // A plan in the surface's orientation turned the face's way.- let oriented_face = |face: &mt::Face, fp: FacePlan| FacePlan {- flip: !face.orientation(),- triangles: if face.orientation() { fp.triangles } else { fp.triangles.into_iter().map(|[a, b, c]| [a, c, b]).collect() },- ..fp- };- let surface_of = |fi: usize| match sized.get(fi) {- Some((Some(eval), Some(size))) => Ok((eval.clone(), *size)),- _ => Err(unsupported(format!("face {fi}: surface not evaluable"))),- };- // One face, from the edge nodes as they are and with the rules for its level of detail.- let plan_one = |fi: usize, face: &mt::Face, nodes: &HashMap<mt::EdgeID, edges::Nodes>, rules: &size::Rules| {- let (eval, size) = surface_of(fi)?;- let (loops3, known) = boundary(face, &eval, nodes)?;+ let loops = boundary(b, fi, face, eval, map, &nodes, &rules)?;let mut stats = PlanStats::default();- let fp = plan_face(fi, kind(&face.surface), eval, &loops3, &rules, *size, &mut stats)?;+ let fp = plan_face(fi, kind(&face.surface), eval, &loops, &rules, *size, &mut stats)?;let fwd = forward.get(fi).copied().unwrap_or(true);let fp =FacePlan { flip: !fwd, triangles: if fwd { fp.triangles } else { fp.triangles.into_iter().map(|[a, b, c]| [a, c, b]).collect() }, ..fp };@@ -218,13 +176,83 @@ pub(crate) fn plan(b: &brep::Brep, tol: f64, turn: Option<size::Turn>) -> Resultstats.planar += s.planar;stats.structured += s.structured;stats.delaunay += s.delaunay;- stats.filled += s.filled;}plan.edges = nodes.into_iter().map(|n| n.points).collect();plan.edge_faces = edge_faces.into_iter().map(|fs| fs.into_iter().map(|f| u32::try_from(f).unwrap_or(u32::MAX)).collect()).collect();Ok((plan, stats))}+/// Face `fi`'s boundary, loop by loop: the shared edge nodes (each edge's last node is the next+/// edge's first) and their parameters on the face's surface, from each edge use's exact pcurve+/// at the node's curve parameter. A seam edge's two uses on one face have their own pcurves, so+/// its two sides are told apart. Each loop is unwrapped along its periodic directions (a pcurve+/// may start a period along from where the previous one ended). A vertex loop (a pole) bounds no+/// area and has no nodes of its own. A pcurve that does not cover its edge's interval, or puts a+/// node farther off the edge than the tolerances allow, is an error naming the face and edge:+/// the record is wrong there.+fn boundary(+ b: &brep::Brep,+ fi: usize,+ face: &brep::Face,+ s: &EvalSurface,+ map: &surface::Params,+ nodes: &[edges::Nodes],+ rules: &size::Rules,+) -> Result<Vec<seam::Loop>> {+ let dom = uv::domain(s);+ let fail = |e: u32, why: String| unsupported(format!("face {fi} ({}): edge {e}: {why}", kind(&face.surface)));+ let mut loops = Vec::with_capacity(face.loops.len());+ for l in &face.loops {+ let Loop::Edges { edges } = l else { continue };+ let (mut pts, mut uvs): (Vec<Vec3>, Vec<[f64; 2]>) = (Vec::new(), Vec::new());+ for oe in edges {+ let n = nodes.get(oe.edge as usize).ok_or_else(|| fail(oe.edge, "no nodes".into()))?;+ let edge = b.edges.get(oe.edge as usize).ok_or_else(|| fail(oe.edge, "no such edge".into()))?;+ let [t0, t1] = edge.interval;+ let [p0, p1] = oe.pcurve.interval().ok_or_else(|| fail(oe.edge, "a malformed pcurve".into()))?;+ let slack = 1e-9 * (t1 - t0).abs().max(t0.abs()).max(t1.abs()).max(1.0);+ if (p0 - t0).abs() > slack || (p1 - t1).abs() > slack {+ return Err(fail(oe.edge, format!("its pcurve spans [{p0}, {p1}], the edge [{t0}, {t1}]")));+ }+ // The edge's own tolerance: how far its curve may stand off the face.+ let accept = (rules.tol * 10.0).max(1e-6) + edge.tolerance.max(0.0);+ let mut ep: Vec<Vec3> = n.points.clone();+ let mut eq: Vec<[f64; 2]> = Vec::with_capacity(ep.len());+ for (k, (t, p)) in n.params.iter().zip(&n.points).enumerate() {+ let q = oe.pcurve.eval(t.clamp(p0, p1)).ok_or_else(|| fail(oe.edge, format!("its pcurve does not evaluate at t = {t}")))?;+ let q = map.to_eval(q);+ let on =+ s.derivs(q[0], q[1]).map(|d| d.0).ok_or_else(|| fail(oe.edge, format!("node {k}: the surface does not evaluate at {q:?}")))?;+ if !(on.dist(*p) <= accept) {+ return Err(fail(oe.edge, format!("node {k}: its pcurve is {} mm off the edge at t = {t}", on.dist(*p))));+ }+ eq.push(q);+ }+ if !oe.forward {+ ep.reverse();+ eq.reverse();+ }+ ep.pop();+ eq.pop();+ pts.extend(ep);+ uvs.extend(eq);+ }+ let mut prev: Option<[f64; 2]> = None;+ for q in &mut uvs {+ if let Some(pr) = prev {+ for d in 0..2 {+ q[d] = uv::unwrap(q[d], pr[d], dom.period[d]);+ }+ }+ prev = Some(*q);+ }+ if pts.len() >= 2 {+ loops.push(seam::Loop { uv: uvs, pts });+ }+ }+ Ok(loops)+}+/// Bounded work for the facet angle: a face whose boundary would get more than/// [`size::MAX_ANGLE_BOUNDARY`] segments has its curved edges sampled again at twice the angle,/// as often as it takes, down to the tolerance alone (a plate with a thousand small holes is@@ -413,7 +441,7 @@ fn plan_face(fi: usize,kind: &str,s: &EvalSurface,- loops3: &[Vec<Vec3>],+ loops: &[seam::Loop],rules: &size::Rules,size: (f64, [f64; 2]),stats: &mut PlanStats,@@ -421,35 +449,8 @@ fn plan_face(let tol = rules.tol;let (h, speed) = size;let dom = uv::domain(s);- let accept = (tol * 10.0).max(1e-6);let fail = |why: &str| unsupported(format!("face {fi} ({kind}): {why}"));- // Project every loop, unwrapping periodic parameters along it.- let mut projected: Vec<seam::Loop> = Vec::with_capacity(loops3.len());- for (li, pts) in loops3.iter().enumerate() {- let mut luv: Vec<[f64; 2]> = Vec::with_capacity(pts.len());- let mut prev: Option<[f64; 2]> = None;- for (k, p) in pts.iter().enumerate() {- // A projection seeded by the previous node.- let mut q = uv::project(s, &dom, *p, prev, accept)- .ok_or_else(|| fail(&format!("boundary node {k} of loop {li} at {p:?} does not lie on the surface")))?;- if let Some(pr) = prev {- for d in 0..2 {- q[d] = uv::unwrap(q[d], pr[d], dom.period[d]);- }- }- luv.push(q);- prev = Some(q);- }- let mut l = seam::Loop { uv: luv, pts: pts.clone() };- drop_collapsed(&mut l, speed);- for (d, per) in dom.period.iter().enumerate() {- if let Some(per) = per {- seam::close_through_pole(s, &mut l, d, *per);- }- }- projected.push(l);- }- align_periods(&mut projected, &dom);+ let mut projected: Vec<seam::Loop> = loops.to_vec();// A loop that winds around a periodic direction does not close in (u, v): the face is cut// along a parameter line into one period (a seamless cylinder face, a helical trim).// A loop that runs up a seam from a pole and straight back down it (a half sphere bounded@@ -511,7 +512,7 @@ fn plan_face(}ranges.push(start..uvs.len());}- if uvs.len() < MIN_BOUNDARY_NODES {+ if uvs.len() < 3 {return Err(fail("degenerate boundary"));}let mut bbox = [(f64::MAX, f64::MIN); 2];@@ -529,7 +530,7 @@ fn plan_face(let seeds = cdt::lattice(bbox, step, &polys, speed);let eval = |u: f64, v: f64| s.derivs(u, v).map(|(p, su, sv)| (p, su.cross(sv).normalized()));let sag = cdt::Sag { eval: &eval, tol, h, turn: rules.turn, scale: speed };- let fill = cdt::Fill { seeds: &seeds, refine: Some((sag, MAX_REFINE)), shape: !matches!(s, EvalSurface::Plane { .. }), lenient: false };+ let fill = cdt::Fill { seeds: &seeds, refine: Some((sag, MAX_REFINE)), shape: !matches!(s, EvalSurface::Plane { .. }) };cdt::triangulate(&uvs, &ranges, fill).ok_or_else(|| fail("boundary crosses itself"))};// Planar and curved faces alike: a structured grid where the boundary runs along the@@ -766,7 +767,7 @@ fn plan_pieces(f: &Region) -> Option<(Vec<[f64; 2]>, Vec<[u32; 3]>)> {let seeds = cdt::lattice(bbox, [f.h / f.speed[0], f.h / f.speed[1]], &polys, f.speed);let eval = |u: f64, v: f64| s.derivs(u, v).map(|(p, su, sv)| (p, su.cross(sv).normalized()));let sag = cdt::Sag { eval: &eval, tol: f.rules.tol, h: f.h, turn: f.rules.turn, scale: f.speed };- cdt::triangulate(&luv, &ranges, cdt::Fill { seeds: &seeds, refine: Some((sag, MAX_REFINE)), shape: true, lenient: false })+ cdt::triangulate(&luv, &ranges, cdt::Fill { seeds: &seeds, refine: Some((sag, MAX_REFINE)), shape: true })})?;let (lextra, ltris) = local;let base = u32::try_from(n + extra.len()).ok()?;diff --git a/crates/kernel/src/tess/surface.rs b/crates/kernel/src/tess/surface.rsindex 35eb5ef4f5..ad2e5035e5 100644--- a/crates/kernel/src/tess/surface.rs+++ b/crates/kernel/src/tess/surface.rs@@ -1,10 +1,11 @@//! Surface records ([`brep::Surface`]) in the evaluator's form ([`EvalSurface`]), which the-//! planner and the CPU and GPU evaluators share. Planes, splines and revolutions keep their-//! own parameters ([`SurfaceEval`] holds them in that form already). A cylinder, cone, sphere-//! or torus is the revolution it is, exactly: its generator (a line, or a circle as an exact-//! rational arc) turned about its axis, the angle as the revolution's parameter. The planner-//! finds a boundary's parameters by projecting its nodes, so the generator's own parameter-//! need not be the record's height or latitude.+//! planner and the CPU and GPU evaluators share, and the map from the record's (u, v) to the+//! evaluator's, so a face's boundary takes its parameters from its edges' exact pcurves.+//! Planes, splines and revolutions keep their own parameters ([`SurfaceEval`] holds them in+//! that form already). A cylinder, cone, sphere or torus is the revolution it is, exactly: its+//! generator (a line, or a circle as an exact rational arc) turned about its axis by the+//! record's angle u; the generator's parameter is −v on a line and, on an arc, the rational+//! parameter of the record's angle v (closed form, [`Arc::param`]).use solidcraft_geom::Vec3;use solidcraft_geom::brep::eval::SurfaceEval;@@ -20,10 +21,59 @@ fn v3(p: [f64; 3]) -> Vec3 {/// No transform: the revolution as it is.const IDENTITY: [[f64; 4]; 3] = [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]];-/// `surface` in the evaluator's form. `domain` is the face's parameter box in the record's-/// parameters, `[[u0, u1], [v0, v1]]`: a cylinder's or cone's generator spans the face's-/// heights (an unbounded line has no parameter box to project into).-pub(crate) fn eval_surface(surface: &brep::Surface, domain: [[f64; 2]; 2]) -> Result<EvalSurface> {+/// How a face's record parameters become its evaluator's.+#[derive(Clone, Copy, Debug, PartialEq)]+pub(crate) enum Params {+ /// The same parameters (planes, splines, revolutions).+ Same,+ /// A cylinder or cone: (angle, height) to (−height, angle).+ Height,+ /// A sphere or torus: (angle, meridian angle) to (the reversed arc's parameter, angle).+ Meridian(Arc),+}++impl Params {+ /// The evaluator's parameters of the record's `uv`.+ pub(crate) fn to_eval(&self, uv: [f64; 2]) -> [f64; 2] {+ let [u, v] = uv;+ match self {+ Params::Same => uv,+ Params::Height => [-v, u],+ Params::Meridian(a) => [1.0 - a.param(v), u],+ }+ }+}++/// The spans of a circular arc made by [`arc`]: from angle `start`, `count` spans of `step`+/// each, over the parameter [0, 1]; `periodic` for a whole turn.+#[derive(Clone, Copy, Debug, PartialEq)]+pub(crate) struct Arc {+ start: f64,+ step: f64,+ count: usize,+ periodic: bool,+}++impl Arc {+ /// The arc's parameter at `angle`. On a rational quadratic span of half angle α with middle+ /// weight cos α, tan(θ / 2) runs linearly from −tan(α / 2) to tan(α / 2) with the span's+ /// own parameter (θ from the span's middle). A whole turn continues by whole parameters.+ pub(crate) fn param(&self, angle: f64) -> f64 {+ let total = self.step * self.count as f64;+ let turns = if self.periodic { ((angle - self.start) / total).floor() } else { 0.0 };+ let a = angle - turns * total;+ let i = (((a - self.start) / self.step).floor().max(0.0) as usize).min(self.count.saturating_sub(1));+ let half = 0.5 * self.step;+ let local = a - (self.start + (i as f64 + 0.5) * self.step);+ let s = 0.5 * ((0.5 * local).tan() / (0.5 * half).tan() + 1.0);+ (i as f64 + s) / self.count as f64 + turns+ }+}++/// `surface` in the evaluator's form, and how the record's parameters map onto it. `domain` is+/// the face's parameter box in the record's parameters, `[[u0, u1], [v0, v1]]`: a cylinder's+/// or cone's generator spans the face's heights.+pub(crate) fn eval_surface(surface: &brep::Surface, domain: [[f64; 2]; 2]) -> Result<(EvalSurface, Params)> {use std::f64::consts::{FRAC_PI_2, TAU};let invalid = |what: &str| KernelError::Invalid(format!("tessellation: {what}"));let axes = |f: &Frame| {@@ -45,27 +95,32 @@ pub(crate) fn eval_surface(surface: &brep::Surface, domain: [[f64; 2]; 2]) -> Re// Parameter −v: the generator at t is the surface at height −t.Ok(EvalCurve { degree: 1, knots: vec![-h1, -h1, -h0, -h0], ctrl: vec![[a.x, a.y, a.z, 1.0], [b.x, b.y, b.z, 1.0]] })};+ // A meridian from the record's least angle to its greatest, reversed (so the normal is the+ // record's natural one, outward).+ let meridian = |centre: Vec3, x: Vec3, z: Vec3, r: f64, angle: [f64; 2], o: Vec3| -> Result<(EvalSurface, Params)> {+ let (curve, spans) = arc(centre, x, z, r, angle);+ Ok((revolved(curve.reversed(), o, z)?, Params::Meridian(spans)))+ };match surface {brep::Surface::Cylinder { frame, radius } => {let (o, x, z) = axes(frame);- revolved(generator(&|h| o + x * *radius + z * h)?, o, z)+ Ok((revolved(generator(&|h| o + x * *radius + z * h)?, o, z)?, Params::Height))}brep::Surface::Cone { frame, radius_at_origin, half_angle } => {let (o, x, z) = axes(frame);let slope = half_angle.tan();- revolved(generator(&|h| o + x * (radius_at_origin + h * slope) + z * h)?, o, z)+ Ok((revolved(generator(&|h| o + x * (radius_at_origin + h * slope) + z * h)?, o, z)?, Params::Height))}brep::Surface::Sphere { frame, radius } => {let (o, x, z) = axes(frame);- // The meridian from the north pole to the south pole.- revolved(arc(o, x, z, *radius, [-FRAC_PI_2, FRAC_PI_2]).reversed(), o, z)+ meridian(o, x, z, *radius, [-FRAC_PI_2, FRAC_PI_2], o)}brep::Surface::Torus { frame, major_radius, minor_radius } => {let (o, x, z) = axes(frame);- revolved(arc(o + x * *major_radius, x, z, *minor_radius, [0.0, TAU]).reversed(), o, z)+ meridian(o + x * *major_radius, x, z, *minor_radius, [0.0, TAU], o)}_ => match SurfaceEval::new(surface) {- Ok(SurfaceEval::Eval(e, _)) => Ok(e),+ Ok(SurfaceEval::Eval(e, _)) => Ok((e, Params::Same)),Ok(_) => Err(invalid("an analytic surface out of place")),Err(e) => Err(KernelError::Failed(format!("not supported yet: tessellation: {e}"))),},@@ -73,8 +128,8 @@ pub(crate) fn eval_surface(surface: &brep::Surface, domain: [[f64; 2]; 2]) -> Re}/// The circle `centre + r (cos t x + sin t y)` for t in `angle`, as an exact rational quadratic-/// spline over [0, 1], one span per quarter turn or less.-fn arc(centre: Vec3, x: Vec3, y: Vec3, r: f64, angle: [f64; 2]) -> EvalCurve {+/// spline over [0, 1], one span per quarter turn or less, and its spans.+fn arc(centre: Vec3, x: Vec3, y: Vec3, r: f64, angle: [f64; 2]) -> (EvalCurve, Arc) {let span = angle[1] - angle[0];let count = (span / std::f64::consts::FRAC_PI_2).ceil().clamp(1.0, 4.0) as usize;let step = span / count as f64;@@ -93,7 +148,8 @@ fn arc(centre: Vec3, x: Vec3, y: Vec3, r: f64, angle: [f64; 2]) -> EvalCurve {ctrl.push(at(start + step, 1.0, 1.0));knots.extend(std::iter::repeat_n((i + 1) as f64 / count as f64, if i + 1 == count { 3 } else { 2 }));}- EvalCurve { degree: 2, knots, ctrl }+ let periodic = (span - std::f64::consts::TAU).abs() < 1e-12;+ (EvalCurve { degree: 2, knots, ctrl }, Arc { start: angle[0], step, count, periodic })}/// A curve on [0, 1] run the other way: t becomes 1 − t.@@ -112,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;}@@ -150,32 +203,36 @@ mod tests {kinds}- fn solid(b: crate::Body) -> Solid {- (*b.solid).clone()+ fn record(b: crate::Result<crate::Body>) -> 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::<usize>() > 0);+ assert!(assert_matches_the_record(&record(crate::transform_matrix(&cyl, mirror))).iter().sum::<usize>() > 0);}}diff --git a/crates/kernel/src/tess/tests.rs b/crates/kernel/src/tess/tests.rsindex 4a7dbdb1ec..977955cdcd 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 }), b.boundary_tol) {+ 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}@@ -94,7 +99,7 @@ fn a_box_is_a_regular_grid_on_every_face() {fn primitives_are_watertight_with_the_right_volume() {for tol in [0.05, 0.005] {let s = check("box", &ok(crate::box_solid(Vec3::ZERO, Vec3::new(10.0, 6.0, 4.0))), tol, 240.0);- assert_eq!(s, PlanStats { planar: 6, structured: 0, delaunay: 0, filled: 0 });+ assert_eq!(s, PlanStats { planar: 6, structured: 0, delaunay: 0 });let s = check("cylinder", &ok(crate::cylinder(Vec3::new(1.0, 2.0, 3.0), Vec3::new(0.0, 1.0, 1.0), 2.5, 7.0)), tol, PI * 2.5 * 2.5 * 7.0);assert_eq!(s.delaunay, 0, "cylinder walls are structured");let s = check("torus", &ok(crate::torus(Vec3::ZERO, 6.0, 1.5)), tol, 2.0 * PI * PI * 6.0 * 1.5 * 1.5);@@ -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=<dir> 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 }), body.boundary_tol) {+ 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("<svg xmlns='http://www.w3.org/2000/svg' width='1600' height='860' style='background:#fff'>");let label = |x: f64, text: String| format!("<text x='{x}' y='40' font-family='sans-serif' font-size='26'>{text}</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(@@ -297,159 +305,6 @@ fn trace_seeds_grade_from_one_end_to_the_other() {assert!(graded([0.0, 0.0], [0.5, 0.0], 1.0, 1.0, 10.0, [1.0, 1.0], &|_, _| true).is_empty());}-/// A surface that joins itself along a direction is periodic there: its parameters repeat, so-/// the evaluator takes a parameter outside the range back into it. A boundary that runs across-/// the join is unwrapped past the range's end and must still land on the surface.-#[test]-fn a_closed_spline_surface_is_periodic_and_evaluates_outside_its_range() {- use solidcraft_geom::tess::EvalSurface;- // A five-sided tube: the control net's last column repeats its first.- let corner = |k: usize| {- let a = std::f64::consts::TAU * (k % 5) as f64 / 5.0;- (10.0 * a.cos(), 10.0 * a.sin())- };- let mut ctrl = Vec::new();- for j in 0..2 {- for k in 0..=5 {- let (x, y) = corner(k);- ctrl.push([x, y, f64::from(j) * 8.0, 1.0]);- }- }- let s = EvalSurface::Nurbs {- degree: [1, 1],- knots: [vec![0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 5.0], vec![0.0, 0.0, 1.0, 1.0]],- count: [6, 2],- ctrl,- closed: [true, false],- };- let dom = uv::domain(&s);- assert_eq!(dom.period, [Some(5.0), None]);- assert_eq!(dom.range, [Some((0.0, 5.0)), Some((0.0, 1.0))]);- // A parameter a whole range below the start is the same point as one inside it.- for (a, b) in [(-0.5, 4.5), (-2.0, 3.0), (5.5, 0.5), (7.0, 2.0)] {- let (Some(out), Some(inside)) = (s.derivs(a, 0.25), s.derivs(b, 0.25)) else { panic!("evaluates") };- assert!(out.0.dist(inside.0) < 1e-12, "u = {a} and u = {b}: {:?} vs {:?}", out.0, inside.0);- }- // The same net left open is not periodic, and keeps truck's end-span extension outside it.- let EvalSurface::Nurbs { degree, knots, count, ctrl, .. } = s else { panic!("nurbs") };- let open = EvalSurface::Nurbs { degree, knots, count, ctrl, closed: [false, false] };- assert_eq!(uv::domain(&open).period, [None, None]);- let (Some(out), Some(inside)) = (open.derivs(-0.5, 0.25), open.derivs(4.5, 0.25)) else { panic!("evaluates") };- assert!(out.0.dist(inside.0) > 1.0, "an open surface does not wrap");-}--/// Each loop of a face is projected on its own, so on a periodic surface a hole can come out a-/// whole turn away from the boundary that holds it. Every loop is brought into the outer-/// boundary's period, where the hole cuts the face again.-#[test]-fn holes_are_brought_into_the_outer_boundarys_period() {- let tau = std::f64::consts::TAU;- let at = |u: f64, v: f64| seam::Loop { uv: vec![[u, v], [u + 0.2, v], [u + 0.2, v + 0.2], [u, v + 0.2]], pts: vec![Vec3::ZERO; 4] };- let dom = uv::Domain { range: [Some((0.0, 1.0)), Some((0.0, tau))], period: [None, Some(tau)] };- let mut loops = vec![at(0.0, -tau), at(0.4, 1.5), at(0.6, 1.5 - tau)];- align_periods(&mut loops, &dom);- // The outer loop stays where it is; both holes land beside it, a whole turn moved or not.- assert_eq!(loops[0].uv[0], [0.0, -tau]);- assert!((loops[1].uv[0][1] - (1.5 - tau)).abs() < 1e-12, "{:?}", loops[1].uv[0]);- assert!((loops[2].uv[0][1] - (1.5 - tau)).abs() < 1e-12, "{:?}", loops[2].uv[0]);- // Nothing moves along a direction that does not repeat.- assert_eq!(loops[1].uv[0][0], 0.4);-}--/// A boundary that steps out to a node and straight back encloses no area: the pair is dropped,-/// so what is left no longer runs along itself. The two sides need only agree within the given-/// distance — a STEP writer may draw them as two curves a hair apart.-#[test]-fn zero_width_slits_are_dropped_from_a_boundary() {- let p = |x: f64| Vec3::new(x, 0.0, 0.0);- // A square with a slit poked into it from the corner (2, 1): out to (2, 2) and back.- let slit = vec![- p(0.0),- p(1.0),- Vec3::new(2.0, 1.0, 0.0),- Vec3::new(2.0, 2.0, 0.0),- Vec3::new(2.0 + 1e-9, 1.0, 0.0),- p(3.0),- Vec3::new(3.0, 3.0, 0.0),- Vec3::new(0.0, 3.0, 0.0),- ];- let (out, known) = drop_slits(&[slit], &[Vec::new()], 1e-6);- assert_eq!(out.len(), 1);- assert_eq!(out[0], vec![p(0.0), p(1.0), Vec3::new(2.0, 1.0, 0.0), p(3.0), Vec3::new(3.0, 3.0, 0.0), Vec3::new(0.0, 3.0, 0.0)]);- assert_eq!(known[0].len(), out[0].len());- // Too far apart to be one slit: the boundary is left as it is.- let wide = vec![p(0.0), p(1.0), Vec3::new(2.0, 1.0, 0.0), Vec3::new(2.0, 2.0, 0.0), Vec3::new(2.5, 1.0, 0.0), p(3.0)];- let kept = drop_slits(std::slice::from_ref(&wide), &[Vec::new()], 1e-6).0;- assert_eq!(kept, vec![wide]);-}--/// A pointed end read back from STEP (a screw's tip, a cone): its cone face is written as the-/// rim, the seam up to the apex and the seam back, and a sector of one as its rim and two-/// sides meeting at the apex. Unwrapped to the nearest copy at every step, the jump at the-/// apex made such a loop seem to wind around the cone; the face could not be cut along a seam-/// it did not have, and the whole body lost its display plan.-#[test]-fn a_pointed_end_read_back_from_step_is_planned() {- use solidcraft_geom::{Loop2, Plane, Region2, Vec2};- let (r, tip, len) = (3.0, 4.0, 20.0);- let Some(axis) = Plane::new(Vec3::ZERO, Vec3::X, Vec3::Z) else { panic!("plane") };- let pts = [Vec2::new(0.0, 0.0), Vec2::new(r, tip), Vec2::new(r, len), Vec2::new(0.0, len)];- for turn in [360.0f64, 300.0, 270.0, 200.0] {- let region = Region2 { outer: Loop2::polygon(&pts), holes: vec![] };- let Some(body) = ok(crate::revolve(&axis, &[region], Vec2::ZERO, Vec2::Y, turn.to_radians())).pop() else { panic!("revolve") };- let imported = ok(crate::step_import(&ok(crate::step_export(&[&body], "pointed end"))));- let Some(copy) = imported.bodies.first() else { panic!("no body") };- let volume = std::f64::consts::PI * r * r * (len - tip + tip / 3.0) * turn / 360.0;- check(&format!("pointed end turned {turn} deg"), ©.body, 0.02, volume);- }-}--/// A point on a spline that turns many times (a thread's helical flank, as other CAD systems-/// write it to STEP, has hundreds of spans along the helix) is found from a hint a few turns-/// away: between 16 starts across the whole spline Newton settled on another turn, the point-/// was taken as off the surface and the body lost its display plan.-#[test]-fn a_point_on_a_long_helical_spline_projects() {- let (turns, per_turn, pitch) = (40usize, 24usize, 0.5);- let n = turns * per_turn + 1;- let at = |i: usize, r: f64| {- let t = std::f64::consts::TAU * i as f64 / per_turn as f64;- [r * t.cos(), r * t.sin(), pitch * t / std::f64::consts::TAU, 1.0]- };- let mut ctrl: Vec<[f64; 4]> = Vec::with_capacity(2 * n);- for i in 0..n {- ctrl.push(at(i, 0.7));- ctrl.push(at(i, 0.55));- }- let mut ku = vec![0.0; 3];- ku.extend((1..n - 2).map(|i| i as f64 / (n - 2) as f64));- ku.extend([1.0; 3]);- let s = EvalSurface::Nurbs { degree: [2, 1], knots: [ku, vec![0.0, 0.0, 1.0, 1.0]], count: [n, 2], ctrl, closed: [false, false] };- let dom = uv::domain(&s);- for (target, hint) in [([0.5, 1.0], [0.488, 1.0]), ([0.2, 0.0], [0.23, 1.0]), ([0.9, 0.5], [0.1, 0.5])] {- let Some((p, _, _)) = s.derivs(target[0], target[1]) else { panic!("evaluate") };- let Some(q) = uv::project(&s, &dom, p, Some(hint), 1e-4) else { panic!("{target:?} from {hint:?}: not found") };- let Some((back, _, _)) = s.derivs(q[0], q[1]) else { panic!("evaluate") };- assert!(back.dist(p) <= 1e-4, "{target:?} from {hint:?}: {q:?} is {} away", back.dist(p));- }-}--/// A sliver face as STEP files write one: two edges between the same two vertices, a rounding-/// apart, so its boundary runs out and back along itself. Its slits dropped, nothing is left to-/// bound a face (it is planned empty); a boundary enclosing anything keeps its loop.-#[test]-fn a_boundary_running_back_along_itself_is_all_slit() {- let out: Vec<Vec3> = (0..5).map(|i| Vec3::new(f64::from(i), 0.5 * f64::from(i), 0.0)).collect();- let mut sliver = out.clone();- sliver.extend(out.iter().rev().skip(1).take(3).map(|p| *p + Vec3::new(0.0, 1e-14, 0.0)));- let (left, _) = drop_slits(&[sliver], &[Vec::new()], 1e-9);- assert!(left.iter().all(|l| l.len() < 3), "{left:?}");- let mut lens = out.clone();- lens.extend(out.iter().rev().skip(1).take(3).map(|p| *p + Vec3::new(0.0, 1e-3, 0.0)));- let (kept, _) = drop_slits(std::slice::from_ref(&lens), &[Vec::new()], 1e-9);- assert_eq!(kept, vec![lens]);-}-/// Distance of `p` from the analytic surface `fs`.fn off_surface(fs: &crate::FaceSurface, p: Vec3) -> f64 {use crate::FaceSurface as F;@@ -652,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