use std::collections::BTreeMap;
use super::policy::{self, AXIS_ALIGNED_COS};
use crate::kernel::brep::Part;
use crate::kernel::geom::{Curve, Surface, V3, dominant_axis, norm};
use crate::kernel::py;
internal_entry! {
pub fn effective_plane(part: &Part, face: usize) -> Option<(V3, f64)> {
let frame = match &part.faces[face].surface {
Surface::Plane { frame } => *frame,
Surface::Freeform {
kind: "BSPLINE" | "BEZIER",
..
} => match part.recovered(face) {
Some(Surface::Plane { frame }) => *frame,
_ => return None,
},
_ => return None,
};
let z = frame.z;
let dominant = (0..3)
.max_by(|&a, &b| py::order(z[a].abs(), z[b].abs()))
.expect("three components");
let sign = if z[dominant] >= 0.0 { 1.0 } else { -1.0 };
let normal = z.map(|c| if c == 0.0 { 0.0 } else { sign * c });
let offset = py::sum((0..3).map(|i| frame.origin[i] * normal[i]));
let norm = py::sum(normal.iter().map(|c| c * c)).sqrt();
((norm - 1.0).abs() <= 1e-9 && offset.is_finite()).then_some((normal, offset))
}
}
internal_entry! {
pub fn axis_aligned_axis(part: &Part, face: usize) -> Option<(usize, f64)> {
let Surface::Plane { frame } = &part.faces[face].surface else {
return None;
};
let comp = frame.z.map(f64::abs);
if comp.iter().copied().fold(f64::NEG_INFINITY, f64::max) <= AXIS_ALIGNED_COS {
return None;
}
let ax = dominant_axis(comp);
Some((ax, frame.origin[ax]))
}
}
internal_entry! {
pub fn nearest_axis_aligned_planes(
part: &Part,
face: usize,
centre: [f64; 3],
exclude_axis: usize,
_refuse_equidistant: bool,
) -> BTreeMap<usize, f64> {
let candidates = nearest_axis_aligned_plane_candidates(part, face, centre, exclude_axis);
let mut selected = BTreeMap::new();
for (axis, coords) in candidates {
let distance = |c: f64| (c - centre[axis]).abs();
let tied = coords;
let far = tied
.iter()
.map(|c| distance(*c))
.fold(f64::NEG_INFINITY, f64::max);
let (lo, hi) = tied
.iter()
.fold((f64::INFINITY, f64::NEG_INFINITY), |(l, h), c| {
(l.min(*c), h.max(*c))
});
if hi - lo > policy::length_tol(far, 1e-9) {
continue;
}
selected.insert(axis, 0.5 * (lo + hi));
}
selected
}
}
pub(crate) fn nearest_axis_aligned_plane_candidates(
part: &Part,
face: usize,
centre: V3,
exclude_axis: usize,
) -> BTreeMap<usize, Vec<f64>> {
let mut candidates: BTreeMap<usize, Vec<f64>> = BTreeMap::new();
for other in part.neighbours(face) {
match axis_aligned_axis(part, other) {
Some((ax, coord)) if ax != exclude_axis => {
candidates.entry(ax).or_default().push(coord)
}
_ => {}
}
}
candidates
.into_iter()
.map(|(axis, coords)| {
let distance = |c: f64| (c - centre[axis]).abs();
let nearest = coords
.iter()
.map(|c| distance(*c))
.fold(f64::INFINITY, f64::min);
let tied: Vec<f64> = coords
.iter()
.copied()
.filter(|c| {
(distance(*c) - nearest).abs()
<= policy::length_tol(distance(*c).max(nearest), 1e-9)
})
.collect();
(axis, tied)
})
.collect()
}
internal_entry! {
pub fn normalized(a: V3) -> Option<V3> {
let l = norm(a);
(l > 0.0).then(|| a.map(|c| c / l))
}
}
internal_entry! {
pub fn coordinates(p: V3, digits: usize) -> V3 {
p.map(|c| py::without_negative_zero(py::round_to(c, digits)))
}
}
internal_entry! {
pub fn linear_quad(part: &Part, face: usize) -> bool {
let f = &part.faces[face];
let edges = part.face_edges(face);
let mut vertices: Vec<usize> = edges
.iter()
.flat_map(|&e| [part.edges[e].vertices.0, part.edges[e].vertices.1])
.collect();
vertices.sort_unstable();
vertices.dedup();
matches!(f.surface, Surface::Plane { .. })
&& f.loops.len() == 1
&& part.outer_edges(face).len() == 4
&& part
.outer_edges(face)
.iter()
.all(|&e| matches!(part.edges[e].curve, Curve::Line { .. }))
&& vertices.len() == 4
}
}
internal_entry! {
pub fn unit_plane_normal(part: &Part, face: usize) -> Option<V3> {
part.face_normal(face, 0.0, 0.0).and_then(normalized)
}
}