use super::*;
use crate::topology::BrepSolid;
struct Runout {
spine: usize,
vertex: u64,
point: Vec3,
floor: u64,
}
fn edges_at(solid: &BrepSolid, vertex: u64) -> Vec<u64> {
solid
.edges
.iter()
.filter(|edge| !edge.degenerate && (edge.start_vertex_id == vertex || edge.end_vertex_id == vertex))
.map(|edge| edge.id)
.collect()
}
fn is_planar(solid: &BrepSolid, face_id: u64) -> bool {
solid
.shells
.iter()
.flat_map(|shell| &shell.faces)
.find(|face| face.id == face_id)
.is_some_and(|face| matches!(face.surface.analytic(), Some(crate::AnalyticSurface::Plane { .. })))
}
fn detect(solid: &BrepSolid, selected_ids: &[u64], radius: f64) -> Option<(Vec<Runout>, bool)> {
let open: Vec<usize> = (0..selected_ids.len())
.filter(|index| {
solid
.edges
.iter()
.find(|edge| edge.id == selected_ids[*index])
.is_some_and(|edge| edge.start_vertex_id != edge.end_vertex_id)
})
.collect();
let open_ids: Vec<u64> = open.iter().map(|index| selected_ids[*index]).collect();
let mates = crate::blend::selection_mates(solid, &open_ids, radius).ok()?;
let convex = crate::blend::selection_convexity(solid, &open_ids, radius)?;
let mut runouts = Vec::new();
let mut split = false;
for (slot, &index) in open.iter().enumerate() {
if !convex[slot] {
continue;
}
let (edge, spine_faces) = (mates[slot].0, mates[slot].1.map(|face| face.id));
enum End {
Runout(u64),
Free,
Star,
Other,
}
let mut ends = Vec::with_capacity(2);
for vertex in [edge.start_vertex_id, edge.end_vertex_id] {
let around = edges_at(solid, vertex);
let selected_here: Vec<usize> = around
.iter()
.filter_map(|id| open_ids.iter().position(|open_id| open_id == id))
.filter(|other| *other != slot)
.collect();
if selected_here.is_empty() && around.iter().all(|id| !selected_ids.contains(id) || *id == edge.id) {
ends.push((vertex, End::Free));
continue;
}
if !selected_here.is_empty()
&& selected_here.iter().all(|other| convex[*other])
&& around.iter().all(|id| !selected_ids.contains(id) || open_ids.contains(id))
{
ends.push((vertex, End::Star));
continue;
}
if around.len() != 3 || selected_here.len() != 2 || selected_here.iter().any(|other| convex[*other]) {
ends.push((vertex, End::Other));
continue;
}
let [a, b] = [mates[selected_here[0]].1.map(|face| face.id), mates[selected_here[1]].1.map(|face| face.id)];
let shared: Vec<u64> = a.iter().copied().filter(|id| b.contains(id)).collect();
match shared[..] {
[floor] if !spine_faces.contains(&floor) => ends.push((vertex, End::Runout(floor))),
_ => ends.push((vertex, End::Other)),
}
}
if !ends.iter().any(|(_, end)| matches!(end, End::Runout(_))) {
continue;
}
for (vertex, end) in ends {
match end {
End::Runout(floor) if is_planar(solid, floor) => return None,
End::Runout(floor) => {
let point = solid.vertices.iter().find(|candidate| candidate.id == vertex)?.point;
runouts.push(Runout { spine: index, vertex, point, floor });
}
End::Free => {}
End::Star => split = true,
End::Other => return None,
}
}
}
(!runouts.is_empty()).then_some((runouts, split))
}
fn bridge_point(input: &BrepSolid, staged: &BrepSolid, runout: &Runout) -> Option<Vec3> {
let old_faces: Vec<u64> = input.shells.iter().flat_map(|shell| &shell.faces).map(|face| face.id).collect();
let faces_of = |edge_id: u64| -> Vec<u64> {
staged
.shells
.iter()
.flat_map(|shell| &shell.faces)
.filter(|face| face.loops.iter().any(|lp| lp.coedges.iter().any(|coedge| coedge.edge_id == edge_id)))
.map(|face| face.id)
.collect()
};
let mut best: Option<(f64, Vec3)> = None;
for edge in staged.edges.iter().filter(|edge| !edge.degenerate) {
let faces = faces_of(edge.id);
if faces.len() != 2 || !faces.contains(&runout.floor) || faces.iter().all(|face| old_faces.contains(face)) {
continue;
}
let Ok(middle) = edge.curve.evaluate(0.5 * (edge.t0 + edge.t1)) else {
continue;
};
let distance = middle.sub(runout.point).length();
if best.is_none_or(|(nearest, _)| distance < nearest) {
best = Some((distance, middle));
}
}
best.map(|(_, point)| point)
}
#[allow(clippy::too_many_arguments)]
pub(super) fn spine_first_composition(
solid: &BrepSolid,
selected_ids: &[u64],
edge_points: &[Vec3],
edge_names: Option<&[String]>,
radius: f64,
name: Option<&str>,
) -> Option<(BrepSolid, BlendSelection)> {
let debug = std::env::var("BREP_DEBUG_NETWORK").is_ok();
let (runouts, split) = detect(solid, selected_ids, radius)?;
let mut spines: Vec<usize> = runouts.iter().map(|runout| runout.spine).collect();
spines.sort_unstable();
spines.dedup();
if debug {
for runout in &runouts {
eprintln!(
"spine-first: spine #{} dies into the curved floor {} at vertex {} ({:.4}, {:.4}, {:.4})",
runout.spine, runout.floor, runout.vertex, runout.point.x, runout.point.y, runout.point.z
);
}
}
if edge_names.is_some_and(|names| names.len() < edge_points.len()) {
return None;
}
let name_of = |index: usize| edge_names.and_then(|names| names.get(index)).cloned().unwrap_or_default();
let pick_names = |indices: &[usize]| edge_names.map(|_| indices.iter().map(|index| name_of(*index)).collect::<Vec<_>>());
let spine_points: Vec<Vec3> = spines.iter().map(|index| edge_points[*index]).collect();
let spine_names = pick_names(&spines);
let staged = match fillet_edges_reported(solid, &spine_points, spine_names.as_deref(), radius, false, name) {
Ok((staged, selection)) if selection.is_complete() => staged,
Ok(_) | Err(_) => {
if debug {
eprintln!("spine-first: the spines did not build alone; the selection takes the other lanes");
}
return None;
}
};
let rest: Vec<usize> = (0..edge_points.len()).filter(|index| !spines.contains(index)).collect();
let mut points: Vec<Vec3> = rest
.iter()
.map(|index| {
staged
.edges
.iter()
.find(|edge| edge.id == selected_ids[*index] && !edge.degenerate)
.and_then(|edge| edge.curve.evaluate(0.5 * (edge.t0 + edge.t1)).ok())
.unwrap_or(edge_points[*index])
})
.collect();
let mut names = pick_names(&rest);
for runout in &runouts {
let Some(bridge) = bridge_point(solid, &staged, runout) else {
if debug {
eprintln!("spine-first: no bridge edge on the floor {} by vertex {}", runout.floor, runout.vertex);
}
return None;
};
points.push(bridge);
if let Some(names) = names.as_mut() {
names.push(format!("{}:FOOT", name_of(runout.spine)));
}
}
let result = if split {
split_rest(solid, &staged, selected_ids, edge_points, &rest, points, names, &name_of, edge_names.is_some(), radius, name, debug)?
} else {
build_stage(&staged, &points, names.as_deref(), radius, name, "the second stage", debug)?
};
let n = edge_points.len();
Some((result, BlendSelection { requested: n, applied: (0..n).collect(), rejected: Vec::new(), reason: None }))
}
fn build_stage(
body: &BrepSolid,
points: &[Vec3],
names: Option<&[String]>,
radius: f64,
name: Option<&str>,
what: &str,
debug: bool,
) -> Option<BrepSolid> {
match fillet_edges_reported(body, points, names, radius, false, name) {
Ok((result, selection)) if selection.is_complete() => Some(result),
Ok((_, selection)) => {
if debug {
eprintln!("spine-first: {what} dropped {:?}", selection.rejected);
}
None
}
Err(error) => {
if debug {
eprintln!("spine-first: {what} refused: {error}");
}
None
}
}
}
#[allow(clippy::too_many_arguments)]
fn split_rest(
input: &BrepSolid,
staged: &BrepSolid,
selected_ids: &[u64],
edge_points: &[Vec3],
rest: &[usize],
points: Vec<Vec3>,
names: Option<Vec<String>>,
name_of: &dyn Fn(usize) -> String,
named: bool,
radius: f64,
name: Option<&str>,
debug: bool,
) -> Option<BrepSolid> {
let concave: Vec<bool> = rest
.iter()
.map(|index| {
let id = selected_ids[*index];
let closed = input.edges.iter().find(|edge| edge.id == id).is_none_or(|edge| edge.start_vertex_id == edge.end_vertex_id);
!closed && crate::blend::selection_convexity(input, &[id], radius).is_some_and(|convex| !convex[0])
})
.collect();
let (mut concave_points, mut concave_names) = (Vec::new(), Vec::new());
let (mut other_slots, mut other_names) = (Vec::new(), Vec::new());
for (slot, point) in points.iter().enumerate() {
let name = names.as_ref().map(|names| names[slot].clone()).unwrap_or_default();
if slot >= rest.len() || concave[slot] {
concave_points.push(*point);
concave_names.push(name);
} else {
other_slots.push(slot);
other_names.push(name);
}
}
let concave_staged = build_stage(staged, &concave_points, named.then_some(&concave_names[..]), radius, name, "the concave chain", debug)?;
let mut other_points: Vec<Vec3> = other_slots
.iter()
.map(|slot| {
concave_staged
.edges
.iter()
.find(|edge| edge.id == selected_ids[rest[*slot]] && !edge.degenerate)
.and_then(|edge| edge.curve.evaluate(0.5 * (edge.t0 + edge.t1)).ok())
.unwrap_or(edge_points[rest[*slot]])
})
.collect();
for (point, source) in smooth_runs(input, &concave_staged, &other_points) {
other_points.push(point);
other_names.push(format!("{}:RUN", name_of(rest[other_slots[source]])));
}
if debug {
eprintln!("spine-first: the last stage takes {} edges ({} added runs)", other_points.len(), other_points.len() - other_slots.len());
}
let selected_here: Vec<u64> = other_points[..other_slots.len()]
.iter()
.filter_map(|point| super::edges::resolve_edge_by_point(&concave_staged, *point).ok())
.collect();
let _misfit_off = crate::blend::relief::first_misfit(&concave_staged, &selected_here, radius)
.is_none()
.then(crate::blend::relief::MisfitOff::enter);
build_stage(&concave_staged, &other_points, named.then_some(&other_names[..]), radius, name, "the last stage", debug)
}
fn smooth_runs(input: &BrepSolid, body: &BrepSolid, picked: &[Vec3]) -> Vec<(Vec3, usize)> {
let old: Vec<u64> = input.edges.iter().map(|edge| edge.id).collect();
let mut run: Vec<(u64, usize)> = picked
.iter()
.enumerate()
.filter_map(|(slot, point)| super::edges::resolve_edge_by_point(body, *point).ok().map(|id| (id, slot)))
.collect();
let leaving = |edge: &crate::topology::EdgeRecord, vertex: u64| -> Option<Vec3> {
let at_start = edge.start_vertex_id == vertex;
let tangent = edge.curve.unit_tangent(if at_start { edge.t0 } else { edge.t1 }, edge.t0, edge.t1).ok()?;
Some(if at_start { tangent } else { tangent.scale(-1.0) })
};
let creased = |edge: &crate::topology::EdgeRecord| -> bool {
let Ok(middle) = edge.curve.evaluate(0.5 * (edge.t0 + edge.t1)) else {
return false;
};
let normals: Vec<Vec3> = body
.shells
.iter()
.flat_map(|shell| &shell.faces)
.filter(|face| face.loops.iter().any(|lp| lp.coedges.iter().any(|coedge| coedge.edge_id == edge.id)))
.filter_map(|face| {
let uv = crate::project_point_to_surface(&face.surface, middle).ok()?;
let normal = face.surface.normal(uv.u, uv.v).ok()?;
Some(if face.same_sense { normal } else { normal.scale(-1.0) })
})
.collect();
normals.len() == 2 && normals[0].dot(normals[1]) < 1.0 - 1e-6
};
let mut added = Vec::new();
loop {
let mut grew = false;
for at in 0..run.len() {
let (id, source) = run[at];
let Some(edge) = body.edges.iter().find(|edge| edge.id == id) else {
continue;
};
for vertex in [edge.start_vertex_id, edge.end_vertex_id] {
let Some(own) = leaving(edge, vertex) else {
continue;
};
let candidates: Vec<&crate::topology::EdgeRecord> = body
.edges
.iter()
.filter(|other| {
!other.degenerate
&& other.start_vertex_id != other.end_vertex_id
&& (other.start_vertex_id == vertex || other.end_vertex_id == vertex)
&& !old.contains(&other.id)
&& !run.iter().any(|(taken, _)| *taken == other.id)
})
.collect();
for other in candidates {
let smooth = leaving(other, vertex).is_some_and(|theirs| own.dot(theirs) <= -(1.0 - 1e-6));
if smooth && creased(other) {
if let Ok(middle) = other.curve.evaluate(0.5 * (other.t0 + other.t1)) {
run.push((other.id, source));
added.push((middle, source));
grew = true;
}
}
}
}
}
if !grew {
break;
}
}
added
}