use super::*;
use std::borrow::Cow;
use crate::classification::classify_point;
pub(super) fn ball_align_enabled() -> bool {
switch!("BREP_BALL_ALIGN", true)
}
fn whole_ball(solid: &BrepSolid) -> Option<(Vec3, f64, Vec3)> {
let [shell] = solid.shells.as_slice() else { return None };
let [face] = shell.faces.as_slice() else { return None };
let Some(crate::AnalyticSurface::Sphere { frame, radius }) = face.surface.analytic() else { return None };
let [lp] = face.loops.as_slice() else { return None };
if lp.coedges.len() > 4 || solid.edges.len() > 3 || solid.vertices.len() > 2 || !face.same_sense {
return None;
}
let seam_or_pole = solid.edges.iter().all(|edge| {
edge.degenerate || edge.curve.evaluate(0.5 * (edge.t0 + edge.t1)).is_ok_and(|point| {
let offset = point.sub(frame.origin);
(offset.length() - radius).abs() <= 1e-9 * radius.max(1.0) && offset.dot(frame.y_axis).abs() <= 1e-9 * radius.max(1.0)
})
});
seam_or_pole.then_some((frame.origin, *radius, frame.axis))
}
fn cutting_line(centre: Vec3, radius: f64, other: &BrepSolid, tolerance: f64) -> Option<Vec3> {
let near = 1e-9 * radius.max(1.0);
const CLEAR: f64 = 0.05;
let clear = |circle: f64| circle > CLEAR * radius && circle < (1.0 - CLEAR) * radius;
let sphere_cut = |other_centre: Vec3, other_radius: f64| -> Option<Vec3> {
let offset = other_centre.sub(centre);
let distance = offset.length();
if distance <= near || distance >= radius + other_radius || distance <= (radius - other_radius).abs() {
return None;
}
let along = (distance * distance + radius * radius - other_radius * other_radius) / (2.0 * distance);
let circle = (radius * radius - along * along).max(0.0).sqrt();
clear(circle).then(|| offset.normalized().ok()).flatten()
};
if let Some((other_centre, other_radius, _)) = whole_ball(other) {
return sphere_cut(other_centre, other_radius);
}
let ball = crate::spatial::Aabb {
minimum: centre.sub(Vec3::new(radius, radius, radius)),
maximum: centre.add(Vec3::new(radius, radius, radius)),
};
let mut line: Option<Vec3> = None;
let mut planes: Vec<Vec3> = Vec::new();
let parallel = |a: Vec3, b: Vec3| a.cross(b).length() <= 1e-12;
for face in other.shells.iter().flat_map(|shell| &shell.faces) {
let Ok(bounds) = crate::Aabb::from_surface_controls(&face.surface) else { return None };
if !bounds.intersects(ball, tolerance) {
continue;
}
match face.surface.analytic()? {
crate::AnalyticSurface::Plane { origin, u_dir, v_dir, .. } => {
let normal = u_dir.cross(*v_dir).normalized().ok()?;
let height = origin.sub(centre).dot(normal);
if height.abs() < radius && !clear((radius * radius - height * height).sqrt()) {
return None;
}
planes.push(normal);
}
crate::AnalyticSurface::Sphere { frame, radius: other_radius } => {
let through = sphere_cut(frame.origin, *other_radius)?;
match line {
Some(seen) if !parallel(seen, through) => return None,
_ => line = Some(through),
}
}
crate::AnalyticSurface::RuledRevolution { frame, rho0, rho1, .. } => {
let offset = centre.sub(frame.origin);
if offset.sub(frame.axis.scale(offset.dot(frame.axis))).length() > near {
return None;
}
if !(clear(rho0.abs()) || rho0.abs() >= radius) || !(clear(rho1.abs()) || rho1.abs() >= radius) {
return None;
}
if (rho0 - rho1).abs() <= near && !clear(rho0.abs()) {
return None;
}
match line {
Some(seen) if !parallel(seen, frame.axis) => return None,
_ => line = Some(frame.axis),
}
}
_ => return None,
}
}
match line {
Some(axis) => planes.iter().all(|&normal| parallel(normal, axis)).then_some(axis),
None => {
let first = *planes.first()?;
planes.iter().all(|&normal| parallel(normal, first)).then_some(first)
}
}
}
fn oriented(centre: Vec3, radius: f64, line: Vec3, other: &BrepSolid, removed_inside: bool, tolerance: f64) -> Vec3 {
let wanted = if removed_inside { PointClass::In } else { PointClass::Out };
let class = |direction: Vec3| classify_point(centre.add(direction.scale(radius)), other, tolerance).ok().map(|read| read.class);
if class(line) == Some(wanted) {
line
} else if class(line.scale(-1.0)) == Some(wanted) {
line.scale(-1.0)
} else {
line
}
}
fn seam_hidden(solid: &BrepSolid, other: &BrepSolid, removed_inside: bool, tolerance: f64) -> bool {
let removed = |point: Vec3| match classify_point(point, other, tolerance).map(|read| read.class) {
Ok(PointClass::In) => removed_inside,
Ok(PointClass::Out) => !removed_inside,
Ok(_) => true,
Err(_) => false,
};
solid.edges.iter().all(|edge| {
(0..=8).all(|step| edge.curve.evaluate(edge.t0 + (edge.t1 - edge.t0) * step as f64 / 8.0).is_ok_and(removed))
})
}
fn aligned(solid: &BrepSolid, other: &BrepSolid, removed_inside: bool, tolerance: f64) -> Option<BrepSolid> {
let (centre, radius, axis) = whole_ball(solid)?;
let line = cutting_line(centre, radius, other, tolerance)?;
if axis.cross(line).length() <= 1e-12 {
return None;
}
if seam_hidden(solid, other, removed_inside, tolerance) {
return None;
}
let line = oriented(centre, radius, line, other, removed_inside, tolerance);
turned_onto(solid, centre, radius, line)
}
fn turned_onto(solid: &BrepSolid, centre: Vec3, radius: f64, line: Vec3) -> Option<BrepSolid> {
let canonical = if line.x.abs() > 1e-12 { line.scale(line.x.signum()) } else if line.y.abs() > 1e-12 { line.scale(line.y.signum()) } else { line.scale(line.z.signum()) };
let seam = canonical.perpendicular().ok()?;
let mut turned = crate::make_sphere_brep_framed(centre, radius, line, Some(seam)).ok()?;
let (old, new) = (&solid.shells[0].faces[0], &mut turned.shells[0].faces[0]);
if new.same_sense != old.same_sense {
return None;
}
new.id = old.id;
new.name = old.name.clone();
turned.shells[0].id = solid.shells[0].id;
turned.id = solid.id;
turned.kind = solid.kind;
Some(turned)
}
pub(super) fn align_balls<'a>(
first: &'a BrepSolid,
second: &'a BrepSolid,
operation: BooleanOperation,
tolerance: f64,
) -> (Cow<'a, BrepSolid>, Cow<'a, BrepSolid>) {
if !ball_align_enabled() {
return (Cow::Borrowed(first), Cow::Borrowed(second));
}
let (first_inside, second_inside) = match operation {
BooleanOperation::Union => (true, true),
BooleanOperation::Subtract => (true, false),
BooleanOperation::Intersect => (false, false),
};
let a = aligned(first, second, first_inside, tolerance).map_or(Cow::Borrowed(first), Cow::Owned);
let b = aligned(second, first, second_inside, tolerance).map_or(Cow::Borrowed(second), Cow::Owned);
(a, b)
}
pub(super) fn align_balls_nary(operands: &[BrepSolid], operation: BooleanOperation, tolerance: f64) -> Option<Vec<BrepSolid>> {
if !ball_align_enabled() || operands.len() < 2 {
return None;
}
let mut changed = false;
let mut out = operands.to_vec();
for (index, operand) in operands.iter().enumerate() {
let Some((centre, radius, axis)) = whole_ball(operand) else { continue };
let ball = crate::spatial::Aabb {
minimum: centre.sub(Vec3::new(radius, radius, radius)),
maximum: centre.add(Vec3::new(radius, radius, radius)),
};
let mut line: Option<Vec3> = None;
let mut cutter: Option<usize> = None;
let mut single = true;
for (other_index, other) in operands.iter().enumerate() {
if other_index == index {
continue;
}
let reaches = other.shells.iter().flat_map(|shell| &shell.faces).any(|face| {
crate::Aabb::from_surface_controls(&face.surface).is_ok_and(|bounds| bounds.intersects(ball, tolerance))
});
if !reaches {
continue;
}
match cutting_line(centre, radius, other, tolerance) {
Some(through) if line.is_none_or(|seen| seen.cross(through).length() <= 1e-12) => {
line = Some(through);
cutter.get_or_insert(other_index);
}
_ => {
single = false;
break;
}
}
}
let (true, Some(line)) = (single, line) else { continue };
if axis.cross(line).length() <= 1e-12 {
continue;
}
let (reference, removed_inside) = match operation {
BooleanOperation::Union => (cutter, true),
BooleanOperation::Subtract if index == 0 => (cutter, true),
BooleanOperation::Subtract => (Some(0), false),
BooleanOperation::Intersect => (cutter, false),
};
if reference.is_some_and(|reference| seam_hidden(operand, &operands[reference], removed_inside, tolerance)) {
continue;
}
let line = match reference {
Some(reference) => oriented(centre, radius, line, &operands[reference], removed_inside, tolerance),
None => line,
};
if let Some(turned) = turned_onto(operand, centre, radius, line) {
out[index] = turned;
changed = true;
}
}
changed.then_some(out)
}