SDF Utilities¶
isoext.sdf provides optional utilities for working with signed distance functions. These are convenient for testing and demos, but you can always compute values with your own code.
Note: These utilities are not required — see Working with Grids for examples using raw PyTorch.
import isoext
from isoext.sdf import *
from _viz import show_mesh
grid = isoext.UniformGrid([128, 128, 128])
Primitives¶
SphereSDF¶
SphereSDF(radius: float)
sphere = SphereSDF(radius=0.7)
grid.set_values(sphere(grid.get_points()))
v, f = isoext.marching_cubes(grid)
show_mesh(v, f)
2025-12-30 01:10:13.373 ( 0.446s) [ 7676C8F11740]vtkXOpenGLRenderWindow.:1458 WARN| bad X server connection. DISPLAY=
TorusSDF¶
TorusSDF(R: float, r: float) # R=major radius, r=tube radius
torus = TorusSDF(R=0.6, r=0.2)
grid.set_values(torus(grid.get_points()))
v, f = isoext.marching_cubes(grid)
show_mesh(v, f, color="gold")
CuboidSDF¶
CuboidSDF(size: list[float]) # Full size in [x, y, z]
cube = CuboidSDF(size=[1.0, 1.0, 1.0])
grid.set_values(cube(grid.get_points()))
v, f = isoext.marching_cubes(grid)
show_mesh(v, f, color="salmon")
CSG Operations¶
Combine shapes using Constructive Solid Geometry:
Operation |
Description |
|---|---|
|
Combine shapes (min of SDFs) |
|
Keep overlap (max of SDFs) |
|
Invert inside/outside |
|
Smooth blend with radius k |
# Sphere with a hole drilled through it
sphere = SphereSDF(radius=0.7)
hole = CuboidSDF(size=[0.3, 0.3, 2.0])
drilled = IntersectionOp([sphere, NegationOp(hole)])
grid.set_values(drilled(grid.get_points()))
v, f = isoext.marching_cubes(grid)
show_mesh(v, f, color="orchid")
Transformations¶
Transform |
Description |
|---|---|
|
Move by |
|
Rotate around axis (degrees by default) |
# Two spheres with smooth blending
s1 = TranslationOp(SphereSDF(radius=0.4), offset=[-0.3, 0, 0])
s2 = TranslationOp(SphereSDF(radius=0.4), offset=[0.3, 0, 0])
blended = SmoothUnionOp([s1, s2], k=0.15)
grid.set_values(blended(grid.get_points()))
v, f = isoext.marching_cubes(grid)
show_mesh(v, f, color="tomato")