mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-25 10:29:51 +08:00
Mesh export (#809)
* start of mesh PR * point cloud export mvp * start of mesh PR * point cloud export mvp * Save out mesh. Poor quality atm * reorganized mesh algorithms and specified CLI * CLI reorg * tsdf algorithm in progress * debugging * tsdf functional but camera distortion params not in * playing with distortion params * TSDF works but texture looks bad * added trimesh * moved to exporter * removed files * saving progress * save texture image * seems to work w/o barycentric * barycentric start * gap between triangles * blocking out diagonal * new barcentric and scaling * meshing working. need to remove extra rows * fixed triangle packing * gitignore changes * removed package * fix Literal import * comments * update .mtl file * poisson mesh with model output normals & resolved some PR comments on git * starting docs. breaking up big function. todo: add xatlas * xatlas and nvdiffrast working * triangle_id * progress on expensive texture coordinates method * pytorch meshing works * removing nvdiffrast * remove trimesh * cleanup * few changes * pred normals by default and add minimum additions to viewer * fix cli in viewer * add xatlas as dependency * make point cloud more dense * reverted viewer changes * turn off normals by default * fix docs * finished todos on github * remove xatlas * xatlas * black Co-authored-by: Matt Tancik <tancik@berkeley.edu>
This commit is contained in:
1 parent
d9395f2576
commit
1052fccec2
15 files changed
+1526
-166
No files matched your search
@@ -1,128 +0,0 @@
|
||||
# Copyright 2022 The Nerfstudio Team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
"""Algorithms for exporting data."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from typing import Tuple
|
||||
|
||||
import open3d as o3d
|
||||
import torch
|
||||
from rich.console import Console
|
||||
from rich.progress import (
|
||||
BarColumn,
|
||||
Progress,
|
||||
TaskProgressColumn,
|
||||
TextColumn,
|
||||
TimeRemainingColumn,
|
||||
)
|
||||
|
||||
from nerfstudio.configs.base_config import Config # pylint: disable=unused-import
|
||||
from nerfstudio.pipelines.base_pipeline import Pipeline
|
||||
|
||||
CONSOLE = Console(width=120)
|
||||
|
||||
|
||||
def generate_point_cloud(
|
||||
pipeline: Pipeline,
|
||||
num_points: int = 1000000,
|
||||
remove_outliers: bool = True,
|
||||
estimate_normals: bool = False,
|
||||
rgb_output_name: str = "rgb",
|
||||
depth_output_name: str = "depth",
|
||||
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),
|
||||
) -> o3d.geometry.PointCloud:
|
||||
"""Generate a point cloud from a nerf.
|
||||
|
||||
Args:
|
||||
pipeline: Pipeline to evaluate with.
|
||||
num_points: Number of points to generate. May result in less if outlier removal is used.
|
||||
remove_outliers: Whether to remove outliers.
|
||||
estimate_normals: Whether to estimate normals.
|
||||
rgb_output_name: Name of the RGB output.
|
||||
depth_output_name: Name of the depth output.
|
||||
use_bounding_box: Whether to use a bounding box to sample points.
|
||||
bounding_box_min: Minimum of the bounding box.
|
||||
bounding_box_max: Maximum of the bounding box.
|
||||
|
||||
Returns:
|
||||
Point cloud.
|
||||
"""
|
||||
|
||||
progress = Progress(
|
||||
TextColumn(":cloud: Computing Point Cloud :cloud:"),
|
||||
BarColumn(),
|
||||
TaskProgressColumn(show_speed=True),
|
||||
TimeRemainingColumn(elapsed_when_finished=True, compact=True),
|
||||
)
|
||||
points = []
|
||||
rgbs = []
|
||||
with progress as progress_bar:
|
||||
task = progress_bar.add_task("Generating Point Cloud", total=num_points)
|
||||
while not progress_bar.finished:
|
||||
with torch.no_grad():
|
||||
ray_bundle, _ = pipeline.datamanager.next_train(0)
|
||||
outputs = pipeline.model(ray_bundle)
|
||||
if rgb_output_name not in outputs:
|
||||
CONSOLE.rule("Error", style="red")
|
||||
CONSOLE.print(f"Could not find {rgb_output_name} in the model outputs", justify="center")
|
||||
CONSOLE.print(f"Please set --rgb_output_name to one of: {outputs.keys()}", justify="center")
|
||||
sys.exit(1)
|
||||
if depth_output_name not in outputs:
|
||||
CONSOLE.rule("Error", style="red")
|
||||
CONSOLE.print(f"Could not find {depth_output_name} in the model outputs", justify="center")
|
||||
CONSOLE.print(f"Please set --depth_output_name to one of: {outputs.keys()}", justify="center")
|
||||
sys.exit(1)
|
||||
rgb = outputs[rgb_output_name]
|
||||
depth = outputs[depth_output_name]
|
||||
point = ray_bundle.origins + ray_bundle.directions * depth
|
||||
|
||||
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)
|
||||
point = point[mask]
|
||||
rgb = rgb[mask]
|
||||
|
||||
points.append(point)
|
||||
rgbs.append(rgb)
|
||||
progress.advance(task, point.shape[0])
|
||||
points = torch.cat(points, dim=0)
|
||||
rgbs = torch.cat(rgbs, dim=0)
|
||||
|
||||
pcd = o3d.geometry.PointCloud()
|
||||
pcd.points = o3d.utility.Vector3dVector(points.float().cpu().numpy())
|
||||
pcd.colors = o3d.utility.Vector3dVector(rgbs.float().cpu().numpy())
|
||||
|
||||
if remove_outliers:
|
||||
CONSOLE.print("Cleaning Point Cloud")
|
||||
pcd, _ = pcd.remove_statistical_outlier(nb_neighbors=20, std_ratio=1.0)
|
||||
print("\033[A\033[A")
|
||||
CONSOLE.print("[bold green]:white_check_mark: Cleaning Point Cloud")
|
||||
|
||||
if estimate_normals:
|
||||
CONSOLE.print("Estimating Point Cloud Normals")
|
||||
pcd.estimate_normals()
|
||||
print("\033[A\033[A")
|
||||
CONSOLE.print("[bold green]:white_check_mark: Estimating Point Cloud Normals")
|
||||
|
||||
return pcd
|
||||
@@ -26,19 +26,25 @@ from nerfstudio.cameras.rays import RayBundle
|
||||
from nerfstudio.data.scene_box import SceneBox
|
||||
|
||||
|
||||
class SceneBoxCollider(nn.Module):
|
||||
class SceneCollider(nn.Module):
|
||||
"""Module for setting near and far values for rays."""
|
||||
|
||||
def __init__(self, **kwargs) -> None:
|
||||
self.kwargs = kwargs
|
||||
super().__init__()
|
||||
|
||||
def forward(self, ray_bundle: RayBundle) -> RayBundle:
|
||||
def set_nears_and_fars(self, ray_bundle) -> RayBundle:
|
||||
"""To be implemented."""
|
||||
raise NotImplementedError
|
||||
|
||||
def forward(self, ray_bundle: RayBundle) -> RayBundle:
|
||||
"""Sets the nears and fars if they are not set already."""
|
||||
if ray_bundle.nears is not None and ray_bundle.fars is not None:
|
||||
return ray_bundle
|
||||
return self.set_nears_and_fars(ray_bundle)
|
||||
|
||||
class AABBBoxCollider(SceneBoxCollider):
|
||||
|
||||
class AABBBoxCollider(SceneCollider):
|
||||
"""Module for colliding rays with the scene box to compute near and far values.
|
||||
|
||||
Args:
|
||||
@@ -88,7 +94,7 @@ class AABBBoxCollider(SceneBoxCollider):
|
||||
|
||||
return nears, fars
|
||||
|
||||
def forward(self, ray_bundle: RayBundle) -> RayBundle:
|
||||
def set_nears_and_fars(self, ray_bundle: RayBundle) -> RayBundle:
|
||||
"""Intersects the rays with the scene box and updates the near and far values.
|
||||
Populates nears and fars fields and returns the ray_bundle.
|
||||
|
||||
@@ -102,7 +108,7 @@ class AABBBoxCollider(SceneBoxCollider):
|
||||
return ray_bundle
|
||||
|
||||
|
||||
class NearFarCollider(SceneBoxCollider):
|
||||
class NearFarCollider(SceneCollider):
|
||||
"""Sets the nears and fars with fixed values.
|
||||
|
||||
Args:
|
||||
@@ -115,7 +121,7 @@ class NearFarCollider(SceneBoxCollider):
|
||||
self.far_plane = far_plane
|
||||
super().__init__(**kwargs)
|
||||
|
||||
def forward(self, ray_bundle: RayBundle) -> RayBundle:
|
||||
def set_nears_and_fars(self, ray_bundle: RayBundle) -> RayBundle:
|
||||
ones = torch.ones_like(ray_bundle.origins[..., 0:1])
|
||||
near_plane = self.near_plane if self.training else 0
|
||||
ray_bundle.nears = ones * near_plane
|
||||
|
||||
Reference in new issue
Block a user