text
stringlengths
0
1.47k
# Source -> target: two nearest for the ratio test.
k2 = min(2, Nt)
d_sorted, i_sorted = torch.topk(dist, k2, dim=1, largest=False) # (Ns, k2)
s2t = i_sorted[:, 0] # (Ns,) best target for each source
if k2 == 2:
d1 = d_sorted[:, 0].clamp_min(1e-12)
d2 = d_sorted[:, 1].clamp_min(1e-12)
ratio_ok = d1 < ratio * d2 # (Ns,)
else:
ratio_ok = torch.ones(Ns, dtype=torch.bool, device=dist.device)
# Target -> source nearest (for mutual check).
t2s = dist.argmin(dim=0) # (Nt,)
src_range = torch.arange(Ns, device=dist.device)
mutual = t2s[s2t] == src_range # (Ns,)
keep = ratio_ok & mutual
src_idx = src_range[keep]
tgt_idx = s2t[keep]
return src_idx, tgt_idx
# ── robust pose estimation: maximal-clique pre-filter + RANSAC ──────────────────
def _clique_prefilter(
cs: torch.Tensor,
ct: torch.Tensor,
tol: float,
max_keep: int = 200,
) -> torch.Tensor:
"""Keep the largest mutually distance-consistent subset of correspondences (TEASER-lite).
A rigid/similarity transform preserves *pairwise distances up to a global scale*. Two
correspondences (i, j) are consistent if ``| ||cs_i - cs_j|| - ||ct_i - ct_j|| || <= tol``
(rigid), gross mismatches violate this with most other correspondences. We build the
consistency graph and greedily grow a clique from the highest-degree vertex: that vertex's
consistent neighbourhood is a near-clique of inliers, which we return as the surviving indices.
This is a cheap O(M^2) inlier pre-selection (M is small after ratio/MNN), enough to let a
fixed RANSAC threshold succeed.
Args:
cs: ``(M, 3)`` source correspondence points.
ct: ``(M, 3)`` target correspondence points.
tol: pairwise-distance consistency tolerance (metres).
max_keep: cap on the consistency-matrix size (subsamples if M is large).
Returns:
``(K,)`` int64 indices (into the M correspondences) of the kept consistent subset.
"""
M = cs.shape[0]
if M <= 4:
return torch.arange(M, device=cs.device)
if M > max_keep:
sel = torch.linspace(0, M - 1, max_keep, device=cs.device).round().to(torch.int64)
else:
sel = torch.arange(M, device=cs.device)
cs_s = cs[sel]
ct_s = ct[sel]
ds = torch.cdist(cs_s, cs_s) # (m, m) source pairwise distances
dt = torch.cdist(ct_s, ct_s) # (m, m) target pairwise distances
consistent = (ds - dt).abs() <= tol # (m, m) bool consistency graph (rigid scale=1)
deg = consistent.sum(dim=1) # (m,) vertex degree
seed = int(deg.argmax().item())
# The clique candidate = the seed's consistent neighbourhood.
member = consistent[seed] # (m,)
kept = sel[member]
if kept.numel() < 4:
return torch.arange(M, device=cs.device)
return kept
def _robust_register(
src_pts: torch.Tensor,
tgt_pts: torch.Tensor,
src_idx: torch.Tensor,
tgt_idx: torch.Tensor,
*,
n_iters: int,
inlier_tol: float,
with_scale: bool,
rng_seed: int = 42,
) -> dict:
"""RANSAC (3-point Umeyama) over a clique-prefiltered correspondence set + ambiguity probe.
Returns a dict with:
``T`` best 4×4 transform,
``n_inliers`` best inlier count,
``n_matches`` number of input correspondences,
``ambiguity`` rotation spread (deg) among near-best hypotheses (0 if a single basin),
``hypotheses`` number of distinct near-best rotation clusters.
The ambiguity probe keeps the top hypotheses (by inlier count), takes those within
``_FS_HYP_SCORE_SLACK`` of the best, and measures the max pairwise geodesic rotation distance
among them: a large spread means the inliers do not constrain rotation (the disambiguating
feature is gone) → the pose is inherently ambiguous.
"""
dev = src_pts.device
dtype = src_pts.dtype