Wave 436/509: Blends: an edge is smooth when its faces' normals agree all along it, decided exactly (and 3 more)

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

- dfa84e84d jyviko: Blends: an edge is smooth when its faces' normals agree all along it, decided exactly
- 6cc759750 jyviko: SMLib import: an extruded circle is a cylinder over the face's trimmed range
- 9b3572797 jyviko: Fillet rig: the known-failing list names the report- cases, not their old ids
- 9851718ea jyviko: Curvature comb: purple envelope and orange teeth from their own theme tokens

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
This commit is contained in:
jyvikoandClaude Opus 5.5 committed 2026-10-30 07:06:10 -04:00
1 parent e58e85e3d6
commit faf08ec1bb
11 files changed
+554 -54

No files matched your search

+4
View File
@@ -230,6 +230,8 @@ impl Tokens {
bend_up: Color32::from_rgb(30, 140, 50),
bend_down: Color32::from_rgb(200, 90, 20),
interference: Color32::from_rgb(235, 45, 40),
comb_envelope: Color32::from_rgb(130, 40, 175),
comb_teeth: Color32::from_rgb(215, 105, 0),
continuity: [
Color32::from_rgb(0, 158, 115),
Color32::from_rgb(0, 114, 178),
@@ -365,6 +367,8 @@ impl Tokens {
t.box_crossing = Color32::from_rgb(80, 200, 120);
t.bend_up = Color32::from_rgb(110, 200, 120);
t.bend_down = Color32::from_rgb(240, 150, 80);
t.comb_envelope = Color32::from_rgb(195, 130, 255);
t.comb_teeth = Color32::from_rgb(255, 160, 60);
t.form_ink = t.text;
t.form_dim = Color32::from_rgb(180, 187, 200);
t.form_primary = t.accent;
+26 -5
View File
@@ -467,11 +467,13 @@ fn peg_front_round(from: f64) -> f64 {
simpson(|y| spandrel(y) * (wall(y) - from), 0.0, 2.0, 20_000)
}
/// The peg's front top edge rounded r2: SMLib's result holds the cylinder's wall as a rational
/// spline whose parameter range runs more than a turn round. As a cylinder over that range (past
/// 360°) SMLib refused it on the way back (SM_ERR_INVALID_INPUT), so the round seemed to build
/// and the body could not be measured or changed. It stays the spline: the body measures, and a
/// round of the three edges at the bar's other top corner builds on it.
/// The peg's front top edge rounded r2: SMLib's result holds the upper half of the cylinder's
/// wall as a rational spline whose parameter range runs more than a turn round, past the face's
/// trims (half a turn). Taken as a cylinder over the spline's range (past 360°) SMLib refused it
/// on the way back (SM_ERR_INVALID_INPUT), so the round seemed to build and the body could not
/// be measured or changed. It goes back as the cylinder over the face's own range: the body
/// measures, a round of the three edges at the bar's other top corner builds on it, and that
/// wall comes back from SMLib a cylinder of radius 8.
#[test]
fn rounds_on_a_bar_running_tangent_into_a_cylinder() {
let mut s = Session::default();
@@ -491,6 +493,25 @@ fn rounds_on_a_bar_running_tangent_into_a_cylinder() {
let bad = b.body.validity();
assert!(bad.is_empty(), "{}: {bad:?}", b.name);
}
// The wall's upper half (x < 40, z in [10, 20], round (40, 8)): every face on the surface
// through (32, 8, 15) is a cylinder of radius 8.
let state = s.model.state();
let rec = state.bodies[0].body.record();
let p0 = solidcraft_geom::Vec3::new(32.0, 8.0, 15.0);
let on: Vec<&solidcraft_geom::brep::Surface> = rec
.faces
.iter()
.map(|f| &f.surface)
.filter(|s| {
let e = solidcraft_geom::brep::eval::SurfaceEval::new(s).ok();
e.and_then(|e| e.project(p0, None, 1e-6).and_then(|uv| e.point(uv))).is_some_and(|q| q.dist(p0) < 1e-6)
})
.collect();
let kinds: Vec<String> = on.iter().map(|s| format!("{s:?}").chars().take(48).collect()).collect();
assert!(
!on.is_empty() && on.iter().all(|s| matches!(s, solidcraft_geom::brep::Surface::Cylinder { radius, .. } if (radius - 8.0).abs() < 1e-9)),
"{kinds:?}"
);
}
/// After the peg's front top round, the edge where that round runs into the cylinder's wall: its
+58 -31
View File
@@ -657,9 +657,10 @@ pub fn blend_edges(body: &Body, picks: &[Vec3]) -> Result<Vec<usize>> {
/// tangentially (within 0.5° at a shared vertex), until no more do. Each pick takes its edge as a
/// blend does (a pick at a vertex where a seam meets a rim takes the rim). An edge only one face
/// uses ends a chain, and so do a seam and a smooth edge (its two faces meet tangentially, within
/// 0.5°): they have no corner to round. A chain does not run on into an edge of the other
/// convexity (a boss's concave base running on tangentially as the convex rim of its overhang):
/// a round cannot pass from one side of the faces to the other, so it ends at that vertex.
/// 0.5°, all along it): they have no corner to round. A chain does not run on into an edge of the
/// other convexity (a boss's concave base running on tangentially as the convex rim of its
/// overhang): a round cannot pass from one side of the faces to the other, so it ends at that
/// vertex.
/// Picks that find no edge are refused.
pub fn tangent_chain(body: &Body, picks: &[Vec3]) -> Result<Vec<Vec3>> {
if picks.is_empty() || picks.len() > 1000 || picks.iter().any(|p| !p.is_finite()) {
@@ -673,7 +674,7 @@ pub fn tangent_chain(body: &Body, picks: &[Vec3]) -> Result<Vec<Vec3>> {
picked.push(e);
}
}
let chain = chain_of(&rec, &picked, &hidden);
let chain = chain_of(&rec, &picked, &hidden)?;
let mut out = picks.to_vec();
for e in chain.into_iter().skip(picked.len()) {
out.push(edge_middle(&rec, e).ok_or_else(|| KernelError::Failed("a point on a chain edge".into()))?);
@@ -696,35 +697,34 @@ fn normal_reach(rec: &Faces) -> f64 {
rec.rec.tolerance.max(1e-7) * 10.0
}
/// Stations along an edge, as fractions of its interval, where [`smooth_join`] compares its
/// faces' normals: its ends, and between them.
const SMOOTH_STATIONS: [f64; 5] = [0.0, 0.25, 0.5, 0.75, 1.0];
/// Whether edge `e`'s two faces meet tangentially (within [`TANGENT_DEGREES`]) all along it: a
/// smooth edge, with no corner to blend. An edge whose faces are tangent at one end only (a
/// round running into a wall it is tangent to where the wall meets its face) is a crease
/// everywhere else, which is not smooth.
fn smooth_join(rec: &Faces, e: usize) -> bool {
let cos = TANGENT_DEGREES.to_radians().cos();
let accept = normal_reach(rec);
let faces: Vec<u32> = rec.faces_of(e).into_iter().collect();
let ([a, b], Some(edge)) = (faces.as_slice(), rec.rec.edges.get(e)) else { return false };
let Ok(curve) = CurveEval::new(edge.parameters()) else { return false };
let [t0, t1] = edge.interval;
SMOOTH_STATIONS.iter().all(|s| {
let Some(p) = curve.point(t0 + (t1 - t0) * s) else { return false };
match (rec.normal_at(*a as usize, p, accept), rec.normal_at(*b as usize, p, accept)) {
(Some(na), Some(nb)) => na.dot(nb) > cos,
_ => false,
}
})
/// Whether edge `e`'s two uses meet tangentially (within [`TANGENT_DEGREES`]) all along it: a
/// smooth edge, with no corner to blend (a seam a face meets itself across is one). An edge
/// whose faces are tangent at one end only (a round running into a wall it is tangent to where
/// the wall meets its face) is a crease everywhere else, which is not smooth; nor is a cusp, its
/// faces folded back on each other (outward normals opposite), which SMLib rounds like any other
/// edge. Decided exactly from the faces' surfaces and the uses' pcurves
/// ([`crate::tangent_edge`]).
fn smooth_join(rec: &Faces, e: usize) -> Result<bool> {
crate::tangent_edge::tangent_all_along(&rec.rec, |f| rec.side(f), e, TANGENT_DEGREES.to_radians())
}
/// `seeds` (edges in the record's order, each once), then every other edge that continues one
/// of them tangentially, as [`tangent_chain`] follows them; `hidden` marks the edges a pick
/// passes over.
fn chain_of(rec: &Faces, seeds: &[usize], hidden: &[bool]) -> Vec<usize> {
fn chain_of(rec: &Faces, seeds: &[usize], hidden: &[bool]) -> Result<Vec<usize>> {
let mut chain = seeds.to_vec();
// Each edge's smoothness, decided once.
let mut smooth: Vec<Option<bool>> = vec![None; rec.rec.edges.len()];
let mut smooth_at = |f: usize| -> Result<bool> {
if let Some(known) = smooth.get(f).copied().flatten() {
return Ok(known);
}
let s = smooth_join(rec, f)?;
if let Some(slot) = smooth.get_mut(f) {
*slot = Some(s);
}
Ok(s)
};
// The unit direction edge `e` leaves its end `v` in.
let leaving = |e: usize, v: u32| -> Option<Vec3> {
let [a, b] = rec.rec.edges.get(e)?.vertices;
@@ -761,7 +761,7 @@ fn chain_of(rec: &Faces, seeds: &[usize], hidden: &[bool]) -> Vec<usize> {
let Some(d) = leaving(e, v) else { continue };
for (f, other) in rec.rec.edges.iter().enumerate() {
let passed_over = hidden.get(f).copied().unwrap_or(false);
if passed_over || chain.contains(&f) || other.uses.len() < 2 || !other.vertices.contains(&v) || smooth_join(rec, f) {
if passed_over || chain.contains(&f) || other.uses.len() < 2 || !other.vertices.contains(&v) || smooth_at(f)? {
continue;
}
let turns_over = matches!((convex(e, v), convex(f, v)), (Some(a), Some(b)) if a != b);
@@ -771,7 +771,7 @@ fn chain_of(rec: &Faces, seeds: &[usize], hidden: &[bool]) -> Vec<usize> {
}
}
}
chain
Ok(chain)
}
/// Before blending exactly `edges` (the record's edges `picks` took, in order): refuses an edge
@@ -783,7 +783,7 @@ pub(crate) fn unfinished_chain(body: &Body, picks: &[Vec3], edges: &[usize], hid
let rec = Faces::of(body)?;
let at = |i: usize| picks.get(i).map(|p| format!("({:.3}, {:.3}, {:.3})", p.x, p.y, p.z)).unwrap_or_default();
for (i, &e) in edges.iter().enumerate() {
if smooth_join(&rec, e) {
if smooth_join(&rec, e)? {
return Err(KernelError::Invalid(format!("the edge at {} is smooth: its faces meet tangentially, so there is nothing to round", at(i))));
}
}
@@ -793,7 +793,7 @@ pub(crate) fn unfinished_chain(body: &Body, picks: &[Vec3], edges: &[usize], hid
seeds.push(e);
}
}
Ok(chain_of(&rec, &seeds, hidden).get(seeds.len()).and_then(|&next| edge_middle(&rec, next)))
Ok(chain_of(&rec, &seeds, hidden)?.get(seeds.len()).and_then(|&next| edge_middle(&rec, next)))
}
/// A blend's failure where a picked edge continues tangentially into `next`, an edge left out
@@ -857,6 +857,33 @@ mod tests {
assert_eq!(tangent_chain(&rounded, &[side]).unwrap(), vec![side], "the chain ran into the round's seam");
}
/// Smoothness is decided all along an edge. A bar 40 × 16 × 10 whose front face runs tangent
/// into a cylinder R8 at its far end, its front top edge rounded r2: the round's contact with
/// the front face and the front face's tangent line with the wall are smooth (plane against
/// cylinder, tangent all along); the round's edge with the wall is tangent at its lower end
/// (40, 0, 8) only, a crease everywhere else; a box edge is a crease. A knife edge (a wall of
/// radius 21 running out into a plane, #851: its faces tangent all along but folded back on
/// each other) is a cusp, which is not smooth.
#[test]
fn an_edge_is_smooth_only_where_its_faces_are_tangent_all_along_it() {
let bar = crate::build::box_solid(Vec3::ZERO, Vec3::new(40.0, 16.0, 10.0)).unwrap();
let post = crate::build::cylinder(Vec3::new(40.0, 8.0, 0.0), Vec3::Z, 8.0, 20.0).unwrap();
let peg = crate::boolean(&bar, &post, crate::BoolOp::Union).unwrap().unwrap();
let rounded = fillet(&peg, &[Vec3::new(20.0, 0.0, 10.0)], 2.0).unwrap();
let rec = Faces::of(&rounded).unwrap();
let smooth = |p: Vec3| smooth_join(&rec, blend_edges(&rounded, &[p]).unwrap()[0]).unwrap();
assert!(smooth(Vec3::new(20.0, 0.0, 8.0)), "the round's contact with the front face");
assert!(smooth(Vec3::new(40.0, 0.0, 4.0)), "the front face's tangent line with the wall");
assert!(!smooth(Vec3::new(37.2641, 0.4827, 9.3025)), "the round's edge with the wall");
assert!(!smooth(Vec3::new(20.0, 0.0, 0.0)), "the bar's front bottom edge");
let tip = 62.0 + 1.0 / 6.0;
let slab = crate::build::box_solid(Vec3::new(0.0, -21.0, 0.0), Vec3::new(tip, 0.0, 10.0)).unwrap();
let disc = crate::build::cylinder(Vec3::new(tip, -21.0, 0.0), Vec3::Z, 21.0, 10.0).unwrap();
let knife = crate::boolean(&slab, &disc, crate::BoolOp::Cut).unwrap().unwrap();
let rec = Faces::of(&knife).unwrap();
assert!(!smooth_join(&rec, blend_edges(&knife, &[Vec3::new(tip, 0.0, 5.0)]).unwrap()[0]).unwrap(), "the knife edge");
}
#[test]
fn g2_fillet_has_the_quintics_area() {
let body = crate::build::box_solid(Vec3::ZERO, Vec3::new(20.0, 20.0, 20.0)).unwrap();
+1
View File
@@ -139,6 +139,7 @@ mod surfaces;
mod sweep_path;
#[cfg(test)]
mod sweep_rails_tests;
mod tangent_edge;
mod tess;
#[cfg(test)]
mod thin_face_tests;
+349
View File
@@ -0,0 +1,349 @@
//! Whether the two faces on either side of an edge meet tangentially all along it, decided
//! exactly from the record.
//!
//! Each use of the edge carries its pcurve `(u(t), v(t))` in its face's parameters
//! ([`Face::parameters`]: a plane, or the B-spline surface SMLib holds the face as), on the edge's
//! own parameter `t`, so the two uses' points at one `t` are one point of the edge. The face's
//! natural normal there is `S_u × S_v` at the pcurve's point. On a piece of `t` where the pcurve
//! stays within one span of its own knots and one knot cell of its surface, both are
//! polynomials: `S` of degree `(p, q)` in `(u, v)` (rational: its homogeneous form, of the same
//! degree), the pcurve of degree `r` in `t`. So `S_u × S_v`, scaled by a positive power of the
//! weights, is a polynomial in `t` of degree at most `(2p + 2q - 2) r` (rational: `(4p + 4q - 2) r`,
//! from `(A_u w - A w_u) × (A_v w - A w_v)`), and a plane's is constant. The cross product of the
//! two faces' normals is then a polynomial vector of degree at most the sum `D` of theirs, and a
//! polynomial of degree `D` that vanishes at `D + 1` points vanishes everywhere: the faces are
//! parallel all along the piece exactly when they are at `D + 1` of its points, its Chebyshev
//! points, and on one side (smooth, not folded back on each other) when their outward normals
//! point the same way there. No point is a guess at where the faces part: one where they do is
//! a witness of a crease (or a cusp, the faces folded back), and agreement at all of them is
//! a proof of tangency, within the angle the caller allows at each (between them, the cross
//! product is bounded by its largest value at them times the Lebesgue constant of `D + 1`
//! Chebyshev points, under `1 + (2/π) ln(D + 1)`).
//!
//! The pieces' ends are the pcurves' knots and the parameters where a pcurve crosses a knot line
//! of its surface (periodic copies included), each a root of `U(t) - k W(t)` on one Bézier span
//! of the pcurve, isolated by subdividing its Bernstein coefficients (no sign change: no root;
//! one: exactly one) and found to rounding by bisection.
use solidcraft_geom::Vec3;
use solidcraft_geom::brep::eval::SurfaceEval;
use solidcraft_geom::brep::{Brep, Curve2, Face, Surface};
use solidcraft_geom::bspline::{HPoint, segment};
use crate::{KernelError, Result};
/// Most points one edge's decision evaluates.
const MAX_POINTS: usize = 1_000_000;
/// Largest degree of a piece's cross product the decision takes.
const MAX_DEGREE: usize = 4096;
/// Subdivisions of one Bernstein polynomial while isolating its roots: its pieces are then
/// 2⁻⁴⁸ of a pcurve span, where a root left unisolated (a pcurve touching a knot line) ends a
/// piece there.
const MAX_DEPTH: u32 = 48;
/// Subdivisions one root isolation may make: a root that touches its knot line (a double root,
/// the only kind that keeps two sign changes as the pieces shrink) costs one a level.
const MAX_SUBDIVISIONS: usize = 4096;
/// Below this sine of the angle between `S_u` and `S_v` a face's normal is undefined at that
/// point (a pole): the normal, and so the cross product, is zero there.
const DEGENERATE: f64 = 1e-9;
/// One use of the edge: its face's surface, the sign turning its natural normal outward, its
/// pcurve and the degree of its normal along a piece.
struct Use<'a> {
surface: SurfaceEval,
sign: f64,
pcurve: &'a Curve2,
degree: usize,
/// The knot lines of the surface in u and v (its distinct knots over its domain) and its
/// periods: a polynomial cell's sides.
lines: [Vec<f64>; 2],
period: [Option<f64>; 2],
}
fn failed(msg: String) -> KernelError {
KernelError::Failed(msg)
}
/// A B-spline's degree from its knot and control point counts.
fn degree_of(knots: usize, count: usize) -> Option<usize> {
knots.checked_sub(count.checked_add(1)?)
}
/// The distinct knots of a knot vector over its domain `[knots[p], knots[n]]`.
fn lines(knots: &[f64], count: usize) -> Option<Vec<f64>> {
let p = degree_of(knots.len(), count)?;
let mut out: Vec<f64> = knots.get(p..=count)?.to_vec();
out.dedup();
Some(out)
}
impl<'a> Use<'a> {
fn new(face: &Face, sign: f64, pcurve: &'a Curve2) -> Result<Use<'a>> {
let surface = face.parameters();
let r = match pcurve {
Curve2::Spline { knots, points } => degree_of(knots.len(), points.len()),
Curve2::RationalSpline { knots, homogeneous } => degree_of(knots.len(), homogeneous.len()),
}
.ok_or_else(|| failed("a pcurve's degree".into()))?;
// The normal's degree in (u, v) and the knot lines.
let (uv_degree, lines) = match surface {
Surface::Plane { .. } | Surface::PlaneThrough { .. } => (0, [Vec::new(), Vec::new()]),
Surface::Spline { u_knots, v_knots, points } => {
let (nu, nv) = (points.len(), points.first().map_or(0, Vec::len));
let (p, q) = (degree_of(u_knots.len(), nu), degree_of(v_knots.len(), nv));
let (Some(p), Some(q), Some(lu), Some(lv)) = (p, q, lines(u_knots, nu), lines(v_knots, nv)) else {
return Err(failed("a spline face's degree".into()));
};
((2 * p + 2 * q).saturating_sub(2), [lu, lv])
}
Surface::RationalSpline { u_knots, v_knots, homogeneous } => {
let (nu, nv) = (homogeneous.len(), homogeneous.first().map_or(0, Vec::len));
let (p, q) = (degree_of(u_knots.len(), nu), degree_of(v_knots.len(), nv));
let (Some(p), Some(q), Some(lu), Some(lv)) = (p, q, lines(u_knots, nu), lines(v_knots, nv)) else {
return Err(failed("a spline face's degree".into()));
};
((4 * p + 4 * q).saturating_sub(2), [lu, lv])
}
// SMLib gives every curved analytic face its NURBS form, which its pcurves are on.
_ => return Err(crate::blend::unsupported("deciding tangency along an edge of a face that is no plane and has no NURBS form")),
};
let eval = SurfaceEval::new(surface).map_err(|e| failed(format!("a face's surface: {e}")))?;
let period = eval.domain().period;
Ok(Use { surface: eval, sign, pcurve, degree: uv_degree.saturating_mul(r), lines, period })
}
/// The outward unit normal at the pcurve's point at `t`; `Ok(None)` where it is undefined (a
/// pole: a zero of the normal).
fn normal(&self, t: f64) -> Result<Option<Vec3>> {
let uv = self.pcurve.eval(t).ok_or_else(|| failed(format!("a pcurve at {t}")))?;
let (_, su, sv) = self.surface.derivs(uv[0], uv[1]).ok_or_else(|| failed(format!("a face at ({}, {})", uv[0], uv[1])))?;
let n = su.cross(sv);
if !(n.len() > DEGENERATE * su.len() * sv.len()) {
return Ok(None);
}
Ok(n.normalized().map(|n| n * self.sign))
}
/// The parameters in `(t0, t1)` where the normal stops being one polynomial: the pcurve's
/// own knots, and where it crosses a knot line of the surface.
fn breaks(&self, [t0, t1]: [f64; 2], out: &mut Vec<f64>) -> Result<()> {
let (knots, ctrl): (&[f64], Vec<HPoint>) = match self.pcurve {
Curve2::Spline { knots, points } => (knots, points.iter().map(|p| HPoint { x: p[0], y: p[1], z: 0.0, w: 1.0 }).collect()),
Curve2::RationalSpline { knots, homogeneous } => {
(knots, homogeneous.iter().map(|h| HPoint { x: h[0], y: h[1], z: 0.0, w: h[2] }).collect())
}
};
let r = degree_of(knots.len(), ctrl.len()).ok_or_else(|| failed("a pcurve's degree".into()))?;
let mut spans: Vec<f64> = vec![t0];
spans.extend(knots.iter().copied().filter(|k| *k > t0 && *k < t1));
spans.push(t1);
spans.dedup();
out.extend(spans.iter().copied());
if self.lines.iter().all(Vec::is_empty) {
return Ok(());
}
for w in spans.windows(2) {
let &[a, b] = w else { continue };
let (_, bez) = segment(r, knots, &ctrl, a, b).map_err(|e| failed(format!("a pcurve span: {e}")))?;
if bez.len() != r + 1 {
return Err(failed("a pcurve span is no Bézier piece".into()));
}
for d in 0..2 {
let coord = |h: &HPoint| if d == 0 { h.x } else { h.y };
// The span's range in this direction: its control points' hull (weights > 0).
let along: Vec<f64> = bez.iter().map(|h| coord(h) / h.w).collect();
let lo = along.iter().copied().fold(f64::INFINITY, f64::min);
let hi = along.iter().copied().fold(f64::NEG_INFINITY, f64::max);
if !(lo.is_finite() && hi.is_finite()) {
return Err(failed("a pcurve's control points".into()));
}
for k in cell_sides(&self.lines[d], self.period[d], lo, hi)? {
// Coefficients within rounding of zero are zero: a pcurve running along
// the knot line (a seam, a boundary) crosses none there.
let scale = bez.iter().map(|h| coord(h).abs() + (k * h.w).abs()).fold(0.0, f64::max);
let floor = 64.0 * f64::EPSILON * scale;
let c: Vec<f64> = bez.iter().map(|h| coord(h) - k * h.w).map(|x| if x.abs() <= floor { 0.0 } else { x }).collect();
if c.iter().all(|x| *x == 0.0) {
continue;
}
let mut roots = Vec::new();
let mut work = MAX_SUBDIVISIONS;
bernstein_roots(&c, 0.0, 1.0, MAX_DEPTH, &mut work, &mut roots);
out.extend(roots.into_iter().map(|s| a + (b - a) * s));
}
}
}
Ok(())
}
}
/// The knot lines `lines` (and, with a period, their copies a whole number of periods away) that
/// lie in `[lo, hi]`.
fn cell_sides(lines: &[f64], period: Option<f64>, lo: f64, hi: f64) -> Result<Vec<f64>> {
let Some(p) = period.filter(|p| *p > 0.0) else {
return Ok(lines.iter().copied().filter(|k| *k >= lo && *k <= hi).collect());
};
let (Some(&first), Some(&last)) = (lines.first(), lines.last()) else { return Ok(Vec::new()) };
let (n0, n1) = (((lo - last) / p).floor(), ((hi - first) / p).ceil());
if !(n1 - n0 <= 64.0) {
return Err(failed("a pcurve running round its surface too many times".into()));
}
let mut out = Vec::new();
let mut n = n0;
while n <= n1 {
out.extend(lines.iter().map(|k| k + n * p).filter(|k| *k >= lo && *k <= hi));
n += 1.0;
}
Ok(out)
}
/// The value at `s` of the polynomial with Bernstein coefficients `c` on `[0, 1]` (de Casteljau).
fn bernstein_value(c: &[f64], s: f64) -> f64 {
let mut b = c.to_vec();
while b.len() > 1 {
b = b.windows(2).map(|w| if let [x, y] = w { x * (1.0 - s) + y * s } else { 0.0 }).collect();
}
b.first().copied().unwrap_or(0.0)
}
/// The Bernstein coefficients of the polynomial `c` on `[0, ½]` and `[½, 1]`.
fn bernstein_halves(c: &[f64]) -> (Vec<f64>, Vec<f64>) {
let mut left = Vec::with_capacity(c.len());
let mut right = Vec::with_capacity(c.len());
let mut b = c.to_vec();
while let (Some(&f), Some(&l)) = (b.first(), b.last()) {
left.push(f);
right.push(l);
b = b.windows(2).map(|w| if let [x, y] = w { 0.5 * (x + y) } else { 0.0 }).collect();
}
right.reverse();
(left, right)
}
/// The roots in `[lo, hi]` of the polynomial with Bernstein coefficients `c` there, mapped
/// into `[lo, hi]`'s place in `[0, 1]`: none where the coefficients keep one sign, exactly one
/// where they change sign once (found by bisection to rounding), otherwise those of each half.
fn bernstein_roots(c: &[f64], lo: f64, hi: f64, depth: u32, work: &mut usize, out: &mut Vec<f64>) {
let (Some(&first), Some(&last)) = (c.first(), c.last()) else { return };
if first == 0.0 {
out.push(lo);
}
if last == 0.0 {
out.push(hi);
}
let signs: Vec<bool> = c.iter().filter(|x| **x != 0.0).map(|x| *x > 0.0).collect();
let changes = signs.windows(2).filter(|w| matches!(w, [x, y] if x != y)).count();
if changes == 0 {
return;
}
if changes == 1 && first != 0.0 && last != 0.0 {
let (mut a, mut b) = (0.0, 1.0);
for _ in 0..64 {
let m = 0.5 * (a + b);
if !(m > a && m < b) {
break;
}
if (bernstein_value(c, m) > 0.0) == (first > 0.0) {
a = m;
} else {
b = m;
}
}
out.push(lo + (hi - lo) * 0.5 * (a + b));
return;
}
let mid = 0.5 * (lo + hi);
// Out of depth or work, the roots are within this piece: it ends a piece of the edge at
// both its ends.
if depth == 0 || *work == 0 {
out.extend([lo, hi]);
return;
}
*work -= 1;
let (l, r) = bernstein_halves(c);
bernstein_roots(&l, lo, mid, depth - 1, work, out);
bernstein_roots(&r, mid, hi, depth - 1, work, out);
}
/// Whether edge `e`'s two uses' faces meet with outward normals within `max_angle` (radians) of
/// each other all along it, `sign(f)` turning face `f`'s natural normal outward. An edge with
/// other than two uses has no such pair: false. See the module documentation.
pub(crate) fn tangent_all_along(rec: &Brep, sign: impl Fn(usize) -> Option<f64>, e: usize, max_angle: f64) -> Result<bool> {
let edge = rec.edges.get(e).ok_or_else(|| failed(format!("edge {e}")))?;
let [ua, ub] = edge.uses.as_slice() else { return Ok(false) };
let use_of = |u: &solidcraft_geom::brep::EdgeUseRef| -> Result<Use<'_>> {
let face = rec.faces.get(u.face as usize).ok_or_else(|| failed(format!("face {}", u.face)))?;
let oriented = match face.loops.get(u.r#loop as usize) {
Some(solidcraft_geom::brep::Loop::Edges { edges }) => edges.get(u.position as usize),
_ => None,
}
.ok_or_else(|| failed(format!("a use of edge {e}")))?;
let s = sign(u.face as usize).ok_or_else(|| failed(format!("face {} is in no shell", u.face)))?;
Use::new(face, s, &oriented.pcurve)
};
let (a, b) = (use_of(ua)?, use_of(ub)?);
let degree = a.degree.saturating_add(b.degree);
if degree > MAX_DEGREE {
return Err(failed(format!("deciding tangency along edge {e}: its faces' normals are of degree {degree}")));
}
let interval = edge.interval;
let mut breaks = Vec::new();
a.breaks(interval, &mut breaks)?;
b.breaks(interval, &mut breaks)?;
breaks.retain(|t| *t >= interval[0] && *t <= interval[1]);
breaks.sort_by(f64::total_cmp);
breaks.dedup();
let nodes = degree + 1;
if breaks.len().saturating_mul(nodes) > MAX_POINTS {
return Err(failed(format!("deciding tangency along edge {e}: {} pieces of degree {degree}", breaks.len())));
}
let cos = max_angle.cos();
let mut defined = false;
for w in breaks.windows(2) {
let &[t0, t1] = w else { continue };
let (m, h) = (0.5 * (t0 + t1), 0.5 * (t1 - t0));
if !(h > 0.0) {
continue;
}
for j in 0..nodes {
let t = m + h * ((2 * j + 1) as f64 * std::f64::consts::PI / (2 * nodes) as f64).cos();
if let (Some(na), Some(nb)) = (a.normal(t)?, b.normal(t)?) {
if !(na.dot(nb) >= cos) {
return Ok(false);
}
defined = true;
}
}
}
if !defined {
return Err(failed(format!("edge {e}: its faces' normals are undefined all along it")));
}
Ok(true)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bernstein_roots_are_isolated_and_exact() {
// (s - 0.25)(s - 0.75) = s² - s + 3/16 in Bernstein form on [0, 1]: 3/16, 3/16 - 1/2, 3/16.
let c = [0.1875, 0.1875 - 0.5, 0.1875];
let mut roots = Vec::new();
let mut work = MAX_SUBDIVISIONS;
bernstein_roots(&c, 0.0, 1.0, MAX_DEPTH, &mut work, &mut roots);
roots.sort_by(f64::total_cmp);
assert_eq!(roots.len(), 2, "{roots:?}");
assert!((roots[0] - 0.25).abs() < 1e-15 && (roots[1] - 0.75).abs() < 1e-15, "{roots:?}");
// No sign change: no root.
let mut none = Vec::new();
bernstein_roots(&[1.0, 0.5, 2.0], 0.0, 1.0, MAX_DEPTH, &mut work, &mut none);
assert!(none.is_empty());
}
#[test]
fn periodic_knot_lines_repeat_by_whole_periods() {
let sides = cell_sides(&[0.0, 1.0, 2.0], Some(2.0), 1.5, 4.5).unwrap();
assert_eq!(sides, vec![2.0, 2.0, 3.0, 4.0, 4.0]);
}
}
+105 -11
View File
@@ -22,6 +22,9 @@ use solidcraft_geom::brep::eval::{CurveEval, SurfaceEval};
use super::{R, Unsupported};
/// Halvings one span's extent may take.
const MAX_EXTENT_PIECES: usize = 4096;
const NOT_ANALYTIC: Unsupported = Unsupported::NotYet("revolved surfaces other than planes, cylinders, cones, spheres and tori");
/// `brep` with every revolution and extruded circle replaced by its analytic surface (borrowed
@@ -43,7 +46,12 @@ pub(super) fn analytic(brep: &b::Brep) -> R<Cow<'_, b::Brep>> {
}
}
b::Surface::RationalSpline { u_knots, v_knots, homogeneous } => {
extruded_circle(u_knots, v_knots, homogeneous, f.domain, 1e-9 * size.max(1.0))
let found = extruded_circle(u_knots, v_knots, homogeneous, trimmed(brep, f)?, 1e-9 * size.max(1.0));
// A face's trims run at most one turn round a cylinder (SMLib refuses more).
if found.as_ref().is_some_and(|(_, range)| range[0][1] - range[0][0] > TAU * (1.0 + 1e-9)) {
return Err(Unsupported::Invalid("a cylinder's face whose trims run more than one turn round"));
}
found
}
_ => None,
};
@@ -76,14 +84,16 @@ pub(super) fn analytic(brep: &b::Brep) -> R<Cow<'_, b::Brep>> {
/// A rational spline that is a circular arc swept straight along the arc's axis, as a cylinder
/// with the face's range on it: one direction linear (two rows of control points), the second
/// row the first moved by one vector at right angles to the circle the first row runs on, the
/// weights alike. `None` where it is not, or where its range runs more than one turn round.
fn extruded_circle(u_knots: &[f64], v_knots: &[f64], net: &[Vec<[f64; 4]>], domain: [[f64; 2]; 2], tol: f64) -> Option<(b::Surface, [[f64; 2]; 2])> {
/// weights alike. `None` where it is not. The range is the face's own, its trims' box `trims`
/// ([`trimmed`]), not the spline's: SMLib's spline of a cylinder's wall may run on past the face,
/// more than a turn round (a blend's support surface extended past the face's ends).
fn extruded_circle(u_knots: &[f64], v_knots: &[f64], net: &[Vec<[f64; 4]>], trims: [[f64; 2]; 2], tol: f64) -> Option<(b::Surface, [[f64; 2]; 2])> {
let linear = |k: &[f64]| k.len() == 4 && k[0] == k[1] && k[2] == k[3] && k[1] < k[2];
let (rows, arc_knots, arc_range, line_knots, line_range): ([Vec<[f64; 4]>; 2], &[f64], [f64; 2], &[f64], [f64; 2]) =
if net.iter().all(|r| r.len() == 2) && linear(v_knots) && !linear(u_knots) {
([net.iter().map(|r| r[0]).collect(), net.iter().map(|r| r[1]).collect()], u_knots, domain[0], v_knots, domain[1])
([net.iter().map(|r| r[0]).collect(), net.iter().map(|r| r[1]).collect()], u_knots, trims[0], v_knots, trims[1])
} else if net.len() == 2 && linear(u_knots) && !linear(v_knots) {
([net.first()?.clone(), net.get(1)?.clone()], v_knots, domain[1], u_knots, domain[0])
([net.first()?.clone(), net.get(1)?.clone()], v_knots, trims[1], u_knots, trims[0])
} else {
return None;
};
@@ -117,18 +127,102 @@ fn extruded_circle(u_knots: &[f64], v_knots: &[f64], net: &[Vec<[f64; 4]>], doma
prev = a;
angle = a;
}
// A cylinder's range is at most one turn (SMLib refuses more). A spline whose range runs
// further round (SMLib's own, its parameter range past the face's trims) has no such range:
// it stays the exact spline it is.
if angle.abs() > TAU * (1.0 + 1e-9) {
return None;
}
let height = |s: f64| (s - line_knots[1]) / (line_knots[2] - line_knots[1]) * d.len();
let sorted = |a: f64, b: f64| if a <= b { [a, b] } else { [b, a] };
let frame = b::Frame { origin: arr(center), x_axis: arr(x), y_axis: arr(y) };
Some((b::Surface::Cylinder { frame, radius }, [sorted(0.0, angle), sorted(height(line_range[0]), height(line_range[1]))]))
}
/// The box in its surface's parameters round face `f`'s loops: each use's pcurve over its edge's
/// interval, its least and greatest u and v ([`pcurve_box`]). The range the face takes of its
/// surface, which may run on past it.
fn trimmed(brep: &b::Brep, f: &b::Face) -> R<[[f64; 2]; 2]> {
let mut lo = [f64::INFINITY; 2];
let mut hi = [f64::NEG_INFINITY; 2];
for l in &f.loops {
let b::Loop::Edges { edges } = l else { return Err(Unsupported::Invalid("a vertex loop on a cylinder's face")) };
for u in edges {
let interval = brep.edges.get(u.edge as usize).ok_or(Unsupported::Invalid("edge"))?.interval;
let [ru, rv] = pcurve_box(&u.pcurve, interval).ok_or(Unsupported::Invalid("a pcurve that does not evaluate"))?;
for (k, r) in [ru, rv].into_iter().enumerate() {
lo[k] = lo[k].min(r[0]);
hi[k] = hi[k].max(r[1]);
}
}
}
if !(lo.iter().chain(&hi).all(|x| x.is_finite()) && lo[0] <= hi[0] && lo[1] <= hi[1]) {
return Err(Unsupported::Invalid("a face without trims"));
}
Ok([[lo[0], hi[0]], [lo[1], hi[1]]])
}
/// A pcurve's least and greatest u and v over `interval`, to rounding: on each Bézier span, the
/// greatest value found at a span's points (its ends, then the ends of its halves) bounds the
/// greatest from below, and its control points' (weights positive) from above; halves whose
/// control points can't beat the best found are dropped, the others halved, until the two meet.
fn pcurve_box(c: &b::Curve2, interval: [f64; 2]) -> Option<[[f64; 2]; 2]> {
use solidcraft_geom::bspline::{HPoint, bezier_segment, segment};
let (knots, ctrl): (&[f64], Vec<HPoint>) = match c {
b::Curve2::Spline { knots, points } => (knots, points.iter().map(|p| HPoint { x: p[0], y: p[1], z: 0.0, w: 1.0 }).collect()),
b::Curve2::RationalSpline { knots, homogeneous } => {
(knots, homogeneous.iter().map(|h| HPoint { x: h[0], y: h[1], z: 0.0, w: h[2] }).collect())
}
};
let p = knots.len().checked_sub(ctrl.len() + 1)?;
let [t0, t1] = interval;
let mut ends: Vec<f64> = vec![t0];
ends.extend(knots.iter().copied().filter(|k| *k > t0 && *k < t1));
ends.push(t1);
ends.dedup();
let mut out = [[f64::INFINITY, f64::NEG_INFINITY]; 2];
for w in ends.windows(2) {
let &[a, z] = w else { continue };
let (_, span) = segment(p, knots, &ctrl, a, z).ok()?;
if span.len() != p + 1 {
return None;
}
for (k, r) in out.iter_mut().enumerate() {
let along = |h: &HPoint| if k == 0 { h.x / h.w } else { h.y / h.w };
for sign in [1.0, -1.0] {
let value = |piece: &[HPoint]| -> Option<[f64; 2]> {
let (first, last) = (piece.first()?, piece.last()?);
let at = (sign * along(first)).max(sign * along(last));
let hull = piece.iter().map(|h| sign * along(h)).fold(f64::NEG_INFINITY, f64::max);
Some([at, hull])
};
let [mut best, _] = value(&span)?;
let mut pieces = vec![span.clone()];
let mut work = 0usize;
while let Some(piece) = pieces.pop() {
let [at, hull] = value(&piece)?;
best = best.max(at);
if hull <= best + 64.0 * f64::EPSILON * (1.0 + best.abs()) {
continue;
}
// Out of work (a pcurve running along its extreme to rounding), the piece's
// control points bound it: the box still holds the curve.
work += 1;
if work > MAX_EXTENT_PIECES {
best = hull;
continue;
}
pieces.push(bezier_segment(&piece, 0.0, 0.5).ok()?);
pieces.push(bezier_segment(&piece, 0.5, 1.0).ok()?);
}
if !best.is_finite() {
return None;
}
if sign > 0.0 {
r[1] = r[1].max(best);
} else {
r[0] = r[0].min(-best);
}
}
}
}
Some(out)
}
/// An analytic surface's frame turned by `angle` about its axis: u then counts from there.
fn turn_frame(s: &mut b::Surface, angle: f64) {
if let b::Surface::Cylinder { frame, .. } | b::Surface::Cone { frame, .. } | b::Surface::Sphere { frame, .. } | b::Surface::Torus { frame, .. } =
@@ -293,6 +293,7 @@ fn groups(t: &Tokens) -> Vec<Group> {
// Every axis drawn: origin, view triad, navigation cube, Move gizmo… (one set, #976).
group("axes", &[("axis_x", t.axis_x), ("axis_y", t.axis_y), ("axis_z", t.axis_z)], &[("viewport_top", t.viewport_top)]),
group("bend lines", &[("bend_up", t.bend_up), ("bend_down", t.bend_down)], &[("viewport_top", t.viewport_top)]),
group("curvature comb", &[("comb_envelope", t.comb_envelope), ("comb_teeth", t.comb_teeth)], &[("viewport_top", t.viewport_top)]),
group("box select", &[("box_window", t.box_window), ("box_crossing", t.box_crossing)], &[("viewport_top", t.viewport_top)]),
group(
"selection",
+2 -2
View File
@@ -490,10 +490,10 @@ fn combs(app: &SolveApp, painter: &egui::Painter, proj: &Proj) {
let mut tips: Vec<Pos2> = Vec::new();
for [a, b] in comb {
let (Some(p), Some(q)) = (proj.to_screen(*a), proj.to_screen(*b)) else { continue };
painter.line_segment([p, q], Stroke::new(1.0, tk.warning.gamma_multiply(0.8)));
painter.line_segment([p, q], Stroke::new(1.0, tk.comb_teeth));
tips.push(q);
}
painter.add(egui::Shape::line(tips, Stroke::new(1.4, tk.warning)));
painter.add(egui::Shape::line(tips, Stroke::new(1.4, tk.comb_envelope)));
}
});
}
+6
View File
@@ -811,6 +811,10 @@ pub struct Tokens {
pub bend_down: Color32,
/// Interference: the clash fill, before its shading.
pub interference: Color32,
/// Curvature Comb: the envelope through the teeth tips (purple) and the teeth, the scaled
/// normals (orange).
pub comb_envelope: Color32,
pub comb_teeth: Color32,
/// Continuity Analysis seams, best to worst as `inspect.continuity` judges them: smooth (G2),
/// tangent with a curvature jump (G1), a crease (G0) and a gap. Bluish green, blue, yellow
/// and reddish purple stay apart in every colour vision mode, so no mode changes them.
@@ -920,6 +924,8 @@ impl Tokens {
bend_up: view.bend_up,
bend_down: view.bend_down,
interference: view.interference,
comb_envelope: view.comb_envelope,
comb_teeth: view.comb_teeth,
continuity: view.continuity,
analysis: view.analysis,
..self
+1 -1
View File
@@ -309,7 +309,7 @@ theme_keys! {
sketch_point, sketch_point_free, dialog_bg, dialog_border, chip, section_cap,
profile_fill, glyph_bg, glyph_edge, glyph_text, overlay, rubber_band, app_bar_text, app_bar_hover, timeline_item,
timeline_item_error, timeline_marker, field, box_window, box_crossing, sketch_projected, sketch_lost,
sketch_construction, sketch_centerline, bend_up, bend_down, interference,
sketch_construction, sketch_centerline, bend_up, bend_down, interference, comb_envelope, comb_teeth,
sketch_ink, sketch_soft, surface_fill, surface_ink, surface_soft, mesh_fill, mesh_ink, mesh_soft, form_fill, form_tab_ink, form_soft, sheet_fill, sheet_ink, sheet_soft, render_fill, render_ink, render_soft, tab_on_fill_dark,
app_bar_separator, surface_accent, mesh_accent, form_accent, render_accent, on_accent, agent_ok, agent_busy, app_bar_hover_subtle,
app_bar_hover_button, app_bar_hover_strong, app_bar_tab_active,
+1 -4
View File
@@ -15,8 +15,6 @@
1080-fillet-sloped-basin-1-mm-rim-r4-4-edges not supported yet: a convex round past its face is not yet built by subtraction
1113-chamfer-arc-top-edge-full-wall-height-refused builds the chamfer as tall as the wall where it should be refused as too large
1207-fillet-bar-top-edge-after-boss-arc-round-r0-5 SMLib's check refuses the round: internal coincident control points in a B-spline curve (an internal pole)
1207-fillet-concave-arc-bar-top-into-cylinder-solver-residues SMLib status 1001: SmFilletNx1Corner cannot make the topology at the arc's end (fillet error 3, corner type 8)
1207-fillet-corner-after-round-running-into-cylinder-solver-residues SMLib's check refuses the round: its corner's surface of revolution is close to singular at its boundary (SmSurface test 5)
1668-fillet-outer-top-edge-of-thin-wall-r7-past-inside-corner a slit along the hollow, 0.507 mm^3 off: 38458.389 is V0 less the round's whole section along 104, so the section's part over the hollow (0.005068 mm^2 along 100), which is not material, is taken anyway
190-fillet-disc-rim-past-its-2mm-wall SMLib status 1001: a convex round past its face is not yet built by subtraction
2852-fillet-outer-round-r30-past-inner-round-refused refused for another reason: SMLib's result is not a closed solid, not too large naming 25.24
@@ -34,7 +32,6 @@
851-chamfer-knife-edge-tip-2-box-less-cylinder SMLib SM_ERR_INVALID_INPUT on the knife edge (opposite normals on its two faces)
851-chamfer-knife-edge-tip-2-sketched SMLib SM_ERR_INVALID_INPUT on the knife edge (opposite normals on its two faces)
851-chamfer-knife-edge-tip-5 SMLib SM_ERR_INVALID_INPUT on the knife edge (opposite normals on its two faces)
896-fillet-edge-tangent-at-one-end-into-cylinder SMLib's check refuses the round that fades out at the edge's tangent end: internal poles, coincident control points, cusps
9-fillet-corner-curvature-with-reduce-fit not supported yet: solid.fillet builds corner g2 with the exact fit only
9-fillet-corner-curvature-with-taper-fit not supported yet: solid.fillet builds corner g2 with the exact fit only
926-chamfer-top-edge-ending-on-boss-wall-above-it not supported yet: the chamfer is refused as larger than its faces where it ends on the boss's wall
@@ -52,4 +49,4 @@ every-pocket-rim-edges-r5-as-deep-as-pocket a corner patch where two rim rounds
report-fillet-concave-arc-bar-top-into-cylinder-solver-residues SMLib status 1001: SmFilletNx1Corner cannot make the topology at the arc's end (fillet error 3, corner type 8)
report-fillet-corner-after-round-running-into-cylinder-solver-residues SMLib's check refuses the round: its corner's surface of revolution is close to singular at its boundary (SmSurface test 5)
report-fillet-curvature-three-edges-at-bar-corner SMLib's check refuses the open setback sheet of curvature rounds: a 2D B-spline curve cusp (SmBSplineCurve test 13)
report-fillet-edge-tangent-at-one-end-into-cylinder SMLib's check refuses the round that fades out at the edge's tangent end: internal poles, coincident control points, cusps
report-fillet-edge-tangent-at-one-end-into-cylinder SMLib refuses the round as larger than the faces beside its edges allow (SMLIB_ERR_NO_ROOM): it fades to nothing at the edge's tangent end, on the wall SMLib now gets back as its cylinder