Cleanup aabb intersection logic (#1329)

This commit is contained in:
Matthew Tancik authored and GitHub committed 2023-02-01 12:18:09 -08:00
1 parent 9966c43811
commit 567268854b
2 files changed
+63 -40

No files matched your search

+1 -10
View File
@@ -16,7 +16,6 @@
Camera Models Camera Models
""" """
import base64 import base64
import importlib
import math import math
import os import os
from dataclasses import dataclass from dataclasses import dataclass
@@ -472,15 +471,7 @@ class Cameras(TensorDataclass):
rays_o = rays_o.reshape((-1, 3)) rays_o = rays_o.reshape((-1, 3))
rays_d = rays_d.reshape((-1, 3)) rays_d = rays_d.reshape((-1, 3))
if self._use_nerfacc: t_min, t_max = nerfstudio.utils.math.intersect_aabb(rays_o, rays_d, tensor_aabb)
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_min.reshape([shape[0], shape[1], 1]) t_min = t_min.reshape([shape[0], shape[1], 1])
t_max = t_max.reshape([shape[0], shape[1], 1]) t_max = t_max.reshape([shape[0], shape[1], 1])
+62 -30
View File
@@ -15,10 +15,13 @@
""" Math Helper Functions """ """ Math Helper Functions """
from dataclasses import dataclass from dataclasses import dataclass
from typing import Tuple
import torch import torch
from torchtyping import TensorType from torchtyping import TensorType
_USE_NERFACC = True
def components_from_spherical_harmonics(levels: int, directions: TensorType[..., 3]) -> TensorType[..., "components"]: 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) 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 Implementation of ray intersection with AABB box
:param origin: N,3 tensor of 3d positions
:param direction: N,3 tensor of normalized directions Args:
:param aabb: 6,1 array of aabb box in the form of [x_min, y_min, z_min, x_max, y_max, z_max] origins: [N,3] tensor of 3d positions
:return: t_min, t_max - two tensors of shapes N,1 representing the intersection signed distance from directions: [N,3] tensor of normalized directions
the origin. t_min is clipped to 0 in case it is negative, in case of no intersection t_min is 1e10. 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_min = torch.min(tx_min, tx_max)
t_max = torch.max(tx_min, tx_max) t_max = torch.max(tx_min, tx_max)
ty_min_temp = (aabb[1] - origins[:, 1]) / directions[:, 1] t_min = torch.max(t_min, dim=-1).values
ty_max_temp = (aabb[4] - origins[:, 1]) / directions[:, 1] t_max = torch.min(t_max, dim=-1).values
ty_min = torch.min(ty_min_temp, ty_max_temp)
ty_max = torch.max(ty_min_temp, ty_max_temp)
cond = torch.logical_or((t_min > ty_max), (ty_min > t_max)) t_min = torch.clamp(t_min, min=0, max=max_bound)
t_min = torch.where(cond, max_bound, t_min) t_max = torch.clamp(t_max, min=0, max=max_bound)
t_max = torch.where(cond, max_bound, t_max)
t_min = torch.max(t_min, ty_min) cond = t_max <= t_min
t_max = torch.min(t_max, ty_max) 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] return t_min, t_max
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)
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) def intersect_aabb(
t_max = torch.min(t_max, tz_max) origins: TensorType["N", 3],
t_min = torch.where(t_min > 0, t_min, 0) directions: TensorType["N", 3],
t_max = torch.where(t_min == max_bound, max_bound, t_max) 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 return t_min, t_max