Math#

Math Helper Functions

class nerfstudio.utils.math.Gaussians(mean: Float[Tensor, '*batch dim'], cov: Float[Tensor, '*batch dim dim'])[source]#

Bases: object

Stores Gaussians

Parameters:
  • mean – Mean of multivariate Gaussian

  • cov – Covariance of multivariate Gaussian.

nerfstudio.utils.math.columnwise_squared_l2_distance(x: Float[Tensor, '*M N'], y: Float[Tensor, '*M N']) Float[Tensor, 'N N'][source]#

Compute the squared Euclidean distance between all pairs of columns. Adapted from https://github.com/google-research/multinerf/blob/5b4d4f64608ec8077222c52fdf814d40acc10bc1/internal/geopoly.py

Parameters:
  • x – tensor of floats, with shape [M, N].

  • y – tensor of floats, with shape [M, N].

Returns:

tensor of floats, with shape [N, N].

Return type:

sq_dist

nerfstudio.utils.math.compute_3d_gaussian(directions: Float[Tensor, '*batch 3'], means: Float[Tensor, '*batch 3'], dir_variance: Float[Tensor, '*batch 1'], radius_variance: Float[Tensor, '*batch 1']) Gaussians[source]#

Compute gaussian along ray.

Parameters:
  • directions – Axis of Gaussian.

  • means – Mean of Gaussian.

  • dir_variance – Variance along direction axis.

  • radius_variance – Variance tangent to direction axis.

Returns:

Oriented 3D gaussian.

Return type:

Gaussians

nerfstudio.utils.math.conical_frustum_to_gaussian(origins: Float[Tensor, '*batch 3'], directions: Float[Tensor, '*batch 3'], starts: Float[Tensor, '*batch 1'], ends: Float[Tensor, '*batch 1'], radius: Float[Tensor, '*batch 1']) Gaussians[source]#

Approximates conical frustums with a Gaussian distributions.

Uses stable parameterization described in mip-NeRF publication.

Parameters:
  • origins – Origins of cones.

  • directions – Direction (axis) of frustums.

  • starts – Start of conical frustums.

  • ends – End of conical frustums.

  • radius – Radii of cone a distance of 1 from the origin.

Returns:

Approximation of conical frustums

Return type:

Gaussians

nerfstudio.utils.math.cylinder_to_gaussian(origins: Float[Tensor, '*batch 3'], directions: Float[Tensor, '*batch 3'], starts: Float[Tensor, '*batch 1'], ends: Float[Tensor, '*batch 1'], radius: Float[Tensor, '*batch 1']) Gaussians[source]#

Approximates cylinders with a Gaussian distributions.

Parameters:
  • origins – Origins of cylinders.

  • directions – Direction (axis) of cylinders.

  • starts – Start of cylinders.

  • ends – End of cylinders.

  • radius – Radii of cylinders.

Returns:

Approximation of cylinders

Return type:

Gaussians

nerfstudio.utils.math.expected_sin(x_means: Tensor, x_vars: Tensor) Tensor[source]#

Computes the expected value of sin(y) where y ~ N(x_means, x_vars)

Parameters:
  • x_means – Mean values.

  • x_vars – Variance of values.

Returns:

The expected value of sin.

Return type:

torch.Tensor

nerfstudio.utils.math.generate_polyhedron_basis(basis_shape: Literal['icosahedron', 'octahedron'], angular_tesselation: int, remove_symmetries: bool = True, eps: float = 0.0001) Tensor[source]#

Generates a 3D basis by tesselating a geometric polyhedron. Basis is used to construct Fourier features for positional encoding. See Mip-Nerf360 paper: https://arxiv.org/abs/2111.12077 Adapted from https://github.com/google-research/multinerf/blob/5b4d4f64608ec8077222c52fdf814d40acc10bc1/internal/geopoly.py

Parameters:
  • base_shape – string, the name of the starting polyhedron, must be either ‘icosahedron’ or ‘octahedron’.

  • angular_tesselation – int, the number of times to tesselate the polyhedron, must be >= 1 (a value of 1 is a no-op to the polyhedron).

  • remove_symmetries – bool, if True then remove the symmetric basis columns, which is usually a good idea because otherwise projections onto the basis will have redundant negative copies of each other.

  • eps – float, a small number used to determine symmetries.

Returns:

a matrix with shape [3, n].

Return type:

basis

nerfstudio.utils.math.intersect_aabb(origins: Tensor, directions: Tensor, aabb: Tensor, max_bound: float = 10000000000.0, invalid_value: float = 10000000000.0) Tuple[Tensor, Tensor][source]#

Implementation of ray intersection with AABB box

Parameters:
  • 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.

nerfstudio.utils.math.intersect_obb(origins: Tensor, directions: Tensor, obb: OrientedBox, max_bound: float = 10000000000.0, invalid_value: float = 10000000000.0)[source]#

Ray intersection with an oriented bounding box (OBB)

Parameters:
  • 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

nerfstudio.utils.math.k_nearest_sklearn(x: Tensor, k: int, metric: str = 'euclidean') Tuple[Float[Tensor, '*batch k'], Int[Tensor, '*batch k']][source]#

Find k-nearest neighbors using sklearn’s NearestNeighbors.

Parameters:
  • x – input tensor

  • k – number of neighbors to find

  • metric – metric to use for distance computation

Returns:

distances to the k-nearest neighbors indices: indices of the k-nearest neighbors

Return type:

distances

nerfstudio.utils.math.masked_reduction(input_tensor: Float[Tensor, '1 32 mult'], mask: Bool[Tensor, '1 32 mult'], reduction_type: Literal['image', 'batch']) Tensor[source]#

Whether to consolidate the input_tensor across the batch or across the image :param input_tensor: input tensor :param mask: mask tensor :param reduction_type: either “batch” or “image”

Returns:

reduced input_tensor

Return type:

input_tensor

nerfstudio.utils.math.normalized_depth_scale_and_shift(prediction: Float[Tensor, '1 32 mult'], target: Float[Tensor, '1 32 mult'], mask: Bool[Tensor, '1 32 mult'])[source]#

More info here: https://arxiv.org/pdf/2206.00665.pdf supplementary section A2 Depth Consistency Loss This function computes scale/shift required to normalizes predicted depth map, to allow for using normalized depth maps as input from monocular depth estimation networks. These networks are trained such that they predict normalized depth maps.

Solves for scale/shift using a least squares approach with a closed form solution: Based on: https://github.com/autonomousvision/monosdf/blob/d9619e948bf3d85c6adec1a643f679e2e8e84d4b/code/model/loss.py#L7 :param prediction: predicted depth map :param target: ground truth depth map :param mask: mask of valid pixels

Returns:

scale and shift for depth prediction

nerfstudio.utils.math.random_quat_tensor(N: int) Float[Tensor, '*batch 4'][source]#

Defines a random quaternion tensor.

Parameters:

N – Number of quaternions to generate

Returns:

a random quaternion tensor of shape (N, 4)

nerfstudio.utils.math.safe_normalize(vectors: Float[Tensor, '*batch_dim N'], eps: float = 1e-10) Float[Tensor, '*batch_dim N'][source]#

Normalizes vectors.

Parameters:
  • vectors – Vectors to normalize.

  • eps – Epsilon value to avoid division by zero.

Returns:

Normalized vectors.