Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Hovering a long helical edge no longer freezes (#3745) (#3818)

bherbruck7e05c8b23cd1
diff --git a/crates/geom/src/mesh.rs b/crates/geom/src/mesh.rs
index eea0cd305b..dc166714cf 100644
--- a/crates/geom/src/mesh.rs
+++ b/crates/geom/src/mesh.rs
@@ -504,11 +504,42 @@ impl Mesh {
if self.smooth_seam(e, cos) {
return chain;
}
+ // The edges by the grid cell of each end, so each step looks only at the edges that end
+ // near it (#3745: scanning every edge for every edge of a long helical chain held a hover
+ // for seconds). Cells are twice the join tolerance: two ends within it share a cell or
+ // touch neighbouring ones.
+ let cell = tol * 2.0;
+ let key = |p: Vec3| [(p.x / cell).floor() as i64, (p.y / cell).floor() as i64, (p.z / cell).floor() as i64];
+ let mut at_end: std::collections::HashMap<[i64; 3], Vec<usize>> = std::collections::HashMap::new();
+ for (i, p) in self.edges.iter().enumerate() {
+ for q in [p.first(), p.last()].into_iter().flatten().filter(|q| q.is_finite()) {
+ let slot = at_end.entry(key(*q)).or_default();
+ if slot.last() != Some(&i) {
+ slot.push(i);
+ }
+ }
+ }
+ let mut in_chain: std::collections::HashSet<usize> = chain.iter().copied().collect();
let mut todo = vec![e];
while let Some(cur) = todo.pop() {
let Some(ce) = ends(cur) else { continue };
- for (i, _) in self.edges.iter().enumerate() {
- if chain.contains(&i) || !self.edge_pickable(i) || self.smooth_seam(i, cos) {
+ // The edges with an end near one of `cur`'s, in edge order (as a scan of every edge
+ // meets them).
+ let mut near: Vec<usize> = Vec::new();
+ for (p, _) in &ce {
+ let [x, y, z] = key(*p);
+ for dx in -1..=1 {
+ for dy in -1..=1 {
+ for dz in -1..=1 {
+ near.extend(at_end.get(&[x + dx, y + dy, z + dz]).into_iter().flatten());
+ }
+ }
+ }
+ }
+ near.sort_unstable();
+ near.dedup();
+ for i in near {
+ if in_chain.contains(&i) || !self.edge_pickable(i) || self.smooth_seam(i, cos) {
continue;
}
let Some(ne) = ends(i) else { continue };
@@ -516,6 +547,7 @@ impl Mesh {
let smooth = ce.iter().any(|(p, d)| ne.iter().any(|(q, dn)| p.dist(*q) < tol && d.dot(-*dn) > cos));
if smooth && chain.len() < 10_000 {
chain.push(i);
+ in_chain.insert(i);
todo.push(i);
}
}
diff --git a/crates/geom/src/tests.rs b/crates/geom/src/tests.rs
index a50384a862..1aa5948af8 100644
--- a/crates/geom/src/tests.rs
+++ b/crates/geom/src/tests.rs
@@ -164,6 +164,32 @@ fn tangent_chains_follow_smooth_edges() {
assert_eq!(seam.tangent_chain(0, 5f64.to_radians()), vec![0]);
}
+/// #3745: the tangent chain of a long helix (a thread's crest, in many edge pieces) is found
+/// without scanning every edge for every edge of it: hovering a threaded part's edge looked for
+/// its rim this way, and took seconds inside the frame. Every piece joins, in edge order, and
+/// a mesh of 3 000 pieces takes well under a frame.
+#[test]
+fn a_long_helical_tangent_chain_is_found_quickly() {
+ let pieces = 3_000usize;
+ let at = |t: f64| Vec3::new(4.0 * t.cos(), 4.0 * t.sin(), 0.2 * t);
+ let step = 0.05;
+ let mut m = Mesh::default();
+ for k in 0..pieces {
+ let t0 = k as f64 * step * 4.0;
+ m.edges.push((0..=4).map(|j| at(t0 + j as f64 * step)).collect());
+ m.edge_faces.push(vec![0]);
+ }
+ // The mesh's points (its size sets the join tolerance).
+ m.positions = m.edges.iter().flatten().copied().collect();
+ let began = std::time::Instant::now();
+ let chain = m.tangent_chain(pieces / 2, 2f64.to_radians());
+ let ms = began.elapsed().as_secs_f64() * 1000.0;
+ let mut sorted = chain.clone();
+ sorted.sort_unstable();
+ assert_eq!(sorted, (0..pieces).collect::<Vec<_>>(), "every piece of the helix is in the chain");
+ assert!(ms < 200.0, "{ms:.1} ms for {pieces} pieces");
+}
+
#[test]
fn circular_rims_group_only_closed_cocircular_edges() {
let arc = |start: f64, sweep: f64, yscale: f64| -> Vec<Vec3> {