DeepSDFStruct.SDF#
Functions
|
Generates an equidistant 3D grid of points within the given bounding box. |
Transform mesh coordinates uniformly to [-1, 1] in all axes. |
|
|
Calculates the minimum distance from one or more query points to one or more line segments defined by endpoints P1 and P2. |
|
D: np.array of shape (num_points, num_geometries) k: smoothness parameter |
|
D: np.array of shape (num_points, num_geometries) k: smoothness parameter |
Classes
|
|
A dictionary type describing boundary conditions ("caps") for each axis direction (x, y, z). |
|
|
|
|
|
|
Abstract base class for Signed Distance Functions with optional deformation and parametrization. |
|
|
|
|
|
Create an SDF from a triangle mesh using closest-point queries. |
|
|
|
Generic SDF wrapper that applies a transformation to the input queries. |
- class DeepSDFStruct.SDF.BoxSDF(box_size=1, center=tensor([0, 0, 0]))#
Bases:
DeepSDFStruct.SDF.SDFBase- Parameters:
box_size (float)
center (torch._VariableFunctionsClass.tensor)
- class DeepSDFStruct.SDF.CapBorderDict#
Bases:
TypedDictA dictionary type describing boundary conditions (“caps”) for each axis direction (x, y, z).
Each key (x0, x1, y0, y1, z0, z1) corresponds to one boundary face of a 3D domain, and maps to a dictionary with two fields:
cap (int): Type of cap applied (e.g., -1 = none, 1 = active).
measure (float): Numerical measure associated with the cap (e.g., thickness, scaling factor, tolerance).
Example
>>> caps: CapBorderDict = { ... "x0": {"cap": 1, "measure": 0.02}, ... "x1": {"cap": 1, "measure": 0.02}, ... "y0": {"cap": 1, "measure": 0.02}, ... "y1": {"cap": 1, "measure": 0.02}, ... "z0": {"cap": 1, "measure": 0.02}, ... "z1": {"cap": 1, "measure": 0.02}, ... }
- class DeepSDFStruct.SDF.NegatedCallable(obj)#
Bases:
DeepSDFStruct.SDF.SDFBase
- class DeepSDFStruct.SDF.SDF2D(obj, axes, offset=0.0)#
Bases:
DeepSDFStruct.SDF.SDFBase- Parameters:
axes (list[int])
- class DeepSDFStruct.SDF.SDFBase(deformation_spline=None, parametrization=None, cap_border_dict=None, cap_outside_of_unitcube=False, geometric_dim=3)#
Bases:
abc.ABCAbstract base class for Signed Distance Functions with optional deformation and parametrization.
This class provides the foundation for all SDF representations in DeepSDFStruct. SDFs represent geometry as an implicit function that returns the signed distance from any query point to the nearest surface. Negative values indicate points inside the geometry, positive values indicate points outside, and zero indicates points on the surface.
The class supports: - Optional spline-based deformations for smooth geometric transformations - Parametrization functions for spatially-varying properties - Border capping to constrain geometry within specified bounds - Composition operations (union, intersection) via operator overloading
- Parameters:
deformation_spline (TorchSpline, optional) – A spline function that maps from parametric to physical space, enabling smooth deformations of the base geometry.
parametrization (torch.nn.Module, optional) – A neural network or function that provides spatially-varying parameters for the SDF (e.g., varying thickness in a lattice).
cap_border_dict (CapBorderDict, optional) – Dictionary specifying boundary conditions for each face of the domain. Keys are ‘x0’, ‘x1’, ‘y0’, ‘y1’, ‘z0’, ‘z1’ for the six faces.
cap_outside_of_unitcube (bool, default False) – If True, caps the SDF values outside the unit cube to create a bounded geometry.
geometric_dim (int, default 3) – Geometric dimension of the SDF (2 or 3).
Notes
Subclasses must implement: -
_compute(queries): Calculate SDF values for query points -_get_domain_bounds(): Return the bounding box of the geometryExamples
>>> from DeepSDFStruct.sdf_primitives import SphereSDF >>> import torch >>> >>> # Create a sphere SDF >>> sphere = SphereSDF(center=[0, 0, 0], radius=1.0) >>> >>> # Query SDF values >>> points = torch.tensor([[0.0, 0.0, 0.0], [2.0, 0.0, 0.0]]) >>> distances = sphere(points) >>> print(distances) # [-1.0, 1.0] (inside, outside)
- property cap_border_dict#
- property deformation_spline#
- property parametrization#
- plot_slice(*args, **kwargs)#
- to2D(axes, offset=0.0)#
Converts SDF to 2D
- Parameters:
axis – list of axes that will be used for the 2D
axes (list[int])
- class DeepSDFStruct.SDF.SDFfromDeepSDF(model, max_batch=32768)#
Bases:
DeepSDFStruct.SDF.SDFBase- Parameters:
- set_latent_vec(latent_vec)#
Set conditioning parameters for the model (e.g., latent code).
- Parameters:
latent_vec (torch.Tensor)
- class DeepSDFStruct.SDF.SDFfromLineMesh(line_mesh, thickness, smoothness=0)#
Bases:
DeepSDFStruct.SDF.SDFBase- Parameters:
line_mesh (gustaf.edges.Edges)
-
line_mesh:
Edges#
- class DeepSDFStruct.SDF.SDFfromMesh(mesh, dtype=<class 'numpy.float32'>, flip_sign=False, scale=True, threshold=1e-05)#
Bases:
DeepSDFStruct.SDF.SDFBaseCreate an SDF from a triangle mesh using closest-point queries.
This class wraps a triangle mesh and computes signed distances by finding the closest point on the mesh surface to each query point. The sign is determined using winding number or ray casting to determine inside/outside.
The mesh can be optionally normalized to fit within a unit cube centered at the origin, which is useful for consistent scaling across different geometries.
- Parameters:
mesh (trimesh.Trimesh or gustaf.faces.Faces) – The input triangle mesh. If a gustaf Faces object is provided, it will be converted to a trimesh object.
dtype (numpy dtype, default np.float32) – Data type for distance calculations.
flip_sign (bool, default False) – If True, flips the sign of the computed distances (inside becomes outside and vice versa).
scale (bool, default True) – If True, normalizes the mesh to fit within a unit cube [-1, 1]^3 centered at the origin.
threshold (float, default 1e-5) – Small threshold value for numerical stability in distance computations.
- mesh#
The (possibly normalized) triangle mesh.
- Type:
trimesh.Trimesh
- dtype#
Data type used for calculations.
- Type:
numpy dtype
- flip_sign#
Whether distances are sign-flipped.
- Type:
bool
- threshold#
Numerical threshold for stability.
- Type:
float
Notes
This class uses libigl for efficient closest-point queries and embree for ray intersection tests to determine inside/outside status.
Examples
>>> import trimesh >>> from DeepSDFStruct.SDF import SDFfromMesh >>> import torch >>> >>> # Load or create a mesh >>> mesh = trimesh.creation.box(extents=[1, 1, 1]) >>> >>> # Create SDF from mesh >>> sdf = SDFfromMesh(mesh, scale=True) >>> >>> # Query distances >>> points = torch.tensor([[0.0, 0.0, 0.0], [2.0, 0.0, 0.0]]) >>> distances = sdf(points)
- class DeepSDFStruct.SDF.SummedSDF(obj1, obj2)#
Bases:
DeepSDFStruct.SDF.SDFBase- Parameters:
obj1 (DeepSDFStruct.SDF.SDFBase)
obj2 (DeepSDFStruct.SDF.SDFBase)
- class DeepSDFStruct.SDF.TransformedSDF(sdf, rotation=None, translation=None, scale=None)#
Bases:
DeepSDFStruct.SDF.SDFBaseGeneric SDF wrapper that applies a transformation to the input queries. Transformation can be rotation, translation, or scaling.
- Parameters:
- DeepSDFStruct.SDF.get_equidistant_grid_sample(bounds, grid_spacing, dtype=torch.float32, device='cpu')#
Generates an equidistant 3D grid of points within the given bounding box.
- Parameters:
bounds (torch.Tensor) – Tensor of shape (2,3), [[xmin, ymin, zmin], [xmax, ymax, zmax]]
grid_spacing (float) – Approximate spacing between points along each axis.
- Returns:
points – Tensor of shape (N,3) containing all grid points.
- Return type:
torch.Tensor
- DeepSDFStruct.SDF.normalize_mesh_to_unit_cube(mesh)#
Transform mesh coordinates uniformly to [-1, 1] in all axes. Keeps aspect ratio of original mesh.
- Parameters:
mesh (trimesh.base.Trimesh)
- DeepSDFStruct.SDF.point_segment_distance(P1, P2, query_points)#
Calculates the minimum distance from one or more query points to one or more line segments defined by endpoints P1 and P2.
- Parameters:
P1 (np.ndarray) – Array of shape (M, 2) or (2,) representing first endpoints of segments.
P2 (np.ndarray) – Array of shape (M, 2) or (2,) representing second endpoints of segments.
query_points (np.ndarray) – Array of shape (N, 2) or (2,) representing query point(s).
- Returns:
- Array of shape (N,) with the minimum distance from each query point
to the closest segment.
- Return type:
np.ndarray
- DeepSDFStruct.SDF.union_numpy(D, k=0)#
D: np.array of shape (num_points, num_geometries) k: smoothness parameter
- DeepSDFStruct.SDF.union_torch(D, k=0)#
D: np.array of shape (num_points, num_geometries) k: smoothness parameter