DeepSDFStruct.torch_spline#

PyTorch-Compatible B-Spline Operations#

This module provides differentiable B-spline evaluation using PyTorch, enabling spline-based deformations and parametrizations in gradient-based optimization workflows.

Key Features#

TorchSpline Class

Main interface for PyTorch-compatible spline evaluation. Wraps splinepy splines and provides forward evaluation with automatic differentiation support.

Low-level Functions
  • torch_spline_1D: Evaluate 1D B-splines using de Boor’s algorithm

  • torch_spline_2D: Evaluate 2D tensor product B-splines

  • torch_spline_3D: Evaluate 3D tensor product B-splines

  • bspline_basis: Compute B-spline basis functions

The implementation uses vectorized de Boor’s algorithm for efficient batch evaluation on GPU. All operations are differentiable with respect to control points and query coordinates.

Examples

Create and evaluate a TorchSpline:

import splinepy
import torch
from DeepSDFStruct.torch_spline import TorchSpline

# Create a splinepy B-spline
spline = splinepy.BSpline(
    degrees=[2, 2, 2],
    control_points=torch.rand(27, 3),
    knot_vectors=[
        [0, 0, 0, 1, 1, 1],
        [0, 0, 0, 1, 1, 1],
        [0, 0, 0, 1, 1, 1]
    ]
)

# Wrap in TorchSpline
torch_spline = TorchSpline(spline, device='cuda')

# Evaluate at query points
queries = torch.rand(1000, 3, device='cuda')
values = torch_spline(queries)

# Compute gradients
values.sum().backward()
print(torch_spline.control_points.grad)

Notes

The module supports: - B-splines (non-rational) - NURBS (rational B-splines) - Bezier splines (special case of B-splines) - 1D, 2D, and 3D parametric dimensions - Arbitrary output dimensions

Functions

bspline_basis(t, p, queries)

generate_bbox_spline(bounds)

Takes bounding box and generates a spline box.

torch_spline_1D(knot_vectors, ...)

torch_spline_2D(knot_vectors, ...)

Evaluate a 2D B-spline volume at query points.

torch_spline_3D(knot_vectors, ...)

Evaluate a 3D B-spline volume at query points.

Classes

TorchScaling(scale_factors, translation, bounds)

A simple scaling function to transform the mesh from parametric to physical space.

TorchSpline(spline[, device, dtype])

V2: uses custom torch spline as spline backend

class DeepSDFStruct.torch_spline.TorchScaling(scale_factors, translation, bounds, device='cpu', dtype=torch.float32)#

Bases: torch.nn.modules.module.Module

A simple scaling function to transform the mesh from parametric to physical space. Used as a baseline deformation function when no complex deformation is needed.

forward(queries)#

param -> phys

Parameters:

queries (torch.Tensor)

inverse_target_points(query_points_phys_space)#

phys -> param

Parameters:

query_points_phys_space (torch.Tensor)

class DeepSDFStruct.torch_spline.TorchSpline(spline, device='cpu', dtype=torch.float32)#

Bases: torch.nn.modules.module.Module

V2: uses custom torch spline as spline backend

Parameters:

spline (splinepy.bspline.BSpline)

forward(queries)#

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Parameters:

queries (torch.Tensor)

inverse_target_points(query_points_phys_space, return_verbose=False)#
Parameters:

query_points_phys_space (torch.Tensor)

property knot_vectors#
spline: splinepy.bspline.BSpline#
DeepSDFStruct.torch_spline.bspline_basis(t, p, queries)#
Parameters:

queries (torch._VariableFunctionsClass.tensor)

DeepSDFStruct.torch_spline.generate_bbox_spline(bounds)#

Takes bounding box and generates a spline box. Works for 2D [[xmin, ymin], [xmax, ymax]] and 3D [[xmin, ymin, zmin], [xmax, ymax, zmax]].

Parameters:

bounds ((2, d) array-like) –

[[min1, min2, …],

[max1, max2, …]]

Returns:

spline – BSpline representing the bounding box.

Return type:

splinepy.BSpline

DeepSDFStruct.torch_spline.torch_spline_1D(knot_vectors, control_points, degrees, queries)#
Parameters:

queries (torch._VariableFunctionsClass.tensor)

DeepSDFStruct.torch_spline.torch_spline_2D(knot_vectors, control_points, degrees, queries)#

Evaluate a 2D B-spline volume at query points.

Parameters:
  • tx

  • x (ty knot vectors for)

  • y

  • px

  • degrees (py)

  • cp – control points, shape (nx, ny, 2)

  • qx (N,)

  • qy (N,)

  • coordinates (query)

  • shape (N,)

Returns:

evaluated spline, shape (N, 2)

Return type:

y

DeepSDFStruct.torch_spline.torch_spline_3D(knot_vectors, control_points, degrees, queries)#

Evaluate a 3D B-spline volume at query points.

Parameters:
  • tx – knot vectors for x, y, z

  • ty – knot vectors for x, y, z

  • tz – knot vectors for x, y, z

  • px – degrees

  • py – degrees

  • pz – degrees

  • cp – control points, shape (nx, ny, nz, d)

  • qx – query coordinates, shape (N,)

  • qy – query coordinates, shape (N,)

  • qz – query coordinates, shape (N,)

Returns:

evaluated spline, shape (N, d)

Return type:

y