use std::collections::BTreeMap;
use super::Context;
use super::policy::{AXIS_ALIGNED_COS, INTERIOR_PROBE_FRAC};
use crate::kernel::brep::Part;
use crate::kernel::classify::material_at;
use crate::kernel::geom::{Bounds, Surface, V3};
const RUN_AXIS_COS: f64 = 0.05;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BevelReject {
Nonplanar,
Aligned,
Compound,
}
#[derive(Clone, Copy, Debug)]
pub struct Bevel {
pub edge_axis: usize,
pub normal: V3,
pub bounds: Bounds,
pub leg_hi: f64,
pub leg_lo: f64,
}
internal_entry! {
pub fn classify_bevel(part: &Part, face: usize) -> Result<Bevel, BevelReject> {
let Surface::Plane { .. } = part.faces[face].surface else {
return Err(BevelReject::Nonplanar);
};
let normal = part
.face_normal(face, 0.0, 0.0)
.ok_or(BevelReject::Nonplanar)?;
if normal.iter().any(|c| c.abs() > AXIS_ALIGNED_COS) {
return Err(BevelReject::Aligned);
}
let edge_axis = (0..3)
.find(|&i| normal[i].abs() < RUN_AXIS_COS)
.ok_or(BevelReject::Compound)?;
let bounds = part.face_bounds(face);
let legs: Vec<f64> = (0..3)
.filter(|&j| j != edge_axis)
.map(|j| bounds.max[j] - bounds.min[j])
.collect();
Ok(Bevel {
edge_axis,
normal,
bounds,
leg_hi: legs[0].max(legs[1]),
leg_lo: legs[0].min(legs[1]),
})
}
}
internal_entry! {
pub fn near_corner(centre: V3, edge_axis: usize, planes: &BTreeMap<usize, f64>, toward: f64) -> V3 {
let mut corner = [0.0; 3];
corner[edge_axis] = centre[edge_axis];
for (&axis, &coord) in planes {
corner[axis] = coord;
}
let step = toward * INTERIOR_PROBE_FRAC;
[0, 1, 2].map(|i| corner[i] + step * (centre[i] - corner[i]))
}
}
internal_entry! {
pub fn material_beyond_corner(
ctx: &Context<'_>,
centre: V3,
edge_axis: usize,
planes: &BTreeMap<usize, f64>,
) -> bool {
material_at(
ctx.classifier(),
near_corner(centre, edge_axis, planes, -1.0),
)
}
}
internal_entry! {
pub fn convex_bevel(
ctx: &Context<'_>,
centre: V3,
edge_axis: usize,
planes: &BTreeMap<usize, f64>,
) -> bool {
!material_at(
ctx.classifier(),
near_corner(centre, edge_axis, planes, 1.0),
)
}
}
pub(crate) fn proved_bevel_planes(
ctx: &super::Context<'_>,
face: usize,
centre: V3,
edge_axis: usize,
) -> Option<BTreeMap<usize, f64>> {
let crate::kernel::geom::Surface::Plane { frame } = ctx.part.faces[face].surface else {
return None;
};
let candidates =
super::planes::nearest_axis_aligned_plane_candidates(ctx.part, face, centre, edge_axis);
let ambiguous = candidates.values().any(|coords| {
let lo = coords.iter().copied().fold(f64::INFINITY, f64::min);
let hi = coords.iter().copied().fold(f64::NEG_INFINITY, f64::max);
hi - lo > crate::kernel::geom::COORD_FLOOR
});
let others: Vec<_> = (0..3).filter(|&i| i != edge_axis).collect();
let mut proved: Option<(V3, BTreeMap<usize, f64>)> = None;
for &a in candidates.get(&others[0])? {
for &b in candidates.get(&others[1])? {
let planes = BTreeMap::from([(others[0], a), (others[1], b)]);
let mut corner = centre;
corner[others[0]] = a;
corner[others[1]] = b;
if (ambiguous
&& crate::kernel::geom::dot(
crate::kernel::geom::sub(corner, frame.origin),
frame.z,
)
.abs()
<= crate::kernel::geom::COORD_FLOOR)
|| !convex_bevel(ctx, centre, edge_axis, &planes)
{
continue;
}
if let Some((previous, _)) = &proved {
if crate::kernel::py::dist(previous, &corner) > crate::kernel::geom::COORD_FLOOR {
return None;
}
} else {
proved = Some((corner, planes));
}
}
}
proved.map(|(_, planes)| planes)
}
pub(crate) struct TransverseCorner {
pub point: V3,
pub normals: [V3; 2],
pub areas: [f64; 2],
}
pub(crate) fn transverse_corner(
part: &Part,
face: usize,
centre: V3,
run: V3,
) -> Option<TransverseCorner> {
use super::planes::unit_plane_normal;
use crate::kernel::geom::{self, COORD_FLOOR, SMOOTH_ARC_GAP};
let mut supports: Vec<(V3, V3, f64)> = Vec::with_capacity(2);
for f in part.neighbours(face) {
let Surface::Plane { frame } = part.faces[f].surface else {
continue;
};
let normal = unit_plane_normal(part, f)?;
if geom::dot(normal, run).abs() > SMOOTH_ARC_GAP {
continue;
}
let area = part.face_mass(f)?[0];
if let Some((_, origin, total)) = supports
.iter_mut()
.find(|(n, _, _)| geom::dot(*n, normal).abs() >= 1. - SMOOTH_ARC_GAP)
{
if geom::dot(geom::sub(frame.origin, *origin), normal).abs() > COORD_FLOOR {
return None;
}
*total += area;
continue;
}
if supports.len() == 2
|| supports
.iter()
.any(|(n, _, _)| geom::dot(*n, normal).abs() > SMOOTH_ARC_GAP)
{
return None;
}
supports.push((normal, frame.origin, area));
}
let [(a, pa, aa), (b, pb, ab)] = supports.as_slice() else {
return None;
};
let d = geom::dot(*a, *b);
let da = geom::dot(geom::sub(*pa, centre), *a);
let db = geom::dot(geom::sub(*pb, centre), *b);
Some(TransverseCorner {
point: geom::add(
centre,
geom::add(
geom::scale(*a, (da - d * db) / (1. - d * d)),
geom::scale(*b, (db - d * da) / (1. - d * d)),
),
),
normals: [*a, *b],
areas: [*aa, *ab],
})
}