use serde::{Deserialize, Serialize};
use super::Context;
use super::evidence::{self, EvidenceError, Occurrence};
use super::placed::{Grid, Placed};
use super::policy;
use super::turned::cone_rims;
use crate::kernel::brep::Part;
use crate::kernel::geom::{self, Surface, V3};
use crate::kernel::py;
const MINOR_MATCH_FRAC: f64 = 0.0167;
const COAXIAL_FRAC: f64 = 0.0167;
const MAX_INCLUDED_ANGLE: f64 = 160.0;
pub(super) const MIN_MAJOR_RATIO: f64 = 1.5;
const MIN_SWEEP_DEG: f64 = 270.0;
const SAME_CONE_ANGLE: f64 = 1e-6;
const HOLE_DIA_FRAC: f64 = 0.0333;
const HOLE_AXIS_FRAC: f64 = 0.0333;
const HOLE_MOUTH_FRAC: f64 = 0.0833;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CounterSink {
pub axis: V3,
pub location: Placed<V3>,
pub major_diameter: f64,
pub drill_diameter: f64,
pub included_angle: f64,
pub depth: f64,
}
pub struct HoleMouth {
pub axis: V3,
pub location: V3,
pub diameter: f64,
pub depth: f64,
pub through: bool,
}
internal_entry! {
pub fn discover_verified(ctx: &Context<'_>) -> Result<Vec<Occurrence<CounterSink>>, EvidenceError> {
evidence::verified(ctx.part, discover(ctx))
}
}
internal_entry! {
pub fn recognise_countersinks(part: &Part) -> Vec<CounterSink> {
discover(&Context::new(part))
.into_iter()
.map(|o| o.record)
.collect()
}
}
internal_entry! {
pub fn countersink_matches_hole(cs: &CounterSink, hole: &HoleMouth) -> bool {
let minor = [0, 1, 2].map(|i| cs.location[i] + cs.depth * cs.axis[i]);
let offset = geom::sub(minor, hole.location);
let axial = py::sum((0..3).map(|i| offset[i] * hole.axis[i]));
let perpendicular = py::hypot(&[0, 1, 2].map(|i| offset[i] - axial * hole.axis[i]));
if perpendicular > policy::length_tol(hole.diameter, HOLE_AXIS_FRAC)
|| (cs.drill_diameter - hole.diameter).abs()
> policy::length_tol(hole.diameter, HOLE_DIA_FRAC)
{
return false;
}
let mouth = policy::length_tol(hole.diameter, HOLE_MOUTH_FRAC);
axial.abs() <= mouth || (hole.through && (axial - hole.depth).abs() <= mouth)
}
}
fn opens_into_void(part: &Part, face: usize) -> bool {
let Surface::Cone { frame, .. } = part.faces[face].surface else {
return false;
};
let Some((u0, u1, v0, v1)) = part.uv_bounds(face) else {
return false;
};
let (u, v) = (0.5 * (u0 + u1), 0.5 * (v0 + v1));
let sample = part.faces[face].surface.value(u, v);
let Some(normal) = part.face_normal(face, u, v) else {
return false;
};
let offset = geom::sub(sample, frame.origin);
let radial = geom::sub(offset, geom::scale(frame.z, geom::dot(offset, frame.z)));
geom::dot(radial, normal) < 0.0
}
fn nappe_side(part: &Part, face: usize, apex: V3, z: V3) -> Option<f64> {
let (u0, u1, v0, v1) = part.uv_bounds(face)?;
let mid = part.faces[face]
.surface
.value(0.5 * (u0 + u1), 0.5 * (v0 + v1));
Some(geom::dot(geom::sub(mid, apex), z))
}
fn sweep_deg(part: &Part, face: usize, cones: &[usize], tol: f64) -> f64 {
let surface = &part.faces[face].surface;
let (
Surface::Cone {
frame, semi_angle, ..
},
Some(apex),
) = (surface, surface.cone_apex())
else {
return 0.0;
};
let Some(side) = nappe_side(part, face, apex, frame.z) else {
return 0.0;
};
let spans = cones.iter().filter_map(|&other| {
let s = &part.faces[other].surface;
let Surface::Cone {
frame: f,
semi_angle: a,
..
} = s
else {
return None;
};
let same = (a.abs() - semi_angle.abs()).abs() <= SAME_CONE_ANGLE
&& geom::dot(f.z, frame.z).abs() > 1.0 - 1e-3
&& s.cone_apex()
.is_some_and(|p| geom::norm(geom::sub(p, apex)) <= tol)
&& nappe_side(part, other, apex, frame.z).is_some_and(|o| o * side > 0.0);
same.then(|| part.uv_bounds(other).map_or(0.0, |(u0, u1, _, _)| u1 - u0))
});
py::fsum(spans).to_degrees()
}
fn dist_to_line(point: V3, line_point: V3, line_dir: V3) -> f64 {
let v = geom::sub(point, line_point);
geom::norm(geom::sub(v, geom::scale(line_dir, geom::dot(v, line_dir))))
}
internal_entry! {
pub fn discover(ctx: &Context<'_>) -> Vec<Occurrence<CounterSink>> {
let part = ctx.part;
let cones: Vec<usize> = (0..part.faces.len())
.filter(|&f| matches!(part.faces[f].surface, Surface::Cone { .. }))
.collect();
if cones.is_empty() {
return Vec::new();
}
let cylinders: Vec<(f64, V3, V3)> = part
.faces
.iter()
.filter_map(|f| match f.surface {
Surface::Cylinder { frame, radius } => Some((radius, frame.origin, frame.z)),
_ => None,
})
.collect();
let mut out = Vec::new();
for &face in &cones {
let Some(((minor_r, minor_pt), (major_r, opening), included_angle)) = cone_rims(part, face)
else {
continue; };
if major_r < MIN_MAJOR_RATIO * minor_r || included_angle > MAX_INCLUDED_ANGLE {
continue;
}
if !opens_into_void(part, face) {
continue;
}
if sweep_deg(part, face, &cones, policy::length_tol(2.0 * minor_r, COAXIAL_FRAC))
< MIN_SWEEP_DEG
{
continue; }
let along = geom::sub(minor_pt, opening);
let length = match geom::norm(along) {
l if l > 0.0 => l,
_ => 1.0,
};
let axis = along.map(|c| c / length);
let seated = cylinders.iter().any(|&(r, lp, ld)| {
(r - minor_r).abs() <= policy::length_tol(minor_r, MINOR_MATCH_FRAC)
&& geom::dot(axis, ld).abs() > 1.0 - 1e-3
&& dist_to_line(opening, lp, ld) <= policy::length_tol(2.0 * minor_r, COAXIAL_FRAC)
});
if !seated {
continue;
}
let r4 = |x: f64| py::round_to(x, 4);
out.push(Occurrence {
record: CounterSink {
axis: axis.map(r4),
location: Placed::new(opening, Grid::Decimals(4)),
major_diameter: r4(2.0 * major_r),
drill_diameter: r4(2.0 * minor_r),
included_angle,
depth: r4(length),
},
defining: vec![face],
context: vec![],
});
}
out.sort_by(|a, b| {
let key = |o: &Occurrence<CounterSink>| {
[
o.record.location[0],
o.record.location[1],
o.record.location[2],
o.record.major_diameter,
]
};
py::tuple_order(&key(a), &key(b))
});
out
}
}