use crate::{face_area, shell_vector_areas, solid_piece_by_plane, BrepSolid, FaceRecord, KernelRefusal, KernelStage, NurbsCurve, Vec3};
fn sub_band_area_enabled() -> bool {
crate::switch!("BREP_SECTION_SUB_BAND_AREA", true)
}
enum Cut {
Miss,
Caps(BrepSolid, Vec<usize>),
SubBand(f64),
}
const COVERS: [f64; 4] = [1.2, 1.5, 2.0, 3.0];
fn caps(solid: &BrepSolid, origin: Vec3, normal: Vec3) -> Result<Cut, KernelRefusal> {
let unit = normal
.normalized()
.map_err(|_| KernelRefusal::input(KernelStage::Collect, "section_normal", "section: the plane normal has no direction"))?;
let refuse = |message: String| KernelRefusal::internal(KernelStage::Fragment, "section_split", format!("section: {message}"));
let surfaces = solid.shells.iter().flat_map(|shell| &shell.faces).flat_map(|face| face.surface.control_points.iter().flatten());
let curves = solid.edges.iter().flat_map(|edge| edge.curve.control_points.iter());
let (mut low, mut high) = (f64::INFINITY, f64::NEG_INFINITY);
for control in surfaces.chain(curves) {
let height = control.point().map_err(|message| refuse(message))?.sub(origin).dot(unit);
low = low.min(height);
high = high.max(height);
}
if !(low < 0.0 && high > 0.0) {
return Ok(Cut::Miss);
}
let mut declined = Vec::new();
let mut found = None;
'covers: for cover in COVERS {
for above in [false, true] {
match solid_piece_by_plane(solid, origin, unit, above, cover) {
Ok(piece) => {
found = Some((piece, above));
break 'covers;
}
Err(message) => declined.push(message),
}
}
}
let Some((piece, piece_above)) = found else {
return Err(refuse(declined.join("; ")));
};
let Some(piece) = piece else {
return Ok(Cut::Miss);
};
let extent = piece.vertices.iter().fold(0.0f64, |extent, vertex| {
extent.max(vertex.point.x.abs()).max(vertex.point.y.abs()).max(vertex.point.z.abs())
});
let scale = 1.0 + extent;
let tolerance = 1e-9 * scale;
let on_plane = |face: &FaceRecord| -> bool {
if !face.surface.is_affine().unwrap_or(false) {
return false;
}
face.surface.control_points.iter().flatten().all(|control| {
control.point().is_ok_and(|point| point.sub(origin).dot(unit).abs() <= tolerance)
})
};
let faces: Vec<usize> = piece.shells.iter().flat_map(|shell| &shell.faces).enumerate().filter(|(_, face)| on_plane(face)).map(|(index, _)| index).collect();
if faces.is_empty() {
let other = !piece_above;
let mut other_declined = Vec::new();
for cover in COVERS {
match solid_piece_by_plane(solid, origin, unit, other, cover) {
Ok(None) => return Ok(Cut::Miss),
Ok(Some(other_piece)) => {
if lumps_part(solid, &piece, &other_piece, origin, unit, other, tolerance) {
return Ok(Cut::Miss);
}
if sub_band_area_enabled() {
if let Some(area) = sub_band_area(solid, &piece, &other_piece, unit, piece_above) {
return Ok(Cut::SubBand(area));
}
}
return Err(refuse("the solid lies on both sides of the plane but the cut left no face on it".into()));
}
Err(message) => other_declined.push(message),
}
}
return Err(refuse(format!(
"the cut left no face on the plane, and the other side declined: {}",
other_declined.join("; ")
)));
}
Ok(Cut::Caps(piece, faces))
}
fn sub_band_area(solid: &BrepSolid, piece: &BrepSolid, other: &BrepSolid, unit: Vec3, piece_above: bool) -> Option<f64> {
let residual = |body: &BrepSolid| -> Option<(Vec3, f64)> {
let report = shell_vector_areas(body);
if !report.unreadable.is_empty() || report.shells.is_empty() {
return None;
}
let mut sum = Vec3::new(0.0, 0.0, 0.0);
let mut error = 0.0;
for shell in &report.shells {
if !shell.closed || shell.gave_up || !shell.trim_residual.length().is_finite() {
return None;
}
sum = sum.add(shell.trim_residual);
error += shell.quadrature_error;
}
Some((sum, error))
};
let (whole, whole_error) = residual(solid)?;
let (first, first_error) = residual(piece)?;
let (second, second_error) = residual(other)?;
let (above, below) = if piece_above { (first, second) } else { (second, first) };
let (area_above, area_below) = (above.dot(unit), -below.dot(unit));
if !(area_above > 0.0 && area_below > 0.0) {
return None;
}
let off = |vector: Vec3, along: f64| vector.sub(unit.scale(along)).length();
let noise = whole.length()
+ off(above, area_above)
+ off(below, -area_below)
+ (area_above - area_below).abs()
+ 10.0 * (whole_error + first_error + second_error);
let area = 0.5 * (area_above + area_below);
(area > 10.0 * noise).then_some(area)
}
fn lumps_part(solid: &BrepSolid, first: &BrepSolid, second: &BrepSolid, origin: Vec3, unit: Vec3, second_above: bool, tolerance: f64) -> bool {
let faces = |body: &BrepSolid| body.shells.iter().map(|shell| shell.faces.len()).sum::<usize>();
if faces(first) + faces(second) != faces(solid) {
return false;
}
let side = |body: &BrepSolid, above: bool| {
body.vertices.iter().all(|vertex| {
let height = vertex.point.sub(origin).dot(unit);
if above { height > tolerance } else { height < -tolerance }
})
};
if !(side(first, !second_above) && side(second, second_above)) {
return false;
}
match (crate::solid_mass_properties(solid), crate::solid_mass_properties(first), crate::solid_mass_properties(second)) {
(Ok(whole), Ok(a), Ok(b)) => (a.volume + b.volume - whole.volume).abs() <= 1e-6 * whole.volume.abs().max(f64::MIN_POSITIVE),
_ => false,
}
}
fn sample_edge(curve: &NurbsCurve, t0: f64, t1: f64, tolerance: f64) -> Result<Vec<Vec3>, String> {
let (low, high) = (t0.min(t1), t0.max(t1));
let mut breaks: Vec<f64> = vec![low];
breaks.extend(curve.knots.iter().copied().filter(|&knot| knot > low && knot < high));
breaks.push(high);
breaks.dedup();
let chord_error = |a: f64, b: f64| -> Result<f64, String> {
let (p, q) = (curve.evaluate(a)?, curve.evaluate(b)?);
let chord = q.sub(p);
let length = chord.length();
let mut worst = 0.0f64;
for fraction in [0.25, 0.5, 0.75] {
let point = curve.evaluate(a + (b - a) * fraction)?;
let offset = point.sub(p);
let along = if length > 0.0 { offset.dot(chord) / (length * length) } else { 0.0 };
worst = worst.max(offset.sub(chord.scale(along.clamp(0.0, 1.0))).length());
}
Ok(worst)
};
let mut parameters = vec![low];
for pair in breaks.windows(2) {
let mut start = pair[0];
let mut pending = vec![(pair[1], 0u32)];
while let Some((end, depth)) = pending.pop() {
if depth < 24 && chord_error(start, end)? > tolerance {
let middle = 0.5 * (start + end);
pending.push((end, depth + 1));
pending.push((middle, depth + 1));
continue;
}
parameters.push(end);
start = end;
}
}
let mut points = parameters.iter().map(|&t| curve.evaluate(t)).collect::<Result<Vec<_>, _>>()?;
if t1 < t0 {
points.reverse();
}
Ok(points)
}
pub fn solid_section(
solid: &BrepSolid,
origin: Vec3,
normal: Vec3,
chord_tolerance: f64,
) -> Result<Vec<Vec<Vec3>>, KernelRefusal> {
if !(chord_tolerance > 0.0 && chord_tolerance.is_finite()) {
return Err(KernelRefusal::input(KernelStage::Collect, "section_tolerance", "section: the chord tolerance must be positive"));
}
let (below, faces) = match caps(solid, origin, normal)? {
Cut::Miss => return Ok(Vec::new()),
Cut::Caps(below, faces) => (below, faces),
Cut::SubBand(area) => {
return Err(KernelRefusal::internal(
KernelStage::Fragment,
"section_split",
format!(
"section: every region of the cut is thinner than the boolean's weld band (area {area:.3e}), \
so no face on the plane holds its curves"
),
))
}
};
let all: Vec<&FaceRecord> = below.shells.iter().flat_map(|shell| &shell.faces).collect();
let mut pieces = Vec::new();
for index in faces {
for coedge in all[index].loops.iter().flat_map(|loop_record| &loop_record.coedges) {
let edge = below.edges.iter().find(|edge| edge.id == coedge.edge_id).ok_or_else(|| {
KernelRefusal::internal(KernelStage::Collect, "section_edge", format!("section: edge {} is missing", coedge.edge_id))
})?;
if edge.degenerate {
continue;
}
let points = sample_edge(&edge.curve, edge.t0, edge.t1, chord_tolerance)
.map_err(|message| KernelRefusal::internal(KernelStage::Collect, "section_sample", format!("section: {message}")))?;
pieces.push(points);
}
}
Ok(pieces)
}
pub fn solid_section_area(solid: &BrepSolid, origin: Vec3, normal: Vec3) -> Result<f64, KernelRefusal> {
let (below, faces) = match caps(solid, origin, normal)? {
Cut::Miss => return Ok(0.0),
Cut::Caps(below, faces) => (below, faces),
Cut::SubBand(area) => return Ok(area),
};
let all: Vec<&FaceRecord> = below.shells.iter().flat_map(|shell| &shell.faces).collect();
let mut area = 0.0;
for index in faces {
area += face_area(all[index])
.map_err(|message| KernelRefusal::internal(KernelStage::Collect, "section_area", format!("section area: {message}")))?;
}
Ok(area)
}