File size: 1,779 Bytes
f8fe8a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""3D Gaussian Splatting model container and rendering, on top of gsplat.

Parameters are stored in their optimization spaces (scales in log, opacities in
logit) to match gsplat's DefaultStrategy expectations.  Field layout:

    means      [N,3]   float
    scales     [N,3]   log-scale
    quats      [N,4]   (normalized internally by gsplat)
    opacities  [N]     logit
    sh0        [N,1,3] SH DC term
    shN        [N,M,3] SH higher-order terms, M = (sh_degree+1)^2 - 1
"""
from typing import Dict, Tuple

import torch
from gsplat import rasterization

# canonical ordering of fields and the per-field component counts
FIELDS = ["means", "scales", "quats", "opacities", "sh0", "shN"]


def colors_from_params(params: Dict[str, torch.Tensor]) -> torch.Tensor:
    return torch.cat([params["sh0"], params["shN"]], dim=1)  # [N, K, 3]


def render(params: Dict[str, torch.Tensor], viewmats: torch.Tensor, Ks: torch.Tensor,
           W: int, H: int, sh_degree: int, bg_white: bool = True,
           packed: bool = True, absgrad: bool = False) -> Tuple[torch.Tensor, torch.Tensor, dict]:
    """Render C cameras. Returns (renders[C,H,W,3] clamped to [0,1], alphas, info)."""
    colors = colors_from_params(params)
    renders, alphas, info = rasterization(
        params["means"], params["quats"], torch.exp(params["scales"]),
        torch.sigmoid(params["opacities"]), colors, viewmats, Ks, W, H,
        sh_degree=sh_degree, packed=packed, absgrad=absgrad,
        rasterize_mode="classic",
    )
    # gsplat composites over black; composite over white using accumulated alpha.
    if bg_white:
        renders = renders + (1.0 - alphas)
    return renders, alphas, info


def num_gaussians(params: Dict[str, torch.Tensor]) -> int:
    return params["means"].shape[0]