Skip to main content

brepkit_math/nurbs/intersection/
mod.rs

1//! Surface-surface and curve-surface intersection routines.
2//!
3//! These are the geometric foundations for boolean operations on NURBS solids.
4//!
5//! ## Algorithms
6//!
7//! - **Plane-NURBS**: Sample the NURBS surface on a grid, find sign changes of the
8//!   signed distance to the plane, trace zero-crossings via linear interpolation,
9//!   then refine with Newton iteration.
10//! - **NURBS-NURBS**: Subdivision + marching method in (u1,v1,u2,v2) parameter space.
11//! - **Line-surface**: Newton iteration from grid-based seed points.
12
13#![allow(
14    clippy::many_single_char_names,
15    clippy::similar_names,
16    clippy::suboptimal_flops,
17    clippy::needless_range_loop,
18    clippy::cast_precision_loss,
19    clippy::doc_markdown,
20    clippy::missing_const_for_fn,
21    clippy::manual_let_else
22)]
23
24mod chaining;
25mod curve_surface;
26mod line;
27mod plane;
28mod ray;
29mod surface_marching;
30mod surface_seeding;
31
32use crate::nurbs::curve::NurbsCurve;
33use crate::vec::Point3;
34
35pub use chaining::chain_intersection_points;
36pub use curve_surface::{CurveSurfaceHit, intersect_curve_surface};
37pub use line::intersect_line_nurbs;
38pub use plane::intersect_plane_nurbs;
39pub use ray::{RayHit, RayHits, RaySurface};
40pub use surface_seeding::intersect_nurbs_nurbs;
41
42/// Maximum work-queue entries for the branch-aware SSI marcher.
43const MAX_QUEUE_SIZE: usize = 100;
44
45/// Maximum traced curve segments before stopping branch exploration.
46const MAX_SEGMENTS: usize = 50;
47
48/// Maximum branch points detected per march direction.
49const MAX_BRANCHES_PER_DIRECTION: usize = 10;
50
51/// Maximum iterations for Newton-type solvers.
52///
53/// 20 iterations is sufficient for quadratic convergence from reasonable seeds
54/// (quadratic convergence achieves ~1e-12 in ~6 iterations from a 1e-1 seed).
55/// The limit is generous to handle near-singular cases where convergence slows.
56const MAX_NEWTON_ITER: usize = 20;
57
58/// A point on an intersection curve, with parameter values on both surfaces.
59#[derive(Debug, Clone, Copy)]
60pub struct IntersectionPoint {
61    /// 3D position of the intersection.
62    pub point: Point3,
63    /// Parameter on the first surface (u1, v1) or the curve parameter.
64    pub param1: (f64, f64),
65    /// Parameter on the second surface (u2, v2).
66    pub param2: (f64, f64),
67}
68
69/// Result of a surface-surface intersection: a list of intersection curves.
70#[derive(Debug, Clone)]
71pub struct IntersectionCurve {
72    /// The 3D intersection curve as a NURBS.
73    pub curve: NurbsCurve,
74    /// Sampled points along the curve with parameter values.
75    pub points: Vec<IntersectionPoint>,
76}
77
78#[cfg(test)]
79mod tests;