text
stringlengths
0
1.47k
[r * torch.sin(alpha), r * torch.cos(alpha), rr * torch.sin(beta), rr * torch.cos(beta)], dim=1
)
def _quats_to_R(q: torch.Tensor) -> torch.Tensor:
"""Unit quaternions ``(n, 4)`` ``(x, y, z, w)`` -> rotation matrices ``(n, 3, 3)``."""
q = q / q.norm(dim=1, keepdim=True).clamp_min(1e-12)
x, y, z, w = q[:, 0], q[:, 1], q[:, 2], q[:, 3]
R = q.new_empty((q.shape[0], 3, 3))
R[:, 0, 0] = 1 - 2 * (y * y + z * z)
R[:, 0, 1] = 2 * (x * y - z * w)
R[:, 0, 2] = 2 * (x * z + y * w)
R[:, 1, 0] = 2 * (x * y + z * w)
R[:, 1, 1] = 1 - 2 * (x * x + z * z)
R[:, 1, 2] = 2 * (y * z - x * w)
R[:, 2, 0] = 2 * (x * z - y * w)
R[:, 2, 1] = 2 * (y * z + x * w)
R[:, 2, 2] = 1 - 2 * (x * x + y * y)
return R
def super_fibonacci_so3(n: int, device=None, dtype=torch.float64) -> torch.Tensor:
"""Deterministic near-uniform SO(3) covering as ``(n, 3, 3)`` rotation matrices (on-device).
Super-Fibonacci spiral on the quaternion 3-sphere (Alexa, CVPR 2022). ~26deg covering at
``n == 256``.
"""
return _quats_to_R(_superfib_quats(int(n), device, dtype))
# ── PCA sign-flip seeds (on-device torch) ─────────────────────────────────────────────
# Eigenvalue-spread threshold for degenerate-PCA detection (symmetric-object note).
# If the ratio of the largest to smallest PCA eigenvalue is below this, the cloud is
# near-isotropic (sphere-like) and the PCA axes are arbitrary/unstable.
# Used only for diagnostic purposes in _pca_seed_rotations; the PCA seeds are kept
# regardless (removing them degrades performance on the sphere because PCA seeds
# accidentally provide good centroid-alignment candidates that the 256-seed Fibonacci
# grid at default density would miss, since a sphere is near-rotationally symmetric
# but NOT perfectly so at N=800 — individual seeds still differ in Chamfer score by
# ~9mm, which the trimmed ICP can distinguish).
_PCA_ISOTROPY_THRESH = 2.0
def _pca_axes(pts_centered: torch.Tensor) -> torch.Tensor:
"""Principal axes (as columns), descending variance, from centered points."""
_, _, Vh = torch.linalg.svd(pts_centered, full_matrices=False)
return Vh.transpose(-2, -1)
def _pca_seed_rotations(src: torch.Tensor, tgt: torch.Tensor) -> torch.Tensor:
"""Identity + PCA principal-axis-match sign flips, ``(<=5, 3, 3)`` on-device.
ICP from identity alone has a small basin; PCA-axis seeds cover large symmetry-axis
rotations a uniform grid may straddle.
Note on symmetric objects: for near-isotropic clouds the PCA axes
are arbitrary (eigenvalue spread < _PCA_ISOTROPY_THRESH). We keep the PCA seeds
anyway because on the test sphere (N=800) individual rotations still differ in
Chamfer score by ~9mm, so the batched trimmed ICP still selects among them
meaningfully, and at default 256 Fibonacci seeds the grid is too coarse to guarantee
a near-centroid seed without the PCA candidates providing additional coverage.
Removing PCA seeds for isotropic clouds was tested and reliably made the symmetric
result WORSE (8/9→3/9) due to the Fibonacci grid missing the correct basin.
"""
dev, dt = src.device, src.dtype
Vs = _pca_axes(src - src.mean(0))
Vt = _pca_axes(tgt - tgt.mean(0))
cands = [torch.eye(3, device=dev, dtype=dt)]
for sx in (1.0, -1.0):
for sy in (1.0, -1.0):
S = torch.diag(torch.tensor([sx, sy, sx * sy], device=dev, dtype=dt))
R = Vt @ S @ Vs.transpose(-2, -1)
if torch.linalg.det(R) > 0: # proper rotation only
cands.append(R)
return torch.stack(cands, dim=0)
# ── closed-form similarity (Umeyama 1991, on-device torch) ────────────────────────────
def _umeyama(src: torch.Tensor, dst: torch.Tensor, with_scale: bool):
"""Least-squares similarity ``dst ≈ s * (src @ R.T) + t`` (Umeyama 1991), all in torch.
``with_scale=False`` fixes ``s = 1`` (rigid Kabsch). Returns ``(s, R, t)`` torch tensors on
``src``'s device/dtype. Run in float64 (cast by the caller) for SVD precision.
"""
n = src.shape[0]
src_mean = src.mean(0)
dst_mean = dst.mean(0)
sc = src - src_mean
dc = dst - dst_mean
cov = (dc.transpose(-2, -1) @ sc) / n
U, D, Vh = torch.linalg.svd(cov)
S = torch.eye(3, device=src.device, dtype=src.dtype)
if torch.linalg.det(U) * torch.linalg.det(Vh) < 0: # reflection guard
S[2, 2] = -1.0
R = U @ S @ Vh
if with_scale:
var_src = sc.pow(2).sum() / n