use super::policy::round_measurement;
use super::recess_faces::{Cylinder, union_bb};
use super::recess_records::Recess;
use crate::kernel::geom::Bounds;
const CORNER_FIT_TOL: f64 = 0.011;
const QUARTER_RATIO_TOL: f64 = 0.1;
fn close(left: f64, right: f64) -> bool {
(left - right).abs() <= CORNER_FIT_TOL
}
fn same_span(actual: (f64, f64), expected: (f64, f64)) -> bool {
close(actual.0, expected.0) && close(actual.1, expected.1)
}
fn axis_bounds(bb: &Bounds, axis: usize) -> (f64, f64) {
(bb.min[axis], bb.max[axis])
}
fn candidate_radii<R: Recess>(cylinders: &[Cylinder], record: &R) -> Vec<f64> {
let mut radii: Vec<f64> = Vec::new();
for c in cylinders {
if !c.concave
|| c.axis != record.depth_axis()
|| c.radius <= 0.0
|| !same_span(axis_bounds(&c.bb, c.axis), (record.d_lo(), record.d_hi()))
{
continue;
}
if !radii.iter().any(|&r| close(c.radius, r)) {
radii.push(c.radius);
}
}
radii
}
fn site_matches<R: Recess>(
cylinders: &[Cylinder],
record: &R,
radius: f64,
long_side: i32,
width_side: i32,
) -> bool {
let (la, wa, da) = (record.long_axis(), record.width_axis(), record.depth_axis());
let long_bounds = if long_side < 0 {
(record.lo() - radius, record.lo())
} else {
(record.hi(), record.hi() + radius)
};
let width_low = record.w_center() - record.width() / 2.0;
let width_high = record.w_center() + record.width() / 2.0;
let width_bounds = if width_side < 0 {
(width_low, width_low + radius)
} else {
(width_high - radius, width_high)
};
let depth_bounds = (record.d_lo(), record.d_hi());
let long_center = if long_side < 0 {
record.lo()
} else {
record.hi()
};
let width_center = record.w_center() + f64::from(width_side) * (record.width() / 2.0 - radius);
let matches: Vec<&Cylinder> = cylinders
.iter()
.filter(|c| {
c.concave
&& c.axis == da
&& close(c.radius, radius)
&& close(c.location[la], long_center)
&& close(c.location[wa], width_center)
&& same_span(axis_bounds(&c.bb, da), depth_bounds)
})
.collect();
let Some((first, rest)) = matches.split_first() else {
return false;
};
let combined = rest.iter().fold(first.bb, |b, c| union_bb(&b, &c.bb));
let expected = [(la, long_bounds), (wa, width_bounds), (da, depth_bounds)];
if !expected
.iter()
.all(|&(axis, span)| same_span(axis_bounds(&combined, axis), span))
{
return false;
}
[la, wa].iter().all(|&axis| {
let (lo, hi) = axis_bounds(&combined, axis);
((hi - lo) / radius - 1.0).abs() <= QUARTER_RATIO_TOL
})
}
fn proved_corner_radius<R: Recess>(record: &R, cylinders: &[Cylinder]) -> Option<f64> {
if record.edge_anchored() {
return None;
}
let mut proved: Vec<f64> = Vec::new();
for radius in candidate_radii(cylinders, record) {
if radius >= record.width() / 2.0 - CORNER_FIT_TOL {
continue;
}
let all = [(-1, -1), (-1, 1), (1, -1), (1, 1)]
.iter()
.all(|&(l, w)| site_matches(cylinders, record, radius, l, w));
if all {
let published = round_measurement(radius, 2);
if published > 0.0
&& !proved
.iter()
.any(|r: &f64| (r - radius).abs() <= crate::kernel::geom::COORD_FLOOR)
{
proved.push(radius);
}
}
}
match proved[..] {
[only] => Some(only),
_ => None,
}
}
internal_entry! {
pub fn with_proved_corner_radius<R: Recess>(record: R, cylinders: &[Cylinder]) -> R {
let Some(radius) = proved_corner_radius(&record, cylinders) else {
return record;
};
let mut out = record.spanned(
record.lo() - radius,
record.hi() + radius,
round_measurement(record.hi() - record.lo() + 2.0 * radius, 2),
);
out.set_corner_radius(round_measurement(radius, 2));
out
}
}