Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flesh out viewer beta #2086

Merged
merged 82 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
b95daad
Increase max_res of first proposal network
kerrj Jan 24, 2023
bf0f2e5
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Jan 27, 2023
d2698e0
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Jan 28, 2023
fdd3c72
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Feb 1, 2023
85491fa
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Feb 9, 2023
bae8e5c
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Feb 16, 2023
bb02ae0
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Feb 22, 2023
03efc6a
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Mar 14, 2023
6f89617
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Mar 18, 2023
03a4a45
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Mar 23, 2023
4a63d6b
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Mar 27, 2023
a886dc4
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Apr 2, 2023
ea2378e
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Apr 4, 2023
1b1bb22
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Apr 5, 2023
06825f4
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Apr 14, 2023
4b040a1
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Apr 16, 2023
0f56cc6
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Apr 17, 2023
134ff3e
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Apr 21, 2023
4ac02b3
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Apr 22, 2023
b421b2e
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Apr 22, 2023
1ebfaed
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Apr 24, 2023
9b86877
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Apr 24, 2023
a259f93
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj May 18, 2023
7e32c8c
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj May 19, 2023
9db295a
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj May 19, 2023
86135cf
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj May 22, 2023
04f7af5
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj May 25, 2023
09c24de
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj May 26, 2023
c06e2b5
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Jun 11, 2023
4697122
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Jun 15, 2023
d67e176
Merge branch 'main' of https://github.com/nerfstudio-project/nerfstudio
kerrj Jun 16, 2023
9db9777
fix render statemachine logic for viewer beta
kerrj Jun 16, 2023
efd1826
clicking on cameras in the viewer
kerrj Jun 16, 2023
cb66e31
fix fov calculation, update camera positions while training
kerrj Jun 18, 2023
0a9eb8a
cleanup
kerrj Jun 18, 2023
6c23c65
backup
brentyi Aug 8, 2023
ce6ac6c
fix configure theme
terrancewang Aug 9, 2023
ef0d2c3
Merge branch 'main' of github.com:nerfstudio-project/nerfstudio into …
brentyi Aug 9, 2023
e0768c1
Fix theme configure for latest dev version of viser
brentyi Aug 9, 2023
19f3cf2
Merge branch 'justin/camera_clicking_beta' of github.com:nerfstudio-p…
brentyi Aug 9, 2023
99ba4ee
Fix runtime errors
brentyi Aug 9, 2023
d86aa8c
add set up direction button
terrancewang Aug 9, 2023
a1a6819
add pause resume button
terrancewang Aug 9, 2023
7da09ad
Export panel
brentyi Aug 10, 2023
31d6600
Render tab skeleton
brentyi Aug 10, 2023
92cc3e5
make responsiveness more snappy by stopping training
kerrj Aug 11, 2023
06b6d63
depth composite
kerrj Aug 13, 2023
00a93f5
start implementing viewercontrol in new viewer
kerrj Aug 23, 2023
8563499
refactor background img to use depth and add depth layering option
kerrj Aug 24, 2023
a994a58
show/hide images
kerrj Aug 25, 2023
d68e5e9
speed up rendering
kerrj Aug 26, 2023
17e9744
fix crop color background
kerrj Aug 26, 2023
c0e2c9b
crop
kerrj Aug 26, 2023
4edfff1
merge main, crop color fix, improvements to render statemachine
kerrj Aug 26, 2023
c186b3b
rotated crops
kerrj Aug 27, 2023
346d7fd
rotated crops
kerrj Aug 27, 2023
6f1351b
transform handle on crop box
kerrj Aug 28, 2023
9ccb063
fix xyzw for crop
kerrj Aug 28, 2023
3b67fac
oriented crop export for pointclouds, not for meshes yet
kerrj Aug 31, 2023
8970e4c
Initial render panel
brentyi Aug 31, 2023
442e76a
Fix export button
brentyi Aug 31, 2023
3d82471
merge
kerrj Aug 31, 2023
556b7e8
Theme tweaks
brentyi Sep 2, 2023
1aa77dd
Merge branch 'justin/camera_clicking_beta' of github.com:nerfstudio-p…
brentyi Sep 2, 2023
676e79c
fix background issue in cropping
kerrj Sep 3, 2023
453cf33
export rotated poisson meshe
kerrj Sep 3, 2023
8dced54
double normals definition in export panel
kerrj Sep 3, 2023
1ab6813
Visual nits
brentyi Sep 3, 2023
bf14936
remove tsdf from export panel
kerrj Sep 5, 2023
4564549
Merge branch 'main' into justin/camera_clicking_beta
brentyi Sep 5, 2023
62eb8d7
Add licenses
brentyi Sep 7, 2023
ac4963f
Fix some CI errors
brentyi Sep 7, 2023
de890a3
Merge branch 'main' of github.com:nerfstudio-project/nerfstudio into …
brentyi Sep 7, 2023
069e6d4
Type fixes
brentyi Sep 7, 2023
47b7e86
Fix tests
brentyi Sep 7, 2023
72a58d2
Fix numpy broken
brentyi Sep 7, 2023
f30bd12
various bugfix
kerrj Sep 7, 2023
2ba083f
Merge branch 'justin/camera_clicking_beta' of https://github.com/nerf…
kerrj Sep 7, 2023
7d940b1
Bump viser
brentyi Sep 7, 2023
087eb91
Run black
brentyi Sep 7, 2023
8a9f30b
Fix pyright
brentyi Sep 8, 2023
1a3a1bc
Merge branch 'main' into justin/camera_clicking_beta
brentyi Sep 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
"typescript.suggestionActions.enabled": false,
"javascript.suggestionActions.enabled": false,
"[python]": {
"editor.defaultFormatter": "ms-python.python",
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.codeActionsOnSave": {
"source.organizeImports": true,
"source.fixAll": true
}
},
"editor.formatOnSave": true,
"python.envFile": "${workspaceFolder}/.env",
"python.formatting.provider": "black",
"python.formatting.provider": "none",
"python.formatting.blackArgs": ["--line-length=120"],
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": false,
Expand Down
19 changes: 11 additions & 8 deletions nerfstudio/cameras/cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import nerfstudio.utils.poses as pose_utils
from nerfstudio.cameras import camera_utils
from nerfstudio.cameras.rays import RayBundle
from nerfstudio.data.scene_box import SceneBox
from nerfstudio.data.scene_box import SceneBox, OrientedBox
from nerfstudio.utils.tensor_dataclass import TensorDataclass

