use super::face_move_recut::{convex_solid, three_plane_point, HalfSpace};
use super::*;
use crate::offset_retrim::plane_of_surface;
use crate::{boolean_operation, make_cone_brep, make_cylinder_brep, make_sphere_brep, make_torus_brep, BooleanOperation, BooleanOptions};
use crate::{KernelRefusal, KernelStage, OrRefuse};
const OP: &str = "transformFace(convex re-cut)";
const VOLUME_TOLERANCE: f64 = 1e-9;
fn declined(what: &str, message: String) -> KernelRefusal {
KernelRefusal::unsupported(KernelStage::Classify, what, format!("{OP}: {message}"))
}
#[derive(Clone, Copy, Debug)]
struct Ruled {
origin: Vec3,
axis: Vec3,
rho0: f64,
slope: f64,
}
impl Ruled {
fn radius(&self, z: f64) -> f64 {
self.rho0 + self.slope * z
}
fn station(&self, point: Vec3) -> f64 {
point.sub(self.origin).dot(self.axis)
}
fn same(&self, other: &Ruled, band: f64) -> bool {
let sign = if self.axis.dot(other.axis) < 0.0 { -1.0 } else { 1.0 };
if self.axis.cross(other.axis).length() > 1e-9 {
return false;
}
let offset = other.origin.sub(self.origin);
if offset.sub(self.axis.scale(offset.dot(self.axis))).length() > band {
return false;
}
let z = self.station(other.origin);
(self.radius(z) - other.rho0).abs() <= band && (self.slope - sign * other.slope).abs() <= 1e-9
}
}
fn ruled_carrier(face: &FaceRecord) -> Option<Ruled> {
match face.surface.analytic()? {
AnalyticSurface::RuledRevolution { frame, rho0, rho1, height } if *height > 0.0 => Some(Ruled {
origin: frame.origin,
axis: frame.axis,
rho0: *rho0,
slope: (rho1 - rho0) / height,
}),
_ => None,
}
}
fn sample(face: &FaceRecord) -> Option<(Vec3, Vec3)> {
let [u0, u1] = face.surface.domain_u().ok()?;
let [v0, v1] = face.surface.domain_v().ok()?;
let (u, v) = (0.5 * (u0 + u1), 0.5 * (v0 + v1));
let point = face.surface.evaluate(u, v).ok()?;
let normal = face.surface.normal(u, v).ok()?;
Some((point, if face.same_sense { normal } else { normal.scale(-1.0) }))
}
enum Region {
Half(HalfSpace),
Inside(Ruled, f64),
Closed(Closed),
}
#[derive(Clone, Copy, Debug)]
enum Closed {
Ball { centre: Vec3, radius: f64 },
Torus { centre: Vec3, axis: Vec3, major: f64, minor: f64 },
}
impl Closed {
fn of(face: &FaceRecord) -> Option<Closed> {
match face.surface.analytic()? {
AnalyticSurface::Sphere { frame, radius } => Some(Closed::Ball { centre: frame.origin, radius: *radius }),
AnalyticSurface::Torus { frame, major_radius, minor_radius } => Some(Closed::Torus {
centre: frame.origin,
axis: frame.axis,
major: *major_radius,
minor: *minor_radius,
}),
_ => None,
}
}
fn core(&self, point: Vec3) -> Option<Vec3> {
match *self {
Closed::Ball { centre, .. } => Some(centre),
Closed::Torus { centre, axis, major, .. } => {
let offset = point.sub(centre);
let radial = offset.sub(axis.scale(offset.dot(axis))).normalized().ok()?;
Some(centre.add(radial.scale(major)))
}
}
}
fn same(&self, other: &Closed, band: f64) -> bool {
match (*self, *other) {
(Closed::Ball { centre: a, radius: r }, Closed::Ball { centre: b, radius: s }) => {
a.sub(b).length() <= band && (r - s).abs() <= band
}
(
Closed::Torus { centre: a, axis: p, major: r, minor: m },
Closed::Torus { centre: b, axis: q, major: s, minor: n },
) => a.sub(b).length() <= band && p.cross(q).length() <= 1e-9 && (r - s).abs() <= band && (m - n).abs() <= band,
_ => false,
}
}
fn tool(&self) -> Result<BrepSolid, String> {
match *self {
Closed::Ball { centre, radius } => make_sphere_brep(centre, radius, Vec3::new(0.0, 0.0, 1.0)),
Closed::Torus { centre, axis, major, minor } => make_torus_brep(centre, axis, major, minor),
}
}
}
fn oriented_plane(face: &FaceRecord, band: f64) -> Option<HalfSpace> {
let plane = plane_of_surface(&face.surface, band, OP).ok()?;
let normal = if face.same_sense { plane.normal } else { plane.normal.scale(-1.0) };
Some(HalfSpace { normal, offset: normal.dot(plane.origin) })
}
fn regions(solid: &BrepSolid, band: f64, excluded: &[u64]) -> Result<Vec<(u64, Option<String>, Region)>, KernelRefusal> {
let mut out = Vec::new();
for face in solid.shells.iter().flat_map(|shell| &shell.faces).filter(|face| !excluded.contains(&face.id)) {
if let Some(space) = oriented_plane(face, band) {
out.push((face.id, face.name.clone(), Region::Half(space)));
continue;
}
if let Some(closed) = Closed::of(face) {
let (point, normal) = sample(face)
.ok_or_else(|| declined("curved_neighbour", format!("face {} cannot be sampled", face.id)))?;
let core = closed
.core(point)
.ok_or_else(|| declined("curved_neighbour", format!("face {} sits on its torus's axis", face.id)))?;
if normal.dot(point.sub(core)) <= 0.0 {
return Err(declined("not_convex", format!("face {} bounds a hollow, not material", face.id)));
}
out.push((face.id, face.name.clone(), Region::Closed(closed)));
continue;
}
let Some(ruled) = ruled_carrier(face) else {
return Err(declined(
"curved_neighbour",
format!("face {} is neither a plane, a cylinder, a cone, a sphere nor a torus", face.id),
));
};
let (point, normal) = sample(face)
.ok_or_else(|| declined("curved_neighbour", format!("face {} cannot be sampled", face.id)))?;
let offset = point.sub(ruled.origin);
let radial = offset.sub(ruled.axis.scale(offset.dot(ruled.axis)));
if normal.dot(radial) <= 0.0 {
return Err(declined("not_convex", format!("face {} bounds a hole, not material", face.id)));
}
let station = ruled.station(point);
out.push((face.id, face.name.clone(), Region::Inside(ruled, station)));
}
Ok(out)
}
struct Motion {
rotation: Option<AffineTransform>,
translation: Vec3,
}
impl Motion {
fn point(&self, point: Vec3) -> Vec3 {
match &self.rotation {
None => point,
Some(rotation) => rotation.point(point),
}
.add(self.translation)
}
fn half_space(&self, space: HalfSpace) -> Result<HalfSpace, KernelRefusal> {
let (normal, point) = match &self.rotation {
None => (space.normal, space.normal.scale(space.offset)),
Some(rotation) => (
super::face_rotate::rotate_direction(rotation, space.normal)
.normalized()
.or_refuse(KernelStage::Collect, "rotation")?,
rotation.point(space.normal.scale(space.offset)),
),
};
let point = point.add(self.translation);
Ok(HalfSpace { normal, offset: normal.dot(point) })
}
}
fn intersect(
regions: &[(u64, Option<String>, Region)],
selected: &[u64],
motion: Option<&Motion>,
(centre, half): (Vec3, f64),
apex_whole: bool,
tolerance: f64,
) -> Result<(BrepSolid, Vec<HalfSpace>, Vec<HalfSpace>), KernelRefusal> {
let mut artificial: Vec<HalfSpace> = Vec::new();
let mut caps: Vec<HalfSpace> = Vec::new();
for axis in [Vec3::new(1.0, 0.0, 0.0), Vec3::new(0.0, 1.0, 0.0), Vec3::new(0.0, 0.0, 1.0)] {
for sign in [1.0, -1.0] {
let normal = axis.scale(sign);
artificial.push(HalfSpace { normal, offset: normal.dot(centre) + half });
}
}
let mut spaces = artificial.clone();
for (id, _, region) in regions {
if let Region::Half(space) = region {
let space = match motion {
Some(motion) if selected.contains(id) => motion.half_space(*space)?,
_ => *space,
};
let band = tolerance.max(1e-9) * 1e3;
if !spaces.iter().any(|other| other.normal.sub(space.normal).length() <= 1e-9 && (other.offset - space.offset).abs() <= band) {
spaces.push(space);
}
}
}
let band = tolerance.max(1e-9) * 10.0;
let mut corners: Vec<Vec3> = Vec::new();
for a in 0..spaces.len() {
for b in (a + 1)..spaces.len() {
for c in (b + 1)..spaces.len() {
let Some(point) = three_plane_point(&spaces[a], &spaces[b], &spaces[c]) else { continue };
if spaces.iter().all(|space| space.normal.dot(point) - space.offset <= band) {
corners.push(point);
}
}
}
}
if corners.len() < 4 {
return Err(declined("unbounded", "the planes enclose no volume".into()));
}
let inside = corners.iter().fold(Vec3::new(0.0, 0.0, 0.0), |sum, point| sum.add(*point)).scale(1.0 / corners.len() as f64);
let mut body = convex_solid(&spaces, inside, tolerance)
.map_err(|error| declined("unbounded", format!("the planes' polytope does not build: {error}")))?;
let seam_hint = motion.and_then(|motion| {
regions.iter().find_map(|(id, _, region)| match region {
Region::Half(space) if selected.contains(id) => motion.half_space(*space).ok().map(|space| space.normal),
_ => None,
})
});
for (_, _, region) in regions {
if let Region::Closed(closed) = region {
let tool = closed.tool().map_err(|error| declined("tool", format!("the region's tool does not build: {error}")))?;
body = boolean_operation(&body, &tool, BooleanOperation::Intersect, &BooleanOptions::default())
.map_err(|error| error.with_message(|message| format!("{OP}: the intersection refused — {message}")))?;
continue;
}
let Region::Inside(ruled, station) = region else { continue };
let reach = half * 3f64.sqrt();
let middle = ruled.station(centre);
let (mut from, mut to) = (middle - reach, middle + reach);
let mut apex_end = None;
if ruled.slope.abs() > 1e-12 {
let apex = -ruled.rho0 / ruled.slope;
let keep_short = if apex_whole { 0.0 } else { 1e-3 * half };
if *station > apex {
if apex + keep_short > from {
from = apex + keep_short;
apex_end = Some(from);
}
} else if apex - keep_short < to {
to = apex - keep_short;
apex_end = Some(to);
}
if let (Some(end), false) = (apex_end, apex_whole) {
let normal = if *station > apex { ruled.axis.scale(-1.0) } else { ruled.axis };
caps.push(HalfSpace { normal, offset: normal.dot(ruled.origin.add(ruled.axis.scale(end))) });
}
}
if to - from <= 0.0 {
return Err(declined("unbounded", "a cone's nappe misses the clip".into()));
}
let (wide, narrow) = if ruled.radius(from).abs() >= ruled.radius(to).abs() { (from, to) } else { (to, from) };
let direction = if wide <= narrow { ruled.axis } else { ruled.axis.scale(-1.0) };
let base = ruled.origin.add(ruled.axis.scale(wide));
let narrow_radius = if apex_whole && apex_end.is_some() { 0.0 } else { ruled.radius(narrow).abs() };
let tool = if ruled.slope.abs() <= 1e-12 {
make_cylinder_brep(base, direction, ruled.rho0.abs(), (to - from).abs())
} else {
make_cone_brep(base, direction, ruled.radius(wide).abs(), narrow_radius, (to - from).abs())
}
.map_err(|error| declined("tool", format!("the region's tool does not build: {error}")))?;
let tool = match seam_hint {
Some(hint) => turn_seam_towards(tool, ruled, hint)?,
None => tool,
};
body = boolean_operation(&body, &tool, BooleanOperation::Intersect, &BooleanOptions::default())
.map_err(|error| error.with_message(|message| format!("{OP}: the intersection refused — {message}")))?;
}
Ok((body, artificial, caps))
}
fn turn_seam_towards(tool: BrepSolid, ruled: &Ruled, hint: Vec3) -> Result<BrepSolid, KernelRefusal> {
let wanted = hint.sub(ruled.axis.scale(hint.dot(ruled.axis)));
if wanted.length() <= 0.1 {
return Ok(tool);
}
let Some(seam) = tool.shells.iter().flat_map(|shell| &shell.faces).find_map(|face| match face.surface.analytic() {
Some(AnalyticSurface::RuledRevolution { frame, .. }) => Some(frame.x_axis),
_ => None,
}) else {
return Ok(tool);
};
let angle = seam.cross(wanted).dot(ruled.axis).atan2(seam.dot(wanted));
let rotation = super::face_rotate::rotation_about(ruled.origin, ruled.axis, angle).or_refuse(KernelStage::Collect, "rotation")?;
crate::transform_brep(&tool, rotation, false).or_refuse(KernelStage::Collect, "transform_brep")
}
fn solve(
regions: &[(u64, Option<String>, Region)],
selected: &[u64],
motion: Option<&Motion>,
(centre, half): (Vec3, f64),
band: f64,
tolerance: f64,
) -> Result<BrepSolid, KernelRefusal> {
let touches = |body: &BrepSolid, planes: &[HalfSpace], size: f64| {
let touch = band.max(1e-7 * size);
body.vertices
.iter()
.map(|vertex| vertex.point)
.find(|point| planes.iter().any(|space| (space.normal.dot(*point) - space.offset).abs() <= touch))
};
let mut last = None;
for grow in [1.0, 10.0, 100.0, 1000.0] {
let size = half * grow;
let (mut body, mut cube, caps) = intersect(regions, selected, motion, (centre, size), false, tolerance)?;
if touches(&body, &caps, size).is_some() {
(body, cube, _) = intersect(regions, selected, motion, (centre, size), true, tolerance)?;
}
match touches(&body, &cube, size) {
None => return Ok(body),
Some(point) => last = Some(point),
}
}
Err(declined(
"beyond_reach",
format!(
"the moved body still reaches the clip at {:?} with the clip a thousand times the \
body's size: the faces left open there either have no body at all or one reaching \
farther than this road builds",
last.unwrap_or_default()
),
))
}
pub fn recut_convex_body(
solid: &BrepSolid,
face_ids: &[u64],
turn: Option<(Vec3, Vec3, f64)>,
translation: Vec3,
) -> Result<BrepSolid, KernelRefusal> {
let tolerance = solid_model_scale(solid).max(1.0) * 1e-9;
let band = tolerance * 1e3;
if solid.shells.len() != 1 {
return Err(declined("not_convex", "the body has more than one shell".into()));
}
let regions = regions(solid, band, &[])?;
for id in face_ids {
let Some((_, _, Region::Half(space))) = regions.iter().find(|(face, ..)| face == id) else {
return Err(declined("curved_carrier", format!("selected face {id} is not a plane")));
};
if let Some((other, ..)) = regions.iter().find(|(other, _, region)| {
!face_ids.contains(other)
&& matches!(region, Region::Half(o) if o.normal.sub(space.normal).length() <= 1e-9 && (o.offset - space.offset).abs() <= band)
}) {
return Err(declined(
"coplanar_unselected",
format!("face {other} lies on selected face {id}'s plane but is not selected; moving the plane would move it too"),
));
}
}
let motion = Motion {
rotation: match turn {
None => None,
Some((point, axis, angle)) => Some(
super::face_rotate::rotation_about(point, axis, angle).or_refuse(KernelStage::Collect, "rotation")?,
),
},
translation,
};
let bounds = crate::recentre::solid_bounds(solid);
let centre = bounds.minimum.add(bounds.maximum).scale(0.5);
let diagonal = bounds.maximum.sub(bounds.minimum).length();
if !diagonal.is_finite() {
return Err(declined("unbounded", "the body's bounds cannot be read".into()));
}
let pivot_reach = turn.map(|(point, ..)| point.sub(centre).length()).unwrap_or(0.0);
let half = 2.0 * (diagonal + pivot_reach) + translation.length();
let input_volume = solid_signed_volume(solid).map(f64::abs).or_refuse(KernelStage::Collect, "volume")?;
let identity = solve(®ions, face_ids, None, (centre, half), band, tolerance)?;
let identity_volume = solid_signed_volume(&identity).map(f64::abs).or_refuse(KernelStage::Collect, "volume")?;
if identity.shells.len() != 1 || (identity_volume - input_volume).abs() > VOLUME_TOLERANCE * input_volume {
return Err(declined(
"not_convex",
format!(
"the intersection of the faces' own regions measures {identity_volume} in {} \
shell(s), not the body's {input_volume}: the body is not that intersection",
identity.shells.len()
),
));
}
let body = solve(®ions, face_ids, Some(&motion), (centre, half), band, tolerance)?;
let volume = solid_signed_volume(&body).map(f64::abs).or_refuse(KernelStage::Collect, "volume")?;
if body.shells.len() != 1 || volume <= VOLUME_TOLERANCE * input_volume {
return Err(declined(
"unbounded",
format!("the moved intersection is {} shell(s) of volume {volume}", body.shells.len()),
));
}
let body = crate::merge_same_surface_faces(&body, tolerance).unwrap_or(body);
let mut body = restamp(body, solid, ®ions, face_ids, &motion, band);
super::face_move_redrill::restore_edge_names(&mut body, solid, band);
Ok(body)
}
pub(super) fn filled_without(solid: &BrepSolid, excluded: &[u64]) -> Result<BrepSolid, KernelRefusal> {
let tolerance = solid_model_scale(solid).max(1.0) * 1e-9;
let band = tolerance * 1e3;
let regions = regions(solid, band, excluded)?;
let bounds = crate::recentre::solid_bounds(solid);
let centre = bounds.minimum.add(bounds.maximum).scale(0.5);
let diagonal = bounds.maximum.sub(bounds.minimum).length();
if !diagonal.is_finite() {
return Err(declined("unbounded", "the body's bounds cannot be read".into()));
}
let body = solve(®ions, &[], None, (centre, 2.0 * diagonal), band, tolerance)?;
let body = crate::merge_same_surface_faces(&body, tolerance).unwrap_or(body);
Ok(restamp(body, solid, ®ions, &[], &Motion { rotation: None, translation: Vec3::new(0.0, 0.0, 0.0) }, band))
}
fn restamp(
mut body: BrepSolid,
solid: &BrepSolid,
regions: &[(u64, Option<String>, Region)],
selected: &[u64],
motion: &Motion,
band: f64,
) -> BrepSolid {
let mut wanted: Vec<(String, Region, Option<Vec3>)> = Vec::new();
for (id, name, region) in regions {
let Some(name) = name.clone() else { continue };
let at = solid.shells.iter().flat_map(|shell| &shell.faces).find(|face| face.id == *id).and_then(|face| face_point(solid, face));
match region {
Region::Half(space) if selected.contains(id) => {
if let Ok(moved) = motion.half_space(*space) {
wanted.push((name, Region::Half(moved), at.map(|point| motion.point(point))));
}
}
Region::Half(space) => wanted.push((name, Region::Half(*space), at)),
Region::Inside(ruled, station) => wanted.push((name, Region::Inside(*ruled, *station), at)),
Region::Closed(closed) => wanted.push((name, Region::Closed(*closed), at)),
}
}
let points: HashMap<u64, Vec3> = body
.shells
.iter()
.flat_map(|shell| &shell.faces)
.filter_map(|face| face_point(&body, face).map(|point| (face.id, point)))
.collect();
let mut used: HashMap<String, usize> = HashMap::default();
for shell in &mut body.shells {
for face in &mut shell.faces {
let on_carrier = |region: &Region| {
if let Some(space) = oriented_plane(face, band) {
matches!(region, Region::Half(other)
if other.normal.sub(space.normal).length() <= 1e-9 && (other.offset - space.offset).abs() <= band)
} else if let Some(closed) = Closed::of(face) {
matches!(region, Region::Closed(other) if other.same(&closed, band))
} else if let Some(ruled) = ruled_carrier(face) {
matches!(region, Region::Inside(other, _) if other.same(&ruled, band))
} else {
false
}
};
let here = points.get(&face.id).copied();
let distance = |at: &Option<Vec3>| match (here, at) {
(Some(here), Some(at)) => here.sub(*at).length(),
_ => f64::INFINITY,
};
let found = wanted
.iter()
.filter(|(_, region, _)| on_carrier(region))
.min_by(|a, b| distance(&a.2).total_cmp(&distance(&b.2)));
let Some((name, ..)) = found else {
face.name = None;
continue;
};
let count = used.entry(name.clone()).or_insert(0);
*count += 1;
face.name = Some(if *count == 1 { name.clone() } else { format!("{name}_{}", *count - 1) });
}
}
body
}
fn face_point(solid: &BrepSolid, face: &FaceRecord) -> Option<Vec3> {
let mut sum = Vec3::new(0.0, 0.0, 0.0);
let mut count = 0usize;
for coedge in face.loops.iter().flat_map(|lp| &lp.coedges) {
let edge = solid.edges.iter().find(|edge| edge.id == coedge.edge_id)?;
for id in [edge.start_vertex_id, edge.end_vertex_id] {
sum = sum.add(solid.vertices.iter().find(|vertex| vertex.id == id)?.point);
count += 1;
}
}
(count > 0).then(|| sum.scale(1.0 / count as f64))
}