Complete merge

This commit is contained in:
Aayush committed 2025-01-18 21:09:17 -08:00
1 parent 2786a57a05
commit 0c3a4cdcef
3 files changed
+76 -21

No files matched your search

+27
View File
@@ -14,7 +14,10 @@
"""Sphecal Harmonics utils."""
import math
import torch
from e3nn.o3 import Irreps
from jaxtyping import Float
from torch import Tensor
@@ -109,3 +112,27 @@ def SH2RGB(sh):
"""
C0 = 0.28209479177387814
return sh * C0 + 0.5
def rotate_spherical_harmonics(
coeffs: Float[Tensor, "*batch dim_sh"],
rotation_matrix: Float[Tensor, "3 3"],
) -> Float[Tensor, "*batch dim_sh"]:
"""Rotates real spherical harmonic coefficients of degree l using given 3x3 rotation matrix.
Args:
coeffs : SH coefficients
rotation_matrix : A 3x3 rotation matrix.
Returns:
The rotated SH coefficients
"""
dim_sh = coeffs.shape[-1]
assert math.isqrt(dim_sh) ** 2 == dim_sh, "dim_sh must be a perfect square (l+1)^2"
sh_degree = int(math.sqrt(dim_sh)) - 1
rotation_matrix = rotation_matrix.cpu()
irreps = Irreps(" + ".join([f"{i}e" for i in range(sh_degree + 1)])) # Even parity spherical harmonics of degree l
D_matrix = irreps.D_from_matrix(rotation_matrix).to(coeffs.device) # Construct Wigner D-matrix
rotated_coeffs = torch.einsum("ij,...j->...i", D_matrix, coeffs)
return rotated_coeffs