TORCH_DEVICE = Union[torch.device, str]
Expand Down Expand Up @@ -323,6 +323,7 @@ def generate_rays(
keep_shape: Optional[bool] = None,
disable_distortion: bool = False,
aabb_box: Optional[SceneBox] = None,
obb_box: Optional[OrientedBox] = None,
) -> RayBundle:
"""Generates rays for the given camera indices.

Expand Down Expand Up @@ -465,20 +466,24 @@ def generate_rays(
if keep_shape is False:
raybundle = raybundle.flatten()

if aabb_box:
if aabb_box is not None or obb_box is not None:
with torch.no_grad():
tensor_aabb = Parameter(aabb_box.aabb.flatten(), requires_grad=False)

rays_o = raybundle.origins.contiguous()
rays_d = raybundle.directions.contiguous()

tensor_aabb = tensor_aabb.to(rays_o.device)
shape = rays_o.shape

rays_o = rays_o.reshape((-1, 3))
rays_d = rays_d.reshape((-1, 3))

t_min, t_max = nerfstudio.utils.math.intersect_aabb(rays_o, rays_d, tensor_aabb)
if aabb_box is not None:
tensor_aabb = Parameter(aabb_box.aabb.flatten(), requires_grad=False)
tensor_aabb = tensor_aabb.to(rays_o.device)
t_min, t_max = nerfstudio.utils.math.intersect_aabb(rays_o, rays_d, tensor_aabb)
elif obb_box is not None:
t_min, t_max = nerfstudio.utils.math.intersect_obb(rays_o, rays_d, obb_box)
else:
assert False

t_min = t_min.reshape([shape[0], shape[1], 1])
t_max = t_max.reshape([shape[0], shape[1], 1])
Expand Down Expand Up @@ -747,8 +752,6 @@ def _compute_rays_for_vr180(

rotation = c2w[..., :3, :3]

-torch.pi * ((x - cx) / fx)[0]

# interocular axis of the VR180 camera
vr180_x_axis = torch.tensor([1, 0, 0], device=c2w.device)

Expand Down
38 changes: 37 additions & 1 deletion nerfstudio/data/scene_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"""

