Skip to content

geometry

Geometric operations: bounding boxes, rotations, point transforms, vertex normals, and spherical sampling.

Functions:

axis_aligned_bounding_box

axis_aligned_bounding_box(pos: Tensor) -> Tensor

Compute the axis aligned bounding box of a set of points, parameterized by \((c_x, c_y, c_z)\) and \((d_x, d_y, d_z)\) where \((c_x, c_y, c_z)\) is the center point of the box, and \(d_x\) is the x-axis length of the box.

Parameters:

  • pos (Tensor) –

    Points of shape \((N, 3)\), in XYZ order.

Returns:

  • Tensor –

    The axis aligned bounding box of shape \((6,)\).

transform_points

transform_points(
    points: Tensor, transform: Tensor
) -> Tensor

Transform points using a \(4 \times 4\) transformation matrix.

This function applies a \(4 \times 4\) transformation matrix to a set of points. The transformation matrix is assumed to be in the form:

\[ \begin{bmatrix} R & t \newline 0 & 1 \end{bmatrix} \]

Where \(R\) is a \(3 \times 3\) rotation matrix and \(t\) is a \(3 \times 1\) translation vector.

Parameters:

  • points (Tensor) –

    Points of shape \((N, 3)\), in XYZ order.

  • transform (Tensor) –

    A \(4 \times 4\) transformation matrix.

Returns:

  • Tensor –

    The transformed points of shape \((N, 3)\).

Examples:

>>> points = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
>>> transform = torch.tensor([[1.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 2.0], [0.0, 0.0, 1.0, 3.0], [0.0, 0.0, 0.0, 1.0]])
>>> transform_points(points, transform)
tensor([[2., 4., 6.],
        [5., 7., 9.]])

rotate

rotate(x: Tensor, R: Tensor) -> Tensor

Apply a rotation matrix to a 3D tensor.

Parameters:

  • x (Tensor) –

    Input tensor of shape \((\ldots, 3)\).

  • R (Tensor) –

    Rotation matrix of shape \((3, 3)\).

Returns:

  • Tensor –

    Rotated tensor with the same shape as x.

cross_product_matrix

cross_product_matrix(k: Tensor) -> Tensor

Constructs a skew-symmetric matrix (also known as a cross-product matrix) for a given 3D vector \(k = [k_1, k_2, k_3]\). The function returns a \(3 \times 3\) skew-symmetric matrix \(M(k)\) of the form:

\[ M(k) = \begin{bmatrix} 0 & -k_3 & k_2 \newline k_3 & 0 & -k_1 \newline -k_2 & k_1 & 0 \end{bmatrix} \]

Parameters:

  • k (Tensor) –

    A tensor of shape \((3,)\) representing the 3D vector.

Returns:

  • Tensor –

    A \(3 \times 3\) skew-symmetric matrix corresponding to the cross-product operation.

Examples:

>>> k = torch.tensor([1.0, 2.0, 3.0])
>>> v = torch.tensor([4.0, 5.0, 6.0])
>>> m = cross_product_matrix(k)
>>> cross_product = torch.matmul(m, v)

rodrigues_rotation_matrix

rodrigues_rotation_matrix(
    axis: Tensor, theta: float
) -> Tensor

Computes a 3D rotation matrix using Rodrigues' rotation formula.

This function rotates a vector in 3D space around a specified axis by a given angle in radians. The rotation matrix is computed using:

\[ R = I + \sin(\theta)K + (1 - \cos(\theta))K^2 \]

Where:

  • \(I\) is the identity matrix.
  • \(K\) is the skew-symmetric matrix (cross-product matrix) derived from the axis of rotation.
  • \(\theta\) is the rotation angle in radians.

Parameters:

  • axis (Tensor) –

    A 3D vector representing the axis of rotation.

  • theta (float) –

    The angle of rotation in radians. Pass math.radians(deg) to convert from degrees.

Returns:

  • Tensor –

    A \(3 \times 3\) rotation matrix that rotates a vector around the specified axis by the specified angle.

Raises:

  • Warning –

    If \(\theta > 2\pi\), which likely indicates degrees were passed instead of radians.

vertex_normals

vertex_normals(vertices: Tensor, face: Tensor) -> Tensor

Compute the vertex normal of a mesh.

Parameters:

  • vertices (Tensor) –

    The vertices of the mesh. Shape: \((V, 3)\).

  • face (Tensor) –

    The face of the mesh. Shape: \((F, 3)\).

Returns:

  • Tensor –

    The vertex normal of the mesh. Shape: \((V, 3)\).

Examples:

>>> vertices = torch.tensor([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.]])
>>> face = torch.tensor([[0, 1, 2]])
>>> vertex_normals(vertices, face)
tensor([[0., 0., 1.],
        [0., 0., 1.],
        [0., 0., 1.]])

random_spherical_points

random_spherical_points(
    radius: float,
    num_points: int,
    bounds: Union[float, Tuple[float, float]] = 1.0,
) -> Tensor

