Switch from point samples to frustum samples

This commit is contained in:
Matt Tancik authored and Matthew Tancik committed 2022-06-06 16:43:11 -07:00
1 parent 609bf32d22
commit 1b20028ea3
17 files changed
+255 -102

No files matched your search

+58 -31
View File
@@ -13,12 +13,40 @@ from pyrad.utils.misc import is_not_none
@dataclass
class Frustums:
"""Describes region of space as a frustum.
Args:
origins (TensorType[..., 3]): xyz coordinate for ray origin.
directions (TensorType[..., 3]): Direction of ray.
frustum_starts (TensorType[..., 1]): Where the frustum starts along a ray.
frustum_ends (TensorType[..., 3]): Where the frustim ends along a ray.
pixel_area (TensorType[..., 1]): Projected area of pixel a distance 1 away from origin.
"""
origins: TensorType[..., 3]
directions: TensorType[..., 3]
frustum_starts: TensorType[..., 1]
frustum_ends: TensorType[..., 1]
radius: TensorType[..., 1]
pixel_area: TensorType[..., 1]
def get_positions(self) -> TensorType[..., 3]:
"""Returns "center" position of frustum. Not weighted by mass."""
return self.origins + self.directions * (self.frustum_starts + self.frustum_ends) / 2
@classmethod
def get_mock_frustum(cls) -> "Frustums":
"""Helper function to generate a placeholder frustum.
Returns:
Frustums: A size 1 frustum with meaningless values.
"""
return Frustums(
origins=torch.ones((1, 3)),
directions=torch.ones((1, 3)),
frustum_starts=torch.ones((1, 1)),
frustum_ends=torch.ones((1, 1)) + 1,
pixel_area=torch.ones((1, 1)),
)
def apply_masks(self, mask: TensorType) -> "Frustums":
"""Use valid_mask to mask samples.
@@ -34,7 +62,7 @@ class Frustums:
directions=self.directions[mask],
frustum_starts=self.frustum_starts[mask],
frustum_ends=self.frustum_ends[mask],
radius=self.radius[mask],
pixel_area=self.pixel_area[mask],
)
@@ -43,10 +71,10 @@ class PointSamples:
"""Samples in space.
Args:
positions (TensorType[..., 3]): XYZ locations
directions (TensorType[..., 3]): Unit direction vector
camera_indices (TensorType[..., 1]): Camera index
valid_mask (TensorType[...]): Rays that are valid
frustims (Frustums): Frustums along ray.
directions (TensorType[..., 3]): Unit direction vector.
camera_indices (TensorType[..., 1]): Camera index.
valid_mask (TensorType[...]): Rays that are valid.
"""
frustums: Frustums
@@ -79,18 +107,17 @@ class RaySamples:
"""Samples along a ray
Args:
positions (TensorType[..., 3]): XYZ locations
directions (TensorType[..., 3]): Unit direction vector
camera_indices (TensorType[..., 1]): Camera index
valid_mask (TensorType[...]): Rays that are valid
ts (TensorType[..., 1]): "time steps", distances along ray
deltas )TensorType[..., 1]): "width" of each sample
frustums (Frustums): Frustums along ray.
camera_indices (TensorType[..., 1]): Camera index.
valid_mask (TensorType[...]): Rays that are valid.
bins (TensorType[..., 1]): frustum bins along ray.
deltas )TensorType[..., 1]): "width" of each sample.
"""
frustums: TensorType[..., 3]
camera_indices: TensorType[..., 1] = None
valid_mask: TensorType[...] = None
ts: TensorType[..., 1] = None
bins: TensorType[..., 1] = None
deltas: TensorType[..., 1] = None
def to_point_samples(self) -> PointSamples:
@@ -144,6 +171,7 @@ class RayBundle:
Args:
origins (TensorType[..., 3]): Ray origins (XYZ)
directions (TensorType[..., 3]): Unit ray direction vector
pixel_area (TensorType[..., 1]): Projected area of pixel a distance 1 away from origin.
camera_indices (TensorType[..., 1]): Camera indices
nears (TensorType[..., 1]): Distance along ray to start sampling
fars (TensorType[..., 1]): Rays Distance along ray to stop sampling
@@ -152,7 +180,7 @@ class RayBundle:
origins: TensorType["num_rays", 3]
directions: TensorType["num_rays", 3]
radius: TensorType["num_rays", 1]
pixel_area: TensorType["num_rays", 1]
camera_indices: Optional[TensorType["num_rays", 1]] = None
nears: Optional[TensorType["num_rays"]] = None
fars: Optional[TensorType["num_rays"]] = None
@@ -195,7 +223,7 @@ class RayBundle:
return RayBundle(
origins=self.origins[indices],
directions=self.directions[indices],
radius=self.radius[indices],
pixel_area=self.pixel_area[indices],
camera_indices=self.camera_indices[indices],
)
@@ -211,7 +239,7 @@ class RayBundle:
return RayBundle(
origins=self.origins[valid_mask],
directions=self.directions[valid_mask],
radius=self.radius[valid_mask],
pixel_area=self.pixel_area[valid_mask],
camera_indices=self.camera_indices[valid_mask] if is_not_none(self.camera_indices) else None,
nears=self.nears[valid_mask] if is_not_none(self.nears) else None,
fars=self.fars[valid_mask] if is_not_none(self.fars) else None,
@@ -235,46 +263,45 @@ class RayBundle:
return RayBundle(
origins=self.origins.view(-1, 3)[start_idx:end_idx],
directions=self.directions.view(-1, 3)[start_idx:end_idx],
radius=self.radius.view(-1, 1)[start_idx:end_idx],
pixel_area=self.pixel_area.view(-1, 1)[start_idx:end_idx],
camera_indices=camera_indices,
)
def get_ray_samples(self, ts: TensorType[..., "num_samples"]) -> RaySamples:
def get_ray_samples(self, bins: TensorType["num_rays", "num_samples+1"]) -> RaySamples:
"""Produces samples for each ray by projection points along the ray direction.
Args:
ts (TensorType[..., "num_samples"]): Distance from origin of sample points.
bins (TensorType["num_rays", "num_samples+1"]): Distance from origin of sample points.
Returns:
RaySamples: Samples projected along ray.
"""
positions = self.origins[:, None] + ts[:, :, None] * self.directions[:, None]
num_samples = bins.shape[-1] - 1
valid_mask = torch.ones_like(ts, dtype=torch.bool)
valid_mask = torch.ones((bins.shape[0], num_samples), dtype=torch.bool, device=bins.device)
dists = ts[..., 1:] - ts[..., :-1]
dists = torch.cat([dists, dists[..., -1:]], -1) # [N_rays, N_samples]
deltas = dists * torch.norm(self.directions[..., None, :], dim=-1)
dists = bins[:, 1:] - bins[:, :-1] # [N_rays, N_samples]
deltas = dists * torch.norm(self.directions[:, None, :], dim=-1)
if is_not_none(self.camera_indices):
camera_indices = self.camera_indices.unsqueeze(1).repeat(1, positions.shape[1])
camera_indices = self.camera_indices.unsqueeze(1).repeat(1, num_samples)
else:
camera_indices = None
frustums = Frustums(
origins=self.origins,
directions=self.directions.unsqueeze(1).repeat(1, positions.shape[1], 1),
frustum_starts=ts[:, :-1, None],
frustum_ends=ts[:, :-1, None],
radius=self.radius,
origins=self.origins.unsqueeze(1).repeat(1, num_samples, 1),
directions=self.directions.unsqueeze(1).repeat(1, num_samples, 1),
frustum_starts=bins[:, :-1, None],
frustum_ends=bins[:, 1:, None],
pixel_area=self.pixel_area.unsqueeze(1).repeat(1, num_samples, 1),
)
ray_samples = RaySamples(
frustums=frustums,
camera_indices=camera_indices,
valid_mask=valid_mask,
ts=ts,
bins=bins,
deltas=deltas,
)