Skip to main content

Design fields to fabrication-ready geometry

Project description

XelToFab

PyPI Python 3.13+ License: MIT

A design field post-processing pipeline that transforms continuous scalar fields from optimization solvers and neural networks into clean, fabrication-ready triangle meshes (3D) and contour representations (2D). The pipeline handles thresholding, smoothing, mesh extraction, quality improvement, visualization, and multi-format export.

Installation

# From PyPI (stable)
pip install xeltofab

# With optional format support
pip install "xeltofab[vtk,hdf5]"

# From source (development)
git clone https://github.com/xarthurx/XelToFab.git
cd XelToFab
uv sync

Quick Start

# Process a 3D density field into an STL mesh
xtf process density.npy -o output.stl

# Process with custom parameters and save a comparison plot
xtf process density.npy -o output.stl --threshold 0.4 --sigma 1.5 --viz

# Visualize a 2D density field
xtf viz density_2d.npy -o comparison.png

2D → 3D extrusion

# Turn a 2D density field into a print-ready STL
xtf extrude density_2d.npy -o part.stl --thickness 10

# Printability cleanup: drop small islands + pre-smooth
xtf extrude beam.npy -o beam.stl -t 15 --min-component-area 20 --smooth-sigma 0.8

From Python:

import numpy as np
import xeltofab as xtf

field = np.load("density_2d.npy")
mesh = xtf.extrude_2d(field, thickness=10)
mesh.export("part.stl")

Pipeline

Scalar Field → Preprocess → Extract → Smooth → Mesh/Contours
     (.npy)      threshold     marching    Taubin     (.stl/.obj)
                 + morphology  cubes/sq.   filter
  1. Preprocess — Gaussian smoothing, Heaviside thresholding, morphological cleanup, small component removal
  2. Extract — Marching cubes (3D) or marching squares (2D) via scikit-image
  3. Smooth — Taubin smoothing via trimesh (3D meshes only)

Python API

import numpy as np
from xeltofab.state import PipelineState, PipelineParams
from xeltofab.pipeline import process
from xeltofab.io import save_mesh

# Create a scalar field (e.g., a sphere)
z, y, x = np.mgrid[-1:1:50j, -1:1:50j, -1:1:50j]
field = (x**2 + y**2 + z**2 < 0.5**2).astype(float)

# Run the pipeline
params = PipelineParams(threshold=0.5, smooth_sigma=1.0)
state = PipelineState(field=field, params=params)
result = process(state)

# Export
save_mesh(result, "sphere.stl")

Converting SDF to density

If you have an SDF array and want to feed it into the density-mode pipeline (for example to share a density field with a downstream library such as EngiBench), use sdf_to_density:

import numpy as np
from xeltofab import sdf_to_density, process, PipelineParams, PipelineState

# sdf_grid: ndarray, negative inside the shape, positive outside
density = sdf_to_density(sdf_grid, method="linear", bandwidth=1.0)

# direct_extraction=True skips the density preprocessor (Gaussian smooth +
# threshold + morphology) so the iso=0.5 surface of the converted field is
# preserved exactly. Drop it to run the full density pipeline instead.
params = PipelineParams(
    field_type="density",
    direct_extraction=True,
    extraction_level=0.5,
)
result = process(PipelineState(field=density, params=params))

Three methods are available:

Method Use when you want
"heaviside" Binary density (no sub-voxel information).
"linear" (default) Compact-support linear ramp — matches MC-at-0.5 behavior.
"sigmoid" Smooth, differentiable profile — for gradient-based optimizers.

bandwidth controls the transition width in the same units as the SDF (voxel units when the SDF came from scipy.ndimage.distance_transform_edt, world units when it came from an analytic SDF on a world-space grid).

Using Example Data

Pre-computed topology optimization results are included in data/examples/ (sourced from IDEALLab EngiBench):

from xeltofab.io import load_field
from xeltofab.pipeline import process

state = load_field("data/examples/beams_2d_50x100_sample0.npy")
result = process(state)

Supported Input Formats

Format Extensions Install
NumPy .npy, .npz Built-in
MATLAB .mat Built-in (via scipy)
CSV/Text .csv, .txt Built-in
VTK .vtk, .vtr, .vti pip install "xeltofab[vtk]"
HDF5/XDMF .h5, .hdf5, .xdmf pip install "xeltofab[hdf5]"
All formats pip install "xeltofab[all-formats]"

List available formats: xtf formats

Development

uv sync                          # Install deps
uv run pytest tests/ -v          # Run tests
uv run ruff check src/ tests/    # Lint
uv run ruff format src/ tests/   # Format
uv run marimo edit notebooks/demo.py  # Interactive demo

Project Structure

src/xeltofab/
├── state.py        Pipeline state + parameter models
├── preprocess.py   Field preprocessing
├── extract.py      Mesh/contour extraction
├── smooth.py       Taubin smoothing
├── pipeline.py     Orchestrator
├── io.py           File I/O (multi-format load, mesh export)
├── loaders/        Format-specific loaders (numpy, matlab, csv, vtk, hdf5)
├── field_plots.py  Matplotlib visualization
└── cli.py          CLI (xtf)

See docs/ARCHITECTURE.md for detailed architecture documentation.

Documentation

Full documentation is available at xeltofab.ethz.ch.

Requirements

  • Python 3.13+
  • uv for development

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xeltofab-0.3.6.tar.gz (33.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xeltofab-0.3.6-py3-none-any.whl (49.5 kB view details)

Uploaded Python 3

File details

Details for the file xeltofab-0.3.6.tar.gz.

File metadata

  • Download URL: xeltofab-0.3.6.tar.gz
  • Upload date:
  • Size: 33.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for xeltofab-0.3.6.tar.gz
Algorithm Hash digest
SHA256 c09c03792fb4dd16f0a5b1fc6a6b06ea616659edd3e5c9d727863054c474a895
MD5 0c46e53bafe57e97d60cb43efd0ce9ad
BLAKE2b-256 b76365ad54997baa46e2d181c815aaf0e95d9e01a498987862902f4e74a22e78

See more details on using hashes here.

Provenance

The following attestation bundles were made for xeltofab-0.3.6.tar.gz:

Publisher: publish-pypi.yml on xarthurx/XelToFab

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file xeltofab-0.3.6-py3-none-any.whl.

File metadata

  • Download URL: xeltofab-0.3.6-py3-none-any.whl
  • Upload date:
  • Size: 49.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for xeltofab-0.3.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d6b3b20d49028c0f52d54c732e0201d36f265a243b6599cb185db878699471c9
MD5 ff641912861b952f97f7ed277bf27609
BLAKE2b-256 93d9c7f6ab42b28a315b5517a9dde77c7126b5cada03892728ee27376e29215c

See more details on using hashes here.

Provenance

The following attestation bundles were made for xeltofab-0.3.6-py3-none-any.whl:

Publisher: publish-pypi.yml on xarthurx/XelToFab

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page