use serde::{Deserialize, Serialize};
use super::Context;
use super::body::BodyKey;
use super::cylinders::{
CylinderEvidence, canonical_axis_direction, full_cylinders, line_key, z_then_cross,
};
use super::evidence::{self, EvidenceError, Occurrence};
use super::placed::{Grid, Placed};
use super::planes::effective_plane;
use super::turned::{TurnedProfileKey, axis_letter, profile_key_from_bands};
use crate::kernel::brep::Part;
use crate::kernel::geom::{Bounds, dominant_axis_preferring_z};
use crate::kernel::py;
const OD_SPAN_PAD: f64 = 0.7;
const CHAMFER_ALLOWANCE_ABS: f64 = 0.5;
const CHAMFER_ALLOWANCE_FRAC: f64 = 0.12;
const SQUARENESS_TOL: f64 = 0.15;
const OD_FILL_MIN: f64 = 0.6;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TurnedStep {
pub axis: char,
pub lo: Placed<f64>,
pub hi: Placed<f64>,
pub diameter: f64,
pub profile: Option<TurnedProfileKey>,
}
impl TurnedStep {
pub fn length(&self) -> f64 {
*self.hi - *self.lo
}
}
internal_entry! {
pub fn recognise_turned_steps(part: &Part) -> Vec<TurnedStep> {
sorted(super::records(discover(&Context::new(part))))
}
}
internal_entry! {
pub fn sorted(mut steps: Vec<TurnedStep>) -> Vec<TurnedStep> {
steps.sort_by(order);
steps
}
}
internal_entry! {
pub fn sorted_occurrences(mut found: Vec<Occurrence<TurnedStep>>) -> Vec<Occurrence<TurnedStep>> {
found.sort_by(|a, b| order(&a.record, &b.record));
found
}
}
fn order(a: &TurnedStep, b: &TurnedStep) -> std::cmp::Ordering {
a.profile
.is_some()
.cmp(&b.profile.is_some())
.then_with(|| match (&a.profile, &b.profile) {
(Some(p), Some(q)) => p.order(q),
_ => std::cmp::Ordering::Equal,
})
.then(a.axis.cmp(&b.axis))
.then(py::order(*a.lo, *b.lo))
.then(py::order(*a.hi, *b.hi))
.then(py::order(a.diameter, b.diameter))
}
internal_entry! {
pub fn discover_verified(ctx: &Context<'_>) -> Result<Vec<Occurrence<TurnedStep>>, EvidenceError> {
evidence::verified(ctx.part, discover(ctx))
}
}
struct Scope<'c> {
faces: Vec<usize>,
bounds: Bounds,
key: Option<BodyKey>,
cyls: Vec<&'c CylinderEvidence>,
}
internal_entry! {
pub fn discover(ctx: &Context<'_>) -> Vec<Occurrence<TurnedStep>> {
let part = ctx.part;
let (z, cross) = z_then_cross(ctx.cylinders());
let inventory: Vec<&CylinderEvidence> = z.iter().chain(&cross).collect();
let scopes: Vec<Scope> = if part.solids.is_empty() {
vec![Scope {
faces: (0..part.faces.len()).collect(),
bounds: *ctx.bounds(),
key: None,
cyls: inventory.clone(),
}]
} else {
let keys = ctx.body_keys(true);
let single = part.solids.len() == 1;
part.solids
.iter()
.enumerate()
.map(|(s, solid)| Scope {
faces: solid.faces.clone(),
bounds: part.solid_bounds(s),
key: keys[s].clone(),
cyls: inventory
.iter()
.copied()
.filter(|c| single || c.solid == s)
.collect(),
})
.collect()
};
let mut out = Vec::new();
for scope in &scopes {
for (step, over) in proposals_one(part, scope) {
out.push(Occurrence {
record: step,
defining: over.iter().map(|c| c.face).collect(),
context: vec![],
});
}
}
out
}
}
type Proposal<'c> = (TurnedStep, Vec<&'c CylinderEvidence>);
fn proposals_one<'c>(part: &Part, scope: &Scope<'c>) -> Vec<Proposal<'c>> {
let ext: Vec<&CylinderEvidence> = scope.cyls.iter().copied().filter(|c| c.external).collect();
let owned: Vec<CylinderEvidence> = ext.iter().map(|c| (*c).clone()).collect();
let supported = full_cylinders(&owned);
if supported.is_empty() {
return vec![];
}
let mut counts: Vec<(usize, usize)> = Vec::new();
for c in &supported {
match counts.iter_mut().find(|(a, _)| *a == c.axis) {
Some((_, n)) => *n += 1,
None => counts.push((c.axis, 1)),
}
}
let most = counts.iter().map(|c| c.1).max().unwrap();
let modes: Vec<_> = counts.iter().filter(|c| c.1 == most).collect();
let [mode] = modes.as_slice() else {
return vec![];
};
let axis = mode.0;
let lines: Vec<Vec<f64>> = supported
.iter()
.filter(|c| c.axis == axis)
.map(line_key)
.collect();
let mut groups: Vec<(Vec<f64>, Vec<&CylinderEvidence>)> = Vec::new();
for band in ext {
let key = line_key(band);
if !lines.contains(&key) {
continue;
}
match groups.iter_mut().find(|(k, _)| *k == key) {
Some((_, members)) => members.push(band),
None => groups.push((key, vec![band])),
}
}
groups.sort_by(|a, b| py::tuple_order(&a.0, &b.0));
groups
.iter()
.flat_map(|(_, bands)| proposals_coaxial(part, scope, axis, bands))
.collect()
}
fn proposals_coaxial<'c>(
part: &Part,
scope: &Scope<'c>,
axis: usize,
bands: &[&'c CylinderEvidence],
) -> Vec<Proposal<'c>> {
let mut diameters: Vec<f64> = bands.iter().map(|c| py::round_to(c.diameter, 2)).collect();
diameters.sort_by(|a, b| py::order(*a, *b));
diameters.dedup();
if diameters.len() < 2 {
return vec![]; }
let b = &scope.bounds;
let perp: Vec<f64> = (0..3)
.filter(|&i| i != axis)
.map(|i| b.max[i] - b.min[i])
.collect();
let cross = perp[0].max(perp[1]);
let max_od = bands
.iter()
.map(|c| c.diameter)
.fold(f64::NEG_INFINITY, f64::max);
if cross <= 0.0
|| max_od < OD_FILL_MIN * cross
|| (perp[0] - perp[1]).abs() > SQUARENESS_TOL * cross
{
return vec![];
}
let profile = profile_key_from_bands(axis, bands, &scope.bounds, scope.key.clone());
let bands_over = |pos: f64| -> Vec<&'c CylinderEvidence> {
let mut over: Vec<&CylinderEvidence> = bands
.iter()
.copied()
.filter(|c| c.s_lo <= pos && pos <= c.s_hi)
.collect();
if over.is_empty() {
over = bands
.iter()
.copied()
.filter(|c| {
let pad = OD_SPAN_PAD.min((c.s_hi - c.s_lo) / 2.0);
c.s_lo - pad <= pos && pos <= c.s_hi + pad
})
.collect();
}
let widest = over
.iter()
.map(|c| c.diameter)
.fold(f64::NEG_INFINITY, f64::max);
over.retain(|c| c.diameter == widest);
over
};
let local_od = |pos: f64| bands_over(pos).first().map_or(0.0, |c| c.diameter / 2.0);
let planes = shoulder_stations(part, &scope.faces, axis, &profile, local_od);
if planes.len() < 3 {
return vec![]; }
let mut found: Vec<Proposal<'c>> = Vec::new();
for w in planes.windows(2) {
let (lo, hi) = (w[0], w[1]);
let mut over = bands_over((*lo + *hi) / 2.0);
if let Some(first) = over.first() {
let within: Vec<&CylinderEvidence> = bands
.iter()
.copied()
.filter(|c| c.diameter == first.diameter && c.s_hi > *lo && c.s_lo < *hi)
.collect();
if !within.is_empty() {
over = within;
}
}
let diameter = over.first().map_or(0.0, |c| c.diameter);
if diameter <= 0.0 {
continue; }
match found.last_mut() {
Some((previous, sources)) if previous.hi == lo && previous.diameter == diameter => {
previous.hi = hi;
for band in over {
if !sources.iter().any(|s| std::ptr::eq(*s, band)) {
sources.push(band);
}
}
}
_ => found.push((
TurnedStep {
axis: axis_letter(axis),
lo,
hi,
diameter,
profile: Some(profile.clone()),
},
over,
)),
}
}
if found.len() < 2 {
return vec![]; }
found
}
fn shoulder_stations(
part: &Part,
faces: &[usize],
axis: usize,
profile: &TurnedProfileKey,
local_od: impl Fn(f64) -> f64,
) -> Vec<Placed<f64>> {
let origin = profile.axis_origin;
let mut shoulders: Vec<Placed<f64>> = Vec::new();
for &face in faces {
let Some((normal, offset)) = effective_plane(part, face) else {
continue;
};
if dominant_axis_preferring_z(normal) != axis || !axis_aligned(axis, normal) {
continue;
}
let bb = part.face_bounds(face);
let others = (0..3).filter(|&j| j != axis);
let pos = (offset - py::sum(others.clone().map(|j| normal[j] * origin[j]))) / normal[axis];
let outer = others
.map(|j| {
(bb.min[j] - origin[j])
.abs()
.max((bb.max[j] - origin[j]).abs())
})
.fold(f64::NEG_INFINITY, f64::max);
let od = local_od(pos);
let allowance = (CHAMFER_ALLOWANCE_ABS + CHAMFER_ALLOWANCE_FRAC * od).min(od / 2.0);
if outer >= od - allowance {
let station = Placed::new(pos, Grid::Decimals(3));
match shoulders.iter_mut().find(|s| **s == station) {
Some(s) if station.raw() < s.raw() => *s = station,
Some(_) => {}
None => shoulders.push(station),
}
}
}
shoulders.sort_by(|a, b| py::order(**a, **b));
shoulders
}
fn axis_aligned(axis: usize, direction: [f64; 3]) -> bool {
let v = canonical_axis_direction(axis, direction);
(0..3).all(|i| {
if i == axis {
(v[i] - 1.0).abs() <= 1e-3
} else {
v[i].abs() <= 1e-3
}
})
}