text
stringlengths
0
1.47k
settles several degrees off (both were verified directly on the benchmark crops):
* **Pivot the incremental rotation about the OVERLAP centroid, not the source centroid.** This
is the load-bearing fix. A one-sided crop's centroid does not correspond to the full-source
centroid, so a rotation parametrised about the source/world origin couples rotation into a
large translation error and the trimmed objective's minimum drifts ~5-13° off the true pose.
Mapped instead about the centroid of the matched (overlapping) region, ``q.mean(0)``, the
trimmed target→source cost becomes a SHARP basin whose minimum sits exactly at the true pose
(residual ~0), so a seed within the basin descends straight to it. (Measured: with the source
pivot the cost is minimised 3-13° from ground truth; with the overlap pivot the cost rises
monotonically from 0 at the true pose to ~70× at 10° off, a clean, attracting basin.)
* **Levenberg-style diagonal damping** (``lam`` × diag(JᵀJ)). At a perfect subset-overlap
optimum the matched residuals are ~0 and the one-sided crop leaves some rotational DoF weakly
constrained, so an undamped Gauss-Newton step jitters in that near-null space and walks OFF the
optimum even when started exactly on it. Damping pins the unconstrained directions so the step
stays on the true pose once reached.
Why point-to-plane (not point-to-point): point-to-plane lets each observed target point slide
ALONG the source surface to its correct tangential position, so the true basin sharpens to
residual ~0 while wrong basins stay high, a separable score that both recovers the pose AND lets
the honest ambiguity gate fire only on the truly-unrecoverable crops.
Each iteration: match every (trimmed) target point to its nearest transformed-source point, then
take one damped Gauss-Newton point-to-plane step (linearised small-angle SE(3), rotation about
the overlap centroid) minimising ``sum_i [ n_i . ( (R q_i + t) - p_i ) ]^2`` over the matched
source point ``q_i`` (normal ``n_i``) and target point ``p_i``. The trim drops the ``1-trim``
worst matches (occlusion boundary / outside-overlap source) so the partial target can't drag the
fit off-pose. All math in float64; CPU-friendly (one ``cdist`` + one damped 6×6 solve per iter).
Args:
src: full source cloud ``(Ns, 3)`` float64.
snrm: per-source-point unit normals ``(Ns, 3)`` float64.
tgt: (partial) target cloud ``(Nt, 3)`` float64.
R0, t0: initial rotation ``(3, 3)`` and translation ``(3,)`` (source→target).
iters: ICP iterations.
trim: fraction of best target matches kept each iteration.
lam: Levenberg diagonal-damping factor (× the mean of diag(JᵀJ)).
Returns:
``(R, t, residual)``, refined rotation/translation and the final trimmed target→source
overlap residual (lower = better fit).
"""
R = R0.clone()
t = t0.clone()
dt = src.dtype
keep_k = max(6, int(round(trim * tgt.shape[0])))
keep_k = min(keep_k, tgt.shape[0])
eye6 = torch.eye(6, device=src.device, dtype=dt)
eye3 = torch.eye(3, device=src.device, dtype=dt)
for _ in range(int(iters)):
TA = src @ R.transpose(-1, -2) + t # transformed source (Ns, 3)
nrmA = snrm @ R.transpose(-1, -2) # transformed source normals (Ns, 3)
d = torch.cdist(tgt, TA) # (Nt, Ns)
dmin, nn = d.min(dim=1) # nearest transformed-source per target
thr = torch.kthvalue(dmin, keep_k).values
m = dmin <= thr
if int(m.sum().item()) < 6:
break
p = tgt[m] # target points (M, 3)
q = TA[nn[m]] # matched source points (M, 3)
nq = nrmA[nn[m]] # matched source normals (M, 3)
pivot = q.mean(0) # OVERLAP centroid — rotate the increment about this, not the origin
qc = q - pivot
# point-to-plane linear system (rotation about the overlap pivot): row = [ qc x n , n ].
Arow = torch.cat([torch.cross(qc, nq, dim=1), nq], dim=1) # (M, 6)
b = -(nq * (q - p)).sum(dim=1) # (M,)
AtA = Arow.transpose(-1, -2) @ Arow
H = AtA + lam * torch.diag(torch.diagonal(AtA).clamp_min(1e-12)) + 1e-12 * eye6
try:
x = torch.linalg.solve(H, Arow.transpose(-1, -2) @ b)
except Exception:
break
if not torch.isfinite(x).all():
break
om = x[:3]
tr = x[3:]
th = om.norm().clamp_min(1e-12)
K = torch.zeros(3, 3, device=src.device, dtype=dt)
K[0, 1], K[0, 2] = -om[2], om[1]
K[1, 0], K[1, 2] = om[2], -om[0]
K[2, 0], K[2, 1] = -om[1], om[0]
dR = eye3 + (torch.sin(th) / th) * K + ((1.0 - torch.cos(th)) / (th * th)) * (K @ K)
# Apply the increment about the overlap pivot: x' = dR (x - pivot) + pivot + tr ⇒
# R ← dR R , t ← dR t + (pivot + tr - dR pivot).
R = dR @ R
t = dR @ t + (pivot + tr - dR @ pivot)
TA = src @ R.transpose(-1, -2) + t
dmin = torch.cdist(tgt, TA).min(dim=1).values
res = float(dmin.topk(keep_k, largest=False).values.mean().item())
return R, t, res
def _batched_t2s_prefilter(
src: torch.Tensor,
tgt: torch.Tensor,
seeds: torch.Tensor,
*,
iters: int,
trim: float,
seed_batch: int = 128,