A high-performance C++ library for converting between triangle meshes and Signed Distance Fields (SDFs) using adaptive octree-based sampling and parallel processing.
The SDF Converter provides efficient tools for:
- Converting triangle meshes (STL files) to Signed Distance Fields
- Converting SDFs back to triangle meshes
- Adaptive sampling using octree spatial subdivision
- Parallel processing with OpenMP
- Mesh smoothing and optimization
- Boundary preservation for surface meshes
- Eigen (Linear algebra)
- VTK (Visualization Toolkit)
- OpenVDB
- TBB (Threading Building Blocks)
- nanoflann (Fast KD-tree implementation)
- OpenMP (Parallel processing)
- HWLOC (Optional - Hardware locality)
- vcpkg (Package manager)
- Install vcpkg
- Set up vcpkg:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.bat # Windows
./bootstrap-vcpkg.sh # Linux/macOS- Install required packages via vcpkg:
vcpkg install vtk:x64-windows # Windows
vcpkg install openvdb:x64-windows
vcpkg install eigen3:x64-windows
vcpkg install tbb:x64-windowsmkdir build
cd build
cmake -DCMAKE_TOOLCHAIN_FILE=[path_to_vcpkg]/scripts/buildsystems/vcpkg.cmake ..
cmake --build .For Windows users, the default vcpkg path is typically:
cmake -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake .../sdf_converter <input_stl> <output_stl> [options]--upsample-factor <n>: Grid upsampling factor (default: 1)--min-voxel-size <n>: Minimum voxel size (default: 0.051)--max-voxel-size <n>: Maximum voxel size (default: 4.50)--adaptivity-factor <n>: Controls grid refinement (default: 0.8)--num-threads <n>: Number of parallel threads (default: 10)--max-depth <n>: Maximum octree depth (default: 18)--surface-band <n>: Surface influence range (default: 2.0)
#include "SDFConverter.h"
// Initialize converter with custom parameters
sdf::SDFConverter::Parameters params;
params.minVoxelSize = 0.05;
params.maxVoxelSize = 4.5;
params.adaptivityFactor = 0.8;
params.numThreads = 10;
sdf::SDFConverter converter(params);
// Convert mesh to SDF
auto sdfImage = converter.polyDataToSDF(inputMesh);
// Convert SDF back to mesh
auto outputMesh = converter.sdfToPolyData(sdfImage);The converter uses an octree-based adaptive sampling system that:
- Concentrates samples near surface features
- Reduces sample density in smooth regions
- Maintains smooth transitions between different resolution levels
- Automatically adjusts to local geometric complexity
Signed distances are computed using:
- Fast KD-tree nearest neighbor searches
- Weighted normal-based sign determination
- Smooth distance field interpolation
- Thread-safe distance caching
The output mesh generation includes:
- Marching Cubes implementation
- Advanced smoothing filters
- Normal computation and optimization
- Boundary preservation
- Connected component analysis
- Use appropriate
min-voxel-sizeandmax-voxel-sizefor your geometry - Adjust
adaptivity-factorto balance accuracy and performance - Set
num-threadsbased on available CPU cores - Consider enabling distance caching for repeated conversions
- Monitor memory usage with large meshes or small voxel sizes
- Large meshes may require significant memory
- Very small features might not be captured at default resolution
- Surface offset feature is experimental and may cause instability
- Boundary preservation only works with surface meshes
Contributions are welcome! But it's better to fork this and do your own thing. This project is not aimed at being production ready, but if you think that this may be useful for your application, feel free to use the code.
See the LICENSE file in the repo. tl;dr; use however you see fit, the code is provided under the MIT License, which is extremely permissive and allows for commercial use, modification, and redistribution.
- VTK for visualization and mesh processing functionality
- nanoflann for efficient KD-tree implementation
- Eigen for linear algebra operations
- TBB for concurrent memory allocation
- OpenMP for parallel processing support