use crate::{TwoTurns, Vec3};
use std::f64::consts::{PI, TAU};
#[derive(Clone, Copy, Debug)]
pub struct ThreadTool {
pub axis_point: Vec3,
pub axis_direction: Vec3,
pub reference: Vec3,
pub lead: f64,
pub pitch_radius: f64,
pub taper: f64,
pub half_width: f64,
pub flank_tan: f64,
pub tip: f64,
pub root: f64,
pub start: f64,
pub end: f64,
}
pub const LEADING_FLANK: u32 = 1 << 0;
pub const TRAILING_FLANK: u32 = 1 << 1;
pub const TIP: u32 = 1 << 2;
pub const ROOT: u32 = 1 << 3;
pub const START: u32 = 1 << 4;
pub const END: u32 = 1 << 5;
const TIGHT: f64 = 1e-7;
impl ThreadTool {
pub fn margin(&self, y: Vec3) -> (f64, u32) {
let d = y.sub(self.axis_point);
let s = d.dot(self.axis_direction);
let radial = d.sub(self.axis_direction.scale(s));
let r = radial.length();
let across = self.axis_direction.cross(self.reference);
let theta = radial.dot(across).atan2(radial.dot(self.reference));
let pitch = self.pitch_radius + self.taper * s;
let along = s - self.lead * theta / TAU;
let offset = along - self.lead.abs() * (along / self.lead.abs()).round();
let half = self.half_width - (r - pitch) * self.flank_tan;
let slack = [half - offset, half + offset, pitch + self.tip - r, r - (pitch - self.root), s - self.start, self.end - s];
let least = slack.iter().copied().fold(f64::INFINITY, f64::min);
let bind = slack.iter().enumerate().filter(|(_, &v)| v - least <= TIGHT).fold(0u32, |m, (i, _)| m | 1 << i);
(least, bind)
}
pub fn check(&self) -> Result<(), String> {
let fraction = 2.0 * self.half_width / self.lead.abs();
if !(self.lead.is_finite() && self.lead != 0.0 && fraction >= 1e-5) {
return Err(format!("the thread's width is {fraction:e} of its lead, under the 1e-5 a sweep can resolve"));
}
if !(self.tip + self.root > 0.0 && self.end > self.start && self.pitch_radius > 0.0) {
return Err("the thread's section or its ends are empty".into());
}
if !(self.inner_radius() >= 0.01 * (self.tip + self.root)) {
return Err(format!("the thread comes within {:e} of its axis, under a hundredth of its depth", self.inner_radius()));
}
Ok(())
}
pub fn inner_radius(&self) -> f64 {
(self.pitch_radius + self.taper * self.start).min(self.pitch_radius + self.taper * self.end) - self.root
}
pub fn reach(&self) -> f64 {
let at = |s: f64| self.pitch_radius + self.taper * s + self.tip;
at(self.start).max(at(self.end))
}
}
pub fn carried_back(motion: &TwoTurns, x: Vec3, t: f64) -> Vec3 {
let back = |p: Vec3, (point, direction): (Vec3, Vec3), angle: f64| point.add(p.sub(point).rotate_about(direction, angle));
let unit = |d: Vec3| d.normalized().unwrap_or(d);
let y = back(x, (motion.axes[1].0, unit(motion.axes[1].1)), -motion.rates[1] * t);
back(y, (motion.axes[0].0, unit(motion.axes[0].1)), -motion.rates[0] * t)
}
pub fn margin_at(tool: &ThreadTool, motion: &TwoTurns, x: Vec3, t: f64) -> (f64, u32) {
tool.margin(carried_back(motion, x, t))
}
const WINDOW_SAMPLES: usize = 720;
pub fn window(tool: &ThreadTool, motion: &TwoTurns, x: Vec3) -> Vec<(f64, f64)> {
let rate = motion.rates[1];
let (from, to) = (motion.from, motion.to);
if rate == 0.0 || !(to > from) {
return if within(tool, motion, x, from) { vec![(from, to)] } else { Vec::new() };
}
let (point, direction) = (motion.axes[1].0, motion.axes[1].1.normalized().unwrap_or(motion.axes[1].1));
let off = x.sub(point);
let rho = off.sub(direction.scale(off.dot(direction))).length();
let depth = tool.tip + tool.root + WINDOW_BAND;
let angle = (TAU / WINDOW_SAMPLES as f64).min(0.25 * depth / rho.max(1e-12));
let step = (angle / rate.abs()).min((to - from) / 8.0);
let n = ((to - from) / step).ceil() as usize;
let at = |i: usize| (from + step * i as f64).min(to);
let mut out: Vec<(f64, f64)> = Vec::new();
let mut open: Option<f64> = None;
for i in 0..=n {
let inside = within(tool, motion, x, at(i));
match (inside, open) {
(true, None) => open = Some(at(i.saturating_sub(1))),
(false, Some(start)) => {
out.push((start, at(i)));
open = None;
}
_ => {}
}
}
if let Some(start) = open {
out.push((start, to));
}
out
}
pub const WINDOW_BAND: f64 = 1.0;
fn within(tool: &ThreadTool, motion: &TwoTurns, x: Vec3, t: f64) -> bool {
let y = carried_back(motion, x, t);
let d = y.sub(tool.axis_point);
let s = d.dot(tool.axis_direction);
let reach = tool.reach() + WINDOW_BAND;
d.sub(tool.axis_direction.scale(s)).length() <= reach && s >= tool.start - reach && s <= tool.end + reach
}
fn phase_step(tool: &ThreadTool, motion: &TwoTurns) -> f64 {
let width = (2.0 * tool.half_width / tool.lead.abs()).min(1.0);
let own = (TAU / motion.rates[0].abs().max(1e-12)) * (width / 8.0).min(1.0 / 40.0) * 40.0;
let across = (2.0 * tool.half_width / tool.reach().max(1e-12)) / motion.rates[1].abs().max(1e-12);
own.min(across) / 40.0
}
fn time_scale(tool: &ThreadTool, motion: &TwoTurns, x: Vec3) -> (f64, f64) {
let (point, axis) = (motion.axes[1].0, motion.axes[1].1.normalized().unwrap_or(Vec3::new(0.0, 0.0, 1.0)));
let rel = x.sub(point);
let rho = rel.sub(axis.scale(rel.dot(axis))).length();
let speed = rho * motion.rates[1].abs();
let inner = tool.inner_radius();
let turning = motion.rates[0].abs() + speed / inner;
let rate = speed * (1.0 + tool.flank_tan.abs() + tool.taper.abs()) + tool.lead.abs() / TAU * turning;
let extent = (2.0 * tool.half_width).min(tool.tip + tool.root);
let step = phase_step(tool, motion).min(0.125 * extent / rate.max(1e-300));
(step, rate)
}
const MAX_SAMPLES: usize = 2_000_000;
pub fn swept_margin(tool: &ThreadTool, motion: &TwoTurns, x: Vec3) -> (f64, u32) {
let (step, _) = time_scale(tool, motion, x);
let mut top = (-1.0, 0u32);
let mut first = true;
for (a, b) in window(tool, motion, x) {
let n = ((b - a) / step).ceil().clamp(2.0, MAX_SAMPLES as f64) as usize;
let samples: Vec<f64> = (0..=n).map(|i| margin_at(tool, motion, x, a + (b - a) * i as f64 / n as f64).0).collect();
let mut order: Vec<usize> = (0..=n).collect();
order.sort_by(|&i, &j| samples[j].total_cmp(&samples[i]));
for &i in order.iter().take(3) {
let sampled = margin_at(tool, motion, x, a + (b - a) * i as f64 / n as f64);
if first || sampled.0 > top.0 {
top = sampled;
first = false;
}
let (mut l, mut h) = (a + (b - a) * i.saturating_sub(1) as f64 / n as f64, a + (b - a) * (i + 1).min(n) as f64 / n as f64);
let ratio = 0.5 * (5f64.sqrt() - 1.0);
let (mut c, mut d) = (h - ratio * (h - l), l + ratio * (h - l));
let (mut fc, mut fd) = (margin_at(tool, motion, x, c).0, margin_at(tool, motion, x, d).0);
for _ in 0..48 {
if fc > fd {
h = d;
(d, fd) = (c, fc);
c = h - ratio * (h - l);
fc = margin_at(tool, motion, x, c).0;
} else {
l = c;
(c, fc) = (d, fd);
d = l + ratio * (h - l);
fd = margin_at(tool, motion, x, d).0;
}
}
let found = margin_at(tool, motion, x, 0.5 * (l + h));
if first || found.0 > top.0 {
top = found;
first = false;
}
}
}
top
}
pub fn swept_holds(tool: &ThreadTool, motion: &TwoTurns, x: Vec3) -> bool {
let (step, rate) = time_scale(tool, motion, x);
for (a, b) in window(tool, motion, x) {
let n = ((b - a) / step).ceil().clamp(2.0, MAX_SAMPLES as f64) as usize;
let time = |i: usize| a + (b - a) * i as f64 / n as f64;
let mut samples = Vec::with_capacity(n + 1);
for i in 0..=n {
let m = margin_at(tool, motion, x, time(i)).0;
if m > 0.0 {
return true;
}
samples.push(m);
}
let reach = rate * (b - a) / n as f64;
let mut order: Vec<usize> = (0..=n).filter(|&i| samples[i] + reach >= 0.0).collect();
order.sort_by(|&i, &j| samples[j].total_cmp(&samples[i]));
for &i in order.iter().take(3) {
let (mut l, mut h) = (time(i.saturating_sub(1)), time((i + 1).min(n)));
let ratio = 0.5 * (5f64.sqrt() - 1.0);
let (mut c, mut d) = (h - ratio * (h - l), l + ratio * (h - l));
let (mut fc, mut fd) = (margin_at(tool, motion, x, c).0, margin_at(tool, motion, x, d).0);
for _ in 0..48 {
if fc > 0.0 || fd > 0.0 {
return true;
}
if fc > fd {
h = d;
(d, fd) = (c, fc);
c = h - ratio * (h - l);
fc = margin_at(tool, motion, x, c).0;
} else {
l = c;
(c, fc) = (d, fd);
d = l + ratio * (h - l);
fd = margin_at(tool, motion, x, d).0;
}
}
if fc > 0.0 || fd > 0.0 || margin_at(tool, motion, x, 0.5 * (l + h)).0 > 0.0 {
return true;
}
}
}
false
}
pub fn swept_margin_brute(tool: &ThreadTool, motion: &TwoTurns, x: Vec3, samples: usize) -> f64 {
(0..=samples)
.map(|i| margin_at(tool, motion, x, motion.from + (motion.to - motion.from) * i as f64 / samples as f64).0)
.fold(f64::NEG_INFINITY, f64::max)
}
#[derive(Clone, Copy, Debug)]
pub struct RingBlank {
pub inner: f64,
pub outer: f64,
pub bottom: f64,
pub face: f64,
pub face_outer: f64,
}
impl RingBlank {
pub fn face_at(&self, r: f64) -> f64 {
self.face + (self.face_outer - self.face) * (r - self.inner) / (self.outer - self.inner)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum Family {
Cylinders,
Planes,
}
fn gear_frame(motion: &TwoTurns) -> (Vec3, Vec3, Vec3, Vec3) {
let (point, d) = (motion.axes[1].0, motion.axes[1].1.normalized().unwrap_or(Vec3::new(0.0, 0.0, 1.0)));
let seed = if d.x.abs() < 0.9 { Vec3::new(1.0, 0.0, 0.0) } else { Vec3::new(0.0, 1.0, 0.0) };
let e1 = seed.sub(d.scale(seed.dot(d))).normalized().unwrap_or(seed);
(point, d, e1, d.cross(e1))
}
pub const AIR_STEP: f64 = 0.03;
fn slice(tool: &ThreadTool, motion: &TwoTurns, blank: &RingBlank, family: Family, pitch: f64, r: f64, seed: f64, fallback: Option<(f64, f64)>, count: usize) -> Result<(Vec<Vec3>, f64, f64), String> {
let (o, d, e1, e2) = gear_frame(motion);
let place = |f: f64, z: f64| match family {
Family::Cylinders => o.add(e1.scale(r * f.cos())).add(e2.scale(r * f.sin())).add(d.scale(z)),
Family::Planes => o.add(e1.scale(z * f.cos())).add(e2.scale(z * f.sin())).add(d.scale(r)),
};
let cut = |f: f64, z: f64| swept_holds(tool, motion, place(f, z));
let (face, floor, weight) = match family {
Family::Cylinders => (blank.face_at(r), blank.bottom, r),
Family::Planes => (blank.outer, blank.inner, blank.outer),
};
let n = 720;
let (lo, hi) = (seed - 1.5 * pitch, seed + 1.5 * pitch);
let at = |k: usize| lo + (hi - lo) * k as f64 / n as f64;
let depth = tool.tip + tool.root;
let mut level = face - 0.004 * depth;
let mut cuts: Vec<bool> = (0..=n).map(|k| cut(at(k), level)).collect();
while cuts.iter().all(|&c| c) && level > floor {
level -= 0.02 * depth;
cuts = (0..=n).map(|k| cut(at(k), level)).collect();
}
let mid = n / 2;
let distance = |a: usize, b: usize| if mid < a { a - mid } else if mid > b { mid - b } else { 0 };
let (mut best, mut k): (Option<(usize, usize)>, usize) = (None, 0);
while k <= n {
if cuts[k] {
let start = k;
while k <= n && cuts[k] {
k += 1;
}
let end = k - 1;
if start > 0 && end < n && best.is_none_or(|(a, b)| distance(start, end) < distance(a, b)) {
best = Some((start, end));
}
} else {
k += 1;
}
}
let (centre, delta) = match (best, fallback) {
(None, Some(fallback)) if cuts.iter().all(|&c| !c) => fallback,
(best, _) => {
let (start, end) = best.ok_or_else(|| format!("no tooth space on the face at r = {r}"))?;
let (mut left, mut right) = (start, end);
while left > 0 && !cuts[left - 1] {
left -= 1;
}
while right < n && !cuts[right + 1] {
right += 1;
}
if left == 0 || right == n {
return Err(format!("the tooth tops beside the space at r = {r} reach past the search"));
}
let (ml, el, er, mr) = (0.5 * (at(left) + at(start - 1)), at(start), at(end), 0.5 * (at(end + 1) + at(right)));
let lowered = level < face - 0.004 * depth;
let delta = if lowered { (0.3 * (el - ml).min(mr - er)).max(0.02 * pitch) } else { 0.3 * (el - ml).min(mr - er) };
let centre = 0.5 * (el + er);
(centre, delta)
}
};
let (ml, mr) = (centre - 0.5 * pitch - delta, centre + 0.5 * pitch + delta);
let lowest = match family {
Family::Cylinders => {
let height = |s: f64| tool.axis_point.add(tool.axis_direction.scale(s)).sub(o).dot(d);
height(tool.start).min(height(tool.end)) - tool.reach()
}
Family::Planes => {
let flat = |v: Vec3| v.sub(d.scale(v.dot(d)));
let (a, u) = (flat(tool.axis_point.sub(o)), flat(tool.axis_direction));
let s = if u.dot(u) > 0.0 { (-a.dot(u) / u.dot(u)).clamp(tool.start, tool.end) } else { tool.start };
a.add(u.scale(s)).length() - tool.reach()
}
};
let (z0, z1) = (floor.max(lowest) - 0.05 * depth, face + 0.2 * depth);
let inside = |f: f64, z: f64| -> bool {
if z >= z1 || f <= ml || f >= mr || z <= z0 {
return false;
}
if z > face + AIR_STEP * depth {
return true;
}
cut(f, z)
};
let (nf, nz) = (220isize, 160isize);
let fx = |i: isize| ml + (mr - ml) * (i as f64 + 0.5) / nf as f64;
let zy = |j: isize| z0 + (z1 - z0) * (j as f64 + 0.5) / nz as f64;
let grid: Vec<std::cell::Cell<u8>> = (0..nf * nz).map(|_| std::cell::Cell::new(0)).collect();
let held = |i: isize, j: isize| {
if i < 0 || j < 0 || i >= nf || j >= nz {
return false;
}
let cell = &grid[(i * nz + j) as usize];
if cell.get() == 0 {
cell.set(if inside(fx(i), zy(j)) { 1 } else { 2 });
}
cell.get() == 1
};
let mut first = None;
'find: for j in (0..nz).rev() {
for i in 0..nf {
if held(i, j) {
first = Some((i, j));
break 'find;
}
}
}
let first = first.ok_or_else(|| format!("the space at r = {r} is empty"))?;
let dirs = [(1isize, 0isize), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)];
let mut cells = vec![first];
let (mut c, mut back) = (first, 4usize);
for _ in 0..(4 * nf * nz) {
let mut moved = false;
for k in 1..=8 {
let dir = (back + k) % 8;
let q = (c.0 + dirs[dir].0, c.1 + dirs[dir].1);
if held(q.0, q.1) {
back = (dir + 4) % 8;
c = q;
moved = true;
break;
}
}
if !moved || c == first {
break;
}
cells.push(c);
}
if cells.len() < 8 {
return Err(format!("the space at r = {r} is too small to trace"));
}
let mut points: Vec<(f64, f64)> = Vec::with_capacity(cells.len());
for &(i, j) in &cells {
let (mut dx, mut dy) = (0.0, 0.0);
for step in [(1, 0), (-1, 0), (0, 1), (0, -1)] {
if !held(i + step.0, j + step.1) {
dx += step.0 as f64;
dy += step.1 as f64;
}
}
let (cf, cz) = (fx(i), zy(j));
if dx == 0.0 && dy == 0.0 {
let outside: Vec<(isize, isize)> = [(-1, 0), (1, 0), (0, 1), (0, -1)].into_iter().filter(|s| !held(i + s.0, j + s.1)).collect();
let frame = outside.iter().find(|s| (s.0 == -1 && i == 0) || (s.0 == 1 && i == nf - 1) || (s.1 == 1 && j == nz - 1));
if let Some(&(a, b)) = frame.or(outside.first()) {
(dx, dy) = (a as f64, b as f64);
}
}
let norm = (dx * dx + dy * dy).sqrt();
if norm == 0.0 {
points.push((cf, cz));
continue;
}
let (sf, sz) = (dx / norm * (mr - ml) / nf as f64 * 1.5, dy / norm * (z1 - z0) / nz as f64 * 1.5);
if inside(cf + sf, cz + sz) {
points.push((cf + sf, cz + sz));
continue;
}
let (mut a, mut b) = (0.0f64, 1.0f64);
for _ in 0..18 {
let m = 0.5 * (a + b);
if inside(cf + sf * m, cz + sz * m) {
a = m;
} else {
b = m;
}
}
points.push((cf + sf * a, cz + sz * a));
}
let loop_points = periodic_loop(&points, weight, pitch, (ml, mr), delta, z1, (mr - ml) / nf as f64, (z1 - z0) / nz as f64, count)?;
Ok((loop_points.into_iter().map(|(f, z)| place(f, z)).collect(), centre, delta))
}
#[allow(clippy::too_many_arguments)]
fn periodic_loop(points: &[(f64, f64)], r: f64, pitch: f64, (lo, hi): (f64, f64), delta: f64, z1: f64, cell_f: f64, cell_z: f64, count: usize) -> Result<Vec<(f64, f64)>, String> {
let on_frame = |q: (f64, f64)| q.0 - lo < 1e-3 * cell_f || hi - q.0 < 1e-3 * cell_f || z1 - q.1 < 1e-3 * cell_z;
let n = points.len();
let start = (0..n).find(|&k| on_frame(points[k])).ok_or_else(|| format!("the slice at r = {r} does not reach its frame"))?;
let (mut best, mut k) = ((0usize, 0usize), 0usize);
while k < n {
if on_frame(points[(start + k) % n]) {
k += 1;
continue;
}
let first = k;
while k < n && !on_frame(points[(start + k) % n]) {
k += 1;
}
if k - first > best.1 {
best = (first, k - first);
}
}
let mut bottom: Vec<(f64, f64)> = (0..best.1).map(|i| points[(start + best.0 + i) % n]).collect();
if bottom.len() < 4 {
return Err(format!("the slice at r = {r} has no bottom"));
}
if bottom[0].0 > bottom[bottom.len() - 1].0 {
bottom.reverse();
}
let strip = 2.0 * delta;
if !(bottom[0].0 < lo + 3.0 * cell_f && bottom[bottom.len() - 1].0 > hi - 3.0 * cell_f) {
return Err(format!("the bottom of the slice at r = {r} does not run from wall to wall"));
}
let enter = bottom.iter().position(|q| q.0 > lo + strip).ok_or_else(|| format!("the slice at r = {r} has no middle"))?;
if enter == 0 {
return Err(format!("the slice at r = {r} has no left strip"));
}
let (a, b) = (bottom[enter - 1], bottom[enter]);
let exit = (lo + strip, a.1 + (b.1 - a.1) * (lo + strip - a.0) / (b.0 - a.0));
let mut left_path = bottom[..enter].to_vec();
left_path.push(exit);
let first = bottom[0];
let target = (first.0 + pitch, first.1);
let end = (enter..bottom.len())
.min_by(|&x, &y| {
let d = |q: (f64, f64)| (r * (q.0 - target.0)).hypot(q.1 - target.1);
d(bottom[x]).total_cmp(&d(bottom[y]))
})
.unwrap_or(bottom.len() - 1);
let [_, walls, _, _, _, _] = sides(count);
let (before, after) = strip_counts(count);
let (strips, top) = (before + after, (count / 12).max(2));
let middle_count = count.checked_sub(2 * walls + 2 * strips + top).filter(|&m| m >= 8).ok_or_else(|| format!("{count} points are too few for a slice"))?;
let resample = |path: &[(f64, f64)], n: usize| -> Vec<(f64, f64)> {
let mut length = vec![0.0];
for k in 1..path.len() {
length.push(length[k - 1] + (r * (path[k].0 - path[k - 1].0)).hypot(path[k].1 - path[k - 1].1));
}
let total = length[length.len() - 1];
let mut piece = 0;
(0..n)
.map(|k| {
let s = total * k as f64 / n as f64;
while piece + 2 < length.len() && length[piece + 1] < s {
piece += 1;
}
let w = (s - length[piece]) / (length[piece + 1] - length[piece]).max(1e-300);
let (a, b) = (path[piece], path[piece + 1]);
(a.0 + (b.0 - a.0) * w, a.1 + (b.1 - a.1) * w)
})
.collect()
};
let (low, high) = left_path.iter().fold((f64::INFINITY, f64::NEG_INFINITY), |(a, b), q| (a.min(q.1), b.max(q.1)));
let crest = if high - low < 1e-6 * (z1 - low) {
left_path.iter().enumerate().min_by(|a, b| (a.1 .0 - lo - delta).abs().total_cmp(&(b.1 .0 - lo - delta).abs())).map_or(0, |(k, _)| k)
} else {
left_path.iter().enumerate().max_by(|a, b| a.1 .1.total_cmp(&b.1 .1)).map_or(0, |(k, _)| k)
};
if crest == 0 || crest + 1 >= left_path.len() {
return Err(format!("the left strip of the slice at r = {r} has its crest at an end"));
}
let mut left = resample(&left_path[..=crest], before);
left.extend(resample(&left_path[crest..], after));
let mut middle = vec![exit];
middle.extend_from_slice(&bottom[enter..end]);
middle.push(target);
if middle.len() < 3 {
return Err(format!("the slice at r = {r} is narrower than its strips"));
}
let mut out = Vec::with_capacity(count);
out.extend((0..walls).map(|k| (lo, z1 + (first.1 - z1) * k as f64 / walls as f64)));
out.extend_from_slice(&left);
out.extend(resample(&middle, middle_count));
out.extend(left.iter().map(|&(f, z)| (f + pitch, z)));
out.extend((0..walls).map(|k| (hi, exit.1 + (z1 - exit.1) * k as f64 / walls as f64)));
out.extend((0..top).map(|k| (hi + (lo - hi) * k as f64 / top as f64, z1)));
Ok(out)
}
fn sides(count: usize) -> [usize; 6] {
let (walls, top, (before, after)) = ((count / 18).max(2), (count / 12).max(2), strip_counts(count));
[0, walls, walls + before, count - top - walls - after, count - top - walls, count - top]
}
fn strip_counts(count: usize) -> (usize, usize) {
let strips = (count / 15).max(4);
(strips / 2, strips - strips / 2)
}
fn closed_polyline(points: &[Vec3], breaks: &[usize]) -> Result<Vec<crate::NurbsCurve>, String> {
let n = points.len();
let open = |from: usize, to: usize| -> Result<crate::NurbsCurve, String> {
let m = to - from;
let mut knots = vec![0.0];
knots.extend((0..=m).map(|k| k as f64 / m as f64));
knots.push(1.0);
crate::NurbsCurve::new(1, knots, (from..=to).map(|k| crate::Vec4::from_point(points[k % n], 1.0)).collect())
};
(0..breaks.len()).map(|k| open(breaks[k], if k + 1 < breaks.len() { breaks[k + 1] } else { n + breaks[0] })).collect()
}
pub fn tooth_space(tool: &ThreadTool, motion: &TwoTurns, blank: &RingBlank, pitch: f64, radii: &[f64], seed: f64, count: usize) -> Result<crate::BrepSolid, crate::KernelRefusal> {
Ok(traced_space(tool, motion, blank, Family::Cylinders, pitch, radii, seed, count)?.0)
}
fn traced_space(tool: &ThreadTool, motion: &TwoTurns, blank: &RingBlank, family: Family, pitch: f64, radii: &[f64], seed: f64, count: usize) -> Result<(crate::BrepSolid, Vec<f64>), crate::KernelRefusal> {
let fail = |message: String| crate::KernelRefusal::unsupported(crate::KernelStage::Classify, "thread_space", format!("threadSweep: {message}"));
tool.check().map_err(fail)?;
if radii.len() < 2 || count < 8 {
return Err(fail("a space needs two slices or more, of eight points or more".into()));
}
let mid = radii.len() / 2;
let mut sections: Vec<Vec<Vec3>> = vec![Vec::new(); radii.len()];
let mut centres = vec![0.0; radii.len()];
let (first, centre, delta) = slice(tool, motion, blank, family, pitch, radii[mid], seed, None, count).map_err(fail)?;
sections[mid] = first;
centres[mid] = divider_angle(centre, delta, pitch);
let middle_delta = delta;
let (mut near, mut strip) = (centre, middle_delta);
for k in (0..mid).rev() {
let (points, c, delta) = slice(tool, motion, blank, family, pitch, radii[k], near, Some((near, strip)), count).map_err(fail)?;
sections[k] = points;
(centres[k], near, strip) = (divider_angle(c, delta, pitch), c, delta);
}
(near, strip) = (centre, middle_delta);
for k in mid + 1..radii.len() {
let (points, c, delta) = slice(tool, motion, blank, family, pitch, radii[k], near, Some((near, strip)), count).map_err(fail)?;
sections[k] = points;
(centres[k], near, strip) = (divider_angle(c, delta, pitch), c, delta);
}
let (o, d, _, _) = gear_frame(motion);
let (low, high) = match family {
Family::Cylinders => (blank.inner, blank.outer),
Family::Planes => (blank.bottom, blank.face),
};
if !(radii[0] < low && radii[radii.len() - 1] > high) {
return Err(fail(format!("the end slices ({} and {}) must lie beyond the blank ({low} and {high})", radii[0], radii[radii.len() - 1])));
}
let ends: &[(usize, bool)] = if family == Family::Cylinders { &[(0, false), (radii.len() - 1, true)] } else { &[] };
for &(k, outer) in ends {
let centre = sections[k].iter().fold(Vec3::new(0.0, 0.0, 0.0), |a, q| a.add(*q)).scale(1.0 / count as f64);
let off = centre.sub(o);
let normal = off.sub(d.scale(off.dot(d))).normalized().map_err(|e| fail(e))?;
let widest = sections[k]
.iter()
.map(|q| {
let rel = q.sub(o);
let flat = rel.sub(d.scale(rel.dot(d)));
(flat.dot(normal) / flat.length()).clamp(-1.0, 1.0)
})
.fold(1.0f64, f64::min);
let distance = if outer { radii[k].max(blank.outer) } else { radii[k].min(blank.inner * widest * (1.0 - 1e-9)) };
for q in sections[k].iter_mut() {
let rel = q.sub(o);
let along = d.scale(rel.dot(d));
let flat = rel.sub(along);
*q = o.add(along).add(flat.scale(distance / flat.dot(normal)));
}
}
let profiles: Vec<Vec<crate::NurbsCurve>> = sections.iter().map(|s| closed_polyline(s, &sides(count))).collect::<Result<_, _>>().map_err(fail)?;
let skin = if profiles.len() >= 4 { crate::loft_topology::loft_profile_brep_broken(&profiles, &[1, profiles.len() - 2])? } else { crate::loft_profile_brep(&profiles)? };
Ok((skin, centres))
}
pub fn thread_gear(tool: &ThreadTool, motion: &TwoTurns, blank: &RingBlank, teeth: usize) -> Result<crate::BrepSolid, crate::KernelRefusal> {
let width = blank.outer - blank.inner;
let (from, to) = (blank.inner - width / 30.0, blank.outer + width / 30.0);
let mut radii = vec![blank.inner - width / 12.0];
radii.extend((0..13).map(|k| from + (to - from) * k as f64 / 12.0));
radii.push(blank.outer + width / 12.0);
thread_cut_gear(tool, motion, blank, teeth, &radii, 0.5 * TAU / teeth.max(1) as f64, 180)
}
pub fn thread_wheel(tool: &ThreadTool, motion: &TwoTurns, blank: &RingBlank, teeth: usize) -> Result<crate::BrepSolid, crate::KernelRefusal> {
let thick = blank.face - blank.bottom;
let layouts = [(13usize, 0.7), (17, 0.3), (11, 0.5)];
let mut last = None;
for perturbation in [false, true] {
for end in [thick / 12.0, thick / 24.0, thick / 48.0] {
for (inner, beyond) in layouts {
let (from, to) = (blank.bottom - end * beyond, blank.face + end * beyond);
let mut levels = vec![blank.bottom - end];
levels.extend((0..inner).map(|k| from + (to - from) * k as f64 / (inner - 1) as f64));
levels.push(blank.face + end);
match thread_cut_wheel_with(tool, motion, blank, teeth, &levels, 0.5 * TAU / teeth.max(1) as f64, 180, perturbation) {
Ok(wheel) => return Ok(wheel),
Err(refusal) => last = Some(refusal),
}
}
}
}
Err(last.expect("layouts tried"))
}
pub fn thread_cut_wheel(tool: &ThreadTool, motion: &TwoTurns, blank: &RingBlank, teeth: usize, levels: &[f64], seed: f64, count: usize) -> Result<crate::BrepSolid, crate::KernelRefusal> {
thread_cut_wheel_with(tool, motion, blank, teeth, levels, seed, count, true)
}
#[allow(clippy::too_many_arguments)]
fn thread_cut_wheel_with(tool: &ThreadTool, motion: &TwoTurns, blank: &RingBlank, teeth: usize, levels: &[f64], seed: f64, count: usize, perturbation: bool) -> Result<crate::BrepSolid, crate::KernelRefusal> {
let boolean_options = crate::BooleanOptions::default();
let boolean = |a: &crate::BrepSolid, b: &crate::BrepSolid, operation: crate::BooleanOperation| {
if perturbation { crate::boolean_operation(a, b, operation, &boolean_options) } else { crate::without_perturbation(|| crate::boolean_operation(a, b, operation, &boolean_options)) }
};
let fail = |message: String| crate::KernelRefusal::unsupported(crate::KernelStage::Classify, "thread_wheel", format!("threadSweep: {message}"));
if teeth < 2 || !(blank.inner >= 0.0 && blank.outer > blank.inner && blank.face > blank.bottom) {
return Err(fail("a wheel needs two teeth or more and a blank of positive size".into()));
}
let pitch = TAU / teeth as f64;
let (space, dividers) = traced_space(tool, motion, blank, Family::Planes, pitch, levels, seed, count)?;
let (o, d, e1, e2) = gear_frame(motion);
let middle: Vec<(f64, f64)> = levels.iter().copied().zip(dividers.iter().copied()).collect();
let at = |z: f64| -> (f64, f64) {
let k = (1..middle.len()).find(|&k| middle[k].0 >= z).unwrap_or(middle.len() - 1);
let (a, b) = (middle[k - 1], middle[k]);
(z, a.1 + (b.1 - a.1) * (z - a.0) / (b.0 - a.0))
};
let thick = blank.face - blank.bottom;
let (low, high) = (blank.bottom - thick / 24.0, blank.face + thick / 24.0);
let gap = 0.02 * thick;
let mut plan = vec![at(low)];
plan.extend(middle.iter().copied().filter(|m| m.0 > low + gap && m.0 < high - gap));
plan.push(at(high));
if plan.len() < 4 {
return Err(fail("the slices give fewer than four divider angles across the wheel".into()));
}
let line = |a: Vec3, b: Vec3| crate::NurbsCurve::new(1, vec![0.0, 0.0, 1.0, 1.0], vec![crate::Vec4::from_point(a, 1.0), crate::Vec4::from_point(b, 1.0)]);
let reach = 1.25 * blank.outer;
let mut sections = Vec::with_capacity(plan.len());
for &(z, a) in &plan {
let b = a - pitch;
let centre = o.add(d.scale(z));
let point = |rho: f64, angle: f64| centre.add(e1.scale(rho * angle.cos())).add(e2.scale(rho * angle.sin()));
sections.push(vec![
line(centre, point(reach, b)).map_err(fail)?,
crate::make_arc(centre, e1, e2, reach, b, a).map_err(fail)?,
line(point(reach, a), centre).map_err(fail)?,
]);
}
let wedge = crate::loft_profile_brep(§ions)?;
let away = 0.5 * (plan[0].1 + plan[plan.len() - 1].1) - 0.5 * pitch + PI;
let radial = e1.scale(away.cos()).add(e2.scale(away.sin()));
let at_rz = |rho: f64, z: f64| o.add(radial.scale(rho)).add(d.scale(z));
let corners = [at_rz(blank.inner, blank.bottom), at_rz(blank.outer, blank.bottom), at_rz(blank.outer, blank.face), at_rz(blank.inner, blank.face)];
let profile: Vec<crate::NurbsCurve> = (0..4).map(|k| line(corners[k], corners[(k + 1) % 4])).collect::<Result<_, _>>().map_err(fail)?;
let whole = crate::revolve_profile_brep(&profile, o, d, TAU).map_err(fail)?;
let piece = boolean(&whole, &wedge, crate::BooleanOperation::Intersect)?;
let [piece_shell] = wedge.shells.as_slice() else {
return Err(fail("the wheel's cell is not one shell".into()));
};
let samples = |solid: &crate::BrepSolid, face: &crate::topology::FaceRecord| -> Vec<Vec3> {
let vertex = |id: u64| solid.vertices.iter().find(|v| v.id == id).map(|v| v.point);
let mut points = Vec::new();
for e in face.loops.iter().flat_map(|l| &l.coedges).filter_map(|c| solid.edges.iter().find(|e| e.id == c.edge_id)) {
points.extend([vertex(e.start_vertex_id), vertex(e.end_vertex_id)].into_iter().flatten());
points.extend((1..4).filter_map(|k| e.curve.evaluate(e.t0 + (e.t1 - e.t0) * k as f64 / 4.0).ok()));
}
points
};
let radius = |q: Vec3| {
let rel = q.sub(o);
rel.sub(d.scale(rel.dot(d))).length()
};
let height = |q: Vec3| q.sub(o).dot(d);
let divider_surfaces: Vec<&crate::NurbsSurface> = piece_shell
.faces
.iter()
.filter(|face| {
let points = samples(&wedge, face);
let spread = |f: &dyn Fn(Vec3) -> f64| points.iter().map(|&q| f(q)).fold(f64::NEG_INFINITY, f64::max) - points.iter().map(|&q| f(q)).fold(f64::INFINITY, f64::min);
spread(&radius) > 0.5 * reach && spread(&height) > 0.5 * thick
})
.map(|face| &face.surface)
.collect();
if divider_surfaces.len() != 2 {
return Err(fail(format!("the wheel's cell has {} divider faces, not two", divider_surfaces.len())));
}
let cell = boolean(&piece, &space, crate::BooleanOperation::Subtract)?;
let [shell] = cell.shells.as_slice() else {
return Err(fail("the cut cell is not one shell".into()));
};
let tolerance = 1e-7 * blank.outer;
let on = |q: Vec3| divider_surfaces.iter().any(|surface| crate::project_point_to_surface(surface, q).is_ok_and(|f| f.distance < tolerance));
let mut kept = Vec::new();
let mut dropped = 0;
for face in &shell.faces {
let points = samples(&cell, face);
if !points.is_empty() && points.iter().all(|&q| on(q)) {
dropped += 1;
} else {
kept.push(face.id);
}
}
if dropped == 0 {
return Err(fail("no face of the cut cell lies on its dividers".into()));
}
let (wheel, report, _) = crate::sew_rotated_copies(&cell, &kept, o, d, pitch, teeth, (2e-8 * blank.outer).max(1e-6))?;
if let Some(issue) = report.issues.first() {
return Err(fail(format!("the cells do not sew into one valid solid: {issue}")));
}
Ok(wheel)
}
fn divider_angle(centre: f64, delta: f64, pitch: f64) -> f64 {
centre + 0.5 * pitch + 0.4 * delta
}
pub fn thread_cut_gear(tool: &ThreadTool, motion: &TwoTurns, blank: &RingBlank, teeth: usize, radii: &[f64], seed: f64, count: usize) -> Result<crate::BrepSolid, crate::KernelRefusal> {
let fail = |message: String| crate::KernelRefusal::unsupported(crate::KernelStage::Classify, "thread_gear", format!("threadSweep: {message}"));
if teeth < 2 || !(blank.inner > 0.0 && blank.outer > blank.inner && blank.face.min(blank.face_outer) > blank.bottom) {
return Err(fail("a gear needs two teeth or more and a ring of positive size".into()));
}
let pitch = TAU / teeth as f64;
let (space, dividers) = traced_space(tool, motion, blank, Family::Cylinders, pitch, radii, seed, count)?;
let (o, d, e1, e2) = gear_frame(motion);
let middle: Vec<(f64, f64)> = radii.iter().copied().zip(dividers.iter().copied()).collect();
let at = |r: f64| -> (f64, f64) {
let k = (1..middle.len()).find(|&k| middle[k].0 >= r).unwrap_or(middle.len() - 1);
let (a, b) = (middle[k - 1], middle[k]);
(r, a.1 + (b.1 - a.1) * (r - a.0) / (b.0 - a.0))
};
let gap = 0.02 * (blank.outer - blank.inner);
let mut plan = vec![at(blank.inner)];
plan.extend(middle.iter().copied().filter(|m| m.0 > blank.inner + gap && m.0 < blank.outer - gap));
plan.push(at(blank.outer));
if plan.len() < 4 {
return Err(fail("the slices give fewer than four tooth middles inside the ring".into()));
}
let floor = o.add(d.scale(blank.bottom));
let point = |r: f64, a: f64| floor.add(e1.scale(r * a.cos())).add(e2.scale(r * a.sin()));
let divider = |shift: f64| -> Result<crate::NurbsCurve, String> {
let points: Vec<Vec3> = plan.iter().map(|&(r, a)| point(r, a - shift)).collect();
let mut parameters = vec![0.0];
for k in 1..points.len() {
parameters.push(parameters[k - 1] + points[k].sub(points[k - 1]).length());
}
let total = parameters[parameters.len() - 1];
crate::interpolate_curve(&points, 3, ¶meters.iter().map(|t| t / total).collect::<Vec<_>>())
};
let (ahead, behind) = (divider(0.0).map_err(fail)?, divider(pitch).map_err(fail)?);
let (a_in, a_out) = (plan[0].1, plan[plan.len() - 1].1);
let (b_in, b_out) = (a_in - pitch, a_out - pitch);
let rim = crate::make_arc(floor, e1, e2, blank.outer, b_out, a_out).map_err(fail)?;
let bore = crate::make_arc(floor, e1, e2, blank.inner, b_in, a_in).map_err(fail)?;
let section = vec![ahead.clone(), rim.reversed().map_err(fail)?, behind.reversed().map_err(fail)?, bore];
let top = blank.face.max(blank.face_outer);
let piece = crate::extrude_profile_brep(§ion, d, top - blank.bottom)?;
let mut operands = vec![piece];
if blank.face_outer != blank.face {
let depth = tool.tip + tool.root;
let width = blank.outer - blank.inner;
let (near, far) = (blank.inner - 0.25 * width, blank.outer + 0.25 * width);
let away = 0.5 * (a_in + a_out) + PI;
let radial = e1.scale(away.cos()).add(e2.scale(away.sin()));
let at = |rho: f64, z: f64| o.add(radial.scale(rho)).add(d.scale(z));
let corners = [at(near, blank.face_at(near)), at(far, blank.face_at(far)), at(far, top + depth), at(near, top + depth)];
let profile: Vec<crate::NurbsCurve> = (0..4)
.map(|k| crate::NurbsCurve::new(1, vec![0.0, 0.0, 1.0, 1.0], vec![crate::Vec4::from_point(corners[k], 1.0), crate::Vec4::from_point(corners[(k + 1) % 4], 1.0)]))
.collect::<Result<_, _>>()
.map_err(fail)?;
operands.push(crate::revolve_profile_brep(&profile, o, d, TAU).map_err(fail)?);
}
operands.push(space.clone());
let mut cell = operands[0].clone();
for cutter in &operands[1..] {
cell = crate::boolean_operation(&cell, cutter, crate::BooleanOperation::Subtract, &crate::BooleanOptions::default())?;
}
let tolerance = 1e-7 * blank.outer;
let on = |q: Vec3| {
let flat = q.sub(d.scale(q.sub(floor).dot(d)));
[&ahead, &behind].iter().any(|c| crate::project_point_to_curve(c, flat).is_ok_and(|f| f.distance < tolerance))
};
let [shell] = cell.shells.as_slice() else {
return Err(fail("the cut cell is not one shell".into()));
};
let vertex = |id: u64| cell.vertices.iter().find(|v| v.id == id).map(|v| v.point);
let edge = |id: u64| cell.edges.iter().find(|e| e.id == id);
let mut kept = Vec::new();
let mut dropped = 0;
for face in &shell.faces {
let mut points = Vec::new();
for e in face.loops.iter().flat_map(|l| &l.coedges).filter_map(|c| edge(c.edge_id)) {
points.extend([vertex(e.start_vertex_id), vertex(e.end_vertex_id)].into_iter().flatten());
points.extend((1..4).filter_map(|k| e.curve.evaluate(e.t0 + (e.t1 - e.t0) * k as f64 / 4.0).ok()));
}
if !points.is_empty() && points.iter().all(|&q| on(q)) {
dropped += 1;
} else {
kept.push(face.id);
}
}
if dropped == 0 {
return Err(fail("no face of the cut cell lies on its dividers".into()));
}
let (gear, report, _) = crate::sew_rotated_copies(&cell, &kept, o, d, pitch, teeth, (2e-8 * blank.outer).max(1e-6))?;
if let Some(issue) = report.issues.first() {
return Err(fail(format!("the cells do not sew into one valid solid: {issue}")));
}
Ok(gear)
}
#[doc(hidden)]
pub fn wrapped(angle: f64) -> f64 {
(angle + PI).rem_euclid(TAU) - PI
}