mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-26 10:59:53 +08:00
Switch from point samples to frustum samples
This commit is contained in:
1 parent
609bf32d22
commit
1b20028ea3
17 files changed
+255
-102
No files matched your search
+57
-20
@@ -83,15 +83,15 @@ class Camera:
|
||||
@abstractmethod
|
||||
def generate_rays(
|
||||
cls,
|
||||
intrinsics: TensorType["num_rays", "num_intrinsics_params"],
|
||||
camera_to_world: TensorType["num_rays", 3, 4],
|
||||
coords: TensorType["image_height", "image_width", 2],
|
||||
intrinsics: TensorType[..., "num_intrinsics_params"],
|
||||
camera_to_world: TensorType[..., 3, 4],
|
||||
coords: TensorType[..., 2],
|
||||
) -> RayBundle:
|
||||
"""
|
||||
Args:
|
||||
intrinsics (TensorType["num_rays", "num_intrinsics_params"]): Camera intrinsics
|
||||
camera_to_world (TensorType["num_rays", 3, 4]): Camera to world transformation matrix
|
||||
coords (TensorType["image_height", "image_width", 2]): Image grid coordinates
|
||||
intrinsics (TensorType[..., "num_intrinsics_params"]): Camera intrinsics
|
||||
camera_to_world (TensorType[..., 3, 4]): Camera to world transformation matrix
|
||||
coords (TensorType[..., 2]): Image grid coordinates
|
||||
|
||||
Returns:
|
||||
RayBundle: A bundle of rays for each grid coordinate.
|
||||
@@ -193,7 +193,17 @@ class PinholeCamera(Camera):
|
||||
) # (..., 1, 3) * (..., 3, 3) -> (..., 3)
|
||||
directions = normalize(directions, dim=-1)
|
||||
origins = camera_to_world[..., :3, 3] # (..., 3)
|
||||
return RayBundle(origins=origins, directions=directions)
|
||||
|
||||
## Calculate pixel area directly
|
||||
dirx_min = normalize(torch.cat([(x - cx - 0.5) / fx, -(y - cy) / fy, -torch.ones_like(x)], -1), dim=-1)
|
||||
dirx_max = normalize(torch.cat([(x - cx + 0.5) / fx, -(y - cy) / fy, -torch.ones_like(x)], -1), dim=-1)
|
||||
diry_min = normalize(torch.cat([(x - cx) / fx, -(y - cy - 0.5) / fy, -torch.ones_like(x)], -1), dim=-1)
|
||||
diry_max = normalize(torch.cat([(x - cx) / fx, -(y - cy + 0.5) / fy, -torch.ones_like(x)], -1), dim=-1)
|
||||
dx = torch.sqrt(torch.sum((dirx_max - dirx_min) ** 2, dim=-1))
|
||||
dy = torch.sqrt(torch.sum((diry_max - diry_min) ** 2, dim=-1))
|
||||
pixel_area = dx * dy
|
||||
|
||||
return RayBundle(origins=origins, directions=directions, pixel_area=pixel_area[..., None])
|
||||
|
||||
|
||||
class SimplePinholeCamera(PinholeCamera):
|
||||
@@ -234,25 +244,52 @@ class EquirectangularCamera(Camera):
|
||||
@classmethod
|
||||
def generate_rays(
|
||||
cls,
|
||||
intrinsics: TensorType["num_rays", 2],
|
||||
camera_to_world: TensorType["num_rays", 3, 4],
|
||||
coords: TensorType["num_rays", 2],
|
||||
intrinsics: TensorType[..., 2],
|
||||
camera_to_world: TensorType[..., 3, 4],
|
||||
coords: TensorType[..., 2],
|
||||
) -> RayBundle:
|
||||
|
||||
phi = coords[:, 0:1] # (num_rays, 1)
|
||||
theta = coords[:, 1:2] # (num_rays, 1)
|
||||
y = coords[..., 0:1] # (num_rays, 1)
|
||||
x = coords[..., 1:2] # (num_rays, 1)
|
||||
|
||||
height = intrinsics[:, 0:1]
|
||||
width = intrinsics[:, 1:2]
|
||||
height = intrinsics[..., 0:1]
|
||||
width = intrinsics[..., 1:2]
|
||||
|
||||
phi = phi / height * torch.pi
|
||||
theta = -theta / width * 2 * torch.pi
|
||||
phi = y / height * torch.pi
|
||||
theta = -x / width * 2 * torch.pi
|
||||
|
||||
directions = torch.stack(
|
||||
[torch.cos(theta) * torch.sin(phi), torch.sin(theta) * torch.sin(phi), torch.cos(phi)], dim=-1
|
||||
d_phi_min = (y + 0.5) / height * torch.pi
|
||||
d_phi_max = (y - 0.5) / height * torch.pi
|
||||
d_theta_min = -(x - 0.5) / width * 2 * torch.pi
|
||||
d_theta_max = -(x + 0.5) / width * 2 * torch.pi
|
||||
|
||||
s_phi = torch.sin(phi)
|
||||
c_phi = torch.cos(phi)
|
||||
s_theta = torch.sin(theta)
|
||||
c_theta = torch.cos(theta)
|
||||
|
||||
directions = torch.cat([c_theta * s_phi, s_theta * s_phi, c_phi], dim=-1)
|
||||
origins = camera_to_world[..., :3, 3] # (num_rays, 3)
|
||||
|
||||
## Calculate area directly
|
||||
dirx_min = normalize(
|
||||
torch.cat([torch.cos(d_theta_min) * s_phi, torch.sin(d_theta_min) * s_phi, c_phi], -1), dim=-1
|
||||
)
|
||||
origins = camera_to_world[:, :3, 3] # (num_rays, 3)
|
||||
return RayBundle(origins=origins, directions=directions)
|
||||
dirx_max = normalize(
|
||||
torch.cat([torch.cos(d_theta_max) * s_phi, torch.sin(d_theta_max) * s_phi, c_phi], -1), dim=-1
|
||||
)
|
||||
diry_min = normalize(
|
||||
torch.cat([c_theta * torch.sin(d_phi_min), s_theta * torch.sin(d_phi_min), torch.cos(d_phi_min)], -1),
|
||||
dim=-1,
|
||||
)
|
||||
diry_max = normalize(
|
||||
torch.cat([c_theta * torch.sin(d_phi_max), s_theta * torch.sin(d_phi_max), torch.cos(d_phi_max)], -1),
|
||||
dim=-1,
|
||||
)
|
||||
dx = torch.sqrt(torch.sum((dirx_max - dirx_min) ** 2, dim=-1))
|
||||
dy = torch.sqrt(torch.sum((diry_max - diry_min) ** 2, dim=-1))
|
||||
pixel_area = dx * dy
|
||||
return RayBundle(origins=origins, directions=directions, pixel_area=pixel_area)
|
||||
|
||||
|
||||
def get_camera_model(num_intrinsics_params: int) -> Type[Camera]:
|
||||
|
||||
+58
-31
@@ -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,
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user