Skip to main content

brep_kernel/construction/
sweep_round_corners.rs

1//! A sweep of a plane section along straight runs, carried round each corner
2//! by turning it about the corner.
3//!
4//! Each run is the section slid straight along it, ending flat at the corner.
5//! At a corner the section is turned about the line through the corner square
6//! to both runs, from square to the one run to square to the next. That line
7//! lies in the section's plane and may cross the section, so the section is
8//! cut along it and each part revolved by itself; the part outside the corner
9//! fills the wedge the two runs leave open (for a disc, a piece of a ball),
10//! and the part inside it lies where the runs overlap. The pieces are united.
11use crate::{
12    boolean_operation, boolean_operation_nary, extrude_profile_brep, face_from_surface_trimmed, make_plane, revolve_profile_brep, solid_from_polygons,
13    BooleanOperation, BooleanOptions, BrepSolid, KernelRefusal, KernelStage, NurbsCurve, OrRefuse, Vec3, Vec4,
14};
15
16/// The prefix of every refusal of this operation.
17pub const SWEEP_ROUND_CORNERS_REFUSAL: &str = "sweepRoundCorners";
18
19fn input(what: &'static str, message: String) -> KernelRefusal {
20    KernelRefusal::input(KernelStage::Collect, what, format!("{SWEEP_ROUND_CORNERS_REFUSAL}: {message}"))
21}
22
23/// `curve` with every point of it moved by `map`, a rigid motion (which
24/// carries a rational curve pole by pole).
25fn moved(curve: &NurbsCurve, map: &dyn Fn(Vec3) -> Vec3) -> Result<NurbsCurve, KernelRefusal> {
26    let poles = curve.control_points.iter().map(|pole| Vec4::from_point(map(Vec3::new(pole.x / pole.w, pole.y / pole.w, pole.z / pole.w)), pole.w)).collect();
27    NurbsCurve::new(curve.degree, curve.knots.clone(), poles).or_refuse(KernelStage::Collect, "sweepRoundCorners.move")
28}
29
30/// The solid `section` sweeps along the straight runs through `path`, turned
31/// about each corner.
32///
33/// `section` is a closed loop of curves (or one closed curve) in the plane
34/// through `path[0]` square to the first run. Consecutive runs may turn by any
35/// angle short of a half turn; runs in one line simply continue.
36///
37/// Refuses a path of fewer than two points or with a run of no length, a
38/// corner that turns straight back, and whatever the extrudes, the revolves
39/// or the union refuse.
40pub fn sweep_profile_round_corners(section: &[NurbsCurve], path: &[Vec3]) -> Result<BrepSolid, KernelRefusal> {
41    if path.len() < 2 {
42        return Err(input("path", "the path needs at least two points".into()));
43    }
44    if section.is_empty() {
45        return Err(input("section", "the section is empty".into()));
46    }
47    let stage = KernelStage::Collect;
48    let poles = |curves: &[NurbsCurve]| -> Vec<Vec3> {
49        curves.iter().flat_map(|curve| &curve.control_points).map(|pole| Vec3::new(pole.x / pole.w, pole.y / pole.w, pole.z / pole.w)).collect()
50    };
51    let reach = poles(section).iter().map(|point| point.sub(path[0]).length()).fold(0.0, f64::max);
52    if reach <= 0.0 {
53        return Err(input("section", "the section has no extent".into()));
54    }
55    let mut section = section.to_vec();
56    let mut parts = Vec::new();
57    let mut fixed: Vec<Vec3> = Vec::new();
58    for index in 0..path.len() - 1 {
59        let run = path[index + 1].sub(path[index]);
60        let along = run.normalized().map_err(|_| input("path", format!("run {index} has no length")))?;
61        parts.push(extrude_profile_brep(&section, along, run.length())?);
62        section = section.iter().map(|curve| moved(curve, &|point| point.add(run))).collect::<Result<_, _>>()?;
63        let Some(after) = path.get(index + 2) else { break };
64        let next = after.sub(path[index + 1]).normalized().map_err(|_| input("path", format!("run {} has no length", index + 1)))?;
65        let turn = along.cross(next);
66        if turn.length() <= 1e-12 {
67            if along.dot(next) < 0.0 {
68                return Err(input("corner", format!("the path turns straight back at point {}", index + 1)));
69            }
70            continue;
71        }
72        let corner = path[index + 1];
73        let axis = turn.normalized().or_refuse(stage, "sweepRoundCorners.axis")?;
74        let angle = turn.length().atan2(along.dot(next));
75        // the section's plane at the corner: the axis, and the line across it
76        let across = axis.cross(along);
77        let span = 4.0 * reach;
78        let plane = make_plane(corner.sub(axis.scale(span)).sub(across.scale(span)), axis, across, 2.0 * span, 2.0 * span).or_refuse(stage, "sweepRoundCorners.plane")?;
79        let sheet = face_from_surface_trimmed(&plane, &[section.clone()])?;
80        for side in [1.0, -1.0] {
81            // the block on one side of the axis, holding that side of the section
82            let at = |a: f64, b: f64, c: f64| corner.add(axis.scale(a * span)).add(across.scale(side * b * span)).add(along.scale(c * span));
83            let mut faces = Vec::new();
84            for s in [0.0, 1.0] {
85                faces.push(vec![at(-1.0, s, -1.0), at(1.0, s, -1.0), at(1.0, s, 1.0), at(-1.0, s, 1.0)]);
86                faces.push(vec![at(2.0 * s - 1.0, 0.0, -1.0), at(2.0 * s - 1.0, 1.0, -1.0), at(2.0 * s - 1.0, 1.0, 1.0), at(2.0 * s - 1.0, 0.0, 1.0)]);
87                faces.push(vec![at(-1.0, 0.0, 2.0 * s - 1.0), at(1.0, 0.0, 2.0 * s - 1.0), at(1.0, 1.0, 2.0 * s - 1.0), at(-1.0, 1.0, 2.0 * s - 1.0)]);
88            }
89            let block = solid_from_polygons(&faces)?;
90            let part = boolean_operation(&sheet, &block, BooleanOperation::Intersect, &BooleanOptions::default())?;
91            for face in part.shells.iter().flat_map(|shell| &shell.faces) {
92                if face.loops.len() != 1 {
93                    return Err(KernelRefusal::unsupported(KernelStage::Fragment, "section_holes", format!("{SWEEP_ROUND_CORNERS_REFUSAL}: a section with a hole is not turned about a corner")));
94                }
95                let mut outline = Vec::new();
96                for coedge in &face.loops[0].coedges {
97                    let edge = part.edges.iter().find(|edge| edge.id == coedge.edge_id).ok_or_else(|| input("section", "the cut section refers to a missing edge".into()))?;
98                    // the edge is its curve between t0 and t1
99                    let [low, high] = edge.curve.domain().or_refuse(stage, "sweepRoundCorners.domain")?;
100                    let margin = 1e-9 * (high - low);
101                    let mut piece = edge.curve.clone();
102                    if edge.t1 < high - margin {
103                        piece = piece.split(edge.t1).or_refuse(stage, "sweepRoundCorners.trim")?.0;
104                    }
105                    if edge.t0 > low + margin {
106                        piece = piece.split(edge.t0).or_refuse(stage, "sweepRoundCorners.trim")?.1;
107                    }
108                    outline.push(if coedge.forward { piece } else { piece.reversed().or_refuse(stage, "sweepRoundCorners.reverse")? });
109                }
110                let turned = revolve_profile_brep(&outline, corner, axis, angle).or_refuse(KernelStage::Fragment, "sweepRoundCorners.turn")?;
111                // where the section crosses the corner's axis the turn has a point that does not move
112                for vertex in &turned.vertices {
113                    let d = vertex.point.sub(corner);
114                    if d.sub(axis.scale(d.dot(axis))).length() <= 1e-9 * reach {
115                        fixed.push(vertex.point);
116                    }
117                }
118                parts.push(turned);
119            }
120        }
121        section = section.iter().map(|curve| moved(curve, &|point| corner.add(point.sub(corner).rotate_about(axis, angle)))).collect::<Result<_, _>>()?;
122    }
123    if parts.len() == 1 {
124        return Ok(parts.remove(0));
125    }
126    let mut solid = boolean_operation_nary(&parts, BooleanOperation::Union)?;
127    if axis_points_on() {
128        // Both runs and the turn touch along the section's tangents at a point
129        // on the corner's axis, so the union finds its vertex there only to the
130        // square root of round-off; the point itself is known.
131        let mut put = Vec::new();
132        for vertex in &mut solid.vertices {
133            if let Some(point) = fixed.iter().find(|point| point.sub(vertex.point).length() <= 1e-6 * reach) {
134                vertex.point = *point;
135                put.push((vertex.id, *point));
136            }
137        }
138        // and the edges that end there end there: an edge that is the whole of
139        // its curve has that end's control point at the vertex
140        for edge in &mut solid.edges {
141            let Ok([low, high]) = edge.curve.domain() else { continue };
142            let margin = 1e-12 * (high - low);
143            let last = edge.curve.control_points.len() - 1;
144            for (vertex, at_end, index) in [(edge.start_vertex_id, (edge.t0 - low).abs() <= margin, 0), (edge.end_vertex_id, (edge.t1 - high).abs() <= margin, last)] {
145                let Some((_, point)) = put.iter().find(|(id, _)| *id == vertex) else { continue };
146                if at_end {
147                    let control = &mut edge.curve.control_points[index];
148                    (control.x, control.y, control.z) = (point.x * control.w, point.y * control.w, point.z * control.w);
149                }
150            }
151        }
152    }
153    Ok(solid)
154}
155
156/// `BREP_SWEEP_CORNER_AXIS_POINTS` (default on, `=0` restores): the vertices
157/// of a sweep with round corners that lie where the section crosses a
158/// corner's axis are put on those points exactly.
159fn axis_points_on() -> bool {
160    crate::switch!("BREP_SWEEP_CORNER_AXIS_POINTS", true)
161}
162