|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import torch |
|
import math |
|
from diff_gaussian_rasterization import ( |
|
GaussianRasterizationSettings, |
|
GaussianRasterizer, |
|
) |
|
from scene.gaussian_model import GaussianModel |
|
from utils.sh_utils import eval_sh |
|
from utils.pose_utils import get_camera_from_tensor, quadmultiply |
|
|
|
|
|
def render( |
|
viewpoint_camera, |
|
pc: GaussianModel, |
|
pipe, |
|
bg_color: torch.Tensor, |
|
scaling_modifier=1.0, |
|
override_color=None, |
|
camera_pose=None, |
|
filtering=None |
|
): |
|
""" |
|
Render the scene. |
|
|
|
Background tensor (bg_color) must be on GPU! |
|
""" |
|
if filtering is None: |
|
filtering = torch.ones(pc.get_xyz.shape[0], dtype=torch.bool, device="cuda") |
|
|
|
screenspace_points = ( |
|
torch.zeros_like( |
|
pc.get_xyz[filtering], dtype=pc.get_xyz.dtype, requires_grad=True, device="cuda" |
|
) |
|
+ 0 |
|
) |
|
try: |
|
screenspace_points.retain_grad() |
|
except: |
|
pass |
|
|
|
|
|
tanfovx = math.tan(viewpoint_camera.FoVx * 0.5) |
|
tanfovy = math.tan(viewpoint_camera.FoVy * 0.5) |
|
|
|
|
|
w2c = torch.eye(4).cuda() |
|
projmatrix = ( |
|
w2c.unsqueeze(0).bmm(viewpoint_camera.projection_matrix.unsqueeze(0)) |
|
).squeeze(0) |
|
camera_pos = w2c.inverse()[3, :3] |
|
raster_settings = GaussianRasterizationSettings( |
|
image_height=int(viewpoint_camera.image_height), |
|
image_width=int(viewpoint_camera.image_width), |
|
tanfovx=tanfovx, |
|
tanfovy=tanfovy, |
|
bg=bg_color, |
|
scale_modifier=scaling_modifier, |
|
|
|
|
|
viewmatrix=w2c, |
|
projmatrix=projmatrix, |
|
sh_degree=pc.active_sh_degree, |
|
|
|
campos=camera_pos, |
|
prefiltered=False, |
|
debug=pipe.debug, |
|
) |
|
|
|
rasterizer = GaussianRasterizer(raster_settings=raster_settings) |
|
|
|
|
|
rel_w2c = get_camera_from_tensor(camera_pose) |
|
|
|
gaussians_xyz = pc._xyz.clone()[filtering] |
|
gaussians_rot = pc._rotation.clone()[filtering] |
|
|
|
xyz_ones = torch.ones(gaussians_xyz.shape[0], 1).cuda().float() |
|
xyz_homo = torch.cat((gaussians_xyz, xyz_ones), dim=1) |
|
gaussians_xyz_trans = (rel_w2c @ xyz_homo.T).T[:, :3] |
|
gaussians_rot_trans = quadmultiply(camera_pose[:4], gaussians_rot) |
|
means3D = gaussians_xyz_trans |
|
means2D = screenspace_points |
|
|
|
opacity = pc.get_opacity[filtering] |
|
|
|
opacity = opacity * pc._conf_static.reshape(-1, 1)[pc.aggregated_mask] |
|
|
|
|
|
|
|
scales = None |
|
rotations = None |
|
cov3D_precomp = None |
|
if pipe.compute_cov3D_python: |
|
cov3D_precomp = pc.get_covariance(scaling_modifier) |
|
else: |
|
scales = pc.get_scaling[filtering] |
|
rotations = gaussians_rot_trans |
|
|
|
|
|
|
|
shs = None |
|
colors_precomp = None |
|
if override_color is None: |
|
if pipe.convert_SHs_python: |
|
shs_view = pc.get_features.transpose(1, 2).view( |
|
-1, 3, (pc.max_sh_degree + 1) ** 2 |
|
) |
|
dir_pp = pc.get_xyz - viewpoint_camera.camera_center.repeat( |
|
pc.get_features.shape[0], 1 |
|
) |
|
dir_pp_normalized = dir_pp / dir_pp.norm(dim=1, keepdim=True) |
|
sh2rgb = eval_sh(pc.active_sh_degree, shs_view, dir_pp_normalized) |
|
colors_precomp = torch.clamp_min(sh2rgb + 0.5, 0.0) |
|
else: |
|
shs = pc.get_features[filtering] |
|
else: |
|
colors_precomp = override_color |
|
|
|
|
|
rendered_image, radii = rasterizer( |
|
means3D=means3D, |
|
means2D=means2D, |
|
shs=shs, |
|
colors_precomp=colors_precomp, |
|
opacities=opacity, |
|
scales=scales, |
|
rotations=rotations, |
|
cov3D_precomp=cov3D_precomp, |
|
) |
|
|
|
|
|
|
|
return { |
|
"render": rendered_image, |
|
"viewspace_points": screenspace_points, |
|
"visibility_filter": radii > 0, |
|
"radii": radii, |
|
} |
|
|
|
|
|
def render_test( |
|
viewpoint_camera, |
|
pc: GaussianModel, |
|
pipe, |
|
bg_color: torch.Tensor, |
|
scaling_modifier=1.0, |
|
override_color=None, |
|
camera_pose=None, |
|
filtering=None |
|
): |
|
""" |
|
Render the scene. |
|
|
|
Background tensor (bg_color) must be on GPU! |
|
""" |
|
if filtering is None: |
|
filtering = torch.ones(pc.get_xyz.shape[0], dtype=torch.bool, device="cuda") |
|
|
|
screenspace_points = ( |
|
torch.zeros_like( |
|
pc.get_xyz[filtering], dtype=pc.get_xyz.dtype, requires_grad=True, device="cuda" |
|
) |
|
+ 0 |
|
) |
|
try: |
|
screenspace_points.retain_grad() |
|
except: |
|
pass |
|
|
|
|
|
tanfovx = math.tan(viewpoint_camera.FoVx * 0.5) |
|
tanfovy = math.tan(viewpoint_camera.FoVy * 0.5) |
|
|
|
|
|
w2c = torch.eye(4).cuda() |
|
projmatrix = ( |
|
w2c.unsqueeze(0).bmm(viewpoint_camera.projection_matrix.unsqueeze(0)) |
|
).squeeze(0) |
|
camera_pos = w2c.inverse()[3, :3] |
|
raster_settings = GaussianRasterizationSettings( |
|
image_height=int(viewpoint_camera.image_height), |
|
image_width=int(viewpoint_camera.image_width), |
|
tanfovx=tanfovx, |
|
tanfovy=tanfovy, |
|
bg=bg_color, |
|
scale_modifier=scaling_modifier, |
|
|
|
|
|
viewmatrix=w2c, |
|
projmatrix=projmatrix, |
|
sh_degree=pc.active_sh_degree, |
|
|
|
campos=camera_pos, |
|
prefiltered=False, |
|
debug=pipe.debug, |
|
) |
|
|
|
rasterizer = GaussianRasterizer(raster_settings=raster_settings) |
|
|
|
|
|
rel_w2c = get_camera_from_tensor(camera_pose) |
|
|
|
gaussians_xyz = pc._xyz.clone()[filtering] |
|
gaussians_rot = pc._rotation.clone()[filtering] |
|
|
|
xyz_ones = torch.ones(gaussians_xyz.shape[0], 1).cuda().float() |
|
xyz_homo = torch.cat((gaussians_xyz, xyz_ones), dim=1) |
|
gaussians_xyz_trans = (rel_w2c @ xyz_homo.T).T[:, :3] |
|
gaussians_rot_trans = quadmultiply(camera_pose[:4], gaussians_rot) |
|
means3D = gaussians_xyz_trans |
|
means2D = screenspace_points |
|
|
|
opacity = pc.get_opacity[filtering] |
|
|
|
opacity = opacity * pc._conf_static |
|
|
|
|
|
scales = None |
|
rotations = None |
|
cov3D_precomp = None |
|
if pipe.compute_cov3D_python: |
|
cov3D_precomp = pc.get_covariance(scaling_modifier) |
|
else: |
|
scales = pc.get_scaling[filtering] |
|
rotations = gaussians_rot_trans |
|
|
|
|
|
|
|
shs = None |
|
colors_precomp = None |
|
if override_color is None: |
|
if pipe.convert_SHs_python: |
|
shs_view = pc.get_features.transpose(1, 2).view( |
|
-1, 3, (pc.max_sh_degree + 1) ** 2 |
|
) |
|
dir_pp = pc.get_xyz - viewpoint_camera.camera_center.repeat( |
|
pc.get_features.shape[0], 1 |
|
) |
|
dir_pp_normalized = dir_pp / dir_pp.norm(dim=1, keepdim=True) |
|
sh2rgb = eval_sh(pc.active_sh_degree, shs_view, dir_pp_normalized) |
|
colors_precomp = torch.clamp_min(sh2rgb + 0.5, 0.0) |
|
else: |
|
shs = pc.get_features[filtering] |
|
else: |
|
colors_precomp = override_color |
|
|
|
|
|
rendered_image, radii = rasterizer( |
|
means3D=means3D, |
|
means2D=means2D, |
|
shs=shs, |
|
colors_precomp=colors_precomp, |
|
opacities=opacity, |
|
scales=scales, |
|
rotations=rotations, |
|
cov3D_precomp=cov3D_precomp, |
|
) |
|
|
|
|
|
|
|
return { |
|
"render": rendered_image, |
|
"viewspace_points": screenspace_points, |
|
"visibility_filter": radii > 0, |
|
"radii": radii, |
|
} |
|
|
|
def render_no_soft( |
|
viewpoint_camera, |
|
pc: GaussianModel, |
|
pipe, |
|
bg_color: torch.Tensor, |
|
scaling_modifier=1.0, |
|
override_color=None, |
|
camera_pose=None, |
|
filtering=None |
|
): |
|
""" |
|
Render the scene. |
|
|
|
Background tensor (bg_color) must be on GPU! |
|
""" |
|
if filtering is None: |
|
filtering = torch.ones(pc.get_xyz.shape[0], dtype=torch.bool, device="cuda") |
|
|
|
screenspace_points = ( |
|
torch.zeros_like( |
|
pc.get_xyz[filtering], dtype=pc.get_xyz.dtype, requires_grad=True, device="cuda" |
|
) |
|
+ 0 |
|
) |
|
try: |
|
screenspace_points.retain_grad() |
|
except: |
|
pass |
|
|
|
FoVx = pc.FoVx |
|
FoVy = pc.FoVy |
|
|
|
|
|
tanfovx = math.tan(FoVx * 0.5) |
|
tanfovy = math.tan(FoVy * 0.5) |
|
|
|
|
|
w2c = torch.eye(4).cuda() |
|
projmatrix = ( |
|
w2c.unsqueeze(0).bmm(viewpoint_camera.get_projection_matrix(FoVx, FoVy).unsqueeze(0)) |
|
).squeeze(0) |
|
camera_pos = w2c.inverse()[3, :3] |
|
raster_settings = GaussianRasterizationSettings( |
|
image_height=int(viewpoint_camera.image_height), |
|
image_width=int(viewpoint_camera.image_width), |
|
tanfovx=tanfovx, |
|
tanfovy=tanfovy, |
|
bg=bg_color, |
|
scale_modifier=scaling_modifier, |
|
|
|
|
|
viewmatrix=w2c, |
|
projmatrix=projmatrix, |
|
sh_degree=pc.active_sh_degree, |
|
|
|
campos=camera_pos, |
|
prefiltered=False, |
|
debug=pipe.debug, |
|
) |
|
|
|
rasterizer = GaussianRasterizer(raster_settings=raster_settings) |
|
|
|
|
|
rel_w2c = get_camera_from_tensor(camera_pose) |
|
|
|
gaussians_xyz = pc._xyz.clone()[filtering] |
|
gaussians_rot = pc._rotation.clone()[filtering] |
|
|
|
xyz_ones = torch.ones(gaussians_xyz.shape[0], 1).cuda().float() |
|
xyz_homo = torch.cat((gaussians_xyz, xyz_ones), dim=1) |
|
gaussians_xyz_trans = (rel_w2c @ xyz_homo.T).T[:, :3] |
|
gaussians_rot_trans = quadmultiply(camera_pose[:4], gaussians_rot) |
|
means3D = gaussians_xyz_trans |
|
means2D = screenspace_points |
|
|
|
opacity = pc.get_opacity[filtering] |
|
|
|
|
|
|
|
|
|
|
|
scales = None |
|
rotations = None |
|
cov3D_precomp = None |
|
if pipe.compute_cov3D_python: |
|
cov3D_precomp = pc.get_covariance(scaling_modifier) |
|
else: |
|
scales = pc.get_scaling[filtering] |
|
rotations = gaussians_rot_trans |
|
|
|
|
|
|
|
shs = None |
|
colors_precomp = None |
|
if override_color is None: |
|
if pipe.convert_SHs_python: |
|
shs_view = pc.get_features.transpose(1, 2).view( |
|
-1, 3, (pc.max_sh_degree + 1) ** 2 |
|
) |
|
dir_pp = pc.get_xyz - viewpoint_camera.camera_center.repeat( |
|
pc.get_features.shape[0], 1 |
|
) |
|
dir_pp_normalized = dir_pp / dir_pp.norm(dim=1, keepdim=True) |
|
sh2rgb = eval_sh(pc.active_sh_degree, shs_view, dir_pp_normalized) |
|
colors_precomp = torch.clamp_min(sh2rgb + 0.5, 0.0) |
|
else: |
|
shs = pc.get_features[filtering] |
|
else: |
|
colors_precomp = override_color |
|
|
|
|
|
rendered_image, radii = rasterizer( |
|
means3D=means3D, |
|
means2D=means2D, |
|
shs=shs, |
|
colors_precomp=colors_precomp, |
|
opacities=opacity, |
|
scales=scales, |
|
rotations=rotations, |
|
cov3D_precomp=cov3D_precomp, |
|
) |
|
|
|
|
|
|
|
return { |
|
"render": rendered_image, |
|
"viewspace_points": screenspace_points, |
|
"visibility_filter": radii > 0, |
|
"radii": radii, |
|
} |
|
|
|
def render_confidence( |
|
viewpoint_camera, |
|
pc: GaussianModel, |
|
pipe, |
|
bg_color: torch.Tensor, |
|
scaling_modifier=1.0, |
|
override_color=None, |
|
camera_pose=None, |
|
filtering=None |
|
): |
|
""" |
|
Render the confidence. |
|
|
|
Background tensor (bg_color) must be on GPU! |
|
""" |
|
if filtering is None: |
|
filtering = torch.ones(pc.get_xyz.shape[0], dtype=torch.bool, device="cuda") |
|
|
|
screenspace_points = ( |
|
torch.zeros_like( |
|
pc.get_xyz[filtering], dtype=pc.get_xyz.dtype, requires_grad=True, device="cuda" |
|
) |
|
+ 0 |
|
) |
|
try: |
|
screenspace_points.retain_grad() |
|
except: |
|
pass |
|
|
|
|
|
tanfovx = math.tan(viewpoint_camera.FoVx * 0.5) |
|
tanfovy = math.tan(viewpoint_camera.FoVy * 0.5) |
|
|
|
|
|
w2c = torch.eye(4).cuda() |
|
projmatrix = ( |
|
w2c.unsqueeze(0).bmm(viewpoint_camera.projection_matrix.unsqueeze(0)) |
|
).squeeze(0) |
|
camera_pos = w2c.inverse()[3, :3] |
|
raster_settings = GaussianRasterizationSettings( |
|
image_height=int(viewpoint_camera.image_height), |
|
image_width=int(viewpoint_camera.image_width), |
|
tanfovx=tanfovx, |
|
tanfovy=tanfovy, |
|
bg=bg_color, |
|
scale_modifier=scaling_modifier, |
|
|
|
|
|
viewmatrix=w2c, |
|
projmatrix=projmatrix, |
|
sh_degree=pc.active_sh_degree, |
|
|
|
campos=camera_pos, |
|
prefiltered=False, |
|
debug=pipe.debug, |
|
) |
|
|
|
rasterizer = GaussianRasterizer(raster_settings=raster_settings) |
|
|
|
|
|
rel_w2c = get_camera_from_tensor(camera_pose) |
|
|
|
gaussians_xyz = pc._xyz.clone()[filtering] |
|
gaussians_rot = pc._rotation.clone()[filtering] |
|
|
|
xyz_ones = torch.ones(gaussians_xyz.shape[0], 1).cuda().float() |
|
xyz_homo = torch.cat((gaussians_xyz, xyz_ones), dim=1) |
|
gaussians_xyz_trans = (rel_w2c @ xyz_homo.T).T[:, :3] |
|
gaussians_rot_trans = quadmultiply(camera_pose[:4], gaussians_rot) |
|
means3D = gaussians_xyz_trans |
|
means2D = screenspace_points |
|
opacity = pc.get_opacity[filtering] |
|
opacity = torch.ones_like(opacity) |
|
|
|
|
|
|
|
scales = None |
|
rotations = None |
|
cov3D_precomp = None |
|
if pipe.compute_cov3D_python: |
|
cov3D_precomp = pc.get_covariance(scaling_modifier) |
|
else: |
|
scales = pc.get_scaling[filtering] |
|
rotations = gaussians_rot_trans |
|
|
|
shs = None |
|
colors_precomp = pc._conf[filtering].unsqueeze(1).repeat(1, 3) |
|
|
|
|
|
rendered_image, radii = rasterizer( |
|
means3D=means3D, |
|
means2D=means2D, |
|
shs=shs, |
|
colors_precomp=colors_precomp, |
|
opacities=opacity, |
|
scales=scales, |
|
rotations=rotations, |
|
cov3D_precomp=cov3D_precomp, |
|
) |
|
|
|
return rendered_image |
|
|