use crate::KnotDirection;
use crate::{NurbsSurface, Vec3};
#[derive(Clone, Debug, PartialEq)]
pub enum Irregularity {
Collapses { u: f64, v: f64 },
FoldsOver { u: f64, v: f64 },
Uncertified { u: f64, v: f64, reason: String },
}
impl std::fmt::Display for Irregularity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Collapses { u, v } => write!(f, "the normal vanishes at ({u:.6}, {v:.6})"),
Self::FoldsOver { u, v } => write!(f, "the normal turns back near ({u:.6}, {v:.6})"),
Self::Uncertified { u, v, reason } => write!(f, "regularity not certified near ({u:.6}, {v:.6}): {reason}"),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct RegularityReport {
pub pieces: usize,
pub boxes: usize,
pub creases: usize,
}
const BOX_BUDGET: usize = 400_000;
const MAX_DEPTH: u32 = 40;
const WITNESS_SIZE: f64 = 1.0 / 1024.0;
const VANISHING: f64 = 1e-9;
const MARGIN: f64 = 1e-13;
pub fn certify_regular(surface: &NurbsSurface) -> Result<RegularityReport, Irregularity> {
let uncertified = |reason: String| Irregularity::Uncertified { u: f64::NAN, v: f64::NAN, reason };
let [u0, u1] = surface.domain_u().map_err(&uncertified)?;
let [v0, v1] = surface.domain_v().map_err(&uncertified)?;
let (p, q) = (surface.degree_u, surface.degree_v);
if p == 0 || q == 0 {
return Err(uncertified(format!("degree ({p}, {q}) has no normal")));
}
for (knots, degree, name) in [(&surface.knots_u, p, "u"), (&surface.knots_v, q, "v")] {
let n = knots.len();
if knots[..=degree].iter().any(|k| *k != knots[0]) || knots[n - degree - 1..].iter().any(|k| *k != knots[n - 1]) {
return Err(uncertified(format!("the {name} knot vector is not clamped")));
}
}
let rational = {
let w0 = surface.control_points[0][0].w;
surface.control_points.iter().flatten().any(|point| point.w != w0)
};
if surface.control_points.iter().flatten().any(|point| !(point.w > 0.0) || !point.w.is_finite()) {
return Err(uncertified("a weight is not positive".into()));
}
let (breaks_u, insert_u) = breaks(&surface.knots_u, p, u0, u1);
let (breaks_v, insert_v) = breaks(&surface.knots_v, q, v0, v1);
let refined = surface
.refine_knots(KnotDirection::U, &insert_u)
.and_then(|refined| refined.refine_knots(KnotDirection::V, &insert_v))
.map_err(|error| uncertified(error.to_string()))?;
let (pieces_u, pieces_v) = (breaks_u.len() - 1, breaks_v.len() - 1);
if refined.control_points.len() != pieces_u * p + 1 || refined.control_points.iter().any(|row| row.len() != pieces_v * q + 1) {
return Err(uncertified("the refined net does not split into Bézier pieces".into()));
}
let mut report = RegularityReport::default();
let mut normals: Vec<Vec<Grid<Vec3>>> = Vec::with_capacity(pieces_u);
for a in 0..pieces_u {
let mut row = Vec::with_capacity(pieces_v);
for b in 0..pieces_v {
let net: Vec<Vec<crate::Vec4>> =
(0..=p).map(|i| refined.control_points[a * p + i][b * q..=b * q + q].to_vec()).collect();
let normal = piece_normal(&net, p, q, rational);
let piece = Piece {
u: [breaks_u[a], breaks_u[a + 1]],
v: [breaks_v[b], breaks_v[b + 1]],
corners: [
a == 0 && b == 0,
a == pieces_u - 1 && b == 0,
a == 0 && b == pieces_v - 1,
a == pieces_u - 1 && b == pieces_v - 1,
],
};
certify_piece(surface, &normal, &piece, &mut report)?;
report.pieces += 1;
row.push(normal);
}
normals.push(row);
}
let multiplicity = |knots: &[f64], value: f64| knots.iter().filter(|k| **k == value).count();
for a in 0..pieces_u.saturating_sub(1) {
if multiplicity(&surface.knots_u, breaks_u[a + 1]) < p {
continue;
}
for b in 0..pieces_v {
let (before, after) = (&normals[a][b], &normals[a + 1][b]);
let left: Vec<Vec3> = (0..=before.n).map(|j| before.at(before.m, j)).collect();
let right: Vec<Vec3> = (0..=after.n).map(|j| after.at(0, j)).collect();
report.creases += 1;
certify_crease(&left, &right, breaks_v[b], breaks_v[b + 1])
.map_err(|t| Irregularity::FoldsOver { u: breaks_u[a + 1], v: t })?;
}
}
for b in 0..pieces_v.saturating_sub(1) {
if multiplicity(&surface.knots_v, breaks_v[b + 1]) < q {
continue;
}
for a in 0..pieces_u {
let (before, after) = (&normals[a][b], &normals[a][b + 1]);
let below: Vec<Vec3> = (0..=before.m).map(|i| before.at(i, before.n)).collect();
let above: Vec<Vec3> = (0..=after.m).map(|i| after.at(i, 0)).collect();
report.creases += 1;
certify_crease(&below, &above, breaks_u[a], breaks_u[a + 1])
.map_err(|s| Irregularity::FoldsOver { u: s, v: breaks_v[b + 1] })?;
}
}
Ok(report)
}
fn breaks(knots: &[f64], degree: usize, start: f64, end: f64) -> (Vec<f64>, Vec<f64>) {
let mut values: Vec<f64> = Vec::new();
let mut insert = Vec::new();
for &knot in knots {
if knot < start || knot > end || values.last() == Some(&knot) {
continue;
}
values.push(knot);
if knot > start && knot < end {
let have = knots.iter().filter(|k| **k == knot).count();
insert.extend(std::iter::repeat(knot).take(degree.saturating_sub(have)));
}
}
(values, insert)
}
struct Piece {
u: [f64; 2],
v: [f64; 2],
corners: [bool; 4],
}
#[derive(Clone, Debug)]
struct Grid<T> {
m: usize,
n: usize,
c: Vec<T>,
}
trait Linear: Copy {
fn zero() -> Self;
fn plus(self, other: Self) -> Self;
fn times(self, factor: f64) -> Self;
}
impl Linear for f64 {
fn zero() -> Self {
0.0
}
fn plus(self, other: Self) -> Self {
self + other
}
fn times(self, factor: f64) -> Self {
self * factor
}
}
impl Linear for Vec3 {
fn zero() -> Self {
Vec3::new(0.0, 0.0, 0.0)
}
fn plus(self, other: Self) -> Self {
self.add(other)
}
fn times(self, factor: f64) -> Self {
self.scale(factor)
}
}
fn binomial(n: usize, k: usize) -> f64 {
let k = k.min(n - k);
(0..k).fold(1.0, |value, i| value * (n - i) as f64 / (i + 1) as f64)
}
impl<T: Linear> Grid<T> {
fn at(&self, i: usize, j: usize) -> T {
self.c[i * (self.n + 1) + j]
}
fn du(&self) -> Grid<T> {
let (m, n) = (self.m, self.n);
let c = (0..m)
.flat_map(|i| (0..=n).map(move |j| (i, j)))
.map(|(i, j)| self.at(i + 1, j).plus(self.at(i, j).times(-1.0)).times(m as f64))
.collect();
Grid { m: m - 1, n, c }
}
fn dv(&self) -> Grid<T> {
let (m, n) = (self.m, self.n);
let c = (0..=m)
.flat_map(|i| (0..n).map(move |j| (i, j)))
.map(|(i, j)| self.at(i, j + 1).plus(self.at(i, j).times(-1.0)).times(n as f64))
.collect();
Grid { m, n: n - 1, c }
}
fn plus(&self, other: &Grid<T>) -> Grid<T> {
debug_assert!(self.m == other.m && self.n == other.n);
Grid { m: self.m, n: self.n, c: self.c.iter().zip(&other.c).map(|(a, b)| a.plus(*b)).collect() }
}
fn halves(&self, along_u: bool) -> (Grid<T>, Grid<T>) {
let (m, n) = (self.m, self.n);
let mut low = self.clone();
let mut high = self.clone();
let (count, length) = if along_u { (n + 1, m + 1) } else { (m + 1, n + 1) };
let index = |line: usize, k: usize| if along_u { k * (n + 1) + line } else { line * (n + 1) + k };
let mut work = vec![T::zero(); length];
for line in 0..count {
for (k, slot) in work.iter_mut().enumerate() {
*slot = self.c[index(line, k)];
}
for level in 0..length {
low.c[index(line, level)] = work[0];
high.c[index(line, length - 1 - level)] = work[length - 1 - level];
for k in 0..length - 1 - level {
work[k] = work[k].plus(work[k + 1]).times(0.5);
}
}
}
(low, high)
}
fn value(&self, s: f64, t: f64) -> T {
let rows: Vec<T> = (0..=self.m)
.map(|i| casteljau(&(0..=self.n).map(|j| self.at(i, j)).collect::<Vec<_>>(), t))
.collect();
casteljau(&rows, s)
}
}
fn casteljau<T: Linear>(points: &[T], t: f64) -> T {
let mut work = points.to_vec();
for level in 1..work.len() {
for k in 0..work.len() - level {
work[k] = work[k].times(1.0 - t).plus(work[k + 1].times(t));
}
}
work[0]
}
fn product<A: Linear, B: Linear, C: Linear>(a: &Grid<A>, b: &Grid<B>, combine: impl Fn(A, B) -> C) -> Grid<C> {
let (m, n) = (a.m + b.m, a.n + b.n);
let mut c = vec![C::zero(); (m + 1) * (n + 1)];
for i in 0..=a.m {
for j in 0..=a.n {
for k in 0..=b.m {
for l in 0..=b.n {
let factor = binomial(a.m, i) * binomial(b.m, k) / binomial(m, i + k) * binomial(a.n, j)
* binomial(b.n, l)
/ binomial(n, j + l);
let slot = &mut c[(i + k) * (n + 1) + j + l];
*slot = slot.plus(combine(a.at(i, j), b.at(k, l)).times(factor));
}
}
}
}
Grid { m, n, c }
}
fn piece_normal(net: &[Vec<crate::Vec4>], p: usize, q: usize, rational: bool) -> Grid<Vec3> {
let cells = |f: &dyn Fn(&crate::Vec4) -> Vec3| Grid {
m: p,
n: q,
c: net.iter().flat_map(|row| row.iter().map(f)).collect::<Vec<Vec3>>(),
};
if !rational {
let points = cells(&|point| Vec3::new(point.x / point.w, point.y / point.w, point.z / point.w));
return product(&points.du(), &points.dv(), Vec3::cross);
}
let points = cells(&|point| Vec3::new(point.x, point.y, point.z));
let weights = Grid { m: p, n: q, c: net.iter().flat_map(|row| row.iter().map(|point| point.w)).collect::<Vec<f64>>() };
let (pu, pv) = (points.du(), points.dv());
let first = product(&weights, &product(&pu, &pv, Vec3::cross), |w, n| n.scale(w));
let second = product(&weights.dv(), &product(&points, &pu, Vec3::cross), |w, n| n.scale(w));
let third = product(&weights.du(), &product(&pv, &points, Vec3::cross), |w, n| n.scale(w));
first.plus(&second).plus(&third)
}
struct Cell {
normal: Grid<Vec3>,
s: [f64; 2],
t: [f64; 2],
depth: u32,
}
fn certify_piece(
surface: &NurbsSurface,
normal: &Grid<Vec3>,
piece: &Piece,
report: &mut RegularityReport,
) -> Result<(), Irregularity> {
let scale = normal.c.iter().map(|c| c.length()).fold(0.0_f64, f64::max);
let at = |s: f64, t: f64| (piece.u[0] + (piece.u[1] - piece.u[0]) * s, piece.v[0] + (piece.v[1] - piece.v[0]) * t);
if !(scale > 0.0) || !scale.is_finite() {
let (u, v) = at(0.5, 0.5);
return Err(Irregularity::Collapses { u, v });
}
let vanishes = |s: f64, t: f64| {
let (u, v) = at(s, t);
match surface.deriv1(u, v) {
Ok((_, su, sv)) => !(su.cross(sv).length() > VANISHING * su.length() * sv.length()),
Err(_) => true,
}
};
let mut stack = vec![Cell { normal: normal.clone(), s: [0.0, 1.0], t: [0.0, 1.0], depth: 0 }];
while let Some(cell) = stack.pop() {
report.boxes += 1;
let grid = &cell.normal;
let (m, n) = (grid.m, grid.n);
let corner_index = [(0, 0), (m, 0), (0, n), (m, n)];
let corner_st = |k: usize| (if k % 2 == 0 { cell.s[0] } else { cell.s[1] }, if k < 2 { cell.t[0] } else { cell.t[1] });
let exempt = |k: usize| {
let (s, t) = ([cell.s[0], cell.s[1], cell.s[0], cell.s[1]][k], [cell.t[0], cell.t[0], cell.t[1], cell.t[1]][k]);
let piece_corner = match (s, t) {
(s, t) if s == 0.0 && t == 0.0 => Some(0),
(s, t) if s == 1.0 && t == 0.0 => Some(1),
(s, t) if s == 0.0 && t == 1.0 => Some(2),
(s, t) if s == 1.0 && t == 1.0 => Some(3),
_ => None,
};
piece_corner.is_some_and(|c| piece.corners[c])
};
let mut reference = grid.value(0.5, 0.5);
if !(reference.length() > VANISHING * scale) {
reference = grid.c.iter().fold(Vec3::new(0.0, 0.0, 0.0), |sum, c| sum.add(*c));
}
let largest = grid.c.iter().map(|c| c.length()).fold(0.0_f64, f64::max);
if reference.length() > 0.0 && largest > 0.0 {
let direction = reference.scale(1.0 / reference.length());
let margin = MARGIN * largest;
let dots: Vec<f64> = grid.c.iter().map(|c| c.dot(direction)).collect();
let certified = if dots.iter().all(|d| *d > margin) {
true
} else {
let corner_ok = (0..4).all(|k| {
let (ci, cj) = corner_index[k];
dots[ci * (n + 1) + cj] > margin || (exempt(k) && vanishes(corner_st(k).0, corner_st(k).1))
});
let side_ok = |side: &dyn Fn(usize) -> Option<usize>| (0..=m.max(n)).filter_map(side).any(|index| dots[index] > margin);
corner_ok
&& dots.iter().all(|d| *d >= -margin)
&& side_ok(&|j| (j <= n).then_some(j))
&& side_ok(&|j| (j <= n).then_some(m * (n + 1) + j))
&& side_ok(&|i| (i <= m).then_some(i * (n + 1)))
&& side_ok(&|i| (i <= m).then_some(i * (n + 1) + n))
};
if certified {
continue;
}
}
let corner_point = |k: usize| {
let s = if k % 2 == 0 { cell.s[0] } else { cell.s[1] };
let t = if k < 2 { cell.t[0] } else { cell.t[1] };
at(s, t)
};
let corners: Vec<(usize, Vec3)> = (0..4).filter(|&k| !exempt(k)).map(|k| (k, grid.at(corner_index[k].0, corner_index[k].1))).collect();
if let Some(&(k, _)) = corners.iter().find(|(k, _)| vanishes(corner_st(*k).0, corner_st(*k).1)) {
let (u, v) = corner_point(k);
return Err(Irregularity::Collapses { u, v });
}
let small = cell.s[1] - cell.s[0] <= WITNESS_SIZE && cell.t[1] - cell.t[0] <= WITNESS_SIZE;
if small && corners.iter().any(|(_, a)| corners.iter().any(|(_, b)| a.dot(*b) < 0.0)) {
let (u, v) = at(0.5 * (cell.s[0] + cell.s[1]), 0.5 * (cell.t[0] + cell.t[1]));
return Err(Irregularity::FoldsOver { u, v });
}
if cell.depth >= MAX_DEPTH || report.boxes >= BOX_BUDGET {
let (u, v) = at(0.5 * (cell.s[0] + cell.s[1]), 0.5 * (cell.t[0] + cell.t[1]));
let reason = if cell.depth >= MAX_DEPTH { "the subdivision reached its depth limit" } else { "the subdivision budget is spent" };
return Err(Irregularity::Uncertified { u, v, reason: reason.into() });
}
let along_u = cell.s[1] - cell.s[0] >= cell.t[1] - cell.t[0];
let (low, high) = grid.halves(along_u);
let (first, second) = if along_u {
let mid = 0.5 * (cell.s[0] + cell.s[1]);
(
Cell { normal: low, s: [cell.s[0], mid], t: cell.t, depth: cell.depth + 1 },
Cell { normal: high, s: [mid, cell.s[1]], t: cell.t, depth: cell.depth + 1 },
)
} else {
let mid = 0.5 * (cell.t[0] + cell.t[1]);
(
Cell { normal: low, s: cell.s, t: [cell.t[0], mid], depth: cell.depth + 1 },
Cell { normal: high, s: cell.s, t: [mid, cell.t[1]], depth: cell.depth + 1 },
)
};
stack.push(second);
stack.push(first);
}
Ok(())
}
fn certify_crease(before: &[Vec3], after: &[Vec3], start: f64, end: f64) -> Result<(), f64> {
let to_grid = |c: &[Vec3]| Grid { m: 0, n: c.len() - 1, c: c.to_vec() };
let dot = product(&to_grid(before), &to_grid(after), |a, b| a.dot(b));
let mut stack = vec![(dot, [0.0_f64, 1.0_f64], 0_u32)];
while let Some((grid, range, depth)) = stack.pop() {
let largest = grid.c.iter().map(|c| c.abs()).fold(0.0_f64, f64::max);
if grid.c.iter().all(|c| *c > MARGIN * largest) {
continue;
}
let place = |r: f64| start + (end - start) * r;
if let Some(r) = [(grid.c[0], range[0]), (grid.c[grid.n], range[1])].iter().find(|(c, _)| *c <= 0.0).map(|(_, r)| *r) {
return Err(place(r));
}
if depth >= MAX_DEPTH {
return Err(place(0.5 * (range[0] + range[1])));
}
let mid = 0.5 * (range[0] + range[1]);
let (low, high) = grid.halves(false);
stack.push((high, [mid, range[1]], depth + 1));
stack.push((low, [range[0], mid], depth + 1));
}
Ok(())
}