Seoryxa / research /smooth.py
PureOne's picture
Release Seoryxa research and verification data
10d1f61 verified
Raw History Blame Contribute Delete
3.38 kB
"""Smooth harmonic image operator on point clouds with distinct positions.
The implementation uses polynomial node-multiset descriptors and a Gaussian root
kernel. This reference path omits edge attributes. All triples include repetitions.
The high-degree moment features make this a small-instance reference computation.
"""
from itertools import product
import numpy as np
from scipy.spatial.distance import pdist, squareform
from frames import harmonics
def descriptors(r, labels, triples, anchors):
# Newton identities turn power sums into the monic multiset polynomial.
# Evaluating on enough fixed t nodes determines each coefficient polynomial.
n, count = len(r), len(triples)
p = np.einsum('ni,mij->mnj', r, anchors)
role = (np.arange(n)[None, :, None] == triples[:, None, :])
attr = np.broadcast_to(labels[None, :, :], (count, n, labels.shape[1]))
records = np.concatenate((p, role, attr), axis=-1)
width = records.shape[-1]
t = np.linspace(-1, 1, n * (width - 1) + 1)
powers = np.vstack([t**j for j in range(width)])
projections = (records @ powers) / width
gram = (np.swapaxes(anchors, 1, 2) @ anchors).reshape(count, -1)
moments = [(projections**k).sum(axis=1) for k in range(1, n+1)]
return np.column_stack([gram, *moments])
def solid(degree, vectors):
shape = vectors.shape[:-1]
flat = vectors.reshape(-1, 3)
length = np.linalg.norm(flat, axis=1)
vals = harmonics(degree, flat).T * length[:, None]**degree
return vals.reshape(*shape, 2*degree+1)
def derivative_seeds(degree, vectors, normals):
if degree == 0:
return np.zeros((*vectors.shape[:-1], 1))
t = np.linspace(-1, 1, degree+1)
powers = np.vstack([t**k for k in range(degree+1)])
rhs = np.zeros(degree+1)
rhs[1] = 1.
weights = np.linalg.solve(powers, rhs)
return sum(w * solid(degree, vectors + s * normals[None, :, :])
for w, s in zip(weights, t))
def smooth_operator(points, degree, twist=0, bandwidth=0.4, labels=None):
if bandwidth <= 0 or degree < 0 or twist not in (0, 1):
raise ValueError("Expected positive bandwidth, degree >= 0 and twist 0 or 1.")
r = np.asarray(points, dtype=float)
if r.ndim != 2 or r.shape[1] != 3 or len(r) == 0:
raise ValueError("Expected nonempty Nx3 points.")
r = r - r.mean(axis=0)
n = len(r)
labels = (np.empty((n, 0)) if labels is None
else np.asarray(labels, dtype=float).reshape(n, -1))
triples = np.asarray(list(product(range(n), repeat=3)))
anchors = np.swapaxes(r[triples], 1, 2)
data = descriptors(r, labels, triples, anchors)
distance2 = squareform(pdist(data, metric='sqeuclidean'))
kernel = np.exp(-distance2 / (2 * bandwidth**2))
# [0,1] is an affine rescaling of the finite interpolation grid.
abscissas = np.linspace(0., 1., degree+1)
grid = np.asarray(list(product(abscissas, repeat=3)))
v = np.einsum('mij,qj->qmi', anchors, grid)
polar = solid(degree, v)
if twist:
normal = np.cross(anchors[:, :, 0], anchors[:, :, 1])
banks = [polar * np.linalg.det(anchors)[None, :, None],
derivative_seeds(degree, v, normal)]
else:
banks = [polar]
result = sum((np.swapaxes(bank, 1, 2) @ kernel @ bank).sum(axis=0)
for bank in banks)
return (result + result.T) / 2