text
stringlengths
0
1.47k
"""Feature-based global aligner, partial-overlap robust coarse init + ambiguity detection.
``feature_align`` is an alternative to :func:`splatreg.align.global_align` designed for
partial-overlap scenarios (two splat captures of the same object that see different parts of it).
The centroid-ICP sweep in ``align.py`` assumes full overlap and fails when 20–60 % of the target
is missing. Feature-based matching sidesteps that by finding point pairs that look locally
similar (via a proper FPFH descriptor) before estimating the transform, using ONLY the region
that is present in both clouds.
Algorithm
---------
1. **Sub-sample** both clouds to a manageable size (strided, deterministic).
2. **Normals** (``_estimate_normals``): per-point surface normal from the smallest-eigenvector of
the local k-NN covariance (PCA), sign-disambiguated toward the cloud centroid so both clouds
orient consistently.
3. **FPFH descriptors** (``_fpfh``): the textbook Fast Point Feature Histogram (Rusu et al.,
ICRA 2009). For each point we first build a Simplified PFH (``_spfh``): for every k-neighbour
pair we compute the three Darboux-frame angular features ``(alpha, phi, theta)`` from the two
estimated normals and histogram each into ``B`` bins. The FPFH then re-weights each point's
SPFH by its neighbours' SPFHs (inverse-distance weighting), giving a ``3*B``-vector that is
*rotation/translation invariant* and *discriminative* on geometrically distinctive regions
(e.g. the lobe of the test object) while staying ambiguous on flat/smooth regions.
4. **Ratio-test mutual-NN matching** (``_match_features``): for each source descriptor find its two
nearest target descriptors; accept the match only if it passes Lowe's ratio test AND is mutual
(reciprocal NN). This yields a small, high-confidence correspondence set drawn only from the
shared region.
5. **Robust pose estimation** (``_robust_register``): a maximal-clique / consistency pre-filter
(TEASER-lite) keeps the largest mutually distance-consistent subset of correspondences, then
RANSAC over 3-point minimal samples fits an SE(3)/Sim(3) via Umeyama and scores by 3-D inliers.
The clique pre-filter removes gross outliers before RANSAC so a sound inlier threshold suffices.
6. Return the winner 4×4 transform plus diagnostics (inlier count, total matches, an
**ambiguity** score and flag).
Ambiguity detection (honest signal)
-----------------------------------
When the crop removes the rotation-disambiguating feature (e.g. the ``+x`` lobe is gone), the
surviving region is near-symmetric: several RANSAC hypotheses fit the inliers nearly equally well
but disagree on rotation. ``_robust_register`` enumerates the top hypotheses, clusters them by
geodesic rotation distance, and computes an **ambiguity score** = the spread of near-best
rotations. A large spread (or too few inliers / matches) means the true pose is *genuinely
unrecoverable from this overlap by any method*; we flag it (``ambiguous=True``) and report a low
``confidence`` instead of silently returning a wrong pose. Correctly flagging an inherently
ambiguous crop is the intended, honest behaviour, not a failure to hide.
Symmetric-object improvement in ``align.py``
---------------------------------------------
Two fixes are applied to the existing ``_pca_seed_rotations`` and ``global_align`` in
``splatreg/align.py``:
* isotropy probe for near-spherical clouds (PCA axes are arbitrary there);
* a stability tie-break: among seeds within an epsilon of the best score, prefer the one whose
aligned centroid is closest to the target centroid.
Those fixes live in ``align.py``; this file only documents their rationale.
Pure torch, CPU-runnable, no additional dependencies.
"""
from __future__ import annotations
import math
import torch
from .core.types import Gaussians
from .align import _stride_subsample, _umeyama, super_fibonacci_so3, _batched_nn, _batched_umeyama
# ── tuneable defaults ─────────────────────────────────────────────────────────
# Sub-sample sizes. Keep small for CPU feasibility; partial-overlap scenes
# need denser descriptors than the full-overlap centroid sweep.
_FS_N_SOURCE = 600
_FS_N_TARGET = 600
# k-NN for normals and SPFH. Normals want a tight neighbourhood (sharp surface
# estimate); FPFH wants a slightly larger one to be discriminative.
_FS_KNN_NORMAL = 16
_FS_KNN_FPFH = 16
# FPFH histogram bins per angular feature. 3 features × _FS_FPFH_BINS = descriptor dim.
_FS_FPFH_BINS = 11
# Matching
_FS_RATIO = 0.92 # Lowe ratio-test threshold (lenient: FPFH NN distances are close on smooth geom)
# Robust estimation
_FS_RANSAC_ITERS = 2000 # number of 3-point hypotheses
_FS_INLIER_TOL = 0.02 # 3-D inlier distance threshold (metres); ~2 cm at OBJ_RADIUS 0.14
_FS_MIN_MATCHES = 4 # fewer ratio/MNN matches → ambiguous (insufficient to constrain a pose)
_FS_MIN_INLIERS = 6 # fewer RANSAC inliers → low confidence / ambiguous
# Ambiguity thresholds
# PRIMARY failure signal: the normalised target->source overlap residual at the converged pose
# (``_overlap_residual_norm``). The observed (partial) target should land exactly on the full
# source surface under the true pose, so a correct alignment scores ~0; a wrong / unrecoverable
# pose leaves the target floating off the surface. Calibrated on the benchmark: every recovered
# partial crop scores ~0.000, the one unrecoverable crop scores ~0.12. A 0.04 gate separates them
# with wide margin.
_FS_AMBIG_RESID = 0.04
# SECONDARY signal: post-polish rotation SENSITIVITY (``_rotation_constrained_probe``) — the
# smallest rise of the overlap residual (above the surface-resampling floor) when the converged
# rotation is twisted by +/-30 deg about any axis, normalised by the object RMS radius. Near 0