use super::prism_erosion::{append_disjoint, eroded_loops, Piece, P2};
use super::revolved_parallel::half_section_polygon;
use super::*;
use crate::{make_arc, make_line, revolve_profile_brep};
pub(super) fn revolved_erosion_on() -> bool {
switch!("BREP_OFFSET_REVOLVED_EROSION", true)
}
pub(super) fn revolved_erosion(source: &BrepSolid, distance: f64, only_new_topology: bool) -> Option<OffsetShellResultRecord> {
if !revolved_erosion_on() || !(distance < 0.0) || !distance.is_finite() {
return None;
}
match revolved_erosion_impl(source, -distance, only_new_topology) {
Ok(result) => result,
Err(reason) => {
os_debug!("revolved erosion declined: {reason}");
None
}
}
}
fn revolved_erosion_impl(source: &BrepSolid, depth: f64, only_new_topology: bool) -> Result<Option<OffsetShellResultRecord>, String> {
let scale = crate::solid_scale(source).max(1.0);
let tolerance = 1e-9 * scale;
let section = half_section_polygon(source).ok_or("not a solid of revolution with a polygon section")?;
let outline: Vec<P2> = section.corners.iter().map(|&(rho, z)| P2 { x: rho, y: z }).collect();
let real: Vec<bool> = section.on_axis.iter().map(|on| !on).collect();
let n = outline.len();
if n < 3 {
return Err("the section has no area".into());
}
for k in 0..n {
let (a, b) = (outline[k], outline[(k + 1) % n]);
let touches = a.x.abs() <= 1e-7 * scale || b.x.abs() <= 1e-7 * scale;
if real[k] && touches && (a.y - b.y).abs() > 1e-7 * scale {
return Err("a section edge meets the axis at a slant".into());
}
}
let (loops, new_topology) = eroded_loops(&outline, &real, depth, tolerance)?;
if only_new_topology && !new_topology {
return Ok(None);
}
for piece in loops.iter().flatten() {
for t in [0.0, 0.5, 1.0] {
if piece.at(depth, t).x < -1e-7 * scale {
return Err("a piece of the erosion crosses the axis".into());
}
}
}
let place = |point: P2| section.origin.add(section.x.scale(point.x)).add(section.axis.scale(point.y));
let mut body: Option<BrepSolid> = None;
for chain in &loops {
let joints: Vec<P2> = chain
.iter()
.map(|piece| {
let mut point = piece.at(depth, 0.0);
if point.x.abs() <= 1e-7 * scale {
point.x = 0.0;
}
point
})
.collect();
let mut curves = Vec::with_capacity(chain.len());
for (k, piece) in chain.iter().enumerate() {
let end = joints[(k + 1) % chain.len()];
let curve = match *piece {
Piece::Line { .. } => {
if joints[k].sub(end).len() <= tolerance {
continue;
}
make_line(place(joints[k]), place(end))?
}
Piece::Arc { center, start, sweep } if sweep > 0.0 => make_arc(place(center), section.x, section.axis, depth, start, start + sweep)?,
Piece::Arc { center, start, sweep } => {
make_arc(place(center), section.x, section.axis.scale(-1.0), depth, -start, -start - sweep)?
}
};
curves.push(curve);
}
let part = revolve_profile_brep(&curves, section.origin, section.axis, std::f64::consts::TAU)?;
match &mut body {
None => body = Some(part),
Some(solid) => append_disjoint(solid, part),
}
}
let solid = body.ok_or("nothing is left")?;
let issues = solid.validate();
if !issues.is_empty() {
return Err(format!("the eroded revolution does not validate: {:?}", issues.first()));
}
let face_images = solid
.shells
.iter()
.flat_map(|shell| &shell.faces)
.map(|_| OffsetShellFaceImageRecord { role: OffsetFaceRole::Offset, source_face_id: 0 })
.collect();
Ok(Some(OffsetShellResultRecord { solid, face_images }))
}