Signed distance function explained

In mathematics and its applications, the signed distance function or signed distance field (SDF) is the orthogonal distance of a given point x to the boundary of a set Ω in a metric space (such as the surface of a geometric shape), with the sign determined by whether or not x is in the interior of Ω. The function has positive values at points x inside Ω, it decreases in value as x approaches the boundary of Ω where the signed distance function is zero, and it takes negative values outside of Ω.[1] However, the alternative convention is also sometimes taken instead (i.e., negative inside Ω and positive outside).[2] The concept also sometimes goes by the name oriented distance function/field.

Definition

Let be a subset of a metric space with metric, and

\partial\Omega

be its boundary. The distance between a point of and the subset

\partial\Omega

of is defined as usual as

d(x,\partial\Omega)=infyd(x,y),

where

inf

denotes the infimum. The signed distance function from a point of to

\Omega

is defined by

f(x)=\begin{cases} d(x,\partial\Omega)&ifx\in\Omega\\ -d(x,\partial\Omega)&ifx\notin\Omega. \end{cases}

Properties in Euclidean space

If Ω is a subset of the Euclidean space Rn with piecewise smooth boundary, then the signed distance function is differentiable almost everywhere, and its gradient satisfies the eikonal equation

|\nablaf|=1.

If the boundary of Ω is Ck for k ≥ 2 (see Differentiability classes) then d is Ck on points sufficiently close to the boundary of Ω. In particular, on the boundary f satisfies

\nablaf(x)=N(x),

where N is the inward normal vector field. The signed distance function is thus a differentiable extension of the normal vector field. In particular, the Hessian of the signed distance function on the boundary of Ω gives the Weingarten map.

If, further, Γ is a region sufficiently close to the boundary of Ω that f is twice continuously differentiable on it, then there is an explicit formula involving the Weingarten map Wx for the Jacobian of changing variables in terms of the signed distance function and nearest boundary point. Specifically, if T(Ω, μ) is the set of points within distance μ of the boundary of Ω (i.e. the tubular neighbourhood of radius μ), and g is an absolutely integrable function on Γ, then

\intT(\partial\Omega,\mu)g(x)dx=\int\partial\Omega

\mu
\int
-\mu

g(uN(u))\det(IWu)dλdSu,

where denotes the determinant and dSu indicates that we are taking the surface integral.

Algorithms

Algorithms for calculating the signed distance function include the efficient fast marching method, fast sweeping method[3] and the more general level-set method.

For voxel rendering, a fast algorithm for calculating the SDF in taxicab geometry uses summed-area tables.[4]

Applications

Signed distance functions are applied, for example, in real-time rendering,[5] for instance the method of SDF ray marching, and computer vision.[6] [7]

SDF has been used to describe object geometry in real-time rendering, usually in a raymarching context, starting in the mid 2000s. By 2007, Valve is using SDFs to render large pixel-size (or high DPI) smooth fonts with GPU acceleration in its games.[8] Valve's method is not perfect as it runs in raster space in order to avoid the computational complexity of solving the problem in the (continuous) vector space. The rendered text often loses sharp corners. In 2014, an improved method was presented by Behdad Esfahbod. Behdad's GLyphy approximates the font's Bézier curves with arc splines, accelerated by grid-based discretization techniques (which culls too-far-away points) to run in real time.[9]

A modified version of SDF was introduced as a loss function to minimise the error in interpenetration of pixels while rendering multiple objects.[10] In particular, for any pixel that does not belong to an object, if it lies outside the object in rendition, no penalty is imposed; if it does, a positive value proportional to its distance inside the object is imposed.

f(x)=\begin{cases} 0&ifx\in\Omegac\\ d(x,\partial\Omega)&ifx\in\Omega \end{cases}

In 2020, the FOSS game engine Godot 4.0 received SDF-based real-time global illumination (SDFGI), that became a compromise between more realistic voxel-based GI and baked GI. Its core advantage is that it can be applied to infinite space, which allows developers to use it for open-world games.[11]

In 2023, a "GPUI" UI framework was released to draw all UI elements using the GPU, many parts using SDF. The author claims to have produced a Zed code editor that renders at 120 fps. The work makes use of Inigo Quilez's list of geometric primitives in SDF, Evan Wallace (co-founder of Figma)'s approximated gaussian blur in SDF, and a new rounded rectangle SDF.[12]

See also

References

Notes and References

  1. Level set based shape prior segmentation . Chan, T. . Zhu, W. . 2005 . IEEE Computer Society Conference on Computer Vision and Pattern Recognition . 10.1109/CVPR.2005.212 .
  2. Shape modeling with front propagation: a level set approach . Malladi, R. . Sethian, J.A. . Vemuri, B.C. . IEEE Transactions on Pattern Analysis and Machine Intelligence . 17 . 2 . 158–175 . 1995 . 10.1109/34.368173 . 10.1.1.33.2443. 9505101 .
  3. [Zhao Hongkai]
  4. Web site: Nilsson . Tobias . 2019 . Optimization Methods for Direct Volume Rendering on the Client Side Web . 2022-07-08 . Digitala Vetenskapliga Arkivet.
  5. Book: Tomas Akenine-Möller. Eric Haines. Naty Hoffman. Real-Time Rendering, Fourth Edition. 6 August 2018. CRC Press. 978-1-351-81615-1.
  6. Book: Perera. S.. Barnes. N.. He. X.. Izadi. S.. Kohli. P.. Glocker. B.. 2015 IEEE Winter Conference on Applications of Computer Vision . Motion Segmentation of Truncated Signed Distance Function Based Volumetric Surfaces . January 2015. 1046–1053. 10.1109/WACV.2015.144. 978-1-4799-6683-7. 16811314.
  7. Book: Izadi. Shahram. Kim. David. Hilliges. Otmar. Molyneaux. David. Newcombe. Richard. Kohli. Pushmeet. Shotton. Jamie. Hodges. Steve. Freeman. Dustin. Proceedings of the 24th annual ACM symposium on User interface software and technology . KinectFusion . 2011. UIST '11. New York, NY, USA. ACM. 559–568. 10.1145/2047196.2047270. 9781450307161. 3345516.
  8. Book: Green, Chris . 10.1145/1281500.1281665 . 9–18 . 2007 . ACM SIGGRAPH 2007 courses . Improved alpha-tested magnification for vector textures and special effects . 9781450318235 . 10.1.1.170.9418 . 7479538 .
  9. Behdad Esfahbod . live. https://ghostarchive.org/varchive/youtube/20211211/7tHv6mcIIeo. 2021-12-11. GLyphy: high-quality glyph rendering using OpenGL ES2 shaders [linux.conf.au 2014] ]. YouTube. Source Code
  10. Jiang. Wen. Kolotouros. Nikos. Pavlakos. Georgios. Zhou. Xiaowei. Daniilidis. Kostas. 2020-06-15. Coherent Reconstruction of Multiple Humans from a Single Image. cs.CV. 2006.08586.
  11. Web site: Engine . Godot . Godot 4.0 gets SDF based real-time global illumination . Godot Engine . en.
  12. Web site: Scandurra . Antonio . Leveraging Rust and the GPU to render user interfaces at 120 FPS - Zed Blog . Zed . 7 March 2023.