use crate::topology::FaceRecord;
use crate::Vec3;
use std::cell::Cell;
use std::sync::atomic::{AtomicU64, Ordering};
thread_local! {
static ACTIVE: Cell<bool> = const { Cell::new(false) };
}
pub fn enabled() -> bool {
crate::switch!(live "BREP_SOUNDNESS_SCAN_MESH", true)
}
pub struct ScanMeshScope {
previous: bool,
}
impl ScanMeshScope {
pub fn open() -> Self {
Self::set(enabled())
}
pub(super) fn set(on: bool) -> Self {
Self { previous: ACTIVE.with(|cell| cell.replace(on)) }
}
}
impl Drop for ScanMeshScope {
fn drop(&mut self) {
ACTIVE.with(|cell| cell.set(self.previous));
}
}
pub(super) fn active() -> bool {
ACTIVE.with(|cell| cell.get())
}
static BOUNDED: AtomicU64 = AtomicU64::new(0);
static FALLBACKS: AtomicU64 = AtomicU64::new(0);
pub(super) fn count(bounded: bool) {
if bounded { &BOUNDED } else { &FALLBACKS }.fetch_add(1, Ordering::Relaxed);
}
pub fn peek_counts() -> (u64, u64) {
(BOUNDED.load(Ordering::Relaxed), FALLBACKS.load(Ordering::Relaxed))
}
pub fn take_counts() -> (u64, u64) {
(BOUNDED.swap(0, Ordering::Relaxed), FALLBACKS.swap(0, Ordering::Relaxed))
}
#[derive(Clone, Debug)]
pub enum FaceBound {
Plane,
Sphere { centre: Vec3, radius: f64 },
Ruled { origin: Vec3, axis: Vec3, rho0: f64, rho1: f64, height: f64 },
Affine { sphere: crate::affine_sphere::AffineSphere, largest_semi_axis: f64 },
Spline(std::sync::Arc<SplineBound>),
}
pub fn spline_enabled() -> bool {
spline_force().unwrap_or_else(|| crate::switch!(live "BREP_SCAN_MESH_SPLINE", true))
}
pub fn spline_check() -> bool {
crate::debug_flag!("BREP_SCAN_MESH_SPLINE_CHECK")
}
thread_local! {
static SPLINE_FORCE: Cell<Option<bool>> = const { Cell::new(None) };
}
pub fn spline_force() -> Option<bool> {
SPLINE_FORCE.with(|cell| cell.get())
}
pub struct SplineForceScope {
previous: Option<bool>,
}
impl SplineForceScope {
pub fn set(force: Option<bool>) -> Self {
Self { previous: SPLINE_FORCE.with(|cell| cell.replace(force)) }
}
}
impl Drop for SplineForceScope {
fn drop(&mut self) {
SPLINE_FORCE.with(|cell| cell.set(self.previous));
}
}
#[derive(Debug)]
pub struct SplineBound {
knots_u: Vec<f64>,
knots_v: Vec<f64>,
degree_u: usize,
degree_v: usize,
spans_v: usize,
table: Vec<[f64; 3]>,
}
impl SplineBound {
pub fn new(surface: &crate::NurbsSurface) -> Option<Self> {
let net = &surface.control_points;
let (p, q) = (surface.degree_u, surface.degree_v);
let rows = net.len();
let columns = net.first()?.len();
let (ku, kv) = (&surface.knots_u, &surface.knots_v);
if rows <= p || columns <= q || net.iter().any(|row| row.len() != columns)
|| ku.len() != rows + p + 1 || kv.len() != columns + q + 1
{
return None;
}
let w0 = net[0][0].w;
if !(w0.is_finite() && w0 > 0.0) || net.iter().flatten().any(|c| c.w != w0) {
return None;
}
let points: Vec<Vec<Vec3>> = net
.iter()
.map(|row| row.iter().map(|c| c.point().ok().filter(|x| x.x.is_finite() && x.y.is_finite() && x.z.is_finite())).collect())
.collect::<Option<_>>()?;
let c0 = |knots: &[f64], degree: usize, count: usize| {
(degree + 1..count).any(|k| knots[k] > knots[degree] && knots[k] < knots[count] && {
knots.iter().filter(|&&x| x == knots[k]).count() >= degree.max(1)
})
};
if c0(ku, p, rows) || c0(kv, q, columns) {
return None;
}
let ratio = |num: f64, den: f64| if den > 0.0 { num / den } else { 0.0 };
let qu: Vec<Vec<Vec3>> = (0..rows.saturating_sub(1))
.map(|i| (0..columns).map(|j| points[i + 1][j].sub(points[i][j]).scale(ratio(p as f64, ku[i + p + 1] - ku[i + 1]))).collect())
.collect();
let qv: Vec<Vec<Vec3>> = (0..rows)
.map(|i| (0..columns.saturating_sub(1)).map(|j| points[i][j + 1].sub(points[i][j]).scale(ratio(q as f64, kv[j + q + 1] - kv[j + 1]))).collect())
.collect();
let spans_u = rows - p;
let spans_v = columns - q;
let mut table = vec![[0.0f64; 3]; spans_u * spans_v];
for k in p..rows {
for l in q..columns {
let mut m = [0.0f64; 3];
if p >= 2 {
for i in k - p..=k - 2 {
let den = ku[i + p + 1] - ku[i + 2];
if !(den > 0.0) { continue; }
for j in l - q..=l {
m[0] = m[0].max(qu[i + 1][j].sub(qu[i][j]).scale((p - 1) as f64 / den).length());
}
}
}
if p >= 1 && q >= 1 {
for i in k - p..=k - 1 {
let den_u = ku[i + p + 1] - ku[i + 1];
if !(den_u > 0.0) { continue; }
for j in l - q..=l - 1 {
let den_v = kv[j + q + 1] - kv[j + 1];
if !(den_v > 0.0) { continue; }
let twist = points[i + 1][j + 1].sub(points[i][j + 1]).sub(points[i + 1][j]).add(points[i][j]);
m[1] = m[1].max(twist.scale((p * q) as f64 / (den_u * den_v)).length());
}
}
}
if q >= 2 {
for j in l - q..=l - 2 {
let den = kv[j + q + 1] - kv[j + 2];
if !(den > 0.0) { continue; }
for i in k - p..=k {
m[2] = m[2].max(qv[i][j + 1].sub(qv[i][j]).scale((q - 1) as f64 / den).length());
}
}
}
if !m.iter().all(|x| x.is_finite()) {
return None;
}
table[(k - p) * spans_v + (l - q)] = m;
}
}
Some(Self { knots_u: ku.clone(), knots_v: kv.clone(), degree_u: p, degree_v: q, spans_v, table })
}
pub fn edge_metric(&self, a: [f64; 2], b: [f64; 2]) -> Option<f64> {
let [muu, muv, mvv] = self.over([a[0].min(b[0]), a[0].max(b[0]), a[1].min(b[1]), a[1].max(b[1])])?;
let (du, dv) = (b[0] - a[0], b[1] - a[1]);
let value = (muu * du * du + muv * (du * dv).abs() + mvv * dv * dv).sqrt();
value.is_finite().then_some(value)
}
fn over(&self, [u0, u1, v0, v1]: [f64; 4]) -> Option<[f64; 3]> {
let span = |knots: &[f64], degree: usize, x: f64| -> Option<usize> {
let count = knots.len() - degree - 1;
if !(x >= knots[degree] && x <= knots[count]) {
return None;
}
let mut k = degree;
while k + 1 < count && knots[k + 1] <= x {
k += 1;
}
Some(k)
};
let (p, q) = (self.degree_u, self.degree_v);
let low = |knots: &[f64], degree: usize, x: f64| span(knots, degree, x).map(|k| if k > degree && knots[k] == x { k - 1 } else { k });
let (ka, kb) = (low(&self.knots_u, p, u0)?, span(&self.knots_u, p, u1)?);
let (la, lb) = (low(&self.knots_v, q, v0)?, span(&self.knots_v, q, v1)?);
let mut m = [0.0f64; 3];
for k in ka..=kb {
for l in la..=lb {
let entry = self.table[(k - p) * self.spans_v + (l - q)];
for slot in 0..3 {
m[slot] = m[slot].max(entry[slot]);
}
}
}
Some(m)
}
pub fn triangle(&self, uv: [[f64; 2]; 3]) -> Option<f64> {
let lo = |k: usize| uv.iter().map(|p| p[k]).fold(f64::INFINITY, f64::min);
let hi = |k: usize| uv.iter().map(|p| p[k]).fold(f64::NEG_INFINITY, f64::max);
let [muu, muv, mvv] = self.over([lo(0), hi(0), lo(1), hi(1)])?;
let form = |c: [f64; 2]| {
let du = uv.iter().map(|p| (p[0] - c[0]).powi(2)).fold(0.0f64, f64::max);
let dv = uv.iter().map(|p| (p[1] - c[1]).powi(2)).fold(0.0f64, f64::max);
let (a, b) = if du > 0.0 && dv > 0.0 {
let alpha = (dv / du).sqrt();
(muu + alpha * muv, mvv + muv / alpha)
} else {
(muu, mvv)
};
uv.iter().map(|p| a * (p[0] - c[0]).powi(2) + b * (p[1] - c[1]).powi(2)).fold(0.0f64, f64::max)
};
let mid = |i: usize, j: usize| [(uv[i][0] + uv[j][0]) * 0.5, (uv[i][1] + uv[j][1]) * 0.5];
let centroid = [(uv[0][0] + uv[1][0] + uv[2][0]) / 3.0, (uv[0][1] + uv[1][1] + uv[2][1]) / 3.0];
let best = [form(centroid), form(mid(0, 1)), form(mid(1, 2)), form(mid(2, 0))].into_iter().fold(f64::INFINITY, f64::min);
let value = 0.5 * best;
value.is_finite().then_some(value)
}
}
pub fn face_bound(face: &FaceRecord) -> Option<FaceBound> {
if !active() {
return None;
}
use crate::AnalyticSurface;
match face.surface.analytic() {
Some(AnalyticSurface::Plane { .. }) => return Some(FaceBound::Plane),
Some(AnalyticSurface::Sphere { frame, radius }) => {
return (radius.is_finite() && *radius > 0.0).then_some(FaceBound::Sphere { centre: frame.origin, radius: *radius });
}
Some(AnalyticSurface::RuledRevolution { frame, rho0, rho1, height }) => {
return (rho0.is_finite() && rho1.is_finite() && height.is_finite() && *height > 0.0).then_some(FaceBound::Ruled {
origin: frame.origin,
axis: frame.axis,
rho0: *rho0,
rho1: *rho1,
height: *height,
});
}
_ => {}
}
if let Some(affine) = face.surface.affine_sphere() {
let largest = affine.semi_axes()[0];
if !(largest.is_finite() && largest > 0.0) {
return None;
}
return Some(FaceBound::Affine { sphere: affine.clone(), largest_semi_axis: largest });
}
if spline_enabled() && face.surface.analytic().is_none() {
return SplineBound::new(&face.surface).map(|bound| FaceBound::Spline(std::sync::Arc::new(bound)));
}
None
}
fn pad(scale: f64) -> f64 {
256.0 * f64::EPSILON * scale.max(1.0)
}
pub(super) fn reaches_apex(bound: &FaceBound, positions: impl Iterator<Item = Vec3>, margin: f64) -> bool {
let FaceBound::Ruled { origin, axis, rho0, rho1, height } = *bound else {
return false;
};
if rho0 == rho1 {
return false;
}
let apex = rho0 * height / (rho0 - rho1);
let (mut low, mut high) = (f64::INFINITY, f64::NEG_INFINITY);
for p in positions {
let z = p.sub(origin).dot(axis);
low = low.min(z);
high = high.max(z);
}
!(low <= high) || (apex >= low - margin && apex <= high + margin)
}
pub fn triangle_bound(bound: &FaceBound, t: [Vec3; 3]) -> Option<f64> {
if t.iter().any(|p| !(p.x.is_finite() && p.y.is_finite() && p.z.is_finite())) {
return None;
}
let scale = t.iter().map(|p| p.x.abs().max(p.y.abs()).max(p.z.abs())).fold(0.0f64, f64::max);
let value = match *bound {
FaceBound::Plane => 0.0,
FaceBound::Sphere { centre, radius } => sphere_bound(centre, radius, t),
FaceBound::Ruled { origin, axis, rho0, rho1, height } => {
let radial = |p: Vec3| {
let d = p.sub(origin);
d.sub(axis.scale(d.dot(axis)))
};
let projected = [radial(t[0]), radial(t[1]), radial(t[2])];
let surface_rho = |p: Vec3| rho0 + (rho1 - rho0) * (p.sub(origin).dot(axis) / height);
let mut reach = 0.0f64;
let mut outward = 0.0f64;
for (p, q) in t.iter().zip(&projected) {
let (rho, surface) = (q.length(), surface_rho(*p));
reach = reach.max(rho).max(surface);
outward = outward.max(rho - surface);
}
let inner = point_triangle_distance(Vec3::default(), projected);
let mut inside = reach - inner;
if let Some(n) = bisector(projected) {
let linear = t
.iter()
.zip(&projected)
.map(|(p, q)| surface_rho(*p) - q.dot(n))
.fold(f64::NEG_INFINITY, f64::max);
inside = inside.min(linear);
}
inside.max(0.0) + outward.max(0.0)
}
FaceBound::Spline(_) => return None,
FaceBound::Affine { ref sphere, largest_semi_axis } => {
let back = [sphere.preimage(t[0]), sphere.preimage(t[1]), sphere.preimage(t[2])];
largest_semi_axis * sphere_bound(Vec3::default(), 1.0, back)
}
};
value.is_finite().then(|| value + pad(scale))
}
fn bisector(projected: [Vec3; 3]) -> Option<Vec3> {
let mut directions = [Vec3::default(); 3];
for (slot, q) in directions.iter_mut().zip(&projected) {
let length = q.length();
if !(length > 0.0) {
return None;
}
*slot = q.scale(1.0 / length);
}
let pairs = [(0, 1, 2), (1, 2, 0), (0, 2, 1)];
let (i, j, k) = pairs
.into_iter()
.min_by(|a, b| directions[a.0].dot(directions[a.1]).total_cmp(&directions[b.0].dot(directions[b.1])))?;
let sum = directions[i].add(directions[j]);
let length = sum.length();
if !(length > 1e-6) {
return None;
}
let n = sum.scale(1.0 / length);
(directions[k].dot(n) >= directions[i].dot(n) - 1e-12).then_some(n)
}
fn sphere_bound(centre: Vec3, radius: f64, t: [Vec3; 3]) -> f64 {
let reach = t.iter().map(|p| p.sub(centre).length()).fold(0.0f64, f64::max);
let inner = point_triangle_distance(centre, t);
(reach.max(radius) - inner).max(0.0) + (reach - radius).max(0.0)
}
pub(super) fn point_triangle_distance(p: Vec3, t: [Vec3; 3]) -> f64 {
let segment = |a: Vec3, b: Vec3| -> f64 {
let ab = b.sub(a);
let length_squared = ab.dot(ab);
if !(length_squared > 0.0) {
return p.sub(a).length();
}
let s = (p.sub(a).dot(ab) / length_squared).clamp(0.0, 1.0);
p.sub(a.add(ab.scale(s))).length()
};
let edges = segment(t[0], t[1]).min(segment(t[1], t[2])).min(segment(t[2], t[0]));
let normal = t[1].sub(t[0]).cross(t[2].sub(t[0]));
let area_squared = normal.dot(normal);
let magnitude = t.iter().map(|q| q.sub(p).length()).fold(0.0f64, f64::max);
let mut best = edges;
if area_squared > 0.0 {
let n = normal.scale(1.0 / area_squared.sqrt());
let height = p.sub(t[0]).dot(n);
let foot = p.sub(n.scale(height));
let inside = (0..3).all(|i| {
let a = t[i];
let b = t[(i + 1) % 3];
b.sub(a).cross(foot.sub(a)).dot(normal) >= 0.0
});
if inside {
best = best.min(height.abs());
}
}
(best - pad(magnitude)).max(0.0)
}