diff --git a/ROADMAP.md b/ROADMAP.md index 327201486..2c06fcd4f 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -347,9 +347,10 @@ completed drawings publish atomically. The browser still exports synchronously. - Associative shell-face sketches require a unique certified planar source. Nested shell attachments and ambiguous coincident sources remain unsupported. -- G2 fillets: exact quintic blends of straight edges between planar faces, with one perpendicular - planar end face at each end. The UI and `solid.fillet` expose `curvature` (alias `g2`); the size - is an equivalent circular setback. Curved faces/edges, sloped ends and corner junctions remain unsupported. +- G2 fillets: exact quintic blends of straight edges between planar faces, with one planar + end face at each end, including sloped ends (#2708). The UI and `solid.fillet` expose + `curvature` (alias `g2`); the size is an equivalent circular setback. Curved faces/edges, + multi-face ends and corner junctions remain unsupported. - Fillets and chamfers: constant, variable-radius and chord-length fillets; a radius per edge, rounds next to earlier rounds of another radius, and smooth corner patches where three rounds meet (a sphere for equal radii); perpendicular corner fillets after a closed ring of earlier diff --git a/crates/engine/src/cmd/edit_tests.rs b/crates/engine/src/cmd/edit_tests.rs index 9e78e2a14..d4acb82ae 100644 --- a/crates/engine/src/cmd/edit_tests.rs +++ b/crates/engine/src/cmd/edit_tests.rs @@ -52,6 +52,58 @@ fn g2_fillet_survives_edit_undo_and_reopen() { assert!(s.execute("solid.fillet", &json!({"edges": [[0, 0, 10]], "radius": 2, "type": "curvature", "fit": "reduce"})).is_err()); } +#[test] +fn redefining_a_fillet_as_g2_on_sloped_ends_survives_undo_and_reopen() { + use solidcraft_doc::{Document, FeatureKind, FilletStyle}; + let mut s = Session::default(); + run(&mut s, "sketch.create", json!({"plane": "XY"})); + run(&mut s, "sketch.line", json!({"points": [[0, 0], [40, 0], [30, 20], [10, 20]], "closed": true})); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "solid.extrude", json!({"distance": 20})); + run(&mut s, "solid.fillet", json!({"edges": [[20, 0, 20]], "radius": 5, "name": "Fillet1"})); + let original = s.doc.clone(); + let id = s.doc.find_feature("Fillet1").unwrap().id; + // #2708: changing an existing constant fillet to Curvature (G2) in Edit Feature. + run( + &mut s, + "timeline.redefine", + json!({"feature": id, "command": "solid.fillet", "params": {"edges": [[20, 0, 20]], "radius": "5", "type": "curvature"}}), + ); + let f = s.doc.find_feature("Fillet1").unwrap(); + assert_eq!(f.id, id); + assert!(matches!(&f.kind, FeatureKind::Fillet { style: FilletStyle::Curvature, .. })); + // Use a fixed fine chord tolerance for the analytic comparison; Measure's + // adaptive display tolerance is coarser and need not satisfy this bound. + let volume = |s: &Session| { + let bodies = &s.model.state().bodies; + assert_eq!(bodies.len(), 1); + bodies.first().unwrap().body.tessellate(0.0001).unwrap().measure().volume + }; + let expected = |r: f64| 12000.0 - 40.0 * 113.0 / 756.0 * r.powi(2) + 13711.0 / 486486.0 * r.powi(3); + let actual = volume(&s); + assert!((actual - expected(5.0)).abs() < 0.05, "{actual} vs {}", expected(5.0)); + let edited = s.doc.clone(); + run(&mut s, "edit.undo", json!({})); + assert_eq!(s.doc, original); + run(&mut s, "edit.redo", json!({})); + assert_eq!(s.doc, edited); + let mut reopened = Session::new(Document::from_json(&s.doc.to_json()).unwrap()); + assert!((volume(&reopened) - expected(5.0)).abs() < 0.05); + run(&mut reopened, "timeline.edit", json!({"feature": id, "set": {"radius": "3"}})); + assert!((volume(&reopened) - expected(3.0)).abs() < 0.05); + let before = reopened.doc.clone(); + assert!( + reopened + .execute( + "timeline.redefine", + &json!({"feature": id, "command": "solid.fillet", "params": {"edges": [[20, 0, 20]], "radius": 100, "type": "curvature"}}) + ) + .is_err() + ); + assert_eq!(reopened.doc, before); + assert!((volume(&reopened) - expected(3.0)).abs() < 0.05); +} + #[test] fn redefining_a_blend_replaces_persistent_edge_selection_and_survives_resize() { let mut s = Session::default(); diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs index 4d5c01f8d..ec9d9748e 100644 --- a/crates/engine/src/cmd/features.rs +++ b/crates/engine/src/cmd/features.rs @@ -36,7 +36,7 @@ pub static COMMANDS: &[CommandSpec] = &[ .at("SOLID", "MODIFY") .icon("fillet") .key("F") - .params("edges: [[x,y,z] point on edge | {body, index}]; radius: expr; body?; type?: constant|curvature (alias g2: curvature-continuous quintic between planar faces along straight edges, one perpendicular planar end face at each end; radius sets the equivalent circular setback, not constant curvature; exact fit only)|chord (radius is the width across)|variable (radius at the end nearest start, radius2 at the other; one end may be 0, where the round closes to the corner)|spline (the radius along the edge is a clamped B-spline from the end nearest start: radius is its first control radius, radii the rest, knots its knot vector from 0 to 1; one end may be 0); radius2?; start?: [x,y,z]; radii?: [expr, …]; knots?: [number, …]; mid?: [[position 0…1 from start, radius expr], …] (variable radii between the ends); smooth?: bool (smooth instead of straight between the radii); corner?: rolling|g2 (rolling, the default: the corners where three blended edges meet are pieces of the rolling ball's sphere, tangent to each blend; g2: a patch repeating each blend's surface up to the second order, constant-radius exact fillets only, refused where it cannot be built); fit?: exact|reduce|taper (exact, the default: a radius too large for the geometry around the edges is refused; reduce: where it doesn't fit it is reduced to the largest that does, one radius for edges sharing a face; the smallest used is returned as `radius_applied`, each edge's as `radii_applied: [{edge: [x,y,z], radius}]`, and noted in the feature's warning; taper: along each straight edge between planar faces where it doesn't fit, the radius follows the room there as a smooth spline, down to 0 where a face closes to a point, noted in the feature's warning)"), + .params("edges: [[x,y,z] point on edge | {body, index}]; radius: expr; body?; type?: constant|curvature (alias g2: curvature-continuous quintic between planar faces along straight edges, one planar end face at each end (including sloped ends); radius sets the equivalent circular setback, not constant curvature; exact fit only)|chord (radius is the width across)|variable (radius at the end nearest start, radius2 at the other; one end may be 0, where the round closes to the corner)|spline (the radius along the edge is a clamped B-spline from the end nearest start: radius is its first control radius, radii the rest, knots its knot vector from 0 to 1; one end may be 0); radius2?; start?: [x,y,z]; radii?: [expr, …]; knots?: [number, …]; mid?: [[position 0…1 from start, radius expr], …] (variable radii between the ends); smooth?: bool (smooth instead of straight between the radii); corner?: rolling|g2 (rolling, the default: the corners where three blended edges meet are pieces of the rolling ball's sphere, tangent to each blend; g2: a patch repeating each blend's surface up to the second order, constant-radius exact fillets only, refused where it cannot be built); fit?: exact|reduce|taper (exact, the default: a radius too large for the geometry around the edges is refused; reduce: where it doesn't fit it is reduced to the largest that does, one radius for edges sharing a face; the smallest used is returned as `radius_applied`, each edge's as `radii_applied: [{edge: [x,y,z], radius}]`, and noted in the feature's warning; taper: along each straight edge between planar faces where it doesn't fit, the radius follows the room there as a smooth spline, down to 0 where a face closes to a point, noted in the feature's warning)"), CommandSpec::new("solid.chamfer", "Chamfer", chamfer).at("SOLID", "MODIFY").icon("chamfer").params("edges: [[x,y,z] | {body, index}]; distance: expr; distance2?: expr (along the second face) | angle?: expr (from the first face); flip?: bool (which face is first: by default the one facing up most); body?"), CommandSpec::new("solid.combine", "Combine", combine).at("SOLID", "MODIFY").icon("combine").params("target: body; tools: [body]; operation?: join|cut|intersect; keep_tools?: bool; new_component?: bool (the result goes into a new component; component_name?)"), CommandSpec::new("solid.pattern.rectangular", "Rectangular Pattern", pattern_rect) diff --git a/crates/kernel/src/blend.rs b/crates/kernel/src/blend.rs index 60f349e4f..66a2d3d17 100644 --- a/crates/kernel/src/blend.rs +++ b/crates/kernel/src/blend.rs @@ -1,9 +1,9 @@ //! Edge fillets and chamfers as a local B-rep operation. //! //! Supported locally: a straight edge between two planar faces whose two end vertices each -//! touch exactly one more planar face. Fillets require perpendicular end faces; chamfers -//! intersect their offset rails with the end planes, including sloped walls. The end faces get an arc -//! (fillet) or a line (chamfer) at the corner, and a cylindrical (or planar) face is inserted. +//! touch exactly one more planar face. The offset rails intersect the end planes, including +//! sloped walls. The end faces get an arc (round fillet), a quintic (G2 fillet) or a line +//! (chamfer) at the corner, and a cylindrical, quintic or planar face is inserted. //! The result is exact. Edges meeting at an already blended corner, curved edges and other //! configurations are reported as not supported yet. @@ -468,10 +468,8 @@ fn blend_geometry( [x, y] if shared(i1, y).is_some() && shared(i2, x).is_some() => (y, x), _ => return Err(unsupported("each end of the edge must touch exactly one more face")), }; - if shape == Shape::Curvature - && (others.len() != 1 || faces.get(ga).and_then(|f| plane_normal(f)).is_none_or(|n| n.dot(d).abs() < 1.0 - 1e-12)) - { - return Err(unsupported("G2 fillets require one planar end face perpendicular to the edge at each end")); + if shape == Shape::Curvature && (others.len() != 1 || faces.get(ga).and_then(|f| plane_normal(f)).is_none()) { + return Err(unsupported("G2 fillets require one planar end face at each end")); } // An end face that is not a plane, or meets the side faces in curves: the rails end where // they reach its surface, and the blend's end is where its surface meets the end face @@ -879,6 +877,11 @@ fn blend_geometry( let rail_a = used_up(i1, &end0.ca, &end1.ca)?; let rail_b = used_up(i2, &end0.cb, &end1.cb)?; let vertex = |c: &Option, p: Vec3| c.clone().unwrap_or_else(|| builder::vertex(p3(p))); + // Projection along the edge is affine, so projecting the six controls gives + // the exact quintic on an oblique end plane. The end-plane check above has + // already refused planes parallel to d. Use these same controls for the + // surface below, including where an outward slope extends past the old edge. + let g2_section = |p: Vec3, (o, n): (Vec3, Vec3)| g2_controls(p, t1, t2, s).map(|q| q - d * ((q - o).dot(n) / d.dot(n))); // Corner curves on the end faces (A → B): the blend's cross-section through the end vertex, // slid along the edge onto each end plane (an ellipse arc; the circle itself on a square // end face). @@ -887,7 +890,7 @@ fn blend_geometry( return Ok(builder::line(va, vb)); } if shape == Shape::Curvature { - let controls = g2_controls(p, t1, t2, s); + let controls = g2_section(p, plane); return Ok(builder::bezier(va, vb, controls.into_iter().skip(1).take(4).map(p3).collect())); } let c = centre(p); @@ -1028,8 +1031,17 @@ fn blend_geometry( let blend_face = match shape { Shape::Flat => builder::try_attach_plane(&[wire]).map_err(|e| KernelError::Failed(format!("chamfer face: {e}")))?, Shape::Curvature => { - let controls = g2_controls(p0, t1, t2, s); - let ctrl = controls.into_iter().map(|p| vec![p3(p), p3(p + p1 - p0)]).collect(); + let ([plane0], [plane1]) = (&end0.planes[..], &end1.planes[..]) else { + return Err(unsupported("G2 fillets require one planar end face at each end")); + }; + let (start, end) = (g2_section(p0, *plane0), g2_section(p1, *plane1)); + // A ruled patch between the two sections stays on the quintic's + // extrusion: corresponding controls differ only along d. Positive + // spans throughout their convex hull keep the surface from folding. + if start.iter().zip(&end).any(|(a, b)| !a.is_finite() || !b.is_finite() || (*b - *a).dot(d) <= tol) { + return Err(too_large("the blend is larger than the neighbouring faces")); + } + let ctrl = start.into_iter().zip(end).map(|(a, b)| vec![p3(a), p3(b)]).collect(); let bsp = mt::BSplineSurface::new((mt::KnotVec::bezier_knot(5), mt::KnotVec::bezier_knot(1)), ctrl); let mut surf = mt::Surface::BSplineSurface(bsp); use mt::ParametricSurface3D; @@ -1096,7 +1108,7 @@ fn g2_controls(p: Vec3, t1: Vec3, t2: Vec3, setback: f64) -> [Vec3; 6] { /// Curvature-continuous (G2) fillets between planar faces along straight edges. /// `radius` sets the same setback as a circular fillet; the profile is not circular. -/// Each end must meet one planar face perpendicular to the edge. Unsupported +/// Each end must meet one planar face, which may be oblique to the edge. Unsupported /// configurations are refused, never replaced with a G1 circular fillet. pub fn fillet_g2(body: &Body, edges: &[Vec3], radius: f64) -> Result { body.require_brep("G2 fillet")?; @@ -2694,6 +2706,115 @@ mod tests { } } + #[test] + fn g2_fillet_meets_sloped_planar_ends_with_zero_rail_curvature() { + use mt::{InnerSpace, ParametricCurve, ParametricSurface, SearchParameter}; + // Inward and outward sloping ends, and one square end, in two orientations. + // Outward slopes need end curves beyond the old edge's axial range (#2708). + for plane in [Plane::XY, Plane::XZ] { + for (left, right) in [(-0.5, -0.5), (0.5, 0.5), (0.0, 0.5), (-0.25, 0.5)] { + let region = Region2 { + outer: Loop2::polygon(&[Vec2::ZERO, Vec2::new(40.0, 0.0), Vec2::new(40.0 - right * 20.0, 20.0), Vec2::new(left * 20.0, 20.0)]), + holes: vec![], + }; + let body = crate::extrude(&plane, &[region], 0.0, 20.0).unwrap().pop().unwrap(); + let pick = plane.to_world(Vec2::new(20.0, 0.0)) + plane.normal() * 20.0; + let out = fillet_g2(&body, &[pick], 5.0).unwrap(); + assert!(out.validity().is_empty(), "{:?}", out.validity()); + assert_eq!(out.crossed_face_loops().unwrap(), 0); + assert_eq!(out.face_count(), 7); + let solid = out.deep_copy(); + let face = solid.face_iter().find(|f| matches!(f.surface(), mt::Surface::BSplineSurface(_))).unwrap(); + let surf = face.surface(); + let mut sections = 0; + for edge in face.edge_iter() { + if let mt::Curve::BSplineCurve(c) = edge.curve() { + for k in 0..=20 { + let q = c.subs(f64::from(k) / 20.0); + let uv = plane.to_local(from_p3(q)); + assert!((uv.x - left * uv.y).abs() < 1e-8 || (uv.x + right * uv.y - 40.0).abs() < 1e-8); + let (u, v) = surf.search_parameter(q, None, 100).expect("end curve on the G2 surface"); + assert!((surf.subs(u, v) - q).magnitude() < 1e-8); + } + sections += 1; + } + } + assert_eq!(sections, 2); + // A parameter swap on inversion is allowed; identify the rails by + // the original side planes and check the whole second fundamental form. + for along in [0.1, 0.5, 0.9] { + let mut rails = 0; + for (u, v) in [(0.0, along), (1.0, along), (along, 0.0), (along, 1.0)] { + let q = from_p3(surf.subs(u, v)); + let y = plane.to_local(q).y; + let z = (q - plane.origin).dot(plane.normal()); + let expected = if y.abs() < 1e-8 { + plane.y + } else if (z - 20.0).abs() < 1e-8 { + plane.normal() + } else { + continue; + }; + let n = surf.uder(u, v).cross(surf.vder(u, v)).normalize(); + assert!(n.cross(v3(expected)).magnitude() < 1e-8); + for second in [surf.uuder(u, v), surf.uvder(u, v), surf.vvder(u, v)] { + assert!(second.dot(n).abs() < 1e-8); + } + rails += 1; + } + assert_eq!(rails, 2); + } + // Analytic volume: the removed section has area 113 r² / 756 + // and first moment ∫y dA = 13711 r³ / 486486, obtained by integrating + // the quintic's x(t)y'(t) and x(t)y(t)y'(t). Axial length is 40−(left+right)y. + let removed = 40.0 * 113.0 / 756.0 * 25.0 - (left + right) * 13711.0 / 486486.0 * 125.0; + let expected = 16000.0 - 4000.0 * (left + right) - removed; + let volume = out.tessellate(0.0001).unwrap().measure().volume; + assert!((volume - expected).abs() < 0.05, "{volume} vs {expected}"); + } + } + } + + #[test] + fn g2_fillet_on_concave_edge_meets_caps_sloped_in_both_directions() { + use mt::{ParametricCurve, ParametricSurface, SearchParameter}; + let region = Region2 { + outer: Loop2::polygon(&[ + Vec2::ZERO, + Vec2::new(20.0, 0.0), + Vec2::new(20.0, 10.0), + Vec2::new(10.0, 10.0), + Vec2::new(10.0, 20.0), + Vec2::new(0.0, 20.0), + ]), + holes: vec![], + }; + let body = crate::extrude(&Plane::XY, &[region], 0.0, 20.0).unwrap().pop().unwrap(); + // z -> z + 0.3x + 0.4y tilts both caps while leaving the vertical edge + // and its side planes unchanged. This shear preserves volume. + let body = crate::transform_matrix(&body, [[1.0, 0.0, 0.3, 0.0], [0.0, 1.0, 0.4, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]).unwrap(); + let out = fillet_g2(&body, &[Vec3::new(10.0, 10.0, 17.0)], 2.0).unwrap(); + assert!(out.validity().is_empty(), "{:?}", out.validity()); + assert_eq!(out.crossed_face_loops().unwrap(), 0); + let solid = out.deep_copy(); + let face = solid.face_iter().find(|f| matches!(f.surface(), mt::Surface::BSplineSurface(_))).unwrap(); + let surf = face.surface(); + for edge in face.edge_iter() { + if let mt::Curve::BSplineCurve(c) = edge.curve() { + for k in 0..=20 { + let q = c.subs(f64::from(k) / 20.0); + let height = q.z - 0.3 * q.x - 0.4 * q.y; + assert!(height.abs() < 1e-8 || (height - 20.0).abs() < 1e-8); + let (u, v) = surf.search_parameter(q, None, 100).unwrap(); + assert!(from_p3(surf.subs(u, v)).dist(from_p3(q)) < 1e-8); + } + } + } + let volume = out.tessellate(0.0001).unwrap().measure().volume; + let expected = 6000.0 + 113.0 / 756.0 * 4.0 * 20.0; + assert!((volume - expected).abs() < 0.02, "{volume} vs {expected}"); + } + #[test] fn g2_fillet_refuses_invalid_and_unsupported_geometry() { let body = crate::build::box_solid(Vec3::ZERO, Vec3::new(20.0, 20.0, 20.0)).unwrap(); @@ -2714,17 +2835,6 @@ mod tests { fillet_g2(&boss, &[Vec3::new(40.0, 4.0, 20.0)], 0.8), Err(KernelError::Failed(message)) if message.contains("G2 fillets require one planar end face") )); - for slope in [-0.5, 0.5] { - let region = Region2 { - outer: Loop2::polygon(&[Vec2::ZERO, Vec2::new(40.0, 0.0), Vec2::new(40.0 - slope * 20.0, 20.0), Vec2::new(slope * 20.0, 20.0)]), - holes: vec![], - }; - let sloped = crate::extrude(&Plane::XY, &[region], 0.0, 20.0).unwrap().pop().unwrap(); - assert!(matches!( - fillet_g2(&sloped, &[Vec3::new(20.0, 0.0, 20.0)], 2.0), - Err(KernelError::Failed(message)) if message.contains("G2 fillets require one planar end face") - )); - } } #[test] diff --git a/docs/book/src/guide/features.md b/docs/book/src/guide/features.md index 40485f110..6d91b167b 100644 --- a/docs/book/src/guide/features.md +++ b/docs/book/src/guide/features.md @@ -98,8 +98,9 @@ ball). See the roadmap's known limitations for the rest. **Curvature (G2)** in the Fillet dialog's **Radius Type** blends straight edges between two planar faces with an exact quintic section. The tangent plane and curvature match each side face: curvature goes to zero at both contact rails, unlike a circular G1 fillet. Each end -must meet one planar face perpendicular to the edge. Curved edges/faces, sloped or multi-face -ends, and intersecting corner blends are not supported; they return an error, not a G1 fallback. +must meet one planar face; perpendicular and sloped planar ends are supported. Curved +edges/faces, multi-face ends, and intersecting corner blends are not supported; they return +an error, not a G1 fallback. **Equivalent Radius** sets the same contact setback as a circular fillet (for a right angle, setback equals the entered value); it is not a constant curvature radius. Reduce/Taper to Fit are unavailable for this type.