use std::cmp::Ordering;
use haecceity::Part;
use haecceity::geom::{Bounds, COORD_FLOOR, Curve, Surface, V3, cross, sub};
use haecceity::py;
use haecceity::sampling::edge_interval;
use serde::Serialize;
const AXIS_ALIGNED_COS: f64 = 0.99;
const AXIS_ZERO_COS: f64 = 0.01;
fn clears_threshold(magnitude: f64, threshold: f64) -> bool {
magnitude >= threshold * (1.0 + 1e-9)
}
fn cluster_coordinates(coordinates: &[f64], tol: f64) -> Vec<Vec<usize>> {
let mut order: Vec<usize> = (0..coordinates.len()).collect();
order.sort_by(|&a, &b| py::order(coordinates[a], coordinates[b]).then(a.cmp(&b)));
let mut out = Vec::new();
let mut start = 0;
while start < order.len() {
let mut end = start + 1;
while end < order.len()
&& coordinates[order[end]] - coordinates[order[end - 1]] <= tol + COORD_FLOOR
{
end += 1;
}
let component = &order[start..end];
let n = component.len();
let mut minimum = vec![n + 1; n + 1];
let mut ways = vec![0usize; n + 1];
let mut next = vec![0; n];
minimum[n] = 0;
ways[n] = 1;
for i in (0..n).rev() {
for j in i..n {
if coordinates[component[j]] - coordinates[component[i]] > tol + COORD_FLOOR {
break;
}
let count = 1 + minimum[j + 1];
if count < minimum[i] {
minimum[i] = count;
ways[i] = ways[j + 1];
next[i] = j + 1;
} else if count == minimum[i] {
ways[i] = (ways[i] + ways[j + 1]).min(2);
}
}
}
if ways[0] == 1 {
let mut i = 0;
while i < n {
out.push(component[i..next[i]].to_vec());
i = next[i];
}
}
start = end;
}
out
}
const TOL: f64 = 0.5;
const STEP_MIN_AREA_FRAC: f64 = 0.01;
const STRUCTURAL_RAMP_MIN_FRAC: f64 = 0.1;
const BOUNDED_RISER_AREA_FRAC: f64 = 0.5;
const RISER_MIN_AREA_FRAC: f64 = 0.15;
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct FaceLevel {
pub(crate) z: f64,
pub(crate) x_span: (f64, f64),
pub(crate) y_span: (f64, f64),
}
impl FaceLevel {
fn order(&self, other: &Self) -> Ordering {
py::order(self.z, other.z)
.then_with(|| {
py::tuple_order(
&[self.x_span.0, self.x_span.1],
&[other.x_span.0, other.x_span.1],
)
})
.then_with(|| {
py::tuple_order(
&[self.y_span.0, self.y_span.1],
&[other.y_span.0, other.y_span.1],
)
})
}
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct RiserEvidence {
pub(crate) faces: Vec<usize>,
pub(crate) vertical: bool,
pub(crate) axis: char,
pub(crate) positions: Vec<f64>,
pub(crate) other_axis: char,
pub(crate) other_positions: Vec<f64>,
pub(crate) z_lo: f64,
pub(crate) z_hi: f64,
pub(crate) lo_at_envelope: bool,
pub(crate) hi_at_envelope: bool,
pub(crate) tol: f64,
pub(crate) body_levels: Vec<FaceLevel>,
}
impl RiserEvidence {
fn order(&self, other: &Self) -> Ordering {
self.vertical
.cmp(&other.vertical)
.then(self.axis.cmp(&other.axis))
.then_with(|| py::tuple_order(&self.positions, &other.positions))
.then(self.other_axis.cmp(&other.other_axis))
.then_with(|| py::tuple_order(&self.other_positions, &other.other_positions))
.then(py::order(self.z_lo, other.z_lo))
.then(py::order(self.z_hi, other.z_hi))
.then(self.lo_at_envelope.cmp(&other.lo_at_envelope))
.then(self.hi_at_envelope.cmp(&other.hi_at_envelope))
.then(py::order(self.tol, other.tol))
.then_with(|| {
self.body_levels
.iter()
.zip(&other.body_levels)
.map(|(a, b)| a.order(b))
.find(|o| o.is_ne())
.unwrap_or(self.body_levels.len().cmp(&other.body_levels.len()))
})
}
}
pub(crate) fn drawn_faces(part: &Part) -> Vec<usize> {
let mut faces: Vec<usize> = part
.solids
.iter()
.flat_map(|s| s.faces.iter().copied())
.collect();
if faces.is_empty() {
faces = (0..part.faces.len()).collect();
}
faces.sort_unstable();
faces.dedup();
faces
}
pub(crate) fn drawn_bounds(part: &Part, faces: &[usize]) -> Bounds {
let mut b = Bounds::empty();
for &f in faces {
b.merge(&part.face_bounds(f));
}
b
}
struct Scope {
faces: Vec<usize>,
bounds: Bounds,
}
fn scopes(part: &Part) -> Vec<Scope> {
if part.solids.is_empty() {
return vec![Scope {
faces: (0..part.faces.len()).collect(),
bounds: part.bounds(),
}];
}
part.solids
.iter()
.enumerate()
.map(|(s, solid)| Scope {
faces: solid.faces.clone(),
bounds: part.solid_bounds(s),
})
.collect()
}
fn level_proposals(
part: &Part,
scope: &Scope,
tol: f64,
min_area_frac: f64,
) -> Vec<(FaceLevel, Vec<usize>)> {
let mut zs = Vec::new();
let mut faces = Vec::new();
for &f in &scope.faces {
if let Surface::Plane { frame } = part.faces[f].surface
&& frame.z[2].abs() > AXIS_ALIGNED_COS
{
zs.push(frame.origin[2]);
faces.push(f);
}
}
let b = &scope.bounds;
let threshold = min_area_frac * (b.max[0] - b.min[0]) * (b.max[1] - b.min[1]);
let mut out = Vec::new();
for cluster in cluster_coordinates(&zs, tol) {
if min_area_frac > 0.0 {
let areas: Option<Vec<f64>> = cluster
.iter()
.map(|&i| part.face_mass(faces[i]).map(|m| m[0]))
.collect();
if !areas.is_some_and(|a| clears_threshold(py::sum(a), threshold)) {
continue;
}
}
let spans: Vec<Bounds> = cluster
.iter()
.map(|&i| part.face_bounds(faces[i]))
.collect();
let fold = |pick: fn(&Bounds) -> f64, max: bool| {
spans.iter().map(pick).reduce(|a, x| {
let better = if max { x > a } else { x < a };
if better { x } else { a }
})
};
let span = |lo, hi| {
fold(lo, false)
.zip(fold(hi, true))
.expect("non-empty cluster")
};
out.push((
FaceLevel {
z: zs[cluster[py::first_min(&cluster, |&i| zs[i])]],
x_span: span(|b| b.min[0], |b| b.max[0]),
y_span: span(|b| b.min[1], |b| b.max[1]),
},
cluster.iter().map(|&i| faces[i]).collect(),
));
}
out
}
pub(crate) fn face_levels_with_faces(
part: &Part,
min_area_frac: f64,
) -> Vec<(FaceLevel, Vec<usize>)> {
let mut out: Vec<(FaceLevel, Vec<usize>)> = scopes(part)
.iter()
.flat_map(|scope| level_proposals(part, scope, TOL, min_area_frac))
.collect();
out.sort_by(|a, b| a.0.order(&b.0));
out
}
fn body_levels(part: &Part, scope: &Scope, tol: f64) -> Vec<FaceLevel> {
let (lo, hi) = (scope.bounds.min[2], scope.bounds.max[2]);
level_proposals(part, scope, TOL, STEP_MIN_AREA_FRAC)
.into_iter()
.filter(|(level, _)| lo + tol < level.z && level.z < hi - tol)
.map(|(level, _)| level)
.collect()
}
pub(crate) fn recognise_risers(part: &Part) -> Vec<RiserEvidence> {
let mut out: Vec<RiserEvidence> = scopes(part)
.iter()
.flat_map(|scope| {
let levels = body_levels(part, scope, TOL);
riser_proposals(part, scope, RISER_MIN_AREA_FRAC, TOL, levels)
})
.collect();
out.sort_by(|a, b| a.order(b));
out
}
fn riser_orientation(n: V3) -> Option<(bool, usize)> {
let on_x = n[0].abs() > AXIS_ZERO_COS && n[1].abs() <= AXIS_ZERO_COS;
let on_y = n[1].abs() > AXIS_ZERO_COS && n[0].abs() <= AXIS_ZERO_COS;
if !(on_x || on_y) {
return None;
}
Some((n[2].abs() <= AXIS_ZERO_COS, if on_x { 0 } else { 1 }))
}
fn riser_proposals(
part: &Part,
scope: &Scope,
min_area_frac: f64,
tol: f64,
body_levels: Vec<FaceLevel>,
) -> Vec<RiserEvidence> {
let bb = &scope.bounds;
let ext = |i: usize| bb.max[i] - bb.min[i];
let mut found: Vec<RiserEvidence> = Vec::new();
for &f in &scope.faces {
let Surface::Plane { frame } = part.faces[f].surface else {
continue;
};
let Some(normal) = part.face_normal(f, 0.0, 0.0) else {
continue;
};
let Some((vertical, axis)) = riser_orientation(normal) else {
continue;
};
let fb = part.face_bounds(f);
let other = 1 - axis;
let (flo, fhi) = (fb.min[other], fb.max[other]);
let full_span = flo <= bb.min[other] + tol && fhi >= bb.max[other] - tol;
let interior = |i: usize, p: f64| {
p - bb.min[i] > tol + COORD_FLOOR && bb.max[i] - p > tol + COORD_FLOOR
};
let height = fb.max[2] - fb.min[2];
let (positions, other_positions) = if vertical {
let pos = frame.origin[axis];
if !full_span || !interior(axis, pos) {
continue;
}
(vec![pos], vec![])
} else {
if tol >= height {
continue;
}
let along = vec![fb.min[axis], fb.max[axis]];
if full_span {
(along, vec![])
} else if fhi - flo < STRUCTURAL_RAMP_MIN_FRAC * ext(other)
|| along[1] - along[0] < STRUCTURAL_RAMP_MIN_FRAC * ext(axis)
|| STRUCTURAL_RAMP_MIN_FRAC * ext(2) > height
{
continue; } else {
(along, vec![flo, fhi])
}
};
let cross = ext(other) * ext(2);
let area_floor = if full_span {
min_area_frac * cross
} else {
BOUNDED_RISER_AREA_FRAC * ((fhi - flo) * height)
};
let Some([area, _]) = part.face_mass(f) else {
continue;
};
if cross <= 0.0 || area < area_floor {
continue;
}
let at_envelope = |z: f64| (z - bb.min[2]).abs() < tol || (z - bb.max[2]).abs() < tol;
let record = RiserEvidence {
faces: vec![f],
vertical,
axis: ['x', 'y'][axis],
positions: positions
.into_iter()
.filter(|&p| interior(axis, p))
.map(py::round_to3)
.collect(),
other_axis: ['x', 'y'][other],
other_positions: other_positions
.into_iter()
.filter(|&p| interior(other, p))
.map(py::round_to3)
.collect(),
z_lo: fb.min[2],
z_hi: fb.max[2],
lo_at_envelope: at_envelope(fb.min[2]),
hi_at_envelope: at_envelope(fb.max[2]),
tol,
body_levels: body_levels.clone(),
};
if let Some(existing) = found.iter_mut().find(|r| r.order(&record).is_eq()) {
existing.faces.push(f);
} else {
found.push(record);
}
}
found.sort_by(|a, b| a.order(b));
found
}
pub(crate) fn inner_wires(part: &Part, face: usize) -> Vec<Vec<usize>> {
let loops = &part.faces[face].loops;
if loops.len() < 2 {
return Vec::new();
}
let Some(outer) = part.outer_loop(face) else {
return Vec::new();
};
loops
.iter()
.enumerate()
.filter(|&(l, lp)| l != outer && !lp.edges.is_empty())
.map(|(_, lp)| lp.edges.iter().map(|&(e, _)| e).collect())
.collect()
}
const POSITION_TOL: f64 = 1e-6;
const DIRECTION_TOL: f64 = 2e-8;
#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) struct ProfileLine {
pub(crate) start: V3,
pub(crate) end: V3,
}
impl ProfileLine {
fn direction(&self) -> V3 {
let length = py::dist(&self.start, &self.end);
[0, 1, 2].map(|i| (self.end[i] - self.start[i]) / length)
}
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) struct ProfileArc {
pub(crate) start: V3,
pub(crate) end: V3,
pub(crate) center: V3,
pub(crate) radius: f64,
pub(crate) sweep: f64,
}
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub(crate) enum ProfileSupport {
Line(ProfileLine),
Arc(ProfileArc),
}
impl ProfileSupport {
fn start(&self) -> V3 {
match self {
ProfileSupport::Line(l) => l.start,
ProfileSupport::Arc(a) => a.start,
}
}
fn end(&self) -> V3 {
match self {
ProfileSupport::Line(l) => l.end,
ProfileSupport::Arc(a) => a.end,
}
}
fn reversed(&self) -> Self {
match self {
ProfileSupport::Line(l) => ProfileSupport::Line(ProfileLine {
start: l.end,
end: l.start,
}),
ProfileSupport::Arc(a) => ProfileSupport::Arc(ProfileArc {
start: a.end,
end: a.start,
sweep: -a.sweep,
..a.clone()
}),
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) struct PlanarOuterProfile {
pub(crate) origin: V3,
pub(crate) normal: V3,
pub(crate) supports: Vec<ProfileSupport>,
pub(crate) inner_loop_count: usize,
pub(crate) schema_version: u8,
pub(crate) boundary_kind: &'static str,
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct PlanarOuterProfileEvidence {
pub(crate) profile: PlanarOuterProfile,
pub(crate) body_faces: Vec<usize>,
pub(crate) edges: Vec<usize>,
}
fn common_valid_solid(part: &Part, faces: &[usize]) -> Option<usize> {
let owner = part.faces.get(*faces.first()?)?.solid?;
(part.solid_is_geometrically_valid(owner)
&& faces.iter().all(|&f| {
part.faces
.get(f)
.is_some_and(|face| face.solid == Some(owner))
}))
.then_some(owner)
}
fn tangent(support: &ProfileSupport, normal: V3, end: bool) -> Option<V3> {
match support {
ProfileSupport::Line(l) => Some(l.direction()),
ProfileSupport::Arc(a) => {
let radius = sub(if end { a.end } else { a.start }, a.center);
let t = cross(normal, radius);
let length = py::hypot(&t);
if length == 0.0 {
return None;
}
let sign = 1.0f64.copysign(a.sweep);
Some(t.map(|v| v * sign / length))
}
}
}
fn turns(supports: &[ProfileSupport], normal: V3) -> Option<Vec<f64>> {
let n = supports.len();
let curvature = |s: &ProfileSupport| match s {
ProfileSupport::Line(_) => 0.0,
ProfileSupport::Arc(a) => 1.0f64.copysign(a.sweep) / a.radius,
};
(0..n)
.map(|at| {
let (first, second) = (&supports[at], &supports[(at + 1) % n]);
let before = tangent(first, normal, true)?;
let after = tangent(second, normal, false)?;
let turn = py::dot(&cross(before, after), &normal).atan2(py::dot(&before, &after));
let (k1, k2) = (curvature(first), curvature(second));
let sum = k1 + k2;
Some(
if turn.abs() > std::f64::consts::PI - DIRECTION_TOL
&& sum.abs() > 1e-9 * (k1.abs() + k2.abs())
{
-std::f64::consts::PI.copysign(sum)
} else {
turn
},
)
})
.collect()
}
fn winding(turns: &[f64], supports: &[ProfileSupport]) -> f64 {
py::fsum(turns.iter().copied())
+ py::fsum(supports.iter().filter_map(|s| match s {
ProfileSupport::Arc(a) => Some(a.sweep),
ProfileSupport::Line(_) => None,
}))
}
fn finite(p: V3) -> bool {
p.iter().all(|v| v.is_finite())
}
fn line(start: V3, end: V3) -> Option<ProfileLine> {
let length = py::dist(&start, &end);
(finite(start) && finite(end) && length.is_finite() && length != 0.0)
.then_some(ProfileLine { start, end })
}
fn arc(start: V3, end: V3, center: V3, radius: f64, sweep: f64) -> Option<ProfileArc> {
(finite(start)
&& finite(end)
&& finite(center)
&& radius.is_finite()
&& radius > 0.0
&& sweep.is_finite()
&& 0.0 < sweep.abs()
&& sweep.abs() < std::f64::consts::TAU)
.then_some(ProfileArc {
start,
end,
center,
radius,
sweep,
})
}
fn profile(
origin: V3,
normal: V3,
supports: Vec<ProfileSupport>,
inner_loop_count: usize,
) -> Option<PlanarOuterProfile> {
if !finite(origin) || !finite(normal) || (py::hypot(&normal) - 1.0).abs() > 1e-8 {
return None;
}
let n = supports.len();
if n < 2 {
return None;
}
let off_plane = |p: V3| py::dot(&sub(p, origin), &normal).abs() > 1e-6;
for (at, support) in supports.iter().enumerate() {
if support.end() != supports[(at + 1) % n].start() {
return None;
}
if off_plane(support.start()) || off_plane(support.end()) {
return None;
}
if let ProfileSupport::Arc(a) = support {
if off_plane(a.center)
|| [a.start, a.end]
.iter()
.any(|p| (py::dist(p, &a.center) - a.radius).abs() > 1e-6)
{
return None;
}
let radial = sub(a.start, a.center);
let crossed = cross(normal, radial);
let reconstructed = [0, 1, 2]
.map(|i| a.center[i] + a.sweep.cos() * radial[i] + a.sweep.sin() * crossed[i]);
if py::dist(&reconstructed, &a.end) > 1e-6 {
return None;
}
}
}
let turns = turns(&supports, normal)?;
if (winding(&turns, &supports) - std::f64::consts::TAU).abs() > 2e-8 {
return None;
}
let lines = supports
.iter()
.filter(|s| matches!(s, ProfileSupport::Line(_)))
.count();
let schema_version = if turns.iter().any(|&t| t < -2e-8) || lines < 2 {
2
} else {
1
};
Some(PlanarOuterProfile {
origin,
normal,
supports,
inner_loop_count,
schema_version,
boundary_kind: "outer",
})
}
fn read_profile(part: &Part, face: usize) -> Option<(PlanarOuterProfile, Vec<usize>)> {
let f = &part.faces[face];
if !matches!(f.surface, Surface::Plane { .. }) {
return None;
}
let outer = part.outer_loop(face)?;
let inner_loop_count = f.loops.len() - 1;
let wire = &f.loops[outer].edges;
if wire.is_empty() || (1..wire.len()).any(|i| wire[..i].iter().any(|w| w.0 == wire[i].0)) {
return None;
}
if wire.iter().any(|&(e, _)| {
part.unresolved_edges().contains(&e)
|| !matches!(
part.edges[e].curve,
Curve::Line { .. } | Curve::Circle { .. }
)
}) {
return None;
}
if wire.len() < 2 {
return None;
}
let normal = part.face_normal(face, 0.0, 0.0)?;
let first = |&(e, forward): &(usize, bool)| {
let edge = &part.edges[e];
if forward {
(edge.vertices.0, edge.start)
} else {
(edge.vertices.1, edge.end)
}
};
let last = |&(e, forward): &(usize, bool)| {
let edge = &part.edges[e];
if forward {
edge.vertices.1
} else {
edge.vertices.0
}
};
let points: Vec<V3> = wire.iter().map(|w| first(w).1).collect();
let origin = points[0];
let mut supports = Vec::with_capacity(wire.len());
for (at, w) in wire.iter().enumerate() {
let following = (at + 1) % wire.len();
if last(w) != first(&wire[following]).0 {
return None;
}
let (start, end) = (points[at], points[following]);
if py::dist(&start, &end) == 0.0 {
return None;
}
let edge = &part.edges[w.0];
let (a, b) = edge_interval(
&edge.curve,
edge.start,
edge.end,
edge.same_sense,
edge.is_closed(),
);
let curve_ends = (edge.curve.value(a), edge.curve.value(b));
let apart = |(p, q): (V3, V3)| py::dist(&start, &p).max(py::dist(&end, &q));
if apart(curve_ends).min(apart((curve_ends.1, curve_ends.0))) > POSITION_TOL {
return None;
}
let midpoint = edge.curve.value(0.5 * (a + b));
if [start, end, midpoint]
.iter()
.any(|p| py::dot(&sub(*p, origin), &normal).abs() > POSITION_TOL)
{
return None;
}
match &edge.curve {
Curve::Line { .. } => {
let support = line(start, end)?;
let source = line(curve_ends.0, curve_ends.1)?.direction();
let d = support.direction();
if py::dist(&d, &source).min(py::dist(&d, &source.map(|v| -v))) > DIRECTION_TOL {
return None;
}
supports.push(ProfileSupport::Line(support));
}
Curve::Circle { frame, radius } => {
let center = frame.origin;
let sense = py::dot(&cross(sub(start, center), sub(midpoint, center)), &normal);
if sense == 0.0 {
return None;
}
let length = radius * (b - a).abs();
let sweep = (length / radius).copysign(sense);
supports.push(ProfileSupport::Arc(arc(
start, end, center, *radius, sweep,
)?));
}
_ => return None,
}
}
let mut edges: Vec<usize> = wire.iter().map(|w| w.0).collect();
let turns = turns(&supports, normal)?;
let mut winding = winding(&turns, &supports);
if winding < 0.0 {
supports = supports
.iter()
.rev()
.map(ProfileSupport::reversed)
.collect();
edges.reverse();
winding = -winding;
}
if (winding - std::f64::consts::TAU).abs() > DIRECTION_TOL {
return None;
}
let starts: Vec<V3> = supports.iter().map(ProfileSupport::start).collect();
let mut least = 0;
for at in 1..starts.len() {
if py::tuple_order(&starts[at], &starts[least]).is_lt() {
least = at;
}
}
supports.rotate_left(least);
edges.rotate_left(least);
let profile = profile(supports[0].start(), normal, supports, inner_loop_count)?;
Some((profile, edges))
}
pub(crate) fn planar_outer_profile(part: &Part, face: usize) -> Option<PlanarOuterProfileEvidence> {
let owner = common_valid_solid(part, &[face])?;
let mut body_faces = part.solids[owner].faces.clone();
if common_valid_solid(part, &body_faces) != Some(owner) {
return None;
}
body_faces.sort_unstable();
body_faces.dedup();
let (profile, edges) = read_profile(part, face)?;
Some(PlanarOuterProfileEvidence {
profile,
body_faces,
edges,
})
}