SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Wave 213/509: kernel: a tangent chain stops at a smooth edge (and 3 more)
jyviko0de308b21ded
diff --git a/Cargo.lock b/Cargo.lockindex 01aa3c652b..2d7acdb880 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -4253,6 +4253,7 @@ dependencies = ["serde_json","solidcraft-engine","solidcraft-gpu",+ "solidcraft-kernel","solidcraft-mcp",]diff --git a/apps/solidcraft-cli/Cargo.toml b/apps/solidcraft-cli/Cargo.tomlindex e5da188fff..e0c2523af4 100644--- a/apps/solidcraft-cli/Cargo.toml+++ b/apps/solidcraft-cli/Cargo.toml@@ -38,3 +38,5 @@ serde_json = { workspace = true }[dev-dependencies]# Snapshot tests decode the pictures to compare them with a tolerance.image = { workspace = true }+# Tests measure meshes at their own tolerances.+solidcraft-kernel = { workspace = true }diff --git a/crates/kernel/src/blend.rs b/crates/kernel/src/blend.rsindex 5a0ba85e17..8b70cfbf36 100644--- a/crates/kernel/src/blend.rs+++ b/crates/kernel/src/blend.rs@@ -88,9 +88,8 @@ impl Faces {}/// The outward unit normal of face `f` at `p` (a point within `accept` of its surface).- #[cfg(test)]pub(crate) fn normal_at(&self, f: usize, p: Vec3, accept: f64) -> Option<Vec3> {- let s = SurfaceEval::new(&self.rec.faces.get(f)?.surface).ok()?;+ let s = SurfaceEval::new(self.rec.faces.get(f)?.parameters()).ok()?;let uv = s.project(p, None, accept)?;let (_, su, sv) = s.derivs(uv[0], uv[1])?;Some(su.cross(sv).normalized()? * self.side(f)?)@@ -392,7 +391,8 @@ pub fn chamfer_sides(body: &Body, edges: &[Vec3], d1: f64, side: ChamferSide, fl/// The picked edges' tangent chains, for a blend that is to run along them: `picks` (each the/// nearest edge of `body` to it), then a point on every other edge that continues one of them/// tangentially (within 0.5° at a shared vertex), until no more do. An edge only one face uses-/// ends a chain. Picks that find no edge are refused.+/// ends a chain, and so does a smooth edge (its two faces meet tangentially, within 0.5°): it has+/// no corner to round. Picks that find no edge are refused.pub fn tangent_chain(body: &Body, picks: &[Vec3]) -> Result<Vec<Vec3>> {/// Edges meeting at an angle below this continue one another.const TANGENT_DEGREES: f64 = 0.5;@@ -421,6 +421,16 @@ pub fn tangent_chain(body: &Body, picks: &[Vec3]) -> Result<Vec<Vec3>> {if a == v { rec.tangent(e, false) } else { rec.tangent(e, true).map(|t| t * -1.0) }};let cos = TANGENT_DEGREES.to_radians().cos();+ let accept = rec.rec.tolerance.max(1e-7) * 10.0;+ // Whether edge `e`'s two faces meet tangentially at its start.+ let smooth = |e: usize| -> bool {+ let faces: Vec<u32> = rec.faces_of(e).into_iter().collect();+ let (Some(&a), Some(&b), Some((p, _))) = (faces.first(), faces.get(1), rec.ends(e)) else { return false };+ match (rec.normal_at(a as usize, p, accept), rec.normal_at(b as usize, p, accept)) {+ (Some(na), Some(nb)) => faces.len() == 2 && na.dot(nb) > cos,+ _ => false,+ }+ };let mut k = 0;while k < chain.len() {let Some(&e) = chain.get(k) else { break };@@ -429,7 +439,7 @@ pub fn tangent_chain(body: &Body, picks: &[Vec3]) -> Result<Vec<Vec3>> {for v in edge.vertices {let Some(d) = leaving(e, v) else { continue };for (f, other) in rec.rec.edges.iter().enumerate() {- if chain.contains(&f) || other.uses.len() < 2 || !other.vertices.contains(&v) {+ if chain.contains(&f) || other.uses.len() < 2 || !other.vertices.contains(&v) || smooth(f) {continue;}if leaving(f, v).is_some_and(|g| d.dot(g) < -cos) {diff --git a/crates/kernel/src/prism.rs b/crates/kernel/src/prism.rsindex a22bce98fa..4deb7ab5c0 100644--- a/crates/kernel/src/prism.rs+++ b/crates/kernel/src/prism.rs@@ -4,8 +4,7 @@//! result's boundaries are the operands' curves, cut at their crossings and never refitted.//! Also the test of whether a record surface is a wall swept along a direction.-use solidcraft_geom::brep::Surface;-use solidcraft_geom::{Plane, Region2, Seg2, Vec2, Vec3};+use solidcraft_geom::{Plane, Region2, Seg2, Vec2};use crate::body::Body;use crate::{BoolOp, KernelError, Result};@@ -111,6 +110,8 @@ struct Regions {#[cfg(feature = "smlib")]mod sheets {+ use solidcraft_geom::Vec3;+ use solidcraft_geom::brep::Surface;use solidcraft_geom::Loop2;use solidcraft_geom::brep::Loop;use solidcraft_smlib::{BooleanOp, Brep};diff --git a/crates/kernel/src/topo.rs b/crates/kernel/src/topo.rsindex e56f604f75..a9967a1502 100644--- a/crates/kernel/src/topo.rs+++ b/crates/kernel/src/topo.rs@@ -122,8 +122,6 @@ pub fn merged_topology(b: &Body, mesh: &Mesh) -> Result<TopoCounts> {let mut weld_e: HashMap<(usize, usize, (i64, i64, i64)), usize> = HashMap::new();let mut welded_faces: Vec<Vec<usize>> = Vec::new();let mut welded_ends: Vec<(usize, usize)> = Vec::new();- // How many face uses each welded edge has (a seam: two, on one face).- let mut welded_uses: Vec<usize> = Vec::new();// A record edge standing for each welded edge (for its points).let mut rep: Vec<u32> = Vec::new();for (ei, (e, between)) in record.edges.iter().zip(edge_faces(record)).enumerate() {@@ -135,12 +133,8 @@ pub fn merged_topology(b: &Body, mesh: &Mesh) -> Result<TopoCounts> {if we == welded_faces.len() {welded_faces.push(Vec::new());welded_ends.push((a, z));- welded_uses.push(0);rep.push(ei as u32);}- if let Some(n) = welded_uses.get_mut(we) {- *n += e.uses.len();- }if let Some(fs) = welded_faces.get_mut(we) {for f in between {if !fs.contains(&f) {@@ -212,12 +206,8 @@ pub fn merged_topology(b: &Body, mesh: &Mesh) -> Result<TopoCounts> {}continue;}- // An edge between two pieces of one merged face, or a seam (one face on both its sides),- // is no edge of the merged topology; one face on one side only is an open edge.- let seam = welded_uses.get(we).is_some_and(|n| *n >= 2);let pair = match gs[..] {[a, b] if a == b => continue,- [_] if seam => continue,[a, b] => (a.min(b), a.max(b)),[a] => (a, a),_ => (usize::MAX, usize::MAX),diff --git a/tests/kernel-cases/tessellation/818-blind-hole-splits-vertical-edges/expected.json b/tests/kernel-cases/tessellation/818-blind-hole-splits-vertical-edges/expected.jsonindex 899094f85f..04c464f114 100644--- a/tests/kernel-cases/tessellation/818-blind-hole-splits-vertical-edges/expected.json+++ b/tests/kernel-cases/tessellation/818-blind-hole-splits-vertical-edges/expected.json@@ -4,11 +4,11 @@],"what": "a blind hole cut into a ring whose six concave corners are filleted: the cut used to split every vertical edge (the outer seam and all twelve fillet tangent edges) at the hole's depth, and the fillet corners drew black","source": "analytic",- "method": "Ring: circle d=55 less the hexagon inscribed in r=25 (vertex at (0,25)), 10 mm tall. Hexagon area 3 sqrt(3)/2 25^2 = 1623.798. Each of its six concave corners (interior angle 120 deg) filled by a 4 mm round gains t r - r^2 (pi-120deg)/2 = 0.8600 (tangent length t = r/tan 60 = 2.3094). Plan area A_p = pi 27.5^2 - hexagon + 6 gain = 757.192. The hole d=3, 3 deep at (24,0) lies between the hexagon's flat (x=21.65) and the rim: V = 10 A_p - pi 1.5^2 3 = 7550.714. Area = top + bottom (2 A_p, the hole's floor replaces what its opening removes from the top) + outer wall 2 pi 27.5 10 + inner wall 10 (6 (25-2t) + 6 r (pi - 120deg)) + hole wall 2 pi 1.5 3 = 4744.733. Faces: 8 cylinders (outer, 6 rounds, hole) and 9 planes (top, bottom, 6 hexagon flats, hole floor). Edges, counting each whole curve once and no seams: top 1+12+1, bottom 1+12, vertical 12 tangent edges, hole floor 1 = 40; vertices 28.",+ "method": "Ring: circle d=55 less the hexagon inscribed in r=25 (vertex at (0,25)), 10 mm tall. Hexagon area 3 sqrt(3)/2 25^2 = 1623.798. Each of its six concave corners (interior angle 120 deg) filled by a 4 mm round gains t r - r^2 (pi-120deg)/2 = 0.8600 (tangent length t = r/tan 60 = 2.3094). Plan area A_p = pi 27.5^2 - hexagon + 6 gain = 757.192. The hole d=3, 3 deep at (24,0) lies between the hexagon's flat (x=21.65) and the rim: V = 10 A_p - pi 1.5^2 3 = 7550.714. Area = top + bottom (2 A_p, the hole's floor replaces what its opening removes from the top) + outer wall 2 pi 27.5 10 + inner wall 10 (6 (25-2t) + 6 r (pi - 120deg)) + hole wall 2 pi 1.5 3 = 4744.733. Faces: 8 cylinders (outer, 6 rounds, hole) and 9 planes (top, bottom, 6 hexagon flats, hole floor). Edges, counting each whole curve once and each cylinder's seam (a whole circle is one closed edge): top 1+12+1, bottom 1+12, vertical 12 tangent edges, hole floor 1, seams of the outer wall and the hole wall 2 = 42; vertices 28.","body_count": 1,"volume_mm3": 7550.714,"area_mm2": 4744.733,"faces": 17,- "edges": 40,+ "edges": 42,"vertices": 28-}\ No newline at end of file+}