text
stringlengths
0
1.47k
src_c = src.mean(0)
tgt_c = tgt.mean(0)
seeds = super_fibonacci_so3(n_rot, device=dev, dtype=torch.float64)
# Stage 0 (fully batched, fast): rank ALL seeds by a cheap point-to-point target→source ICP so the
# expensive pivoted point-to-plane refine only touches the top few candidates. This keeps a DENSE
# seed covering (so the small basins of moderate crops are hit) affordable.
R_ranked = _batched_t2s_prefilter(src, tgt, seeds, iters=int(icp_iters), trim=trim)
topn = max(1, int(topk))
R_ranked = R_ranked[:topn]
# Stage 1: precise pivoted, LM-damped point-to-plane refine on the top seeds (the recoverer). The
# overlap-pivot + damping makes the trimmed objective a sharp basin AT the true subset pose, so a
# ranked seed inside the basin descends straight to residual ~0; wrong basins stay high. Pick the
# lowest-residual refine. On heavy crops no seed reaches a low residual → the caller's ambiguity
# gate honestly flags it.
# Collect ALL refined candidates, then rank by a SYMMETRIC residual. The point-to-plane ICP
# residual is one-directional (target→source): a flipped pose on a self-similar surface can slide
# the partial target onto the source and so score a low one-directional residual even though a big
# chunk of the in-overlap SOURCE then floats off the target. Adding the source→target term
# (:func:`_symmetric_overlap_residual`) penalises exactly that flip, which is what lets the
# MODERATE keep≈60 % crops resolve to the true basin instead of a 170-ish mirror.
cands: list[tuple[float, torch.Tensor, torch.Tensor]] = []
for R0 in R_ranked:
t0 = tgt_c - (R0 @ src_c)
R2, t2, res2 = _point_to_plane_overlap_icp(
src, snrm, tgt, R0, t0, iters=refine_iters, trim=refine_trim
)
cands.append((res2, R2, t2))
if not cands:
return eye4
# Restrict the (more expensive) symmetric re-rank to the candidates whose one-directional residual
# is within a small factor of the best — i.e. the genuinely competing basins — so a clearly bad
# converge can't win on a fluke of the symmetric term.
r_min = min(c[0] for c in cands)
contenders = [c for c in cands if c[0] <= max(r_min * 3.0, r_min + 1e-4)] or [
min(cands, key=lambda c: c[0])
]
best_sym = float("inf")
best = eye4.clone()
for res2, R2, t2 in contenders:
sym = _symmetric_overlap_residual(src, tgt, R2, 1.0, t2)
if sym < best_sym:
best_sym = sym
Tb = torch.eye(4, device=dev, dtype=torch.float64)
Tb[:3, :3] = R2
Tb[:3, 3] = t2
best = Tb.to(dtype=dtype, device=dev)
return best
# ── ambiguity probe (does the surviving overlap pin down the rotation?) ─────────
def _overlap_residual_norm(
src_full: torch.Tensor,
tgt_full: torch.Tensor,
T: torch.Tensor,
*,
with_scale: bool,
n_src: int = 2048,
n_tgt: int = 1024,
) -> float:
"""Normalised target→source overlap residual at the converged pose (0 = the shapes fit).
Because the (possibly cropped) target should be a SUBSET of the transformed full source under
the true pose, every observed target point must land on the source surface, so a CORRECT pose
drives this trimmed nearest-neighbour residual to ~0. A wrong / unrecoverable pose leaves the
target floating off the surface → a large residual. This is the decisive failure signal: it
directly measures "did we actually align the observed geometry", independent of any rotation
bookkeeping. Normalised by the target RMS radius so it is a dimensionless, object-relative
fraction.
"""
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 float("inf")
block = T[:3, :3].double()
t = T[:3, 3].double()
TA = src @ block.transpose(-1, -2) + t # full source -> target frame (block already s*R)
d = torch.cdist(tgt, TA).min(dim=1).values # each observed target -> nearest source
k = max(3, int(0.85 * d.shape[0])) # trim occlusion-boundary tail
resid = float(d.topk(k, largest=False).values.mean().item())
scale_ref = float((tgt - tgt.mean(dim=0)).norm(dim=1).pow(2).mean().sqrt().clamp_min(1e-9).item())
return resid / scale_ref
def _symmetric_overlap_residual(
src: torch.Tensor, tgt: torch.Tensor, R: torch.Tensor, s: float, t: torch.Tensor, trim: float = 0.8
) -> float:
"""BIDIRECTIONAL trimmed overlap residual (already-subsampled, double clouds).
The one-directional target→source residual (:func:`_overlap_residual_norm`) is blind to scale:
shrinking the source toward the target region keeps every target point near *some* source point,
so it never penalises a too-small scale. The scale DoF is only pinned by *also* requiring the
overlapping source points to land on the target, i.e. a symmetric Chamfer over the shared band.
This returns ``mean(trimmed tgt→src) + mean(trimmed src_overlap→tgt)``; the second term is the one
that makes the residual a true function of scale, so a 1-D line-search over ``s`` has a real
minimum at the correct scale instead of a flat valley.
"""