gsgen3d / gsgen

[CVPR 2024] Text-to-3D using Gaussian Splatting
https://arxiv.org/abs/2309.16585
MIT License
771 stars 46 forks source link

Explanation of distance_to_gaussian_surface #24

Closed jaidevshriram closed 10 months ago

jaidevshriram commented 11 months ago

Hi, I was going through the codebase and came across the following function that computes the distance between points and the gaussian surface. Is there any derivation or explanation available for this code?

For instance:

def distance_to_gaussian_surface(mean, svec, rotmat, query):

    # mean - N x 3 - torch.Tensor (mean position)
    # svec - N x 3 - torch.Tensor (scale vector)
    # rotmat - N x 3 x 3 - torch.Tensor (rotation matrix)
    # query - N x 3 - torch.Tensor (query points)

    xyz = query - mean
    # TODO: check here
    # breakpoint()

    xyz = torch.einsum("bij,bj->bi", rotmat.transpose(-1, -2), xyz)
    xyz = F.normalize(xyz, dim=-1)

    z = xyz[..., 2]
    y = xyz[..., 1]
    x = xyz[..., 0]

    r_xy = torch.sqrt(x**2 + y**2 + 1e-10)

    cos_theta = z
    sin_theta = r_xy
    cos_phi = x / r_xy
    sin_phi = y / r_xy

    d2 = svec[..., 0] ** 2 * cos_phi**2 + svec[..., 1] ** 2 * sin_phi**2
    r2 = svec[..., 2] ** 2 * cos_theta**2 + d2**2 * sin_theta**2

    return torch.sqrt(r2 + 1e-10)