use super::*;
const DELTA: f64 = 1e-12;
pub(super) fn enabled() -> bool {
crate::switch!("BREP_SOUNDNESS_TORUS_FACE", true)
}
pub(super) fn certifies(solid: &BrepSolid, _options: SelfIntersectionOptions) -> bool {
let faces: Vec<&FaceRecord> = solid.shells.iter().flat_map(|shell| &shell.faces).collect();
let [face] = faces[..] else { return false };
let Some((u, v)) = exact_torus_domain(&face.surface) else { return false };
let span = (u[1] - u[0]).abs().max(v[1] - v[0]).max(1.0);
let band = 1e-12 * span;
face.loops.iter().flat_map(|loop_record| &loop_record.coedges).all(|coedge| {
coedge.pcurve.control_points.iter().all(|control| {
let w = control.w;
if !(w > 0.0 && w.is_finite()) {
return false;
}
let (x, y) = (control.x / w, control.y / w);
x >= u[0] - band && x <= u[1] + band && y >= v[0] - band && y <= v[1] + band
})
})
}
fn exact_torus_domain(s: &NurbsSurface) -> Option<([f64; 2], [f64; 2])> {
let Some(crate::AnalyticSurface::Torus { frame, major_radius, minor_radius }) = s.analytic() else {
return None;
};
let (big, small) = (*major_radius, *minor_radius);
if !(small > 0.0 && big.is_finite() && small < big * (1.0 - 1e-6)) {
return None;
}
let rebuilt = crate::make_torus_surface(frame.origin, frame.axis, big, small).ok()?;
let same_bits = |a: &[f64], b: &[f64]| a.len() == b.len() && a.iter().zip(b).all(|(x, y)| x.to_bits() == y.to_bits());
if !same_bits(&s.knots_u, &rebuilt.knots_u)
|| !same_bits(&s.knots_v, &rebuilt.knots_v)
|| s.degree_u != rebuilt.degree_u
|| s.degree_v != rebuilt.degree_v
|| s.control_points.len() != rebuilt.control_points.len()
|| s.control_points.iter().zip(&rebuilt.control_points).any(|(a, b)| a.len() != b.len())
{
return None;
}
let size = big + small;
if !(DELTA * size <= 1e-3 * (big - small)) {
return None;
}
let close = s.control_points.iter().flatten().zip(rebuilt.control_points.iter().flatten()).all(|(a, b)| {
(a.x - b.x).abs() <= DELTA * size
&& (a.y - b.y).abs() <= DELTA * size
&& (a.z - b.z).abs() <= DELTA * size
&& (a.w - b.w).abs() <= DELTA
&& a.w > 0.0
});
if !close {
return None;
}
Some(([*s.knots_u.first()?, *s.knots_u.last()?], [*s.knots_v.first()?, *s.knots_v.last()?]))
}