From 567268854bd7822cd98a438015cb3220f959dc48 Mon Sep 17 00:00:00 2001 From: Matthew Tancik Date: Wed, 1 Feb 2023 12:18:09 -0800 Subject: [PATCH] Cleanup aabb intersection logic (#1329) --- nerfstudio/cameras/cameras.py | 11 +---- nerfstudio/utils/math.py | 92 +++++++++++++++++++++++------------ 2 files changed, 63 insertions(+), 40 deletions(-) diff --git a/nerfstudio/cameras/cameras.py b/nerfstudio/cameras/cameras.py index 240c771b6..d7d228bae 100644 --- a/nerfstudio/cameras/cameras.py +++ b/nerfstudio/cameras/cameras.py @@ -16,7 +16,6 @@ Camera Models """ import base64 -import importlib import math import os from dataclasses import dataclass @@ -472,15 +471,7 @@ class Cameras(TensorDataclass): rays_o = rays_o.reshape((-1, 3)) rays_d = rays_d.reshape((-1, 3)) - if self._use_nerfacc: - try: - nerfacc = importlib.import_module("nerfacc") - t_min, t_max = nerfacc.ray_aabb_intersect(rays_o, rays_d, tensor_aabb) - except: # pylint: disable=bare-except - t_min, t_max = nerfstudio.utils.math.intersect_aabb(rays_o, rays_d, tensor_aabb) - self._use_nerfacc = False - else: - t_min, t_max = nerfstudio.utils.math.intersect_aabb(rays_o, rays_d, tensor_aabb) + t_min, t_max = nerfstudio.utils.math.intersect_aabb(rays_o, rays_d, tensor_aabb) t_min = t_min.reshape([shape[0], shape[1], 1]) t_max = t_max.reshape([shape[0], shape[1], 1]) diff --git a/nerfstudio/utils/math.py b/nerfstudio/utils/math.py index 2658b1c39..9c2077f4b 100644 --- a/nerfstudio/utils/math.py +++ b/nerfstudio/utils/math.py @@ -15,10 +15,13 @@ """ Math Helper Functions """ from dataclasses import dataclass +from typing import Tuple import torch from torchtyping import TensorType +_USE_NERFACC = True + def components_from_spherical_harmonics(levels: int, directions: TensorType[..., 3]) -> TensorType[..., "components"]: """ @@ -193,47 +196,76 @@ def expected_sin(x_means: torch.Tensor, x_vars: torch.Tensor) -> torch.Tensor: return torch.exp(-0.5 * x_vars) * torch.sin(x_means) -def intersect_aabb(origins, directions, aabb): +@torch.jit.script +def _intersect_aabb( + origins: torch.Tensor, + directions: torch.Tensor, + aabb: torch.Tensor, + max_bound: float = 1e10, + invalid_value: float = 1e10, +) -> Tuple[torch.Tensor, torch.Tensor]: """ - pytorch implementation of ray intersection with AABB box - :param origin: N,3 tensor of 3d positions - :param direction: N,3 tensor of normalized directions - :param aabb: 6,1 array of aabb box in the form of [x_min, y_min, z_min, x_max, y_max, z_max] - :return: t_min, t_max - two tensors of shapes N,1 representing the intersection signed distance from - the origin. t_min is clipped to 0 in case it is negative, in case of no intersection t_min is 1e10. + Implementation of ray intersection with AABB box + + Args: + origins: [N,3] tensor of 3d positions + directions: [N,3] tensor of normalized directions + aabb: [6] array of aabb box in the form of [x_min, y_min, z_min, x_max, y_max, z_max] + max_bound: Maximum value of t_max + invalid_value: Value to return in case of no intersection + + Returns: + t_min, t_max - two tensors of shapes N representing distance of intersection from the origin. """ - max_bound = 1e10 + tx_min = (aabb[:3] - origins) / directions + tx_max = (aabb[3:] - origins) / directions - tx_min = (aabb[0] - origins[:, 0]) / directions[:, 0] - tx_max = (aabb[3] - origins[:, 0]) / directions[:, 0] t_min = torch.min(tx_min, tx_max) t_max = torch.max(tx_min, tx_max) - ty_min_temp = (aabb[1] - origins[:, 1]) / directions[:, 1] - ty_max_temp = (aabb[4] - origins[:, 1]) / directions[:, 1] - ty_min = torch.min(ty_min_temp, ty_max_temp) - ty_max = torch.max(ty_min_temp, ty_max_temp) + t_min = torch.max(t_min, dim=-1).values + t_max = torch.min(t_max, dim=-1).values - cond = torch.logical_or((t_min > ty_max), (ty_min > t_max)) - t_min = torch.where(cond, max_bound, t_min) - t_max = torch.where(cond, max_bound, t_max) + t_min = torch.clamp(t_min, min=0, max=max_bound) + t_max = torch.clamp(t_max, min=0, max=max_bound) - t_min = torch.max(t_min, ty_min) - t_max = torch.min(t_max, ty_max) + cond = t_max <= t_min + t_min = torch.where(cond, invalid_value, t_min) + t_max = torch.where(cond, invalid_value, t_max) - tz_min_temp = (aabb[2] - origins[:, 2]) / directions[:, 2] - tz_max_temp = (aabb[5] - origins[:, 2]) / directions[:, 2] - tz_min = torch.min(tz_min_temp, tz_max_temp) - tz_max = torch.max(tz_min_temp, tz_max_temp) + return t_min, t_max - cond = torch.logical_or((t_min > tz_max), tz_min > t_max) - t_min = torch.where(cond, max_bound, t_min) - t_max = torch.where(cond, max_bound, t_max) - t_min = torch.max(t_min, tz_min) - t_max = torch.min(t_max, tz_max) - t_min = torch.where(t_min > 0, t_min, 0) - t_max = torch.where(t_min == max_bound, max_bound, t_max) +def intersect_aabb( + origins: TensorType["N", 3], + directions: TensorType["N", 3], + aabb: TensorType[6], +) -> Tuple[TensorType["N"], TensorType["N"]]: + """ + Implementation of ray intersection with AABB box + + Args: + origins: 3d positions + directions: Normalized directions + aabb: array of aabb box in the form of [x_min, y_min, z_min, x_max, y_max, z_max] + max_bound: Maximum value of t_max + invalid_value: Value to return in case of no intersection + + Returns: + t_min, t_max - two tensors of shapes N representing distance of intersection from the origin. + """ + + global _USE_NERFACC # pylint: disable=global-statement + if _USE_NERFACC: + try: + import nerfacc # pylint: disable=import-outside-toplevel + + t_min, t_max = nerfacc.ray_aabb_intersect(origins, directions, aabb) + except: # pylint: disable=bare-except + t_min, t_max = _intersect_aabb(origins, directions, aabb, max_bound=1e10, invalid_value=1e10) + _USE_NERFACC = False + else: + t_min, t_max = _intersect_aabb(origins, directions, aabb, max_bound=1e10, invalid_value=1e10) return t_min, t_max