diff --git a/solidcore/crates/solidcore-kernel/src/query/face_normal_angle.rs b/solidcore/crates/solidcore-kernel/src/query/face_normal_angle.rs index b6a3cb8941d..156f842ee55 100644 --- a/solidcore/crates/solidcore-kernel/src/query/face_normal_angle.rs +++ b/solidcore/crates/solidcore-kernel/src/query/face_normal_angle.rs @@ -121,6 +121,10 @@ fn interval( SurfaceKind::BSpline(_) | SurfaceKind::SurfOfExtrusion(_) => { net_interval(k, b, f, fu, surface, d) } + // a revolution's cone holds its normal lines, not one sense: its generator can turn + // back inside a ball, so the surface there can be two sheets facing apart, and one + // point's outward normal does not decide the sense of the rest + SurfaceKind::SurfOfRevolution(_) => Ok(None), _ => closed_form_interval(k, b, f, fu, surface, d), } } diff --git a/solidcore/crates/solidcore-kernel/src/surface/normal_cone.rs b/solidcore/crates/solidcore-kernel/src/surface/normal_cone.rs index b6dc0eeba7e..89a0027112a 100644 --- a/solidcore/crates/solidcore-kernel/src/surface/normal_cone.rs +++ b/solidcore/crates/solidcore-kernel/src/surface/normal_cone.rs @@ -10,7 +10,7 @@ //! //! | Rust | Kind | //! |------------------------------------|-----------------------------------------------| -//! | [`Surface::normal_cone_in_ball`] | plane, cylinder, cone, sphere, ring torus, ball patch | +//! | [`Surface::normal_cone_in_ball`] | plane, cylinder, cone, sphere, ring torus, ball patch, revolution | //! | `NormalCone::angle_interval` (crate) | the normals' angles to a direction | //! | [`intersection_reach`] | a curve with a hull (`curve/proximity.rs`) | //! | [`crossing_line_in_ball`] | the two surfaces' crossing over a ball | @@ -36,6 +36,27 @@ //! plane, `u` the unit radial direction. `|u(p) - u(m)| <= phi` as above, so //! `|(p - C(p)) - (m - C(m))| <= rho + R phi`, seen from the origin within the `asin` of //! that over `|m - C(m)|`. +//! - surface of revolution about `(o, a)`: the turn of its generator `G` about the axis, +//! so its normal at a point `p` lies in `p`'s meridian plane, `n_r rho(p) + n_a a` with +//! `rho(p)` the unit radial direction of `p` and `(n_r, n_a) = (t_a, -t_r)` the generator's +//! tangent `t` at the point it turned from, read in that point's own meridian frame +//! (`t_r = rho . t`, `t_a = a . t`; the part of `t` around the axis drops out). The map +//! of a point to its distance from the axis and its height moves no two points apart, so +//! a generator piece (its ball, [`Curve::piece_ball`](crate::curve::curve::Curve)) whose +//! image lies farther than that from the image of `m` holds no point turned into the +//! ball and is dropped; the others are halved while larger than the ball. On a piece +//! whose ball of radius `s` about `c` stays clear of the axis, `rho` lies within +//! `psi = asin(s / |q(c)|)` of `rho(c)`, and a unit tangent within `beta` of its tangent +//! cone's axis `t0` ([`Curve::tangent_cone`]) gives a `(t_r, t_a)` within `psi + beta` of +//! `(rho(c) . t0, a . t0)` (each a chord below its angle), so its direction lies within +//! `asin((psi + beta) / |(rho(c) . t0, a . t0)|)` of that pair's. The pieces' meridian +//! normals, as lines, lie within `alpha` of the first's; a point of the ball lies within +//! `phi = asin(rho / |q(m)|)` of `m` about the axis (as for the cone), and turning about +//! the axis by at most `phi` moves a direction by at most `phi`, so the normals lie within +//! `alpha + phi` of the first meridian normal set in `m`'s meridian plane. A ball or a +//! reaching piece that reaches the axis, a generator without a hull or evaluated beyond +//! it, a piece whose tangent cone is not known or whose meridian direction is not bounded +//! and cannot be halved, or more than [`REVOLUTION_PIECES`] pieces, has none. //! - linear sweep, read only by the seam-strip proof ([`seam_strip_width`], `#19680`; //! every other proof leaves a sweep without a cone, as upstream: building it costs a //! walk of the generator per ball): `S(u, t) = A(u) + t D(u)`, `t` in `0..=1`, `D = B - A`: an extrusion @@ -85,9 +106,9 @@ //! most [`MAX_PIECES`] pieces in all. Where a piece no larger than `rho` proves no angle //! (the surfaces tangent there, or a kind without a closed form), nothing is proved. //! -//! Deviations: not a port. Surfaces of other kinds (B-spline, extrusion, revolution, -//! offset, blend, a torus without a hole, a ball patch whose net is not certified on its -//! sphere) have no cone here, and nothing is proved for their curves, except a linear +//! Deviations: not a port. Surfaces of other kinds (B-spline, extrusion, offset, blend, +//! a torus without a hole, a ball patch whose net is not certified on its sphere) have no +//! cone here, and nothing is proved for their curves, except a linear //! sweep in the seam-strip proof. No recursion: the pieces wait on a stack bounded by [`MAX_PIECES`]. use crate::curve::curve::Curve; @@ -104,6 +125,7 @@ use crate::nurbs::math::{CPoint, NOW, NOZ}; use crate::nurbs::srf_basic::Surface as NlSurface; use crate::surface::ball_patch::{BallPatch, ON_SPHERE_REL_TOL}; use crate::surface::surf_of_extrusion::p1::SurfOfExtrusion; +use crate::surface::surf_of_revolution::p1::SurfOfRevolution; use crate::surface::surface::p2::SurfaceKind; use crate::surface::surface::Surface; @@ -122,6 +144,10 @@ pub const FINE: f64 = 0.02; /// The most curve pieces the normal cone of a linear sweep reads (module docs). pub const EXTRUSION_PIECES: usize = 256; +/// The most generator pieces the normal cone of a surface of revolution reads (module +/// docs). +pub const REVOLUTION_PIECES: usize = 256; + /// How many times a surface in its STEP parameterisation is unwrapped. const STEP_DEPTH: usize = 8; @@ -375,13 +401,144 @@ fn sphere_cone(c: Vec3, centre: Vec3, radius: f64) -> Option { cone_about(v, seen_within(radius, v.length(), e)?) } +/// The cone of the surface of revolution `r` over the ball of `radius` about `centre` +/// (module docs, "surface of revolution"): its generator's pieces whose meridian images +/// reach the ball's are read by their tangent cones, combined as lines, and turned by the +/// angle the ball is seen within about the axis. `None` where the ball reaches the axis, +/// the generator has no hull or is evaluated beyond it, a reaching piece reaches the axis +/// or has no bounded meridian direction and cannot be halved, no piece reaches the ball, +/// the pieces run out, or the cone is a quarter turn or more. +fn revolution_cone(r: &SurfOfRevolution, centre: Vec3, radius: f64) -> Option { + if r.is_out_of_bounds_enabled() { + return None; + } + let generator = r.gen_curve()?; + let (o, a) = (r.position.origin(), r.position.z_axis()); + // the part of `w - o` across the axis, and its height along it + let split = |w: Vec3| { + let v = w - o; + let h = v.dot(a); + (v - a * h, h) + }; + let (q, height) = split(centre); + let reach = q.length(); + let e = gamma(16) * (centre.length() + o.length() + reach + radius); + // the angle about the axis between the ball's points and its centre + let phi = seen_within(radius, reach, e)?; + let radial = q / reach; + let domain = generator.natural_interval(); + let cuts = generator + .hull_breaks(domain.min(), domain.max()) + .ok() + .flatten()?; + let mut stack: Vec<(f64, f64)> = cuts + .windows(2) + .filter_map(|w| match *w { + [lo, hi] if lo < hi => Some((lo, hi)), + _ => None, + }) + .collect(); + // each reaching piece's meridian normal, as a unit (radial, axial) pair, and the angle + // within which every normal of the piece lies of it + let mut seen: Vec<([f64; 2], f64)> = Vec::new(); + let mut tried = 0_usize; + while let Some((lo, hi)) = stack.pop() { + tried += 1; + if tried > REVOLUTION_PIECES { + return None; + } + let (c, s) = generator.piece_ball(lo, hi).ok().flatten()?; + let (qc, hc) = split(c); + let rc = qc.length(); + let err = gamma(16) * (c.length() + o.length() + rc + s + e); + // the meridian image (distance from the axis, height) moves no two points apart + let gap = det::hypot(rc - reach, hc - height) - s - radius - 2.0 * err; + if gap.is_nan() { + return None; + } + if gap > 0.0 { + continue; + } + let mid = 0.5 * (lo + hi); + let halves = lo < mid && mid < hi; + if s > radius && halves { + stack.push((mid, hi)); + stack.push((lo, mid)); + continue; + } + match piece_meridian_normal(generator, lo, hi, a, qc, rc, s, err) { + Some(n) => seen.push(n), + None if halves => { + stack.push((mid, hi)); + stack.push((lo, mid)); + } + None => return None, + } + } + // every meridian normal line lies within `alpha` of the first piece's + let &(first, _) = seen.first()?; + let mut alpha = 0.0_f64; + for &(other, half) in &seen { + let between = first[0] * other[1] - first[1] * other[0]; + let along = first[0] * other[0] + first[1] * other[1]; + let angle = det::atan2(between.abs(), along.abs()); + alpha = alpha.max(angle + half); + } + // the normal at a point of the ball is its meridian normal in that point's meridian + // plane, turned from the centre's about the axis by at most `phi` + let half = (alpha + phi) * (1.0 + gamma(8)) + gamma(8); + if !(half.is_finite() && half < 0.5 * std::f64::consts::PI) { + return None; + } + cone_about(radial * first[0] + a * first[1], half) +} + +/// The meridian normal of the generator's piece over `[lo, hi]`, whose ball holds no point +/// nearer the axis `a` than `rc - s` (`qc` its centre's part across the axis, `rc` its +/// length, `err` the rounding of both): a unit (radial, axial) pair and the angle within +/// which every normal of the piece lies of it (module docs, "surface of revolution"). +/// `None` where the piece reaches the axis, has no tangent cone, or its meridian direction +/// is not bounded. +#[allow(clippy::too_many_arguments)] +fn piece_meridian_normal( + generator: &Curve, + lo: f64, + hi: f64, + a: Vec3, + qc: Vec3, + rc: f64, + s: f64, + err: f64, +) -> Option<([f64; 2], f64)> { + // the radial directions of the piece's points lie within `psi` of the centre's + let psi = seen_within(s, rc, err)?; + let tc = generator.tangent_cone(lo, hi).ok().flatten()?; + let radial = qc / rc; + let (tr, ta) = (tc.axis.dot(radial), tc.axis.dot(a)); + let len = det::hypot(tr, ta); + // a unit tangent `t` within `beta` of the axis: `(rho . t, a . t)` lies within `psi + + // beta` (chords below the angles) of `(radial . axis, a . axis)` + let off = (psi + tc.half_angle) * (1.0 + gamma(8)) + gamma(8) + err; + let room = len - err; + if !(off.is_finite() && room.is_finite() && room > 0.0 && off < room) { + return None; + } + let half = det::asin(off / room) * (1.0 + gamma(8)) + gamma(8); + if !(half.is_finite() && half < 0.5 * std::f64::consts::PI) { + return None; + } + // the tangent turned a quarter turn in the meridian plane + Some(([ta / len, -tr / len], half)) +} + impl Surface { /// A cone holding the normal line of this surface at every point of it within - /// `radius` of `centre` (module docs): a plane, cylinder, cone, sphere, ring torus or - /// ball patch (its sphere, where its net is certified on it), in closed form with its - /// rounding. `None` for another kind, a ball patch whose net is not certified on its - /// sphere, a ball that reaches the surface's axis, centre or apex height, or a cone of a - /// quarter turn or more. + /// `radius` of `centre` (module docs): a plane, cylinder, cone, sphere, ring torus, + /// ball patch (its sphere, where its net is certified on it) or surface of revolution + /// (from its generator's tangent cones), in closed form with its rounding. `None` for + /// another kind, a ball patch whose net is not certified on its sphere, a ball that + /// reaches the surface's axis, centre or apex height, a generator whose pieces near the + /// ball are not bounded, or a cone of a quarter turn or more. #[must_use] pub(crate) fn normal_cone_in_ball(&self, centre: Vec3, radius: f64) -> Option { if !(radius.is_finite() && radius >= 0.0 && centre.length().is_finite()) { @@ -435,6 +592,7 @@ impl Surface { let spread = radius + major * phi * (1.0 + gamma(4)); cone_about(from_circle, seen_within(spread, from_circle.length(), e)?) } + SurfaceKind::SurfOfRevolution(r) => revolution_cone(r, centre, radius), _ => None, } } @@ -1009,6 +1167,166 @@ mod tests { .is_none()); } + /// `generator` (in the plane y = 0) revolved once about z. + fn revolved(k: &Kernel, generator: BSplineCurve) -> Surface { + use crate::surface::surf_of_revolution::SurfOfRevolution; + let rev = SurfOfRevolution::create_canonical( + k, + Curve::from(generator), + Point3::ZERO, + Vec3::Z, + false, + ) + .unwrap(); + Surface::from(rev) + } + + /// The half circle r `minor` about `(major, 0, 0)` in the plane y = 0 from its bottom out + /// to its top, as two quadratic arcs (a generator of two pieces, the knot at its outermost + /// point). + fn half_circle(major: f64, minor: f64) -> BSplineCurve { + use crate::geom::axis2_placement::Axis2Placement; + use crate::nurbs::b_spline_curve::NurbCircleParam; + let frame = Axis2Placement::new(Point3::new(major, 0.0, 0.0), Vec3::X, Vec3::Z); + BSplineCurve::create_circle_segment_min_arcs( + 3, + &frame, + minor, + -90.0, + 90.0, + NurbCircleParam::Quadratic, + 2, + ) + .unwrap() + } + + /// The exact normal lines of two surfaces of revolution at points inside a ball lie in the + /// ball's cone: the half circle r 1 about (3, 0, 0) revolved (the torus of major radius + /// 3), at a generic point and at the knot between its generator's two arcs (the ball + /// spans both), and the half circle r 1 about the origin revolved (the unit sphere), at a + /// point 0.2 from the axis. A ball reaching the axis has none. + #[test] + fn a_surface_of_revolution_s_normal_cone_holds_the_normals_in_its_ball() { + type Point = dyn Fn(f64, f64) -> (Vec3, Vec3); + let k = Kernel::default(); + let torus = revolved(&k, half_circle(3.0, 1.0)); + let sphere = revolved(&k, half_circle(0.0, 1.0)); + assert!(matches!(torus.kind(), SurfaceKind::SurfOfRevolution(_))); + assert!(matches!(sphere.kind(), SurfaceKind::SurfOfRevolution(_))); + let on_torus = |u: f64, v: f64| { + let radial = Vec3::new(det::cos(u), det::sin(u), 0.0); + let n = radial * det::cos(v) + Vec3::Z * det::sin(v); + (radial * 3.0 + n, n) + }; + let on_sphere = |u: f64, v: f64| { + let n = Vec3::new( + det::cos(v) * det::cos(u), + det::cos(v) * det::sin(u), + det::sin(v), + ); + (n, n) + }; + let radius = 0.05; + // (surface, its points, the ball's centre's (u, v), whether the cone is checked as + // no idle bound): near the axis the turn about it bounds a normal that barely turns + let near_axis = det::acos(0.2); + let cases: [(&Surface, &Point, [f64; 2], bool); 3] = [ + (&torus, &on_torus, [0.7, 0.3], true), + (&torus, &on_torus, [0.7, 0.0], true), + (&sphere, &on_sphere, [0.7, near_axis], false), + ]; + for (surface, point, [u0, v0], tight) in cases { + let (m, _) = point(u0, v0); + let held = surface + .normal_cone_in_ball(m, radius) + .unwrap_or_else(|| panic!("no cone at ({u0}, {v0})")); + let mut widest = 0.0_f64; + let mut inside = 0; + for i in -40..=40 { + for j in -40..=40 { + let (p, n) = point(u0 + 0.004 * f64::from(i), v0 + 0.002 * f64::from(j)); + if (p - m).length() > radius { + continue; + } + inside += 1; + let off = line_angle(held.axis, n); + assert!(off <= held.half_angle, "{off} > {}", held.half_angle); + widest = widest.max(off); + } + } + assert!(inside > 100, "{inside}"); + if tight { + assert!(widest > 0.2 * held.half_angle, "{widest} {held:?}"); + } + } + // a ball reaching the axis has none: about a point 0.03 from it, and about the pole + let (close, _) = on_sphere(0.7, det::acos(0.03)); + assert!(sphere.normal_cone_in_ball(close, radius).is_none()); + assert!(sphere.normal_cone_in_ball(Vec3::Z, radius).is_none()); + assert!(torus.normal_cone_in_ball(Point3::ZERO, 0.1).is_none()); + } + + /// A generator whose tangent has a part around the axis: the line `(1, s, s)`, `s` in + /// `[-1, 1]`, revolved about z is the hyperboloid of one sheet `x^2 + y^2 - z^2 = 1`, its + /// normal along `(x, y, -z)`. Its exact normal lines at points inside a ball lie in the + /// ball's cone, and the cone is no idle bound. + #[test] + fn a_skew_line_s_hyperboloid_has_the_normals_in_its_ball_in_its_cone() { + let k = Kernel::default(); + let line = BSplineCurve::create_line_segment( + 3, + Point3::new(1.0, -1.0, -1.0), + Point3::new(1.0, 1.0, 1.0), + ) + .unwrap(); + let rev = { + use crate::surface::surf_of_revolution::SurfOfRevolution; + SurfOfRevolution::create_canonical(&k, Curve::from(line), Point3::ZERO, Vec3::Z, false) + .unwrap() + }; + let hyperboloid = Surface::from(rev); + assert!(matches!( + hyperboloid.kind(), + SurfaceKind::SurfOfRevolution(_) + )); + // the line's point at `s` turned by `u` about z, and the normal there + let point = |u: f64, s: f64| { + let (c, n) = (det::cos(u), det::sin(u)); + let p = Vec3::new(c - s * n, n + s * c, s); + (p, Vec3::new(p.x, p.y, -p.z)) + }; + let radius = 0.05; + for [u0, s0] in [[0.7, 0.4], [2.0, -0.6], [0.0, 0.0]] { + let (m, _) = point(u0, s0); + let held = hyperboloid + .normal_cone_in_ball(m, radius) + .unwrap_or_else(|| panic!("no cone at ({u0}, {s0})")); + let mut widest = 0.0_f64; + let mut inside = 0; + for i in -40..=40 { + for j in -40..=40 { + let (p, n) = point(u0 + 0.002 * f64::from(i), s0 + 0.002 * f64::from(j)); + if (p - m).length() > radius { + continue; + } + inside += 1; + let off = line_angle(held.axis, n); + assert!( + off <= held.half_angle, + "({u0}, {s0}): {off} > {}", + held.half_angle + ); + widest = widest.max(off); + } + } + assert!(inside > 100, "{inside}"); + assert!( + widest > 0.2 * held.half_angle, + "({u0}, {s0}): {widest} {held:?}" + ); + } + } + /// A plane through a unit sphere at the height `h` crosses it at `acos h`: the /// circle's reach is the tolerance over `sqrt(1 - h^2)`, enclosed from above and /// within a few per cent. A plane 1e-9 inside the sphere's top is tangent to it as diff --git a/solidcore/crates/solidcore-ops/tests/blend.rs b/solidcore/crates/solidcore-ops/tests/blend.rs index 5b66e113424..df432b7e87a 100644 --- a/solidcore/crates/solidcore-ops/tests/blend.rs +++ b/solidcore/crates/solidcore-ops/tests/blend.rs @@ -14652,6 +14652,101 @@ fn a_cut_sphere_s_rim_rounded_past_a_notch_beside_its_seam_builds_as_turned_else } } +/// SolidCraft #25576's cones (stress-corpus S2989 and ten more): a frustum r 2 at z = 0 to r 1 at +/// z = 3, a notch r 0.12 from z = 2 up through the top's rim, its axis at (cos a, sin a), then +/// R0.1 on the rim (its smooth chain) and on the notch's edge on the top, the notch built as +/// SolidCraft's cylinder is (its circle from 0.6131 rad). At a = 6.877 degrees the notch reaches +/// 0.00027 across the cone's seam (y = 0): the end of the notch edge's round on the cone runs +/// across the seam, traced in two pieces that the seam's join refused to prove one curve, since +/// the round's surface of revolution had no normal cone ("failure to calculate intersection of +/// fillet surface with side face"); 20 and 45 degrees further on it built. The frustum is a +/// solid of revolution about z, so turned by t the part is the first one turned by t: all three +/// build valid, to each other's volume. +/// +/// Hand-worked: the frustum is pi h (R^2 + R r + r^2) / 3 = 7 pi; the notch takes from it the +/// cylinder r 0.12 about the axis at distance 1 for z in [2, 2.64] (the cone's radius 2 - z/3 is +/// 1.12 there), and above that the lens of the notch's disc with the cone's, 0.041661962815 +/// over [2, 3] by quadrature: 21.949486612313 before the round. The round takes off about its +/// section r^2 (cot(t/2) - (pi - t)/2) along the rim, t = 90 + atan(1/3) degrees between the +/// top and the side (0.000962363), times the rim's arc left by the notch (2 pi - 2 asin(0.06)), +/// and (1 - pi/4) r^2 along the notch's edge on the top (0.12 * 2 acos(0.06)): 0.0067093, the +/// corners where they meet not counted; the built round takes off 0.0065991. +#[test] +#[allow(clippy::disallowed_methods)] // the test's turn of the picks, not ops geometry +fn a_cones_rim_rounded_past_a_notch_beside_its_seam_builds_as_turned_elsewhere() { + all_fixes(); + let notch = |axis: [f64; 2]| { + let circle = Curve::Arc { + center: [axis[0], axis[1], 2.0], + axis: [0.0, 0.0, 1.0], + reference: [1.0, 0.0, 0.0], + radius: 0.12, + start: 0.6131, + end: 0.6131 + 2.0 * PI, + }; + extrude( + &[circle], + [0.0, 0.0, 1.0], + 1.0, + 0.0, + DraftCorner::default(), + true, + ) + .unwrap() + }; + let before = 7.0 * PI - 0.041_661_962_815; + let mut volumes = Vec::new(); + for turn in [20.0_f64, 45.0, 0.0] { + // S2989's own coordinates at no turn, the same turned about z otherwise + let (c, s) = if turn == 0.0 { + (1.0, 0.0) + } else { + let t = turn.to_radians(); + (det::cos(t), det::sin(t)) + }; + let at = |[x, y]: [f64; 2]| [c * x - s * y, s * x + c * y]; + let axis = at([0.992_806_241_411_158_9, 0.119_732_063_437_692_67]); + let pick = at([0.873_669_492_441_819_9, 0.105_364_215_825_169_55]); + let part = solidcore_ops::boolean::boolean( + make_cone([0.0; 3], 2.0, 1.0, 3.0).unwrap(), + notch(axis), + solidcore_ops::BooleanOp::Subtract, + ) + .unwrap() + .unwrap(); + assert!( + (volume(&part) - before).abs() < 1e-9, + "turned {turn}: {}", + volume(&part) + ); + let rim = at([0.0, -1.0]); + let picks = [ + edge_at(&part, [rim[0], rim[1], 3.0]), + edge_at(&part, [pick[0], pick[1], 3.0]), + ]; + let edges = tangent_chain(&part, None, &picks, &[], PI / 90.0) + .unwrap() + .members; + blend(&part, Blend::Round, &edges, &[0.1], &[], Corners::Filled) + .unwrap_or_else(|e| panic!("turned {turn}: {e:?}")); + assert!( + part.check(CheckLevel::All).unwrap().is_valid(), + "turned {turn}" + ); + assert!(part.is_manifold_solid().unwrap(), "turned {turn}"); + let v = volume(&part); + let taken = before - v; + assert!( + taken > 0.95 * 0.006_709_3 && taken < 1.05 * 0.006_709_3, + "turned {turn}: the round takes off {taken}" + ); + volumes.push(v); + } + for v in &volumes { + assert!(rel(*v, volumes[0]) < 1e-9, "{volumes:?}"); + } +} + /// A 20 x 20 x 10 block with the corner under V = (0, 0, 10) cut off by the plane /// x + y + z = 10 through V, so four edges meet at V: the top's edges along x and y and the /// cut's edges down the faces x = 0 and y = 0. The cut is the tetrahedron (0, 0, 0), diff --git a/solidcore/crates/solidcore-ops/tests/boolean.rs b/solidcore/crates/solidcore-ops/tests/boolean.rs index efab8ba1c5d..f6aec26f298 100644 --- a/solidcore/crates/solidcore-ops/tests/boolean.rs +++ b/solidcore/crates/solidcore-ops/tests/boolean.rs @@ -2665,14 +2665,16 @@ fn a_round_notch_a_tolerance_over_a_revolved_cones_seam_is_cut() { /// less the r 0.06 notch from z = 999.5 to the top, centred on the r 0.5 circle at the angle /// (0.06 + d) / 0.5 from the seam (the half plane y = 0, x > 0), for d = 2e-5 (the issue's /// script), turned about +z to d = 1e-5, 0 and -1e-5. The notch's wall dips across the round's -/// seam by up to 1.2e-4 between where it meets the seam at the round's foot and higher up the -/// round. Its curve runs within tolerance of the seam from the foot and is kept on the seam -/// there; that run ends where the curve leaves the seam, 3.5e-5 from it, and the boolean puts -/// the run's end vertex on the seam. The step after the merge's cleanup that moves a vertex onto -/// the corner its faces define then moved that vertex 3.5e-5 off the seam, onto the wall's -/// corner with the round, and the round's seam test refused the result (test 36, "Face Near -/// missing Seam"). Each turn is one valid solid of 7 faces, 16 edges and 11 vertices (15 edges -/// and 10 vertices at d = -1e-5). +/// seam by up to 1.5e-4 between where it meets the seam at the round's foot and higher up the +/// round. The cut once kept the stretch of that curve within tolerance of the seam on the seam, +/// as an edge of its own ending at a vertex on the seam where the curve leaves it, 3.5e-5 away. +/// The step after the merge's cleanup that moves a vertex onto the corner its faces define moved +/// that vertex 3.5e-5 off the seam, onto the wall's corner with the round, and the round's seam +/// test refused the result (test 36, "Face Near missing Seam"). The cut is one edge along the +/// notch's wall from the round's foot to where the wall meets the seam's plane again on the +/// round, both ends worked by hand. Each turn is one valid solid of 7 faces, 15 edges and 10 +/// vertices (14 edges and 9 vertices at d = -1e-5, where the notch's foot on the cone is on the +/// seam). /// V = the rounded frustum less the notch: the frustum up to where the round meets the cone and /// the round's torus above it, by Pappus in closed form, less the integral over z of the area /// the notch's disc shares with the profile's circle there (two circles' lens, in closed form), @@ -2736,10 +2738,10 @@ fn a_round_notch_beside_a_rounded_cone_rims_seam_is_cut() { let want = rounded - on_cone - on_round; let cone = include_bytes!("data/issue_22814_rounded_cone.smb"); for (d, counts) in [ - (2e-5, [7, 16, 11]), - (1e-5, [7, 16, 11]), - (0.0, [7, 16, 11]), - (-1e-5_f64, [7, 15, 10]), + (2e-5, [7, 15, 10]), + (1e-5, [7, 15, 10]), + (0.0, [7, 15, 10]), + (-1e-5_f64, [7, 14, 9]), ] { let notch = read(include_bytes!("data/issue_22814_notch.smb")); // the turn about +z that takes the notch's centre from d = 2e-5 to d (`p' = p·A`) @@ -2756,6 +2758,36 @@ fn a_round_notch_beside_a_rounded_cone_rims_seam_is_cut() { .unwrap() .unwrap(); one_valid_solid_of_volume(&cut, counts, want, 1e-9); + // the notch's wall (centre `at`, radius r) meets the seam's plane on the round at `x`, + // where the round's torus is at height `top[2]`; the cut runs there from the round's foot + let at = [c[0] * co - c[1] * s, c[0] * s + c[1] * co]; + let x = at[0] - (r * r - at[1] * at[1]).sqrt(); + let foot = [cone_at(zt), 0.0, zt]; + let top = [x, 0.0, zc + (round * round - (x - rc) * (x - rc)).sqrt()]; + let (record, order) = cut.to_record().unwrap(); + let fractions: Vec = (0..=8).map(|i| f64::from(i) / 8.0).collect(); + let samples = cut.edge_samples(&order, &fractions).unwrap(); + let apart = |p: [f64; 3], q: [f64; 3]| { + ((p[0] - q[0]).powi(2) + (p[1] - q[1]).powi(2) + (p[2] - q[2]).powi(2)).sqrt() + }; + let cuts = record + .edges + .iter() + .zip(&samples) + .filter(|(edge, points)| { + let on_wall = points + .iter() + .all(|p| (det::hypot(p[0] - at[0], p[1] - at[1]) - r).abs() <= edge.tolerance); + let tol = |end: usize| record.vertices[edge.vertices[end] as usize].tolerance; + let ends = [points[0], points[points.len() - 1]]; + let joins = |a, b| apart(ends[0], a) <= tol(0) && apart(ends[1], b) <= tol(1); + on_wall && (joins(foot, top) || joins(top, foot)) + }) + .count(); + assert_eq!( + cuts, 1, + "d {d}: edges on the notch's wall from the round's foot to its seam" + ); } }