use crate::{FrameCurve, OutlinePiece, SolidTool, Vec3};
pub const ENVELOPE: f64 = 1e6;
pub fn upright_slot_on() -> bool {
crate::switch!("BREP_SWEEP_UPRIGHT_SLOT", false)
}
pub fn sweep_upright_along(tool: &SolidTool, path: &FrameCurve) -> Option<Result<crate::BrepSolid, crate::KernelRefusal>> {
upright_slot(tool, path).map(|slot| slot.solid())
}
#[derive(Clone, Debug)]
pub struct UprightSlot {
pub origin: Vec3,
pub x: Vec3,
pub y: Vec3,
pub up: Vec3,
pub curve: [[f64; 2]; 4],
pub meridian: Vec<OutlinePiece>,
pub heights: [f64; 2],
}
fn turns_within(a: f64, lo: f64, hi: f64) -> impl Iterator<Item = f64> {
let tau = std::f64::consts::TAU;
let (first, last) = (((lo - a) / tau).ceil(), ((hi - a) / tau).floor());
let count = if first.is_finite() && last.is_finite() && last >= first && last - first < 1e6 { (last - first) as i64 + 1 } else { 0 };
(0..count).map(move |k| a + tau * (first + k as f64))
}
fn piece_extent(piece: &OutlinePiece) -> (f64, f64) {
match *piece {
OutlinePiece::Line { from, to } => (from[0].abs().max(to[0].abs()), from[1].abs().max(to[1].abs())),
OutlinePiece::Arc { centre, radius, start, end } => {
let (a, b) = (start.min(end), start.max(end));
let at = |t: f64| [centre[0] + radius * t.cos(), centre[1] + radius * t.sin()];
let mut points = vec![at(start), at(end)];
for k in [0.0, std::f64::consts::FRAC_PI_2, std::f64::consts::PI, -std::f64::consts::FRAC_PI_2] {
points.extend(turns_within(k, a, b).take(1).map(at));
}
points.iter().fold((0.0f64, 0.0f64), |(r, h), p| (r.max(p[0].abs()), h.max(p[1].abs())))
}
OutlinePiece::Ellipse { .. } => (f64::INFINITY, f64::INFINITY),
}
}
fn sub(a: [f64; 2], b: [f64; 2]) -> [f64; 2] {
[a[0] - b[0], a[1] - b[1]]
}
fn dot(a: [f64; 2], b: [f64; 2]) -> f64 {
a[0] * b[0] + a[1] * b[1]
}
pub fn upright_slot(tool: &SolidTool, path: &FrameCurve) -> Option<UprightSlot> {
let SolidTool::Revolved { axis_origin, axis_direction, radial, meridian } = tool else { return None };
let finite = |p: Vec3| p.x.is_finite() && p.y.is_finite() && p.z.is_finite();
let pieces_finite = meridian.iter().all(|piece| match *piece {
OutlinePiece::Line { from, to } => from.iter().chain(&to).all(|x| x.is_finite()),
OutlinePiece::Arc { centre, radius, start, end } => centre.iter().all(|x| x.is_finite()) && radius.is_finite() && start.is_finite() && end.is_finite(),
OutlinePiece::Ellipse { .. } => false,
});
if !(pieces_finite && path.control.iter().all(|p| finite(*p)) && finite(path.up) && finite(*axis_origin) && finite(*axis_direction) && finite(*radial)) {
return None;
}
let unit = |d: Vec3| -> Option<Vec3> {
let big = d.x.abs().max(d.y.abs()).max(d.z.abs());
if !(big > 0.0 && big.is_finite()) {
return None;
}
let n = d.scale(1.0 / big).normalized().ok()?;
((n.length() - 1.0).abs() <= 1e-12).then_some(n)
};
let up = unit(path.up)?;
let axis = unit(*axis_direction)?;
let along = axis.dot(up).signum();
let start = path.control[0];
let (widest, tall) = meridian.iter().map(piece_extent).fold((0.0f64, 0.0f64), |(r, h), (pr, ph)| (r.max(pr), h.max(ph)));
if !(widest > 0.0 && widest.is_finite() && tall.is_finite()) {
return None;
}
if meridian.iter().any(|piece| matches!(*piece, OutlinePiece::Arc { radius, .. } if !(radius > 0.0 && radius <= ENVELOPE * widest))) {
return None;
}
let extent = path.control.iter().map(|p| p.sub(start).length()).fold(0.0f64, f64::max);
let one_turn = meridian.iter().all(|piece| match *piece {
OutlinePiece::Arc { start, end, .. } => (end - start).abs() <= std::f64::consts::TAU * (1.0 + 1e-12),
_ => true,
});
let sized = |x: f64| (1e-60..=1e60).contains(&x);
if !(one_turn && sized(widest) && sized(extent) && sized(tall.max(widest)) && tall <= ENVELOPE * widest && tall >= widest / ENVELOPE && extent <= ENVELOPE * widest && extent >= widest / ENVELOPE) {
return None;
}
let band = 1e-12 * widest;
if axis.cross(up).length() * tall.max(widest) > band {
return None;
}
let off = axis_origin.sub(start);
if off.sub(up.scale(off.dot(up))).length() > band {
return None;
}
if path.control.iter().any(|p| p.sub(start).dot(up).abs() > band) {
return None;
}
let radial_unit = unit(*radial)?;
if radial_unit.dot(axis).abs() > 1e-12 {
return None;
}
if path.control.iter().all(|p| p.sub(start).length() <= band) {
return None;
}
let lift = off.dot(up);
let meridian: Vec<OutlinePiece> = meridian
.iter()
.map(|piece| match *piece {
OutlinePiece::Line { from, to } => Some(OutlinePiece::Line { from: [from[0], lift + along * from[1]], to: [to[0], lift + along * to[1]] }),
OutlinePiece::Arc { centre, radius, start, end } => {
if along > 0.0 {
Some(OutlinePiece::Arc { centre: [centre[0], lift + centre[1]], radius, start, end })
} else {
Some(OutlinePiece::Arc { centre: [centre[0], lift - centre[1]], radius, start: -start, end: -end })
}
}
OutlinePiece::Ellipse { .. } => None,
})
.collect::<Option<_>>()?;
let tangent = path.control[1].sub(start);
let x = tangent.sub(up.scale(tangent.dot(up))).normalized().or_else(|_| up.perpendicular()).ok()?;
let y = up.cross(x);
let flat = |p: Vec3| [p.sub(start).dot(x), p.sub(start).dot(y)];
let mut slot = UprightSlot { origin: start, x, y, up, curve: path.control.map(flat), meridian, heights: [0.0; 2] };
if !slot.regular() {
return None;
}
let ends = slot.meridian.iter().flat_map(|piece| match *piece {
OutlinePiece::Line { from, to } => vec![from[1], to[1]],
OutlinePiece::Arc { centre, radius, start, end } => {
let mut hs = vec![centre[1] + radius * start.sin(), centre[1] + radius * end.sin()];
for a in turns_within(std::f64::consts::FRAC_PI_2, start.min(end), start.max(end)).chain(turns_within(-std::f64::consts::FRAC_PI_2, start.min(end), start.max(end))) {
hs.push(centre[1] + radius * a.sin());
}
hs
}
OutlinePiece::Ellipse { .. } => vec![],
});
let (low, high) = ends.fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), h| (lo.min(h), hi.max(h)));
if !(high > low) {
return None;
}
slot.heights = [low, high];
if !slot.sections_are_discs() {
return None;
}
Some(slot)
}
impl UprightSlot {
fn sections_are_discs(&self) -> bool {
let [low, high] = self.heights;
let tol = 1e-12 * (high - low).abs().max(1e-300);
let tol_r = 1e-12 * self.widest().max(1e-300);
let ends = |piece: &OutlinePiece| -> Option<([f64; 2], [f64; 2])> {
match *piece {
OutlinePiece::Line { from, to } => Some((from, to)),
OutlinePiece::Arc { centre, radius, start, end } => Some((
[centre[0] + radius * start.cos(), centre[1] + radius * start.sin()],
[centre[0] + radius * end.cos(), centre[1] + radius * end.sin()],
)),
OutlinePiece::Ellipse { .. } => None,
}
};
let on_axis = |p: [f64; 2]| p[0].abs() <= tol_r;
let axis_piece = |piece: &OutlinePiece| matches!(*piece, OutlinePiece::Line { from, to } if on_axis(from) && on_axis(to) && (from[1] - to[1]).abs() >= high - low - tol);
let Some(axis_index) = self.meridian.iter().position(axis_piece) else { return false };
let n = self.meridian.len();
let chain: Vec<&OutlinePiece> = (1..n).map(|k| &self.meridian[(axis_index + k) % n]).collect();
let Some((first, _)) = chain.first().and_then(|p| ends(p)) else { return false };
let Some((_, last)) = chain.last().and_then(|p| ends(p)) else { return false };
let Some((axis_from, axis_to)) = ends(&self.meridian[axis_index]) else { return false };
let same = |a: [f64; 2], b: [f64; 2]| (a[0] - b[0]).abs() <= tol_r && (a[1] - b[1]).abs() <= tol;
if !(same(first, axis_to) && same(last, axis_from)) {
return false;
}
let rising = last[1] > first[1];
let mut previous = first;
let last_index = chain.len() - 1;
for (index, piece) in chain.iter().enumerate() {
let Some((from, to)) = ends(piece) else { return false };
if (from[0] - previous[0]).abs() > tol_r || (from[1] - previous[1]).abs() > tol {
return false;
}
let monotone = match **piece {
OutlinePiece::Line { from, to } => {
let dh = to[1] - from[1];
if dh.abs() <= tol_r.min(tol) {
(index == 0 || index == last_index) && ((from[1] - low).abs() <= tol || (from[1] - high).abs() <= tol)
} else {
(dh > 0.0) == rising
}
}
OutlinePiece::Arc { radius, start, end, .. } => {
let (a, b) = (start.min(end), start.max(end));
let inner = 1e-12 * (b - a).abs().max(1.0);
let turns = turns_within(std::f64::consts::FRAC_PI_2, a + inner, b - inner)
.chain(turns_within(-std::f64::consts::FRAC_PI_2, a + inner, b - inner))
.next()
.is_some();
!turns && radius > 0.0 && ((to[1] - from[1] > 0.0) == rising || (to[1] - from[1]).abs() <= tol)
}
OutlinePiece::Ellipse { .. } => false,
};
if !monotone {
return false;
}
if index != last_index && on_axis(to) {
return false;
}
let off = match **piece {
OutlinePiece::Line { from, to } => from[0] >= -tol_r && to[0] >= -tol_r,
OutlinePiece::Arc { centre, radius, start, end } => {
let (a, b) = (start.min(end), start.max(end));
let inner = 1e-12 * (b - a).abs().max(1.0);
let ends_in = centre[0] + radius * start.cos() >= -tol_r && centre[0] + radius * end.cos() >= -tol_r;
ends_in && (turns_within(std::f64::consts::PI, a + inner, b - inner).next().is_none() || centre[0] - radius > tol_r)
}
OutlinePiece::Ellipse { .. } => false,
};
if !off {
return false;
}
previous = to;
}
true
}
fn regular(&self) -> bool {
let c = &self.curve;
let polygon = (0..3).map(|k| sub(c[k + 1], c[k])).map(|d| 3.0 * dot(d, d).sqrt()).fold(0.0f64, f64::max);
if !(polygon > 0.0) {
return false;
}
let speed2 = |t: f64| {
let d = self.derivative(t);
dot(d, d)
};
let slope = |t: f64| dot(self.derivative(t), self.second(t));
let mut least = speed2(0.0).min(speed2(1.0));
let n = 256;
let mut previous = slope(0.0);
for k in 1..=n {
let t1 = k as f64 / n as f64;
let current = slope(t1);
if (previous < 0.0) != (current < 0.0) {
let (mut a, mut b, mut sa) = ((k - 1) as f64 / n as f64, t1, previous);
for _ in 0..100 {
let m = 0.5 * (a + b);
let sm = slope(m);
if (sm < 0.0) == (sa < 0.0) {
a = m;
sa = sm;
} else {
b = m;
}
}
least = least.min(speed2(0.5 * (a + b)));
}
previous = current;
}
least.sqrt() >= 1e-4 * polygon
}
fn crossings(&self, h: f64) -> Vec<f64> {
let mut found: Vec<f64> = Vec::new();
for piece in &self.meridian {
match *piece {
OutlinePiece::Line { from, to } => {
let (a, b) = (from[1] - h, to[1] - h);
if a == 0.0 && b == 0.0 {
continue;
}
if (a <= 0.0 && b >= 0.0) || (a >= 0.0 && b <= 0.0) {
let s = a / (a - b);
found.push(from[0] + s * (to[0] - from[0]));
}
}
OutlinePiece::Arc { centre, radius, start, end } => {
let s = (h - centre[1]) / radius;
if s.abs() > 1.0 {
continue;
}
let base = s.asin();
for a in [base, std::f64::consts::PI - base] {
for angle in turns_within(a, start.min(end), start.max(end)) {
found.push(centre[0] + radius * angle.cos());
}
}
}
OutlinePiece::Ellipse { .. } => {}
}
}
let widest = found.iter().fold(0.0f64, |m, r| m.max(r.abs()));
let floor = 1e-12 * widest;
let mut off: Vec<f64> = found.into_iter().filter(|r| *r > floor).collect();
off.sort_by(f64::total_cmp);
off.dedup_by(|a, b| (*a - *b).abs() <= 1e-12 * a.abs());
off
}
pub fn radius(&self, h: f64) -> f64 {
if !(h >= self.heights[0] && h <= self.heights[1]) {
return 0.0;
}
self.crossings(h).last().copied().unwrap_or(0.0)
}
fn point(&self, t: f64) -> [f64; 2] {
let c = &self.curve;
let s = 1.0 - t;
let w = [s * s * s, 3.0 * s * s * t, 3.0 * s * t * t, t * t * t];
[(0..4).map(|k| w[k] * c[k][0]).sum(), (0..4).map(|k| w[k] * c[k][1]).sum()]
}
fn derivative(&self, t: f64) -> [f64; 2] {
let c = &self.curve;
let s = 1.0 - t;
let w = [3.0 * s * s, 6.0 * s * t, 3.0 * t * t];
let d = [sub(c[1], c[0]), sub(c[2], c[1]), sub(c[3], c[2])];
[(0..3).map(|k| w[k] * d[k][0]).sum(), (0..3).map(|k| w[k] * d[k][1]).sum()]
}
fn second(&self, t: f64) -> [f64; 2] {
let c = &self.curve;
let a = [c[2][0] - 2.0 * c[1][0] + c[0][0], c[2][1] - 2.0 * c[1][1] + c[0][1]];
let b = [c[3][0] - 2.0 * c[2][0] + c[1][0], c[3][1] - 2.0 * c[2][1] + c[1][1]];
[6.0 * ((1.0 - t) * a[0] + t * b[0]), 6.0 * ((1.0 - t) * a[1] + t * b[1])]
}
pub fn distance(&self, q: [f64; 2]) -> (f64, f64) {
let at = |t: f64| {
let r = sub(self.point(t), q);
dot(r, r)
};
let mut best = (at(0.0), 0.0);
let end = at(1.0);
if end < best.0 {
best = (end, 1.0);
}
for t in self.stationary(q) {
let d2 = at(t);
if d2 < best.0 {
best = (d2, t);
}
}
(best.0.sqrt(), best.1)
}
fn stationary(&self, q: [f64; 2]) -> Vec<f64> {
let c = &self.curve;
let mut g = [0.0f64; 6];
for k in 0..2 {
let (p0, p1, p2, p3) = (c[0][k], c[1][k], c[2][k], c[3][k]);
let a = [p0 - q[k], 3.0 * (p1 - p0), 3.0 * (p2 - 2.0 * p1 + p0), p3 - 3.0 * p2 + 3.0 * p1 - p0];
let d = [a[1], 2.0 * a[2], 3.0 * a[3]];
for i in 0..4 {
for j in 0..3 {
g[i + j] += a[i] * d[j];
}
}
}
let choose = |n: usize, r: usize| -> f64 { (0..r).fold(1.0, |acc, i| acc * (n - i) as f64 / (i + 1) as f64) };
let mut bern = [0.0f64; 6];
for k in 0..6 {
bern[k] = (0..=k).map(|i| choose(k, i) / choose(5, i) * g[i]).sum();
}
let value = |t: f64| g.iter().rev().fold(0.0, |acc, &x| acc * t + x);
let size = bern.iter().fold(0.0f64, |m, b| m.max(b.abs()));
if !(size > 0.0 && size.is_finite()) {
return Vec::new();
}
let mut roots = Vec::new();
let mut spans = 0usize;
let mut stack: Vec<([f64; 6], f64, f64, u32)> = vec![(bern, 0.0, 1.0, 0)];
while let Some((b, lo, hi, depth)) = stack.pop() {
spans += 1;
if spans > 4096 {
roots.push(0.5 * (lo + hi));
continue;
}
let changes = (0..5).filter(|&i| (b[i] < 0.0) != (b[i + 1] < 0.0) || b[i] == 0.0).count() + usize::from(b[5] == 0.0);
if changes == 0 {
continue;
}
let (glo, ghi) = (value(lo), value(hi));
if changes == 1 && (glo < 0.0) != (ghi < 0.0) {
let (mut a, mut z, mut ga) = (lo, hi, glo);
for _ in 0..200 {
let m = 0.5 * (a + z);
if m <= a || m >= z {
break;
}
let gm = value(m);
if gm == 0.0 {
a = m;
z = m;
break;
}
if (gm < 0.0) == (ga < 0.0) {
a = m;
ga = gm;
} else {
z = m;
}
}
let mut t = 0.5 * (a + z);
for _ in 0..4 {
let r = sub(self.point(t), q);
let d = self.derivative(t);
let slope = dot(d, d) + dot(r, self.second(t));
if !(slope != 0.0 && slope.is_finite()) {
break;
}
let next = t - dot(r, d) / slope;
if !(next >= lo && next <= hi) {
break;
}
t = next;
}
roots.push(t);
continue;
}
if depth >= 60 || hi - lo <= 1e-15 {
roots.push(0.5 * (lo + hi));
continue;
}
let mut left = [0.0f64; 6];
let mut right = [0.0f64; 6];
let mut work = b;
for level in 0..6 {
left[level] = work[0];
right[5 - level] = work[5 - level];
for i in 0..5 - level {
work[i] = 0.5 * (work[i] + work[i + 1]);
}
}
let mid = 0.5 * (lo + hi);
stack.push((right, mid, hi, depth + 1));
stack.push((left, lo, mid, depth + 1));
}
roots
}
pub fn contains(&self, p: Vec3) -> bool {
let r = p.sub(self.origin);
let h = r.dot(self.up);
if !(h >= self.heights[0] && h <= self.heights[1]) {
return false;
}
self.distance([r.dot(self.x), r.dot(self.y)]).0 <= self.radius(h)
}
pub fn level_loops(&self, rho: f64, cell: f64) -> Vec<Vec<[f64; 2]>> {
if !(cell > 0.0 && cell.is_finite() && rho >= 0.0 && rho.is_finite()) {
return Vec::new();
}
let (lo, hi) = self.curve.iter().fold(([f64::INFINITY; 2], [f64::NEG_INFINITY; 2]), |(lo, hi), c| {
([lo[0].min(c[0]), lo[1].min(c[1])], [hi[0].max(c[0]), hi[1].max(c[1])])
});
let pad = rho + 2.0 * cell;
let (x0, y0) = (lo[0] - pad, lo[1] - pad);
let (wx, wy) = (((hi[0] - lo[0] + 2.0 * pad) / cell).ceil(), ((hi[1] - lo[1] + 2.0 * pad) / cell).ceil());
if !(wx.is_finite() && wy.is_finite() && (wx + 1.0) * (wy + 1.0) <= 1e8) {
return Vec::new();
}
let (nx, ny) = (wx as usize + 1, wy as usize + 1);
let node = |i: usize, j: usize| [x0 + cell * i as f64, y0 + cell * j as f64];
let f = |p: [f64; 2]| self.distance(p).0 - rho;
let values: Vec<f64> = (0..ny).flat_map(|j| (0..nx).map(move |i| (i, j))).map(|(i, j)| f(node(i, j))).collect();
let value = |i: usize, j: usize| values[j * nx + i];
let crossing = |a: [f64; 2], b: [f64; 2]| -> [f64; 2] {
let (mut fa, mut lo, mut hi) = (f(a), 0.0f64, 1.0f64);
for _ in 0..60 {
let m = 0.5 * (lo + hi);
let fm = f([a[0] + (b[0] - a[0]) * m, a[1] + (b[1] - a[1]) * m]);
if (fm <= 0.0) == (fa <= 0.0) {
lo = m;
fa = fm;
} else {
hi = m;
}
}
let m = 0.5 * (lo + hi);
[a[0] + (b[0] - a[0]) * m, a[1] + (b[1] - a[1]) * m]
};
let h_edge = |i: usize, j: usize| 2 * (j * nx + i);
let v_edge = |i: usize, j: usize| 2 * (j * nx + i) + 1;
let mut next: std::collections::HashMap<usize, usize> = std::collections::HashMap::new();
for j in 0..ny - 1 {
for i in 0..nx - 1 {
let inside = [value(i, j) <= 0.0, value(i + 1, j) <= 0.0, value(i + 1, j + 1) <= 0.0, value(i, j + 1) <= 0.0];
let edges = [h_edge(i, j), v_edge(i + 1, j), h_edge(i, j + 1), v_edge(i, j)];
let mut exits: Vec<usize> = Vec::new();
let mut entries: Vec<usize> = Vec::new();
for k in 0..4 {
let (a, b) = (inside[k], inside[(k + 1) % 4]);
if a && !b {
exits.push(k);
} else if !a && b {
entries.push(k);
}
}
match (exits.as_slice(), entries.as_slice()) {
([e], [n]) => {
next.insert(edges[*n], edges[*e]);
}
([e0, e1], [n0, n1]) => {
let middle = f([x0 + cell * (i as f64 + 0.5), y0 + cell * (j as f64 + 0.5)]) <= 0.0;
let pair = |n: usize, exits: [usize; 2]| -> usize {
let after = |e: usize| (e + 4 - n) % 4;
if middle { *exits.iter().max_by_key(|&&e| after(e)).unwrap() } else { *exits.iter().min_by_key(|&&e| after(e)).unwrap() }
};
next.insert(edges[*n0], edges[pair(*n0, [*e0, *e1])]);
next.insert(edges[*n1], edges[pair(*n1, [*e0, *e1])]);
}
_ => {}
}
}
}
let edge_point = |id: usize| -> [f64; 2] {
let (cell_id, vertical) = (id / 2, id % 2 == 1);
let (i, j) = (cell_id % nx, cell_id / nx);
let (a, b) = if vertical { (node(i, j), node(i, j + 1)) } else { (node(i, j), node(i + 1, j)) };
if f(a) <= 0.0 { crossing(a, b) } else { crossing(b, a) }
};
let mut loops = Vec::new();
let mut seen: std::collections::HashSet<usize> = std::collections::HashSet::new();
let mut starts: Vec<usize> = next.keys().copied().collect();
starts.sort_unstable();
for start in starts {
if seen.contains(&start) {
continue;
}
let mut ring = Vec::new();
let mut at = start;
while seen.insert(at) {
ring.push(edge_point(at));
match next.get(&at) {
Some(&n) => at = n,
None => break,
}
}
if ring.len() >= 3 {
ring.reverse();
loops.push(ring);
}
}
loops
}
pub fn level_area(&self, rho: f64, cell: f64) -> Option<f64> {
if !self.grid_fits(rho, cell) {
return None;
}
Some(
self.level_loops(rho, cell)
.iter()
.map(|ring| {
let n = ring.len();
0.5 * (0..n).map(|k| ring[k][0] * ring[(k + 1) % n][1] - ring[(k + 1) % n][0] * ring[k][1]).sum::<f64>()
})
.sum(),
)
}
fn grid_fits(&self, rho: f64, cell: f64) -> bool {
if !(cell > 0.0 && cell.is_finite() && rho >= 0.0 && rho.is_finite() && cell <= 0.25 * rho) {
return false;
}
let (lo, hi) = self.curve.iter().fold(([f64::INFINITY; 2], [f64::NEG_INFINITY; 2]), |(lo, hi), c| {
([lo[0].min(c[0]), lo[1].min(c[1])], [hi[0].max(c[0]), hi[1].max(c[1])])
});
let pad = rho + 2.0 * cell;
let (wx, wy) = (((hi[0] - lo[0] + 2.0 * pad) / cell).ceil(), ((hi[1] - lo[1] + 2.0 * pad) / cell).ceil());
wx.is_finite() && wy.is_finite() && (wx + 1.0) * (wy + 1.0) <= 1e8
}
pub fn volume(&self, h0: f64, h1: f64, cell: f64) -> Option<f64> {
let (a, b) = (h0.max(self.heights[0]), h1.min(self.heights[1]));
if !(b > a) {
return Some(0.0);
}
if !self.grid_fits(self.widest(), cell) {
return None;
}
const X: [f64; 4] = [0.1834346424956498, 0.5255324099163290, 0.7966664774136267, 0.9602898564975363];
const W: [f64; 4] = [0.3626837833783620, 0.3137066458778873, 0.2223810344533745, 0.1012285362903763];
let gauss = |f: &dyn Fn(f64) -> f64, lo: f64, hi: f64| -> f64 {
let mut total = 0.0;
for k in 0..4 {
let (s0, s1) = (lo + (hi - lo) * k as f64 / 4.0, lo + (hi - lo) * (k + 1) as f64 / 4.0);
let (mid, half) = (0.5 * (s0 + s1), 0.5 * (s1 - s0));
for i in 0..4 {
total += half * W[i] * (f(mid - half * X[i]) + f(mid + half * X[i]));
}
}
total
};
let mut total = 0.0;
for piece in &self.meridian {
match *piece {
OutlinePiece::Line { from, to } => {
let tol_r = 1e-12 * self.widest();
if from[0].abs() <= tol_r && to[0].abs() <= tol_r {
continue;
}
let dh = to[1] - from[1];
if dh == 0.0 {
continue;
}
let (s0, s1) = (((a - from[1]) / dh).clamp(0.0, 1.0), ((b - from[1]) / dh).clamp(0.0, 1.0));
let (lo, hi) = (s0.min(s1), s0.max(s1));
if hi <= lo {
continue;
}
let radius = |s: f64| from[0] + s * (to[0] - from[0]);
if from[0] == to[0] {
total += dh.abs() * (hi - lo) * self.level_area(from[0], cell)?;
} else {
total += dh.abs() * gauss(&|s| self.level_area(radius(s), cell).unwrap_or(f64::NAN), lo, hi);
}
}
OutlinePiece::Arc { centre, radius, start, end } => {
let (t0, t1) = (start.min(end), start.max(end));
let height = |t: f64| centre[1] + radius * t.sin();
let clip = |h: f64| -> f64 {
let (mut lo, mut hi) = (t0, t1);
let rising = height(t1) > height(t0);
for _ in 0..200 {
let m = 0.5 * (lo + hi);
if (height(m) < h) == rising { lo = m } else { hi = m }
}
0.5 * (lo + hi)
};
let (ha, hb) = (height(t0), height(t1));
let (hlo, hhi) = (ha.min(hb), ha.max(hb));
let (ca, cb) = (a.max(hlo), b.min(hhi));
if cb <= ca {
continue;
}
let (u0, u1) = (clip(ca), clip(cb));
let (lo, hi) = (u0.min(u1), u0.max(u1));
let f = |t: f64| self.level_area(centre[0] + radius * t.cos(), cell).unwrap_or(f64::NAN) * (radius * t.cos()).abs();
total += gauss(&f, lo, hi);
}
OutlinePiece::Ellipse { .. } => {}
}
}
total.is_finite().then_some(total)
}
fn section_reach(&self) -> f64 {
let (o, x, z) = (Vec3::new(0.0, 0.0, 0.0), Vec3::new(1.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0));
self.meridian
.iter()
.filter_map(|piece| world_piece(piece, o, x, z).ok())
.flat_map(|curve| curve.control_points.into_iter())
.filter_map(|c| c.point().ok())
.map(|p| p.x.abs())
.fold(0.0f64, f64::max)
}
fn widest(&self) -> f64 {
self.meridian.iter().map(|piece| piece_extent(piece).0).fold(0.0f64, f64::max)
}
}
fn world_piece(piece: &OutlinePiece, foot: Vec3, out: Vec3, up: Vec3) -> Result<crate::NurbsCurve, String> {
let at = |p: [f64; 2]| foot.add(out.scale(p[0])).add(up.scale(p[1]));
match *piece {
OutlinePiece::Line { from, to } => crate::make_line(at(from), at(to)),
OutlinePiece::Arc { centre, radius, start, end } => {
let c = at(centre);
if end > start {
crate::make_arc(c, out, up, radius, start, end)
} else {
crate::make_arc(c, out, up, radius, end, start)?.reversed()
}
}
OutlinePiece::Ellipse { .. } => Err("an elliptic meridian piece".into()),
}
}
impl UprightSlot {
fn chain(&self) -> Option<Vec<OutlinePiece>> {
let tol_r = 1e-12 * self.widest();
let along_axis = |piece: &OutlinePiece| matches!(*piece, OutlinePiece::Line { from, to } if from[0].abs() <= tol_r && to[0].abs() <= tol_r);
let axis_index = self.meridian.iter().position(along_axis)?;
let n = self.meridian.len();
Some((1..n).map(|k| self.meridian[(axis_index + k) % n].clone()).collect())
}
fn tool_at(&self, t: f64) -> Result<crate::BrepSolid, crate::KernelRefusal> {
let refuse = |message: String| crate::KernelRefusal::internal(crate::KernelStage::Collect, "upright_stroke", format!("uprightStroke: {message}"));
let p = self.point(t);
let foot = self.origin.add(self.x.scale(p[0])).add(self.y.scale(p[1]));
let curves: Vec<crate::NurbsCurve> = self.meridian.iter().map(|piece| world_piece(piece, foot, self.x, self.up)).collect::<Result<_, _>>().map_err(refuse)?;
crate::revolve_profile_brep(&curves, foot, self.up, std::f64::consts::TAU).map_err(|e| {
crate::KernelRefusal::input(crate::KernelStage::Collect, "upright_scale", format!("uprightStroke: the tool is outside the construction's range: the revolve builder declined it ({e})"))
})
}
fn section_at(&self, t: f64) -> Result<Vec<crate::NurbsCurve>, String> {
let chain = self.chain().ok_or("no axis piece")?;
let p = self.point(t);
let d = self.derivative(t);
let length = dot(d, d).sqrt();
if !(length > 0.0) {
return Err("the curve stops".into());
}
let normal = [-d[1] / length, d[0] / length];
let foot = self.origin.add(self.x.scale(p[0])).add(self.y.scale(p[1]));
let left = self.x.scale(normal[0]).add(self.y.scale(normal[1]));
let mut loop_curves: Vec<crate::NurbsCurve> = chain.iter().map(|piece| world_piece(piece, foot, left, self.up)).collect::<Result<_, _>>()?;
let mirrored: Vec<crate::NurbsCurve> = chain.iter().rev().map(|piece| world_piece(piece, foot, left.scale(-1.0), self.up).and_then(|c| c.reversed())).collect::<Result<_, _>>()?;
loop_curves.extend(mirrored);
Ok(loop_curves)
}
fn world_piece_of_curve(&self, a: f64, b: f64) -> Result<crate::NurbsCurve, String> {
let split = |c: [[f64; 2]; 4], t: f64| -> ([[f64; 2]; 4], [[f64; 2]; 4]) {
let l = |x: [f64; 2], y: [f64; 2]| [x[0] + (y[0] - x[0]) * t, x[1] + (y[1] - x[1]) * t];
let (p01, p12, p23) = (l(c[0], c[1]), l(c[1], c[2]), l(c[2], c[3]));
let (p012, p123) = (l(p01, p12), l(p12, p23));
let m = l(p012, p123);
([c[0], p01, p012, m], [m, p123, p23, c[3]])
};
let right = if a > 0.0 { split(self.curve, a).1 } else { self.curve };
let piece = if b < 1.0 { split(right, (b - a) / (1.0 - a)).0 } else { right };
let world = |p: [f64; 2]| crate::Vec4 { x: self.origin.x + self.x.x * p[0] + self.y.x * p[1], y: self.origin.y + self.x.y * p[0] + self.y.y * p[1], z: self.origin.z + self.x.z * p[0] + self.y.z * p[1], w: 1.0 };
crate::NurbsCurve::new(3, vec![0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0], piece.iter().map(|p| world(*p)).collect())
}
fn section_sweep(&self, a: f64, b: f64) -> Result<crate::BrepSolid, crate::KernelRefusal> {
let refuse = |message: String| crate::KernelRefusal::internal(crate::KernelStage::Collect, "upright_stroke", format!("uprightStroke: {message}"));
let section = self.section_at(a).map_err(refuse)?;
let path = self.world_piece_of_curve(a, b).map_err(refuse)?;
let chain = crate::SweepPath::new(vec![path], Vec::new())?;
crate::sweep_profile_along_chain(§ion, &chain, 0.0, None, None, crate::SectionPlacement::Rigid, "an upright tool's section along one piece of a cubic")
}
pub fn pieces(&self) -> Vec<(f64, f64, bool)> {
let widest = self.section_reach().max(self.widest());
let c = &self.curve;
let polygon = (0..3).map(|k| 3.0 * dot(sub(c[k + 1], c[k]), sub(c[k + 1], c[k])).sqrt()).fold(0.0f64, f64::max);
let second_most = { let (a, b) = (self.second(0.0), self.second(1.0)); dot(a, a).sqrt().max(dot(b, b).sqrt()) };
let third = { let d = sub(self.second(1.0), self.second(0.0)); dot(d, d).sqrt() };
let cross = |t: f64| { let (d, dd) = (self.derivative(t), self.second(t)); (d[0] * dd[1] - d[1] * dd[0]).abs() };
let speed = |t: f64| dot(self.derivative(t), self.derivative(t)).sqrt();
let threshold = 0.99 / widest;
let may_be_tight = |a: f64, b: f64| -> bool {
let (m, h) = (0.5 * (a + b), 0.5 * (b - a));
let low = speed(m) - second_most * h;
if !(low > 0.0) {
return true;
}
(cross(m) + polygon * third * h) / (low * low * low) >= threshold
};
let is_tight_at = |t: f64| cross(t) / speed(t).powi(3) >= threshold;
let mut tight_spans: Vec<(f64, f64)> = Vec::new();
let mut stack: Vec<(f64, f64, u32)> = (0..256).rev().map(|k| (k as f64 / 256.0, (k + 1) as f64 / 256.0, 0)).collect();
let mut read = 0usize;
while let Some((a, b, depth)) = stack.pop() {
read += 1;
if !may_be_tight(a, b) {
continue;
}
if read > 1 << 20 || is_tight_at(0.5 * (a + b)) || depth >= 40 {
match tight_spans.last_mut() {
Some(last) if (last.1 - a).abs() <= 1e-15 => last.1 = b,
_ => tight_spans.push((a, b)),
}
continue;
}
let m = 0.5 * (a + b);
stack.push((m, b, depth + 1));
stack.push((a, m, depth + 1));
}
let mut runs: Vec<(f64, f64, bool)> = Vec::new();
let mut at = 0.0;
for (a, b) in tight_spans {
if a > at {
runs.push((at, a, false));
}
runs.push((a, b, true));
at = b;
}
if at < 1.0 {
runs.push((at, 1.0, false));
}
let heading = |t: f64| {
let d = self.derivative(t);
d[1].atan2(d[0])
};
let most = 150f64.to_radians();
let mut pieces = Vec::new();
for (a, b, is_tight) in runs {
if !(b > a) {
continue;
}
let steps = 2048;
let mut turn = 0.0;
let mut previous = heading(a);
let mut start = a;
for k in 1..=steps {
let t = a + (b - a) * k as f64 / steps as f64;
let h = heading(t);
let mut delta = h - previous;
while delta > std::f64::consts::PI { delta -= std::f64::consts::TAU; }
while delta < -std::f64::consts::PI { delta += std::f64::consts::TAU; }
turn += delta.abs();
previous = h;
if turn >= most && k < steps {
pieces.push((start, t, is_tight));
start = t;
turn = 0.0;
}
}
pieces.push((start, b, is_tight));
}
pieces
}
pub fn solid(&self) -> Result<crate::BrepSolid, crate::KernelRefusal> {
let small_arc = self.meridian.iter().any(|piece| matches!(*piece, OutlinePiece::Arc { radius, .. } if radius < 1e-6));
let widest = self.widest();
if small_arc || !(5e-2..=1e6).contains(&widest) {
return Err(crate::KernelRefusal::input(
crate::KernelStage::Collect,
"upright_scale",
format!("uprightStroke: a tool {widest:e} wide is outside the construction's range (5e-2 to 1e6, its arcs at least 1e-6: the revolve builder reads 1e-3 from the axis)"),
));
}
let pieces = self.pieces();
if let Some(&(a, b, _)) = pieces.iter().find(|p| p.2) {
return Err(crate::KernelRefusal::unsupported(
crate::KernelStage::Collect,
"upright_tight_turn",
format!("uprightStroke: the curve turns tighter than the tool reaches over [{a:.4}, {b:.4}]; its folded concave wall is not built"),
));
}
let mut whole = self.tool_at(0.0)?;
for (index, &(_, b, _)) in pieces.iter().enumerate() {
whole = crate::boolean_operation_nary(&[whole, self.tool_at(b)?], crate::BooleanOperation::Union)
.map_err(|e| e.with_message(|m| format!("{m} (adding the tool at break {index}, t = {b})")))?;
}
for (index, &(a, b, _)) in pieces.iter().enumerate() {
whole = crate::boolean_operation_nary(&[whole, self.section_sweep(a, b)?], crate::BooleanOperation::Union)
.map_err(|e| e.with_message(|m| format!("{m} (adding sweep {index} of {}, [{a}, {b}])", pieces.len())))?;
}
Ok(whole)
}
pub fn stroke(&self, a: f64, b: f64) -> Result<crate::BrepSolid, crate::KernelRefusal> {
let refuse = |message: String| crate::KernelRefusal::internal(crate::KernelStage::Collect, "upright_stroke", format!("uprightStroke: {message}"));
if !(0.0 <= a && a < b && b <= 1.0) {
return Err(crate::KernelRefusal::input(crate::KernelStage::Collect, "upright_stroke_range", format!("uprightStroke: the piece [{a}, {b}] is not within the curve's [0, 1]")));
}
let section = self.section_at(a).map_err(refuse)?;
let path = self.world_piece_of_curve(a, b).map_err(refuse)?;
let chain = crate::SweepPath::new(vec![path], Vec::new())?;
let swept = crate::sweep_profile_along_chain(§ion, &chain, 0.0, None, None, crate::SectionPlacement::Rigid, "an upright tool's section along one piece of a cubic")?;
crate::boolean_operation_nary(&[swept, self.tool_at(a)?, self.tool_at(b)?], crate::BooleanOperation::Union)
}
}