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=
_images/e069eab6179d4867d076f101d49befa808e913fa50a9be6c2520b6c72fd4ce62.png

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")
_images/0cd26d040416cb9cbf6b71e2400c061f015dcf0a36fb6346e1a464aea7dd57b7.png

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")
_images/a55cf9bc759b928696ac63f68746e8bea976a8beb020f9327cb422deb64eafa0.png

CSG Operations

Combine shapes using Constructive Solid Geometry:

Operation

Description

UnionOp([...])

Combine shapes (min of SDFs)

IntersectionOp([...])

Keep overlap (max of SDFs)

NegationOp(sdf)

Invert inside/outside

SmoothUnionOp([...], k)

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")
_images/570fa72df1c0dc938d7340f291a0ff412ce615463834463a465e8073e0658542.png

Transformations

Transform

Description

TranslationOp(sdf, offset)

Move by [x, y, z]

RotationOp(sdf, axis, angle)

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")
_images/ddb3cca37e7ed11160ffc936aa7f2fd82e4fc75ee2975458e2ac56789fcec8bc.png