use super::*;
use super::edges::{per_edge_name, refuse_invalid_result, resolve_edge_by_point};
use crate::{NurbsSurface, Vec4};
fn vanishing_cone_on() -> bool {
switch!("BREP_FILLET_VANISHING_CONE", true)
}
pub(super) fn vanishing_round(
solid: &BrepSolid,
edge_points: &[Vec3],
edge_names: Option<&[String]>,
law: &crate::law::RadiusLaw,
chamfer: bool,
name: Option<&str>,
entry: &str,
) -> Option<Result<BrepSolid, KernelRefusal>> {
if !vanishing_cone_on() || edge_points.len() != 1 {
return None;
}
let (r0, r1) = law.linear_ends()?;
let full = r0.max(r1);
if !((r0 == 0.0) != (r1 == 0.0)) || !(full.is_finite() && full > 0.0) {
return None;
}
let edge_id = resolve_edge_by_point(solid, edge_points[0]).ok()?;
let (_, faces) = super::analyze::edge_and_faces(solid, edge_id).ok()?;
if faces.len() != 2 || !faces.iter().all(|face| matches!(face.surface.analytic(), Some(crate::AnalyticSurface::Plane { .. }))) {
return None;
}
let cross = analyze_edge(solid, edge_id, full).ok()?;
let EdgePath::Straight { direction, length } = cross.path else { return None };
if !cross.convex {
return None;
}
let forward = super::chamfer_extent::check_ball_extent(solid, edge_id, &|f| law.radius_at_fraction(f), entry);
let backward = super::chamfer_extent::check_ball_extent(solid, edge_id, &|f| law.radius_at_fraction(1.0 - f), entry);
if let (Err(too_large), Err(_)) = (forward, backward) {
return Some(Err(too_large));
}
Some(build(solid, &cross, direction, length, r0 == 0.0, full, chamfer, per_edge_name(edge_names, name, 0), entry))
}
#[allow(clippy::too_many_arguments)]
fn build(
solid: &BrepSolid,
cross: &EdgeCross,
direction: Vec3,
length: f64,
vanishes_at_start: bool,
radius: f64,
chamfer: bool,
name: Option<&str>,
entry: &str,
) -> Result<BrepSolid, KernelRefusal> {
let far = cross.start.add(direction.scale(length));
let (apex, base) = if vanishes_at_start { (cross.start, far) } else { (far, cross.start) };
let at_base = EdgeCross { start: base, path: EdgePath::Straight { direction, length }, into_first: cross.into_first, into_second: cross.into_second, convex: cross.convex };
let section = super::tool::cross_section_profile(&at_base, radius, chamfer)?;
let k = 1.0 + radius / length;
let scaled: Vec<NurbsCurve> = section
.iter()
.map(|curve| {
let mut out = curve.clone();
for cp in out.control_points.iter_mut() {
cp.x = apex.x * cp.w + k * (cp.x - apex.x * cp.w);
cp.y = apex.y * cp.w + k * (cp.y - apex.y * cp.w);
cp.z = apex.z * cp.w + k * (cp.z - apex.z * cp.w);
}
out
})
.collect();
let fragment = |message: String| KernelRefusal::internal(KernelStage::Fragment, "vanishing_cone", format!("{entry}: {message}"));
let wall = super::tool::scaffold_tag(name);
let named = |mut sheet: BrepSolid, round: bool| {
for face in sheet.shells.iter_mut().flat_map(|shell| shell.faces.iter_mut()) {
face.name = if round { name.map(str::to_string) } else { Some(wall.clone()) };
}
sheet
};
let mut sheets = Vec::with_capacity(scaled.len() + 1);
for (index, curve) in scaled.iter().enumerate() {
let rows = curve.control_points.iter().map(|cp| vec![*cp, Vec4 { x: apex.x * cp.w, y: apex.y * cp.w, z: apex.z * cp.w, w: cp.w }]).collect();
let surface = NurbsSurface::new(curve.degree, 1, curve.knots.clone(), vec![0.0, 0.0, 1.0, 1.0], rows).map_err(fragment)?;
sheets.push(named(crate::face_from_surface(&surface)?, index == 1));
}
let cap_origin = apex.add(base.sub(apex).scale(k));
let u = cross.into_first.sub(direction.scale(cross.into_first.dot(direction))).normalized().map_err(|_| fragment("the section's plane".into()))?;
let v = direction.cross(u);
let reach = 4.0 * k * radius / (1.0 - cross.into_first.dot(cross.into_second).abs()).max(1e-3).sqrt() + 1.0;
let plane = crate::make_plane(cap_origin.sub(u.scale(reach)).sub(v.scale(reach)), u, v, 2.0 * reach, 2.0 * reach).map_err(fragment)?;
sheets.push(named(crate::face_from_surface_trimmed(&plane, &[scaled.clone()])?, false));
let tool = crate::solid_from_sheets(&sheets, (1e-8 * (length + radius)).max(1e-7))?;
let result = super::tool::apply_tool(solid, &tool, cross.convex).map(super::tool::settle_cutter_scaffold)?;
refuse_invalid_result(&result, entry)?;
Ok(result)
}