Generate random points inside a sphere or a spherical shell based on radius limits.

Parameters:

  • radius (float) –

    The radius of the sphere.

  • num_points (int) –

    The number of points to generate.

  • bounds (Union[float, Tuple[float, float]], default: 1.0 ) –

    A float or tuple of floats defining the inner and outer bounds of the sphere. If a single float is provided, it is treated as the outer limit, with the inner limit as 0. Defaults to 1.0.

Returns:

  • Tensor –

    Generated points of shape (num_points, dimension).

spherical_points_gradient

spherical_points_gradient(
    radius: float,
    num_points: int,
    fixed_position: Literal[
        "none", "center", "vertical"
    ] = "center",
    ratio: float = 0.66,
    max_steps: int = 10000,
    step_size: float = 0.01,
    step_decay: float = 0.9995,
    convergence_threshold: float = 1e-05,
    max_step_size: Optional[float] = None,
    return_grad_norms: Literal[False] = False,
) -> Tensor
spherical_points_gradient(
    radius: float,
    num_points: int,
    fixed_position: Literal[
        "none", "center", "vertical"
    ] = "center",
    ratio: float = 0.66,
    max_steps: int = 10000,
    step_size: float = 0.01,
    step_decay: float = 0.9995,
    convergence_threshold: float = 1e-05,
    max_step_size: Optional[float] = None,
    return_grad_norms: Literal[True] = True,
) -> Tuple[Tensor, Tensor]
spherical_points_gradient(
    radius: float,
    num_points: int,
    fixed_position: Literal[
        "none", "center", "vertical"
    ] = "center",
    ratio: float = 0.66,
    max_steps: int = 10000,
    step_size: float = 0.01,
    step_decay: float = 0.9995,
    convergence_threshold: float = 1e-05,
    max_step_size: Optional[float] = None,
    return_grad_norms: bool = False,
) -> Union[Tensor, Tuple[Tensor, Tensor]]

Creation of kernel points via optimization of potentials for a single kernel.

Parameters:

  • radius (float) –

    Radius of the kernel.

  • num_points (int) –

    Number of points composing the kernel.

  • fixed_position (Literal['none', 'center', 'vertical'], default: 'center' ) –

    Fix position of certain kernel points ('none', 'center', or 'vertical').

  • ratio (float, default: 0.66 ) –

    Ratio of the radius where you want the kernel points to be placed.

  • max_steps (int, default: 10000 ) –

    Maximum number of optimization steps.

  • step_size (float, default: 0.01 ) –

    Step size for moving points based on gradient norms.

  • step_decay (float, default: 0.9995 ) –

    Decay factor for reducing the step size over time.

  • convergence_threshold (float, default: 1e-05 ) –

    Threshold for stopping the optimization when gradient norm changes are small.

  • max_step_size (Optional[float], default: None ) –

    Maximum distance a point can move in a single step.

  • return_grad_norms (bool, default: False ) –

    Whether to also return the gradient norms recorded during the optimization.

Returns:

  • Union[Tensor, Tuple[Tensor, Tensor]] –

    Optimized kernel points of shape \((\text{num\_points}, 3)\), or a tuple of the kernel points and the recorded gradient norms when return_grad_norms=True.

spherical_points_lloyd

spherical_points_lloyd(
    radius: float,
    num_points: int,
    fixed_position: Literal[
        "none", "center", "vertical"
    ] = "none",
    approximation: Literal[
        "discretization", "monte-carlo"
    ] = "discretization",
    approx_n: int = 5000,
    max_iter: int = 500,
    momentum: float = 0.9,
) -> Tensor

Generate kernel points using Lloyd's algorithm on a sphere.

Parameters:

  • radius (float) –

    Radius of the sphere.

  • num_points (int) –

    Number of kernel points (Voronoi cells).

  • fixed_position (str, default: 'none' ) –

    Fix the position of specific kernel points. Defaults to 'none'. Options: - 'none': No kernel points are fixed. All points move freely during optimization. - 'center': The first kernel point is fixed at the center of the sphere. - 'vertical': (3D only) The first three kernel points are fixed along the z-axis: - The first point is fixed at the center. - The second point is placed above the center along the positive z-axis. - The third point is placed below the center along the negative z-axis.

  • approximation (str, default: 'discretization' ) –

    Approximation method for Lloyd's algorithm. Defaults to 'discretization'. Options: - 'discretization': Approximates the Voronoi cells using a regular grid of points within the sphere. - 'monte-carlo': Approximates the Voronoi cells by randomly sampling points within the sphere.

  • approx_n (int, default: 5000 ) –

    Number of points used for approximation. Defaults to 5000.

  • max_iter (int, default: 500 ) –

    Maximum number of iterations. Defaults to 500.

  • momentum (float, default: 0.9 ) –

    Momentum factor for smoothing kernel point positions. Defaults to 0.9.

Returns:

  • Tensor ( Tensor ) –

    Tensor of shape [num_points, dimension] with the final kernel points on the sphere.