deadsy / sdfx

A simple CAD package using signed distance functions
MIT License
543 stars 51 forks source link

Convert triangle meshes to SDF3 and voxel-based cache/smoothing #44

Closed Yeicor closed 2 years ago

Yeicor commented 2 years ago

This allows importing triangle-based closed-surface meshes, solving #14. It could be optimized by filtering triangles on evaluation, but it is good enough for low poly meshes.

The new VoxelSdf SDF3 can be used both as a cache for slow evaluations (large SDF3 hierarchies) and SDF3 smoothing with trilinear interpolation (if its meshCells < renderer's meshCells for that bounding box).

New example (monkey_hat)

Original model (a slightly modified Suzanne from Blender --- eyes were different surfaces): Screenshot_20220101_010431

Result (without VoxelSdf and larger meshCells): Screenshot_20220101_010435

Yeicor commented 2 years ago

After the latest optimizations it can now render a detailed mesh (of 972688 triangles) in a 129x96x106 cell grid in only 16 seconds, when previously this would take forever.

Screenshot_20220108_192927

Note that this is the best case escenario in which the closest triangle's bounding box is enough to reconstruct the surface. Usually, more triangles are needed (for example 20 out of 366 triangles for the previous monkey model). I used an RTree built on load to quickly extract the nearest neighbors for each Evaluate() call.

soypat commented 2 years ago

Late to the party but hey, I'll drop this here in case it interest's yall: https://github.com/soypat/sdf/blob/main/helpers/sdfexp/import.go. image

Left: original 128 cell on longest side model render using SDFs. 140k triangles

Right: Imported triangles using ImportModel (from left model) and rendering using 128 cells on longest side. The resulting amount of triangles is near identical and model quality is preserved. 6.5s elapsed.