text stringlengths 0 1.47k |
|---|
eye4 = torch.eye(4, device=dev, dtype=dtype) |
M0 = int(src_idx.shape[0]) |
out = { |
"T": eye4.clone(), |
"n_inliers": 0, |
"n_matches": M0, |
"ambiguity": 180.0, |
"hypotheses": 0, |
} |
if M0 < 3: |
return out |
cs_all = src_pts[src_idx].double() # (M0, 3) |
ct_all = tgt_pts[tgt_idx].double() # (M0, 3) |
# Clique pre-filter (rigid pairwise-distance consistency) — drops gross outliers. |
keep = _clique_prefilter(cs_all, ct_all, tol=inlier_tol) |
cs = cs_all[keep] |
ct = ct_all[keep] |
M = cs.shape[0] |
if M < 3: |
cs, ct, M = cs_all, ct_all, M0 # fall back to all matches if clique collapsed |
rng = torch.Generator(device="cpu") |
rng.manual_seed(rng_seed) |
best_T = eye4.clone() |
best_n = 0 |
best_res = float("inf") |
# Track the top hypotheses' rotations + inlier counts for the ambiguity probe. |
hyp_R: list[torch.Tensor] = [] |
hyp_n: list[int] = [] |
for _ in range(n_iters): |
perm = torch.randperm(M, generator=rng)[:3] |
s_s = cs[perm] |
s_t = ct[perm] |
try: |
s_est, R_est, t_est = _umeyama(s_s, s_t, with_scale) |
except Exception: |
continue |
# Degenerate sample (collinear) → skip. |
if not torch.isfinite(R_est).all() or not torch.isfinite(t_est).all(): |
continue |
cs_t = cs @ R_est.transpose(-1, -2) * s_est + t_est # (M, 3) |
res = (cs_t - ct).norm(dim=1) |
inl = res < inlier_tol |
n_in = int(inl.sum().item()) |
mean_res = float(res[inl].mean().item()) if n_in > 0 else float("inf") |
if n_in >= 3: |
hyp_R.append(R_est.detach()) |
hyp_n.append(n_in) |
if n_in > best_n or (n_in == best_n and mean_res < best_res): |
best_n = n_in |
best_res = mean_res |
T_cand = torch.eye(4, device=dev, dtype=torch.float64) |
T_cand[:3, :3] = s_est * R_est |
T_cand[:3, 3] = t_est |
best_T = T_cand.to(dtype=dtype, device=dev) |
# Refine the winner on ALL its inliers (Umeyama on the consensus set). |
if best_n >= 3: |
R_b = best_T[:3, :3].double() |
s_b = float(torch.linalg.det(R_b).abs().clamp_min(1e-12) ** (1.0 / 3.0)) if with_scale else 1.0 |
Rb_pure = R_b / s_b if with_scale else R_b |
cs_t = cs @ Rb_pure.transpose(-1, -2) * s_b + best_T[:3, 3].double() |
inl = (cs_t - ct).norm(dim=1) < inlier_tol |
if int(inl.sum().item()) >= 3: |
try: |
s_r, R_r, t_r = _umeyama(cs[inl], ct[inl], with_scale) |
T_ref = torch.eye(4, device=dev, dtype=torch.float64) |
T_ref[:3, :3] = s_r * R_r |
T_ref[:3, 3] = t_r |
best_T = T_ref.to(dtype=dtype, device=dev) |
except Exception: |
pass |
out["T"] = best_T |
out["n_inliers"] = best_n |
# ── ambiguity probe ────────────────────────────────────────────────────── |
if best_n >= 3 and len(hyp_R) >= 1: |
thresh = _FS_HYP_SCORE_SLACK * best_n |
near = [R for R, n in zip(hyp_R, hyp_n) if n >= thresh] |
if len(near) > _FS_TOPK_HYP: |
# keep the top-k by inlier count |
order = sorted(range(len(hyp_n)), key=lambda i: -hyp_n[i]) |
near = [hyp_R[i] for i in order[:_FS_TOPK_HYP] if hyp_n[i] >= thresh] |
if len(near) >= 2: |
Rs = torch.stack([R.to(torch.float64) for R in near], dim=0) # (H, 3, 3) |
# pairwise geodesic rotation distance (deg); max spread among near-best hypotheses |
H = Rs.shape[0] |
rel = torch.einsum("aij,bkj->abik", Rs, Rs) # (H, H, 3, 3) = Ra @ Rb^T |
tr = rel[..., 0, 0] + rel[..., 1, 1] + rel[..., 2, 2] |
cosang = ((tr - 1.0) * 0.5).clamp(-1.0, 1.0) |
ang = torch.rad2deg(torch.arccos(cosang)) # (H, H) |
spread = float(ang.max().item()) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.