Skip to main content

brepkit_math/
analytic_intersection.rs

1//! Closed-form and semi-analytic intersections of analytic surfaces with planes.
2//!
3//! Provides specialized intersection algorithms for cylinder, cone, sphere,
4//! and torus surfaces with planes, as well as a general marching approach
5//! for analytic-analytic surface intersections.
6
7use std::f64::consts::{FRAC_PI_2, TAU};
8
9use crate::MathError;
10use crate::curves::{Circle3D, Ellipse3D};
11use crate::frame::Frame3;
12use crate::nurbs::fitting::interpolate;
13use crate::nurbs::intersection::{IntersectionCurve, IntersectionPoint};
14use crate::surfaces::{ConicalSurface, CylindricalSurface, SphericalSurface, ToroidalSurface};
15use crate::tolerance::Tolerance;
16use crate::vec::{Point3, Vec3};
17
18/// Exact curve type resulting from plane-analytic surface intersection.
19#[derive(Debug, Clone)]
20pub enum ExactIntersectionCurve {
21    /// A circle (plane perpendicular to axis of cylinder/cone/sphere).
22    Circle(Circle3D),
23    /// An ellipse (plane oblique to cylinder/cone axis).
24    Ellipse(Ellipse3D),
25    /// Fallback to sampled point chain (torus, degenerate cases).
26    Points(Vec<Point3>),
27}
28
29/// Compute exact intersection curves between a plane and an analytic surface.
30///
31/// Returns exact `Circle3D` or `Ellipse3D` where possible, falling back to
32/// sampled points for complex cases (torus).
33///
34/// The plane is defined by `dot(normal, p) = d`.
35///
36/// # Errors
37///
38/// Returns an error if the intersection computation fails.
39pub fn exact_plane_analytic(
40    surface: AnalyticSurface<'_>,
41    plane_normal: Vec3,
42    plane_d: f64,
43) -> Result<Vec<ExactIntersectionCurve>, MathError> {
44    match surface {
45        AnalyticSurface::Cylinder(cyl) => exact_plane_cylinder(cyl, plane_normal, plane_d),
46        AnalyticSurface::Sphere(sphere) => exact_plane_sphere(sphere, plane_normal, plane_d),
47        AnalyticSurface::Cone(cone) => exact_plane_cone(cone, plane_normal, plane_d),
48        AnalyticSurface::Torus(torus) => {
49            // Torus intersections are degree-4 — fall back to sampling.
50            let chains = sample_plane_torus(torus, plane_normal, plane_d)?;
51            Ok(chains
52                .into_iter()
53                .map(ExactIntersectionCurve::Points)
54                .collect())
55        }
56    }
57}
58
59/// Exact plane-cylinder intersection.
60///
61/// - Plane perpendicular to axis → `Circle3D`
62/// - Plane oblique to axis → `Ellipse3D`
63/// - Plane parallel to axis → `Points` fallback (0 or 2 lines)
64fn exact_plane_cylinder(
65    cyl: &CylindricalSurface,
66    normal: Vec3,
67    d: f64,
68) -> Result<Vec<ExactIntersectionCurve>, MathError> {
69    let axis = cyl.axis();
70    let cos_theta = normal.dot(axis).abs();
71    let r = cyl.radius();
72
73    if cos_theta < 1e-10 {
74        // Plane parallel to cylinder axis → 0 or 2 line segments.
75        // Fall back to sampled points.
76        let chains = sample_plane_cylinder(cyl, normal, d)?;
77        return Ok(chains
78            .into_iter()
79            .map(ExactIntersectionCurve::Points)
80            .collect());
81    }
82
83    // Find where axis intersects the plane: axis_point + t*axis, dot(normal, P) = d
84    // t = (d - dot(normal, origin)) / dot(normal, axis)
85    let n_dot_axis = normal.dot(axis);
86    let n_dot_origin = dot_np(normal, cyl.origin());
87    let t = (d - n_dot_origin) / n_dot_axis;
88    let center_on_axis = Point3::new(
89        cyl.origin().x() + t * axis.x(),
90        cyl.origin().y() + t * axis.y(),
91        cyl.origin().z() + t * axis.z(),
92    );
93
94    if cos_theta > 1.0 - 1e-10 {
95        // Plane perpendicular to axis → Circle
96        let circle = Circle3D::new(center_on_axis, normal, r)?;
97        Ok(vec![ExactIntersectionCurve::Circle(circle)])
98    } else {
99        // Oblique plane → Ellipse
100        // Semi-minor = r (the cylinder radius, unchanged)
101        // Semi-major = r / cos(θ) where θ = angle between plane normal and axis
102        let semi_minor = r;
103        let semi_major = r / cos_theta;
104
105        // The major axis direction lies in the intersection of the plane
106        // with the plane containing the axis and the plane normal.
107        // It's the projection of the axis onto the cutting plane, normalized.
108        let axis_proj = Vec3::new(
109            axis.x() - n_dot_axis * normal.x(),
110            axis.y() - n_dot_axis * normal.y(),
111            axis.z() - n_dot_axis * normal.z(),
112        );
113        let u_axis = axis_proj.normalize()?;
114        let v_axis = normal.cross(u_axis);
115
116        let ellipse = Ellipse3D::with_axes(
117            center_on_axis,
118            normal,
119            semi_major,
120            semi_minor,
121            u_axis,
122            v_axis,
123        )?;
124        Ok(vec![ExactIntersectionCurve::Ellipse(ellipse)])
125    }
126}
127
128/// Exact plane-sphere intersection.
129///
130/// Always produces a `Circle3D` (or empty if no intersection).
131fn exact_plane_sphere(
132    sphere: &SphericalSurface,
133    normal: Vec3,
134    d: f64,
135) -> Result<Vec<ExactIntersectionCurve>, MathError> {
136    let h = dot_np(normal, sphere.center()) - d;
137    let r = sphere.radius();
138
139    if h.abs() > r - 1e-10 {
140        return Ok(vec![]);
141    }
142
143    let circle_r = (r.mul_add(r, -(h * h))).sqrt();
144    let circle_center = Point3::new(
145        h.mul_add(-normal.x(), sphere.center().x()),
146        h.mul_add(-normal.y(), sphere.center().y()),
147        h.mul_add(-normal.z(), sphere.center().z()),
148    );
149
150    let circle = Circle3D::new(circle_center, normal, circle_r)?;
151    Ok(vec![ExactIntersectionCurve::Circle(circle)])
152}
153
154/// Exact plane-cone intersection.
155///
156/// The conic type is set by the cone's half-opening angle from the axis
157/// (`γ = π/2 − half_angle`) versus the plane-axis angle `ψ`:
158/// - Plane perpendicular to axis (`ψ = π/2`) → `Circle3D`
159/// - `ψ > γ` (ellipse) → closed-form `Ellipse3D`
160/// - `ψ ≤ γ` (parabola/hyperbola) → bounded single-branch `Points` (one chain
161///   per branch — a hyperbola's two nappes never share a chain)
162fn exact_plane_cone(
163    cone: &ConicalSurface,
164    normal: Vec3,
165    d: f64,
166) -> Result<Vec<ExactIntersectionCurve>, MathError> {
167    let axis = cone.axis();
168    let cos_theta = normal.dot(axis).abs();
169    let half_angle = cone.half_angle();
170
171    if cos_theta > 1.0 - 1e-10 {
172        // Plane perpendicular to axis → Circle
173        // Find where axis meets the plane
174        let n_dot_axis = normal.dot(axis);
175        let n_dot_apex = dot_np(normal, cone.apex());
176        let t = (d - n_dot_apex) / n_dot_axis;
177
178        // t is the signed distance from apex to plane along the axis.
179        // The real cone is a single nappe; the perpendicular-plane section is a
180        // circle whose radius follows from the axial offset |t|.
181        // |t| ≈ 0 means the plane passes through the apex → degenerate point.
182        if t.abs() < 1e-10 {
183            return Ok(vec![]);
184        }
185
186        let center = Point3::new(
187            cone.apex().x() + t * axis.x(),
188            cone.apex().y() + t * axis.y(),
189            cone.apex().z() + t * axis.z(),
190        );
191        // half_angle is the angle from the radial plane to the surface.
192        // Axial distance t = v * sin(half_angle), so v = t / sin(half_angle).
193        // Radius at v = v * cos(half_angle) = t * cos(half_angle) / sin(half_angle).
194        let circle_r = t.abs() * half_angle.cos() / half_angle.sin();
195        if circle_r < 1e-15 {
196            return Ok(vec![]);
197        }
198
199        let circle = Circle3D::new(center, normal, circle_r)?;
200        return Ok(vec![ExactIntersectionCurve::Circle(circle)]);
201    }
202
203    // Oblique plane. Classify the conic in the plane-aligned frame.
204    //
205    // Decompose the (unit) axis as a = c·n + p·e1, where c = n·a, e1 is the unit
206    // in-plane projection of the axis, and p = |projection| = sqrt(1−c²). Write a
207    // point Q on the plane as Q = apex + e·n + s·e1 + t·e2 (e = d − n·apex,
208    // e2 = n×e1). The cone equation (w·a)² = cos²γ·(w·w) with k = cos²γ =
209    // sin²(half_angle) reduces to (no s·t cross term, since e1/e2 align with the
210    // conic axes):
211    //     (p²−k)·s² + 2ecp·s + e²(c²−k) = k·t²
212    // The s² coefficient A = p²−k = sin²θ − sin²(half_angle) sets the type:
213    // A < 0 → ellipse, A = 0 → parabola, A > 0 → hyperbola.
214    let c = normal.dot(axis);
215    let p2 = (1.0 - c * c).max(0.0);
216    let p = p2.sqrt();
217    let k = half_angle.sin().powi(2);
218    let a_coeff = p2 - k;
219
220    // Build the plane-aligned frame e1 (in-plane axis projection), e2 = n×e1.
221    let m = Vec3::new(
222        axis.x() - c * normal.x(),
223        axis.y() - c * normal.y(),
224        axis.z() - c * normal.z(),
225    );
226    let m_len = m.length();
227    if m_len < 1e-12 {
228        // Axis parallel to normal — handled by the perpendicular branch above;
229        // fall back to sampling for safety.
230        let chains = sample_plane_cone(cone, normal, d)?;
231        return Ok(chains
232            .into_iter()
233            .map(ExactIntersectionCurve::Points)
234            .collect());
235    }
236    let e1 = m * (1.0 / m_len);
237    let e2 = normal.cross(e1);
238    let apex = cone.apex();
239    let e = d - dot_np(normal, apex);
240
241    // Ellipse → closed form. A = p²−k < 0 with a margin to keep the
242    // near-parabolic regime on the robust sampled path.
243    if a_coeff < -1e-9 {
244        let abs_a = -a_coeff; // = k − p² > 0
245        // Real-nappe guard: in the ellipse regime n·g(u) keeps constant sign(c),
246        // so v = e/(n·g) ≥ 0 only when e and c share a sign. When e·c < 0 the
247        // plane is offset to the far side of the apex from the cone's opening —
248        // the section lies entirely on the phantom nappe, so there is no real
249        // curve (RHS below is positive regardless of sign, so it can't catch this).
250        if e * c < 0.0 {
251            return Ok(vec![]);
252        }
253        // |A|(s − s_c)² + k·t² = RHS, with s_c = ecp/|A| and
254        // RHS = e²·k·(1−k)/|A| (always > 0 for a real ellipse).
255        let s_c = e * c * p / abs_a;
256        let rhs = e * e * k * (1.0 - k) / abs_a;
257        if rhs <= 0.0 {
258            return Ok(vec![]);
259        }
260        let semi_s = (rhs / abs_a).sqrt(); // extent along e1
261        let semi_t = (rhs / k).sqrt(); // extent along e2
262        if semi_s < 1e-12 || semi_t < 1e-12 {
263            return Ok(vec![]);
264        }
265        let center = apex + normal * e + e1 * s_c;
266        let (semi_major, semi_minor, u_axis, v_axis) = if semi_s >= semi_t {
267            (semi_s, semi_t, e1, e2)
268        } else {
269            (semi_t, semi_s, e2, e1)
270        };
271        let ellipse = Ellipse3D::with_axes(center, normal, semi_major, semi_minor, u_axis, v_axis)?;
272        return Ok(vec![ExactIntersectionCurve::Ellipse(ellipse)]);
273    }
274
275    // Parabola / hyperbola (and the near-parabolic ellipse margin): the section
276    // is unbounded, so emit bounded, branch-separated sample chains.
277    let chains = sample_plane_cone(cone, normal, d)?;
278    Ok(chains
279        .into_iter()
280        .map(ExactIntersectionCurve::Points)
281        .collect())
282}
283
284/// Reference to an analytic surface for intersection dispatch.
285#[derive(Clone, Copy)]
286pub enum AnalyticSurface<'a> {
287    /// Cylindrical surface reference.
288    Cylinder(&'a CylindricalSurface),
289    /// Conical surface reference.
290    Cone(&'a ConicalSurface),
291    /// Spherical surface reference.
292    Sphere(&'a SphericalSurface),
293    /// Toroidal surface reference.
294    Torus(&'a ToroidalSurface),
295}
296
297/// Compute `n . p` treating a `Point3` as a position vector.
298fn dot_np(n: Vec3, p: Point3) -> f64 {
299    n.dot(Vec3::new(p.x(), p.y(), p.z()))
300}
301
302/// Intersect a plane with an analytic surface.
303///
304/// The plane is defined by `dot(normal, p) = d`.
305///
306/// # Errors
307///
308/// Returns an error if the intersection computation fails.
309pub fn intersect_plane_analytic(
310    surface: AnalyticSurface<'_>,
311    normal: Vec3,
312    d: f64,
313) -> Result<Vec<IntersectionCurve>, MathError> {
314    match surface {
315        AnalyticSurface::Cylinder(cyl) => intersect_plane_cylinder(cyl, normal, d),
316        AnalyticSurface::Cone(cone) => intersect_plane_cone(cone, normal, d),
317        AnalyticSurface::Sphere(sphere) => intersect_plane_sphere(sphere, normal, d),
318        AnalyticSurface::Torus(torus) => intersect_plane_torus(torus, normal, d),
319    }
320}
321
322/// Sample points on the plane-analytic intersection without NURBS curve fitting.
323///
324/// Returns chains of ordered 3D sample points. Each chain is one connected
325/// component of the intersection curve. This is much faster than
326/// `intersect_plane_analytic` when only sample points are needed (e.g. for
327/// boolean intersection segment generation).
328///
329/// # Errors
330///
331/// Returns an error if the intersection computation fails.
332pub fn sample_plane_analytic(
333    surface: AnalyticSurface<'_>,
334    normal: Vec3,
335    d: f64,
336) -> Result<Vec<Vec<Point3>>, MathError> {
337    match surface {
338        AnalyticSurface::Cylinder(cyl) => sample_plane_cylinder(cyl, normal, d),
339        AnalyticSurface::Cone(cone) => sample_plane_cone(cone, normal, d),
340        AnalyticSurface::Sphere(sphere) => sample_plane_sphere(sphere, normal, d),
341        AnalyticSurface::Torus(torus) => sample_plane_torus(torus, normal, d),
342    }
343}
344
345/// Sample the plane-cylinder intersection as ordered 3D points.
346#[allow(clippy::cast_precision_loss, clippy::unnecessary_wraps)]
347fn sample_plane_cylinder(
348    cyl: &CylindricalSurface,
349    normal: Vec3,
350    d: f64,
351) -> Result<Vec<Vec<Point3>>, MathError> {
352    let n_samples = 64_usize;
353    let mut points = Vec::with_capacity(n_samples + 1);
354
355    for i in 0..=n_samples {
356        let u = TAU * (i as f64) / (n_samples as f64);
357        let base = cyl.evaluate(u, 0.0);
358        let n_dot_axis = normal.dot(cyl.axis());
359        let n_dot_base = dot_np(normal, base);
360
361        if n_dot_axis.abs() < 1e-12 {
362            if (n_dot_base - d).abs() < 1e-6 {
363                points.push(base);
364            }
365        } else {
366            let v = (d - n_dot_base) / n_dot_axis;
367            if v.abs() <= 100.0 {
368                points.push(cyl.evaluate(u, v));
369            }
370        }
371    }
372
373    if points.len() < 2 {
374        Ok(vec![])
375    } else {
376        Ok(vec![points])
377    }
378}
379
380/// Sample the plane-sphere intersection as ordered 3D points.
381#[allow(clippy::cast_precision_loss)]
382fn sample_plane_sphere(
383    sphere: &SphericalSurface,
384    normal: Vec3,
385    d: f64,
386) -> Result<Vec<Vec<Point3>>, MathError> {
387    let h = dot_np(normal, sphere.center()) - d;
388    let r = sphere.radius();
389
390    if h.abs() > r - 1e-10 {
391        return Ok(vec![]);
392    }
393
394    let circle_r = (r.mul_add(r, -(h * h))).sqrt();
395    let circle_center = Point3::new(
396        h.mul_add(-normal.x(), sphere.center().x()),
397        h.mul_add(-normal.y(), sphere.center().y()),
398        h.mul_add(-normal.z(), sphere.center().z()),
399    );
400
401    let basis = Frame3::from_normal(circle_center, normal)?;
402    let u_dir = basis.x;
403    let v_dir = basis.y;
404
405    let n_samples = 64_usize;
406    let mut points = Vec::with_capacity(n_samples + 1);
407
408    for i in 0..=n_samples {
409        let theta = TAU * (i as f64) / (n_samples as f64);
410        let (sin_t, cos_t) = theta.sin_cos();
411        points.push(circle_center + u_dir * (circle_r * cos_t) + v_dir * (circle_r * sin_t));
412    }
413
414    Ok(vec![points])
415}
416
417/// Sample the plane-cone intersection as ordered 3D points.
418///
419/// The cone is the single real nappe `v >= 0` of `P(u,v) = apex + v·g(u)`.
420/// Along each generator `g(u)` the plane `n·P = d` is linear in `v`, so
421/// `v = (d − n·apex) / (n·g(u))`. We keep only `v >= 0` (the phantom `v < 0`
422/// nappe is geometrically absent) and `v` below a finite bound (near an
423/// asymptote `n·g(u) → 0` so `v → ∞` — those points run off the surface and
424/// must be excluded). The angular samples that survive form one contiguous arc
425/// (ellipse) or two (parabola/hyperbola, one per branch); each contiguous run
426/// is returned as a separate ordered chain so the consumer never stitches two
427/// disjoint branches into one curve.
428#[allow(clippy::cast_precision_loss, clippy::unnecessary_wraps)]
429fn sample_plane_cone(
430    cone: &ConicalSurface,
431    normal: Vec3,
432    d: f64,
433) -> Result<Vec<Vec<Point3>>, MathError> {
434    let apex = cone.apex();
435    let n_dot_apex = dot_np(normal, apex);
436    let e = d - n_dot_apex;
437
438    // Per-generator solve: along g(u) the plane is linear in v, v = e / (n·g(u)).
439    // Sample u densely; keep only the real nappe (v >= 0) and skip near-asymptote
440    // generators (n·g(u) ≈ 0 → v → ∞).
441    let n_samples = 512_usize;
442    let mut vs: Vec<Option<f64>> = Vec::with_capacity(n_samples);
443    let mut v_min = f64::INFINITY;
444    for i in 0..n_samples {
445        let u = TAU * (i as f64) / (n_samples as f64);
446        let g = cone.evaluate(u, 1.0) - apex;
447        let n_dot_g = normal.dot(Vec3::new(g.x(), g.y(), g.z()));
448        if n_dot_g.abs() < 1e-12 {
449            vs.push(None);
450            continue;
451        }
452        let v = e / n_dot_g;
453        if v >= -1e-12 {
454            let v = v.max(0.0);
455            v_min = v_min.min(v);
456            vs.push(Some(v));
457        } else {
458            vs.push(None);
459        }
460    }
461
462    if !v_min.is_finite() {
463        return Ok(Vec::new());
464    }
465
466    // Bound the arc around the conic vertex (closest approach to the apex, at
467    // v_min). An ellipse is naturally bounded; a parabola/hyperbola is not, so
468    // cap the cone radius at a generous multiple of the vertex radius. This is
469    // scale-invariant and centred on where any finite cone face's overlap lies;
470    // the downstream consumer trims the fitted curve to the actual face AABB, so
471    // over-coverage is harmless. The floor handles a vertex at the apex (v_min≈0).
472    let v_max = (8.0 * v_min).max(v_min + 4.0);
473
474    // Per-sample v within the cap; the raw values stay in `vs` for the
475    // boundary solve below.
476    let kept: Vec<Option<f64>> = vs.iter().map(|v| v.filter(|&v| v <= v_max)).collect();
477
478    let point_at = |u: f64, v: f64| -> Point3 {
479        let g = cone.evaluate(u, 1.0) - apex;
480        apex + g * v
481    };
482    #[allow(clippy::cast_precision_loss)]
483    let u_of = |i: usize| TAU * (i as f64) / (n_samples as f64);
484    let n_dot_g_at = |u: f64| -> f64 {
485        let g = cone.evaluate(u, 1.0) - apex;
486        normal.dot(Vec3::new(g.x(), g.y(), g.z()))
487    };
488
489    if kept.iter().all(Option::is_some) {
490        // Closed loop (ellipse regime): emit all points and repeat the first.
491        let mut pts: Vec<Point3> = kept
492            .iter()
493            .enumerate()
494            .filter_map(|(i, v)| v.map(|v| point_at(u_of(i), v)))
495            .collect();
496        if let Some(&first) = pts.first() {
497            pts.push(first);
498        }
499        return Ok(vec![pts]);
500    }
501
502    // A hyperbola/parabola tail diverges as 1/(n·g), so between the last kept
503    // sample and its dropped neighbour v can leap far past `v_max` in one
504    // uniform-u pitch — and any finite face window inside that leap is lost
505    // (a taper cone grazed 0.05 by a prism plane lost its entire 0.5-tall
506    // section to exactly this aliasing). Extend each run end to the exact
507    // `v_max` boundary: bisect u for `n·g(u) = e/v_max` inside the dropped
508    // pitch (n·g is monotone there — its extrema sit at the conic vertex,
509    // far from any asymptote), then fill the tail with uniform-u samples.
510    let tail = |i_end: usize, forward: bool, kept: &[Option<f64>]| -> Vec<Point3> {
511        let Some(v_end) = kept[i_end] else {
512            return Vec::new();
513        };
514        let u_end = u_of(i_end);
515        #[allow(clippy::cast_precision_loss)]
516        let pitch = TAU / (n_samples as f64);
517        let u_next = if forward {
518            u_end + pitch
519        } else {
520            u_end - pitch
521        };
522        let target = e / v_max;
523        let h_end = n_dot_g_at(u_end) - target;
524        let h_next = n_dot_g_at(u_next) - target;
525        if v_end >= v_max || h_end == 0.0 || h_end.signum() == h_next.signum() {
526            return Vec::new();
527        }
528        let (mut lo, mut hi) = (u_end, u_next);
529        for _ in 0..60 {
530            let mid = f64::midpoint(lo, hi);
531            if (n_dot_g_at(mid) - target).signum() == h_end.signum() {
532                lo = mid;
533            } else {
534                hi = mid;
535            }
536        }
537        let u_star = f64::midpoint(lo, hi);
538        let tail_n = 8_usize;
539        (1..=tail_n)
540            .filter_map(|k| {
541                #[allow(clippy::cast_precision_loss)]
542                let u = u_end + (u_star - u_end) * (k as f64) / (tail_n as f64);
543                let ng = n_dot_g_at(u);
544                if ng.abs() < 1e-12 {
545                    return None;
546                }
547                let v = e / ng;
548                (v >= -1e-12 && v <= v_max * (1.0 + 1e-9)).then(|| point_at(u, v.max(0.0)))
549            })
550            .collect()
551    };
552
553    // Split into contiguous runs of kept samples, treating the array as
554    // circular (rotate past a gap) so a branch straddling u=0 stays whole.
555    let gap = kept.iter().position(Option::is_none).unwrap_or(0);
556    let mut chains: Vec<Vec<Point3>> = Vec::new();
557    let mut run: Vec<usize> = Vec::new();
558    let flush = |run: &mut Vec<usize>, chains: &mut Vec<Vec<Point3>>| {
559        if run.len() >= 2 {
560            let first = run[0];
561            let last = run[run.len() - 1];
562            let mut pts: Vec<Point3> = tail(first, false, &kept);
563            pts.reverse();
564            pts.extend(
565                run.iter()
566                    .filter_map(|&i| kept[i].map(|v| point_at(u_of(i), v))),
567            );
568            pts.extend(tail(last, true, &kept));
569            chains.push(pts);
570        }
571        run.clear();
572    };
573    for k in 0..n_samples {
574        let idx = (gap + k) % n_samples;
575        if kept[idx].is_some() {
576            run.push(idx);
577        } else {
578            flush(&mut run, &mut chains);
579        }
580    }
581    flush(&mut run, &mut chains);
582    Ok(chains.into_iter().filter(|c| c.len() >= 2).collect())
583}
584
585/// Sample the plane-torus intersection as ordered 3D points.
586///
587/// Uses the same Newton-refined sampling grid as `intersect_plane_torus`
588/// but skips NURBS curve fitting.
589#[allow(clippy::cast_precision_loss)]
590fn sample_plane_torus(
591    torus: &ToroidalSurface,
592    normal: Vec3,
593    d: f64,
594) -> Result<Vec<Vec<Point3>>, MathError> {
595    let curves = intersect_plane_torus(torus, normal, d)?;
596    Ok(curves
597        .into_iter()
598        .map(|c| c.points.into_iter().map(|p| p.point).collect())
599        .collect())
600}
601
602/// Intersect a plane with a cylindrical surface.
603///
604/// For each `u` in `[0, 2pi)`, the cylinder point is linear in `v`,
605/// so the plane equation `dot(normal, P(u,v)) = d` is linear in `v`
606/// and can be solved directly.
607///
608/// # Errors
609///
610/// Returns an error if curve fitting fails.
611#[allow(clippy::cast_precision_loss)]
612pub fn intersect_plane_cylinder(
613    cyl: &CylindricalSurface,
614    normal: Vec3,
615    d: f64,
616) -> Result<Vec<IntersectionCurve>, MathError> {
617    let n_samples = 64_usize;
618    let mut points_3d = Vec::new();
619    let mut ipoints = Vec::new();
620
621    for i in 0..=n_samples {
622        let u = TAU * (i as f64) / (n_samples as f64);
623        // P(u, v) = origin + r*(cos(u)*x + sin(u)*y) + v*axis
624        // dot(normal, P) = d  =>  dot(normal, base(u)) + v * dot(normal, axis) = d
625        let base = cyl.evaluate(u, 0.0);
626        let n_dot_axis = normal.dot(cyl.axis());
627        let n_dot_base = dot_np(normal, base);
628
629        if n_dot_axis.abs() < 1e-12 {
630            // Plane parallel to axis -- check if base is on plane.
631            if (n_dot_base - d).abs() < 1e-6 {
632                let pt = base;
633                points_3d.push(pt);
634                ipoints.push(IntersectionPoint {
635                    point: pt,
636                    param1: (u, 0.0),
637                    param2: (0.0, 0.0),
638                });
639            }
640        } else {
641            let v = (d - n_dot_base) / n_dot_axis;
642            // Only keep points within a reasonable v range.
643            if v.abs() <= 100.0 {
644                let pt = cyl.evaluate(u, v);
645                points_3d.push(pt);
646                ipoints.push(IntersectionPoint {
647                    point: pt,
648                    param1: (u, v),
649                    param2: (0.0, 0.0),
650                });
651            }
652        }
653    }
654
655    build_curves_from_points(&points_3d, ipoints)
656}
657
658/// Intersect a plane with a spherical surface.
659///
660/// The intersection of a plane with a sphere is a circle (or empty/point).
661/// Computes the circle center, radius, and samples points on it.
662///
663/// # Errors
664///
665/// Returns an error if curve fitting fails.
666#[allow(clippy::cast_precision_loss)]
667pub fn intersect_plane_sphere(
668    sphere: &SphericalSurface,
669    normal: Vec3,
670    d: f64,
671) -> Result<Vec<IntersectionCurve>, MathError> {
672    let h = dot_np(normal, sphere.center()) - d;
673    let r = sphere.radius();
674
675    // No intersection if plane is too far from center.
676    if h.abs() > r - 1e-10 {
677        return Ok(vec![]);
678    }
679
680    let circle_r = (r.mul_add(r, -(h * h))).sqrt();
681    let circle_center = Point3::new(
682        h.mul_add(-normal.x(), sphere.center().x()),
683        h.mul_add(-normal.y(), sphere.center().y()),
684        h.mul_add(-normal.z(), sphere.center().z()),
685    );
686
687    // Build a local frame on the plane.
688    let basis = Frame3::from_normal(circle_center, normal)?;
689    let u_dir = basis.x;
690    let v_dir = basis.y;
691
692    let n_samples = 64_usize;
693    let mut points_3d = Vec::new();
694    let mut ipoints = Vec::new();
695
696    for i in 0..=n_samples {
697        let theta = TAU * (i as f64) / (n_samples as f64);
698        let (sin_t, cos_t) = theta.sin_cos();
699        let pt = circle_center + u_dir * (circle_r * cos_t) + v_dir * (circle_r * sin_t);
700        points_3d.push(pt);
701        ipoints.push(IntersectionPoint {
702            point: pt,
703            param1: (theta, 0.0),
704            param2: (0.0, 0.0),
705        });
706    }
707
708    build_curves_from_points(&points_3d, ipoints)
709}
710
711/// Intersect a plane with a conical surface.
712///
713/// Like a cylinder, the cone is linear along each generatrix, so the plane
714/// equation is linear in `v` for each fixed `u`.
715///
716/// # Errors
717///
718/// Returns an error if curve fitting fails.
719#[allow(clippy::cast_precision_loss)]
720pub fn intersect_plane_cone(
721    cone: &ConicalSurface,
722    normal: Vec3,
723    d: f64,
724) -> Result<Vec<IntersectionCurve>, MathError> {
725    let n_samples = 64_usize;
726    let mut points_3d = Vec::new();
727    let mut ipoints = Vec::new();
728
729    for i in 0..n_samples {
730        let u = TAU * (i as f64) / (n_samples as f64);
731        // P(u, v) = apex + v * dir(u)
732        // dot(normal, apex) + v * dot(normal, dir(u)) = d
733        let apex = cone.apex();
734        let n_dot_apex = dot_np(normal, apex);
735        // dir(u) = P(u,1) - apex
736        let p1 = cone.evaluate(u, 1.0);
737        let dir = p1 - apex;
738        let n_dot_dir = normal.dot(dir);
739
740        if n_dot_dir.abs() < 1e-12 {
741            continue;
742        }
743
744        let v = (d - n_dot_apex) / n_dot_dir;
745        // Allow negative v — the cone surface extends in both directions from the apex.
746        if v.abs() > 1e-10 && v.abs() < 100.0 {
747            let pt = cone.evaluate(u, v);
748            points_3d.push(pt);
749            ipoints.push(IntersectionPoint {
750                point: pt,
751                param1: (u, v),
752                param2: (0.0, 0.0),
753            });
754        }
755    }
756
757    build_curves_from_points(&points_3d, ipoints)
758}
759
760/// Intersect a plane with a toroidal surface.
761///
762/// The torus-plane intersection is a degree-4 curve with no simple closed form.
763/// Uses grid sampling with sign-change detection and Newton refinement.
764///
765/// # Errors
766///
767/// Returns an error if curve fitting fails.
768#[allow(
769    clippy::cast_precision_loss,
770    clippy::too_many_lines,
771    clippy::unnecessary_wraps
772)]
773pub fn intersect_plane_torus(
774    torus: &ToroidalSurface,
775    normal: Vec3,
776    d: f64,
777) -> Result<Vec<IntersectionCurve>, MathError> {
778    let n_grid = 128_usize;
779
780    // Signed distance to plane for a torus point.
781    let sdf = |u: f64, v: f64| -> f64 { dot_np(normal, torus.evaluate(u, v)) - d };
782
783    // Collect zero-crossing points by scanning edges of a (u,v) grid.
784    let mut crossing_pts: Vec<(f64, f64, Point3)> = Vec::new();
785
786    let du = TAU / (n_grid as f64);
787    let dv = TAU / (n_grid as f64);
788
789    // Offset grid by half a cell to avoid landing exactly on zero crossings
790    // (e.g. sin(0) = 0.0 exactly in IEEE 754, which defeats sign-change detection).
791    let u_off = du * 0.5;
792    let v_off = dv * 0.5;
793
794    for iu in 0..n_grid {
795        for iv in 0..n_grid {
796            let u0 = (iu as f64).mul_add(du, u_off);
797            let v0 = (iv as f64).mul_add(dv, v_off);
798            let u1 = u0 + du;
799            let v1 = v0 + dv;
800
801            let f00 = sdf(u0, v0);
802            let f10 = sdf(u1, v0);
803            let f01 = sdf(u0, v1);
804
805            // Check horizontal edge (u0,v0)-(u1,v0).
806            if f00 * f10 < 0.0 {
807                let t = f00 / (f00 - f10);
808                let u = t.mul_add(u1 - u0, u0);
809                let (u_r, v_r) = newton_refine_torus(torus, normal, d, u, v0);
810                crossing_pts.push((u_r, v_r, torus.evaluate(u_r, v_r)));
811            }
812
813            // Check vertical edge (u0,v0)-(u0,v1).
814            if f00 * f01 < 0.0 {
815                let t = f00 / (f00 - f01);
816                let v = t.mul_add(v1 - v0, v0);
817                let (u_r, v_r) = newton_refine_torus(torus, normal, d, u0, v);
818                crossing_pts.push((u_r, v_r, torus.evaluate(u_r, v_r)));
819            }
820        }
821    }
822
823    if crossing_pts.is_empty() {
824        return Ok(vec![]);
825    }
826
827    // Group nearby points into connected curves via greedy chaining.
828    let mut used = vec![false; crossing_pts.len()];
829    let mut curves = Vec::new();
830
831    for start in 0..crossing_pts.len() {
832        if used[start] {
833            continue;
834        }
835        used[start] = true;
836        let mut chain = vec![start];
837
838        loop {
839            let last = chain[chain.len() - 1];
840            let last_pt = crossing_pts[last].2;
841            let mut best_idx = None;
842            let mut best_dist = 1.0_f64;
843
844            for (j, &is_used) in used.iter().enumerate() {
845                if is_used {
846                    continue;
847                }
848                let dist = (crossing_pts[j].2 - last_pt).length();
849                if dist < best_dist {
850                    best_dist = dist;
851                    best_idx = Some(j);
852                }
853            }
854
855            if let Some(j) = best_idx {
856                used[j] = true;
857                chain.push(j);
858            } else {
859                break;
860            }
861        }
862
863        if chain.len() >= 4 {
864            let mut pts: Vec<Point3> = chain.iter().map(|&i| crossing_pts[i].2).collect();
865            let mut ipts: Vec<IntersectionPoint> = chain
866                .iter()
867                .map(|&i| IntersectionPoint {
868                    point: crossing_pts[i].2,
869                    param1: (crossing_pts[i].0, crossing_pts[i].1),
870                    param2: (0.0, 0.0),
871                })
872                .collect();
873
874            // Plane × full torus is always a set of CLOSED loops, but the greedy
875            // nearest-neighbour chaining stops one step short of closing: the
876            // first point is already `used`, so the walk never re-adds it and the
877            // last point sits ~one grid step from the start. Detect that wrap (the
878            // end-to-start gap is comparable to the chain's own point spacing) and
879            // append the exact start point, so the fitted NURBS closes
880            // (`evaluate(0) == evaluate(1)`) and downstream consumers see a closed
881            // section curve instead of a near-closed open one. A fragmented chain
882            // (greedy walk broke a loop at a near-tangency) ends FAR from its
883            // start (gap ≫ spacing) and is left open — it must not be force-closed
884            // into a wrong loop.
885            let closing_gap = (pts[pts.len() - 1] - pts[0]).length();
886            let median_spacing = {
887                let mut spac: Vec<f64> = pts.windows(2).map(|w| (w[1] - w[0]).length()).collect();
888                spac.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
889                spac.get(spac.len() / 2).copied().unwrap_or(0.0)
890            };
891            // Wrap when the closing gap is within ~2 point-spacings (measured
892            // ratio ≈ 1.0 for the census ovals) and not already coincident.
893            let wrapped =
894                closing_gap > 1e-9 && median_spacing > 1e-12 && closing_gap <= 2.0 * median_spacing;
895            if wrapped {
896                pts.push(pts[0]);
897                ipts.push(ipts[0]);
898            }
899
900            if let Ok(curve) = interpolate(&pts, 3.min(pts.len() - 1)) {
901                curves.push(IntersectionCurve {
902                    curve,
903                    points: ipts,
904                });
905            }
906        }
907    }
908
909    Ok(curves)
910}
911
912/// Newton-refine a torus parameter to lie on the cutting plane.
913fn newton_refine_torus(
914    torus: &ToroidalSurface,
915    normal: Vec3,
916    d: f64,
917    mut u: f64,
918    mut v: f64,
919) -> (f64, f64) {
920    let eps = 1e-6;
921    for _ in 0..10 {
922        let f = dot_np(normal, torus.evaluate(u, v)) - d;
923        if f.abs() < 1e-12 {
924            break;
925        }
926        // Numerical gradient via central differences.
927        let fu = (dot_np(normal, torus.evaluate(u + eps, v))
928            - dot_np(normal, torus.evaluate(u - eps, v)))
929            / (2.0 * eps);
930        let fv = (dot_np(normal, torus.evaluate(u, v + eps))
931            - dot_np(normal, torus.evaluate(u, v - eps)))
932            / (2.0 * eps);
933
934        let grad_sq = fu.mul_add(fu, fv * fv);
935        if grad_sq < 1e-20 {
936            break;
937        }
938        let step = f / grad_sq;
939        u -= step * fu;
940        v -= step * fv;
941    }
942    (u, v)
943}
944
945/// Real intersection parameters `t` of the line `origin + t·dir` with a torus.
946///
947/// A line meets a torus in up to four points (degree-4). Substituting the line
948/// into the torus implicit `(a² + b² + c² + R² − r²)² = 4R²(a² + b²)` — where
949/// `(a, b, c)` are the line point's coordinates in the torus frame — gives a
950/// quartic in `t`, solved here for its real roots (each refined by one Newton
951/// step against the implicit). `dir` need not be unit length; `t` is in units of
952/// `dir`. Returns the roots sorted ascending (0–4 of them).
953///
954/// Used by the boolean section trimmer to find where a plane×torus oval exits a
955/// box face's straight boundary edge — the exact crossing shared by the two
956/// adjacent faces, which is what makes the notch watertight.
957#[must_use]
958pub fn intersect_line_torus(torus: &ToroidalSurface, origin: Point3, dir: Vec3) -> Vec<f64> {
959    let c = torus.center();
960    let (xa, ya, za) = (torus.x_axis(), torus.y_axis(), torus.z_axis());
961    let big_r = torus.major_radius();
962    let small_r = torus.minor_radius();
963
964    // Line point in torus frame: a(t)=a0+a1 t, b(t)=b0+b1 t, c(t)=c0+c1 t.
965    let o = Vec3::new(origin.x() - c.x(), origin.y() - c.y(), origin.z() - c.z());
966    let (a0, a1) = (xa.dot(o), xa.dot(dir));
967    let (b0, b1) = (ya.dot(o), ya.dot(dir));
968    let (c0, c1) = (za.dot(o), za.dot(dir));
969
970    // G(t) = a² + b² + c² + R² − r²  (quadratic: g2 t² + g1 t + g0)
971    let g2 = a1.mul_add(a1, b1.mul_add(b1, c1 * c1));
972    let g1 = 2.0 * a1.mul_add(a0, b1.mul_add(b0, c1 * c0));
973    let g0 = a0.mul_add(
974        a0,
975        b0.mul_add(b0, c0.mul_add(c0, big_r.mul_add(big_r, -small_r * small_r))),
976    );
977
978    // H(t) = 4R² (a² + b²)  (quadratic: h2 t² + h1 t + h0)
979    let four_rr = 4.0 * big_r * big_r;
980    let h2 = four_rr * a1.mul_add(a1, b1 * b1);
981    let h1 = four_rr * (2.0 * a1.mul_add(a0, b1 * b0));
982    let h0 = four_rr * a0.mul_add(a0, b0 * b0);
983
984    // Quartic G² − H = 0:  e4 t⁴ + e3 t³ + e2 t² + e1 t + e0.
985    let e4 = g2 * g2;
986    let e3 = 2.0 * g2 * g1;
987    let e2 = g1.mul_add(g1, 2.0 * g2 * g0) - h2;
988    let e1 = 2.0f64.mul_add(g1 * g0, -h1);
989    let e0 = g0.mul_add(g0, -h0);
990
991    let mut roots = real_roots_quartic(e4, e3, e2, e1, e0);
992    // One Newton polish against the torus implicit for full precision.
993    let impl_f = |t: f64| -> f64 {
994        let p = origin + dir * t;
995        let q = Vec3::new(p.x() - c.x(), p.y() - c.y(), p.z() - c.z());
996        let (a, b, cc) = (xa.dot(q), ya.dot(q), za.dot(q));
997        (a.hypot(b) - big_r).hypot(cc) - small_r
998    };
999    for t in &mut roots {
1000        let eps = 1e-7;
1001        let f = impl_f(*t);
1002        let df = (impl_f(*t + eps) - impl_f(*t - eps)) / (2.0 * eps);
1003        if df.abs() > 1e-12 {
1004            *t -= f / df;
1005        }
1006    }
1007    roots.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
1008    roots
1009}
1010
1011/// Real roots of `c4 x⁴ + c3 x³ + c2 x² + c1 x + c0` via Durand–Kerner, falling
1012/// back to the lower-degree solvers when the leading coefficients vanish.
1013fn real_roots_quartic(c4: f64, c3: f64, c2: f64, c1: f64, c0: f64) -> Vec<f64> {
1014    // Degenerate leading coefficient → lower degree.
1015    if c4.abs() < 1e-14 {
1016        return real_roots_cubic(c3, c2, c1, c0);
1017    }
1018    // Monic: x⁴ + a x³ + b x² + c x + d.
1019    let (a, b, c, d) = (c3 / c4, c2 / c4, c1 / c4, c0 / c4);
1020    let eval = |z: Complex| -> Complex {
1021        // Horner.
1022        let mut acc = Complex::new(1.0, 0.0);
1023        acc = acc * z + Complex::new(a, 0.0);
1024        acc = acc * z + Complex::new(b, 0.0);
1025        acc = acc * z + Complex::new(c, 0.0);
1026        acc * z + Complex::new(d, 0.0)
1027    };
1028    // Durand–Kerner: four roots seeded on a circle, iterated to convergence.
1029    let seed = Complex::new(0.4, 0.9);
1030    let mut r = [
1031        Complex::new(1.0, 0.0),
1032        seed,
1033        seed * seed,
1034        seed * seed * seed,
1035    ];
1036    for _ in 0..100 {
1037        let mut max_step = 0.0_f64;
1038        for i in 0..4 {
1039            let mut denom = Complex::new(1.0, 0.0);
1040            for j in 0..4 {
1041                if i != j {
1042                    denom = denom * (r[i] - r[j]);
1043                }
1044            }
1045            if denom.norm() < 1e-300 {
1046                continue;
1047            }
1048            let step = eval(r[i]) / denom;
1049            r[i] = r[i] - step;
1050            max_step = max_step.max(step.norm());
1051        }
1052        if max_step < 1e-14 {
1053            break;
1054        }
1055    }
1056    // Keep roots with negligible imaginary part AND a small REAL-polynomial
1057    // residual — Durand–Kerner stops after a fixed iteration cap whether or not
1058    // it converged, so a non-converged iterate could otherwise be returned as a
1059    // spurious root. Evaluate the monic quartic at each candidate (real part) and
1060    // keep only |p(x)| below a magnitude-scaled tolerance; de-dup near-equal
1061    // roots (a double root converges to two near-identical iterates).
1062    let p_real = |x: f64| -> f64 { (((x + a) * x + b) * x + c) * x + d };
1063    let mut out: Vec<f64> = Vec::new();
1064    for z in r {
1065        if z.im.abs() >= 1e-7 {
1066            continue;
1067        }
1068        let x = z.re;
1069        // Residual tolerance scales with the polynomial's coefficient magnitude
1070        // and |x|^4 so large-coefficient quartics are not over-rejected.
1071        let scale = 1.0 + a.abs() + b.abs() + c.abs() + d.abs() + x.abs().powi(4);
1072        if p_real(x).abs() > 1e-6 * scale {
1073            continue;
1074        }
1075        if out.iter().any(|&y| (y - x).abs() < 1e-9 * (1.0 + x.abs())) {
1076            continue;
1077        }
1078        out.push(x);
1079    }
1080    out
1081}
1082
1083/// Real roots of `a x³ + b x² + c x + d` (Cardano), with quadratic fallback.
1084fn real_roots_cubic(a: f64, b: f64, c: f64, d: f64) -> Vec<f64> {
1085    if a.abs() < 1e-14 {
1086        return real_roots_quadratic(b, c, d);
1087    }
1088    // Depressed cubic t³ + p t + q via x = t − b/(3a).
1089    let (b, c, d) = (b / a, c / a, d / a);
1090    let p = c - b * b / 3.0;
1091    let q = 2.0 * b * b * b / 27.0 - b * c / 3.0 + d;
1092    let shift = -b / 3.0;
1093    let disc = q * q / 4.0 + p * p * p / 27.0;
1094    if disc > 1e-14 {
1095        let sq = disc.sqrt();
1096        let u = (-q / 2.0 + sq).cbrt();
1097        let v = (-q / 2.0 - sq).cbrt();
1098        vec![u + v + shift]
1099    } else if disc < -1e-14 {
1100        // Three real roots (trigonometric).
1101        let m = 2.0 * (-p / 3.0).sqrt();
1102        let theta = (3.0 * q / (p * m)).clamp(-1.0, 1.0).acos() / 3.0;
1103        (0..3)
1104            .map(|k| {
1105                m.mul_add(
1106                    (theta - 2.0 * std::f64::consts::PI * f64::from(k) / 3.0).cos(),
1107                    shift,
1108                )
1109            })
1110            .collect()
1111    } else {
1112        // Repeated roots.
1113        let u = (-q / 2.0).cbrt();
1114        vec![2.0 * u + shift, -u + shift]
1115    }
1116}
1117
1118/// Real roots of `a x² + b x + c`, with linear fallback.
1119fn real_roots_quadratic(a: f64, b: f64, c: f64) -> Vec<f64> {
1120    if a.abs() < 1e-14 {
1121        if b.abs() < 1e-14 {
1122            return Vec::new();
1123        }
1124        return vec![-c / b];
1125    }
1126    let disc = b * b - 4.0 * a * c;
1127    if disc < 0.0 {
1128        Vec::new()
1129    } else {
1130        let sq = disc.sqrt();
1131        vec![(-b - sq) / (2.0 * a), (-b + sq) / (2.0 * a)]
1132    }
1133}
1134
1135/// Minimal complex number for the quartic root finder.
1136#[derive(Clone, Copy)]
1137struct Complex {
1138    re: f64,
1139    im: f64,
1140}
1141
1142impl Complex {
1143    const fn new(re: f64, im: f64) -> Self {
1144        Self { re, im }
1145    }
1146    fn norm(self) -> f64 {
1147        self.re.hypot(self.im)
1148    }
1149}
1150
1151impl std::ops::Add for Complex {
1152    type Output = Self;
1153    fn add(self, o: Self) -> Self {
1154        Self::new(self.re + o.re, self.im + o.im)
1155    }
1156}
1157
1158impl std::ops::Sub for Complex {
1159    type Output = Self;
1160    fn sub(self, o: Self) -> Self {
1161        Self::new(self.re - o.re, self.im - o.im)
1162    }
1163}
1164
1165impl std::ops::Mul for Complex {
1166    type Output = Self;
1167    fn mul(self, o: Self) -> Self {
1168        Self::new(
1169            self.re.mul_add(o.re, -(self.im * o.im)),
1170            self.re.mul_add(o.im, self.im * o.re),
1171        )
1172    }
1173}
1174
1175impl std::ops::Div for Complex {
1176    type Output = Self;
1177    fn div(self, o: Self) -> Self {
1178        let den = o.re.mul_add(o.re, o.im * o.im);
1179        Self::new(
1180            self.re.mul_add(o.re, self.im * o.im) / den,
1181            self.im.mul_add(o.re, -(self.re * o.im)) / den,
1182        )
1183    }
1184}
1185
1186/// Build intersection curves from a collection of ordered 3D points.
1187///
1188/// If there are enough points, fits a NURBS curve through them.
1189fn build_curves_from_points(
1190    points_3d: &[Point3],
1191    ipoints: Vec<IntersectionPoint>,
1192) -> Result<Vec<IntersectionCurve>, MathError> {
1193    if points_3d.len() < 2 {
1194        return Ok(vec![]);
1195    }
1196
1197    let degree = 3.min(points_3d.len() - 1);
1198    let curve = interpolate(points_3d, degree)?;
1199    Ok(vec![IntersectionCurve {
1200        curve,
1201        points: ipoints,
1202    }])
1203}
1204
1205// -- Analytic-Analytic Intersection -------------------------------------------
1206
1207/// Intersect two analytic surfaces using a general marching approach.
1208///
1209/// Seeds intersection points by sampling both parameter spaces on a grid,
1210/// then marches along the intersection curve using the cross product of
1211/// the two surface normals as the tangent direction.
1212///
1213/// # Errors
1214///
1215/// Returns an error if curve fitting fails.
1216#[allow(
1217    clippy::cast_precision_loss,
1218    clippy::too_many_lines,
1219    clippy::similar_names,
1220    clippy::unnecessary_wraps,
1221    clippy::type_complexity
1222)]
1223pub fn intersect_analytic_analytic(
1224    a: AnalyticSurface<'_>,
1225    b: AnalyticSurface<'_>,
1226    grid_res: usize,
1227) -> Result<Vec<IntersectionCurve>, MathError> {
1228    intersect_analytic_analytic_bounded(a, b, grid_res, None, None)
1229}
1230
1231/// Intersect two analytic surfaces with optional v-range overrides.
1232///
1233/// When `v_range_hint_a` or `v_range_hint_b` is `Some((min, max))`, the
1234/// marching algorithm searches that v-range instead of the hardcoded default.
1235/// This is essential for cylinders and cones whose default v-range is small
1236/// (-1..1 or 0.01..2) but whose actual face may extend much further.
1237///
1238/// # Errors
1239///
1240/// Returns `MathError` if algebraic intersection fails or marching diverges.
1241pub fn intersect_analytic_analytic_bounded(
1242    a: AnalyticSurface<'_>,
1243    b: AnalyticSurface<'_>,
1244    grid_res: usize,
1245    v_range_hint_a: Option<(f64, f64)>,
1246    v_range_hint_b: Option<(f64, f64)>,
1247) -> Result<Vec<IntersectionCurve>, MathError> {
1248    // Try algebraic specialization for known surface pairs before falling
1249    // back to the general marching approach.
1250    if let Some(result) = try_algebraic_intersection(&a, &b, v_range_hint_a, v_range_hint_b)? {
1251        return Ok(result);
1252    }
1253
1254    let (surf_a, norm_a, u_range_a, default_v_a) = surface_closures(&a);
1255    let (surf_b, norm_b, u_range_b, default_v_b) = surface_closures(&b);
1256    let v_range_a = v_range_hint_a.unwrap_or(default_v_a);
1257    let v_range_b = v_range_hint_b.unwrap_or(default_v_b);
1258
1259    // Compute characteristic surface dimensions for adaptive parameters.
1260    let diag_a = {
1261        let p00 = surf_a(u_range_a.0, v_range_a.0);
1262        let p11 = surf_a(u_range_a.1, v_range_a.1);
1263        (p00 - p11).length()
1264    };
1265    let diag_b = {
1266        let p00 = surf_b(u_range_b.0, v_range_b.0);
1267        let p11 = surf_b(u_range_b.1, v_range_b.1);
1268        (p00 - p11).length()
1269    };
1270    let char_size = diag_a.min(diag_b).max(0.1);
1271
1272    // Sample surface A on a grid. For each grid point, project it
1273    // analytically onto surface B to find the closest point, then check
1274    // if the distance is below threshold (indicating near-intersection).
1275    #[allow(clippy::type_complexity)]
1276    let mut seeds: Vec<(Point3, (f64, f64), (f64, f64))> = Vec::new();
1277    // Coarse threshold scales with the surface size — the distance from
1278    // a grid point on A to its projection on B can be large even near
1279    // the intersection (e.g., sphere R=2 and cylinder R=1 → gap ≈ 1).
1280    let seed_threshold = diag_a.max(diag_b).max(1.0) * 0.5;
1281    let mut min_dist = f64::INFINITY;
1282
1283    #[allow(clippy::cast_precision_loss)]
1284    for ia in 0..grid_res {
1285        for ja in 0..grid_res {
1286            let ua =
1287                u_range_a.0 + (u_range_a.1 - u_range_a.0) * (ia as f64 + 0.5) / (grid_res as f64);
1288            let va =
1289                v_range_a.0 + (v_range_a.1 - v_range_a.0) * (ja as f64 + 0.5) / (grid_res as f64);
1290
1291            let pa = surf_a(ua, va);
1292
1293            // Analytically project onto surface B.
1294            let (ub, vb) = project_analytic(&b, pa, u_range_b, v_range_b);
1295            let pb = surf_b(ub, vb);
1296            let dist = (pa - pb).length();
1297            min_dist = min_dist.min(dist);
1298
1299            if dist < seed_threshold {
1300                // Use the coarse seed directly. The marching algorithm
1301                // corrects positions at each step via projection, so seeds
1302                // don't need to be on the exact intersection — they just
1303                // need to be close enough for the marcher to converge.
1304                let mid = Point3::new(
1305                    (pa.x() + pb.x()) * 0.5,
1306                    (pa.y() + pb.y()) * 0.5,
1307                    (pa.z() + pb.z()) * 0.5,
1308                );
1309                seeds.push((mid, (ua, va), (ub, vb)));
1310            }
1311        }
1312    }
1313
1314    // Cheap rejection: the grid samples surface A; the closest sample's
1315    // distance to B lower-bounds how near the two bounded patches come. A
1316    // transversal crossing puts a sample within ~one grid cell of it
1317    // (distance on the order of a cell), so if even the nearest sample is
1318    // several cells away the patches cannot cross — skip the expensive
1319    // marching and return empty. Result-preserving: non-crossing pairs
1320    // already march to nothing, just slowly (this is the gridfinity lip's
1321    // ~80 inner-wall × outer-wall pairs that dominate pavefiller time).
1322    let reject_dist = (char_size / grid_res as f64) * 3.0;
1323    if min_dist > reject_dist {
1324        return Ok(vec![]);
1325    }
1326
1327    if seeds.is_empty() {
1328        return Ok(vec![]);
1329    }
1330
1331    // Aggressively deduplicate seeds — we only need 1-2 per intersection
1332    // branch. Scale dedup radius to ~2% of characteristic surface size
1333    // (at least 10× the march step size) to avoid redundant marches.
1334    let march_step = (char_size * 0.02).clamp(0.005, 0.5);
1335    let dedup_radius = march_step * 10.0;
1336    let mut unique_seeds = Vec::new();
1337    for seed in &seeds {
1338        let dominated = unique_seeds
1339            .iter()
1340            .any(|s: &(Point3, (f64, f64), (f64, f64))| (s.0 - seed.0).length() < dedup_radius);
1341        if !dominated {
1342            unique_seeds.push(*seed);
1343        }
1344    }
1345
1346    // March from each seed.
1347    let mut curves = Vec::new();
1348    let mut used_seeds = vec![false; unique_seeds.len()];
1349
1350    for si in 0..unique_seeds.len() {
1351        if used_seeds[si] {
1352            continue;
1353        }
1354        used_seeds[si] = true;
1355
1356        let march_result = march_analytic_intersection(
1357            &a,
1358            &b,
1359            surf_a.as_ref(),
1360            norm_a.as_ref(),
1361            surf_b.as_ref(),
1362            norm_b.as_ref(),
1363            unique_seeds[si].0,
1364            u_range_a,
1365            v_range_a,
1366            u_range_b,
1367            v_range_b,
1368            march_step,
1369            is_u_periodic(&a),
1370            is_u_periodic(&b),
1371        );
1372
1373        if march_result.len() >= 2 {
1374            for (sj, other) in unique_seeds.iter().enumerate() {
1375                if !used_seeds[sj]
1376                    && march_result
1377                        .iter()
1378                        .any(|p| (*p - other.0).length() < dedup_radius)
1379                {
1380                    used_seeds[sj] = true;
1381                }
1382            }
1383
1384            let ipts: Vec<IntersectionPoint> = march_result
1385                .iter()
1386                .map(|&pt| IntersectionPoint {
1387                    point: pt,
1388                    param1: (0.0, 0.0),
1389                    param2: (0.0, 0.0),
1390                })
1391                .collect();
1392
1393            let degree = 3.min(march_result.len() - 1);
1394            if let Ok(curve) = interpolate(&march_result, degree) {
1395                curves.push(IntersectionCurve {
1396                    curve,
1397                    points: ipts,
1398                });
1399            }
1400        }
1401    }
1402
1403    Ok(curves)
1404}
1405
1406/// Try algebraic (closed-form or semi-algebraic) intersection for known
1407/// surface pairs before falling back to general marching.
1408///
1409/// Returns `Some(curves)` if a specialized method exists, `None` otherwise.
1410///
1411/// Currently handles:
1412/// - **Sphere-sphere**: intersection is a circle (plane through the two centers)
1413/// - **Coaxial cylinders**: same axis → circle(s) or empty
1414/// - **Sphere-cylinder**: reduce to quadratic in one parameter
1415#[allow(clippy::too_many_lines)]
1416fn try_algebraic_intersection(
1417    a: &AnalyticSurface<'_>,
1418    b: &AnalyticSurface<'_>,
1419    v_range_a: Option<(f64, f64)>,
1420    v_range_b: Option<(f64, f64)>,
1421) -> Result<Option<Vec<IntersectionCurve>>, MathError> {
1422    match (a, b) {
1423        (AnalyticSurface::Cone(cone), AnalyticSurface::Cylinder(cyl)) => {
1424            algebraic_parallel_cone_cylinder(cone, cyl, v_range_a, v_range_b)
1425        }
1426        (AnalyticSurface::Cylinder(cyl), AnalyticSurface::Cone(cone)) => {
1427            algebraic_parallel_cone_cylinder(cone, cyl, v_range_b, v_range_a)
1428        }
1429        (AnalyticSurface::Sphere(s1), AnalyticSurface::Sphere(s2)) => {
1430            algebraic_sphere_sphere(s1, s2).map(Some)
1431        }
1432        (AnalyticSurface::Cylinder(c1), AnalyticSurface::Cylinder(c2)) => {
1433            let axis_dot = c1.axis().dot(c2.axis()).abs();
1434            if axis_dot > 1.0 - 1e-10 {
1435                // Axes are parallel — check if they're the same line.
1436                let delta = c2.origin() - c1.origin();
1437                let delta_vec = Vec3::new(delta.x(), delta.y(), delta.z());
1438                let along = delta_vec.dot(c1.axis());
1439                let perp = (delta_vec - c1.axis() * along).length();
1440                if perp < 1e-8 {
1441                    // Coaxial: same axis, different radii → no intersection
1442                    // (unless equal radius → degenerate overlap, skip)
1443                    if (c1.radius() - c2.radius()).abs() < 1e-8 {
1444                        return Ok(None); // Overlapping — let marcher handle
1445                    }
1446                    return Ok(Some(vec![])); // Coaxial, different radii
1447                }
1448            }
1449            // Non-coaxial: algebraic quadratic in v.
1450            algebraic_cylinder_cylinder(c1, c2)
1451        }
1452        // Sphere-cylinder (both orderings).
1453        (AnalyticSurface::Sphere(s), AnalyticSurface::Cylinder(c))
1454        | (AnalyticSurface::Cylinder(c), AnalyticSurface::Sphere(s)) => {
1455            algebraic_sphere_cylinder(s, c)
1456        }
1457        (AnalyticSurface::Cone(c1), AnalyticSurface::Cone(c2)) => algebraic_cone_cone(c1, c2),
1458        _ => Ok(None),
1459    }
1460}
1461
1462/// Exact coaxial cone-cone intersection: returns the shared circle.
1463///
1464/// Two cones that share an axis are concentric circles at every axial
1465/// station, so they meet only where their radii are equal. Each cone's
1466/// radius is linear in the axial coordinate `t` (measured along the shared
1467/// axis from cone 1's apex): `r1 = m1·t` and `r2 = m2·σ·(t − d2)`, where
1468/// `m_i = cot(half_angle_i)`, `σ = sign(axis2·axis1)`, and `d2` is cone 2's
1469/// apex position in that coordinate. Equating gives a single crossing `t*`
1470/// → one circle (the shared rim). The general marcher mishandles this case:
1471/// at the radii-crossing the surfaces are nearly tangent, so a grid-seeded
1472/// march fragments the clean circle into dozens of degenerate micro-curves.
1473///
1474/// Returns `Some(vec![circle])` for a genuine crossing, `Some(vec![])` when
1475/// the cones do not meet (parallel radius lines or a crossing on the wrong
1476/// nappe), and `None` for the identical-cone overlap or a degenerate
1477/// (near-flat) cone — both of which fall through to the general path.
1478///
1479/// # Errors
1480///
1481/// Returns [`MathError`] if the shared-rim `Circle3D` cannot be constructed
1482/// (e.g. a non-finite center or radius from a malformed cone).
1483pub fn exact_cone_cone(
1484    c1: &ConicalSurface,
1485    c2: &ConicalSurface,
1486) -> Result<Option<Vec<ExactIntersectionCurve>>, MathError> {
1487    let axis = c1.axis();
1488    let axis2 = c2.axis();
1489
1490    // Coaxial check: parallel axes and the second apex lies on the first axis.
1491    if axis.dot(axis2).abs() < 1.0 - 1e-10 {
1492        return Ok(None); // Non-coaxial: quartic curve, let the marcher handle.
1493    }
1494    let apex1 = c1.apex();
1495    let apex2 = c2.apex();
1496    let delta = apex2 - apex1;
1497    let delta_v = Vec3::new(delta.x(), delta.y(), delta.z());
1498    let along = delta_v.dot(axis);
1499    if (delta_v - axis * along).length() > 1e-8 {
1500        return Ok(None); // Parallel but offset axes — not coaxial.
1501    }
1502
1503    let (s1, s2) = (c1.half_angle().sin(), c2.half_angle().sin());
1504    if s1.abs() < 1e-12 || s2.abs() < 1e-12 {
1505        return Ok(None); // Degenerate (near-flat) cone.
1506    }
1507    let m1 = c1.half_angle().cos() / s1;
1508    let m2 = c2.half_angle().cos() / s2;
1509    let sigma = if axis.dot(axis2) >= 0.0 { 1.0 } else { -1.0 };
1510    let d2 = along; // apex2 position along `axis`, measured from apex1.
1511
1512    let denom = m1 - m2 * sigma;
1513    if denom.abs() < 1e-12 {
1514        // Parallel radius lines: identical cones (coincident apex, same opening)
1515        // overlap — defer to the general/same-domain path; otherwise no meeting.
1516        if sigma > 0.0 && d2.abs() < 1e-9 {
1517            return Ok(None);
1518        }
1519        return Ok(Some(vec![]));
1520    }
1521
1522    let t_star = (-m2 * sigma * d2) / denom;
1523    let radius = m1 * t_star;
1524    if radius < 1e-12 {
1525        return Ok(Some(vec![])); // Crossing on the wrong nappe / no real circle.
1526    }
1527
1528    let center = Point3::new(
1529        apex1.x() + axis.x() * t_star,
1530        apex1.y() + axis.y() * t_star,
1531        apex1.z() + axis.z() * t_star,
1532    );
1533    let circle = Circle3D::new(center, axis, radius)?;
1534    Ok(Some(vec![ExactIntersectionCurve::Circle(circle)]))
1535}
1536
1537/// Exact coaxial cone-cylinder intersection: returns the shared circle.
1538///
1539/// A cone and a cylinder sharing an axis are concentric circles at every
1540/// axial station, so they meet only where the cone's radius equals the
1541/// cylinder's. The cone radius is linear in the axial coordinate `t` from its
1542/// apex (`r = m·t`, `m = cot(half_angle)`), the cylinder radius is the
1543/// constant `R`, so `m·t = R` gives a single crossing `t*` → one circle. This
1544/// is the gridfinity lip's top knife edge (inner tapered corner = cone, outer
1545/// corner = cylinder, concentric, radii matching at `Z_PEAK`); the general
1546/// marcher fragments that near-tangent contact into dozens of degenerate
1547/// micro-curves.
1548///
1549/// Returns `Some(vec![circle])` for a genuine crossing, `Some(vec![])` when
1550/// the crossing degenerates to the apex, and `None` (defer to the marcher)
1551/// when the surfaces are not coaxial or the cone is near-flat / near-axial.
1552///
1553/// # Errors
1554///
1555/// Returns [`MathError`] if the shared `Circle3D` cannot be constructed.
1556pub fn exact_cone_cylinder(
1557    cone: &ConicalSurface,
1558    cyl: &CylindricalSurface,
1559) -> Result<Option<Vec<ExactIntersectionCurve>>, MathError> {
1560    let axis = cone.axis();
1561    let cyl_axis = cyl.axis();
1562
1563    // Coaxial check: parallel axes and the cone apex on the cylinder's axis.
1564    if axis.dot(cyl_axis).abs() < 1.0 - 1e-10 {
1565        return Ok(None);
1566    }
1567    let apex = cone.apex();
1568    let delta = apex - cyl.origin();
1569    let delta_v = Vec3::new(delta.x(), delta.y(), delta.z());
1570    let along = delta_v.dot(cyl_axis);
1571    if (delta_v - cyl_axis * along).length() > 1e-8 {
1572        return Ok(None);
1573    }
1574
1575    let s = cone.half_angle().sin();
1576    if s.abs() < 1e-12 {
1577        return Ok(None); // near-flat cone.
1578    }
1579    let m = cone.half_angle().cos() / s; // dr/dt along the cone axis.
1580    if m.abs() < 1e-12 {
1581        return Ok(None); // near-axial cone: radius ~constant.
1582    }
1583
1584    let t_star = cyl.radius() / m; // where the cone radius m·t equals R.
1585    if t_star.abs() < 1e-12 {
1586        return Ok(Some(vec![])); // crossing at the apex — no real circle.
1587    }
1588    let center = Point3::new(
1589        apex.x() + axis.x() * t_star,
1590        apex.y() + axis.y() * t_star,
1591        apex.z() + axis.z() * t_star,
1592    );
1593    let circle = Circle3D::new(center, axis, cyl.radius())?;
1594    Ok(Some(vec![ExactIntersectionCurve::Circle(circle)]))
1595}
1596
1597/// Algebraic coaxial cone-cone intersection (NURBS form for the general
1598/// bounded path). Delegates to [`exact_cone_cone`] and samples each exact
1599/// circle into an interpolated NURBS `IntersectionCurve`, mirroring the
1600/// sphere-cylinder algebraic path. phase FF prefers the exact circle form
1601/// directly (so the section edge links to the coincident boundary), but a
1602/// caller of `intersect_analytic_analytic_bounded` still gets one clean
1603/// curve instead of the marcher's fragments.
1604fn algebraic_cone_cone(
1605    c1: &ConicalSurface,
1606    c2: &ConicalSurface,
1607) -> Result<Option<Vec<IntersectionCurve>>, MathError> {
1608    let Some(exacts) = exact_cone_cone(c1, c2)? else {
1609        return Ok(None);
1610    };
1611    let mut curves = Vec::new();
1612    for exact in exacts {
1613        let ExactIntersectionCurve::Circle(circle) = exact else {
1614            continue;
1615        };
1616        let n_samples = 33;
1617        let mut positions = Vec::with_capacity(n_samples);
1618        let mut points = Vec::with_capacity(n_samples);
1619        #[allow(clippy::cast_precision_loss)]
1620        for i in 0..n_samples {
1621            let theta = TAU * i as f64 / (n_samples - 1) as f64;
1622            let pt = crate::traits::ParametricCurve::evaluate(&circle, theta);
1623            positions.push(pt);
1624            points.push(IntersectionPoint {
1625                point: pt,
1626                param1: (0.0, 0.0),
1627                param2: (0.0, 0.0),
1628            });
1629        }
1630        let degree = 3.min(positions.len() - 1);
1631        let curve = interpolate(&positions, degree)?;
1632        curves.push(IntersectionCurve { curve, points });
1633    }
1634    Ok(Some(curves))
1635}
1636
1637/// Exact coaxial sphere-cylinder intersection: returns the shared circle(s).
1638///
1639/// A sphere of radius `R` centered at `C` and a cylinder of radius `r` whose
1640/// axis passes through `C` meet in concentric circles of radius `r` at the
1641/// axial stations where `sqrt(R² − z²) = r`, i.e. `z = ±sqrt(R² − r²)`
1642/// measured from `C` along the axis. A proper crossing yields two circles; a
1643/// tangent contact (`r = R`) yields one; a cylinder wider than the sphere, or
1644/// a non-coaxial configuration (quartic curve), yields none/defers.
1645///
1646/// Mirrors [`exact_cone_cylinder`] so phase FF can emit the section as an
1647/// exact `Circle3D` (which the closed-circle split + seam adoption recognise)
1648/// rather than the marcher's NURBS fragments.
1649///
1650/// Returns `Some(vec![..])` (0, 1, or 2 circles) for the coaxial case, and
1651/// `None` (defer to the general marcher) when the axes are not coaxial.
1652///
1653/// # Errors
1654///
1655/// Returns [`MathError`] if a shared `Circle3D` cannot be constructed.
1656pub fn exact_sphere_cylinder(
1657    sphere: &SphericalSurface,
1658    cyl: &CylindricalSurface,
1659) -> Result<Option<Vec<ExactIntersectionCurve>>, MathError> {
1660    let sc = sphere.center();
1661    let r_sphere = sphere.radius();
1662    let co = cyl.origin();
1663    let axis = cyl.axis();
1664    let r_cyl = cyl.radius();
1665
1666    // Project sphere center onto the cylinder axis.
1667    let delta = sc - co;
1668    let delta_vec = Vec3::new(delta.x(), delta.y(), delta.z());
1669    let along = delta_vec.dot(axis);
1670    let perp_vec = delta_vec - axis * along;
1671    let d_perp = perp_vec.length();
1672
1673    // Non-coaxial sphere-cylinder intersections produce quartic curves;
1674    // defer those to the general marcher.
1675    if d_perp > 1e-7 {
1676        return Ok(None);
1677    }
1678
1679    // Coaxial: the sphere center lies on the cylinder axis. No real circle
1680    // when the cylinder is wider than the sphere or they are tangent-internal.
1681    if r_cyl > r_sphere + 1e-10 {
1682        return Ok(Some(vec![]));
1683    }
1684    let z_sq = r_sphere * r_sphere - r_cyl * r_cyl;
1685    if z_sq < 0.0 {
1686        return Ok(Some(vec![]));
1687    }
1688    let z = z_sq.sqrt();
1689
1690    // The sphere center projected onto the axis is the midpoint of the two
1691    // section circles, each offset by ±z along the axis with radius `r_cyl`.
1692    let center_axis_pt = Point3::new(
1693        co.x() + axis.x() * along,
1694        co.y() + axis.y() * along,
1695        co.z() + axis.z() * along,
1696    );
1697
1698    let mut circles = Vec::new();
1699    let offsets: &[f64] = if z < 1e-10 { &[0.0] } else { &[z, -z] };
1700    for &z_offset in offsets {
1701        let center = Point3::new(
1702            center_axis_pt.x() + axis.x() * z_offset,
1703            center_axis_pt.y() + axis.y() * z_offset,
1704            center_axis_pt.z() + axis.z() * z_offset,
1705        );
1706        let circle = Circle3D::new(center, axis, r_cyl)?;
1707        circles.push(ExactIntersectionCurve::Circle(circle));
1708    }
1709    Ok(Some(circles))
1710}
1711
1712/// Algebraic sphere-cylinder intersection (NURBS form for the general bounded
1713/// path). Delegates to [`exact_sphere_cylinder`] and samples each exact circle
1714/// into an interpolated NURBS `IntersectionCurve`. phase FF prefers the exact
1715/// circle form directly (so the section edge links to the coincident boundary
1716/// and the closed-circle splitter can carve the spherical band), but a caller
1717/// of `intersect_analytic_analytic_bounded` still gets clean curves instead of
1718/// the marcher's fragments.
1719fn algebraic_sphere_cylinder(
1720    sphere: &SphericalSurface,
1721    cyl: &CylindricalSurface,
1722) -> Result<Option<Vec<IntersectionCurve>>, MathError> {
1723    let Some(exacts) = exact_sphere_cylinder(sphere, cyl)? else {
1724        return Ok(None);
1725    };
1726
1727    let mut curves = Vec::new();
1728    for exact in exacts {
1729        let ExactIntersectionCurve::Circle(circle) = exact else {
1730            continue;
1731        };
1732        let n_samples = 33;
1733        let mut points = Vec::with_capacity(n_samples);
1734        let mut positions = Vec::with_capacity(n_samples);
1735        #[allow(clippy::cast_precision_loss)]
1736        for i in 0..n_samples {
1737            let theta = TAU * i as f64 / (n_samples - 1) as f64;
1738            let pt = crate::traits::ParametricCurve::evaluate(&circle, theta);
1739            positions.push(pt);
1740            points.push(IntersectionPoint {
1741                point: pt,
1742                param1: (0.0, 0.0),
1743                param2: (0.0, 0.0),
1744            });
1745        }
1746        let degree = 3.min(positions.len() - 1);
1747        let curve = interpolate(&positions, degree)?;
1748        curves.push(IntersectionCurve { curve, points });
1749    }
1750
1751    Ok(Some(curves))
1752}
1753
1754/// Algebraic cylinder-cylinder intersection for non-coaxial cylinders.
1755///
1756/// For two cylinders with axes that are NOT parallel, the intersection
1757/// consists of up to two closed space curves. These are found by
1758/// parameterizing one cylinder's angular coordinate `u ∈ [0, 2π]` and
1759/// solving a quadratic in the axial parameter `v` to find where each
1760/// "ring" of cylinder A sits on cylinder B.
1761///
1762/// The quadratic is:
1763///   `v²·(1 - α²) + 2v·(q·a₁ - α·q·a₂) + (|q|² - (q·a₂)² - r₂²) = 0`
1764/// where `α = a₁·a₂`, `q(u)` is the radial point on cylinder 1 minus
1765/// cylinder 2's origin, `a₁`/`a₂` are the cylinder axes, and `r₂` is
1766/// cylinder 2's radius.
1767#[allow(clippy::too_many_lines, clippy::unnecessary_wraps)]
1768fn algebraic_cylinder_cylinder(
1769    c1: &CylindricalSurface,
1770    c2: &CylindricalSurface,
1771) -> Result<Option<Vec<IntersectionCurve>>, MathError> {
1772    let alpha = c1.axis().dot(c2.axis());
1773    let a_coeff = 1.0 - alpha * alpha;
1774
1775    // Should only be called for non-parallel axes.
1776    if a_coeff.abs() < 1e-12 {
1777        return Ok(None);
1778    }
1779
1780    let r1 = c1.radius();
1781    let r2 = c2.radius();
1782    let o1 = c1.origin();
1783    let o2 = c2.origin();
1784    let a1 = c1.axis();
1785    let a2 = c2.axis();
1786    let x1 = c1.x_axis();
1787    let y1 = c1.y_axis();
1788
1789    // Separation check: distance between axes vs sum of radii.
1790    // Closest approach of two skew lines:
1791    let delta = Vec3::new(o1.x() - o2.x(), o1.y() - o2.y(), o1.z() - o2.z());
1792    let cross = a1.cross(a2);
1793    let cross_len = cross.length();
1794    if cross_len > 1e-12 {
1795        let axis_dist = delta.dot(cross).abs() / cross_len;
1796        if axis_dist > r1 + r2 + Tolerance::new().linear {
1797            return Ok(Some(vec![])); // No intersection
1798        }
1799    }
1800
1801    // Sample u from 0 to 2π on cylinder 1. Offset by half a step to avoid
1802    // landing exactly on crossing points where disc=0 and both curves coincide.
1803    // This ensures the two algebraic branches have distinct sample endpoints,
1804    // so the face splitter's wire builder doesn't face 4-way junction ambiguity.
1805    let n_samples = 128;
1806    let mut curve_plus: Vec<Point3> = Vec::with_capacity(n_samples + 1);
1807    let mut curve_minus: Vec<Point3> = Vec::with_capacity(n_samples + 1);
1808    let u_offset = TAU / (n_samples as f64 * 2.0); // half a step
1809
1810    // Sample n_samples DISTINCT points (no duplicate at closure).
1811    // After the loop, explicitly close each curve by copying the first point.
1812    #[allow(clippy::cast_precision_loss)]
1813    for i in 0..n_samples {
1814        let u = u_offset + TAU * i as f64 / n_samples as f64;
1815        let (sin_u, cos_u) = u.sin_cos();
1816
1817        // Radial point on c1 at angle u, height v=0:
1818        // q = c1.origin + r1*(cos(u)*x1 + sin(u)*y1) - c2.origin
1819        let qx = o1.x() + r1 * (cos_u * x1.x() + sin_u * y1.x()) - o2.x();
1820        let qy = o1.y() + r1 * (cos_u * x1.y() + sin_u * y1.y()) - o2.y();
1821        let qz = o1.z() + r1 * (cos_u * x1.z() + sin_u * y1.z()) - o2.z();
1822
1823        let q_dot_a1 = qx * a1.x() + qy * a1.y() + qz * a1.z();
1824        let q_dot_a2 = qx * a2.x() + qy * a2.y() + qz * a2.z();
1825        let q_sq = qx * qx + qy * qy + qz * qz;
1826
1827        let b_coeff = 2.0 * (q_dot_a1 - alpha * q_dot_a2);
1828        let c_coeff = q_sq - q_dot_a2 * q_dot_a2 - r2 * r2;
1829
1830        let disc = b_coeff * b_coeff - 4.0 * a_coeff * c_coeff;
1831        // Clamp tiny negative discriminant (floating-point noise near tangent
1832        // crossing points where disc → 0) to avoid gaps in the sample set.
1833        if disc < -Tolerance::new().linear {
1834            continue;
1835        }
1836
1837        let sqrt_disc = disc.max(0.0).sqrt();
1838        let v_plus = (-b_coeff + sqrt_disc) / (2.0 * a_coeff);
1839        let v_minus = (-b_coeff - sqrt_disc) / (2.0 * a_coeff);
1840
1841        curve_plus.push(c1.evaluate(u, v_plus));
1842        curve_minus.push(c1.evaluate(u, v_minus));
1843    }
1844
1845    // Explicitly close each curve by copying the first point (exact match
1846    // avoids near-zero chord length in NURBS interpolation).
1847    if !curve_plus.is_empty() {
1848        curve_plus.push(curve_plus[0]);
1849    }
1850    if !curve_minus.is_empty() {
1851        curve_minus.push(curve_minus[0]);
1852    }
1853
1854    let mut curves = Vec::new();
1855
1856    for pts in [&curve_plus, &curve_minus] {
1857        if pts.len() < 4 {
1858            continue;
1859        }
1860
1861        let ipts: Vec<IntersectionPoint> = pts
1862            .iter()
1863            .map(|&p| {
1864                let (u1, v1) = c1.project_point(p);
1865                let (u2, v2) = c2.project_point(p);
1866                IntersectionPoint {
1867                    point: p,
1868                    param1: (u1, v1),
1869                    param2: (u2, v2),
1870                }
1871            })
1872            .collect();
1873
1874        let degree = 3.min(pts.len() - 1);
1875        if let Ok(curve) = interpolate(pts, degree) {
1876            curves.push(IntersectionCurve {
1877                curve,
1878                points: ipts,
1879            });
1880        }
1881    }
1882
1883    Ok(Some(curves))
1884}
1885
1886/// Algebraic cone-cylinder intersection for PARALLEL (or antiparallel) axes.
1887///
1888/// When the axes are parallel, every plane perpendicular to them cuts the cone
1889/// in a circle of radius `rho = v * cos(half_angle)` about a FIXED centre and
1890/// the cylinder in a circle of radius `R` about a second FIXED centre, so the
1891/// axis separation `d` is constant in `v`. Two coplanar circles meet at
1892/// `u = phi0 +/- acos((d^2 + rho^2 - R^2) / (2*d*rho))`, giving two branches
1893/// parameterised exactly by the cone's own `v`. The branches exist only where
1894/// `rho` lies in `[|d - R|, d + R]`, which bounds the curve naturally.
1895///
1896/// This replaces the general grid-seeded marcher for the configuration, which
1897/// mis-handles it badly: seeds are accepted anywhere within half the surface
1898/// diagonal of the partner, the march-result dedup only consumes seeds the
1899/// traced polyline passes near, and the survivors are dozens of overlapping
1900/// partial traces of the same curve. Those fragments carry no usable in-face
1901/// span, so a cone corner-round crossed by a boss cylinder never splits (a
1902/// counterbore/countersink meeting a pad — the gridfinity lightweight base).
1903///
1904/// Returns `None` (defer to the caller's other paths) when the axes are not
1905/// parallel, or when they are coaxial — a coaxial pair degenerates to shared
1906/// circles, which [`exact_cone_cylinder`] emits exactly and phase FF calls
1907/// directly. Note that `intersect_analytic_analytic_bounded` does NOT consult
1908/// `exact_cone_cylinder`, so a coaxial pair reaching this path through that
1909/// caller falls through to the marcher; only the FF path gets the exact circles.
1910// Result-wrapped to match the other `try_algebraic_intersection` arms' shape.
1911#[allow(clippy::unnecessary_wraps)]
1912fn algebraic_parallel_cone_cylinder(
1913    cone: &ConicalSurface,
1914    cyl: &CylindricalSurface,
1915    v_range_cone: Option<(f64, f64)>,
1916    v_range_cyl: Option<(f64, f64)>,
1917) -> Result<Option<Vec<IntersectionCurve>>, MathError> {
1918    let axis = cone.axis();
1919    if axis.dot(cyl.axis()).abs() < 1.0 - 1e-10 {
1920        return Ok(None); // Skew/oblique — general marcher.
1921    }
1922
1923    let apex = cone.apex();
1924    let delta = cyl.origin() - apex;
1925    let along = delta.dot(axis);
1926    let perp = delta - axis * along;
1927    let d = perp.length();
1928    if d < 1e-9 {
1929        return Ok(None); // Coaxial — `exact_cone_cylinder` owns this.
1930    }
1931
1932    let (e1, e2) = (cone.x_axis(), cone.y_axis());
1933    let phi0 = perp.dot(e2).atan2(perp.dot(e1));
1934
1935    let (sin_t, cos_t) = cone.half_angle().sin_cos();
1936    if cos_t < 1e-12 || sin_t < 1e-12 {
1937        return Ok(None);
1938    }
1939    let r = cyl.radius();
1940
1941    // Branch existence: |d - R| <= rho <= d + R, with rho = v * cos(half_angle).
1942    let mut v_min = (d - r).abs() / cos_t;
1943    let mut v_max = (d + r) / cos_t;
1944    if v_max <= v_min {
1945        return Ok(Some(vec![]));
1946    }
1947
1948    // Narrow the sampled span to the faces' own extents so the fixed sample
1949    // budget resolves the in-face part of the curve rather than spreading over
1950    // a loop that mostly lies off both patches. A face's crossing can be a
1951    // fraction of a degree of the cone's sweep (the corner-round case above),
1952    // and an unnarrowed sampling puts fewer than one sample across it.
1953    let mut lo = v_min;
1954    let mut hi = v_max;
1955    // Clip EXACTLY to the hints, not to a padded window: an endpoint that lands
1956    // exactly on the face's own v-limit lies ON that boundary rim, so the
1957    // downstream pave machinery anchors it to the rim edge instead of leaving
1958    // the section dangling just past the face.
1959    if let Some((a, b)) = v_range_cone {
1960        let (a, b) = if a <= b { (a, b) } else { (b, a) };
1961        lo = lo.max(a);
1962        hi = hi.min(b);
1963    }
1964    if let Some((a, b)) = v_range_cyl {
1965        // The cylinder's v is a signed distance along its axis from its origin;
1966        // convert both ends to the cone's v via the shared axial direction.
1967        let flip = cyl.axis().dot(axis);
1968        let to_cone_v = |cv: f64| (along + cv * flip) / sin_t;
1969        let (a, b) = (to_cone_v(a), to_cone_v(b));
1970        let (a, b) = if a <= b { (a, b) } else { (b, a) };
1971        lo = lo.max(a);
1972        hi = hi.min(b);
1973    }
1974    v_min = lo.max(v_min);
1975    v_max = hi.min(v_max);
1976    if v_max - v_min <= 1e-12 {
1977        return Ok(Some(vec![]));
1978    }
1979
1980    let n_samples = 128;
1981    let mut plus: Vec<Point3> = Vec::with_capacity(n_samples + 1);
1982    let mut minus: Vec<Point3> = Vec::with_capacity(n_samples + 1);
1983    #[allow(clippy::cast_precision_loss)]
1984    for i in 0..=n_samples {
1985        let v = v_min + (v_max - v_min) * (i as f64) / (n_samples as f64);
1986        let rho = v * cos_t;
1987        if rho < 1e-12 {
1988            // The apex. `cos_alpha` has rho in its denominator, so it is only
1989            // meaningful in the limit: it tends to 0 (alpha -> pi/2) when the
1990            // cylinder passes exactly through the apex (d == R), and diverges
1991            // otherwise — where the clamp would manufacture a spurious alpha of
1992            // 0 or pi. So keep the apex only in the d == R case, where it is a
1993            // genuine point of the intersection and the shared endpoint at
1994            // which the two branches meet.
1995            if (d - r).abs() < 1e-12 {
1996                let apex = cone.evaluate(phi0, v);
1997                plus.push(apex);
1998                minus.push(apex);
1999            }
2000            continue;
2001        }
2002        let cos_alpha = ((d * d + rho * rho - r * r) / (2.0 * d * rho)).clamp(-1.0, 1.0);
2003        let alpha = cos_alpha.acos();
2004        plus.push(cone.evaluate(phi0 + alpha, v));
2005        minus.push(cone.evaluate(phi0 - alpha, v));
2006    }
2007
2008    let mut curves = Vec::new();
2009    for pts in [&plus, &minus] {
2010        // Fewer than four samples in range means this branch does not cross the
2011        // bounded region at all (the other branch may still).
2012        if pts.len() < 4 {
2013            continue;
2014        }
2015        let ipts: Vec<IntersectionPoint> = pts
2016            .iter()
2017            .map(|&p| IntersectionPoint {
2018                point: p,
2019                param1: cone.project_point(p),
2020                param2: cyl.project_point(p),
2021            })
2022            .collect();
2023        let degree = 3.min(pts.len() - 1);
2024        match interpolate(pts, degree) {
2025            Ok(curve) => curves.push(IntersectionCurve {
2026                curve,
2027                points: ipts,
2028            }),
2029            // Emitting only the branch that happened to fit would starve the
2030            // section chain of exactly the piece this path exists to supply —
2031            // the same silent half-answer the marcher's fragments produced.
2032            // Defer the whole pair to the caller's other paths instead.
2033            Err(_) => return Ok(None),
2034        }
2035    }
2036
2037    Ok(Some(curves))
2038}
2039
2040/// Algebraic sphere-sphere intersection.
2041///
2042/// Two spheres intersect in a circle lying in the radical plane.
2043/// The radical plane is perpendicular to the line connecting the centers,
2044/// at a distance d1 from center1 where:
2045///   d1 = (D² + R1² - R2²) / (2D)
2046/// and D is the distance between centers.
2047fn algebraic_sphere_sphere(
2048    s1: &SphericalSurface,
2049    s2: &SphericalSurface,
2050) -> Result<Vec<IntersectionCurve>, MathError> {
2051    let c1 = s1.center();
2052    let c2 = s2.center();
2053    let r1 = s1.radius();
2054    let r2 = s2.radius();
2055
2056    let delta = c2 - c1;
2057    let d_sq = delta.x() * delta.x() + delta.y() * delta.y() + delta.z() * delta.z();
2058    let d = d_sq.sqrt();
2059
2060    if d < 1e-12 {
2061        // Concentric spheres: no intersection (unless same radius → degenerate).
2062        return Ok(vec![]);
2063    }
2064
2065    // Check separation conditions.
2066    if d > r1 + r2 + 1e-10 {
2067        return Ok(vec![]); // Too far apart
2068    }
2069    if d + r2.min(r1) + 1e-10 < r1.max(r2) {
2070        return Ok(vec![]); // One inside the other
2071    }
2072
2073    // Distance from c1 to the radical plane along the center line.
2074    let d1 = (d_sq + r1 * r1 - r2 * r2) / (2.0 * d);
2075
2076    // Radius of the intersection circle.
2077    let r_circle_sq = r1 * r1 - d1 * d1;
2078    if r_circle_sq < 0.0 {
2079        // Tangent or no intersection (numerical noise).
2080        if r_circle_sq > -1e-10 {
2081            // Tangent: single point.
2082            let axis = Vec3::new(delta.x() / d, delta.y() / d, delta.z() / d);
2083            let tangent_pt = Point3::new(
2084                c1.x() + axis.x() * d1,
2085                c1.y() + axis.y() * d1,
2086                c1.z() + axis.z() * d1,
2087            );
2088            let ipt = IntersectionPoint {
2089                point: tangent_pt,
2090                param1: (0.0, 0.0),
2091                param2: (0.0, 0.0),
2092            };
2093            // Single-point "curve" — not very useful but correct.
2094            return Ok(vec![IntersectionCurve {
2095                curve: interpolate(&[tangent_pt, tangent_pt], 1)?,
2096                points: vec![ipt],
2097            }]);
2098        }
2099        return Ok(vec![]);
2100    }
2101
2102    let r_circle = r_circle_sq.sqrt();
2103    let axis = Vec3::new(delta.x() / d, delta.y() / d, delta.z() / d);
2104    let center = Point3::new(
2105        c1.x() + axis.x() * d1,
2106        c1.y() + axis.y() * d1,
2107        c1.z() + axis.z() * d1,
2108    );
2109
2110    // Build a reference frame for the circle.
2111    let basis = Frame3::from_normal(center, axis)?;
2112    let u_dir = basis.x;
2113    let v_dir = basis.y;
2114
2115    // Sample the circle for the IntersectionCurve representation.
2116    let n_samples = 33; // Odd for symmetry
2117    let mut points = Vec::with_capacity(n_samples);
2118    let mut positions = Vec::with_capacity(n_samples);
2119    #[allow(clippy::cast_precision_loss)]
2120    for i in 0..n_samples {
2121        let theta = TAU * i as f64 / (n_samples - 1) as f64;
2122        let (sin_t, cos_t) = theta.sin_cos();
2123        let pt = Point3::new(
2124            center.x() + (u_dir.x() * cos_t + v_dir.x() * sin_t) * r_circle,
2125            center.y() + (u_dir.y() * cos_t + v_dir.y() * sin_t) * r_circle,
2126            center.z() + (u_dir.z() * cos_t + v_dir.z() * sin_t) * r_circle,
2127        );
2128        positions.push(pt);
2129        points.push(IntersectionPoint {
2130            point: pt,
2131            param1: (0.0, 0.0),
2132            param2: (0.0, 0.0),
2133        });
2134    }
2135
2136    let degree = 3.min(positions.len() - 1);
2137    let curve = interpolate(&positions, degree)?;
2138
2139    Ok(vec![IntersectionCurve { curve, points }])
2140}
2141
2142/// Newton correction: project a point back onto the intersection curve
2143/// of two analytic surfaces. Solves the 3×3 system:
2144///   δ · na = -da  (eliminate distance to surface A)
2145///   δ · nb = -db  (eliminate distance to surface B)
2146///   δ · t  = 0    (minimal correction, perpendicular to tangent)
2147#[allow(clippy::too_many_arguments)]
2148fn correct_to_intersection(
2149    a: &AnalyticSurface<'_>,
2150    b: &AnalyticSurface<'_>,
2151    surf_a: &dyn Fn(f64, f64) -> Point3,
2152    norm_a: &dyn Fn(f64, f64) -> Vec3,
2153    surf_b: &dyn Fn(f64, f64) -> Point3,
2154    norm_b: &dyn Fn(f64, f64) -> Vec3,
2155    point: Point3,
2156    u_range_a: (f64, f64),
2157    v_range_a: (f64, f64),
2158    u_range_b: (f64, f64),
2159    v_range_b: (f64, f64),
2160    max_iters: usize,
2161) -> Point3 {
2162    let mut p = point;
2163    for _ in 0..max_iters {
2164        let (ua, va) = project_analytic(a, p, u_range_a, v_range_a);
2165        let (ub, vb) = project_analytic(b, p, u_range_b, v_range_b);
2166        let pa = surf_a(ua, va);
2167        let pb = surf_b(ub, vb);
2168        let na = norm_a(ua, va);
2169        let nb = norm_b(ub, vb);
2170        let pv = Vec3::new(p.x(), p.y(), p.z());
2171
2172        let da = (pv - Vec3::new(pa.x(), pa.y(), pa.z())).dot(na);
2173        let db = (pv - Vec3::new(pb.x(), pb.y(), pb.z())).dot(nb);
2174
2175        if da.abs() < 1e-7 && db.abs() < 1e-7 {
2176            break;
2177        }
2178
2179        let t = na.cross(nb);
2180        let t_len = t.length();
2181        if t_len < 1e-10 {
2182            // Surfaces are tangent — fall back to midpoint.
2183            return Point3::new(
2184                (pa.x() + pb.x()) * 0.5,
2185                (pa.y() + pb.y()) * 0.5,
2186                (pa.z() + pb.z()) * 0.5,
2187            );
2188        }
2189        let t_hat = t * (1.0 / t_len);
2190
2191        // Solve [na; nb; t_hat] · δ = [-da, -db, 0] via Cramer's rule.
2192        let det = na.x() * (nb.y() * t_hat.z() - nb.z() * t_hat.y())
2193            - na.y() * (nb.x() * t_hat.z() - nb.z() * t_hat.x())
2194            + na.z() * (nb.x() * t_hat.y() - nb.y() * t_hat.x());
2195        if det.abs() < 1e-15 {
2196            return Point3::new(
2197                (pa.x() + pb.x()) * 0.5,
2198                (pa.y() + pb.y()) * 0.5,
2199                (pa.z() + pb.z()) * 0.5,
2200            );
2201        }
2202        let inv = 1.0 / det;
2203        // Cramer's rule: replace each column of A with rhs = (-da, -db, 0).
2204        let dx = inv
2205            * (-da * (nb.y() * t_hat.z() - nb.z() * t_hat.y())
2206                + db * (na.y() * t_hat.z() - na.z() * t_hat.y()));
2207        let dy = inv
2208            * (da * (nb.x() * t_hat.z() - nb.z() * t_hat.x())
2209                - db * (na.x() * t_hat.z() - na.z() * t_hat.x()));
2210        let dz = inv
2211            * (-da * (nb.x() * t_hat.y() - nb.y() * t_hat.x())
2212                + db * (na.x() * t_hat.y() - na.y() * t_hat.x()));
2213        let candidate = Point3::new(p.x() + dx, p.y() + dy, p.z() + dz);
2214
2215        // Divergence guard: if the correction moves farther from both
2216        // surfaces, abandon Newton and return the best point so far.
2217        let (uc, vc) = project_analytic(a, candidate, u_range_a, v_range_a);
2218        let (ud, vd) = project_analytic(b, candidate, u_range_b, v_range_b);
2219        let pc_a = surf_a(uc, vc);
2220        let pc_b = surf_b(ud, vd);
2221        let cv = Vec3::new(candidate.x(), candidate.y(), candidate.z());
2222        let da_new = (cv - Vec3::new(pc_a.x(), pc_a.y(), pc_a.z()))
2223            .dot(norm_a(uc, vc))
2224            .abs();
2225        let db_new = (cv - Vec3::new(pc_b.x(), pc_b.y(), pc_b.z()))
2226            .dot(norm_b(ud, vd))
2227            .abs();
2228        if da_new > da.abs() && db_new > db.abs() {
2229            return p;
2230        }
2231
2232        p = candidate;
2233    }
2234    p
2235}
2236
2237/// March along the intersection of two surfaces from a seed point.
2238///
2239/// Uses the cross product of surface normals as the tangent direction
2240/// and projects back onto both surfaces using analytical projection
2241/// (for cylinders/spheres) or grid search (fallback).
2242#[allow(clippy::too_many_arguments)]
2243fn march_analytic_intersection(
2244    a: &AnalyticSurface<'_>,
2245    b: &AnalyticSurface<'_>,
2246    surf_a: &dyn Fn(f64, f64) -> Point3,
2247    norm_a: &dyn Fn(f64, f64) -> Vec3,
2248    surf_b: &dyn Fn(f64, f64) -> Point3,
2249    norm_b: &dyn Fn(f64, f64) -> Vec3,
2250    seed: Point3,
2251    u_range_a: (f64, f64),
2252    v_range_a: (f64, f64),
2253    u_range_b: (f64, f64),
2254    v_range_b: (f64, f64),
2255    initial_step: f64,
2256    u_periodic_a: bool,
2257    u_periodic_b: bool,
2258) -> Vec<Point3> {
2259    let max_steps = 500;
2260    let h_min = 1e-6;
2261    let h_max = initial_step * 4.0;
2262    // Fixed closure threshold: the adaptive step `h` varies with curvature
2263    // and can shrink below the actual miss distance at the seed re-approach.
2264    // Use `initial_step * 5` to robustly detect closure on the first pass.
2265    let closure_dist = initial_step * 5.0;
2266    // Angular thresholds for curvature-adaptive stepping.
2267    let max_angle = 10.0_f64.to_radians();
2268    let min_angle = 2.0_f64.to_radians();
2269
2270    // March forward from seed, collecting points.
2271    let mut forward = Vec::new();
2272    // March backward from seed, collecting points (reversed at end).
2273    let mut backward = Vec::new();
2274
2275    for (direction, points) in [(1.0_f64, &mut forward), (-1.0_f64, &mut backward)] {
2276        let mut current = seed;
2277        let mut h = initial_step;
2278        let mut prev_tangent: Option<Vec3> = None;
2279
2280        for _ in 0..max_steps {
2281            let (ua, va) = project_analytic(a, current, u_range_a, v_range_a);
2282            let (ub, vb) = project_analytic(b, current, u_range_b, v_range_b);
2283
2284            let na = norm_a(ua, va);
2285            let nb = norm_b(ub, vb);
2286
2287            let tangent = na.cross(nb);
2288            let t_len = tangent.length();
2289            if t_len < 1e-10 {
2290                break;
2291            }
2292            let t_dir = tangent * (direction / t_len);
2293
2294            // Curvature-adaptive step: check angular deviation from previous tangent.
2295            if let Some(prev_t) = prev_tangent {
2296                let cos_angle = prev_t.dot(t_dir).clamp(-1.0, 1.0);
2297                let angle = cos_angle.acos();
2298                if angle > max_angle && h > h_min {
2299                    h = (h * 0.5).max(h_min);
2300                } else if angle < min_angle {
2301                    h = (h * 2.0).min(h_max);
2302                }
2303            }
2304            prev_tangent = Some(t_dir);
2305
2306            let next = Point3::new(
2307                h.mul_add(t_dir.x(), current.x()),
2308                h.mul_add(t_dir.y(), current.y()),
2309                h.mul_add(t_dir.z(), current.z()),
2310            );
2311
2312            let (ua2, va2) = project_analytic(a, next, u_range_a, v_range_a);
2313            let (ub2, vb2) = project_analytic(b, next, u_range_b, v_range_b);
2314
2315            let pa = surf_a(ua2, va2);
2316            let pb = surf_b(ub2, vb2);
2317            let mid = Point3::new(
2318                (pa.x() + pb.x()) * 0.5,
2319                (pa.y() + pb.y()) * 0.5,
2320                (pa.z() + pb.z()) * 0.5,
2321            );
2322            let out_a = (!u_periodic_a && (ua2 <= u_range_a.0 || ua2 >= u_range_a.1))
2323                || va2 <= v_range_a.0
2324                || va2 >= v_range_a.1;
2325            let out_b = (!u_periodic_b && (ub2 <= u_range_b.0 || ub2 >= u_range_b.1))
2326                || vb2 <= v_range_b.0
2327                || vb2 >= v_range_b.1;
2328
2329            if out_a || out_b {
2330                break;
2331            }
2332
2333            // Check for loop closure — if we've collected enough points and
2334            // the current point is close to the seed, the curve is closed.
2335            // Require ≥10 steps to avoid premature closure near the seed.
2336            let dist_to_seed = (mid - seed).length();
2337            if points.len() > 10 && dist_to_seed < closure_dist {
2338                points.push(seed);
2339                break;
2340            }
2341
2342            points.push(mid);
2343            current = mid;
2344        }
2345    }
2346
2347    // Assemble result: backward (reversed) + seed + forward
2348    backward.reverse();
2349    let mut result = backward;
2350    result.push(seed);
2351    result.append(&mut forward);
2352
2353    // Refine all points onto the intersection curve via Newton correction.
2354    for pt in &mut result {
2355        *pt = correct_to_intersection(
2356            a, b, surf_a, norm_a, surf_b, norm_b, *pt, u_range_a, v_range_a, u_range_b, v_range_b,
2357            5,
2358        );
2359    }
2360
2361    result
2362}
2363
2364/// Project a 3D point onto an analytic surface using the surface's
2365/// analytical projection method. Falls back to grid search for surface
2366/// types without analytical projection.
2367fn project_analytic(
2368    surface: &AnalyticSurface<'_>,
2369    point: Point3,
2370    u_range: (f64, f64),
2371    v_range: (f64, f64),
2372) -> (f64, f64) {
2373    match surface {
2374        AnalyticSurface::Cylinder(cyl) => {
2375            let (u, v) = cyl.project_point(point);
2376            (u.clamp(u_range.0, u_range.1), v.clamp(v_range.0, v_range.1))
2377        }
2378        AnalyticSurface::Sphere(sphere) => {
2379            let (u, v) = sphere.project_point(point);
2380            (u.clamp(u_range.0, u_range.1), v.clamp(v_range.0, v_range.1))
2381        }
2382        AnalyticSurface::Cone(cone) => {
2383            let (u, v) = cone.project_point(point);
2384            (u.clamp(u_range.0, u_range.1), v.clamp(v_range.0, v_range.1))
2385        }
2386        AnalyticSurface::Torus(torus) => {
2387            let (u, v) = torus.project_point(point);
2388            (u.clamp(u_range.0, u_range.1), v.clamp(v_range.0, v_range.1))
2389        }
2390    }
2391}
2392
2393/// Returns `true` if the surface's u-parameter is periodic (wraps around 2π).
2394/// All current `AnalyticSurface` variants have periodic u — this is trivially
2395/// true today but exists as a guard for future non-periodic analytic types.
2396fn is_u_periodic(surface: &AnalyticSurface<'_>) -> bool {
2397    matches!(
2398        surface,
2399        AnalyticSurface::Cylinder(_)
2400            | AnalyticSurface::Cone(_)
2401            | AnalyticSurface::Sphere(_)
2402            | AnalyticSurface::Torus(_)
2403    )
2404}
2405
2406/// Extract closures and parameter ranges for an analytic surface.
2407#[allow(clippy::type_complexity)]
2408fn surface_closures<'a>(
2409    surface: &'a AnalyticSurface<'a>,
2410) -> (
2411    Box<dyn Fn(f64, f64) -> Point3 + 'a>,
2412    Box<dyn Fn(f64, f64) -> Vec3 + 'a>,
2413    (f64, f64),
2414    (f64, f64),
2415) {
2416    match surface {
2417        AnalyticSurface::Cylinder(cyl) => (
2418            Box::new(|u, v| cyl.evaluate(u, v)),
2419            Box::new(|u, v| cyl.normal(u, v)),
2420            (0.0, TAU),
2421            (-1.0, 1.0),
2422        ),
2423        AnalyticSurface::Cone(cone) => (
2424            Box::new(|u, v| cone.evaluate(u, v)),
2425            Box::new(|u, v| cone.normal(u, v)),
2426            (0.0, TAU),
2427            (0.01, 2.0),
2428        ),
2429        AnalyticSurface::Sphere(sphere) => (
2430            Box::new(|u, v| sphere.evaluate(u, v)),
2431            Box::new(|u, v| sphere.normal(u, v)),
2432            (0.0, TAU),
2433            (-FRAC_PI_2, FRAC_PI_2),
2434        ),
2435        AnalyticSurface::Torus(torus) => (
2436            Box::new(|u, v| torus.evaluate(u, v)),
2437            Box::new(|u, v| torus.normal(u, v)),
2438            (0.0, TAU),
2439            (0.0, TAU),
2440        ),
2441    }
2442}
2443
2444#[cfg(test)]
2445#[allow(clippy::unwrap_used, clippy::expect_used)]
2446mod tests {
2447    use super::*;
2448    use crate::tolerance::Tolerance;
2449
2450    #[test]
2451    fn plane_cylinder_perpendicular() {
2452        let cyl =
2453            CylindricalSurface::new(Point3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 2.0)
2454                .unwrap();
2455
2456        // Horizontal plane at z=3 -- produces a circle at height 3.
2457        let curves = intersect_plane_cylinder(&cyl, Vec3::new(0.0, 0.0, 1.0), 3.0).unwrap();
2458        assert!(!curves.is_empty(), "should find intersection curve");
2459        assert!(
2460            curves[0].points.len() > 10,
2461            "should have many sample points"
2462        );
2463
2464        let tol = Tolerance::loose();
2465        for pt in &curves[0].points {
2466            assert!(
2467                tol.approx_eq(pt.point.z(), 3.0),
2468                "z should be ~3.0, got {}",
2469                pt.point.z()
2470            );
2471            let r = pt.point.x().hypot(pt.point.y());
2472            assert!(tol.approx_eq(r, 2.0), "radius should be ~2.0, got {r}");
2473        }
2474    }
2475
2476    #[test]
2477    fn plane_sphere_equator() {
2478        let sphere = SphericalSurface::new(Point3::new(0.0, 0.0, 0.0), 3.0).unwrap();
2479
2480        let curves = intersect_plane_sphere(&sphere, Vec3::new(0.0, 0.0, 1.0), 0.0).unwrap();
2481        assert!(!curves.is_empty());
2482
2483        let tol = Tolerance::loose();
2484        for pt in &curves[0].points {
2485            assert!(
2486                tol.approx_eq(pt.point.z(), 0.0),
2487                "z should be ~0, got {}",
2488                pt.point.z()
2489            );
2490            let r = pt.point.x().hypot(pt.point.y());
2491            assert!(tol.approx_eq(r, 3.0), "radius should be ~3.0, got {r}");
2492        }
2493    }
2494
2495    #[test]
2496    fn plane_sphere_no_intersection() {
2497        let sphere = SphericalSurface::new(Point3::new(0.0, 0.0, 0.0), 1.0).unwrap();
2498
2499        let curves = intersect_plane_sphere(&sphere, Vec3::new(0.0, 0.0, 1.0), 5.0).unwrap();
2500        assert!(curves.is_empty());
2501    }
2502
2503    #[test]
2504    fn plane_cone_cross_section() {
2505        let cone = ConicalSurface::new(
2506            Point3::new(0.0, 0.0, 0.0),
2507            Vec3::new(0.0, 0.0, 1.0),
2508            std::f64::consts::FRAC_PI_4,
2509        )
2510        .unwrap();
2511
2512        let curves = intersect_plane_cone(&cone, Vec3::new(0.0, 0.0, 1.0), 1.0).unwrap();
2513        assert!(!curves.is_empty(), "should find intersection with cone");
2514    }
2515
2516    #[test]
2517    fn coaxial_cones_cross_at_single_circle() {
2518        // Two coaxial truncated cones (outer base r10->top r8, inner r9->r8
2519        // over height 10) cross where their radii match: z=10, r=8. The
2520        // intersection must be ONE clean circle, not the dozens of degenerate
2521        // micro-curves the general marcher produces at near-tangency.
2522        let outer = ConicalSurface::new(
2523            Point3::new(0.0, 0.0, 50.0),
2524            Vec3::new(0.0, 0.0, -1.0),
2525            5.0_f64.atan(),
2526        )
2527        .unwrap();
2528        let inner = ConicalSurface::new(
2529            Point3::new(0.0, 0.0, 90.0),
2530            Vec3::new(0.0, 0.0, -1.0),
2531            10.0_f64.atan(),
2532        )
2533        .unwrap();
2534
2535        let curves = intersect_analytic_analytic_bounded(
2536            AnalyticSurface::Cone(&outer),
2537            AnalyticSurface::Cone(&inner),
2538            32,
2539            None,
2540            None,
2541        )
2542        .unwrap();
2543
2544        assert_eq!(
2545            curves.len(),
2546            1,
2547            "coaxial cones crossing at one circle must yield exactly one curve, got {}",
2548            curves.len()
2549        );
2550        for p in &curves[0].points {
2551            let r = p.point.x().hypot(p.point.y());
2552            assert!(
2553                (p.point.z() - 10.0).abs() < 1e-6 && (r - 8.0).abs() < 1e-6,
2554                "intersection point off the expected z=10,r=8 circle: {:?}",
2555                p.point
2556            );
2557        }
2558    }
2559
2560    #[test]
2561    fn plane_torus_cross_section() {
2562        let torus = ToroidalSurface::new(Point3::new(0.0, 0.0, 0.0), 5.0, 1.0).unwrap();
2563
2564        let curves = intersect_plane_torus(&torus, Vec3::new(0.0, 0.0, 1.0), 0.0).unwrap();
2565        assert!(
2566            !curves.is_empty(),
2567            "should find intersection curves with torus"
2568        );
2569    }
2570
2571    /// Signed distance of a point to a z-axis torus centred at the origin:
2572    /// `sqrt((sqrt(x^2+y^2) - R)^2 + z^2) - r`.
2573    fn torus_implicit(p: Point3, major: f64, minor: f64) -> f64 {
2574        let rho = p.x().hypot(p.y());
2575        ((rho - major).hypot(p.z())) - minor
2576    }
2577
2578    /// The gridfinity lightweight base's failing corner, reduced: a cavity
2579    /// corner-round cone (apex below the floor, 45 deg, axis +z) crossed by a
2580    /// parallel-axis boss cylinder. The general marcher returned ~49 overlapping
2581    /// partial traces of one curve here; the algebraic path must return exactly
2582    /// the two branches, each ON both surfaces and inside the cone's v-hint.
2583    #[test]
2584    fn parallel_cone_cylinder_gives_two_exact_branches() {
2585        use crate::traits::ParametricCurve;
2586        let cone = ConicalSurface::new(
2587            Point3::new(-5.45, -36.55, -4.85),
2588            Vec3::new(0.0, 0.0, 1.0),
2589            std::f64::consts::FRAC_PI_4,
2590        )
2591        .unwrap();
2592        let cyl = CylindricalSurface::new(
2593            Point3::new(-8.0, -34.0, -5.0),
2594            Vec3::new(0.0, 0.0, 1.0),
2595            4.45,
2596        )
2597        .unwrap();
2598        // The cone face spans z in [-3.8, -3.0]; v = (z - apex_z) / sin(45 deg).
2599        let v_hint = (1.484_924_240_492_058, 2.616_295_090_390_43);
2600        let curves = intersect_analytic_analytic_bounded(
2601            AnalyticSurface::Cone(&cone),
2602            AnalyticSurface::Cylinder(&cyl),
2603            32,
2604            Some(v_hint),
2605            Some((0.0, 2.5)),
2606        )
2607        .unwrap();
2608
2609        assert_eq!(curves.len(), 2, "expected exactly the two branches");
2610        for c in &curves {
2611            let (t0, t1) = c.curve.domain();
2612            for k in 0..=32 {
2613                let t = (t1 - t0).mul_add(f64::from(k) / 32.0, t0);
2614                let p = ParametricCurve::evaluate(&c.curve, t);
2615                // On the cylinder: radial distance from its axis is the radius.
2616                let radial = ((p.x() + 8.0).powi(2) + (p.y() + 34.0).powi(2)).sqrt();
2617                assert!((radial - 4.45).abs() < 1e-6, "off cylinder: {radial}");
2618                // On the cone: radial distance from its axis is z - apex_z.
2619                let cone_r = ((p.x() + 5.45).powi(2) + (p.y() + 36.55).powi(2)).sqrt();
2620                assert!((cone_r - (p.z() + 4.85)).abs() < 1e-6, "off cone at {p:?}");
2621                // Inside the cone face's own v-window (the hint is respected).
2622                assert!(p.z() >= -3.8 - 1e-9 && p.z() <= -3.0 + 1e-9, "z={}", p.z());
2623            }
2624        }
2625    }
2626
2627    /// A coaxial pair has no radical line; the algebraic path must defer rather
2628    /// than divide by a zero axis separation.
2629    #[test]
2630    fn coaxial_cone_cylinder_defers_to_other_paths() {
2631        let cone = ConicalSurface::new(
2632            Point3::new(0.0, 0.0, 0.0),
2633            Vec3::new(0.0, 0.0, 1.0),
2634            std::f64::consts::FRAC_PI_4,
2635        )
2636        .unwrap();
2637        let cyl =
2638            CylindricalSurface::new(Point3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 2.0)
2639                .unwrap();
2640        assert!(
2641            algebraic_parallel_cone_cylinder(&cone, &cyl, None, None)
2642                .unwrap()
2643                .is_none()
2644        );
2645    }
2646
2647    #[test]
2648    fn oblique_cone_cylinder_defers_to_other_paths() {
2649        let cone = ConicalSurface::new(
2650            Point3::new(0.0, 0.0, 0.0),
2651            Vec3::new(0.0, 0.0, 1.0),
2652            std::f64::consts::FRAC_PI_4,
2653        )
2654        .unwrap();
2655        let cyl =
2656            CylindricalSurface::new(Point3::new(3.0, 0.0, 1.0), Vec3::new(1.0, 0.0, 0.0), 1.0)
2657                .unwrap();
2658        assert!(
2659            algebraic_parallel_cone_cylinder(&cone, &cyl, None, None)
2660                .unwrap()
2661                .is_none()
2662        );
2663    }
2664
2665    #[test]
2666    fn plane_torus_lobe_closes_and_stays_on_surface() {
2667        use crate::traits::ParametricCurve;
2668        let (major, minor) = (10.0, 3.0);
2669        let torus = ToroidalSurface::new(Point3::new(0.0, 0.0, 0.0), major, minor).unwrap();
2670
2671        // The census cutting planes (y=-4, x=6) each cut the +x and -x tube lobes
2672        // in a CLOSED oval. The greedy marcher stops one grid step short of
2673        // closing; the wrap-close must make every fitted lobe close exactly.
2674        for (n, d) in [
2675            (Vec3::new(0.0, -1.0, 0.0), 4.0),  // y = -4
2676            (Vec3::new(-1.0, 0.0, 0.0), -6.0), // x = 6
2677            (Vec3::new(0.0, 0.0, 1.0), 0.0),   // z = 0 -> two concentric circles
2678        ] {
2679            let curves = intersect_plane_torus(&torus, n, d).unwrap();
2680            assert!(!curves.is_empty(), "plane n={n:?} d={d} found no curves");
2681            for c in &curves {
2682                let p0 = ParametricCurve::evaluate(&c.curve, 0.0);
2683                let p1 = ParametricCurve::evaluate(&c.curve, 1.0);
2684                assert!(
2685                    (p0 - p1).length() < 1e-7,
2686                    "lobe not closed: gap={} (n={n:?} d={d})",
2687                    (p0 - p1).length()
2688                );
2689                // Every fitted sample stays on the torus (shape-preserving).
2690                for k in 0..=64 {
2691                    let t = f64::from(k) / 64.0;
2692                    let p = ParametricCurve::evaluate(&c.curve, t);
2693                    assert!(
2694                        torus_implicit(p, major, minor).abs() < 1e-2,
2695                        "off-surface point {p:?} implicit={}",
2696                        torus_implicit(p, major, minor)
2697                    );
2698                }
2699            }
2700        }
2701    }
2702
2703    #[test]
2704    fn plane_torus_inner_tangent_figure_eight_stays_open() {
2705        use crate::traits::ParametricCurve;
2706        let (major, minor) = (10.0, 3.0);
2707        let torus = ToroidalSurface::new(Point3::new(0.0, 0.0, 0.0), major, minor).unwrap();
2708
2709        // A plane tangent to the inner equator (x = major - minor = 7) cuts a
2710        // self-touching figure-eight. The marcher traces it as a single chain
2711        // whose end lands on the opposite lobe — FAR from its start (gap is many
2712        // point-spacings). The wrap-close must NOT force-close this into a wrong
2713        // loop; it must stay OPEN so a self-touching curve is never sealed.
2714        let curves =
2715            intersect_plane_torus(&torus, Vec3::new(-1.0, 0.0, 0.0), -(major - minor)).unwrap();
2716        assert!(!curves.is_empty(), "inner-tangent plane found no curves");
2717        let max_gap = curves
2718            .iter()
2719            .map(|c| {
2720                let p0 = ParametricCurve::evaluate(&c.curve, 0.0);
2721                let p1 = ParametricCurve::evaluate(&c.curve, 1.0);
2722                (p0 - p1).length()
2723            })
2724            .fold(0.0_f64, f64::max);
2725        assert!(
2726            max_gap > 1e-2,
2727            "figure-eight chain was wrongly force-closed (max end-gap={max_gap})"
2728        );
2729    }
2730
2731    #[test]
2732    fn line_torus_box_edge_crossing_is_exact() {
2733        // The census box edge x=6, y=-4 (z varying) crosses the torus (R=10,r=3)
2734        // at z = ±sqrt(r² − (rho−R)²), rho = hypot(6,4) ≈ 7.2111 → z ≈ ±1.1055.
2735        let torus = ToroidalSurface::new(Point3::new(0.0, 0.0, 0.0), 10.0, 3.0).unwrap();
2736        let ts = intersect_line_torus(
2737            &torus,
2738            Point3::new(6.0, -4.0, -5.0),
2739            Vec3::new(0.0, 0.0, 1.0),
2740        );
2741        // Vertical line through (6,-4) meets the tube twice.
2742        assert_eq!(ts.len(), 2, "expected 2 crossings, got {ts:?}");
2743        let zs: Vec<f64> = ts.iter().map(|t| -5.0 + t).collect();
2744        let rho = 6.0_f64.hypot(4.0);
2745        let z_exp = (9.0 - (rho - 10.0).powi(2)).sqrt();
2746        assert!(
2747            (zs[0] - (-z_exp)).abs() < 1e-9,
2748            "z0={} exp={}",
2749            zs[0],
2750            -z_exp
2751        );
2752        assert!((zs[1] - z_exp).abs() < 1e-9, "z1={} exp={}", zs[1], z_exp);
2753        // Each crossing lies on the torus.
2754        for &t in &ts {
2755            let p = Point3::new(6.0, -4.0, -5.0 + t);
2756            let rho = p.x().hypot(p.y());
2757            let impl_v = (rho - 10.0).hypot(p.z()) - 3.0;
2758            assert!(impl_v.abs() < 1e-9, "off-torus impl={impl_v}");
2759        }
2760    }
2761
2762    #[test]
2763    fn line_torus_miss_and_tangent() {
2764        let torus = ToroidalSurface::new(Point3::new(0.0, 0.0, 0.0), 10.0, 3.0).unwrap();
2765        // A vertical line at rho beyond the outer rim (x=20) misses entirely.
2766        let miss = intersect_line_torus(
2767            &torus,
2768            Point3::new(20.0, 0.0, 0.0),
2769            Vec3::new(0.0, 0.0, 1.0),
2770        );
2771        assert!(miss.is_empty(), "expected no crossings, got {miss:?}");
2772        // The z-axis (rho=0) passes through the hole — no intersection.
2773        let axis =
2774            intersect_line_torus(&torus, Point3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0));
2775        assert!(axis.is_empty(), "z-axis should miss the tube, got {axis:?}");
2776    }
2777
2778    #[test]
2779    fn dispatch_via_analytic_surface() {
2780        let cyl =
2781            CylindricalSurface::new(Point3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 1.0)
2782                .unwrap();
2783        let curves = intersect_plane_analytic(
2784            AnalyticSurface::Cylinder(&cyl),
2785            Vec3::new(0.0, 0.0, 1.0),
2786            0.0,
2787        )
2788        .unwrap();
2789        assert!(!curves.is_empty());
2790    }
2791
2792    #[test]
2793    fn perpendicular_cylinders_intersect() {
2794        let cyl_z =
2795            CylindricalSurface::new(Point3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 1.0)
2796                .unwrap();
2797        let cyl_x =
2798            CylindricalSurface::new(Point3::new(0.0, 0.0, 0.0), Vec3::new(1.0, 0.0, 0.0), 1.0)
2799                .unwrap();
2800
2801        let curves = intersect_analytic_analytic(
2802            AnalyticSurface::Cylinder(&cyl_z),
2803            AnalyticSurface::Cylinder(&cyl_x),
2804            16,
2805        )
2806        .unwrap();
2807
2808        assert!(
2809            !curves.is_empty(),
2810            "perpendicular cylinders should intersect"
2811        );
2812
2813        for c in &curves {
2814            assert!(
2815                c.points.len() >= 2,
2816                "intersection curve should have >= 2 points, got {}",
2817                c.points.len()
2818            );
2819        }
2820    }
2821
2822    #[test]
2823    fn sphere_cylinder_intersect() {
2824        let sphere = SphericalSurface::new(Point3::new(0.0, 0.0, 0.0), 2.0).unwrap();
2825        let cyl =
2826            CylindricalSurface::new(Point3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 1.0)
2827                .unwrap();
2828
2829        let curves = intersect_analytic_analytic(
2830            AnalyticSurface::Sphere(&sphere),
2831            AnalyticSurface::Cylinder(&cyl),
2832            16,
2833        )
2834        .unwrap();
2835
2836        // A sphere of radius 2 and a cylinder of radius 1, both centered
2837        // at the origin, should intersect (the cylinder passes through
2838        // the sphere).
2839        assert!(!curves.is_empty(), "sphere and cylinder should intersect");
2840    }
2841
2842    #[test]
2843    fn exact_sphere_cylinder_coaxial_two_circles() {
2844        // Sphere r=6 at origin, coaxial cylinder r=3 along z: two latitude
2845        // circles at z = ±sqrt(36-9) = ±sqrt(27), each of radius 3.
2846        let sphere = SphericalSurface::new(Point3::new(0.0, 0.0, 0.0), 6.0).unwrap();
2847        let cyl =
2848            CylindricalSurface::new(Point3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 3.0)
2849                .unwrap();
2850        let circles = exact_sphere_cylinder(&sphere, &cyl)
2851            .unwrap()
2852            .expect("coaxial case returns Some");
2853        assert_eq!(circles.len(), 2, "through-bore meets the sphere twice");
2854        let mut zs: Vec<f64> = circles
2855            .iter()
2856            .filter_map(|c| match c {
2857                ExactIntersectionCurve::Circle(circle) => {
2858                    assert!(
2859                        (circle.radius() - 3.0).abs() < 1e-9,
2860                        "rim radius == cyl radius"
2861                    );
2862                    Some(circle.center().z())
2863                }
2864                _ => None,
2865            })
2866            .collect();
2867        assert_eq!(zs.len(), 2, "both sections must be exact circles");
2868        zs.sort_by(f64::total_cmp);
2869        let z = 27.0_f64.sqrt();
2870        assert!((zs[0] + z).abs() < 1e-9 && (zs[1] - z).abs() < 1e-9);
2871    }
2872
2873    #[test]
2874    fn exact_sphere_cylinder_non_coaxial_defers() {
2875        // Cylinder axis offset from the sphere center → quartic curve, deferred.
2876        let sphere = SphericalSurface::new(Point3::new(0.0, 0.0, 0.0), 6.0).unwrap();
2877        let cyl =
2878            CylindricalSurface::new(Point3::new(2.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 3.0)
2879                .unwrap();
2880        assert!(
2881            exact_sphere_cylinder(&sphere, &cyl).unwrap().is_none(),
2882            "non-coaxial sphere/cylinder defers to the marcher"
2883        );
2884    }
2885
2886    #[test]
2887    fn disjoint_cylinders_no_intersection() {
2888        let cyl_a =
2889            CylindricalSurface::new(Point3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 0.5)
2890                .unwrap();
2891        let cyl_b =
2892            CylindricalSurface::new(Point3::new(5.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 0.5)
2893                .unwrap();
2894
2895        let curves = intersect_analytic_analytic(
2896            AnalyticSurface::Cylinder(&cyl_a),
2897            AnalyticSurface::Cylinder(&cyl_b),
2898            16,
2899        )
2900        .unwrap();
2901
2902        assert!(curves.is_empty(), "disjoint cylinders should not intersect");
2903    }
2904
2905    // ── Oblique plane × cone conic (ellipse / parabola / hyperbola) ──────
2906
2907    /// Collect 3D points from a returned exact curve, sampling analytic forms.
2908    fn collect_points(curve: &ExactIntersectionCurve) -> Vec<Point3> {
2909        use crate::traits::ParametricCurve;
2910        match curve {
2911            ExactIntersectionCurve::Circle(c) => (0..=64)
2912                .map(|i| ParametricCurve::evaluate(c, TAU * f64::from(i) / 64.0))
2913                .collect(),
2914            ExactIntersectionCurve::Ellipse(e) => (0..=64)
2915                .map(|i| ParametricCurve::evaluate(e, TAU * f64::from(i) / 64.0))
2916                .collect(),
2917            ExactIntersectionCurve::Points(pts) => pts.clone(),
2918        }
2919    }
2920
2921    /// Assert every returned point lies on the plane and the cone surface, on
2922    /// the real (`v >= 0`) nappe, and within a sane axial bound.
2923    fn assert_on_plane_and_cone(
2924        curves: &[ExactIntersectionCurve],
2925        cone: &ConicalSurface,
2926        n: Vec3,
2927        d: f64,
2928        z_bound: (f64, f64),
2929    ) {
2930        assert!(!curves.is_empty(), "expected at least one section curve");
2931        let mut total = 0;
2932        for curve in curves {
2933            for p in collect_points(curve) {
2934                total += 1;
2935                let plane_err = (n.x() * p.x() + n.y() * p.y() + n.z() * p.z() - d).abs();
2936                assert!(
2937                    plane_err < 1e-9,
2938                    "point off plane by {plane_err:.2e}: {p:?}"
2939                );
2940                let (u, v) = cone.project_point(p);
2941                let q = cone.evaluate(u, v);
2942                let cone_err =
2943                    ((p.x() - q.x()).powi(2) + (p.y() - q.y()).powi(2) + (p.z() - q.z()).powi(2))
2944                        .sqrt();
2945                assert!(cone_err < 1e-7, "point off cone by {cone_err:.2e}: {p:?}");
2946                assert!(v >= -1e-9, "point on phantom nappe (v={v:.4}): {p:?}");
2947                assert!(
2948                    p.z() >= z_bound.0 - 1e-6 && p.z() <= z_bound.1 + 1e-6,
2949                    "point z={:.4} outside sane bound {z_bound:?}: {p:?}",
2950                    p.z()
2951                );
2952            }
2953        }
2954        assert!(total >= 8, "too few section points ({total})");
2955    }
2956
2957    #[test]
2958    fn oblique_plane_cone_ellipse_is_exact_and_on_both() {
2959        // 45°-half-angle cone (axis +z). A plane tilted only ~16.7° off horizontal
2960        // has plane-axis angle ≈ 73° > 45° (the cone's half-opening from axis) →
2961        // ellipse. Must come back as an exact Ellipse, fully on both surfaces.
2962        let cone = ConicalSurface::new(
2963            Point3::new(0.0, 0.0, 0.0),
2964            Vec3::new(0.0, 0.0, 1.0),
2965            std::f64::consts::FRAC_PI_4,
2966        )
2967        .unwrap();
2968        let n = Vec3::new(0.3, 0.0, 1.0).normalize().unwrap();
2969        // Plane through (0,0,5): d = n·(0,0,5).
2970        let d = n.z() * 5.0;
2971        let curves = exact_plane_cone(&cone, n, d).unwrap();
2972        assert!(
2973            curves
2974                .iter()
2975                .any(|c| matches!(c, ExactIntersectionCurve::Ellipse(_))),
2976            "oblique steep plane × cone must yield an exact Ellipse"
2977        );
2978        // The ellipse straddles z=5; with the 0.3 tilt the z-extent stays modest.
2979        assert_on_plane_and_cone(&curves, &cone, n, d, (0.0, 12.0));
2980    }
2981
2982    #[test]
2983    fn oblique_plane_cone_wrong_nappe_is_empty() {
2984        // Same ellipse-regime plane as above, but offset to the FAR side of the
2985        // apex (z=-5). The +z cone's real (v≥0) nappe is not met — only the
2986        // phantom v<0 nappe — so the result must be EMPTY, not a phantom ellipse.
2987        let cone = ConicalSurface::new(
2988            Point3::new(0.0, 0.0, 0.0),
2989            Vec3::new(0.0, 0.0, 1.0),
2990            std::f64::consts::FRAC_PI_4,
2991        )
2992        .unwrap();
2993        let n = Vec3::new(0.3, 0.0, 1.0).normalize().unwrap();
2994        let d = n.z() * -5.0;
2995        let curves = exact_plane_cone(&cone, n, d).unwrap();
2996        assert!(
2997            curves.is_empty(),
2998            "plane on the phantom-nappe side must yield no real curve, got {}",
2999            curves.len()
3000        );
3001    }
3002
3003    #[test]
3004    fn oblique_plane_cone_parabola_on_both_single_branch() {
3005        // Plane normal at exactly 45° to the axis (= the cone half-opening) → the
3006        // plane is parallel to a generator → parabola. One unbounded branch.
3007        let cone = ConicalSurface::new(
3008            Point3::new(0.0, 0.0, 0.0),
3009            Vec3::new(0.0, 0.0, 1.0),
3010            std::f64::consts::FRAC_PI_4,
3011        )
3012        .unwrap();
3013        let n = Vec3::new(1.0, 0.0, 1.0).normalize().unwrap();
3014        let d = n.x() * 3.0 + n.z() * 3.0; // through (3,0,3)
3015        let curves = exact_plane_cone(&cone, n, d).unwrap();
3016        assert_eq!(
3017            curves.len(),
3018            1,
3019            "a parabola is a single branch, got {}",
3020            curves.len()
3021        );
3022        // Bounded by r_max = 32·|e|; |e| here is O(few), so allow a wide z window.
3023        assert_on_plane_and_cone(&curves, &cone, n, d, (0.0, 400.0));
3024    }
3025
3026    #[test]
3027    fn oblique_plane_cone_hyperbola_real_nappe_only() {
3028        // Faithful scooplabel lip-foot geometry: a 45° cone with axis −z and
3029        // apex at (−59,−59,15.85) (a bin corner), cut by the upper ramp tread
3030        // plane n=(0,0.99518,0.09802), d=−58.36056. The plane is nearly parallel
3031        // to the axis (cos≈0.098) → plane-axis angle ≈ 5.6° < 45° → hyperbola.
3032        // The downward real nappe is hit by exactly one branch; the phantom
3033        // upward nappe (and the asymptote runaway) must NOT appear, and the arc
3034        // must stay near the apex (the plane is ~1.2 mm from it).
3035        let cone = ConicalSurface::new(
3036            Point3::new(-59.0, -59.0, 15.85),
3037            Vec3::new(0.0, 0.0, -1.0),
3038            std::f64::consts::FRAC_PI_4,
3039        )
3040        .unwrap();
3041        let n = Vec3::new(0.0, 0.995_18, 0.098_02).normalize().unwrap();
3042        let d = -58.360_56;
3043        let cos_theta = n.dot(cone.axis()).abs();
3044        assert!(cos_theta < 0.2, "expected a shallow (hyperbola) plane");
3045        let curves = exact_plane_cone(&cone, n, d).unwrap();
3046        // Real downward nappe only: never above the apex (z=15.85). The vertex is
3047        // ~1.2 mm from the apex, so the bounded arc stays within a few mm of it.
3048        assert_on_plane_and_cone(&curves, &cone, n, d, (5.0, 15.85));
3049        // Every returned curve is sampled Points (no false Circle/Ellipse).
3050        for c in &curves {
3051            assert!(
3052                matches!(c, ExactIntersectionCurve::Points(_)),
3053                "hyperbola must be sampled Points, not a closed conic"
3054            );
3055        }
3056    }
3057}