A Python library for working with Signed Distance Functions (SDFs) and performing topology optimization. This project provides a complete toolkit for implicit CAD modeling, allowing you to create complex 3D shapes using mathematical functions rather than explicit geometry.
- SDF Primitives: Basic geometric shapes (sphere, box, cylinder, torus, plane)
- CSG Operations: Union, intersection, difference, and smooth blending
- Transformations: Translation, rotation, scaling, and arbitrary matrix transforms
- Topology Optimization: Level-set method for structural optimization
- Mesh Generation: Marching cubes algorithm for converting SDFs to triangle meshes
- Export Formats: STL and OBJ export support
pip install -r requirements.txtfrom implicit_cad import Sphere, Box, Union, Difference, MeshGenerator
# Create primitives
sphere = Sphere(radius=1.0, center=(0, 0, 0))
box = Box(size=(1.5, 1.5, 1.5), center=(0, 0, 0))
# Combine using CSG operations
result = Difference(sphere, box)
# Generate mesh
bounds = ((-3, -3, -3), (3, 3, 3))
resolution = (64, 64, 64)
generator = MeshGenerator(bounds, resolution)
mesh = generator.generate_mesh(result)
# Export to STL
generator.export_stl(result, "output.stl")from implicit_cad import TopologyOptimizer, MeshGenerator
import numpy as np
def compliance_objective(density_field):
# Define your objective function
# Returns: (objective_value, sensitivity_field)
objective = np.sum(density_field)
sensitivity = -np.ones_like(density_field)
return objective, sensitivity
# Create optimizer
bounds = ((0, 0, 0), (4, 1, 1))
resolution = (80, 20, 20)
optimizer = TopologyOptimizer(
bounds=bounds,
resolution=resolution,
volume_fraction=0.3,
filter_radius=1.5,
)
# Run optimization
final_phi = optimizer.evolve_level_set(
objective_func=compliance_objective,
max_iterations=50,
)
# Generate mesh from optimized result
optimized_sdf = optimizer.get_sdf_from_level_set()
generator = MeshGenerator(bounds, resolution)
mesh = generator.generate_mesh(optimized_sdf)
generator.export_stl(optimized_sdf, "optimized.stl")- SDF Base Class (
sdf.py): Abstract base class for all signed distance functions - Primitives (
primitives.py): Basic geometric shapes - Operations (
operations.py): CSG operations for combining SDFs - Transforms (
transforms.py): Geometric transformations - Topology Optimizer (
topology_optimizer.py): Level-set based optimization - Mesh Generator (
mesh_generator.py): Marching cubes mesh generation
A Signed Distance Function (SDF) is a function that returns the signed distance from any point in 3D space to the surface of a shape:
- Negative values: Point is inside the shape
- Positive values: Point is outside the shape
- Zero: Point is on the surface
SDFs enable powerful operations:
- CSG Operations: Combine shapes using min/max operations
- Smooth Blending: Create organic transitions between shapes
- Topology Optimization: Evolve shapes to optimize objectives
See the examples/ directory for complete examples:
basic_primitives.py: Creating and combining basic shapessmooth_blending.py: Using smooth union and intersectiontopology_optimization.py: Structural optimization examplevisualize_sdf.py: Visualizing SDF values and isosurfaces
Run examples:
python examples/basic_primitives.py
python examples/topology_optimization.pyThe topology optimizer uses a level-set method to evolve a shape and minimize an objective function (e.g., compliance) while satisfying constraints (e.g., volume fraction).
- Level-Set Representation: The shape is represented as a level-set function (SDF)
- Density Field: Converted to a density field using a smooth Heaviside function
- Objective Evaluation: Compute objective and sensitivity (gradient)
- Sensitivity Filtering: Apply filtering to reduce mesh dependency
- Level-Set Evolution: Update the level-set function using gradient descent
- Reinitialization: Periodically reinitialize to maintain signed distance property
To use topology optimization, provide an objective function that:
- Takes a density field (numpy array) as input
- Returns
(objective_value, sensitivity_field)tuple - The sensitivity field indicates where to add/remove material
Example:
def my_objective(density_field):
# Compute objective (e.g., compliance, stress, etc.)
objective = compute_compliance(density_field)
# Compute sensitivity (gradient w.r.t. density)
sensitivity = compute_sensitivity(density_field)
return objective, sensitivityCreate your own SDF by subclassing the SDF base class:
from implicit_cad import SDF
class MyCustomSDF(SDF):
def distance(self, point):
x, y, z = point
# Your distance function here
return np.sqrt(x**2 + y**2 + z**2) - 1.0from implicit_cad import Sphere, Union, Translate
# Create multiple spheres
spheres = [Translate(Sphere(radius=0.5), (i, 0, 0))
for i in range(5)]
# Combine them
result = spheres[0]
for s in spheres[1:]:
result = Union(result, s)from implicit_cad import Sphere, SmoothUnion
s1 = Sphere(radius=1.0, center=(-0.5, 0, 0))
s2 = Sphere(radius=1.0, center=(0.5, 0, 0))
# Smooth union with blending factor k
blended = SmoothUnion(s1, s2, k=0.3) # Larger k = smoother blend- Use appropriate grid resolution: Higher resolution = better quality but slower
- For topology optimization, start with lower resolution and refine
- Use smooth operations sparingly as they're more expensive
- Consider using numpy vectorization for custom SDFs
numpy: Numerical computationsscipy: Scientific computing and optimizationscikit-image: Marching cubes algorithmtrimesh: Mesh manipulationmatplotlib: Visualization (optional)pyvista: 3D visualization (optional)
- Signed Distance Functions
- Level-Set Methods for Structural Topology Optimization
- Marching Cubes Algorithm
Transform 3D scans of physical spaces into optimized AMR (Autonomous Mobile Robot) design envelopes.
This pipeline takes volumetric reconstructions of factories, warehouses, and industrial spaces, computes the "negative space" (where robots can operate), and generates parametric design constraints for rapid robot chassis generation.
Physical Space → 3D Scan → TSDF Volume → ESDF (Negative Space) → Max Inscribed Volume → AMR Envelope
- Reconstruct - Build volumetric representation from RGB-D sensors or existing meshes
- Analyze - Compute Euclidean Signed Distance Field (ESDF) to find navigable space
- Optimize - Find maximum inscribed volume for robot sizing
- Generate - Output design parameters for parametric CAD (nTopology, etc.)
- Visualize - View results in Open3D or NVIDIA Isaac Sim
# Download the Redwood indoor reconstruction
python scripts/download_datasets.py --dataset redwood_indoor
# Or download all available datasets
python scripts/download_datasets.py --dataset all# Basic usage with a mesh file
python scripts/run_reconstruction.py data/raw/redwood_indoor/apartment.ply -o outputs/apartment
# With custom config
python scripts/run_reconstruction.py your_scan.ply -c configs/reconstruction.yaml -o outputs/
# Skip visualization (headless mode)
python scripts/run_reconstruction.py your_scan.ply --no-vizAfter running, you'll find in the output directory:
scan_to_amr_envelope.json- AMR design parametersscan_to_amr_environment.ply- Environment meshscan_to_amr_amr_preview.ply- Simple robot preview meshpathway_analysis.json- Corridor width analysissdf_grid.npz/esdf_grid.npz- Raw volumetric data
Edit configs/reconstruction.yaml:
# Grid settings
voxel_size: 0.02 # 2cm TSDF voxels
grid_resolution: 0.05 # 5cm analysis grid
# Robot constraints
min_robot_height: 0.3 # Minimum 30cm tall
aspect_ratio: null # [width, depth, height] or null for free
# Safety
safety_margin: 0.05 # 5cm wall clearanceFor NVIDIA ecosystem integration:
# After running the main pipeline, generate an Isaac Sim visualization script
python -c "
from scan_to_amr.visualization.omniverse_viz import generate_isaac_sim_script
import json
envelope = json.load(open('outputs/apartment/scan_to_amr_envelope.json'))
generate_isaac_sim_script(
'outputs/apartment/scan_to_amr_environment.ply',
envelope,
'outputs/apartment/run_isaac_sim.py'
)
"
# Run in Isaac Sim
~/.local/share/ov/pkg/isaac_sim-*/python.sh outputs/apartment/run_isaac_sim.pyImplicitsTest/
├── implicit_cad/ # SDF primitives, CSG, topology optimization
├── scan_to_amr/ # 3D reconstruction to AMR pipeline
│ ├── reconstruction/ # TSDF/mesh processing
│ ├── analysis/ # Negative space analysis, max inscribed
│ ├── visualization/ # Open3D and Isaac Sim viewers
│ └── generation/ # AMR envelope output
├── scripts/ # CLI tools for scan-to-amr
├── configs/ # Configuration files
├── examples/ # Implicit CAD examples
└── data/ # Datasets (gitignored)
This project is for educational purposes. Feel free to use and modify as needed.
This is an educational project. Suggestions and improvements are welcome!