use super::*;
pub(in crate::blend) fn enabled() -> bool {
switch!("BREP_BLEND_STATIONARY_BALL", false)
}
fn unit_normal(surface: &NurbsSurface, point: Vec3) -> Option<(Vec3, Vec3)> {
let foot = crate::project_point_to_surface(surface, point).ok()?;
let n = raw_normal(surface, foot.u, foot.v).ok()?.normalized().ok()?;
Some((foot.point, n))
}
pub(in crate::blend) fn is_still(stations: &[Station]) -> bool {
let bits = |p: Vec3| [p.x.to_bits(), p.y.to_bits(), p.z.to_bits()];
stations.len() >= 2 && stations.iter().all(|station| bits(station.center) == bits(stations[0].center))
}
pub(in crate::blend) fn reading_window(stations: &[Station], station_ts: &[f64], parameters: &[f64], edge: &EdgeRecord) -> Option<[f64; 2]> {
if !enabled() || !is_still(stations) || station_ts.len() != parameters.len() {
return None;
}
let first = station_ts.iter().position(|&t| t >= edge.t0)?;
let last = station_ts.iter().rposition(|&t| t <= edge.t1)?;
(first < last).then(|| crate::blend::edge::shipped_window(parameters, parameters[first], parameters[last])).flatten()
}
pub(in crate::blend) fn construct(
edge: &EdgeRecord,
first: &BlendMate,
second: &BlendMate,
radius: f64,
overshoot: f64,
ends: [f64; 2],
station_count: usize,
) -> Option<(Vec<Station>, [usize; 2], Vec<f64>)> {
if !enabled() || !(radius.is_finite() && radius > 0.0) || first.rho.abs() != radius || second.rho.abs() != radius {
return None;
}
let bar = 1e-9 * (1.0 + radius);
let span = edge.t1 - edge.t0;
let at = |t: f64| edge.curve.evaluate(t).ok();
let collapsed = |mate: &BlendMate| -> Option<(Vec3, Vec3)> {
let surface = &mate.face.surface;
let [u0, u1] = surface.domain_u().ok()?;
let [v0, v1] = surface.domain_v().ok()?;
let mut offsets = Vec::with_capacity(25);
for i in 0..=4 {
for j in 0..=4 {
let (u, v) = (u0 + (u1 - u0) * i as f64 / 4.0, v0 + (v1 - v0) * j as f64 / 4.0);
let p = surface.evaluate(u, v).ok()?;
let n = raw_normal(surface, u, v).ok()?.normalized().ok()?;
offsets.push(p.add(n.scale(mate.rho)));
}
}
for k in 0..=8 {
let p = at(edge.t0 + span * k as f64 / 8.0)?;
let (foot, n) = unit_normal(surface, p)?;
offsets.push(foot.add(n.scale(mate.rho)));
}
let a = offsets[0];
let far = offsets.iter().copied().max_by(|x, y| x.sub(a).length().total_cmp(&y.sub(a).length()))?;
if !(far.sub(a).length() > 1e3 * bar) {
return None;
}
let direction = far.sub(a).normalized().ok()?;
let off_line = |q: Vec3| { let d = q.sub(a); d.sub(direction.scale(d.dot(direction))).length() };
offsets.iter().all(|&q| off_line(q) <= bar).then_some((a, direction))
};
let (round, other, (origin, axis)) = if let Some(line) = collapsed(first) {
(first, second, line)
} else if let Some(line) = collapsed(second) {
(second, first, line)
} else {
return None;
};
let residual = |s: f64| -> Option<f64> {
let c = origin.add(axis.scale(s));
let (foot, n) = unit_normal(&other.face.surface, c)?;
Some(c.sub(foot.add(n.scale(other.rho))).dot(axis))
};
let middle = at(edge.t0 + 0.5 * span)?.sub(origin).dot(axis);
let (mut s0, mut s1) = (middle, middle + radius * 0.1);
let (mut f0, mut f1) = (residual(s0)?, residual(s1)?);
for _ in 0..60 {
if f1.abs() <= 1e-14 * (1.0 + radius) || (f1 - f0).abs() <= 1e-300 {
break;
}
let s2 = s1 - f1 * (s1 - s0) / (f1 - f0);
(s0, f0) = (s1, f1);
s1 = s2;
f1 = residual(s1)?;
}
let centre = origin.add(axis.scale(s1));
let (wall_foot, wall_normal) = unit_normal(&other.face.surface, centre)?;
if !(centre.sub(wall_foot.add(wall_normal.scale(other.rho))).length() <= bar) {
return None;
}
for k in 0..=8 {
if !(at(edge.t0 + span * k as f64 / 8.0)?.sub(centre).length() <= 2.0 * radius) {
return None;
}
}
let w = wall_foot;
let w_uv = { let f = crate::project_point_to_surface(&other.face.surface, w).ok()?; [f.u, f.v] };
let (station_ts, vertex_indices) = crate::blend::edge::open_station_parameters(edge, overshoot, ends, station_count.max(16), [false, false]);
if station_ts.len() < 3 || station_ts.windows(2).any(|p| !(p[1] > p[0])) {
return None;
}
let radial_at = |t: f64| -> Option<Vec3> {
let d = at(t)?.sub(origin);
d.sub(axis.scale(d.dot(axis))).normalized().ok()
};
const SAMPLES: usize = 256;
let r0 = radial_at(edge.t0)?;
let side = axis.cross(r0);
let raw_angle = |t: f64| -> Option<f64> { let r = radial_at(t)?; Some(r.dot(side).atan2(r.dot(r0))) };
let mut unwrapped = Vec::with_capacity(SAMPLES + 1);
unwrapped.push(0.0f64);
for k in 1..=SAMPLES {
let angle = raw_angle(edge.t0 + span * k as f64 / SAMPLES as f64)?;
let previous = unwrapped[k - 1];
unwrapped.push(angle + std::f64::consts::TAU * ((previous - angle) / std::f64::consts::TAU).round());
}
let turn = unwrapped[SAMPLES];
if !(turn.abs() > 1e-9) || unwrapped.windows(2).any(|p| !((p[1] - p[0]) * turn > 0.0)) {
return None;
}
let step = span / SAMPLES as f64;
let rate = turn / span;
let angle_at = |t: f64| -> Option<f64> {
if t <= edge.t0 {
return Some(rate * (t - edge.t0));
}
if t >= edge.t1 {
return Some(turn + rate * (t - edge.t1));
}
let near = unwrapped[(((t - edge.t0) / step).round() as usize).min(SAMPLES)];
let angle = raw_angle(t)?;
Some(angle + std::f64::consts::TAU * ((near - angle) / std::f64::consts::TAU).round())
};
let contact_bar = 1e-6 * (1.0 + radius);
let mut stations = Vec::with_capacity(station_ts.len());
for &t in &station_ts {
let angle = angle_at(t)?;
let radial = r0.scale(angle.cos()).add(side.scale(angle.sin()));
let p_round = centre.add(radial.scale(radius));
let round_uv = {
let f = crate::project_point_to_surface(&round.face.surface, p_round).ok()?;
let n = raw_normal(&round.face.surface, f.u, f.v).ok()?.normalized().ok()?;
let inside = t >= edge.t0 && t <= edge.t1;
if inside && !(f.point.sub(p_round).length() <= contact_bar && f.point.add(n.scale(round.rho)).sub(centre).length() <= contact_bar) {
return None;
}
[f.u, f.v]
};
let (d1, d2) = (p_round.sub(centre).scale(1.0 / radius), w.sub(centre).scale(1.0 / radius));
let cosine = d1.dot(d2);
if !(cosine > -1.0 + 1e-9) {
return None;
}
let weight = ((1.0 + cosine) * 0.5).sqrt();
let apex = centre.add(d1.add(d2).scale(radius / (1.0 + cosine)));
let (p1, uv1, p2, uv2) = if std::ptr::eq(round, first) { (p_round, round_uv, w, w_uv) } else { (w, w_uv, p_round, round_uv) };
stations.push(Station { uv1, uv2, p1, p2, center: centre, weight, apex });
}
let on_round = |t: f64| -> Option<bool> {
let angle = angle_at(t)?;
let p = centre.add(r0.scale(angle.cos()).add(side.scale(angle.sin())).scale(radius));
let f = crate::project_point_to_surface(&round.face.surface, p).ok()?;
let n = raw_normal(&round.face.surface, f.u, f.v).ok()?.normalized().ok()?;
Some(f.point.sub(p).length() <= contact_bar && f.point.add(n.scale(round.rho)).sub(centre).length() <= contact_bar)
};
let round_surface = &round.face.surface;
let crossed = |knots: &[f64], a: f64, b: f64| -> usize {
let (low, high) = (a.min(b), a.max(b));
let mut distinct: Vec<f64> = knots.iter().copied().filter(|&k| k > low && k < high).collect();
distinct.dedup();
distinct.len()
};
let round_uv_of = |station: &Station| if std::ptr::eq(round, first) { station.uv1 } else { station.uv2 };
for (pair, ends) in station_ts.windows(2).zip(stations.windows(2)) {
let (a, b) = (round_uv_of(&ends[0]), round_uv_of(&ends[1]));
let spans = 1 + crossed(&round_surface.knots_u, a[0], b[0]) + crossed(&round_surface.knots_v, a[1], b[1]);
let probes = 4 * spans;
for k in 1..probes {
let t = pair[0] + (pair[1] - pair[0]) * k as f64 / probes as f64;
if t > edge.t0 && t < edge.t1 && !on_round(t)? {
return None;
}
}
}
crate::blend::record_count("count.network.stationary_ball", 1);
Some((stations, vertex_indices, station_ts))
}