use crate::{
boolean,
boolean::half_space,
error::{Error, Result},
geom::{Curve, Surface},
math::{Point3, Vec3},
measure, tol,
topo::{Brep, FaceId},
};
fn caps(s: &Brep, origin: Point3, normal: Vec3) -> Result<(Brep, Vec<FaceId>)> {
if s.solid_ids().next().is_none() {
return Ok((s.clone(), Vec::new()));
}
let n = normal
.unit()
.ok_or_else(|| Error::InvalidInput("a plane needs a normal".into()))?;
let cut = boolean::intersection(s, &half_space(s, origin, n)?)?;
let caps = cut
.face_ids()
.filter(|&f| match &cut.face(f).surface {
Surface::Plane(pl) => {
pl.frame.z.cross(n).norm() <= 1e-9
&& (pl.frame.origin - origin).dot(n).abs() <= 10.0 * tol::LINEAR
}
_ => false,
})
.collect();
Ok((cut, caps))
}
pub fn section(s: &Brep, origin: Point3, normal: Vec3, tolerance: f64) -> Result<Vec<Vec<Point3>>> {
let (cut, caps) = caps(s, origin, normal)?;
let mut out = Vec::new();
for f in caps {
for &l in &cut.face(f).loops {
let mut pts: Vec<Point3> = Vec::new();
for &c in &cut.lp(l).coedges {
let co = cut.coedge(c);
let edge = cut.edge(co.edge);
let Some(curve) = &edge.curve else { continue };
let (t0, t1) = edge.range;
let mut run = Vec::new();
chords(curve, t0, t1, tolerance, 0, &mut run);
run.push(curve.eval(t1));
if co.reversed {
run.reverse();
}
run.pop();
pts.extend(run);
}
if !pts.is_empty() {
out.push(pts);
}
}
}
Ok(out)
}
pub fn section_area(s: &Brep, origin: Point3, normal: Vec3) -> Result<f64> {
let (cut, caps) = caps(s, origin, normal)?;
Ok(caps.iter().map(|&f| measure::face_area(&cut, f)).sum())
}
fn chords(c: &Curve, t0: f64, t1: f64, tolerance: f64, depth: u32, out: &mut Vec<Point3>) {
let (a, b) = (c.eval(t0), c.eval(t1));
let off = |t: f64| {
let p = c.eval(t);
match (b - a).unit() {
Some(u) => (p - a).cross(u).norm(),
None => p.dist(a),
}
};
let worst = [0.25, 0.5, 0.75]
.iter()
.map(|&f| off(t0 + (t1 - t0) * f))
.fold(0.0, f64::max);
if depth >= 4 && (worst <= 0.5 * tolerance || depth >= 24) {
out.push(a);
return;
}
let m = 0.5 * (t0 + t1);
chords(c, t0, m, tolerance, depth + 1, out);
chords(c, m, t1, tolerance, depth + 1, out);
}
#[derive(Clone, Debug)]
pub struct SectionEdge {
pub curve: Curve,
pub range: (f64, f64),
pub reversed: bool,
}
pub fn section_curves(s: &Brep, origin: Point3, normal: Vec3) -> Result<Vec<Vec<SectionEdge>>> {
let (cut, caps) = caps(s, origin, normal)?;
let mut out = Vec::new();
for f in caps {
for &l in &cut.face(f).loops {
let mut outline = Vec::new();
for &c in &cut.lp(l).coedges {
let co = cut.coedge(c);
let edge = cut.edge(co.edge);
if let Some(curve) = &edge.curve {
outline.push(SectionEdge {
curve: curve.clone(),
range: edge.range,
reversed: co.reversed,
});
}
}
if !outline.is_empty() {
out.push(outline);
}
}
}
Ok(out)
}