use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use super::Context;
use super::body::BodyKey;
use super::evidence::{self, EvidenceError, Occurrence};
use super::placed::{Grid, Placed};
use super::planes::unit_plane_normal;
use super::policy::{self, AXIS_ALIGNED_COS};
use super::turned::axis_letter;
use crate::kernel::brep::Part;
use crate::kernel::geom::{self, Bounds, Surface, V3};
use crate::kernel::py;
const TOL: f64 = 0.5;
const ORIENTED_ANGLE_DIGITS: usize = 9;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Plate {
pub axis: char,
pub lo: Placed<f64>,
pub hi: Placed<f64>,
pub u: f64,
pub v: f64,
pub body_key: Option<BodyKey>,
}
impl Plate {
pub fn thickness(&self) -> f64 {
*self.hi - *self.lo
}
}
#[derive(Clone, Copy, Debug, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct PlateOptions {
pub min_area_frac: f64,
pub max_thick_frac: f64,
pub tol: Option<f64>,
}
impl Default for PlateOptions {
fn default() -> Self {
PlateOptions {
min_area_frac: 0.4,
max_thick_frac: 0.5,
tol: None,
}
}
}
internal_entry! {
pub fn recognise_plates(part: &Part, opts: &PlateOptions) -> Vec<Plate> {
super::records(discover(&Context::new(part), opts))
}
}
internal_entry! {
pub fn discover_verified(
ctx: &Context<'_>,
opts: &PlateOptions,
) -> Result<Vec<Occurrence<Plate>>, EvidenceError> {
let found = evidence::verified(ctx.part, discover(ctx, opts))?;
let mut used: Vec<usize> = Vec::new();
for o in &found {
if o.defining.iter().any(|f| used.contains(f)) {
return Err(EvidenceError::SharedEvidence);
}
used.extend(&o.defining);
}
Ok(found)
}
}
internal_entry! {
pub fn discover_locally_degraded(
ctx: &Context<'_>,
opts: &PlateOptions,
) -> Result<Vec<Occurrence<Plate>>, EvidenceError> {
evidence::locally_degraded(|| discover_verified(ctx, opts))
}
}
type Entry = (f64, f64, f64, f64, usize);
struct Group {
low: f64,
high: f64,
area: f64,
u_sum: f64,
v_sum: f64,
faces: Vec<usize>,
}
struct Scope {
faces: Vec<usize>,
bounds: Bounds,
key: Option<BodyKey>,
}
internal_entry! {
pub fn discover(ctx: &Context<'_>, opts: &PlateOptions) -> Vec<Occurrence<Plate>> {
let part = ctx.part;
let tol = opts.tol.unwrap_or(TOL);
let scopes: Vec<Scope> = if part.solids.is_empty() {
vec![Scope {
faces: (0..part.faces.len()).collect(),
bounds: *ctx.bounds(),
key: None,
}]
} else {
let keys = ctx.body_keys(true);
part.solids
.iter()
.enumerate()
.map(|(s, solid)| Scope {
faces: solid.faces.clone(),
bounds: part.solid_bounds(s),
key: keys[s].clone(),
})
.collect()
};
let mut out: Vec<Occurrence<Plate>> = Vec::new();
for scope in &scopes {
let mut seen: Vec<(char, f64, f64)> = Vec::new();
for mut o in proposals(part, scope, opts, tol) {
if evidence::local_degradation()
&& evidence::locally_valid_solid(part, &o.defining).is_none()
{
continue;
}
let key = (o.record.axis, *o.record.lo, *o.record.hi);
if seen.contains(&key) {
continue;
}
seen.push(key);
o.record.body_key = scope.key.clone();
out.push(o);
}
}
out.sort_by(|a, b| {
let (a, b) = (&a.record, &b.record);
a.axis
.cmp(&b.axis)
.then(py::order(*a.lo, *b.lo))
.then(py::order(*a.hi, *b.hi))
.then(py::order(a.u, b.u))
.then(py::order(a.v, b.v))
});
out
}
}
fn proposals(part: &Part, scope: &Scope, opts: &PlateOptions, tol: f64) -> Vec<Occurrence<Plate>> {
let b = &scope.bounds;
let extents: V3 = std::array::from_fn(|i| b.max[i] - b.min[i]);
let faces: Vec<usize> = scope
.faces
.iter()
.copied()
.filter(|&f| matches!(part.faces[f].surface, Surface::Plane { .. }))
.collect();
let mut out = Vec::new();
for i in 0..3 {
let oi: Vec<usize> = (0..3).filter(|&j| j != i).collect();
let mut sides: [Vec<Entry>; 2] = [Vec::new(), Vec::new()];
for &face in &faces {
let Some(normal) = unit_plane_normal(part, face) else {
continue;
};
let component = normal[i];
if component.abs() < AXIS_ALIGNED_COS {
continue;
}
let (Some([area, _]), Some(centre)) = (part.face_mass(face), part.face_centre(face))
else {
continue;
};
let Surface::Plane { frame } = part.faces[face].surface else {
unreachable!("planar faces only");
};
sides[usize::from(component > 0.0)].push((
frame.origin[i],
area,
centre[oi[0]] * area,
centre[oi[1]] * area,
face,
));
}
let [negative, positive] = sides.map(|side| group(&side, tol));
let maximum_thickness = opts.max_thick_frac * extents[i];
if !negative.keys().any(|&low| {
positive.keys().any(|&high| {
tol < high.0 - low.0 && policy::clears_threshold(maximum_thickness, high.0 - low.0)
})
}) {
continue;
}
let cross = oriented_cross_area(part, scope, &faces, i, extents);
if cross <= 0.0 {
continue;
}
let threshold = opts.min_area_frac * cross;
let mut events: Vec<(f64, i8, &Group)> = negative
.iter()
.filter(|(_, g)| policy::clears_threshold(g.area, threshold))
.map(|(c, g)| (c.0, -1, g))
.chain(
positive
.iter()
.filter(|(_, g)| policy::clears_threshold(g.area, threshold))
.map(|(c, g)| (c.0, 1, g)),
)
.collect();
events.sort_by(|a, b| py::order(a.0, b.0).then(a.1.cmp(&b.1)));
for w in events.windows(2) {
let ((low, low_sign, low_group), (high, high_sign, high_group)) = (w[0], w[1]);
if low_sign != -1 || high_sign != 1 {
continue;
}
let thickness = high - low;
if thickness <= tol || !policy::clears_threshold(maximum_thickness, thickness) {
continue;
}
let low_skin = supported_skin(part, low_group, high_group, i);
let high_skin = supported_skin(part, high_group, low_group, i);
let low_group = low_skin.as_ref().unwrap_or(low_group);
let high_group = high_skin.as_ref().unwrap_or(high_group);
let combined_area = low_group.area + high_group.area;
out.push(Occurrence {
record: Plate {
axis: axis_letter(i),
lo: Placed::from_parts(low_group.low, low_group.low, Grid::Decimals(3)),
hi: Placed::from_parts(high_group.high, high_group.high, Grid::Decimals(3)),
u: (low_group.u_sum + high_group.u_sum) / combined_area,
v: (low_group.v_sum + high_group.v_sum) / combined_area,
body_key: None,
},
defining: low_group
.faces
.iter()
.chain(&high_group.faces)
.copied()
.collect(),
context: vec![],
});
}
}
out.sort_by(|a, b| {
let (a, b) = (&a.record, &b.record);
a.axis
.cmp(&b.axis)
.then(py::order(*a.lo, *b.lo))
.then(py::order(*a.hi, *b.hi))
});
out
}
#[derive(Clone, Copy, PartialEq)]
struct Coord(f64);
impl Eq for Coord {}
impl PartialOrd for Coord {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Coord {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
py::order(self.0, other.0)
}
}
fn group(side: &[Entry], tol: f64) -> BTreeMap<Coord, Group> {
let locations: Vec<f64> = side.iter().map(|e| e.0).collect();
let mut groups = BTreeMap::new();
for cluster in policy::cluster_coordinates(&locations, tol) {
let low = cluster
.iter()
.map(|&k| side[k].0)
.fold(f64::INFINITY, f64::min);
let high = cluster
.iter()
.map(|&k| side[k].0)
.fold(f64::NEG_INFINITY, f64::max);
let sum = |f: fn(&Entry) -> f64| cluster.iter().fold(0.0, |acc, &k| acc + f(&side[k]));
groups.insert(
Coord(0.5 * (low + high)),
Group {
low,
high,
area: sum(|e| e.1),
u_sum: sum(|e| e.2),
v_sum: sum(|e| e.3),
faces: cluster.iter().map(|&k| side[k].4).collect(),
},
);
}
groups
}
fn supported_skin(part: &Part, band: &Group, opposite: &Group, axis: usize) -> Option<Group> {
let floor = crate::kernel::geom::COORD_FLOOR;
if band.high - band.low <= floor {
return None;
}
let mut envelope = Bounds::empty();
for &face in &opposite.faces {
envelope.merge(&part.face_bounds(face));
}
let faces: Vec<usize> = band
.faces
.iter()
.copied()
.filter(|&face| {
let bounds = part.face_bounds(face);
(0..3).filter(|&j| j != axis).all(|j| {
bounds.min[j] >= envelope.min[j] - floor && bounds.max[j] <= envelope.max[j] + floor
})
})
.collect();
if faces.is_empty() || faces.len() == band.faces.len() {
return None;
}
let other: Vec<usize> = (0..3).filter(|&j| j != axis).collect();
let entries: Vec<Entry> = faces
.into_iter()
.map(|face| {
let Surface::Plane { frame } = part.faces[face].surface else {
unreachable!("planar face band");
};
let [area, _] = part.face_mass(face).unwrap();
let centre = part.face_centre(face).unwrap();
(
frame.origin[axis],
area,
centre[other[0]] * area,
centre[other[1]] * area,
face,
)
})
.collect();
let skin = group(&entries, band.high - band.low).into_values().next()?;
if !policy::clears_threshold(skin.area, band.area - skin.area)
|| (skin.low == band.low && skin.high == band.high)
{
return None;
}
Some(skin)
}
fn oriented_cross_area(part: &Part, scope: &Scope, faces: &[usize], i: usize, extents: V3) -> f64 {
let other: [usize; 2] = match i {
0 => [1, 2],
1 => [0, 2],
_ => [0, 1],
};
let vertices: Vec<V3> = scope
.faces
.iter()
.flat_map(|&f| &part.faces[f].loops)
.flat_map(|lp| {
lp.vertex.into_iter().chain(
lp.edges
.iter()
.flat_map(|&(e, _)| [part.edges[e].start, part.edges[e].end]),
)
})
.collect();
let support_eps = extents.iter().copied().fold(1.0, f64::max) * 1e-9;
let project = |p: V3, n: V3| p[0] * n[0] + p[1] * n[1] + p[2] * n[2];
let mut extreme_projections: Vec<(V3, f64)> = Vec::new();
let mut angles: Vec<f64> = Vec::new();
for &face in faces {
let Some(normal) = unit_plane_normal(part, face) else {
continue;
};
if normal[i].abs() > 1.0 - AXIS_ALIGNED_COS {
continue;
}
if normal[other[0]].hypot(normal[other[1]]) < AXIS_ALIGNED_COS {
continue;
}
let Surface::Plane { frame } = part.faces[face].surface else {
continue;
};
let plane_projection = project(frame.origin, normal);
if !vertices.is_empty() {
let extreme = match extreme_projections.iter().find(|(n, _)| *n == normal) {
Some(&(_, e)) => e,
None => {
let e = vertices
.iter()
.map(|&v| project(v, normal))
.fold(f64::NEG_INFINITY, f64::max);
extreme_projections.push((normal, e));
e
}
};
if extreme > plane_projection + support_eps {
continue; }
}
let angle = py::modulo(normal[other[1]].atan2(normal[other[0]]).to_degrees(), 90.0);
let angle = py::modulo(py::round_to(angle, ORIENTED_ANGLE_DIGITS), 90.0);
if !angles.contains(&angle) {
angles.push(angle);
}
}
if angles.is_empty() {
return extents[other[0]] * extents[other[1]];
}
let sign = if i == 1 { 1.0 } else { -1.0 };
let mut axis = [0.0; 3];
axis[i] = 1.0;
angles
.iter()
.map(|&angle| {
if angle == 0.0 {
return extents[other[0]] * extents[other[1]];
}
let (s, c) = (-sign * angle).to_radians().sin_cos();
let size = |j: usize| {
let mut e = [0.0; 3];
e[j] = 1.0;
let d = geom::add(geom::scale(e, c), geom::scale(geom::cross(axis, e), s));
let (lo, hi) = part.extent_along(&scope.faces, d);
hi - lo
};
size(other[0]) * size(other[1])
})
.fold(f64::INFINITY, f64::min)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn face_band_midpoint_is_covariant_under_axis_reversal() {
let side = [(0.0, 1.0, 0.0, 0.0, 0), (0.5, 1.0, 0.0, 0.0, 1)];
let groups = group(&side, 0.5);
assert_eq!(groups.len(), 1);
let (key, g) = groups.first_key_value().unwrap();
assert_eq!(key.0, 0.25);
assert_eq!((g.low, g.high), (0.0, 0.5));
let moved = side.map(|(c, a, u, v, f)| (123.456 - c, a, u, v, f));
let groups = group(&moved, 0.5);
assert_eq!(groups.len(), 1);
let (key, g) = groups.first_key_value().unwrap();
assert!((key.0 - (123.456 - 0.25)).abs() < 1e-12);
assert_eq!((g.low, g.high), (123.456 - 0.5, 123.456));
}
}