Wave 192/509: kernel: corner rules for direction, rails and closed paths; no feature gate on SMLib sweeps (and 3 more)

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

- af4e97b64 jyviko: cli: the rig and the oracle reach MeshTolerance through the engine
- f7daf6124 jyviko: doc, engine: the unused revolve targets and the formatting of the exact tools
- caa5ed0e6 jyviko: kernel: twisted and scaled sweeps round corners by the plain sweep's corner model
- dead22ba7 jyviko: kernel: corner rules for direction, rails and closed paths; no feature gate on SMLib sweeps

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
This commit is contained in:
jyviko
2026-10-22 07:49:18 -04:00
parent 816e43c5d0
commit 50ca3c5a61
9 changed files with 307 additions and 148 deletions
+1 -1
View File
@@ -235,7 +235,7 @@ fn check_case(dir: &str, dumps: Option<&std::path::Path>) -> Value {
shells += m.shells as u64;
if want["watertight"].as_bool() == Some(true) {
let tol = want["tolerance"]["mesh_mm"].as_f64().unwrap_or(MESH_TOL);
let (open, over) = match b.body.mesh(&solidcraft_kernel::MeshTolerance::chord(tol)) {
let (open, over) = match b.body.mesh(&solidcraft_engine::kernel::MeshTolerance::chord(tol)) {
Ok(mesh) => open_edges(&mesh.positions, &mesh.triangles, want["tolerance"]["weld_mm"].as_f64().unwrap_or(1e-4)),
Err(e) => {
c.push("watertight", json!(format!("{}: {e}", b.name)), json!("no open edges"), false);
+1 -1
View File
@@ -187,7 +187,7 @@ fn step_export_check(s: &solidcraft_engine::Session, volume: f64) -> (bool, Stri
fn our_mesh(s: &solidcraft_engine::Session) -> Result<Vec<Tri>, String> {
let mut ours: Vec<Tri> = Vec::new();
for b in &s.world_state().bodies {
let m = b.body.mesh(&solidcraft_kernel::MeshTolerance::chord(MESH_TOL)).map_err(|e| e.to_string())?;
let m = b.body.mesh(&solidcraft_engine::kernel::MeshTolerance::chord(MESH_TOL)).map_err(|e| e.to_string())?;
let p = |i: u32| m.positions.get(i as usize).map(|v: &Vec3| [v.x, v.y, v.z]);
for t in &m.triangles {
if let (Some(a), Some(b), Some(c)) = (p(t[0]), p(t[1]), p(t[2])) {
+1 -1
View File
@@ -4734,7 +4734,7 @@ fn feature_tools_with_constructor(
}
})
.collect::<Result<Vec<_>>>()?;
let (operation, targets) = feature_op(f);
let (operation, _) = feature_op(f);
let axis_w = ss.plane.dir_to_world(d);
let origin_w = ss.plane.to_world(o);
// To a curved face: turn past the whole target body, then cut back to its surface
+2 -5
View File
@@ -1946,11 +1946,8 @@ pub(super) fn plastic_eval(doc: &Document, vals: &BTreeMap<String, Value>, f: &F
// The post: a (tapered) cylinder standing on the face.
let base = Plane::new(position.0, x, z.cross(x)).ok_or_else(|| DocError::Invalid("boss frame".into()))?;
let disc = Region2 { outer: Loop2::circle(Vec2::ZERO, r0), holes: Vec::new() };
let post = if dr.abs() > 1e-12 {
vec![kernel::extrude_tapered(&base, &disc, h, 1.0, -dr)?]
} else {
kernel::extrude(&base, &[disc], 0.0, h)?
};
let post =
if dr.abs() > 1e-12 { vec![kernel::extrude_tapered(&base, &disc, h, 1.0, -dr)?] } else { kernel::extrude(&base, &[disc], 0.0, h)? };
let mut tools: Vec<Traced> = post.into_iter().map(|b| crate::lineage::part_tool(b, "post")).collect();
if let Some(n) = ribs {
let n = val(vals, n, Kind::Unitless)?.round();
+1 -7
View File
@@ -331,13 +331,7 @@ pub fn golden(seed: u64, max_steps: usize) -> Value {
fn run_session(seed: u64, max_steps: usize, on_step: &mut dyn FnMut(&Step)) -> (Run, Session) {
let mut r = Rng::new(seed);
let mut s = Session::default();
let mut run = Run {
seed,
steps: Vec::new(),
outcome: Outcome::ok(),
unsupported: Vec::new(),
rejected: 0,
};
let mut run = Run { seed, steps: Vec::new(), outcome: Outcome::ok(), unsupported: Vec::new(), rejected: 0 };
let mut last_feature: Option<String> = None;
let n = 2 + r.below(max_steps.max(2) - 1);
let mut tries = 0;
-9
View File
@@ -1240,15 +1240,6 @@ fn smlib_sweep(plane: &Plane, region: &Region2, path: &[PathSeg], ctl: &crate::S
if path.iter().any(|s| s.tangents().is_none()) {
return Err(KernelError::Invalid("a path segment has no direction".into()));
}
let controlled = ctl.varies() || matches!(ctl.orientation, crate::SweepOrientation::Direction(_));
if controlled {
let ends: Vec<(Vec3, Vec3)> = path.iter().filter_map(PathSeg::tangents).collect();
if ends.windows(2).any(|w| w[0].1.dot(w[1].0) < (1e-3f64).cos()) {
return Err(crate::smlib_sweeps::refuse(
"sweep with a twist, an end scale, guide rails or an orientation direction along a path with corners",
));
}
}
let translate = match ctl.orientation {
_ if ctl.varies() => return crate::smlib_sweeps::sweep_controlled(plane, &oriented(region)?, path, ctl),
crate::SweepOrientation::Perpendicular => false,
+4 -28
View File
@@ -179,30 +179,14 @@ fn polyhedron_record(hs: &[HalfSpace], verts: &[Vec3], faces: &[(usize, Vec<usiz
/// Hollow a body: walls of `thickness` inside, open at the faces under `open` (SMLib's shell).
/// The result's history is over `[b]`: the inner walls are offsets of the faces they line.
pub fn shell(b: &Body, open: &[Vec3], thickness: f64) -> Result<Body> {
#[cfg(feature = "smlib")]
{
crate::offset_backend::shell(b, open, thickness)
}
#[cfg(not(feature = "smlib"))]
{
let _ = (b, open, thickness);
Err(crate::smlib_sweeps::not_built())
}
crate::offset_backend::shell(b, open, thickness)
}
/// Tilt the faces near `faces` by `angle` about their line on the neutral plane, so that they
/// lean in (positive angle) going along `pull` (SMLib's draft).
pub fn draft(b: &Body, faces: &[Vec3], neutral: &solidcraft_geom::Plane, pull: Vec3, angle: f64) -> Result<Body> {
b.require_brep("draft")?;
#[cfg(feature = "smlib")]
{
crate::offset_backend::draft(b, faces, neutral, pull, angle)
}
#[cfg(not(feature = "smlib"))]
{
let _ = (faces, neutral, pull, angle);
Err(crate::smlib_sweeps::not_built())
}
crate::offset_backend::draft(b, faces, neutral, pull, angle)
}
/// Move the faces at the given points along their outward normals by `distance` (negative: into
@@ -233,14 +217,6 @@ pub fn offset_faces(b: &Body, at: &[Vec3], distance: f64) -> Result<Body> {
}
}
let chosen = crate::offset::with_same_surface(&healed, &chosen);
#[cfg(feature = "smlib")]
{
let moved = crate::offset_backend::offset_faces(&healed, &chosen, distance)?;
Ok(crate::record::after(moved, &healed, &crate::record::counts(&[b])))
}
#[cfg(not(feature = "smlib"))]
{
let _ = chosen;
Err(crate::smlib_sweeps::not_built())
}
let moved = crate::offset_backend::offset_faces(&healed, &chosen, distance)?;
Ok(crate::record::after(moved, &healed, &crate::record::counts(&[b])))
}
+297 -65
View File
@@ -24,7 +24,6 @@ use crate::numbered::{NumberedLoop, NumberedRegion};
use crate::{KernelError, Result};
/// A profile segment as SMLib gets it, with the profile segments it stands for.
#[cfg_attr(not(feature = "smlib"), allow(dead_code))]
pub(crate) struct Piece {
pub seg: Seg2,
pub numbers: Vec<usize>,
@@ -61,7 +60,6 @@ fn not_supported(what: &str) -> KernelError {
/// How an extrusion's walls lean.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(not(feature = "smlib"), allow(dead_code))]
pub(crate) struct Draft {
/// Radians; positive grows the profile (as [`crate::extrude_tapered`] takes it).
pub taper: f64,
@@ -70,70 +68,16 @@ pub(crate) struct Draft {
pub foot: f64,
}
#[cfg(feature = "smlib")]
pub(crate) use imp::{
body_from_record, curve, extrude, face_section, loft, loft_rails, loft_to_point, path_curve, project_onto_face, revolve, screw, sphere, sweep,
sweep_controlled,
};
/// The error of an SMLib operation in a build without SMLib.
#[cfg(not(feature = "smlib"))]
pub(crate) fn not_built() -> KernelError {
not_supported("this build has no SMLib (feature `smlib`)")
}
#[cfg(not(feature = "smlib"))]
mod stubs {
use super::*;
pub(crate) fn body_from_record(_: &solidcraft_geom::brep::Brep, _: &str) -> Result<Body> {
Err(not_built())
}
pub(crate) fn sphere(_: Vec3, _: f64) -> Result<Body> {
Err(not_built())
}
pub(crate) fn project_onto_face(_: &Body, _: Vec3, _: &Plane, _: &[Seg2], _: Vec3) -> Result<Vec<(solidcraft_geom::brep::Curve, [f64; 2])>> {
Err(not_built())
}
pub(crate) fn face_section(_: &Body, _: Vec3, _: Vec3, _: Vec3) -> Result<Vec<(solidcraft_geom::brep::Curve, [f64; 2])>> {
Err(not_built())
}
pub(crate) fn extrude(_: &Plane, _: &[Piece], _: Option<&Region2>, _: f64, _: f64, _: f64, _: Option<Draft>) -> Result<Body> {
Err(not_built())
}
pub(crate) fn revolve(_: &Plane, _: &[Piece], _: Vec3, _: Vec3, _: f64) -> Result<Body> {
Err(not_built())
}
pub(crate) fn sweep(_: &Plane, _: &[Piece], _: &[crate::PathSeg], _: bool) -> Result<Body> {
Err(not_built())
}
pub(crate) fn loft(_: &[(Plane, Vec<Piece>)], _: usize) -> Result<Body> {
Err(not_built())
}
pub(crate) fn screw(_: &Plane, _: &[Piece], _: Vec3, _: Vec3, _: f64, _: f64) -> Result<Body> {
Err(not_built())
}
pub(crate) fn sweep_controlled(_: &Plane, _: &NumberedRegion, _: &[crate::PathSeg], _: &crate::SweepControl) -> Result<Body> {
Err(not_built())
}
pub(crate) fn loft_rails(_: &[(Plane, Vec<Piece>)], _: usize, _: &[Vec<Vec3>], _: crate::RailFit) -> Result<Body> {
Err(not_built())
}
pub(crate) fn loft_to_point(_: &Plane, _: &[Piece], _: Vec3) -> Result<Body> {
Err(not_built())
}
}
#[cfg(not(feature = "smlib"))]
pub(crate) use stubs::{
body_from_record, extrude, face_section, loft, loft_rails, loft_to_point, project_onto_face, revolve, screw, sphere, sweep, sweep_controlled,
};
/// Refuses what no SMLib sweep does, before any work.
pub(crate) fn refuse(what: &str) -> KernelError {
not_supported(&format!("SMLib {what}"))
}
#[cfg(feature = "smlib")]
mod imp {
use std::collections::BTreeSet;
@@ -703,20 +647,13 @@ mod imp {
// first meet in a mitre too.
let closed = end.dist(start) <= 1e-6;
let closing = (closed && last_t.dot(first_t) < (1e-3f64).cos()).then_some(first_t);
let mitre = |t1: Vec3, t2: Vec3| -> Result<(Vec3, f64)> {
let turn = t1.dot(t2).clamp(-1.0, 1.0).acos();
if turn > std::f64::consts::PI - 1e-3 {
return Err(KernelError::Invalid("the path turns back on itself at a corner".into()));
}
Ok(((t1 + t2).normalized().ok_or_else(|| KernelError::Invalid("path corner".into()))?, turn))
};
let mut pose = *plane;
let mut body: Option<Brep> = None;
let mut inside = Vec::new();
// The mitre the next run starts from: its point, its normal (along the run) and the turn.
let mut entry: Option<(Vec3, Vec3, f64)> = match closing {
Some(_) if !translate => {
let (m, turn) = mitre(last_t, first_t)?;
let (m, turn) = mitre_of(last_t, first_t)?;
Some((start, m, turn))
}
_ => None,
@@ -733,7 +670,7 @@ mod imp {
None => closing,
};
// The plane halving the corner at the run's end, and the turn there.
let exit = next.map(|t2| mitre(t1, t2)).transpose()?;
let exit = next.map(|t2| mitre_of(t1, t2)).transpose()?;
// Each piece's sweep says where it left the profile: half-way along it each wall lies
// over its piece, and at its end (less any straight lead-out past the corner) the
// profile is where the next run takes it on.
@@ -882,6 +819,10 @@ mod imp {
/// exactly the profile's image, and SMLib skins the walls through them. Round a closed path the
/// last section has to land on the first, and the ends are joined.
pub(crate) fn sweep_controlled(plane: &Plane, region: &NumberedRegion, path: &[crate::PathSeg], ctl: &crate::SweepControl) -> Result<Body> {
let runs = smooth_runs(path)?;
if runs.len() > 1 {
return cornered_controlled(plane, region, path, &runs, ctl);
}
let pieces = pieces(region.loops());
let c = curves(plane, &pieces)?;
let start = path.first().map(crate::PathSeg::start).ok_or_else(|| KernelError::Invalid("the path needs 1…1000 segments".into()))?;
@@ -927,6 +868,297 @@ mod imp {
with_history(body, &roles, &pieces)
}
/// `x` moved by `a`.
fn affine_apply(a: &Affine, x: Vec3) -> Vec3 {
let [c0, c1, c2] = a.m;
let v = |q: [f64; 3]| Vec3::new(q[0], q[1], q[2]);
v(c0) * x.x + v(c1) * x.y + v(c2) * x.z + v(a.t)
}
/// `v` turned by `a`'s linear part.
fn affine_turn(a: &Affine, v: Vec3) -> Vec3 {
affine_apply(a, v) - affine_apply(a, Vec3::ZERO)
}
/// `a` after `b`: x goes to a(b(x)).
fn affine_after(a: &Affine, b: &Affine) -> Affine {
let col = |c: [f64; 3]| p(affine_turn(a, Vec3::new(c[0], c[1], c[2])));
Affine { m: [col(b.m[0]), col(b.m[1]), col(b.m[2])], t: p(affine_apply(a, Vec3::new(b.t[0], b.t[1], b.t[2]))) }
}
/// The rigid turn by `angle` about the unit `k` through `center`, as an affine map.
fn affine_turned(center: Vec3, k: Vec3, angle: f64) -> Affine {
let col = |e: Vec3| p(turned(e, k, angle));
Affine { m: [col(Vec3::X), col(Vec3::Y), col(Vec3::Z)], t: p(center - turned(center, k, angle)) }
}
fn affine_moved(a: &Affine, by: Vec3) -> Affine {
Affine { m: a.m, t: p(Vec3::new(a.t[0], a.t[1], a.t[2]) + by) }
}
const IDENTITY: Affine = Affine { m: [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]], t: [0.0; 3] };
/// A sweep that twists, scales, keeps an orientation direction or follows guide rails along
/// a path with corners, by the corner model of the plain sweep ([`cornered`]): each smooth run
/// of the path is swept through stations by the laws a smooth path has (the twist and the
/// scale running with the length along the whole path, each run going on from the section the
/// last one ended with), and the runs meet at each corner in a mitre. Turning with the path,
/// each run is carried on straight past its corners as a prism of its section there (the laws
/// hold still across a mitre: the two runs' sections meet on the plane halving the corner only
/// when both are the corner's section), cut back to that plane, and the section turned through
/// the corner about its binormal; only moving along the path, the runs meet at the corner's
/// section.
///
/// At a corner an orientation direction turns the section as the corner does, and along the
/// next run it turns back about the path to the direction's frame, evenly along the run's
/// length, as a twist. Guide rails must turn with the path: each rail has a corner of its own
/// at every corner of the path, in the plane halving it and by the same turn, where it is cut
/// into the runs' rails. Round a closed path the last section has to land on the first: a
/// twist where the profile maps onto itself; an end scale never does.
fn cornered_controlled(
plane: &Plane,
region: &NumberedRegion,
path: &[crate::PathSeg],
runs: &[std::ops::Range<usize>],
ctl: &crate::SweepControl,
) -> Result<Body> {
let translate = ctl.orientation == crate::SweepOrientation::Parallel;
let pieces = pieces(region.loops());
let c = curves(plane, &pieces)?;
let bad = || KernelError::Invalid("a path segment has no direction".into());
let start = path.first().map(crate::PathSeg::start).ok_or_else(|| KernelError::Invalid("empty path".into()))?;
let end = path.last().map(crate::PathSeg::end).ok_or_else(|| KernelError::Invalid("empty path".into()))?;
let lens: Vec<f64> = path.iter().map(seg_len).collect();
let total: f64 = lens.iter().sum();
if !(total > 1e-9 && total.is_finite()) {
return Err(KernelError::Invalid("the sweep path has no length".into()));
}
let tangent = |s: Option<&crate::PathSeg>| s.and_then(crate::PathSeg::tangents).ok_or_else(bad);
let (first_t, _) = tangent(path.first())?;
let (_, last_t) = tangent(path.last())?;
let closed = end.dist(start) <= 1e-6;
if closed && ctl.scale != 1.0 {
return Err(KernelError::Invalid(
"a sweep with an end scale can't go round a closed path: its last section would have to land on its first".into(),
));
}
let closing = (closed && last_t.dot(first_t) < (1e-3f64).cos()).then_some(first_t);
let reach = pieces
.iter()
.flat_map(|x| (0..=8).map(move |i| x.seg.point_at(f64::from(i) / 8.0)))
.map(|q| plane.to_world(q).dist(start) * ctl.scale.max(1.0))
.fold(0.0f64, f64::max);
let size = 4.0 * (total + reach) + 1.0;
let law = |u: f64| 1.0 + (ctl.scale - 1.0) * u;
let n0 = plane.normal().normalized().ok_or_else(|| KernelError::Invalid("profile plane".into()))?;
// Each run's rails, cut at the corners.
let rails = run_rails(path, runs, &ctl.rails, closing)?;
// The map carrying the profile as drawn onto the section where the next run starts, the
// mitre that run starts from (its point, its normal along the run, its turn) and how far
// along the path it starts.
let mut at_start = IDENTITY;
let mut entry: Option<(Vec3, Vec3, f64)> = match closing {
Some(_) if !translate => {
let (m, turn) = mitre_of(last_t, first_t)?;
Some((start, m, turn))
}
_ => None,
};
let mut before = 0.0f64;
let mut body: Option<Brep> = None;
let mut inside = Vec::new();
for (ri, run) in runs.iter().enumerate() {
let segs = path.get(run.clone()).ok_or_else(|| KernelError::Invalid("path run".into()))?;
let length: f64 = lens.get(run.clone()).ok_or_else(|| KernelError::Invalid("path run".into()))?.iter().sum();
let (u0, u1) = (before / total, (before + length) / total);
let (Some(first), Some(last)) = (segs.first(), segs.last()) else { return Err(KernelError::Invalid("path run".into())) };
let ((t0, _), (_, t1)) = (first.tangents().ok_or_else(bad)?, last.tangents().ok_or_else(bad)?);
let (a0, b1) = (first.start(), last.end());
let normal = affine_turn(&at_start, n0).normalized().ok_or_else(|| KernelError::Invalid("the sweep's section is degenerate".into()))?;
let pose = Plane::from_normal(a0, normal).ok_or_else(|| KernelError::Invalid("the sweep's section is degenerate".into()))?;
// The run's own laws on its section at the start: the twist over its length (less the
// turn back to the orientation direction after a corner), the scale from where the
// last run left it to where this one ends, the rails from corner to corner.
let back = match ctl.orientation {
crate::SweepOrientation::Direction(_) if ri > 0 => direction_offset(plane, start, first_t, a0, t0, u0, ctl, &at_start)?,
_ => 0.0,
};
let run_ctl = crate::SweepControl {
orientation: ctl.orientation,
twist: ctl.twist * (u1 - u0) - back,
scale: law(u1) / law(u0),
rails: rails.get(ri).cloned().unwrap_or_default(),
rail_fit: ctl.rail_fit,
};
// A station per degree the section turns, with the path and the twist.
let turn: f64 = segs.iter().map(path_turn).sum::<f64>() + run_ctl.twist.abs();
let n = ((turn / (std::f64::consts::PI / 180.0)).ceil() as usize).clamp(MIN_STATIONS, MAX_STATIONS);
let (pts, tangents) = path_samples(segs, n)?;
let st = crate::sweep_path::controlled_stations(&pose, &pts, &tangents, &[], &run_ctl)?;
let maps: Vec<Affine> = st
.iter()
.map(|s| {
let [a, b, cc] = s.m;
affine_after(&Affine { m: [p(a), p(b), p(cc)], t: p(s.at - s.apply(a0)) }, &at_start)
})
.collect();
let (Some(&first_map), Some(&end_map)) = (maps.first(), maps.last()) else { return Err(KernelError::Invalid("sweep path".into())) };
let mid = maps.get(maps.len() / 2).copied().unwrap_or(end_map);
inside.extend(seeds(plane, &pieces, |q| affine_apply(&mid, q)));
let mut piece = Brep::sweep_stations(&c, &maps, Ends::Capped).map_err(|e| failed("sweep", e))?;
let next = match runs.get(ri + 1).and_then(|r| path.get(r.start)) {
Some(s) => Some(tangent(Some(s))?.0),
None => closing,
};
let exit = next.map(|t2| mitre_of(t1, t2)).transpose()?;
if !translate {
// On past each mitre, the run's section there held still.
let over = |turn: f64| reach * (turn * 0.5).tan() * 1.1 + reach * 1e-3 + 1e-6;
let prism = |map: Affine, toward: Vec3, by: f64| -> Result<Brep> {
Brep::sweep_stations(&c, &[map, affine_moved(&map, toward * by)], Ends::Capped).map_err(|e| failed("sweep corner", e))
};
if let Some((at, m, turn)) = entry {
let lead = prism(affine_moved(&first_map, t0 * -over(turn)), t0, over(turn))?;
piece = combined(&piece, &lead, solidcraft_smlib::BooleanOp::Union, "sweep corner")?;
piece = combined(&piece, &half_space(at, -m, size)?, solidcraft_smlib::BooleanOp::Subtract, "sweep mitre")?;
}
if let Some((m, turn)) = exit {
let tail = prism(end_map, t1, over(turn))?;
piece = combined(&piece, &tail, solidcraft_smlib::BooleanOp::Union, "sweep corner")?;
piece = combined(&piece, &half_space(b1, m, size)?, solidcraft_smlib::BooleanOp::Subtract, "sweep mitre")?;
}
}
body = Some(match body {
None => piece,
Some(b) => combined(&b, &piece, solidcraft_smlib::BooleanOp::Union, "sweep corner")?,
});
// The section at the run's end, turned through the corner onto the next run.
at_start = end_map;
entry = None;
if let (Some(t2), Some((m, turn)), false) = (next, exit, translate) {
let k = t1.cross(t2).normalized().ok_or_else(|| KernelError::Invalid("path corner".into()))?;
at_start = affine_after(&affine_turned(b1, k, turn), &end_map);
entry = Some((b1, m, turn));
}
before += length;
}
let brep = body.ok_or_else(|| KernelError::Invalid("empty path".into()))?;
if closed {
// Back at the start, the section has to be the profile again (a whole number of its
// symmetries of twist).
let to_station = |a: &Affine| {
let v = |q: [f64; 3]| Vec3::new(q[0], q[1], q[2]);
crate::sweep_path::Station { at: affine_apply(a, start), m: [v(a.m[0]), v(a.m[1]), v(a.m[2])] }
};
for l in region.loops() {
crate::sweep_path::closing_offset(plane, l, &[to_station(&IDENTITY), to_station(&at_start)])?;
}
}
let body = body_of(&brep, "sweep")?;
let roles = if closed {
roles(&body, &plane.offset(size * 10.0), None, &[], &inside, false)?
} else {
let end_plane = Plane::new(affine_apply(&at_start, plane.origin), affine_turn(&at_start, plane.x), affine_turn(&at_start, plane.y))
.ok_or_else(|| KernelError::Invalid("the sweep's end section is degenerate".into()))?;
roles(&body, plane, Some(&end_plane), &seeds(plane, &pieces, |q| q), &inside, false)?
};
with_history(body, &roles, &pieces)
}
/// After a corner, how far the section at `a0` (the profile carried there by `at_start`) is
/// turned about its normal (toward the run's direction `t0`) from where the orientation direction's law puts it
/// (the law of a smooth path from `start`, leaving along `first_t`, `u0` of the way along):
/// the turn the next run takes back, as a twist. Radians, right-handed about `t0`.
#[allow(clippy::too_many_arguments)]
fn direction_offset(
plane: &Plane,
start: Vec3,
first_t: Vec3,
a0: Vec3,
t0: Vec3,
u0: f64,
ctl: &crate::SweepControl,
at_start: &Affine,
) -> Result<f64> {
let law = crate::SweepControl { twist: ctl.twist * u0, scale: 1.0 + (ctl.scale - 1.0) * u0, rails: Vec::new(), ..ctl.clone() };
// A two-station path from the start to here with the two directions: the law's section here.
let st = crate::sweep_path::controlled_stations(plane, &[start, a0], &[first_t, t0], &[], &law)?;
let want = st.last().map(|s| s.apply(plane.x)).ok_or_else(|| KernelError::Invalid("sweep path".into()))?;
let have = affine_turn(at_start, plane.x);
// About the section's normal, pointing the way the run leaves it (the twist's axis).
let n = affine_turn(at_start, plane.normal());
let axis = (if n.dot(t0) < 0.0 { n * -1.0 } else { n })
.normalized()
.ok_or_else(|| KernelError::Invalid("the sweep's section is degenerate".into()))?;
let flat = |v: Vec3| (v - axis * v.dot(axis)).normalized();
let (Some(w), Some(h)) = (flat(want), flat(have)) else {
return Err(KernelError::Invalid("the profile lies along the sweep path at a corner".into()));
};
Ok(w.cross(h).dot(axis).atan2(w.dot(h)))
}
/// The guide rails cut at the path's corners, one set per run: each rail must turn with the
/// path there, with a corner of its own in the plane halving the path's corner and by the
/// same turn about the same axis.
fn run_rails(
path: &[crate::PathSeg],
runs: &[std::ops::Range<usize>],
rails: &[Vec<Vec3>],
closing: Option<Vec3>,
) -> Result<Vec<Vec<Vec<Vec3>>>> {
if rails.is_empty() {
return Ok(vec![Vec::new(); runs.len()]);
}
if closing.is_some() {
return Err(KernelError::Invalid("guide rails can't follow a closed path with corners: a rail runs from the start to the end".into()));
}
let refused = |i: usize| {
KernelError::Invalid(format!(
"at corner {} of the path the guide rails have to turn with it: each rail needs a corner of its own there, in the plane halving the path's corner and by the same turn",
i + 1
))
};
let mut rest: Vec<Vec<Vec3>> = rails.iter().map(|r| crate::sweep_path::clean(r)).collect::<Result<_>>()?;
let mut out = Vec::with_capacity(runs.len());
for (ci, w) in runs.windows(2).enumerate() {
let (Some(a), Some(b)) = (w[0].end.checked_sub(1).and_then(|i| path.get(i)), path.get(w[1].start)) else { return Err(refused(ci)) };
let ((_, t1), (t2, _)) = (a.tangents().ok_or_else(|| refused(ci))?, b.tangents().ok_or_else(|| refused(ci))?);
let (m, turn) = mitre_of(t1, t2)?;
let at = a.end();
let k = t1.cross(t2).normalized().ok_or_else(|| refused(ci))?;
let mut here = Vec::with_capacity(rest.len());
for rail in &mut rest {
let size = rail.windows(2).map(|q| q[0].dist(q[1])).sum::<f64>().max(1e-9);
let tol = (size * 1e-6).max(1e-6);
let corner = (1..rail.len().saturating_sub(1)).find(|&j| {
let (Some(p), Some(q), Some(r)) = (rail.get(j - 1), rail.get(j), rail.get(j + 1)) else { return false };
let (Some(d1), Some(d2)) = ((*q - *p).normalized(), (*r - *q).normalized()) else { return false };
(*q - at).dot(m).abs() <= tol
&& (d1.dot(d2).clamp(-1.0, 1.0).acos() - turn).abs() <= 1e-3
&& d1.cross(d2).normalized().is_some_and(|kr| kr.dot(k) > 1.0 - 1e-6)
});
let j = corner.ok_or_else(|| refused(ci))?;
let tail = rail.split_off(j);
let mut head = std::mem::replace(rail, tail);
head.extend(rail.first().copied());
here.push(head);
}
out.push(here);
}
out.push(rest);
Ok(out)
}
/// The plane halving the corner from direction `t1` to `t2` (its unit normal) and the turn
/// there; a path that turns back on itself has none.
fn mitre_of(t1: Vec3, t2: Vec3) -> Result<(Vec3, f64)> {
let turn = t1.dot(t2).clamp(-1.0, 1.0).acos();
if turn > std::f64::consts::PI - 1e-3 {
return Err(KernelError::Invalid("the path turns back on itself at a corner".into()));
}
Ok(((t1 + t2).normalized().ok_or_else(|| KernelError::Invalid("path corner".into()))?, turn))
}
/// A loft through `sections` (each a closed loop of pieces, all re-split to the same count
/// and lined up), `first` the index of the section given first (its cap is the start cap).
pub(crate) fn loft(sections: &[(Plane, Vec<Piece>)], first: usize) -> Result<Body> {
-31
View File
@@ -152,10 +152,6 @@ pub(crate) fn clean(path: &[Vec3]) -> Result<Vec<Vec3>> {
Ok(out)
}
pub(crate) fn closed_path(pts: &[Vec3]) -> bool {
pts.len() > 3 && pts.first().zip(pts.last()).is_some_and(|(a, b)| a.dist(*b) <= 1e-7)
}
/// The cyclic correspondence of a loop's last section to its first. Matching whole curves
/// (rather than vertices alone) prevents sewing an incompatible twist or scale shut.
pub(crate) fn closing_offset(plane: &Plane, profile: &NumberedLoop, st: &[Station]) -> Result<usize> {
@@ -185,13 +181,6 @@ pub(crate) fn closing_offset(plane: &Plane, profile: &NumberedLoop, st: &[Statio
.ok_or_else(fail)
}
fn turn(a: Vec3, b: Vec3) -> f64 {
match (a.normalized(), b.normalized()) {
(Some(a), Some(b)) => a.dot(b).clamp(-1.0, 1.0).acos(),
_ => 0.0,
}
}
/// Rotation-minimising frames (double reflection) at the points, for the given unit tangents;
/// each station maps the first frame onto its own.
fn frames(pts: &[Vec3], tangents: &[Vec3]) -> Option<Vec<[Vec3; 3]>> {
@@ -214,26 +203,6 @@ fn frames(pts: &[Vec3], tangents: &[Vec3]) -> Option<Vec<[Vec3; 3]>> {
Some(out)
}
/// Stations: each maps a world vector at the first station to the same vector at its own,
/// optionally stretched across a corner (mitre).
fn stations(pts: &[Vec3], tangents: &[Vec3], mitre: &[Option<(Vec3, f64)>]) -> Result<Vec<Station>> {
let f = frames(pts, tangents).ok_or_else(|| KernelError::Invalid("the sweep path doubles back".into()))?;
let f0 = *f.first().ok_or_else(|| KernelError::Invalid("sweep path".into()))?;
let mut out = Vec::with_capacity(f.len());
for (i, fi) in f.iter().enumerate() {
// Rotation taking f0 to fi: columns are the images of the world axes.
let col = |e: Vec3| fi[0] * f0[0].dot(e) + fi[1] * f0[1].dot(e) + fi[2] * f0[2].dot(e);
let mut m = [col(Vec3::X), col(Vec3::Y), col(Vec3::Z)];
if let Some(Some((b, s))) = mitre.get(i) {
for c in &mut m {
*c += *b * ((s - 1.0) * c.dot(*b));
}
}
out.push(Station { at: *pts.get(i).ok_or_else(|| KernelError::Invalid("sweep path".into()))?, m });
}
Ok(out)
}
/// Frames square to the path whose turn about it keeps `d`'s projection: none where the path
/// runs along `d`.
fn frames_toward(tangents: &[Vec3], d: Vec3) -> Result<Vec<[Vec3; 3]>> {