# Which package/function for signed distance fields?

**URL:** https://discourse.julialang.org/t/which-package-function-for-signed-distance-fields/43487
**Category:** General Usage
**Tags:** question, package, graphics
**Created:** [July 22, 2020, 12:30pm UTC](https://discourse.julialang.org/t/which-package-function-for-signed-distance-fields/43487 "2020-07-22T12:30:34Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)
#### Post date: [July 22, 2020, 12:30pm UTC](https://discourse.julialang.org/t/which-package-function-for-signed-distance-fields/43487/1 "2020-07-22T12:30:34Z")

</div>

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](https://github.com/JuliaGeometry/GeometryBasics.jl) imports the `SignedDistanceField` type from [GeometryTypes](https://github.com/JuliaGeometry/GeometryTypes.jl). but it’s discontinued in favor of GeometryBasics??? [Meshing](https://github.com/JuliaGeometry/Meshing.jl) and [Descartes](https://github.com/sjkelly/Descartes.jl) 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:

```julia
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](https://github.com/freemin7/SignedDistanceFields.jl/blob/master/src/SignedDistanceFields.jl) 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?

---

<div class="post-metadata">

### Author: ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)
#### Post date: [July 22, 2020, 9:56pm UTC](https://discourse.julialang.org/t/which-package-function-for-signed-distance-fields/43487/2 "2020-07-22T21:56:45Z")

</div>

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://unitylist.com/p/k6u/Mesh-To-SDF)  
[https://github.com/xraxra/SDFr](https://github.com/xraxra/SDFr)  
[https://github.com/christopherbatty/SDFGen](https://github.com/christopherbatty/SDFGen)  
[https://github.com/marian42/mesh\_to\_sdf](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? 😅

---

<div class="post-metadata">

### Author: ![sjkelly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sjkelly/32/9280_2.png) [@sjkelly](https://discourse.julialang.org/u/sjkelly)
#### Post date: [October 8, 2020, 5:35pm UTC](https://discourse.julialang.org/t/which-package-function-for-signed-distance-fields/43487/3 "2020-10-08T17:35:25Z")

</div>

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](https://github.com/JuliaRobotics/EnhancedGJK.jl#meshes)

---

<div class="post-metadata">

### Author: ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)
#### Post date: [October 10, 2020, 9:44am UTC](https://discourse.julialang.org/t/which-package-function-for-signed-distance-fields/43487/4 "2020-10-10T09:44:02Z")

</div>

Thanks, I’ll look into this one.
