mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-14 11:54:01 +08:00
57 lines
2.2 KiB
Python
57 lines
2.2 KiB
Python
"""
|
|
Ray generator.
|
|
"""
|
|
from torch import nn
|
|
from torchtyping import TensorType
|
|
from pyrad.structures.rays import RayBundle
|
|
from pyrad.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
|
|
|
|
# NOTE(ethan): we currently assume all images have the same height and width
|
|
camera_index = 0
|
|
self.camera_class = get_camera_model(self.num_intrinsics_params)
|
|
camera = self.camera_class(*self.intrinsics[camera_index].tolist())
|
|
self.image_coords = nn.Parameter(camera.get_image_coords(), requires_grad=False)
|
|
|
|
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]
|
|
coords = self.image_coords[y, x]
|
|
|
|
ray_bundle = self.camera_class.generate_rays(
|
|
intrinsics=intrinsics, camera_to_world=camera_to_world, coords=coords
|
|
)
|
|
ray_bundle.camera_indices = c
|
|
return ray_bundle
|