text
stringlengths
0
1.47k
TA = src @ R.transpose(-1, -2) * s + t # source -> target frame
d_ts = torch.cdist(tgt, TA).min(dim=1).values # each target -> nearest source
k_t = max(3, int(trim * d_ts.shape[0]))
r_ts = d_ts.topk(k_t, largest=False).values.mean()
# Source->target, but only the source points actually inside the overlap band (the kept target
# matches define which source points are in-overlap); use the trimmed nearest src→tgt.
d_st = torch.cdist(TA, tgt).min(dim=1).values # each source -> nearest target
k_s = max(3, int(0.5 * d_st.shape[0])) # keep the closest half (the in-overlap source)
r_st = d_st.topk(k_s, largest=False).values.mean()
return float((r_ts + r_st).item())
def _scale_line_search(
src_full: torch.Tensor,
tgt_full: torch.Tensor,
T: torch.Tensor,
*,
n_src: int = 3000,
n_tgt: int = 1500,
span: float = 0.4,
iters: int = 28,
) -> torch.Tensor:
"""Golden-section line-search on the Sim(3) SCALE that minimises the SYMMETRIC overlap residual.
Under partial overlap the joint Umeyama scale is loosely pinned (the surviving matches span a
small one-sided extent), so the recovered ``s`` can drift ~25 %. Holding the (well-recovered)
rotation fixed, we search ``s`` over ``[s0/(1+span), s0*(1+span)]`` against
:func:`_symmetric_overlap_residual`, whose source→target term gives scale a real gradient, and
recompute ``t`` for each candidate so the fit stays consistent. Returns the corrected 4×4 (or the
input unchanged if the search does not improve on it).
"""
dev, dtype = T.device, T.dtype
src = _stride_subsample(src_full, n_src).double()
tgt = _stride_subsample(tgt_full, n_tgt).double()
if src.shape[0] < 8 or tgt.shape[0] < 8:
return T
block = T[:3, :3].double()
s0 = float(torch.linalg.det(block).abs().clamp_min(1e-18) ** (1.0 / 3.0))
R = block / s0
src_c = src.mean(0)
tgt_c = tgt.mean(0)
def resid_at(s: float) -> float:
t = tgt_c - s * (R @ src_c)
return _symmetric_overlap_residual(src, tgt, R, s, t)
lo, hi = s0 / (1.0 + span), s0 * (1.0 + span)
r0 = resid_at(s0)
invphi = (math.sqrt(5.0) - 1.0) / 2.0
c = hi - (hi - lo) * invphi
d = lo + (hi - lo) * invphi
fc, fd = resid_at(c), resid_at(d)
for _ in range(iters):
if fc < fd:
hi, d, fd = d, c, fc
c = hi - (hi - lo) * invphi
fc = resid_at(c)
else:
lo, c, fc = c, d, fd
d = lo + (hi - lo) * invphi
fd = resid_at(d)
s_best = 0.5 * (lo + hi)
if resid_at(s_best) > r0: # no improvement over the joint-solve scale → keep it
return T
t_best = tgt_c - s_best * (R @ src_c)
out = torch.eye(4, device=dev, dtype=torch.float64)
out[:3, :3] = s_best * R
out[:3, 3] = t_best
return out.to(device=dev, dtype=dtype)
def _overlap_inlier_count(
src_full: torch.Tensor,
tgt_full: torch.Tensor,
T: torch.Tensor,
tol: float,
*,
n_src: int = 2048,
n_tgt: int = 1024,
) -> int:
"""Number of observed target points landing within ``tol`` of the transformed source surface.
The overlap-aware inlier measure for a pose recovered by the basin sweep (which has no RANSAC
correspondence set): a target→source nearest-neighbour count under the same 3-D tolerance used
by RANSAC, so the inlier-count gate is consistent across the FPFH and sweep paths.
"""
src = _stride_subsample(src_full, n_src).double()
tgt = _stride_subsample(tgt_full, n_tgt).double()
if src.shape[0] < 3 or tgt.shape[0] < 3:
return 0
TA = src @ T[:3, :3].double().transpose(-1, -2) + T[:3, 3].double()
dmin = torch.cdist(tgt, TA).min(dim=1).values
return int((dmin < tol).sum().item())
def _axis_angle_R(axis: torch.Tensor, rad: float) -> torch.Tensor:
"""Rodrigues rotation (3×3, float64) of ``rad`` radians about a unit ``axis`` tensor."""
a = axis / axis.norm().clamp_min(1e-12)
K = torch.tensor(
[[0.0, -a[2], a[1]], [a[2], 0.0, -a[0]], [-a[1], a[0], 0.0]],