Naming: a face swept from one of many sketch curves is named without measuring every curve (#8643)
Naming a face swept from a sketch flattened every curve of the sketch to a polyline and measured its distance to each, for every face. On a sketch with thousands of curves (a sheet with many cut-outs) that grew with curves times faces. The curves are now flattened once per feature, each with its box, and a curve whose box is farther than the best distance so far is not measured. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+105
-35
@@ -17,7 +17,7 @@
|
||||
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
use solidcraft_geom::{Aabb3, Mesh, Seg2, Vec2, Vec3};
|
||||
use solidcraft_geom::{Aabb3, Mesh, Vec2, Vec3};
|
||||
|
||||
use crate::history_names::Names;
|
||||
use crate::lineage::Lineage;
|
||||
@@ -539,6 +539,7 @@ fn geometric_from(b: &ModelBody, o: &NamingOrigin, nf: usize, names_of: fn(&Mode
|
||||
}
|
||||
}
|
||||
let empty = Vec::new();
|
||||
let curves = std::cell::OnceCell::new();
|
||||
let names: Vec<String> = names
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
@@ -551,7 +552,7 @@ fn geometric_from(b: &ModelBody, o: &NamingOrigin, nf: usize, names_of: fn(&Mode
|
||||
{
|
||||
return format!("F{fid}:inner:{}", strip_piece(&n));
|
||||
}
|
||||
role_name(&o.feature, &o.before, ps, nn, fi, fid)
|
||||
role_name(&o.feature, &o.before, ps, nn, fi, fid, &curves)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
@@ -742,7 +743,7 @@ pub(crate) fn face_centres(b: &ModelBody) -> Vec<Vec3> {
|
||||
}
|
||||
|
||||
/// The name of a face new to `f`.
|
||||
fn role_name(f: &Feature, before: &ModelState, ps: &[Vec3], ns: &[Vec3], fi: usize, fid: u64) -> String {
|
||||
fn role_name(f: &Feature, before: &ModelState, ps: &[Vec3], ns: &[Vec3], fi: usize, fid: u64, curves: &std::cell::OnceCell<Curves>) -> String {
|
||||
let c = ps.iter().fold(Vec3::ZERO, |a, p| a + *p) * (1.0 / ps.len().max(1) as f64);
|
||||
match &f.kind {
|
||||
FeatureKind::Extrude { sketch, .. } => {
|
||||
@@ -755,7 +756,12 @@ fn role_name(f: &Feature, before: &ModelState, ps: &[Vec3], ns: &[Vec3], fi: usi
|
||||
return format!("F{fid}:{}", crate::lineage::extrude_cap_role((c - ss.plane.origin).dot(n)));
|
||||
}
|
||||
let q = ss.plane.to_local(c);
|
||||
nearest_curve(ss, Vec2::new(q.x, q.y)).map(|id| format!("F{fid}:side:{id}")).unwrap_or_else(|| format!("F{fid}:face:{fi}"))
|
||||
curves
|
||||
.get_or_init(|| Curves::of(ss))
|
||||
.nearest(Vec2::new(q.x, q.y))
|
||||
.map(|(_, id)| id)
|
||||
.map(|id| format!("F{fid}:side:{id}"))
|
||||
.unwrap_or_else(|| format!("F{fid}:face:{fi}"))
|
||||
}
|
||||
FeatureKind::Revolve { sketch, axis, .. } => {
|
||||
let Some(ss) = before.sketch(sketch.0) else { return format!("F{fid}:face:{fi}") };
|
||||
@@ -779,7 +785,7 @@ fn role_name(f: &Feature, before: &ModelState, ps: &[Vec3], ns: &[Vec3], fi: usi
|
||||
.filter_map(|u| {
|
||||
let w = ss.plane.to_local(o + d * t + *u * r);
|
||||
let q2 = Vec2::new(w.x, w.y);
|
||||
nearest_curve_dist(ss, q2)
|
||||
curves.get_or_init(|| Curves::of(ss)).nearest(q2)
|
||||
})
|
||||
.min_by(|a, b| a.0.total_cmp(&b.0));
|
||||
best.map(|(_, id)| format!("F{fid}:side:{id}")).unwrap_or_else(|| format!("F{fid}:face:{fi}"))
|
||||
@@ -803,7 +809,12 @@ fn role_name(f: &Feature, before: &ModelState, ps: &[Vec3], ns: &[Vec3], fi: usi
|
||||
// A side: the profile curve under its sample nearest the start.
|
||||
let Some(p0) = ps.iter().min_by(|a, b| h(a).abs().total_cmp(&h(b).abs())) else { return format!("F{fid}:face:{fi}") };
|
||||
let q = ss.plane.to_local(*p0);
|
||||
nearest_curve(ss, Vec2::new(q.x, q.y)).map(|id| format!("F{fid}:side:{id}")).unwrap_or_else(|| format!("F{fid}:face:{fi}"))
|
||||
curves
|
||||
.get_or_init(|| Curves::of(ss))
|
||||
.nearest(Vec2::new(q.x, q.y))
|
||||
.map(|(_, id)| id)
|
||||
.map(|id| format!("F{fid}:side:{id}"))
|
||||
.unwrap_or_else(|| format!("F{fid}:face:{fi}"))
|
||||
}
|
||||
FeatureKind::Box { .. } => {
|
||||
let n = ns.first().copied().unwrap_or(Vec3::Z);
|
||||
@@ -842,42 +853,51 @@ fn role_name(f: &Feature, before: &ModelState, ps: &[Vec3], ns: &[Vec3], fi: usi
|
||||
}
|
||||
}
|
||||
|
||||
/// The sketch curve (id) nearest a point in the sketch plane, and how near.
|
||||
fn nearest_curve_dist(ss: &crate::SolvedSketch, q: Vec2) -> Option<(f64, String)> {
|
||||
let mut best: Option<(f64, String)> = None;
|
||||
for (ci, c) in ss.sketch.curves.iter().enumerate() {
|
||||
if c.construction {
|
||||
continue;
|
||||
}
|
||||
for s in ss.sketch.segs(ci) {
|
||||
let d = seg_dist(&s, q);
|
||||
if best.as_ref().is_none_or(|(bd, _)| d < *bd) {
|
||||
best = Some((d, c.id.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
best
|
||||
/// A sketch's (non-construction) curves as the polylines a face's name is looked up on, each with
|
||||
/// its box: a face swept from one of many curves (a sheet of holes) is named without measuring
|
||||
/// its distance to every curve.
|
||||
struct Curves {
|
||||
segs: Vec<(String, Vec<Vec2>, (Vec2, Vec2))>,
|
||||
}
|
||||
|
||||
/// The sketch curve (id) nearest a point in the sketch plane.
|
||||
fn nearest_curve(ss: &crate::SolvedSketch, q: Vec2) -> Option<String> {
|
||||
let mut best: Option<(f64, String)> = None;
|
||||
for (ci, c) in ss.sketch.curves.iter().enumerate() {
|
||||
if c.construction {
|
||||
continue;
|
||||
}
|
||||
for s in ss.sketch.segs(ci) {
|
||||
let d = seg_dist(&s, q);
|
||||
if best.as_ref().is_none_or(|(bd, _)| d < *bd) {
|
||||
best = Some((d, c.id.clone()));
|
||||
impl Curves {
|
||||
fn of(ss: &crate::SolvedSketch) -> Self {
|
||||
let mut segs = Vec::new();
|
||||
for (ci, c) in ss.sketch.curves.iter().enumerate() {
|
||||
if c.construction {
|
||||
continue;
|
||||
}
|
||||
for s in ss.sketch.segs(ci) {
|
||||
let pts = s.polyline(1e-3);
|
||||
let Some(&first) = pts.first() else { continue };
|
||||
let b =
|
||||
pts.iter().fold((first, first), |(lo, hi), p| (Vec2::new(lo.x.min(p.x), lo.y.min(p.y)), Vec2::new(hi.x.max(p.x), hi.y.max(p.y))));
|
||||
segs.push((c.id.clone(), pts, b));
|
||||
}
|
||||
}
|
||||
Self { segs }
|
||||
}
|
||||
|
||||
/// The curve (id) nearest `q`, and how near: the first of the nearest in the sketch's order.
|
||||
/// A curve whose box is farther than the nearest so far (by more than rounding) can't be nearer.
|
||||
fn nearest(&self, q: Vec2) -> Option<(f64, String)> {
|
||||
let mut best: Option<(f64, &str)> = None;
|
||||
for (id, pts, b) in &self.segs {
|
||||
let (lo, hi) = b;
|
||||
let out = Vec2::new((lo.x - q.x).max(q.x - hi.x).max(0.0), (lo.y - q.y).max(q.y - hi.y).max(0.0));
|
||||
if best.is_some_and(|(bd, _)| out.len() > bd + 1e-9 * (1.0 + q.len())) {
|
||||
continue;
|
||||
}
|
||||
let d = polyline_dist(pts, q);
|
||||
if best.is_none_or(|(bd, _)| d < bd) {
|
||||
best = Some((d, id));
|
||||
}
|
||||
}
|
||||
best.map(|(d, id)| (d, id.to_string()))
|
||||
}
|
||||
best.map(|(_, id)| id)
|
||||
}
|
||||
|
||||
fn seg_dist(s: &Seg2, q: Vec2) -> f64 {
|
||||
let pts = s.polyline(1e-3);
|
||||
fn polyline_dist(pts: &[Vec2], q: Vec2) -> f64 {
|
||||
pts.windows(2)
|
||||
.map(|w| {
|
||||
let (a, b) = (w[0], w[1]);
|
||||
@@ -1318,4 +1338,54 @@ mod tests {
|
||||
second.join().unwrap();
|
||||
assert!(weak.iter().all(|node| node.upgrade().is_none()));
|
||||
}
|
||||
|
||||
/// A face is named after the nearest sketch curve; skipping curves by their boxes finds the
|
||||
/// same curve as measuring every one, the first of equally near ones included.
|
||||
#[test]
|
||||
fn the_curve_index_finds_the_curve_a_full_scan_finds() {
|
||||
use solidcraft_sketch::{Sketch, find_profiles, solve};
|
||||
let mut sketch = Sketch::new();
|
||||
for i in 0..6 {
|
||||
for j in 0..4 {
|
||||
sketch.add_circle(Vec2::new(10. * f64::from(i), 10. * f64::from(j)), 2.5, None, Some(&format!("c{i}_{j}"))).unwrap();
|
||||
}
|
||||
}
|
||||
sketch.add_line(Vec2::new(-5., -5.), Vec2::new(55., -5.), None, None, Some("bottom")).unwrap();
|
||||
sketch.add_arc(Vec2::new(25., 15.), Vec2::new(45., 15.), Vec2::new(5., 15.), [None; 3], Some("arc")).unwrap();
|
||||
let hidden = sketch.add_line(Vec2::new(-5., 12.), Vec2::new(60., 12.), None, None, Some("guide")).unwrap();
|
||||
sketch.curves[hidden].construction = true;
|
||||
let report = solve(&mut sketch);
|
||||
let profiles = find_profiles(&sketch);
|
||||
let ss = crate::SolvedSketch {
|
||||
feature: 1,
|
||||
name: "S".into(),
|
||||
plane: solidcraft_geom::Plane::XY,
|
||||
sketch,
|
||||
report,
|
||||
profiles,
|
||||
ends: Default::default(),
|
||||
};
|
||||
let full_scan = |q: Vec2| {
|
||||
let mut best: Option<(f64, String)> = None;
|
||||
for (ci, c) in ss.sketch.curves.iter().enumerate().filter(|(_, c)| !c.construction) {
|
||||
for s in ss.sketch.segs(ci) {
|
||||
let d = polyline_dist(&s.polyline(1e-3), q);
|
||||
if best.as_ref().is_none_or(|(bd, _)| d < *bd) {
|
||||
best = Some((d, c.id.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
best
|
||||
};
|
||||
let curves = Curves::of(&ss);
|
||||
// A grid of points over and around the sketch; x = 5, 15, ... lie midway between circles.
|
||||
for x in -16..=124 {
|
||||
for y in -16..=84 {
|
||||
let q = Vec2::new(f64::from(x) * 0.5, f64::from(y) * 0.5);
|
||||
assert_eq!(curves.nearest(q), full_scan(q), "at {q:?}");
|
||||
}
|
||||
}
|
||||
assert_eq!(curves.nearest(Vec2::new(5., 0.)).map(|(_, id)| id).as_deref(), Some("c0_0"), "a tie keeps the first curve");
|
||||
assert!(curves.segs.iter().all(|(id, ..)| id != "guide"), "construction curves name nothing");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user