use crate::{
boolean,
error::{Error, Result},
math::{Frame, Point3, Vec2, Vec3},
measure,
profile::Profile,
sweep::extrude,
topo::Brep,
};
pub fn half_space(s: &Brep, origin: Point3, normal: Vec3) -> Result<Brep> {
let n = normal
.unit()
.ok_or_else(|| Error::InvalidInput("a plane needs a normal".into()))?;
let b = measure::bbox(s);
let reach = 2.0 * (b.diagonal() + b.center().dist(origin)) + 1.0;
let frame = Frame::new(origin, n, n.any_perpendicular())?;
extrude(
&Profile::rect(Vec2::ZERO, 2.0 * reach, 2.0 * reach),
&frame,
reach,
"half",
)
}
pub fn split(s: &Brep, origin: Point3, normal: Vec3) -> Result<(Brep, Brep)> {
if s.solid_ids().next().is_none() {
return Ok((s.clone(), s.clone()));
}
let h = half_space(s, origin, normal)?;
Ok((boolean::intersection(s, &h)?, boolean::difference(s, &h)?))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::measure::section_area;
fn prism(pts: &[[f64; 2]], h: f64) -> Brep {
let pts: Vec<Vec2> = pts.iter().map(|p| Vec2::new(p[0], p[1])).collect();
let names: Vec<String> = (0..pts.len()).map(|i| format!("e{i}")).collect();
let names: Vec<&str> = names.iter().map(String::as_str).collect();
extrude(&Profile::polygon(&pts, &names), &Frame::WORLD, h, "p").unwrap()
}
fn close(got: f64, want: f64) {
assert!(
(got - want).abs() < 1e-7 * want.max(1.0),
"{got} against {want}"
);
}
#[test]
fn a_block_split_across_keeps_each_part_and_its_cut() {
let b = prism(&[[0.0, 0.0], [4.0, 0.0], [4.0, 3.0], [0.0, 3.0]], 2.0);
let (o, n) = (Point3::new(1.0, 0.0, 0.0), Vec3::new(-1.0, 0.0, 0.0));
let (near, far) = split(&b, o, n).unwrap();
close(measure::volume(&near), 6.0);
close(measure::volume(&far), 18.0);
close(section_area(&b, o, n).unwrap(), 6.0);
}
}