text
stringlengths
0
1.47k
Returns:
``(N, 3*bins)`` float32 SPFH (each point's row L1-sums to 3, one per feature block).
"""
N, k = idx.shape
dev = pts.device
p = pts.unsqueeze(1) # (N, 1, 3)
q = pts[idx] # (N, k, 3)
n_p = normals.unsqueeze(1) # (N, 1, 3)
n_q = normals[idx] # (N, k, 3)
diff = q - p # (N, k, 3)
dist = diff.norm(dim=2, keepdim=True).clamp_min(1e-9) # (N, k, 1)
pq = diff / dist # (N, k, 3) unit vector p->q
u = n_p.expand(N, k, 3) # (N, k, 3) Darboux u = source normal
v = torch.cross(pq, u, dim=2) # (N, k, 3)
v = v / v.norm(dim=2, keepdim=True).clamp_min(1e-9)
w = torch.cross(u, v, dim=2) # (N, k, 3)
alpha = (v * n_q).sum(dim=2).clamp(-1.0, 1.0) # (N, k)
phi = (u * pq).sum(dim=2).clamp(-1.0, 1.0) # (N, k)
theta = torch.atan2((w * n_q).sum(dim=2), (u * n_q).sum(dim=2)) # (N, k) in [-pi, pi]
# Normalise each feature to [0, 1) for binning.
a01 = (alpha + 1.0) * 0.5
p01 = (phi + 1.0) * 0.5
t01 = (theta + math.pi) / (2.0 * math.pi)
def hist(x01: torch.Tensor) -> torch.Tensor:
# bin index in [0, bins-1]; scatter-add a count per bin per row
b = (x01 * bins).floor().clamp(0, bins - 1).to(torch.int64) # (N, k)
h = torch.zeros(N, bins, device=dev, dtype=torch.float32)
h.scatter_add_(1, b, torch.ones_like(b, dtype=torch.float32))
return h # (N, bins)
spfh = torch.cat([hist(a01), hist(p01), hist(t01)], dim=1) # (N, 3*bins)
# L1-normalise each feature block to k counts -> fraction (robust to varying neighbour counts).
spfh = spfh / float(k)
return spfh
def _fpfh(pts: torch.Tensor, normals: torch.Tensor, k: int, bins: int) -> torch.Tensor:
"""Fast Point Feature Histogram ``(N, 3*bins)`` (Rusu et al., ICRA 2009).
``FPFH(p) = SPFH(p) + (1/k) * sum_j (1/d(p, p_j)) * SPFH(p_j)`` over the k neighbours, with the
neighbour SPFHs inverse-distance weighted. The result is concatenated-and-normalised so the
descriptor stays scale-comparable across clouds of different size.
Args:
pts: ``(N, 3)`` cloud.
normals: ``(N, 3)`` unit normals.
k: neighbourhood size.
bins: histogram bins per feature.
Returns:
``(N, 3*bins)`` float32 FPFH descriptors.
"""
idx = _knn_indices(pts, k) # (N, k)
spfh = _spfh(pts, normals, idx, bins) # (N, 3*bins)
# Inverse-distance neighbour weighting.
diff = pts[idx] - pts.unsqueeze(1) # (N, k, 3)
dist = diff.norm(dim=2).clamp_min(1e-9) # (N, k)
wgt = 1.0 / dist # (N, k)
wgt = wgt / wgt.sum(dim=1, keepdim=True).clamp_min(1e-9) # row-normalised weights
nbr_spfh = spfh[idx] # (N, k, 3*bins)
weighted = (wgt.unsqueeze(2) * nbr_spfh).sum(dim=1) # (N, 3*bins)
fpfh = spfh + weighted
# Final L2-normalise so feature-space NN distances are well-conditioned.
fpfh = fpfh / fpfh.norm(dim=1, keepdim=True).clamp_min(1e-9)
return fpfh.to(torch.float32)
# ── matching: ratio-test + mutual-NN ───────────────────────────────────────────
def _match_features(
desc_src: torch.Tensor,
desc_tgt: torch.Tensor,
ratio: float = _FS_RATIO,
) -> tuple[torch.Tensor, torch.Tensor]:
"""Ratio-test + mutual-nearest-neighbour matching in FPFH space.
For each source descriptor find its two nearest target descriptors; accept only if the best is
sufficiently closer than the second-best (Lowe's ratio test) AND the match is mutual (the
target's nearest source is back to this source). Both filters reject ambiguous matches on
smooth/symmetric regions, leaving a small high-confidence set.
Args:
desc_src: ``(Ns, D)`` source descriptors.
desc_tgt: ``(Nt, D)`` target descriptors.
ratio: Lowe ratio threshold (``d1 < ratio * d2`` to accept).
Returns:
``(src_idx, tgt_idx)`` int64 tensors of matching anchor indices, both ``(M,)``.
"""
dist = torch.cdist(desc_src, desc_tgt) # (Ns, Nt)
Ns, Nt = dist.shape