use super::debug_enabled;
use crate::analytic_surface::AnalyticSurface;
use crate::topology::{BrepSolid, FaceRecord};
use crate::{boolean_operation, classify_point, make_arc, make_line, revolve_profile_brep, BooleanOperation, BooleanOptions, PointClass, Vec3};
use std::f64::consts::PI;
pub(super) fn bore_dimples_enabled() -> bool {
switch!("BREP_OFFSET_BORE_DIMPLES", true)
}
struct Bore {
origin: Vec3,
axis: Vec3,
radius: f64,
span: [f64; 2],
open: [bool; 2],
faces: Vec<u64>,
}
fn cylinder_of(surface: &crate::NurbsSurface) -> Option<(Vec3, Vec3, f64)> {
match surface.analytic()? {
AnalyticSurface::RuledRevolution { frame, rho0, rho1, .. } => {
((rho0 - rho1).abs() <= 1e-9 * rho0.abs().max(1.0)).then_some((frame.origin, frame.axis, *rho0))
}
AnalyticSurface::Revolution { frame, generatrix, .. } => {
let mut radius: Option<f64> = None;
for control in &generatrix.control_points {
let p = control.point().ok()?;
let d = p.sub(frame.origin);
let rho = d.sub(frame.axis.scale(d.dot(frame.axis))).length();
match radius {
None => radius = Some(rho),
Some(r) if (r - rho).abs() <= 1e-9 * r.max(1.0) => {}
_ => return None,
}
}
radius.map(|r| (frame.origin, frame.axis, r))
}
_ => None,
}
}
fn classify(solid: &BrepSolid, point: Vec3, scale: f64) -> Option<PointClass> {
classify_point(point, solid, 1e-9 * scale).ok().map(|c| c.class)
}
fn across(axis: Vec3) -> (Vec3, Vec3) {
let seed = if axis.x.abs() < 0.9 { Vec3::new(1.0, 0.0, 0.0) } else { Vec3::new(0.0, 1.0, 0.0) };
let first = seed.sub(axis.scale(seed.dot(axis)));
let first = first.scale(1.0 / first.length());
(first, axis.cross(first))
}
fn bores(source: &BrepSolid, scale: f64, keep: impl Fn(f64) -> bool) -> Vec<Bore> {
let mut found: Vec<Bore> = Vec::new();
for face in source.shells.iter().flat_map(|shell| &shell.faces) {
let Some((origin, axis, radius)) = cylinder_of(&face.surface) else { continue };
if !(radius > 0.0 && keep(radius)) {
continue;
}
let axial = |p: Vec3| p.sub(origin).dot(axis);
let mut span = [f64::INFINITY, f64::NEG_INFINITY];
for edge in face_edges(source, face) {
for id in [edge.start_vertex_id, edge.end_vertex_id] {
if let Some(vertex) = source.vertices.iter().find(|vertex| vertex.id == id) {
span = [span[0].min(axial(vertex.point)), span[1].max(axial(vertex.point))];
}
}
}
if !(span[1] > span[0]) {
continue;
}
let same = found.iter_mut().find(|bore| {
let off = origin.sub(bore.origin);
bore.axis.cross(axis).length() <= 1e-9
&& off.sub(bore.axis.scale(off.dot(bore.axis))).length() <= 1e-9 * scale
&& (bore.radius - radius).abs() <= 1e-9 * scale
});
match same {
Some(bore) => {
let shift = origin.sub(bore.origin).dot(bore.axis);
let sign = bore.axis.dot(axis).signum();
let (a, b) = (shift + sign * span[0], shift + sign * span[1]);
bore.span = [bore.span[0].min(a.min(b)), bore.span[1].max(a.max(b))];
bore.faces.push(face.id);
}
None => found.push(Bore { origin, axis, radius, span, open: [false; 2], faces: vec![face.id] }),
}
}
found.retain_mut(|bore| {
let (x, y) = across(bore.axis);
let at = |t: f64| bore.origin.add(bore.axis.scale(t));
let middle = 0.5 * (bore.span[0] + bore.span[1]);
let length = bore.span[1] - bore.span[0];
let eps = 1e-3 * length.min(bore.radius);
let out = |p: Vec3| classify(source, p, scale) == Some(PointClass::Out);
let inside = |p: Vec3| classify(source, p, scale) == Some(PointClass::In);
let (low, high) = (at(bore.span[0] - eps), at(bore.span[1] + eps));
bore.open = [out(low), out(high)];
out(at(middle))
&& (bore.open[0] || inside(low))
&& (bore.open[1] || inside(high))
&& (bore.open[0] || bore.open[1])
&& [x, y, x.scale(-1.0), y.scale(-1.0)].iter().all(|r| inside(at(middle).add(r.scale(bore.radius + eps))))
});
found
}
fn face_edges<'a>(source: &'a BrepSolid, face: &FaceRecord) -> Vec<&'a crate::topology::EdgeRecord> {
face.loops
.iter()
.flat_map(|lp| &lp.coedges)
.filter_map(|coedge| source.edges.iter().find(|edge| edge.id == coedge.edge_id))
.collect()
}
fn flat_end(source: &BrepSolid, bore: &Bore, t: f64, out: Vec3, distance: f64, scale: f64) -> bool {
let (x, y) = across(bore.axis);
let centre = bore.origin.add(bore.axis.scale(t));
let eps = 1e-4 * bore.radius.min(distance);
for k in 1..=2 {
let rho = bore.radius + 0.25 * bore.radius.min(distance) * k as f64 / 2.0;
for direction in [x, y, x.scale(-1.0), y.scale(-1.0), x.add(y).scale(0.5f64.sqrt()), x.sub(y).scale(0.5f64.sqrt())] {
let foot = centre.add(direction.scale(rho));
if classify(source, foot.sub(out.scale(eps)), scale) != Some(PointClass::In)
|| classify(source, foot.add(out.scale(eps)), scale) != Some(PointClass::Out)
{
return false;
}
}
}
true
}
fn clear_of_others(source: &BrepSolid, bore: &Bore, t: f64, out: Vec3, distance: f64, scale: f64) -> bool {
reach_of_others(source, bore, t, out, distance, scale, false)
}
fn reach_of_others(source: &BrepSolid, bore: &Bore, t: f64, out: Vec3, distance: f64, scale: f64, beside: bool) -> bool {
let (x, y) = across(bore.axis);
let centre = bore.origin.add(bore.axis.scale(t));
let gap = |p: Vec3| {
let d = p.sub(centre);
let (a, b, z) = (d.dot(x), d.dot(y), d.dot(out));
let radial = ((a * a + b * b).sqrt() - bore.radius).max(0.0);
let axial = (-z).max(z - distance).max(0.0);
radial.hypot(axial)
};
let enough = distance * (1.0 - 1e-9);
let on_rim = |edge: &crate::topology::EdgeRecord| {
let Ok([t0, t1]) = edge.curve.domain() else { return false };
(0..=16).all(|k| {
edge.curve.evaluate(t0 + (t1 - t0) * k as f64 / 16.0).is_ok_and(|p| {
let d = p.sub(centre);
let (a, b, z) = (d.dot(x), d.dot(y), d.dot(out));
z.abs() <= 1e-9 * scale && ((a * a + b * b).sqrt() - bore.radius).abs() <= 1e-9 * scale
})
})
};
let edge_clear = |edge: &crate::topology::EdgeRecord| -> bool {
let Ok([t0, t1]) = edge.curve.domain() else { return false };
let n = 256;
let step = (t1 - t0) / n as f64;
let read = |t: f64| edge.curve.evaluate(t.clamp(t0, t1)).map_or(f64::NEG_INFINITY, gap);
let points: Vec<Vec3> = (0..=n).filter_map(|k| edge.curve.evaluate(t0 + step * k as f64).ok()).collect();
if points.len() != n + 1 {
return false;
}
let spacing = points.windows(2).map(|pair| pair[1].sub(pair[0]).length()).fold(0.0f64, f64::max);
(0..=n).all(|k| {
let here = gap(points[k]);
if here - spacing >= enough {
return true;
}
let (mut a, mut b) = (t0 + step * (k as f64 - 1.0), t0 + step * (k as f64 + 1.0));
let ratio = 0.5 * (5f64.sqrt() - 1.0);
let mut best = here;
for _ in 0..80 {
let (c, d) = (b - ratio * (b - a), a + ratio * (b - a));
let (fc, fd) = (read(c), read(d));
best = best.min(fc).min(fd);
if fc < fd { b = d } else { a = c }
}
best >= enough
})
};
let surface_clear = |surface: &crate::NurbsSurface| -> bool {
let (Ok([u0, u1]), Ok([v0, v1])) = (surface.domain_u(), surface.domain_v()) else { return false };
let n = 64;
let (du, dv) = ((u1 - u0) / n as f64, (v1 - v0) / n as f64);
let read = |u: f64, v: f64| surface.evaluate(u.clamp(u0, u1), v.clamp(v0, v1)).map_or(f64::NEG_INFINITY, gap);
let mut grid = Vec::with_capacity((n + 1) * (n + 1));
for i in 0..=n {
for j in 0..=n {
match surface.evaluate(u0 + du * i as f64, v0 + dv * j as f64) {
Ok(p) => grid.push(p),
Err(_) => return false,
}
}
}
let at = |i: usize, j: usize| grid[i * (n + 1) + j];
let mut spacing = 0.0f64;
for i in 0..n {
for j in 0..n {
spacing = spacing.max(at(i, j).sub(at(i + 1, j + 1)).length()).max(at(i + 1, j).sub(at(i, j + 1)).length());
}
}
(0..=n).all(|i| {
(0..=n).all(|j| {
let here = gap(at(i, j));
if here - spacing >= enough {
return true;
}
let (mut u, mut v, mut best) = (u0 + du * i as f64, v0 + dv * j as f64, here);
let (mut su, mut sv) = (du, dv);
while su > 1e-12 * du {
let mut moved = false;
for (a, b) in [(su, 0.0), (-su, 0.0), (0.0, sv), (0.0, -sv)] {
let value = read(u + a, v + b);
if value < best {
best = value;
u = (u + a).clamp(u0, u1);
v = (v + b).clamp(v0, v1);
moved = true;
}
}
if !moved {
su *= 0.5;
sv *= 0.5;
}
}
best >= enough
})
})
};
let height = |p: Vec3| p.sub(centre).dot(out);
let in_plane_segment = |edge: &crate::topology::EdgeRecord| -> Option<(Vec3, Vec3)> {
edge.curve.straight_segment(1e-9 * scale)?;
let vertex = |id: u64| source.vertices.iter().find(|v| v.id == id).map(|v| v.point);
let segment = (vertex(edge.start_vertex_id)?, vertex(edge.end_vertex_id)?);
let middle = edge.curve.evaluate(0.5 * (edge.t0 + edge.t1)).ok()?;
let on_chord = {
let (a, b) = segment;
let along = b.sub(a);
let length = along.length();
length > 0.0 && middle.sub(a).sub(along.scale(middle.sub(a).dot(along) / (length * length))).length() <= 1e-9 * scale
};
(on_chord && [segment.0, segment.1, middle].iter().all(|p| height(*p).abs() <= 1e-9 * scale)).then_some(segment)
};
for face in source.shells.iter().flat_map(|shell| &shell.faces) {
if bore.faces.contains(&face.id) {
continue;
}
let edges = face_edges(source, face);
let planar = matches!(face.surface.analytic(), Some(AnalyticSurface::Plane { .. }));
let owns_rim = planar && edges.iter().any(|edge| on_rim(edge));
if owns_rim {
for edge in edges.iter().filter(|edge| !on_rim(edge)) {
if edge_clear(edge) {
continue;
}
if !(beside && in_plane_segment(edge).is_some()) {
os_debug!("bore dimples: edge {} of the end face stands within {distance} of the dimple at {t}", edge.id);
return false;
}
}
continue;
}
if surface_clear(&face.surface) {
continue;
}
let side = beside && planar && {
let corners: Vec<Vec3> = edges.iter().flat_map(|edge| [edge.start_vertex_id, edge.end_vertex_id]).filter_map(|id| source.vertices.iter().find(|v| v.id == id)).map(|v| v.point).collect();
let normal = match (face.surface.domain_u(), face.surface.domain_v()) {
(Ok([u0, u1]), Ok([v0, v1])) => face.surface.normal(0.5 * (u0 + u1), 0.5 * (v0 + v1)).ok(),
_ => None,
};
let square = normal.is_some_and(|n| n.dot(out).abs() <= 1e-9);
let below = corners.iter().all(|p| height(*p) <= 1e-9 * scale);
let top: Vec<(Vec3, Vec3)> = edges.iter().filter_map(|edge| in_plane_segment(edge)).collect();
let within = top.len() == 1 && {
let (a, b) = top[0];
let along = b.sub(a);
let length = along.length();
length > 0.0 && corners.iter().all(|p| {
let s = p.sub(a).dot(along) / (length * length);
(-1e-9..=1.0 + 1e-9).contains(&s)
})
};
let straight = edges.iter().all(|edge| edge.curve.straight_segment(1e-9 * scale).is_some());
square && below && within && straight
};
if !side {
os_debug!("bore dimples: face {} stands within {distance} of the dimple at {t}", face.id);
return false;
}
}
true
}
fn dimple(centre: Vec3, out: Vec3, r: f64, d: f64) -> Result<BrepSolid, String> {
let (x, _) = across(out);
let at = |rho: f64, z: f64| centre.add(x.scale(rho)).add(out.scale(z));
let low = (d * d - r * r).max(0.0).sqrt();
let on_axis = make_line(at(0.0, low), at(0.0, d))?;
let lid = make_line(at(0.0, d), at(r, d))?;
let end = PI - (r / d).min(1.0).acos();
let arc = make_arc(at(r, 0.0), x, out, d, 0.5 * PI, end)?;
revolve_profile_brep(&[on_axis, lid, arc], centre, out, 2.0 * PI)
}
pub(super) fn carve_bore_dimples(source: &BrepSolid, grown: &BrepSolid, distance: f64) -> Option<BrepSolid> {
if !bore_dimples_enabled() || !(distance > 0.0) {
return None;
}
let scale = crate::solid_scale(source).max(1.0);
let mut voids = Vec::new();
let found = bores(source, scale, |radius| radius <= distance * (1.0 + 1e-9));
os_debug!("bore dimples: {} bore(s) of radius at most {distance}", found.len());
for bore in found {
for (t, out, open) in [(bore.span[1], bore.axis, bore.open[1]), (bore.span[0], bore.axis.scale(-1.0), bore.open[0])] {
if !open {
continue;
}
if !flat_end(source, &bore, t, out, distance, scale) {
os_debug!("bore dimples: the end at {t} is not flat at its rim");
return None;
}
if !reach_of_others(source, &bore, t, out, distance, scale, true) {
os_debug!("bore dimples: a bore of radius {} declined at its end {t}", bore.radius);
return None;
}
let centre = bore.origin.add(bore.axis.scale(t));
let low = (distance * distance - bore.radius * bore.radius).max(0.0).sqrt();
let probe = centre.add(out.scale(0.5 * (low + distance)));
if classify(grown, probe, scale) != Some(PointClass::In) {
os_debug!("bore dimples: the growth is already carved at {probe:?}");
return None;
}
let void = dimple(centre, out, bore.radius, distance).ok()?;
voids.push(void);
}
}
if voids.is_empty() {
return None;
}
let mut carved = grown.clone();
for void in &voids {
carved = match boolean_operation(&carved, void, BooleanOperation::Subtract, &BooleanOptions::default()) {
Ok(next) => next,
Err(error) => {
os_debug!("bore dimples: the carve declined: {}", error.message);
return None;
}
};
}
carved.validate().is_empty().then_some(carved)
}
pub(super) struct WideRim {
pub centre: Vec3,
pub out: Vec3,
pub radius: f64,
}
impl WideRim {
pub fn holds(&self, p: Vec3, distance: f64, tolerance: f64) -> bool {
let rel = p.sub(self.centre);
let along = rel.dot(self.out);
let rho = rel.sub(self.out.scale(along)).length();
(along - distance).abs() <= tolerance && (rho - (self.radius - distance)).abs() <= tolerance
}
}
pub(super) fn wide_bore_rims(source: &BrepSolid, distance: f64) -> Vec<WideRim> {
if !bore_dimples_enabled() || !(distance > 0.0) {
return Vec::new();
}
let scale = crate::solid_scale(source).max(1.0);
let mut rims = Vec::new();
for bore in bores(source, scale, |radius| radius > distance * (1.0 + 1e-9)) {
for (t, out, open) in [(bore.span[1], bore.axis, bore.open[1]), (bore.span[0], bore.axis.scale(-1.0), bore.open[0])] {
if !open || !flat_end(source, &bore, t, out, distance, scale) || !clear_of_others(source, &bore, t, out, distance, scale) {
os_debug!("bore cusps: the end at {t} of a bore of radius {} does not qualify", bore.radius);
continue;
}
rims.push(WideRim { centre: bore.origin.add(bore.axis.scale(t)), out, radius: bore.radius });
}
}
os_debug!("bore cusps: {} rim(s) of bores wider than {distance}", rims.len());
rims
}
fn cusp(rim: &WideRim, d: f64) -> Result<BrepSolid, String> {
let (x, _) = across(rim.out);
let at = |rho: f64, z: f64| rim.centre.add(x.scale(rho)).add(rim.out.scale(z));
let r = rim.radius;
let on_axis = make_line(at(0.0, 0.0), at(0.0, d))?;
let lid = make_line(at(0.0, d), at(r, d))?;
let arc = make_arc(at(r, 0.0), x, rim.out, d, 0.5 * PI, PI)?;
let floor = make_line(at(r - d, 0.0), at(0.0, 0.0))?;
revolve_profile_brep(&[on_axis, lid, arc, floor], rim.centre, rim.out, 2.0 * PI)
}
pub(super) fn carve_wide_rims(body: &BrepSolid, rims: &[WideRim], distance: f64) -> Result<BrepSolid, String> {
let scale = crate::solid_scale(body).max(1.0);
let mut carved = body.clone();
for rim in rims {
let (x, _) = across(rim.out);
let probe = rim.centre.add(x.scale(rim.radius - 0.5 * distance)).add(rim.out.scale(0.95 * distance));
if classify(&carved, probe, scale) != Some(PointClass::In) {
return Err(format!("the cusp at the rim about {:?} is not material in the growth", rim.centre));
}
let tool = cusp(rim, distance)?;
carved = boolean_operation(&carved, &tool, BooleanOperation::Subtract, &BooleanOptions::default()).map_err(|error| format!("the cusp carve declined: {}", error.message))?;
}
let issues = carved.validate();
if !issues.is_empty() {
return Err(format!("the carved growth is not valid: {issues:?}"));
}
Ok(carved)
}