from dataclasses import dataclass
from typing import Union
from typing import Union, Tuple
import viser.transforms as vtf

import torch
from jaxtyping import Float
Expand Down Expand Up @@ -76,3 +77,38 @@ def from_camera_poses(poses: Float[Tensor, "*batch 3 4"], scale_factor: float) -
xyzs = poses[..., :3, -1]
aabb = torch.stack([torch.min(xyzs, dim=0)[0], torch.max(xyzs, dim=0)[0]])
return SceneBox(aabb=aabb * scale_factor)


@dataclass
class OrientedBox:
R: Float[Tensor, "3 3"]
"""R: rotation matrix."""
T: Float[Tensor, "3"]
"""T: translation vector."""
S: Float[Tensor, "3"]
"""S: scale vector."""

def within(self, pts: Float[Tensor, "n 3"]):
"""Returns a boolean mask indicating whether each point is within the box."""
R, T, S = self.R, self.T, self.S.to(pts)
H = torch.eye(4, device=pts.device, dtype=pts.dtype)
H[:3, :3] = R
H[:3, 3] = T
H_world2bbox = torch.inverse(H)
pts = torch.cat((pts, torch.ones_like(pts[..., :1])), dim=-1)
pts = torch.matmul(H_world2bbox, pts.T).T[..., :3]

comp_l = torch.tensor(-S / 2)
comp_m = torch.tensor(S / 2)
mask = torch.all(torch.concat([pts > comp_l, pts < comp_m], dim=-1), dim=-1)
return mask

@staticmethod
def from_params(
pos: Tuple[float, float, float], rpy: Tuple[float, float, float], scale: Tuple[float, float, float]
):
"""Construct a box from position, rotation, and scale parameters."""
R = torch.tensor(vtf.SO3.from_rpy_radians(rpy[0], rpy[1], rpy[2]).as_matrix())
T = torch.tensor(pos)
S = torch.tensor(scale)
return OrientedBox(R=R, T=T, S=S)
24 changes: 15 additions & 9 deletions nerfstudio/exporter/exporter_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

from nerfstudio.cameras.cameras import Cameras
from nerfstudio.data.datasets.base_dataset import InputDataset
from nerfstudio.data.scene_box import OrientedBox
from nerfstudio.pipelines.base_pipeline import Pipeline, VanillaPipeline
from nerfstudio.utils.rich_utils import CONSOLE, ItersPerSecColumn

