4
0
mirror of https://github.com/nerfstudio-project/nerfstudio.git synced 2026-11-11 10:34:29 +08:00
nerfstudio/mattport/structures/rays.py
Ethan Weber e38040ee4d
Image dataset implemented and ray generation working. (#23)
* working data loader code

* adding gitignore

* fixing linting errors

* set black line length in settings file

* update

* fixed filepath

* black format

* tqdm
2022-04-17 09:26:44 -07:00

68 lines
1.6 KiB
Python

"""
Some ray datastructures.
"""
import random
from dataclasses import dataclass
from typing import Optional
from torchtyping import TensorType
@dataclass
class RayBundle:
"""_summary_
Returns:
_type_: _description_
"""
origins: TensorType["num_rays", 3]
directions: TensorType["num_rays", 3]
camera_indices: Optional[TensorType["num_rays"]] = None
def to_camera_ray_bundle(self, image_height, image_width) -> "CameraRayBundle":
"""_summary_
Args:
image_height (_type_): _description_
image_width (_type_): _description_
Returns:
CameraRayBundle: _description_
"""
camera_ray_bundle = CameraRayBundle(
origins=self.origins.view(image_height, image_width, 3),
directions=self.directions.view(image_height, image_width, 3),
)
return camera_ray_bundle
def __len__(self):
num_rays = self.origins.shape[0]
return num_rays
def sample(self, num_rays: int):
"""Returns a RayBundle as a subset of rays.
Args:
num_rays (int):
Returns:
RayBundle: _description_
"""
assert num_rays <= len(self)
indices = random.sample(range(len(self)), k=num_rays)
return RayBundle(
origins=self.origins[indices],
directions=self.directions[indices],
camera_indices=self.camera_indices[indices],
)
@dataclass
class CameraRayBundle:
"""_summary_"""
origins: TensorType["image_height", "image_width", 3]
directions: TensorType["image_height", "image_width", 3]
camera_index: int = None