From 509179e63cf51919091829dbf14db0cd74b98858 Mon Sep 17 00:00:00 2001 From: aidanpcquinn Date: Tue, 29 Sep 2026 14:31:56 +1000 Subject: [PATCH] Clamp PCA-fallback centreline nodes to mask bounds Masks that Lee thinning removes entirely (e.g. small masks touching the volume edge) were represented by their extreme projections on the principal axis, which could fall outside the volume and fail project validation. Co-Authored-By: Claude Sonnet 5.5 --- src/chronopose_viewer/converter.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/chronopose_viewer/converter.py b/src/chronopose_viewer/converter.py index 79b8603..72bf1eb 100644 --- a/src/chronopose_viewer/converter.py +++ b/src/chronopose_viewer/converter.py @@ -628,8 +628,11 @@ def _populate_pca_fallback(graph: InstanceGraph, coordinates: np.ndarray) -> Non eigenvalues, eigenvectors = np.linalg.eigh(covariance) axis = eigenvectors[:, int(np.argmax(eigenvalues))] projections = centred @ axis - first_position = centre + float(projections.min()) * axis - last_position = centre + float(projections.max()) * axis + # Extreme projections can fall outside the mask (and the volume) for masks at an edge. + lower = coordinates.min(axis=0).astype(np.float64) + upper = coordinates.max(axis=0).astype(np.float64) + first_position = np.clip(centre + float(projections.min()) * axis, lower, upper) + last_position = np.clip(centre + float(projections.max()) * axis, lower, upper) first = graph.add_node(first_position, node_id=_next_node_id(graph)) if np.linalg.norm(last_position - first_position) < 1e-8: return