use super::prism_erosion::P2;
use super::revolved_parallel::half_section_polygon_reading;
use super::*;
use crate::{make_line, project_point_to_surface};
pub(super) fn enabled() -> bool {
switch!("BREP_OFFSET_REVOLVED_SHELL", true)
}
pub(super) fn revolved_shell(source: &BrepSolid, opening_face_ids: &[u64], distance: f64) -> Option<OffsetShellResultRecord> {
if !enabled() || !(distance > MINIMUM_DISTANCE) || !distance.is_finite() || opening_face_ids.is_empty() {
return None;
}
match revolved_shell_impl(source, opening_face_ids, distance) {
Ok(result) => Some(result),
Err(reason) => {
os_debug!("revolved shell declined: {reason}");
None
}
}
}
fn revolved_shell_impl(source: &BrepSolid, opening_face_ids: &[u64], depth: f64) -> Result<OffsetShellResultRecord, String> {
let scale = crate::solid_scale(source).max(1.0);
let tolerance = 1e-9 * scale;
let snap = 1e-7 * scale;
for face in source.shells.iter().flat_map(|shell| &shell.faces) {
if !matches!(face.surface.analytic(), Some(crate::AnalyticSurface::Plane { .. } | crate::AnalyticSurface::RuledRevolution { .. })) {
return Err("a face is not an exact plane or straight revolution".into());
}
}
let section = half_section_polygon_reading(source, true).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 n = outline.len();
if n < 3 {
return Err("the section has no area".into());
}
let place = |point: P2| section.origin.add(section.x.scale(point.x)).add(section.axis.scale(point.y));
let faces: Vec<&FaceRecord> = source.shells.iter().flat_map(|shell| &shell.faces).collect();
let mut face_of: Vec<Option<usize>> = vec![None; n];
for k in 0..n {
if section.on_axis[k] {
continue;
}
let middle = place(outline[k].add(outline[(k + 1) % n]).scale(0.5));
let mut found = None;
for (index, face) in faces.iter().enumerate() {
let foot = project_point_to_surface(&face.surface, middle).map_err(|error| error.to_string())?;
if foot.distance <= snap {
if found.is_some() {
return Err("a run lies on two faces' carriers".into());
}
found = Some(index);
}
}
face_of[k] = Some(found.ok_or("a run lies on no face")?);
}
if (0..faces.len()).any(|index| !face_of.contains(&Some(index))) {
return Err("a face lies on no run".into());
}
if opening_face_ids.iter().any(|id| !faces.iter().any(|face| face.id == *id)) {
return Err("an opening is not a face of the body".into());
}
let real: Vec<bool> = face_of.iter().map(|face| face.is_some_and(|index| !opening_face_ids.contains(&faces[index].id))).collect();
if !real.iter().any(|&r| r) {
return Err("no retained face".into());
}
for k in 0..n {
let (a, b, c) = (outline[(k + n - 1) % n], outline[k], outline[(k + 1) % n]);
if b.sub(a).cross(c.sub(b)) < -tolerance * c.sub(a).len() {
return Err("the section has a reflex corner".into());
}
}
let chain = clipped_cavity(&outline, &real, depth, tolerance)?;
if chain.iter().any(|&(a, b, _, _)| a.x < -snap || b.x < -snap) {
return Err("the cavity crosses the axis".into());
}
let moved: Vec<bool> = chain.iter().map(|&(_, _, _, moved)| moved).collect();
let m = chain.len();
let starts: Vec<usize> = (0..m).filter(|&i| moved[i] && !moved[(i + m - 1) % m]).collect();
let [first] = starts.as_slice() else {
return Err(if moved.iter().all(|&x| x) { "the cavity touches no opening".into() } else { format!("{} stretches of moved runs", starts.len()) });
};
let mut last = *first;
while moved[(last + 1) % m] {
last = (last + 1) % m;
}
let (before, after) = ((first + m - 1) % m, (last + 1) % m);
let (from, to) = (chain[*first].0, chain[last].1);
let (edge_from, edge_to) = (chain[before].2, chain[after].2);
if real[edge_from] || real[edge_to] {
return Err("the cavity touches a retained run".into());
}
if edge_from == edge_to {
let (start, run) = (outline[edge_from], outline[(edge_from + 1) % n].sub(outline[edge_from]));
if !(to.sub(start).dot(run) < from.sub(start).dot(run)) {
return Err("the cavity leaves and rejoins one run the wrong way round".into());
}
}
let mut points: Vec<(P2, (usize, bool))> = vec![(from, (edge_from, false))];
let mut k = (edge_from + 1) % n;
loop {
points.push((outline[k], (k, false)));
if k == edge_to {
break;
}
k = (k + 1) % n;
}
points.push((to, (chain[last].2, true)));
let mut i = last;
while i != *first {
let previous = (i + m - 1) % m;
points.push((chain[i].0, (chain[previous].2, true)));
i = previous;
}
let mut snapped: Vec<(P2, (usize, bool))> = Vec::with_capacity(points.len());
for (mut point, run) in points {
if point.x.abs() <= snap {
point.x = 0.0;
}
match snapped.last_mut() {
Some(last) if last.0.sub(point).len() <= tolerance => last.1 = run,
_ => snapped.push((point, run)),
}
}
while snapped.len() > 1 && snapped[0].0.sub(snapped[snapped.len() - 1].0).len() <= tolerance {
snapped.pop();
}
if snapped.len() < 3 {
return Err("the shell's section has no area".into());
}
let y = section.axis.cross(section.x);
let count = snapped.len();
let mut sheets = Vec::with_capacity(count);
let mut sheet_images = Vec::with_capacity(count);
for k in 0..count {
let ((a, (run, moved)), b) = (snapped[k], snapped[(k + 1) % count].0);
if a.x == 0.0 && b.x == 0.0 {
continue;
}
let Some(index) = face_of[run] else { return Err("a segment off the axis on the axis run".into()) };
let source = faces[index].id;
let role = match (moved, opening_face_ids.contains(&source)) {
(true, false) => OffsetFaceRole::Offset,
(false, false) => OffsetFaceRole::Source,
(false, true) => OffsetFaceRole::Wall,
(true, true) => return Err("an opening's run was moved".into()),
};
sheet_images.push(OffsetShellFaceImageRecord { role, source_face_id: source });
let sheet = if (a.y - b.y).abs() <= tolerance {
let centre = place(P2 { x: 0.0, y: a.y });
let (outer, inner) = (a.x.max(b.x), a.x.min(b.x));
let reach = 2.0 * outer;
let corner = centre.sub(section.x.scale(reach)).sub(y.scale(reach));
let plane = crate::make_plane(corner, section.x, y, 2.0 * reach, 2.0 * reach)?;
let mut loops = vec![vec![crate::make_circle(centre, section.axis, outer)?]];
if inner > snap {
loops.push(vec![crate::make_circle(centre, section.axis, inner)?]);
}
crate::face_from_surface_trimmed(&plane, &loops)
} else {
let line = make_line(place(a), place(b))?;
crate::face_from_surface(&crate::make_revolution(section.origin, section.axis, &line, std::f64::consts::TAU)?)
};
sheets.push(sheet.map_err(|error| error.message)?);
}
let solid = crate::solid_from_sheets(&sheets, snap).map_err(|error| error.message)?;
let issues = solid.validate();
if !issues.is_empty() {
return Err(format!("the shell does not validate: {:?}", issues.first()));
}
let moment: f64 = (0..count)
.map(|k| {
let (a, b) = (snapped[k].0, snapped[(k + 1) % count].0);
(a.x * b.y - b.x * a.y) * (a.x + b.x) / 6.0
})
.sum();
let expected = std::f64::consts::TAU * moment;
let volume = crate::solid_mass_properties(&solid).map_err(|error| error.to_string())?.volume;
if !((volume - expected).abs() <= 1e-9 * expected.abs().max(1.0)) {
return Err(format!("the shell's volume {volume} is not its section's {expected}"));
}
let mut face_images = Vec::new();
for face in solid.shells.iter().flat_map(|shell| &shell.faces) {
let matches: Vec<usize> = (0..sheets.len())
.filter(|&i| sheets[i].shells.iter().flat_map(|shell| &shell.faces).any(|sheet_face| same_carrier(&sheet_face.surface, &face.surface)))
.collect();
let [index] = matches.as_slice() else { return Err(format!("a face matches {} sheets", matches.len())) };
face_images.push(sheet_images[*index].clone());
}
Ok(OffsetShellResultRecord { solid, face_images })
}
fn same_carrier(a: &crate::NurbsSurface, b: &crate::NurbsSurface) -> bool {
let bits = |values: &[f64]| values.iter().map(|value| value.to_bits()).collect::<Vec<_>>();
a.degree_u == b.degree_u
&& a.degree_v == b.degree_v
&& bits(&a.knots_u) == bits(&b.knots_u)
&& bits(&a.knots_v) == bits(&b.knots_v)
&& a.control_points.len() == b.control_points.len()
&& a.control_points.iter().zip(&b.control_points).all(|(row_a, row_b)| {
row_a.len() == row_b.len()
&& row_a.iter().zip(row_b).all(|(p, q)| [p.x, p.y, p.z, p.w].map(f64::to_bits) == [q.x, q.y, q.z, q.w].map(f64::to_bits))
})
}
fn clipped_cavity(outline: &[P2], real: &[bool], depth: f64, tolerance: f64) -> Result<Vec<(P2, P2, usize, bool)>, String> {
let n = outline.len();
let mut polygon: Vec<(P2, usize, bool)> = (0..n).map(|k| (outline[k], k, false)).collect();
for k in (0..n).filter(|&k| real[k]) {
let (a, b) = (outline[k], outline[(k + 1) % n]);
let run = b.sub(a);
let inward = P2 { x: -run.y, y: run.x }.scale(1.0 / run.len());
let side = |p: P2| p.sub(a).dot(inward) - depth;
let mut clipped = Vec::with_capacity(polygon.len() + 1);
let m = polygon.len();
for i in 0..m {
let (p, tag, moved) = polygon[i];
let q = polygon[(i + 1) % m].0;
let (fp, fq) = (side(p), side(q));
let crossing = || p.add(q.sub(p).scale(fp / (fp - fq)));
match (fp >= 0.0, fq >= 0.0) {
(true, true) => clipped.push((p, tag, moved)),
(true, false) => {
clipped.push((p, tag, moved));
clipped.push((crossing(), k, true));
}
(false, true) => clipped.push((crossing(), tag, moved)),
(false, false) => {}
}
}
polygon = clipped;
if polygon.len() < 3 {
return Err("the cavity is empty".into());
}
}
let mut kept: Vec<(P2, usize, bool)> = Vec::with_capacity(polygon.len());
for i in 0..polygon.len() {
let next = polygon[(i + 1) % polygon.len()].0;
if polygon[i].0.sub(next).len() > tolerance {
kept.push(polygon[i]);
}
}
let area2: f64 = (0..kept.len()).map(|i| kept[i].0.cross(kept[(i + 1) % kept.len()].0)).sum();
if kept.len() < 3 || area2 <= tolerance * tolerance {
return Err("the cavity is empty".into());
}
Ok((0..kept.len()).map(|i| (kept[i].0, kept[(i + 1) % kept.len()].0, kept[i].1, kept[i].2)).collect())
}