This guide explains the refactored structure of the procedural design project.
The project has been refactored from notebook-only code into a professional, modular structure with:
- Reusable algorithm modules (
src/algorithms/) - Geometry utilities (
src/geometry/) - End-to-end pipelines (
src/pipelines/) - CLI tools for batch processing (
src/cli/) - Interactive Streamlit app (
src/app/) - Comprehensive tests (
tests/)
procedural-design/
├── src/
│ ├── algorithms/ # Core generative algorithms
│ │ ├── gray_scott.py # Reaction-diffusion simulator
│ │ └── space_colonization.py # Branch growth algorithm
│ ├── geometry/ # Mesh operations
│ │ ├── primitives.py # Basic shapes
│ │ ├── mesh_operations.py # Mesh processing
│ │ └── tube_sweep.py # Tube sweeping
│ ├── pipelines/ # End-to-end workflows
│ │ ├── vase.py # Vase generation pipeline
│ │ └── moss_pole.py # Moss pole pipeline
│ ├── cli/ # Command-line tools
│ │ ├── generate_vase.py
│ │ └── generate_moss_pole.py
│ └── app/ # Web applications
│ └── streamlit_app.py
├── tests/ # Comprehensive test suite
│ ├── test_gray_scott.py
│ ├── test_space_colonization.py
│ └── test_pipelines.py
├── notebooks/ # Jupyter notebooks for experimentation
│ ├── 01_refactored_example.ipynb
│ └── experiments/
└── outputs/ # Generated meshes and renders
└── meshes/stl/
- Algorithms (
src/algorithms/): Pure algorithm implementations - Geometry (
src/geometry/): Mesh generation and operations - Pipelines (
src/pipelines/): Combine algorithms + geometry - Interfaces (
src/cli/,src/app/): User-facing tools
All pipelines use dataclasses for configuration:
from pipelines.vase import VaseConfig, VasePipeline
config = VaseConfig(
height=150.0,
pattern_type='spots'
)
pipeline = VasePipeline(config)Algorithms support both CPU and GPU:
# CPU
sim = GrayScottSimulator(use_gpu=False)
# GPU (requires Taichi)
sim = GrayScottSimulator(use_gpu=True)All components have comprehensive unit tests:
pytest tests/ -vBefore (in notebook):
def gray_scott_multi(size=256, steps=20000, ...):
U = np.ones((size, size))
V = np.zeros((size, size))
# ... implementation
return V
field = gray_scott_multi(size=256, steps=10000)After (using module):
from algorithms.gray_scott import GrayScottSimulator, GrayScottConfig
config = GrayScottConfig(pattern_type='spots')
sim = GrayScottSimulator(resolution=256, config=config)
sim.initialize_random(n_seeds=5)
field = sim.run(steps=10000)Before:
# 100 lines of mesh generation code in notebook
vertices = []
faces = []
for i in range(h):
# Complex mesh building logic...
mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
mesh.export('output.stl')After:
from pipelines.vase import VasePipeline, VaseConfig
config = VaseConfig(height=150.0)
pipeline = VasePipeline(config)
mesh = pipeline.generate()
pipeline.export('output.stl')Use notebooks for parameter exploration:
# notebooks/experiments/my_experiment.ipynb
from pipelines.vase import VasePipeline, VaseConfig
# Quick iteration
for feed_rate in [0.03, 0.04, 0.05]:
config = VaseConfig(pattern_type='custom')
config.feed_rate = feed_rate
pipeline = VasePipeline(config)
mesh = pipeline.generate()
pipeline.export(f'vase_f{feed_rate}.stl')Use CLI for batch processing:
# Generate multiple variants
for seed in {1..10}; do
python src/cli/generate_vase.py \
--pattern spots \
--random-seed $seed \
--output "outputs/vase_$seed.stl"
doneUse Streamlit for interactive parameter tuning:
streamlit run src/app/streamlit_app.pyUse modules directly in Python scripts:
# generate_batch.py
from pipelines.vase import VasePipeline, VaseConfig
patterns = ['spots', 'stripes', 'waves']
for pattern in patterns:
config = VaseConfig(pattern_type=pattern)
pipeline = VasePipeline(config)
mesh = pipeline.generate()
pipeline.export(f'vase_{pattern}.stl')cd procedural-design
pytest tests/ -v# Algorithm tests
pytest tests/test_gray_scott.py -v
# Pipeline tests
pytest tests/test_pipelines.py -v
# Taichi GPU tests only
pytest tests/test_gray_scott.py::TestTaichiKernels -vIf Taichi isn't installed:
pytest tests/ -v -m "not taichi"- Import from
src/modules - Keep notebooks under 100 lines
- Use notebooks for experimentation only
- Extract stable code to
src/
- Clear notebook outputs before committing
- Use
.gitignorefor generated meshes - Keep parameter files in
data/parameters/
- Add docstrings to all functions
- Use type hints
- Include examples in docstrings
- Update README when adding features
- Profile before optimising
- Use GPU for large simulations
- Consider batch processing for multiple variants
- Cache expensive computations
- Create file in
src/algorithms/ - Implement config dataclass
- Implement algorithm class
- Add tests in
tests/ - Create pipeline in
src/pipelines/
Example:
# src/algorithms/turing_patterns.py
from dataclasses import dataclass
@dataclass
class TuringConfig:
"""Configuration for Turing pattern."""
param1: float = 1.0
class TuringSimulator:
"""Turing pattern simulator."""
def __init__(self, config: TuringConfig):
self.config = config
def run(self, steps: int):
# Implementation
pass- Create file in
src/pipelines/ - Define config with all parameters
- Implement generation methods
- Add validation and export
- Create CLI tool in
src/cli/
- Add new page function to
src/app/streamlit_app.py - Add sidebar controls
- Add generation logic
- Add visualization and export
Make sure src/ is in your Python path:
import sys
from pathlib import Path
sys.path.insert(0, str(Path.cwd().parent / 'src'))If GPU acceleration fails:
# Fall back to CPU
sim = GrayScottSimulator(use_gpu=False)Boolean operations can be unstable:
# Try simplifying meshes first
mesh = mesh.simplify_quadric_decimation(1000)
# Or use alternative library
import pymeshlab
# ... use MeshLab booleans