diff --git a/crates/kernel/src/tess/mod.rs b/crates/kernel/src/tess/mod.rs index a912b3097..b00504c70 100644 --- a/crates/kernel/src/tess/mod.rs +++ b/crates/kernel/src/tess/mod.rs @@ -288,6 +288,12 @@ fn boundary( let mut out = Boundary { loops: Vec::with_capacity(face.loops.len()), segments: Vec::with_capacity(face.loops.len()), reach }; // Each vertex's parameters, from the first edge use of the face to start there. let mut at_vertex: std::collections::HashMap = std::collections::HashMap::new(); + // The vertex pairs an edge of the face joins. + let joined: std::collections::HashSet<(u32, u32)> = face_edges(face) + .filter_map(|oe| b.edges.get(oe.edge as usize)) + .map(|e| (e.vertices[0].min(e.vertices[1]), e.vertices[0].max(e.vertices[1]))) + .collect(); + let vertex_point = |v: u32| b.vertices.get(v as usize).map(|x| (Vec3::new(x.point[0], x.point[1], x.point[2]), x.tolerance.max(0.0))); for l in &face.loops { let Loop::Edges { edges } = l else { continue }; let (mut pts, mut uvs): (Vec, Vec<[f64; 2]>) = (Vec::new(), Vec::new()); @@ -341,6 +347,24 @@ fn boundary( } } None => { + // Another vertex of the face within the tolerance of both (and of the + // chord tolerance), and not joined to this one by an edge (which makes + // them two points however near: a step that small), is the same point: + // the record's corners a hair apart (a planar boolean's, where a cut-out's + // corner touches another loop's corner) are one point of the face, so its + // loops touch there instead of crossing. + let same = vertex_point(v).and_then(|(p, tv)| { + at_vertex.iter().filter(|(w, _)| **w != v && !joined.contains(&(v.min(**w), v.max(**w)))).find_map(|(w, wq)| { + let (pw, tw) = vertex_point(*w)?; + (p.dist(pw) <= tv.max(tw).min(rules.tol)).then_some(*wq) + }) + }); + if let Some(mut w) = same.filter(|w| !(0..2).any(|d| pole_along(*w, d) || pole_along(*q, d))) { + for d in 0..2 { + w[d] = uv::unwrap(w[d], q[d], dom.period[d]); + } + *q = w; + } at_vertex.insert(v, *q); } } diff --git a/crates/kernel/src/thread_display_tests.rs b/crates/kernel/src/thread_display_tests.rs index 63f5f3cf8..f8107f0b0 100644 --- a/crates/kernel/src/thread_display_tests.rs +++ b/crates/kernel/src/thread_display_tests.rs @@ -144,12 +144,14 @@ fn thread_display_mesh_stays_on_its_surfaces() { let [a, b2, c] = t.map(|i| mesh.positions[i as usize]); let (ra, rb, rcn) = (r(a), r(b2), r(c)); let centre = r((a + b2 + c) * (1.0 / 3.0)); + // An end disc is flat: its triangles are its boundary's, and one with its three + // corners on the rim (at r = rc) lies on the disc, not on a cylinder. + let end_disc = [a, b2, c].iter().all(|v| v.z.abs() < 1e-9) || [a, b2, c].iter().all(|v| (v.z - (turns + 2.0) * p).abs() < 1e-9); for want in [rc, rm] { - if [ra, rb, rcn].iter().all(|x| (x - want).abs() < 1e-4) { + if !end_disc && [ra, rb, rcn].iter().all(|x| (x - want).abs() < 1e-4) { assert!(centre > want - 2.0 * tol, "{name}: triangle {ti} on the r = {want} cylinder cuts in to {centre}"); } } - let end_disc = [a, b2, c].iter().all(|v| v.z.abs() < 1e-9) || [a, b2, c].iter().all(|v| (v.z - (turns + 2.0) * p).abs() < 1e-9); assert!(end_disc || (centre > rc - 2.0 * tol && centre < rm + 1e-6), "{name}: triangle {ti} at radius {centre}, outside {rc}…{rm}"); } // A mesh within the tolerance of the surfaces is within area × tolerance of the volume.