Which package/function for signed distance fields?

I need a signed distance field (SDF) and corresponding unit normal vector field (acronym unknown) to immerse solid boundaries into my fluid simulator. I’m trying to use “other people’s code” wherever possible on this project, and it seems like JuliaGeometry and JuliaGraphics are the best bet.

It looks like GeometryBasics imports the SignedDistanceField type from GeometryTypes. but it’s discontinued in favor of GeometryBasics??? Meshing and Descartes both use this type, so I guess it is safe, but I wanted to check.

Looking through all of these and various other attempts at Google haven’t uncovered a function for computing the SDF and normal vector field from a general geometry description, or even from a mesh. For example, something which lets both of these work:

x = # position field in x,y or x,y,z where the SDF should be measured
using MakieGallery
dis2 = SDF(MakieGallery.loadasset("cat.obj"),x)
using GeometryBasics
dis1 = SDF(Sphere(center,radius),x) # could be native or create a mesh first

I would’ve thought this would be pretty normal in a graphics setting. Meshing.jl solves the opposite problem (getting surfaces from SDFs) and SignedDistanceFields seems to be standalone and computes it’s own SDF explicitly, but only for a few analytic shapes. Is there a place where this is done efficiently for meshes?

Looked around the internet a little more broadly and found packages to do this in other languages. Here are a few:
https://unitylist.com/p/k6u/Mesh-To-SDF
https://github.com/xraxra/SDFr
https://github.com/christopherbatty/SDFGen
https://github.com/marian42/mesh_to_sdf

Do Makie or any of the other graphics packages have a similar feature? Does anyone else out there need this, or am I barking into the dark? :sweat_smile:

1 Like

I have tried Enhanced GJK for this in the past with decent results. As described, it isn’t 100% correct for non-convex meshes, but it worked decent for feeding into DistMesh.jl as a basic test.
https://github.com/JuliaRobotics/EnhancedGJK.jl#meshes

Thanks, I’ll look into this one.