Expand Down Expand Up @@ -84,8 +85,9 @@ def generate_point_cloud(
depth_output_name: str = "depth",
normal_output_name: Optional[str] = None,
use_bounding_box: bool = True,
bounding_box_min: Tuple[float, float, float] = (-1.0, -1.0, -1.0),
bounding_box_max: Tuple[float, float, float] = (1.0, 1.0, 1.0),
bounding_box_min: Optional[Tuple[float, float, float]] = None,
bounding_box_max: Optional[Tuple[float, float, float]] = None,
crop_obb: Optional[OrientedBox] = None,
std_ratio: float = 10.0,
) -> o3d.geometry.PointCloud:
"""Generate a point cloud from a nerf.
Expand Down Expand Up @@ -119,7 +121,8 @@ def generate_point_cloud(
rgbs = []
normals = []
view_directions = []

if use_bounding_box and (crop_obb is not None and bounding_box_max is not None):
CONSOLE.print("Provided aabb and crop_obb at the same time, using only the obb", style="bold yellow")
with progress as progress_bar:
task = progress_bar.add_task("Generating Point Cloud", total=num_points)
while not progress_bar.finished:
Expand Down Expand Up @@ -163,12 +166,15 @@ def generate_point_cloud(
normal = normal[mask]

if use_bounding_box:
comp_l = torch.tensor(bounding_box_min, device=point.device)
comp_m = torch.tensor(bounding_box_max, device=point.device)
assert torch.all(
comp_l < comp_m
), f"Bounding box min {bounding_box_min} must be smaller than max {bounding_box_max}"
mask = torch.all(torch.concat([point > comp_l, point < comp_m], dim=-1), dim=-1)
if crop_obb is None:
comp_l = torch.tensor(bounding_box_min, device=point.device)
comp_m = torch.tensor(bounding_box_max, device=point.device)
assert torch.all(
comp_l < comp_m
), f"Bounding box min {bounding_box_min} must be smaller than max {bounding_box_max}"
mask = torch.all(torch.concat([point > comp_l, point < comp_m], dim=-1), dim=-1)
else:
mask = crop_obb.within(point)
point = point[mask]
rgb = rgb[mask]
view_direction = view_direction[mask]
Expand Down
1 change: 1 addition & 0 deletions nerfstudio/model_components/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ def forward(
Returns:
Outputs of rgb values.
"""

if background_color is None:
background_color = self.background_color

Expand Down
28 changes: 25 additions & 3 deletions nerfstudio/scripts/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

