Wave 283/509: STEP import: an edge through a face's pole is split there (and 3 more)

4 commits, squashed; the originals stay reachable from tag archive/merge-main-develop.

- 0694fd4e8 jyviko: STEP import: an edge through a face's pole is split there
- 1d70ac267 jyviko: tess: a face whose boundary still crosses itself after untangling is filled leniently, not the whole body refused
- 77bd85cab jyviko: cli: snapshot --quality coarse|normal|fine|ultra meshes the bodies at that display quality
- 1404bc1f5 jyviko: STEP import: a spline face's edges are measured on their pcurves only, not by nearest point first

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
This commit is contained in:
jyviko
2026-10-27 20:45:43 -04:00
parent ffb0e6650b
commit fceb07b797
7 changed files with 206 additions and 23 deletions
+57
View File
@@ -2039,3 +2039,60 @@ fn spline_edges_off_their_plane_by_less_than_the_file_states_get_their_closed_fo
let m = measure(&imp.bodies[0].body).unwrap();
assert!(rel(m.area, 900.0) < 1e-6, "{}", m.area);
}
/// A spherical triangle (radius 0.5) whose edge runs from the equator over the sphere's pole to
/// a vertex 1.8° past it, as a corner ball meets its rounds (Poppy Humanoid writes one): on the
/// sphere's parameters the pole is a line where that edge's pcurve would jump half a turn. The
/// edge is split at the pole, and the face reads with its area by the spherical excess.
#[test]
fn an_edge_over_a_spheres_pole_is_split_there() {
let r = 0.5f64;
let over = (91.8f64).to_radians();
let a = [r, 0.0, 0.0];
let b = [0.0, r, 0.0];
let p = [r * over.cos(), 0.0, r * over.sin()];
let f = |x: f64| format!("{x:.17E}");
let pt = |id: u64, q: [f64; 3]| format!("#{id}=CARTESIAN_POINT('',({},{},{}));\n", f(q[0]), f(q[1]), f(q[2]));
let dir = |id: u64, q: [f64; 3]| format!("#{id}=DIRECTION('',({},{},{}));\n", f(q[0]), f(q[1]), f(q[2]));
let mut t = String::from(HEAD);
t += &pt(1, [0.0; 3]);
t += &dir(2, [0.0, 0.0, 1.0]);
t += &dir(3, [1.0, 0.0, 0.0]);
t += "#4=AXIS2_PLACEMENT_3D('',#1,#2,#3);\n#5=SPHERICAL_SURFACE('',#4,0.5);\n";
t += &pt(10, a);
t += &pt(11, b);
t += &pt(12, p);
t += "#13=VERTEX_POINT('',#10);\n#14=VERTEX_POINT('',#11);\n#15=VERTEX_POINT('',#12);\n";
// A to P in the x–z plane over the pole (normal −y, from x towards z); P to B (normal P × B,
// from P); B to A on the equator (normal −z, from y towards x).
let n = |u: [f64; 3], v: [f64; 3]| {
let c = [u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]];
let l = (c[0] * c[0] + c[1] * c[1] + c[2] * c[2]).sqrt();
[c[0] / l, c[1] / l, c[2] / l]
};
let unit = |u: [f64; 3]| [u[0] / r, u[1] / r, u[2] / r];
t += &dir(20, [0.0, -1.0, 0.0]);
t += "#21=AXIS2_PLACEMENT_3D('',#1,#20,#3);\n#22=CIRCLE('',#21,0.5);\n#23=EDGE_CURVE('',#13,#15,#22,.T.);\n";
t += &dir(30, n(p, b));
t += &dir(31, unit(p));
t += "#32=AXIS2_PLACEMENT_3D('',#1,#30,#31);\n#33=CIRCLE('',#32,0.5);\n#34=EDGE_CURVE('',#15,#14,#33,.T.);\n";
t += &dir(40, [0.0, 0.0, -1.0]);
t += &dir(41, [0.0, 1.0, 0.0]);
t += "#42=AXIS2_PLACEMENT_3D('',#1,#40,#41);\n#43=CIRCLE('',#42,0.5);\n#44=EDGE_CURVE('',#14,#13,#43,.T.);\n";
// Counter-clockwise seen from outside: A, B, P, so every edge the other way round.
t += "#50=ORIENTED_EDGE('',*,*,#44,.F.);\n#51=ORIENTED_EDGE('',*,*,#34,.F.);\n#52=ORIENTED_EDGE('',*,*,#23,.F.);\n";
t += "#53=EDGE_LOOP('',(#50,#51,#52));\n#54=FACE_OUTER_BOUND('',#53,.T.);\n#55=ADVANCED_FACE('',(#54),#5,.T.);\n";
t += "#60=OPEN_SHELL('',(#55));\n#61=SHELL_BASED_SURFACE_MODEL('Ball corner',(#60));\n#62=MANIFOLD_SURFACE_SHAPE_REPRESENTATION('',(#61),#70);\n";
t += &context(70, "(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.))");
t += TAIL;
let imp = step_import(&t).unwrap();
assert_eq!(imp.bodies.len(), 1, "{:?}", imp.warnings);
assert!(imp.warnings.iter().any(|w| w.contains("1 edges split where they pass through a face's pole")), "{:?}", imp.warnings);
// The spherical excess of the unit vectors' triangle, tan(E/2) = |a·(b×c)| / (1 + a·b + b·c + c·a).
let (ua, ub, up) = (unit(a), unit(b), unit(p));
let dot = |u: [f64; 3], v: [f64; 3]| u[0] * v[0] + u[1] * v[1] + u[2] * v[2];
let triple = dot(ua, n(ub, up)) * (1.0 - dot(ub, up).powi(2)).sqrt();
let excess = 2.0 * triple.abs().atan2(1.0 + dot(ua, ub) + dot(ub, up) + dot(up, ua));
let m = measure(&imp.bodies[0].body).unwrap();
assert!(rel(m.area, excess * r * r) < 1e-3, "{} vs {}", m.area, excess * r * r);
}
+28 -14
View File
@@ -192,12 +192,13 @@ pub(crate) fn plan(b: &brep::Brep, tol: f64, turn: Option<size::Turn>) -> Result
};
let mut boundaries: Vec<Boundary> =
crate::par::map("face boundaries", &indexed, curved >= PARALLEL_FACES, |&fi| face_boundary(fi, &nodes)).into_iter().collect::<Result<_>>()?;
untangle(b, &evals, &edge_faces, &mut nodes, &mut boundaries, &face_boundary)?;
let tangled = untangle(b, &evals, &edge_faces, &mut nodes, &mut boundaries, &face_boundary)?;
let planned = crate::par::map("face tessellation", &indexed, curved >= PARALLEL_FACES, |&fi| {
let (Some(face), Some(eval), Some(size), Some(fb)) = (faces.get(fi), evals.get(fi), sizes.get(fi), boundaries.get(fi)) else {
return Err(unsupported(format!("face {fi}: missing")));
};
let (fp, stats) = plan_or_fill(fi, kind(face.parameters()), eval, &fb.loops, &rules, *size)?;
let refused = tangled.iter().find(|(f, _)| *f == fi).map(|(_, why)| why.as_str());
let (fp, stats) = plan_or_fill(fi, kind(face.parameters()), eval, &fb.loops, &rules, *size, refused)?;
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 };
@@ -356,7 +357,7 @@ fn untangle(
nodes: &mut [edges::Nodes],
boundaries: &mut [Boundary],
face_boundary: &(dyn Fn(usize, &[edges::Nodes]) -> Result<Boundary> + Sync),
) -> Result<()> {
) -> Result<Vec<(usize, String)>> {
let crossing = |fi: usize, fb: &Boundary| -> Vec<(u32, u32)> {
let Some(s) = evals.get(fi) else { return Vec::new() };
tangle::crossing(s, &fb.loops).into_iter().filter_map(|(l, k)| fb.segments.get(l)?.get(k).copied()).collect()
@@ -370,15 +371,21 @@ fn untangle(
.collect::<Result<_>>()?;
let mut tangled: Vec<(usize, Vec<(u32, u32)>)> = found.into_iter().enumerate().filter(|(_, c)| !c.is_empty()).collect();
for round in 0..=MAX_UNTANGLE {
let Some((fi, first)) = tangled.first() else { return Ok(()) };
if tangled.is_empty() {
return Ok(Vec::new());
}
if round == MAX_UNTANGLE {
let mut es: Vec<u32> = first.iter().map(|(e, _)| *e).collect();
es.sort_unstable();
es.dedup();
let kind = b.faces.get(*fi).map_or("?", |f| kind(&f.surface));
return Err(unsupported(format!(
"face {fi} ({kind}): boundary crosses itself: edges {es:?} run closer together than their chords can follow after {MAX_UNTANGLE} halvings"
)));
// Each face still crossing, with why: planned leniently, and named (`plan_or_fill`).
return Ok(tangled
.iter()
.map(|(fi, c)| {
let mut es: Vec<u32> = c.iter().map(|(e, _)| *e).collect();
es.sort_unstable();
es.dedup();
let kind = b.faces.get(*fi).map_or("?", |f| kind(&f.surface));
(*fi, format!("face {fi} ({kind}): boundary crosses itself: edges {es:?} run closer together than their chords can follow after {MAX_UNTANGLE} halvings"))
})
.collect());
}
let mut split: Vec<(u32, u32)> = tangled.iter().flat_map(|(_, c)| c.iter().copied()).collect();
split.sort_unstable();
@@ -405,7 +412,7 @@ fn untangle(
}
}
}
Ok(())
Ok(Vec::new())
}
/// Bounded work for the facet angle: a face whose boundary would get more than
@@ -626,7 +633,9 @@ fn holes_beside_outer(dom: &uv::Domain, loops: &mut [seam::Loop]) {
/// One face, planned by [`plan_face`]; a face it refuses because its loops cross in (u, v) is
/// planned once more leniently (see [`Lenient`]) and named in the stats with the first refusal,
/// so the defect upstream is still reported. When the lenient fill refuses too, the first
/// refusal is the error.
/// refusal is the error. `refused`: a face whose boundary still crosses itself after
/// [`untangle`] (two edges meeting tangentially at a cusp, whose chords cross however often
/// they are halved), filled leniently straight away with that reason.
fn plan_or_fill(
fi: usize,
kind: &str,
@@ -634,9 +643,14 @@ fn plan_or_fill(
loops: &[seam::Loop],
rules: &size::Rules,
size: (f64, [f64; 2]),
refused: Option<&str>,
) -> Result<(FacePlan, PlanStats)> {
let mut stats = PlanStats::default();
match plan_face(fi, kind, s, loops, rules, size, &mut stats, Lenient::No) {
let strict = match refused {
Some(why) => Err(unsupported(why.to_string())),
None => plan_face(fi, kind, s, loops, rules, size, &mut stats, Lenient::No),
};
match strict {
Ok(fp) => Ok((fp, stats)),
Err(first) => {
let mut stats = PlanStats::default();
+17 -1
View File
@@ -743,7 +743,7 @@ fn a_curved_face_crossing_itself_is_filled_on_its_surface() {
fn a_face_filled_leniently_is_named_in_the_stats() {
let (s, l, rules) = a_face_crossing_itself();
let size = size::face_size(&s, &uv::domain(&s), &rules);
let Ok((fp, stats)) = plan_or_fill(7, "spline", &s, std::slice::from_ref(&l), &rules, size) else { panic!("filled leniently") };
let Ok((fp, stats)) = plan_or_fill(7, "spline", &s, std::slice::from_ref(&l), &rules, size, None) else { panic!("filled leniently") };
assert!(!fp.triangles.is_empty());
assert_eq!((stats.planar, stats.structured, stats.delaunay), (0, 0, 0));
let [lenient] = stats.lenient.as_slice() else { panic!("one lenient face: {:?}", stats.lenient) };
@@ -751,6 +751,22 @@ fn a_face_filled_leniently_is_named_in_the_stats() {
assert!(lenient.reason.contains("face 7 (spline)"), "{}", lenient.reason);
}
/// A face whose boundary still crosses itself after untangling (two edges leaving a vertex
/// tangentially: their chords cross however often they are halved, as on one of Poppy
/// Humanoid's faces) is filled leniently straight away with that reason, and named: the body's
/// other faces are planned as usual instead of the whole body failing with it.
#[test]
fn a_face_untangling_cannot_resolve_is_filled_leniently_and_named() {
let (s, l, rules) = a_face_crossing_itself();
let size = size::face_size(&s, &uv::domain(&s), &rules);
let why = "face 7 (spline): boundary crosses itself: edges [1, 2] run closer together than their chords can follow";
let Ok((fp, stats)) = plan_or_fill(7, "spline", &s, std::slice::from_ref(&l), &rules, size, Some(why)) else { panic!("filled leniently") };
assert!(!fp.triangles.is_empty());
let [lenient] = stats.lenient.as_slice() else { panic!("one lenient face: {:?}", stats.lenient) };
assert_eq!(lenient.face, 7);
assert!(lenient.reason.contains(why), "{}", lenient.reason);
}
#[test]
fn a_face_bounded_by_two_short_arcs_keeps_an_area_to_mesh() {
// A lens on z = 0 between two radius-1000 circles crossing at (±10, 0), in a record whose