mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-14 11:54:01 +08:00
* occupancy grid in progress * visualizing occupancy grid. currently restructing field and renderer head outputs * black and pylint. broke nerfw and semantic nerf but it's okay * get_project_root
54 lines
2.2 KiB
Python
54 lines
2.2 KiB
Python
"""
|
|
Ray generator.
|
|
"""
|
|
from torch import nn
|
|
from torchtyping import TensorType
|
|
from mattport.structures.rays import RayBundle
|
|
from mattport.structures.cameras import get_camera_model
|
|
|
|
|
|
class RayGenerator(nn.Module):
|
|
"""torch.nn Module for generating rays.
|
|
This class will store the intrinsics and extrinsics parameters of the cameras."""
|
|
|
|
def __init__(
|
|
self,
|
|
intrinsics: TensorType["num_cameras", "num_intrinsics_params"],
|
|
camera_to_world: TensorType["num_cameras", 3, 4],
|
|
) -> None:
|
|
"""_summary_
|
|
|
|
Args:
|
|
intrinsics (TensorType["num_cameras", "num_intrinsics_params"]):
|
|
The intrinsics parameters.
|
|
camera_to_world (TensorType["num_cameras", 3, 4]): Camera to world transformation matrix.
|
|
"""
|
|
super().__init__()
|
|
self.num_cameras, self.num_intrinsics_params = intrinsics.shape
|
|
assert self.num_cameras >= 0
|
|
self.intrinsics = nn.Parameter(intrinsics, requires_grad=False)
|
|
self.camera_to_world = nn.Parameter(camera_to_world, requires_grad=False)
|
|
# TODO(ethan): add learnable parameters that are deltas on the intrinsics and camera_to_world parameters
|
|
|
|
def forward(self, ray_indices: TensorType["num_rays", 3]) -> RayBundle:
|
|
"""Index into the cameras to generate the rays.
|
|
|
|
Args:
|
|
x (_type_): _description_
|
|
"""
|
|
c = ray_indices[:, 0] # camera indices
|
|
y = ray_indices[:, 1] # row indices
|
|
x = ray_indices[:, 2] # col indices
|
|
intrinsics = self.intrinsics[c]
|
|
camera_to_world = self.camera_to_world[c]
|
|
# NOTE(ethan): we currently assume all images have the same height and width
|
|
camera_index = 0
|
|
camera_class = get_camera_model(self.num_intrinsics_params)
|
|
camera = camera_class(*self.intrinsics[camera_index].tolist())
|
|
image_coords = camera.get_image_coords().to(ray_indices.device) # NOTE(ethan): I can actually avoid this step
|
|
coords = image_coords[y, x]
|
|
|
|
ray_bundle = camera_class.generate_rays(intrinsics=intrinsics, camera_to_world=camera_to_world, coords=coords)
|
|
ray_bundle.camera_indices = c
|
|
return ray_bundle
|