use std::collections::BTreeMap;
use std::f64::consts::PI;
use serde::{Deserialize, Serialize};
use super::Context;
use super::bevel::{cylinder_support_tolerance, tangent_cylinder_corner};
use super::cylinders::CylinderEvidence;
use super::evidence::{self, EvidenceError, Occurrence};
use super::placed::{Grid, Placed};
use super::policy::AXIS_ALIGNED_COS;
use super::turned;
use crate::kernel::brep::Part;
use crate::kernel::classify::material_at;
use crate::kernel::geom::{self, Surface, V3, dominant_axis};
use crate::kernel::py;
const MIN_RADIUS: f64 = 0.6;
const FILLET_MAX_EXTENT: f64 = 3.0 * PI / 4.0;
const FILLET_SWEEP_TOL_DEG: f64 = 0.5;
const TURNED_FILLET_MAX_EXTENT: f64 = PI / 2.0 * 1.05;
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Fillet {
pub axis: FilletAxis,
pub radius: f64,
pub at: Placed<V3>,
pub turned: bool,
pub side: &'static str,
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
#[serde(untagged)]
pub enum FilletAxis {
Principal(char),
Direction(V3),
}
impl FilletAxis {
fn order(self, other: Self) -> std::cmp::Ordering {
use FilletAxis::{Direction, Principal};
match (self, other) {
(Principal(a), Principal(b)) => a.cmp(&b),
(Principal(_), Direction(_)) => std::cmp::Ordering::Less,
(Direction(_), Principal(_)) => std::cmp::Ordering::Greater,
(Direction(a), Direction(b)) => py::tuple_order(&a, &b),
}
}
}
#[derive(Clone, Copy, Debug, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct FilletOptions {
pub min_radius: Option<f64>,
pub max_radius_frac: f64,
pub include_cylindrical: bool,
}
impl Default for FilletOptions {
fn default() -> Self {
FilletOptions {
min_radius: None,
max_radius_frac: 0.45,
include_cylindrical: true,
}
}
}
internal_entry! {
pub fn recognise_fillets(part: &Part, opts: &FilletOptions) -> Vec<Fillet> {
discover(&Context::new(part), opts)
.into_iter()
.map(|o| o.record)
.collect()
}
}
internal_entry! {
pub fn discover_verified(
ctx: &Context<'_>,
opts: &FilletOptions,
) -> Result<Vec<Occurrence<Fillet>>, EvidenceError> {
evidence::verified(ctx.part, discover(ctx, opts))
}
}
internal_entry! {
pub fn fillet_anchor(part: &Part, face: usize) -> Option<V3> {
let (u0, u1, v0, v1) = part.uv_bounds(face)?;
Some(
part.faces[face]
.surface
.value(0.5 * (u0 + u1), 0.5 * (v0 + v1)),
)
}
}
fn occurrence(
axis: FilletAxis,
radius: f64,
at: V3,
turned: bool,
face: usize,
context: Vec<usize>,
) -> Occurrence<Fillet> {
Occurrence {
record: Fillet {
axis,
radius: py::round_to3(radius),
at: Placed::new(at, Grid::Decimals(3)),
turned,
side: "convex",
},
defining: vec![face],
context,
}
}
fn principal_axis(direction: V3) -> Option<usize> {
let comp = direction.map(f64::abs);
(comp[0].max(comp[1]).max(comp[2]) > AXIS_ALIGNED_COS).then(|| dominant_axis(comp))
}
internal_entry! {
pub fn discover(ctx: &Context<'_>, opts: &FilletOptions) -> Vec<Occurrence<Fillet>> {
let part = ctx.part;
let min_radius = opts.min_radius.unwrap_or(MIN_RADIUS);
let max_radius = opts.max_radius_frac * ctx.bounds().max_extent();
let mut out = Vec::new();
if opts.include_cylindrical {
for face in 0..part.faces.len() {
if let Some(found) = prismatic(ctx, face, min_radius, max_radius) {
out.push(found);
}
}
}
let external = turned::external_cylinders(ctx);
for face in 0..part.faces.len() {
if let Some(found) = turned(part, face, min_radius, max_radius, &external) {
out.push(found);
}
}
attach_spherical_corners(part, &mut out);
out.sort_by(|a, b| {
let key = |o: &Occurrence<Fillet>| (o.record.axis, o.record.at);
let (ka, kb) = (key(a), key(b));
ka.0.order(kb.0).then_with(|| py::tuple_order(&ka.1[..], &kb.1[..]))
});
out
}
}
fn prismatic(
ctx: &Context<'_>,
face: usize,
min_radius: f64,
max_radius: f64,
) -> Option<Occurrence<Fillet>> {
let part = ctx.part;
let Surface::Cylinder { frame, radius } = part.faces[face].surface else {
return None;
};
let (u0, u1, _, _) = part.uv_bounds(face)?;
if radius < min_radius || radius > max_radius {
return None;
}
let outward = part.frame_points_outward(face)?;
let at = fillet_anchor(part, face)?;
let corner =
tangent_cylinder_corner(part, face, at, frame.z, frame.origin, radius).or_else(|| {
(!outward)
.then(|| curved_wall_corner(part, face, at, frame.z, frame.origin, radius))
.flatten()
})?;
if corner.normals.iter().any(|&n| {
(geom::dot(geom::sub(frame.origin, corner.point), n).abs() - radius).abs()
> cylinder_support_tolerance(part)
}) || !sweep_matches_walls(u1 - u0, corner.normals)
{
return None;
}
let near = geom::add(
corner.point,
geom::scale(
geom::sub(at, corner.point),
super::policy::INTERIOR_PROBE_FRAC,
),
);
if material_at(ctx.classifier(), near) == outward {
return None;
}
let axis = run_axis(frame.z)?;
let mut found = occurrence(axis, radius, at, false, face, vec![]);
found.record.side = if outward { "convex" } else { "concave" };
Some(found)
}
pub(crate) fn curved_wall_corner(
part: &Part,
face: usize,
at: V3,
run: V3,
origin: V3,
radius: f64,
) -> Option<super::bevel::TransverseCorner> {
let tol = cylinder_support_tolerance(part);
let mut supports: Vec<(V3, V3)> = Vec::new();
let mut curved = false;
for n in part.neighbours(face) {
let (point, normal) = match part.faces[n].surface {
Surface::Plane { frame } => (frame.origin, super::planes::unit_plane_normal(part, n)?),
Surface::Cylinder {
frame,
radius: wall_radius,
} => {
if geom::norm(geom::cross(frame.z, run)) > geom::SMOOTH_ARC_GAP
|| part.arc(face, n) != Some(crate::kernel::brep::Arc::Smooth)
{
continue;
}
let delta = geom::sub(frame.origin, origin);
let transverse = geom::sub(delta, geom::scale(run, geom::dot(delta, run)));
let direction = geom::unit(transverse)?;
let station = geom::add(
origin,
geom::scale(run, geom::dot(geom::sub(at, origin), run)),
);
let shared = part.shared_edges(face, n);
if shared.is_empty() {
return None;
}
let mut contact = None;
for sign in [-1., 1.] {
let point = geom::add(station, geom::scale(direction, sign * radius));
if (geom::norm(geom::cross(geom::sub(point, frame.origin), frame.z))
- wall_radius)
.abs()
> tol
{
continue;
}
let (u, v) = part.faces[n].surface.parameters(point, None)?;
let normal = part.face_normal(n, u, v)?;
let (u, v) = part.faces[face].surface.parameters(point, None)?;
if geom::norm(geom::sub(normal, part.face_normal(face, u, v)?))
> geom::SMOOTH_ARC_GAP
{
continue;
}
if shared.iter().any(|&e| {
!matches!(part.edges[e].curve, crate::kernel::geom::Curve::Line { .. })
|| [part.edges[e].start, part.edges[e].end]
.iter()
.any(|&p| geom::norm(geom::cross(geom::sub(p, point), run)) > tol)
}) {
continue;
}
if contact.is_some() {
return None;
}
contact = Some((point, normal));
}
curved = true;
contact?
}
_ => continue,
};
if geom::dot(normal, run).abs() > geom::SMOOTH_ARC_GAP
|| (geom::dot(geom::sub(origin, point), normal).abs() - radius).abs() > tol
{
continue;
}
if let Some((p, n)) = supports
.iter()
.find(|(_, n)| geom::dot(*n, normal).abs() >= 1. - geom::SMOOTH_ARC_GAP)
{
if geom::dot(geom::sub(*p, point), *n).abs() > geom::COORD_FLOOR {
return None;
}
} else {
supports.push((point, normal));
}
}
let [(pa, a), (pb, b)] = supports.as_slice() else {
return None;
};
if !curved {
return None;
}
let d = geom::dot(*a, *b);
let da = geom::dot(geom::sub(*pa, at), *a);
let db = geom::dot(geom::sub(*pb, at), *b);
Some(super::bevel::TransverseCorner {
point: geom::add(
at,
geom::add(
geom::scale(*a, (da - d * db) / (1. - d * d)),
geom::scale(*b, (db - d * da) / (1. - d * d)),
),
),
normals: [*a, *b],
areas: [0.; 2],
})
}
fn attach_spherical_corners(part: &Part, found: &mut [Occurrence<Fillet>]) {
let cylinders: BTreeMap<usize, usize> = found
.iter()
.enumerate()
.filter(|(_, o)| !o.record.turned && o.record.side == "concave")
.map(|(i, o)| (o.defining[0], i))
.collect();
for face in 0..part.faces.len() {
let Some(rounds) = spherical_corner(part, face, &cylinders) else {
continue;
};
let owner = cylinders[&rounds[0]];
found[owner].defining.push(face);
found[owner].defining.sort_unstable();
for round in part
.neighbours(face)
.into_iter()
.filter(|&n| n != rounds[0])
{
if !found[owner].context.contains(&round) {
found[owner].context.push(round);
}
}
}
}
fn spherical_corner(
part: &Part,
face: usize,
cylinders: &BTreeMap<usize, usize>,
) -> Option<Vec<usize>> {
let Surface::Sphere { frame, radius } = part.faces[face].surface else {
return None;
};
if part.frame_points_outward(face) != Some(false) {
return None;
}
let mut neighbours = part.neighbours(face);
neighbours.sort_unstable();
let rounds: [usize; 3] = neighbours.try_into().ok()?;
let tol = cylinder_support_tolerance(part);
let mut axes = BTreeMap::new();
let mut straight = Vec::new();
for &n in &rounds {
let run = match part.faces[n].surface {
Surface::Cylinder {
frame: run,
radius: r,
} => {
cylinders.get(&n)?;
if (radius - r).abs() > tol
|| geom::norm(geom::cross(geom::sub(frame.origin, run.origin), run.z)) > tol
{
return None;
}
straight.push(n);
run.z
}
Surface::Torus {
frame: ring,
major,
minor,
} => {
let relative = geom::sub(frame.origin, ring.origin);
if (radius - minor).abs() > tol
|| geom::dot(relative, ring.z).abs() > tol
|| (geom::norm(relative) - major).abs() > tol
{
return None;
}
geom::unit(geom::cross(ring.z, relative))?
}
_ => return None,
};
if part.arc(face, n) != Some(crate::kernel::brep::Arc::Smooth) {
return None;
}
axes.insert(n, run);
}
if straight.len() < 2 {
return None;
}
if geom::dot(
axes[&rounds[0]],
geom::cross(axes[&rounds[1]], axes[&rounds[2]]),
)
.abs()
<= crate::kernel::geom::SMOOTH_ARC_GAP
{
return None;
}
let edges = part.face_edges(face);
if edges.is_empty() {
return None;
}
for edge in edges {
let owners = &part.edge_faces()[edge];
if owners.len() != 2 {
return None;
}
let n = *owners.iter().find(|&&n| n != face)?;
if !rounds.contains(&n) {
return None;
}
let run = axes[&n];
let crate::kernel::geom::Curve::Circle {
frame: arc,
radius: r,
} = part.edges[edge].curve
else {
return None;
};
if (r - radius).abs() > tol
|| geom::norm(geom::sub(arc.origin, frame.origin)) > tol
|| geom::norm(geom::cross(arc.z, run)) > crate::kernel::geom::SMOOTH_ARC_GAP
{
return None;
}
}
Some(straight)
}
fn run_axis(direction: V3) -> Option<FilletAxis> {
Some(if let Some(axis) = principal_axis(direction) {
FilletAxis::Principal(['x', 'y', 'z'][axis])
} else {
let direction = geom::unit(direction)?;
let sign = if direction[dominant_axis(direction.map(f64::abs))] < 0.0 {
-1.0
} else {
1.0
};
FilletAxis::Direction(geom::scale(direction, sign))
})
}
fn sweep_matches_walls(sweep: f64, normals: [V3; 2]) -> bool {
if !(sweep > 0.0 && sweep <= FILLET_MAX_EXTENT) {
return false;
}
let expected =
geom::norm(geom::cross(normals[0], normals[1])).atan2(geom::dot(normals[0], normals[1]));
(sweep - expected).abs() <= FILLET_SWEEP_TOL_DEG.to_radians()
}
fn turned(
part: &Part,
face: usize,
min_radius: f64,
max_radius: f64,
external: &BTreeMap<usize, &CylinderEvidence>,
) -> Option<Occurrence<Fillet>> {
let Surface::Torus { frame, minor, .. } = part.faces[face].surface else {
return None;
};
if minor < min_radius || minor > max_radius {
return None;
}
let (_, _, v0, v1) = part.uv_bounds(face)?;
if (v1 - v0).abs() > TURNED_FILLET_MAX_EXTENT {
return None;
}
let edge_axis = principal_axis(frame.z)?;
let context = turned_context(part, face, &part.neighbours(face), external)?;
let at = fillet_anchor(part, face)?;
Some(occurrence(
FilletAxis::Principal(['x', 'y', 'z'][edge_axis]),
minor,
at,
true,
face,
context,
))
}
internal_entry! {
pub fn turned_context(
part: &Part,
torus: usize,
neighbours: &[usize],
external: &BTreeMap<usize, &CylinderEvidence>,
) -> Option<Vec<usize>> {
let Surface::Torus { frame, .. } = part.faces[torus].surface else {
return None;
};
let coaxial = turned::coaxial_cylinders(neighbours, external, frame.origin, frame.z);
if coaxial.is_empty() {
return None;
}
let transverse = turned::transverse_planes(part, neighbours, frame.z);
let continuations: Vec<usize> = neighbours
.iter()
.copied()
.filter(|&n| matches!(part.faces[n].surface, Surface::Sphere { .. }))
.collect();
if transverse.is_empty() && !turned::bridges_two_bands(&coaxial) && continuations.is_empty() {
return None; }
let mut context: Vec<usize> = Vec::new();
for f in coaxial
.iter()
.map(|c| c.face)
.chain(transverse)
.chain(continuations)
{
if !context.contains(&f) {
context.push(f);
}
}
Some(context)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn principal_runs_keep_the_legacy_axis_letter_throughout_the_alignment_band() {
for degrees in [0_f64, 2.94, 5., 8.] {
let (s, c) = degrees.to_radians().sin_cos();
for (axis, direction) in [('x', [c, s, 0.]), ('y', [0., c, s]), ('z', [s, 0., c])] {
assert_eq!(run_axis(direction), Some(FilletAxis::Principal(axis)));
assert_eq!(
run_axis(direction.map(|d| -d)),
Some(FilletAxis::Principal(axis))
);
assert_eq!(
serde_json::to_value(run_axis(direction).unwrap()).unwrap(),
axis.to_string()
);
}
}
let (s, c) = 9_f64.to_radians().sin_cos();
assert!(matches!(
run_axis([c, s, 0.]),
Some(FilletAxis::Direction(_))
));
}
#[test]
fn sweep_follows_acute_right_and_obtuse_walls() {
let tolerance = FILLET_SWEEP_TOL_DEG.to_radians();
for wall_degrees in [60_f64, 86., 90., 94., 120., 160.] {
let sweep = PI - wall_degrees.to_radians();
let walls = [[1., 0., 0.], [sweep.cos(), sweep.sin(), 0.]];
assert!(sweep_matches_walls(sweep, walls));
assert!(sweep_matches_walls(sweep + 0.5 * tolerance, walls));
assert!(!sweep_matches_walls(sweep + 2.0 * tolerance, walls));
assert!(!sweep_matches_walls(sweep - 2.0 * tolerance, walls));
if wall_degrees != 90. {
assert!(!sweep_matches_walls(PI - sweep, walls));
}
for invalid in [0., -sweep, PI, 2. * PI, f64::NAN] {
assert!(!sweep_matches_walls(invalid, walls));
}
}
}
#[test]
fn modelled_near_quarter_rounds_pass_but_truncated_bend_skins_do_not() {
let walls = [[1., 0., 0.], [0., 1., 0.]];
assert!(sweep_matches_walls(
90.02457403831906_f64.to_radians(),
walls
));
assert!(!sweep_matches_walls(
25.376933525173_f64.to_radians(),
walls
));
assert!(sweep_matches_walls(90.499_f64.to_radians(), walls));
assert!(!sweep_matches_walls(90.501_f64.to_radians(), walls));
assert!(sweep_matches_walls(89.501_f64.to_radians(), walls));
assert!(!sweep_matches_walls(89.499_f64.to_radians(), walls));
}
#[test]
fn edge_rounds_stop_at_135_degrees() {
for degrees in [45_f64, 40., 5.879068972873341] {
let sweep = PI - degrees.to_radians();
let walls = [[1., 0., 0.], [sweep.cos(), sweep.sin(), 0.]];
assert_eq!(sweep_matches_walls(sweep, walls), degrees >= 45.);
}
}
#[test]
fn drafted_round_still_requires_tangent_walls_and_convex_material() {
let part = crate::read_step_file(
&std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/cast_bearing_unit.step.gz"),
)
.unwrap();
let face = 24;
let Surface::Cylinder { frame, radius } = part.faces[face].surface else {
panic!()
};
let accepts = |p: &Part| prismatic(&Context::new(p), face, MIN_RADIUS, 100.).is_some();
assert!(accepts(&part));
let mut faces = part.faces.clone();
faces[face].surface = Surface::Cylinder {
frame,
radius: radius + 0.1,
};
assert!(!accepts(&Part::new(
faces,
part.edges.clone(),
part.solids.clone()
)));
let mut faces = part.faces.clone();
faces[face].reversed = !faces[face].reversed;
assert!(!accepts(&Part::new(
faces,
part.edges.clone(),
part.solids.clone()
)));
let mut faces = part.faces.clone();
for n in part.neighbours(face) {
if matches!(faces[n].surface, Surface::Plane { .. }) {
faces[n].surface = Surface::Other {
kind: "missing support",
};
}
}
assert!(!accepts(&Part::new(
faces,
part.edges.clone(),
part.solids.clone()
)));
assert!(prismatic(&Context::new(&part), face, radius + 0.1, 100.).is_none());
assert!(prismatic(&Context::new(&part), face, MIN_RADIUS, radius - 0.1).is_none());
let mut over_limit = 0;
for f in 0..part.faces.len() {
if matches!(part.faces[f].surface, Surface::Cylinder { .. })
&& let Some((u0, u1, _, _)) = part.uv_bounds(f)
&& u1 - u0 >= PI
{
over_limit += 1;
assert!(prismatic(&Context::new(&part), f, MIN_RADIUS, 100.).is_none());
}
}
assert!(
over_limit > 0,
"fixture must exercise the bore/extent guard"
);
}
}