from nerfstudio.cameras.rays import RayBundle
from nerfstudio.data.datamanagers.base_datamanager import VanillaDataManager
from nerfstudio.data.scene_box import OrientedBox
from nerfstudio.exporter import texture_utils, tsdf_utils
from nerfstudio.exporter.exporter_utils import (
collect_camera_poses,
Expand Down Expand Up @@ -109,10 +110,17 @@ class ExportPointCloud(Exporter):
"""Name of the RGB output."""
use_bounding_box: bool = True
"""Only query points within the bounding box"""
bounding_box_min: Tuple[float, float, float] = (-1, -1, -1)
bounding_box_min: Optional[Tuple[float, float, float]] = (-1, -1, -1)
"""Minimum of the bounding box, used if use_bounding_box is True."""
bounding_box_max: Tuple[float, float, float] = (1, 1, 1)
bounding_box_max: Optional[Tuple[float, float, float]] = (1, 1, 1)
"""Maximum of the bounding box, used if use_bounding_box is True."""

obb_center: Optional[Tuple[float, float, float]] = None
"""Center of the oriented bounding box."""
obb_rotation: Optional[Tuple[float, float, float]] = None
"""Rotation of the oriented bounding box. Expressed as RPY Euler angles in radians"""
obb_scale: Optional[Tuple[float, float, float]] = None
"""Scale of the oriented bounding box along each axis."""
num_rays_per_batch: int = 32768
"""Number of rays to evaluate per batch. Decrease if you run out of memory."""
std_ratio: float = 10.0
Expand All @@ -135,7 +143,9 @@ def main(self) -> None:

# Whether the normals should be estimated based on the point cloud.
estimate_normals = self.normal_method == "open3d"

crop_obb = None
if self.obb_center is not None and self.obb_rotation is not None and self.obb_scale is not None:
crop_obb = OrientedBox.from_params(self.obb_center, self.obb_rotation, self.obb_scale)
pcd = generate_point_cloud(
pipeline=pipeline,
num_points=self.num_points,
Expand All @@ -148,6 +158,7 @@ def main(self) -> None:
use_bounding_box=self.use_bounding_box,
bounding_box_min=self.bounding_box_min,
bounding_box_max=self.bounding_box_max,
crop_obb=crop_obb,
std_ratio=self.std_ratio,
)
torch.cuda.empty_cache()
Expand Down Expand Up @@ -264,6 +275,12 @@ class ExportPoissonMesh(Exporter):
"""Minimum of the bounding box, used if use_bounding_box is True."""
bounding_box_max: Tuple[float, float, float] = (1, 1, 1)
"""Minimum of the bounding box, used if use_bounding_box is True."""
obb_center: Optional[Tuple[float, float, float]] = None
"""Center of the oriented bounding box."""
obb_rotation: Optional[Tuple[float, float, float]] = None
"""Rotation of the oriented bounding box. Expressed as RPY Euler angles in radians"""
obb_scale: Optional[Tuple[float, float, float]] = None
"""Scale of the oriented bounding box along each axis."""
num_rays_per_batch: int = 32768
"""Number of rays to evaluate per batch. Decrease if you run out of memory."""
texture_method: Literal["point_cloud", "nerf"] = "nerf"
Expand Down Expand Up @@ -296,6 +313,10 @@ def main(self) -> None:

# Whether the normals should be estimated based on the point cloud.
estimate_normals = self.normal_method == "open3d"
if self.obb_center is not None and self.obb_rotation is not None and self.obb_scale is not None:
crop_obb = OrientedBox.from_params(self.obb_center, self.obb_rotation, self.obb_scale)
else:
crop_obb = None

pcd = generate_point_cloud(
pipeline=pipeline,
Expand All @@ -309,6 +330,7 @@ def main(self) -> None:
use_bounding_box=self.use_bounding_box,
bounding_box_min=self.bounding_box_min,
bounding_box_max=self.bounding_box_max,
crop_obb=crop_obb,
std_ratio=self.std_ratio,
)
torch.cuda.empty_cache()
Expand Down
40 changes: 38 additions & 2 deletions nerfstudio/utils/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from jaxtyping import Bool, Float
from torch import Tensor

from nerfstudio.utils.misc import torch_compile
from nerfstudio.data.scene_box import OrientedBox


def components_from_spherical_harmonics(
Expand Down Expand Up @@ -199,7 +199,7 @@ def expected_sin(x_means: torch.Tensor, x_vars: torch.Tensor) -> torch.Tensor:
return torch.exp(-0.5 * x_vars) * torch.sin(x_means)


@torch_compile(dynamic=True, mode="reduce-overhead", backend="eager")
# @torch_compile(dynamic=True, mode="reduce-overhead", backend="eager")
def intersect_aabb(
origins: torch.Tensor,
directions: torch.Tensor,
Expand Down Expand Up @@ -240,6 +240,42 @@ def intersect_aabb(
return t_min, t_max


def intersect_obb(
origins: torch.Tensor,
directions: torch.Tensor,
obb: OrientedBox,
max_bound: float = 1e10,
invalid_value: float = 1e10,
):
"""
Ray intersection with an oriented bounding box (OBB)

Args:
origins: [N,3] tensor of 3d positions
directions: [N,3] tensor of normalized directions
R: [3,3] rotation matrix
T: [3] translation vector
S: [3] extents of the bounding box
max_bound: Maximum value of t_max
invalid_value: Value to return in case of no intersection
"""
# Transform ray to OBB space
R, T, S = obb.R, obb.T, obb.S.to(origins.device)
H = torch.eye(4, device=origins.device, dtype=origins.dtype)
H[:3, :3] = R
H[:3, 3] = T
H_world2bbox = torch.inverse(H)
origins = torch.cat((origins, torch.ones_like(origins[..., :1])), dim=-1)
origins = torch.matmul(H_world2bbox, origins.T).T[..., :3]
directions = torch.matmul(H_world2bbox[:3, :3], directions.T).T

# Compute intersection with axis-aligned bounding box with min as -S and max as +S
aabb = torch.concat((-S / 2, S / 2))
t_min, t_max = intersect_aabb(origins, directions, aabb, max_bound=max_bound, invalid_value=invalid_value)

return t_min, t_max


def safe_normalize(
vectors: Float[Tensor, "*batch_dim N"],
eps: float = 1e-10,
Expand Down
Loading