use super::*;
pub fn parallel_extrusion_sections(a: &ExtrusionStructure, b: &ExtrusionStructure, tolerance: f64) -> Option<Vec<NurbsCurve>> {
if !tolerance.is_finite() || tolerance <= 0.0 { return None; }
let axis = a.direction.normalized().ok()?;
let other = b.direction.normalized().ok()?;
if axis.cross(other).length() > 32.0*f64::EPSILON { return None; }
let drift = |e: &ExtrusionStructure| -> Option<f64> {
let length = e.sweep_domain[1]-e.sweep_domain[0];
let value = e.direction.sub(axis.scale(e.direction.dot(axis))).length()*length;
if !length.is_finite() || length <= 0.0 || !value.is_finite() { return None; }
Some(value)
};
let drift_a = drift(a)?;
let drift_b = drift(b)?;
if drift_a+drift_b > tolerance { return None; }
let origin = a.profile.control_points.first()?.point().ok()?;
let projected = |curve: &NurbsCurve| -> Option<NurbsCurve> {
let controls = curve.control_points.iter().map(|p| {
let delta = p.point().ok()?.sub(origin);
let q = delta.sub(axis.scale(delta.dot(axis)));
Some(crate::Vec4{x:q.x*p.w,y:q.y*p.w,z:q.z*p.w,w:p.w})
}).collect::<Option<Vec<_>>>()?;
NurbsCurve::new(curve.degree,curve.knots.clone(),controls).ok()
};
let pa = projected(&a.profile)?; let pb = projected(&b.profile)?;
let hits = match crate::curve_curve_intersection::intersect_curves_exact_only(&pa,&pb,tolerance) {
Some(hits) => {
if hits.iter().any(|h|h.tangential) { return None; }
hits
}
None if spline_crossings_enabled() => {
let mut hits = crate::curve_curve_intersection::intersect_curves(&pa,&pb,tolerance).ok()?;
hits.sort_by(|x,y| x.s.total_cmp(&y.s));
if hits.iter().any(|h|h.tangential) && !crossings_alternate(&pa,&pb,axis,&hits,tolerance) { return None; }
hits
}
None => return None,
};
if hits.is_empty() { return None; }
let mut curves = Vec::new();
for hit in hits {
let p = a.profile.evaluate(hit.s).ok()?;
let q = b.profile.evaluate(hit.t).ok()?;
let relative = q.sub(p);
let residual = relative.sub(axis.scale(relative.dot(axis))).length();
if !residual.is_finite() || residual+drift_a+drift_b>tolerance { return None; }
let a_end = a.direction.dot(axis)*(a.sweep_domain[1]-a.sweep_domain[0]);
let b_start = relative.dot(axis);
let b_end = b_start+b.direction.dot(axis)*(b.sweep_domain[1]-b.sweep_domain[0]);
let low = 0.0_f64.max(b_start.min(b_end));
let high = a_end.min(b_start.max(b_end));
if high < low-tolerance { continue; }
if high-low <= tolerance { return None; }
curves.push(crate::make_line(p.add(axis.scale(low)),p.add(axis.scale(high))).ok()?);
}
Some(curves)
}
fn spline_crossings_enabled() -> bool {
crate::switch!("BREP_EXTRUSION_SPLINE_CROSSINGS", false)
}
fn crossings_alternate(a: &NurbsCurve, b: &NurbsCurve, axis: Vec3, hits: &[crate::curve_curve_intersection::CurveCurveIntersection], tolerance: f64) -> bool {
let (Ok([s0, s1]), Ok([t0, t1])) = (a.domain(), b.domain()) else { return false };
let closed = |c: &NurbsCurve, lo: f64, hi: f64| match (c.evaluate(lo), c.evaluate(hi)) {
(Ok(p), Ok(q)) => p.sub(q).length() <= tolerance,
_ => false,
};
let (a_closed, b_closed) = (closed(a, s0, s1), closed(b, t0, t1));
let side = |s: f64, mut t: f64| -> Option<f64> {
let p = a.evaluate(s).ok()?;
for _ in 0..32 {
let (q, d) = b.deriv1(t).ok()?;
let dd = d.dot(d);
if !(dd > 0.0) { return None; }
let step = p.sub(q).dot(d) / dd;
t += step;
if b_closed { t = t0 + (t - t0).rem_euclid(t1 - t0); } else { t = t.clamp(t0, t1); }
if step.abs() <= 1e-15 * (t1 - t0) { break; }
}
let (q, d) = b.deriv1(t).ok()?;
let gap = p.sub(q);
if gap.dot(d).abs() > 1e-6 * gap.length() * d.length() + tolerance * d.length() { return None; }
Some(d.normalized().ok()?.cross(gap).dot(axis))
};
let mut pairs: Vec<(usize, usize)> = (1..hits.len()).map(|i| (i - 1, i)).collect();
if a_closed && b_closed && hits.len() >= 2 { pairs.push((hits.len() - 1, 0)); }
if a_closed != b_closed || pairs.is_empty() { return false; }
let mut last: Option<f64> = None;
for (i, j) in pairs {
let (h, k) = (&hits[i], &hits[j]);
let span_s = if k.s > h.s { k.s - h.s } else { k.s + (s1 - s0) - h.s };
let mut span_t = k.t - h.t;
if b_closed { span_t = (span_t - t0 + (t1 - t0) * 0.5).rem_euclid(t1 - t0) - (t1 - t0) * 0.5 + t0; }
let wrap = |x: f64, lo: f64, hi: f64| if a_closed { lo + (x - lo).rem_euclid(hi - lo) } else { x };
let Some(value) = side(wrap(h.s + 0.5 * span_s, s0, s1), wrap(h.t + 0.5 * span_t, t0, t1)) else { return false };
if !(value.abs() > 10.0 * tolerance) { return false; }
if let Some(previous) = last {
if previous.signum() == value.signum() { return false; }
}
last = Some(value);
}
!(a_closed && hits.len() % 2 == 1)
}