Skip to content

Commit 9f01f3a

Browse files
committed
Refactor and enhance curve-surface intersection module:
- Remove unused functions and imports to streamline `_ncsx2.py`. - Introduce modular functions for overlap merging, isolated point handling, and improved global parameter mapping. - Optimize NURBS curve and surface routines for better precision and rational handling. - Replace redundant logic with reusable structured functions to improve readability and maintainability.
1 parent 0563bc9 commit 9f01f3a

3 files changed

Lines changed: 367 additions & 629 deletions

File tree

mmcore/numeric/intersection/ccx/_utils.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -233,25 +233,7 @@ def _pairwise_overlap_nd_dense(boxes, *, closed=True):
233233
np.fill_diagonal(ov, False)
234234
return ov
235235

236-
def _connected_components_from_adj(adj):
237-
"""Connected components on a dense boolean adjacency; returns labels (N,)."""
238-
n = adj.shape[0]
239-
labels = -np.ones(n, dtype=int)
240-
label = 0
241-
for i in range(n):
242-
if labels[i] != -1:
243-
continue
244-
stack = [i]
245-
labels[i] = label
246-
while stack:
247-
v = stack.pop()
248-
neigh = np.flatnonzero(adj[v])
249-
unvisited = neigh[labels[neigh] == -1]
250-
labels[unvisited] = label
251-
# extend with Python list to avoid repeated allocations
252-
stack.extend(unvisited.tolist())
253-
label += 1
254-
return labels
236+
255237

256238
def _merge_by_labels_nd(boxes, labels):
257239
"""Merge each component to its envelope. boxes: (N,2,D) -> (K,2,D)."""

mmcore/numeric/intersection/csx/_bez_csx3.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,10 +547,12 @@ def project_G0(
547547
raise TypeError("fixed must be a tuple[bool, bool, bool] of length 3: (fix_t, fix_u, fix_v).")
548548

549549
fix_t, fix_u, fix_v = (bool(fixed[0]), bool(fixed[1]), bool(fixed[2]))
550-
550+
def snap(v):
551+
return 0. if v < (1-v) else 1.
551552
# Must fix at least one and free at least one
552553
if (fix_t and fix_u and fix_v) or ((not fix_t) and (not fix_u) and (not fix_v)):
553-
raise ValueError("fixed must contain at least one True and at least one False (fix 1 or 2 variables).")
554+
return snap(t0),snap(u0),snap(v0),np.array([0.,0.,0]),0.
555+
#raise ValueError(f"fixed must contain at least one True and at least one False (fix 1 or 2 variables). ({t0}, {u0}, {v0}),({fix_t}, {fix_u}, {fix_v}).")
554556

555557
def _curve_and_d1(t):
556558
"""Return (c, ct) at t; ct via analytic helper if available, else finite-difference."""
@@ -768,6 +770,7 @@ def trace_curve_surface_overlap(
768770
):
769771
"""Trace a curve-on-surface overlap by stepping along the curve parameter t."""
770772
# scale & tolerances
773+
771774
def bbox_diag_len(Cc, Ss):
772775
ec = dehomogenize_ctrl(Cc, rational=rational)
773776
es = dehomogenize_ctrl(Ss, rational=rational).reshape(-1, ec.shape[-1])
@@ -1192,7 +1195,7 @@ def bezier_curve_surface_intersect_certified(
11921195
sv_thresh: float = 1e-8,
11931196
atol: float = 1e-3,
11941197
rational: bool | None = None,
1195-
angle_tol: float = 1e-3,
1198+
angle_tol: float = 0.01,
11961199
max_depth: int = 60,
11971200
) -> IntersectionResult:
11981201
"""Certified intersection for (possibly rational) Bézier curve & surface.
@@ -1224,7 +1227,7 @@ def bezier_curve_surface_intersect_certified(
12241227

12251228
atol_sq = atol * atol
12261229

1227-
dn0 = distance_squared_net_curve_surface(C, S, rational=rational)[..., np.newaxis] - atol_sq
1230+
dn0 = distance_squared_net_curve_surface(C, S, rational=rational)[..., np.newaxis]
12281231
dt0 = bernstein_partial_derivative_coeffs(dn0, 0)
12291232
du0 = bernstein_partial_derivative_coeffs(dn0, 1)
12301233
dv0 = bernstein_partial_derivative_coeffs(dn0, 2)
@@ -1429,6 +1432,7 @@ def near_existing_isolated(point: NDArray):
14291432
isolated.append({"t": t_hit, "u": u_hit, "v": v_hit, "point": x})
14301433
stats["pruned"] += 1
14311434
stats["pruned_by"].append("small_cell")
1435+
14321436
continue
14331437

14341438
# -------------------------------------------------------------------

0 commit comments

Comments
 (0)