Is there a library for Javascript distance functions?

No, it is a special approach and is based on E. Hartmann’s triangulation algorithm, which he implemented in Pascal over 20 years ago.


(partly German)
Implizite Fläche – Wikipedia
https://www2.mathematik.tu-darmstadt.de/~ehartmann/cdgen0104.pdf
https://www2.mathematik.tu-darmstadt.de/~ehartmann/cdg0/cdg0n.pdf


I have already used this algorithm in a modified form several times.
See
GitHub - hofk/THREEi.js: three.js addon for triangulation of implicit surfaces and for forms with holes. The addon generates indexed BufferGeometries.

Single-branched geometry organically shaped in the web editor

Specific changes must be made for each application. The original algorithm requires an implicit function whose partial derivatives must be given, e.g.


// Example: implicit surface genus2:

const isf = ( x, y, z ) => ( 2*y*( y*y - 3*x*x )*( 1 - z*z ) + ( x*x + y*y )*( x*x + y*y ) - ( 9*z*z - 1 )*( 1 - z*z ) );// IMPLICIT SURFACE Function
const dx = ( x, y, z ) => ( -12*x*y*( 1 - z*z ) + 4*x*( x*x + y*y ) );// PARTIAL DERIVATE to x
const dy = ( x, y, z ) => ( 6*( y*y - x*x )*( 1 - z*z ) + 4*y*( x*x + y*y ) );// PARTIAL DERIVATE to y
const dz = ( x, y, z ) => ( -4*y*( y*y - 3*x*x )*z + 36*z*z*z - 20*z );// PARTIAL DERIVATE to z

I still have open problems with the adjustment to SDFs.
For edges with external angles of 270° or more, the connection to the surface is no longer clear.
Such angles also occur during subtraction and there are no clear tangents at the sharp cutting edges.

I still have to come up with something.
I hope that these problems can be solved somehow. :thinking: